From 3ab5419dec8cd9d973147b30870d256b53a443ac Mon Sep 17 00:00:00 2001 From: Dan J Miller Date: Fri, 9 Jul 2021 15:39:43 -0230 Subject: [PATCH] =?UTF-8?q?Ensure=20simple=20send=20gas=20estimation=20hap?= =?UTF-8?q?pens=20after=20the=20recipient=20is=20iden=E2=80=A6=20(#11485)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Ensure simple send gas estimation happens after the recipient is identified * Update ui/ducks/send/send.test.js Co-authored-by: Alex Donesky * Improve test name * Lint fix Co-authored-by: Alex Donesky --- ui/ducks/send/send.js | 10 +++--- ui/ducks/send/send.test.js | 68 ++++++++++++++++++++++++++++++-------- 2 files changed, 59 insertions(+), 19 deletions(-) diff --git a/ui/ducks/send/send.js b/ui/ducks/send/send.js index 122ca2c94..718d78d93 100644 --- a/ui/ducks/send/send.js +++ b/ui/ducks/send/send.js @@ -407,7 +407,8 @@ export const initializeSendState = createAsyncThunk( : GAS_LIMITS.SIMPLE; if ( basicEstimateStatus === BASIC_ESTIMATE_STATES.READY && - stage !== SEND_STAGES.EDIT + stage !== SEND_STAGES.EDIT && + recipient.address ) { // Run our estimateGasLimit logic to get a more accurate estimation of // required gas. If this value isn't nullish, set it as the new gasLimit @@ -1201,12 +1202,9 @@ export function useMyAccountsForRecipientSearch() { * @returns {void} */ export function updateRecipient({ address, nickname }) { - return async (dispatch, getState) => { + return async (dispatch) => { await dispatch(actions.updateRecipient({ address, nickname })); - const state = getState(); - if (state.send.asset.type === ASSET_TYPES.TOKEN) { - await dispatch(computeEstimatedGasLimit()); - } + await dispatch(computeEstimatedGasLimit()); }; } diff --git a/ui/ducks/send/send.test.js b/ui/ducks/send/send.test.js index dd182882e..c9f9fe32d 100644 --- a/ui/ducks/send/send.test.js +++ b/ui/ducks/send/send.test.js @@ -1347,12 +1347,36 @@ describe('Send Slice', () => { nickname: '', }; - it('should create an action to update recipient', async () => { + it('should create actions to update recipient and recalculate gas limit if the asset type is not set', async () => { + global.eth = { + getCode: sinon.stub(), + }; + const updateRecipientState = { + metamask: { + provider: { + chainId: '0x1', + }, + }, send: { + account: { + balance: '', + }, asset: { type: '', }, + gas: { + gasPrice: '', + }, + recipient: { + address: '', + }, + amount: { + value: '', + }, + draftTransaction: { + userInputHexData: '', + }, }, }; @@ -1362,18 +1386,20 @@ describe('Send Slice', () => { const actionResult = store.getActions(); - const expectedActionResult = [ - { - type: 'send/updateRecipient', - payload: recipient, - }, - ]; - - expect(actionResult).toHaveLength(1); - expect(actionResult).toStrictEqual(expectedActionResult); + expect(actionResult).toHaveLength(4); + expect(actionResult[0].type).toStrictEqual('send/updateRecipient'); + expect(actionResult[1].type).toStrictEqual( + 'send/computeEstimatedGasLimit/pending', + ); + expect(actionResult[2].type).toStrictEqual( + 'metamask/gas/SET_CUSTOM_GAS_LIMIT', + ); + expect(actionResult[3].type).toStrictEqual( + 'send/computeEstimatedGasLimit/fulfilled', + ); }); - it('should create actions to update recipient and recalculate gas limit if the asset is a token', async () => { + it('should create actions to reset recipient input and ens, calculate gas and then validate input', async () => { const tokenState = { metamask: { blockGasLimit: '', @@ -1442,6 +1468,13 @@ describe('Send Slice', () => { address: 'Address', nickname: 'NickName', }, + gas: { + gasPrice: '0x1', + }, + amount: { + value: '0x1', + }, + draftTransaction: {}, }, }; @@ -1450,14 +1483,23 @@ describe('Send Slice', () => { await store.dispatch(resetRecipientInput()); const actionResult = store.getActions(); - expect(actionResult).toHaveLength(4); + expect(actionResult).toHaveLength(7); expect(actionResult[0].type).toStrictEqual( 'send/updateRecipientUserInput', ); expect(actionResult[0].payload).toStrictEqual(''); expect(actionResult[1].type).toStrictEqual('send/updateRecipient'); - expect(actionResult[2].type).toStrictEqual('ENS/resetEnsResolution'); + expect(actionResult[2].type).toStrictEqual( + 'send/computeEstimatedGasLimit/pending', + ); expect(actionResult[3].type).toStrictEqual( + 'metamask/gas/SET_CUSTOM_GAS_LIMIT', + ); + expect(actionResult[4].type).toStrictEqual( + 'send/computeEstimatedGasLimit/fulfilled', + ); + expect(actionResult[5].type).toStrictEqual('ENS/resetEnsResolution'); + expect(actionResult[6].type).toStrictEqual( 'send/validateRecipientUserInput', ); });