Redeemable and Unlocking in staking.info (#959)

* init

* init2

* redeemable

* unlocking

* address some comments

* remove map and exit early

* add test for unlocking

* lint

* comment and lint

* use SubmittableResult

* swap _stakingLedger and stakingLedger
This commit is contained in:
Thibaut Sardan
2019-06-12 16:45:45 +12:00
committed by Ian He
parent 249c120e42
commit 018c63b641
3 changed files with 129 additions and 15 deletions
+77 -12
View File
@@ -2,42 +2,107 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { DerivedStaking } from '../types';
import { Observable, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { ApiInterface$Rx } from '@plugnet/api/types';
import { AccountId, Exposure, Option, StakingLedger, StructAny, ValidatorPrefs } from '@plugnet/types';
import { AccountId, BlockNumber, Exposure, Option, StakingLedger,StructAny, ValidatorPrefs, UnlockChunk } from '@plugnet/types';
import { DerivedStaking, DerivedUnlocking } from '../types';
import BN from 'bn.js';
import { combineLatest, Observable, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { bestNumber } from '../chain/bestNumber';
import { drr } from '../util/drr';
import { eraLength } from '../session/eraLength';
import { isUndefined } from '@plugnet/util';
function calculateUnlocking (stakingLedger: StakingLedger | undefined, eraLength: BN, bestNumber: BlockNumber): DerivedUnlocking | undefined {
if (isUndefined(stakingLedger)) {
return undefined;
}
// select the Unlockchunks that can't be redeemed yet.
const unlockingChunks = stakingLedger.unlocking.filter((chunk) => remainingBlocks(chunk.era, eraLength, bestNumber).gtn(0));
if (!unlockingChunks.length) {
return undefined;
}
// group the Unlockchunks that have the same era and sum their values
const groupedResult = groupByEra(unlockingChunks);
const results = Object.entries(groupedResult).map(([eraString, value]) => ({
value,
remainingBlocks: remainingBlocks(new BlockNumber(eraString), eraLength, bestNumber)
}));
return results.length ? results : undefined;
}
function groupByEra (list: UnlockChunk[]) {
return list.reduce((map, { era, value }) => {
const key = era.toString();
if (!map[key]) {
map[key] = value;
} else {
map[key] = map[key].add(value);
}
return map;
}, {} as { [index: string]: BN });
}
function redeemableSum (stakingLedger: StakingLedger | undefined, eraLength: BN, bestNumber: BlockNumber) {
if (isUndefined(stakingLedger)) {
return new BN(0);
}
return stakingLedger.unlocking
.filter((chunk) => remainingBlocks(chunk.era, eraLength, bestNumber).eqn(0))
.reduce((curr, prev) => {
return curr.add(prev.value);
}, new BN(0));
}
function remainingBlocks (era: BN, eraLength: BN, bestNumber: BlockNumber) {
const remaining = eraLength.mul(era).sub(bestNumber);
return remaining.lten(0) ? new BN(0) : remaining;
}
function withStashController (api: ApiInterface$Rx, accountId: AccountId, controllerId: AccountId): Observable<DerivedStaking> {
const stashId = accountId;
return (
combineLatest([
eraLength(api)(),
bestNumber(api)(),
api.queryMulti([
[api.query.session.nextKeyFor, controllerId],
[api.query.staking.ledger, controllerId],
[api.query.staking.nominators, stashId],
[api.query.staking.stakers, stashId],
[api.query.staking.validators, stashId]
]) as any as Observable<[Option<AccountId>, Option<StakingLedger>, [Array<AccountId>], Exposure, [ValidatorPrefs]]>
])
]) as any as Observable<[BN, BlockNumber, [Option<AccountId>, Option<StakingLedger>, [Array<AccountId>], Exposure, [ValidatorPrefs]]]>
).pipe(
map(([nextKeyFor, stakingLedger, [nominators], stakers, [validatorPrefs]]) =>
new StructAny({
map(([eraLength, bestNumber,[nextKeyFor, _stakingLedger, [nominators], stakers, [validatorPrefs]]]) => {
const stakingLedger = _stakingLedger.isSome ? _stakingLedger.unwrap() : undefined;
return new StructAny({
accountId,
controllerId,
nextSessionId: nextKeyFor.isSome
? nextKeyFor.unwrap()
: undefined,
nominators,
redeemable: redeemableSum(stakingLedger, eraLength, bestNumber),
stakers,
stakingLedger: stakingLedger.isSome
? stakingLedger.unwrap()
: undefined,
stakingLedger,
stashId,
unlocking: calculateUnlocking(stakingLedger, eraLength, bestNumber),
validatorPrefs
}) as DerivedStaking),
}) as DerivedStaking;
}),
drr()
);
}
+4
View File
@@ -63,8 +63,12 @@ export interface DerivedStaking extends StructAny {
controllerId?: AccountId;
nextSessionId?: AccountId;
nominators?: Array<AccountId>;
redeemable?: BN;
stakers?: Exposure;
stakingLedger?: StakingLedger;
stashId?: AccountId;
unlocking?: DerivedUnlocking;
validatorPrefs?: ValidatorPrefs;
}
export type DerivedUnlocking = Array<{remainingBlocks: BN, value: BN}>;
+48 -3
View File
@@ -2,8 +2,12 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import ApiPromise from '@plugnet/api/promise/Api';
import { BlockNumber } from '@plugnet/types';
import { DerivedStaking } from '../../src/types';
import { SubmittableResult } from '../../../api/src';
import ApiPromise from '@plugnet/api/promise/Api';
import testKeyring from '@plugnet/keyring/testing';
import { WsProvider } from '@plugnet/rpc-provider';
const WS = 'ws://127.0.0.1:9944/';
@@ -22,7 +26,7 @@ describe.skip('derive e2e', () => {
});
it('returns correct results', async () => {
// https://github.com/plugblockchain/api.js/issues/777
// https://github.com/polkadot-js/api/issues/777
const block1 = await api.derive.chain.bestNumber();
await new Promise((resolve) => setTimeout(resolve, 15000));
const block2 = await api.derive.chain.bestNumber();
@@ -89,9 +93,13 @@ describe.skip('derive e2e', () => {
it('retrieves all staking info (for stash)', (done) => {
const accountId = '5GNJqTPyNqANBkUVMN1LPPrxXnFouWXoe2wNSmmEoLctxiZY';
return api.derive.staking.info(accountId, (info) => {
return api.derive.staking.info(accountId, (info: DerivedStaking) => {
console.error(JSON.stringify(info));
if (!info.stashId || !info.controllerId || !info.stakingLedger) {
return done.fail(new Error('At leat one of info.stashId, info.controllerId or info.stakingLedger is undefined.'));
}
expect(info.accountId.eq(accountId)).toBe(true);
expect(info.controllerId.eq('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY')).toBe(true);
expect(info.stashId.eq(accountId)).toBe(true);
@@ -100,4 +108,41 @@ describe.skip('derive e2e', () => {
done();
});
});
describe('verifies derive.staking.unlocking',() => {
const BOND_VALUE = 10;
const UNBOND_VALUE = 1;
const ALICE_STASH = '5GNJqTPyNqANBkUVMN1LPPrxXnFouWXoe2wNSmmEoLctxiZY';
const ALICE = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY';
const keyring = testKeyring();
const aliceStashPair = keyring.getPair(ALICE_STASH);
const alicePair = keyring.getPair(ALICE);
it('bondsExtra funds for Alice Stash', (done) => {
return api.tx.staking.bondExtra(BOND_VALUE)
.signAndSend(aliceStashPair, (result: SubmittableResult) => {
if (result.status.isFinalized) {
done();
}
});
});
it('unbonds dots for Alice (from Alice Stash)', (done) => {
return api.tx.staking.unbond(UNBOND_VALUE)
.signAndSend(alicePair, (result: SubmittableResult) => {
if (result.status.isFinalized) {
done();
}
});
});
it('verifies that derive.staking.unlocking isn\'t empty/undefined', () => {
return api.derive.session.info(ALICE_STASH, (info: DerivedStaking) => {
expect(info.unlocking).toBeGreaterThan(0);
});
});
});
});