From ea84b69c78da825ed8b95dfcc96bed2fd58eb540 Mon Sep 17 00:00:00 2001 From: Ariella Vu <20778143+digiwand@users.noreply.github.com> Date: Thu, 27 Jun 2024 17:58:32 +0200 Subject: [PATCH] fix: `Cannot destructure property 'balance' of 'K[Z]' as it is undefined` fromAddress error when no transaction is loaded in ConfirmTransactionBase (#25506) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In `confirm-transaction-base.container.js`, while the transaction is switching, the component may be in a state without transaction data. In this case, we can handle the empty transaction data appropriately until the expected data comes in to allow the user flow to continue without breaking. **note:** I was unable to reproduce this in `develop` after some time, but I successfully repro'd this in `Version-v12.0.0` which allowed me to verify the fix works. Possibly the underlying empty transaction data state was fixed by other code in `develop` This PR is planned to be cherry-picked for Version-v12.0.0 Fixes: https://github.com/MetaMask/metamask-extension/issues/25406 See [Issue](https://github.com/MetaMask/metamask-extension/issues/25406) for repro steps `confirm-transaction-base.container.js` ![CleanShot 2024-06-25 at 15 58 13@2x](https://github.com/MetaMask/metamask-extension/assets/20778143/4c3ccc3f-8583-4b76-b6cc-9bebc1f1045c) See error in [Issue](https://github.com/MetaMask/metamask-extension/issues/25406) screenshots No error message is shown and app works as expected - [ ] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md). - [ ] I've completed the PR template to the best of my ability - [ ] I’ve included tests if applicable - [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [ ] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. --- .../confirm-transaction-base.container.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/ui/pages/confirmations/confirm-transaction-base/confirm-transaction-base.container.js b/ui/pages/confirmations/confirm-transaction-base/confirm-transaction-base.container.js index 3734b231dad5..bbcde4171463 100644 --- a/ui/pages/confirmations/confirm-transaction-base/confirm-transaction-base.container.js +++ b/ui/pages/confirmations/confirm-transaction-base/confirm-transaction-base.container.js @@ -5,6 +5,8 @@ import { TransactionStatus, TransactionType, } from '@metamask/transaction-controller'; +import { captureException } from '@sentry/browser'; + ///: BEGIN:ONLY_INCLUDE_IF(build-mmi) import { showCustodianDeepLink } from '@metamask-institutional/extension'; import { mmiActionsFactory } from '../../../store/institutional/institution-background'; @@ -169,7 +171,16 @@ const mapStateToProps = (state, ownProps) => { const transactionData = parseStandardTokenTransactionData(data); const tokenToAddress = getTokenAddressParam(transactionData); - const { balance } = accounts[fromAddress]; + if (!accounts[fromAddress]) { + captureException( + new Error( + `ConfirmTransactionBase: Unexpected state - No account found for sender address. ` + + `chainId: ${chainId}. fromAddress?: ${Boolean(fromAddress)}`, + ), + ); + } + + const { balance } = accounts[fromAddress] || { balance: '0x0' }; const fromInternalAccount = getInternalAccountByAddress(state, fromAddress); const fromName = fromInternalAccount?.metadata.name; const keyring = findKeyringForAddress(state, fromAddress);