Skip to content

Commit

Permalink
Fix tests in ContractCallServicePrecompileModificationTest (#10151)
Browse files Browse the repository at this point in the history
 Fix 4 tests in ContractCallServicePrecompileModificationTest

---------

Signed-off-by: sdimitrov9 <[email protected]>
  • Loading branch information
sdimitrov9 authored Jan 17, 2025
1 parent 468fec2 commit e760376
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public Long convert(String source) {
return null;
}


var parts = source.split("\\.");
if (parts.length == 3) {
return EntityId.of(source).getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public Token(
String name,
String symbol,
int decimals,
Long totalSupply,
long totalSupply,
AccountID treasuryAccountId,
Key adminKey,
Key kycKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ private TransactionBody buildContractCallTransactionBody(
.build())
.functionParameters(com.hedera.pbj.runtime.io.buffer.Bytes.wrap(
params.getCallData().toArrayUnsafe()))
.amount(params.getValue()) // tinybars sent to contract
.gas(estimatedGas)
.build())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.mockito.Mockito.doAnswer;

import com.hedera.mirror.common.domain.balance.AccountBalance;
import com.hedera.mirror.common.domain.entity.Entity;
import com.hedera.mirror.common.domain.entity.EntityId;
import com.hedera.mirror.rest.model.OpcodesResponse;
Expand Down Expand Up @@ -237,4 +238,18 @@ private Address entityAddress(Entity entity) {
}
return toAddress(entity.toEntityId());
}

protected void accountBalanceRecordsPersist(Entity sender) {
domainBuilder
.accountBalance()
.customize(ab -> ab.id(new AccountBalance.Id(sender.getCreatedTimestamp(), sender.toEntityId()))
.balance(sender.getBalance()))
.persist();

domainBuilder
.accountBalance()
.customize(ab -> ab.id(new AccountBalance.Id(sender.getCreatedTimestamp(), treasuryEntity.toEntityId()))
.balance(treasuryEntity.getBalance()))
.persist();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ protected Entity accountEntityPersist() {
return domainBuilder
.entity()
.customize(e ->
e.type(EntityType.ACCOUNT).evmAddress(null).alias(null).balance(1_000_000_000_000L))
e.type(EntityType.ACCOUNT).evmAddress(null).alias(null).balance(100_000_000_000_000_000L))
.persist();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
import com.hedera.services.store.models.Id;
import com.hedera.services.utils.EntityIdUtils;
import com.hederahashgraph.api.proto.java.Key.KeyCase;
import com.swirlds.base.time.Time;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
Expand Down Expand Up @@ -665,26 +664,32 @@ void unpauseToken() throws Exception {
@Test
void createFungibleToken() throws Exception {
// Given
var initialSupply = BigInteger.valueOf(10L);
var decimals = BigInteger.valueOf(10L);
var value = 10000L * 100_000_000L;
final var value = 10000L * 100_000_000_000L;
final var sender = accountEntityPersist();

accountBalanceRecordsPersist(sender);

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

final var treasuryAccount = domainBuilder
.entity()
.customize(e -> e.type(EntityType.ACCOUNT).deleted(false).evmAddress(null))
.persist();
testWeb3jService.setValue(value);

testWeb3jService.setSender(toAddress(sender.toEntityId()).toHexString());

final var treasuryAccount = accountEntityPersist();

final var token = populateHederaToken(
contract.getContractAddress(), TokenTypeEnum.FUNGIBLE_COMMON, treasuryAccount.toEntityId());
final var initialTokenSupply = BigInteger.valueOf(10L);
final var decimalPlacesSupportedByToken = BigInteger.valueOf(10L); // e.g. 1.0123456789

// When
testWeb3jService.setValue(value);
final var functionCall = contract.call_createFungibleTokenExternal(token, initialSupply, decimals);
final var functionCall =
contract.call_createFungibleTokenExternal(token, initialTokenSupply, decimalPlacesSupportedByToken);
final var result = functionCall.send();

final var contractFunctionProvider = ContractFunctionProviderRecord.builder()
.contractAddress(Address.fromHexString(contract.getContractAddress()))
.sender(toAddress(sender.toEntityId()))
.value(value)
.build();

Expand All @@ -702,15 +707,22 @@ void createFungibleTokenWithCustomFees() throws Exception {
var decimals = BigInteger.valueOf(10L);
var value = 10000L * 100_000_000L;

final var sender = accountEntityPersist();

accountBalanceRecordsPersist(sender);

final var treasuryAccount = accountEntityPersist();

final var tokenForDenomination = persistFungibleToken();
final var feeCollector = accountEntityWithEvmAddressPersist();

tokenAccountPersist(tokenForDenomination, feeCollector);

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

final var treasuryAccount = domainBuilder
.entity()
.customize(e -> e.type(EntityType.ACCOUNT).deleted(false).evmAddress(null))
.persist();
testWeb3jService.setSender(toAddress(sender.toEntityId()).toHexString());
testWeb3jService.setValue(value);

final var token = populateHederaToken(
contract.getContractAddress(), TokenTypeEnum.FUNGIBLE_COMMON, treasuryAccount.toEntityId());

Expand All @@ -729,13 +741,13 @@ void createFungibleTokenWithCustomFees() throws Exception {
getAliasFromEntity(feeCollector));

// When
testWeb3jService.setValue(value);
final var functionCall = contract.call_createFungibleTokenWithCustomFeesExternal(
token, initialSupply, decimals, List.of(fixedFee), List.of(fractionalFee));
final var result = functionCall.send();

final var contractFunctionProvider = ContractFunctionProviderRecord.builder()
.contractAddress(Address.fromHexString(contract.getContractAddress()))
.sender(toAddress(sender.toEntityId()))
.value(value)
.build();

Expand All @@ -750,9 +762,14 @@ void createFungibleTokenWithCustomFees() throws Exception {
void createNonFungibleToken() throws Exception {
// Given
var value = 10000L * 100_000_000L;
final var sender = accountEntityPersist();

accountBalanceRecordsPersist(sender);

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

testWeb3jService.setSender(toAddress(sender.toEntityId()).toHexString());

final var treasuryAccount = domainBuilder
.entity()
.customize(e -> e.type(EntityType.ACCOUNT).deleted(false).evmAddress(null))
Expand All @@ -767,6 +784,7 @@ void createNonFungibleToken() throws Exception {

final var contractFunctionProvider = ContractFunctionProviderRecord.builder()
.contractAddress(Address.fromHexString(contract.getContractAddress()))
.sender(toAddress(sender.toEntityId()))
.value(value)
.build();

Expand All @@ -781,12 +799,20 @@ void createNonFungibleToken() throws Exception {
void createNonFungibleTokenWithCustomFees() throws Exception {
// Given
var value = 10000L * 100_000_000L;
final var sender = accountEntityPersist();

accountBalanceRecordsPersist(sender);

final var tokenForDenomination = persistFungibleToken();
final var feeCollector = accountEntityWithEvmAddressPersist();

tokenAccountPersist(tokenForDenomination, feeCollector);

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

testWeb3jService.setSender(toAddress(sender.toEntityId()).toHexString());
testWeb3jService.setValue(value);

final var treasuryAccount = domainBuilder
.entity()
.customize(e -> e.type(EntityType.ACCOUNT).deleted(false).evmAddress(null))
Expand Down Expand Up @@ -816,6 +842,7 @@ void createNonFungibleTokenWithCustomFees() throws Exception {

final var contractFunctionProvider = ContractFunctionProviderRecord.builder()
.contractAddress(Address.fromHexString(contract.getContractAddress()))
.sender(toAddress(sender.toEntityId()))
.value(value)
.build();
// Then
Expand Down Expand Up @@ -1409,7 +1436,9 @@ private void verifyEthCallAndEstimateGas(

private HederaToken populateHederaToken(
final String contractAddress, final TokenTypeEnum tokenType, final EntityId treasuryAccountId) {
final var autoRenewAccount = accountEntityWithEvmAddressPersist();
final var autoRenewAccount =
accountEntityWithEvmAddressPersist(); // the account that is going to be charged for token renewal upon
// expiration
final var tokenEntity = domainBuilder
.entity()
.customize(e -> e.type(EntityType.TOKEN).autoRenewAccountId(autoRenewAccount.getId()))
Expand All @@ -1419,21 +1448,26 @@ private HederaToken populateHederaToken(
.customize(t -> t.tokenId(tokenEntity.getId()).type(tokenType).treasuryAccountId(treasuryAccountId))
.persist();

final var supplyKey =
new KeyValue(Boolean.FALSE, contractAddress, new byte[0], new byte[0], Address.ZERO.toHexString());
final var supplyKey = new KeyValue(
Boolean.FALSE,
contractAddress,
new byte[0],
new byte[0],
Address.ZERO.toHexString()); // the key needed for token minting or burning
final var keys = new ArrayList<TokenKey>();
keys.add(new TokenKey(AbstractContractCallServiceTest.KeyType.SUPPLY_KEY.getKeyTypeNumeric(), supplyKey));

return new HederaToken(
token.getName(),
token.getSymbol(),
getAddressFromEntityId(treasuryAccountId),
tokenEntity.getMemo(),
getAddressFromEntityId(treasuryAccountId), // id of the account holding the initial token supply
tokenEntity.getMemo(), // token description encoded in UTF-8 format
true,
BigInteger.valueOf(10_000L),
false,
keys,
new Expiry(
BigInteger.valueOf(Time.getCurrent().currentTimeMillis() + 1_000_000_000),
BigInteger.valueOf(Instant.now().getEpochSecond() + 8_000_000L),
getAliasFromEntity(autoRenewAccount),
BigInteger.valueOf(8_000_000)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public class TestWeb3jService implements Web3jService {
private boolean isEstimateGas = false;
private String transactionResult;
private Supplier<String> estimatedGas;
private long value = 0L;
private long value = 0L; // the amount sent to the smart contract, if the contract function is payable.
private boolean persistContract = true;
private byte[] contractRuntime;
private BlockType blockType = BlockType.LATEST;
Expand Down

0 comments on commit e760376

Please sign in to comment.