tornado-classic-ui/store/gasPrices.js

115 lines
3.3 KiB
JavaScript
Raw Normal View History

2022-04-22 05:05:56 +02:00
/* eslint-disable no-console */
2022-05-26 16:04:27 +02:00
import Web3 from 'web3'
2022-06-24 17:56:03 +02:00
import { toHex, fromWei } from 'web3-utils'
2022-04-22 05:05:56 +02:00
import { GasPriceOracle } from 'gas-price-oracle'
2022-05-26 16:04:27 +02:00
import { serialize } from '@ethersproject/transactions'
2022-04-22 05:05:56 +02:00
2022-05-26 16:04:27 +02:00
import networkConfig from '@/networkConfig'
import OvmGasPriceOracleABI from '@/abis/OvmGasPriceOracle.abi.json'
import { DUMMY_NONCE, DUMMY_WITHDRAW_DATA } from '@/constants/variables'
2022-04-22 05:05:56 +02:00
export const state = () => {
return {
2022-06-24 17:56:03 +02:00
gasParams: { gasPrice: 50 },
2022-05-26 16:04:27 +02:00
l1Fee: '0'
2022-04-22 05:05:56 +02:00
}
}
export const getters = {
2022-06-24 17:56:03 +02: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,
defaultFallbackGasPrices: gasPrices
})
},
2022-05-26 16:04:27 +02: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)
return new web3.eth.Contract(OvmGasPriceOracleABI, address)
}
return null
},
l1Fee: (state) => {
return state.l1Fee
},
2022-06-24 17:56:03 +02:00
getGasParams: (state) => {
return state.gasParams
2022-04-22 05:05:56 +02:00
},
2022-06-24 17:56:03 +02:00
gasPrice: (state, getters) => {
const { gasPrice, maxFeePerGas } = getters.getGasParams
return toHex(maxFeePerGas || gasPrice)
2022-04-22 05:05:56 +02:00
},
2022-06-24 17:56:03 +02:00
gasPriceInGwei: (state, getters) => {
return fromWei(getters.gasPrice, 'gwei')
2022-04-22 05:05:56 +02:00
}
}
export const mutations = {
2022-06-24 17:56:03 +02:00
SAVE_GAS_PARAMS(state, payload) {
state.gasParams = payload
2022-05-26 16:04:27 +02:00
},
SAVE_L1_FEE(state, l1Fee) {
state.l1Fee = l1Fee
2022-04-22 05:05:56 +02:00
}
}
export const actions = {
2022-06-24 17:56:03 +02:00
async fetchGasPrice({ getters, dispatch, commit, rootGetters }) {
const netId = rootGetters['metamask/netId']
2022-04-22 05:05:56 +02:00
const { pollInterval } = rootGetters['metamask/networkConfig']
2022-06-24 17:56:03 +02:00
const isLegacy = netId === 137
2022-04-22 05:05:56 +02:00
2022-06-24 17:56:03 +02:00
try {
const txGasParams = await getters.oracle.getTxGasParams({ isLegacy })
commit('SAVE_GAS_PARAMS', txGasParams)
2022-05-26 16:04:27 +02:00
await dispatch('fetchL1Fee')
2022-04-22 05:05:56 +02:00
} catch (e) {
console.error('fetchGasPrice', e)
2022-06-24 17:56:03 +02:00
} finally {
2022-04-22 05:05:56 +02:00
setTimeout(() => dispatch('fetchGasPrice'), 1000 * pollInterval)
}
},
setDefault({ commit, rootGetters }) {
const { gasPrices } = rootGetters['metamask/networkConfig']
2022-06-24 17:56:03 +02:00
commit('SAVE_GAS_PARAMS', { gasPrice: gasPrices?.fast })
2022-05-26 16:04:27 +02:00
},
2022-06-24 17:56:03 +02:00
async fetchL1Fee({ commit, getters, rootGetters }) {
2022-05-26 16:04:27 +02: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 17:56:03 +02:00
gasPrice: getters.gasPrice,
2022-05-26 16:04:27 +02: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 05:05:56 +02:00
}
}