Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/types/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ These primitive types are available:
| [[U128]] | A 128-bit unsigned integer |
| [[U256]] | A 256-bit unsigned integer |
| [[USize]] | A System default unsigned number, typically used in RPC to report non-consensus data |
| [[Weight]] | Numeric range of a transaction weight. |
| [[WeightMultiplier]] | Representation of a weight multiplier. This represents how a fee value can be computed from a weighted transaction. |


## Substrate types
Expand Down Expand Up @@ -102,6 +104,7 @@ These custom types implement specific types that are found as part of the Substr
| [[Extrinsics]] | A [[Vector]] of [[Extrinsic]] |
| [[ExtrinsicSignature]] | A container for the [[Signature]] associated with a specific [[Extrinsic]] |
| [[Gas]] | A gas number type for Substrate, extending [[U64]] |
| [[Heartbeat]] | Heartbeat which is send/received. |
| [[IndividualExposure]] | The Substrate IndividualExposure for staking |
| [[InherentOfflineReport]] | Describes the offline-reporting extrinsic |
| [[Justification]] | A generic justification as a stream of [[Bytes]], this is specific per consensus implementation |
Expand Down Expand Up @@ -139,6 +142,7 @@ These custom types implement specific types that are found as part of the Substr
| [[SignaturePayloadRaw]] | A version of the [[SignaturePayload]] where it doesn't rely on [[Method]] with metadata, rather it treats the values as a raw byte stream |
| [[StakingLedger]] | The ledger of a (bonded) stash |
| [[StoredPendingChange]] | Stored pending change for a Grandpa events |
| [[StoredState]] | Current state of the GRANDPA authority set. State transitions must happen in the same order of states defined below, e.g. `Paused` implies a prior `PendingPause` |
| [[TreasuryProposal]] | A Proposal made for Treasury |
| [[UncleEntryItem]] | Information about an uncle to include |
| [[UnlockChunk]] | Just a Balance/BlockNumber tuple to encode when a chunk of funds will be unlocked |
Expand Down
145 changes: 126 additions & 19 deletions packages/types/src/Metadata/v6/latest.substrate.v6.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,36 @@
"name": "AllExtrinsicsWeight",
"modifier": "Optional",
"type": {
"PlainType": "u32"
"PlainType": "Weight"
Copy link
Member Author

@jacogr jacogr Jul 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to create some issues down the line as soon as Babe comes online. Babe also has weights, defined as u64, but the above is u32

},
"fallback": "0x00",
"documentation": [
" Total weight for all extrinsics put together, for the current block."
]
},
{
"name": "AllExtrinsicsLen",
"modifier": "Optional",
"type": {
"PlainType": "u32"
},
"fallback": "0x00",
"documentation": [
" Total length (in bytes) for all extrinsics put together, for the current block."
]
},
{
"name": "NextWeightMultiplier",
"modifier": "Default",
"type": {
"PlainType": "WeightMultiplier"
},
"fallback": "0x0000000000000000",
"documentation": [
" The next weight multiplier. This should be updated at the end of each block based on the",
" saturation level (weight)."
]
},
{
"name": "BlockHash",
"modifier": "Default",
Expand Down Expand Up @@ -2224,15 +2247,6 @@
}
],
"events": [
{
"name": "_Phantom",
"args": [
"Phantom"
],
"documentation": [
" Dummy to manage the fact we have instancing."
]
},
{
"name": "Proposed",
"args": [
Expand Down Expand Up @@ -2445,15 +2459,6 @@
}
],
"events": [
{
"name": "_Phantom",
"args": [
"Phantom"
],
"documentation": [
" Dummy to manage the fact we have instancing."
]
},
{
"name": "Proposed",
"args": [
Expand Down Expand Up @@ -3142,6 +3147,17 @@
" The current authority set."
]
},
{
"name": "State",
"modifier": "Default",
"type": {
"PlainType": "StoredState"
},
"fallback": "0x00",
"documentation": [
" State of the current authority set."
]
},
{
"name": "PendingChange",
"modifier": "Optional",
Expand Down Expand Up @@ -3199,6 +3215,20 @@
"documentation": [
" New authority set has been applied."
]
},
{
"name": "Paused",
"args": [],
"documentation": [
" Current authority set has been paused."
]
},
{
"name": "Resumed",
"args": [],
"documentation": [
" Current authority set has been resumed."
]
}
],
"constants": []
Expand Down Expand Up @@ -3924,6 +3954,83 @@
}
],
"constants": []
},
{
"name": "im_online",
"prefix": "ImOnline",
"storage": [
{
"name": "GossipAt",
"modifier": "Default",
"type": {
"PlainType": "BlockNumber"
},
"fallback": "0x0000000000000000",
"documentation": []
},
{
"name": "LastNewEraStart",
"modifier": "Optional",
"type": {
"PlainType": "SessionIndex"
},
"fallback": "0x00",
"documentation": []
},
{
"name": "ReceivedHeartbeats",
"modifier": "Default",
"type": {
"DoubleMapType": {
"hasher": "Blake2_256",
"key1": "SessionIndex",
"key2": "AuthorityId",
"value": "Bytes",
"key2Hasher": "Blake2_256"
}
},
"fallback": "0x00",
"documentation": []
}
],
"calls": [
{
"name": "heartbeat",
"args": [
{
"name": "heartbeat",
"type": "Heartbeat"
},
{
"name": "_signature",
"type": "Bytes"
}
],
"documentation": []
}
],
"events": [
{
"name": "HeartbeatReceived",
"args": [
"BlockNumber",
"AuthorityId"
],
"documentation": [
" A new heartbeat was received at this `BlockNumber` from `AuthorityId`"
]
}
],
"constants": [
{
"name": "SessionsPerEra",
"type": "SessionIndex",
"value": "0x06000000",
"documentation": [
" Number of sessions per era."
]
}
]
}
]
}
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/Metadata/v6/static.ts

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -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 U64 from '../primitive/U64';
import I64 from './I64';

