Skip to content

Commit 92dfe00

Browse files
authored
Revise api.derive.balances.all to include ED when necessary (#5957)
1 parent f82c572 commit 92dfe00

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

packages/api-derive/src/balances/all.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,16 @@ function calcShared (api: DeriveApi, bestNumber: BlockNumber, data: DeriveBalanc
5858

5959
if (data.frameSystemAccountInfo?.frozen) {
6060
const { frameSystemAccountInfo, freeBalance, reservedBalance } = data;
61+
const noFrozenReserved = frameSystemAccountInfo.frozen.isZero() && reservedBalance.isZero();
62+
const ED = api.consts.balances.existentialDeposit;
63+
const maybeED = noFrozenReserved ? new BN(0) : ED;
64+
const frozenReserveDif = frameSystemAccountInfo.frozen.sub(reservedBalance);
6165

6266
transferable = api.registry.createType(
6367
'Balance',
6468
allLocked
6569
? 0
66-
: freeBalance.sub(bnMax(new BN(0), frameSystemAccountInfo.frozen.sub(reservedBalance)))
70+
: freeBalance.sub(bnMax(maybeED, frozenReserveDif))
6771
);
6872
}
6973

packages/api-derive/src/balances/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ export interface DeriveBalancesAllAccountData extends DeriveBalancesAccountData
4141
*/
4242
lockedBreakdown: (PalletBalancesBalanceLock | BalanceLockTo212)[];
4343
/**
44-
* Calculated transferable balance. This uses the formula: free - max(0, frozen - reserve)
44+
* Calculated transferable balance. This uses the formula: free - max(maybeEd, frozen - reserve)
45+
* Where `maybeEd` means if there is no frozen and reserves it will be zero, else it will be the existential deposit.
4546
* This is only correct when the return type of `api.query.system.account` is `FrameSystemAccountInfo`.
4647
* Which is the most up to date calulcation for transferrable balances.
4748
*

0 commit comments

Comments
 (0)