Skip to content
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

feat: update flexible multisig #2709

Merged
merged 3 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -32,6 +32,7 @@ export const SelectSignatoriesThreshold = () => {
const ownedSignatoriesWallets = useUnit(signatoryModel.$ownedSignatoriesWallets);
const hasDuplicateSignatories = useUnit(signatoryModel.$hasDuplicateSignatories);
const hasEmptySignatories = useUnit(signatoryModel.$hasEmptySignatories);
const hasEmptySignatoryName = useUnit(signatoryModel.$hasEmptySignatoryName);

const hasOwnedSignatory = !!ownedSignatoriesWallets && ownedSignatoriesWallets?.length > 0;
const hasEnoughSignatories = signatories.length >= MIN_THRESHOLD;
Expand All @@ -43,7 +44,9 @@ export const SelectSignatoriesThreshold = () => {
!multisigAlreadyExists &&
!hasEmptySignatories &&
isThresholdValid &&
!hasDuplicateSignatories;
!hasEmptySignatoryName &&
!hasDuplicateSignatories &&
!hiddenMultisig;

const onSubmit = (event: FormEvent) => {
if (!hasClickedNext) {
Expand Down Expand Up @@ -96,6 +99,14 @@ export const SelectSignatoriesThreshold = () => {
>
<Alert.Item withDot={false}>{t('createMultisigAccount.notEmptySignatory')}</Alert.Item>
</Alert>

<Alert
active={hasClickedNext && hasEmptySignatoryName}
title={t('createMultisigAccount.notEmptySignatoryNameTitle')}
variant="error"
>
<Alert.Item withDot={false}>{t('createMultisigAccount.notEmptySignatoryName')}</Alert.Item>
</Alert>
</div>
<div className="flex items-center gap-x-4">
<Box width="300px">
Expand Down Expand Up @@ -141,11 +152,7 @@ export const SelectSignatoriesThreshold = () => {
</Alert.Item>
</Alert>

<Alert
active={!multisigAlreadyExists && Boolean(hiddenMultisig)}
title={t('createMultisigAccount.multisigExistTitle')}
variant="info"
>
<Alert active={Boolean(hiddenMultisig)} title={t('createMultisigAccount.multisigExistTitle')} variant="error">
<Alert.Item withDot={false}>{t('createMultisigAccount.multisigHiddenExistText')}</Alert.Item>
<Alert.Item withDot={false}>
<Button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { networkModel, networkUtils } from '@/entities/network';
import { getExtrinsic, transactionBuilder } from '@/entities/transaction';
import { walletModel, walletUtils } from '@/entities/wallet';
import { signModel } from '@/features/operations/OperationSign/model/sign-model';
import { ExtrinsicResult, submitModel, submitUtils } from '@/features/operations/OperationSubmit';
import { submitModel, submitUtils } from '@/features/operations/OperationSubmit';
import { walletPairingModel } from '@/features/wallets';

import { confirmModel } from './confirm-model';
Expand Down Expand Up @@ -378,9 +378,38 @@ sample({
contacts: contactModel.$contacts,
},
fn: ({ signatories, contacts }) => {
const signatoriesWithoutSigner = signatories.slice(1);
const contactMap = new Map(contacts.map((c) => [c.accountId, c]));
const updatedContacts: Contact[] = [];

for (const { address, name } of signatoriesWithoutSigner) {
const contact = contactMap.get(toAccountId(address));

if (!contact) continue;

updatedContacts.push({
...contact,
name,
});
}

return updatedContacts;
},
target: contactModel.effects.updateContactsFx,
});

sample({
clock: signModel.output.formSubmitted,
source: {
signatories: signatoryModel.$signatories,
contacts: contactModel.$contacts,
},
fn: ({ signatories, contacts }) => {
const contactsSet = new Set(contacts.map((c) => c.accountId));

return signatories
.slice(1)
.filter((signatory) => !contacts.some((contact) => contact.accountId === toAccountId(signatory.address)))
.filter((signatory) => !contactsSet.has(toAccountId(signatory.address)))
.map(
({ address, name }) =>
({
Expand Down Expand Up @@ -450,26 +479,11 @@ sample({

sample({
clock: walletModel.events.walletCreatedDone,
filter: ({ wallet, external }) => wallet.type === WalletType.FLEXIBLE_MULTISIG && !external,
fn: ({ wallet }) => wallet.id,
target: walletProviderModel.events.completed,
});

sample({
clock: submitModel.output.formSubmitted,
source: {
step: $step,
hiddenMultisig: formModel.$hiddenMultisig,
},
filter: ({ step, hiddenMultisig }, results) => {
const isSubmitStep = isStep(step, Step.SUBMIT);
const isNonNullable = nonNullable(hiddenMultisig);
const isSuccessResult = results[0]?.result === ExtrinsicResult.SUCCESS;

return isSubmitStep && isNonNullable && isSuccessResult;
},
fn: ({ hiddenMultisig }) => hiddenMultisig!.id,
target: walletModel.events.walletRemoved,
});

sample({
clock: delay(submitModel.output.formSubmitted, 2000),
source: $step,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { combine, createEvent, sample } from 'effector';
import { combine, sample } from 'effector';
import { createForm } from 'effector-forms';

import { type Chain, type ChainId, CryptoType, type Wallet } from '@/shared/core';
import { type Chain, type ChainId, CryptoType } from '@/shared/core';
import { nonNullable, toAccountId } from '@/shared/lib/utils';
import { networkModel, networkUtils } from '@/entities/network';
import { accountUtils, walletModel, walletUtils } from '@/entities/wallet';
Expand All @@ -10,8 +10,6 @@ import { signatoryModel } from './signatory-model';

const DEFAULT_CHAIN: ChainId = '0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3'; // Polkadot

const restoreWallet = createEvent<Wallet>();

type FormParams = {
threshold: number;
chainId: ChainId;
Expand Down Expand Up @@ -144,11 +142,6 @@ sample({
target: $createMultisigForm.fields.threshold.reset,
});

sample({
clock: restoreWallet,
target: walletModel.events.walletRestored,
});

export const formModel = {
$createMultisigForm,
$multisigAccountId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,24 @@ const $hasEmptySignatories = combine($signatories, (signatories) => {
return signatories.map(({ address }) => address).includes('');
});

const $hasEmptySignatoryName = combine($signatories, (signatories) => {
return signatories.map(({ name }) => name).includes('');
});

const $ownedSignatoriesWallets = combine(
{ wallets: walletModel.$wallets, signatories: $signatories },
({ wallets, signatories }) =>
walletUtils.getWalletsFilteredAccounts(wallets, {
{
wallets: walletModel.$wallets,
signatories: $signatories,
},
({ wallets, signatories }) => {
const matchWallets = walletUtils.getWalletsFilteredAccounts(wallets, {
walletFn: (w) => walletUtils.isValidSignatory(w),
accountFn: (a) => signatories.some((s) => toAccountId(s.address) === a.accountId),
}) || [],
);
});

return matchWallets || [];
},
);
const populateBalanceFx = createEffect((wallets: Wallet[]) => {
for (const wallet of wallets) {
balanceSubModel.events.walletToSubSet(wallet);
Expand Down Expand Up @@ -104,6 +113,7 @@ export const signatoryModel = {
$ownedSignatoriesWallets,
$hasDuplicateSignatories,
$hasEmptySignatories,
$hasEmptySignatoryName,
events: {
addSignatory,
changeSignatory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ sample({
target: walletModel.events.selectWallet,
});

sample({
clock: walletModel.events.walletRestoredSuccess,
fn: ({ result }) => result.id,
target: walletModel.events.selectWallet,
});

sample({
clock: walletModel.events.selectWallet,
source: walletModel.$activeWallet,
Expand Down
19 changes: 1 addition & 18 deletions src/renderer/widgets/CreateWallet/model/flow-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { networkModel, networkUtils } from '@/entities/network';
import { transactionService } from '@/entities/transaction';
import { accountUtils, walletModel, walletUtils } from '@/entities/wallet';
import { signModel } from '@/features/operations/OperationSign/model/sign-model';
import { ExtrinsicResult, submitModel, submitUtils } from '@/features/operations/OperationSubmit';
import { submitModel, submitUtils } from '@/features/operations/OperationSubmit';
import { walletPairingModel } from '@/features/wallets';
import { type AddMultisigStore, type FormSubmitEvent } from '../lib/types';

Expand Down Expand Up @@ -387,23 +387,6 @@ sample({
}),
});

sample({
clock: submitModel.output.formSubmitted,
source: {
step: $step,
hiddenMultisig: formModel.$hiddenMultisig,
},
filter: ({ step, hiddenMultisig }, results) => {
const isSubmitStep = isStep(step, Step.SUBMIT);
const isNonNullable = nonNullable(hiddenMultisig);
const isSuccessResult = results[0]?.result === ExtrinsicResult.SUCCESS;

return isSubmitStep && isNonNullable && isSuccessResult;
},
fn: ({ hiddenMultisig }) => hiddenMultisig!.id,
target: walletModel.events.walletRemoved,
});

sample({
clock: delay(submitModel.output.formSubmitted, 2000),
source: $step,
Expand Down
11 changes: 2 additions & 9 deletions src/renderer/widgets/CreateWallet/model/form-model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { combine, createEvent, sample } from 'effector';
import { combine, sample } from 'effector';
import { createForm } from 'effector-forms';

import { type Chain, type ChainId, CryptoType, type Wallet } from '@/shared/core';
import { type Chain, type ChainId, CryptoType } from '@/shared/core';
import { nonNullable, toAccountId } from '@/shared/lib/utils';
import { networkModel, networkUtils } from '@/entities/network';
import { accountUtils, walletModel, walletUtils } from '@/entities/wallet';
Expand All @@ -11,8 +11,6 @@ import { signatoryModel } from './signatory-model';

const DEFAULT_CHAIN: ChainId = '0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3'; // Polkadot

const restoreWallet = createEvent<Wallet>();

const $createMultisigForm = createForm<FormParams>({
fields: {
threshold: {
Expand Down Expand Up @@ -139,11 +137,6 @@ sample({
target: $createMultisigForm.fields.threshold.reset,
});

sample({
clock: restoreWallet,
target: walletModel.events.walletRestored,
});

export const formModel = {
$chain,
$createMultisigForm,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export const SelectSignatoriesThreshold = () => {
!hasEmptySignatories &&
!hasEmptySignatoryName &&
isThresholdValid &&
!hasDuplicateSignatories;
!hasDuplicateSignatories &&
!hiddenMultisig;

const onSubmit = (event: FormEvent) => {
if (!hasClickedNext) {
Expand Down Expand Up @@ -156,11 +157,7 @@ export const SelectSignatoriesThreshold = () => {
</Alert.Item>
</Alert>

<Alert
active={!multisigAlreadyExists && Boolean(hiddenMultisig)}
title={t('createMultisigAccount.multisigExistTitle')}
variant="info"
>
<Alert active={Boolean(hiddenMultisig)} title={t('createMultisigAccount.multisigExistTitle')} variant="error">
<Alert.Item withDot={false}>{t('createMultisigAccount.multisigHiddenExistText')}</Alert.Item>
<Alert.Item withDot={false}>
<Button
Expand Down
Loading