/**
* @name AuthorityWeight
* @name I64Fixed
* @description
* The weight of an authority
* A 64-bit signed integer (Fixed64 in Rust)
*/
export default class AuthorityWeight extends U64 {
export default class I64Fixed extends I64 {
}
4 changes: 3 additions & 1 deletion packages/types/src/primitive/Type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ 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
// alias for grandpa, as used in polkadot
Type._alias('grandpa::AuthorityId', 'AuthorityId'),
// specific for SessionIndex (cvould make this session::, but be conservative)
Type._alias('session::SessionIndex', 'SessionIndex'),
// HACK duplication between contracts & primitives, however contracts prefixed with exec
Type._alias('exec::StorageKey', 'ContractStorageKey'),
// flattens tuples with one value, `(AccountId)` -> `AccountId`
Expand Down
13 changes: 13 additions & 0 deletions packages/types/src/primitive/Weight.ts
Original file line number Diff line number Diff line change
@@ -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 './U32';

/**
* @name Weight
* @description
* Numeric range of a transaction weight.
*/
export default class Weight extends U32 {
}
13 changes: 13 additions & 0 deletions packages/types/src/primitive/WeightMultiplier.ts
Original file line number Diff line number Diff line change
@@ -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 Fixed64 from '../primitive/I64Fixed';

/**
* @name WeightMultiplier
* @description
* Representation of a weight multiplier. This represents how a fee value can be computed from a weighted transaction, extending [[Fixed64]]
*/
export default class WeightMultiplier extends Fixed64 {
}
3 changes: 3 additions & 0 deletions packages/types/src/primitive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export { default as I8, default as i8 } from './I8';
export { default as I16, default as i16 } from './I16';
export { default as I32, default as i32 } from './I32';
export { default as I64, default as i64 } from './I64';
export { default as Fixed64 } from './I64Fixed';
export { default as I128, default as i128 } from './I128';
export { default as I256, default as i256 } from './I256';
export { default as Method } from './Method';
Expand All @@ -49,3 +50,5 @@ 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, default as usize } from './USize';
export { default as Weight } from './Weight';
export { default as WeightMultiplier } from './WeightMultiplier';
37 changes: 25 additions & 12 deletions packages/types/src/scripts/srmlTS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,30 @@ import fs from 'fs';
import { isString } from '@polkadot/util';

import { getTypeDef, TypeDef, TypeDefInfo, TypeDefExtVecFixed } from '../codec/createType';
import * as primitives from '../primitive';
import * as substrate from '../type';
import * as definitions from '../srml/definitions';

// these map all the codec and primitive types for import, see the TypeImports below. If
// we have an unseen type, it is `undefined`/`false`, if we need to import it, it is `true`
interface TypeExist {
[index: string]: boolean;
}
type TypeExist = Record<string, boolean>;
interface TypeImports {
codecTypes: TypeExist;
otherTypes: TypeExist;
ownTypes: string[];
primitiveTypes: TypeExist;
substrateTypes: TypeExist;
}

