Skip to content

Commit e1dfd45

Browse files
committed
improved solution and fixed integration test
Signed-off-by: Nischal Sharma <[email protected]>
1 parent 8282eb4 commit e1dfd45

File tree

4 files changed

+95
-14
lines changed

4 files changed

+95
-14
lines changed

core/src/main/java/org/web3j/tx/Contract.java

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -395,31 +395,63 @@ TransactionReceipt executeTransaction(
395395
ContractEIP1559GasProvider eip1559GasProvider =
396396
(ContractEIP1559GasProvider) gasProvider;
397397
if (eip1559GasProvider.isEIP1559Enabled()) {
398+
Transaction tx;
399+
if (constructor) {
400+
tx =
401+
Transaction.createContractTransaction(
402+
this.transactionManager.getFromAddress(),
403+
BigInteger.ONE,
404+
gasProvider.getGasPrice(),
405+
data);
406+
} else {
407+
tx =
408+
Transaction.createFunctionCallTransaction(
409+
this.transactionManager.getFromAddress(),
410+
BigInteger.ONE,
411+
gasProvider.getGasPrice(),
412+
gasProvider.getGasLimit(),
413+
contractAddress,
414+
data);
415+
}
398416
receipt =
399417
sendEIP1559(
400418
eip1559GasProvider.getChainId(),
401419
contractAddress,
402420
data,
403421
weiValue,
404-
eip1559GasProvider.getGasLimit(
405-
Transaction.createEthCallTransaction(
406-
ZERO_ADDRESS, contractAddress, data)),
422+
eip1559GasProvider.getGasLimit(tx),
407423
eip1559GasProvider.getMaxPriorityFeePerGas(),
408424
eip1559GasProvider.getMaxFeePerGas(),
409425
constructor);
410426
}
411427
}
412428

