Skip to content

Commit

Permalink
feat: add existential deposit
Browse files Browse the repository at this point in the history
  • Loading branch information
sokolova-an committed Nov 21, 2024
1 parent 2b70515 commit 77c0773
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { BN } from '@polkadot/util';
import { useUnit } from 'effector-react';
import { memo } from 'react';

Expand All @@ -22,7 +21,7 @@ export const MultisigFees = memo(({ asset }: Props) => {
const proxyDeposit = useUnit(flexibleMultisigModel.$proxyDeposit);
const isLoading = useUnit(flexibleMultisigModel.$isLoading);

const totalFee = multisigDeposit.add(fee).add(new BN(proxyDeposit));
const totalFee = multisigDeposit.add(fee).add(proxyDeposit);

if (isLoading) {
return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { type ApiPromise } from '@polkadot/api';
import { BN } from '@polkadot/util';
import { BN, BN_ZERO } from '@polkadot/util';
import { combine, createEffect, createEvent, createStore, restore, sample } from 'effector';
import sortBy from 'lodash/sortBy';
import { delay, or, spread } from 'patronum';

import { balanceService } from '@/shared/api/balances';
import { proxyService } from '@/shared/api/proxy';
import {
type Account,
AccountType,
type Asset,
type Chain,
ChainType,
type Contact,
Expand All @@ -24,7 +26,6 @@ import {
SS58_DEFAULT_PREFIX,
Step,
TEST_ACCOUNTS,
ZERO_BALANCE,
isStep,
nonNullable,
toAccountId,
Expand Down Expand Up @@ -76,7 +77,7 @@ const walletCreated = createEvent<{

const $step = restore(stepChanged, Step.NAME_NETWORK).reset(flowFinished);

const $proxyDeposit = createStore(ZERO_BALANCE).reset(flowFinished);
const $proxyDeposit = createStore(BN_ZERO).reset(flowFinished);
const $error = createStore('').reset(flowFinished);
const $wrappedTx = createStore<Transaction | null>(null).reset(flowFinished);
const $coreTx = createStore<Transaction | null>(null).reset(flowFinished);
Expand Down Expand Up @@ -119,7 +120,7 @@ const $transaction = combine(
proxyDeposit: $proxyDeposit,
},
({ api, form, chain, isConnected, signatories, signer, proxyDeposit, multisigAccountId }) => {
if (!isConnected || !chain || !api || !multisigAccountId || !form.threshold || !proxyDeposit || !signer) {
if (!isConnected || !chain || !api || !multisigAccountId || !form.threshold || !signer) {
return null;
}

Expand All @@ -132,7 +133,7 @@ const $transaction = combine(
signatories: signatoriesWrapped,
multisigAccountId,
threshold: form.threshold,
proxyDeposit,
proxyDeposit: proxyDeposit.toString(),
});
},
);
Expand Down Expand Up @@ -181,18 +182,28 @@ const { $deposit: $multisigDeposit, $pending: $pendingDeposit } = createDepositC
$threshold: formModel.$createMultisigForm.fields.threshold.$value,
});

const getProxyDepositFx = createEffect((api: ApiPromise): string => {
return proxyService.getProxyDeposit(api, '0', 1);
type GetDepositParams = {
api: ApiPromise;
asset: Asset;
};

const getDepositFx = createEffect(async ({ api, asset }: GetDepositParams): Promise<BN> => {
const minDeposit = await balanceService.getExistentialDeposit(api, asset);
const proxyDeposit = new BN(proxyService.getProxyDeposit(api, '0', 1));

return BN.max(minDeposit, proxyDeposit);
});

sample({
clock: $api,
filter: nonNullable,
target: getProxyDepositFx,
source: formModel.$chain,
filter: (chain, api) => nonNullable(api) && nonNullable(chain) && nonNullable(chain.assets?.[0]),
fn: (chain, api) => ({ api: api!, asset: chain!.assets[0] }),
target: getDepositFx,
});

sample({
clock: getProxyDepositFx.doneData,
clock: getDepositFx.doneData,
target: $proxyDeposit,
});

Expand Down Expand Up @@ -508,7 +519,7 @@ export const flexibleMultisigModel = {
$fee,
$proxyDeposit,
$multisigDeposit,
$isLoading: or($pendingFee, $pendingDeposit, getProxyDepositFx.pending),
$isLoading: or($pendingFee, $pendingDeposit, getDepositFx.pending),
$isEnoughBalance,

events: {
Expand Down
14 changes: 13 additions & 1 deletion src/renderer/shared/api/balances/service/balanceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { type Vec } from '@polkadot/types';
import { type AccountData, type Balance as ChainBalance } from '@polkadot/types/interfaces';
import { type PalletBalancesBalanceLock } from '@polkadot/types/lookup';
import { type Codec } from '@polkadot/types/types';
import { type BN, BN_ZERO, hexToU8a } from '@polkadot/util';
import { BN, BN_ZERO, hexToU8a } from '@polkadot/util';
import noop from 'lodash/noop';
import uniq from 'lodash/uniq';

Expand All @@ -25,6 +25,7 @@ type NoIdBalance = Omit<Balance, 'id'>;
export const balanceService = {
subscribeBalances,
subscribeLockBalances,
getExistentialDeposit,
};

/**
Expand Down Expand Up @@ -308,3 +309,14 @@ function subscribeLockOrmlAssetChange(
callback(newLocks);
});
}

async function getExistentialDeposit(api: ApiPromise, asset: Asset): Promise<BN> {
switch (asset.type) {
case AssetType.NATIVE:
return api.consts.balances.existentialDeposit.toBn();
case AssetType.STATEMINE:
return await api.query.assets.asset(asset.assetId).then((balance) => balance.value.minBalance.toBn());
case AssetType.ORML:
return new BN((asset.typeExtras as OrmlExtras).existentialDeposit);
}
}

0 comments on commit 77c0773

Please sign in to comment.