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