From 1643ec7ddf6a648e2099690f2f0a4c2c865bd110 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Mon, 12 Oct 2020 15:53:45 +0200 Subject: [PATCH] Provide chain properties for contract registry (#2714) --- packages/api-contract/src/Abi.ts | 6 +++--- packages/api-contract/src/ContractRegistry.ts | 9 +++++++-- packages/api-contract/src/base/Base.ts | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/api-contract/src/Abi.ts b/packages/api-contract/src/Abi.ts index 6891f5b533..5714a1f35e 100644 --- a/packages/api-contract/src/Abi.ts +++ b/packages/api-contract/src/Abi.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import { AnyJson } from '@polkadot/types/types'; -import { InkConstructorSpec, InkMessageSpec } from '@polkadot/types/interfaces'; +import { ChainProperties, InkConstructorSpec, InkMessageSpec } from '@polkadot/types/interfaces'; import { AbiConstructor, AbiMessage, AbiMessageParam } from './types'; import { assert, isNumber, isObject, isString, stringCamelCase } from '@polkadot/util'; @@ -28,14 +28,14 @@ export default class Abi extends ContractRegistry { public readonly messages: AbiMessage[]; - constructor (abiJson: AnyJson) { + constructor (abiJson: AnyJson, chainProperties?: ChainProperties) { const json = isString(abiJson) ? JSON.parse(abiJson) as AnyJson : abiJson; assert(isObject(json) && !Array.isArray(json) && json.metadataVersion && isObject(json.spec) && !Array.isArray(json.spec) && Array.isArray(json.spec.constructors) && Array.isArray(json.spec.messages), 'Invalid JSON ABI structure supplied, expected a recent metadata version'); - super(json); + super(json, chainProperties); this.json = json; this.constructors = this.project.spec.constructors.map((spec: InkConstructorSpec, index) => diff --git a/packages/api-contract/src/ContractRegistry.ts b/packages/api-contract/src/ContractRegistry.ts index 75a69cb152..cc3592beef 100644 --- a/packages/api-contract/src/ContractRegistry.ts +++ b/packages/api-contract/src/ContractRegistry.ts @@ -1,7 +1,7 @@ // Copyright 2017-2020 @polkadot/api-contract authors & contributors // SPDX-License-Identifier: Apache-2.0 -import { InkProject, MtField, MtLookupTypeId, MtType, MtTypeDefArray, MtTypeDefVariant, MtTypeDefSequence, MtTypeDefTuple, MtVariant } from '@polkadot/types/interfaces'; +import { ChainProperties, InkProject, MtField, MtLookupTypeId, MtType, MtTypeDefArray, MtTypeDefVariant, MtTypeDefSequence, MtTypeDefTuple, MtVariant } from '@polkadot/types/interfaces'; import { AnyJson, Registry, TypeDef, TypeDefInfo } from '@polkadot/types/types'; import { assert, isUndefined } from '@polkadot/util'; @@ -19,8 +19,13 @@ export default class ContractRegistry { public readonly project: InkProject; - constructor (json: Record) { + constructor (json: Record, chainProperties?: ChainProperties) { this.registry = new TypeRegistry(); + + if (chainProperties) { + this.registry.setChainProperties(chainProperties); + } + this.project = this.registry.createType('InkProject', json); // Generate TypeDefs for each provided registry type diff --git a/packages/api-contract/src/base/Base.ts b/packages/api-contract/src/base/Base.ts index 8a8d027b88..81e8501e25 100644 --- a/packages/api-contract/src/base/Base.ts +++ b/packages/api-contract/src/base/Base.ts @@ -21,7 +21,7 @@ export default abstract class Base { constructor (api: ApiBase, abi: AnyJson | Abi, decorateMethod: DecorateMethod) { this.abi = abi instanceof Abi ? abi - : new Abi(abi); + : new Abi(abi, api.registry.getChainProperties()); this.api = api; this.registry = this.abi.registry; this._decorateMethod = decorateMethod;