Align redeemable check with Substrate (exact) (#5243)

* Align redeemable check with Substrate (exact)

* Fix comment
This commit is contained in:
Jaco
2022-09-27 15:09:45 +03:00
committed by GitHub
parent 7b0fd81b4e
commit 2d09e8d94e
+5 -6
View File
@@ -48,12 +48,11 @@ function calculateUnlocking (api: DeriveApi, stakingLedger: PalletStakingStaking
function redeemableSum (api: DeriveApi, stakingLedger: PalletStakingStakingLedger | undefined, sessionInfo: DeriveSessionInfo): Balance {
return api.registry.createType('Balance', (stakingLedger?.unlocking || [] as PalletStakingUnlockChunk[]).reduce((total, { era, value }): BN => {
// aligns with https://github.com/paritytech/substrate/blob/fdfdc73f9e64dc47934b72eb9af3e1989e4ba699/frame/staking/src/pallet/mod.rs#L973-L975
// (ensure era > currentEra passed, as per https://github.com/paritytech/substrate/blob/fdfdc73f9e64dc47934b72eb9af3e1989e4ba699/frame/staking/src/lib.rs#L477-L494)
// NOTE: Previously we used era >= active_era, while the outcome doesn't change using era > current_era (active_era
// being one less), this aligns correctly with the Rust source
return sessionInfo.currentEra.gt(era.unwrap())
? total.iadd(value.unwrap())
: total;
// (ensure currentEra >= era passed, as per https://github.com/paritytech/substrate/blob/fdfdc73f9e64dc47934b72eb9af3e1989e4ba699/frame/staking/src/lib.rs#L477-L494)
// NOTE: Previously we used activeEra >= era, which is incorrect for the last session
return era.unwrap().gt(sessionInfo.currentEra)
? total
: total.iadd(value.unwrap());
}, new BN(0)));
}