@dedot/contracts 0.1.1 → 0.3.0

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 CHANGED
@@ -7,6 +7,7 @@ export declare class Contract<ContractApi extends GenericContractApi = GenericCo
7
7
  #private;
8
8
  constructor(api: ISubstrateClient, metadata: ContractMetadata | string, address: AccountId32Like);
9
9
  decodeEvent(eventRecord: IEventRecord): ContractEvent;
10
+ decodeEvents(eventRecords: IEventRecord[]): ContractEvent[];
10
11
  get metadata(): ContractMetadata;
11
12
  get address(): AccountId32;
12
13
  get registry(): TypinkRegistry;
package/Contract.js CHANGED
@@ -17,6 +17,9 @@ export class Contract {
17
17
  decodeEvent(eventRecord) {
18
18
  return this.#registry.decodeEvent(eventRecord);
19
19
  }
20
+ decodeEvents(eventRecords) {
21
+ return this.#registry.decodeEvents(eventRecords);
22
+ }
20
23
  get metadata() {
21
24
  return this.#metadata;
22
25
  }
@@ -5,5 +5,6 @@ export declare class TypinkRegistry extends TypeRegistry {
5
5
  #private;
6
6
  constructor(metadata: ContractMetadata);
7
7
  get metadata(): ContractMetadata;
8
+ decodeEvents(records: IEventRecord[]): ContractEvent[];
8
9
  decodeEvent(eventRecord: IEventRecord): ContractEvent;
9
10
  }
package/TypinkRegistry.js CHANGED
@@ -11,6 +11,11 @@ export class TypinkRegistry extends TypeRegistry {
11
11
  get metadata() {
12
12
  return this.#metadata;
13
13
  }
14
+ decodeEvents(records) {
15
+ return records
16
+ .filter(({ event }) => this.#isContractEmittedEvent(event)) // prettier-end-here
17
+ .map((record) => this.decodeEvent(record));
18
+ }
14
19
  decodeEvent(eventRecord) {
15
20
  const { version } = this.#metadata;
16
21
  switch (version) {
@@ -22,9 +27,9 @@ export class TypinkRegistry extends TypeRegistry {
22
27
  throw new DedotError('Unsupported metadata version!');
23
28
  }
24
29
  }
25
- #isContractEmittedEvent(eventRecord) {
30
+ #isContractEmittedEvent(event) {
26
31
  // @ts-ignore
27
- return eventRecord.pallet === 'Contracts' && eventRecord.palletEvent.name === 'ContractEmitted';
32
+ return event.pallet === 'Contracts' && event.palletEvent.name === 'ContractEmitted';
28
33
  }
29
34
  #decodeEventV4(eventRecord) {
30
35
  assert(this.#metadata.version === '4', 'Invalid metadata version!');
package/cjs/Contract.js CHANGED
@@ -20,6 +20,9 @@ class Contract {
20
20
  decodeEvent(eventRecord) {
21
21
  return this.#registry.decodeEvent(eventRecord);
22
22
  }
23
+ decodeEvents(eventRecords) {
24
+ return this.#registry.decodeEvents(eventRecords);
25
+ }
23
26
  get metadata() {
24
27
  return this.#metadata;
25
28
  }
@@ -37,6 +37,11 @@ class TypinkRegistry extends codecs_1.TypeRegistry {
37
37
  get metadata() {
38
38
  return this.#metadata;
39
39
  }
40
+ decodeEvents(records) {
41
+ return records
42
+ .filter(({ event }) => this.#isContractEmittedEvent(event)) // prettier-end-here
43
+ .map((record) => this.decodeEvent(record));
44
+ }
40
45
  decodeEvent(eventRecord) {
41
46
  const { version } = this.#metadata;
42
47
  switch (version) {
@@ -48,9 +53,9 @@ class TypinkRegistry extends codecs_1.TypeRegistry {
48
53
  throw new utils_1.DedotError('Unsupported metadata version!');
49
54
  }
50
55
  }
51
- #isContractEmittedEvent(eventRecord) {
56
+ #isContractEmittedEvent(event) {
52
57
  // @ts-ignore
53
- return eventRecord.pallet === 'Contracts' && eventRecord.palletEvent.name === 'ContractEmitted';
58
+ return event.pallet === 'Contracts' && event.palletEvent.name === 'ContractEmitted';
54
59
  }
55
60
  #decodeEventV4(eventRecord) {
56
61
  (0, utils_1.assert)(this.#metadata.version === '4', 'Invalid metadata version!');
package/cjs/errors.js ADDED
@@ -0,0 +1,264 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isContractInstantiateLangError = exports.isContractInstantiateDispatchError = exports.isContractInstantiateError = exports.isContractLangError = exports.isContractDispatchError = exports.isContractExecutionError = exports.ContractLangError = exports.ContractDispatchError = exports.ContractExecutionError = exports.ContractInstantiateLangError = exports.ContractInstantiateDispatchError = exports.ContractInstantiateError = void 0;
4
+ const utils_1 = require("@dedot/utils");
5
+ const utils_js_1 = require("./utils.js");
6
+ /**
7
+ * Represents an error that occurred during the instantiation of a smart contract.
8
+ * This class extends the base `DedotError` and includes a `raw` property of type `ContractInstantiateResult`.
9
+ *
10
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
11
+ *
12
+ * @extends DedotError
13
+ */
14
+ class ContractInstantiateError extends utils_1.DedotError {
15
+ /**
16
+ * The raw result of the contract instantiation.
17
+ */
18
+ raw;
19
+ /**
20
+ * Constructs a new `ContractInstantiateError` instance.
21
+ *
22
+ * @param raw - The raw result of the contract instantiation.
23
+ */
24
+ constructor(raw) {
25
+ super();
26
+ this.raw = raw;
27
+ }
28
+ }
29
+ exports.ContractInstantiateError = ContractInstantiateError;
30
+ /**
31
+ * Represents an error that occurred during the dispatch phase of contract instantiation.
32
+ * This class extends `ContractInstantiateError` and includes a `DispatchError` property.
33
+ *
34
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
35
+ *
36
+ * @extends ContractInstantiateError
37
+ */
38
+ class ContractInstantiateDispatchError extends ContractInstantiateError {
39
+ /**
40
+ * The error that occurred during the dispatch phase.
41
+ */
42
+ dispatchError;
43
+ /**
44
+ * Constructs a new `ContractInstantiateDispatchError` instance.
45
+ *
46
+ * @param err - The `DispatchError` that occurred during the dispatch phase.
47
+ * @param raw - The raw result of the contract instantiation.
48
+ */
49
+ constructor(err, raw) {
50
+ super(raw);
51
+ this.dispatchError = err;
52
+ }
53
+ }
54
+ exports.ContractInstantiateDispatchError = ContractInstantiateDispatchError;
55
+ /**
56
+ * Represents an error that occurred during the instantiation of a smart contract due to a language-specific error.
57
+ * This class extends `ContractInstantiateError` and includes a `LangError` property.
58
+ *
59
+ * Ref: https://use.ink/faq/migrating-from-ink-3-to-4#:~:text=Add%20support%20for,equivalent%20LangError.
60
+ *
61
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
62
+ *
63
+ * @extends ContractInstantiateError
64
+ */
65
+ class ContractInstantiateLangError extends ContractInstantiateError {
66
+ /**
67
+ * The language-specific error that occurred during the instantiation.
68
+ */
69
+ langError;
70
+ /**
71
+ * Decoded `ReturnFlags` from contract call result.
72
+ */
73
+ flags;
74
+ /**
75
+ * Constructs a new `ContractInstantiateLangError` instance.
76
+ *
77
+ * @param err - The `LangError` that occurred during the instantiation phase.
78
+ * @param raw - The raw result of the contract instantiation.
79
+ */
80
+ constructor(err, raw) {
81
+ (0, utils_1.assert)(raw.result.isOk, 'Should not throw DispatchError!');
82
+ super(raw);
83
+ this.langError = err;
84
+ this.flags = (0, utils_js_1.toReturnFlags)(raw.result.value.result.flags.bits);
85
+ }
86
+ }
87
+ exports.ContractInstantiateLangError = ContractInstantiateLangError;
88
+ /**
89
+ * Represents an error that occurred during the execution of a smart contract call.
90
+ * This class extends the base `DedotError` and includes a `raw` property of type `ContractCallResult`.
91
+ *
92
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
93
+ *
94
+ * @extends DedotError
95
+ */
96
+ class ContractExecutionError extends utils_1.DedotError {
97
+ /**
98
+ * The raw result of the contract call.
99
+ */
100
+ raw;
101
+ /**
102
+ * Constructs a new `ContractExecutionError` instance.
103
+ *
104
+ * @param raw - The raw result of the contract call.
105
+ */
106
+ constructor(raw) {
107
+ super();
108
+ this.raw = raw;
109
+ }
110
+ }
111
+ exports.ContractExecutionError = ContractExecutionError;
112
+ /**
113
+ * Represents an error that occurred during the execution of a smart contract call due to a dispatch error.
114
+ * This class extends `ContractExecutionError` and includes a `DispatchError` property.
115
+ *
116
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
117
+ *
118
+ * @extends ContractExecutionError
119
+ */
120
+ class ContractDispatchError extends ContractExecutionError {
121
+ /**
122
+ * The error that occurred during the dispatch phase.
123
+ */
124
+ dispatchError;
125
+ /**
126
+ * Constructs a new `ContractDispatchError` instance.
127
+ *
128
+ * @param err - The `DispatchError` that occurred during the dispatch phase.
129
+ * @param raw - The raw result of the contract call.
130
+ */
131
+ constructor(err, raw) {
132
+ super(raw);
133
+ this.dispatchError = err;
134
+ }
135
+ }
136
+ exports.ContractDispatchError = ContractDispatchError;
137
+ /**
138
+ * Represents an error that occurred during the execution of a smart contract call due to a language-specific error.
139
+ * This class extends `ContractExecutionError` and includes a `LangError` property.
140
+ *
141
+ * Ref: https://use.ink/faq/migrating-from-ink-3-to-4#:~:text=Add%20support%20for,equivalent%20LangError.
142
+ *
143
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
144
+ *
145
+ * @extends ContractExecutionError
146
+ */
147
+ class ContractLangError extends ContractExecutionError {
148
+ /**
149
+ * The language-specific error that occurred during the execution.
150
+ */
151
+ langError;
152
+ /**
153
+ * Decoded `ReturnFlags` from contract call result.
154
+ */
155
+ flags;
156
+ /**
157
+ * Constructs a new `ContractLangError` instance.
158
+ *
159
+ * @param err - The `LangError` that occurred during the execution phase.
160
+ * @param raw - The raw result of the contract call.
161
+ */
162
+ constructor(err, raw) {
163
+ (0, utils_1.assert)(raw.result.isOk, 'Should not throw DispatchError!');
164
+ super(raw);
165
+ this.langError = err;
166
+ this.flags = (0, utils_js_1.toReturnFlags)(raw.result.value.flags.bits);
167
+ }
168
+ }
169
+ exports.ContractLangError = ContractLangError;
170
+ /**
171
+ * Checks if the provided error is an instance of `ContractExecutionError`.
172
+ *
173
+ * This function is used to determine if a given error is a result of an execution error during a smart contract call.
174
+ *
175
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
176
+ *
177
+ * @param e - The error to be checked.
178
+ *
179
+ * @returns `true` if the error is an instance of `ContractExecutionError`, `false` otherwise.
180
+ */
181
+ function isContractExecutionError(e) {
182
+ return e instanceof ContractExecutionError;
183
+ }
184
+ exports.isContractExecutionError = isContractExecutionError;
185
+ /**
186
+ * Checks if the provided error is an instance of `ContractDispatchError`.
187
+ *
188
+ * This function is used to determine if a given error is a result of a dispatch error during a smart contract call.
189
+ *
190
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
191
+ *
192
+ * @param e - The error to be checked. This should be an instance of `Error`.
193
+ *
194
+ * @returns `true` if the error is an instance of `ContractDispatchError`, `false` otherwise.
195
+ * This function returns a boolean value indicating whether the provided error is of type `ContractDispatchError`.
196
+ */
197
+ function isContractDispatchError(e) {
198
+ return e instanceof ContractDispatchError;
199
+ }
200
+ exports.isContractDispatchError = isContractDispatchError;
201
+ /**
202
+ * Checks if the provided error is an instance of `ContractLangError`.
203
+ *
204
+ * This function is used to determine if a given error is a result of a language-specific error during a smart contract call.
205
+ *
206
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
207
+ *
208
+ * @param e - The error to be checked. This should be an instance of `Error`.
209
+ *
210
+ * @returns `true` if the error is an instance of `ContractLangError`, `false` otherwise.
211
+ * This function returns a boolean value indicating whether the provided error is of type `ContractLangError`.
212
+ */
213
+ function isContractLangError(e) {
214
+ return e instanceof ContractLangError;
215
+ }
216
+ exports.isContractLangError = isContractLangError;
217
+ /**
218
+ * Checks if the provided error is an instance of `ContractInstantiateError`.
219
+ *
220
+ * This function is used to determine if a given error is a result of an execution error during the instantiation of a smart contract.
221
+ *
222
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
223
+ *
224
+ * @param e - The error to be checked. This should be an instance of `Error`.
225
+ *
226
+ * @returns `true` if the error is an instance of `ContractInstantiateError`, `false` otherwise.
227
+ * This function returns a boolean value indicating whether the provided error is of type `ContractInstantiateError`.
228
+ */
229
+ function isContractInstantiateError(e) {
230
+ return e instanceof ContractInstantiateError;
231
+ }
232
+ exports.isContractInstantiateError = isContractInstantiateError;
233
+ /**
234
+ * Checks if the provided error is an instance of `ContractInstantiateDispatchError`.
235
+ *
236
+ * This function is used to determine if a given error is a result of a dispatch error during the instantiation of a smart contract.
237
+ *
238
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
239
+ *
240
+ * @param e - The error to be checked. This should be an instance of `Error`.
241
+ *
242
+ * @returns `true` if the error is an instance of `ContractInstantiateDispatchError`, `false` otherwise.
243
+ * This function returns a boolean value indicating whether the provided error is of type `ContractInstantiateDispatchError`.
244
+ */
245
+ function isContractInstantiateDispatchError(e) {
246
+ return e instanceof ContractInstantiateDispatchError;
247
+ }
248
+ exports.isContractInstantiateDispatchError = isContractInstantiateDispatchError;
249
+ /**
250
+ * Checks if the provided error is an instance of `ContractInstantiateLangError`.
251
+ *
252
+ * This function is used to determine if a given error is a result of a language-specific error during the instantiation of a smart contract.
253
+ *
254
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
255
+ *
256
+ * @param e - The error to be checked. This should be an instance of `Error`.
257
+ *
258
+ * @returns `true` if the error is an instance of `ContractInstantiateLangError`, `false` otherwise.
259
+ * This function returns a boolean value indicating whether the provided error is of type `ContractInstantiateLangError`.
260
+ */
261
+ function isContractInstantiateLangError(e) {
262
+ return e instanceof ContractInstantiateLangError;
263
+ }
264
+ exports.isContractInstantiateLangError = isContractInstantiateLangError;
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ConstructorQueryExecutor = void 0;
4
4
  const utils_1 = require("@dedot/utils");
5
+ const errors_js_1 = require("../errors.js");
5
6
  const utils_js_1 = require("../utils.js");
6
7
  const Executor_js_1 = require("./Executor.js");
7
8
  class ConstructorQueryExecutor extends Executor_js_1.Executor {
@@ -13,7 +14,7 @@ class ConstructorQueryExecutor extends Executor_js_1.Executor {
13
14
  doExecute(constructor) {
14
15
  const meta = this.#findConstructorMeta(constructor);
15
16
  (0, utils_1.assert)(meta, `Constructor message not found: ${constructor}`);
16
- const callFn = (...params) => {
17
+ const callFn = async (...params) => {
17
18
  const { args } = meta;
18
19
  (0, utils_1.assert)(params.length === args.length + 1, `Expected ${args.length + 1} arguments, got ${params.length}`);
19
20
  const callOptions = params[args.length];
@@ -26,7 +27,21 @@ class ConstructorQueryExecutor extends Executor_js_1.Executor {
26
27
  type: (0, utils_1.isWasm)(this.code) ? 'Upload' : 'Existing',
27
28
  value: this.code,
28
29
  };
29
- return this.api.call.contractsApi.instantiate(caller, value, gasLimit, storageDepositLimit, code, bytes, salt);
30
+ const raw = await this.api.call.contractsApi.instantiate(caller, value, gasLimit, storageDepositLimit, code, bytes, salt);
31
+ if (raw.result.isErr) {
32
+ throw new errors_js_1.ContractInstantiateDispatchError(raw.result.err, raw);
33
+ }
34
+ const data = this.tryDecode(meta, raw.result.value.result.data);
35
+ if (data.isErr) {
36
+ throw new errors_js_1.ContractInstantiateLangError(data.err, raw);
37
+ }
38
+ const bits = raw.result.value.result.flags.bits;
39
+ return {
40
+ data: data.value,
41
+ raw,
42
+ address: raw.result.value.accountId,
43
+ flags: (0, utils_js_1.toReturnFlags)(bits),
44
+ };
30
45
  };
31
46
  callFn.meta = meta;
32
47
  return callFn;
@@ -1,26 +1,51 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.EventExecutor = void 0;
4
- const Executor_js_1 = require("./Executor.js");
4
+ const api_1 = require("@dedot/api");
5
5
  const utils_1 = require("@dedot/utils");
6
+ const Executor_js_1 = require("./Executor.js");
6
7
  class EventExecutor extends Executor_js_1.Executor {
7
8
  doExecute(eventName) {
8
- const eventMeta = this.#findEventMeta(eventName);
9
- (0, utils_1.assert)(eventMeta, "Contract event metadata not found!");
9
+ const meta = this.#findEventMeta(eventName);
10
+ (0, utils_1.assert)(meta, 'Contract event metadata not found!');
10
11
  const is = (event) => {
12
+ if ((0, api_1.isEventRecord)(event)) {
13
+ try {
14
+ event = this.registry.decodeEvent(event);
15
+ }
16
+ catch {
17
+ return false;
18
+ }
19
+ }
11
20
  return event.name === eventName;
12
21
  };
13
- const as = (event) => {
14
- return is(event) ? event : undefined;
22
+ const find = (events) => {
23
+ if (!events || events.length === 0)
24
+ return undefined;
25
+ if ((0, api_1.isEventRecord)(events[0])) {
26
+ return this.registry.decodeEvents(events).find(is);
27
+ }
28
+ else {
29
+ return events.find(is);
30
+ }
31
+ };
32
+ const filter = (events) => {
33
+ if ((0, api_1.isEventRecord)(events[0])) {
34
+ return this.registry.decodeEvents(events).filter(is);
35
+ }
36
+ else {
37
+ return events.filter(is);
38
+ }
15
39
  };
16
40
  return {
17
41
  is,
18
- as,
19
- meta: eventMeta,
42
+ find,
43
+ filter,
44
+ meta,
20
45
  };
21
46
  }
22
47
  #findEventMeta(eventName) {
23
- return this.registry.metadata.spec.events.find(one => (0, utils_1.stringPascalCase)(one.label) === eventName);
48
+ return this.registry.metadata.spec.events.find((one) => (0, utils_1.stringPascalCase)(one.label) === eventName);
24
49
  }
25
50
  }
26
51
  exports.EventExecutor = EventExecutor;
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.QueryExecutor = void 0;
4
4
  const utils_1 = require("@dedot/utils");
5
+ const errors_js_1 = require("../errors.js");
5
6
  const utils_js_1 = require("../utils.js");
6
7
  const Executor_js_1 = require("./Executor.js");
7
8
  class QueryExecutor extends Executor_js_1.Executor {
@@ -17,17 +18,19 @@ class QueryExecutor extends Executor_js_1.Executor {
17
18
  const formattedInputs = args.map((arg, index) => this.tryEncode(arg, params[index]));
18
19
  const bytes = (0, utils_1.u8aToHex)((0, utils_1.concatU8a)((0, utils_1.hexToU8a)(meta.selector), ...formattedInputs));
19
20
  const raw = await this.api.call.contractsApi.call(caller, this.address, value, gasLimit, storageDepositLimit, bytes);
20
- return (raw.result.isOk
21
- ? {
22
- isOk: true,
23
- data: this.tryDecode(meta, raw.result.value.data),
24
- raw,
25
- }
26
- : {
27
- isOk: false,
28
- err: raw.result.err,
29
- raw,
30
- });
21
+ if (raw.result.isErr) {
22
+ throw new errors_js_1.ContractDispatchError(raw.result.err, raw);
23
+ }
24
+ const data = this.tryDecode(meta, raw.result.value.data);
25
+ if (data.isErr) {
26
+ throw new errors_js_1.ContractLangError(data.err, raw);
27
+ }
28
+ const bits = raw.result.value.flags.bits;
29
+ return {
30
+ data: data.value,
31
+ raw,
32
+ flags: (0, utils_js_1.toReturnFlags)(bits),
33
+ };
31
34
  };
32
35
  callFn.meta = meta;
33
36
  return callFn;
package/cjs/index.js CHANGED
@@ -15,6 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./utils.js"), exports);
18
+ __exportStar(require("./errors.js"), exports);
18
19
  __exportStar(require("./executor/index.js"), exports);
19
20
  __exportStar(require("./types/index.js"), exports);
20
21
  __exportStar(require("./TypinkRegistry.js"), exports);
package/cjs/utils.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.normalizeLabel = exports.ensureSupportContractsPallet = exports.newProxyChain = exports.parseRawMetadata = exports.normalizeContractTypeDef = exports.extractContractTypes = void 0;
3
+ exports.toReturnFlags = exports.normalizeLabel = exports.ensureSupportContractsPallet = exports.newProxyChain = exports.parseRawMetadata = exports.normalizeContractTypeDef = exports.extractContractTypes = void 0;
4
4
  const utils_1 = require("@dedot/utils");
5
5
  const extractContractTypes = (contractMetadata) => {
6
6
  const { types } = contractMetadata;
@@ -106,3 +106,12 @@ function normalizeLabel(label) {
106
106
  return (0, utils_1.stringCamelCase)(label.replaceAll('::', '_'));
107
107
  }
108
108
  exports.normalizeLabel = normalizeLabel;
109
+ // https://github.com/paritytech/polkadot-sdk/blob/d2fd53645654d3b8e12cbf735b67b93078d70113/substrate/frame/contracts/uapi/src/flags.rs#L23-L26
110
+ const REVERT_FLAG = 1;
111
+ function toReturnFlags(bits) {
112
+ return {
113
+ bits,
114
+ revert: bits === REVERT_FLAG,
115
+ };
116
+ }
117
+ exports.toReturnFlags = toReturnFlags;
package/errors.d.ts ADDED
@@ -0,0 +1,216 @@
1
+ import { DispatchError } from '@dedot/codecs';
2
+ import { DedotError } from '@dedot/utils';
3
+ import { ContractCallResult, ContractInstantiateResult, GenericContractApi, ReturnFlags } from './types/index.js';
4
+ /**
5
+ * Represents an error that occurred during the instantiation of a smart contract.
6
+ * This class extends the base `DedotError` and includes a `raw` property of type `ContractInstantiateResult`.
7
+ *
8
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
9
+ *
10
+ * @extends DedotError
11
+ */
12
+ export declare class ContractInstantiateError<ContractApi extends GenericContractApi = GenericContractApi> extends DedotError {
13
+ /**
14
+ * The raw result of the contract instantiation.
15
+ */
16
+ raw: ContractInstantiateResult<ContractApi['types']['ChainApi']>;
17
+ /**
18
+ * Constructs a new `ContractInstantiateError` instance.
19
+ *
20
+ * @param raw - The raw result of the contract instantiation.
21
+ */
22
+ constructor(raw: ContractInstantiateResult<ContractApi['types']['ChainApi']>);
23
+ }
24
+ /**
25
+ * Represents an error that occurred during the dispatch phase of contract instantiation.
26
+ * This class extends `ContractInstantiateError` and includes a `DispatchError` property.
27
+ *
28
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
29
+ *
30
+ * @extends ContractInstantiateError
31
+ */
32
+ export declare class ContractInstantiateDispatchError<ContractApi extends GenericContractApi = GenericContractApi> extends ContractInstantiateError<ContractApi> {
33
+ /**
34
+ * The error that occurred during the dispatch phase.
35
+ */
36
+ dispatchError: DispatchError;
37
+ /**
38
+ * Constructs a new `ContractInstantiateDispatchError` instance.
39
+ *
40
+ * @param err - The `DispatchError` that occurred during the dispatch phase.
41
+ * @param raw - The raw result of the contract instantiation.
42
+ */
43
+ constructor(err: DispatchError, raw: ContractInstantiateResult<ContractApi['types']['ChainApi']>);
44
+ }
45
+ /**
46
+ * Represents an error that occurred during the instantiation of a smart contract due to a language-specific error.
47
+ * This class extends `ContractInstantiateError` and includes a `LangError` property.
48
+ *
49
+ * Ref: https://use.ink/faq/migrating-from-ink-3-to-4#:~:text=Add%20support%20for,equivalent%20LangError.
50
+ *
51
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
52
+ *
53
+ * @extends ContractInstantiateError
54
+ */
55
+ export declare class ContractInstantiateLangError<ContractApi extends GenericContractApi = GenericContractApi> extends ContractInstantiateError<ContractApi> {
56
+ /**
57
+ * The language-specific error that occurred during the instantiation.
58
+ */
59
+ langError: ContractApi['types']['LangError'];
60
+ /**
61
+ * Decoded `ReturnFlags` from contract call result.
62
+ */
63
+ flags: ReturnFlags;
64
+ /**
65
+ * Constructs a new `ContractInstantiateLangError` instance.
66
+ *
67
+ * @param err - The `LangError` that occurred during the instantiation phase.
68
+ * @param raw - The raw result of the contract instantiation.
69
+ */
70
+ constructor(err: ContractApi['types']['LangError'], raw: ContractInstantiateResult<ContractApi['types']['ChainApi']>);
71
+ }
72
+ /**
73
+ * Represents an error that occurred during the execution of a smart contract call.
74
+ * This class extends the base `DedotError` and includes a `raw` property of type `ContractCallResult`.
75
+ *
76
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
77
+ *
78
+ * @extends DedotError
79
+ */
80
+ export declare class ContractExecutionError<ContractApi extends GenericContractApi = GenericContractApi> extends DedotError {
81
+ /**
82
+ * The raw result of the contract call.
83
+ */
84
+ raw: ContractCallResult<ContractApi['types']['ChainApi']>;
85
+ /**
86
+ * Constructs a new `ContractExecutionError` instance.
87
+ *
88
+ * @param raw - The raw result of the contract call.
89
+ */
90
+ constructor(raw: ContractCallResult<ContractApi['types']['ChainApi']>);
91
+ }
92
+ /**
93
+ * Represents an error that occurred during the execution of a smart contract call due to a dispatch error.
94
+ * This class extends `ContractExecutionError` and includes a `DispatchError` property.
95
+ *
96
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
97
+ *
98
+ * @extends ContractExecutionError
99
+ */
100
+ export declare class ContractDispatchError<ContractApi extends GenericContractApi = GenericContractApi> extends ContractExecutionError<ContractApi> {
101
+ /**
102
+ * The error that occurred during the dispatch phase.
103
+ */
104
+ dispatchError: DispatchError;
105
+ /**
106
+ * Constructs a new `ContractDispatchError` instance.
107
+ *
108
+ * @param err - The `DispatchError` that occurred during the dispatch phase.
109
+ * @param raw - The raw result of the contract call.
110
+ */
111
+ constructor(err: DispatchError, raw: ContractCallResult<ContractApi['types']['ChainApi']>);
112
+ }
113
+ /**
114
+ * Represents an error that occurred during the execution of a smart contract call due to a language-specific error.
115
+ * This class extends `ContractExecutionError` and includes a `LangError` property.
116
+ *
117
+ * Ref: https://use.ink/faq/migrating-from-ink-3-to-4#:~:text=Add%20support%20for,equivalent%20LangError.
118
+ *
119
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
120
+ *
121
+ * @extends ContractExecutionError
122
+ */
123
+ export declare class ContractLangError<ContractApi extends GenericContractApi = GenericContractApi> extends ContractExecutionError<ContractApi> {
124
+ /**
125
+ * The language-specific error that occurred during the execution.
126
+ */
127
+ langError: ContractApi['types']['LangError'];
128
+ /**
129
+ * Decoded `ReturnFlags` from contract call result.
130
+ */
131
+ flags: ReturnFlags;
132
+ /**
133
+ * Constructs a new `ContractLangError` instance.
134
+ *
135
+ * @param err - The `LangError` that occurred during the execution phase.
136
+ * @param raw - The raw result of the contract call.
137
+ */
138
+ constructor(err: ContractApi['types']['LangError'], raw: ContractCallResult<ContractApi['types']['ChainApi']>);
139
+ }
140
+ /**
141
+ * Checks if the provided error is an instance of `ContractExecutionError`.
142
+ *
143
+ * This function is used to determine if a given error is a result of an execution error during a smart contract call.
144
+ *
145
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
146
+ *
147
+ * @param e - The error to be checked.
148
+ *
149
+ * @returns `true` if the error is an instance of `ContractExecutionError`, `false` otherwise.
150
+ */
151
+ export declare function isContractExecutionError<ContractApi extends GenericContractApi = GenericContractApi>(e: Error): e is ContractExecutionError<ContractApi>;
152
+ /**
153
+ * Checks if the provided error is an instance of `ContractDispatchError`.
154
+ *
155
+ * This function is used to determine if a given error is a result of a dispatch error during a smart contract call.
156
+ *
157
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
158
+ *
159
+ * @param e - The error to be checked. This should be an instance of `Error`.
160
+ *
161
+ * @returns `true` if the error is an instance of `ContractDispatchError`, `false` otherwise.
162
+ * This function returns a boolean value indicating whether the provided error is of type `ContractDispatchError`.
163
+ */
164
+ export declare function isContractDispatchError<ContractApi extends GenericContractApi = GenericContractApi>(e: Error): e is ContractDispatchError<ContractApi>;
165
+ /**
166
+ * Checks if the provided error is an instance of `ContractLangError`.
167
+ *
168
+ * This function is used to determine if a given error is a result of a language-specific error during a smart contract call.
169
+ *
170
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
171
+ *
172
+ * @param e - The error to be checked. This should be an instance of `Error`.
173
+ *
174
+ * @returns `true` if the error is an instance of `ContractLangError`, `false` otherwise.
175
+ * This function returns a boolean value indicating whether the provided error is of type `ContractLangError`.
176
+ */
177
+ export declare function isContractLangError<ContractApi extends GenericContractApi = GenericContractApi>(e: Error): e is ContractLangError<ContractApi>;
178
+ /**
179
+ * Checks if the provided error is an instance of `ContractInstantiateError`.
180
+ *
181
+ * This function is used to determine if a given error is a result of an execution error during the instantiation of a smart contract.
182
+ *
183
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
184
+ *
185
+ * @param e - The error to be checked. This should be an instance of `Error`.
186
+ *
187
+ * @returns `true` if the error is an instance of `ContractInstantiateError`, `false` otherwise.
188
+ * This function returns a boolean value indicating whether the provided error is of type `ContractInstantiateError`.
189
+ */
190
+ export declare function isContractInstantiateError<ContractApi extends GenericContractApi = GenericContractApi>(e: Error): e is ContractInstantiateError<ContractApi>;
191
+ /**
192
+ * Checks if the provided error is an instance of `ContractInstantiateDispatchError`.
193
+ *
194
+ * This function is used to determine if a given error is a result of a dispatch error during the instantiation of a smart contract.
195
+ *
196
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
197
+ *
198
+ * @param e - The error to be checked. This should be an instance of `Error`.
199
+ *
200
+ * @returns `true` if the error is an instance of `ContractInstantiateDispatchError`, `false` otherwise.
201
+ * This function returns a boolean value indicating whether the provided error is of type `ContractInstantiateDispatchError`.
202
+ */
203
+ export declare function isContractInstantiateDispatchError<ContractApi extends GenericContractApi = GenericContractApi>(e: Error): e is ContractInstantiateDispatchError<ContractApi>;
204
+ /**
205
+ * Checks if the provided error is an instance of `ContractInstantiateLangError`.
206
+ *
207
+ * This function is used to determine if a given error is a result of a language-specific error during the instantiation of a smart contract.
208
+ *
209
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
210
+ *
211
+ * @param e - The error to be checked. This should be an instance of `Error`.
212
+ *
213
+ * @returns `true` if the error is an instance of `ContractInstantiateLangError`, `false` otherwise.
214
+ * This function returns a boolean value indicating whether the provided error is of type `ContractInstantiateLangError`.
215
+ */
216
+ export declare function isContractInstantiateLangError<ContractApi extends GenericContractApi = GenericContractApi>(e: Error): e is ContractInstantiateLangError<ContractApi>;
package/errors.js ADDED
@@ -0,0 +1,249 @@
1
+ import { DedotError, assert } from '@dedot/utils';
2
+ import { toReturnFlags } from './utils.js';
3
+ /**
4
+ * Represents an error that occurred during the instantiation of a smart contract.
5
+ * This class extends the base `DedotError` and includes a `raw` property of type `ContractInstantiateResult`.
6
+ *
7
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
8
+ *
9
+ * @extends DedotError
10
+ */
11
+ export class ContractInstantiateError extends DedotError {
12
+ /**
13
+ * The raw result of the contract instantiation.
14
+ */
15
+ raw;
16
+ /**
17
+ * Constructs a new `ContractInstantiateError` instance.
18
+ *
19
+ * @param raw - The raw result of the contract instantiation.
20
+ */
21
+ constructor(raw) {
22
+ super();
23
+ this.raw = raw;
24
+ }
25
+ }
26
+ /**
27
+ * Represents an error that occurred during the dispatch phase of contract instantiation.
28
+ * This class extends `ContractInstantiateError` and includes a `DispatchError` property.
29
+ *
30
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
31
+ *
32
+ * @extends ContractInstantiateError
33
+ */
34
+ export class ContractInstantiateDispatchError extends ContractInstantiateError {
35
+ /**
36
+ * The error that occurred during the dispatch phase.
37
+ */
38
+ dispatchError;
39
+ /**
40
+ * Constructs a new `ContractInstantiateDispatchError` instance.
41
+ *
42
+ * @param err - The `DispatchError` that occurred during the dispatch phase.
43
+ * @param raw - The raw result of the contract instantiation.
44
+ */
45
+ constructor(err, raw) {
46
+ super(raw);
47
+ this.dispatchError = err;
48
+ }
49
+ }
50
+ /**
51
+ * Represents an error that occurred during the instantiation of a smart contract due to a language-specific error.
52
+ * This class extends `ContractInstantiateError` and includes a `LangError` property.
53
+ *
54
+ * Ref: https://use.ink/faq/migrating-from-ink-3-to-4#:~:text=Add%20support%20for,equivalent%20LangError.
55
+ *
56
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
57
+ *
58
+ * @extends ContractInstantiateError
59
+ */
60
+ export class ContractInstantiateLangError extends ContractInstantiateError {
61
+ /**
62
+ * The language-specific error that occurred during the instantiation.
63
+ */
64
+ langError;
65
+ /**
66
+ * Decoded `ReturnFlags` from contract call result.
67
+ */
68
+ flags;
69
+ /**
70
+ * Constructs a new `ContractInstantiateLangError` instance.
71
+ *
72
+ * @param err - The `LangError` that occurred during the instantiation phase.
73
+ * @param raw - The raw result of the contract instantiation.
74
+ */
75
+ constructor(err, raw) {
76
+ assert(raw.result.isOk, 'Should not throw DispatchError!');
77
+ super(raw);
78
+ this.langError = err;
79
+ this.flags = toReturnFlags(raw.result.value.result.flags.bits);
80
+ }
81
+ }
82
+ /**
83
+ * Represents an error that occurred during the execution of a smart contract call.
84
+ * This class extends the base `DedotError` and includes a `raw` property of type `ContractCallResult`.
85
+ *
86
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
87
+ *
88
+ * @extends DedotError
89
+ */
90
+ export class ContractExecutionError extends DedotError {
91
+ /**
92
+ * The raw result of the contract call.
93
+ */
94
+ raw;
95
+ /**
96
+ * Constructs a new `ContractExecutionError` instance.
97
+ *
98
+ * @param raw - The raw result of the contract call.
99
+ */
100
+ constructor(raw) {
101
+ super();
102
+ this.raw = raw;
103
+ }
104
+ }
105
+ /**
106
+ * Represents an error that occurred during the execution of a smart contract call due to a dispatch error.
107
+ * This class extends `ContractExecutionError` and includes a `DispatchError` property.
108
+ *
109
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
110
+ *
111
+ * @extends ContractExecutionError
112
+ */
113
+ export class ContractDispatchError extends ContractExecutionError {
114
+ /**
115
+ * The error that occurred during the dispatch phase.
116
+ */
117
+ dispatchError;
118
+ /**
119
+ * Constructs a new `ContractDispatchError` instance.
120
+ *
121
+ * @param err - The `DispatchError` that occurred during the dispatch phase.
122
+ * @param raw - The raw result of the contract call.
123
+ */
124
+ constructor(err, raw) {
125
+ super(raw);
126
+ this.dispatchError = err;
127
+ }
128
+ }
129
+ /**
130
+ * Represents an error that occurred during the execution of a smart contract call due to a language-specific error.
131
+ * This class extends `ContractExecutionError` and includes a `LangError` property.
132
+ *
133
+ * Ref: https://use.ink/faq/migrating-from-ink-3-to-4#:~:text=Add%20support%20for,equivalent%20LangError.
134
+ *
135
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
136
+ *
137
+ * @extends ContractExecutionError
138
+ */
139
+ export class ContractLangError extends ContractExecutionError {
140
+ /**
141
+ * The language-specific error that occurred during the execution.
142
+ */
143
+ langError;
144
+ /**
145
+ * Decoded `ReturnFlags` from contract call result.
146
+ */
147
+ flags;
148
+ /**
149
+ * Constructs a new `ContractLangError` instance.
150
+ *
151
+ * @param err - The `LangError` that occurred during the execution phase.
152
+ * @param raw - The raw result of the contract call.
153
+ */
154
+ constructor(err, raw) {
155
+ assert(raw.result.isOk, 'Should not throw DispatchError!');
156
+ super(raw);
157
+ this.langError = err;
158
+ this.flags = toReturnFlags(raw.result.value.flags.bits);
159
+ }
160
+ }
161
+ /**
162
+ * Checks if the provided error is an instance of `ContractExecutionError`.
163
+ *
164
+ * This function is used to determine if a given error is a result of an execution error during a smart contract call.
165
+ *
166
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
167
+ *
168
+ * @param e - The error to be checked.
169
+ *
170
+ * @returns `true` if the error is an instance of `ContractExecutionError`, `false` otherwise.
171
+ */
172
+ export function isContractExecutionError(e) {
173
+ return e instanceof ContractExecutionError;
174
+ }
175
+ /**
176
+ * Checks if the provided error is an instance of `ContractDispatchError`.
177
+ *
178
+ * This function is used to determine if a given error is a result of a dispatch error during a smart contract call.
179
+ *
180
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
181
+ *
182
+ * @param e - The error to be checked. This should be an instance of `Error`.
183
+ *
184
+ * @returns `true` if the error is an instance of `ContractDispatchError`, `false` otherwise.
185
+ * This function returns a boolean value indicating whether the provided error is of type `ContractDispatchError`.
186
+ */
187
+ export function isContractDispatchError(e) {
188
+ return e instanceof ContractDispatchError;
189
+ }
190
+ /**
191
+ * Checks if the provided error is an instance of `ContractLangError`.
192
+ *
193
+ * This function is used to determine if a given error is a result of a language-specific error during a smart contract call.
194
+ *
195
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
196
+ *
197
+ * @param e - The error to be checked. This should be an instance of `Error`.
198
+ *
199
+ * @returns `true` if the error is an instance of `ContractLangError`, `false` otherwise.
200
+ * This function returns a boolean value indicating whether the provided error is of type `ContractLangError`.
201
+ */
202
+ export function isContractLangError(e) {
203
+ return e instanceof ContractLangError;
204
+ }
205
+ /**
206
+ * Checks if the provided error is an instance of `ContractInstantiateError`.
207
+ *
208
+ * This function is used to determine if a given error is a result of an execution error during the instantiation of a smart contract.
209
+ *
210
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
211
+ *
212
+ * @param e - The error to be checked. This should be an instance of `Error`.
213
+ *
214
+ * @returns `true` if the error is an instance of `ContractInstantiateError`, `false` otherwise.
215
+ * This function returns a boolean value indicating whether the provided error is of type `ContractInstantiateError`.
216
+ */
217
+ export function isContractInstantiateError(e) {
218
+ return e instanceof ContractInstantiateError;
219
+ }
220
+ /**
221
+ * Checks if the provided error is an instance of `ContractInstantiateDispatchError`.
222
+ *
223
+ * This function is used to determine if a given error is a result of a dispatch error during the instantiation of a smart contract.
224
+ *
225
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
226
+ *
227
+ * @param e - The error to be checked. This should be an instance of `Error`.
228
+ *
229
+ * @returns `true` if the error is an instance of `ContractInstantiateDispatchError`, `false` otherwise.
230
+ * This function returns a boolean value indicating whether the provided error is of type `ContractInstantiateDispatchError`.
231
+ */
232
+ export function isContractInstantiateDispatchError(e) {
233
+ return e instanceof ContractInstantiateDispatchError;
234
+ }
235
+ /**
236
+ * Checks if the provided error is an instance of `ContractInstantiateLangError`.
237
+ *
238
+ * This function is used to determine if a given error is a result of a language-specific error during the instantiation of a smart contract.
239
+ *
240
+ * @template ContractApi - The type of the contract API. Defaults to `GenericContractApi`.
241
+ *
242
+ * @param e - The error to be checked. This should be an instance of `Error`.
243
+ *
244
+ * @returns `true` if the error is an instance of `ContractInstantiateLangError`, `false` otherwise.
245
+ * This function returns a boolean value indicating whether the provided error is of type `ContractInstantiateLangError`.
246
+ */
247
+ export function isContractInstantiateLangError(e) {
248
+ return e instanceof ContractInstantiateLangError;
249
+ }
@@ -1,5 +1,6 @@
1
1
  import { assert, concatU8a, hexToU8a, isWasm, u8aToHex } from '@dedot/utils';
2
- import { normalizeLabel } from '../utils.js';
2
+ import { ContractInstantiateDispatchError, ContractInstantiateLangError } from '../errors.js';
3
+ import { normalizeLabel, toReturnFlags } from '../utils.js';
3
4
  import { Executor } from './Executor.js';
4
5
  export class ConstructorQueryExecutor extends Executor {
5
6
  code;
@@ -10,7 +11,7 @@ export class ConstructorQueryExecutor extends Executor {
10
11
  doExecute(constructor) {
11
12
  const meta = this.#findConstructorMeta(constructor);
12
13
  assert(meta, `Constructor message not found: ${constructor}`);
13
- const callFn = (...params) => {
14
+ const callFn = async (...params) => {
14
15
  const { args } = meta;
15
16
  assert(params.length === args.length + 1, `Expected ${args.length + 1} arguments, got ${params.length}`);
16
17
  const callOptions = params[args.length];
@@ -23,7 +24,21 @@ export class ConstructorQueryExecutor extends Executor {
23
24
  type: isWasm(this.code) ? 'Upload' : 'Existing',
24
25
  value: this.code,
25
26
  };
26
- return this.api.call.contractsApi.instantiate(caller, value, gasLimit, storageDepositLimit, code, bytes, salt);
27
+ const raw = await this.api.call.contractsApi.instantiate(caller, value, gasLimit, storageDepositLimit, code, bytes, salt);
28
+ if (raw.result.isErr) {
29
+ throw new ContractInstantiateDispatchError(raw.result.err, raw);
30
+ }
31
+ const data = this.tryDecode(meta, raw.result.value.result.data);
32
+ if (data.isErr) {
33
+ throw new ContractInstantiateLangError(data.err, raw);
34
+ }
35
+ const bits = raw.result.value.result.flags.bits;
36
+ return {
37
+ data: data.value,
38
+ raw,
39
+ address: raw.result.value.accountId,
40
+ flags: toReturnFlags(bits),
41
+ };
27
42
  };
28
43
  callFn.meta = meta;
29
44
  return callFn;
@@ -1,6 +1,6 @@
1
- import { GenericSubstrateApi } from "@dedot/types";
2
- import { Executor } from "./Executor.js";
3
- import { GenericContractEvent } from "../types/index.js";
1
+ import { GenericSubstrateApi } from '@dedot/types';
2
+ import { GenericContractEvent } from '../types/index.js';
3
+ import { Executor } from './Executor.js';
4
4
  export declare class EventExecutor<ChainApi extends GenericSubstrateApi> extends Executor<ChainApi> {
5
5
  #private;
6
6
  doExecute(eventName: string): GenericContractEvent;
@@ -1,22 +1,47 @@
1
- import { Executor } from "./Executor.js";
2
- import { assert, stringPascalCase } from "@dedot/utils";
1
+ import { isEventRecord } from '@dedot/api';
2
+ import { assert, stringPascalCase } from '@dedot/utils';
3
+ import { Executor } from './Executor.js';
3
4
  export class EventExecutor extends Executor {
4
5
  doExecute(eventName) {
5
- const eventMeta = this.#findEventMeta(eventName);
6
- assert(eventMeta, "Contract event metadata not found!");
6
+ const meta = this.#findEventMeta(eventName);
7
+ assert(meta, 'Contract event metadata not found!');
7
8
  const is = (event) => {
9
+ if (isEventRecord(event)) {
10
+ try {
11
+ event = this.registry.decodeEvent(event);
12
+ }
13
+ catch {
14
+ return false;
15
+ }
16
+ }
8
17
  return event.name === eventName;
9
18
  };
10
- const as = (event) => {
11
- return is(event) ? event : undefined;
19
+ const find = (events) => {
20
+ if (!events || events.length === 0)
21
+ return undefined;
22
+ if (isEventRecord(events[0])) {
23
+ return this.registry.decodeEvents(events).find(is);
24
+ }
25
+ else {
26
+ return events.find(is);
27
+ }
28
+ };
29
+ const filter = (events) => {
30
+ if (isEventRecord(events[0])) {
31
+ return this.registry.decodeEvents(events).filter(is);
32
+ }
33
+ else {
34
+ return events.filter(is);
35
+ }
12
36
  };
13
37
  return {
14
38
  is,
15
- as,
16
- meta: eventMeta,
39
+ find,
40
+ filter,
41
+ meta,
17
42
  };
18
43
  }
19
44
  #findEventMeta(eventName) {
20
- return this.registry.metadata.spec.events.find(one => stringPascalCase(one.label) === eventName);
45
+ return this.registry.metadata.spec.events.find((one) => stringPascalCase(one.label) === eventName);
21
46
  }
22
47
  }
@@ -3,7 +3,7 @@ import { SubstrateApi } from '@dedot/api/chaintypes';
3
3
  import { AccountId32 } from '@dedot/codecs';
4
4
  import { GenericSubstrateApi, RpcVersion } from '@dedot/types';
5
5
  import { TypinkRegistry } from '../TypinkRegistry.js';
6
- import { ContractMessageArg, ContractCallMessage } from '../types/index.js';
6
+ import { ContractMessageArg, ContractMessage } from '../types/index.js';
7
7
  import { ContractMetadata } from '../types/index.js';
8
8
  export declare abstract class Executor<ChainApi extends GenericSubstrateApi = SubstrateApi[RpcVersion]> {
9
9
  #private;
@@ -14,5 +14,5 @@ export declare abstract class Executor<ChainApi extends GenericSubstrateApi = Su
14
14
  get registry(): TypinkRegistry;
15
15
  abstract doExecute(...paths: string[]): unknown;
16
16
  tryEncode(param: ContractMessageArg, value: any): Uint8Array;
17
- tryDecode(meta: ContractCallMessage, raw: any): unknown;
17
+ tryDecode(meta: ContractMessage, raw: any): unknown;
18
18
  }
@@ -1,5 +1,6 @@
1
1
  import { assert, concatU8a, hexToU8a, u8aToHex } from '@dedot/utils';
2
- import { normalizeLabel } from '../utils.js';
2
+ import { ContractDispatchError, ContractLangError } from '../errors.js';
3
+ import { normalizeLabel, toReturnFlags } from '../utils.js';
3
4
  import { Executor } from './Executor.js';
4
5
  export class QueryExecutor extends Executor {
5
6
  doExecute(message) {
@@ -14,17 +15,19 @@ export class QueryExecutor extends Executor {
14
15
  const formattedInputs = args.map((arg, index) => this.tryEncode(arg, params[index]));
15
16
  const bytes = u8aToHex(concatU8a(hexToU8a(meta.selector), ...formattedInputs));
16
17
  const raw = await this.api.call.contractsApi.call(caller, this.address, value, gasLimit, storageDepositLimit, bytes);
17
- return (raw.result.isOk
18
- ? {
19
- isOk: true,
20
- data: this.tryDecode(meta, raw.result.value.data),
21
- raw,
22
- }
23
- : {
24
- isOk: false,
25
- err: raw.result.err,
26
- raw,
27
- });
18
+ if (raw.result.isErr) {
19
+ throw new ContractDispatchError(raw.result.err, raw);
20
+ }
21
+ const data = this.tryDecode(meta, raw.result.value.data);
22
+ if (data.isErr) {
23
+ throw new ContractLangError(data.err, raw);
24
+ }
25
+ const bits = raw.result.value.flags.bits;
26
+ return {
27
+ data: data.value,
28
+ raw,
29
+ flags: toReturnFlags(bits),
30
+ };
28
31
  };
29
32
  callFn.meta = meta;
30
33
  return callFn;
package/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './utils.js';
2
+ export * from './errors.js';
2
3
  export * from './executor/index.js';
3
4
  export * from './types/index.js';
4
5
  export * from './TypinkRegistry.js';
package/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './utils.js';
2
+ export * from './errors.js';
2
3
  export * from './executor/index.js';
3
4
  export * from './types/index.js';
4
5
  export * from './TypinkRegistry.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dedot/contracts",
3
- "version": "0.1.1",
3
+ "version": "0.3.0",
4
4
  "description": "Delightful ink! Contract APIs",
5
5
  "author": "Tung Vu <tung@coongcrafts.io>",
6
6
  "homepage": "https://github.com/dedotdev/dedot/tree/main/packages/contracts",
@@ -18,17 +18,17 @@
18
18
  "test": "vitest --watch=false"
19
19
  },
20
20
  "dependencies": {
21
- "@dedot/api": "0.1.1",
22
- "@dedot/codecs": "0.1.1",
23
- "@dedot/types": "0.1.1",
24
- "@dedot/utils": "0.1.1"
21
+ "@dedot/api": "0.3.0",
22
+ "@dedot/codecs": "0.3.0",
23
+ "@dedot/types": "0.3.0",
24
+ "@dedot/utils": "0.3.0"
25
25
  },
26
26
  "publishConfig": {
27
27
  "access": "public",
28
28
  "directory": "dist"
29
29
  },
30
30
  "license": "Apache-2.0",
31
- "gitHead": "ea6d1e7ba00a5931b86a7946afba820101a06fc2",
31
+ "gitHead": "40d0f3c7eee292b7615649e6a5d54887bee75fe2",
32
32
  "module": "./index.js",
33
33
  "types": "./index.d.ts",
34
34
  "exports": {
package/types/index.d.ts CHANGED
@@ -1,20 +1,27 @@
1
1
  import { SubstrateApi } from '@dedot/api/chaintypes';
2
- import { AccountId32Like, BytesLike, DispatchError, Weight } from '@dedot/codecs';
3
- import { AnyFunc, AsyncMethod, GenericSubstrateApi, RpcVersion, VersionedGenericSubstrateApi } from '@dedot/types';
2
+ import { AccountId32, AccountId32Like, BytesLike, Weight } from '@dedot/codecs';
3
+ import { AnyFunc, AsyncMethod, GenericSubstrateApi, IEventRecord, RpcVersion, VersionedGenericSubstrateApi } from '@dedot/types';
4
4
  import { ContractCallMessage, ContractConstructorMessage } from './shared.js';
5
5
  import { ContractEventV4, ContractMetadataV4 } from './v4.js';
6
6
  import { ContractEventV5, ContractMetadataV5 } from './v5.js';
7
7
  export * from './shared.js';
8
8
  export type ContractEventMeta = ContractEventV4 | ContractEventV5;
9
- export type GenericContractCallResult<DecodedData, ContractResult> = ({
10
- isOk: true;
9
+ /**
10
+ * Flags used by a contract to customize exit behaviour.
11
+ * Ref: https://github.com/paritytech/polkadot-sdk/blob/d2fd53645654d3b8e12cbf735b67b93078d70113/substrate/frame/contracts/uapi/src/flags.rs#L23-L26
12
+ */
13
+ export type ReturnFlags = {
14
+ bits: number;
15
+ revert: boolean;
16
+ };
17
+ export interface GenericContractCallResult<DecodedData = any, ContractResult = any> {
11
18
  data: DecodedData;
12
- } | {
13
- isOk: false;
14
- err: DispatchError;
15
- }) & {
16
19
  raw: ContractResult;
17
- };
20
+ flags: ReturnFlags;
21
+ }
22
+ export interface GenericConstructorCallResult<DecodedData = any, ContractResult = any> extends GenericContractCallResult<DecodedData, ContractResult> {
23
+ address: AccountId32;
24
+ }
18
25
  export type ContractCallResult<ChainApi extends GenericSubstrateApi> = Awaited<ReturnType<ChainApi['call']['contractsApi']['call']>>;
19
26
  export type ContractInstantiateResult<ChainApi extends GenericSubstrateApi> = Awaited<ReturnType<ChainApi['call']['contractsApi']['instantiate']>>;
20
27
  export type ContractSubmittableExtrinsic<ChainApi extends GenericSubstrateApi> = ReturnType<ChainApi['tx']['contracts']['call']>;
@@ -47,7 +54,7 @@ export type GenericContractQueryCall<ChainApi extends GenericSubstrateApi, F ext
47
54
  export type GenericContractTxCall<ChainApi extends GenericSubstrateApi, F extends AnyFunc = (...args: any[]) => ContractSubmittableExtrinsic<ChainApi>> = F & {
48
55
  meta: ContractCallMessage;
49
56
  };
50
- export type GenericConstructorQueryCall<ChainApi extends GenericSubstrateApi, F extends AsyncMethod = (...args: any[]) => Promise<ContractInstantiateResult<ChainApi>>> = F & {
57
+ export type GenericConstructorQueryCall<ChainApi extends GenericSubstrateApi, F extends AsyncMethod = (...args: any[]) => Promise<GenericConstructorCallResult<any, ContractInstantiateResult<ChainApi>>>> = F & {
51
58
  meta: ContractConstructorMessage;
52
59
  };
53
60
  export type GenericConstructorTxCall<ChainApi extends GenericSubstrateApi, F extends AnyFunc = (...args: any[]) => GenericInstantiateSubmittableExtrinsic<ChainApi>> = F & {
@@ -72,17 +79,23 @@ export type ContractEvent<EventName extends string = string, Data extends any =
72
79
  data: Data;
73
80
  };
74
81
  export interface GenericContractEvent<EventName extends string = string, Data extends any = any> {
75
- is: (event: ContractEvent) => event is ContractEvent<EventName, Data>;
76
- as: (event: ContractEvent) => ContractEvent<EventName, Data> | undefined;
82
+ is: (event: IEventRecord | ContractEvent) => event is ContractEvent<EventName, Data>;
83
+ find: (events: IEventRecord[] | ContractEvent[]) => ContractEvent<EventName, Data> | undefined;
84
+ filter: (events: IEventRecord[] | ContractEvent[]) => ContractEvent<EventName, Data>[];
77
85
  meta: ContractEventMeta;
78
86
  }
79
87
  export interface GenericContractEvents<_ extends GenericSubstrateApi> {
80
88
  [event: string]: GenericContractEvent;
81
89
  }
90
+ export type GenericInkLangError = 'CouldNotReadInput' | any;
82
91
  export interface GenericContractApi<Rv extends RpcVersion = RpcVersion, ChainApi extends VersionedGenericSubstrateApi = SubstrateApi> {
83
92
  query: GenericContractQuery<ChainApi[Rv]>;
84
93
  tx: GenericContractTx<ChainApi[Rv]>;
85
94
  constructorQuery: GenericConstructorQuery<ChainApi[Rv]>;
86
95
  constructorTx: GenericConstructorTx<ChainApi[Rv]>;
87
96
  events: GenericContractEvents<ChainApi[Rv]>;
97
+ types: {
98
+ LangError: GenericInkLangError;
99
+ ChainApi: ChainApi[Rv];
100
+ };
88
101
  }
package/utils.d.ts CHANGED
@@ -2,10 +2,11 @@ import { ISubstrateClient } from '@dedot/api';
2
2
  import { PortableType, TypeDef } from '@dedot/codecs';
3
3
  import { GenericSubstrateApi } from '@dedot/types';
4
4
  import { Executor } from './executor/index.js';
5
- import { ContractMetadata, ContractTypeDef } from './types/index.js';
5
+ import { ContractMetadata, ContractTypeDef, ReturnFlags } from './types/index.js';
6
6
  export declare const extractContractTypes: (contractMetadata: ContractMetadata) => PortableType[];
7
7
  export declare const normalizeContractTypeDef: (def: ContractTypeDef) => TypeDef;
8
8
  export declare const parseRawMetadata: (rawMetadata: string) => ContractMetadata;
9
9
  export declare function newProxyChain<ChainApi extends GenericSubstrateApi>(carrier: Executor<ChainApi>): unknown;
10
10
  export declare function ensureSupportContractsPallet<ChainApi extends GenericSubstrateApi>(api: ISubstrateClient<ChainApi>): void;
11
11
  export declare function normalizeLabel(label?: string): string;
12
+ export declare function toReturnFlags(bits: number): ReturnFlags;
package/utils.js CHANGED
@@ -97,3 +97,11 @@ export function normalizeLabel(label) {
97
97
  return '';
98
98
  return stringCamelCase(label.replaceAll('::', '_'));
99
99
  }
100
+ // https://github.com/paritytech/polkadot-sdk/blob/d2fd53645654d3b8e12cbf735b67b93078d70113/substrate/frame/contracts/uapi/src/flags.rs#L23-L26
101
+ const REVERT_FLAG = 1;
102
+ export function toReturnFlags(bits) {
103
+ return {
104
+ bits,
105
+ revert: bits === REVERT_FLAG,
106
+ };
107
+ }