Skip to content

Commit

Permalink
fix: basket operations (#2438)
Browse files Browse the repository at this point in the history
  • Loading branch information
sokolova-an authored Oct 9, 2024
1 parent 7a981a5 commit a62c8af
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 49 deletions.
38 changes: 13 additions & 25 deletions src/renderer/features/governance/aggregates/locksPeriod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { combine, createEvent, sample } from 'effector';
import { type ApiPromise } from '@polkadot/api';
import { createStore, sample } from 'effector';
import { createGate } from 'effector-react';

import { type Chain } from '@/shared/core';
Expand All @@ -9,29 +10,17 @@ import { networkSelectorModel } from '../model/networkSelector';

const flow = createGate<{ chain?: Chain }>();

const $lockPeriodsInChain = combine(networkSelectorModel.$network, lockPeriodsModel.$lockPeriods, (network, locks) => {
if (!network) return null;

return locks[network.chain.chainId] ?? null;
});

const requestLockPeriods = createEvent();

sample({
clock: requestLockPeriods,
source: networkSelectorModel.$network,
filter: nonNullable,
target: lockPeriodsModel.events.requestLockPeriods,
});
const $network = createStore<{ chain: Chain; api: ApiPromise } | null>(null).reset(flow.close);

sample({
clock: networkSelectorModel.$network,
filter: nonNullable,
target: requestLockPeriods,
target: $network,
});

// When Confirmation screen opens, the chain may not be available at flow.open.
sample({
clock: flow.open,
clock: flow.state,
source: networkModel.$apis,
filter: (apis, { chain }) => {
return nonNullable(chain) && chain.chainId in apis;
Expand All @@ -40,17 +29,16 @@ sample({
api: apis[chain!.chainId],
chain: chain!,
}),
target: requestLockPeriods,
target: $network,
});

export const locksPeriodsAggregate = {
$lockPeriods: $lockPeriodsInChain,
$isLoading: lockPeriodsModel.$isLoading,

events: {
requestLockPeriods,
},
sample({
clock: $network,
filter: nonNullable,
target: lockPeriodsModel.events.requestLockPeriods,
});

export const locksPeriodsAggregate = {
gates: {
flow,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { type ReactNode } from 'react';

import { useI18n } from '@/app/providers';
import { formatAsset, formatBalance, toAddress, toNumberWithPrecision } from '@/shared/lib/utils';
import { Button, DetailRow, HeadlineText, Icon } from '@/shared/ui';
import { Button, DetailRow, HeadlineText, Icon, Loader } from '@/shared/ui';
import { Box } from '@/shared/ui-kit';
import { LockPeriodDiff, LockValueDiff, voteTransactionService, votingService } from '@/entities/governance';
import { SignButton } from '@/entities/operations';
import { Fee } from '@/entities/transaction';
Expand All @@ -26,13 +27,15 @@ export const Confirmation = ({ id = 0, secondaryActionButton, hideSignButton, on
const { t } = useI18n();

const trackLocks = useUnit(locksAggregate.$trackLocks);

const confirm = useStoreMap({
store: confirmModel.$confirmMap,
keys: [id],
fn: (value, [id]) => value?.[id] ?? null,
});

useGate(locksPeriodsAggregate.gates.flow, { chain: confirm?.meta.chain });
useGate(locksAggregate.gates.flow, { chain: confirm?.meta.chain });

const lockPeriods = useStoreMap({
store: lockPeriodsModel.$lockPeriods,
keys: [confirm?.meta.chain],
Expand All @@ -41,11 +44,12 @@ export const Confirmation = ({ id = 0, secondaryActionButton, hideSignButton, on

const isMultisigExists = useUnit(confirmModel.$isMultisigExists);

useGate(locksPeriodsAggregate.gates.flow, { chain: confirm?.meta.chain });
useGate(locksAggregate.gates.flow, { chain: confirm?.meta.chain });

if (!confirm) {
return null;
if (!confirm || !lockPeriods) {
return (
<Box width="440px" height="430px" verticalAlign="center" horizontalAlign="center">
<Loader color="primary" />
</Box>
);
}

const { asset, existingVote, wrappedTransactions, api } = confirm.meta;
Expand Down
50 changes: 50 additions & 0 deletions src/renderer/pages/Basket/lib/prepareTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ import { governanceService, votingService } from '@/entities/governance';
import { networkUtils } from '@entities/network';
import { eraService, useStakingData, validatorsService } from '@entities/staking';
import { transactionService } from '@entities/transaction';
import { type VoteConfirm } from '@/features/operations/OperationsConfirm';
import { type FeeMap } from '@/features/operations/OperationsValidation';
import { type UnlockFormData } from '@features/governance/types/structs';
import { type RemoveVoteConfirm } from '@features/operations/OperationsConfirm/Referendum/RemoveVote/model/confirm-model';

import { getCoreTx } from './utils';

Expand Down Expand Up @@ -61,6 +63,8 @@ export const prepareTransaction = {
prepareDelegateTransaction,
prepareRevokeDelegationTransaction,
prepareEditDelegationTransaction,
prepareVoteTransaction,
prepareRemoveVoteTransaction,
};

async function getTransactionData(
Expand Down Expand Up @@ -654,3 +658,49 @@ async function prepareRevokeDelegationTransaction({
multisigDeposit: '0',
} satisfies RevokeDelegationInput;
}

async function prepareVoteTransaction({ transaction, wallets, chains, apis, feeMap }: DataParams) {
const coreTx = getCoreTx(transaction);

const { chainId, chain, account } = await getTransactionData(transaction, feeMap, apis, chains, wallets);
const api = apis[chainId];

return {
id: transaction.id,
api,
chain,
asset: chain.assets[0],
account: account!,
existingVote: coreTx.args.vote,
wrappedTransactions: transactionService.getWrappedTransaction({
api,
addressPrefix: chain.addressPrefix,
transaction: transaction.coreTx,
txWrappers: transaction.txWrappers,
}),
description: '',
} satisfies VoteConfirm;
}

async function prepareRemoveVoteTransaction({ transaction, wallets, chains, apis, feeMap }: DataParams) {
const coreTxs = transaction.coreTx.args.transactions || [transaction.coreTx];
const { chainId, chain, account } = await getTransactionData(transaction, feeMap, apis, chains, wallets);
const api = apis[chainId];

return {
id: transaction.id,
account: account!,
chain,
wrappedTransactions: transactionService.getWrappedTransaction({
api,
addressPrefix: chain.addressPrefix,
transaction: transaction.coreTx,
txWrappers: transaction.txWrappers,
}),

api,
asset: chain.assets[0],
votes: coreTxs.map((t: Transaction) => t.args),
description: '',
} satisfies RemoveVoteConfirm;
}
9 changes: 3 additions & 6 deletions src/renderer/pages/Basket/model/basket-page-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,7 @@ const validateFx = createEffect(({ transactions, feeMap }: ValidateParams) => {
const TransactionValidatorsRecord: Record<
Exclude<
TransactionType,
| TransferTransactionTypes
| XcmTransactionTypes
| MultisigTransactionTypes
| UtilityTransactionTypes
// TODO: Add remove vote types
| TransactionType.REMOVE_VOTE
TransferTransactionTypes | XcmTransactionTypes | MultisigTransactionTypes | UtilityTransactionTypes
>,
EventCallable<ValidationStartedParams>
> = {
Expand All @@ -158,6 +153,8 @@ const validateFx = createEffect(({ transactions, feeMap }: ValidateParams) => {
[TransactionType.EDIT_DELEGATION]: delegateValidateModel.events.validationStarted,
[TransactionType.VOTE]: voteValidateModel.events.validationStarted,
[TransactionType.REVOTE]: voteValidateModel.events.validationStarted,
// TODO: add separate validation for remove vote
[TransactionType.REMOVE_VOTE]: voteValidateModel.events.validationStarted,
};

if (coreTx.type in TransactionValidatorsRecord) {
Expand Down
12 changes: 4 additions & 8 deletions src/renderer/pages/Basket/model/sign-operations-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,7 @@ const startDataPreparationFx = createEffect(async ({ transactions, ...preparatio
const TransactionData: Record<
Exclude<
TransactionType,
| TransferTransactionTypes
| XcmTransactionTypes
| MultisigTransactionTypes
| UtilityTransactionTypes
// TODO: Add vote types
| TransactionType.VOTE
| TransactionType.REMOVE_VOTE
| TransactionType.REVOTE
TransferTransactionTypes | XcmTransactionTypes | MultisigTransactionTypes | UtilityTransactionTypes
>,
(dataParams: DataParams) => Promise<unknown>
> = {
Expand All @@ -126,6 +119,9 @@ const startDataPreparationFx = createEffect(async ({ transactions, ...preparatio
[TransactionType.DELEGATE]: prepareTransaction.prepareDelegateTransaction,
[TransactionType.EDIT_DELEGATION]: prepareTransaction.prepareEditDelegationTransaction,
[TransactionType.UNDELEGATE]: prepareTransaction.prepareRevokeDelegationTransaction,
[TransactionType.VOTE]: prepareTransaction.prepareVoteTransaction,
[TransactionType.REVOTE]: prepareTransaction.prepareVoteTransaction,
[TransactionType.REMOVE_VOTE]: prepareTransaction.prepareRemoveVoteTransaction,
};

if (coreTx.type in TransactionData) {
Expand Down
12 changes: 9 additions & 3 deletions src/renderer/widgets/UnlockModal/ui/UnlockConfirmation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { type ReactNode } from 'react';

import { useI18n } from '@app/providers';
import { useToggle } from '@/shared/lib/hooks';
import { Box } from '@/shared/ui-kit';
import { cnTw } from '@shared/lib/utils';
import { Button, CaptionText, DetailRow, FootnoteText, Icon, Tooltip } from '@shared/ui';
import { Button, CaptionText, DetailRow, FootnoteText, Icon, Loader, Tooltip } from '@shared/ui';
import { BalanceDiff } from '@/entities/governance';
import { SignButton } from '@/entities/operations';
import { AccountsModal } from '@/entities/staking';
Expand Down Expand Up @@ -60,8 +61,13 @@ export const UnlockConfirmation = ({ id = 0, hideSignButton, secondaryActionButt

const [isAccountsOpen, toggleAccounts] = useToggle();

if (!confirmStore || !initiatorWallet || !confirmStore.chain) return null;

if (!confirmStore || !initiatorWallet || !confirmStore.chain) {
return (
<Box width="440px" height="430px" verticalAlign="center" horizontalAlign="center">
<Loader color="primary" />
</Box>
);
}
const { chain, asset, amount, shards, totalLock } = confirmStore;

return (
Expand Down

0 comments on commit a62c8af

Please sign in to comment.