mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Use fromAccount instead of selectedAcccount for account updates on edit (#15449)
This commit is contained in:
parent
a4f0944517
commit
71dd39724b
@ -1454,28 +1454,30 @@ const slice = createSlice({
|
|||||||
extraReducers: (builder) => {
|
extraReducers: (builder) => {
|
||||||
builder
|
builder
|
||||||
.addCase(ACCOUNT_CHANGED, (state, action) => {
|
.addCase(ACCOUNT_CHANGED, (state, action) => {
|
||||||
// If we are on the edit flow then we need to watch for changes to the
|
// This event occurs when the user's account details update due to
|
||||||
// current account.address in state and keep balance updated
|
// background state changes. If the account that is being updated is
|
||||||
// appropriately
|
// the current from account on the edit flow we need to update
|
||||||
if (
|
// the balance for the account and revalidate the send state.
|
||||||
state.stage === SEND_STAGES.EDIT &&
|
if (state.stage === SEND_STAGES.EDIT && action.payload.account) {
|
||||||
action.payload.account.address === state.selectedAccount.address
|
|
||||||
) {
|
|
||||||
// This event occurs when the user's account details update due to
|
|
||||||
// background state changes. If the account that is being updated is
|
|
||||||
// the current from account on the edit flow we need to update
|
|
||||||
// the balance for the account and revalidate the send state.
|
|
||||||
state.selectedAccount.balance = action.payload.account.balance;
|
|
||||||
// We need to update the asset balance if the asset is the native
|
|
||||||
// network asset. Once we update the balance we recompute error state.
|
|
||||||
const draftTransaction =
|
const draftTransaction =
|
||||||
state.draftTransactions[state.currentTransactionUUID];
|
state.draftTransactions[state.currentTransactionUUID];
|
||||||
if (draftTransaction?.asset.type === ASSET_TYPES.NATIVE) {
|
if (
|
||||||
draftTransaction.asset.balance = action.payload.account.balance;
|
draftTransaction &&
|
||||||
|
draftTransaction.fromAccount &&
|
||||||
|
draftTransaction.fromAccount.address ===
|
||||||
|
action.payload.account.address
|
||||||
|
) {
|
||||||
|
draftTransaction.fromAccount.balance =
|
||||||
|
action.payload.account.balance;
|
||||||
|
// We need to update the asset balance if the asset is the native
|
||||||
|
// network asset. Once we update the balance we recompute error state.
|
||||||
|
if (draftTransaction.asset.type === ASSET_TYPES.NATIVE) {
|
||||||
|
draftTransaction.asset.balance = action.payload.account.balance;
|
||||||
|
}
|
||||||
|
slice.caseReducers.validateAmountField(state);
|
||||||
|
slice.caseReducers.validateGasField(state);
|
||||||
|
slice.caseReducers.validateSendState(state);
|
||||||
}
|
}
|
||||||
slice.caseReducers.validateAmountField(state);
|
|
||||||
slice.caseReducers.validateGasField(state);
|
|
||||||
slice.caseReducers.validateSendState(state);
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.addCase(ADDRESS_BOOK_UPDATED, (state, action) => {
|
.addCase(ADDRESS_BOOK_UPDATED, (state, action) => {
|
||||||
|
@ -1154,9 +1154,14 @@ describe('Send Slice', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('Account Changed', () => {
|
describe('Account Changed', () => {
|
||||||
it('should', () => {
|
it('should correctly update the fromAccount in an edit', () => {
|
||||||
const accountsChangedState = {
|
const accountsChangedState = {
|
||||||
...INITIAL_SEND_STATE_FOR_EXISTING_DRAFT,
|
...getInitialSendStateWithExistingTxState({
|
||||||
|
fromAccount: {
|
||||||
|
address: '0xAddress',
|
||||||
|
balance: '0x0',
|
||||||
|
},
|
||||||
|
}),
|
||||||
stage: SEND_STAGES.EDIT,
|
stage: SEND_STAGES.EDIT,
|
||||||
selectedAccount: {
|
selectedAccount: {
|
||||||
address: '0xAddress',
|
address: '0xAddress',
|
||||||
@ -1176,11 +1181,42 @@ describe('Send Slice', () => {
|
|||||||
|
|
||||||
const result = sendReducer(accountsChangedState, action);
|
const result = sendReducer(accountsChangedState, action);
|
||||||
|
|
||||||
expect(result.selectedAccount.balance).toStrictEqual(
|
const draft = getTestUUIDTx(result);
|
||||||
|
|
||||||
|
expect(draft.fromAccount.balance).toStrictEqual(
|
||||||
action.payload.account.balance,
|
action.payload.account.balance,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should gracefully handle missing account param in payload', () => {
|
||||||
|
const accountsChangedState = {
|
||||||
|
...getInitialSendStateWithExistingTxState({
|
||||||
|
fromAccount: {
|
||||||
|
address: '0xAddress',
|
||||||
|
balance: '0x0',
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
stage: SEND_STAGES.EDIT,
|
||||||
|
selectedAccount: {
|
||||||
|
address: '0xAddress',
|
||||||
|
balance: '0x0',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const action = {
|
||||||
|
type: 'ACCOUNT_CHANGED',
|
||||||
|
payload: {
|
||||||
|
account: undefined,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = sendReducer(accountsChangedState, action);
|
||||||
|
|
||||||
|
const draft = getTestUUIDTx(result);
|
||||||
|
|
||||||
|
expect(draft.fromAccount.balance).toStrictEqual('0x0');
|
||||||
|
});
|
||||||
|
|
||||||
it(`should not edit account balance if action payload address is not the same as state's address`, () => {
|
it(`should not edit account balance if action payload address is not the same as state's address`, () => {
|
||||||
const accountsChangedState = {
|
const accountsChangedState = {
|
||||||
...INITIAL_SEND_STATE_FOR_EXISTING_DRAFT,
|
...INITIAL_SEND_STATE_FOR_EXISTING_DRAFT,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user