Add suport for latest ink! metadata (#5650)

* add v5

* add environment

* tov5

* use correct version

* decode env types

* fix tests

* remove unnecesary checks

* update fixtures

* add maxEventTopics

* get type defs for environment

* fix conversion to v4
This commit is contained in:
Andreea Eftene
2023-05-29 20:19:55 +03:00
committed by GitHub
parent 2f07e1c45f
commit 0be50b285b
11 changed files with 741 additions and 139 deletions
+28 -5
View File
@@ -3,7 +3,7 @@
import type { Bytes } from '@polkadot/types';
import type { ChainProperties, ContractConstructorSpecLatest, ContractEventSpecLatest, ContractMessageParamSpecLatest, ContractMessageSpecLatest, ContractMetadata, ContractMetadataLatest, ContractProjectInfo } from '@polkadot/types/interfaces';
import type { Codec, Registry } from '@polkadot/types/types';
import type { Codec, Registry, TypeDef } from '@polkadot/types/types';
import type { AbiConstructor, AbiEvent, AbiMessage, AbiParam, DecodedEvent, DecodedMessage } from '../types.js';
import { TypeRegistry } from '@polkadot/types';
@@ -82,6 +82,7 @@ export class Abi {
readonly messages: AbiMessage[];
readonly metadata: ContractMetadataLatest;
readonly registry: Registry;
readonly environment: Map<string, TypeDef | number> = new Map();
constructor (abiJson: Record<string, unknown> | string, chainProperties?: ChainProperties) {
[this.json, this.registry, this.metadata, this.info] = parseJson(
@@ -90,11 +91,18 @@ export class Abi {
: abiJson,
chainProperties
);
this.constructors = this.metadata.spec.constructors.map((spec: ContractConstructorSpecLatest, index) =>
this.#createMessage(spec, index, {
this.constructors = this.metadata.spec.constructors.map((spec: ContractConstructorSpecLatest, index) => {
const typeSpec = spec.returnType.unwrapOr(null);
return this.#createMessage(spec, index, {
isConstructor: true,
isPayable: spec.payable.isTrue
})
isDefault: spec.default.isTrue,
isPayable: spec.payable.isTrue,
returnType: typeSpec
? this.registry.lookup.getTypeDef(typeSpec.type)
: null
});
}
);
this.events = this.metadata.spec.events.map((spec: ContractEventSpecLatest, index) =>
this.#createEvent(spec, index)
@@ -103,6 +111,7 @@ export class Abi {
const typeSpec = spec.returnType.unwrapOr(null);
return this.#createMessage(spec, index, {
isDefault: spec.default.isTrue,
isMutating: spec.mutates.isTrue,
isPayable: spec.payable.isTrue,
returnType: typeSpec
@@ -110,6 +119,19 @@ export class Abi {
: null
});
});
const rawEnv = this.metadata.spec.environment.unwrapOr(null);
if (rawEnv) {
for (const [key, value] of rawEnv.entries()) {
const typeSpec = value.toPrimitive();
if (typeof typeSpec === 'object' && typeSpec !== null && 'type' in typeSpec) {
this.environment.set(key, this.registry.lookup.getTypeDef(typeSpec.type as number));
} else {
this.environment.set(key, typeSpec as number);
}
}
}
}
/**
@@ -215,6 +237,7 @@ export class Abi {
}),
identifier,
index,
isDefault: spec.default.isTrue,
method: stringCamelCase(identifier),
path: identifier.split('::').map((s) => stringCamelCase(s)),
selector: spec.selector,
+13 -2
View File
@@ -4,6 +4,17 @@
import type { ContractMetadataV3, ContractMetadataV4 } from '@polkadot/types/interfaces';
import type { Registry } from '@polkadot/types/types';
export function v3ToV4 (_registry: Registry, v3: ContractMetadataV3): ContractMetadataV4 {
return v3;
import { objectSpread } from '@polkadot/util';
export function v3ToV4 (registry: Registry, v3: ContractMetadataV3): ContractMetadataV4 {
return registry.createType('ContractMetadataV4', objectSpread({}, v3, {
spec: objectSpread({}, v3.spec, {
constructors: v3.spec.constructors.map((c) =>
registry.createType('ContractConstructorSpecV4', objectSpread({}, c))
),
messages: v3.spec.messages.map((m) =>
registry.createType('ContractMessageSpecV3', objectSpread({}, m))
)
})
}));
}