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 existential deposit #2706

Merged
merged 6 commits into from
Nov 21, 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
@@ -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,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<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,
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,
});

Expand Down
17 changes: 16 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,17 @@ 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);
}
}
}
Loading