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 tests code #10293

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

import com.google.protobuf.ByteString;
import com.hedera.mirror.common.domain.balance.AccountBalance;
import com.hedera.mirror.common.domain.balance.TokenBalance;
import com.hedera.mirror.common.domain.entity.Entity;
Expand Down Expand Up @@ -72,6 +73,7 @@
public abstract class AbstractContractCallServiceTest extends Web3IntegrationTest {

protected static final String TREASURY_ADDRESS = EvmTokenUtils.toAddress(2).toHexString();
public static final long BALANCE = 1_000_000_000_000_000L;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a generic abstract class, so we probably need a more specific name for this constant, such as DEFAULT_ACCOUNT_BALANCE or something like that.


@Resource
protected TestWeb3jService testWeb3jService;
Expand Down Expand Up @@ -227,6 +229,53 @@ protected Entity tokenEntityPersist() {
return domainBuilder.entity().customize(e -> e.type(EntityType.TOKEN)).persist();
}

/**
*
* @return Token object that is persisted in db
*/
protected Token fungibleTokenPersist() {
return fungibleTokenCustomizable(t -> {});
}

/**
*
* @param treasuryEntityId - the treasuryEntityId that has to be set in the token
* @return Token object that is persisted in db
*/
protected Token fungibleTokenPersistWithTreasuryAccount(final EntityId treasuryEntityId) {
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
* @return Token object which is persisted in the db
*/
protected Token fungibleTokenCustomizable(Consumer<Token.TokenBuilder<?, ?>> customizer) {
final var tokenEntity =
domainBuilder.entity().customize(e -> e.type(EntityType.TOKEN)).persist();

return domainBuilder
.token()
.customize(t -> {
t.tokenId(tokenEntity.getId()).type(TokenTypeEnum.FUNGIBLE_COMMON);
customizer.accept(t); // Apply any customizations provided
})
.persist();
}

/**
* Persists fungible token in the token db table.
*
Expand Down Expand Up @@ -307,19 +356,47 @@ protected NftAllowance nftAllowancePersist(Token token, Entity owner, Entity spe
.persist();
}

/**
* Creates an account with evmAddress and alias set to null and persist to db
* @return Entity object that is persisted in the db
*/
protected Entity accountEntityPersist() {
return domainBuilder
.entity()
.customize(e ->
e.type(EntityType.ACCOUNT).evmAddress(null).alias(null).balance(100_000_000_000_000_000L))
.persist();
return accountEntityPersistCustomizable(
e -> e.type(EntityType.ACCOUNT).evmAddress(null).alias(null).balance(BALANCE));
}

protected Entity accountEntityWithEvmAddressPersist() {
return domainBuilder
.entity()
.customize(e -> e.type(EntityType.ACCOUNT).balance(1_000_000_000_000_000L))
.persist();
return accountEntityPersistCustomizable(e -> e.type(EntityType.ACCOUNT).balance(BALANCE));
}

/**
*
* @param alias - the alias with which the account is created
* @param publicKey - the public key with which the account is created
* @return Entity object that is persisted in the db
*/
protected Entity accountPersistWithAlias(final Address alias, final ByteString publicKey) {
return accountEntityPersistCustomizable(
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
* @return
*/
protected Entity accountEntityPersistCustomizable(Consumer<Entity.EntityBuilder<?, ?>> customizer) {
return domainBuilder.entity().customize(customizer).persist();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.google.protobuf.ByteString;
import com.hedera.mirror.common.domain.entity.EntityId;
import com.hedera.mirror.common.domain.token.Token;
import com.hedera.mirror.common.domain.token.TokenKycStatusEnum;
Expand Down Expand Up @@ -94,7 +93,7 @@ void burnTokenGetTotalSupplyAndBalanceOfTreasury(
final var treasuryAddress = toAddress(treasuryEntityId.getId());

final var tokenEntity = tokenType == TokenTypeEnum.FUNGIBLE_COMMON
? fungibleTokenPersist(treasuryEntityId)
? fungibleTokenPersistWithTreasuryAccount(treasuryEntityId)
: nftPersist(treasuryEntityId);
final var tokenAddress = toAddress(tokenEntity.getTokenId());

Expand Down Expand Up @@ -129,7 +128,7 @@ void wipeTokenGetTotalSupplyAndBalanceOfTreasury(
final var senderAddress = toAddress(senderEntityId.getId());

final var tokenEntity = tokenType == TokenTypeEnum.FUNGIBLE_COMMON
? fungibleTokenPersist(treasuryEntityId)
? fungibleTokenPersistWithTreasuryAccount(treasuryEntityId)
: nftPersist(treasuryEntityId, senderEntityId);
final var tokenAddress = toAddress(tokenEntity.getTokenId());

Expand Down Expand Up @@ -160,7 +159,7 @@ void pauseTokenGetPauseStatusUnpauseGetPauseStatus(final TokenTypeEnum tokenType
final var treasuryEntityId = accountPersist();

final var tokenEntity = tokenType == TokenTypeEnum.FUNGIBLE_COMMON
? fungibleTokenPersist(treasuryEntityId)
? fungibleTokenPersistWithTreasuryAccount(treasuryEntityId)
: nftPersist(treasuryEntityId);
final var tokenAddress = toAddress(tokenEntity.getTokenId());

Expand Down Expand Up @@ -188,7 +187,7 @@ void freezeTokenGetPauseStatusUnpauseGetPauseStatus(final TokenTypeEnum tokenTyp
final var treasuryAddress = toAddress(treasuryEntityId.getId());

final var tokenEntity = tokenType == TokenTypeEnum.FUNGIBLE_COMMON
? fungibleTokenPersist(treasuryEntityId)
? fungibleTokenPersistWithTreasuryAccount(treasuryEntityId)
: nftPersist(treasuryEntityId);
final var tokenAddress = toAddress(tokenEntity.getTokenId());

Expand Down Expand Up @@ -254,7 +253,7 @@ void associateTokenTransfer(final TokenTypeEnum tokenType, final long amount, fi
final var senderAddress = toAddress(senderEntityId.getId());

final var tokenEntity = tokenType == TokenTypeEnum.FUNGIBLE_COMMON
? fungibleTokenPersist(treasuryEntityId, null)
? fungibleTokenPersistWithTreasuryAccountAndKYCKey(treasuryEntityId, null)
: nftPersist(treasuryEntityId, treasuryEntityId, treasuryEntityId, null);
final var tokenAddress = toAddress(tokenEntity.getTokenId());

Expand Down Expand Up @@ -294,7 +293,7 @@ void associateTokenDissociateFailTransferEthCall(
final var senderAddress = toAddress(senderEntityId.getId());

final var tokenEntity = tokenType == TokenTypeEnum.FUNGIBLE_COMMON
? fungibleTokenPersist(treasuryEntityId, null)
? fungibleTokenPersistWithTreasuryAccount(treasuryEntityId)
: nftPersist(treasuryEntityId, ownerEntityId);
final var tokenAddress = toAddress(tokenEntity.getTokenId());

Expand Down Expand Up @@ -339,7 +338,7 @@ void approveTokenGetAllowance(final TokenTypeEnum tokenType, final long amount,
final var spenderEntityId = accountPersist();

final var tokenEntity = tokenType == TokenTypeEnum.FUNGIBLE_COMMON
? fungibleTokenPersist(treasuryEntityId)
? fungibleTokenPersistWithTreasuryAccount(treasuryEntityId)
: nftPersist(treasuryEntityId, ownerEntityId, spenderEntityId);
final var tokenAddress = toAddress(tokenEntity.getTokenId());
final var tokenEntityId = entityIdFromEvmAddress(toAddress(tokenEntity.getTokenId()));
Expand Down Expand Up @@ -388,7 +387,7 @@ void approveTokenTransferFromGetAllowanceGetBalance(
final var contractEntityId = entityIdFromEvmAddress(contractAddress);

final var tokenEntity = tokenType == TokenTypeEnum.FUNGIBLE_COMMON
? fungibleTokenPersist(treasuryEntityId)
? fungibleTokenPersistWithTreasuryAccount(treasuryEntityId)
: nftPersist(treasuryEntityId, contractEntityId, spenderEntityId);
final var tokenAddress = toAddress(tokenEntity.getTokenId());
final var tokenEntityId = entityIdFromEvmAddress(tokenAddress);
Expand Down Expand Up @@ -431,7 +430,7 @@ void approveTokenTransferGetAllowanceGetBalance(
final var contractEntityId = entityIdFromEvmAddress(contractAddress);

final var tokenEntity = tokenType == TokenTypeEnum.FUNGIBLE_COMMON
? fungibleTokenPersist(treasuryEntityId)
? fungibleTokenPersistWithTreasuryAccount(treasuryEntityId)
: nftPersist(treasuryEntityId, senderEntityId);
final var tokenAddress = toAddress(tokenEntity.getTokenId());
final var tokenEntityId = entityIdFromEvmAddress(tokenAddress);
Expand Down Expand Up @@ -469,7 +468,7 @@ void approveTokenCryptoTransferGetAllowanceGetBalance(
final var contractEntityId = entityIdFromEvmAddress(contractAddress);

final var tokenEntity = tokenType == TokenTypeEnum.FUNGIBLE_COMMON
? fungibleTokenPersist(treasuryEntityId)
? fungibleTokenPersistWithTreasuryAccount(treasuryEntityId)
: nftPersist(treasuryEntityId, spenderEntityId);
final var tokenAddress = toAddress(tokenEntity.getTokenId());
final var tokenEntityId = entityIdFromEvmAddress(tokenAddress);
Expand Down Expand Up @@ -581,7 +580,7 @@ void cryptoTransferFromGetAllowanceGetBalance(
final var contractEntityId = entityIdFromEvmAddress(contractAddress);

final var tokenEntity = tokenType == TokenTypeEnum.FUNGIBLE_COMMON
? fungibleTokenPersist(treasuryEntityId)
? fungibleTokenPersistWithTreasuryAccount(treasuryEntityId)
: nftPersist(treasuryEntityId, spenderEntityId);
final var tokenAddress = toAddress(tokenEntity.getTokenId());
final var tokenEntityId = entityIdFromEvmAddress(tokenAddress);
Expand Down Expand Up @@ -659,7 +658,7 @@ void transferFromGetAllowanceGetBalance(final TokenTypeEnum tokenType, final lon
final var contractEntityId = entityIdFromEvmAddress(contractAddress);

final var tokenEntity = tokenType == TokenTypeEnum.FUNGIBLE_COMMON
? fungibleTokenPersist(treasuryEntityId)
? fungibleTokenPersistWithTreasuryAccount(treasuryEntityId)
: nftPersist(treasuryEntityId, treasuryEntityId);
final var tokenAddress = toAddress(tokenEntity.getTokenId());
final var tokenEntityId = entityIdFromEvmAddress(tokenAddress);
Expand Down Expand Up @@ -693,7 +692,7 @@ void grantKycRevokeKyc(final TokenTypeEnum tokenType) {
final var contract = testWeb3jService.deploy(DynamicEthCalls::deploy);

final var tokenEntity = tokenType == TokenTypeEnum.FUNGIBLE_COMMON
? fungibleTokenPersist(treasuryEntityId)
? fungibleTokenPersistWithTreasuryAccount(treasuryEntityId)
: nftPersist(treasuryEntityId, treasuryEntityId);
final var tokenAddress = toAddress(tokenEntity.getTokenId());
final var tokenEntityId = entityIdFromEvmAddress(tokenAddress);
Expand Down Expand Up @@ -764,23 +763,6 @@ void getAddressThisWithLongZeroRecipientThatHasEvmAlias() throws Exception {
verifyOpcodeTracerCall(functionCall.encodeFunctionCall(), contract);
}

private Token fungibleTokenPersist(final EntityId treasuryEntityId) {
return fungibleTokenPersist(treasuryEntityId, domainBuilder.key());
}

private Token fungibleTokenPersist(final EntityId treasuryEntityId, final byte[] kycKey) {
final var tokenEntity =
domainBuilder.entity().customize(e -> e.type(TOKEN)).persist();

return domainBuilder
.token()
.customize(t -> t.tokenId(tokenEntity.getId())
.type(TokenTypeEnum.FUNGIBLE_COMMON)
.treasuryAccountId(treasuryEntityId)
.kycKey(kycKey))
.persist();
}

private Token nftPersist(final EntityId treasuryEntityId) {
return nftPersist(treasuryEntityId, treasuryEntityId);
}
Expand Down Expand Up @@ -822,27 +804,15 @@ private Token nftPersist(
}

private EntityId accountPersist() {
return domainBuilder
.entity()
.customize(e -> e.evmAddress(null))
.persist()
.toEntityId();
}

private EntityId accountPersistWithAlias(final Address alias, final ByteString publicKey) {
return domainBuilder
.entity()
.customize(e -> e.evmAddress(alias.toArray()).alias(publicKey.toByteArray()))
.persist()
.toEntityId();
return accountEntityPersist().toEntityId();
}

private EntityId senderEntityPersistWithAlias() {
return accountPersistWithAlias(SENDER_ALIAS, SENDER_PUBLIC_KEY);
return accountPersistWithAlias(SENDER_ALIAS, SENDER_PUBLIC_KEY).toEntityId();
bilyana-gospodinova marked this conversation as resolved.
Show resolved Hide resolved
}

private EntityId spenderEntityPersistWithAlias() {
return accountPersistWithAlias(SPENDER_ALIAS, SPENDER_PUBLIC_KEY);
return accountPersistWithAlias(SPENDER_ALIAS, SPENDER_PUBLIC_KEY).toEntityId();
}

private void tokenAccountPersist(final EntityId tokenEntityId, final EntityId accountId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,22 +583,6 @@ private NestedCalls.HederaToken populateHederaToken(
BigInteger.valueOf(8_000_000)));
}

private Token fungibleTokenPersist() {
return fungibleTokenPersist(domainBuilder.entity().persist());
}

private Token fungibleTokenPersist(final Entity treasuryEntity) {
final var tokenEntity =
domainBuilder.entity().customize(e -> e.type(TOKEN)).persist();

return domainBuilder
.token()
.customize(t -> t.tokenId(tokenEntity.getId())
.type(TokenTypeEnum.FUNGIBLE_COMMON)
.treasuryAccountId(treasuryEntity.toEntityId()))
.persist();
}

private Token nftPersist() {
return nftPersist(domainBuilder.entity().persist());
}
Expand Down
Loading
Loading