Skip to content

fix: prevent multiple account creations in the same flow. #31543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,17 @@ describe('CreateSnapAccount', () => {
});
});
});

it('only calls createAccount once', async () => {
const { getByTestId } = render();

const createButton = getByTestId('submit-add-account-with-name');
fireEvent.click(createButton);
fireEvent.click(createButton);
fireEvent.click(createButton);

await waitFor(() => {
expect(mockCreateAccount).toHaveBeenCalledTimes(1);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback } from 'react';
import React, { useCallback, useRef } from 'react';
import { CaipChainId } from '@metamask/utils';
import { KeyringTypes } from '@metamask/keyring-controller';
import { getNextAvailableAccountName } from '../../../store/actions';
Expand Down Expand Up @@ -43,10 +43,16 @@ export const CreateSnapAccount = ({
chainId,
}: CreateSnapAccountProps) => {
const snapClient = useMultichainWalletSnapClient(clientType);
const isCreatingAccount = useRef(false);

const onCreateAccount = useCallback(
async (_accountNameSuggestion?: string) => {
if (isCreatingAccount.current) {
return;
}

try {
isCreatingAccount.current = true;
await snapClient.createAccount({
scope: chainId,
///: BEGIN:ONLY_INCLUDE_IF(multi-srp)
Expand All @@ -57,6 +63,8 @@ export const CreateSnapAccount = ({
onActionComplete(true);
} catch (error) {
onActionComplete(false);
} finally {
isCreatingAccount.current = false;
}
},
[snapClient, chainId, selectedKeyringId, onActionComplete],
Expand Down
Loading