Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch eth_gasprice in send flow on non-Mainnet networks #10444

Merged
merged 9 commits into from
Feb 19, 2021
47 changes: 47 additions & 0 deletions ui/app/ducks/gas/gas-duck.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import assert from 'assert';
import nock from 'nock';
import sinon from 'sinon';
import proxyquire from 'proxyquire';
import BN from 'bn.js';

const fakeStorage = {};

Expand Down Expand Up @@ -58,6 +59,16 @@ describe('Gas Duck', function () {
basicEstimateIsLoading: true,
basicPriceEstimatesLastRetrieved: 0,
};

const providerState = {
chainId: '0x1',
nickname: '',
rpcPrefs: {},
rpcUrl: '',
ticker: 'ETH',
type: 'mainnet',
};

const BASIC_GAS_ESTIMATE_LOADING_FINISHED =
'metamask/gas/BASIC_GAS_ESTIMATE_LOADING_FINISHED';
const BASIC_GAS_ESTIMATE_LOADING_STARTED =
Expand Down Expand Up @@ -165,6 +176,7 @@ describe('Gas Duck', function () {

await fetchBasicGasEstimates()(mockDistpatch, () => ({
gas: { ...initState, basicPriceAEstimatesLastRetrieved: 1000000 },
metamask: { provider: { ...providerState } },
}));
assert.deepStrictEqual(mockDistpatch.getCall(0).args, [
{ type: BASIC_GAS_ESTIMATE_LOADING_STARTED },
Expand Down Expand Up @@ -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 () {
const mockDistpatch = sinon.spy();

Expand All @@ -209,6 +254,7 @@ describe('Gas Duck', function () {

await fetchBasicGasEstimates()(mockDistpatch, () => ({
gas: { ...initState },
metamask: { provider: { ...providerState } },
}));
assert.deepStrictEqual(mockDistpatch.getCall(0).args, [
{ type: BASIC_GAS_ESTIMATE_LOADING_STARTED },
Expand Down Expand Up @@ -244,6 +290,7 @@ describe('Gas Duck', function () {

await fetchBasicGasEstimates()(mockDistpatch, () => ({
gas: { ...initState },
metamask: { provider: { ...providerState } },
}));
assert.deepStrictEqual(mockDistpatch.getCall(0).args, [
{ type: BASIC_GAS_ESTIMATE_LOADING_STARTED },
Expand Down
62 changes: 53 additions & 9 deletions ui/app/ducks/gas/gas.duck.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { cloneDeep } from 'lodash';
import BigNumber from 'bignumber.js';
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 { getIsMainnet, getCurrentChainId } from '../../selectors';

const fetchWithTimeout = getFetchWithTimeout(30000);

Expand Down Expand Up @@ -111,7 +115,9 @@ async function basicGasPriceQuery() {

export function fetchBasicGasEstimates() {
return async (dispatch, getState) => {
const isMainnet = getIsMainnet(getState());
const { basicPriceEstimatesLastRetrieved } = getState().gas;

const timeLastRetrieved =
basicPriceEstimatesLastRetrieved ||
(await getStorageItem('BASIC_PRICE_ESTIMATES_LAST_RETRIEVED')) ||
Expand All @@ -120,15 +126,19 @@ export function fetchBasicGasEstimates() {
dispatch(basicGasEstimatesLoadingStarted());

let basicEstimates;
if (Date.now() - timeLastRetrieved > 75000) {
basicEstimates = await fetchExternalBasicGasEstimates(dispatch);
if (isMainnet || process.env.IN_TEST) {
if (Date.now() - timeLastRetrieved > 75000) {
basicEstimates = await fetchExternalBasicGasEstimates(dispatch);
} else {
const cachedBasicEstimates = await getStorageItem(
'BASIC_PRICE_ESTIMATES',
);
basicEstimates =
cachedBasicEstimates ||
(await fetchExternalBasicGasEstimates(dispatch));
}
} else {
const cachedBasicEstimates = await getStorageItem(
'BASIC_PRICE_ESTIMATES',
);
basicEstimates =
cachedBasicEstimates ||
(await fetchExternalBasicGasEstimates(dispatch));
basicEstimates = await fetchEthGasPriceEstimates(getState());
}

dispatch(setBasicGasEstimateData(basicEstimates));
Expand Down Expand Up @@ -161,6 +171,40 @@ async function fetchExternalBasicGasEstimates(dispatch) {
setStorageItem('BASIC_PRICE_ESTIMATES_LAST_RETRIEVED', timeRetrieved),
]);
dispatch(setBasicPriceEstimatesLastRetrieved(timeRetrieved));
return basicEstimates;
}

async function fetchEthGasPriceEstimates(state) {
const chainId = getCurrentChainId(state);
let timeLastRetrieved, cachedBasicEstimates;
await Promise.all([
getStorageItem(`${chainId}_BASIC_PRICE_ESTIMATES_LAST_RETRIEVED`),
getStorageItem(`${chainId}_BASIC_PRICE_ESTIMATES`),
]).then((values) => {
timeLastRetrieved = values[0] ? values[0] : 0;
cachedBasicEstimates = values[1];
});
NiranjanaBinoy marked this conversation as resolved.
Show resolved Hide resolved
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([
Gudahtt marked this conversation as resolved.
Show resolved Hide resolved
setStorageItem(`${chainId}_BASIC_PRICE_ESTIMATES`, basicEstimates),
setStorageItem(
`${chainId}_BASIC_PRICE_ESTIMATES_LAST_RETRIEVED`,
timeRetrieved,
),
]);

return basicEstimates;
}
Expand Down
1 change: 1 addition & 0 deletions ui/app/pages/send/send.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export default class SendTransactionScreen extends Component {
address,
});
updateToNicknameIfNecessary(to, toNickname, addressBook);
this.props.fetchBasicGasEstimates();
updateGas = true;
}
}
Expand Down