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: {
|
2021-11-15 14:21:04 +01:00
|
|
|
maxFeePerGas: '0x25FF7A6000',
|
|
|
|
maxPriorityFeePerGas: '0x77359400',
|
2021-10-27 17:41:14 +02:00
|
|
|
},
|
2021-11-15 14:21:04 +01:00
|
|
|
prices: Object.assign(networkConfig.netId1.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-11-15 14:21:04 +01:00
|
|
|
isEip1559Supported: (state, getters, rootState, rootGetters) => {
|
|
|
|
const { isEip1559Supported } = rootGetters['provider/getNetwork']
|
|
|
|
return isEip1559Supported
|
2020-11-03 13:49:11 +01:00
|
|
|
},
|
2021-11-15 14:21:04 +01:00
|
|
|
gasPrice: (state, getters) => {
|
|
|
|
if (getters.isEip1559Supported) {
|
|
|
|
return state.params.maxFeePerGas
|
|
|
|
}
|
|
|
|
|
|
|
|
return toHex(toWei(state.prices.fast.toString(), 'gwei'))
|
2021-10-27 17:41:14 +02:00
|
|
|
},
|
2021-11-15 14:21:04 +01:00
|
|
|
txGasParams: (state, getters) => (isDisable = false) => {
|
|
|
|
if (!isDisable && getters.isEip1559Supported) {
|
|
|
|
return state.params
|
2021-10-27 17:41:14 +02:00
|
|
|
}
|
2021-11-15 14:21:04 +01:00
|
|
|
return { gasPrice: getters.gasPrice }
|
2020-11-03 13:49:11 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
export const mutations = {
|
2021-11-15 14:21:04 +01:00
|
|
|
SAVE_GAS_PRICES(state, gas) {
|
|
|
|
this._vm.$set(state, 'prices', gas)
|
2020-11-03 13:49:11 +01:00
|
|
|
},
|
2021-11-15 14:21:04 +01:00
|
|
|
SET_GAS_PARAMS(state, params) {
|
|
|
|
this._vm.$set(state, 'params', 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 }) {
|
2021-11-15 14:21:04 +01:00
|
|
|
const { pollInterval, isEip1559Supported } = rootGetters[
|
2021-10-27 17:41:14 +02:00
|
|
|
'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()),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
const gas = await getters.oracle.gasPrices()
|
2021-11-15 14:21:04 +01:00
|
|
|
commit('SAVE_GAS_PRICES', gas)
|
2021-10-27 17:41:14 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|