Skip to content

Commit

Permalink
improved solution and fixed integration test
Browse files Browse the repository at this point in the history
Signed-off-by: Nischal Sharma <[email protected]>
  • Loading branch information
NickSneo committed Jan 20, 2025
1 parent 8282eb4 commit e1dfd45
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 14 deletions.
44 changes: 38 additions & 6 deletions core/src/main/java/org/web3j/tx/Contract.java
Original file line number Diff line number Diff line change
Expand Up @@ -395,31 +395,63 @@ TransactionReceipt executeTransaction(
ContractEIP1559GasProvider eip1559GasProvider =
(ContractEIP1559GasProvider) gasProvider;
if (eip1559GasProvider.isEIP1559Enabled()) {
Transaction tx;
if (constructor) {
tx =
Transaction.createContractTransaction(
this.transactionManager.getFromAddress(),
BigInteger.ONE,
gasProvider.getGasPrice(),
data);
} else {
tx =
Transaction.createFunctionCallTransaction(
this.transactionManager.getFromAddress(),
BigInteger.ONE,
gasProvider.getGasPrice(),
gasProvider.getGasLimit(),
contractAddress,
data);
}
receipt =
sendEIP1559(
eip1559GasProvider.getChainId(),
contractAddress,
data,
weiValue,
eip1559GasProvider.getGasLimit(
Transaction.createEthCallTransaction(
ZERO_ADDRESS, contractAddress, data)),
eip1559GasProvider.getGasLimit(tx),
eip1559GasProvider.getMaxPriorityFeePerGas(),
eip1559GasProvider.getMaxFeePerGas(),
constructor);
}
}

