Skip to content

Commit cd4c177

Browse files
committed
add: dynamic base gas fees
1 parent 6f2cb54 commit cd4c177

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/server/blockchain/BaseAdminWallet.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ const { base, env } = conf
88
const options = {
99
ethereum: base,
1010
network: `${env}-base`,
11-
maxFeePerGas: (1e8).toFixed(0),
12-
maxPriorityFeePerGas: (1e4).toFixed(0),
11+
maxFeePerGas: undefined, // will force use of estimatefees
12+
maxPriorityFeePerGas: undefined, // will force use of estimate fees
1313
fetchGasPrice: false,
1414
faucetTxCost: 500000
1515
}

src/server/blockchain/Web3Wallet.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,21 @@ export class Web3Wallet {
960960
}
961961
}
962962

963+
async getFeeEstimates() {
964+
const result = await this.web3.eth.getFeeHistory('0x5', 'latest', [10])
965+
966+
const baseFees = result.baseFeePerGas.map(hex => parseInt(hex, 16))
967+
const rewards = result.reward.map(r => parseInt(r[0], 16)) // 10th percentile
968+
969+
const latestBaseFee = baseFees[baseFees.length - 1]
970+
const minPriorityFee = Math.min(...rewards)
971+
972+
return {
973+
baseFee: Math.floor(latestBaseFee * 1.1), // in wei
974+
priorityFee: minPriorityFee // in wei
975+
}
976+
}
977+
963978
/**
964979
* Helper function to handle a tx Send call
965980
* @param tx
@@ -1011,6 +1026,11 @@ export class Web3Wallet {
10111026
maxFeePerGas = maxFeePerGas || this.maxFeePerGas
10121027
maxPriorityFeePerGas = maxPriorityFeePerGas || this.maxPriorityFeePerGas
10131028

1029+
if (!maxFeePerGas || !maxPriorityFeePerGas) {
1030+
const { baseFee, priorityFee } = await this.getFeeEstimates()
1031+
maxFeePerGas = maxFeePerGas || baseFee
1032+
maxPriorityFeePerGas = maxPriorityFeePerGas || priorityFee
1033+
}
10141034
logger.trace('getting tx lock:', { txuuid })
10151035

10161036
const { nonce, release, address } = await this.txManager.lock(this.filledAddresses)

0 commit comments

Comments
 (0)