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);
}
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);
}
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);
}
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);
}
}
}
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);
}
Aggregations