Skip to content

Commit

Permalink
Fix hbar and token transfer tests (#10152)
Browse files Browse the repository at this point in the history
This Pr fixes cryptoTransferHbars, cryptoTransferToken and cryptoTransferHbarsAndToken test in ContractCallServicePrecompileModificationTest.
Those test were failing in the opcode assertion calls.
In the opcode flow we are always having a timestamp in the request.

So even if the timestamp is pretty similar to the latest case token balance and account balance are calculated with the historical queries. This PR adds the token and account balance setup for opcode flow.

Having the blueprint of the token and account balance, now we can fix other opcode test failure which are failing with insufficient payer or token balance.

This PR modifies ... in order to support ...
ContractCallServicePrecompileModificationTest - add token and account balance setup for transfer tests

---------

Signed-off-by: Kristiyan Selveliev <[email protected]>
  • Loading branch information
kselveliev authored Jan 17, 2025
1 parent 90540c5 commit 0a8166b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

import com.hedera.mirror.common.domain.balance.AccountBalance;
import com.hedera.mirror.common.domain.balance.TokenBalance;
import com.hedera.mirror.common.domain.entity.Entity;
import com.hedera.mirror.common.domain.entity.EntityId;
import com.hedera.mirror.common.domain.entity.EntityType;
Expand Down Expand Up @@ -273,6 +274,30 @@ protected void tokenAccountPersist(final Entity token, final Long accountId) {
.persist();
}

protected void persistAccountBalance(Entity account, long balance, long timestamp) {
domainBuilder
.accountBalance()
.customize(ab -> ab.id(new AccountBalance.Id(timestamp, account.toEntityId()))
.balance(balance))
.persist();
}

protected void persistAccountBalance(Entity account, long balance) {
domainBuilder
.accountBalance()
.customize(ab -> ab.id(new AccountBalance.Id(account.getCreatedTimestamp(), account.toEntityId()))
.balance(balance))
.persist();
}

protected void persistTokenBalance(Entity account, Entity token, long timestamp) {
domainBuilder
.tokenBalance()
.customize(ab -> ab.id(new TokenBalance.Id(timestamp, account.toEntityId(), token.toEntityId()))
.balance(100))
.persist();
}

protected String getAddressFromEntity(Entity entity) {
return EvmTokenUtils.toAddress(entity.toEntityId()).toHexString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,11 @@ void cryptoTransferHbars() throws Exception {
final var receiver = accountEntityWithEvmAddressPersist();
final var payer = accountEntityWithEvmAddressPersist();

long timestampForBalances = payer.getCreatedTimestamp();
persistAccountBalance(payer, payer.getBalance());
persistAccountBalance(sender, sender.getBalance(), timestampForBalances);
persistAccountBalance(treasuryEntity, treasuryEntity.getBalance(), timestampForBalances);

// When
testWeb3jService.setSender(getAliasFromEntity(payer));
final var transferList = new TransferList(List.of(
Expand All @@ -1241,10 +1246,19 @@ void cryptoTransferToken() throws Exception {
final var receiver = accountEntityWithEvmAddressPersist();
final var payer = accountEntityWithEvmAddressPersist();

tokenAccountPersist(tokenEntity, payer);
long timestampForBalances = payer.getCreatedTimestamp();
persistAccountBalance(payer, payer.getBalance());
persistTokenBalance(payer, tokenEntity, timestampForBalances);

persistAccountBalance(sender, sender.getBalance(), timestampForBalances);
persistTokenBalance(sender, tokenEntity, timestampForBalances);

persistTokenBalance(receiver, tokenEntity, timestampForBalances);
persistAccountBalance(treasuryEntity, treasuryEntity.getBalance(), timestampForBalances);

tokenAccountPersist(tokenEntity, sender);
tokenAccountPersist(tokenEntity, receiver);

tokenAccountPersist(tokenEntity, payer);
// When
testWeb3jService.setSender(getAliasFromEntity(payer));
final var tokenTransferList = new TokenTransferList(
Expand Down Expand Up @@ -1273,9 +1287,20 @@ void cryptoTransferHbarsAndToken() throws Exception {
final var receiver = accountEntityWithEvmAddressPersist();
final var payer = accountEntityWithEvmAddressPersist();

tokenAccountPersist(tokenEntity, payer);
long timestampForBalances = payer.getCreatedTimestamp();
persistAccountBalance(payer, payer.getBalance());
persistTokenBalance(payer, tokenEntity, timestampForBalances);

persistAccountBalance(sender, sender.getBalance(), timestampForBalances);
persistTokenBalance(sender, tokenEntity, timestampForBalances);

persistTokenBalance(receiver, tokenEntity, timestampForBalances);

persistAccountBalance(treasuryEntity, treasuryEntity.getBalance(), timestampForBalances);

tokenAccountPersist(tokenEntity, sender);
tokenAccountPersist(tokenEntity, receiver);
tokenAccountPersist(tokenEntity, payer);

// When
testWeb3jService.setSender(getAliasFromEntity(payer));
Expand Down

0 comments on commit 0a8166b

Please sign in to comment.