tornado-initiation-ui/store/gasPrice.js

90 lines
2.6 KiB
JavaScript
Raw Normal View History

2020-11-03 13:49:11 +01:00
/* eslint-disable no-console */
import networkConfig from '@/networkConfig'
2021-10-27 17:41:14 +02:00
import { GasPriceOracle } from 'gas-price-oracle'
import { estimateFees } from '@mycrypto/gas-estimation'
2020-11-03 13:49:11 +01:00
const { toHex, toWei } = require('web3-utils')
export const state = () => {
return {
2021-10-27 17:41:14 +02:00
params: {
1: {
maxFeePerGas: '0x25FF7A6000',
maxPriorityFeePerGas: '0x77359400',
},
},
prices: {
1: Object.assign(networkConfig.netId1.gasPrices),
100: Object.assign(networkConfig.netId100.gasPrices),
},
2020-11-03 13:49:11 +01:00
}
}
export const getters = {
oracle: (state, getters, rootState, rootGetters) => {
2021-10-27 17:41:14 +02:00
const { id: chainId, gasPrices } = rootGetters['provider/getNetwork']
const currentRpc = rootGetters['provider/getRpc']
2020-11-03 13:49:11 +01:00
console.log('currentRpc', currentRpc)
2021-08-21 04:42:17 +02:00
return new GasPriceOracle({
2021-10-27 17:41:14 +02:00
chainId,
2021-08-21 04:42:17 +02:00
defaultRpc: currentRpc,
2021-10-27 17:41:14 +02:00
defaultFallbackGasPrices: gasPrices,
2021-08-21 04:42:17 +02:00
})
2020-11-03 13:49:11 +01:00
},
2021-10-27 17:41:14 +02:00
gasParams: (state) => (chainId) => {
return state.params[chainId]
2020-11-03 13:49:11 +01:00
},
2021-10-27 17:41:14 +02:00
gasPrice: (state) => (chainId) => {
const currentGas = state.prices[chainId]
return toHex(toWei(currentGas.fast.toString(), 'gwei'))
},
txGasParams: (state, getters, rootState, rootGetters) => {
const { id: chainId, isEip1559Supported } = rootGetters[
'provider/getNetwork'
]
if (isEip1559Supported) {
return getters.gasParams(chainId)
}
return { gasPrice: getters.gasPrice(chainId) }
2020-11-03 13:49:11 +01:00
},
}
export const mutations = {
2021-10-27 17:41:14 +02:00
SAVE_GAS_PRICES(state, { chainId, ...gas }) {
this._vm.$set(state.prices, chainId, gas)
2020-11-03 13:49:11 +01:00
},
2021-10-27 17:41:14 +02:00
SET_GAS_PARAMS(state, { chainId, ...params }) {
this._vm.$set(state.params, chainId, params)
2020-11-03 13:49:11 +01:00
},
}
export const actions = {
2021-10-27 17:41:14 +02:00
async fetchGasParams({ getters, commit, dispatch, rootGetters, state }) {
const { pollInterval, id: chainId, isEip1559Supported } = rootGetters[
'provider/getNetwork'
]
const rpcUrl = rootGetters['provider/getRpc']
2020-11-03 13:49:11 +01:00
try {
2021-10-27 17:41:14 +02:00
if (isEip1559Supported) {
const web3 = this.$provider.getWeb3(rpcUrl)
const { maxFeePerGas, maxPriorityFeePerGas } = await estimateFees(web3)
commit('SET_GAS_PARAMS', {
maxFeePerGas: toHex(maxFeePerGas.toString()),
maxPriorityFeePerGas: toHex(maxPriorityFeePerGas.toString()),
chainId,
})
} else {
const gas = await getters.oracle.gasPrices()
commit('SAVE_GAS_PRICES', { chainId, ...gas })
console.log(`Got fast gas price ${gas.fast}`)
}
setTimeout(() => dispatch('fetchGasParams'), 1000 * pollInterval)
2020-11-03 13:49:11 +01:00
} catch (e) {
2021-10-27 17:41:14 +02:00
console.error('fetchGasParams', e)
setTimeout(() => dispatch('fetchGasParams'), 1000 * pollInterval)
2020-11-03 13:49:11 +01:00
}
},
}