classic-ui/store/gasPrices.js

143 lines
4.3 KiB
JavaScript
Raw Permalink Normal View History

2022-04-22 06:05:56 +03:00
/* eslint-disable no-console */
2022-05-26 17:04:27 +03:00
import Web3 from 'web3'
import { toHex, toWei, fromWei, toBN } from 'web3-utils'
import { GasPriceOracle } from '@tornado/gas-price-oracle'
2022-05-26 17:04:27 +03:00
import { serialize } from '@ethersproject/transactions'
2022-04-22 06:05:56 +03:00
2022-05-26 17:04:27 +03:00
import networkConfig from '@/networkConfig'
import OvmGasPriceOracleABI from '@/abis/OvmGasPriceOracle.abi.json'
import { DUMMY_NONCE, DUMMY_WITHDRAW_DATA } from '@/constants/variables'
2022-04-22 06:05:56 +03:00
export const state = () => {
return {
gasParams: { gasPrice: toWei(toBN(50), 'gwei') },
2022-05-26 17:04:27 +03:00
l1Fee: '0'
2022-04-22 06:05:56 +03:00
}
}
export const getters = {
2022-06-24 18:56:03 +03:00
oracle: (state, getters, rootState, rootGetters) => {
const netId = Number(rootGetters['metamask/netId'])
const { gasPrices } = rootGetters['metamask/networkConfig']
return new GasPriceOracle({
chainId: netId,
defaultRpc: rootGetters['settings/currentRpc'].url,
minPriority: netId === 1 || netId === 5 ? 2 : 0.05,
percentile: 5,
blocksCount: 20,
2022-06-24 18:56:03 +03:00
defaultFallbackGasPrices: gasPrices
})
},
2022-05-26 17:04:27 +03:00
ovmGasPriceOracleContract: (state, getters, rootState) => ({ netId }) => {
const config = networkConfig[`netId${netId}`]
const { url } = rootState.settings[`netId${netId}`].rpc
const address = config.ovmGasPriceOracleContract
if (address) {
const web3 = new Web3(url)
2022-05-26 17:04:27 +03:00
return new web3.eth.Contract(OvmGasPriceOracleABI, address)
}
return null
},
l1Fee: (state) => {
return state.l1Fee
},
2022-06-24 18:56:03 +03:00
getGasParams: (state) => {
return state.gasParams
2022-04-22 06:05:56 +03:00
},
2022-06-24 18:56:03 +03:00
gasPrice: (state, getters) => {
const { gasPrice, maxFeePerGas } = getters.getGasParams
return toHex(maxFeePerGas || gasPrice)
2022-04-22 06:05:56 +03:00
},
2022-06-24 18:56:03 +03:00
gasPriceInGwei: (state, getters) => {
return fromWei(getters.gasPrice, 'gwei')
2022-04-22 06:05:56 +03:00
}
}
export const mutations = {
2022-06-24 18:56:03 +03:00
SAVE_GAS_PARAMS(state, payload) {
state.gasParams = payload
2022-05-26 17:04:27 +03:00
},
SAVE_L1_FEE(state, l1Fee) {
state.l1Fee = l1Fee
2022-04-22 06:05:56 +03:00
}
}
export const actions = {
async fetchGasPrice({ getters, dispatch, commit, rootGetters, rootState }) {
2022-04-22 06:05:56 +03:00
const { pollInterval } = rootGetters['metamask/networkConfig']
const netId = Number(rootGetters['metamask/netId'])
const { url: rpcUrl } = rootState.settings[`netId${netId}`].rpc
2022-04-22 06:05:56 +03:00
2022-06-24 18:56:03 +03:00
try {
// Bump more for Polygon (MATIC) and for Goerli, because minPriority for this sidechains don't affect correctly
let bumpPercent
switch (netId) {
case 5:
bumpPercent = 100
break
case 137:
case 43114:
case 100:
bumpPercent = 30
break
default:
bumpPercent = 10
}
let txGasParams = {}
try {
// Use maxFeePerGas if eip1599 gas support by chain, use fast if legacy gas fetched
txGasParams = await getters.oracle.getTxGasParams({ legacySpeed: 'fast', bumpPercent }) // in wei
} catch (e) {
const web3 = new Web3(rpcUrl)
const wei = toBN(await web3.eth.getGasPrice())
const bumped = wei.add(wei.mul(toBN(bumpPercent)).div(toBN(100)))
txGasParams = { gasPrice: toHex(bumped) }
}
2022-06-24 18:56:03 +03:00
commit('SAVE_GAS_PARAMS', txGasParams)
2022-05-26 17:04:27 +03:00
await dispatch('fetchL1Fee')
2022-04-22 06:05:56 +03:00
} catch (e) {
console.error('fetchGasPrice', e)
2022-06-24 18:56:03 +03:00
} finally {
setTimeout(() => dispatch('fetchGasPrice'), 2000 * pollInterval)
2022-04-22 06:05:56 +03:00
}
},
setDefault({ commit, rootGetters }) {
const { gasPrices } = rootGetters['metamask/networkConfig']
commit('SAVE_GAS_PARAMS', { gasPrice: toWei(gasPrices?.fast?.toFixed(9) || 0, 'gwei') })
2022-05-26 17:04:27 +03:00
},
2022-06-24 18:56:03 +03:00
async fetchL1Fee({ commit, getters, rootGetters }) {
2022-05-26 17:04:27 +03:00
const netId = rootGetters['metamask/netId']
const isOptimismConnected = rootGetters['application/isOptimismConnected']
const oracleInstance = getters.ovmGasPriceOracleContract({ netId })
if (isOptimismConnected && oracleInstance) {
try {
const gasLimit = rootGetters['application/withdrawGas']
const tornadoProxyInstance = rootGetters['application/tornadoProxyContract']({ netId })
const tx = serialize({
type: 0,
gasLimit,
chainId: netId,
nonce: DUMMY_NONCE,
data: DUMMY_WITHDRAW_DATA,
2022-06-24 18:56:03 +03:00
gasPrice: getters.gasPrice,
2022-05-26 17:04:27 +03:00
to: tornadoProxyInstance._address
})
const l1Fee = await oracleInstance.methods.getL1Fee(tx).call()
commit('SAVE_L1_FEE', l1Fee)
} catch (err) {
console.error('fetchL1Fee has error:', err.message)
}
}
2022-04-22 06:05:56 +03:00
}
}