Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor web3 test code #10358

Merged
merged 15 commits into from
Feb 13, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ public static String asHexedEvmAddress(final Id id) {
return CommonUtils.hex(asEvmAddress(id.num()));
}

public static String asHexedEvmAddress(long tokenId) {
return CommonUtils.hex(asEvmAddress(tokenId));
}

public static boolean isAlias(final AccountID idOrAlias) {
return idOrAlias.getAccountNum() == 0 && !idOrAlias.getAlias().isEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public abstract class AbstractContractCallServiceTest extends Web3IntegrationTes

protected static final String TREASURY_ADDRESS = EvmTokenUtils.toAddress(2).toHexString();
protected static final long DEFAULT_ACCOUNT_BALANCE = 100_000_000_000_000_000L;
protected static final int DEFAULT_TOKEN_BALANCE = 100;

@Resource
protected TestWeb3jService testWeb3jService;
Expand Down Expand Up @@ -257,18 +258,6 @@ protected Token fungibleTokenPersistWithTreasuryAccount(final EntityId treasuryE
return fungibleTokenCustomizable(t -> t.treasuryAccountId(treasuryEntityId));
}

/**
*
* @param treasuryEntity - the treasuryEntity which has to be set in the token
* @param kycKey - the kycKey that has to be set in the token
* @return Token object that is persisted in db
*/
protected Token fungibleTokenPersistWithTreasuryAccountAndKYCKey(
final EntityId treasuryEntity, final byte[] kycKey) {
return fungibleTokenCustomizable(
t -> t.treasuryAccountId(treasuryEntity).kycKey(kycKey));
}

/**
* Method used to customize different fields of a token and persist it in db
* @param customizer - the consumer used to customize the token
Expand Down Expand Up @@ -392,16 +381,6 @@ protected Entity accountPersistWithAlias(final Address alias, final ByteString p
e -> e.evmAddress(alias.toArray()).alias(publicKey.toByteArray()));
}

/**
*
* @param balance - the balance with which the account is created
* @return Entity object that is persisted in the db
*/
protected Entity accountEntityPersistWithBalance(final long balance) {
return accountEntityPersistCustomizable(
e -> e.type(EntityType.ACCOUNT).evmAddress(null).alias(null).balance(balance));
}

/**
*
* @param customizer - the consumer with which to customize the entity
Expand All @@ -416,17 +395,6 @@ protected Entity accountEntityPersistCustomizable(Consumer<Entity.EntityBuilder<
* hold and operate with the token. Otherwise, ACCOUNT_KYC_NOT_GRANTED_FOR_TOKEN will be thrown when executing a
* transaction involving the token that requires the account to have KYC approval.
*/
protected void tokenAccountPersist(final Entity token, final Entity account) {
tokenAccount(
ta -> ta.tokenId(token.getId()).accountId(account.toEntityId().getId()));
}

protected void tokenAccountPersist(final Entity token, final Entity account, Long balance) {
tokenAccount(ta -> ta.tokenId(token.getId())
.accountId(account.toEntityId().getId())
.balance(balance));
}

protected TokenAccount tokenAccount(Consumer<TokenAccount.TokenAccountBuilder<?, ?>> consumer) {
return domainBuilder
.tokenAccount()
Expand All @@ -437,6 +405,10 @@ protected TokenAccount tokenAccount(Consumer<TokenAccount.TokenAccountBuilder<?,
.persist();
}

protected void tokenAccountPersist(final long tokenId, final long accountId) {
tokenAccount(ta -> ta.tokenId(tokenId).accountId(accountId));
}

/**
* Creates a non-fungible token instance with a specific serial number(a record in the nft table is persisted). The
* instance is tied to a specific token in the token db table.
Expand All @@ -462,22 +434,13 @@ protected Nft nonFungibleTokenInstancePersist(
* When an account balance is updated during a consensus event, an account_balance record with the consensus_timestamp,
* account_id and balance is created.The balance_timestamp for the account entry is updated as well in the entity table.
* @param account The account that the account_balance record is going to be created for
* @param balance The account balance that is going to be stored for the particular timestamp
* @param timestamp The timestamp indicating the account balance update
*/
protected void persistAccountBalance(Entity account, long balance, long timestamp) {
protected void accountBalancePersist(Entity account, 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))
.balance(account.getBalance()))
.persist();
}

