2018-11-13 18:36:35 +01:00
|
|
|
import { clone, uniqBy, flatten } from 'ramda'
|
2018-10-09 18:35:54 +02:00
|
|
|
import BigNumber from 'bignumber.js'
|
2018-10-24 08:31:45 +02:00
|
|
|
import {
|
|
|
|
loadLocalStorageData,
|
|
|
|
saveLocalStorageData,
|
2019-03-22 00:03:30 +01:00
|
|
|
} from '../../../lib/local-storage-helpers'
|
2018-12-09 21:48:06 +01:00
|
|
|
import {
|
|
|
|
decGWEIToHexWEI,
|
2019-03-22 00:03:30 +01:00
|
|
|
} from '../../helpers/utils/conversions.util'
|
2019-03-22 15:50:12 +01:00
|
|
|
import {
|
|
|
|
isEthereumNetwork,
|
2020-05-02 21:41:17 +02:00
|
|
|
} from '../../selectors'
|
2018-08-16 14:28:27 +02:00
|
|
|
|
|
|
|
// Actions
|
|
|
|
const BASIC_GAS_ESTIMATE_LOADING_FINISHED = 'metamask/gas/BASIC_GAS_ESTIMATE_LOADING_FINISHED'
|
|
|
|
const BASIC_GAS_ESTIMATE_LOADING_STARTED = 'metamask/gas/BASIC_GAS_ESTIMATE_LOADING_STARTED'
|
2018-10-09 18:35:54 +02:00
|
|
|
const GAS_ESTIMATE_LOADING_FINISHED = 'metamask/gas/GAS_ESTIMATE_LOADING_FINISHED'
|
|
|
|
const GAS_ESTIMATE_LOADING_STARTED = 'metamask/gas/GAS_ESTIMATE_LOADING_STARTED'
|
2018-08-16 14:28:27 +02:00
|
|
|
const RESET_CUSTOM_GAS_STATE = 'metamask/gas/RESET_CUSTOM_GAS_STATE'
|
2018-09-20 18:06:23 +02:00
|
|
|
const RESET_CUSTOM_DATA = 'metamask/gas/RESET_CUSTOM_DATA'
|
2018-08-16 14:28:27 +02:00
|
|
|
const SET_BASIC_GAS_ESTIMATE_DATA = 'metamask/gas/SET_BASIC_GAS_ESTIMATE_DATA'
|
|
|
|
const SET_CUSTOM_GAS_ERRORS = 'metamask/gas/SET_CUSTOM_GAS_ERRORS'
|
|
|
|
const SET_CUSTOM_GAS_LIMIT = 'metamask/gas/SET_CUSTOM_GAS_LIMIT'
|
|
|
|
const SET_CUSTOM_GAS_PRICE = 'metamask/gas/SET_CUSTOM_GAS_PRICE'
|
|
|
|
const SET_CUSTOM_GAS_TOTAL = 'metamask/gas/SET_CUSTOM_GAS_TOTAL'
|
2018-10-09 18:35:54 +02:00
|
|
|
const SET_PRICE_AND_TIME_ESTIMATES = 'metamask/gas/SET_PRICE_AND_TIME_ESTIMATES'
|
2018-10-24 08:31:45 +02:00
|
|
|
const SET_API_ESTIMATES_LAST_RETRIEVED = 'metamask/gas/SET_API_ESTIMATES_LAST_RETRIEVED'
|
2018-11-13 17:36:52 +01:00
|
|
|
const SET_BASIC_API_ESTIMATES_LAST_RETRIEVED = 'metamask/gas/SET_BASIC_API_ESTIMATES_LAST_RETRIEVED'
|
2018-12-10 22:51:00 +01:00
|
|
|
const SET_BASIC_PRICE_ESTIMATES_LAST_RETRIEVED = 'metamask/gas/SET_BASIC_PRICE_ESTIMATES_LAST_RETRIEVED'
|
2018-08-16 14:28:27 +02:00
|
|
|
|
|
|
|
const initState = {
|
|
|
|
customData: {
|
2018-09-13 10:47:05 +02:00
|
|
|
price: null,
|
2018-12-12 15:54:03 +01:00
|
|
|
limit: null,
|
2018-08-16 14:28:27 +02:00
|
|
|
},
|
|
|
|
basicEstimates: {
|
|
|
|
average: null,
|
|
|
|
fastestWait: null,
|
|
|
|
fastWait: null,
|
|
|
|
fast: null,
|
|
|
|
safeLowWait: null,
|
|
|
|
blockNum: null,
|
|
|
|
avgWait: null,
|
|
|
|
blockTime: null,
|
|
|
|
speed: null,
|
|
|
|
fastest: null,
|
|
|
|
safeLow: null,
|
|
|
|
},
|
|
|
|
basicEstimateIsLoading: true,
|
2018-10-09 18:35:54 +02:00
|
|
|
gasEstimatesLoading: true,
|
|
|
|
priceAndTimeEstimates: [],
|
2018-10-24 08:31:45 +02:00
|
|
|
priceAndTimeEstimatesLastRetrieved: 0,
|
2018-11-13 17:36:52 +01:00
|
|
|
basicPriceAndTimeEstimatesLastRetrieved: 0,
|
2018-12-10 22:51:00 +01:00
|
|
|
basicPriceEstimatesLastRetrieved: 0,
|
2018-08-16 14:28:27 +02:00
|
|
|
errors: {},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reducer
|
2020-02-06 17:38:14 +01:00
|
|
|
export default function reducer (state = initState, action) {
|
2018-08-16 14:28:27 +02:00
|
|
|
switch (action.type) {
|
|
|
|
case BASIC_GAS_ESTIMATE_LOADING_STARTED:
|
|
|
|
return {
|
2020-02-06 17:38:14 +01:00
|
|
|
...state,
|
2018-08-16 14:28:27 +02:00
|
|
|
basicEstimateIsLoading: true,
|
|
|
|
}
|
|
|
|
case BASIC_GAS_ESTIMATE_LOADING_FINISHED:
|
|
|
|
return {
|
2020-02-06 17:38:14 +01:00
|
|
|
...state,
|
2018-08-16 14:28:27 +02:00
|
|
|
basicEstimateIsLoading: false,
|
|
|
|
}
|
2018-10-09 18:35:54 +02:00
|
|
|
case GAS_ESTIMATE_LOADING_STARTED:
|
|
|
|
return {
|
2020-02-06 17:38:14 +01:00
|
|
|
...state,
|
2018-10-09 18:35:54 +02:00
|
|
|
gasEstimatesLoading: true,
|
|
|
|
}
|
|
|
|
case GAS_ESTIMATE_LOADING_FINISHED:
|
|
|
|
return {
|
2020-02-06 17:38:14 +01:00
|
|
|
...state,
|
2018-10-09 18:35:54 +02:00
|
|
|
gasEstimatesLoading: false,
|
|
|
|
}
|
2018-08-16 14:28:27 +02:00
|
|
|
case SET_BASIC_GAS_ESTIMATE_DATA:
|
|
|
|
return {
|
2020-02-06 17:38:14 +01:00
|
|
|
...state,
|
2018-08-16 14:28:27 +02:00
|
|
|
basicEstimates: action.value,
|
|
|
|
}
|
|
|
|
case SET_CUSTOM_GAS_PRICE:
|
|
|
|
return {
|
2020-02-06 17:38:14 +01:00
|
|
|
...state,
|
2018-08-16 14:28:27 +02:00
|
|
|
customData: {
|
2020-02-06 17:38:14 +01:00
|
|
|
...state.customData,
|
2018-08-16 14:28:27 +02:00
|
|
|
price: action.value,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
case SET_CUSTOM_GAS_LIMIT:
|
|
|
|
return {
|
2020-02-06 17:38:14 +01:00
|
|
|
...state,
|
2018-08-16 14:28:27 +02:00
|
|
|
customData: {
|
2020-02-06 17:38:14 +01:00
|
|
|
...state.customData,
|
2018-08-16 14:28:27 +02:00
|
|
|
limit: action.value,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
case SET_CUSTOM_GAS_TOTAL:
|
|
|
|
return {
|
2020-02-06 17:38:14 +01:00
|
|
|
...state,
|
2018-08-16 14:28:27 +02:00
|
|
|
customData: {
|
2020-02-06 17:38:14 +01:00
|
|
|
...state.customData,
|
2018-08-16 14:28:27 +02:00
|
|
|
total: action.value,
|
|
|
|
},
|
|
|
|
}
|
2018-10-09 18:35:54 +02:00
|
|
|
case SET_PRICE_AND_TIME_ESTIMATES:
|
|
|
|
return {
|
2020-02-06 17:38:14 +01:00
|
|
|
...state,
|
2018-10-09 18:35:54 +02:00
|
|
|
priceAndTimeEstimates: action.value,
|
|
|
|
}
|
2018-08-16 14:28:27 +02:00
|
|
|
case SET_CUSTOM_GAS_ERRORS:
|
|
|
|
return {
|
2020-02-06 17:38:14 +01:00
|
|
|
...state,
|
2018-08-16 14:28:27 +02:00
|
|
|
errors: {
|
2020-02-06 17:38:14 +01:00
|
|
|
...state.errors,
|
2018-08-16 14:28:27 +02:00
|
|
|
...action.value,
|
|
|
|
},
|
|
|
|
}
|
2018-10-24 08:31:45 +02:00
|
|
|
case SET_API_ESTIMATES_LAST_RETRIEVED:
|
|
|
|
return {
|
2020-02-06 17:38:14 +01:00
|
|
|
...state,
|
2018-10-24 08:31:45 +02:00
|
|
|
priceAndTimeEstimatesLastRetrieved: action.value,
|
|
|
|
}
|
2018-11-13 17:36:52 +01:00
|
|
|
case SET_BASIC_API_ESTIMATES_LAST_RETRIEVED:
|
|
|
|
return {
|
2020-02-06 17:38:14 +01:00
|
|
|
...state,
|
2018-11-13 17:36:52 +01:00
|
|
|
basicPriceAndTimeEstimatesLastRetrieved: action.value,
|
|
|
|
}
|
2018-12-10 22:51:00 +01:00
|
|
|
case SET_BASIC_PRICE_ESTIMATES_LAST_RETRIEVED:
|
|
|
|
return {
|
2020-02-06 17:38:14 +01:00
|
|
|
...state,
|
2018-12-10 22:51:00 +01:00
|
|
|
basicPriceEstimatesLastRetrieved: action.value,
|
|
|
|
}
|
2018-09-20 18:06:23 +02:00
|
|
|
case RESET_CUSTOM_DATA:
|
|
|
|
return {
|
2020-02-06 17:38:14 +01:00
|
|
|
...state,
|
2018-09-20 18:06:23 +02:00
|
|
|
customData: clone(initState.customData),
|
|
|
|
}
|
2018-08-16 14:28:27 +02:00
|
|
|
case RESET_CUSTOM_GAS_STATE:
|
|
|
|
return clone(initState)
|
|
|
|
default:
|
2020-02-06 17:38:14 +01:00
|
|
|
return state
|
2018-08-16 14:28:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Action Creators
|
|
|
|
export function basicGasEstimatesLoadingStarted () {
|
|
|
|
return {
|
|
|
|
type: BASIC_GAS_ESTIMATE_LOADING_STARTED,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function basicGasEstimatesLoadingFinished () {
|
|
|
|
return {
|
|
|
|
type: BASIC_GAS_ESTIMATE_LOADING_FINISHED,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 18:35:54 +02:00
|
|
|
export function gasEstimatesLoadingStarted () {
|
|
|
|
return {
|
|
|
|
type: GAS_ESTIMATE_LOADING_STARTED,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function gasEstimatesLoadingFinished () {
|
|
|
|
return {
|
|
|
|
type: GAS_ESTIMATE_LOADING_FINISHED,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function fetchBasicGasEstimates () {
|
2019-07-04 21:18:12 +02:00
|
|
|
return async (dispatch, getState) => {
|
|
|
|
const { basicPriceEstimatesLastRetrieved } = getState().gas
|
2018-12-10 22:51:00 +01:00
|
|
|
const timeLastRetrieved = basicPriceEstimatesLastRetrieved || loadLocalStorageData('BASIC_PRICE_ESTIMATES_LAST_RETRIEVED') || 0
|
|
|
|
|
2018-08-16 14:28:27 +02:00
|
|
|
dispatch(basicGasEstimatesLoadingStarted())
|
|
|
|
|
2019-07-04 21:18:12 +02:00
|
|
|
let basicEstimates
|
|
|
|
if (Date.now() - timeLastRetrieved > 75000) {
|
|
|
|
basicEstimates = await fetchExternalBasicGasEstimates(dispatch)
|
|
|
|
} else {
|
|
|
|
const cachedBasicEstimates = loadLocalStorageData('BASIC_PRICE_ESTIMATES')
|
|
|
|
basicEstimates = cachedBasicEstimates || await fetchExternalBasicGasEstimates(dispatch)
|
|
|
|
}
|
2018-12-10 22:51:00 +01:00
|
|
|
|
2019-07-04 21:18:12 +02:00
|
|
|
dispatch(setBasicGasEstimateData(basicEstimates))
|
|
|
|
dispatch(basicGasEstimatesLoadingFinished())
|
2018-12-10 22:51:00 +01:00
|
|
|
|
2019-07-04 21:18:12 +02:00
|
|
|
return basicEstimates
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function fetchExternalBasicGasEstimates (dispatch) {
|
2020-04-15 19:23:27 +02:00
|
|
|
const response = await window.fetch('https://ethgasstation.info/json/ethgasAPI.json', {
|
2019-07-04 21:18:12 +02:00
|
|
|
'headers': {},
|
2019-08-22 16:04:52 +02:00
|
|
|
'referrer': 'http://ethgasstation.info/json/',
|
2019-07-04 21:18:12 +02:00
|
|
|
'referrerPolicy': 'no-referrer-when-downgrade',
|
|
|
|
'body': null,
|
|
|
|
'method': 'GET',
|
2019-08-22 16:04:52 +02:00
|
|
|
'mode': 'cors',
|
|
|
|
})
|
|
|
|
|
2019-07-04 21:18:12 +02:00
|
|
|
const {
|
2019-08-22 16:04:52 +02:00
|
|
|
safeLow: safeLowTimes10,
|
|
|
|
average: averageTimes10,
|
|
|
|
fast: fastTimes10,
|
|
|
|
fastest: fastestTimes10,
|
2019-07-04 21:18:12 +02:00
|
|
|
block_time: blockTime,
|
|
|
|
blockNum,
|
|
|
|
} = await response.json()
|
2018-12-10 22:51:00 +01:00
|
|
|
|
2019-08-22 16:04:52 +02:00
|
|
|
const [average, fast, fastest, safeLow] = [
|
|
|
|
averageTimes10,
|
|
|
|
fastTimes10,
|
|
|
|
fastestTimes10,
|
|
|
|
safeLowTimes10,
|
2020-02-15 21:34:12 +01:00
|
|
|
].map((price) => (new BigNumber(price)).div(10).toNumber())
|
2019-08-22 16:04:52 +02:00
|
|
|
|
2019-07-04 21:18:12 +02:00
|
|
|
const basicEstimates = {
|
|
|
|
safeLow,
|
|
|
|
average,
|
|
|
|
fast,
|
|
|
|
fastest,
|
|
|
|
blockTime,
|
|
|
|
blockNum,
|
2018-11-13 17:36:52 +01:00
|
|
|
}
|
2019-07-04 21:18:12 +02:00
|
|
|
|
|
|
|
const timeRetrieved = Date.now()
|
|
|
|
saveLocalStorageData(basicEstimates, 'BASIC_PRICE_ESTIMATES')
|
|
|
|
saveLocalStorageData(timeRetrieved, 'BASIC_PRICE_ESTIMATES_LAST_RETRIEVED')
|
|
|
|
dispatch(setBasicPriceEstimatesLastRetrieved(timeRetrieved))
|
|
|
|
|
|
|
|
return basicEstimates
|
2018-11-13 17:36:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function fetchBasicGasAndTimeEstimates () {
|
2019-07-04 21:18:12 +02:00
|
|
|
return async (dispatch, getState) => {
|
|
|
|
const { basicPriceAndTimeEstimatesLastRetrieved } = getState().gas
|
2018-11-13 17:36:52 +01:00
|
|
|
const timeLastRetrieved = basicPriceAndTimeEstimatesLastRetrieved || loadLocalStorageData('BASIC_GAS_AND_TIME_API_ESTIMATES_LAST_RETRIEVED') || 0
|
|
|
|
|
|
|
|
dispatch(basicGasEstimatesLoadingStarted())
|
|
|
|
|
2019-07-04 21:18:12 +02:00
|
|
|
let basicEstimates
|
|
|
|
if (Date.now() - timeLastRetrieved > 75000) {
|
|
|
|
basicEstimates = await fetchExternalBasicGasAndTimeEstimates(dispatch)
|
|
|
|
} else {
|
|
|
|
const cachedBasicEstimates = loadLocalStorageData('BASIC_GAS_AND_TIME_API_ESTIMATES')
|
|
|
|
basicEstimates = cachedBasicEstimates || await fetchExternalBasicGasAndTimeEstimates(dispatch)
|
|
|
|
}
|
2018-11-27 18:30:41 +01:00
|
|
|
|
2019-07-04 21:18:12 +02:00
|
|
|
dispatch(setBasicGasEstimateData(basicEstimates))
|
|
|
|
dispatch(basicGasEstimatesLoadingFinished())
|
|
|
|
return basicEstimates
|
|
|
|
}
|
|
|
|
}
|
2018-11-13 17:36:52 +01:00
|
|
|
|
2019-07-04 21:18:12 +02:00
|
|
|
async function fetchExternalBasicGasAndTimeEstimates (dispatch) {
|
2020-04-15 19:23:27 +02:00
|
|
|
const response = await window.fetch('https://ethgasstation.info/json/ethgasAPI.json', {
|
2019-07-04 21:18:12 +02:00
|
|
|
'headers': {},
|
|
|
|
'referrer': 'http://ethgasstation.info/json/',
|
|
|
|
'referrerPolicy': 'no-referrer-when-downgrade',
|
|
|
|
'body': null,
|
|
|
|
'method': 'GET',
|
2019-08-22 16:04:52 +02:00
|
|
|
'mode': 'cors',
|
|
|
|
})
|
|
|
|
|
2019-07-04 21:18:12 +02:00
|
|
|
const {
|
|
|
|
average: averageTimes10,
|
|
|
|
avgWait,
|
|
|
|
block_time: blockTime,
|
|
|
|
blockNum,
|
|
|
|
fast: fastTimes10,
|
|
|
|
fastest: fastestTimes10,
|
|
|
|
fastestWait,
|
|
|
|
fastWait,
|
|
|
|
safeLow: safeLowTimes10,
|
|
|
|
safeLowWait,
|
|
|
|
speed,
|
|
|
|
} = await response.json()
|
|
|
|
const [average, fast, fastest, safeLow] = [
|
|
|
|
averageTimes10,
|
|
|
|
fastTimes10,
|
|
|
|
fastestTimes10,
|
|
|
|
safeLowTimes10,
|
2020-02-15 21:34:12 +01:00
|
|
|
].map((price) => (new BigNumber(price)).div(10).toNumber())
|
2018-11-13 17:36:52 +01:00
|
|
|
|
2019-07-04 21:18:12 +02:00
|
|
|
const basicEstimates = {
|
|
|
|
average,
|
|
|
|
avgWait,
|
|
|
|
blockTime,
|
|
|
|
blockNum,
|
|
|
|
fast,
|
|
|
|
fastest,
|
|
|
|
fastestWait,
|
|
|
|
fastWait,
|
|
|
|
safeLow,
|
|
|
|
safeLowWait,
|
|
|
|
speed,
|
2018-10-09 18:35:54 +02:00
|
|
|
}
|
2019-07-04 21:18:12 +02:00
|
|
|
|
|
|
|
const timeRetrieved = Date.now()
|
|
|
|
saveLocalStorageData(basicEstimates, 'BASIC_GAS_AND_TIME_API_ESTIMATES')
|
|
|
|
saveLocalStorageData(timeRetrieved, 'BASIC_GAS_AND_TIME_API_ESTIMATES_LAST_RETRIEVED')
|
|
|
|
dispatch(setBasicApiEstimatesLastRetrieved(timeRetrieved))
|
|
|
|
|
|
|
|
return basicEstimates
|
2018-10-09 18:35:54 +02:00
|
|
|
}
|
|
|
|
|
2018-11-13 18:36:35 +01:00
|
|
|
function extrapolateY ({ higherY, lowerY, higherX, lowerX, xForExtrapolation }) {
|
|
|
|
higherY = new BigNumber(higherY, 10)
|
|
|
|
lowerY = new BigNumber(lowerY, 10)
|
|
|
|
higherX = new BigNumber(higherX, 10)
|
|
|
|
lowerX = new BigNumber(lowerX, 10)
|
|
|
|
xForExtrapolation = new BigNumber(xForExtrapolation, 10)
|
|
|
|
const slope = (higherY.minus(lowerY)).div(higherX.minus(lowerX))
|
|
|
|
const newTimeEstimate = slope.times(higherX.minus(xForExtrapolation)).minus(higherY).negated()
|
|
|
|
|
|
|
|
return Number(newTimeEstimate.toPrecision(10))
|
|
|
|
}
|
|
|
|
|
|
|
|
function getRandomArbitrary (min, max) {
|
|
|
|
min = new BigNumber(min, 10)
|
|
|
|
max = new BigNumber(max, 10)
|
|
|
|
const random = new BigNumber(String(Math.random()), 10)
|
|
|
|
return new BigNumber(random.times(max.minus(min)).plus(min)).toPrecision(10)
|
|
|
|
}
|
|
|
|
|
|
|
|
function calcMedian (list) {
|
|
|
|
const medianPos = (Math.floor(list.length / 2) + Math.ceil(list.length / 2)) / 2
|
|
|
|
return medianPos === Math.floor(medianPos)
|
|
|
|
? (list[medianPos - 1] + list[medianPos]) / 2
|
|
|
|
: list[Math.floor(medianPos)]
|
|
|
|
}
|
|
|
|
|
|
|
|
function quartiles (data) {
|
|
|
|
const lowerHalf = data.slice(0, Math.floor(data.length / 2))
|
|
|
|
const upperHalf = data.slice(Math.floor(data.length / 2) + (data.length % 2 === 0 ? 0 : 1))
|
|
|
|
const median = calcMedian(data)
|
|
|
|
const lowerQuartile = calcMedian(lowerHalf)
|
|
|
|
const upperQuartile = calcMedian(upperHalf)
|
|
|
|
return {
|
|
|
|
median,
|
|
|
|
lowerQuartile,
|
|
|
|
upperQuartile,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function inliersByIQR (data, prop) {
|
2020-02-15 21:34:12 +01:00
|
|
|
const { lowerQuartile, upperQuartile } = quartiles(data.map((d) => (prop ? d[prop] : d)))
|
2018-11-13 18:36:35 +01:00
|
|
|
const IQR = upperQuartile - lowerQuartile
|
2020-02-15 21:56:48 +01:00
|
|
|
const lowerBound = lowerQuartile - (1.5 * IQR)
|
|
|
|
const upperBound = upperQuartile + (1.5 * IQR)
|
2020-02-15 21:34:12 +01:00
|
|
|
return data.filter((d) => {
|
2018-11-13 18:36:35 +01:00
|
|
|
const value = prop ? d[prop] : d
|
|
|
|
return value >= lowerBound && value <= upperBound
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-09 18:35:54 +02:00
|
|
|
export function fetchGasEstimates (blockTime) {
|
2018-10-24 08:31:45 +02:00
|
|
|
return (dispatch, getState) => {
|
2019-03-22 15:50:12 +01:00
|
|
|
const state = getState()
|
|
|
|
|
2019-04-03 18:01:43 +02:00
|
|
|
if (!isEthereumNetwork(state)) {
|
2019-03-22 15:50:12 +01:00
|
|
|
return Promise.resolve(null)
|
|
|
|
}
|
|
|
|
|
2018-10-24 08:31:45 +02:00
|
|
|
const {
|
|
|
|
priceAndTimeEstimatesLastRetrieved,
|
|
|
|
priceAndTimeEstimates,
|
2019-03-22 15:50:12 +01:00
|
|
|
} = state.gas
|
2018-11-05 17:50:06 +01:00
|
|
|
const timeLastRetrieved = priceAndTimeEstimatesLastRetrieved || loadLocalStorageData('GAS_API_ESTIMATES_LAST_RETRIEVED') || 0
|
2018-10-24 08:31:45 +02:00
|
|
|
|
2018-10-09 18:35:54 +02:00
|
|
|
dispatch(gasEstimatesLoadingStarted())
|
|
|
|
|
2018-10-24 08:31:45 +02:00
|
|
|
const promiseToFetch = Date.now() - timeLastRetrieved > 75000
|
2020-04-15 19:23:27 +02:00
|
|
|
? window.fetch('https://ethgasstation.info/json/predictTable.json', {
|
2019-07-31 22:17:11 +02:00
|
|
|
'headers': {},
|
|
|
|
'referrer': 'http://ethgasstation.info/json/',
|
|
|
|
'referrerPolicy': 'no-referrer-when-downgrade',
|
|
|
|
'body': null,
|
|
|
|
'method': 'GET',
|
2019-12-03 21:50:55 +01:00
|
|
|
'mode': 'cors' }
|
2019-07-31 22:17:11 +02:00
|
|
|
)
|
2020-02-15 21:34:12 +01:00
|
|
|
.then((r) => r.json())
|
|
|
|
.then((r) => {
|
2018-10-24 08:31:45 +02:00
|
|
|
const estimatedPricesAndTimes = r.map(({ expectedTime, expectedWait, gasprice }) => ({ expectedTime, expectedWait, gasprice }))
|
|
|
|
const estimatedTimeWithUniquePrices = uniqBy(({ expectedTime }) => expectedTime, estimatedPricesAndTimes)
|
2018-11-13 18:36:35 +01:00
|
|
|
|
|
|
|
const withSupplementalTimeEstimates = flatten(estimatedTimeWithUniquePrices.map(({ expectedWait, gasprice }, i, arr) => {
|
|
|
|
const next = arr[i + 1]
|
|
|
|
if (!next) {
|
|
|
|
return [{ expectedWait, gasprice }]
|
|
|
|
} else {
|
|
|
|
const supplementalPrice = getRandomArbitrary(gasprice, next.gasprice)
|
|
|
|
const supplementalTime = extrapolateY({
|
|
|
|
higherY: next.expectedWait,
|
|
|
|
lowerY: expectedWait,
|
|
|
|
higherX: next.gasprice,
|
|
|
|
lowerX: gasprice,
|
|
|
|
xForExtrapolation: supplementalPrice,
|
|
|
|
})
|
|
|
|
const supplementalPrice2 = getRandomArbitrary(supplementalPrice, next.gasprice)
|
|
|
|
const supplementalTime2 = extrapolateY({
|
|
|
|
higherY: next.expectedWait,
|
|
|
|
lowerY: supplementalTime,
|
|
|
|
higherX: next.gasprice,
|
|
|
|
lowerX: supplementalPrice,
|
|
|
|
xForExtrapolation: supplementalPrice2,
|
|
|
|
})
|
|
|
|
return [
|
|
|
|
{ expectedWait, gasprice },
|
|
|
|
{ expectedWait: supplementalTime, gasprice: supplementalPrice },
|
|
|
|
{ expectedWait: supplementalTime2, gasprice: supplementalPrice2 },
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
const withOutliersRemoved = inliersByIQR(withSupplementalTimeEstimates.slice(0).reverse(), 'expectedWait').reverse()
|
|
|
|
const timeMappedToSeconds = withOutliersRemoved.map(({ expectedWait, gasprice }) => {
|
|
|
|
const expectedTime = (new BigNumber(expectedWait)).times(Number(blockTime), 10).toNumber()
|
2018-10-24 08:31:45 +02:00
|
|
|
return {
|
|
|
|
expectedTime,
|
2018-11-13 18:36:35 +01:00
|
|
|
gasprice: (new BigNumber(gasprice, 10).toNumber()),
|
2018-10-24 08:31:45 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const timeRetrieved = Date.now()
|
|
|
|
dispatch(setApiEstimatesLastRetrieved(timeRetrieved))
|
|
|
|
saveLocalStorageData(timeRetrieved, 'GAS_API_ESTIMATES_LAST_RETRIEVED')
|
2018-11-13 18:36:35 +01:00
|
|
|
saveLocalStorageData(timeMappedToSeconds, 'GAS_API_ESTIMATES')
|
2018-10-24 08:31:45 +02:00
|
|
|
|
2018-11-13 18:36:35 +01:00
|
|
|
return timeMappedToSeconds
|
2018-10-09 18:35:54 +02:00
|
|
|
})
|
2018-10-24 08:31:45 +02:00
|
|
|
: Promise.resolve(priceAndTimeEstimates.length
|
2019-07-31 22:17:11 +02:00
|
|
|
? priceAndTimeEstimates
|
|
|
|
: loadLocalStorageData('GAS_API_ESTIMATES')
|
|
|
|
)
|
|
|
|
|
2020-02-15 21:34:12 +01:00
|
|
|
return promiseToFetch.then((estimates) => {
|
2019-07-31 22:17:11 +02:00
|
|
|
dispatch(setPricesAndTimeEstimates(estimates))
|
|
|
|
dispatch(gasEstimatesLoadingFinished())
|
|
|
|
})
|
2018-08-16 14:28:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-09 21:48:06 +01:00
|
|
|
export function setCustomGasPriceForRetry (newPrice) {
|
|
|
|
return (dispatch) => {
|
|
|
|
if (newPrice !== '0x0') {
|
|
|
|
dispatch(setCustomGasPrice(newPrice))
|
|
|
|
} else {
|
|
|
|
const { fast } = loadLocalStorageData('BASIC_PRICE_ESTIMATES')
|
|
|
|
dispatch(setCustomGasPrice(decGWEIToHexWEI(fast)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-16 14:28:27 +02:00
|
|
|
export function setBasicGasEstimateData (basicGasEstimateData) {
|
|
|
|
return {
|
|
|
|
type: SET_BASIC_GAS_ESTIMATE_DATA,
|
|
|
|
value: basicGasEstimateData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 18:35:54 +02:00
|
|
|
export function setPricesAndTimeEstimates (estimatedPricesAndTimes) {
|
|
|
|
return {
|
|
|
|
type: SET_PRICE_AND_TIME_ESTIMATES,
|
|
|
|
value: estimatedPricesAndTimes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-16 14:28:27 +02:00
|
|
|
export function setCustomGasPrice (newPrice) {
|
|
|
|
return {
|
|
|
|
type: SET_CUSTOM_GAS_PRICE,
|
|
|
|
value: newPrice,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setCustomGasLimit (newLimit) {
|
|
|
|
return {
|
|
|
|
type: SET_CUSTOM_GAS_LIMIT,
|
|
|
|
value: newLimit,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setCustomGasTotal (newTotal) {
|
|
|
|
return {
|
|
|
|
type: SET_CUSTOM_GAS_TOTAL,
|
|
|
|
value: newTotal,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setCustomGasErrors (newErrors) {
|
|
|
|
return {
|
|
|
|
type: SET_CUSTOM_GAS_ERRORS,
|
|
|
|
value: newErrors,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-24 08:31:45 +02:00
|
|
|
export function setApiEstimatesLastRetrieved (retrievalTime) {
|
|
|
|
return {
|
|
|
|
type: SET_API_ESTIMATES_LAST_RETRIEVED,
|
|
|
|
value: retrievalTime,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-13 17:36:52 +01:00
|
|
|
export function setBasicApiEstimatesLastRetrieved (retrievalTime) {
|
|
|
|
return {
|
|
|
|
type: SET_BASIC_API_ESTIMATES_LAST_RETRIEVED,
|
|
|
|
value: retrievalTime,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-10 22:51:00 +01:00
|
|
|
export function setBasicPriceEstimatesLastRetrieved (retrievalTime) {
|
|
|
|
return {
|
|
|
|
type: SET_BASIC_PRICE_ESTIMATES_LAST_RETRIEVED,
|
|
|
|
value: retrievalTime,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-16 14:28:27 +02:00
|
|
|
export function resetCustomGasState () {
|
|
|
|
return { type: RESET_CUSTOM_GAS_STATE }
|
|
|
|
}
|
2018-09-20 18:06:23 +02:00
|
|
|
|
|
|
|
export function resetCustomData () {
|
|
|
|
return { type: RESET_CUSTOM_DATA }
|
|
|
|
}
|