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

Preserve send amount when editing an ERC20 transaction (#15275)

* preserve amount when editing an ERC20 transaction

* fix tests
This commit is contained in:
Alex Donesky 2022-07-18 12:20:02 -05:00 committed by GitHub
parent 751c119d10
commit 3ddebf818b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 80 additions and 63 deletions

View File

@ -924,31 +924,23 @@ const slice = createSlice({
* *
* @param {SendStateDraft} state - A writable draft of the send state to be * @param {SendStateDraft} state - A writable draft of the send state to be
* updated. * updated.
* @param {UpdateAssetPayload} action - The asest to set in the * @param {UpdateAssetPayload} action - The asset to set in the
* draftTransaction. * draftTransaction.
* @returns {void} * @returns {void}
*/ */
updateAsset: (state, action) => { updateAsset: (state, action) => {
const { asset, initialAssetSet } = action.payload;
const draftTransaction = const draftTransaction =
state.draftTransactions[state.currentTransactionUUID]; state.draftTransactions[state.currentTransactionUUID];
// If an asset update occurs that changes the type from 'NATIVE' to draftTransaction.asset.type = asset.type;
// 'NATIVE' then this is likely the initial asset set of an edit draftTransaction.asset.balance = asset.balance;
// transaction. We don't need to set the amount to zero in this case. draftTransaction.asset.error = asset.error;
// The only times where an update would occur of this nature that we
// would want to set the amount to zero is on a network or account change
// but that update is handled elsewhere.
const skipAmountUpdate =
action.payload.type === ASSET_TYPES.NATIVE &&
draftTransaction.asset.type === ASSET_TYPES.NATIVE;
draftTransaction.asset.type = action.payload.type;
draftTransaction.asset.balance = action.payload.balance;
draftTransaction.asset.error = action.payload.error;
if ( if (
draftTransaction.asset.type === ASSET_TYPES.TOKEN || draftTransaction.asset.type === ASSET_TYPES.TOKEN ||
draftTransaction.asset.type === ASSET_TYPES.COLLECTIBLE draftTransaction.asset.type === ASSET_TYPES.COLLECTIBLE
) { ) {
draftTransaction.asset.details = action.payload.details; draftTransaction.asset.details = asset.details;
} else { } else {
// clear the details object when sending native currency // clear the details object when sending native currency
draftTransaction.asset.details = null; draftTransaction.asset.details = null;
@ -962,7 +954,7 @@ const slice = createSlice({
// to zero. This will revalidate the send amount field. // to zero. This will revalidate the send amount field.
if (state.amountMode === AMOUNT_MODES.MAX) { if (state.amountMode === AMOUNT_MODES.MAX) {
slice.caseReducers.updateAmountToMax(state); slice.caseReducers.updateAmountToMax(state);
} else if (skipAmountUpdate === false) { } else if (initialAssetSet === false) {
slice.caseReducers.updateSendAmount(state, { payload: '0x0' }); slice.caseReducers.updateSendAmount(state, { payload: '0x0' });
} }
// validate send state // validate send state
@ -1702,7 +1694,7 @@ export function editExistingTransaction(assetType, transactionId) {
await dispatch( await dispatch(
updateSendAsset( updateSendAsset(
{ type: ASSET_TYPES.NATIVE }, { type: ASSET_TYPES.NATIVE },
{ skipComputeEstimatedGasLimit: true }, { initialAssetSet: true },
), ),
); );
} else { } else {
@ -1759,7 +1751,7 @@ export function editExistingTransaction(assetType, transactionId) {
: {}), : {}),
}, },
}, },
{ skipComputeEstimatedGasLimit: true }, { initialAssetSet: true },
), ),
); );
} }
@ -1958,7 +1950,7 @@ export function updateSendAmount(amount) {
*/ */
export function updateSendAsset( export function updateSendAsset(
{ type, details: providedDetails }, { type, details: providedDetails },
{ skipComputeEstimatedGasLimit = false } = {}, { initialAssetSet = false } = {},
) { ) {
return async (dispatch, getState) => { return async (dispatch, getState) => {
const state = getState(); const state = getState();
@ -1979,10 +1971,13 @@ export function updateSendAsset(
); );
await dispatch( await dispatch(
actions.updateAsset({ actions.updateAsset({
type, asset: {
details: null, type,
balance: account.balance, details: null,
error: null, balance: account.balance,
error: null,
},
initialAssetSet,
}), }),
); );
} else { } else {
@ -2065,9 +2060,9 @@ export function updateSendAsset(
} }
} }
await dispatch(actions.updateAsset(asset)); await dispatch(actions.updateAsset({ asset, initialAssetSet }));
} }
if (skipComputeEstimatedGasLimit === false) { if (initialAssetSet === false) {
await dispatch(computeEstimatedGasLimit()); await dispatch(computeEstimatedGasLimit());
} }
}; };

View File

