Provide chain properties for contract registry (#2714)

This commit is contained in:
Jaco Greeff
2020-10-12 15:53:45 +02:00
committed by GitHub
parent 76c206d60d
commit 1643ec7ddf
3 changed files with 11 additions and 6 deletions
+3 -3
View File
@@ -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) =>
@@ -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<string, AnyJson>) {
constructor (json: Record<string, AnyJson>, 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
+1 -1
View File
@@ -21,7 +21,7 @@ export default abstract class Base<ApiType extends ApiTypes> {
constructor (api: ApiBase<ApiType>, abi: AnyJson | Abi, decorateMethod: DecorateMethod<ApiType>) {
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;