if (receipt == null) {
Transaction tx;
if (constructor) {
tx =
Transaction.createContractTransaction(
this.transactionManager.getFromAddress(),
BigInteger.ONE,
gasProvider.getGasPrice(),
data);
} else {
tx =
Transaction.createFunctionCallTransaction(
this.transactionManager.getFromAddress(),
BigInteger.ONE,
gasProvider.getGasPrice(),
gasProvider.getGasLimit(),
contractAddress,
data);
}
receipt =
send(
contractAddress,
data,
weiValue,
gasProvider.getGasPrice(),
gasProvider.getGasLimit(
Transaction.createEthCallTransaction(
ZERO_ADDRESS, contractAddress, data)),
gasProvider.getGasLimit(tx),
constructor);
}
} catch (JsonRpcError error) {
Expand Down
15 changes: 13 additions & 2 deletions core/src/main/java/org/web3j/tx/gas/DynamicEIP1559GasProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.request.Transaction;
import org.web3j.protocol.core.methods.response.EthEstimateGas;
import org.web3j.protocol.core.methods.response.EthGasPrice;
import org.web3j.protocol.core.methods.response.EthMaxPriorityFeePerGas;

Expand All @@ -26,6 +27,7 @@ public class DynamicEIP1559GasProvider implements ContractEIP1559GasProvider, Pr
private long chainId;
private final Priority priority;
private final BigDecimal customMultiplier;
public static final BigInteger GAS_LIMIT = BigInteger.valueOf(9_000_000);

public DynamicEIP1559GasProvider(Web3j web3j, long chainId) {
this(web3j, chainId, Priority.NORMAL);
Expand Down Expand Up @@ -81,12 +83,21 @@ public BigInteger getGasPrice() {

@Override
public BigInteger getGasLimit(Transaction transaction) {
return null;
try {
EthEstimateGas ethEstimateGas = web3j.ethEstimateGas(transaction).send();
if (ethEstimateGas.hasError()) {
throw new RuntimeException(
"Error estimating gas limit: " + ethEstimateGas.getError().getMessage());
}
return ethEstimateGas.getAmountUsed();
} catch (Exception e) {
throw new RuntimeException("Failed to estimate gas limit", e);
}
}

@Override
public BigInteger getGasLimit() {
return null;
return GAS_LIMIT;
}

private BigInteger fetchCurrentGasPrice() {
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/org/web3j/tx/gas/DynamicGasProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class DynamicGasProvider implements ContractGasProvider, PriorityGasProvi
private final Web3j web3j;
private final Priority priority;
private final BigDecimal customMultiplier;
public static final BigInteger GAS_LIMIT = BigInteger.valueOf(9_000_000);

public DynamicGasProvider(Web3j web3j) {
this(web3j, Priority.NORMAL);
Expand All @@ -47,7 +48,7 @@ public BigInteger getGasPrice() {

@Override
public BigInteger getGasLimit() {
return null;
return GAS_LIMIT;
}

public BigInteger getGasLimit(Transaction transaction) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.web3j.protocol.scenarios;

import java.math.BigDecimal;
import java.math.BigInteger;

import org.junit.jupiter.api.BeforeAll;
Expand All @@ -23,6 +24,7 @@
import org.web3j.test.contract.HumanStandardToken;
import org.web3j.tx.gas.ContractGasProvider;
import org.web3j.tx.gas.DynamicGasProvider;
import org.web3j.tx.gas.PriorityGasProvider;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand All @@ -31,6 +33,10 @@
public class DynamicGasProviderIT extends Scenario {
static ContractGasProvider dynamicGasProvider;
static String contractAddress;
static final String TOKEN_NAME = "Alice Token";
static final String TOKEN_SYMBOL = "ATK";
static final BigInteger TOKEN_DECIMALS = BigInteger.valueOf(18L);
static final BigInteger TOKEN_SUPPLY = BigInteger.valueOf(10_000_000L);

@BeforeAll
static void setUp(Web3j web3j) throws Exception {
Expand All @@ -54,7 +60,38 @@ public void callSmartContractFunction() throws Exception {
HumanStandardToken humanStandardToken =
HumanStandardToken.load(contractAddress, web3j, ALICE, dynamicGasProvider);

assertEquals(humanStandardToken.name().send(), "Alice Token");
assertEquals(humanStandardToken.name().send(), TOKEN_NAME);

assertNotNull(humanStandardToken.transfer(BOB.getAddress(), BigInteger.ONE).send());
assertEquals(humanStandardToken.balanceOf(BOB.getAddress()).send(), BigInteger.ONE);
}

@Test
public void callSmartContractFunctionWithPriority() throws Exception {
DynamicGasProvider fastDynamicGasProvider =
new DynamicGasProvider(web3j, PriorityGasProvider.Priority.FAST);
DynamicGasProvider slowDynamicGasProvider =
new DynamicGasProvider(web3j, PriorityGasProvider.Priority.SLOW);
DynamicGasProvider customDynamicGasProvider =
new DynamicGasProvider(
web3j, PriorityGasProvider.Priority.CUSTOM, BigDecimal.valueOf(1.5));

assertEquals(
dynamicGasProvider.getGasPrice().multiply(BigInteger.TWO),
fastDynamicGasProvider.getGasPrice());
assertEquals(
dynamicGasProvider.getGasPrice().divide(BigInteger.TWO),
slowDynamicGasProvider.getGasPrice());
assertEquals(
new BigDecimal(dynamicGasProvider.getGasPrice())
.multiply(BigDecimal.valueOf(1.5))
.toBigInteger(),
customDynamicGasProvider.getGasPrice());

HumanStandardToken humanStandardToken =
HumanStandardToken.load(contractAddress, web3j, ALICE, fastDynamicGasProvider);

assertEquals(humanStandardToken.name().send(), TOKEN_NAME);

assertNotNull(humanStandardToken.transfer(BOB.getAddress(), BigInteger.ONE).send());
assertEquals(humanStandardToken.balanceOf(BOB.getAddress()).send(), BigInteger.ONE);
Expand All @@ -65,10 +102,10 @@ private static String sendTransaction() throws Exception {
web3j,
ALICE,
dynamicGasProvider,
BigInteger.valueOf(100L),
"Alice Token",
BigInteger.valueOf(18L),
"ATK")
TOKEN_SUPPLY,
TOKEN_NAME,
TOKEN_DECIMALS,
TOKEN_SYMBOL)
.send()
.getContractAddress();
}
Expand Down

0 comments on commit e1dfd45

Please sign in to comment.