From 435319a633c0689a3220ce23571f8e6442e239a9 Mon Sep 17 00:00:00 2001 From: tarikgul Date: Mon, 19 Aug 2024 13:01:23 -0400 Subject: [PATCH] Update formula --- packages/api-derive/src/balances/all.ts | 6 +++++- packages/api-derive/src/balances/types.ts | 3 ++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/api-derive/src/balances/all.ts b/packages/api-derive/src/balances/all.ts index a9224155ceac..a2fe133fb84c 100644 --- a/packages/api-derive/src/balances/all.ts +++ b/packages/api-derive/src/balances/all.ts @@ -58,12 +58,16 @@ function calcShared (api: DeriveApi, bestNumber: BlockNumber, data: DeriveBalanc if (data.frameSystemAccountInfo?.frozen) { const { frameSystemAccountInfo, freeBalance, reservedBalance } = data; + const noFrozenReserved = frameSystemAccountInfo.frozen.isZero() && reservedBalance.isZero(); + const ED = api.consts.balances.existentialDeposit; + const maybeED = noFrozenReserved ? new BN(0) : ED; + const frozenReserveDif = frameSystemAccountInfo.frozen.sub(reservedBalance); transferable = api.registry.createType( 'Balance', allLocked ? 0 - : freeBalance.sub(bnMax(new BN(0), frameSystemAccountInfo.frozen.sub(reservedBalance))) + : freeBalance.sub(bnMax(maybeED, frozenReserveDif)) ); } diff --git a/packages/api-derive/src/balances/types.ts b/packages/api-derive/src/balances/types.ts index 94605cb64872..612ace6d2a18 100644 --- a/packages/api-derive/src/balances/types.ts +++ b/packages/api-derive/src/balances/types.ts @@ -41,7 +41,8 @@ export interface DeriveBalancesAllAccountData extends DeriveBalancesAccountData */ lockedBreakdown: (PalletBalancesBalanceLock | BalanceLockTo212)[]; /** - * Calculated transferable balance. This uses the formula: free - max(0, frozen - reserve) + * Calculated transferable balance. This uses the formula: free - max(maybeEd, frozen - reserve) + * Where `maybeEd` means if there is no frozen and reserves it will be zero, else it will be the existential deposit. * This is only correct when the return type of `api.query.system.account` is `FrameSystemAccountInfo`. * Which is the most up to date calulcation for transferrable balances. *