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: add remove proxy with proxy #1633

Merged
merged 8 commits into from
Apr 8, 2024
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 @@ -12,13 +12,14 @@ import { Step } from '../../lib/types';
import { formModel } from '../form-model';
import { confirmModel } from '../confirm-model';
import { addProxyModel } from '../add-proxy-model';
import { Transaction } from '@entities/transaction';

jest.mock('@shared/lib/utils', () => ({
...jest.requireActual('@shared/lib/utils'),
getProxyTypes: jest.fn().mockReturnValue(['Any', 'Staking']),
}));

describe('widgets/AddPureProxyModal/model/add-proxy-model', () => {
describe('widgets/AddProxyModal/model/add-proxy-model', () => {
beforeAll(() => {
jest.useFakeTimers();
});
Expand Down Expand Up @@ -49,14 +50,19 @@ describe('widgets/AddPureProxyModal/model/add-proxy-model', () => {
await allSettled(formModel.output.formSubmitted, {
scope,
params: {
proxyDeposit: '1',
oldProxyDeposit: '0',
proxyNumber: 1,
chain: testChain,
account: { accountId: '0x00' } as unknown as Account,
delegate: '0x00',
proxyType: ProxyType.ANY,
description: '',
transactions: {
wrappedTx: {} as Transaction,
coreTx: {} as Transaction,
},
formData: {
proxyDeposit: '1',
proxyNumber: 1,
chain: testChain,
account: { accountId: '0x00' } as unknown as Account,
delegate: '0x00',
proxyType: ProxyType.ANY,
description: '',
},
},
});

Expand Down
95 changes: 19 additions & 76 deletions src/renderer/widgets/AddProxyModal/model/add-proxy-model.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { createEvent, createStore, sample } from 'effector';
import { spread, delay } from 'patronum';

import { Transaction, TransactionType, transactionService, TxWrapper, WrapperKind } from '@entities/transaction';
import { Transaction } from '@entities/transaction';
import { signModel } from '@features/operations/OperationSign/model/sign-model';
import { submitModel } from '@features/operations/OperationSubmit';
import { toAddress, toAccountId } from '@shared/lib/utils';
import { toAccountId } from '@shared/lib/utils';
import { walletSelectModel } from '@features/wallets';
import { walletModel, walletUtils } from '@entities/wallet';
import { ProxyGroup, NoID, MultisigAccount, Account } from '@shared/core';
import { walletModel } from '@entities/wallet';
import { ProxyGroup, NoID } from '@shared/core';
import { proxyModel, proxyUtils } from '@entities/proxy';
import { networkModel } from '@entities/network';
import { balanceSubModel } from '@features/balances';
import { Step, TxWrappers, AddProxyStore } from '../lib/types';
import { Step, AddProxyStore } from '../lib/types';
import { formModel } from './form-model';
import { confirmModel } from './confirm-model';

Expand All @@ -22,12 +21,10 @@ const flowFinished = createEvent();

const $step = createStore<Step>(Step.NONE);

const $addProxyStore = createStore<AddProxyStore | null>(null);
const $wrappedTx = createStore<Transaction | null>(null);
const $coreTx = createStore<Transaction | null>(null);
const $multisigTx = createStore<Transaction | null>(null);

const $txWrappers = createStore<TxWrappers>([]);
const $addProxyStore = createStore<AddProxyStore | null>(null).reset(flowFinished);
const $wrappedTx = createStore<Transaction | null>(null).reset(flowFinished);
const $coreTx = createStore<Transaction | null>(null).reset(flowFinished);
const $multisigTx = createStore<Transaction | null>(null).reset(flowFinished);

sample({ clock: stepChanged, target: $step });

Expand Down Expand Up @@ -59,77 +56,24 @@ sample({

sample({
clock: formModel.output.formSubmitted,
target: $addProxyStore,
});

sample({
clock: formModel.output.formSubmitted,
source: {
wallet: walletSelectModel.$walletForDetails,
wallets: walletModel.$wallets,
},
fn: ({ wallet, wallets }, { account }): TxWrappers => {
if (!wallet) return [];
if (walletUtils.isMultisig(wallet)) return ['multisig'];
if (!walletUtils.isProxied(wallet)) return [];

const accountWallet = walletUtils.getWalletById(wallets, account.walletId);

return walletUtils.isMultisig(accountWallet) ? ['multisig', 'proxy'] : ['proxy'];
},
target: $txWrappers,
});

sample({
clock: formModel.output.formSubmitted,
source: {
txWrappers: $txWrappers,
apis: networkModel.$apis,
},
fn: ({ txWrappers, apis }, formData) => {
const { chain, account, signatory, delegate, proxyType } = formData;

const transaction: Transaction = {
chainId: chain.chainId,
address: toAddress(account.accountId, { prefix: chain.addressPrefix }),
type: TransactionType.ADD_PROXY,
args: { delegate, proxyType, delay: 0 },
};

const isMultisig = txWrappers.includes('multisig');
const txWrappersAdapter: TxWrapper[] = isMultisig
? [
{
kind: WrapperKind.MULTISIG,
multisigAccount: account as MultisigAccount,
signatories: (account as MultisigAccount).signatories.map((s) => ({ accountId: s.accountId })) as Account[],
signer: { accountId: signatory!.accountId } as Account,
},
]
: [];

const transactions = transactionService.getWrappedTransaction({
api: apis[chain.chainId],
addressPrefix: chain.addressPrefix,
transaction,
txWrappers: txWrappersAdapter,
});

return { ...transactions, multisigTx: transactions.multisigTx || null };
},
fn: ({ transactions, formData }) => ({
wrappedTx: transactions.wrappedTx,
multisigTx: transactions.multisigTx || null,
coreTx: transactions.coreTx,
store: formData,
}),
target: spread({
wrappedTx: $wrappedTx,
coreTx: $coreTx,
multisigTx: $multisigTx,
coreTx: $coreTx,
store: $addProxyStore,
}),
});

sample({
clock: formModel.output.formSubmitted,
source: $wrappedTx,
filter: (wrappedTx: Transaction | null): wrappedTx is Transaction => Boolean(wrappedTx),
fn: (wrappedTx, formData) => ({
event: { ...formData, transaction: wrappedTx },
fn: ({ formData, transactions }) => ({
event: { ...formData, transaction: transactions.wrappedTx },
step: Step.CONFIRM,
}),
target: spread({
Expand Down Expand Up @@ -166,7 +110,6 @@ sample({
addProxyStore: $addProxyStore,
coreTx: $coreTx,
multisigTx: $multisigTx,
txWrappers: $txWrappers,
},
filter: (proxyData) => {
return Boolean(proxyData.addProxyStore) && Boolean(proxyData.coreTx);
Expand Down
23 changes: 20 additions & 3 deletions src/renderer/widgets/AddProxyModal/model/confirm-model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createEvent, combine, restore } from 'effector';

import { Chain, Account, ProxyType, Address } from '@shared/core';
import { Chain, Account, ProxyType, Address, ProxiedAccount } from '@shared/core';
import { networkModel } from '@entities/network';
import { Transaction } from '@entities/transaction';
import { walletModel, walletUtils } from '@entities/wallet';
Expand All @@ -13,15 +13,16 @@ type Input = {
delegate: Address;
description: string;
transaction: Transaction;
proxiedAccount?: ProxiedAccount;

oldProxyDeposit: string;
proxyDeposit: string;
proxyNumber: number;
};

const formInitiated = createEvent<Input>();
const formSubmitted = createEvent();

const $confirmStore = restore<Input>(formInitiated, null);
const $confirmStore = restore<Input>(formInitiated, null).reset(formSubmitted);

const $api = combine(
{
Expand All @@ -43,6 +44,20 @@ const $initiatorWallet = combine(

return walletUtils.getWalletById(wallets, store.account.walletId);
},
{ skipVoid: false },
);

const $proxiedWallet = combine(
{
store: $confirmStore,
wallets: walletModel.$wallets,
},
({ store, wallets }) => {
if (!store || !store.proxiedAccount) return undefined;

return walletUtils.getWalletById(wallets, store.proxiedAccount.walletId);
},
{ skipVoid: false },
);

const $signerWallet = combine(
Expand All @@ -55,12 +70,14 @@ const $signerWallet = combine(

return walletUtils.getWalletById(wallets, store.signatory?.walletId || store.account.walletId);
},
{ skipVoid: false },
);

export const confirmModel = {
$confirmStore,
$initiatorWallet,
$signerWallet,
$proxiedWallet,
$api,
events: {
formInitiated,
Expand Down
Loading
Loading