Skip to content

Commit

Permalink
fix: rename wallet accounts (#2633)
Browse files Browse the repository at this point in the history
  • Loading branch information
sokolova-an authored Nov 12, 2024
1 parent f88b3c2 commit 0c4f26c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 24 deletions.
32 changes: 15 additions & 17 deletions src/renderer/entities/walletConnect/model/wallet-connect-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import keyBy from 'lodash/keyBy';

import { localStorageService } from '@/shared/api/local-storage';
import { storageService } from '@/shared/api/storage';
import { type Account, type ID, type Wallet, type WcAccount, kernelModel } from '@/shared/core';
import { type Account, type ID, type Wallet, kernelModel } from '@/shared/core';
import { nonNullable } from '@/shared/lib/utils';
import { walletModel, walletUtils } from '@/entities/wallet';
import {
Expand All @@ -29,7 +29,7 @@ type SessionTopicParams = {

type UpdateAccountsParams = {
walletId: ID;
accounts: WcAccount[];
accounts: Account[];
};

const connect = createEvent<Omit<InitConnectParams, 'client'>>();
Expand Down Expand Up @@ -187,25 +187,23 @@ const removePairingFx = createEffect(async ({ client, topic }: { client: Client;

type UpdateParams = {
wallet: Wallet;
accounts: WcAccount[];
accounts: Account[];
};
const updateWcAccountsFx = createEffect(
async ({ wallet, accounts }: UpdateParams): Promise<WcAccount[] | undefined> => {
const oldAccountIds = wallet.accounts.map((account) => account.id);
const newAccountsWithoutId = accounts.map((account) => {
const { id: _, ...newAccount } = account;
const updateWcAccountsFx = createEffect(async ({ wallet, accounts }: UpdateParams): Promise<Account[] | undefined> => {
const oldAccountIds = wallet.accounts.map((account) => account.id);
const newAccountsWithoutId = accounts.map((account) => {
const { id: _, ...newAccount } = account;

return newAccount;
});
return newAccount;
});

const [_, newAccounts] = await Promise.all([
storageService.accounts.deleteAll(oldAccountIds),
storageService.accounts.createAll(newAccountsWithoutId),
]);
const [_, newAccounts] = await Promise.all([
storageService.accounts.deleteAll(oldAccountIds),
storageService.accounts.createAll(newAccountsWithoutId),
]);

return newAccounts as WcAccount[];
},
);
return newAccounts;
});

type DisconnectParams = {
client: Client;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { SigningType, WalletType } from '@/shared/core';
import { type Account, AccountType, CryptoType, SigningType, WalletType } from '@/shared/core';

const wallet1 = {
id: 1,
accounts: [{ walletId: 1, cryptoType: CryptoType.SR25519, type: AccountType.BASE }] as Account[],
name: 'My first wallet',
isActive: false,
type: WalletType.MULTISIG,
Expand All @@ -10,6 +11,7 @@ const wallet1 = {

const wallet2 = {
id: 2,
accounts: [{ walletId: 2, cryptoType: CryptoType.SR25519, type: AccountType.BASE }] as Account[],
name: 'My second wallet',
isActive: false,
type: WalletType.WATCH_ONLY,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import { allSettled, fork } from 'effector';

import { storageService } from '@/shared/api/storage';
import { type Account } from '@/shared/core';
import { walletModel } from '@/entities/wallet';
import { renameWalletModel } from '../rename-wallet-model';

import { walletMock } from './mocks/wallet-mock';

jest.mock('@walletconnect/utils', () => ({
getSdkError: jest.fn(),
}));

jest.mock('@walletconnect/sign-client', () => ({
Client: {},
}));

describe('entities/wallet/model/wallet-model', () => {
afterEach(() => {
jest.clearAllMocks();
Expand All @@ -26,9 +35,15 @@ describe('entities/wallet/model/wallet-model', () => {

test('should updated wallet name after form submit', async () => {
const newName = 'New wallet name';
const updatedWallet = { ...walletMock.wallet1, name: newName };
const updatedWallet = {
...walletMock.wallet1,
name: newName,
accounts: [{ cryptoType: 0, name: 'New wallet name', type: 'base', walletId: 1 }] as Account[],
};

jest.spyOn(storageService.wallets, 'update').mockResolvedValue(updatedWallet.id);
jest.spyOn(storageService.accounts, 'deleteAll').mockResolvedValue([1]);
jest.spyOn(storageService.accounts, 'createAll').mockResolvedValue(updatedWallet.accounts);

const scope = fork({
values: new Map().set(walletModel.$allWallets, [walletMock.wallet1]),
Expand All @@ -38,6 +53,6 @@ describe('entities/wallet/model/wallet-model', () => {
await allSettled(renameWalletModel.$walletForm.fields.name.onChange, { scope, params: newName });
await allSettled(renameWalletModel.$walletForm.submit, { scope });

expect(scope.getState(walletModel.$wallets)).toEqual([updatedWallet]);
expect(scope.getState(walletModel.$allWallets)).toEqual([updatedWallet]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { not } from 'patronum';

import { storageService } from '@/shared/api/storage';
import { type Wallet } from '@/shared/core';
import { splice } from '@/shared/lib/utils';
import { walletModel } from '@/entities/wallet';
import { nonNullable, splice } from '@/shared/lib/utils';
import { walletModel, walletUtils } from '@/entities/wallet';
import { walletConnectModel } from '@/entities/walletConnect';

export type Callbacks = {
onSubmit: () => void;
Expand Down Expand Up @@ -77,11 +78,30 @@ function validateNameExist(value: string, _: unknown, params: SourceParams): boo
sample({
clock: $walletForm.formValidated,
source: $walletToEdit,
filter: (walletToEdit) => walletToEdit !== null,
fn: (walletToEdit, form) => ({ ...walletToEdit!, name: form.name }),
filter: (walletToEdit) => nonNullable(walletToEdit),
fn: (walletToEdit, form) => ({
...walletToEdit!,
name: form.name,
accounts:
walletUtils.isPolkadotVault(walletToEdit!) || walletUtils.isMultiShard(walletToEdit!)
? walletToEdit!.accounts
: walletToEdit!.accounts?.map((acc) => ({ ...acc, name: form.name })),
}),
target: renameWalletFx,
});

sample({
clock: renameWalletFx.doneData,
filter: (updatedWallet) => {
return !walletUtils.isPolkadotVault(updatedWallet) && !walletUtils.isMultiShard(updatedWallet);
},
fn: (updatedWallet) => ({
walletId: updatedWallet.id,
accounts: updatedWallet.accounts,
}),
target: walletConnectModel.events.accountsUpdated,
});

sample({
clock: renameWalletFx.doneData,
source: walletModel.$allWallets,
Expand Down

0 comments on commit 0c4f26c

Please sign in to comment.