1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Fix #10081 - Use fetchWithCache to retrieve and store basic gas estimates (#10384)

This commit is contained in:
David Walsh 2021-03-09 13:49:27 -06:00 committed by GitHub
parent aeffe176b3
commit 444d8dd51a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 211 deletions

View File

@ -1,48 +1,26 @@
import assert from 'assert'; import assert from 'assert';
import nock from 'nock'; import nock from 'nock';
import sinon from 'sinon'; import sinon from 'sinon';
import proxyquire from 'proxyquire';
import BN from 'bn.js'; import BN from 'bn.js';
const fakeStorage = {}; import GasDuck, {
const GasDuck = proxyquire('./gas.duck.js', {
'../../../lib/storage-helpers': fakeStorage,
});
const {
basicGasEstimatesLoadingStarted, basicGasEstimatesLoadingStarted,
basicGasEstimatesLoadingFinished, basicGasEstimatesLoadingFinished,
setBasicGasEstimateData, setBasicGasEstimateData,
setCustomGasPrice, setCustomGasPrice,
setCustomGasLimit, setCustomGasLimit,
resetCustomGasState,
fetchBasicGasEstimates, fetchBasicGasEstimates,
} = GasDuck; } from './gas.duck';
const GasReducer = GasDuck.default;
describe('Gas Duck', function () {
let tempDateNow;
const mockGasPriceApiResponse = { const mockGasPriceApiResponse = {
SafeGasPrice: 10, SafeGasPrice: 10,
ProposeGasPrice: 20, ProposeGasPrice: 20,
FastGasPrice: 30, FastGasPrice: 30,
}; };
beforeEach(function () { const GasReducer = GasDuck;
tempDateNow = global.Date.now;
fakeStorage.getStorageItem = sinon.stub();
fakeStorage.setStorageItem = sinon.spy();
global.Date.now = () => 2000000;
});
afterEach(function () {
sinon.restore();
global.Date.now = tempDateNow;
});
describe('Gas Duck', function () {
const mockState = { const mockState = {
mockProp: 123, mockProp: 123,
}; };
@ -57,7 +35,6 @@ describe('Gas Duck', function () {
safeLow: null, safeLow: null,
}, },
basicEstimateIsLoading: true, basicEstimateIsLoading: true,
basicPriceEstimatesLastRetrieved: 0,
}; };
const providerState = { const providerState = {
@ -73,13 +50,10 @@ describe('Gas Duck', function () {
'metamask/gas/BASIC_GAS_ESTIMATE_LOADING_FINISHED'; 'metamask/gas/BASIC_GAS_ESTIMATE_LOADING_FINISHED';
const BASIC_GAS_ESTIMATE_LOADING_STARTED = const BASIC_GAS_ESTIMATE_LOADING_STARTED =
'metamask/gas/BASIC_GAS_ESTIMATE_LOADING_STARTED'; 'metamask/gas/BASIC_GAS_ESTIMATE_LOADING_STARTED';
const RESET_CUSTOM_GAS_STATE = 'metamask/gas/RESET_CUSTOM_GAS_STATE';
const SET_BASIC_GAS_ESTIMATE_DATA = const SET_BASIC_GAS_ESTIMATE_DATA =
'metamask/gas/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_LIMIT = 'metamask/gas/SET_CUSTOM_GAS_LIMIT';
const SET_CUSTOM_GAS_PRICE = 'metamask/gas/SET_CUSTOM_GAS_PRICE'; const SET_CUSTOM_GAS_PRICE = 'metamask/gas/SET_CUSTOM_GAS_PRICE';
const SET_BASIC_PRICE_ESTIMATES_LAST_RETRIEVED =
'metamask/gas/SET_BASIC_PRICE_ESTIMATES_LAST_RETRIEVED';
describe('GasReducer()', function () { describe('GasReducer()', function () {
it('should initialize state', function () { it('should initialize state', function () {
@ -139,13 +113,6 @@ describe('Gas Duck', function () {
{ customData: { limit: 9876 }, ...mockState }, { customData: { limit: 9876 }, ...mockState },
); );
}); });
it('should return the initial state in response to a RESET_CUSTOM_GAS_STATE action', function () {
assert.deepStrictEqual(
GasReducer(mockState, { type: RESET_CUSTOM_GAS_STATE }),
initState,
);
});
}); });
describe('basicGasEstimatesLoadingStarted', function () { describe('basicGasEstimatesLoadingStarted', function () {
@ -167,7 +134,6 @@ describe('Gas Duck', function () {
describe('fetchBasicGasEstimates', function () { describe('fetchBasicGasEstimates', function () {
it('should call fetch with the expected params', async function () { it('should call fetch with the expected params', async function () {
const mockDistpatch = sinon.spy(); const mockDistpatch = sinon.spy();
const windowFetchSpy = sinon.spy(window, 'fetch'); const windowFetchSpy = sinon.spy(window, 'fetch');
nock('https://api.metaswap.codefi.network') nock('https://api.metaswap.codefi.network')
@ -175,32 +141,21 @@ describe('Gas Duck', function () {
.reply(200, mockGasPriceApiResponse); .reply(200, mockGasPriceApiResponse);
await fetchBasicGasEstimates()(mockDistpatch, () => ({ await fetchBasicGasEstimates()(mockDistpatch, () => ({
gas: { ...initState, basicPriceAEstimatesLastRetrieved: 1000000 }, gas: { ...initState },
metamask: { provider: { ...providerState } }, metamask: { provider: { ...providerState } },
})); }));
assert.deepStrictEqual(mockDistpatch.getCall(0).args, [ assert.deepStrictEqual(mockDistpatch.getCall(0).args, [
{ type: BASIC_GAS_ESTIMATE_LOADING_STARTED }, { type: BASIC_GAS_ESTIMATE_LOADING_STARTED },
]); ]);
assert.ok( assert.ok(
windowFetchSpy windowFetchSpy
.getCall(0) .getCall(0)
.args[0].startsWith('https://api.metaswap.codefi.network/gasPrices'), .args[0].startsWith('https://api.metaswap.codefi.network/gasPrices'),
'should fetch metaswap /gasPrices', 'should fetch metaswap /gasPrices',
); );
assert.deepStrictEqual(mockDistpatch.getCall(1).args, [
{ type: SET_BASIC_PRICE_ESTIMATES_LAST_RETRIEVED, value: 2000000 },
]);
assert.deepStrictEqual(mockDistpatch.getCall(2).args, [ assert.deepStrictEqual(mockDistpatch.getCall(2).args, [
{
type: SET_BASIC_GAS_ESTIMATE_DATA,
value: {
average: 20,
fast: 30,
safeLow: 10,
},
},
]);
assert.deepStrictEqual(mockDistpatch.getCall(3).args, [
{ type: BASIC_GAS_ESTIMATE_LOADING_FINISHED }, { type: BASIC_GAS_ESTIMATE_LOADING_FINISHED },
]); ]);
}); });
@ -209,7 +164,7 @@ describe('Gas Duck', function () {
global.eth = { gasPrice: sinon.fake.returns(new BN(48199313, 10)) }; global.eth = { gasPrice: sinon.fake.returns(new BN(48199313, 10)) };
const mockDistpatch = sinon.spy(); const mockDistpatch = sinon.spy();
const providerStateForTestNetwrok = { const providerStateForTestNetwork = {
chainId: '0x5', chainId: '0x5',
nickname: '', nickname: '',
rpcPrefs: {}, rpcPrefs: {},
@ -219,8 +174,8 @@ describe('Gas Duck', function () {
}; };
await fetchBasicGasEstimates()(mockDistpatch, () => ({ await fetchBasicGasEstimates()(mockDistpatch, () => ({
gas: { ...initState, basicPriceAEstimatesLastRetrieved: 1000000 }, gas: { ...initState },
metamask: { provider: { ...providerStateForTestNetwrok } }, metamask: { provider: { ...providerStateForTestNetwork } },
})); }));
assert.deepStrictEqual(mockDistpatch.getCall(0).args, [ assert.deepStrictEqual(mockDistpatch.getCall(0).args, [
{ type: BASIC_GAS_ESTIMATE_LOADING_STARTED }, { type: BASIC_GAS_ESTIMATE_LOADING_STARTED },
@ -237,88 +192,6 @@ describe('Gas Duck', function () {
{ type: BASIC_GAS_ESTIMATE_LOADING_FINISHED }, { type: BASIC_GAS_ESTIMATE_LOADING_FINISHED },
]); ]);
}); });
it('should fetch recently retrieved estimates from storage', async function () {
const mockDistpatch = sinon.spy();
const windowFetchSpy = sinon.spy(window, 'fetch');
fakeStorage.getStorageItem
.withArgs('BASIC_PRICE_ESTIMATES_LAST_RETRIEVED')
.returns(2000000 - 1); // one second ago from "now"
fakeStorage.getStorageItem.withArgs('BASIC_PRICE_ESTIMATES').returns({
average: 25,
fast: 35,
safeLow: 15,
});
await fetchBasicGasEstimates()(mockDistpatch, () => ({
gas: { ...initState },
metamask: { provider: { ...providerState } },
}));
assert.deepStrictEqual(mockDistpatch.getCall(0).args, [
{ type: BASIC_GAS_ESTIMATE_LOADING_STARTED },
]);
assert.ok(windowFetchSpy.notCalled);
assert.deepStrictEqual(mockDistpatch.getCall(1).args, [
{
type: SET_BASIC_GAS_ESTIMATE_DATA,
value: {
average: 25,
fast: 35,
safeLow: 15,
},
},
]);
assert.deepStrictEqual(mockDistpatch.getCall(2).args, [
{ type: BASIC_GAS_ESTIMATE_LOADING_FINISHED },
]);
});
it('should fallback to network if retrieving estimates from storage fails', async function () {
const mockDistpatch = sinon.spy();
const windowFetchSpy = sinon.spy(window, 'fetch');
nock('https://api.metaswap.codefi.network')
.get('/gasPrices')
.reply(200, mockGasPriceApiResponse);
fakeStorage.getStorageItem
.withArgs('BASIC_PRICE_ESTIMATES_LAST_RETRIEVED')
.returns(2000000 - 1); // one second ago from "now"
await fetchBasicGasEstimates()(mockDistpatch, () => ({
gas: { ...initState },
metamask: { provider: { ...providerState } },
}));
assert.deepStrictEqual(mockDistpatch.getCall(0).args, [
{ type: BASIC_GAS_ESTIMATE_LOADING_STARTED },
]);
assert.ok(
windowFetchSpy
.getCall(0)
.args[0].startsWith('https://api.metaswap.codefi.network/gasPrices'),
'should fetch metaswap /gasPrices',
);
assert.deepStrictEqual(mockDistpatch.getCall(1).args, [
{ type: SET_BASIC_PRICE_ESTIMATES_LAST_RETRIEVED, value: 2000000 },
]);
assert.deepStrictEqual(mockDistpatch.getCall(2).args, [
{
type: SET_BASIC_GAS_ESTIMATE_DATA,
value: {
safeLow: 10,
average: 20,
fast: 30,
},
},
]);
assert.deepStrictEqual(mockDistpatch.getCall(3).args, [
{ type: BASIC_GAS_ESTIMATE_LOADING_FINISHED },
]);
});
}); });
describe('setBasicGasEstimateData', function () { describe('setBasicGasEstimateData', function () {
@ -347,12 +220,4 @@ describe('Gas Duck', function () {
}); });
}); });
}); });
describe('resetCustomGasState', function () {
it('should create the correct action', function () {
assert.deepStrictEqual(resetCustomGasState(), {
type: RESET_CUSTOM_GAS_STATE,
});
});
});
}); });

View File

@ -5,23 +5,18 @@ import {
decGWEIToHexWEI, decGWEIToHexWEI,
getValueFromWeiHex, getValueFromWeiHex,
} from '../../helpers/utils/conversions.util'; } from '../../helpers/utils/conversions.util';
import getFetchWithTimeout from '../../../../shared/modules/fetch-with-timeout';
import { getIsMainnet, getCurrentChainId } from '../../selectors'; import { getIsMainnet, getCurrentChainId } from '../../selectors';
import fetchWithCache from '../../helpers/utils/fetch-with-cache';
const fetchWithTimeout = getFetchWithTimeout(30000);
// Actions // Actions
const BASIC_GAS_ESTIMATE_LOADING_FINISHED = const BASIC_GAS_ESTIMATE_LOADING_FINISHED =
'metamask/gas/BASIC_GAS_ESTIMATE_LOADING_FINISHED'; 'metamask/gas/BASIC_GAS_ESTIMATE_LOADING_FINISHED';
const BASIC_GAS_ESTIMATE_LOADING_STARTED = const BASIC_GAS_ESTIMATE_LOADING_STARTED =
'metamask/gas/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 RESET_CUSTOM_DATA = 'metamask/gas/RESET_CUSTOM_DATA';
const SET_BASIC_GAS_ESTIMATE_DATA = 'metamask/gas/SET_BASIC_GAS_ESTIMATE_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_LIMIT = 'metamask/gas/SET_CUSTOM_GAS_LIMIT';
const SET_CUSTOM_GAS_PRICE = 'metamask/gas/SET_CUSTOM_GAS_PRICE'; const SET_CUSTOM_GAS_PRICE = 'metamask/gas/SET_CUSTOM_GAS_PRICE';
const SET_BASIC_PRICE_ESTIMATES_LAST_RETRIEVED =
'metamask/gas/SET_BASIC_PRICE_ESTIMATES_LAST_RETRIEVED';
const initState = { const initState = {
customData: { customData: {
@ -34,7 +29,6 @@ const initState = {
fast: null, fast: null,
}, },
basicEstimateIsLoading: true, basicEstimateIsLoading: true,
basicPriceEstimatesLastRetrieved: 0,
}; };
// Reducer // Reducer
@ -71,18 +65,11 @@ export default function reducer(state = initState, action) {
limit: action.value, limit: action.value,
}, },
}; };
case SET_BASIC_PRICE_ESTIMATES_LAST_RETRIEVED:
return {
...state,
basicPriceEstimatesLastRetrieved: action.value,
};
case RESET_CUSTOM_DATA: case RESET_CUSTOM_DATA:
return { return {
...state, ...state,
customData: cloneDeep(initState.customData), customData: cloneDeep(initState.customData),
}; };
case RESET_CUSTOM_GAS_STATE:
return cloneDeep(initState);
default: default:
return state; return state;
} }
@ -103,40 +90,27 @@ export function basicGasEstimatesLoadingFinished() {
async function basicGasPriceQuery() { async function basicGasPriceQuery() {
const url = `https://api.metaswap.codefi.network/gasPrices`; const url = `https://api.metaswap.codefi.network/gasPrices`;
return await fetchWithTimeout(url, { return await fetchWithCache(
headers: {}, url,
referrer: 'https://api.metaswap.codefi.network/gasPrices', {
referrer: url,
referrerPolicy: 'no-referrer-when-downgrade', referrerPolicy: 'no-referrer-when-downgrade',
body: null,
method: 'GET', method: 'GET',
mode: 'cors', mode: 'cors',
}); },
{ cacheRefreshTime: 75000 },
);
} }
export function fetchBasicGasEstimates() { export function fetchBasicGasEstimates() {
return async (dispatch, getState) => { return async (dispatch, getState) => {
const isMainnet = getIsMainnet(getState()); const isMainnet = getIsMainnet(getState());
const { basicPriceEstimatesLastRetrieved } = getState().gas;
const timeLastRetrieved =
basicPriceEstimatesLastRetrieved ||
(await getStorageItem('BASIC_PRICE_ESTIMATES_LAST_RETRIEVED')) ||
0;
dispatch(basicGasEstimatesLoadingStarted()); dispatch(basicGasEstimatesLoadingStarted());
let basicEstimates; let basicEstimates;
if (isMainnet || process.env.IN_TEST) { if (isMainnet || process.env.IN_TEST) {
if (Date.now() - timeLastRetrieved > 75000) { basicEstimates = await fetchExternalBasicGasEstimates();
basicEstimates = await fetchExternalBasicGasEstimates(dispatch);
} else {
const cachedBasicEstimates = await getStorageItem(
'BASIC_PRICE_ESTIMATES',
);
basicEstimates =
cachedBasicEstimates ||
(await fetchExternalBasicGasEstimates(dispatch));
}
} else { } else {
basicEstimates = await fetchEthGasPriceEstimates(getState()); basicEstimates = await fetchEthGasPriceEstimates(getState());
} }
@ -148,10 +122,12 @@ export function fetchBasicGasEstimates() {
}; };
} }
async function fetchExternalBasicGasEstimates(dispatch) { async function fetchExternalBasicGasEstimates() {
const response = await basicGasPriceQuery(); const {
SafeGasPrice,
const { SafeGasPrice, ProposeGasPrice, FastGasPrice } = await response.json(); ProposeGasPrice,
FastGasPrice,
} = await basicGasPriceQuery();
const [safeLow, average, fast] = [ const [safeLow, average, fast] = [
SafeGasPrice, SafeGasPrice,
@ -165,12 +141,6 @@ async function fetchExternalBasicGasEstimates(dispatch) {
fast, 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; return basicEstimates;
} }
@ -209,7 +179,7 @@ async function fetchEthGasPriceEstimates(state) {
export function setCustomGasPriceForRetry(newPrice) { export function setCustomGasPriceForRetry(newPrice) {
return async (dispatch) => { return async (dispatch) => {
if (newPrice === '0x0') { if (newPrice === '0x0') {
const { fast } = await getStorageItem('BASIC_PRICE_ESTIMATES'); const { fast } = await fetchExternalBasicGasEstimates();
dispatch(setCustomGasPrice(decGWEIToHexWEI(fast))); dispatch(setCustomGasPrice(decGWEIToHexWEI(fast)));
} else { } else {
dispatch(setCustomGasPrice(newPrice)); dispatch(setCustomGasPrice(newPrice));
@ -238,17 +208,6 @@ export function setCustomGasLimit(newLimit) {
}; };
} }
export function setBasicPriceEstimatesLastRetrieved(retrievalTime) {
return {
type: SET_BASIC_PRICE_ESTIMATES_LAST_RETRIEVED,
value: retrievalTime,
};
}
export function resetCustomGasState() {
return { type: RESET_CUSTOM_GAS_STATE };
}
export function resetCustomData() { export function resetCustomData() {
return { type: RESET_CUSTOM_DATA }; return { type: RESET_CUSTOM_DATA };
} }