From 5f43da06230acb9d528518a5fbd5976280a8102a Mon Sep 17 00:00:00 2001 From: Thibaut Sardan <33178835+Tbaut@users.noreply.github.com> Date: Fri, 7 Jun 2019 12:14:14 +0200 Subject: [PATCH] Add rewardDestination to derive.staking.info (#977) * rewardDestination in staking.info * squash a couple lines together to please CodeClimate --- packages/api-derive/src/staking/info.ts | 22 +++++----- packages/api-derive/src/types.ts | 3 +- packages/api-derive/test/e2e/promise.spec.ts | 46 ++++++++++++++------ 3 files changed, 45 insertions(+), 26 deletions(-) diff --git a/packages/api-derive/src/staking/info.ts b/packages/api-derive/src/staking/info.ts index ce3a08a1ec..23ffd77b18 100644 --- a/packages/api-derive/src/staking/info.ts +++ b/packages/api-derive/src/staking/info.ts @@ -3,7 +3,7 @@ // of the Apache-2.0 license. See the LICENSE file for details. import { ApiInterface$Rx } from '@polkadot/api/types'; -import { AccountId, BlockNumber, Exposure, Option, StakingLedger,StructAny, ValidatorPrefs, UnlockChunk } from '@polkadot/types'; +import { AccountId, BlockNumber, Exposure, Option, RewardDestination, StakingLedger, StructAny, ValidatorPrefs, UnlockChunk } from '@polkadot/types'; import { DerivedStaking, DerivedUnlocking } from '../types'; import BN from 'bn.js'; @@ -80,22 +80,22 @@ function withStashController (api: ApiInterface$Rx, accountId: AccountId, contro [api.query.session.nextKeyFor, controllerId], [api.query.staking.ledger, controllerId], [api.query.staking.nominators, stashId], + [api.query.staking.payee, stashId], [api.query.staking.stakers, stashId], [api.query.staking.validators, stashId] ]) - ]) as any as Observable<[BN, BlockNumber, [Option, Option, [Array], Exposure, [ValidatorPrefs]]]> + ]) as any as Observable<[BN, BlockNumber, [Option, Option, [Array], RewardDestination, Exposure, [ValidatorPrefs]]]> ).pipe( - map(([eraLength, bestNumber,[nextKeyFor, _stakingLedger, [nominators], stakers, [validatorPrefs]]]) => { + map(([eraLength, bestNumber,[nextKeyFor, _stakingLedger, [nominators], rewardDestination, stakers, [validatorPrefs]]]) => { const stakingLedger = _stakingLedger.isSome ? _stakingLedger.unwrap() : undefined; return new StructAny({ accountId, controllerId, - nextSessionId: nextKeyFor.isSome - ? nextKeyFor.unwrap() - : undefined, + nextSessionId: nextKeyFor.unwrapOr(null) || undefined, nominators, redeemable: redeemableSum(stakingLedger, eraLength, bestNumber), + rewardDestination, stakers, stakingLedger, stashId, @@ -115,18 +115,18 @@ function withControllerLedger (api: ApiInterface$Rx, accountId: AccountId, staki api.queryMulti([ [api.query.session.nextKeyFor, controllerId], [api.query.staking.nominators, stashId], + [api.query.staking.payee, stashId], [api.query.staking.stakers, stashId], [api.query.staking.validators, stashId] - ]) as any as Observable<[Option, [Array], Exposure, [ValidatorPrefs]]> + ]) as any as Observable<[Option, [Array], RewardDestination, Exposure, [ValidatorPrefs]]> ).pipe( - map(([nextKeyFor, [nominators], stakers, [validatorPrefs]]) => + map(([nextKeyFor, [nominators], rewardDestination, stakers, [validatorPrefs]]) => new StructAny({ accountId, controllerId, - nextSessionId: nextKeyFor.isSome - ? nextKeyFor.unwrap() - : undefined, + nextSessionId: nextKeyFor.unwrapOr(null) || undefined, nominators, + rewardDestination, stakers, stakingLedger, stashId, diff --git a/packages/api-derive/src/types.ts b/packages/api-derive/src/types.ts index f226f65089..77bcc47e23 100644 --- a/packages/api-derive/src/types.ts +++ b/packages/api-derive/src/types.ts @@ -3,7 +3,7 @@ // of the Apache-2.0 license. See the LICENSE file for details. import BN from 'bn.js'; -import { AccountId, Balance, BlockNumber, Exposure, Index, StakingLedger, StructAny, ValidatorPrefs, Vote } from '@polkadot/types'; +import { AccountId, Balance, BlockNumber, Exposure, Index, RewardDestination, StakingLedger, StructAny, ValidatorPrefs, Vote } from '@polkadot/types'; export interface DerivedBalances extends StructAny { accountId: AccountId; @@ -64,6 +64,7 @@ export interface DerivedStaking extends StructAny { nextSessionId?: AccountId; nominators?: Array; redeemable?: BN; + rewardDestination?: RewardDestination; stakers?: Exposure; stakingLedger?: StakingLedger; stashId?: AccountId; diff --git a/packages/api-derive/test/e2e/promise.spec.ts b/packages/api-derive/test/e2e/promise.spec.ts index 0ef46dbee8..7da3a0fa69 100644 --- a/packages/api-derive/test/e2e/promise.spec.ts +++ b/packages/api-derive/test/e2e/promise.spec.ts @@ -120,24 +120,12 @@ describe.skip('derive e2e', () => { }); 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) => { @@ -149,8 +137,38 @@ describe.skip('derive e2e', () => { }); it('verifies that derive.staking.unlocking isn\'t empty/undefined', () => { - return api.derive.session.info(ALICE_STASH, (info: DerivedStaking) => { - expect(info.unlocking).toBeGreaterThan(0); + return api.derive.staking.info(ALICE_STASH, (info: DerivedStaking) => { + expect(info.unlocking).toBeDefined(); + }); + }); + }); + + describe('verifies derive.staking.rewardDestination',() => { + const PAYEE = 2; + const ALICE_STASH = '5GNJqTPyNqANBkUVMN1LPPrxXnFouWXoe2wNSmmEoLctxiZY'; + const ALICE = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY'; + const keyring = testKeyring(); + const alicePair = keyring.getPair(ALICE); + + it('Set payee for ALICE to 2', (done) => { + return api.tx.staking.setPayee(PAYEE) + .signAndSend(alicePair, (result: SubmittableResult) => { + if (result.status.isFinalized) { + + done(); + } + }); + }); + + it('verifies payee for ALICE_STASH', (done) => { + return api.derive.staking.info(ALICE_STASH, (info: DerivedStaking) => { + if (!info.rewardDestination) { + return done.fail(new Error('rewardDestination is undefined.')); + } else { + expect(info.rewardDestination.toString()).toBe('Controller'); + } + + done(); }); }); });