Search in sources :

Example 1 with NftId

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;
}
Also used : NftAllowance(com.hederahashgraph.api.proto.java.NftAllowance) NftId(com.hedera.mirror.common.domain.token.NftId) LinkedList(java.util.LinkedList)

Example 2 with NftId

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));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) CryptoAllowanceRepository(com.hedera.mirror.importer.repository.CryptoAllowanceRepository) EntityId(com.hedera.mirror.common.domain.entity.EntityId) NonFeeTransfer(com.hedera.mirror.common.domain.transaction.NonFeeTransfer) ContractRepository(com.hedera.mirror.importer.repository.ContractRepository) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) StringValue(com.google.protobuf.StringValue) Duration(com.hederahashgraph.api.proto.java.Duration) RecordParserProperties(com.hedera.mirror.importer.parser.record.RecordParserProperties) UtilityTest(com.hedera.mirror.importer.util.UtilityTest) CryptoDeleteLiveHashTransactionBody(com.hederahashgraph.api.proto.java.CryptoDeleteLiveHashTransactionBody) KeyList(com.hederahashgraph.api.proto.java.KeyList) NftAllowanceRepository(com.hedera.mirror.importer.repository.NftAllowanceRepository) TestUtils(com.hedera.mirror.importer.TestUtils) Utility(com.hedera.mirror.importer.util.Utility) RecordItemBuilder(com.hedera.mirror.importer.parser.domain.RecordItemBuilder) CryptoCreateTransactionBody(com.hederahashgraph.api.proto.java.CryptoCreateTransactionBody) CryptoUpdateTransactionBody(com.hederahashgraph.api.proto.java.CryptoUpdateTransactionBody) Collection(java.util.Collection) Resource(javax.annotation.Resource) CryptoTransfer(com.hedera.mirror.common.domain.transaction.CryptoTransfer) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) ByteString(com.google.protobuf.ByteString) Test(org.junit.jupiter.api.Test) AccountAmount(com.hederahashgraph.api.proto.java.AccountAmount) List(java.util.List) Nft(com.hedera.mirror.common.domain.token.Nft) RealmID(com.hederahashgraph.api.proto.java.RealmID) Stream(java.util.stream.Stream) ShardID(com.hederahashgraph.api.proto.java.ShardID) NftRemoveAllowance(com.hederahashgraph.api.proto.java.NftRemoveAllowance) StakingRewardTransfer(com.hedera.mirror.common.domain.transaction.StakingRewardTransfer) Optional(java.util.Optional) AccountIdConverter(com.hedera.mirror.common.converter.AccountIdConverter) RecordItem(com.hedera.mirror.common.domain.transaction.RecordItem) ErrataType(com.hedera.mirror.common.domain.transaction.ErrataType) Transaction(com.hederahashgraph.api.proto.java.Transaction) TransactionRecord(com.hederahashgraph.api.proto.java.TransactionRecord) NftId(com.hedera.mirror.common.domain.token.NftId) CsvSource(org.junit.jupiter.params.provider.CsvSource) Contract(com.hedera.mirror.common.domain.contract.Contract) PartialDataAction(com.hedera.mirror.importer.parser.PartialDataAction) BoolValue(com.google.protobuf.BoolValue) HashMap(java.util.HashMap) EnumSource(org.junit.jupiter.params.provider.EnumSource) NftAllowance(com.hederahashgraph.api.proto.java.NftAllowance) TransactionBody(com.hederahashgraph.api.proto.java.TransactionBody) SignedTransaction(com.hederahashgraph.api.proto.java.SignedTransaction) AliasNotFoundException(com.hedera.mirror.importer.exception.AliasNotFoundException) LinkedList(java.util.LinkedList) AccountID(com.hederahashgraph.api.proto.java.AccountID) LiveHash(com.hedera.mirror.common.domain.transaction.LiveHash) Timestamp(com.hederahashgraph.api.proto.java.Timestamp) TransferList(com.hederahashgraph.api.proto.java.TransferList) NftRepository(com.hedera.mirror.importer.repository.NftRepository) TokenAllowanceRepository(com.hedera.mirror.importer.repository.TokenAllowanceRepository) Int32Value(com.google.protobuf.Int32Value) IterableAssert(org.assertj.core.api.IterableAssert) DomainUtils(com.hedera.mirror.common.util.DomainUtils) ResponseCodeEnum(com.hederahashgraph.api.proto.java.ResponseCodeEnum) TransactionID(com.hederahashgraph.api.proto.java.TransactionID) DisplayName(org.junit.jupiter.api.DisplayName) Entity(com.hedera.mirror.common.domain.entity.Entity) Consumer(java.util.function.Consumer) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) TokenID(com.hederahashgraph.api.proto.java.TokenID) Assertions(org.junit.jupiter.api.Assertions) Condition(org.assertj.core.api.Condition) CryptoAddLiveHashTransactionBody(com.hederahashgraph.api.proto.java.CryptoAddLiveHashTransactionBody) NftRemoveAllowance(com.hederahashgraph.api.proto.java.NftRemoveAllowance) NftId(com.hedera.mirror.common.domain.token.NftId) Nft(com.hedera.mirror.common.domain.token.Nft) RecordItem(com.hedera.mirror.common.domain.transaction.RecordItem) UtilityTest(com.hedera.mirror.importer.util.UtilityTest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with NftId

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;
}
Also used : NftId(com.hedera.mirror.common.domain.token.NftId) Nft(com.hedera.mirror.common.domain.token.Nft)

Example 4 with NftId

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);
}
Also used : TokenTransferList(com.hederahashgraph.api.proto.java.TokenTransferList) Transaction(com.hederahashgraph.api.proto.java.Transaction) NftId(com.hedera.mirror.common.domain.token.NftId) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with NftId

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));
}
Also used : NftId(com.hedera.mirror.common.domain.token.NftId) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

NftId (com.hedera.mirror.common.domain.token.NftId)12 Test (org.junit.jupiter.api.Test)6 Nft (com.hedera.mirror.common.domain.token.Nft)4 Transaction (com.hederahashgraph.api.proto.java.Transaction)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 EntityId (com.hedera.mirror.common.domain.entity.EntityId)2 NftAllowance (com.hederahashgraph.api.proto.java.NftAllowance)2 TokenTransferList (com.hederahashgraph.api.proto.java.TokenTransferList)2 LinkedList (java.util.LinkedList)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 BoolValue (com.google.protobuf.BoolValue)1 ByteString (com.google.protobuf.ByteString)1 Int32Value (com.google.protobuf.Int32Value)1 StringValue (com.google.protobuf.StringValue)1 AccountIdConverter (com.hedera.mirror.common.converter.AccountIdConverter)1 Contract (com.hedera.mirror.common.domain.contract.Contract)1 Entity (com.hedera.mirror.common.domain.entity.Entity)1 CryptoTransfer (com.hedera.mirror.common.domain.transaction.CryptoTransfer)1 ErrataType (com.hedera.mirror.common.domain.transaction.ErrataType)1 LiveHash (com.hedera.mirror.common.domain.transaction.LiveHash)1