Search in sources :

Example 1 with NftAllowance

use of com.hedera.mirror.common.domain.entity.NftAllowance in project hedera-mirror-node by hashgraph.

the class SqlEntityListenerTest method onNftAllowance.

@Test
void onNftAllowance() {
    // given
    NftAllowance nftAllowance1 = domainBuilder.nftAllowance().get();
    NftAllowance nftAllowance2 = domainBuilder.nftAllowance().get();
    // when
    sqlEntityListener.onNftAllowance(nftAllowance1);
    sqlEntityListener.onNftAllowance(nftAllowance2);
    completeFileAndCommit();
    // then
    assertThat(entityRepository.count()).isZero();
    assertThat(nftAllowanceRepository.findAll()).containsExactlyInAnyOrder(nftAllowance1, nftAllowance2);
    assertThat(findHistory(NftAllowance.class, "payer_account_id, spender, token_id")).isEmpty();
}
Also used : NftAllowance(com.hedera.mirror.common.domain.entity.NftAllowance) Test(org.junit.jupiter.api.Test) IntegrationTest(com.hedera.mirror.importer.IntegrationTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with NftAllowance

use of com.hedera.mirror.common.domain.entity.NftAllowance in project hedera-mirror-node by hashgraph.

the class SqlEntityListenerTest method onNftAllowanceHistory.

@ValueSource(ints = { 1, 2 })
@ParameterizedTest
void onNftAllowanceHistory(int commitIndex) {
    // given
    final String idColumns = "payer_account_id, spender, token_id";
    var builder = domainBuilder.nftAllowance();
    NftAllowance nftAllowanceCreate = builder.customize(c -> c.approvedForAll(true)).get();
    NftAllowance nftAllowanceUpdate1 = builder.get();
    nftAllowanceUpdate1.setTimestampLower(nftAllowanceCreate.getTimestampLower() + 1);
    // Expected merged objects
    NftAllowance mergedCreate = TestUtils.clone(nftAllowanceCreate);
    NftAllowance mergedUpdate1 = TestUtils.merge(nftAllowanceCreate, nftAllowanceUpdate1);
    mergedCreate.setTimestampUpper(nftAllowanceUpdate1.getTimestampLower());
    // when
    sqlEntityListener.onNftAllowance(nftAllowanceCreate);
    if (commitIndex > 1) {
        completeFileAndCommit();
        assertThat(nftAllowanceRepository.findAll()).containsExactly(nftAllowanceCreate);
        assertThat(findHistory(NftAllowance.class, idColumns)).isEmpty();
    }
    sqlEntityListener.onNftAllowance(nftAllowanceUpdate1);
    completeFileAndCommit();
    // then
    assertThat(nftAllowanceRepository.findAll()).containsExactly(mergedUpdate1);
    assertThat(findHistory(NftAllowance.class, idColumns)).containsExactly(mergedCreate);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) SCHEDULE(com.hedera.mirror.common.domain.entity.EntityType.SCHEDULE) CryptoAllowanceRepository(com.hedera.mirror.importer.repository.CryptoAllowanceRepository) SneakyThrows(lombok.SneakyThrows) EntityId(com.hedera.mirror.common.domain.entity.EntityId) NonFeeTransfer(com.hedera.mirror.common.domain.transaction.NonFeeTransfer) ContractRepository(com.hedera.mirror.importer.repository.ContractRepository) ContractResultRepository(com.hedera.mirror.importer.repository.ContractResultRepository) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Autowired(org.springframework.beans.factory.annotation.Autowired) NodeStakeRepository(com.hedera.mirror.importer.repository.NodeStakeRepository) ScheduleRepository(com.hedera.mirror.importer.repository.ScheduleRepository) EntityType(com.hedera.mirror.common.domain.entity.EntityType) TOKEN(com.hedera.mirror.common.domain.entity.EntityType.TOKEN) NftTransfer(com.hedera.mirror.common.domain.token.NftTransfer) TokenSupplyTypeEnum(com.hedera.mirror.common.domain.token.TokenSupplyTypeEnum) RecordFile(com.hedera.mirror.common.domain.transaction.RecordFile) NftAllowanceRepository(com.hedera.mirror.importer.repository.NftAllowanceRepository) Token(com.hedera.mirror.common.domain.token.Token) TokenId(com.hedera.mirror.common.domain.token.TokenId) TestUtils(com.hedera.mirror.importer.TestUtils) TransactionSignatureRepository(com.hedera.mirror.importer.repository.TransactionSignatureRepository) FileDataRepository(com.hedera.mirror.importer.repository.FileDataRepository) MethodSource(org.junit.jupiter.params.provider.MethodSource) ContractResult(com.hedera.mirror.common.domain.contract.ContractResult) TokenRepository(com.hedera.mirror.importer.repository.TokenRepository) ContractStateChange(com.hedera.mirror.common.domain.contract.ContractStateChange) TokenFreezeStatusEnum(com.hedera.mirror.common.domain.token.TokenFreezeStatusEnum) TopicMessage(com.hedera.mirror.common.domain.topic.TopicMessage) CryptoAllowance(com.hedera.mirror.common.domain.entity.CryptoAllowance) TokenPauseStatusEnum(com.hedera.mirror.common.domain.token.TokenPauseStatusEnum) StakingRewardTransferRepository(com.hedera.mirror.importer.repository.StakingRewardTransferRepository) Arguments(org.junit.jupiter.params.provider.Arguments) StandardCharsets(java.nio.charset.StandardCharsets) NftAllowance(com.hedera.mirror.common.domain.entity.NftAllowance) ByteString(com.google.protobuf.ByteString) Test(org.junit.jupiter.api.Test) List(java.util.List) Nft(com.hedera.mirror.common.domain.token.Nft) Stream(java.util.stream.Stream) EntityRepository(com.hedera.mirror.importer.repository.EntityRepository) ContractLogRepository(com.hedera.mirror.importer.repository.ContractLogRepository) CryptoTransferRepository(com.hedera.mirror.importer.repository.CryptoTransferRepository) TokenAccountRepository(com.hedera.mirror.importer.repository.TokenAccountRepository) EthereumTransactionRepository(com.hedera.mirror.importer.repository.EthereumTransactionRepository) TokenAccount(com.hedera.mirror.common.domain.token.TokenAccount) ACCOUNT(com.hedera.mirror.common.domain.entity.EntityType.ACCOUNT) Schedule(com.hedera.mirror.common.domain.schedule.Schedule) TokenTypeEnum(com.hedera.mirror.common.domain.token.TokenTypeEnum) NftId(com.hedera.mirror.common.domain.token.NftId) Contract(com.hedera.mirror.common.domain.contract.Contract) TransactionRepository(com.hedera.mirror.importer.repository.TransactionRepository) Hex(org.apache.commons.codec.binary.Hex) ArrayList(java.util.ArrayList) LiveHashRepository(com.hedera.mirror.importer.repository.LiveHashRepository) DomainBuilder(com.hedera.mirror.common.domain.DomainBuilder) TransactionSignature(com.hedera.mirror.common.domain.transaction.TransactionSignature) NftTransferRepository(com.hedera.mirror.importer.repository.NftTransferRepository) ContractStateChangeRepository(com.hedera.mirror.importer.repository.ContractStateChangeRepository) LiveHash(com.hedera.mirror.common.domain.transaction.LiveHash) ValueSource(org.junit.jupiter.params.provider.ValueSource) NftRepository(com.hedera.mirror.importer.repository.NftRepository) TokenAllowanceRepository(com.hedera.mirror.importer.repository.TokenAllowanceRepository) TopicMessageRepository(com.hedera.mirror.importer.repository.TopicMessageRepository) ContractLog(com.hedera.mirror.common.domain.contract.ContractLog) TokenTransferRepository(com.hedera.mirror.importer.repository.TokenTransferRepository) Entity(com.hedera.mirror.common.domain.entity.Entity) Consumer(java.util.function.Consumer) IntegrationTest(com.hedera.mirror.importer.IntegrationTest) Key(com.hederahashgraph.api.proto.java.Key) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) RecordFileRepository(com.hedera.mirror.importer.repository.RecordFileRepository) TokenKycStatusEnum(com.hedera.mirror.common.domain.token.TokenKycStatusEnum) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Assertions(org.junit.jupiter.api.Assertions) TokenAllowance(com.hedera.mirror.common.domain.entity.TokenAllowance) Transaction(com.hedera.mirror.common.domain.transaction.Transaction) TokenTransfer(com.hedera.mirror.common.domain.token.TokenTransfer) NftAllowance(com.hedera.mirror.common.domain.entity.NftAllowance) ByteString(com.google.protobuf.ByteString) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with NftAllowance

