From 9ffeda1749b9b44c2db815e01100b503441f17fb Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Mon, 2 Sep 2019 17:58:36 +0200 Subject: [PATCH] Fix redeemables (#1358) --- packages/api-derive/src/staking/info.ts | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/packages/api-derive/src/staking/info.ts b/packages/api-derive/src/staking/info.ts index eadcc11bab..adf43f3184 100644 --- a/packages/api-derive/src/staking/info.ts +++ b/packages/api-derive/src/staking/info.ts @@ -48,19 +48,14 @@ function remainingBlocks (era: BN, eraLength: BN, bestNumber: BlockNumber): BN { : remaining; } -// select the Unlockchunks that can't be redeemed yet -function calcChunks (stakingLedger: StakingLedger, eraLength: BN, bestNumber: BlockNumber): UnlockChunk[] { - return stakingLedger.unlocking.filter((chunk): boolean => - remainingBlocks(chunk.era.unwrap(), eraLength, bestNumber).gtn(0) - ); -} - function calculateUnlocking (stakingLedger: StakingLedger | undefined, eraLength: BN, bestNumber: BlockNumber): DerivedUnlocking | undefined { if (isUndefined(stakingLedger)) { return undefined; } - const unlockingChunks = calcChunks(stakingLedger, eraLength, bestNumber); + const unlockingChunks = stakingLedger.unlocking.filter(({ era }): boolean => + remainingBlocks(era.unwrap(), eraLength, bestNumber).gtn(0) + ); if (!unlockingChunks.length) { return undefined; @@ -81,9 +76,11 @@ function redeemableSum (stakingLedger: StakingLedger | undefined, eraLength: BN, return new BN(0); } - return calcChunks(stakingLedger, eraLength, bestNumber).reduce((curr, prev): BN => - curr.add(prev.value.unwrap()), new BN(0) - ); + return stakingLedger.unlocking.reduce((total, { era, value }): BN => { + return remainingBlocks(era.unwrap(), eraLength, bestNumber).eqn(0) + ? total.add(value.unwrap()) + : total; + }, new BN(0)); } function unwrapSessionIds (stashId: AccountId, queuedKeys: Option | Vec<[AccountId, Keys] & Codec>, nextKeys: Option): { nextSessionIds: AccountId[]; nextSessionId?: AccountId; sessionIds: AccountId[]; sessionId?: AccountId } {