Skip to content

Commit

Permalink
fix: now multisig wallet got selected after being created (#2658)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnthecat authored Nov 15, 2024
1 parent c419488 commit 52ace73
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 51 deletions.
5 changes: 5 additions & 0 deletions src/renderer/entities/wallet/model/wallet-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ const walletCreatedDone = sample({
clock: [walletCreatedFx.doneData, multishardCreatedFx.doneData],
}).filter({ fn: nonNullable });

const walletCreationFail = sample({
clock: [walletCreatedFx.fail, multishardCreatedFx.fail],
}).filter({ fn: nonNullable });

sample({
clock: [walletConnectCreated, watchOnlyCreated, multisigCreated, singleshardCreated, proxiedCreated],
target: walletCreatedFx,
Expand Down Expand Up @@ -369,6 +373,7 @@ export const walletModel = {
walletConnectCreated,
proxiedCreated,
walletCreatedDone,
walletCreationFail,
selectWallet,
updateAccounts,
updateWallet,
Expand Down
86 changes: 35 additions & 51 deletions src/renderer/widgets/CreateWallet/model/flow-model.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { BN } from '@polkadot/util';
import { combine, createEffect, createEvent, createStore, restore, sample } from 'effector';
import { combine, createEvent, createStore, restore, sample } from 'effector';
import sortBy from 'lodash/sortBy';
import { delay, spread } from 'patronum';

import {
type Account,
AccountType,
type ChainId,
ChainType,
type Contact,
CryptoType,
type MultisigAccount,
type Signatory,
type NoID,
SigningType,
type Transaction,
TransactionType,
Expand Down Expand Up @@ -186,43 +185,6 @@ const $isEnoughBalance = combine(
},
);

type CreateWalletParams = {
name: string;
threshold: number;
signatories: Signatory[];
chainId: ChainId | null;
isEthereumChain: boolean;
};

const createWalletFx = createEffect(
async ({ name, threshold, signatories, chainId, isEthereumChain }: CreateWalletParams) => {
const cryptoType = isEthereumChain ? CryptoType.ETHEREUM : CryptoType.SR25519;
const accountIds = signatories.map((s) => s.accountId);
const accountId = accountUtils.getMultisigAccountId(accountIds, threshold, cryptoType);

walletModel.events.multisigCreated({
wallet: {
name,
type: WalletType.MULTISIG,
signingType: SigningType.MULTISIG,
},
accounts: [
{
signatories,
chainId: chainId || undefined,
name: name.trim(),
accountId: accountId,
threshold: threshold,
creatorAccountId: accountId,
cryptoType: isEthereumChain ? CryptoType.ETHEREUM : CryptoType.SR25519,
chainType: isEthereumChain ? ChainType.ETHEREUM : ChainType.SUBSTRATE,
type: AccountType.MULTISIG,
},
],
});
},
);

const $fakeTx = combine(
{
chain: formModel.$createMultisigForm.fields.chain.$value,
Expand Down Expand Up @@ -259,34 +221,56 @@ sample({
step: $step,
},
filter: ({ step }, results) => {
return submitUtils.isSuccessResult(results[0].result) && isStep(Step.SUBMIT, step);
return isStep(Step.SUBMIT, step) && results.some(({ result }) => submitUtils.isSuccessResult(result));
},
fn: ({ signatories, chain, name, threshold }) => {
const sortedSignatories = sortBy(
Array.from(signatories.values()).map((a) => ({ address: a.address, accountId: toAccountId(a.address) })),
'accountId',
);

return {
name,
threshold,
chainId: chain.chainId,
const isEthereumChain = networkUtils.isEthereumBased(chain.options);
const cryptoType = isEthereumChain ? CryptoType.ETHEREUM : CryptoType.SR25519;
const accountIds = sortedSignatories.map((s) => s.accountId);
const accountId = accountUtils.getMultisigAccountId(accountIds, threshold, cryptoType);

const account: Omit<NoID<MultisigAccount>, 'walletId'> = {
signatories: sortedSignatories,
isEthereumChain: networkUtils.isEthereumBased(chain.options),
chainId: chain.chainId,
name: name.trim(),
accountId: accountId,
threshold: threshold,
creatorAccountId: accountId,
cryptoType: isEthereumChain ? CryptoType.ETHEREUM : CryptoType.SR25519,
chainType: isEthereumChain ? ChainType.ETHEREUM : ChainType.SUBSTRATE,
type: AccountType.MULTISIG,
};

return {
wallet: {
name,
type: WalletType.MULTISIG,
signingType: SigningType.MULTISIG,
},
accounts: [account],
};
},
target: createWalletFx,
target: walletModel.events.multisigCreated,
});

sample({
clock: createWalletFx.failData,
fn: (error) => error.message,
clock: walletModel.events.walletCreationFail,
filter: ({ params }) => params.wallet.type === WalletType.MULTISIG,
fn: ({ error }) => error.message,
target: $error,
});

sample({
clock: createWalletFx.doneData,
target: walletProviderModel.events.completed,
clock: walletModel.events.walletCreatedDone,
filter: ({ wallet }) => wallet.type === WalletType.MULTISIG,
fn: ({ wallet }) => wallet.id,
// wallet selection shouldn't be here, but here we are
target: [walletModel.events.selectWallet, walletProviderModel.events.completed],
});

// Submit
Expand Down

0 comments on commit 52ace73

Please sign in to comment.