Expand All @@ -487,11 +450,11 @@ protected void persistAccountBalance(Entity account, long balance) {
* No record for the token balance at a particular timestamp may result in INSUFFICIENT_TOKEN_BALANCE exception
* for a historical query with the same timestamp.
*/
protected void persistTokenBalance(Entity account, Entity token, long timestamp) {
protected void tokenBalancePersist(EntityId account, EntityId token, long timestamp) {
domainBuilder
.tokenBalance()
.customize(ab -> ab.id(new TokenBalance.Id(timestamp, account.toEntityId(), token.toEntityId()))
.balance(100))
.customize(ab ->
ab.id(new TokenBalance.Id(timestamp, account, token)).balance(DEFAULT_TOKEN_BALANCE))
.persist();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.hedera.mirror.common.domain.entity.Entity;
import com.hedera.mirror.common.domain.entity.EntityId;
import com.hedera.mirror.common.domain.token.Token;
import com.hedera.mirror.common.domain.token.TokenKycStatusEnum;
import com.hedera.mirror.common.domain.token.TokenTypeEnum;
import com.hedera.mirror.web3.common.ContractCallContext;
import com.hedera.mirror.web3.evm.store.Store.OnMissing;
Expand Down Expand Up @@ -95,7 +94,7 @@ void burnTokenGetTotalSupplyAndBalanceOfTreasury(final TokenTypeEnum tokenType)
final var treasuryAccount = accountEntityPersist();
final var tokenEntity = tokenEntityPersist();

tokenAccountPersist(tokenEntity, treasuryAccount, 1L);
tokenAccountPersist(tokenEntity.getId(), treasuryAccount.getId());

if (tokenType.equals(TokenTypeEnum.FUNGIBLE_COMMON)) {
fungibleTokenPersist(tokenEntity, treasuryAccount);
Expand Down Expand Up @@ -251,11 +250,12 @@ void associateTokenTransfer(final TokenTypeEnum tokenType, final long amount, fi
final var senderAddress = toAddress(senderEntityId.getId());

final var tokenEntity = tokenType == TokenTypeEnum.FUNGIBLE_COMMON
? fungibleTokenPersistWithTreasuryAccountAndKYCKey(treasuryEntityId, null)
? fungibleTokenCustomizable(
t -> t.treasuryAccountId(treasuryEntityId).kycKey(null))
: nftPersist(treasuryEntityId, treasuryEntityId, treasuryEntityId, null);
final var tokenAddress = toAddress(tokenEntity.getTokenId());

tokenAccountPersist(entityIdFromEvmAddress(tokenAddress), treasuryEntityId);
tokenAccountPersist(tokenEntity.getTokenId(), treasuryEntityId.getId());

final var contract = testWeb3jService.deploy(DynamicEthCalls::deploy);

Expand Down Expand Up @@ -339,14 +339,15 @@ void approveTokenGetAllowance(final TokenTypeEnum tokenType, final long amount,
? fungibleTokenPersistWithTreasuryAccount(treasuryEntityId)
: nftPersist(treasuryEntityId, ownerEntityId, spenderEntityId);
final var tokenAddress = toAddress(tokenEntity.getTokenId());
final var tokenId = tokenEntity.getTokenId();
final var tokenEntityId = entityIdFromEvmAddress(toAddress(tokenEntity.getTokenId()));

final var contract = testWeb3jService.deploy(DynamicEthCalls::deploy);
final var contractAddress = Address.fromHexString(contract.getContractAddress());
final var contractEntityId = entityIdFromEvmAddress(contractAddress);

tokenAccountPersist(tokenEntityId, contractEntityId);
tokenAccountPersist(tokenEntityId, ownerEntityId);
tokenAccountPersist(tokenId, contractEntityId.getId());
tokenAccountPersist(tokenId, ownerEntityId.getId());

if (tokenType == TokenTypeEnum.NON_FUNGIBLE_UNIQUE) {
nftAllowancePersist(tokenEntityId, contractEntityId, ownerEntityId);
Expand Down Expand Up @@ -389,10 +390,11 @@ void approveTokenTransferFromGetAllowanceGetBalance(
: nftPersist(treasuryEntityId, contractEntityId, spenderEntityId);
final var tokenAddress = toAddress(tokenEntity.getTokenId());
final var tokenEntityId = entityIdFromEvmAddress(tokenAddress);
final var tokenId = tokenEntity.getTokenId();

tokenAccountPersist(tokenEntityId, contractEntityId);
tokenAccountPersist(tokenEntityId, spenderEntityId);
tokenAccountPersist(tokenEntityId, ownerEntityId);
tokenAccountPersist(tokenId, contractEntityId.getId());
tokenAccountPersist(tokenId, spenderEntityId.getId());
tokenAccountPersist(tokenId, ownerEntityId.getId());

if (tokenType == TokenTypeEnum.NON_FUNGIBLE_UNIQUE) {
nftAllowancePersist(tokenEntityId, contractEntityId, ownerEntityId);
Expand Down Expand Up @@ -430,11 +432,11 @@ void approveTokenTransferGetAllowanceGetBalance(
final var tokenEntity = tokenType == TokenTypeEnum.FUNGIBLE_COMMON
? fungibleTokenPersistWithTreasuryAccount(treasuryEntityId)
: nftPersist(treasuryEntityId, senderEntityId);
final var tokenAddress = toAddress(tokenEntity.getTokenId());
final var tokenEntityId = entityIdFromEvmAddress(tokenAddress);
final var tokenId = tokenEntity.getTokenId();
final var tokenAddress = toAddress(tokenId);

tokenAccountPersist(tokenEntityId, senderEntityId);
tokenAccountPersist(tokenEntityId, contractEntityId);
tokenAccountPersist(tokenId, senderEntityId.getId());
tokenAccountPersist(tokenId, contractEntityId.getId());

// When
final var functionCall = contract.send_approveTokenTransferGetAllowanceGetBalance(
Expand Down Expand Up @@ -468,11 +470,11 @@ void approveTokenCryptoTransferGetAllowanceGetBalance(
final var tokenEntity = tokenType == TokenTypeEnum.FUNGIBLE_COMMON
? fungibleTokenPersistWithTreasuryAccount(treasuryEntityId)
: nftPersist(treasuryEntityId, spenderEntityId);
final var tokenAddress = toAddress(tokenEntity.getTokenId());
final var tokenEntityId = entityIdFromEvmAddress(tokenAddress);
final var tokenId = tokenEntity.getTokenId();
final var tokenAddress = toAddress(tokenId);

tokenAccountPersist(tokenEntityId, spenderEntityId);
tokenAccountPersist(tokenEntityId, contractEntityId);
tokenAccountPersist(tokenId, spenderEntityId.getId());
tokenAccountPersist(tokenId, contractEntityId.getId());

TokenTransferList tokenTransferList;
if (tokenType == TokenTypeEnum.FUNGIBLE_COMMON) {
Expand Down Expand Up @@ -514,10 +516,10 @@ void approveForAllTokenTransferFromGetAllowance() {

final var tokenEntity = nftPersist(treasuryEntityId, spenderEntityId);
final var tokenAddress = toAddress(tokenEntity.getTokenId());
final var tokenEntityId = entityIdFromEvmAddress(tokenAddress);
final var tokenId = tokenEntity.getTokenId();

tokenAccountPersist(tokenEntityId, spenderEntityId);
tokenAccountPersist(tokenEntityId, contractEntityId);
tokenAccountPersist(tokenId, spenderEntityId.getId());
tokenAccountPersist(tokenId, contractEntityId.getId());

// When
final var functionCall = contract.send_approveForAllTokenTransferGetAllowance(
Expand All @@ -540,10 +542,10 @@ void approveForAllCryptoTransferGetAllowance() {

final var tokenEntity = nftPersist(treasuryEntityId, spenderEntityId);
final var tokenAddress = toAddress(tokenEntity.getTokenId());
final var tokenEntityId = entityIdFromEvmAddress(tokenAddress);
final var tokenId = tokenEntity.getTokenId();

tokenAccountPersist(tokenEntityId, spenderEntityId);
tokenAccountPersist(tokenEntityId, contractEntityId);
tokenAccountPersist(tokenId, spenderEntityId.getId());
tokenAccountPersist(tokenId, contractEntityId.getId());

var tokenTransferList = new TokenTransferList(
tokenAddress.toHexString(),
Expand Down Expand Up @@ -580,11 +582,11 @@ void cryptoTransferFromGetAllowanceGetBalance(
final var tokenEntity = tokenType == TokenTypeEnum.FUNGIBLE_COMMON
? fungibleTokenPersistWithTreasuryAccount(treasuryEntityId)
: nftPersist(treasuryEntityId, spenderEntityId);
final var tokenAddress = toAddress(tokenEntity.getTokenId());
final var tokenEntityId = entityIdFromEvmAddress(tokenAddress);
final var tokenId = tokenEntity.getTokenId();
final var tokenAddress = toAddress(tokenId);

tokenAccountPersist(tokenEntityId, spenderEntityId);
tokenAccountPersist(tokenEntityId, contractEntityId);
tokenAccountPersist(tokenId, spenderEntityId.getId());
tokenAccountPersist(tokenId, contractEntityId.getId());

TokenTransferList tokenTransferList;
if (tokenType == TokenTypeEnum.FUNGIBLE_COMMON) {
Expand Down Expand Up @@ -622,14 +624,15 @@ void transferFromNFTGetAllowance() {
final var spenderEntityId = accountEntityPersist().toEntityId();

final var tokenEntity = nftPersist(treasuryEntityId, spenderEntityId);
final var tokenAddress = toAddress(tokenEntity.getTokenId());
final var tokenId = tokenEntity.getTokenId();
final var tokenAddress = toAddress(tokenId);

final var contract = testWeb3jService.deploy(DynamicEthCalls::deploy);
final var contractAddress = Address.fromHexString(contract.getContractAddress());
final var contractEntityId = entityIdFromEvmAddress(contractAddress);

tokenAccountPersist(entityIdFromEvmAddress(tokenAddress), spenderEntityId);
tokenAccountPersist(entityIdFromEvmAddress(tokenAddress), contractEntityId);
tokenAccountPersist(tokenId, spenderEntityId.getId());
tokenAccountPersist(tokenId, contractEntityId.getId());

// When
final var functionCall = contract.send_transferFromNFTGetAllowance(tokenAddress.toHexString(), BigInteger.ONE);
Expand Down Expand Up @@ -659,11 +662,11 @@ void transferFromGetAllowanceGetBalance(final TokenTypeEnum tokenType, final lon
? fungibleTokenPersistWithTreasuryAccount(treasuryEntityId)
: nftPersist(treasuryEntityId, treasuryEntityId);
final var tokenAddress = toAddress(tokenEntity.getTokenId());
final var tokenEntityId = entityIdFromEvmAddress(tokenAddress);
final var tokenId = tokenEntity.getTokenId();

tokenAccountPersist(tokenEntityId, treasuryEntityId);
tokenAccountPersist(tokenEntityId, spenderEntityId);
tokenAccountPersist(tokenEntityId, contractEntityId);
tokenAccountPersist(tokenId, treasuryEntityId.getId());
tokenAccountPersist(tokenId, spenderEntityId.getId());
tokenAccountPersist(tokenId, contractEntityId.getId());

// When
final var functionCall = contract.send_transferFromGetAllowanceGetBalance(
Expand Down Expand Up @@ -692,10 +695,10 @@ void grantKycRevokeKyc(final TokenTypeEnum tokenType) {
final var tokenEntity = tokenType == TokenTypeEnum.FUNGIBLE_COMMON
? fungibleTokenPersistWithTreasuryAccount(treasuryEntityId)
: nftPersist(treasuryEntityId, treasuryEntityId);
final var tokenAddress = toAddress(tokenEntity.getTokenId());
final var tokenEntityId = entityIdFromEvmAddress(tokenAddress);
final var tokenId = tokenEntity.getTokenId();
final var tokenAddress = toAddress(tokenId);

tokenAccountPersist(tokenEntityId, spenderEntityId);
tokenAccountPersist(tokenId, spenderEntityId.getId());

// When
final var functionCall =
Expand Down Expand Up @@ -805,16 +808,6 @@ private EntityId spenderEntityPersistWithAlias() {
return accountPersistWithAlias(SPENDER_ALIAS, SPENDER_PUBLIC_KEY).toEntityId();
}

private void tokenAccountPersist(final EntityId tokenEntityId, final EntityId accountId) {
domainBuilder
.tokenAccount()
.customize(e -> e.accountId(accountId.getId())
.tokenId(tokenEntityId.getId())
.associated(true)
.kycStatus(TokenKycStatusEnum.GRANTED))
.persist();
}

private void nftAllowancePersist(
final EntityId tokenEntityId, final EntityId spenderEntityId, final EntityId ownerEntityId) {
domainBuilder
Expand All @@ -829,12 +822,14 @@ private void nftAllowancePersist(

private Entity setUpToken(TokenTypeEnum tokenType, Entity treasuryAccount, Entity owner, Entity spender) {
final var tokenEntity = tokenEntityPersist();

tokenAccountPersist(tokenEntity, treasuryAccount, 1L);
tokenAccountPersist(tokenEntity, spender, 1L);

if (!Objects.equals(owner.getId(), spender.getId())) {
tokenAccountPersist(tokenEntity, owner, 1L);
final var tokenId = tokenEntity.getId();
final var spenderId = spender.getId();
final var ownerId = owner.getId();
tokenAccountPersist(tokenId, treasuryAccount.getId());
tokenAccountPersist(tokenId, spenderId);

if (!Objects.equals(ownerId, spenderId)) {
tokenAccountPersist(tokenId, ownerId);
}

if (tokenType.equals(TokenTypeEnum.FUNGIBLE_COMMON)) {
Expand Down
Loading
Loading