Add types from latest metadata (#466)

* Update to latest metadata

* Remove whitespaces

* Fix ::Proposal

* Add missing types

* Allow Option<> in metadata

* Empty typle

* Remove useless indenetation

* getTypeClass add Option

* Add test with Option
This commit is contained in:
Amaury Martiny
2018-12-13 17:20:45 +01:00
committed by GitHub
parent 25b1e9bccd
commit 04c639ded9
10 changed files with 193 additions and 25 deletions
@@ -0,0 +1,13 @@
// Copyright 2017-2018 @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';
/**
* @name InherentOfflineReport
* @description
* Describes the offline-reporting extrinsic
*/
export default class InherentOfflineReport extends Tuple.with([]) {
}
+1
View File
@@ -137,6 +137,7 @@ export default class Metadata extends Struct {
return decoded.type; return decoded.type;
case TypeDefInfo.Compact: case TypeDefInfo.Compact:
case TypeDefInfo.Option:
case TypeDefInfo.Vector: case TypeDefInfo.Vector:
return extractTypes([(decoded.sub as TypeDef).type]); return extractTypes([(decoded.sub as TypeDef).type]);
+79 -11
View File
@@ -298,7 +298,7 @@
{ {
"name": "NewAuthorities", "name": "NewAuthorities",
"arguments": [ "arguments": [
"Vec<(SessionKey, u64)>" "Vec<(SessionKey,u64)>"
], ],
"documentation": [ "documentation": [
" New authority set has been applied." " New authority set has been applied."
@@ -494,6 +494,17 @@
] ]
} }
}, },
{
"prefix": "aura",
"module": {
"name": "Module",
"call": {
"name": "Call",
"functions": []
}
},
"storage": null
},
{ {
"prefix": "timestamp", "prefix": "timestamp",
"module": { "module": {
@@ -579,14 +590,12 @@
"name": "note_offline", "name": "note_offline",
"arguments": [ "arguments": [
{ {
"name": "offline_val_indices", "name": "offline",
"type": "Vec<u32>" "type": "InherentOfflineReport"
} }
], ],
"documentation": [ "documentation": [
" Note the previous block's validator missed their opportunity to propose a block.", " Note the previous block's validator missed their opportunity to propose a block."
" This only comes in if 2/3+1 of the validators agree that no proposal was submitted.",
" It's only relevant for the previous block."
] ]
}, },
{ {
@@ -1251,7 +1260,7 @@
{ {
"name": "StakeRange", "name": "StakeRange",
"modifier": 1, "modifier": 1,
"type": "(Balance, Balance)", "type": "(Balance,Balance)",
"documentation": [ "documentation": [
" The highest and lowest staked validator slashable balances." " The highest and lowest staked validator slashable balances."
] ]
@@ -1353,8 +1362,12 @@
"type": "Proposal" "type": "Proposal"
}, },
{ {
"name": "vote_threshold", "name": "threshold",
"type": "VoteThreshold" "type": "VoteThreshold"
},
{
"name": "delay",
"type": "BlockNumber"
} }
], ],
"documentation": [ "documentation": [
@@ -1373,6 +1386,23 @@
"documentation": [ "documentation": [
" Remove a referendum." " Remove a referendum."
] ]
},
{
"id": 5,
"name": "cancel_queued",
"arguments": [
{
"name": "when",
"type": "BlockNumber"
},
{
"name": "which",
"type": "u32"
}
],
"documentation": [
" Cancel a proposal queued for enactment."
]
} }
] ]
} }
@@ -1423,6 +1453,14 @@
" The minimum amount to be used as a deposit for a public referendum proposal." " The minimum amount to be used as a deposit for a public referendum proposal."
] ]
}, },
{
"name": "PublicDelay",
"modifier": 1,
"type": "BlockNumber",
"documentation": [
" The delay before enactment for all public referenda."
]
},
{ {
"name": "VotingPeriod", "name": "VotingPeriod",
"modifier": 1, "modifier": 1,
@@ -1452,12 +1490,34 @@
"modifier": 0, "modifier": 0,
"type": { "type": {
"key": "ReferendumIndex", "key": "ReferendumIndex",
"value": "(BlockNumber,Proposal,VoteThreshold)" "value": "(ReferendumInfo)"
}, },
"documentation": [ "documentation": [
" Information concerning any given referendum." " Information concerning any given referendum."
] ]
}, },
{
"name": "DispatchQueue",
"modifier": 1,
"type": {
"key": "BlockNumber",
"value": "Vec<Option<(Proposal,ReferendumIndex)>>"
},
"documentation": [
" Queue of successful referenda to be dispatched."
]
},
{
"name": "Bondage",
"modifier": 1,
"type": {
"key": "AccountId",
"value": "BlockNumber"
},
"documentation": [
" The block at which the `who`'s funds become liquid."
]
},
{ {
"name": "VotersFor", "name": "VotersFor",
"modifier": 1, "modifier": 1,
@@ -1473,7 +1533,7 @@
"name": "VoteOf", "name": "VoteOf",
"modifier": 0, "modifier": 0,
"type": { "type": {
"key": "( ReferendumIndex , AccountId )", "key": "(ReferendumIndex,AccountId)",
"value": "bool" "value": "bool"
}, },
"documentation": [ "documentation": [
@@ -1909,6 +1969,14 @@
"type": "BlockNumber", "type": "BlockNumber",
"documentation": [] "documentation": []
}, },
{
"name": "EnactDelayPeriod",
"modifier": 1,
"type": "BlockNumber",
"documentation": [
" Number of blocks by which to delay enactment of successful, non-unanimous-council-instigated referendum proposals."
]
},
{ {
"name": "Proposals", "name": "Proposals",
"modifier": 1, "modifier": 1,
@@ -1937,7 +2005,7 @@
"name": "CouncilVoteOf", "name": "CouncilVoteOf",
"modifier": 0, "modifier": 0,
"type": { "type": {
"key": "( Hash , AccountId )", "key": "(Hash,AccountId)",
"value": "bool" "value": "bool"
}, },
"documentation": [] "documentation": []
+59
View File
@@ -0,0 +1,59 @@
// Copyright 2017-2018 @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';
import Struct from './codec/Struct';
import Proposal from './Proposal';
import VoteThreshold from './VoteThreshold';
type ReferendumInfoValue = {
end?: BlockNumber,
proposal?: Proposal,
threshold?: VoteThreshold,
delay?: BlockNumber
};
/**
* @name ReferendumInfo
* @description
* Info regarding an ongoing referendum
*/
export default class ReferendumInfo extends Struct {
constructor (value?: ReferendumInfoValue | Uint8Array) {
super({
end: BlockNumber,
proposal: Proposal,
threshold: VoteThreshold,
delay: BlockNumber
}, value);
}
/**
* @description When voting on this referendum will end
*/
get end (): BlockNumber {
return this.get('end') as BlockNumber;
}
/**
* @description The proposal being voted on
*/
get proposal (): Proposal {
return this.get('proposal') as Proposal;
}
/**
* @description The thresholding mechanism to determine whether it passed
*/
get threshold (): VoteThreshold {
return this.get('threshold') as VoteThreshold;
}
/**
* @description The delay (in blocks) to wait after a successful referendum before deploying
*/
get delay (): BlockNumber {
return this.get('delay') as BlockNumber;
}
}
+10 -4
View File
@@ -21,13 +21,13 @@ describe('Type', () => {
it('handles nested types (embedded)', () => { it('handles nested types (embedded)', () => {
expect( expect(
new Type('(u32, Box<Vec<AccountId>>)').toString() new Type('(u32, Box<Vec<AccountId>>)').toString()
).toEqual('(u32, Vec<AccountId>)'); ).toEqual('(u32,Vec<AccountId>)');
}); });
it('handles aliasses, multiples per line', () => { it('handles aliasses, multiples per line', () => {
expect( expect(
new Type('(Vec<u8>, AccountId, Vec<u8>)').toString() new Type('(Vec<u8>, AccountId, Vec<u8>)').toString()
).toEqual('(Bytes, AccountId, Bytes)'); ).toEqual('(Bytes,AccountId,Bytes)');
}); });
it('removes whitespaces', () => { it('removes whitespaces', () => {
@@ -39,13 +39,13 @@ describe('Type', () => {
it('changes PairOf<T> -> (T, T)', () => { it('changes PairOf<T> -> (T, T)', () => {
expect( expect(
new Type('PairOf<T::Balance>').toString() new Type('PairOf<T::Balance>').toString()
).toEqual('(Balance, Balance)'); ).toEqual('(Balance,Balance)');
}); });
it('changes PairOf<T> (embedded) -> (T, T)', () => { it('changes PairOf<T> (embedded) -> (T, T)', () => {
expect( expect(
new Type('(Vec<u8>, PairOf<T::Balance>, Vec<AccountId>)').toString() new Type('(Vec<u8>, PairOf<T::Balance>, Vec<AccountId>)').toString()
).toEqual('(Bytes, (Balance, Balance), Vec<AccountId>)'); ).toEqual('(Bytes,(Balance,Balance),Vec<AccountId>)');
}); });
it('does not allow toU8a', () => { it('does not allow toU8a', () => {
@@ -67,4 +67,10 @@ describe('Type', () => {
new Type('<T::Balance as HasCompact>::Type').toString() new Type('<T::Balance as HasCompact>::Type').toString()
).toEqual('Compact<Balance>'); ).toEqual('Compact<Balance>');
}); });
it('handles InherentOfflineReport', () => {
expect(
new Type('<T::InherentOfflineReport as InherentOfflineReport>::Inherent').toString()
).toEqual('InherentOfflineReport');
});
}); });
+6 -6
View File
@@ -36,6 +36,8 @@ export default class Type extends Text {
private static decodeType (value: string): string { private static decodeType (value: string): string {
const mappings: Array<Mapper> = [ const mappings: Array<Mapper> = [
// alias <T::InherentOfflineReport as InherentOfflineReport>::Inherent -> InherentOfflineReport
Type._alias('<T::InherentOfflineReport as InherentOfflineReport>::Inherent', 'InherentOfflineReport'),
// <T::Balance as HasCompact> // <T::Balance as HasCompact>
Type._cleanupCompact(), Type._cleanupCompact(),
// Remove all the trait prefixes // Remove all the trait prefixes
@@ -160,7 +162,7 @@ export default class Type extends Text {
const end = Type._findClosing(value, start); const end = Type._findClosing(value, start);
const type = value.substr(start, end - start); const type = value.substr(start, end - start);
value = `${value.substr(0, index)}(${type}, ${type})${value.substr(end + 1)}`; value = `${value.substr(0, index)}(${type},${type})${value.substr(end + 1)}`;
} }
} }
@@ -172,15 +174,13 @@ export default class Type extends Text {
private static _removeTraits (): Mapper { private static _removeTraits (): Mapper {
return (value: string): string => { return (value: string): string => {
return value return value
// `T :: AccountId` -> `T::AccountId` https://github.com/paritytech/substrate/issues/1244 // remove all whitespaces
.replace(/\s+::\s+/g, '::') .replace(/\s/g, '')
// anything `T::<type>` to end up as `<type>` // anything `T::<type>` to end up as `<type>`
.replace(/T::/g, '') .replace(/T::/g, '')
// `system::` with `` - basically we find `<T as system::Trait>` // `system::` with `` - basically we find `<T as system::Trait>`
.replace(/system::/g, '') .replace(/system::/g, '')
// replace `<T as Trait>::` (possibly sanitised just above) // replace `<T as Trait>::` (whitespaces were removed above)
.replace(/<T as Trait>::/g, '')
// `<TasTrait>::type` -> `type` https://github.com/paritytech/substrate/issues/1244
.replace(/<TasTrait>::/g, '') .replace(/<TasTrait>::/g, '')
// replace `<...>::Type` // replace `<...>::Type`
.replace(/::Type/g, ''); .replace(/::Type/g, '');
+10 -2
View File
@@ -63,10 +63,10 @@ describe('getTypeValue', () => {
it('returns a type structure', () => { it('returns a type structure', () => {
expect( expect(
getTypeDef('(u32, Compact<u32>, Vec<u64>, (Text, Vec<(Bool, u128)>))') getTypeDef('(u32, Compact<u32>, Vec<u64>, Option<u128>, (Text, Vec<(Bool, u128)>))')
).toEqual({ ).toEqual({
info: TypeDefInfo.Tuple, info: TypeDefInfo.Tuple,
type: '(u32, Compact<u32>, Vec<u64>, (Text, Vec<(Bool, u128)>))', type: '(u32, Compact<u32>, Vec<u64>, Option<u128>, (Text, Vec<(Bool, u128)>))',
sub: [ sub: [
{ {
info: TypeDefInfo.Plain, info: TypeDefInfo.Plain,
@@ -88,6 +88,14 @@ describe('getTypeValue', () => {
type: 'u64' type: 'u64'
} }
}, },
{
info: TypeDefInfo.Option,
type: 'Option<u128>',
sub: {
info: TypeDefInfo.Plain,
type: 'u128'
}
},
{ {
info: TypeDefInfo.Tuple, info: TypeDefInfo.Tuple,
type: '(Text, Vec<(Bool, u128)>)', type: '(Text, Vec<(Bool, u128)>)',
+11
View File
@@ -7,6 +7,7 @@ import { assert } from '@polkadot/util';
import { Codec, Constructor } from '../types'; import { Codec, Constructor } from '../types';
import Text from '../Text'; import Text from '../Text';
import Compact from './Compact'; import Compact from './Compact';
import Option from './Option';
import Tuple from './Tuple'; import Tuple from './Tuple';
import UInt from './UInt'; import UInt from './UInt';
import Vector from './Vector'; import Vector from './Vector';
@@ -14,6 +15,7 @@ import registry from './typeRegistry';
export enum TypeDefInfo { export enum TypeDefInfo {
Compact, Compact,
Option,
Plain, Plain,
Tuple, Tuple,
Vector Vector
@@ -94,6 +96,9 @@ export function getTypeDef (_type: Text | string): TypeDef {
} else if (startingWith(type, 'Compact<', '>')) { } else if (startingWith(type, 'Compact<', '>')) {
value.info = TypeDefInfo.Compact; value.info = TypeDefInfo.Compact;
value.sub = getTypeDef(subType); value.sub = getTypeDef(subType);
} else if (startingWith(type, 'Option<', '>')) {
value.info = TypeDefInfo.Option;
value.sub = getTypeDef(subType);
} else if (startingWith(type, 'Vec<', '>')) { } else if (startingWith(type, 'Vec<', '>')) {
value.info = TypeDefInfo.Vector; value.info = TypeDefInfo.Vector;
value.sub = getTypeDef(subType); value.sub = getTypeDef(subType);
@@ -116,6 +121,12 @@ export function getTypeClass (value: TypeDef): Constructor {
return Compact.with( return Compact.with(
getTypeClass(value.sub as TypeDef) as Constructor<UInt> getTypeClass(value.sub as TypeDef) as Constructor<UInt>
); );
} else if (value.info === TypeDefInfo.Option) {
assert(value.sub && !Array.isArray(value.sub), 'Expected subtype for Option');
return Option.with(
getTypeClass(value.sub as TypeDef)
);
} else if (value.info === TypeDefInfo.Vector) { } else if (value.info === TypeDefInfo.Vector) {
assert(value.sub && !Array.isArray(value.sub), 'Expected subtype for Vector'); assert(value.sub && !Array.isArray(value.sub), 'Expected subtype for Vector');
+2
View File
@@ -42,6 +42,7 @@ export { default as Gas } from './Gas';
// export { default as H512 } from './H512'; // export { default as H512 } from './H512';
export { default as Hash } from './Hash'; export { default as Hash } from './Hash';
export { default as Header } from './Header'; export { default as Header } from './Header';
export { default as InherentOfflineReport } from './InherentOfflineReport';
export { default as KeyValue } from './KeyValue'; export { default as KeyValue } from './KeyValue';
export { default as Metadata } from './Metadata'; export { default as Metadata } from './Metadata';
export { default as Method } from './Method'; export { default as Method } from './Method';
@@ -61,6 +62,7 @@ export { default as PropIndex } from './PropIndex';
export { default as Proposal } from './Proposal'; export { default as Proposal } from './Proposal';
export { default as ProposalIndex } from './ProposalIndex'; export { default as ProposalIndex } from './ProposalIndex';
export { default as ReferendumIndex } from './ReferendumIndex'; export { default as ReferendumIndex } from './ReferendumIndex';
export { default as ReferendumInfo } from './ReferendumInfo';
export { default as RuntimeVersion } from './RuntimeVersion'; export { default as RuntimeVersion } from './RuntimeVersion';
export { default as SessionKey } from './SessionKey'; export { default as SessionKey } from './SessionKey';
export { default as Signature } from './Signature'; export { default as Signature } from './Signature';
File diff suppressed because one or more lines are too long