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:
parent
751c119d10
commit
3ddebf818b
@ -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({
|
||||||
|
asset: {
|
||||||
type,
|
type,
|
||||||
details: null,
|
details: null,
|
||||||
balance: account.balance,
|
balance: account.balance,
|
||||||
error: null,
|
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());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -458,18 +458,22 @@ describe('Send Slice', () => {
|
|||||||
const action = {
|
const action = {
|
||||||
type: 'send/updateAsset',
|
type: 'send/updateAsset',
|
||||||
payload: {
|
payload: {
|
||||||
|
asset: {
|
||||||
type: 'new type',
|
type: 'new type',
|
||||||
balance: 'new balance',
|
balance: 'new balance',
|
||||||
},
|
},
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const result = sendReducer(updateAssetState, action);
|
const result = sendReducer(updateAssetState, action);
|
||||||
|
|
||||||
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,8 +490,10 @@ describe('Send Slice', () => {
|
|||||||
const action = {
|
const action = {
|
||||||
type: 'send/updateAsset',
|
type: 'send/updateAsset',
|
||||||
payload: {
|
payload: {
|
||||||
|
asset: {
|
||||||
type: 'New Type',
|
type: 'New Type',
|
||||||
},
|
},
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const result = sendReducer(recipientErrorState, action);
|
const result = sendReducer(recipientErrorState, action);
|
||||||
@ -504,6 +510,7 @@ describe('Send Slice', () => {
|
|||||||
const action = {
|
const action = {
|
||||||
type: 'send/updateAsset',
|
type: 'send/updateAsset',
|
||||||
payload: {
|
payload: {
|
||||||
|
asset: {
|
||||||
type: ASSET_TYPES.TOKEN,
|
type: ASSET_TYPES.TOKEN,
|
||||||
details: {
|
details: {
|
||||||
address: '0xTokenAddress',
|
address: '0xTokenAddress',
|
||||||
@ -511,6 +518,7 @@ describe('Send Slice', () => {
|
|||||||
symbol: 'TKN',
|
symbol: 'TKN',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const result = sendReducer(
|
const result = sendReducer(
|
||||||
@ -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({
|
||||||
|
asset: {
|
||||||
type: ASSET_TYPES.NATIVE,
|
type: ASSET_TYPES.NATIVE,
|
||||||
balance: '0x0',
|
balance: '0x0',
|
||||||
error: null,
|
error: null,
|
||||||
details: null,
|
details: null,
|
||||||
|
},
|
||||||
|
initialAssetSet: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(actionResult[2].type).toStrictEqual(
|
expect(actionResult[2].type).toStrictEqual(
|
||||||
@ -1616,6 +1629,7 @@ 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({
|
||||||
|
asset: {
|
||||||
type: ASSET_TYPES.TOKEN,
|
type: ASSET_TYPES.TOKEN,
|
||||||
details: {
|
details: {
|
||||||
address: 'tokenAddress',
|
address: 'tokenAddress',
|
||||||
@ -1626,6 +1640,8 @@ describe('Send Slice', () => {
|
|||||||
},
|
},
|
||||||
balance: '0x0',
|
balance: '0x0',
|
||||||
error: null,
|
error: null,
|
||||||
|
},
|
||||||
|
initialAssetSet: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(actionResult[4].type).toStrictEqual(
|
expect(actionResult[4].type).toStrictEqual(
|
||||||
@ -2550,6 +2566,7 @@ describe('Send Slice', () => {
|
|||||||
expect(actionResult[5]).toStrictEqual({
|
expect(actionResult[5]).toStrictEqual({
|
||||||
type: 'send/updateAsset',
|
type: 'send/updateAsset',
|
||||||
payload: {
|
payload: {
|
||||||
|
asset: {
|
||||||
balance: '0x1',
|
balance: '0x1',
|
||||||
details: {
|
details: {
|
||||||
address: '0xCollectibleAddress',
|
address: '0xCollectibleAddress',
|
||||||
@ -2560,6 +2577,8 @@ describe('Send Slice', () => {
|
|||||||
error: null,
|
error: null,
|
||||||
type: ASSET_TYPES.COLLECTIBLE,
|
type: ASSET_TYPES.COLLECTIBLE,
|
||||||
},
|
},
|
||||||
|
initialAssetSet: true,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
expect(actionResult[6].type).toStrictEqual(
|
expect(actionResult[6].type).toStrictEqual(
|
||||||
'send/initializeSendState/pending',
|
'send/initializeSendState/pending',
|
||||||
@ -2736,6 +2755,7 @@ describe('Send Slice', () => {
|
|||||||
expect(actionResult[5]).toStrictEqual({
|
expect(actionResult[5]).toStrictEqual({
|
||||||
type: 'send/updateAsset',
|
type: 'send/updateAsset',
|
||||||
payload: {
|
payload: {
|
||||||
|
asset: {
|
||||||
balance: '0x0',
|
balance: '0x0',
|
||||||
type: ASSET_TYPES.TOKEN,
|
type: ASSET_TYPES.TOKEN,
|
||||||
error: null,
|
error: null,
|
||||||
@ -2747,6 +2767,8 @@ describe('Send Slice', () => {
|
|||||||
standard: 'ERC20',
|
standard: 'ERC20',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
initialAssetSet: true,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
expect(actionResult[6].type).toStrictEqual(
|
expect(actionResult[6].type).toStrictEqual(
|
||||||
'send/initializeSendState/pending',
|
'send/initializeSendState/pending',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user