use of com.hedera.mirror.common.domain.entity.NftAllowance in project hedera-mirror-node by hashgraph.

the class NftAllowanceRepositoryTest method save.

@Test
void save() {
    NftAllowance nftAllowance = domainBuilder.nftAllowance().persist();
    assertThat(nftAllowanceRepository.findById(nftAllowance.getId())).get().isEqualTo(nftAllowance);
}
Also used : NftAllowance(com.hedera.mirror.common.domain.entity.NftAllowance) Test(org.junit.jupiter.api.Test)

Example 4 with NftAllowance

use of com.hedera.mirror.common.domain.entity.NftAllowance in project hedera-mirror-node by hashgraph.

the class CryptoApproveAllowanceTransactionHandler method parseNftAllowances.

private void parseNftAllowances(List<com.hederahashgraph.api.proto.java.NftAllowance> nftAllowances, RecordItem recordItem) {
    var consensusTimestamp = recordItem.getConsensusTimestamp();
    var payerAccountId = recordItem.getPayerAccountId();
    var nftAllowanceState = new HashMap<NftAllowance.Id, NftAllowance>();
    var nftSerialAllowanceState = new HashMap<NftId, Nft>();
    // iterate the nft allowance list in reverse order and honor the last allowance for either
    // the same owner, spender, and token for approved for all allowances, or the last serial allowance for
    // the same owner, spender, token, and serial
    var iterator = nftAllowances.listIterator(nftAllowances.size());
    while (iterator.hasPrevious()) {
        var nftApproval = iterator.previous();
        EntityId ownerAccountId = getOwnerAccountId(nftApproval.getOwner(), payerAccountId);
        if (ownerAccountId == EntityId.EMPTY) {
            // and the partialDataAction is SKIP
            continue;
        }
        EntityId spender = EntityId.of(nftApproval.getSpender());
        EntityId tokenId = EntityId.of(nftApproval.getTokenId());
        if (nftApproval.hasApprovedForAll()) {
            var approvedForAll = nftApproval.getApprovedForAll().getValue();
            NftAllowance nftAllowance = new NftAllowance();
            nftAllowance.setApprovedForAll(approvedForAll);
            nftAllowance.setOwner(ownerAccountId.getId());
            nftAllowance.setPayerAccountId(payerAccountId);
            nftAllowance.setSpender(spender.getId());
            nftAllowance.setTokenId(tokenId.getId());
            nftAllowance.setTimestampLower(consensusTimestamp);
            if (nftAllowanceState.putIfAbsent(nftAllowance.getId(), nftAllowance) == null) {
                entityListener.onNftAllowance(nftAllowance);
            }
        }
        EntityId delegatingSpender = EntityId.of(nftApproval.getDelegatingSpender());
        for (var serialNumber : nftApproval.getSerialNumbersList()) {
            // services allows the same serial number of a nft token appears in multiple nft allowances to
            // different spenders. The last spender will be granted such allowance.
            Nft nft = new Nft(serialNumber, tokenId);
            nft.setAccountId(ownerAccountId);
            nft.setDelegatingSpender(delegatingSpender);
            nft.setModifiedTimestamp(consensusTimestamp);
            nft.setSpender(spender);
            if (nftSerialAllowanceState.putIfAbsent(nft.getId(), nft) == null) {
                entityListener.onNft(nft);
            }
        }
    }
}
Also used : EntityId(com.hedera.mirror.common.domain.entity.EntityId) HashMap(java.util.HashMap) NftAllowance(com.hedera.mirror.common.domain.entity.NftAllowance) Nft(com.hedera.mirror.common.domain.token.Nft)

