|
| 1 | +/* |
| 2 | + * Copyright 2025 Web3 Labs Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | +package org.web3j.tx.gas; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.math.BigDecimal; |
| 17 | +import java.math.BigInteger; |
| 18 | + |
| 19 | +import org.web3j.protocol.Web3j; |
| 20 | +import org.web3j.protocol.core.DefaultBlockParameterName; |
| 21 | +import org.web3j.protocol.core.methods.request.Transaction; |
| 22 | +import org.web3j.protocol.core.methods.response.EthEstimateGas; |
| 23 | +import org.web3j.protocol.core.methods.response.EthGasPrice; |
| 24 | +import org.web3j.protocol.core.methods.response.EthMaxPriorityFeePerGas; |
| 25 | + |
| 26 | +public class DynamicEIP1559GasProvider implements ContractEIP1559GasProvider, PriorityGasProvider { |
| 27 | + private Web3j web3j; |
| 28 | + private long chainId; |
| 29 | + private final Priority priority; |
| 30 | + private final BigDecimal customMultiplier; |
| 31 | + private BigInteger maxGasLimit = BigInteger.valueOf(9_000_000); |
| 32 | + |
| 33 | + public DynamicEIP1559GasProvider(Web3j web3j, long chainId) { |
| 34 | + this(web3j, chainId, Priority.NORMAL); |
| 35 | + } |
| 36 | + |
| 37 | + public DynamicEIP1559GasProvider(Web3j web3j, long chainId, Priority priority) { |
| 38 | + this(web3j, chainId, priority, BigDecimal.ONE); |
| 39 | + } |
| 40 | + |
| 41 | + public DynamicEIP1559GasProvider( |
| 42 | + Web3j web3j, long chainId, Priority priority, BigDecimal customMultiplier) { |
| 43 | + this.web3j = web3j; |
| 44 | + this.chainId = chainId; |
| 45 | + this.priority = priority; |
| 46 | + this.customMultiplier = customMultiplier; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public long getChainId() { |
| 51 | + return chainId; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public BigInteger getMaxFeePerGas() { |
| 56 | + try { |
| 57 | + BigInteger baseFee = |
| 58 | + web3j.ethGetBlockByNumber(DefaultBlockParameterName.LATEST, false) |
| 59 | + .send() |
| 60 | + .getBlock() |
| 61 | + .getBaseFeePerGas(); |
| 62 | + |
| 63 | + return baseFee.multiply(BigInteger.valueOf(2)).add(getMaxPriorityFeePerGas()); |
| 64 | + } catch (IOException e) { |
| 65 | + throw new RuntimeException("Failed to get ethMaxFeePerGas"); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public BigInteger getMaxPriorityFeePerGas() { |
| 71 | + try { |
| 72 | + EthMaxPriorityFeePerGas ethMaxPriorityFeePerGas = |
| 73 | + web3j.ethMaxPriorityFeePerGas().send(); |
| 74 | + if (ethMaxPriorityFeePerGas.hasError()) { |
| 75 | + throw new RuntimeException( |
| 76 | + "Error fetching ethMaxPriorityFeePerGas: " |
| 77 | + + ethMaxPriorityFeePerGas.getError().getMessage()); |
| 78 | + } |
| 79 | + return ethMaxPriorityFeePerGas.getMaxPriorityFeePerGas(); |
| 80 | + } catch (IOException e) { |
| 81 | + throw new RuntimeException("Failed to get ethMaxPriorityFeePerGas"); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public BigInteger getGasPrice() { |
| 87 | + return applyPriority(fetchCurrentGasPrice(), priority, customMultiplier); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public BigInteger getGasLimit(Transaction transaction) { |
| 92 | + try { |
| 93 | + EthEstimateGas ethEstimateGas = web3j.ethEstimateGas(transaction).send(); |
| 94 | + if (ethEstimateGas.hasError()) { |
| 95 | + throw new RuntimeException( |
| 96 | + "Error estimating gas limit: " + ethEstimateGas.getError().getMessage()); |
| 97 | + } |
| 98 | + return ethEstimateGas.getAmountUsed(); |
| 99 | + } catch (Exception e) { |
| 100 | + throw new RuntimeException("Failed to estimate gas limit", e); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public BigInteger getGasLimit() { |
| 106 | + return maxGasLimit; |
| 107 | + } |
| 108 | + |
| 109 | + public void setMaxGasLimit(BigInteger gasLimit) { |
| 110 | + maxGasLimit = gasLimit; |
| 111 | + } |
| 112 | + |
| 113 | + private BigInteger fetchCurrentGasPrice() { |
| 114 | + try { |
| 115 | + EthGasPrice ethGasPrice = web3j.ethGasPrice().send(); |
| 116 | + if (ethGasPrice.hasError()) { |
| 117 | + throw new RuntimeException( |
| 118 | + "Error fetching gas price: " + ethGasPrice.getError().getMessage()); |
| 119 | + } |
| 120 | + return ethGasPrice.getGasPrice(); |
| 121 | + } catch (Exception e) { |
| 122 | + throw new RuntimeException("Failed to fetch gas price", e); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments