Search in sources :

Example 1 with TokenAllowance

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

the class SqlEntityListenerTest method onTokenAllowanceHistory.

@ValueSource(ints = { 1, 2, 3 })
@ParameterizedTest
void onTokenAllowanceHistory(int commitIndex) {
    // given
    final String idColumns = "payer_account_id, spender, token_id";
    var builder = domainBuilder.tokenAllowance();
    TokenAllowance tokenAllowanceCreate = builder.get();
    TokenAllowance tokenAllowanceUpdate1 = builder.customize(c -> c.amount(999L)).get();
    tokenAllowanceUpdate1.setTimestampLower(tokenAllowanceCreate.getTimestampLower() + 1);
    TokenAllowance tokenAllowanceUpdate2 = builder.customize(c -> c.amount(0)).get();
    tokenAllowanceUpdate2.setTimestampLower(tokenAllowanceCreate.getTimestampLower() + 2);
    // Expected merged objects
    TokenAllowance mergedCreate = TestUtils.clone(tokenAllowanceCreate);
    TokenAllowance mergedUpdate1 = TestUtils.merge(tokenAllowanceCreate, tokenAllowanceUpdate1);
    TokenAllowance mergedUpdate2 = TestUtils.merge(mergedUpdate1, tokenAllowanceUpdate2);
    mergedCreate.setTimestampUpper(tokenAllowanceUpdate1.getTimestampLower());
    // when
    sqlEntityListener.onTokenAllowance(tokenAllowanceCreate);
    if (commitIndex > 1) {
        completeFileAndCommit();
        assertThat(tokenAllowanceRepository.findAll()).containsExactly(tokenAllowanceCreate);
        assertThat(findHistory(TokenAllowance.class, idColumns)).isEmpty();
    }
    sqlEntityListener.onTokenAllowance(tokenAllowanceUpdate1);
    if (commitIndex > 2) {
        completeFileAndCommit();
        assertThat(tokenAllowanceRepository.findAll()).containsExactly(mergedUpdate1);
        assertThat(findHistory(TokenAllowance.class, idColumns)).containsExactly(mergedCreate);
    }
    sqlEntityListener.onTokenAllowance(tokenAllowanceUpdate2);
    completeFileAndCommit();
    // then
    mergedUpdate1.setTimestampUpper(tokenAllowanceUpdate2.getTimestampLower());
    assertThat(tokenAllowanceRepository.findAll()).containsExactly(mergedUpdate2);
    assertThat(findHistory(TokenAllowance.class, idColumns)).containsExactly(mergedCreate, mergedUpdate1);
}
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) ByteString(com.google.protobuf.ByteString) TokenAllowance(com.hedera.mirror.common.domain.entity.TokenAllowance) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with TokenAllowance

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

the class TokenAllowanceRepositoryTest method history.

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

Example 3 with TokenAllowance

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

the class TokenAllowanceRepositoryTest method save.

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

Example 4 with TokenAllowance

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

the class CryptoApproveAllowanceTransactionHandler method parseTokenAllowances.

private void parseTokenAllowances(List<com.hederahashgraph.api.proto.java.TokenAllowance> tokenAllowances, RecordItem recordItem) {
    var consensusTimestamp = recordItem.getConsensusTimestamp();
    var payerAccountId = recordItem.getPayerAccountId();
    var tokenAllowanceState = new HashMap<TokenAllowance.Id, TokenAllowance>();
    // iterate the token allowance list in reverse order and honor the last allowance for the same owner, spender,
    // and token
    var iterator = tokenAllowances.listIterator(tokenAllowances.size());
    while (iterator.hasPrevious()) {
        var tokenApproval = iterator.previous();
        EntityId ownerAccountId = getOwnerAccountId(tokenApproval.getOwner(), payerAccountId);
        if (ownerAccountId == EntityId.EMPTY) {
            // and the partialDataAction is SKIP
            continue;
        }
        TokenAllowance tokenAllowance = new TokenAllowance();
        tokenAllowance.setAmount(tokenApproval.getAmount());
        tokenAllowance.setOwner(ownerAccountId.getId());
        tokenAllowance.setPayerAccountId(payerAccountId);
        tokenAllowance.setSpender(EntityId.of(tokenApproval.getSpender()).getId());
        tokenAllowance.setTokenId(EntityId.of(tokenApproval.getTokenId()).getId());
        tokenAllowance.setTimestampLower(consensusTimestamp);
        if (tokenAllowanceState.putIfAbsent(tokenAllowance.getId(), tokenAllowance) == null) {
            entityListener.onTokenAllowance(tokenAllowance);
        }
    }
}
Also used : EntityId(com.hedera.mirror.common.domain.entity.EntityId) HashMap(java.util.HashMap) TokenAllowance(com.hedera.mirror.common.domain.entity.TokenAllowance)

Example 5 with TokenAllowance

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

the class SqlEntityListener method onTokenAllowance.

@Override
public void onTokenAllowance(TokenAllowance tokenAllowance) {
    TokenAllowance merged = tokenAllowanceState.merge(tokenAllowance.getId(), tokenAllowance, this::mergeTokenAllowance);
    tokenAllowances.add(merged);
}
Also used : TokenAllowance(com.hedera.mirror.common.domain.entity.TokenAllowance)

Aggregations

TokenAllowance (com.hedera.mirror.common.domain.entity.TokenAllowance)8 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 NftAllowance (com.hedera.mirror.common.domain.entity.NftAllowance)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 Nft (com.hedera.mirror.common.domain.token.Nft)1 NftId (com.hedera.mirror.common.domain.token.NftId)1