diff --git a/src/renderer/features/flexible-multisig-create/components/MultisigFees.tsx b/src/renderer/features/flexible-multisig-create/components/MultisigFees.tsx index 4b145bec9..d53c5d4d4 100644 --- a/src/renderer/features/flexible-multisig-create/components/MultisigFees.tsx +++ b/src/renderer/features/flexible-multisig-create/components/MultisigFees.tsx @@ -1,4 +1,3 @@ -import { BN } from '@polkadot/util'; import { useUnit } from 'effector-react'; import { memo } from 'react'; @@ -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 ( diff --git a/src/renderer/features/flexible-multisig-create/model/flexible-multisig-create.ts b/src/renderer/features/flexible-multisig-create/model/flexible-multisig-create.ts index 7f9c1ea5e..1607fd20e 100644 --- a/src/renderer/features/flexible-multisig-create/model/flexible-multisig-create.ts +++ b/src/renderer/features/flexible-multisig-create/model/flexible-multisig-create.ts @@ -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, @@ -24,7 +26,6 @@ import { SS58_DEFAULT_PREFIX, Step, TEST_ACCOUNTS, - ZERO_BALANCE, isStep, nonNullable, toAccountId, @@ -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(null).reset(flowFinished); const $coreTx = createStore(null).reset(flowFinished); @@ -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; } @@ -132,7 +133,7 @@ const $transaction = combine( signatories: signatoriesWrapped, multisigAccountId, threshold: form.threshold, - proxyDeposit, + proxyDeposit: proxyDeposit.toString(), }); }, ); @@ -181,13 +182,23 @@ 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 getProxyDepositFx = createEffect(async ({ api, asset }: GetDepositParams): Promise => { + 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, + source: formModel.$chain, + filter: (chain, api) => nonNullable(api) && nonNullable(chain) && nonNullable(chain.assets?.[0]), + fn: (chain, api) => ({ api: api!, asset: chain!.assets[0] }), target: getProxyDepositFx, }); diff --git a/src/renderer/shared/api/balances/service/balanceService.ts b/src/renderer/shared/api/balances/service/balanceService.ts index b248d8538..fd408f1cb 100644 --- a/src/renderer/shared/api/balances/service/balanceService.ts +++ b/src/renderer/shared/api/balances/service/balanceService.ts @@ -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'; @@ -25,6 +25,7 @@ type NoIdBalance = Omit; export const balanceService = { subscribeBalances, subscribeLockBalances, + getExistentialDeposit, }; /** @@ -308,3 +309,17 @@ function subscribeLockOrmlAssetChange( callback(newLocks); }); } + +async function getExistentialDeposit(api: ApiPromise, asset: Asset): Promise { + 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); + } + } +}