From 0323b27da4cb457c857dda32732b6f99db923a4b Mon Sep 17 00:00:00 2001 From: Xiliang Chen Date: Tue, 30 Jul 2019 16:47:14 +1200 Subject: [PATCH] Generic Asset module type definitions (#1229) * Fix missing type import for generated code * Add GenericAsset module type definations * eslint --fix --- packages/types/src/interfaceRegistry.ts | 22 +++++++++- packages/types/src/interfaces/definitions.ts | 1 + .../interfaces/genericAsset/definitions.ts | 29 +++++++++++++ .../src/interfaces/genericAsset/index.ts | 3 ++ .../src/interfaces/genericAsset/types.ts | 43 +++++++++++++++++++ .../src/interfaces/runtime/definitions.ts | 1 + .../types/src/interfaces/runtime/types.ts | 3 ++ packages/types/src/interfaces/types.ts | 1 + packages/types/src/scripts/interfacesTs.ts | 11 ++--- 9 files changed, 108 insertions(+), 6 deletions(-) create mode 100644 packages/types/src/interfaces/genericAsset/definitions.ts create mode 100644 packages/types/src/interfaces/genericAsset/index.ts create mode 100644 packages/types/src/interfaces/genericAsset/types.ts diff --git a/packages/types/src/interfaceRegistry.ts b/packages/types/src/interfaceRegistry.ts index efa5a4284a..1e4c95111a 100644 --- a/packages/types/src/interfaceRegistry.ts +++ b/packages/types/src/interfaceRegistry.ts @@ -2,7 +2,7 @@ import { Compact, Option, Vec } from './codec'; import { Bytes, Data, Fixed64, H160, H256, H512, Null, StorageHasher, StorageKey, Text, Type, 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, Call, Consensus, ConsensusEngineId, Digest, DigestItem, Ed25519Signature, Extrinsic, ExtrinsicEra, ExtrinsicPayload, Hash, Header, ImmortalEra, Index, Justification, KeyTypeId, KeyValue, LockIdentifier, Moment, MortalEra, Origin, Perbill, Permill, Phantom, PreRuntime, Seal, SealV0, Signature, SignedBlock, Sr25519Signature, StorageData, ValidatorId, Weight, WeightMultiplier } from './interfaces/runtime'; +import { AccountId, AccountIdOf, AccountIndex, Address, AssetId, Balance, BalanceOf, Block, BlockNumber, Call, Consensus, ConsensusEngineId, Digest, DigestItem, Ed25519Signature, Extrinsic, ExtrinsicEra, ExtrinsicPayload, Hash, Header, ImmortalEra, Index, Justification, KeyTypeId, KeyValue, LockIdentifier, Moment, MortalEra, Origin, Perbill, Permill, Phantom, PreRuntime, Seal, SealV0, 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'; @@ -14,6 +14,7 @@ import { MemberCount, ProposalIndex, Votes } from './interfaces/collective'; import { AliveContractInfo, CodeHash, ContractInfo, ContractStorageKey, Gas, PrefabWasmModule, PrefabWasmModuleReserved, Schedule, SeedOf, TombstoneContractInfo, TrieId } from './interfaces/contracts'; import { Conviction, PropIndex, Proposal, ReferendumIndex, ReferendumInfo } from './interfaces/democracy'; import { AccountInfo, Amount, AssetOf, InherentOfflineReport, LockPeriods, NewAccountOutcome, OpaqueKey, SessionKey } from './interfaces/deprecated'; +import { AssetOptions, Owner, PermissionLatest, PermissionVersions, PermissionsV1 } from './interfaces/genericAsset'; 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'; @@ -117,6 +118,10 @@ export interface InterfaceRegistry { Address: Address; 'Option
': Option
; 'Vec
': Vec
; + AssetId: AssetId; + 'Compact': Compact; + 'Option': Option; + 'Vec': Vec; Balance: Balance; 'Compact': Compact; 'Option': Option; @@ -403,6 +408,21 @@ export interface InterfaceRegistry { OpaqueKey: OpaqueKey; 'Option': Option; 'Vec': Vec; + AssetOptions: AssetOptions; + 'Option': Option; + 'Vec': Vec; + PermissionsV1: PermissionsV1; + 'Option': Option; + 'Vec': Vec; + Owner: Owner; + 'Option': Option; + 'Vec': Vec; + PermissionVersions: PermissionVersions; + 'Option': Option; + 'Vec': Vec; + PermissionLatest: PermissionLatest; + 'Option': Option; + 'Vec': Vec; OpaqueMultiaddr: OpaqueMultiaddr; 'Option': Option; 'Vec': Vec; diff --git a/packages/types/src/interfaces/definitions.ts b/packages/types/src/interfaces/definitions.ts index a531e724f0..d26d96dccd 100644 --- a/packages/types/src/interfaces/definitions.ts +++ b/packages/types/src/interfaces/definitions.ts @@ -29,6 +29,7 @@ export { default as collective } from './collective/definitions'; export { default as contracts } from './contracts/definitions'; export { default as democracy } from './democracy/definitions'; export { default as deprecated } from './deprecated/definitions'; +export { default as genericAsset } from './genericAsset/definitions'; export { default as imOnline } from './imOnline/definitions'; export { default as parachains } from './parachains/definitions'; export { default as staking } from './staking/definitions'; diff --git a/packages/types/src/interfaces/genericAsset/definitions.ts b/packages/types/src/interfaces/genericAsset/definitions.ts new file mode 100644 index 0000000000..21c128ba7a --- /dev/null +++ b/packages/types/src/interfaces/genericAsset/definitions.ts @@ -0,0 +1,29 @@ +// 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. + +export default { + types: { + Owner: { + _enum: { + None: 'Null', + Address: 'AccountId' + } + }, + PermissionsV1: { + update: 'Owner', + mint: 'Owner', + burn: 'Owner' + }, + PermissionVersions: { + _enum: { + V1: 'PermissionsV1' + } + }, + PermissionLatest: 'PermissionsV1', + AssetOptions: { + initalIssuance: 'Balance', + permissions: 'PermissionLatest' + } + } +}; diff --git a/packages/types/src/interfaces/genericAsset/index.ts b/packages/types/src/interfaces/genericAsset/index.ts new file mode 100644 index 0000000000..f94c8a0ae8 --- /dev/null +++ b/packages/types/src/interfaces/genericAsset/index.ts @@ -0,0 +1,3 @@ +// Auto-generated via `yarn build:interfaces`, do not edit + +export * from './types'; diff --git a/packages/types/src/interfaces/genericAsset/types.ts b/packages/types/src/interfaces/genericAsset/types.ts new file mode 100644 index 0000000000..e32ab42065 --- /dev/null +++ b/packages/types/src/interfaces/genericAsset/types.ts @@ -0,0 +1,43 @@ +// Auto-generated via `yarn build:interfaces`, do not edit + +import { Enum, Struct } from '../../codec'; +import { AccountId, Balance } from '../runtime'; + +/** Struct */ +export interface AssetOptions extends Struct { + /** Balance */ + readonly initalIssuance: Balance; + /** PermissionLatest */ + readonly permissions: PermissionLatest; +} + +/** Enum */ +export interface Owner extends Enum { + /** 0:: None */ + readonly isNone: boolean; + /** 1:: Address(AccountId) */ + readonly isAddress: boolean; + /** AccountId */ + readonly asAddress: AccountId; +} + +/** PermissionsV1 */ +export type PermissionLatest = PermissionsV1; + +/** Struct */ +export interface PermissionsV1 extends Struct { + /** Owner */ + readonly update: Owner; + /** Owner */ + readonly mint: Owner; + /** Owner */ + readonly burn: Owner; +} + +/** Enum */ +export interface PermissionVersions extends Enum { + /** 0:: V1(PermissionsV1) */ + readonly isV1: boolean; + /** PermissionsV1 */ + readonly asV1: PermissionsV1; +} diff --git a/packages/types/src/interfaces/runtime/definitions.ts b/packages/types/src/interfaces/runtime/definitions.ts index c7c2c4826c..9246200d99 100644 --- a/packages/types/src/interfaces/runtime/definitions.ts +++ b/packages/types/src/interfaces/runtime/definitions.ts @@ -8,6 +8,7 @@ export default { AccountIdOf: 'AccountId', AccountIndex: 'GenericAccountIndex', Address: 'GenericAddress', + AssetId: 'u32', 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 fead314fab..ca8b45d5e3 100644 --- a/packages/types/src/interfaces/runtime/types.ts +++ b/packages/types/src/interfaces/runtime/types.ts @@ -16,6 +16,9 @@ export type AccountIndex = GenericAccountIndex; /** GenericAddress */ export type Address = GenericAddress; +/** u32 */ +export type AssetId = u32; + /** u128 */ export type Balance = u128; diff --git a/packages/types/src/interfaces/types.ts b/packages/types/src/interfaces/types.ts index b59d193c27..0a6a1828d1 100644 --- a/packages/types/src/interfaces/types.ts +++ b/packages/types/src/interfaces/types.ts @@ -12,6 +12,7 @@ export * from './collective/types'; export * from './contracts/types'; export * from './democracy/types'; export * from './deprecated/types'; +export * from './genericAsset/types'; export * from './imOnline/types'; export * from './parachains/types'; export * from './staking/types'; diff --git a/packages/types/src/scripts/interfacesTs.ts b/packages/types/src/scripts/interfacesTs.ts index 9dbc2c5ba3..b63521803a 100644 --- a/packages/types/src/scripts/interfacesTs.ts +++ b/packages/types/src/scripts/interfacesTs.ts @@ -81,7 +81,8 @@ function exportType (name: string = '', base: string): string { } // helper to generate a `readonly : ;` getter -function createGetter (name: string = '', type: string, doc?: string): string { +function createGetter (name: string = '', type: string, imports: TypeImports, doc?: string): string { + setImports(imports, [type]); return ` /** ${doc || type} */\n readonly ${name}: ${type};\n`; } @@ -112,8 +113,8 @@ function tsEnum ({ name: enumName, sub }: TypeDef, imports: TypeImports): string const getter = stringUpperFirst(stringCamelCase(name.replace(' ', '_'))); const [enumType, asGetter] = type === 'Null' ? ['', ''] - : [`(${type})`, createGetter(`as${getter}`, type)]; - const isGetter = createGetter(`is${getter}`, 'boolean', `${index}:: ${name}${enumType}`); + : [`(${type})`, createGetter(`as${getter}`, type, imports)]; + const isGetter = createGetter(`is${getter}`, 'boolean', imports, `${index}:: ${name}${enumType}`); switch (info) { case TypeDefInfo.Plain: @@ -186,7 +187,7 @@ function tsSet ({ name: setName, sub }: TypeDef, imports: TypeImports): string { setImports(imports, ['Set']); const types = (sub as TypeDef[]).map(({ name }): string => - createGetter(`is${name}`, 'boolean') + createGetter(`is${name}`, 'boolean', imports) ); return exportInterface(setName, 'Set', types.join('')); @@ -198,7 +199,7 @@ function tsStruct ({ name: structName, sub }: TypeDef, imports: TypeImports): st setImports(imports, ['Struct', embedType]); - return createGetter(typedef.name, returnType); + return createGetter(typedef.name, returnType, imports); }); return exportInterface(structName, 'Struct', keys.join(''));