tornado-initiation-ui/store/gasPrice.js

56 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

2020-11-03 13:49:11 +01:00
/* eslint-disable no-console */
import { GasPriceOracle } from 'gas-price-oracle'
import networkConfig from '@/networkConfig'
const { toHex, toWei } = require('web3-utils')
export const state = () => {
return {
2021-06-02 11:47:40 +02:00
instant: networkConfig.netId56.gasPrices.instant,
fast: networkConfig.netId56.gasPrices.fast,
standard: networkConfig.netId56.gasPrices.standard,
low: networkConfig.netId56.gasPrices.low,
2020-11-03 13:49:11 +01:00
custom: null,
}
}
export const getters = {
oracle: (state, getters, rootState, rootGetters) => {
const currentRpc = rootGetters['provider/getNetwork'].rpcUrls.Infura.url
console.log('currentRpc', currentRpc)
return new GasPriceOracle({ defaultRpc: currentRpc })
},
fastGasPrice: (state) => {
return toHex(toWei(state.fast.toString(), 'gwei'))
},
lowGasPrice: (state) => {
return toHex(toWei(state.standard.toString(), 'gwei'))
},
}
export const mutations = {
SAVE_GAS_PRICES(state, { instant, fast, standard, low }) {
this._vm.$set(state, 'instant', instant)
this._vm.$set(state, 'fast', fast)
this._vm.$set(state, 'standard', standard)
this._vm.$set(state, 'low', low)
},
SAVE_CUSTOM_GAS_PRICE(state, { custom }) {
this._vm.$set(state, 'custom', custom)
},
}
export const actions = {
async fetchGasPrice({ getters, commit, dispatch, rootGetters, state }) {
const { pollInterval } = rootGetters['provider/getNetwork']
try {
const gas = await getters.oracle.gasPrices(state)
commit('SAVE_GAS_PRICES', gas)
console.log(`Got fast gas price ${state.fast}`)
setTimeout(() => dispatch('fetchGasPrice'), 1000 * pollInterval)
} catch (e) {
console.error('fetchGasPrice', e)
setTimeout(() => dispatch('fetchGasPrice'), 1000 * pollInterval)
}
},
}