const HEADER = '/* eslint-disable @typescript-eslint/no-empty-interface */\n// Auto-generated, do not edit\n\n';
const FOOTER = '\n';

function setImports ({ codecTypes, otherTypes, ownTypes }: TypeImports, type: string | null, codecType: string | null): void {
if (type && !ownTypes.includes(type) && !otherTypes[type]) {
otherTypes[type] = true;
function setImports ({ codecTypes, ownTypes, primitiveTypes, substrateTypes }: TypeImports, type: string | null, codecType: string | null): void {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make is slightly more clever - it can now import from types/type in addition to types/primitive

if (type && !ownTypes.includes(type)) {
if ((primitives as any)[type]) {
primitiveTypes[type] = true;
} else if ((substrate as any)[type]) {
substrateTypes[type] = true;
}
}

if (codecType) {
Expand Down Expand Up @@ -63,14 +68,16 @@ function tsPlain ({ name: plainName, type }: TypeDef, imports: TypeImports): str
return `export interface ${plainName} extends ${type} {}`;
}

function _tsStructGetterType (structName: string | undefined, { info, sub, type }: TypeDef): [string, string] {
function _tsStructGetterType (structName: string | undefined, { info, sub, type }: TypeDef, imports: TypeImports): [string, string] {
switch (info) {
case TypeDefInfo.Plain:
return [type, type];

case TypeDefInfo.Vector:
const _type = (sub as TypeDef).type;

setImports(imports, null, 'Vector');

return [_type, `Vector<${_type}>`];

default:
Expand All @@ -80,7 +87,7 @@ function _tsStructGetterType (structName: string | undefined, { info, sub, type

function tsStruct ({ name: structName, sub }: TypeDef, imports: TypeImports): string {
const keys = (sub as TypeDef[]).map((typedef): string => {
const [embedType, returnType] = _tsStructGetterType(structName, typedef);
const [embedType, returnType] = _tsStructGetterType(structName, typedef, imports);

setImports(imports, embedType, 'Struct');

Expand Down Expand Up @@ -130,17 +137,19 @@ function generateTsDef (srmlName: string, { types }: { types: Record<string, any
};

const codecTypes: TypeExist = {};
const otherTypes: TypeExist = {};
const ownTypes = Object.keys(types);
const primitiveTypes: TypeExist = {};
const substrateTypes: TypeExist = {};
const interfaces = Object.entries(types).map(([name, type]): [string, string] => {
const def = getTypeDef(isString(type) ? type.toString() : JSON.stringify(type), name);

return [name, generators[def.info](def, { codecTypes, otherTypes, ownTypes })];
return [name, generators[def.info](def, { codecTypes, ownTypes, primitiveTypes, substrateTypes })];
});

let header = HEADER;
const codecImports = Object.keys(codecTypes).filter((name): boolean => name !== 'Tuple').sort();
const primitiveImports = Object.keys(otherTypes).filter((type): boolean => otherTypes[type]).sort();
const primitiveImports = Object.keys(primitiveTypes).filter((type): boolean => primitiveTypes[type]).sort();
const substrateImports = Object.keys(substrateTypes).filter((type): boolean => substrateTypes[type]).sort();
const sortedDefs = interfaces.sort((a, b): number => a[0].localeCompare(b[0])).map(([, definition]): string => definition);

if (codecTypes['Tuple']) {
Expand All @@ -155,6 +164,10 @@ function generateTsDef (srmlName: string, { types }: { types: Record<string, any
header = header.concat(`import { ${primitiveImports.join(', ')} } from '../../primitive';\n`);
}

if (substrateImports.length) {
header = header.concat(`import { ${substrateImports.join(', ')} } from '../../type';\n`);
}

fs.writeFileSync(`packages/types/src/srml/${srmlName}/types.ts`, header.concat('\n').concat(sortedDefs.join('\n\n')).concat(FOOTER), { flag: 'w' });
}

Expand Down
6 changes: 6 additions & 0 deletions packages/types/src/srml/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.

// NOTE The order here is NOT alphabetical, rather it is setup so that lower
// layers can re-use types from higher layers as this is being injected. The
// injection order is critical.

export { default as grandpa } from './grandpa/definitions';
export { default as imOnline } from './imOnline/definitions';
export { default as parachains } from './parachains/definitions';
Loading