Skip to content

Commit 0118ad6

Browse files
committed
Added DynamicEIP1559Gas Provider
Signed-off-by: Nischal Sharma <nischal@web3labs.com>
1 parent 70629ea commit 0118ad6

File tree

6 files changed

+89
-26
lines changed

6 files changed

+89
-26
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
import java.math.BigInteger;
1818

1919
public interface ContractGasProvider {
20-
BigInteger getGasPrice(String contractFunc);
2120

22-
@Deprecated
2321
BigInteger getGasPrice();
2422

2523
BigInteger getGasLimit(Transaction transaction);
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package org.web3j.tx.gas;
2+
3+
import org.web3j.protocol.Web3j;
4+
import org.web3j.protocol.core.methods.request.Transaction;
5+
import org.web3j.protocol.core.methods.response.EthGasPrice;
6+
import org.web3j.protocol.core.methods.response.EthMaxPriorityFeePerGas;
7+
8+
import java.io.IOException;
9+
import java.math.BigDecimal;
10+
import java.math.BigInteger;
11+
12+
public class DynamicEIP1559GasProvider implements ContractEIP1559GasProvider, PriorityGasProvider{
13+
private Web3j web3j;
14+
private long chainId;
15+
private final Priority priority;
16+
private final BigDecimal customMultiplier;
17+
18+
public DynamicEIP1559GasProvider(Web3j web3j, long chainId) {
19+
this(web3j, chainId, Priority.NORMAL);
20+
}
21+
22+
public DynamicEIP1559GasProvider(Web3j web3j, long chainId, Priority priority) {
23+
this(web3j, chainId, priority, BigDecimal.ONE);
24+
}
25+
26+
public DynamicEIP1559GasProvider(Web3j web3j, long chainId, Priority priority, BigDecimal customMultiplier) {
27+
this.web3j = web3j;
28+
this.chainId = chainId;
29+
this.priority = priority;
30+
this.customMultiplier = customMultiplier;
31+
}
32+
33+
@Override
34+
public boolean isEIP1559Enabled() {
35+
return true;
36+
}
37+
38+
@Override
39+
public long getChainId() {
40+
return chainId;
41+
}
42+
43+
@Override
44+
public BigInteger getMaxFeePerGas(String contractFunc) {
45+
return getGasPrice();
46+
}
47+
48+
@Override
49+
public BigInteger getMaxPriorityFeePerGas(String contractFunc) {
50+
try {
51+
EthMaxPriorityFeePerGas ethMaxPriorityFeePerGas = web3j.ethMaxPriorityFeePerGas().send();
52+
if (ethMaxPriorityFeePerGas.hasError()) {
53+
throw new RuntimeException("Error fetching ethMaxPriorityFeePerGas: " + ethMaxPriorityFeePerGas.getError().getMessage());
54+
}
55+
return ethMaxPriorityFeePerGas.getMaxPriorityFeePerGas();
56+
} catch (IOException e) {
57+
throw new RuntimeException("Failed to get ethMaxPriorityFeePerGas");
58+
}
59+
60+
}
61+
62+
@Override
63+
public BigInteger getGasPrice() {
64+
return applyPriority(fetchCurrentGasPrice(), priority, customMultiplier);
65+
}
66+
67+
@Override
68+
public BigInteger getGasLimit(Transaction transaction) {
69+
return null;
70+
}
71+
72+
@Override
73+
public BigInteger getGasLimit() {
74+
return null;
75+
}
76+
77+
private BigInteger fetchCurrentGasPrice() {
78+
try {
79+
EthGasPrice ethGasPrice = web3j.ethGasPrice().send();
80+
if (ethGasPrice.hasError()) {
81+
throw new RuntimeException("Error fetching gas price: " + ethGasPrice.getError().getMessage());
82+
}
83+
return ethGasPrice.getGasPrice();
84+
} catch (Exception e) {
85+
throw new RuntimeException("Failed to fetch gas price", e);
86+
}
87+
}
88+
}

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,7 @@ public DynamicGasProvider(Web3j web3j, Priority priority, BigDecimal customMulti
3131

3232
@Override
3333
public BigInteger getGasPrice() {
34-
return calculateGasPrice(fetchCurrentGasPrice(), priority, customMultiplier);
35-
}
36-
37-
@Override
38-
public BigInteger getGasPrice(String contractFunc) {
39-
return calculateGasPrice(fetchCurrentGasPrice(), priority, customMultiplier);
34+
return applyPriority(fetchCurrentGasPrice(), priority, customMultiplier);
4035
}
4136

4237
@Override
@@ -68,9 +63,4 @@ private BigInteger fetchCurrentGasPrice() {
6863
throw new RuntimeException("Failed to fetch gas price", e);
6964
}
7065
}
71-
72-
@Override
73-
public BigInteger calculateGasPrice(BigInteger baseGasPrice, Priority priority, BigDecimal customMultiplier) {
74-
return applyPriority(baseGasPrice, priority, customMultiplier);
75-
}
7666
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ enum Priority {
1010
SLOW,
1111
CUSTOM
1212
}
13-
14-
BigInteger calculateGasPrice(BigInteger baseGasPrice, Priority priority, BigDecimal customMultiplier);
15-
1613
default BigInteger applyPriority(BigInteger baseGasPrice, Priority priority, BigDecimal customMultiplier) {
1714

1815
return switch (priority) {

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ public StaticEIP1559GasProvider(
3333
this.gasLimit = gasLimit;
3434
}
3535

36-
@Override
37-
public BigInteger getGasPrice(String contractFunc) {
38-
return maxFeePerGas;
39-
}
40-
4136
@Override
4237
public BigInteger getGasPrice() {
4338
return maxFeePerGas;

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ public StaticGasProvider(BigInteger gasPrice, BigInteger gasLimit) {
2626
this.gasLimit = gasLimit;
2727
}
2828

29-
@Override
30-
public BigInteger getGasPrice(String contractFunc) {
31-
return gasPrice;
32-
}
33-
3429
@Override
3530
public BigInteger getGasPrice() {
3631
return gasPrice;

0 commit comments

Comments
 (0)