Example 5 with NftAllowance

use of com.hedera.mirror.common.domain.entity.NftAllowance in project hedera-mirror-node by hashgraph.

the class NftAllowanceRepositoryTest method history.

/**
 * This test verifies that the domain object and table definition are in sync with the history table.
 */
@Test
void history() {
    NftAllowance nftAllowance = domainBuilder.nftAllowance().persist();
    jdbcOperations.update("insert into nft_allowance_history select * from nft_allowance");
    List<NftAllowance> nftAllowanceHistory = jdbcOperations.query("select * from nft_allowance_history", ROW_MAPPER);
    assertThat(nftAllowanceRepository.findAll()).containsExactly(nftAllowance);
    assertThat(nftAllowanceHistory).containsExactly(nftAllowance);
}
Also used : NftAllowance(com.hedera.mirror.common.domain.entity.NftAllowance) Test(org.junit.jupiter.api.Test)

Aggregations

NftAllowance (com.hedera.mirror.common.domain.entity.NftAllowance)7 Test (org.junit.jupiter.api.Test)5 IntegrationTest (com.hedera.mirror.importer.IntegrationTest)3 CryptoAllowance (com.hedera.mirror.common.domain.entity.CryptoAllowance)2 EntityId (com.hedera.mirror.common.domain.entity.EntityId)2 TokenAllowance (com.hedera.mirror.common.domain.entity.TokenAllowance)2 Nft (com.hedera.mirror.common.domain.token.Nft)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 ByteString (com.google.protobuf.ByteString)1 DomainBuilder (com.hedera.mirror.common.domain.DomainBuilder)1 Contract (com.hedera.mirror.common.domain.contract.Contract)1 ContractLog (com.hedera.mirror.common.domain.contract.ContractLog)1 ContractResult (com.hedera.mirror.common.domain.contract.ContractResult)1 ContractStateChange (com.hedera.mirror.common.domain.contract.ContractStateChange)1 Entity (com.hedera.mirror.common.domain.entity.Entity)1 EntityType (com.hedera.mirror.common.domain.entity.EntityType)1 ACCOUNT (com.hedera.mirror.common.domain.entity.EntityType.ACCOUNT)1 SCHEDULE (com.hedera.mirror.common.domain.entity.EntityType.SCHEDULE)1 TOKEN (com.hedera.mirror.common.domain.entity.EntityType.TOKEN)1 Schedule (com.hedera.mirror.common.domain.schedule.Schedule)1