Skip to content

Commit 64eb2fa

Browse files
authored
fix: prevent multiple account creations in the same flow. (#31543)
<!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> ## **Description** This pr prevents the creation of multiple accounts when clicking the create button multiple times prior to the modal closing. <!-- Write a short description of the changes included in this pull request, also include relevant motivation and context. Have in mind the following questions: 1. What is the reason for the change? 2. What is the improvement/solution? --> [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/31543?quickstart=1) ## **Related issues** Fixes: https://consensyssoftware.atlassian.net/browse/MMMULTISRP-134?atlOrigin=eyJpIjoiOTAzNjNjNmJlNTc2NDllNjliZTE5MDYwN2M5NmUxYTYiLCJwIjoiaiJ9 ## **Manual testing steps** 1. Go to account menu 2. Click on solana and go to the modal 3. Click on the modal multiple times and see that only one account has been created after the modal closes. ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> ## **Pre-merge author checklist** - [x] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [x] I've completed the PR template to the best of my ability - [x] I’ve included tests if applicable - [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [x] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] 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.
1 parent e96f851 commit 64eb2fa

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

ui/components/multichain/create-snap-account/create-snap-account.test.tsx

+13
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,17 @@ describe('CreateSnapAccount', () => {
144144
});
145145
});
146146
});
147+
148+
it('only calls createAccount once', async () => {
149+
const { getByTestId } = render();
150+
151+
const createButton = getByTestId('submit-add-account-with-name');
152+
fireEvent.click(createButton);
153+
fireEvent.click(createButton);
154+
fireEvent.click(createButton);
155+
156+
await waitFor(() => {
157+
expect(mockCreateAccount).toHaveBeenCalledTimes(1);
158+
});
159+
});
147160
});

ui/components/multichain/create-snap-account/create-snap-account.tsx

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useCallback } from 'react';
1+
import React, { useCallback, useRef } from 'react';
22
import { CaipChainId } from '@metamask/utils';
33
import { KeyringTypes } from '@metamask/keyring-controller';
44
import { getNextAvailableAccountName } from '../../../store/actions';
@@ -43,10 +43,16 @@ export const CreateSnapAccount = ({
4343
chainId,
4444
}: CreateSnapAccountProps) => {
4545
const snapClient = useMultichainWalletSnapClient(clientType);
46+
const isCreatingAccount = useRef(false);
4647

4748
const onCreateAccount = useCallback(
4849
async (_accountNameSuggestion?: string) => {
50+
if (isCreatingAccount.current) {
51+
return;
52+
}
53+
4954
try {
55+
isCreatingAccount.current = true;
5056
await snapClient.createAccount({
5157
scope: chainId,
5258
///: BEGIN:ONLY_INCLUDE_IF(multi-srp)
@@ -57,6 +63,8 @@ export const CreateSnapAccount = ({
5763
onActionComplete(true);
5864
} catch (error) {
5965
onActionComplete(false);
66+
} finally {
67+
isCreatingAccount.current = false;
6068
}
6169
},
6270
[snapClient, chainId, selectedKeyringId, onActionComplete],

0 commit comments

Comments
 (0)