Skip to content

Commit

Permalink
Min 30gwei polygon main and mumbai fees (#1671)
Browse files Browse the repository at this point in the history
* wip tx underpriced

* cleanups

* small fixes

* added comments
  • Loading branch information
bogdanfazakas committed Dec 22, 2022
1 parent 94fd276 commit 8de514f
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/utils/ContractUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { Config } from '../config'
import { minAbi, GASLIMIT_DEFAULT, LoggerInstance, FEE_HISTORY_NOT_SUPPORTED } from '.'
import { TransactionReceipt } from 'web3-core'

const MIN_GAS_FEE_POLYGON = 30000000000 // minimum recommended 30 gwei polygon main and mumbai fees
const POLYGON_NETWORK_ID = 137
const MUMBAI_NETWORK_ID = 80001

export function setContractDefaults(contract: Contract, config: Config): Contract {
if (config) {
if (config.transactionBlockTimeout)
Expand Down Expand Up @@ -108,6 +112,7 @@ export async function sendTx(
from,
gas: estGas + 1
}
const networkId = await web3.eth.getChainId()
try {
const feeHistory = await web3.eth.getFeeHistory(1, 'latest', [75])
if (feeHistory && feeHistory?.baseFeePerGas?.[0] && feeHistory?.reward?.[0]?.[0]) {
Expand All @@ -124,6 +129,25 @@ export async function sendTx(
.plus(new BigNumber(feeHistory?.baseFeePerGas?.[0]).multipliedBy(2))
.integerValue(BigNumber.ROUND_DOWN)
.toString(10)

// if network is polygon and mumbai and fees is lower than the 30 gwei trashold, sets MIN_GAS_FEE_POLYGON
sendTxValue.maxPriorityFeePerGas =
(networkId === MUMBAI_NETWORK_ID || networkId === POLYGON_NETWORK_ID) &&
new BigNumber(sendTxValue.maxPriorityFeePerGas).lte(
new BigNumber(MIN_GAS_FEE_POLYGON)
)
? new BigNumber(MIN_GAS_FEE_POLYGON)
.integerValue(BigNumber.ROUND_DOWN)
.toString(10)
: sendTxValue.maxPriorityFeePerGas

sendTxValue.maxFeePerGas =
(networkId === MUMBAI_NETWORK_ID || networkId === POLYGON_NETWORK_ID) &&
new BigNumber(sendTxValue.maxFeePerGas).lte(new BigNumber(MIN_GAS_FEE_POLYGON))
? new BigNumber(MIN_GAS_FEE_POLYGON)
.integerValue(BigNumber.ROUND_DOWN)
.toString(10)
: sendTxValue.maxFeePerGas
} else {
sendTxValue.gasPrice = await getFairGasPrice(web3, gasFeeMultiplier)
}
Expand Down

0 comments on commit 8de514f

Please sign in to comment.