mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Fetch eth_gasprice in send flow on non-Mainnet networks (#10444)
This commit is contained in:
parent
5996e84596
commit
fab22ee05f
@ -2,6 +2,7 @@ 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 proxyquire from 'proxyquire';
|
||||||
|
import BN from 'bn.js';
|
||||||
|
|
||||||
const fakeStorage = {};
|
const fakeStorage = {};
|
||||||
|
|
||||||
@ -58,6 +59,16 @@ describe('Gas Duck', function () {
|
|||||||
basicEstimateIsLoading: true,
|
basicEstimateIsLoading: true,
|
||||||
basicPriceEstimatesLastRetrieved: 0,
|
basicPriceEstimatesLastRetrieved: 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const providerState = {
|
||||||
|
chainId: '0x1',
|
||||||
|
nickname: '',
|
||||||
|
rpcPrefs: {},
|
||||||
|
rpcUrl: '',
|
||||||
|
ticker: 'ETH',
|
||||||
|
type: 'mainnet',
|
||||||
|
};
|
||||||
|
|
||||||
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 =
|
||||||
@ -165,6 +176,7 @@ describe('Gas Duck', function () {
|
|||||||
|
|
||||||
await fetchBasicGasEstimates()(mockDistpatch, () => ({
|
await fetchBasicGasEstimates()(mockDistpatch, () => ({
|
||||||
gas: { ...initState, basicPriceAEstimatesLastRetrieved: 1000000 },
|
gas: { ...initState, basicPriceAEstimatesLastRetrieved: 1000000 },
|
||||||
|
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 },
|
||||||
@ -193,6 +205,39 @@ describe('Gas Duck', function () {
|
|||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should call fetch with the expected params for test network', async function () {
|
||||||
|
global.eth = { gasPrice: sinon.fake.returns(new BN(48199313, 10)) };
|
||||||
|
|
||||||
|
const mockDistpatch = sinon.spy();
|
||||||
|
const providerStateForTestNetwrok = {
|
||||||
|
chainId: '0x5',
|
||||||
|
nickname: '',
|
||||||
|
rpcPrefs: {},
|
||||||
|
rpcUrl: '',
|
||||||
|
ticker: 'ETH',
|
||||||
|
type: 'goerli',
|
||||||
|
};
|
||||||
|
|
||||||
|
await fetchBasicGasEstimates()(mockDistpatch, () => ({
|
||||||
|
gas: { ...initState, basicPriceAEstimatesLastRetrieved: 1000000 },
|
||||||
|
metamask: { provider: { ...providerStateForTestNetwrok } },
|
||||||
|
}));
|
||||||
|
assert.deepStrictEqual(mockDistpatch.getCall(0).args, [
|
||||||
|
{ type: BASIC_GAS_ESTIMATE_LOADING_STARTED },
|
||||||
|
]);
|
||||||
|
assert.deepStrictEqual(mockDistpatch.getCall(1).args, [
|
||||||
|
{
|
||||||
|
type: SET_BASIC_GAS_ESTIMATE_DATA,
|
||||||
|
value: {
|
||||||
|
average: 0.0482,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
assert.deepStrictEqual(mockDistpatch.getCall(2).args, [
|
||||||
|
{ type: BASIC_GAS_ESTIMATE_LOADING_FINISHED },
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
it('should fetch recently retrieved estimates from storage', async function () {
|
it('should fetch recently retrieved estimates from storage', async function () {
|
||||||
const mockDistpatch = sinon.spy();
|
const mockDistpatch = sinon.spy();
|
||||||
|
|
||||||
@ -209,6 +254,7 @@ describe('Gas Duck', function () {
|
|||||||
|
|
||||||
await fetchBasicGasEstimates()(mockDistpatch, () => ({
|
await fetchBasicGasEstimates()(mockDistpatch, () => ({
|
||||||
gas: { ...initState },
|
gas: { ...initState },
|
||||||
|
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 },
|
||||||
@ -244,6 +290,7 @@ describe('Gas Duck', function () {
|
|||||||
|
|
||||||
await fetchBasicGasEstimates()(mockDistpatch, () => ({
|
await fetchBasicGasEstimates()(mockDistpatch, () => ({
|
||||||
gas: { ...initState },
|
gas: { ...initState },
|
||||||
|
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 },
|
||||||
|
@ -1,8 +1,12 @@
|
|||||||
import { cloneDeep } from 'lodash';
|
import { cloneDeep } from 'lodash';
|
||||||
import BigNumber from 'bignumber.js';
|
import BigNumber from 'bignumber.js';
|
||||||
import { getStorageItem, setStorageItem } from '../../../lib/storage-helpers';
|
import { getStorageItem, setStorageItem } from '../../../lib/storage-helpers';
|
||||||
import { decGWEIToHexWEI } from '../../helpers/utils/conversions.util';
|
import {
|
||||||
|
decGWEIToHexWEI,
|
||||||
|
getValueFromWeiHex,
|
||||||
|
} from '../../helpers/utils/conversions.util';
|
||||||
import getFetchWithTimeout from '../../../../shared/modules/fetch-with-timeout';
|
import getFetchWithTimeout from '../../../../shared/modules/fetch-with-timeout';
|
||||||
|
import { getIsMainnet, getCurrentChainId } from '../../selectors';
|
||||||
|
|
||||||
const fetchWithTimeout = getFetchWithTimeout(30000);
|
const fetchWithTimeout = getFetchWithTimeout(30000);
|
||||||
|
|
||||||
@ -111,7 +115,9 @@ async function basicGasPriceQuery() {
|
|||||||
|
|
||||||
export function fetchBasicGasEstimates() {
|
export function fetchBasicGasEstimates() {
|
||||||
return async (dispatch, getState) => {
|
return async (dispatch, getState) => {
|
||||||
|
const isMainnet = getIsMainnet(getState());
|
||||||
const { basicPriceEstimatesLastRetrieved } = getState().gas;
|
const { basicPriceEstimatesLastRetrieved } = getState().gas;
|
||||||
|
|
||||||
const timeLastRetrieved =
|
const timeLastRetrieved =
|
||||||
basicPriceEstimatesLastRetrieved ||
|
basicPriceEstimatesLastRetrieved ||
|
||||||
(await getStorageItem('BASIC_PRICE_ESTIMATES_LAST_RETRIEVED')) ||
|
(await getStorageItem('BASIC_PRICE_ESTIMATES_LAST_RETRIEVED')) ||
|
||||||
@ -120,15 +126,19 @@ export function fetchBasicGasEstimates() {
|
|||||||
dispatch(basicGasEstimatesLoadingStarted());
|
dispatch(basicGasEstimatesLoadingStarted());
|
||||||
|
|
||||||
let basicEstimates;
|
let basicEstimates;
|
||||||
if (Date.now() - timeLastRetrieved > 75000) {
|
if (isMainnet || process.env.IN_TEST) {
|
||||||
basicEstimates = await fetchExternalBasicGasEstimates(dispatch);
|
if (Date.now() - timeLastRetrieved > 75000) {
|
||||||
|
basicEstimates = await fetchExternalBasicGasEstimates(dispatch);
|
||||||
|
} else {
|
||||||
|
const cachedBasicEstimates = await getStorageItem(
|
||||||
|
'BASIC_PRICE_ESTIMATES',
|
||||||
|
);
|
||||||
|
basicEstimates =
|
||||||
|
cachedBasicEstimates ||
|
||||||
|
(await fetchExternalBasicGasEstimates(dispatch));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
const cachedBasicEstimates = await getStorageItem(
|
basicEstimates = await fetchEthGasPriceEstimates(getState());
|
||||||
'BASIC_PRICE_ESTIMATES',
|
|
||||||
);
|
|
||||||
basicEstimates =
|
|
||||||
cachedBasicEstimates ||
|
|
||||||
(await fetchExternalBasicGasEstimates(dispatch));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dispatch(setBasicGasEstimateData(basicEstimates));
|
dispatch(setBasicGasEstimateData(basicEstimates));
|
||||||
@ -161,6 +171,37 @@ async function fetchExternalBasicGasEstimates(dispatch) {
|
|||||||
setStorageItem('BASIC_PRICE_ESTIMATES_LAST_RETRIEVED', timeRetrieved),
|
setStorageItem('BASIC_PRICE_ESTIMATES_LAST_RETRIEVED', timeRetrieved),
|
||||||
]);
|
]);
|
||||||
dispatch(setBasicPriceEstimatesLastRetrieved(timeRetrieved));
|
dispatch(setBasicPriceEstimatesLastRetrieved(timeRetrieved));
|
||||||
|
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,
|
||||||
|
),
|
||||||
|
]);
|
||||||
|
|
||||||
return basicEstimates;
|
return basicEstimates;
|
||||||
}
|
}
|
||||||
|
@ -147,6 +147,7 @@ export default class SendTransactionScreen extends Component {
|
|||||||
address,
|
address,
|
||||||
});
|
});
|
||||||
updateToNicknameIfNecessary(to, toNickname, addressBook);
|
updateToNicknameIfNecessary(to, toNickname, addressBook);
|
||||||
|
this.props.fetchBasicGasEstimates();
|
||||||
updateGas = true;
|
updateGas = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user