413429
if (receipt == null) {
430+
Transaction tx;
431+
if (constructor) {
432+
tx =
433+
Transaction.createContractTransaction(
434+
this.transactionManager.getFromAddress(),
435+
BigInteger.ONE,
436+
gasProvider.getGasPrice(),
437+
data);
438+
} else {
439+
tx =
440+
Transaction.createFunctionCallTransaction(
441+
this.transactionManager.getFromAddress(),
442+
BigInteger.ONE,
443+
gasProvider.getGasPrice(),
444+
gasProvider.getGasLimit(),
445+
contractAddress,
446+
data);
447+
}
414448
receipt =
415449
send(
416450
contractAddress,
417451
data,
418452
weiValue,
419453
gasProvider.getGasPrice(),
420-
gasProvider.getGasLimit(
421-
Transaction.createEthCallTransaction(
422-
ZERO_ADDRESS, contractAddress, data)),
454+
gasProvider.getGasLimit(tx),
423455
constructor);
424456
}
425457
} catch (JsonRpcError error) {

core/src/main/java/org/web3j/tx/gas/DynamicEIP1559GasProvider.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.web3j.protocol.Web3j;
2020
import org.web3j.protocol.core.methods.request.Transaction;
21+
import org.web3j.protocol.core.methods.response.EthEstimateGas;
2122
import org.web3j.protocol.core.methods.response.EthGasPrice;
2223
import org.web3j.protocol.core.methods.response.EthMaxPriorityFeePerGas;
2324

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

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

8284
@Override
8385
public BigInteger getGasLimit(Transaction transaction) {
84-
return null;
86+
try {
87+
EthEstimateGas ethEstimateGas = web3j.ethEstimateGas(transaction).send();
88+
if (ethEstimateGas.hasError()) {
89+
throw new RuntimeException(
90+
"Error estimating gas limit: " + ethEstimateGas.getError().getMessage());
91+
}
92+
return ethEstimateGas.getAmountUsed();
93+
} catch (Exception e) {
94+
throw new RuntimeException("Failed to estimate gas limit", e);
95+
}
8596
}
8697

8798
@Override
8899
public BigInteger getGasLimit() {
89-
return null;
100+
return GAS_LIMIT;
90101
}
91102

92103
private BigInteger fetchCurrentGasPrice() {

core/src/main/java/org/web3j/tx/gas/DynamicGasProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class DynamicGasProvider implements ContractGasProvider, PriorityGasProvi
2525
private final Web3j web3j;
2626
private final Priority priority;
2727
private final BigDecimal customMultiplier;
28+
public static final BigInteger GAS_LIMIT = BigInteger.valueOf(9_000_000);
2829

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

4849
@Override
4950
public BigInteger getGasLimit() {
50-
return null;
51+
return GAS_LIMIT;
5152
}
5253

5354
public BigInteger getGasLimit(Transaction transaction) {

integration-tests/src/test/java/org/web3j/protocol/scenarios/DynamicGasProviderIT.java

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
package org.web3j.protocol.scenarios;
1414

15+
import java.math.BigDecimal;
1516
import java.math.BigInteger;
1617

1718
import org.junit.jupiter.api.BeforeAll;
@@ -23,6 +24,7 @@
2324
import org.web3j.test.contract.HumanStandardToken;
2425
import org.web3j.tx.gas.ContractGasProvider;
2526
import org.web3j.tx.gas.DynamicGasProvider;
27+
import org.web3j.tx.gas.PriorityGasProvider;
2628

2729
import static org.junit.jupiter.api.Assertions.assertEquals;
2830
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -31,6 +33,10 @@
3133
public class DynamicGasProviderIT extends Scenario {
3234
static ContractGasProvider dynamicGasProvider;
3335
static String contractAddress;
36+
static final String TOKEN_NAME = "Alice Token";
37+
static final String TOKEN_SYMBOL = "ATK";
38+
static final BigInteger TOKEN_DECIMALS = BigInteger.valueOf(18L);
39+
static final BigInteger TOKEN_SUPPLY = BigInteger.valueOf(10_000_000L);
3440

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

57-
assertEquals(humanStandardToken.name().send(), "Alice Token");
63+
assertEquals(humanStandardToken.name().send(), TOKEN_NAME);
64+
65+
assertNotNull(humanStandardToken.transfer(BOB.getAddress(), BigInteger.ONE).send());
66+
assertEquals(humanStandardToken.balanceOf(BOB.getAddress()).send(), BigInteger.ONE);
67+
}
68+
69+
@Test
70+
public void callSmartContractFunctionWithPriority() throws Exception {
71+
DynamicGasProvider fastDynamicGasProvider =
72+
new DynamicGasProvider(web3j, PriorityGasProvider.Priority.FAST);
73+
DynamicGasProvider slowDynamicGasProvider =
74+
new DynamicGasProvider(web3j, PriorityGasProvider.Priority.SLOW);
75+
DynamicGasProvider customDynamicGasProvider =
76+
new DynamicGasProvider(
77+
web3j, PriorityGasProvider.Priority.CUSTOM, BigDecimal.valueOf(1.5));
78+
79+
assertEquals(
80+
dynamicGasProvider.getGasPrice().multiply(BigInteger.TWO),
81+
fastDynamicGasProvider.getGasPrice());
82+
assertEquals(
83+
dynamicGasProvider.getGasPrice().divide(BigInteger.TWO),
84+
slowDynamicGasProvider.getGasPrice());
85+
assertEquals(
86+
new BigDecimal(dynamicGasProvider.getGasPrice())
87+
.multiply(BigDecimal.valueOf(1.5))
88+
.toBigInteger(),
89+
customDynamicGasProvider.getGasPrice());
90+
91+
HumanStandardToken humanStandardToken =
92+
HumanStandardToken.load(contractAddress, web3j, ALICE, fastDynamicGasProvider);
93+
94+
assertEquals(humanStandardToken.name().send(), TOKEN_NAME);
5895

5996
assertNotNull(humanStandardToken.transfer(BOB.getAddress(), BigInteger.ONE).send());
6097
assertEquals(humanStandardToken.balanceOf(BOB.getAddress()).send(), BigInteger.ONE);
@@ -65,10 +102,10 @@ private static String sendTransaction() throws Exception {
65102
web3j,
66103
ALICE,
67104
dynamicGasProvider,
68-
BigInteger.valueOf(100L),
69-
"Alice Token",
70-
BigInteger.valueOf(18L),
71-
"ATK")
105+
TOKEN_SUPPLY,
106+
TOKEN_NAME,
107+
TOKEN_DECIMALS,
108+
TOKEN_SYMBOL)
72109
.send()
73110
.getContractAddress();
74111
}

0 commit comments

Comments
 (0)