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 @@ -26,6 +26,7 @@
import com.hedera.hapi.node.base.AccountID.AccountOneOfType;
import com.hedera.mirror.common.domain.entity.Entity;
import com.hedera.mirror.common.domain.entity.EntityId;
import com.hedera.mirror.web3.evm.utils.EvmTokenUtils;
import com.hedera.pbj.runtime.OneOf;
import com.hedera.services.store.models.Id;
import com.hedera.services.store.models.NftId;
Expand Down Expand Up @@ -324,4 +325,13 @@ public static String readableId(final Object o) {
public static boolean isAliasSizeGreaterThanEvmAddress(final ByteString alias) {
return alias.size() > EVM_ADDRESS_SIZE;
}

public static String addressFromId(long tokenId) {
return EvmTokenUtils.toAddress(tokenId).toHexString();
}

public static EntityId entityIdFromTokenId(long tokenId) {
var address = EvmTokenUtils.toAddress(tokenId);
bilyana-gospodinova marked this conversation as resolved.
Show resolved Hide resolved
return EvmTokenUtils.entityIdFromEvmAddress(address);
}
}
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,19 @@ protected TokenAccount tokenAccount(Consumer<TokenAccount.TokenAccountBuilder<?,
.persist();
}

protected void tokenAccountPersist(final long tokenId, final long accountId, Long balance) {
tokenAccount(ta -> {
ta.tokenId(tokenId).accountId(accountId);
if (balance != null) {
bilyana-gospodinova marked this conversation as resolved.
Show resolved Hide resolved
ta.balance(balance);
}
});
}

protected void tokenAccountPersist(final long tokenId, final long accountId) {
tokenAccountPersist(tokenId, accountId, null);
}

/**
* 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 Down Expand Up @@ -487,11 +468,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 persistTokenBalance(EntityId account, EntityId token, long timestamp) {
bilyana-gospodinova marked this conversation as resolved.
Show resolved Hide resolved
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(), 1L);
tokenAccountPersist(tokenId, spenderId, 1L);

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

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