CreateType is typed, add createTypeUnsafe just in case (#1199)

* Use unsafe internally

* Add comments

* Remove <> in createType

* Fix lint

* Fix classOf

* Remove createType from Text

* Fix lint

* Fix Fallback type

* Fix fallback in createType
This commit is contained in:
Amaury Martiny
2019-07-26 14:00:25 +02:00
committed by Jaco Greeff
parent ec0b9a84a3
commit 441148889d
24 changed files with 112 additions and 104 deletions
+3 -3
View File
@@ -20,9 +20,9 @@ import { drr } from '../util/drr';
type Result = [AccountId | undefined, BlockNumber | undefined, [Balance?, Balance?, BalanceLock[]?, Option<VestingSchedule>?, Index?]];
const EMPTY_ACCOUNT = new AccountId();
const ZERO = createType<Balance>('Balance', 0);
const ZERO = createType('Balance', 0);
function calcBalances ([accountId = EMPTY_ACCOUNT, bestNumber = ZERO, [freeBalance = ZERO, reservedBalance = ZERO, locks = [], vesting = new Option<VestingSchedule>(ClassOf<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'), null), accountNonce = ZERO]]: Result): DerivedBalances {
let lockedBalance = ZERO;
if (Array.isArray(locks)) {
@@ -35,7 +35,7 @@ function calcBalances ([accountId = EMPTY_ACCOUNT, bestNumber = ZERO, [freeBalan
}
// offset = balance locked at genesis, perBlock is the unlock amount
const { offset, perBlock } = vesting.unwrapOr(createType<VestingSchedule>('VestingSchedule'));
const { offset, perBlock } = vesting.unwrapOr(createType('VestingSchedule'));
const vestedNow: BN = perBlock.mul(bestNumber);
const vestedBalance: BN = vestedNow.gt(offset)
? freeBalance
@@ -2,8 +2,6 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Balance } from '@polkadot/types/interfaces';
import BN from 'bn.js';
import { combineLatest, Observable, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
@@ -28,7 +26,7 @@ export function referendumVotesFor (api: ApiInterfaceRx): (referendumId: BN | nu
map(([votersFor, votes, balances]): DerivedReferendumVote[] =>
votersFor.map((accountId, index): DerivedReferendumVote => ({
accountId,
balance: balances[index].votingBalance || createType<Balance>('Balance', 0),
balance: balances[index].votingBalance || createType('Balance', 0),
vote: votes[index] || new Vote(0)
} as unknown as DerivedReferendumVote))
),
@@ -44,7 +44,7 @@ export function voterPositions (api: ApiInterfaceRx): () => Observable<DerivedVo
result[accountId.toString()] = {
globalIndex: setSize.muln(setIndex).addn(index),
index: new BN(index),
setIndex: createType<SetIndex>('SetIndex', setIndex)
setIndex: createType('SetIndex', setIndex)
};
}
});
+1 -1
View File
@@ -58,7 +58,7 @@ function calculateUnlocking (stakingLedger: StakingLedger | undefined, eraLength
const groupedResult = groupByEra(unlockingChunks);
const results = Object.entries(groupedResult).map(([eraString, value]): { value: BN; remainingBlocks: BN } => ({
value,
remainingBlocks: remainingBlocks(createType<BlockNumber>('BlockNumber', eraString), eraLength, bestNumber)
remainingBlocks: remainingBlocks(createType('BlockNumber', eraString), eraLength, bestNumber)
}));
return results.length ? results : undefined;
@@ -13,7 +13,7 @@ import { AnyJsonObject } from '@polkadot/types/types';
* @description
* A [[ReferendumInfo]] with an additional `index` field
*/
export default class ReferendumInfoExtended extends ClassOf<ReferendumInfo>('ReferendumInfo') {
export default class ReferendumInfoExtended extends ClassOf('ReferendumInfo') {
private _index: ReferendumIndex;
public constructor (value: ReferendumInfo | ReferendumInfoExtended, index?: BN | number) {
@@ -21,7 +21,7 @@ export default class ReferendumInfoExtended extends ClassOf<ReferendumInfo>('Ref
this._index = value instanceof ReferendumInfoExtended
? value.index
: createType<ReferendumIndex>('ReferendumIndex', index);
: createType('ReferendumIndex', index);
}
/**