diff --git a/hedera-mirror-web3/src/main/java/com/hedera/services/utils/EntityIdUtils.java b/hedera-mirror-web3/src/main/java/com/hedera/services/utils/EntityIdUtils.java index 7eddff60cfa..e69eee53906 100644 --- a/hedera-mirror-web3/src/main/java/com/hedera/services/utils/EntityIdUtils.java +++ b/hedera-mirror-web3/src/main/java/com/hedera/services/utils/EntityIdUtils.java @@ -273,6 +273,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(); } diff --git a/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/AbstractContractCallServiceTest.java b/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/AbstractContractCallServiceTest.java index b2608c1949a..693d6be95b2 100644 --- a/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/AbstractContractCallServiceTest.java +++ b/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/AbstractContractCallServiceTest.java @@ -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; @@ -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 @@ -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 @@ -416,17 +395,6 @@ protected Entity accountEntityPersistCustomizable(Consumer 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> consumer) { return domainBuilder .tokenAccount() @@ -437,6 +405,10 @@ protected TokenAccount tokenAccount(Consumer 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. @@ -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(); } @@ -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(); } diff --git a/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/ContractCallDynamicCallsTest.java b/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/ContractCallDynamicCallsTest.java index ebfc8baf134..5ad341980c3 100644 --- a/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/ContractCallDynamicCallsTest.java +++ b/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/ContractCallDynamicCallsTest.java @@ -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; @@ -95,12 +94,12 @@ 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); } else { - Token token = nonFungibleTokenPersist(tokenEntity, treasuryAccount); + var token = nonFungibleTokenPersist(tokenEntity, treasuryAccount); nonFungibleTokenInstancePersist( token, 1L, @@ -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); @@ -290,10 +290,10 @@ void associateTokenDissociateFailTransferEthCall( final var senderEntityId = accountEntityPersist().toEntityId(); final var senderAddress = toAddress(senderEntityId.getId()); - final var tokenEntity = tokenType == TokenTypeEnum.FUNGIBLE_COMMON + final var token = tokenType == TokenTypeEnum.FUNGIBLE_COMMON ? fungibleTokenPersistWithTreasuryAccount(treasuryEntityId) : nftPersist(treasuryEntityId, ownerEntityId); - final var tokenAddress = toAddress(tokenEntity.getTokenId()); + final var tokenAddress = toAddress(token.getTokenId()); final var contract = testWeb3jService.deploy(DynamicEthCalls::deploy); @@ -335,18 +335,19 @@ void approveTokenGetAllowance(final TokenTypeEnum tokenType, final long amount, final var ownerAddress = toAddress(ownerEntityId); final var spenderEntityId = accountEntityPersist().toEntityId(); - final var tokenEntity = tokenType == TokenTypeEnum.FUNGIBLE_COMMON + final var token = tokenType == TokenTypeEnum.FUNGIBLE_COMMON ? fungibleTokenPersistWithTreasuryAccount(treasuryEntityId) : nftPersist(treasuryEntityId, ownerEntityId, spenderEntityId); - final var tokenAddress = toAddress(tokenEntity.getTokenId()); - final var tokenEntityId = entityIdFromEvmAddress(toAddress(tokenEntity.getTokenId())); + final var tokenId = token.getTokenId(); + final var tokenAddress = toAddress(tokenId); + final var tokenEntityId = entityIdFromEvmAddress(toAddress(tokenId)); 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); @@ -384,15 +385,16 @@ void approveTokenTransferFromGetAllowanceGetBalance( final var contractAddress = Address.fromHexString(contract.getContractAddress()); final var contractEntityId = entityIdFromEvmAddress(contractAddress); - final var tokenEntity = tokenType == TokenTypeEnum.FUNGIBLE_COMMON + final var token = tokenType == TokenTypeEnum.FUNGIBLE_COMMON ? fungibleTokenPersistWithTreasuryAccount(treasuryEntityId) : nftPersist(treasuryEntityId, contractEntityId, spenderEntityId); - final var tokenAddress = toAddress(tokenEntity.getTokenId()); + final var tokenId = token.getTokenId(); + final var tokenAddress = toAddress(tokenId); final var tokenEntityId = entityIdFromEvmAddress(tokenAddress); - 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); @@ -427,14 +429,14 @@ void approveTokenTransferGetAllowanceGetBalance( final var contractAddress = Address.fromHexString(contract.getContractAddress()); final var contractEntityId = entityIdFromEvmAddress(contractAddress); - final var tokenEntity = tokenType == TokenTypeEnum.FUNGIBLE_COMMON + final var token = tokenType == TokenTypeEnum.FUNGIBLE_COMMON ? fungibleTokenPersistWithTreasuryAccount(treasuryEntityId) : nftPersist(treasuryEntityId, senderEntityId); - final var tokenAddress = toAddress(tokenEntity.getTokenId()); - final var tokenEntityId = entityIdFromEvmAddress(tokenAddress); + final var tokenId = token.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( @@ -465,14 +467,14 @@ void approveTokenCryptoTransferGetAllowanceGetBalance( final var contractAddress = Address.fromHexString(contract.getContractAddress()); final var contractEntityId = entityIdFromEvmAddress(contractAddress); - final var tokenEntity = tokenType == TokenTypeEnum.FUNGIBLE_COMMON + final var token = tokenType == TokenTypeEnum.FUNGIBLE_COMMON ? fungibleTokenPersistWithTreasuryAccount(treasuryEntityId) : nftPersist(treasuryEntityId, spenderEntityId); - final var tokenAddress = toAddress(tokenEntity.getTokenId()); - final var tokenEntityId = entityIdFromEvmAddress(tokenAddress); + final var tokenId = token.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) { @@ -513,11 +515,11 @@ void approveForAllTokenTransferFromGetAllowance() { final var contractEntityId = entityIdFromEvmAddress(contractAddress); final var tokenEntity = 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()); // When final var functionCall = contract.send_approveForAllTokenTransferGetAllowance( @@ -539,11 +541,11 @@ void approveForAllCryptoTransferGetAllowance() { final var contractEntityId = entityIdFromEvmAddress(contractAddress); final var tokenEntity = 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()); var tokenTransferList = new TokenTransferList( tokenAddress.toHexString(), @@ -577,14 +579,14 @@ void cryptoTransferFromGetAllowanceGetBalance( final var contractAddress = Address.fromHexString(contract.getContractAddress()); final var contractEntityId = entityIdFromEvmAddress(contractAddress); - final var tokenEntity = tokenType == TokenTypeEnum.FUNGIBLE_COMMON + final var token = tokenType == TokenTypeEnum.FUNGIBLE_COMMON ? fungibleTokenPersistWithTreasuryAccount(treasuryEntityId) : nftPersist(treasuryEntityId, spenderEntityId); - final var tokenAddress = toAddress(tokenEntity.getTokenId()); - final var tokenEntityId = entityIdFromEvmAddress(tokenAddress); + final var tokenId = token.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) { @@ -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); @@ -655,15 +658,15 @@ void transferFromGetAllowanceGetBalance(final TokenTypeEnum tokenType, final lon final var contractAddress = Address.fromHexString(contract.getContractAddress()); final var contractEntityId = entityIdFromEvmAddress(contractAddress); - final var tokenEntity = tokenType == TokenTypeEnum.FUNGIBLE_COMMON + final var token = tokenType == TokenTypeEnum.FUNGIBLE_COMMON ? fungibleTokenPersistWithTreasuryAccount(treasuryEntityId) : nftPersist(treasuryEntityId, treasuryEntityId); - final var tokenAddress = toAddress(tokenEntity.getTokenId()); - final var tokenEntityId = entityIdFromEvmAddress(tokenAddress); + final var tokenId = token.getTokenId(); + final var tokenAddress = toAddress(tokenId); - 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( @@ -689,13 +692,13 @@ void grantKycRevokeKyc(final TokenTypeEnum tokenType) { final var contract = testWeb3jService.deploy(DynamicEthCalls::deploy); - final var tokenEntity = tokenType == TokenTypeEnum.FUNGIBLE_COMMON + final var token = tokenType == TokenTypeEnum.FUNGIBLE_COMMON ? fungibleTokenPersistWithTreasuryAccount(treasuryEntityId) : nftPersist(treasuryEntityId, treasuryEntityId); - final var tokenAddress = toAddress(tokenEntity.getTokenId()); - final var tokenEntityId = entityIdFromEvmAddress(tokenAddress); + final var tokenId = token.getTokenId(); + final var tokenAddress = toAddress(tokenId); - tokenAccountPersist(tokenEntityId, spenderEntityId); + tokenAccountPersist(tokenId, spenderEntityId.getId()); // When final var functionCall = @@ -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 @@ -829,18 +822,20 @@ 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)) { fungibleTokenPersist(tokenEntity, treasuryAccount); } else { - Token token = nonFungibleTokenPersist(tokenEntity, treasuryAccount); + var token = nonFungibleTokenPersist(tokenEntity, treasuryAccount); nonFungibleTokenInstancePersist(token, 1L, owner.toEntityId(), spender.toEntityId()); } diff --git a/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/ContractCallNestedCallsTest.java b/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/ContractCallNestedCallsTest.java index 257b346bfa9..95f044eb492 100644 --- a/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/ContractCallNestedCallsTest.java +++ b/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/ContractCallNestedCallsTest.java @@ -84,9 +84,9 @@ class ContractCallNestedCallsTest extends AbstractContractCallServiceOpcodeTrace void updateTokenKeysAndGetUpdatedTokenKeyForFungibleToken(final KeyValueType keyValueType, final KeyType keyType) throws Exception { // Given - final var tokenEntityId = fungibleTokenPersistWithTreasuryAccount( + final var token = fungibleTokenPersistWithTreasuryAccount( domainBuilder.entity().persist().toEntityId()); - final var tokenAddress = toAddress(tokenEntityId.getTokenId()); + final var tokenAddress = toAddress(token.getTokenId()); final var contract = testWeb3jService.deploy(NestedCalls::deploy); final var contractAddress = contract.getContractAddress(); diff --git a/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/ContractCallServiceERCTokenModificationFunctionsTest.java b/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/ContractCallServiceERCTokenModificationFunctionsTest.java index 34691f6fec1..46f82d438cd 100644 --- a/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/ContractCallServiceERCTokenModificationFunctionsTest.java +++ b/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/ContractCallServiceERCTokenModificationFunctionsTest.java @@ -72,9 +72,10 @@ void approveFungibleToken() { // Given final var spender = accountEntityPersist(); final var token = fungibleTokenPersist(); + final var tokenId = token.getTokenId(); final var amountGranted = 13L; - final var tokenEntity = entityIdFromEvmAddress(toAddress(token.getTokenId())); - final var tokenAddress = toAddress(token.getTokenId()); + final var tokenEntity = entityIdFromEvmAddress(toAddress(tokenId)); + final var tokenAddress = toAddress(tokenId); final var contract = testWeb3jService.deploy(ERCTestContract::deploy); final var contractAddress = Address.fromHexString(contract.getContractAddress()); final var contractEntityId = entityIdFromEvmAddress(contractAddress); @@ -163,10 +164,11 @@ void approveFungibleTokenWithAlias() { accountPersistWithAlias(SPENDER_ALIAS, SPENDER_PUBLIC_KEY).toEntityId(); final var treasury = accountEntityPersist().toEntityId(); final var token = fungibleTokenPersistWithTreasuryAccount(treasury); + final var tokenId = token.getTokenId(); final var amountGranted = 13L; - final var tokenEntity = entityIdFromEvmAddress(toAddress(token.getTokenId())); - final var tokenAddress = toAddress(token.getTokenId()); - tokenAssociateAccountPersist(spender, entityIdFromEvmAddress(toAddress(token.getTokenId()))); + final var tokenEntity = entityIdFromEvmAddress(toAddress(tokenId)); + final var tokenAddress = toAddress(tokenId); + tokenAssociateAccountPersist(spender, entityIdFromEvmAddress(toAddress(tokenId))); final var contract = testWeb3jService.deploy(ERCTestContract::deploy); final var contractAddress = Address.fromHexString(contract.getContractAddress()); @@ -205,9 +207,10 @@ void transfer() { final var recipient = accountEntityPersist().toEntityId(); final var treasury = accountEntityPersist().toEntityId(); final var token = fungibleTokenPersistWithTreasuryAccount(treasury); - final var tokenEntity = entityIdFromEvmAddress(toAddress(token.getTokenId())); + final var tokenId = token.getTokenId(); + final var tokenEntity = entityIdFromEvmAddress(toAddress(tokenId)); final var tokenAddress = toAddress(tokenEntity.getId()); - tokenAssociateAccountPersist(recipient, entityIdFromEvmAddress(toAddress(tokenEntity.getId()))); + tokenAssociateAccountPersist(recipient, entityIdFromEvmAddress(toAddress(tokenId))); final var contract = testWeb3jService.deploy(ERCTestContract::deploy); final var contractAddress = Address.fromHexString(contract.getContractAddress()); @@ -230,7 +233,8 @@ void transferFrom() { final var recipient = accountEntityPersist().toEntityId(); final var token = fungibleTokenPersistWithTreasuryAccount(treasury); - final var tokenEntity = entityIdFromEvmAddress(toAddress(token.getTokenId())); + final var tokenId = token.getTokenId(); + final var tokenEntity = entityIdFromEvmAddress(toAddress(tokenId)); tokenAssociateAccountPersist(owner, tokenEntity); tokenAssociateAccountPersist(recipient, tokenEntity); @@ -239,11 +243,10 @@ void transferFrom() { final var contractEntityId = entityIdFromEvmAddress(contractAddress); tokenAssociateAccountPersist(contractEntityId, tokenEntity); final var amount = 10L; - fungibleTokenAllowancePersist( - contractEntityId, owner, entityIdFromEvmAddress(toAddress(tokenEntity.getId())), amount); + fungibleTokenAllowancePersist(contractEntityId, owner, entityIdFromEvmAddress(toAddress(tokenId)), amount); // When final var functionCall = contract.send_transferFrom( - toAddress(tokenEntity.getId()).toHexString(), + toAddress(tokenId).toHexString(), toAddress(owner).toHexString(), toAddress(recipient).toHexString(), BigInteger.valueOf(amount)); @@ -320,17 +323,18 @@ void transferWithAlias() { final var treasury = accountPersistWithAlias(SENDER_ALIAS, SENDER_PUBLIC_KEY).toEntityId(); final var token = fungibleTokenPersistWithTreasuryAccount(treasury); - tokenAssociateAccountPersist(recipient.toEntityId(), entityIdFromEvmAddress(toAddress(token.getTokenId()))); + final var tokenId = token.getTokenId(); + tokenAssociateAccountPersist(recipient.toEntityId(), entityIdFromEvmAddress(toAddress(tokenId))); final var contract = testWeb3jService.deploy(ERCTestContract::deploy); final var contractAddress = Address.fromHexString(contract.getContractAddress()); final var contractEntityId = entityIdFromEvmAddress(contractAddress); - tokenAssociateAccountPersist(contractEntityId, entityIdFromEvmAddress(toAddress(token.getTokenId()))); + tokenAssociateAccountPersist(contractEntityId, entityIdFromEvmAddress(toAddress(tokenId))); final var amount = 10L; // When final var functionCall = contract.send_transfer( - toAddress(token.getTokenId()).toHexString(), SPENDER_ALIAS.toHexString(), BigInteger.valueOf(amount)); + toAddress(tokenId).toHexString(), SPENDER_ALIAS.toHexString(), BigInteger.valueOf(amount)); // Then verifyEthCallAndEstimateGas(functionCall, contract); } @@ -398,10 +402,11 @@ void approveFungibleTokenRedirect() { // Given final var spender = accountEntityPersist().toEntityId(); final var token = fungibleTokenPersist(); + final var tokenId = token.getTokenId(); final var amountGranted = 13L; - final var tokenEntity = entityIdFromEvmAddress(toAddress(token.getTokenId())); - final var tokenAddress = toAddress(token.getTokenId()); - tokenAssociateAccountPersist(spender, entityIdFromEvmAddress(toAddress(token.getTokenId()))); + final var tokenEntity = entityIdFromEvmAddress(toAddress(tokenId)); + final var tokenAddress = toAddress(tokenId); + tokenAssociateAccountPersist(spender, entityIdFromEvmAddress(toAddress(tokenId))); final var contract = testWeb3jService.deploy(RedirectTestContract::deploy); final var contractAddress = Address.fromHexString(contract.getContractAddress()); @@ -455,10 +460,11 @@ void approveFungibleTokenWithAliasRedirect() { final var spender = accountPersistWithAlias(SPENDER_ALIAS, SPENDER_PUBLIC_KEY).toEntityId(); final var token = fungibleTokenPersist(); + final var tokenId = token.getTokenId(); final var amountGranted = 13L; - final var tokenEntity = entityIdFromEvmAddress(toAddress(token.getTokenId())); - final var tokenAddress = toAddress(token.getTokenId()); - tokenAssociateAccountPersist(spender, entityIdFromEvmAddress(toAddress(token.getTokenId()))); + final var tokenEntity = entityIdFromEvmAddress(toAddress(tokenId)); + final var tokenAddress = toAddress(tokenId); + tokenAssociateAccountPersist(spender, entityIdFromEvmAddress(toAddress(tokenId))); final var contract = testWeb3jService.deploy(RedirectTestContract::deploy); final var contractAddress = Address.fromHexString(contract.getContractAddress()); @@ -524,19 +530,20 @@ void transferFromRedirect() { final var recipient = accountEntityPersist().toEntityId(); final var token = fungibleTokenPersistWithTreasuryAccount(treasury); - final var tokenEntity = entityIdFromEvmAddress(toAddress(token.getTokenId())); + final var tokenId = token.getTokenId(); + final var tokenEntity = entityIdFromEvmAddress(toAddress(tokenId)); tokenAssociateAccountPersist(owner, tokenEntity); tokenAssociateAccountPersist(recipient, tokenEntity); final var contract = testWeb3jService.deploy(RedirectTestContract::deploy); final var contractAddress = Address.fromHexString(contract.getContractAddress()); final var contractEntityId = entityIdFromEvmAddress(contractAddress); - tokenAssociateAccountPersist(contractEntityId, entityIdFromEvmAddress(toAddress(tokenEntity.getId()))); + tokenAssociateAccountPersist(contractEntityId, entityIdFromEvmAddress(toAddress(tokenId))); final var amount = 10L; fungibleTokenAllowancePersist(contractEntityId, owner, tokenEntity, amount); // When final var functionCall = contract.send_transferFromRedirect( - toAddress(tokenEntity.getId()).toHexString(), + toAddress(tokenId).toHexString(), toAddress(owner).toHexString(), toAddress(recipient).toHexString(), BigInteger.valueOf(amount)); @@ -618,17 +625,18 @@ void transferWithAliasRedirect() { final var treasury = accountPersistWithAlias(SENDER_ALIAS, SENDER_PUBLIC_KEY).toEntityId(); final var token = fungibleTokenPersistWithTreasuryAccount(treasury); - tokenAssociateAccountPersist(recipient, entityIdFromEvmAddress(toAddress(token.getTokenId()))); + final var tokenId = token.getTokenId(); + tokenAssociateAccountPersist(recipient, entityIdFromEvmAddress(toAddress(tokenId))); final var contract = testWeb3jService.deploy(RedirectTestContract::deploy); final var contractAddress = Address.fromHexString(contract.getContractAddress()); final var contractEntityId = entityIdFromEvmAddress(contractAddress); - tokenAssociateAccountPersist(contractEntityId, entityIdFromEvmAddress(toAddress(token.getTokenId()))); + tokenAssociateAccountPersist(contractEntityId, entityIdFromEvmAddress(toAddress(tokenId))); final var amount = 10L; // When final var functionCall = contract.send_transferRedirect( - toAddress(token.getTokenId()).toHexString(), SPENDER_ALIAS.toHexString(), BigInteger.valueOf(amount)); + toAddress(tokenId).toHexString(), SPENDER_ALIAS.toHexString(), BigInteger.valueOf(amount)); // Then verifyEthCallAndEstimateGas(functionCall, contract); } @@ -642,7 +650,8 @@ void transferFromWithAliasRedirect() { final var recipient = accountPersistWithAlias(SPENDER_ALIAS, SPENDER_PUBLIC_KEY).toEntityId(); final var token = fungibleTokenPersistWithTreasuryAccount(treasury); - final var tokenEntity = entityIdFromEvmAddress(toAddress(token.getTokenId())); + final var tokenId = token.getTokenId(); + final var tokenEntity = entityIdFromEvmAddress(toAddress(tokenId)); tokenAssociateAccountPersist(owner, tokenEntity); tokenAssociateAccountPersist(recipient, tokenEntity); @@ -651,11 +660,10 @@ void transferFromWithAliasRedirect() { final var contractEntityId = entityIdFromEvmAddress(contractAddress); tokenAssociateAccountPersist(contractEntityId, tokenEntity); final var amount = 10L; - fungibleTokenAllowancePersist( - contractEntityId, owner, entityIdFromEvmAddress(toAddress(tokenEntity.getId())), amount); + fungibleTokenAllowancePersist(contractEntityId, owner, entityIdFromEvmAddress(toAddress(tokenId)), amount); // When final var functionCall = contract.send_transferFromRedirect( - toAddress(tokenEntity.getId()).toHexString(), + toAddress(tokenId).toHexString(), SENDER_ALIAS.toHexString(), SPENDER_ALIAS.toHexString(), BigInteger.valueOf(amount)); diff --git a/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/ContractCallServiceERCTokenReadOnlyFunctionsTest.java b/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/ContractCallServiceERCTokenReadOnlyFunctionsTest.java index e9170c5a89a..7560534da9b 100644 --- a/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/ContractCallServiceERCTokenReadOnlyFunctionsTest.java +++ b/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/ContractCallServiceERCTokenReadOnlyFunctionsTest.java @@ -208,14 +208,13 @@ void ethCallIsApprovedForAllWithAliasNonStatic() throws Exception { void ethCallAllowanceStatic() throws Exception { final var owner = accountPersist(); final var spender = accountPersist(); - final var tokenEntity = fungibleTokenPersist(); + final var token = fungibleTokenPersist(); + final var tokenId = token.getTokenId(); var entity = domainBuilder .tokenAllowance() - .customize(a -> a.tokenId(tokenEntity.getTokenId()) - .owner(owner.getNum()) - .spender(spender.getNum())) + .customize(a -> a.tokenId(tokenId).owner(owner.getNum()).spender(spender.getNum())) .persist(); - final var tokenAddress = toAddress(tokenEntity.getTokenId()); + final var tokenAddress = toAddress(tokenId); final var contract = testWeb3jService.deploy(ERCTestContract::deploy); final var result = contract.call_allowance( tokenAddress.toHexString(), @@ -234,14 +233,13 @@ void ethCallAllowanceStatic() throws Exception { void ethCallAllowanceNonStatic() throws Exception { final var owner = accountPersist(); final var spender = accountPersist(); - final var tokenEntity = fungibleTokenPersist(); + final var token = fungibleTokenPersist(); + final var tokenId = token.getTokenId(); var entity = domainBuilder .tokenAllowance() - .customize(a -> a.tokenId(tokenEntity.getTokenId()) - .owner(owner.getNum()) - .spender(spender.getNum())) + .customize(a -> a.tokenId(tokenId).owner(owner.getNum()).spender(spender.getNum())) .persist(); - final var tokenAddress = toAddress(tokenEntity.getTokenId()); + final var tokenAddress = toAddress(tokenId); final var contract = testWeb3jService.deploy(ERCTestContract::deploy); final var result = contract.call_allowanceNonStatic( tokenAddress.toHexString(), @@ -260,14 +258,13 @@ void ethCallAllowanceNonStatic() throws Exception { void ethCallAllowanceWithAliasStatic() throws Exception { final var spender = spenderEntityPersistWithAlias(); final var owner = senderEntityPersistWithAlias(); - final var tokenEntity = fungibleTokenPersist(); + final var token = fungibleTokenPersist(); + final var tokenId = token.getTokenId(); var entity = domainBuilder .tokenAllowance() - .customize(a -> a.tokenId(tokenEntity.getTokenId()) - .owner(owner.getNum()) - .spender(spender.getNum())) + .customize(a -> a.tokenId(tokenId).owner(owner.getNum()).spender(spender.getNum())) .persist(); - final var tokenAddress = toAddress(tokenEntity.getTokenId()); + final var tokenAddress = toAddress(tokenId); final var contract = testWeb3jService.deploy(ERCTestContract::deploy); final var result = contract.call_allowance( tokenAddress.toHexString(), SENDER_ALIAS.toHexString(), SPENDER_ALIAS.toHexString()) @@ -282,14 +279,13 @@ void ethCallAllowanceWithAliasStatic() throws Exception { void ethCallAllowanceWithAliasNonStatic() throws Exception { final var spender = spenderEntityPersistWithAlias(); final var owner = senderEntityPersistWithAlias(); - final var tokenEntity = fungibleTokenPersist(); + final var token = fungibleTokenPersist(); + final var tokenId = token.getTokenId(); var entity = domainBuilder .tokenAllowance() - .customize(a -> a.tokenId(tokenEntity.getTokenId()) - .owner(owner.getNum()) - .spender(spender.getNum())) + .customize(a -> a.tokenId(tokenId).owner(owner.getNum()).spender(spender.getNum())) .persist(); - final var tokenAddress = toAddress(tokenEntity.getTokenId()); + final var tokenAddress = toAddress(tokenId); final var contract = testWeb3jService.deploy(ERCTestContract::deploy); final var result = contract.call_allowanceNonStatic( tokenAddress.toHexString(), SENDER_ALIAS.toHexString(), SPENDER_ALIAS.toHexString()) @@ -438,12 +434,13 @@ void ethCallSymbolNonStatic() throws Exception { @Test void ethCallBalanceOfStatic() throws Exception { final var owner = accountPersist(); - final var tokenEntity = fungibleTokenPersistWithTreasuryAccount(owner); + final var token = fungibleTokenPersistWithTreasuryAccount(owner); + final var tokenId = token.getTokenId(); var entity = domainBuilder .tokenAccount() - .customize(e -> e.accountId(owner.getId()).tokenId(tokenEntity.getTokenId())) + .customize(e -> e.accountId(owner.getId()).tokenId(tokenId)) .persist(); - final var tokenAddress = toAddress(tokenEntity.getTokenId()); + final var tokenAddress = toAddress(tokenId); final var contract = testWeb3jService.deploy(ERCTestContract::deploy); final var result = contract.call_balanceOf( tokenAddress.toHexString(), toAddress(owner).toHexString()) @@ -457,12 +454,13 @@ void ethCallBalanceOfStatic() throws Exception { @Test void ethCallBalanceOfNonStatic() throws Exception { final var owner = accountPersist(); - final var tokenEntity = fungibleTokenPersistWithTreasuryAccount(owner); + final var token = fungibleTokenPersistWithTreasuryAccount(owner); + final var tokenId = token.getTokenId(); var entity = domainBuilder .tokenAccount() - .customize(e -> e.accountId(owner.getId()).tokenId(tokenEntity.getTokenId())) + .customize(e -> e.accountId(owner.getId()).tokenId(tokenId)) .persist(); - final var tokenAddress = toAddress(tokenEntity.getTokenId()); + final var tokenAddress = toAddress(tokenId); final var contract = testWeb3jService.deploy(ERCTestContract::deploy); final var result = contract.call_balanceOfNonStatic( tokenAddress.toHexString(), toAddress(owner).toHexString()) @@ -476,12 +474,13 @@ void ethCallBalanceOfNonStatic() throws Exception { @Test void ethCallBalanceOfWithAliasStatic() throws Exception { final var owner = senderEntityPersistWithAlias(); - final var tokenEntity = fungibleTokenPersistWithTreasuryAccount(owner); + final var token = fungibleTokenPersistWithTreasuryAccount(owner); + final var tokenId = token.getTokenId(); var entity = domainBuilder .tokenAccount() - .customize(e -> e.accountId(owner.getId()).tokenId(tokenEntity.getTokenId())) + .customize(e -> e.accountId(owner.getId()).tokenId(tokenId)) .persist(); - final var tokenAddress = toAddress(tokenEntity.getTokenId()); + final var tokenAddress = toAddress(tokenId); final var contract = testWeb3jService.deploy(ERCTestContract::deploy); final var result = contract.call_balanceOf(tokenAddress.toHexString(), SENDER_ALIAS.toHexString()) .send(); @@ -493,12 +492,13 @@ void ethCallBalanceOfWithAliasStatic() throws Exception { @Test void ethCallBalanceOfWithAliasNonStatic() throws Exception { final var owner = senderEntityPersistWithAlias(); - final var tokenEntity = fungibleTokenPersistWithTreasuryAccount(owner); + final var token = fungibleTokenPersistWithTreasuryAccount(owner); + final var tokenId = token.getTokenId(); var entity = domainBuilder .tokenAccount() - .customize(e -> e.accountId(owner.getId()).tokenId(tokenEntity.getTokenId())) + .customize(e -> e.accountId(owner.getId()).tokenId(tokenId)) .persist(); - final var tokenAddress = toAddress(tokenEntity.getTokenId()); + final var tokenAddress = toAddress(tokenId); final var contract = testWeb3jService.deploy(ERCTestContract::deploy); final var result = contract.call_balanceOfNonStatic(tokenAddress.toHexString(), SENDER_ALIAS.toHexString()) .send(); @@ -734,14 +734,13 @@ void ethCallIsApprovedForAllWithAliasRedirect() { void ethCallAllowanceRedirect() { final var owner = accountPersist(); final var spender = accountPersist(); - final var tokenEntity = fungibleTokenPersist(); + final var token = fungibleTokenPersist(); + final var tokenId = token.getTokenId(); domainBuilder .tokenAllowance() - .customize(a -> a.tokenId(tokenEntity.getTokenId()) - .owner(owner.getNum()) - .spender(spender.getNum())) + .customize(a -> a.tokenId(tokenId).owner(owner.getNum()).spender(spender.getNum())) .persist(); - final var tokenAddress = toAddress(tokenEntity.getTokenId()); + final var tokenAddress = toAddress(tokenId); final var contract = testWeb3jService.deploy(RedirectTestContract::deploy); final var functionCall = contract.send_allowanceRedirect( tokenAddress.toHexString(), @@ -754,14 +753,13 @@ void ethCallAllowanceRedirect() { void ethCallAllowanceWithAliasRedirect() { final var spender = spenderEntityPersistWithAlias(); final var owner = senderEntityPersistWithAlias(); - final var tokenEntity = fungibleTokenPersist(); + final var token = fungibleTokenPersist(); + final var tokenId = token.getTokenId(); domainBuilder .tokenAllowance() - .customize(a -> a.tokenId(tokenEntity.getTokenId()) - .owner(owner.getNum()) - .spender(spender.getNum())) + .customize(a -> a.tokenId(tokenId).owner(owner.getNum()).spender(spender.getNum())) .persist(); - final var tokenAddress = toAddress(tokenEntity.getTokenId()); + final var tokenAddress = toAddress(tokenId); final var contract = testWeb3jService.deploy(RedirectTestContract::deploy); final var functionCall = contract.send_allowanceRedirect( tokenAddress.toHexString(), SENDER_ALIAS.toHexString(), SPENDER_ALIAS.toHexString()); @@ -829,12 +827,13 @@ void ethCallSymbolRedirect() { @Test void ethCallBalanceOfRedirect() { final var owner = accountPersist(); - final var tokenEntity = fungibleTokenPersistWithTreasuryAccount(owner); + final var token = fungibleTokenPersistWithTreasuryAccount(owner); + final var tokenId = token.getTokenId(); domainBuilder .tokenAccount() - .customize(e -> e.accountId(owner.getId()).tokenId(tokenEntity.getTokenId())) + .customize(e -> e.accountId(owner.getId()).tokenId(tokenId)) .persist(); - final var tokenAddress = toAddress(tokenEntity.getTokenId()); + final var tokenAddress = toAddress(tokenId); final var contract = testWeb3jService.deploy(RedirectTestContract::deploy); final var functionCall = contract.send_balanceOfRedirect( tokenAddress.toHexString(), toAddress(owner).toHexString()); @@ -844,13 +843,14 @@ void ethCallBalanceOfRedirect() { @Test void ethCallBalanceOfWithAliasRedirect() { final var owner = senderEntityPersistWithAlias(); - final var tokenEntity = fungibleTokenPersistWithTreasuryAccount(owner); + final var token = fungibleTokenPersistWithTreasuryAccount(owner); + final var tokenId = token.getTokenId(); domainBuilder .tokenAccount() - .customize(e -> e.accountId(owner.getId()).tokenId(tokenEntity.getTokenId())) + .customize(e -> e.accountId(owner.getId()).tokenId(tokenId)) .persist(); - final var tokenAddress = toAddress(tokenEntity.getTokenId()); + final var tokenAddress = toAddress(tokenId); final var contract = testWeb3jService.deploy(RedirectTestContract::deploy); final var functionCall = contract.send_balanceOfRedirect(tokenAddress.toHexString(), SENDER_ALIAS.toHexString()); @@ -973,8 +973,8 @@ void decimalsNegativeModularizedServices() throws InvocationTargetException, Ill @Test void ownerOfNegative() { // Given - final var tokenEntity = fungibleTokenPersist(); - final var tokenAddress = toAddress(tokenEntity.getTokenId()); + final var token = fungibleTokenPersist(); + final var tokenAddress = toAddress(token.getTokenId()); final var contract = testWeb3jService.deploy(ERCTestContract::deploy); // When final var functionCall = contract.send_getOwnerOf(tokenAddress.toHexString(), BigInteger.ONE); @@ -985,8 +985,8 @@ void ownerOfNegative() { @Test void tokenURINegative() { // Given - final var tokenEntity = fungibleTokenPersist(); - final var tokenAddress = toAddress(tokenEntity.getTokenId()); + final var token = fungibleTokenPersist(); + final var tokenAddress = toAddress(token.getTokenId()); final var contract = testWeb3jService.deploy(ERCTestContract::deploy); // When final var functionCall = contract.send_tokenURI(tokenAddress.toHexString(), BigInteger.ONE); @@ -1017,8 +1017,8 @@ void decimalsNegativeRedirect() { @Test void ownerOfNegativeRedirect() { // Given - final var tokenEntity = fungibleTokenPersist(); - final var tokenAddress = toAddress(tokenEntity.getTokenId()); + final var token = fungibleTokenPersist(); + final var tokenAddress = toAddress(token.getTokenId()); final var contract = testWeb3jService.deploy(RedirectTestContract::deploy); // When final var functionCall = contract.send_getOwnerOfRedirect(tokenAddress.toHexString(), BigInteger.ONE); @@ -1037,8 +1037,8 @@ void ownerOfNegativeRedirect() { @Test void tokenURINegativeRedirect() { // Given - final var tokenEntity = fungibleTokenPersist(); - final var tokenAddress = toAddress(tokenEntity.getTokenId()); + final var token = fungibleTokenPersist(); + final var tokenAddress = toAddress(token.getTokenId()); final var contract = testWeb3jService.deploy(RedirectTestContract::deploy); // When final var functionCall = contract.send_tokenURIRedirect(tokenAddress.toHexString(), BigInteger.ONE); diff --git a/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/ContractCallServicePrecompileModificationTest.java b/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/ContractCallServicePrecompileModificationTest.java index c35d5d09634..d41c2f53270 100644 --- a/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/ContractCallServicePrecompileModificationTest.java +++ b/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/ContractCallServicePrecompileModificationTest.java @@ -24,6 +24,7 @@ import static com.hedera.mirror.web3.utils.ContractCallTestUtil.ZERO_VALUE; import static com.hedera.mirror.web3.utils.ContractCallTestUtil.isWithinExpectedGasRange; import static com.hedera.mirror.web3.utils.ContractCallTestUtil.longValueOf; +import static com.hedera.services.utils.EntityIdUtils.asHexedEvmAddress; import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.INVALID_TOKEN_ID; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; @@ -55,7 +56,6 @@ import com.hedera.mirror.web3.web3j.generated.ModificationPrecompileTestContract.TransferList; import com.hedera.services.store.contracts.precompile.codec.KeyValueWrapper.KeyValueType; import com.hedera.services.store.models.Id; -import com.hedera.services.utils.EntityIdUtils; import com.hederahashgraph.api.proto.java.Key.KeyCase; import java.math.BigInteger; import java.nio.charset.StandardCharsets; @@ -81,17 +81,17 @@ void transferFrom() throws Exception { final var recipient = accountEntityWithEvmAddressPersist(); final var tokenEntity = tokenEntityPersist(); - + final var tokenId = tokenEntity.getId(); fungibleTokenPersist(tokenEntity, owner); - tokenAccountPersist(tokenEntity, spender); - tokenAccountPersist(tokenEntity, recipient); + tokenAccountPersist(tokenId, spender.getId()); + tokenAccountPersist(tokenId, recipient.getId()); final var contract = testWeb3jService.deploy(ModificationPrecompileTestContract::deploy); final var contractAddress = Address.fromHexString(contract.getContractAddress()); final var contractEntityId = entityIdFromEvmAddress(contractAddress); - tokenAccount(ta -> ta.tokenId(tokenEntity.getId()).accountId(contractEntityId.getId())); + tokenAccount(ta -> ta.tokenId(tokenId).accountId(contractEntityId.getId())); tokenAllowancePersist(10L, tokenEntity, spender, contractEntityId); @@ -112,19 +112,20 @@ void transferFrom() throws Exception { void approve(final BigInteger allowance) throws Exception { // Given final var spender = accountEntityPersist(); - final var tokenEntity = persistFungibleToken(); + final var token = fungibleTokenPersist(); + final var tokenId = token.getTokenId(); - tokenAccountPersist(tokenEntity, spender); + tokenAccountPersist(token.getTokenId(), spender.getId()); final var contract = testWeb3jService.deploy(ModificationPrecompileTestContract::deploy); final var contractAddress = Address.fromHexString(contract.getContractAddress()); final var contractEntityId = entityIdFromEvmAddress(contractAddress); - tokenAccount(ta -> ta.tokenId(tokenEntity.getId()).accountId(contractEntityId.getId())); + tokenAccount(ta -> ta.tokenId(tokenId).accountId(contractEntityId.getId())); // When - final var functionCall = contract.call_approveExternal( - getAddressFromEntity(tokenEntity), getAddressFromEntity(spender), allowance); + final var functionCall = + contract.call_approveExternal(asHexedEvmAddress(tokenId), getAddressFromEntity(spender), allowance); // Then verifyEthCallAndEstimateGas(functionCall, contract, ZERO_VALUE); @@ -139,17 +140,18 @@ void approveNFT(final Boolean approve) throws Exception { final var spender = accountEntityPersist(); final var tokenEntity = tokenEntityPersist(); + final var tokenId = tokenEntity.getId(); - Token token = nonFungibleTokenPersist(tokenEntity); + var token = nonFungibleTokenPersist(tokenEntity); - tokenAccountPersist(tokenEntity, owner); - tokenAccountPersist(tokenEntity, spender); + tokenAccountPersist(tokenId, owner.getId()); + tokenAccountPersist(tokenId, spender.getId()); final var contract = testWeb3jService.deploy(ModificationPrecompileTestContract::deploy); final var contractAddress = Address.fromHexString(contract.getContractAddress()); final var contractEntityId = entityIdFromEvmAddress(contractAddress); - tokenAccount(ta -> ta.tokenId(tokenEntity.getId()).accountId(contractEntityId.getId())); + tokenAccount(ta -> ta.tokenId(tokenId).accountId(contractEntityId.getId())); nonFungibleTokenInstancePersist(token, 1L, contractEntityId, spender.toEntityId()); @@ -176,7 +178,7 @@ void setApprovalForAll() throws Exception { .customize(t -> t.tokenId(tokenEntity.getId()).type(TokenTypeEnum.NON_FUNGIBLE_UNIQUE)) .persist(); - tokenAccountPersist(tokenEntity, spender); + tokenAccountPersist(tokenEntity.getId(), spender.getId()); final var contract = testWeb3jService.deploy(ModificationPrecompileTestContract::deploy); @@ -315,7 +317,7 @@ void mintFungibleToken() throws Exception { .type(TokenTypeEnum.FUNGIBLE_COMMON) .treasuryAccountId(treasury.toEntityId())) .persist(); - tokenAccountPersist(tokenEntity, treasury); + tokenAccountPersist(tokenEntity.getId(), treasury.getId()); final var totalSupply = token.getTotalSupply(); @@ -340,7 +342,7 @@ void mintNFT() throws Exception { nonFungibleTokenPersist(tokenEntity, treasury); - tokenAccountPersist(tokenEntity, treasury); + tokenAccountPersist(tokenEntity.getId(), treasury.getId()); final var contract = testWeb3jService.deploy(ModificationPrecompileTestContract::deploy); @@ -362,16 +364,15 @@ void mintNFT() throws Exception { @Test void burnFungibleToken() throws Exception { // Given - final var tokenEntity = tokenEntityPersist(); final var treasuryAccount = accountEntityPersist(); - final var token = fungibleTokenPersist(tokenEntity, treasuryAccount); - tokenAccountPersist(tokenEntity, treasuryAccount); + final var token = fungibleTokenPersistWithTreasuryAccount(treasuryAccount.toEntityId()); + final var tokenId = token.getTokenId(); + tokenAccountPersist(tokenId, treasuryAccount.getId()); final var sender = accountEntityPersist(); accountBalanceRecordsPersist(sender); - long balanceTimestamp = treasuryAccount.getBalanceTimestamp(); - persistTokenBalance(treasuryAccount, tokenEntity, balanceTimestamp); + tokenBalancePersist(treasuryAccount.toEntityId(), EntityId.of(tokenId), treasuryAccount.getBalanceTimestamp()); testWeb3jService.setSender(getAddressFromEntity(sender)); @@ -380,8 +381,8 @@ void burnFungibleToken() throws Exception { final var contract = testWeb3jService.deploy(ModificationPrecompileTestContract::deploy); // When - final var functionCall = contract.call_burnTokenExternal( - getAddressFromEntity(tokenEntity), BigInteger.valueOf(4), new ArrayList<>()); + final var functionCall = + contract.call_burnTokenExternal(asHexedEvmAddress(tokenId), BigInteger.valueOf(4), new ArrayList<>()); final var result = functionCall.send(); @@ -403,11 +404,10 @@ void burnNFT() throws Exception { .treasuryAccountId(treasury.toEntityId())) .persist(); - tokenAccountPersist(tokenEntity, treasury); + tokenAccountPersist(tokenEntity.getId(), treasury.getId()); final var totalSupply = token.getTotalSupply(); - long balanceTimestamp = treasury.getBalanceTimestamp(); - persistTokenBalance(treasury, tokenEntity, balanceTimestamp); + tokenBalancePersist(treasury.toEntityId(), tokenEntity.toEntityId(), treasury.getBalanceTimestamp()); Nft nft = domainBuilder .nft() @@ -450,11 +450,11 @@ void wipeFungibleToken() throws Exception { final var tokenEntity = tokenEntityPersist(); fungibleTokenPersist(tokenEntity, treasuryEntity); - tokenAccountPersist(tokenEntity, owner); + tokenAccountPersist(tokenEntity.getId(), owner.getId()); Long createdTimestamp = owner.getCreatedTimestamp(); - persistTokenBalance(owner, tokenEntity, createdTimestamp); - persistAccountBalance(treasuryEntity, treasuryEntity.getBalance(), createdTimestamp); + tokenBalancePersist(owner.toEntityId(), tokenEntity.toEntityId(), createdTimestamp); + accountBalancePersist(treasuryEntity, createdTimestamp); final var contract = testWeb3jService.deploy(ModificationPrecompileTestContract::deploy); @@ -480,7 +480,7 @@ void wipeNFT() throws Exception { .treasuryAccountId(tokenTreasury.toEntityId())) .persist(); - tokenAccountPersist(tokenEntity, owner); + tokenAccountPersist(tokenEntity.getId(), owner.getId()); domainBuilder .nft() .customize(n -> n.tokenId(tokenEntity.getId()).serialNumber(1L).accountId(owner.toEntityId())) @@ -508,7 +508,7 @@ void grantTokenKyc() throws Exception { .customize(t -> t.tokenId(tokenEntity.getId()).type(TokenTypeEnum.FUNGIBLE_COMMON)) .persist(); - tokenAccountPersist(tokenEntity, accountWithoutGrant); + tokenAccountPersist(tokenEntity.getId(), accountWithoutGrant.getId()); final var contract = testWeb3jService.deploy(ModificationPrecompileTestContract::deploy); @@ -532,7 +532,7 @@ void revokeTokenKyc() throws Exception { .customize(t -> t.tokenId(tokenEntity.getId()).type(TokenTypeEnum.FUNGIBLE_COMMON)) .persist(); - tokenAccountPersist(tokenEntity, accountWithGrant); + tokenAccountPersist(tokenEntity.getId(), accountWithGrant.getId()); final var contract = testWeb3jService.deploy(ModificationPrecompileTestContract::deploy); @@ -567,14 +567,15 @@ void deleteToken() throws Exception { void freezeToken() throws Exception { // Given final var accountWithoutFreeze = accountEntityWithEvmAddressPersist(); - final var tokenEntity = persistFungibleToken(); - tokenAccountPersist(tokenEntity, accountWithoutFreeze); + final var token = fungibleTokenPersist(); + final var tokenId = token.getTokenId(); + tokenAccountPersist(tokenId, accountWithoutFreeze.getId()); final var contract = testWeb3jService.deploy(ModificationPrecompileTestContract::deploy); // When - final var functionCall = contract.call_freezeTokenExternal( - getAddressFromEntity(tokenEntity), getAliasFromEntity(accountWithoutFreeze)); + final var functionCall = + contract.call_freezeTokenExternal(asHexedEvmAddress(tokenId), getAliasFromEntity(accountWithoutFreeze)); // Then verifyEthCallAndEstimateGas(functionCall, contract, ZERO_VALUE); @@ -586,11 +587,12 @@ void unfreezeToken() throws Exception { // Given final var accountWithFreeze = accountEntityWithEvmAddressPersist(); - final var tokenEntity = persistFungibleToken(); + final var token = fungibleTokenPersist(); + final var tokenId = token.getTokenId(); domainBuilder .tokenAccount() - .customize(ta -> ta.tokenId(tokenEntity.getId()) + .customize(ta -> ta.tokenId(tokenId) .accountId(accountWithFreeze.getId()) .kycStatus(TokenKycStatusEnum.GRANTED) .freezeStatus(TokenFreezeStatusEnum.FROZEN) @@ -601,8 +603,8 @@ void unfreezeToken() throws Exception { final var contract = testWeb3jService.deploy(ModificationPrecompileTestContract::deploy); // When - final var functionCall = contract.call_unfreezeTokenExternal( - getAddressFromEntity(tokenEntity), getAliasFromEntity(accountWithFreeze)); + final var functionCall = + contract.call_unfreezeTokenExternal(asHexedEvmAddress(tokenId), getAliasFromEntity(accountWithFreeze)); // Then verifyEthCallAndEstimateGas(functionCall, contract, ZERO_VALUE); @@ -641,7 +643,7 @@ void unpauseToken() throws Exception { .pauseStatus(TokenPauseStatusEnum.PAUSED)) .persist(); - tokenAccountPersist(tokenEntity, sender); + tokenAccountPersist(tokenEntity.getId(), sender.getId()); final var contract = testWeb3jService.deploy(ModificationPrecompileTestContract::deploy); @@ -705,10 +707,11 @@ void createFungibleTokenWithCustomFees() throws Exception { final var treasuryAccount = accountEntityPersist(); - final var tokenForDenomination = persistFungibleToken(); + final var tokenForDenomination = fungibleTokenPersist(); final var feeCollector = accountEntityWithEvmAddressPersist(); + final var tokenId = tokenForDenomination.getTokenId(); - tokenAccountPersist(tokenForDenomination, feeCollector); + tokenAccountPersist(tokenId, feeCollector.getId()); final var contract = testWeb3jService.deploy(ModificationPrecompileTestContract::deploy); @@ -719,11 +722,7 @@ void createFungibleTokenWithCustomFees() throws Exception { contract.getContractAddress(), TokenTypeEnum.FUNGIBLE_COMMON, treasuryAccount.toEntityId()); final var fixedFee = new FixedFee( - BigInteger.valueOf(100L), - getAddressFromEntityId(tokenForDenomination.toEntityId()), - false, - false, - getAliasFromEntity(feeCollector)); + BigInteger.valueOf(100L), asHexedEvmAddress(tokenId), false, false, getAliasFromEntity(feeCollector)); final var fractionalFee = new FractionalFee( BigInteger.valueOf(1L), BigInteger.valueOf(100L), @@ -795,10 +794,11 @@ void createNonFungibleTokenWithCustomFees() throws Exception { accountBalanceRecordsPersist(sender); - final var tokenForDenomination = persistFungibleToken(); + final var tokenForDenomination = fungibleTokenPersist(); final var feeCollector = accountEntityWithEvmAddressPersist(); + final var tokenId = tokenForDenomination.getTokenId(); - tokenAccountPersist(tokenForDenomination, feeCollector); + tokenAccountPersist(tokenId, feeCollector.getId()); final var contract = testWeb3jService.deploy(ModificationPrecompileTestContract::deploy); @@ -811,18 +811,13 @@ void createNonFungibleTokenWithCustomFees() throws Exception { .persist(); final var token = populateHederaToken( contract.getContractAddress(), TokenTypeEnum.NON_FUNGIBLE_UNIQUE, treasuryAccount.toEntityId()); - final var fixedFee = new FixedFee( - BigInteger.valueOf(100L), - getAddressFromEntityId(tokenForDenomination.toEntityId()), - false, - false, - getAliasFromEntity(feeCollector)); + BigInteger.valueOf(100L), asHexedEvmAddress(tokenId), false, false, getAliasFromEntity(feeCollector)); final var royaltyFee = new RoyaltyFee( BigInteger.valueOf(1L), BigInteger.valueOf(100L), BigInteger.valueOf(10L), - getAddressFromEntity(tokenForDenomination), + asHexedEvmAddress(tokenId), false, getAliasFromEntity(feeCollector)); @@ -848,17 +843,18 @@ void createNonFungibleTokenWithCustomFees() throws Exception { void create2ContractAndTransferFromIt() throws Exception { // Given final var receiver = accountEntityWithEvmAddressPersist(); - final var token = persistFungibleToken(); + final var token = fungibleTokenPersist(); + final var tokenId = token.getTokenId(); final var sponsor = accountEntityWithEvmAddressPersist(); - persistTokenBalance(sponsor, token, sponsor.getCreatedTimestamp()); + tokenBalancePersist(sponsor.toEntityId(), EntityId.of(tokenId), sponsor.getCreatedTimestamp()); accountBalanceRecordsPersist(sponsor); - tokenAccountPersist(token, sponsor); + tokenAccountPersist(tokenId, sponsor.getId()); final var contract = testWeb3jService.deploy(ModificationPrecompileTestContract::deploy); // When final var functionCall = contract.call_createContractViaCreate2AndTransferFromIt( - getAddressFromEntity(token), + asHexedEvmAddress(tokenId), getAliasFromEntity(sponsor), getAliasFromEntity(receiver), BigInteger.valueOf(10L)); @@ -871,14 +867,14 @@ void create2ContractAndTransferFromIt() throws Exception { @Test void notExistingPrecompileCall() throws Exception { // Given - final var token = persistFungibleToken(); + final var token = fungibleTokenPersist(); final var contract = testWeb3jService.deploy(ModificationPrecompileTestContract::deploy); // Then if (mirrorNodeEvmProperties.isModularizedServices()) { - final var modularizedCall = contract.call_callNotExistingPrecompile(getAddressFromEntity(token)); + final var modularizedCall = contract.call_callNotExistingPrecompile(asHexedEvmAddress(token.getTokenId())); assertThat(Bytes.wrap(modularizedCall.send())).isEqualTo(Bytes.EMPTY); } else { - final var functionCall = contract.send_callNotExistingPrecompile(getAddressFromEntity(token)); + final var functionCall = contract.send_callNotExistingPrecompile(asHexedEvmAddress(token.getTokenId())); assertThatThrownBy(functionCall::send) .isInstanceOf(MirrorEvmTransactionException.class) .hasMessage(INVALID_TOKEN_ID.name()); @@ -1097,17 +1093,15 @@ void transferToken(final String type) throws Exception { final var receiver = accountEntityWithEvmAddressPersist(); // Create token-account associations so sender and receiver can operate with the token - tokenAccountPersist(tokenEntity, sender); - tokenAccountPersist(tokenEntity, receiver); + tokenAccountPersist(tokenEntity.getId(), sender.getId()); + tokenAccountPersist(tokenEntity.getId(), receiver.getId()); accountBalanceRecordsPersist(sender.toEntityId(), sender.getCreatedTimestamp(), sender.getBalance()); accountBalanceRecordsPersist(receiver.toEntityId(), receiver.getCreatedTimestamp(), receiver.getBalance()); - long senderBalanceTimestamp = sender.getBalanceTimestamp(); - persistTokenBalance(sender, tokenEntity, senderBalanceTimestamp); + tokenBalancePersist(sender.toEntityId(), tokenEntity.toEntityId(), sender.getBalanceTimestamp()); - long receiverBalanceTimestamp = receiver.getBalanceTimestamp(); - persistTokenBalance(receiver, tokenEntity, receiverBalanceTimestamp); + tokenBalancePersist(receiver.toEntityId(), tokenEntity.toEntityId(), receiver.getBalanceTimestamp()); final var contract = testWeb3jService.deploy(ModificationPrecompileTestContract::deploy); // When @@ -1145,21 +1139,22 @@ void transferNft(final String type) throws Exception { final var tokenEntity = tokenEntityPersist(); final var treasuryAccount = accountEntityPersist(); + final var tokenId = tokenEntity.getId(); accountBalanceRecordsPersist(sender); - Token token = nonFungibleTokenPersist(tokenEntity, treasuryAccount); + var token = nonFungibleTokenPersist(tokenEntity, treasuryAccount); domainBuilder .nft() - .customize(n -> n.tokenId(tokenEntity.getId()).serialNumber(1L).accountId(sender.toEntityId())) + .customize(n -> n.tokenId(tokenId).serialNumber(1L).accountId(sender.toEntityId())) .persist(); final var receiver = accountEntityWithEvmAddressPersist(); nftAllowancePersist(token, sender, getEntity(contractId), sender); - tokenAccountPersist(tokenEntity, sender); - tokenAccountPersist(tokenEntity, receiver); + tokenAccountPersist(tokenId, sender.getId()); + tokenAccountPersist(tokenId, receiver.getId()); // When testWeb3jService.setSender(getAliasFromEntity(sender)); @@ -1209,8 +1204,8 @@ void transferFromNft() throws Exception { nftAllowancePersist(token, sender, getEntity(contractId), sender); - tokenAccountPersist(tokenEntity, sender); - tokenAccountPersist(tokenEntity, receiver); + tokenAccountPersist(tokenEntity.getId(), sender.getId()); + tokenAccountPersist(tokenEntity.getId(), receiver.getId()); // When testWeb3jService.setSender(getAliasFromEntity(sender)); @@ -1240,9 +1235,9 @@ void cryptoTransferHbars() throws Exception { final var payer = accountEntityWithEvmAddressPersist(); long timestampForBalances = payer.getCreatedTimestamp(); - persistAccountBalance(payer, payer.getBalance()); - persistAccountBalance(sender, sender.getBalance(), timestampForBalances); - persistAccountBalance(treasuryEntity, treasuryEntity.getBalance(), timestampForBalances); + accountBalancePersist(payer, timestampForBalances); + accountBalancePersist(sender, timestampForBalances); + accountBalancePersist(treasuryEntity, timestampForBalances); // When testWeb3jService.setSender(getAliasFromEntity(payer)); @@ -1264,27 +1259,29 @@ void cryptoTransferToken() throws Exception { // Given final var contract = testWeb3jService.deploy(ModificationPrecompileTestContract::deploy); final var sender = accountEntityWithEvmAddressPersist(); - final var tokenEntity = persistFungibleToken(); + final var token = fungibleTokenPersist(); final var receiver = accountEntityWithEvmAddressPersist(); final var payer = accountEntityWithEvmAddressPersist(); + final var tokenId = token.getTokenId(); long timestampForBalances = payer.getCreatedTimestamp(); - persistAccountBalance(payer, payer.getBalance()); - persistTokenBalance(payer, tokenEntity, timestampForBalances); + final var entity = EntityId.of(tokenId); + accountBalancePersist(payer, timestampForBalances); + tokenBalancePersist(payer.toEntityId(), entity, timestampForBalances); - persistAccountBalance(sender, sender.getBalance(), timestampForBalances); - persistTokenBalance(sender, tokenEntity, timestampForBalances); + accountBalancePersist(sender, timestampForBalances); + tokenBalancePersist(sender.toEntityId(), entity, timestampForBalances); - persistTokenBalance(receiver, tokenEntity, timestampForBalances); - persistAccountBalance(treasuryEntity, treasuryEntity.getBalance(), timestampForBalances); + tokenBalancePersist(receiver.toEntityId(), entity, timestampForBalances); + accountBalancePersist(treasuryEntity, timestampForBalances); - tokenAccountPersist(tokenEntity, sender); - tokenAccountPersist(tokenEntity, receiver); - tokenAccountPersist(tokenEntity, payer); + tokenAccountPersist(tokenId, sender.getId()); + tokenAccountPersist(tokenId, receiver.getId()); + tokenAccountPersist(tokenId, payer.getId()); // When testWeb3jService.setSender(getAliasFromEntity(payer)); final var tokenTransferList = new TokenTransferList( - getAddressFromEntity(tokenEntity), + asHexedEvmAddress(tokenId), List.of( new AccountAmount(getAliasFromEntity(sender), BigInteger.valueOf(5L), false), new AccountAmount(getAliasFromEntity(receiver), BigInteger.valueOf(-5L), false)), @@ -1305,24 +1302,26 @@ void cryptoTransferHbarsAndToken() throws Exception { // Given final var contract = testWeb3jService.deploy(ModificationPrecompileTestContract::deploy); final var sender = accountEntityWithEvmAddressPersist(); - final var tokenEntity = persistFungibleToken(); + final var tokenEntity = fungibleTokenPersist(); final var receiver = accountEntityWithEvmAddressPersist(); final var payer = accountEntityWithEvmAddressPersist(); - long timestampForBalances = payer.getCreatedTimestamp(); - persistAccountBalance(payer, payer.getBalance()); - persistTokenBalance(payer, tokenEntity, timestampForBalances); + final var tokenId = tokenEntity.getTokenId(); + final var entity = EntityId.of(tokenId); - persistAccountBalance(sender, sender.getBalance(), timestampForBalances); - persistTokenBalance(sender, tokenEntity, timestampForBalances); + accountBalancePersist(payer, timestampForBalances); + tokenBalancePersist(payer.toEntityId(), entity, timestampForBalances); - persistTokenBalance(receiver, tokenEntity, timestampForBalances); + accountBalancePersist(sender, timestampForBalances); + tokenBalancePersist(sender.toEntityId(), entity, timestampForBalances); - persistAccountBalance(treasuryEntity, treasuryEntity.getBalance(), timestampForBalances); + tokenBalancePersist(receiver.toEntityId(), entity, timestampForBalances); - tokenAccountPersist(tokenEntity, sender); - tokenAccountPersist(tokenEntity, receiver); - tokenAccountPersist(tokenEntity, payer); + accountBalancePersist(treasuryEntity, timestampForBalances); + + tokenAccountPersist(tokenId, sender.getId()); + tokenAccountPersist(tokenId, receiver.getId()); + tokenAccountPersist(tokenId, payer.getId()); // When testWeb3jService.setSender(getAliasFromEntity(payer)); @@ -1331,7 +1330,7 @@ void cryptoTransferHbarsAndToken() throws Exception { new AccountAmount(getAliasFromEntity(receiver), BigInteger.valueOf(5L), false))); final var tokenTransferList = new TokenTransferList( - getAddressFromEntity(tokenEntity), + asHexedEvmAddress(tokenId), List.of( new AccountAmount(getAliasFromEntity(sender), BigInteger.valueOf(5L), false), new AccountAmount(getAliasFromEntity(receiver), BigInteger.valueOf(-5L), false)), @@ -1355,21 +1354,22 @@ void cryptoTransferNft() throws Exception { final var receiver = accountEntityWithEvmAddressPersist(); final var payer = accountEntityWithEvmAddressPersist(); final var tokenEntity = tokenEntityPersist(); + final var tokenId = tokenEntity.getId(); accountBalanceRecordsPersist(payer); domainBuilder .token() - .customize(t -> t.tokenId(tokenEntity.getId()) + .customize(t -> t.tokenId(tokenId) .type(TokenTypeEnum.NON_FUNGIBLE_UNIQUE) .treasuryAccountId(tokenTreasury.toEntityId())) .persist(); domainBuilder .nft() - .customize(n -> n.tokenId(tokenEntity.getId()).serialNumber(1L).accountId(sender.toEntityId())) + .customize(n -> n.tokenId(tokenId).serialNumber(1L).accountId(sender.toEntityId())) .persist(); - tokenAccountPersist(tokenEntity, payer); - tokenAccountPersist(tokenEntity, sender); - tokenAccountPersist(tokenEntity, receiver); + tokenAccountPersist(tokenId, payer.getId()); + tokenAccountPersist(tokenId, sender.getId()); + tokenAccountPersist(tokenId, receiver.getId()); // When testWeb3jService.setSender(getAliasFromEntity(payer)); @@ -1540,16 +1540,6 @@ private KeyValue getKeyValueForType(final KeyValueType keyValueType, String cont }; } - private Entity persistFungibleToken() { - final var tokenEntity = tokenEntityPersist(); - domainBuilder - .token() - .customize(t -> t.tokenId(tokenEntity.getId()).type(TokenTypeEnum.FUNGIBLE_COMMON)) - .persist(); - - return tokenEntity; - } - private HederaToken convertTokenEntityToHederaToken(final Token token) { final var tokenEntity = domainBuilder.entity().customize(e -> e.id(token.getTokenId())).get(); @@ -1561,7 +1551,7 @@ private HederaToken convertTokenEntityToHederaToken(final Token token) { token.getName(), token.getSymbol(), treasuryAccountId != null - ? EntityIdUtils.asHexedEvmAddress(new Id( + ? asHexedEvmAddress(new Id( treasuryAccountId.getShard(), treasuryAccountId.getRealm(), treasuryAccountId.getNum())) : Address.ZERO.toHexString(), new String(token.getMetadata(), StandardCharsets.UTF_8), @@ -1571,7 +1561,7 @@ private HederaToken convertTokenEntityToHederaToken(final Token token) { keys, new Expiry( BigInteger.valueOf(tokenEntity.getEffectiveExpiration()), - EntityIdUtils.asHexedEvmAddress(new Id(0, 0, entityRenewAccountId)), + asHexedEvmAddress(new Id(0, 0, entityRenewAccountId)), BigInteger.valueOf(tokenEntity.getEffectiveExpiration()))); } @@ -1583,7 +1573,7 @@ private HederaToken convertTokenEntityToHederaToken(final Token token, final Ent return new HederaToken( token.getName(), token.getSymbol(), - EntityIdUtils.asHexedEvmAddress( + asHexedEvmAddress( new Id(treasuryAccountId.getShard(), treasuryAccountId.getRealm(), treasuryAccountId.getNum())), new String(token.getMetadata(), StandardCharsets.UTF_8), token.getSupplyType().equals(TokenSupplyTypeEnum.FINITE), @@ -1592,7 +1582,7 @@ private HederaToken convertTokenEntityToHederaToken(final Token token, final Ent keys, new Expiry( BigInteger.valueOf(entity.getEffectiveExpiration()), - EntityIdUtils.asHexedEvmAddress(new Id(0, 0, entityRenewAccountId)), + asHexedEvmAddress(new Id(0, 0, entityRenewAccountId)), BigInteger.valueOf(entity.getEffectiveExpiration()))); } } diff --git a/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/ContractCallServicePrecompileReadonlyTest.java b/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/ContractCallServicePrecompileReadonlyTest.java index 18abfa72d50..7ce7d6b009f 100644 --- a/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/ContractCallServicePrecompileReadonlyTest.java +++ b/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/ContractCallServicePrecompileReadonlyTest.java @@ -31,11 +31,14 @@ import static com.hedera.mirror.web3.web3j.generated.PrecompileTestContract.Expiry; import static com.hedera.mirror.web3.web3j.generated.PrecompileTestContract.HederaToken; import static com.hedera.mirror.web3.web3j.generated.PrecompileTestContract.TokenKey; +import static com.hedera.services.utils.EntityIdUtils.asHexedEvmAddress; +import static com.hedera.services.utils.EntityIdUtils.asTypedEvmAddress; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; import com.google.protobuf.InvalidProtocolBufferException; import com.hedera.mirror.common.domain.entity.Entity; +import com.hedera.mirror.common.domain.entity.EntityId; import com.hedera.mirror.common.domain.entity.EntityType; import com.hedera.mirror.common.domain.token.CustomFee; import com.hedera.mirror.common.domain.token.FallbackFee; @@ -96,11 +99,12 @@ void unsupportedPrecompileFails() { @Test void hrcIsAssociatedFails() throws Exception { // Given - final var token = persistFungibleToken(); + final var token = fungibleTokenPersist(); + final var contract = testWeb3jService.deploy(PrecompileTestContract::deploy); // When - final var functionCall = contract.call_hrcIsAssociated(getAddressFromEntity(token)); + final var functionCall = contract.call_hrcIsAssociated(asHexedEvmAddress(token.getTokenId())); // Then if (mirrorNodeEvmProperties.isModularizedServices()) { @@ -181,14 +185,14 @@ void isTokenFrozenWithAlias() throws Exception { void isKycGranted() throws Exception { // Given final var account = accountEntityPersist(); - final var tokenEntity = persistFungibleToken(); - tokenAccountPersist(tokenEntity, account); + final var token = fungibleTokenPersist(); + final var tokenId = token.getTokenId(); + tokenAccountPersist(tokenId, account.getId()); final var contract = testWeb3jService.deploy(PrecompileTestContract::deploy); // When - final var functionCall = - contract.call_isKycGranted(getAddressFromEntity(tokenEntity), getAddressFromEntity(account)); + final var functionCall = contract.call_isKycGranted(asHexedEvmAddress(tokenId), getAddressFromEntity(account)); // Then assertThat(functionCall.send()).isTrue(); @@ -204,14 +208,14 @@ void isKycGrantedWithAlias() throws Exception { .alias(SENDER_PUBLIC_KEY.toByteArray()) .evmAddress(SENDER_ALIAS.toArray())) .persist(); - final var tokenEntity = persistFungibleToken(); - tokenAccountPersist(tokenEntity, account); + final var token = fungibleTokenPersist(); + final var tokenId = token.getTokenId(); + tokenAccountPersist(tokenId, account.getId()); final var contract = testWeb3jService.deploy(PrecompileTestContract::deploy); // When - final var functionCall = - contract.call_isKycGranted(getAddressFromEntity(tokenEntity), getAliasFromEntity(account)); + final var functionCall = contract.call_isKycGranted(asHexedEvmAddress(tokenId), getAliasFromEntity(account)); // Then assertThat(functionCall.send()).isTrue(); @@ -224,7 +228,7 @@ void isKycGrantedForNFT() throws Exception { // Given final var account = accountEntityPersist(); final var tokenEntity = persistNft(); - tokenAccountPersist(tokenEntity, account); + tokenAccountPersist(tokenEntity.getId(), account.getId()); final var contract = testWeb3jService.deploy(PrecompileTestContract::deploy); @@ -248,7 +252,7 @@ void isKycGrantedForNFTWithAlias() throws Exception { .evmAddress(SENDER_ALIAS.toArray())) .persist(); final var tokenEntity = persistNft(); - tokenAccountPersist(tokenEntity, account); + tokenAccountPersist(tokenEntity.getId(), account.getId()); final var contract = testWeb3jService.deploy(PrecompileTestContract::deploy); @@ -265,12 +269,12 @@ void isKycGrantedForNFTWithAlias() throws Exception { @Test void isTokenAddress() throws Exception { // Given - final var tokenEntity = persistFungibleToken(); + final var token = fungibleTokenPersist(); final var contract = testWeb3jService.deploy(PrecompileTestContract::deploy); // When - final var functionCall = contract.call_isTokenAddress(getAddressFromEntity(tokenEntity)); + final var functionCall = contract.call_isTokenAddress(asHexedEvmAddress(token.getTokenId())); // Then assertThat(functionCall.send()).isTrue(); @@ -346,12 +350,12 @@ void getDefaultKycNFT() throws Exception { @Test void getTokenType() throws Exception { // Given - final var tokenEntity = persistFungibleToken(); + final var token = fungibleTokenPersist(); final var contract = testWeb3jService.deploy(PrecompileTestContract::deploy); // When - final var functionCall = contract.call_getType(getAddressFromEntity(tokenEntity)); + final var functionCall = contract.call_getType(asHexedEvmAddress(token.getTokenId())); // Then assertThat(functionCall.send()).isEqualTo(BigInteger.ZERO); @@ -501,15 +505,17 @@ void getTokenKey(final TokenTypeEnum tokenType, final KeyValueType keyValueType, void getCustomFeesForTokenWithFixedFee() throws Exception { // Given final var collectorAccount = accountEntityWithEvmAddressPersist(); - final var tokenEntity = persistFungibleToken(); + final var token = fungibleTokenPersist(); + final var tokenId = token.getTokenId(); + final var entityId = EntityId.of(tokenId); final var fixedFee = com.hedera.mirror.common.domain.token.FixedFee.builder() .amount(100L) .collectorAccountId(collectorAccount.toEntityId()) - .denominatingTokenId(tokenEntity.toEntityId()) + .denominatingTokenId(entityId) .build(); domainBuilder .customFee() - .customize(f -> f.entityId(tokenEntity.getId()) + .customize(f -> f.entityId(tokenId) .fixedFees(List.of(fixedFee)) .fractionalFees(List.of()) .royaltyFees(List.of())) @@ -518,11 +524,11 @@ void getCustomFeesForTokenWithFixedFee() throws Exception { final var contract = testWeb3jService.deploy(PrecompileTestContract::deploy); // When - final var functionCall = contract.call_getCustomFeesForToken(getAddressFromEntity(tokenEntity)); + final var functionCall = contract.call_getCustomFeesForToken(asHexedEvmAddress(tokenId)); final var expectedFee = new FixedFee( BigInteger.valueOf(100L), - getAddressFromEntity(tokenEntity), + asHexedEvmAddress(tokenId), false, false, Address.fromHexString( @@ -539,7 +545,8 @@ void getCustomFeesForTokenWithFixedFee() throws Exception { void getCustomFeesForTokenWithFractionalFee() throws Exception { // Given final var collectorAccount = accountEntityWithEvmAddressPersist(); - final var tokenEntity = persistFungibleToken(); + final var token = fungibleTokenPersist(); + final var tokenId = token.getTokenId(); final var fractionalFee = FractionalFee.builder() .collectorAccountId(collectorAccount.toEntityId()) .denominator(10L) @@ -550,7 +557,7 @@ void getCustomFeesForTokenWithFractionalFee() throws Exception { .build(); domainBuilder .customFee() - .customize(f -> f.entityId(tokenEntity.getId()) + .customize(f -> f.entityId(tokenId) .fractionalFees(List.of(fractionalFee)) .fixedFees(List.of()) .royaltyFees(List.of())) @@ -559,7 +566,7 @@ void getCustomFeesForTokenWithFractionalFee() throws Exception { final var contract = testWeb3jService.deploy(PrecompileTestContract::deploy); // When - final var functionCall = contract.call_getCustomFeesForToken(getAddressFromEntity(tokenEntity)); + final var functionCall = contract.call_getCustomFeesForToken(asHexedEvmAddress(tokenId)); final var expectedFee = new PrecompileTestContract.FractionalFee( BigInteger.valueOf(100L), @@ -581,19 +588,22 @@ void getCustomFeesForTokenWithFractionalFee() throws Exception { void getCustomFeesForTokenWithRoyaltyFee() throws Exception { // Given final var collectorAccount = accountEntityWithEvmAddressPersist(); - final var tokenEntity = persistFungibleToken(); + final var token = fungibleTokenPersist(); + final var tokenId = token.getTokenId(); + final var entityId = EntityId.of(tokenId); + final var royaltyFee = RoyaltyFee.builder() .collectorAccountId(collectorAccount.toEntityId()) .denominator(10L) .fallbackFee(FallbackFee.builder() .amount(100L) - .denominatingTokenId(tokenEntity.toEntityId()) + .denominatingTokenId(entityId) .build()) .numerator(20L) .build(); domainBuilder .customFee() - .customize(f -> f.entityId(tokenEntity.getId()) + .customize(f -> f.entityId(tokenId) .royaltyFees(List.of(royaltyFee)) .fixedFees(List.of()) .fractionalFees(List.of())) @@ -602,14 +612,13 @@ void getCustomFeesForTokenWithRoyaltyFee() throws Exception { final var contract = testWeb3jService.deploy(PrecompileTestContract::deploy); // When - final var functionCall = contract.call_getCustomFeesForToken(getAddressFromEntity(tokenEntity)); + final var functionCall = contract.call_getCustomFeesForToken(asHexedEvmAddress(tokenId)); final var expectedFee = new PrecompileTestContract.RoyaltyFee( BigInteger.valueOf(20L), BigInteger.valueOf(10L), BigInteger.valueOf(100L), - EntityIdUtils.asHexedEvmAddress( - new Id(tokenEntity.getShard(), tokenEntity.getRealm(), tokenEntity.getNum())), + EntityIdUtils.asHexedEvmAddress(new Id(entityId.getShard(), entityId.getRealm(), entityId.getNum())), false, Address.fromHexString( Bytes.wrap(collectorAccount.getEvmAddress()).toHexString()) @@ -663,11 +672,12 @@ void getAllowanceForToken() throws Exception { final var amountGranted = 50L; final var owner = accountEntityWithEvmAddressPersist(); final var spender = accountEntityWithEvmAddressPersist(); - final var tokenEntity = persistFungibleToken(); + final var token = fungibleTokenPersist(); + final var tokenId = token.getTokenId(); domainBuilder .tokenAllowance() - .customize(a -> a.tokenId(tokenEntity.getId()) + .customize(a -> a.tokenId(tokenId) .owner(owner.getNum()) .spender(spender.getNum()) .amount(amountGranted) @@ -678,7 +688,7 @@ void getAllowanceForToken() throws Exception { // When final var functionCall = contract.call_htsAllowance( - getAddressFromEntity(tokenEntity), getAliasFromEntity(owner), getAliasFromEntity(spender)); + asHexedEvmAddress(tokenId), getAliasFromEntity(owner), getAliasFromEntity(spender)); // Then assertThat(functionCall.send()).isEqualTo(BigInteger.valueOf(amountGranted)); @@ -1059,16 +1069,6 @@ private KeyValue getKeyValueForType(final KeyValueType keyValueType, String cont }; } - private Entity persistFungibleToken() { - final var tokenEntity = tokenEntityPersist(); - domainBuilder - .token() - .customize(t -> t.tokenId(tokenEntity.getId()).type(TokenTypeEnum.FUNGIBLE_COMMON)) - .persist(); - - return tokenEntity; - } - private Entity persistNft() { final var tokenEntity = tokenEntityPersist(); domainBuilder @@ -1160,14 +1160,12 @@ private KeyValue getKeyValue(byte[] serializedKey) { return new KeyValue( false, key.getContractID().hasContractNum() - ? EntityIdUtils.asTypedEvmAddress(key.getContractID()) - .toHexString() + ? asTypedEvmAddress(key.getContractID()).toHexString() : Address.ZERO.toHexString(), key.getEd25519().toByteArray(), key.getECDSASecp256K1().toByteArray(), key.getDelegatableContractId().hasContractNum() - ? EntityIdUtils.asTypedEvmAddress(key.getDelegatableContractId()) - .toHexString() + ? asTypedEvmAddress(key.getDelegatableContractId()).toHexString() : Address.ZERO.toHexString()); } catch (InvalidProtocolBufferException e) { throw new IllegalArgumentException("Unable to parse key", e); diff --git a/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/ContractCallServiceTest.java b/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/ContractCallServiceTest.java index 5e210c1f537..78dcf5e5ed4 100644 --- a/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/ContractCallServiceTest.java +++ b/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/ContractCallServiceTest.java @@ -180,7 +180,7 @@ void pureCall() throws Exception { // Given final var gasUsedBeforeExecution = getGasUsedBeforeExecution(ETH_CALL); final var payer = accountEntityWithEvmAddressPersist(); - persistAccountBalance(payer, payer.getBalance()); + accountBalancePersist(payer, payer.getCreatedTimestamp()); testWeb3jService.setSender(toAddress(payer.toEntityId()).toHexString()); final var contract = testWeb3jService.deploy(EthCall::deploy); meterRegistry.clear(); // Clear it as the contract deploy increases the gas limit metric @@ -377,7 +377,7 @@ void viewCall() throws Exception { void estimateGasForViewCall() { // Given final var payer = accountEntityWithEvmAddressPersist(); - persistAccountBalance(payer, payer.getBalance()); + accountBalancePersist(payer, payer.getCreatedTimestamp()); testWeb3jService.setSender(toAddress(payer.toEntityId()).toHexString()); final var contract = testWeb3jService.deploy(EthCall::deploy); @@ -605,7 +605,7 @@ void transferNegative() { final var receiverEntity = accountPersist(); final var receiverAddress = getAliasAddressFromEntity(receiverEntity); final var payer = accountEntityWithEvmAddressPersist(); - persistAccountBalance(payer, payer.getBalance()); + accountBalancePersist(payer, payer.getCreatedTimestamp()); final var serviceParameters = getContractExecutionParametersWithValue( Bytes.EMPTY, toAddress(payer.toEntityId()), receiverAddress, -5L); // Then @@ -653,7 +653,7 @@ void transferThruContract() throws Exception { final var receiverAddress = getAliasAddressFromEntity(receiverEntity); final var contract = testWeb3jService.deploy(EthCall::deploy); final var payer = accountEntityWithEvmAddressPersist(); - persistAccountBalance(payer, payer.getBalance()); + accountBalancePersist(payer, payer.getCreatedTimestamp()); meterRegistry.clear(); testWeb3jService.setSender(toAddress(payer.toEntityId()).toHexString()); // When diff --git a/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/OpcodeServiceTest.java b/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/OpcodeServiceTest.java index 9ff3173551b..402b410647c 100644 --- a/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/OpcodeServiceTest.java +++ b/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/OpcodeServiceTest.java @@ -114,9 +114,9 @@ class OpcodeServiceTest extends AbstractContractCallServiceOpcodeTracerTest { """) void updateTokenKeysAndGetUpdatedTokenKeyForFungibleToken(final KeyValueType keyValueType, final KeyType keyType) { // Given - final var tokenEntity = fungibleTokenPersistWithTreasuryAccount( + final var token = fungibleTokenPersistWithTreasuryAccount( domainBuilder.entity().persist().toEntityId()); - final var tokenAddress = toAddress(tokenEntity.getTokenId()); + final var tokenAddress = toAddress(token.getTokenId()); final var contract = testWeb3jService.deploy(NestedCalls::deploy); final var contractAddress = contract.getContractAddress(); diff --git a/hedera-mirror-web3/src/test/java/com/hedera/services/utils/EntityIdUtilsTest.java b/hedera-mirror-web3/src/test/java/com/hedera/services/utils/EntityIdUtilsTest.java index ff44f2c868e..459575948cf 100644 --- a/hedera-mirror-web3/src/test/java/com/hedera/services/utils/EntityIdUtilsTest.java +++ b/hedera-mirror-web3/src/test/java/com/hedera/services/utils/EntityIdUtilsTest.java @@ -62,6 +62,8 @@ class EntityIdUtilsTest { public static final ByteString EVM_ADDRESS = ByteString.fromHex("ebb9a1be370150759408cd7af48e9eda2b8ead57"); public static final ByteString WRONG_EVM_ADDRESS = ByteString.fromHex("ebb9a1be3701cd7af48e9eda2b8ead57"); + private static final String EXPECTED_HEXED_ADDRESS = "0000000000000000000000000000000000000003"; + @Test void asSolidityAddressBytesWorksProperly() { final var id = AccountID.newBuilder() @@ -218,7 +220,7 @@ void isOfEcdsaPublicAddressSizeWorks() { void asSolidityAddressHexWorksProperly() { final var id = new Id(1, 2, 3); - assertEquals("0000000000000000000000000000000000000003", EntityIdUtils.asHexedEvmAddress(id)); + assertEquals(EXPECTED_HEXED_ADDRESS, EntityIdUtils.asHexedEvmAddress(id)); } @Test @@ -229,7 +231,12 @@ void asSolidityAddressHexWorksProperlyForAccount() { .setAccountNum(3) .build(); - assertEquals("0000000000000000000000000000000000000003", EntityIdUtils.asHexedEvmAddress(accountId)); + assertEquals(EXPECTED_HEXED_ADDRESS, EntityIdUtils.asHexedEvmAddress(accountId)); + } + + @Test + void asSolidityAddressHexWorksProperlyForTokenId() { + assertEquals(EXPECTED_HEXED_ADDRESS, EntityIdUtils.asHexedEvmAddress(3)); } @Test