Move additional classes to definition-only (#1165)

* Move additional classes to definition-only

* Move some additional quick-hit types

* Last of the easier type/* conversions

* Remove rhd files

* Remove commented line

* Add injector to types loading

* typo

* Update comment

* Merge master

* Rework & organize CHANGELOG

* Some small helper cleanups
This commit is contained in:
Jaco Greeff
2019-07-25 10:32:26 +02:00
committed by GitHub
parent 9ef7ee4a67
commit a25110b281
107 changed files with 682 additions and 1558 deletions
+5 -3
View File
@@ -2,11 +2,13 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { BalanceLock, VestingSchedule } from '@polkadot/types/srml/balances/types';
import BN from 'bn.js';
import { combineLatest, of, Observable } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { AccountId, AccountIndex, Address, Balance, BalanceLock, BlockNumber, Index, Option, VestingSchedule } from '@polkadot/types';
import { AccountId, AccountIndex, Address, Balance, BlockNumber, ClassOf, Index, Option, createType } from '@polkadot/types';
import { bnMax } from '@polkadot/util';
import { idAndIndex } from '../accounts/idAndIndex';
@@ -19,7 +21,7 @@ type Result = [AccountId | undefined, BlockNumber | undefined, [Balance?, Balanc
const EMPTY_ACCOUNT = new AccountId();
const ZERO = new Balance(0);
function calcBalances ([accountId = EMPTY_ACCOUNT, bestNumber = ZERO, [freeBalance = ZERO, reservedBalance = ZERO, locks = [], vesting = new Option<VestingSchedule>(VestingSchedule, null), accountNonce = ZERO]]: Result): DerivedBalances {
function calcBalances ([accountId = EMPTY_ACCOUNT, bestNumber = ZERO, [freeBalance = ZERO, reservedBalance = ZERO, locks = [], vesting = new Option<VestingSchedule>(ClassOf<VestingSchedule>('VestingSchedule'), null), accountNonce = ZERO]]: Result): DerivedBalances {
let lockedBalance = ZERO;
if (Array.isArray(locks)) {
@@ -32,7 +34,7 @@ function calcBalances ([accountId = EMPTY_ACCOUNT, bestNumber = ZERO, [freeBalan
}
// offset = balance locked at genesis, perBlock is the unlock amount
const { offset, perBlock } = vesting.unwrapOr(new VestingSchedule());
const { offset, perBlock } = vesting.unwrapOr(createType<VestingSchedule>('VestingSchedule'));
const vestedNow: BN = perBlock.mul(bestNumber);
const vestedBalance: BN = vestedNow.gt(offset)
? freeBalance
@@ -2,11 +2,13 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ReferendumInfo } from '@polkadot/types/srml/democracy/types';
import BN from 'bn.js';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { Option, ReferendumInfo } from '@polkadot/types';
import { Option } from '@polkadot/types';
import { isNull } from '@polkadot/util';
import { ReferendumInfoExtended } from '../type';
@@ -2,11 +2,13 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ReferendumInfo } from '@polkadot/types/srml/democracy/types';
import BN from 'bn.js';
import { Observable, of } from 'rxjs';
import { map } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { Option, ReferendumInfo, Vector } from '@polkadot/types';
import { Option, Vector } from '@polkadot/types';
import { ReferendumInfoExtended } from '../type';
import { drr } from '../util/drr';
@@ -2,11 +2,13 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ReferendumIndex } from '@polkadot/types/srml/democracy/types';
import BN from 'bn.js';
import { Observable, of } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { Option, ReferendumIndex } from '@polkadot/types';
import { Option } from '@polkadot/types';
import { ReferendumInfoExtended } from '../type';
import { drr } from '../util/drr';
@@ -2,11 +2,11 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { SetIndex } from '@polkadot/types/srml/elections/types';
import { ApprovalFlag, SetIndex } from '@polkadot/types/srml/elections/types';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { Observable } from 'rxjs';
import { AccountId, Vector, ApprovalFlag } from '@polkadot/types';
import { AccountId, Vector } from '@polkadot/types';
import { switchMap, map } from 'rxjs/operators';
import { approvalFlagsToBools } from '../util/approvalFlagsToBools';
import { drr } from '../util/drr';
@@ -2,11 +2,11 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { SetIndex } from '@polkadot/types/srml/elections/types';
import { ApprovalFlag, SetIndex } from '@polkadot/types/srml/elections/types';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { Observable } from 'rxjs';
import { AccountId, Vector, ApprovalFlag } from '@polkadot/types';
import { AccountId, Vector } from '@polkadot/types';
import { map } from 'rxjs/operators';
import { approvalFlagsToBools } from '../util/approvalFlagsToBools';
import { drr } from '../util/drr';
+2 -2
View File
@@ -2,13 +2,13 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { SetIndex } from '@polkadot/types/srml/elections/types';
import { SetIndex, VoteIndex } from '@polkadot/types/srml/elections/types';
import BN from 'bn.js';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { AccountId, BlockNumber, VoteIndex } from '@polkadot/types';
import { AccountId, BlockNumber } from '@polkadot/types';
import { DerivedElectionsInfo } from '../types';
import { drr } from '../util/drr';
+6 -5
View File
@@ -2,7 +2,8 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { SessionKey, Keys } from '@polkadot/types/srml/session/types';
import { Keys } from '@polkadot/types/srml/session/types';
import { Exposure, RewardDestination, ValidatorPrefs } from '@polkadot/types/srml/staking/types';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { DerivedStaking, DerivedUnlocking } from '../types';
@@ -10,7 +11,7 @@ import { DerivedStaking, DerivedUnlocking } from '../types';
import BN from 'bn.js';
import { combineLatest, Observable, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { AccountId, BlockNumber, Exposure, Option, RewardDestination, StakingLedger, Tuple, ValidatorPrefs, Vector, UnlockChunk } from '@polkadot/types';
import { AccountId, BlockNumber, Option, StakingLedger, Tuple, Vector, UnlockChunk } from '@polkadot/types';
import { isUndefined } from '@polkadot/util';
@@ -74,7 +75,7 @@ function redeemableSum (stakingLedger: StakingLedger | undefined, eraLength: BN,
}, new BN(0));
}
function unwrapSessionIds (stashId: AccountId, validatorIds: AccountId[], auraIds: AccountId[], nextKeys: Option<SessionKey> | Vector<Tuple>): { nextSessionId?: AccountId; sessionId?: AccountId } {
function unwrapSessionIds (stashId: AccountId, validatorIds: AccountId[], auraIds: AccountId[], nextKeys: Option<AccountId> | Vector<Tuple>): { nextSessionId?: AccountId; sessionId?: AccountId } {
// for 2.x we have a Vec<(ValidatorId,Keys)> of the keys
if (Array.isArray(nextKeys)) {
const validatorIdx = validatorIds.indexOf(stashId);
@@ -124,7 +125,7 @@ function withStashController (api: ApiInterfaceRx, accountId: AccountId, control
[api.query.staking.stakers, stashId],
[api.query.staking.validators, stashId]
])
]) as Observable<[BN, BlockNumber, AccountId[], [Option<SessionKey> | Vector<Tuple>, AccountId[], Option<StakingLedger>, [AccountId[]], RewardDestination, Exposure, [ValidatorPrefs]]]>
]) as Observable<[BN, BlockNumber, AccountId[], [Option<AccountId> | Vector<Tuple>, AccountId[], Option<StakingLedger>, [AccountId[]], RewardDestination, Exposure, [ValidatorPrefs]]]>
).pipe(
map(([eraLength, bestNumber, auraIds, [nextKeys, validatorIds, _stakingLedger, [nominators], rewardDestination, stakers, [validatorPrefs]]]): DerivedStaking => {
const stakingLedger = _stakingLedger.unwrapOr(null) || undefined;
@@ -171,7 +172,7 @@ function withControllerLedger (api: ApiInterfaceRx, accountId: AccountId, stakin
[api.query.staking.stakers, stashId],
[api.query.staking.validators, stashId]
])
]) as Observable<[AccountId[], [Option<SessionKey> | Vector<Tuple>, AccountId[], [AccountId[]], RewardDestination, Exposure, [ValidatorPrefs]]]>
]) as Observable<[AccountId[], [Option<AccountId> | Vector<Tuple>, AccountId[], [AccountId[]], RewardDestination, Exposure, [ValidatorPrefs]]]>
).pipe(
map(([auraIds, [nextKeys, validatorIds, [nominators], rewardDestination, stakers, [validatorPrefs]]]): DerivedStaking => {
const { nextSessionId, sessionId } = unwrapSessionIds(stashId, validatorIds, auraIds, nextKeys);
@@ -2,8 +2,10 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ReferendumIndex, ReferendumInfo } from '@polkadot/types/srml/democracy/types';
import BN from 'bn.js';
import { ReferendumInfo, ReferendumIndex } from '@polkadot/types';
import { ClassOf, createType } from '@polkadot/types';
import { AnyJsonObject } from '@polkadot/types/types';
/**
@@ -11,7 +13,7 @@ import { AnyJsonObject } from '@polkadot/types/types';
* @description
* A [[ReferendumInfo]] with an additional `index` field
*/
export default class ReferendumInfoExtended extends ReferendumInfo {
export default class ReferendumInfoExtended extends ClassOf<ReferendumInfo>('ReferendumInfo') {
private _index: ReferendumIndex;
public constructor (value: ReferendumInfo | ReferendumInfoExtended, index?: BN | number) {
@@ -19,7 +21,7 @@ export default class ReferendumInfoExtended extends ReferendumInfo {
this._index = value instanceof ReferendumInfoExtended
? value.index
: new ReferendumIndex(index);
: createType<ReferendumIndex>('ReferendumIndex', index);
}
/**
+3 -2
View File
@@ -2,10 +2,11 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { SetIndex } from '@polkadot/types/srml/elections/types';
import { SetIndex, VoteIndex } from '@polkadot/types/srml/elections/types';
import { Exposure, RewardDestination, ValidatorPrefs } from '@polkadot/types/srml/staking/types';
import BN from 'bn.js';
import { AccountId, Balance, BlockNumber, Exposure, Index, RewardDestination, StakingLedger, ValidatorPrefs, Vote, VoteIndex } from '@polkadot/types';
import { AccountId, Balance, BlockNumber, Index, StakingLedger, Vote } from '@polkadot/types';
export interface DerivedBalances {
accountId: AccountId;
@@ -2,8 +2,10 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ApprovalFlag } from '@polkadot/types/srml/elections/types';
import BN from 'bn.js';
import { Vector, ApprovalFlag } from '@polkadot/types';
import { Vector } from '@polkadot/types';
export function approvalFlagToBool (flags: Vector<ApprovalFlag>): boolean[] {
const bools: boolean[] = [];
@@ -12,6 +14,8 @@ export function approvalFlagToBool (flags: Vector<ApprovalFlag>): boolean[] {
bools.push(!flag.toBn().uand((new BN(1)).shln(bit)).isZero());
}
}
const lastApproval: number = bools.lastIndexOf(true);
return lastApproval >= 0 ? bools.slice(0, lastApproval + 1) : [];
}
@@ -2,15 +2,20 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Vector, ApprovalFlag } from '@polkadot/types';
import { ApprovalFlag } from '@polkadot/types/srml/elections/types';
import { Vector } from '@polkadot/types';
export function approvalFlagsToBools (flags: Vector<ApprovalFlag>): boolean[] {
const bools: boolean[] = [];
flags.forEach((flag: ApprovalFlag): void => {
const str = flag.toString(2);
for (const bit of str) {
bools.push(!!parseInt(bit, 10));
}
});
return bools;
}