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