From 02cd1331aa3c6a984cf8cc8cba5c3263ecc174fd Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Fri, 26 Jul 2019 16:39:28 +0200 Subject: [PATCH] Move Event, Address, AccountId, AccountIndex to generic (#1202) * Move Event, Address, AccountId, AccountIndex to generic * Move AccountId, AccountIndex, Address & Event to generic * Move Vote --- packages/api-contract/src/RxBlueprint.ts | 4 +- packages/api-contract/src/RxCode.ts | 3 +- packages/api-contract/src/RxContract.ts | 8 +-- packages/api-contract/src/types.ts | 2 +- .../api-derive/src/accounts/idAndIndex.ts | 9 +-- packages/api-derive/src/accounts/idToIndex.ts | 3 +- packages/api-derive/src/accounts/indexToId.ts | 10 ++-- packages/api-derive/src/accounts/indexes.ts | 8 ++- packages/api-derive/src/balances/all.ts | 7 +-- .../api-derive/src/balances/votingBalances.ts | 3 +- .../balances/votingBalancesNominatorsFor.ts | 4 +- .../api-derive/src/chain/subscribeNewHead.ts | 5 +- .../src/democracy/referendumVotesFor.ts | 8 ++- packages/api-derive/src/democracy/votes.ts | 4 +- .../api-derive/src/elections/approvalsOf.ts | 4 +- .../api-derive/src/elections/approvalsOfAt.ts | 4 +- packages/api-derive/src/elections/info.ts | 4 +- .../src/elections/voterPositions.ts | 4 +- packages/api-derive/src/elections/voters.ts | 10 ++-- .../api-derive/src/staking/controllers.ts | 4 +- packages/api-derive/src/staking/info.ts | 6 +- .../api-derive/src/type/HeaderExtended.ts | 4 +- packages/api-derive/src/types.ts | 5 +- packages/api/src/Base.ts | 4 +- packages/api/src/SignerPayload.ts | 6 +- packages/api/src/SubmittableExtrinsic.ts | 6 +- packages/api/test/e2e/api-derive/rx.spec.ts | 10 ++-- .../api/test/e2e/api/promise-alex.spec.ts | 4 +- .../api/test/e2e/api/promise-contract.spec.ts | 3 +- packages/rpc-core/src/cached.spec.ts | 4 +- packages/rpc-core/src/index.ts | 2 +- packages/types/src/codec/Struct.spec.ts | 2 +- packages/types/src/codec/Vec.spec.ts | 2 +- packages/types/src/interfaceRegistry.ts | 59 +++++++++++-------- .../types/src/interfaces/authorship/types.ts | 3 +- .../types/src/interfaces/collective/types.ts | 3 +- .../types/src/interfaces/consensus/types.ts | 2 +- .../src/interfaces/contracts/definitions.ts | 2 +- .../types/src/interfaces/contracts/types.ts | 6 +- .../types/src/interfaces/deprecated/types.ts | 4 +- .../src/interfaces/elections/definitions.ts | 1 + .../types/src/interfaces/elections/types.ts | 5 +- .../types/src/interfaces/parachains/types.ts | 4 +- .../src/interfaces/runtime/definitions.ts | 3 + .../types/src/interfaces/runtime/types.ts | 11 +++- .../types/src/interfaces/staking/types.ts | 4 +- .../src/interfaces/system/EventRecord.spec.ts | 6 +- .../src/interfaces/system/definitions.ts | 1 + packages/types/src/interfaces/system/types.ts | 5 +- .../types/src/interfaces/treasury/types.ts | 3 +- packages/types/src/primitive/EventRecord.ts | 51 ---------------- .../src/primitive/Extrinsic/Extrinsic.ts | 2 +- .../src/primitive/Extrinsic/v1/Extrinsic.ts | 2 +- .../Extrinsic/v1/ExtrinsicSignature.ts | 6 +- .../src/primitive/Extrinsic/v2/Extrinsic.ts | 2 +- .../primitive/Extrinsic/v2/ExtrinsicExtra.ts | 2 +- .../Extrinsic/v2/ExtrinsicSignature.ts | 6 +- .../primitive/{ => Generic}/AccountId.spec.ts | 21 ++++--- .../src/primitive/{ => Generic}/AccountId.ts | 4 +- .../{ => Generic}/AccountIndex.spec.ts | 16 +++-- .../primitive/{ => Generic}/AccountIndex.ts | 7 ++- .../primitive/{ => Generic}/Address.spec.ts | 19 +++--- .../src/primitive/{ => Generic}/Address.ts | 15 ++--- .../src/primitive/{ => Generic}/Event.ts | 18 +++--- .../src/primitive/{ => Generic}/Vote.spec.ts | 2 +- .../types/src/primitive/{ => Generic}/Vote.ts | 18 +++--- packages/types/src/primitive/Generic/index.ts | 5 ++ packages/types/src/primitive/index.ts | 7 +-- packages/types/src/types.ts | 2 +- 69 files changed, 247 insertions(+), 246 deletions(-) delete mode 100644 packages/types/src/primitive/EventRecord.ts rename packages/types/src/primitive/{ => Generic}/AccountId.spec.ts (84%) rename packages/types/src/primitive/{ => Generic}/AccountId.ts (95%) rename packages/types/src/primitive/{ => Generic}/AccountIndex.spec.ts (73%) rename packages/types/src/primitive/{ => Generic}/AccountIndex.ts (94%) rename packages/types/src/primitive/{ => Generic}/Address.spec.ts (84%) rename packages/types/src/primitive/{ => Generic}/Address.ts (88%) rename packages/types/src/primitive/{ => Generic}/Event.ts (90%) rename packages/types/src/primitive/{ => Generic}/Vote.spec.ts (99%) rename packages/types/src/primitive/{ => Generic}/Vote.ts (84%) diff --git a/packages/api-contract/src/RxBlueprint.ts b/packages/api-contract/src/RxBlueprint.ts index f606c8387a..b402c8ff70 100644 --- a/packages/api-contract/src/RxBlueprint.ts +++ b/packages/api-contract/src/RxBlueprint.ts @@ -3,7 +3,7 @@ // of the Apache-2.0 license. See the LICENSE file for details. import { ISubmittableResult, SubmittableResult } from '@polkadot/api/SubmittableExtrinsic'; -import { Hash } from '@polkadot/types/interfaces'; +import { AccountId, Address, Hash } from '@polkadot/types/interfaces'; import { IKeyringPair } from '@polkadot/types/types'; import { ContractABI } from './types'; @@ -11,7 +11,7 @@ import BN from 'bn.js'; import { Observable } from 'rxjs'; import { map } from 'rxjs/operators'; import { ApiRx } from '@polkadot/api'; -import { createType, AccountId, Address } from '@polkadot/types'; +import { createType } from '@polkadot/types'; import Abi from './Abi'; import RxBase from './RxBase'; diff --git a/packages/api-contract/src/RxCode.ts b/packages/api-contract/src/RxCode.ts index 9dc6c310e4..1ec7778b81 100644 --- a/packages/api-contract/src/RxCode.ts +++ b/packages/api-contract/src/RxCode.ts @@ -3,7 +3,7 @@ // of the Apache-2.0 license. See the LICENSE file for details. import { ISubmittableResult, SubmittableResult } from '@polkadot/api/SubmittableExtrinsic'; -import { Hash } from '@polkadot/types/interfaces'; +import { AccountId, Address, Hash } from '@polkadot/types/interfaces'; import { IKeyringPair } from '@polkadot/types/types'; import { ContractABI } from './types'; @@ -11,7 +11,6 @@ import BN from 'bn.js'; import { Observable } from 'rxjs'; import { map } from 'rxjs/operators'; import { ApiRx } from '@polkadot/api'; -import { AccountId, Address } from '@polkadot/types'; import { compactAddLength, u8aToU8a } from '@polkadot/util'; import Abi from './Abi'; diff --git a/packages/api-contract/src/RxContract.ts b/packages/api-contract/src/RxContract.ts index 208f445105..25a168241f 100644 --- a/packages/api-contract/src/RxContract.ts +++ b/packages/api-contract/src/RxContract.ts @@ -2,15 +2,15 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { ContractABI, ContractABIFn, InterfaceContract, InterfaceContractCalls } from './types'; +import { AccountId, Address } from '@polkadot/types/interfaces'; import { IKeyringPair } from '@polkadot/types/types'; +import { ContractABI, ContractABIFn, InterfaceContract, InterfaceContractCalls } from './types'; import BN from 'bn.js'; import { Observable } from 'rxjs'; import { ApiRx } from '@polkadot/api'; import { SubmittableResult } from '@polkadot/api/SubmittableExtrinsic'; -import { AccountId, Address } from '@polkadot/types'; - +import { createType } from '@polkadot/types'; import Abi from './Abi'; import RxBase from './RxBase'; @@ -31,7 +31,7 @@ export default class RxContract extends RxBase implements InterfaceContract { public constructor (api: ApiRx, abi: ContractABI | Abi, address: string | AccountId | Address) { super(api, abi); - this.address = new Address(address); + this.address = createType('Address', address); Object.entries(abi.messages).forEach(([name]): void => { this.calls[name] = (fn: ContractABIFn): CallResult => diff --git a/packages/api-contract/src/types.ts b/packages/api-contract/src/types.ts index 2c6fcebd58..e9db1d6f63 100644 --- a/packages/api-contract/src/types.ts +++ b/packages/api-contract/src/types.ts @@ -3,10 +3,10 @@ // of the Apache-2.0 license. See the LICENSE file for details. import { SubmittableModuleExtrinsics } from '@polkadot/api/types'; +import { Address } from '@polkadot/types/interfaces'; import { CodecArg } from '@polkadot/types/types'; import { ApiPromise, ApiRx } from '@polkadot/api'; -import { Address } from '@polkadot/types'; export type ApiObject = ApiType extends 'rxjs' ? ApiRx diff --git a/packages/api-derive/src/accounts/idAndIndex.ts b/packages/api-derive/src/accounts/idAndIndex.ts index 10b5aad8cc..5e989f30fe 100644 --- a/packages/api-derive/src/accounts/idAndIndex.ts +++ b/packages/api-derive/src/accounts/idAndIndex.ts @@ -2,13 +2,14 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. +import { AccountId, AccountIndex, Address } from '@polkadot/types/interfaces'; + import { Observable, of } from 'rxjs'; import { map, startWith } from 'rxjs/operators'; import { ApiInterfaceRx } from '@polkadot/api/types'; -import { AccountId, AccountIndex, Address } from '@polkadot/types'; import { isU8a } from '@polkadot/util'; import { decodeAddress } from '@polkadot/util-crypto'; - +import { createType } from '@polkadot/types'; import { idToIndex } from './idToIndex'; import { indexToId } from './indexToId'; import { drr } from '../util/drr'; @@ -37,7 +38,7 @@ export function idAndIndex (api: ApiInterfaceRx): (address?: Address | AccountId : decodeAddress((address || '').toString()); if (decoded.length === 32) { - const accountId = new AccountId(decoded); + const accountId = createType('AccountId', decoded); return idToIndex(api)(accountId).pipe( startWith(undefined), @@ -46,7 +47,7 @@ export function idAndIndex (api: ApiInterfaceRx): (address?: Address | AccountId ); } - const accountIndex = new AccountIndex(decoded); + const accountIndex = createType('AccountIndex', decoded); return indexToId(api)(accountIndex).pipe( startWith(undefined), diff --git a/packages/api-derive/src/accounts/idToIndex.ts b/packages/api-derive/src/accounts/idToIndex.ts index 907b860378..000e0e08eb 100644 --- a/packages/api-derive/src/accounts/idToIndex.ts +++ b/packages/api-derive/src/accounts/idToIndex.ts @@ -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 { AccountId, AccountIndex } from '@polkadot/types/interfaces'; + import { Observable } from 'rxjs'; import { map, startWith } from 'rxjs/operators'; import { ApiInterfaceRx } from '@polkadot/api/types'; -import { AccountId, AccountIndex } from '@polkadot/types'; import { indexes, AccountIndexes } from './indexes'; import { drr } from '../util/drr'; diff --git a/packages/api-derive/src/accounts/indexToId.ts b/packages/api-derive/src/accounts/indexToId.ts index 7d6af36f8f..5118813bd0 100644 --- a/packages/api-derive/src/accounts/indexToId.ts +++ b/packages/api-derive/src/accounts/indexToId.ts @@ -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 { AccountId, AccountIndex } from '@polkadot/types/interfaces'; + import { Observable } from 'rxjs'; import { map, startWith } from 'rxjs/operators'; import { ApiInterfaceRx } from '@polkadot/api/types'; -import { ENUMSET_SIZE } from '@polkadot/types/primitive/AccountIndex'; -import { AccountId, AccountIndex, Vec } from '@polkadot/types'; +import { ENUMSET_SIZE } from '@polkadot/types/primitive/Generic/AccountIndex'; +import { createType, ClassOf, Vec } from '@polkadot/types'; import { drr } from '../util/drr'; @@ -26,9 +28,9 @@ import { drr } from '../util/drr'; export function indexToId (api: ApiInterfaceRx): (accountIndex: AccountIndex | string) => Observable { return (_accountIndex: AccountIndex | string): Observable => { const querySection = api.query.indices || api.query.balances; - const accountIndex = _accountIndex instanceof AccountIndex + const accountIndex = _accountIndex instanceof ClassOf('AccountIndex') ? _accountIndex - : new AccountIndex(_accountIndex); + : createType('AccountIndex', _accountIndex); return (querySection.enumSet>(accountIndex.div(ENUMSET_SIZE))) .pipe( diff --git a/packages/api-derive/src/accounts/indexes.ts b/packages/api-derive/src/accounts/indexes.ts index aa0d4134df..ff02725e8a 100644 --- a/packages/api-derive/src/accounts/indexes.ts +++ b/packages/api-derive/src/accounts/indexes.ts @@ -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 { AccountId, AccountIndex } from '@polkadot/types/interfaces'; + import { Observable } from 'rxjs'; import { map, switchMap } from 'rxjs/operators'; import { ApiInterfaceRx } from '@polkadot/api/types'; -import { ENUMSET_SIZE } from '@polkadot/types/primitive/AccountIndex'; -import { AccountId, AccountIndex } from '@polkadot/types'; +import { ENUMSET_SIZE } from '@polkadot/types/primitive/Generic/AccountIndex'; +import { createType } from '@polkadot/types'; import { drr } from '../util/drr'; @@ -47,7 +49,7 @@ export function indexes (api: ApiInterfaceRx): () => Observable // 64 (0..63 in first) is [1][0] (the first index value in set 2) const index = (outerIndex * enumsetSize) + innerIndex; - result[accountId.toString()] = new AccountIndex(index); + result[accountId.toString()] = createType('AccountIndex', index); }); return result; diff --git a/packages/api-derive/src/balances/all.ts b/packages/api-derive/src/balances/all.ts index ec87cfd15d..15e96d012b 100644 --- a/packages/api-derive/src/balances/all.ts +++ b/packages/api-derive/src/balances/all.ts @@ -2,14 +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/interfaces/balances'; -import { Balance, BlockNumber, Index } from '@polkadot/types/interfaces/runtime'; +import { AccountId, AccountIndex, Address, Balance, BalanceLock, BlockNumber, Index, VestingSchedule } from '@polkadot/types/interfaces'; 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, ClassOf, Option, createType } from '@polkadot/types'; +import { ClassOf, Option, createType } from '@polkadot/types'; import { bnMax } from '@polkadot/util'; import { idAndIndex } from '../accounts/idAndIndex'; @@ -19,7 +18,7 @@ import { drr } from '../util/drr'; type Result = [AccountId | undefined, BlockNumber | undefined, [Balance?, Balance?, BalanceLock[]?, Option?, Index?]]; -const EMPTY_ACCOUNT = new AccountId(); +const EMPTY_ACCOUNT = createType('AccountId'); const ZERO = createType('Balance', 0); function calcBalances ([accountId = EMPTY_ACCOUNT, bestNumber = ZERO, [freeBalance = ZERO, reservedBalance = ZERO, locks = [], vesting = new Option(ClassOf('VestingSchedule'), null), accountNonce = ZERO]]: Result): DerivedBalances { diff --git a/packages/api-derive/src/balances/votingBalances.ts b/packages/api-derive/src/balances/votingBalances.ts index dd9fbe5cf7..15bee7c26d 100644 --- a/packages/api-derive/src/balances/votingBalances.ts +++ b/packages/api-derive/src/balances/votingBalances.ts @@ -2,9 +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 { AccountId, AccountIndex, Address } from '@polkadot/types/interfaces'; + import { combineLatest, Observable, of } from 'rxjs'; import { ApiInterfaceRx } from '@polkadot/api/types'; -import { AccountId, AccountIndex, Address } from '@polkadot/types'; import { DerivedBalances } from '../types'; import { drr } from '../util/drr'; diff --git a/packages/api-derive/src/balances/votingBalancesNominatorsFor.ts b/packages/api-derive/src/balances/votingBalancesNominatorsFor.ts index 5f7d21ad9c..862a22dced 100644 --- a/packages/api-derive/src/balances/votingBalancesNominatorsFor.ts +++ b/packages/api-derive/src/balances/votingBalancesNominatorsFor.ts @@ -2,10 +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 { AccountId, AccountIndex, Address } from '@polkadot/types/interfaces'; + import { Observable, of } from 'rxjs'; import { switchMap } from 'rxjs/operators'; import { ApiInterfaceRx } from '@polkadot/api/types'; -import { AccountId, AccountIndex, Address, Vec } from '@polkadot/types'; +import { Vec } from '@polkadot/types'; import { idAndIndex } from '../accounts/idAndIndex'; import { DerivedBalances } from '../types'; diff --git a/packages/api-derive/src/chain/subscribeNewHead.ts b/packages/api-derive/src/chain/subscribeNewHead.ts index cabaf1e919..44fc21ee9e 100644 --- a/packages/api-derive/src/chain/subscribeNewHead.ts +++ b/packages/api-derive/src/chain/subscribeNewHead.ts @@ -2,12 +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 { Header } from '@polkadot/types/interfaces'; +import { ApiInterfaceRx } from '@polkadot/api/types'; +import { AccountId, Header } from '@polkadot/types/interfaces'; import { Observable, combineLatest, of } from 'rxjs'; import { filter, map, switchMap } from 'rxjs/operators'; -import { ApiInterfaceRx } from '@polkadot/api/types'; -import { AccountId } from '@polkadot/types'; import { HeaderExtended } from '../type'; import { drr } from '../util/drr'; diff --git a/packages/api-derive/src/democracy/referendumVotesFor.ts b/packages/api-derive/src/democracy/referendumVotesFor.ts index 5f84243778..bfd3b6d74d 100644 --- a/packages/api-derive/src/democracy/referendumVotesFor.ts +++ b/packages/api-derive/src/democracy/referendumVotesFor.ts @@ -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 { AccountId, Vote } from '@polkadot/types/interfaces'; + import BN from 'bn.js'; import { combineLatest, Observable, of } from 'rxjs'; import { map, switchMap } from 'rxjs/operators'; import { ApiInterfaceRx } from '@polkadot/api/types'; -import { AccountId, Vec, Vote, createType } from '@polkadot/types'; +import { Vec, createType } from '@polkadot/types'; import { DerivedBalances, DerivedReferendumVote } from '../types'; import { drr } from '../util/drr'; @@ -26,8 +28,8 @@ 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', 0), - vote: votes[index] || new Vote(0) + balance: balances[index].votingBalance || createType('Balance'), + vote: votes[index] || createType('Vote') } as unknown as DerivedReferendumVote)) ), drr() diff --git a/packages/api-derive/src/democracy/votes.ts b/packages/api-derive/src/democracy/votes.ts index 212e259bdb..70c32cea2e 100644 --- a/packages/api-derive/src/democracy/votes.ts +++ b/packages/api-derive/src/democracy/votes.ts @@ -2,10 +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 { AccountId, Vote } from '@polkadot/types/interfaces'; + import BN from 'bn.js'; import { Observable, of } from 'rxjs'; import { ApiInterfaceRx } from '@polkadot/api/types'; -import { AccountId, Vec, Vote } from '@polkadot/types'; +import { Vec } from '@polkadot/types'; import { drr } from '../util/drr'; diff --git a/packages/api-derive/src/elections/approvalsOf.ts b/packages/api-derive/src/elections/approvalsOf.ts index f8109dc747..5f641ed494 100644 --- a/packages/api-derive/src/elections/approvalsOf.ts +++ b/packages/api-derive/src/elections/approvalsOf.ts @@ -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 { ApprovalFlag, SetIndex } from '@polkadot/types/interfaces/elections'; +import { AccountId, ApprovalFlag, SetIndex } from '@polkadot/types/interfaces'; import { ApiInterfaceRx } from '@polkadot/api/types'; import { Observable } from 'rxjs'; -import { AccountId, Vec } from '@polkadot/types'; +import { Vec } from '@polkadot/types'; import { switchMap, map } from 'rxjs/operators'; import { approvalFlagsToBools } from '../util/approvalFlagsToBools'; import { drr } from '../util/drr'; diff --git a/packages/api-derive/src/elections/approvalsOfAt.ts b/packages/api-derive/src/elections/approvalsOfAt.ts index ecdbb0abb9..10f39774f6 100644 --- a/packages/api-derive/src/elections/approvalsOfAt.ts +++ b/packages/api-derive/src/elections/approvalsOfAt.ts @@ -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 { ApprovalFlag, SetIndex } from '@polkadot/types/interfaces/elections'; +import { AccountId, ApprovalFlag, SetIndex } from '@polkadot/types/interfaces'; import { ApiInterfaceRx } from '@polkadot/api/types'; import { Observable } from 'rxjs'; -import { AccountId, Vec } from '@polkadot/types'; +import { Vec } from '@polkadot/types'; import { map } from 'rxjs/operators'; import { approvalFlagsToBools } from '../util/approvalFlagsToBools'; import { drr } from '../util/drr'; diff --git a/packages/api-derive/src/elections/info.ts b/packages/api-derive/src/elections/info.ts index c89838b3e3..d2ea370495 100644 --- a/packages/api-derive/src/elections/info.ts +++ b/packages/api-derive/src/elections/info.ts @@ -2,14 +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 { SetIndex, VoteIndex } from '@polkadot/types/interfaces/elections'; -import { BlockNumber } from '@polkadot/types/interfaces'; +import { AccountId, BlockNumber, SetIndex, VoteIndex } from '@polkadot/types/interfaces'; import BN from 'bn.js'; import { Observable } from 'rxjs'; import { map } from 'rxjs/operators'; import { ApiInterfaceRx } from '@polkadot/api/types'; -import { AccountId } from '@polkadot/types'; import { DerivedElectionsInfo } from '../types'; import { drr } from '../util/drr'; diff --git a/packages/api-derive/src/elections/voterPositions.ts b/packages/api-derive/src/elections/voterPositions.ts index f3343b4437..969b3b42de 100644 --- a/packages/api-derive/src/elections/voterPositions.ts +++ b/packages/api-derive/src/elections/voterPositions.ts @@ -2,9 +2,9 @@ // 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/interfaces/elections'; +import { AccountId, SetIndex } from '@polkadot/types/interfaces'; import { ApiInterfaceRx } from '@polkadot/api/types'; -import { AccountId, Vec, Option, createType } from '@polkadot/types'; +import { Vec, Option, createType } from '@polkadot/types'; import { DerivedVoterPositions } from '../types'; import BN from 'bn.js'; diff --git a/packages/api-derive/src/elections/voters.ts b/packages/api-derive/src/elections/voters.ts index 7a3bd74daa..914fe62be0 100644 --- a/packages/api-derive/src/elections/voters.ts +++ b/packages/api-derive/src/elections/voters.ts @@ -3,11 +3,13 @@ // of the Apache-2.0 license. See the LICENSE file for details. import { ApiInterfaceRx } from '@polkadot/api/types'; -import { AccountId, Vec } from '@polkadot/types'; +import { AccountId } from '@polkadot/types/interfaces'; import { DerivedVoterPositions } from '../types'; import { Observable } from 'rxjs'; import { map } from 'rxjs/operators'; +import { createType, Vec } from '@polkadot/types'; + import { drr } from '../util/drr'; import { voterPositions } from './voterPositions'; @@ -28,11 +30,11 @@ export function voters (api: ApiInterfaceRx): () => Observable> { voterPositions(api)().pipe( map( (voterPositions: DerivedVoterPositions): Vec => - new Vec( - AccountId, + createType( + 'Vec', Object.entries(voterPositions) .sort((a, b): 0 | 1 | -1 => a[1].globalIndex.cmp(b[1].globalIndex)) - .map(([accountId]): AccountId => new AccountId(accountId)) + .map(([accountId]): AccountId => createType('AccountId', accountId)) ) ), drr() diff --git a/packages/api-derive/src/staking/controllers.ts b/packages/api-derive/src/staking/controllers.ts index 8a424d8322..07b018c239 100644 --- a/packages/api-derive/src/staking/controllers.ts +++ b/packages/api-derive/src/staking/controllers.ts @@ -2,10 +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 { AccountId } from '@polkadot/types/interfaces'; + import { Observable, combineLatest, of } from 'rxjs'; import { switchMap } from 'rxjs/operators'; import { ApiInterfaceRx } from '@polkadot/api/types'; -import { AccountId, Option } from '@polkadot/types'; +import { Option } from '@polkadot/types'; import { drr } from '../util/drr'; diff --git a/packages/api-derive/src/staking/info.ts b/packages/api-derive/src/staking/info.ts index 125b700588..52edd29ec5 100644 --- a/packages/api-derive/src/staking/info.ts +++ b/packages/api-derive/src/staking/info.ts @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { BlockNumber } from '@polkadot/types/interfaces/runtime'; +import { AccountId, BlockNumber } from '@polkadot/types/interfaces/runtime'; import { Keys } from '@polkadot/types/interfaces/session'; import { Exposure, RewardDestination, StakingLedger, UnlockChunk, ValidatorPrefs } from '@polkadot/types/interfaces/staking'; @@ -12,7 +12,7 @@ import { DerivedStaking, DerivedUnlocking } from '../types'; import BN from 'bn.js'; import { combineLatest, Observable, of } from 'rxjs'; import { map, switchMap } from 'rxjs/operators'; -import { createType, AccountId, Option, Tuple, Vec } from '@polkadot/types'; +import { createType, Option, Tuple, Vec } from '@polkadot/types'; import { isUndefined } from '@polkadot/util'; @@ -199,7 +199,7 @@ function withControllerLedger (api: ApiInterfaceRx, accountId: AccountId, stakin */ export function info (api: ApiInterfaceRx): (_accountId: Uint8Array | string) => Observable { return (_accountId: Uint8Array | string): Observable => { - const accountId = new AccountId(_accountId); + const accountId = createType('AccountId', _accountId); return ( api.queryMulti([ diff --git a/packages/api-derive/src/type/HeaderExtended.ts b/packages/api-derive/src/type/HeaderExtended.ts index ce9a6648bb..b658cb26dc 100644 --- a/packages/api-derive/src/type/HeaderExtended.ts +++ b/packages/api-derive/src/type/HeaderExtended.ts @@ -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 { Header } from '@polkadot/types/interfaces'; +import { AccountId, Header } from '@polkadot/types/interfaces'; import { AnyJsonObject } from '@polkadot/types/types'; -import { AccountId, u64, ClassOf } from '@polkadot/types'; +import { u64, ClassOf } from '@polkadot/types'; /** * @name HeaderExtended diff --git a/packages/api-derive/src/types.ts b/packages/api-derive/src/types.ts index 4a69b926ce..94e1a5e03c 100644 --- a/packages/api-derive/src/types.ts +++ b/packages/api-derive/src/types.ts @@ -2,12 +2,9 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { SetIndex, VoteIndex } from '@polkadot/types/interfaces/elections'; -import { Balance, BlockNumber, Index } from '@polkadot/types/interfaces/runtime'; -import { Exposure, RewardDestination, StakingLedger, ValidatorPrefs } from '@polkadot/types/interfaces/staking'; +import { AccountId, Balance, BlockNumber, Exposure, Index, RewardDestination, SetIndex, StakingLedger, ValidatorPrefs, Vote, VoteIndex } from '@polkadot/types/interfaces'; import BN from 'bn.js'; -import { AccountId, Vote } from '@polkadot/types'; export interface DerivedBalances { accountId: AccountId; diff --git a/packages/api/src/Base.ts b/packages/api/src/Base.ts index f0c61cae9b..3dc6acb751 100644 --- a/packages/api/src/Base.ts +++ b/packages/api/src/Base.ts @@ -23,7 +23,7 @@ import { Storage } from '@polkadot/api-metadata/storage/types'; import storageFromMeta from '@polkadot/api-metadata/storage/fromMetadata'; import RpcCore from '@polkadot/rpc-core'; import { WsProvider } from '@polkadot/rpc-provider'; -import { Event, getTypeRegistry, Metadata, Method, Null, u64 } from '@polkadot/types'; +import { getTypeRegistry, GenericEvent, Metadata, Method, Null, u64 } from '@polkadot/types'; import Linkage, { LinkageResult } from '@polkadot/types/codec/Linkage'; import { DEFAULT_VERSION as EXTRINSIC_DEFAULT_VERSION } from '@polkadot/types/primitive/Extrinsic/constants'; import { MethodFunction, ModulesWithMethods } from '@polkadot/types/primitive/Method'; @@ -533,7 +533,7 @@ export default abstract class ApiBase { // only inject if we are not a clone (global init) if (!this._options.source) { - Event.injectMetadata(this.runtimeMetadata); + GenericEvent.injectMetadata(this.runtimeMetadata); Method.injectMethods(extrinsics); // detect the extrinsic version in-use based on the last block diff --git a/packages/api/src/SignerPayload.ts b/packages/api/src/SignerPayload.ts index dfb4d31f22..639091cb60 100644 --- a/packages/api/src/SignerPayload.ts +++ b/packages/api/src/SignerPayload.ts @@ -2,10 +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 { Balance, BlockNumber, Hash, Index } from '@polkadot/types/interfaces'; +import { Address, Balance, BlockNumber, Hash, Index } from '@polkadot/types/interfaces'; import { SignerPayload as ISignerPayload } from './types'; -import { Address, ClassOf, Compact, ExtrinsicEra, Struct, u8, Method } from '@polkadot/types'; +import { ClassOf, Compact, ExtrinsicEra, Struct, u8, Method } from '@polkadot/types'; export interface SignerPayloadType { address: Address; @@ -20,7 +20,7 @@ export interface SignerPayloadType { } export default class SignerPayload extends Struct.with({ - address: Address, + address: ClassOf('Address'), blockHash: ClassOf('Hash'), blockNumber: ClassOf('BlockNumber'), era: ExtrinsicEra, diff --git a/packages/api/src/SubmittableExtrinsic.ts b/packages/api/src/SubmittableExtrinsic.ts index 26ff428ad8..6a896d9f2a 100644 --- a/packages/api/src/SubmittableExtrinsic.ts +++ b/packages/api/src/SubmittableExtrinsic.ts @@ -2,14 +2,14 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { ExtrinsicStatus, EventRecord, Hash, Header, Index, SignedBlock } from '@polkadot/types/interfaces'; +import { AccountId, Address, ExtrinsicStatus, EventRecord, Hash, Header, Index, SignedBlock } from '@polkadot/types/interfaces'; import { AnyNumber, AnyU8a, Callback, Codec, IExtrinsic, IExtrinsicEra, IKeyringPair, SignatureOptions } from '@polkadot/types/types'; import { ApiInterfaceRx, ApiTypes } from './types'; import BN from 'bn.js'; import { Observable, combineLatest, of } from 'rxjs'; import { first, map, mergeMap, switchMap, tap } from 'rxjs/operators'; -import { createType, AccountId, Address, getTypeRegistry, Method, Vec, ExtrinsicEra } from '@polkadot/types'; +import { createType, ClassOf, Method, Vec, ExtrinsicEra } from '@polkadot/types'; import { isBn, isFunction, isNumber, isUndefined } from '@polkadot/util'; import filterEvents from './util/filterEvents'; @@ -113,7 +113,7 @@ export default function createSubmittableExtrinsic ( extrinsic: Method | Uint8Array | string, trackingCb?: Callback ): SubmittableExtrinsic { - const _extrinsic = new (getTypeRegistry().getOrThrow('Extrinsic'))(extrinsic, { version: api.extrinsicType }) as SubmittableExtrinsic; + const _extrinsic = new (ClassOf('Extrinsic'))(extrinsic, { version: api.extrinsicType }) as unknown as SubmittableExtrinsic; const _noStatusCb = type === 'rxjs'; function updateSigner (updateId: number, status: Hash | ISubmittableResult): void { diff --git a/packages/api/test/e2e/api-derive/rx.spec.ts b/packages/api/test/e2e/api-derive/rx.spec.ts index 506aec67bf..7fdef5f197 100644 --- a/packages/api/test/e2e/api-derive/rx.spec.ts +++ b/packages/api/test/e2e/api-derive/rx.spec.ts @@ -9,7 +9,7 @@ import BN from 'bn.js'; import ApiRx from '@polkadot/api/rx/Api'; import { HeaderExtended } from '@polkadot/api-derive'; import { DerivedBalances, DerivedContractFees, DerivedElectionsInfo, DerivedFees, DerivedSessionInfo } from '@polkadot/api-derive/types'; -import { AccountId, AccountIndex, ClassOf } from '@polkadot/types'; +import { createType, ClassOf } from '@polkadot/types'; import { WsProvider } from '@polkadot/rpc-provider'; const WS = 'ws://127.0.0.1:9944/'; @@ -74,7 +74,7 @@ describe.skip('Api-RX derive e2e', (): void => { api.derive.accounts.indexToId(IX).subscribe((accountId): void => { // The first emitted value for accountId is undefined when passing the IX if (accountId) { - expect(accountId instanceof AccountId).toBe(true); + expect(accountId instanceof ClassOf('AccountId')).toBe(true); expect(accountId.toString()).toEqual(ID); } else { expect(accountId).toEqual(undefined); @@ -90,7 +90,7 @@ describe.skip('Api-RX derive e2e', (): void => { api.derive.accounts.idToIndex(ID).subscribe((accountIndex): void => { // The first emitted value for AccountIndex is undefined when passing the ID if (accountIndex) { - expect(accountIndex instanceof AccountIndex).toBe(true); + expect(accountIndex instanceof ClassOf('AccountIndex')).toBe(true); expect(accountIndex.toString()).toEqual(IX); } else { expect(accountIndex).toEqual(undefined); @@ -107,7 +107,7 @@ describe.skip('Api-RX derive e2e', (): void => { // A local dev chain should have the AccountIndex of Alice expect(accountIndexes).toHaveProperty( ID, - new AccountIndex(IX) + createType('AccountIndex', IX) ); done(); }); @@ -122,7 +122,7 @@ describe.skip('Api-RX derive e2e', (): void => { it('It returns an object with all relevant balance information of an account', async (done): Promise => { api.derive.balances.all(ID).subscribe((balances: DerivedBalances): void => { expect(balances).toEqual(expect.objectContaining({ - accountId: expect.any(AccountId), + accountId: expect.any(ClassOf('AccountId')), accountNonce: expect.any(ClassOf('Index')), availableBalance: expect.any(BN), freeBalance: expect.any(BN), diff --git a/packages/api/test/e2e/api/promise-alex.spec.ts b/packages/api/test/e2e/api/promise-alex.spec.ts index 5df9a6448b..b7ba131cfa 100644 --- a/packages/api/test/e2e/api/promise-alex.spec.ts +++ b/packages/api/test/e2e/api/promise-alex.spec.ts @@ -2,10 +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 { EventRecord, Header } from '@polkadot/types/interfaces'; +import { AccountId, EventRecord, Header } from '@polkadot/types/interfaces'; import WsProvider from '@polkadot/rpc-provider/ws'; -import { AccountId, ClassOf, Option, Vec } from '@polkadot/types'; +import { ClassOf, Option, Vec } from '@polkadot/types'; import ApiPromise from '../../../src/promise'; import describeE2E from '../../util/describeE2E'; diff --git a/packages/api/test/e2e/api/promise-contract.spec.ts b/packages/api/test/e2e/api/promise-contract.spec.ts index 3fc5c38c42..1a93afff9c 100644 --- a/packages/api/test/e2e/api/promise-contract.spec.ts +++ b/packages/api/test/e2e/api/promise-contract.spec.ts @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { Hash } from '@polkadot/types/interfaces'; +import { Address, Hash } from '@polkadot/types/interfaces'; import fs from 'fs'; import path from 'path'; @@ -10,7 +10,6 @@ import path from 'path'; import { Abi } from '@polkadot/api-contract'; import testingPairs from '@polkadot/keyring/testingPairs'; import WsProvider from '@polkadot/rpc-provider/ws'; -import { Address } from '@polkadot/types'; import { SubmittableResult } from '../../../src'; import ApiPromise from '../../../src/promise'; diff --git a/packages/rpc-core/src/cached.spec.ts b/packages/rpc-core/src/cached.spec.ts index 0418af92e0..1e47207f53 100644 --- a/packages/rpc-core/src/cached.spec.ts +++ b/packages/rpc-core/src/cached.spec.ts @@ -4,7 +4,7 @@ import testingPairs from '@polkadot/keyring/testingPairs'; import MockProvider from '@polkadot/rpc-provider/mock'; -import { AccountId } from '@polkadot/types'; +import { createType } from '@polkadot/types'; import Rpc from '.'; @@ -32,7 +32,7 @@ describe('Cached Observables', (): void => { it('creates a single observable (multiple calls, different arguments that should be cached together)', (): void => { const observable1 = rpc.chain.subscribeNewHead(keyring.alice.address); - const observable2 = rpc.chain.subscribeNewHead(new AccountId(keyring.alice.address)); + const observable2 = rpc.chain.subscribeNewHead(createType('AccountId', keyring.alice.address)); expect(observable2).toBe(observable1); }); diff --git a/packages/rpc-core/src/index.ts b/packages/rpc-core/src/index.ts index c34d833a80..a8489bd122 100644 --- a/packages/rpc-core/src/index.ts +++ b/packages/rpc-core/src/index.ts @@ -246,7 +246,7 @@ export default class Rpc implements RpcInterface { length: false, // Normalize args so that different args that should be cached // together are cached together. - // E.g.: `query.my.method('abc') === query.my.method(new AccountId('abc'));` + // E.g.: `query.my.method('abc') === query.my.method(createType('AccountId', 'abc'));` normalizer: JSON.stringify }); diff --git a/packages/types/src/codec/Struct.spec.ts b/packages/types/src/codec/Struct.spec.ts index 3bd73913f5..2571a8762c 100644 --- a/packages/types/src/codec/Struct.spec.ts +++ b/packages/types/src/codec/Struct.spec.ts @@ -5,7 +5,7 @@ import '../injector'; import { ClassOf } from '../codec/createType'; -import AccountId from '../primitive/AccountId'; +import AccountId from '../primitive/Generic/AccountId'; import Text from '../primitive/Text'; import U32 from '../primitive/U32'; import { CodecTo } from '../types'; diff --git a/packages/types/src/codec/Vec.spec.ts b/packages/types/src/codec/Vec.spec.ts index e6116ee882..4a4e789536 100644 --- a/packages/types/src/codec/Vec.spec.ts +++ b/packages/types/src/codec/Vec.spec.ts @@ -7,7 +7,7 @@ import { Codec, CodecTo } from '../types'; import extrinsics from '@polkadot/api-metadata/extrinsics/static'; -import AccountId from '../primitive/AccountId'; +import AccountId from '../primitive/Generic/AccountId'; import Method from '../primitive/Method'; import Text from '../primitive/Text'; import { createTypeUnsafe } from './createType'; diff --git a/packages/types/src/interfaceRegistry.ts b/packages/types/src/interfaceRegistry.ts index 7ce033911d..1d845fc0c5 100644 --- a/packages/types/src/interfaceRegistry.ts +++ b/packages/types/src/interfaceRegistry.ts @@ -1,12 +1,12 @@ // Auto-generated via `yarn build:interfaces`, do not edit import { Compact, Option, Vec } from './codec'; -import { AccountId, AccountIndex, Address, Bool, Bytes, Data, Event, Extrinsic, ExtrinsicEra, Fixed64, GenericBlock, GenericConsensusEngineId, GenericDigest, GenericDigestItem, H160, H256, H512, I128, I16, I256, I32, I64, I8, ImmortalEra, Method, Moment, MortalEra, Null, Origin, SignaturePayload, StorageHasher, StorageKey, Text, Type, U128, U16, U256, U32, U64, U8, Vote, bool, i128, i16, i256, i32, i64, i8, u128, u16, u256, u32, u64, u8, usize } from './primitive'; -import { AccountIdOf, Balance, BalanceOf, Block, BlockNumber, Consensus, ConsensusEngineId, Digest, DigestItem, Ed25519Signature, Hash, Header, Index, Justification, KeyTypeId, KeyValue, LockIdentifier, Perbill, Permill, Phantom, PreRuntime, Seal, SealV0, SessionKeys, Signature, SignedBlock, Sr25519Signature, StorageData, ValidatorId, Weight, WeightMultiplier } from './interfaces/runtime'; +import { Bytes, Data, Extrinsic, ExtrinsicEra, Fixed64, GenericAccountId, GenericAccountIndex, GenericAddress, GenericBlock, GenericConsensusEngineId, GenericDigest, GenericDigestItem, GenericEvent, GenericVote, H160, H256, H512, I128, I16, I256, I32, I64, I8, ImmortalEra, Method, Moment, MortalEra, Null, Origin, SignaturePayload, StorageHasher, StorageKey, Text, Type, U128, U16, U256, U32, U64, U8, bool, i128, i16, i256, i32, i64, i8, u128, u16, u256, u32, u64, u8, usize } from './primitive'; +import { AccountId, AccountIdOf, AccountIndex, Address, Balance, BalanceOf, Block, BlockNumber, Consensus, ConsensusEngineId, Digest, DigestItem, Ed25519Signature, Hash, Header, Index, Justification, KeyTypeId, KeyValue, LockIdentifier, Perbill, Permill, Phantom, PreRuntime, Seal, SealV0, SessionKeys, Signature, SignedBlock, Sr25519Signature, StorageData, ValidatorId, Weight, WeightMultiplier } from './interfaces/runtime'; import { AuthorityId } from './interfaces/consensus'; import { Keys, SessionIndex } from './interfaces/session'; import { AuthorityWeight, NextAuthority, PendingPause, PendingResume, StoredPendingChange, StoredState } from './interfaces/grandpa'; -import { ApprovalFlag, SetIndex, VoteIndex, VoteThreshold, VoterInfo } from './interfaces/elections'; +import { ApprovalFlag, SetIndex, Vote, VoteIndex, VoteThreshold, VoterInfo } from './interfaces/elections'; import { InclusionHeight, Uncle, UncleEntryItem } from './interfaces/authorship'; import { BabeWeight } from './interfaces/babe'; import { BalanceLock, VestingSchedule, WithdrawReasons } from './interfaces/balances'; @@ -17,27 +17,11 @@ import { AccountInfo, Amount, AssetOf, InherentOfflineReport, LockPeriods, NewAc import { Heartbeat, OpaqueMultiaddr, OpaqueNetworkState, OpaquePeerId } from './interfaces/imOnline'; import { AttestedCandidate, AuctionIndex, BalanceUpload, Bidder, CandidateReceipt, CollatorSignature, EgressQueueRoot, HeadData, IncomingParachain, IncomingParachainDeploy, IncomingParachainFixed, LeasePeriod, LeasePeriodOf, NewBidder, ParaId, ParaIdOf, ParachainDispatchOrigin, SlotRange, SubId, UpwardMessage, ValidatorIndex, ValidityAttestation, ValidityVote, WinningData, WinningDataEntry } from './interfaces/parachains'; import { EraIndex, EraRewards, Exposure, IndividualExposure, MomentOf, RewardDestination, StakingLedger, UnlockChunk, ValidatorPrefs } from './interfaces/staking'; -import { DigestOf, EventId, EventIndex, EventRecord, EventRecord0to76, Key, Phase } from './interfaces/system'; +import { DigestOf, Event, EventId, EventIndex, EventRecord, EventRecord0to76, Key, Phase } from './interfaces/system'; import { TreasuryProposal } from './interfaces/treasury'; import { ApiId, ChainProperties, ExtrinsicStatus, Health, KeyValueOption, NetworkState, PeerInfo, RuntimeVersion, RuntimeVersionApi, StorageChangeSet } from './interfaces/rpc'; export interface InterfaceRegistry { - AccountId: AccountId; - 'Option': Option; - 'Vec': Vec; - AccountIndex: AccountIndex; - 'Compact': Compact; - 'Option': Option; - 'Vec': Vec; - Address: Address; - 'Option
': Option
; - 'Vec
': Vec
; - Event: Event; - 'Option': Option; - 'Vec': Vec; - Bool: Bool; - 'Option': Option; - 'Vec': Vec; bool: bool; 'Option': Option; 'Vec': Vec; @@ -172,9 +156,6 @@ export interface InterfaceRegistry { 'Compact': Compact; 'Option': Option; 'Vec': Vec; - Vote: Vote; - 'Option': Option; - 'Vec': Vec; Extrinsic: Extrinsic; 'Option': Option; 'Vec': Vec; @@ -190,6 +171,16 @@ export interface InterfaceRegistry { SignaturePayload: SignaturePayload; 'Option': Option; 'Vec': Vec; + GenericAccountId: GenericAccountId; + 'Option': Option; + 'Vec': Vec; + GenericAccountIndex: GenericAccountIndex; + 'Compact': Compact; + 'Option': Option; + 'Vec': Vec; + GenericAddress: GenericAddress; + 'Option': Option; + 'Vec': Vec; GenericBlock: GenericBlock; 'Option': Option; 'Vec': Vec; @@ -197,15 +188,31 @@ export interface InterfaceRegistry { 'Compact': Compact; 'Option': Option; 'Vec': Vec; + GenericEvent: GenericEvent; + 'Option': Option; + 'Vec': Vec; GenericDigest: GenericDigest; 'Option': Option; 'Vec': Vec; GenericDigestItem: GenericDigestItem; 'Option': Option; 'Vec': Vec; + GenericVote: GenericVote; + 'Option': Option; + 'Vec': Vec; + AccountId: AccountId; + 'Option': Option; + 'Vec': Vec; AccountIdOf: AccountIdOf; 'Option': Option; 'Vec': Vec; + AccountIndex: AccountIndex; + 'Compact': Compact; + 'Option': Option; + 'Vec': Vec; + Address: Address; + 'Option
': Option
; + 'Vec
': Vec
; Balance: Balance; 'Compact': Compact; 'Option': Option; @@ -341,6 +348,9 @@ export interface InterfaceRegistry { 'Compact': Compact; 'Option': Option; 'Vec': Vec; + Vote: Vote; + 'Option': Option; + 'Vec': Vec; VoteIndex: VoteIndex; 'Compact': Compact; 'Option': Option; @@ -596,6 +606,9 @@ export interface InterfaceRegistry { Phase: Phase; 'Option': Option; 'Vec': Vec; + Event: Event; + 'Option': Option; + 'Vec': Vec; EventRecord0to76: EventRecord0to76; 'Option': Option; 'Vec': Vec; diff --git a/packages/types/src/interfaces/authorship/types.ts b/packages/types/src/interfaces/authorship/types.ts index 24ec601f08..3a0883ecd4 100644 --- a/packages/types/src/interfaces/authorship/types.ts +++ b/packages/types/src/interfaces/authorship/types.ts @@ -2,8 +2,7 @@ import { Codec } from '../../types'; import { Enum, Option } from '../../codec'; -import { AccountId } from '../../primitive'; -import { BlockNumber, Hash } from '../runtime'; +import { AccountId, BlockNumber, Hash } from '../runtime'; /** BlockNumber */ export type InclusionHeight = BlockNumber; diff --git a/packages/types/src/interfaces/collective/types.ts b/packages/types/src/interfaces/collective/types.ts index 7d439fa10b..75791bca76 100644 --- a/packages/types/src/interfaces/collective/types.ts +++ b/packages/types/src/interfaces/collective/types.ts @@ -1,7 +1,8 @@ // Auto-generated via `yarn build:interfaces`, do not edit import { Struct, Vec } from '../../codec'; -import { AccountId, u32, u64 } from '../../primitive'; +import { u32, u64 } from '../../primitive'; +import { AccountId } from '../runtime'; /** u64 */ export type MemberCount = u64; diff --git a/packages/types/src/interfaces/consensus/types.ts b/packages/types/src/interfaces/consensus/types.ts index 89fc9d01cb..5f5dba4487 100644 --- a/packages/types/src/interfaces/consensus/types.ts +++ b/packages/types/src/interfaces/consensus/types.ts @@ -1,6 +1,6 @@ // Auto-generated via `yarn build:interfaces`, do not edit -import { AccountId } from '../../primitive'; +import { AccountId } from '../runtime'; /** AccountId */ export type AuthorityId = AccountId; diff --git a/packages/types/src/interfaces/contracts/definitions.ts b/packages/types/src/interfaces/contracts/definitions.ts index 270c85a3cd..acb6608648 100644 --- a/packages/types/src/interfaces/contracts/definitions.ts +++ b/packages/types/src/interfaces/contracts/definitions.ts @@ -45,7 +45,7 @@ export default { maxEventTopics: 'u32', maxStackHeight: 'u32', maxMemoryPages: 'u32', - enablePrintln: 'Bool', + enablePrintln: 'bool', maxSubjectLen: 'u32' }, SeedOf: 'Hash' diff --git a/packages/types/src/interfaces/contracts/types.ts b/packages/types/src/interfaces/contracts/types.ts index 7e212cbc76..037d1f6a93 100644 --- a/packages/types/src/interfaces/contracts/types.ts +++ b/packages/types/src/interfaces/contracts/types.ts @@ -1,7 +1,7 @@ // Auto-generated via `yarn build:interfaces`, do not edit import { Compact, Enum, Option, Struct, Vec } from '../../codec'; -import { Bool, Bytes, Null, u32, u64, u8 } from '../../primitive'; +import { Bytes, Null, bool, u32, u64, u8 } from '../../primitive'; import { Balance, BlockNumber, Hash } from '../runtime'; /** Struct */ @@ -86,8 +86,8 @@ export interface Schedule extends Struct { readonly maxStackHeight: u32; /** u32 */ readonly maxMemoryPages: u32; - /** Bool */ - readonly enablePrintln: Bool; + /** bool */ + readonly enablePrintln: bool; /** u32 */ readonly maxSubjectLen: u32; } diff --git a/packages/types/src/interfaces/deprecated/types.ts b/packages/types/src/interfaces/deprecated/types.ts index 9c263c0b94..db438fcccc 100644 --- a/packages/types/src/interfaces/deprecated/types.ts +++ b/packages/types/src/interfaces/deprecated/types.ts @@ -1,8 +1,8 @@ // Auto-generated via `yarn build:interfaces`, do not edit import { Enum, Struct } from '../../codec'; -import { AccountId, Bytes, Null, i8, u32, u64 } from '../../primitive'; -import { Balance } from '../runtime'; +import { Bytes, Null, i8, u32, u64 } from '../../primitive'; +import { AccountId, Balance } from '../runtime'; /** Struct */ export interface AccountInfo extends Struct { diff --git a/packages/types/src/interfaces/elections/definitions.ts b/packages/types/src/interfaces/elections/definitions.ts index be8dc92adf..f23436be30 100644 --- a/packages/types/src/interfaces/elections/definitions.ts +++ b/packages/types/src/interfaces/elections/definitions.ts @@ -6,6 +6,7 @@ export default { types: { ApprovalFlag: 'u32', SetIndex: 'u32', + Vote: 'GenericVote', VoteIndex: 'u32', VoterInfo: { lastActive: 'VoteIndex', diff --git a/packages/types/src/interfaces/elections/types.ts b/packages/types/src/interfaces/elections/types.ts index d69c6dd6f2..da449f1e9a 100644 --- a/packages/types/src/interfaces/elections/types.ts +++ b/packages/types/src/interfaces/elections/types.ts @@ -1,7 +1,7 @@ // Auto-generated via `yarn build:interfaces`, do not edit import { Enum, Struct } from '../../codec'; -import { u32 } from '../../primitive'; +import { GenericVote, u32 } from '../../primitive'; import { Balance } from '../runtime'; /** u32 */ @@ -10,6 +10,9 @@ export type ApprovalFlag = u32; /** u32 */ export type SetIndex = u32; +/** GenericVote */ +export type Vote = GenericVote; + /** u32 */ export type VoteIndex = u32; diff --git a/packages/types/src/interfaces/parachains/types.ts b/packages/types/src/interfaces/parachains/types.ts index f6030cf5bf..bbf1b0916c 100644 --- a/packages/types/src/interfaces/parachains/types.ts +++ b/packages/types/src/interfaces/parachains/types.ts @@ -2,8 +2,8 @@ import { Codec } from '../../types'; import { Enum, Struct, Vec } from '../../codec'; -import { AccountId, Bytes, u32, u64 } from '../../primitive'; -import { BalanceOf, BlockNumber, Hash, Signature } from '../runtime'; +import { Bytes, u32, u64 } from '../../primitive'; +import { AccountId, BalanceOf, BlockNumber, Hash, Signature } from '../runtime'; /** Struct */ export interface AttestedCandidate extends Struct { diff --git a/packages/types/src/interfaces/runtime/definitions.ts b/packages/types/src/interfaces/runtime/definitions.ts index 83a61efb80..acadec17bc 100644 --- a/packages/types/src/interfaces/runtime/definitions.ts +++ b/packages/types/src/interfaces/runtime/definitions.ts @@ -4,7 +4,10 @@ export default { types: { + AccountId: 'GenericAccountId', AccountIdOf: 'AccountId', + AccountIndex: 'GenericAccountIndex', + Address: 'GenericAddress', Balance: 'u128', BalanceOf: 'Balance', BlockNumber: 'u64', diff --git a/packages/types/src/interfaces/runtime/types.ts b/packages/types/src/interfaces/runtime/types.ts index e29377574b..c640aaee86 100644 --- a/packages/types/src/interfaces/runtime/types.ts +++ b/packages/types/src/interfaces/runtime/types.ts @@ -2,11 +2,20 @@ import { Codec } from '../../types'; import { Compact, Struct, Vec } from '../../codec'; -import { AccountId, Bytes, Fixed64, GenericBlock, GenericConsensusEngineId, GenericDigest, GenericDigestItem, H256, H512, Null, StorageKey, u128, u32, u64, u8 } from '../../primitive'; +import { Bytes, Fixed64, GenericAccountId, GenericAccountIndex, GenericAddress, GenericBlock, GenericConsensusEngineId, GenericDigest, GenericDigestItem, H256, H512, Null, StorageKey, u128, u32, u64, u8 } from '../../primitive'; + +/** GenericAccountId */ +export type AccountId = GenericAccountId; /** AccountId */ export type AccountIdOf = AccountId; +/** GenericAccountIndex */ +export type AccountIndex = GenericAccountIndex; + +/** GenericAddress */ +export type Address = GenericAddress; + /** u128 */ export type Balance = u128; diff --git a/packages/types/src/interfaces/staking/types.ts b/packages/types/src/interfaces/staking/types.ts index 2e057eda72..26f193a3fe 100644 --- a/packages/types/src/interfaces/staking/types.ts +++ b/packages/types/src/interfaces/staking/types.ts @@ -1,8 +1,8 @@ // Auto-generated via `yarn build:interfaces`, do not edit import { Compact, Enum, Struct, Vec } from '../../codec'; -import { AccountId, Moment, u32 } from '../../primitive'; -import { Balance, BlockNumber } from '../runtime'; +import { Moment, u32 } from '../../primitive'; +import { AccountId, Balance, BlockNumber } from '../runtime'; /** u32 */ export type EraIndex = u32; diff --git a/packages/types/src/interfaces/system/EventRecord.spec.ts b/packages/types/src/interfaces/system/EventRecord.spec.ts index 09fac7c351..7bc5446037 100644 --- a/packages/types/src/interfaces/system/EventRecord.spec.ts +++ b/packages/types/src/interfaces/system/EventRecord.spec.ts @@ -10,7 +10,7 @@ import createType from '../../codec/createType'; import Vec from '../../codec/Vec'; import json1 from '../../json/EventRecord.001.json'; import json3 from '../../json/EventRecord.003.json'; -import Event from '../../primitive/Event'; +import GenericEvent from '../../primitive/Generic/Event'; import Metadata from '../../Metadata'; import metadataV0 from '../../Metadata/v0/static'; import metadata from '../../Metadata/static'; @@ -18,7 +18,7 @@ import metadata from '../../Metadata/static'; describe('EventRecord', (): void => { describe('EventRecord_0_76', (): void => { beforeEach((): void => { - Event.injectMetadata( + GenericEvent.injectMetadata( new Metadata(metadataV0) ); }); @@ -33,7 +33,7 @@ describe('EventRecord', (): void => { describe('EventRecord (current)', (): void => { beforeEach((): void => { - Event.injectMetadata( + GenericEvent.injectMetadata( new Metadata(metadata) ); }); diff --git a/packages/types/src/interfaces/system/definitions.ts b/packages/types/src/interfaces/system/definitions.ts index 673aeb5505..78c7d6a204 100644 --- a/packages/types/src/interfaces/system/definitions.ts +++ b/packages/types/src/interfaces/system/definitions.ts @@ -14,6 +14,7 @@ export default { Finalization: 'Null' } }, + Event: 'GenericEvent', EventRecord0to76: { phase: 'Phase', event: 'Event' diff --git a/packages/types/src/interfaces/system/types.ts b/packages/types/src/interfaces/system/types.ts index c0e4e7a1ff..f0c381427b 100644 --- a/packages/types/src/interfaces/system/types.ts +++ b/packages/types/src/interfaces/system/types.ts @@ -1,12 +1,15 @@ // Auto-generated via `yarn build:interfaces`, do not edit import { Enum, Struct, Vec } from '../../codec'; -import { Bytes, Event, u32, u8 } from '../../primitive'; +import { Bytes, GenericEvent, u32, u8 } from '../../primitive'; import { Digest, Hash } from '../runtime'; /** Digest */ export type DigestOf = Digest; +/** GenericEvent */ +export type Event = GenericEvent; + /** Vec */ export type EventId = Vec; diff --git a/packages/types/src/interfaces/treasury/types.ts b/packages/types/src/interfaces/treasury/types.ts index d75a3c10bf..a217b0d30c 100644 --- a/packages/types/src/interfaces/treasury/types.ts +++ b/packages/types/src/interfaces/treasury/types.ts @@ -1,8 +1,7 @@ // Auto-generated via `yarn build:interfaces`, do not edit import { Struct } from '../../codec'; -import { AccountId } from '../../primitive'; -import { Balance } from '../runtime'; +import { AccountId, Balance } from '../runtime'; /** Struct */ export interface TreasuryProposal extends Struct { diff --git a/packages/types/src/primitive/EventRecord.ts b/packages/types/src/primitive/EventRecord.ts deleted file mode 100644 index b5e9134f9b..0000000000 --- a/packages/types/src/primitive/EventRecord.ts +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright 2017-2019 @polkadot/types authors & contributors -// This software may be modified and distributed under the terms -// of the Apache-2.0 license. See the LICENSE file for details. - -import { Hash } from '../interfaces/runtime'; -import { Phase } from '../interfaces/system'; - -import { ClassOf } from '../codec/createType'; -import Struct from '../codec/Struct'; -import Vec from '../codec/Vec'; -import Event from './Event'; - -/** - * @name EventRecord - * @description - * A record for an [[Event]] (as specified by [[Metadata]]) with the specific [[Phase]] of - * application. - */ -export default class EventRecord extends Struct { - public constructor (value: any) { - super({ - phase: ClassOf('Phase'), - event: Event, - topics: ClassOf('Vec') - }, value); - } - - // TODO We want these injected by createType - public static Fallback = ClassOf('EventRecord0to76'); - - /** - * @description The [[Event]] this record refers to - */ - public get event (): Event { - return this.get('event') as Event; - } - - /** - * @description The [[Phase]] where the event was generated - */ - public get phase (): Phase { - return this.get('phase') as Phase; - } - - /** - * @description The [[Hash]] topics for this event - */ - public get topics (): Vec { - return this.get('topics') as Vec; - } -} diff --git a/packages/types/src/primitive/Extrinsic/Extrinsic.ts b/packages/types/src/primitive/Extrinsic/Extrinsic.ts index 1926cba60b..aa786ca21e 100644 --- a/packages/types/src/primitive/Extrinsic/Extrinsic.ts +++ b/packages/types/src/primitive/Extrinsic/Extrinsic.ts @@ -10,7 +10,7 @@ import { assert, isHex, isU8a, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/u import Base from '../../codec/Base'; import Compact from '../../codec/Compact'; import { FunctionMetadata } from '../../Metadata/v7/Calls'; -import Address from '../Address'; +import Address from '../Generic/Address'; import Method from '../Method'; import ExtrinsicV1, { ExtrinsicValueV1 } from './v1/Extrinsic'; import ExtrinsicV2, { ExtrinsicValueV2 } from './v2/Extrinsic'; diff --git a/packages/types/src/primitive/Extrinsic/v1/Extrinsic.ts b/packages/types/src/primitive/Extrinsic/v1/Extrinsic.ts index 53e2dcd09d..7732284bd8 100644 --- a/packages/types/src/primitive/Extrinsic/v1/Extrinsic.ts +++ b/packages/types/src/primitive/Extrinsic/v1/Extrinsic.ts @@ -8,7 +8,7 @@ import { isU8a } from '@polkadot/util'; import Struct from '../../../codec/Struct'; import Method from '../../Method'; -import Address from '../../Address'; +import Address from '../../Generic/Address'; import ExtrinsicSignature from './ExtrinsicSignature'; export interface ExtrinsicValueV1 { diff --git a/packages/types/src/primitive/Extrinsic/v1/ExtrinsicSignature.ts b/packages/types/src/primitive/Extrinsic/v1/ExtrinsicSignature.ts index 71d89efd5d..367e485d95 100644 --- a/packages/types/src/primitive/Extrinsic/v1/ExtrinsicSignature.ts +++ b/packages/types/src/primitive/Extrinsic/v1/ExtrinsicSignature.ts @@ -8,7 +8,7 @@ import { ExtrinsicPayloadValue, IExtrinsicSignature, IKeyringPair, SignatureOpti import createType, { ClassOf } from '../../../codec/createType'; import Compact from '../../../codec/Compact'; import Struct from '../../../codec/Struct'; -import Address from '../../Address'; +import Address from '../../Generic/Address'; import Method from '../../Method'; import ExtrinsicEra from '../ExtrinsicEra'; import { EMPTY_U8A, IMMORTAL_ERA } from '../constants'; @@ -106,7 +106,7 @@ export default class ExtrinsicSignatureV1 extends Struct implements IExtrinsicSi */ public addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | string, payload: ExtrinsicPayloadValue | Uint8Array | string): IExtrinsicSignature { return this.injectSignature( - new Address(signer), + createType('Address', signer), createType('Signature', signature), new SignaturePayload(payload) ); @@ -116,7 +116,7 @@ export default class ExtrinsicSignatureV1 extends Struct implements IExtrinsicSi * @description Generate a payload and pplies the signature from a keypair */ public sign (method: Method, account: IKeyringPair, { blockHash, era, nonce }: SignatureOptions): IExtrinsicSignature { - const signer = new Address(account.publicKey); + const signer = createType('Address', account.publicKey); const payload = new SignaturePayload({ nonce, method: method.toU8a(), diff --git a/packages/types/src/primitive/Extrinsic/v2/Extrinsic.ts b/packages/types/src/primitive/Extrinsic/v2/Extrinsic.ts index 1e3c153443..180fa68040 100644 --- a/packages/types/src/primitive/Extrinsic/v2/Extrinsic.ts +++ b/packages/types/src/primitive/Extrinsic/v2/Extrinsic.ts @@ -8,7 +8,7 @@ import { isU8a } from '@polkadot/util'; import Struct from '../../../codec/Struct'; import Method from '../../Method'; -import Address from '../../Address'; +import Address from '../../Generic/Address'; import ExtrinsicSignature from './ExtrinsicSignature'; const TRANSACTION_VERSION = 2; diff --git a/packages/types/src/primitive/Extrinsic/v2/ExtrinsicExtra.ts b/packages/types/src/primitive/Extrinsic/v2/ExtrinsicExtra.ts index 4801333d26..d82f19e49d 100644 --- a/packages/types/src/primitive/Extrinsic/v2/ExtrinsicExtra.ts +++ b/packages/types/src/primitive/Extrinsic/v2/ExtrinsicExtra.ts @@ -8,7 +8,7 @@ import { AnyNumber } from '../../../types'; import { ClassOf } from '../../../codec/createType'; import Compact from '../../../codec/Compact'; import Struct from '../../../codec/Struct'; -import Address from '../../Address'; +import Address from '../../Generic/Address'; import ExtrinsicEra from '../ExtrinsicEra'; interface ExtrinsicExtraValueV2 { diff --git a/packages/types/src/primitive/Extrinsic/v2/ExtrinsicSignature.ts b/packages/types/src/primitive/Extrinsic/v2/ExtrinsicSignature.ts index 7d5950b7ad..7343ff8350 100644 --- a/packages/types/src/primitive/Extrinsic/v2/ExtrinsicSignature.ts +++ b/packages/types/src/primitive/Extrinsic/v2/ExtrinsicSignature.ts @@ -8,7 +8,7 @@ import { ExtrinsicPayloadValue, IExtrinsicSignature, IKeyringPair, SignatureOpti import createType, { ClassOf } from '../../../codec/createType'; import Compact from '../../../codec/Compact'; import Struct from '../../../codec/Struct'; -import Address from '../../Address'; +import Address from '../../Generic/Address'; import Method from '../../Method'; import ExtrinsicEra from '../ExtrinsicEra'; import SignaturePayload from './SignaturePayload'; @@ -110,7 +110,7 @@ export default class ExtrinsicSignatureV2 extends Struct implements IExtrinsicSi */ public addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | string, payload: ExtrinsicPayloadValue | Uint8Array | string): IExtrinsicSignature { return this.injectSignature( - new Address(signer), + createType('Address', signer), createType('Signature', signature), new SignaturePayload(payload) ); @@ -120,7 +120,7 @@ export default class ExtrinsicSignatureV2 extends Struct implements IExtrinsicSi * @description Generate a payload and pplies the signature from a keypair */ public sign (method: Method, account: IKeyringPair, { blockHash, era, nonce, tip }: SignatureOptions): IExtrinsicSignature { - const signer = new Address(account.publicKey); + const signer = createType('Address', account.publicKey); const payload = new SignaturePayload({ blockHash, era: era || IMMORTAL_ERA, diff --git a/packages/types/src/primitive/AccountId.spec.ts b/packages/types/src/primitive/Generic/AccountId.spec.ts similarity index 84% rename from packages/types/src/primitive/AccountId.spec.ts rename to packages/types/src/primitive/Generic/AccountId.spec.ts index 457512c33f..aa323a19c6 100644 --- a/packages/types/src/primitive/AccountId.spec.ts +++ b/packages/types/src/primitive/Generic/AccountId.spec.ts @@ -2,19 +2,18 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import '../injector'; +import '../../injector'; import { setAddressPrefix } from '@polkadot/util-crypto'; -import createType from '../codec/createType'; -import U8a from '../codec/U8a'; -import Vec from '../codec/Vec'; -import jsonVec from '../json/AccountIdVec.001.json'; +import createType from '../../codec/createType'; +import U8a from '../../codec/U8a'; +import jsonVec from '../../json/AccountIdVec.001.json'; import AccountId from './AccountId'; describe('AccountId', (): void => { describe('defaults', (): void => { - const id = new AccountId(); + const id = createType('AccountId'); it('has a 32-byte length', (): void => { expect(id).toHaveLength(32); @@ -32,13 +31,13 @@ describe('AccountId', (): void => { describe('decoding', (): void => { const testDecode = (type: string, input: Uint8Array | string | AccountId, expected: string): void => it(`can decode from ${type}`, (): void => { - const a = new AccountId(input); + const a = createType('AccountId', input); expect(a.toString()).toBe(expected); }); testDecode( 'AccountId', - new AccountId('0x0102030405060708010203040506070801020304050607080102030405060708'), + createType('AccountId', '0x0102030405060708010203040506070801020304050607080102030405060708'), '5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF' ); testDecode('hex', '0x0102030405060708010203040506070801020304050607080102030405060708', '5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF'); @@ -64,7 +63,7 @@ describe('AccountId', (): void => { describe('encoding', (): void => { const testEncode = (to: 'toHex' | 'toJSON' | 'toString' | 'toU8a', expected: Uint8Array | string, input: string = '5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF'): void => it(`can encode ${to}`, (): void => { - const a = new AccountId(input); + const a = createType('AccountId', input); expect(a[to]()).toEqual(expected); }); @@ -79,7 +78,7 @@ describe('AccountId', (): void => { ])); it('decodes to a non-empty value', (): void => { - expect(new AccountId('7qT1BvpawNbqb3BZaBTMFMMAKrpJKLPf1LmEHR1JyarWJdMX').isEmpty).toBe(false); + expect(createType('AccountId', '7qT1BvpawNbqb3BZaBTMFMMAKrpJKLPf1LmEHR1JyarWJdMX').isEmpty).toBe(false); }); }); @@ -88,7 +87,7 @@ describe('AccountId', (): void => { setAddressPrefix(68); const data = createType('StorageData', jsonVec.params.result.changes[0][1]); - const list = new Vec(AccountId, data).map((accountId): string => accountId.toString()); + const list = createType('Vec', data).map((accountId): string => accountId.toString()); expect(list).toEqual([ '7qVJujLF3EDbZt5WfQXWvueFedMS4Vfk2Hb4GyR8jwksTLup', diff --git a/packages/types/src/primitive/AccountId.ts b/packages/types/src/primitive/Generic/AccountId.ts similarity index 95% rename from packages/types/src/primitive/AccountId.ts rename to packages/types/src/primitive/Generic/AccountId.ts index ba2ab5d390..4a9c6766cc 100644 --- a/packages/types/src/primitive/AccountId.ts +++ b/packages/types/src/primitive/Generic/AccountId.ts @@ -2,12 +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 { AnyString, AnyU8a } from '../types'; +import { AnyString, AnyU8a } from '../../types'; import { hexToU8a, isHex, isString, isU8a, u8aToU8a } from '@polkadot/util'; import { decodeAddress, encodeAddress } from '@polkadot/util-crypto'; -import U8aFixed from '../codec/U8aFixed'; +import U8aFixed from '../../codec/U8aFixed'; /** * @name AccountId diff --git a/packages/types/src/primitive/AccountIndex.spec.ts b/packages/types/src/primitive/Generic/AccountIndex.spec.ts similarity index 73% rename from packages/types/src/primitive/AccountIndex.spec.ts rename to packages/types/src/primitive/Generic/AccountIndex.spec.ts index 72d8e76227..9ac344d6b8 100644 --- a/packages/types/src/primitive/AccountIndex.spec.ts +++ b/packages/types/src/primitive/Generic/AccountIndex.spec.ts @@ -2,40 +2,44 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. +import '../../injector'; + import BN from 'bn.js'; + +import createType from '../../codec/createType'; import AccountIndex from './AccountIndex'; describe('AccountIndex', (): void => { it('creates a BN representation', (): void => { expect( - new AccountIndex(new Uint8Array([17, 18, 19, 20])).toNumber() + createType('AccountIndex', new Uint8Array([17, 18, 19, 20])).toNumber() ).toEqual(336794129); }); it('creates a BN representation (from ss-58)', (): void => { expect( - new AccountIndex('Mwz15xP2').toNumber() + createType('AccountIndex', 'Mwz15xP2').toNumber() ).toEqual(336794129); }); it('constructs 2-byte from number', (): void => { expect( - new AccountIndex(256 * 1).toString() + createType('AccountIndex', 256 * 1).toString() ).toEqual('25GUyv'); }); it('constructs from number', (): void => { expect( - new AccountIndex(new BN(336794129)).toString() + createType('AccountIndex', new BN(336794129)).toString() ).toEqual('Mwz15xP2'); }); it('compares ss-58 values', (): void => { - expect(new AccountIndex(256 * 1).eq('25GUyv')).toBe(true); + expect(createType('AccountIndex', 256 * 1).eq('25GUyv')).toBe(true); }); it('compares numbers', (): void => { - expect(new AccountIndex('2jpAFn').eq(256 * 1)).toBe(true); + expect(createType('AccountIndex', '2jpAFn').eq(256 * 1)).toBe(true); }); describe('calcLength', (): void => { diff --git a/packages/types/src/primitive/AccountIndex.ts b/packages/types/src/primitive/Generic/AccountIndex.ts similarity index 94% rename from packages/types/src/primitive/AccountIndex.ts rename to packages/types/src/primitive/Generic/AccountIndex.ts index 94b884a0b6..e3c6e386f9 100644 --- a/packages/types/src/primitive/AccountIndex.ts +++ b/packages/types/src/primitive/Generic/AccountIndex.ts @@ -2,13 +2,14 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { AnyNumber } from '../types'; +import { AnyNumber } from '../../types'; import BN from 'bn.js'; import { bnToBn, isBn, isNumber, isU8a, isHex } from '@polkadot/util'; import { decodeAddress, encodeAddress } from '@polkadot/util-crypto'; -import U32 from './U32'; +import createType from '../../codec/createType'; +import U32 from '../U32'; export const ENUMSET_SIZE = new BN(64); @@ -92,7 +93,7 @@ export default class AccountIndex extends U32 { } // convert and compare - return super.eq(new AccountIndex(other)); + return super.eq(createType('AccountIndex', other)); } /** diff --git a/packages/types/src/primitive/Address.spec.ts b/packages/types/src/primitive/Generic/Address.spec.ts similarity index 84% rename from packages/types/src/primitive/Address.spec.ts rename to packages/types/src/primitive/Generic/Address.spec.ts index cc55aa117b..b6d2ec3281 100644 --- a/packages/types/src/primitive/Address.spec.ts +++ b/packages/types/src/primitive/Generic/Address.spec.ts @@ -2,6 +2,9 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. +import '../../injector'; + +import createType from '../../codec/createType'; import AccountId from './AccountId'; import AccountIndex from './AccountIndex'; import Address from './Address'; @@ -9,7 +12,7 @@ import Address from './Address'; describe('Address', (): void => { const testDecode = (type: string, input: Address | AccountId | AccountIndex | number[] | Uint8Array, expected: string): void => it(`can decode from ${type}`, (): void => { - const a = new Address(input); + const a = createType('Address', input); expect(a.toString()).toBe(expected); }); @@ -17,35 +20,35 @@ describe('Address', (): void => { it('equals on AccountId', (): void => { const addr = '5DkQbYAExs3M2sZgT1Ec3mKfZnAQCL4Dt9beTCknkCUn5jzo'; - expect(new Address(addr).eq(addr)).toBe(true); + expect(createType('Address', addr).eq(addr)).toBe(true); }); it('equals on AccountIndex', (): void => { // see the test below - these are equivalent (with different prefix encoding) - expect(new Address('2jpAFn').eq('25GUyv')).toBe(true); + expect(createType('Address', '2jpAFn').eq('25GUyv')).toBe(true); }); }); describe('decoding', (): void => { testDecode( 'Address', - new Address('5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF'), + createType('Address', '5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF'), '5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF' ); testDecode( 'AccountId', - new AccountId('5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF'), + createType('AccountId', '5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF'), '5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF' ); testDecode( 'AccountIndex (mixed prefixes)', - new Address('2jpAFn'), + createType('Address', '2jpAFn'), // NOTE Expected adress here is encoded with prefix 42, input above with 68 '25GUyv' ); testDecode( 'AccountIndex (hex)', - new AccountIndex('0x0100'), + createType('AccountIndex', '0x0100'), '25GUyv' ); testDecode( @@ -94,7 +97,7 @@ describe('Address', (): void => { describe('encoding', (): void => { const testEncode = (to: 'toHex' | 'toString' | 'toU8a', expected: string | Uint8Array): void => it(`can encode ${to}`, (): void => { - const a = new Address('5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF'); + const a = createType('Address', '5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF'); expect(a[to]()).toEqual(expected); }); diff --git a/packages/types/src/primitive/Address.ts b/packages/types/src/primitive/Generic/Address.ts similarity index 88% rename from packages/types/src/primitive/Address.ts rename to packages/types/src/primitive/Generic/Address.ts index 21095fd6c3..713c474ddf 100644 --- a/packages/types/src/primitive/Address.ts +++ b/packages/types/src/primitive/Generic/Address.ts @@ -6,7 +6,8 @@ import BN from 'bn.js'; import { hexToU8a, isBn, isHex, isNumber, isU8a, u8aConcat, u8aToHex, u8aToU8a, u8aToBn } from '@polkadot/util'; import { decodeAddress } from '@polkadot/util-crypto'; -import Base from '../codec/Base'; +import createType from '../../codec/createType'; +import Base from '../../codec/Base'; import AccountId from './AccountId'; import AccountIndex from './AccountIndex'; @@ -33,7 +34,7 @@ export default class Address extends Base { if (value instanceof AccountId || value instanceof AccountIndex) { return value; } else if (isBn(value) || isNumber(value)) { - return new AccountIndex(value); + return createType('AccountIndex', value); } else if (value instanceof Address) { return value.raw; } else if (Array.isArray(value)) { @@ -42,14 +43,14 @@ export default class Address extends Base { // This allows us to instantiate an address with a raw publicKey. Do this first before // we checking the first byte, otherwise we may split an already-existent valid address if (value.length === 32) { - return new AccountId(value); + return createType('AccountId', value); } else if (value[0] === 0xff) { - return new AccountId(value.subarray(1)); + return createType('AccountId', value.subarray(1)); } const [offset, length] = AccountIndex.readLength(value); - return new AccountIndex(u8aToBn(value.subarray(offset, offset + length), true)); + return createType('AccountIndex', u8aToBn(value.subarray(offset, offset + length), true)); } else if (isHex(value)) { return Address.decodeAddress(hexToU8a(value)); } @@ -57,8 +58,8 @@ export default class Address extends Base { const decoded = decodeAddress(value); return decoded.length === 32 - ? new AccountId(decoded) - : new AccountIndex(u8aToBn(decoded, true)); + ? createType('AccountId', decoded) + : createType('AccountIndex', u8aToBn(decoded, true)); } /** diff --git a/packages/types/src/primitive/Event.ts b/packages/types/src/primitive/Generic/Event.ts similarity index 90% rename from packages/types/src/primitive/Event.ts rename to packages/types/src/primitive/Generic/Event.ts index 2a170f457b..8446031cf9 100644 --- a/packages/types/src/primitive/Event.ts +++ b/packages/types/src/primitive/Generic/Event.ts @@ -2,18 +2,18 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { EventId } from '../interfaces/system'; -import { Constructor, Codec } from '../types'; +import { EventId } from '../../interfaces/system'; +import { Constructor, Codec } from '../../types'; import { assert, isUndefined, stringCamelCase, u8aToHex } from '@polkadot/util'; -import { ClassOf, TypeDef, getTypeClass, getTypeDef } from '../codec/createType'; -import Struct from '../codec/Struct'; -import Tuple from '../codec/Tuple'; -import Metadata from '../Metadata'; -import { EventMetadata as EventMetadataV7 } from '../Metadata/v7/Events'; -import Null from './Null'; -import Unconstructable from './Unconstructable'; +import { ClassOf, TypeDef, getTypeClass, getTypeDef } from '../../codec/createType'; +import Struct from '../../codec/Struct'; +import Tuple from '../../codec/Tuple'; +import Metadata from '../../Metadata'; +import { EventMetadata as EventMetadataV7 } from '../../Metadata/v7/Events'; +import Null from '../Null'; +import Unconstructable from '../Unconstructable'; const EventTypes: Record> = {}; diff --git a/packages/types/src/primitive/Vote.spec.ts b/packages/types/src/primitive/Generic/Vote.spec.ts similarity index 99% rename from packages/types/src/primitive/Vote.spec.ts rename to packages/types/src/primitive/Generic/Vote.spec.ts index b3b8650db3..cd5c82d1f4 100644 --- a/packages/types/src/primitive/Vote.spec.ts +++ b/packages/types/src/primitive/Generic/Vote.spec.ts @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import '../injector'; +import '../../injector'; import Vote from './Vote'; diff --git a/packages/types/src/primitive/Vote.ts b/packages/types/src/primitive/Generic/Vote.ts similarity index 84% rename from packages/types/src/primitive/Vote.ts rename to packages/types/src/primitive/Generic/Vote.ts index d73b5af50a..dca9f6e132 100644 --- a/packages/types/src/primitive/Vote.ts +++ b/packages/types/src/primitive/Generic/Vote.ts @@ -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 { Conviction } from '../interfaces/democracy'; +import { Conviction } from '../../interfaces/democracy'; import { isBoolean, isNumber, isObject, isU8a, isUndefined } from '@polkadot/util'; -import createType from '../codec/createType'; -import U8a from '../codec/U8a'; -import Bool from './Bool'; +import createType from '../../codec/createType'; +import U8a from '../../codec/U8a'; +import Bool from '../Bool'; /** * @name Vote @@ -36,7 +36,9 @@ export default class Vote extends U8a { } private static decodeVote (value?: any): Uint8Array { - if (isBoolean(value)) { + if (isUndefined(value)) { + return new Uint8Array([0]); + } else if (isBoolean(value)) { return value ? new Uint8Array([0b10000000]) : new Uint8Array([0b0]); @@ -53,8 +55,10 @@ export default class Vote extends U8a { const result = convictionIndex | (aye.eq(true) ? 0b1 << 7 : 0b0); return new Uint8Array([result]); - } else if (isU8a(value) && value.length > 0) { - return value; + } else if (isU8a(value)) { + return value.length + ? value + : new Uint8Array([0]); } throw new Error(`Unable to convert input ${value} to Vote`); diff --git a/packages/types/src/primitive/Generic/index.ts b/packages/types/src/primitive/Generic/index.ts index 827a8dc1d3..9bb547bf7d 100644 --- a/packages/types/src/primitive/Generic/index.ts +++ b/packages/types/src/primitive/Generic/index.ts @@ -2,6 +2,11 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. +export { default as GenericAccountId } from './AccountId'; +export { default as GenericAccountIndex } from './AccountIndex'; +export { default as GenericAddress } from './Address'; export { default as GenericBlock } from './Block'; export { default as GenericConsensusEngineId } from './ConsensusEngineId'; +export { default as GenericEvent } from './Event'; export { default as GenericDigest, DigestItem as GenericDigestItem } from './Digest'; +export { default as GenericVote } from './Vote'; diff --git a/packages/types/src/primitive/index.ts b/packages/types/src/primitive/index.ts index 23d8a2a165..dd655b8b6a 100644 --- a/packages/types/src/primitive/index.ts +++ b/packages/types/src/primitive/index.ts @@ -10,11 +10,7 @@ */ // NOTE We are exporting Bool as bool to match with Rust (however also using Bool internally), // so with the renamed versions, epose both cases, allowing createType to work -export { default as AccountId } from './AccountId'; -export { default as AccountIndex } from './AccountIndex'; -export { default as Address } from './Address'; -export { default as Event } from './Event'; -export { default as Bool, default as bool } from './Bool'; +export { default as bool } from './Bool'; export { default as Bytes } from './Bytes'; export { default as Data } from './Data'; export * from './Extrinsic'; @@ -44,4 +40,3 @@ export { default as u64, default as U64 } from './U64'; export { default as u128, default as U128 } from './U128'; export { default as u256, default as U256 } from './U256'; export { default as usize } from './USize'; -export { default as Vote } from './Vote'; diff --git a/packages/types/src/types.ts b/packages/types/src/types.ts index 5c3fa8849a..1f030ee5ca 100644 --- a/packages/types/src/types.ts +++ b/packages/types/src/types.ts @@ -10,7 +10,7 @@ import Compact from './codec/Compact'; import U8a from './codec/U8a'; import { FunctionMetadata } from './Metadata/v7/Calls'; import Method from './primitive/Method'; -import Address from './primitive/Address'; +import Address from './primitive/Generic/Address'; // eslint-disable-next-line @typescript-eslint/interface-name-prefix export interface IKeyringPair {