1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-18 07:23:21 +01:00
metamask-extension/ui/app/ducks/gas/gas.duck.js

211 lines
5.4 KiB
JavaScript
Raw Normal View History

import { cloneDeep } from 'lodash'
import BigNumber from 'bignumber.js'
import { getStorageItem, setStorageItem } from '../../../lib/storage-helpers'
2020-11-03 00:41:28 +01:00
import { decGWEIToHexWEI } from '../../helpers/utils/conversions.util'
import getFetchWithTimeout from '../../../../shared/modules/fetch-with-timeout'
const fetchWithTimeout = getFetchWithTimeout(30000)
// Actions
2020-11-03 00:41:28 +01:00
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'
const RESET_CUSTOM_GAS_STATE = 'metamask/gas/RESET_CUSTOM_GAS_STATE'
const RESET_CUSTOM_DATA = 'metamask/gas/RESET_CUSTOM_DATA'
const SET_BASIC_GAS_ESTIMATE_DATA = 'metamask/gas/SET_BASIC_GAS_ESTIMATE_DATA'
const SET_CUSTOM_GAS_LIMIT = 'metamask/gas/SET_CUSTOM_GAS_LIMIT'
const SET_CUSTOM_GAS_PRICE = 'metamask/gas/SET_CUSTOM_GAS_PRICE'
2020-11-03 00:41:28 +01:00
const SET_BASIC_PRICE_ESTIMATES_LAST_RETRIEVED =
'metamask/gas/SET_BASIC_PRICE_ESTIMATES_LAST_RETRIEVED'
const initState = {
customData: {
price: null,
limit: null,
},
basicEstimates: {
safeLow: null,
average: null,
fast: null,
},
basicEstimateIsLoading: true,
basicPriceEstimatesLastRetrieved: 0,
}
// Reducer
2020-11-03 00:41:28 +01:00
export default function reducer(state = initState, action) {
switch (action.type) {
case BASIC_GAS_ESTIMATE_LOADING_STARTED:
return {
...state,
basicEstimateIsLoading: true,
}
case BASIC_GAS_ESTIMATE_LOADING_FINISHED:
return {
...state,
basicEstimateIsLoading: false,
}
case SET_BASIC_GAS_ESTIMATE_DATA:
return {
...state,
basicEstimates: action.value,
}
case SET_CUSTOM_GAS_PRICE:
return {
...state,
customData: {
...state.customData,
price: action.value,
},
}
case SET_CUSTOM_GAS_LIMIT:
return {
...state,
customData: {
...state.customData,
limit: action.value,
},
}
case SET_BASIC_PRICE_ESTIMATES_LAST_RETRIEVED:
return {
...state,
basicPriceEstimatesLastRetrieved: action.value,
}
case RESET_CUSTOM_DATA:
return {
...state,
2020-07-08 22:17:53 +02:00
customData: cloneDeep(initState.customData),
}
case RESET_CUSTOM_GAS_STATE:
2020-07-08 22:17:53 +02:00
return cloneDeep(initState)
default:
return state
}
}
// Action Creators
2020-11-03 00:41:28 +01:00
export function basicGasEstimatesLoadingStarted() {
return {
type: BASIC_GAS_ESTIMATE_LOADING_STARTED,
}
}
2020-11-03 00:41:28 +01:00
export function basicGasEstimatesLoadingFinished() {
return {
type: BASIC_GAS_ESTIMATE_LOADING_FINISHED,
}
}
async function basicGasPriceQuery() {
const url = `https://api.metaswap.codefi.network/gasPrices`
return await fetchWithTimeout(url, {
2020-11-03 00:41:28 +01:00
headers: {},
referrer: 'https://api.metaswap.codefi.network/gasPrices',
2020-11-03 00:41:28 +01:00
referrerPolicy: 'no-referrer-when-downgrade',
body: null,
method: 'GET',
mode: 'cors',
})
}
2020-11-03 00:41:28 +01:00
export function fetchBasicGasEstimates() {
return async (dispatch, getState) => {
const { basicPriceEstimatesLastRetrieved } = getState().gas
2020-11-03 00:41:28 +01:00
const timeLastRetrieved =
basicPriceEstimatesLastRetrieved ||
(await getStorageItem('BASIC_PRICE_ESTIMATES_LAST_RETRIEVED')) ||
2020-11-03 00:41:28 +01:00
0
dispatch(basicGasEstimatesLoadingStarted())
let basicEstimates
if (Date.now() - timeLastRetrieved > 75000) {
basicEstimates = await fetchExternalBasicGasEstimates(dispatch)
} else {
const cachedBasicEstimates = await getStorageItem('BASIC_PRICE_ESTIMATES')
2020-11-03 00:41:28 +01:00
basicEstimates =
cachedBasicEstimates || (await fetchExternalBasicGasEstimates(dispatch))
}
dispatch(setBasicGasEstimateData(basicEstimates))
dispatch(basicGasEstimatesLoadingFinished())
return basicEstimates
}
}
2020-11-03 00:41:28 +01:00
async function fetchExternalBasicGasEstimates(dispatch) {
const response = await basicGasPriceQuery()
const { SafeGasPrice, ProposeGasPrice, FastGasPrice } = await response.json()
const [safeLow, average, fast] = [
SafeGasPrice,
ProposeGasPrice,
FastGasPrice,
].map((price) => new BigNumber(price, 10).toNumber())
const basicEstimates = {
safeLow,
average,
fast,
}
const timeRetrieved = Date.now()
await Promise.all([
setStorageItem('BASIC_PRICE_ESTIMATES', basicEstimates),
setStorageItem('BASIC_PRICE_ESTIMATES_LAST_RETRIEVED', timeRetrieved),
])
dispatch(setBasicPriceEstimatesLastRetrieved(timeRetrieved))
return basicEstimates
}
2020-11-03 00:41:28 +01:00
export function setCustomGasPriceForRetry(newPrice) {
return async (dispatch) => {
if (newPrice === '0x0') {
const { fast } = await getStorageItem('BASIC_PRICE_ESTIMATES')
2018-12-09 21:48:06 +01:00
dispatch(setCustomGasPrice(decGWEIToHexWEI(fast)))
} else {
dispatch(setCustomGasPrice(newPrice))
2018-12-09 21:48:06 +01:00
}
}
}
2020-11-03 00:41:28 +01:00
export function setBasicGasEstimateData(basicGasEstimateData) {
return {
type: SET_BASIC_GAS_ESTIMATE_DATA,
value: basicGasEstimateData,
}
}
2020-11-03 00:41:28 +01:00
export function setCustomGasPrice(newPrice) {
return {
type: SET_CUSTOM_GAS_PRICE,
value: newPrice,
}
}
2020-11-03 00:41:28 +01:00
export function setCustomGasLimit(newLimit) {
return {
type: SET_CUSTOM_GAS_LIMIT,
value: newLimit,
}
}
2020-11-03 00:41:28 +01:00
export function setBasicPriceEstimatesLastRetrieved(retrievalTime) {
return {
type: SET_BASIC_PRICE_ESTIMATES_LAST_RETRIEVED,
value: retrievalTime,
}
}
2020-11-03 00:41:28 +01:00
export function resetCustomGasState() {
return { type: RESET_CUSTOM_GAS_STATE }
}
2020-11-03 00:41:28 +01:00
export function resetCustomData() {
return { type: RESET_CUSTOM_DATA }
}