Skip to content

Commit 71dd397

Browse files
brad-deckerdanjm
authored andcommitted
Use fromAccount instead of selectedAcccount for account updates on edit (#15449)
1 parent a4f0944 commit 71dd397

File tree

2 files changed

+60
-22
lines changed

2 files changed

+60
-22
lines changed

ui/ducks/send/send.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,28 +1454,30 @@ const slice = createSlice({
14541454
extraReducers: (builder) => {
14551455
builder
14561456
.addCase(ACCOUNT_CHANGED, (state, action) => {
1457-
// If we are on the edit flow then we need to watch for changes to the
1458-
// current account.address in state and keep balance updated
1459-
// appropriately
1460-
if (
1461-
state.stage === SEND_STAGES.EDIT &&
1462-
action.payload.account.address === state.selectedAccount.address
1463-
) {
1464-
// This event occurs when the user's account details update due to
1465-
// background state changes. If the account that is being updated is
1466-
// the current from account on the edit flow we need to update
1467-
// the balance for the account and revalidate the send state.
1468-
state.selectedAccount.balance = action.payload.account.balance;
1469-
// We need to update the asset balance if the asset is the native
1470-
// network asset. Once we update the balance we recompute error state.
1457+
// This event occurs when the user's account details update due to
1458+
// background state changes. If the account that is being updated is
1459+
// the current from account on the edit flow we need to update
1460+
// the balance for the account and revalidate the send state.
1461+
if (state.stage === SEND_STAGES.EDIT && action.payload.account) {
14711462
const draftTransaction =
14721463
state.draftTransactions[state.currentTransactionUUID];
1473-
if (draftTransaction?.asset.type === ASSET_TYPES.NATIVE) {
1474-
draftTransaction.asset.balance = action.payload.account.balance;
1464+
if (
1465+
draftTransaction &&
1466+
draftTransaction.fromAccount &&
1467+
draftTransaction.fromAccount.address ===
1468+
action.payload.account.address
1469+
) {
1470+
draftTransaction.fromAccount.balance =
1471+
action.payload.account.balance;
1472+
// We need to update the asset balance if the asset is the native
1473+
// network asset. Once we update the balance we recompute error state.
1474+
if (draftTransaction.asset.type === ASSET_TYPES.NATIVE) {
1475+
draftTransaction.asset.balance = action.payload.account.balance;
1476+
}
1477+
slice.caseReducers.validateAmountField(state);
1478+
slice.caseReducers.validateGasField(state);
1479+
slice.caseReducers.validateSendState(state);
14751480
}
1476-
slice.caseReducers.validateAmountField(state);
1477-
slice.caseReducers.validateGasField(state);
1478-
slice.caseReducers.validateSendState(state);
14791481
}
14801482
})
14811483
.addCase(ADDRESS_BOOK_UPDATED, (state, action) => {

ui/ducks/send/send.test.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,9 +1154,14 @@ describe('Send Slice', () => {
11541154
});
11551155

11561156
describe('Account Changed', () => {
1157-
it('should', () => {
1157+
it('should correctly update the fromAccount in an edit', () => {
11581158
const accountsChangedState = {
1159-
...INITIAL_SEND_STATE_FOR_EXISTING_DRAFT,
1159+
...getInitialSendStateWithExistingTxState({
1160+
fromAccount: {
1161+
address: '0xAddress',
1162+
balance: '0x0',
1163+
},
1164+
}),
11601165
stage: SEND_STAGES.EDIT,
11611166
selectedAccount: {
11621167
address: '0xAddress',
@@ -1176,11 +1181,42 @@ describe('Send Slice', () => {
11761181

11771182
const result = sendReducer(accountsChangedState, action);
11781183

1179-
expect(result.selectedAccount.balance).toStrictEqual(
1184+
const draft = getTestUUIDTx(result);
1185+
1186+
expect(draft.fromAccount.balance).toStrictEqual(
11801187
action.payload.account.balance,
11811188
);
11821189
});
11831190

1191+
it('should gracefully handle missing account param in payload', () => {
1192+
const accountsChangedState = {
1193+
...getInitialSendStateWithExistingTxState({
1194+
fromAccount: {
1195+
address: '0xAddress',
1196+
balance: '0x0',
1197+
},
1198+
}),
1199+
stage: SEND_STAGES.EDIT,
1200+
selectedAccount: {
1201+
address: '0xAddress',
1202+
balance: '0x0',
1203+
},
1204+
};
1205+
1206+
const action = {
1207+
type: 'ACCOUNT_CHANGED',
1208+
payload: {
1209+
account: undefined,
1210+
},
1211+
};
1212+
1213+
const result = sendReducer(accountsChangedState, action);
1214+
1215+
const draft = getTestUUIDTx(result);
1216+
1217+
expect(draft.fromAccount.balance).toStrictEqual('0x0');
1218+
});
1219+
11841220
it(`should not edit account balance if action payload address is not the same as state's address`, () => {
11851221
const accountsChangedState = {
11861222
...INITIAL_SEND_STATE_FOR_EXISTING_DRAFT,

0 commit comments

Comments
 (0)