@ -458,8 +458,10 @@ describe('Send Slice', () => {
const action = { const action = {
type: 'send/updateAsset', type: 'send/updateAsset',
payload: { payload: {
type: 'new type', asset: {
balance: 'new balance', type: 'new type',
balance: 'new balance',
},
}, },
}; };
@ -467,9 +469,11 @@ describe('Send Slice', () => {
const draftTransaction = getTestUUIDTx(result); const draftTransaction = getTestUUIDTx(result);
expect(draftTransaction.asset.type).toStrictEqual(action.payload.type); expect(draftTransaction.asset.type).toStrictEqual(
action.payload.asset.type,
);
expect(draftTransaction.asset.balance).toStrictEqual( expect(draftTransaction.asset.balance).toStrictEqual(
action.payload.balance, action.payload.asset.balance,
); );
}); });
@ -486,7 +490,9 @@ describe('Send Slice', () => {
const action = { const action = {
type: 'send/updateAsset', type: 'send/updateAsset',
payload: { payload: {
type: 'New Type', asset: {
type: 'New Type',
},
}, },
}; };
@ -504,11 +510,13 @@ describe('Send Slice', () => {
const action = { const action = {
type: 'send/updateAsset', type: 'send/updateAsset',
payload: { payload: {
type: ASSET_TYPES.TOKEN, asset: {
details: { type: ASSET_TYPES.TOKEN,
address: '0xTokenAddress', details: {
decimals: 0, address: '0xTokenAddress',
symbol: 'TKN', decimals: 0,
symbol: 'TKN',
},
}, },
}, },
}; };
@ -520,9 +528,11 @@ describe('Send Slice', () => {
const draftTransaction = getTestUUIDTx(result); const draftTransaction = getTestUUIDTx(result);
expect(draftTransaction.asset.type).toStrictEqual(action.payload.type); expect(draftTransaction.asset.type).toStrictEqual(
action.payload.asset.type,
);
expect(draftTransaction.asset.details).toStrictEqual( expect(draftTransaction.asset.details).toStrictEqual(
action.payload.details, action.payload.asset.details,
); );
}); });
}); });
@ -1563,10 +1573,13 @@ describe('Send Slice', () => {
}); });
expect(actionResult[1].type).toStrictEqual('send/updateAsset'); expect(actionResult[1].type).toStrictEqual('send/updateAsset');
expect(actionResult[1].payload).toStrictEqual({ expect(actionResult[1].payload).toStrictEqual({
type: ASSET_TYPES.NATIVE, asset: {
balance: '0x0', type: ASSET_TYPES.NATIVE,
error: null, balance: '0x0',
details: null, error: null,
details: null,
},
initialAssetSet: false,
}); });
expect(actionResult[2].type).toStrictEqual( expect(actionResult[2].type).toStrictEqual(
@ -1616,16 +1629,19 @@ describe('Send Slice', () => {
payload: `sendFlow - user set asset to ERC20 token with symbol TokenSymbol and address tokenAddress`, payload: `sendFlow - user set asset to ERC20 token with symbol TokenSymbol and address tokenAddress`,
}); });
expect(actionResult[3].payload).toStrictEqual({ expect(actionResult[3].payload).toStrictEqual({
type: ASSET_TYPES.TOKEN, asset: {
details: { type: ASSET_TYPES.TOKEN,
address: 'tokenAddress', details: {
symbol: 'TokenSymbol', address: 'tokenAddress',
decimals: 18, symbol: 'TokenSymbol',
standard: 'ERC20', decimals: 18,
standard: 'ERC20',
balance: '0x0',
},
balance: '0x0', balance: '0x0',
error: null,
}, },
balance: '0x0', initialAssetSet: false,
error: null,
}); });
expect(actionResult[4].type).toStrictEqual( expect(actionResult[4].type).toStrictEqual(
@ -2550,15 +2566,18 @@ describe('Send Slice', () => {
expect(actionResult[5]).toStrictEqual({ expect(actionResult[5]).toStrictEqual({
type: 'send/updateAsset', type: 'send/updateAsset',
payload: { payload: {
balance: '0x1', asset: {
details: {
address: '0xCollectibleAddress',
balance: '0x1', balance: '0x1',
standard: TOKEN_STANDARDS.ERC721, details: {
tokenId: '15000', address: '0xCollectibleAddress',
balance: '0x1',
standard: TOKEN_STANDARDS.ERC721,
tokenId: '15000',
},
error: null,
type: ASSET_TYPES.COLLECTIBLE,
}, },
error: null, initialAssetSet: true,
type: ASSET_TYPES.COLLECTIBLE,
}, },
}); });
expect(actionResult[6].type).toStrictEqual( expect(actionResult[6].type).toStrictEqual(
@ -2736,16 +2755,19 @@ describe('Send Slice', () => {
expect(actionResult[5]).toStrictEqual({ expect(actionResult[5]).toStrictEqual({
type: 'send/updateAsset', type: 'send/updateAsset',
payload: { payload: {
balance: '0x0', asset: {
type: ASSET_TYPES.TOKEN,
error: null,
details: {
balance: '0x0', balance: '0x0',
address: '0xTokenAddress', type: ASSET_TYPES.TOKEN,
decimals: 18, error: null,
symbol: 'SYMB', details: {
standard: 'ERC20', balance: '0x0',
address: '0xTokenAddress',
decimals: 18,
symbol: 'SYMB',
standard: 'ERC20',
},
}, },
initialAssetSet: true,
}, },
}); });
expect(actionResult[6].type).toStrictEqual( expect(actionResult[6].type).toStrictEqual(