You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
237 lines
5.8 KiB
237 lines
5.8 KiB
4 years ago
|
import { cloneDeep } from 'lodash';
|
||
|
import BigNumber from 'bignumber.js';
|
||
4 years ago
|
import {
|
||
|
getStorageItem,
|
||
|
setStorageItem,
|
||
|
} from '../../helpers/utils/storage-helpers';
|
||
4 years ago
|
import {
|
||
|
decGWEIToHexWEI,
|
||
|
getValueFromWeiHex,
|
||
|
} from '../../helpers/utils/conversions.util';
|
||
|
import { getIsMainnet, getCurrentChainId } from '../../selectors';
|
||
4 years ago
|
import fetchWithCache from '../../helpers/utils/fetch-with-cache';
|
||
6 years ago
|
|
||
4 years ago
|
const BASIC_ESTIMATE_STATES = {
|
||
|
LOADING: 'LOADING',
|
||
|
FAILED: 'FAILED',
|
||
|
READY: 'READY',
|
||
|
};
|
||
|
|
||
|
const GAS_SOURCE = {
|
||
|
METASWAPS: 'MetaSwaps',
|
||
|
ETHGASPRICE: 'eth_gasprice',
|
||
|
};
|
||
|
|
||
6 years ago
|
// Actions
|
||
4 years ago
|
const BASIC_GAS_ESTIMATE_STATUS = 'metamask/gas/BASIC_GAS_ESTIMATE_STATUS';
|
||
4 years ago
|
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';
|
||
4 years ago
|
const SET_ESTIMATE_SOURCE = 'metamask/gas/SET_ESTIMATE_SOURCE';
|
||
6 years ago
|
|
||
|
const initState = {
|
||
|
customData: {
|
||
6 years ago
|
price: null,
|
||
6 years ago
|
limit: null,
|
||
6 years ago
|
},
|
||
|
basicEstimates: {
|
||
4 years ago
|
safeLow: null,
|
||
6 years ago
|
average: null,
|
||
|
fast: null,
|
||
|
},
|
||
4 years ago
|
basicEstimateStatus: BASIC_ESTIMATE_STATES.LOADING,
|
||
|
estimateSource: '',
|
||
4 years ago
|
};
|
||
6 years ago
|
|
||
|
// Reducer
|
||
4 years ago
|
export default function reducer(state = initState, action) {
|
||
6 years ago
|
switch (action.type) {
|
||
4 years ago
|
case BASIC_GAS_ESTIMATE_STATUS:
|
||
6 years ago
|
return {
|
||
5 years ago
|
...state,
|
||
4 years ago
|
basicEstimateStatus: action.value,
|
||
4 years ago
|
};
|
||
6 years ago
|
case SET_BASIC_GAS_ESTIMATE_DATA:
|
||
|
return {
|
||
5 years ago
|
...state,
|
||
6 years ago
|
basicEstimates: action.value,
|
||
4 years ago
|
};
|
||
6 years ago
|
case SET_CUSTOM_GAS_PRICE:
|
||
|
return {
|
||
5 years ago
|
...state,
|
||
6 years ago
|
customData: {
|
||
5 years ago
|
...state.customData,
|
||
6 years ago
|
price: action.value,
|
||
|
},
|
||
4 years ago
|
};
|
||
6 years ago
|
case SET_CUSTOM_GAS_LIMIT:
|
||
|
return {
|
||
5 years ago
|
...state,
|
||
6 years ago
|
customData: {
|
||
5 years ago
|
...state.customData,
|
||
6 years ago
|
limit: action.value,
|
||
|
},
|
||
4 years ago
|
};
|
||
6 years ago
|
case RESET_CUSTOM_DATA:
|
||
|
return {
|
||
5 years ago
|
...state,
|
||
4 years ago
|
customData: cloneDeep(initState.customData),
|
||
4 years ago
|
};
|
||
4 years ago
|
case SET_ESTIMATE_SOURCE:
|
||
|
return {
|
||
|
...state,
|
||
|
estimateSource: action.value,
|
||
|
};
|
||
6 years ago
|
default:
|
||
4 years ago
|
return state;
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// Action Creators
|
||
4 years ago
|
export function setBasicEstimateStatus(status) {
|
||
6 years ago
|
return {
|
||
4 years ago
|
type: BASIC_GAS_ESTIMATE_STATUS,
|
||
|
value: status,
|
||
4 years ago
|
};
|
||
6 years ago
|
}
|
||
|
|
||
4 years ago
|
async function basicGasPriceQuery() {
|
||
4 years ago
|
const url = `https://api.metaswap.codefi.network/gasPrices`;
|
||
4 years ago
|
return await fetchWithCache(
|
||
|
url,
|
||
|
{
|
||
|
referrer: url,
|
||
|
referrerPolicy: 'no-referrer-when-downgrade',
|
||
|
method: 'GET',
|
||
|
mode: 'cors',
|
||
|
},
|
||
|
{ cacheRefreshTime: 75000 },
|
||
|
);
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
export function fetchBasicGasEstimates() {
|
||
5 years ago
|
return async (dispatch, getState) => {
|
||
4 years ago
|
const isMainnet = getIsMainnet(getState());
|
||
6 years ago
|
|
||
4 years ago
|
dispatch(setBasicEstimateStatus(BASIC_ESTIMATE_STATES.LOADING));
|
||
4 years ago
|
let basicEstimates;
|
||
4 years ago
|
try {
|
||
|
dispatch(setEstimateSource(GAS_SOURCE.ETHGASPRICE));
|
||
|
if (isMainnet || process.env.IN_TEST) {
|
||
|
try {
|
||
|
basicEstimates = await fetchExternalBasicGasEstimates();
|
||
|
dispatch(setEstimateSource(GAS_SOURCE.METASWAPS));
|
||
|
} catch (error) {
|
||
|
basicEstimates = await fetchEthGasPriceEstimates(getState());
|
||
|
}
|
||
|
} else {
|
||
|
basicEstimates = await fetchEthGasPriceEstimates(getState());
|
||
|
}
|
||
|
dispatch(setBasicGasEstimateData(basicEstimates));
|
||
|
dispatch(setBasicEstimateStatus(BASIC_ESTIMATE_STATES.READY));
|
||
|
} catch (error) {
|
||
|
dispatch(setBasicEstimateStatus(BASIC_ESTIMATE_STATES.FAILED));
|
||
5 years ago
|
}
|
||
6 years ago
|
|
||
4 years ago
|
return basicEstimates;
|
||
|
};
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
async function fetchExternalBasicGasEstimates() {
|
||
|
const {
|
||
|
SafeGasPrice,
|
||
|
ProposeGasPrice,
|
||
|
FastGasPrice,
|
||
|
} = await basicGasPriceQuery();
|
||
6 years ago
|
|
||
4 years ago
|
const [safeLow, average, fast] = [
|
||
|
SafeGasPrice,
|
||
|
ProposeGasPrice,
|
||
|
FastGasPrice,
|
||
4 years ago
|
].map((price) => new BigNumber(price, 10).toNumber());
|
||
5 years ago
|
|
||
5 years ago
|
const basicEstimates = {
|
||
|
safeLow,
|
||
|
average,
|
||
|
fast,
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
4 years ago
|
return basicEstimates;
|
||
|
}
|
||
|
|
||
|
async function fetchEthGasPriceEstimates(state) {
|
||
|
const chainId = getCurrentChainId(state);
|
||
|
const [cachedTimeLastRetrieved, cachedBasicEstimates] = await Promise.all([
|
||
|
getStorageItem(`${chainId}_BASIC_PRICE_ESTIMATES_LAST_RETRIEVED`),
|
||
|
getStorageItem(`${chainId}_BASIC_PRICE_ESTIMATES`),
|
||
|
]);
|
||
|
const timeLastRetrieved = cachedTimeLastRetrieved || 0;
|
||
|
if (cachedBasicEstimates && Date.now() - timeLastRetrieved < 75000) {
|
||
|
return cachedBasicEstimates;
|
||
|
}
|
||
|
const gasPrice = await global.eth.gasPrice();
|
||
|
const averageGasPriceInDecGWEI = getValueFromWeiHex({
|
||
|
value: gasPrice.toString(16),
|
||
|
numberOfDecimals: 4,
|
||
|
toDenomination: 'GWEI',
|
||
|
});
|
||
|
const basicEstimates = {
|
||
|
average: Number(averageGasPriceInDecGWEI),
|
||
|
};
|
||
|
const timeRetrieved = Date.now();
|
||
|
|
||
|
await Promise.all([
|
||
|
setStorageItem(`${chainId}_BASIC_PRICE_ESTIMATES`, basicEstimates),
|
||
|
setStorageItem(
|
||
|
`${chainId}_BASIC_PRICE_ESTIMATES_LAST_RETRIEVED`,
|
||
|
timeRetrieved,
|
||
|
),
|
||
|
]);
|
||
5 years ago
|
|
||
4 years ago
|
return basicEstimates;
|
||
6 years ago
|
}
|
||
|
|
||
4 years ago
|
export function setCustomGasPriceForRetry(newPrice) {
|
||
4 years ago
|
return async (dispatch) => {
|
||
4 years ago
|
if (newPrice === '0x0') {
|
||
4 years ago
|
const { fast } = await fetchExternalBasicGasEstimates();
|
||
4 years ago
|
dispatch(setCustomGasPrice(decGWEIToHexWEI(fast)));
|
||
4 years ago
|
} else {
|
||
4 years ago
|
dispatch(setCustomGasPrice(newPrice));
|
||
6 years ago
|
}
|
||
4 years ago
|
};
|
||
6 years ago
|
}
|
||
|
|
||
4 years ago
|
export function setBasicGasEstimateData(basicGasEstimateData) {
|
||
6 years ago
|
return {
|
||
|
type: SET_BASIC_GAS_ESTIMATE_DATA,
|
||
|
value: basicGasEstimateData,
|
||
4 years ago
|
};
|
||
6 years ago
|
}
|
||
|
|
||
4 years ago
|
export function setCustomGasPrice(newPrice) {
|
||
6 years ago
|
return {
|
||
|
type: SET_CUSTOM_GAS_PRICE,
|
||
|
value: newPrice,
|
||
4 years ago
|
};
|
||
6 years ago
|
}
|
||
|
|
||
4 years ago
|
export function setCustomGasLimit(newLimit) {
|
||
6 years ago
|
return {
|
||
|
type: SET_CUSTOM_GAS_LIMIT,
|
||
|
value: newLimit,
|
||
4 years ago
|
};
|
||
6 years ago
|
}
|
||
|
|
||
4 years ago
|
export function resetCustomData() {
|
||
4 years ago
|
return { type: RESET_CUSTOM_DATA };
|
||
6 years ago
|
}
|
||
4 years ago
|
|
||
|
export function setEstimateSource(estimateSource) {
|
||
|
return {
|
||
|
type: SET_ESTIMATE_SOURCE,
|
||
|
value: estimateSource,
|
||
|
};
|
||
|
}
|