Latest polkadot metadata (#1050)

* Latest polkadot metadata

* The first bunch of Polkadot types... WinningData is an issue...

* VectorFixed, allowing for [<Type>; <length>]

* Add WinningData
This commit is contained in:
Jaco Greeff
2019-06-22 18:28:59 +02:00
committed by GitHub
parent 9d6f9662cc
commit ed19ba91e1
20 changed files with 4050 additions and 14 deletions
+22 -1
View File
@@ -118,7 +118,6 @@ These custom types implement specific types that are found as part of the Substr
| [[NextAuthority]] | The next authority available as [[SessionKey]] |
| [[Nonce]] | The Nonce or number of transactions sent by a specific account |
| [[NonceCompact]] | The Compact<Nonce> or number of transactions sent by a specific account |
| [[ParaId]] | Identifier for a deployed parachain implemented as a [[U32]] |
| [[OpaqueKey]] | A key represented as a [[Bytes]] |
| [[Perbill]] | Parts per billion (see also [[Permill]]) |
| [[Permill]] | Parts per million (See also [[Perbill]]) |
@@ -153,6 +152,28 @@ These custom types implement specific types that are found as part of the Substr
| [[WithdrawReasons]] | The Substrate WithdrawReasons for staking |
## Polkadot types
These types are only available in Polkadot chains -
| **Types** | |
| --- | --- |
| [[AuctionIndex]] | A parachain auction index as a [[U32]] |
| [[Bidder]] | The desired target of a bidder in an auction. |
| [[LeasePeriod]] | The length of the lease for a parachain |
| [[LeasePeriodOf]] | The length of the lease for a parachain |
| [[IncomingParachain]] | Information regarding a parachain that will be deployed. |
| [[NewBidder]] | A bidder identifier, which is just the combination of an account ID and a sub-bidder ID. This is called `NewBidder` in order to distinguish between bidders that would deploy a *new* parachain and pre-existing parachains bidding to renew themselves. |
| [[ParachainDispatchOrigin]] | Which origin a parachain's message to the relay chain should be dispatched from. |
| [[ParaId]] | Identifier for a deployed parachain implemented as a [[U32]] |
| [[ParaIdOf]] | Identifier for a deployed parachain implemented as a [[U32]] |
| [[SlotRange]] | A compactly represented sub-range from the series (0, 1, 2, 3) |
| [[SubId]] | A sub-bidder identifier. Used to distinguish between different logical bidders coming from the same account ID. |
| [[UpwardMessage]] | A message from a parachain to its Relay Chain |
| [[WinningData]] | Winning data type. This encodes the top bidders of each range together with their bid. |
| [[WinningDataentry]] | And entry in the [[WinningData]] fixed vector |
## RPC types
These types are not used in the runtime, but are rather used in RPC results:
@@ -2,14 +2,24 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import latestSubstrate from './latest.substrate.v5.json';
import rpcData from './static';
import polkadotJson from './latest.polkadot.v5.json';
import substrateJson from './latest.substrate.v5.json';
import polkadotData from './static.polkadot';
import substrateData from './static';
import { decodeLatestSubstrate, defaultValues, toV5 } from '../util/testUtil';
describe('MetadataV5', () => {
decodeLatestSubstrate(5, rpcData, latestSubstrate);
describe('MetadataV5 (substrate)', () => {
decodeLatestSubstrate(5, substrateData, substrateJson);
toV5(5, rpcData);
toV5(5, substrateData);
defaultValues(rpcData);
defaultValues(substrateData);
});
describe('MetadataV5 (polkadot)', () => {
decodeLatestSubstrate(5, polkadotData, polkadotJson);
toV5(5, polkadotData);
defaultValues(polkadotData);
});
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
+74
View File
@@ -0,0 +1,74 @@
// 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 { Codec, Constructor } from '../types';
import { assert, isU8a, u8aConcat, compactToU8a } from '@polkadot/util';
import AbstractArray from './AbstractArray';
import Vector from './Vector';
/**
* @name VectorFixed
* @description
* This manages codec arrays of a fixed length
*/
export default class VectorFixed<T extends Codec> extends AbstractArray<T> {
private _Type: Constructor<T>;
constructor (Type: Constructor<T>, length: number, value: VectorFixed<any> | Uint8Array | string | Array<any> = [] as Array<any>) {
super(
...VectorFixed.decodeVectorFixed(Type, length, value)
);
this._Type = Type;
}
static decodeVectorFixed<T extends Codec> (Type: Constructor<T>, allocLength: number, value: VectorFixed<any> | Uint8Array | string | Array<any>): Array<T> {
const values = Vector.decodeVector(
Type,
isU8a(value)
? u8aConcat(compactToU8a(allocLength), value)
: value
);
while (values.length < allocLength) {
values.push(new Type());
}
assert(values.length === allocLength, `Expected a length of exactly ${allocLength} entries`);
return values;
}
static with<O extends Codec> (Type: Constructor<O>, length: number): Constructor<VectorFixed<O>> {
return class extends VectorFixed<O> {
constructor (value?: Array<any>) {
super(Type, length, value);
}
static Fallback = Type.Fallback
? VectorFixed.with(Type.Fallback, length)
: undefined;
};
}
/**
* @description The type for the items
*/
get Type (): string {
return this._Type.name;
}
toU8a (): Uint8Array {
return super.toU8a(true);
}
/**
* @description Returns the base runtime type name for this instance
*/
toRawType (): string {
return `[${new this._Type().toRawType()};${this.length}`;
}
}
@@ -239,6 +239,12 @@ describe('createType', () => {
).toEqual('0x1200230045006700');
});
it('allows creation of a [u16; 4]', () => {
expect(
createType('[u16; 4]', [0x1200, 0x2300, 0x4500, 0x6700]).toU8a()
).toEqual(new Uint8Array([0x00, 0x12, 0x00, 0x23, 0x00, 0x45, 0x00, 0x67]));
});
it('throw error when create base is a StorageData with null value and isPedantic is true', () => {
const base = createType('StorageData', null);
+14 -5
View File
@@ -16,6 +16,7 @@ import Tuple from './Tuple';
import U8aFixed, { BitLength as U8aFixedBitLength } from './U8aFixed';
import UInt from './UInt';
import Vector from './Vector';
import VectorFixed from './VectorFixed';
import getRegistry from './typeRegistry';
export enum TypeDefInfo {
@@ -124,10 +125,10 @@ export function getTypeDef (_type: Text | string, name?: string): TypeDef {
const vecLen = parseInt(_vecLen.trim(), 10);
// as a first round, only u8 via u8aFixed, we can add more support
assert(vecLen <= 256 && ['u8'].includes(vecType), `Only support for [u8; <length>], where length <= 256`);
assert(vecLen <= 256, `Only support for [Type; <length>], where length <= 256`);
value.info = TypeDefInfo.VectorFixed;
value.ext = { length: vecLen, type: 'u8a' } as TypeDefExtVecFixed;
value.ext = { length: vecLen, type: vecType } as TypeDefExtVecFixed;
} else if (startingWith(type, '{', '}')) {
const parsed = JSON.parse(type);
const keys = Object.keys(parsed);
@@ -173,7 +174,7 @@ export function getTypeDef (_type: Text | string, name?: string): TypeDef {
}
// Returns the type Class for construction
export function getTypeClass (value: TypeDef): Constructor {
export function getTypeClass (value: TypeDef, Fallback?: Constructor): Constructor {
const Type = getRegistry().get(value.type);
if (Type) {
@@ -217,7 +218,7 @@ export function getTypeClass (value: TypeDef): Constructor {
assert(Array.isArray(value.sub), 'Expected nested subtypes for Tuple');
return Tuple.with(
(value.sub as Array<TypeDef>).map(getTypeClass)
(value.sub as Array<TypeDef>).map((Type) => getTypeClass(Type))
);
case TypeDefInfo.Vector:
assert(value.sub && !Array.isArray(value.sub), 'Expected subtype for Vector');
@@ -228,7 +229,11 @@ export function getTypeClass (value: TypeDef): Constructor {
case TypeDefInfo.VectorFixed:
assert(value.ext, 'Expected length & type information for fixed vector');
return U8aFixed.with(((value.ext as TypeDefExtVecFixed).length * 8) as U8aFixedBitLength);
const ext = value.ext as TypeDefExtVecFixed;
return ext.type === 'u8'
? U8aFixed.with((ext.length * 8) as U8aFixedBitLength)
: VectorFixed.with(createClass(ext.type), ext.length);
case TypeDefInfo.Linkage:
assert(value.sub && !Array.isArray(value.sub), 'Expected subtype for Linkage');
@@ -243,6 +248,10 @@ export function getTypeClass (value: TypeDef): Constructor {
return Null;
}
if (Fallback) {
return Fallback;
}
throw new Error(`Unable to determine type from '${value.type}'`);
}
+2 -1
View File
@@ -14,6 +14,7 @@ import Metadata from '../Metadata';
import { EventMetadata as EventMetadataV5 } from '../Metadata/v5/Events';
import Null from './Null';
import U32 from './U32';
import Unconstructable from './Unconstructable';
const EventTypes: { [index: string]: Constructor<EventData> } = {};
@@ -137,7 +138,7 @@ export default class Event extends Struct {
const methodName = meta.name.toString();
const eventIndex = new Uint8Array([sectionIndex, methodIndex]);
const typeDef = meta.args.map((arg) => getTypeDef(arg));
const Types = typeDef.map(getTypeClass);
const Types = typeDef.map((typeDef) => getTypeClass(typeDef, Unconstructable.with(typeDef)));
EventTypes[eventIndex.toString()] = class extends EventData {
constructor (value: Uint8Array) {
+2
View File
@@ -63,6 +63,8 @@ export default class Type extends Text {
Type._alias('Lookup::Source', 'Address'),
// alias Lookup::Target to AccountId (always the case)
Type._alias('Lookup::Target', 'AccountId'),
// alias for grandpa, as used in polkador
Type._alias('grandpa::AuthorityId', 'AuthorityId'),
// HACK duplication between contracts & primitives, however contracts prefixed with exec
Type._alias('exec::StorageKey', 'ContractStorageKey'),
// flattens tuples with one value, `(AccountId)` -> `AccountId`
@@ -0,0 +1,30 @@
// 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 { Constructor } from '../types';
import Null from './Null';
/**
* @name Unconstructable
* @description
* A type that should not be constructed
*/
export default class Unconstructable extends Null {
constructor () {
super();
throw new Error('Unconstructable should not be constructed, it is only a placeholder for compatibility');
}
static with (typeDef: any): Constructor {
return class extends Null {
constructor () {
super();
throw new Error(`Unabdle to construct ${JSON.stringify(typeDef)}`);
}
};
}
}
+13
View File
@@ -0,0 +1,13 @@
// 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 U32 from '../primitive/U32';
/**
* @name AuctionIndex
* @description
* An auction index. We count auctions in this type.
*/
export default class AuctionIndex extends U32 {
}
+37
View File
@@ -0,0 +1,37 @@
// 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 Enum from '../codec/Enum';
import Struct from '../codec/Struct';
import AccountId from '../primitive/AccountId';
import ParaId from './ParaId';
import SubId from './SubId';
/**
* @name NewBidder
* @description
* A bidder identifier, which is just the combination of an account ID and a sub-bidder ID. This is called `NewBidder` in order to distinguish between bidders that would deploy a *new* parachain and pre-existing parachains bidding to renew themselves.
*/
export class NewBidder extends Struct {
constructor (value?: any) {
super({
who: AccountId,
sub: SubId
}, value);
}
}
/**
* @name Bidder
* @description
* The desired target of a bidder in an auction
*/
export default class Bidder extends Enum {
constructor (value?: any) {
super({
New: NewBidder,
Existing: ParaId
}, value);
}
}
@@ -0,0 +1,24 @@
// 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 Enum from '../codec/Enum';
import Struct from '../codec/Struct';
import Bytes from '../primitive/Bytes';
import Hash from '../primitive/Hash';
import { NewBidder } from './Bidder';
/**
* @name IncomingParachain
* @description
* Information regarding a parachain that will be deployed.
*/
export default class Bidder extends Enum {
constructor (value?: any) {
super({
Unset: NewBidder,
Fixed: Struct.with({ codeHash: Hash, initialHeadData: Bytes }),
Deploy: Struct.with({ code: Bytes, initialHeadData: Bytes })
}, value);
}
}
+21
View File
@@ -0,0 +1,21 @@
// 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 BlockNumber from './BlockNumber';
/**
* @name LeasePeriod
* @description
* The length of the parachain lease
*/
export default class LeasePeriod extends BlockNumber {
}
/**
* @name LeasePeriodOf
* @description
* The length of the parachain lease
*/
export class LeasePeriodOf extends LeasePeriod {
}
+8
View File
@@ -11,3 +11,11 @@ import U32 from '../primitive/U32';
*/
export default class ParaId extends U32 {
}
/**
* @name ParaIdOf
* @description
* Identifier for a deployed parachain implemented as a [[U32]]
*/
export class ParaIdOf extends ParaId {
}
+37
View File
@@ -0,0 +1,37 @@
// 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 Enum from '../codec/Enum';
/**
* @name SlotRange
* @description
* A compactly represented sub-range from the series (0, 1, 2, 3).
*/
export default class SlotRange extends Enum {
constructor (value?: any) {
super([
/// Sub range from index 0 to index 0 inclusive.
'ZeroZero', // 0
/// Sub range from index 0 to index 1 inclusive.
'ZeroOne', // 1,
/// Sub range from index 0 to index 2 inclusive.
'ZeroTwo', // 2,
/// Sub range from index 0 to index 3 inclusive.
'ZeroThree', // 3,
/// Sub range from index 1 to index 1 inclusive.
'OneOne', // 4,
/// Sub range from index 1 to index 2 inclusive.
'OneTwo', // 5,
/// Sub range from index 1 to index 3 inclusive.
'OneThree', // 6,
/// Sub range from index 2 to index 2 inclusive.
'TwoTwo', // 7,
/// Sub range from index 2 to index 3 inclusive.
'TwoThree', // 8,
/// Sub range from index 3 to index 3 inclusive.
'ThreeThree' // 9, // == SLOT_RANGE_COUNT - 1
], value);
}
}
+13
View File
@@ -0,0 +1,13 @@
// 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 U32 from '../primitive/U32';
/**
* @name SubId
* @description
* An auction index. We count auctions in this type.
*/
export default class SubId extends U32 {
}
+48
View File
@@ -0,0 +1,48 @@
// 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 Enum from '../codec/Enum';
import Struct from '../codec/Struct';
import Bytes from '../primitive/Bytes';
export class ParachainDispatchOrigin extends Enum {
constructor (value?: any) {
super([
// As a simple `Origin::Signed`, using `ParaId::account_id` as its value. This is good when
// interacting with standard modules such as `balances`.
'Signed',
// As the special `Origin::Parachain(ParaId)`. This is good when interacting with parachain-
// aware modules which need to succinctly verify that the origin is a parachain.
'Parachain'
], value);
}
}
/**
* @name UpwardMessage
* @description
* A message from a parachain to its Relay Chain
*/
export default class UpwardMessage extends Struct {
constructor (value?: any) {
super({
origin: ParachainDispatchOrigin,
data: Bytes
}, value);
}
/**
* @description The total balance backing this validator
*/
get data (): Bytes {
return this.get('data') as Bytes;
}
/**
* @description The validator's own stash that is exposed
*/
get origin (): ParachainDispatchOrigin {
return this.get('origin') as ParachainDispatchOrigin;
}
}
+24
View File
@@ -0,0 +1,24 @@
// 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 Tuple from '../codec/Tuple';
import VectorFixed from '../codec/VectorFixed';
import AccountId from '../primitive/AccountId';
import { BalanceOf } from './Balance';
import { ParaIdOf } from './ParaId';
const SLOT_RANGE_COUNT = 10;
export class WinningDataEntry extends Tuple {
constructor (value?: any) {
super([AccountId, ParaIdOf, BalanceOf], value);
}
}
/**
* @name WinningData
* @description
* Winning data type. This encodes the top bidders of each range together with their bid.
*/
export default class WinningData extends VectorFixed.with(WinningDataEntry, SLOT_RANGE_COUNT) {
}
+9 -1
View File
@@ -12,12 +12,14 @@ export { default as Amount } from './Amount';
export { default as ApprovalFlag } from './ApprovalFlag';
export { default as AssetOf } from './AssetOf';
export { default as AttestedCandidate } from './AttestedCandidate';
export { default as AuctionIndex } from './AuctionIndex';
export { default as AuthorityId } from './AuthorityId';
export { default as AuthorityWeight } from './AuthorityWeight';
export { default as Balance, BalanceOf } from './Balance';
export { default as BalanceLock } from './BalanceLock';
// NOTE Bft items are only used in internal structures
// export * from './Bft;
export { default as Bidder, NewBidder } from './Bidder';
export { default as BlockNumber } from './BlockNumber';
export { default as CodeHash } from './CodeHash';
export { default as Conviction } from './Conviction';
@@ -31,11 +33,13 @@ export { default as ExtrinsicSignature } from './ExtrinsicSignature';
// NOTE Only used internally, exported as PendingExtrinsics
// export { default as Extrinsics } from './Extrinsics';
export { default as Gas } from './Gas';
export { default as IncomingParachain } from './IncomingParachain';
export { default as IndividualExposure } from './IndividualExposure';
export { default as InherentOfflineReport } from './InherentOfflineReport';
export { default as Key } from './Key';
export { default as Keys } from './Keys';
export { default as KeyValue } from './KeyValue';
export { default as LeasePeriod, LeasePeriodOf } from './LeasePeriod';
export { default as LockIdentifier } from './LockIdentifier';
export { default as LockPeriods } from './LockPeriods';
// NOTE Nonce is renamed to Index
@@ -46,7 +50,7 @@ export { default as MemberCount } from './MemberCount';
export { default as MisbehaviorReport } from './MisbehaviorReport';
export { default as NewAccountOutcome } from './NewAccountOutcome';
export { default as OpaqueKey } from './OpaqueKey';
export { default as ParaId } from './ParaId';
export { default as ParaId, ParaIdOf } from './ParaId';
export { default as Permill } from './Permill';
export { default as Perbill } from './Perbill';
export { default as PrefabWasmModule } from './PrefabWasmModule';
@@ -64,10 +68,13 @@ export { default as SessionKeys } from './SessionKeys';
export { default as SetIndex } from './SetIndex';
export { default as Signature, Ed25519Signature, Sr25519Signature } from './Signature';
export { default as SignaturePayload, SignaturePayloadRaw } from './SignaturePayload';
export { default as SlotRange } from './SlotRange';
export { default as StakingLedger } from './StakingLedger';
export { default as StoredPendingChange } from './StoredPendingChange';
export { default as SubId } from './SubId';
export { default as TreasuryProposal } from './TreasuryProposal';
export { default as UnlockChunk } from './UnlockChunk';
export { default as UpwardMessage, ParachainDispatchOrigin } from './UpwardMessage';
export { default as ValidatorPrefs } from './ValidatorPrefs';
export { default as VestingSchedule } from './VestingSchedule';
export { default as Vote } from './Vote';
@@ -75,4 +82,5 @@ export { default as Votes } from './Votes';
export { default as VoteThreshold } from './VoteThreshold';
export { default as VoteIndex } from './VoteIndex';
export { default as VoterInfo } from './VoterInfo';
export { default as WinningData, WinningDataEntry } from './WinningData';
export { default as WithdrawReasons } from './WithdrawReasons';