From 7e1eb0fab052f8ec95d42c19b4fac765c84ff6a1 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 3 Dec 2019 14:05:50 +0100 Subject: [PATCH] Drop circular Linkage deps (#1627) * Drop circular Linkage deps * Cleanup more circular deps * IU8a interface --- package.json | 1 + packages/types/src/codec/Compact.ts | 4 +-- packages/types/src/codec/U8a.ts | 4 +-- .../types/src/codec/create/getTypeClass.ts | 13 ++++++++-- .../types/src/codec/utils/mapToTypeMap.ts | 2 +- packages/types/src/types.ts | 25 +++++++++++++------ 6 files changed, 34 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index 7a5c999868..fc01dd9a37 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "scripts": { "build": "yarn build:interfaces && polkadot-dev-build-ts", "build:interfaces": "node packages/types/src/scripts/interfacesTsWrapper.js", + "circular": "madge --circular --extensions ts ./", "docs": "node packages/types/src/scripts/MetadataMdWrapper.js && polkadot-dev-build-docs", "chain:info": "node packages/types/src/scripts/extractChainWrapper.js", "lint": "eslint --ext .js,.jsx,.ts,.tsx . && tsc --noEmit --pretty", diff --git a/packages/types/src/codec/Compact.ts b/packages/types/src/codec/Compact.ts index 0e9d5a575b..6af372a84d 100644 --- a/packages/types/src/codec/Compact.ts +++ b/packages/types/src/codec/Compact.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 { AnyNumber, Codec, Constructor, InterfaceTypes, Registry } from '../types'; +import { AnyNumber, Codec, Constructor, ICompact, InterfaceTypes, Registry } from '../types'; import BN from 'bn.js'; import { compactAddLength, compactFromU8a, compactStripLength, compactToU8a, isBn, isNumber, isString } from '@polkadot/util'; @@ -26,7 +26,7 @@ export interface CompactEncodable extends Codec { * used by other types to add length-prefixed encoding, or in the case of wrapped types, taking * a number and making the compact representation thereof */ -export default class Compact extends Base { +export default class Compact extends Base implements ICompact { constructor (registry: Registry, Type: Constructor | InterfaceTypes, value: Compact | AnyNumber = 0) { super(registry, Compact.decodeCompact(registry, typeToConstructor(registry, Type), value)); } diff --git a/packages/types/src/codec/U8a.ts b/packages/types/src/codec/U8a.ts index 495c60dc43..419fce056c 100644 --- a/packages/types/src/codec/U8a.ts +++ b/packages/types/src/codec/U8a.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 { AnyU8a, Codec, IHash, Registry } from '../types'; +import { AnyU8a, IHash, IU8a, Registry } from '../types'; import { isU8a, isUndefined, u8aToHex, u8aToU8a } from '@polkadot/util'; import { blake2AsU8a } from '@polkadot/util-crypto'; @@ -16,7 +16,7 @@ import { blake2AsU8a } from '@polkadot/util-crypto'; * actual lengths instead of used directly. * @noInheritDoc */ -export default class U8a extends Uint8Array implements Codec { +export default class U8a extends Uint8Array implements IU8a { public readonly registry: Registry; constructor (registry: Registry, value?: AnyU8a) { diff --git a/packages/types/src/codec/create/getTypeClass.ts b/packages/types/src/codec/create/getTypeClass.ts index 13ea783b50..979e919726 100644 --- a/packages/types/src/codec/create/getTypeClass.ts +++ b/packages/types/src/codec/create/getTypeClass.ts @@ -10,7 +10,6 @@ import { assert } from '@polkadot/util'; import BTreeMap from '../BTreeMap'; import Compact from '../Compact'; import Enum from '../Enum'; -import Linkage from '../Linkage'; import Option from '../Option'; import Result from '../Result'; import CodecSet from '../Set'; @@ -67,7 +66,17 @@ const infoMapping: Record C [TypeDefInfo.Enum]: (registry: Registry, value: TypeDef): Constructor => Enum.with(getTypeClassMap(value)), - [TypeDefInfo.Linkage]: (registry: Registry, value: TypeDef): Constructor => Linkage.withKey(getSubType(value)), + // We have circular deps between Linkage & Struct + [TypeDefInfo.Linkage]: (registry: Registry, value: TypeDef): Constructor => { + const type = `Option<${getSubType(value)}>`; + const Clazz = Struct.with({ previous: type, next: type } as any); + + ClassOf.prototype.toRawType = function (): string { + return `Linkage<${this.next.toRawType(true)}>`; + }; + + return Clazz; + }, // eslint-disable-next-line @typescript-eslint/no-unused-vars [TypeDefInfo.Null]: (registry: Registry, _: TypeDef): Constructor => ClassOf(registry, 'Null'), diff --git a/packages/types/src/codec/utils/mapToTypeMap.ts b/packages/types/src/codec/utils/mapToTypeMap.ts index 008ea7bd0e..3118b8e143 100644 --- a/packages/types/src/codec/utils/mapToTypeMap.ts +++ b/packages/types/src/codec/utils/mapToTypeMap.ts @@ -6,7 +6,7 @@ import { Codec, Constructor, InterfaceTypes, Registry } from '../../types'; import { isString } from '@polkadot/util'; -import { ClassOf } from '../create'; +import { ClassOf } from '../create/createClass'; export function typeToConstructor (registry: Registry, type: InterfaceTypes | Constructor): Constructor { return ( diff --git a/packages/types/src/types.ts b/packages/types/src/types.ts index 17f0d64b98..27f4efbeda 100644 --- a/packages/types/src/types.ts +++ b/packages/types/src/types.ts @@ -4,15 +4,11 @@ import { SignOptions } from '@polkadot/keyring/types'; import { FunctionMetadataLatest } from './interfaces/metadata'; -import { Balance, EcdsaSignature, Ed25519Signature, Index, Sr25519Signature } from './interfaces/runtime'; +import { Address, Balance, Call, EcdsaSignature, Ed25519Signature, Index, Sr25519Signature } from './interfaces/runtime'; import BN from 'bn.js'; -import Compact from './codec/Compact'; -import U8a from './codec/U8a'; import { InterfaceRegistry } from './interfaceRegistry'; -import Call from './primitive/Generic/Call'; -import Address from './primitive/Generic/Address'; export * from './codec/types'; @@ -125,8 +121,21 @@ export interface Codec { toU8a (isBare?: BareOpts): Uint8Array; } +// eslint-disable-next-line @typescript-eslint/interface-name-prefix +export interface IU8a extends Uint8Array, Codec { + bitLength (): number; + toJSON (): any; +} + // eslint-disable-next-line @typescript-eslint/interface-name-prefix,@typescript-eslint/no-empty-interface -export interface IHash extends U8a { } +export interface IHash extends IU8a { } + +// eslint-disable-next-line @typescript-eslint/interface-name-prefix +export interface ICompact extends Codec { + toBn (): BN; + toNumber (): number; + unwrap (): T; +} export type CodecTo = 'toHex' | 'toJSON' | 'toString' | 'toU8a'; @@ -181,10 +190,10 @@ export interface IMethod extends Codec { interface ExtrinsicSignatureBase { readonly isSigned: boolean; readonly era: IExtrinsicEra; - readonly nonce: Compact; + readonly nonce: ICompact; readonly signature: EcdsaSignature | Ed25519Signature | Sr25519Signature; readonly signer: Address; - readonly tip: Compact; + readonly tip: ICompact; } export interface ExtrinsicPayloadValue {