@dedot/contracts 0.0.1-alpha.33
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Contract.d.ts +15 -0
- package/Contract.js +32 -0
- package/ContractDeployer.d.ts +14 -0
- package/ContractDeployer.js +30 -0
- package/LICENSE +201 -0
- package/README.md +3 -0
- package/TypinkRegistry.d.ts +7 -0
- package/TypinkRegistry.js +12 -0
- package/cjs/Contract.js +36 -0
- package/cjs/ContractDeployer.js +34 -0
- package/cjs/TypinkRegistry.js +16 -0
- package/cjs/executor/ConstructorQueryExecutor.js +27 -0
- package/cjs/executor/ConstructorTxExecutor.js +37 -0
- package/cjs/executor/Executor.js +39 -0
- package/cjs/executor/QueryExecutor.js +38 -0
- package/cjs/executor/TxExecutor.js +27 -0
- package/cjs/executor/index.js +20 -0
- package/cjs/index.js +22 -0
- package/cjs/package.json +1 -0
- package/cjs/types/index.js +17 -0
- package/cjs/types/shared.js +2 -0
- package/cjs/types/v4.js +2 -0
- package/cjs/types/v5.js +2 -0
- package/cjs/utils.js +108 -0
- package/executor/ConstructorQueryExecutor.d.ts +6 -0
- package/executor/ConstructorQueryExecutor.js +23 -0
- package/executor/ConstructorTxExecutor.d.ts +13 -0
- package/executor/ConstructorTxExecutor.js +33 -0
- package/executor/Executor.d.ts +18 -0
- package/executor/Executor.js +35 -0
- package/executor/QueryExecutor.d.ts +7 -0
- package/executor/QueryExecutor.js +34 -0
- package/executor/TxExecutor.d.ts +7 -0
- package/executor/TxExecutor.js +23 -0
- package/executor/index.d.ts +4 -0
- package/executor/index.js +4 -0
- package/index.d.ts +6 -0
- package/index.js +6 -0
- package/package.json +36 -0
- package/types/index.d.ts +57 -0
- package/types/index.js +1 -0
- package/types/shared.d.ts +105 -0
- package/types/shared.js +1 -0
- package/types/v4.d.ts +31 -0
- package/types/v4.js +1 -0
- package/types/v5.d.ts +21 -0
- package/types/v5.js +1 -0
- package/utils.d.ts +11 -0
- package/utils.js +99 -0
package/cjs/types/v4.js
ADDED
package/cjs/types/v5.js
ADDED
package/cjs/utils.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normalizeLabel = exports.ensureSupportContractsPallet = exports.newProxyChain = exports.parseRawMetadata = exports.normalizeContractTypeDef = exports.extractContractTypes = void 0;
|
|
4
|
+
const utils_1 = require("@dedot/utils");
|
|
5
|
+
const extractContractTypes = (contractMetadata) => {
|
|
6
|
+
const { types } = contractMetadata;
|
|
7
|
+
return types.map(({ type, id }) => ({
|
|
8
|
+
id,
|
|
9
|
+
type: (0, exports.normalizeContractTypeDef)(type.def),
|
|
10
|
+
params: [],
|
|
11
|
+
path: type?.path || [],
|
|
12
|
+
docs: [],
|
|
13
|
+
}));
|
|
14
|
+
};
|
|
15
|
+
exports.extractContractTypes = extractContractTypes;
|
|
16
|
+
const normalizeContractTypeDef = (def) => {
|
|
17
|
+
let tag;
|
|
18
|
+
let value;
|
|
19
|
+
if (def.variant) {
|
|
20
|
+
tag = 'Enum';
|
|
21
|
+
value = {
|
|
22
|
+
members: def.variant.variants?.map((variant) => ({
|
|
23
|
+
fields: variant.fields?.map((fields) => ({ typeId: fields.type, typeName: fields.typeName })) || [],
|
|
24
|
+
index: variant.index,
|
|
25
|
+
name: variant.name,
|
|
26
|
+
})) || [],
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
else if (def.tuple) {
|
|
30
|
+
tag = 'Tuple';
|
|
31
|
+
value = {
|
|
32
|
+
fields: def.tuple,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
else if (def.sequence) {
|
|
36
|
+
tag = 'Sequence';
|
|
37
|
+
value = {
|
|
38
|
+
typeParam: def.sequence.type,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
else if (def.composite) {
|
|
42
|
+
tag = 'Struct';
|
|
43
|
+
value = {
|
|
44
|
+
fields: def.composite.fields.map((one) => ({
|
|
45
|
+
typeId: one.type,
|
|
46
|
+
name: one.name,
|
|
47
|
+
typeName: one.typeName,
|
|
48
|
+
})),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
else if (def.primitive) {
|
|
52
|
+
tag = 'Primitive';
|
|
53
|
+
value = {
|
|
54
|
+
kind: def.primitive,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
else if (def.array) {
|
|
58
|
+
tag = 'SizedVec';
|
|
59
|
+
value = {
|
|
60
|
+
len: def.array.len,
|
|
61
|
+
typeParam: def.array.type,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
throw Error(`Invalid contract type def: ${JSON.stringify(def)}`);
|
|
66
|
+
}
|
|
67
|
+
return { tag, value };
|
|
68
|
+
};
|
|
69
|
+
exports.normalizeContractTypeDef = normalizeContractTypeDef;
|
|
70
|
+
const UNSUPPORTED_VERSIONS = ['V3', 'V2', 'V1'];
|
|
71
|
+
const SUPPORTED_VERSIONS = ['5', '4'];
|
|
72
|
+
const parseRawMetadata = (rawMetadata) => {
|
|
73
|
+
const metadata = JSON.parse(rawMetadata);
|
|
74
|
+
// This is for V1, V2, V3
|
|
75
|
+
const unsupportedVersion = UNSUPPORTED_VERSIONS.find((o) => metadata[o]);
|
|
76
|
+
if (unsupportedVersion) {
|
|
77
|
+
throw new Error(`Unsupported metadata version: ${unsupportedVersion}`);
|
|
78
|
+
}
|
|
79
|
+
// This is for V4, V5
|
|
80
|
+
if (!SUPPORTED_VERSIONS.includes(metadata.version)) {
|
|
81
|
+
throw new Error(`Unsupported metadata version: ${metadata.version}`);
|
|
82
|
+
}
|
|
83
|
+
return metadata;
|
|
84
|
+
};
|
|
85
|
+
exports.parseRawMetadata = parseRawMetadata;
|
|
86
|
+
function newProxyChain(carrier) {
|
|
87
|
+
return new Proxy(carrier, {
|
|
88
|
+
get(target, property) {
|
|
89
|
+
return target.doExecute(property.toString());
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
exports.newProxyChain = newProxyChain;
|
|
94
|
+
function ensureSupportContractsPallet(api) {
|
|
95
|
+
try {
|
|
96
|
+
!!api.call.contractsApi.call.meta && !!api.tx.contracts.call.meta;
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
throw new Error('Contracts pallet is not available');
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
exports.ensureSupportContractsPallet = ensureSupportContractsPallet;
|
|
103
|
+
function normalizeLabel(label) {
|
|
104
|
+
if (!label)
|
|
105
|
+
return '';
|
|
106
|
+
return (0, utils_1.stringCamelCase)(label.replaceAll('::', '_'));
|
|
107
|
+
}
|
|
108
|
+
exports.normalizeLabel = normalizeLabel;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { GenericSubstrateApi } from '@dedot/types';
|
|
2
|
+
import { GenericConstructorQueryCall } from '../types/index.js';
|
|
3
|
+
import { ConstructorTxExecutor } from './ConstructorTxExecutor.js';
|
|
4
|
+
export declare class ConstructorQueryExecutor<ChainApi extends GenericSubstrateApi> extends ConstructorTxExecutor<ChainApi> {
|
|
5
|
+
doExecute(constructor: string): GenericConstructorQueryCall<ChainApi>;
|
|
6
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { assert, concatU8a, hexToU8a, isWasm, u8aToHex } from '@dedot/utils';
|
|
2
|
+
import { ConstructorTxExecutor } from './ConstructorTxExecutor.js';
|
|
3
|
+
export class ConstructorQueryExecutor extends ConstructorTxExecutor {
|
|
4
|
+
doExecute(constructor) {
|
|
5
|
+
const meta = this.findConstructorMeta(constructor);
|
|
6
|
+
assert(meta, `Constructor message not found: ${constructor}`);
|
|
7
|
+
const callFn = (...params) => {
|
|
8
|
+
// TODO verify number of arguments/params & call options presence
|
|
9
|
+
const { args } = meta;
|
|
10
|
+
const callOptions = params[args.length];
|
|
11
|
+
const { caller, value = 0n, gasLimit, storageDepositLimit, salt } = callOptions;
|
|
12
|
+
const formattedInputs = args.map((arg, index) => this.tryEncode(arg, params[index]));
|
|
13
|
+
const bytes = u8aToHex(concatU8a(hexToU8a(meta.selector), ...formattedInputs));
|
|
14
|
+
const code = {
|
|
15
|
+
tag: isWasm(this.code) ? 'Upload' : 'Existing',
|
|
16
|
+
value: this.code,
|
|
17
|
+
};
|
|
18
|
+
return this.api.call.contractsApi.instantiate(caller, value, gasLimit, storageDepositLimit, code, bytes, salt);
|
|
19
|
+
};
|
|
20
|
+
callFn.meta = meta;
|
|
21
|
+
return callFn;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ISubstrateClient } from '@dedot/api';
|
|
2
|
+
import { Hash } from '@dedot/codecs';
|
|
3
|
+
import { GenericSubstrateApi } from '@dedot/types';
|
|
4
|
+
import { HexString } from '@dedot/utils';
|
|
5
|
+
import { TypinkRegistry } from '../TypinkRegistry.js';
|
|
6
|
+
import { ContractConstructorMessage, GenericConstructorTxCall } from '../types/index.js';
|
|
7
|
+
import { Executor } from './Executor.js';
|
|
8
|
+
export declare class ConstructorTxExecutor<ChainApi extends GenericSubstrateApi> extends Executor<ChainApi> {
|
|
9
|
+
protected readonly code: Hash | Uint8Array | HexString | string;
|
|
10
|
+
constructor(api: ISubstrateClient<ChainApi>, registry: TypinkRegistry, code: Hash | Uint8Array | string);
|
|
11
|
+
doExecute(constructor: string): GenericConstructorTxCall<ChainApi>;
|
|
12
|
+
protected findConstructorMeta(constructor: string): ContractConstructorMessage | undefined;
|
|
13
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { assert, concatU8a, hexToU8a, isWasm, u8aToHex } from '@dedot/utils';
|
|
2
|
+
import { normalizeLabel } from '../utils.js';
|
|
3
|
+
import { Executor } from './Executor.js';
|
|
4
|
+
export class ConstructorTxExecutor extends Executor {
|
|
5
|
+
code;
|
|
6
|
+
constructor(api, registry, code) {
|
|
7
|
+
super(api, registry);
|
|
8
|
+
this.code = code;
|
|
9
|
+
}
|
|
10
|
+
doExecute(constructor) {
|
|
11
|
+
const meta = this.findConstructorMeta(constructor);
|
|
12
|
+
assert(meta, `Constructor message not found: ${constructor}`);
|
|
13
|
+
const callFn = (...params) => {
|
|
14
|
+
// TODO verify number of arguments/params & call options presence
|
|
15
|
+
const { args } = meta;
|
|
16
|
+
const txCallOptions = params[args.length];
|
|
17
|
+
const { value = 0n, gasLimit, storageDepositLimit, salt } = txCallOptions;
|
|
18
|
+
const formattedInputs = args.map((arg, index) => this.tryEncode(arg, params[index]));
|
|
19
|
+
const bytes = u8aToHex(concatU8a(hexToU8a(meta.selector), ...formattedInputs));
|
|
20
|
+
if (isWasm(this.code)) {
|
|
21
|
+
return this.api.tx.contracts.instantiateWithCode(value, gasLimit, storageDepositLimit, this.code, bytes, salt);
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
return this.api.tx.contracts.instantiate(value, gasLimit, storageDepositLimit, this.code, bytes, salt);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
callFn.meta = meta;
|
|
28
|
+
return callFn;
|
|
29
|
+
}
|
|
30
|
+
findConstructorMeta(constructor) {
|
|
31
|
+
return this.metadata.spec.constructors.find((one) => normalizeLabel(one.label) === constructor);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ISubstrateClient } from '@dedot/api';
|
|
2
|
+
import { SubstrateApi } from '@dedot/api/chaintypes';
|
|
3
|
+
import { AccountId32 } from '@dedot/codecs';
|
|
4
|
+
import { GenericSubstrateApi, RpcVersion } from '@dedot/types';
|
|
5
|
+
import { TypinkRegistry } from '../TypinkRegistry.js';
|
|
6
|
+
import { ContractMessageArg, ContractMessage } from '../types/index.js';
|
|
7
|
+
import { ContractMetadata } from '../types/index.js';
|
|
8
|
+
export declare abstract class Executor<ChainApi extends GenericSubstrateApi = SubstrateApi[RpcVersion]> {
|
|
9
|
+
#private;
|
|
10
|
+
constructor(api: ISubstrateClient<ChainApi>, registry: TypinkRegistry, address?: AccountId32);
|
|
11
|
+
get api(): ISubstrateClient<ChainApi>;
|
|
12
|
+
get metadata(): ContractMetadata;
|
|
13
|
+
get address(): AccountId32 | undefined;
|
|
14
|
+
get registry(): TypinkRegistry;
|
|
15
|
+
abstract doExecute(...paths: string[]): unknown;
|
|
16
|
+
tryEncode(param: ContractMessageArg, value: any): Uint8Array;
|
|
17
|
+
tryDecode(meta: ContractMessage, raw: any): unknown;
|
|
18
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { AccountId32 } from '@dedot/codecs';
|
|
2
|
+
export class Executor {
|
|
3
|
+
#api;
|
|
4
|
+
#registry;
|
|
5
|
+
#address;
|
|
6
|
+
constructor(api, registry, address) {
|
|
7
|
+
this.#api = api;
|
|
8
|
+
this.#registry = registry;
|
|
9
|
+
if (address) {
|
|
10
|
+
this.#address = new AccountId32(address);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
get api() {
|
|
14
|
+
return this.#api;
|
|
15
|
+
}
|
|
16
|
+
get metadata() {
|
|
17
|
+
return this.#registry.metadata;
|
|
18
|
+
}
|
|
19
|
+
get address() {
|
|
20
|
+
return this.#address;
|
|
21
|
+
}
|
|
22
|
+
get registry() {
|
|
23
|
+
return this.#registry;
|
|
24
|
+
}
|
|
25
|
+
tryEncode(param, value) {
|
|
26
|
+
const { type } = param.type;
|
|
27
|
+
const $codec = this.registry.findCodec(type);
|
|
28
|
+
return $codec.tryEncode(value);
|
|
29
|
+
}
|
|
30
|
+
tryDecode(meta, raw) {
|
|
31
|
+
const { returnType: { type }, } = meta;
|
|
32
|
+
const $codec = this.registry.findCodec(type);
|
|
33
|
+
return $codec.tryDecode(raw);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { GenericSubstrateApi } from '@dedot/types';
|
|
2
|
+
import { ContractMessage, GenericContractQueryCall } from '../types/index.js';
|
|
3
|
+
import { Executor } from './Executor.js';
|
|
4
|
+
export declare class QueryExecutor<ChainApi extends GenericSubstrateApi> extends Executor<ChainApi> {
|
|
5
|
+
doExecute(message: string): GenericContractQueryCall<ChainApi>;
|
|
6
|
+
protected findMessage(message: string): ContractMessage | undefined;
|
|
7
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { assert, concatU8a, hexToU8a, u8aToHex } from '@dedot/utils';
|
|
2
|
+
import { normalizeLabel } from '../utils.js';
|
|
3
|
+
import { Executor } from './Executor.js';
|
|
4
|
+
export class QueryExecutor extends Executor {
|
|
5
|
+
doExecute(message) {
|
|
6
|
+
const meta = this.findMessage(message);
|
|
7
|
+
assert(meta, `Query message not found: ${message}`);
|
|
8
|
+
const callFn = async (...params) => {
|
|
9
|
+
// TODO verify number of arguments/params & call options presence
|
|
10
|
+
const { args } = meta;
|
|
11
|
+
const callOptions = params[args.length];
|
|
12
|
+
const { caller, value = 0n, gasLimit, storageDepositLimit } = callOptions;
|
|
13
|
+
const formattedInputs = args.map((arg, index) => this.tryEncode(arg, params[index]));
|
|
14
|
+
const bytes = u8aToHex(concatU8a(hexToU8a(meta.selector), ...formattedInputs));
|
|
15
|
+
const contractResult = await this.api.call.contractsApi.call(caller, this.address, value, gasLimit, storageDepositLimit, bytes);
|
|
16
|
+
if (contractResult.result.isOk) {
|
|
17
|
+
return {
|
|
18
|
+
isOk: true,
|
|
19
|
+
data: this.tryDecode(meta, contractResult.result.value.data),
|
|
20
|
+
contractResult: contractResult,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
isOk: false,
|
|
25
|
+
contractResult: contractResult,
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
callFn.meta = meta;
|
|
29
|
+
return callFn;
|
|
30
|
+
}
|
|
31
|
+
findMessage(message) {
|
|
32
|
+
return this.metadata.spec.messages.find((one) => normalizeLabel(one.label) === message);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { GenericSubstrateApi } from '@dedot/types';
|
|
2
|
+
import { ContractMessage, GenericContractTxCall } from '../types/index.js';
|
|
3
|
+
import { Executor } from './Executor.js';
|
|
4
|
+
export declare class TxExecutor<ChainApi extends GenericSubstrateApi> extends Executor<ChainApi> {
|
|
5
|
+
doExecute(message: string): GenericContractTxCall<ChainApi>;
|
|
6
|
+
protected findTxMessage(message: string): ContractMessage | undefined;
|
|
7
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { assert, concatU8a, hexToU8a, u8aToHex } from '@dedot/utils';
|
|
2
|
+
import { normalizeLabel } from '../utils.js';
|
|
3
|
+
import { Executor } from './Executor.js';
|
|
4
|
+
export class TxExecutor extends Executor {
|
|
5
|
+
doExecute(message) {
|
|
6
|
+
const meta = this.findTxMessage(message);
|
|
7
|
+
assert(meta, `Tx message not found: ${message}`);
|
|
8
|
+
const callFn = (...params) => {
|
|
9
|
+
// TODO verify number of arguments/params & call options presence
|
|
10
|
+
const { args } = meta;
|
|
11
|
+
const txCallOptions = params[args.length];
|
|
12
|
+
const { value = 0n, gasLimit, storageDepositLimit } = txCallOptions;
|
|
13
|
+
const formattedInputs = args.map((arg, index) => this.tryEncode(arg, params[index]));
|
|
14
|
+
const bytes = u8aToHex(concatU8a(hexToU8a(meta.selector), ...formattedInputs));
|
|
15
|
+
return this.api.tx.contracts.call(this.address, value, gasLimit, storageDepositLimit, bytes);
|
|
16
|
+
};
|
|
17
|
+
callFn.meta = meta;
|
|
18
|
+
return callFn;
|
|
19
|
+
}
|
|
20
|
+
findTxMessage(message) {
|
|
21
|
+
return this.metadata.spec.messages.find((one) => one.mutates && normalizeLabel(one.label) === message);
|
|
22
|
+
}
|
|
23
|
+
}
|
package/index.d.ts
ADDED
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dedot/contracts",
|
|
3
|
+
"version": "0.0.1-alpha.33",
|
|
4
|
+
"description": "Delightful ink! Contract APIs",
|
|
5
|
+
"author": "Tung Vu <tung@coongcrafts.io>",
|
|
6
|
+
"main": "./cjs/index.js",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"type": "module",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "tsc --project tsconfig.build.json && tsc --project tsconfig.build.cjs.json",
|
|
11
|
+
"clean": "rm -rf ./dist && rm -rf ./tsconfig.tsbuildinfo ./tsconfig.build.tsbuildinfo",
|
|
12
|
+
"test": "vitest --watch=false"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@dedot/api": "0.0.1-alpha.33",
|
|
16
|
+
"@dedot/codecs": "0.0.1-alpha.33",
|
|
17
|
+
"@dedot/types": "0.0.1-alpha.33",
|
|
18
|
+
"@dedot/utils": "0.0.1-alpha.33"
|
|
19
|
+
},
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public",
|
|
22
|
+
"directory": "dist"
|
|
23
|
+
},
|
|
24
|
+
"license": "Apache-2.0",
|
|
25
|
+
"gitHead": "36b30a0cec2d8652915e3660329b78f1c6e4221f",
|
|
26
|
+
"module": "./index.js",
|
|
27
|
+
"types": "./index.d.ts",
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"types": "./index.d.ts",
|
|
31
|
+
"import": "./index.js",
|
|
32
|
+
"require": "./cjs/index.js",
|
|
33
|
+
"default": "./index.js"
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { SubstrateApi } from '@dedot/api/chaintypes';
|
|
2
|
+
import { AccountId32Like, BytesLike, Weight } from '@dedot/codecs';
|
|
3
|
+
import { AnyFunc, AsyncMethod, GenericSubstrateApi, RpcVersion } from '@dedot/types';
|
|
4
|
+
import { ContractConstructorMessage, ContractMessage } from './shared.js';
|
|
5
|
+
import { ContractMetadataV4 } from './v4.js';
|
|
6
|
+
import { ContractMetadataV5 } from './v5.js';
|
|
7
|
+
export * from './shared.js';
|
|
8
|
+
export type ContractMetadata = ContractMetadataV4 | ContractMetadataV5;
|
|
9
|
+
export type CallOptions = {
|
|
10
|
+
value?: bigint;
|
|
11
|
+
gasLimit?: Weight | undefined;
|
|
12
|
+
storageDepositLimit?: bigint | undefined;
|
|
13
|
+
};
|
|
14
|
+
export type ConstructorCallOptions = CallOptions & {
|
|
15
|
+
salt: BytesLike;
|
|
16
|
+
caller: AccountId32Like;
|
|
17
|
+
};
|
|
18
|
+
export type ConstructorTxOptions = CallOptions & {
|
|
19
|
+
salt: BytesLike;
|
|
20
|
+
gasLimit: Weight;
|
|
21
|
+
};
|
|
22
|
+
export type ContractCallOptions = CallOptions & {
|
|
23
|
+
caller: AccountId32Like;
|
|
24
|
+
};
|
|
25
|
+
export type ContractTxOptions = CallOptions & {
|
|
26
|
+
gasLimit: Weight;
|
|
27
|
+
};
|
|
28
|
+
export type GenericContractQueryCall<_ extends GenericSubstrateApi, F extends AsyncMethod = AsyncMethod> = F & {
|
|
29
|
+
meta: ContractMessage;
|
|
30
|
+
};
|
|
31
|
+
export type GenericContractTxCall<_ extends GenericSubstrateApi, F extends AnyFunc = AnyFunc> = F & {
|
|
32
|
+
meta: ContractMessage;
|
|
33
|
+
};
|
|
34
|
+
export type GenericConstructorQueryCall<_ extends GenericSubstrateApi, F extends AsyncMethod = AsyncMethod> = F & {
|
|
35
|
+
meta: ContractConstructorMessage;
|
|
36
|
+
};
|
|
37
|
+
export type GenericConstructorTxCall<_ extends GenericSubstrateApi, F extends AnyFunc = AnyFunc> = F & {
|
|
38
|
+
meta: ContractConstructorMessage;
|
|
39
|
+
};
|
|
40
|
+
export interface GenericContractQuery<ChainApi extends GenericSubstrateApi> {
|
|
41
|
+
[method: string]: GenericContractQueryCall<ChainApi>;
|
|
42
|
+
}
|
|
43
|
+
export interface GenericContractTx<ChainApi extends GenericSubstrateApi> {
|
|
44
|
+
[method: string]: GenericContractTxCall<ChainApi>;
|
|
45
|
+
}
|
|
46
|
+
export interface GenericConstructorQuery<ChainApi extends GenericSubstrateApi> {
|
|
47
|
+
[method: string]: GenericConstructorQueryCall<ChainApi>;
|
|
48
|
+
}
|
|
49
|
+
export interface GenericConstructorTx<ChainApi extends GenericSubstrateApi> {
|
|
50
|
+
[method: string]: GenericConstructorTxCall<ChainApi>;
|
|
51
|
+
}
|
|
52
|
+
export interface GenericContractApi<ChainApi extends GenericSubstrateApi = SubstrateApi[RpcVersion]> {
|
|
53
|
+
query: GenericContractQuery<ChainApi>;
|
|
54
|
+
tx: GenericContractTx<ChainApi>;
|
|
55
|
+
constructorQuery: GenericConstructorQuery<ChainApi>;
|
|
56
|
+
constructorTx: GenericConstructorTx<ChainApi>;
|
|
57
|
+
}
|
package/types/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './shared.js';
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
export interface ContractSource {
|
|
2
|
+
hash: string;
|
|
3
|
+
wasm?: string;
|
|
4
|
+
language: string;
|
|
5
|
+
compiler: string;
|
|
6
|
+
buildInfo: BuildInfo;
|
|
7
|
+
}
|
|
8
|
+
export interface ContractInformation {
|
|
9
|
+
name: string;
|
|
10
|
+
version: string;
|
|
11
|
+
authors: string[];
|
|
12
|
+
}
|
|
13
|
+
export interface BuildInfo {
|
|
14
|
+
buildMode: string;
|
|
15
|
+
cargoContractVersion: string;
|
|
16
|
+
rustToolchain: string;
|
|
17
|
+
wasmOptSettings: WasmOptSettings;
|
|
18
|
+
}
|
|
19
|
+
export interface WasmOptSettings {
|
|
20
|
+
keepDebugSymbols: boolean;
|
|
21
|
+
optimizationPasses: string;
|
|
22
|
+
}
|
|
23
|
+
export interface ContractMessageArg {
|
|
24
|
+
label: string;
|
|
25
|
+
type: ContractTypeInfo;
|
|
26
|
+
}
|
|
27
|
+
export interface ContractTypeInfo {
|
|
28
|
+
displayName: string[];
|
|
29
|
+
type: number;
|
|
30
|
+
}
|
|
31
|
+
export interface ContractConstructorMessage {
|
|
32
|
+
args: ContractMessageArg[];
|
|
33
|
+
default: boolean;
|
|
34
|
+
docs: string[];
|
|
35
|
+
label: string;
|
|
36
|
+
payable: boolean;
|
|
37
|
+
returnType: ContractTypeInfo;
|
|
38
|
+
selector: string;
|
|
39
|
+
}
|
|
40
|
+
export interface ContractMessage {
|
|
41
|
+
args: ContractMessageArg[];
|
|
42
|
+
default: boolean;
|
|
43
|
+
docs: string[];
|
|
44
|
+
label: string;
|
|
45
|
+
mutates: boolean;
|
|
46
|
+
payable: boolean;
|
|
47
|
+
returnType: ContractTypeInfo;
|
|
48
|
+
selector: string;
|
|
49
|
+
}
|
|
50
|
+
export interface ContractType {
|
|
51
|
+
id: number;
|
|
52
|
+
type: {
|
|
53
|
+
def: ContractTypeDef;
|
|
54
|
+
path?: string[];
|
|
55
|
+
params?: ParamInfo[];
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
export interface ContractTypeDef {
|
|
59
|
+
composite?: CompositeType;
|
|
60
|
+
array?: ArrayType;
|
|
61
|
+
primitive?: string;
|
|
62
|
+
sequence?: SequenceType;
|
|
63
|
+
variant?: VariantType;
|
|
64
|
+
tuple?: number[];
|
|
65
|
+
}
|
|
66
|
+
export interface CompositeType {
|
|
67
|
+
fields: {
|
|
68
|
+
type: number;
|
|
69
|
+
typeName: string;
|
|
70
|
+
name?: string;
|
|
71
|
+
}[];
|
|
72
|
+
}
|
|
73
|
+
export interface ArrayType {
|
|
74
|
+
len: number;
|
|
75
|
+
type: number;
|
|
76
|
+
}
|
|
77
|
+
export interface SequenceType {
|
|
78
|
+
type: number;
|
|
79
|
+
}
|
|
80
|
+
export interface VariantType {
|
|
81
|
+
variants?: {
|
|
82
|
+
index: number;
|
|
83
|
+
name: string;
|
|
84
|
+
fields?: {
|
|
85
|
+
type: number;
|
|
86
|
+
typeName?: string;
|
|
87
|
+
name?: string;
|
|
88
|
+
}[];
|
|
89
|
+
}[];
|
|
90
|
+
}
|
|
91
|
+
export interface ParamInfo {
|
|
92
|
+
name: string;
|
|
93
|
+
type: number;
|
|
94
|
+
}
|
|
95
|
+
export interface ContractStorage {
|
|
96
|
+
root: {
|
|
97
|
+
layout: {
|
|
98
|
+
struct: {
|
|
99
|
+
fields: any[];
|
|
100
|
+
name: string;
|
|
101
|
+
};
|
|
102
|
+
};
|
|
103
|
+
root_key: string;
|
|
104
|
+
};
|
|
105
|
+
}
|
package/types/shared.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/types/v4.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { ContractConstructorMessage, ContractInformation, ContractMessage, ContractSource, ContractStorage, ContractType, ContractTypeInfo } from './shared.js';
|
|
2
|
+
export interface ContractMetadataV4 {
|
|
3
|
+
source: ContractSource;
|
|
4
|
+
contract: ContractInformation;
|
|
5
|
+
spec: ContractSpecV4;
|
|
6
|
+
storage: ContractStorage;
|
|
7
|
+
types: ContractType[];
|
|
8
|
+
version: '4';
|
|
9
|
+
}
|
|
10
|
+
export interface ContractSpecV4 {
|
|
11
|
+
constructors: ContractConstructorMessage[];
|
|
12
|
+
docs: string[];
|
|
13
|
+
environment: ContractEnvironmentV4;
|
|
14
|
+
events: ContractEventV4[];
|
|
15
|
+
langError: ContractTypeInfo;
|
|
16
|
+
messages: ContractMessage[];
|
|
17
|
+
}
|
|
18
|
+
export interface ContractEventV4 {
|
|
19
|
+
args: unknown[];
|
|
20
|
+
docs: string[];
|
|
21
|
+
label: string[];
|
|
22
|
+
}
|
|
23
|
+
export interface ContractEnvironmentV4 {
|
|
24
|
+
accountId: ContractTypeInfo;
|
|
25
|
+
balance: ContractTypeInfo;
|
|
26
|
+
blockNumber: ContractTypeInfo;
|
|
27
|
+
chainExtension: ContractTypeInfo;
|
|
28
|
+
hash: ContractTypeInfo;
|
|
29
|
+
maxEventTopics: number;
|
|
30
|
+
timestamp: ContractTypeInfo;
|
|
31
|
+
}
|
package/types/v4.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/types/v5.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ContractInformation, ContractSource, ContractStorage, ContractType } from './shared.js';
|
|
2
|
+
import { ContractEnvironmentV4, ContractEventV4, ContractSpecV4 } from './v4.js';
|
|
3
|
+
export interface ContractMetadataV5 {
|
|
4
|
+
source: ContractSource;
|
|
5
|
+
contract: ContractInformation;
|
|
6
|
+
spec: ContractSpecV5;
|
|
7
|
+
storage: ContractStorage;
|
|
8
|
+
types: ContractType[];
|
|
9
|
+
version: '5';
|
|
10
|
+
}
|
|
11
|
+
export interface ContractSpecV5 extends ContractSpecV4 {
|
|
12
|
+
environment: ContractEnvironmentV5;
|
|
13
|
+
events: ContractEventV5[];
|
|
14
|
+
}
|
|
15
|
+
export interface ContractEventV5 extends ContractEventV4 {
|
|
16
|
+
module_path: string;
|
|
17
|
+
signature_topic: string;
|
|
18
|
+
}
|
|
19
|
+
export interface ContractEnvironmentV5 extends ContractEnvironmentV4 {
|
|
20
|
+
staticBufferSize: number;
|
|
21
|
+
}
|
package/types/v5.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/utils.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ISubstrateClient } from '@dedot/api';
|
|
2
|
+
import { PortableType, TypeDef } from '@dedot/codecs';
|
|
3
|
+
import { GenericSubstrateApi } from '@dedot/types';
|
|
4
|
+
import { Executor } from './executor/index.js';
|
|
5
|
+
import { ContractMetadata, ContractTypeDef } from './types/index.js';
|
|
6
|
+
export declare const extractContractTypes: (contractMetadata: ContractMetadata) => PortableType[];
|
|
7
|
+
export declare const normalizeContractTypeDef: (def: ContractTypeDef) => TypeDef;
|
|
8
|
+
export declare const parseRawMetadata: (rawMetadata: string) => ContractMetadata;
|
|
9
|
+
export declare function newProxyChain<ChainApi extends GenericSubstrateApi>(carrier: Executor<ChainApi>): unknown;
|
|
10
|
+
export declare function ensureSupportContractsPallet<ChainApi extends GenericSubstrateApi>(api: ISubstrateClient<ChainApi>): void;
|
|
11
|
+
export declare function normalizeLabel(label?: string): string;
|