From 6b74ea39c2bd831b09ac96cd16bc11836608daf0 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 19 Nov 2019 16:35:28 +0100 Subject: [PATCH] Adjust available calc (only vested, when free - reserved > vested) (#1588) --- packages/api-derive/src/balances/all.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/api-derive/src/balances/all.ts b/packages/api-derive/src/balances/all.ts index 583784e5de..fcc67187ec 100644 --- a/packages/api-derive/src/balances/all.ts +++ b/packages/api-derive/src/balances/all.ts @@ -38,9 +38,15 @@ function calcBalances ([accountId, bestNumber, [freeBalance, reservedBalance, lo const vestedBalance = createType('Balance', perBlock.mul(bestNumber)); const isVesting = vestedBalance.lt(vestingTotal); - // see what is availble with the current vested amount taken into account - const freeWithVesting = freeBalance.sub(vestingTotal).add(vestedBalance); - const availableBalance = createType('Balance', bnMax(new BN(0), (isVesting ? freeWithVesting : freeBalance).sub(lockedBalance))); + // The available balance & vested has an interplay here + // " + // vesting is a guarantee that the account's balance will never go below a certain amount. so it functions in the opposite way, a bit like a lock that is monotonically decreasing rather than a liquid amount that is monotonically increasing. + // locks function as the same guarantee - that a balance will not be lower than a particular amount. + // because of this you can see that if there is a "vesting lock" that guarantees the balance cannot go below 200, and a "staking lock" that guarantees the balance cannot drop below 300, then we just have two guarantees of which the first is irrelevant. + // i.e. (balance >= 200 && balance >= 300) == (balance >= 300) + // "" + const floating = freeBalance.sub(lockedBalance); + const availableBalance = createType('Balance', bnMax(new BN(0), isVesting && floating.gt(vestedBalance) ? vestedBalance : floating)); return { accountId,