use of com.hedera.mirror.common.domain.token.NftId in project hedera-mirror-node by hashgraph.
the class EntityRecordItemListenerCryptoTest method customizeNftAllowances.
private List<NftAllowance> customizeNftAllowances(Timestamp consensusTimestamp, List<Nft> expectedNfts) {
var delegatingSpender = recordItemBuilder.accountId();
var owner = recordItemBuilder.accountId();
var spender1 = recordItemBuilder.accountId();
var spender2 = recordItemBuilder.accountId();
var tokenId = recordItemBuilder.tokenId();
var nft1 = Nft.builder().id(new NftId(1L, EntityId.of(tokenId))).accountId(EntityId.of(owner)).createdTimestamp(101L).modifiedTimestamp(101L).build();
var nft2 = Nft.builder().id(new NftId(2L, EntityId.of(tokenId))).accountId(EntityId.of(owner)).createdTimestamp(102L).modifiedTimestamp(102L).build();
var nft3 = Nft.builder().id(new NftId(3L, EntityId.of(tokenId))).accountId(EntityId.of(owner)).createdTimestamp(103L).modifiedTimestamp(103L).build();
var timestamp = DomainUtils.timeStampInNanos(consensusTimestamp);
List<NftAllowance> nftAllowances = new LinkedList<>();
nftAllowances.add(NftAllowance.newBuilder().setDelegatingSpender(delegatingSpender).setOwner(owner).addSerialNumbers(1L).addSerialNumbers(2L).setSpender(spender1).setTokenId(tokenId).build());
expectedNfts.add(nft1.toBuilder().delegatingSpender(EntityId.of(delegatingSpender)).modifiedTimestamp(timestamp).spender(EntityId.of(spender1)).build());
nftAllowances.add(NftAllowance.newBuilder().setApprovedForAll(BoolValue.of(false)).setOwner(recordItemBuilder.accountId()).setSpender(recordItemBuilder.accountId()).setTokenId(recordItemBuilder.tokenId()).build());
nftAllowances.add(NftAllowance.newBuilder().setApprovedForAll(BoolValue.of(true)).setOwner(recordItemBuilder.accountId()).setSpender(recordItemBuilder.accountId()).setTokenId(recordItemBuilder.tokenId()).build());
nftAllowances.add(NftAllowance.newBuilder().setApprovedForAll(BoolValue.of(true)).setOwner(owner).addSerialNumbers(2L).addSerialNumbers(3L).setSpender(spender2).setTokenId(tokenId).build());
// duplicate nft allowance
nftAllowances.add(nftAllowances.get(nftAllowances.size() - 1));
// serial number 2's allowance is granted twice, the allowance should be granted to spender2 since it appears
// after the nft allowance to spender1
expectedNfts.add(nft2.toBuilder().modifiedTimestamp(timestamp).spender(EntityId.of(spender2)).build());
expectedNfts.add(nft3.toBuilder().modifiedTimestamp(timestamp).spender(EntityId.of(spender2)).build());
nftRepository.saveAll(List.of(nft1, nft2, nft3));
return nftAllowances;
}
use of com.hedera.mirror.common.domain.token.NftId in project hedera-mirror-node by hashgraph.
the class EntityRecordItemListenerCryptoTest method cryptoDeleteAllowance.
@Test
void cryptoDeleteAllowance() {
// given
var delegatingSpender = EntityId.of(recordItemBuilder.accountId());
var ownerAccountId = EntityId.of(recordItemBuilder.accountId());
var spender1 = EntityId.of(recordItemBuilder.accountId());
var spender2 = EntityId.of(recordItemBuilder.accountId());
var tokenId1 = EntityId.of(recordItemBuilder.tokenId());
var tokenId2 = EntityId.of(recordItemBuilder.tokenId());
List<NftRemoveAllowance> nftRemoveAllowances = List.of(NftRemoveAllowance.newBuilder().setOwner(AccountID.newBuilder().setAccountNum(ownerAccountId.getEntityNum())).setTokenId(TokenID.newBuilder().setTokenNum(tokenId1.getEntityNum())).addSerialNumbers(1L).addSerialNumbers(2L).build(), NftRemoveAllowance.newBuilder().setOwner(AccountID.newBuilder().setAccountNum(ownerAccountId.getEntityNum())).setTokenId(TokenID.newBuilder().setTokenNum(tokenId2.getEntityNum())).addSerialNumbers(1L).addSerialNumbers(2L).addSerialNumbers(2L).build());
RecordItem recordItem = recordItemBuilder.cryptoDeleteAllowance().transactionBody(b -> b.clearNftAllowances().addAllNftAllowances(nftRemoveAllowances)).build();
var nft1 = Nft.builder().accountId(ownerAccountId).createdTimestamp(10L).deleted(false).id(new NftId(1L, tokenId1)).modifiedTimestamp(recordItem.getConsensusTimestamp()).build();
var nft2 = Nft.builder().accountId(ownerAccountId).createdTimestamp(11L).deleted(false).id(new NftId(2L, tokenId1)).modifiedTimestamp(recordItem.getConsensusTimestamp()).build();
var nft3 = Nft.builder().accountId(ownerAccountId).createdTimestamp(12L).deleted(false).id(new NftId(1L, tokenId2)).modifiedTimestamp(recordItem.getConsensusTimestamp()).build();
var nft4 = Nft.builder().accountId(ownerAccountId).createdTimestamp(13L).deleted(false).id(new NftId(2L, tokenId2)).modifiedTimestamp(recordItem.getConsensusTimestamp()).build();
List<Nft> nftsWithAllowance = Stream.of(nft1.toBuilder().delegatingSpender(delegatingSpender).modifiedTimestamp(15L).spender(spender1), nft2.toBuilder().modifiedTimestamp(16L).spender(spender2), nft3.toBuilder().modifiedTimestamp(17L).spender(spender1), nft4.toBuilder().modifiedTimestamp(18L).spender(spender2)).map(Nft.NftBuilder::build).collect(Collectors.toList());
nftRepository.saveAll(nftsWithAllowance);
// when
parseRecordItemAndCommit(recordItem);
// then
assertAll(() -> assertEquals(0, entityRepository.count()), () -> assertTransactionAndRecord(recordItem.getTransactionBody(), recordItem.getRecord()), () -> assertThat(nftRepository.findAll()).containsExactlyInAnyOrder(nft1, nft2, nft3, nft4));
}
use of com.hedera.mirror.common.domain.token.NftId in project hedera-mirror-node by hashgraph.
the class SqlEntityListenerTest method getNft.
private Nft getNft(EntityId tokenId, long serialNumber, EntityId accountId, Long createdTimestamp, Boolean deleted, String metadata, long modifiedTimestamp) {
Nft nft = new Nft();
nft.setAccountId(accountId);
nft.setCreatedTimestamp(createdTimestamp);
nft.setDeleted(deleted);
nft.setMetadata(metadata == null ? null : metadata.getBytes(StandardCharsets.UTF_8));
nft.setId(new NftId(serialNumber, tokenId));
nft.setModifiedTimestamp(modifiedTimestamp);
return nft;
}
use of com.hedera.mirror.common.domain.token.NftId in project hedera-mirror-node by hashgraph.
the class EntityRecordItemListenerTokenTest method nftTransfer.
@Test
void nftTransfer() {
createAndAssociateToken(TOKEN_ID, NON_FUNGIBLE_UNIQUE, SYMBOL, CREATE_TIMESTAMP, ASSOCIATE_TIMESTAMP, PAYER2, false, false, false, 0);
long mintTimestamp1 = 20L;
TokenTransferList mintTransfer1 = nftTransfer(TOKEN_ID, RECEIVER, DEFAULT_ACCOUNT_ID, List.of(SERIAL_NUMBER_1));
Transaction mintTransaction1 = tokenSupplyTransaction(TOKEN_ID, NON_FUNGIBLE_UNIQUE, true, 0, List.of(SERIAL_NUMBER_1));
insertAndParseTransaction(mintTimestamp1, mintTransaction1, builder -> {
builder.getReceiptBuilder().setNewTotalSupply(1L).addSerialNumbers(SERIAL_NUMBER_1);
builder.addTokenTransferLists(mintTransfer1);
});
// approve allowance for nft 1
long approveAllowanceTimestamp = 25L;
var cryptoApproveAllowanceTransaction = buildTransaction(b -> b.getCryptoApproveAllowanceBuilder().addNftAllowances(NftAllowance.newBuilder().setOwner(PAYER).setTokenId(TOKEN_ID).addSerialNumbers(SERIAL_NUMBER_1).setSpender(SPENDER)));
insertAndParseTransaction(approveAllowanceTimestamp, cryptoApproveAllowanceTransaction);
var expectedNft1 = Nft.builder().id(new NftId(SERIAL_NUMBER_1, EntityId.of(TOKEN_ID))).accountId(EntityId.of(PAYER)).createdTimestamp(mintTimestamp1).deleted(false).metadata(METADATA.getBytes()).modifiedTimestamp(approveAllowanceTimestamp).spender(EntityId.of(SPENDER)).build();
assertThat(nftRepository.findById(expectedNft1.getId())).get().isEqualTo(expectedNft1);
long mintTimestamp2 = 30L;
TokenTransferList mintTransfer2 = nftTransfer(TOKEN_ID, RECEIVER, DEFAULT_ACCOUNT_ID, List.of(SERIAL_NUMBER_2));
Transaction mintTransaction2 = tokenSupplyTransaction(TOKEN_ID, NON_FUNGIBLE_UNIQUE, true, 0, List.of(SERIAL_NUMBER_2));
// Verify
insertAndParseTransaction(mintTimestamp2, mintTransaction2, builder -> {
builder.getReceiptBuilder().setNewTotalSupply(2L).addSerialNumbers(SERIAL_NUMBER_2);
builder.addTokenTransferLists(mintTransfer2);
});
// token transfer
Transaction transaction = tokenTransferTransaction();
TokenTransferList transferList1 = TokenTransferList.newBuilder().setToken(TOKEN_ID).addNftTransfers(NftTransfer.newBuilder().setReceiverAccountID(RECEIVER).setSenderAccountID(PAYER).setSerialNumber(SERIAL_NUMBER_1).build()).build();
TokenTransferList transferList2 = TokenTransferList.newBuilder().setToken(TOKEN_ID).addNftTransfers(NftTransfer.newBuilder().setReceiverAccountID(RECEIVER).setSenderAccountID(PAYER).setSerialNumber(SERIAL_NUMBER_2).build()).build();
long transferTimestamp = 40L;
insertAndParseTransaction(transferTimestamp, transaction, builder -> {
builder.addAllTokenTransferLists(List.of(transferList1, transferList2));
});
expectedNft1.setAccountId(EntityId.of(RECEIVER));
expectedNft1.setModifiedTimestamp(transferTimestamp);
expectedNft1.setSpender(null);
var expectedNft2 = Nft.builder().id(new NftId(SERIAL_NUMBER_2, EntityId.of(TOKEN_ID))).accountId(EntityId.of(RECEIVER)).createdTimestamp(mintTimestamp2).deleted(false).metadata(METADATA.getBytes()).modifiedTimestamp(transferTimestamp).build();
assertThat(nftTransferRepository.count()).isEqualTo(4L);
assertNftTransferInRepository(mintTimestamp1, SERIAL_NUMBER_1, TOKEN_ID, RECEIVER, null);
assertNftTransferInRepository(mintTimestamp2, SERIAL_NUMBER_2, TOKEN_ID, RECEIVER, null);
assertNftTransferInRepository(transferTimestamp, 1L, TOKEN_ID, RECEIVER, PAYER);
assertNftTransferInRepository(transferTimestamp, 2L, TOKEN_ID, RECEIVER, PAYER);
assertThat(nftRepository.findAll()).containsExactlyInAnyOrder(expectedNft1, expectedNft2);
}
use of com.hedera.mirror.common.domain.token.NftId in project hedera-mirror-node by hashgraph.
the class CryptoApproveAllowanceTransactionHandlerTest method beforeEach.
@BeforeEach
void beforeEach() {
consensusTimestamp = DomainUtils.timestampInNanosMax(recordItemBuilder.timestamp());
payerAccountId = EntityId.of(recordItemBuilder.accountId());
var cryptoOwner = recordItemBuilder.accountId();
expectedCryptoAllowance = CryptoAllowance.builder().amount(100L).owner(cryptoOwner.getAccountNum()).payerAccountId(payerAccountId).spender(recordItemBuilder.accountId().getAccountNum()).timestampRange(Range.atLeast(consensusTimestamp)).build();
when(entityIdService.lookup(cryptoOwner)).thenReturn(EntityId.of(cryptoOwner));
var nftOwner = recordItemBuilder.accountId();
var nftTokenId = recordItemBuilder.tokenId().getTokenNum();
expectedNft = Nft.builder().id(new NftId(1L, EntityId.of(nftTokenId, EntityType.TOKEN))).accountId(EntityId.of(nftOwner)).delegatingSpender(EntityId.EMPTY).modifiedTimestamp(consensusTimestamp).spender(EntityId.of(recordItemBuilder.accountId())).build();
when(entityIdService.lookup(nftOwner)).thenReturn(expectedNft.getAccountId());
expectedNftAllowance = NftAllowance.builder().approvedForAll(true).owner(expectedNft.getAccountId().getId()).payerAccountId(payerAccountId).spender(expectedNft.getSpender().getId()).timestampRange(Range.atLeast(consensusTimestamp)).tokenId(nftTokenId).build();
var tokenOwner = recordItemBuilder.accountId();
expectedTokenAllowance = TokenAllowance.builder().amount(200L).owner(tokenOwner.getAccountNum()).payerAccountId(payerAccountId).spender(recordItemBuilder.accountId().getAccountNum()).timestampRange(Range.atLeast(consensusTimestamp)).tokenId(recordItemBuilder.tokenId().getTokenNum()).build();
when(entityIdService.lookup(tokenOwner)).thenReturn(EntityId.of(tokenOwner));
}
Aggregations