@dedot/contracts 0.12.1-next.f19f185a.2 → 0.13.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.
Files changed (71) hide show
  1. package/Contract.d.ts +4 -4
  2. package/Contract.js +41 -6
  3. package/ContractDeployer.d.ts +2 -2
  4. package/ContractDeployer.js +8 -6
  5. package/TypinkRegistry.d.ts +29 -6
  6. package/TypinkRegistry.js +144 -24
  7. package/cjs/Contract.js +44 -9
  8. package/cjs/ContractDeployer.js +10 -8
  9. package/cjs/TypinkRegistry.js +144 -24
  10. package/cjs/errors.js +3 -3
  11. package/cjs/executor/ConstructorQueryExecutor.js +49 -9
  12. package/cjs/executor/ConstructorTxExecutor.js +17 -6
  13. package/cjs/executor/QueryExecutor.js +30 -5
  14. package/cjs/executor/TxExecutor.js +10 -1
  15. package/cjs/executor/abstract/ContractExecutor.js +4 -5
  16. package/cjs/executor/abstract/DeployerExecutor.js +2 -2
  17. package/cjs/index.js +1 -1
  18. package/cjs/storage/BaseLazyObject.js +25 -0
  19. package/cjs/storage/LazyMapping.js +22 -0
  20. package/cjs/storage/LazyObject.js +18 -0
  21. package/cjs/storage/LazyStorageVec.js +54 -0
  22. package/cjs/storage/index.js +19 -0
  23. package/cjs/types/index.js +3 -0
  24. package/cjs/types/v6.js +2 -0
  25. package/cjs/utils/address.js +67 -0
  26. package/cjs/utils/ensure.js +68 -0
  27. package/cjs/utils/index.js +21 -0
  28. package/cjs/utils/proxy.js +11 -0
  29. package/cjs/utils/storage.js +44 -0
  30. package/cjs/{utils.js → utils/types.js} +31 -34
  31. package/errors.js +2 -2
  32. package/executor/ConstructorQueryExecutor.js +47 -7
  33. package/executor/ConstructorTxExecutor.js +18 -7
  34. package/executor/QueryExecutor.js +27 -2
  35. package/executor/TxExecutor.js +11 -2
  36. package/executor/abstract/ContractExecutor.d.ts +3 -4
  37. package/executor/abstract/ContractExecutor.js +2 -3
  38. package/executor/abstract/DeployerExecutor.js +1 -1
  39. package/index.d.ts +1 -1
  40. package/index.js +1 -1
  41. package/package.json +7 -6
  42. package/storage/BaseLazyObject.d.ts +10 -0
  43. package/storage/BaseLazyObject.js +21 -0
  44. package/storage/LazyMapping.d.ts +4 -0
  45. package/storage/LazyMapping.js +18 -0
  46. package/storage/LazyObject.d.ts +4 -0
  47. package/storage/LazyObject.js +14 -0
  48. package/storage/LazyStorageVec.d.ts +5 -0
  49. package/storage/LazyStorageVec.js +27 -0
  50. package/storage/index.d.ts +3 -0
  51. package/storage/index.js +3 -0
  52. package/types/index.d.ts +73 -11
  53. package/types/index.js +3 -0
  54. package/types/shared.d.ts +6 -18
  55. package/types/v4.d.ts +47 -3
  56. package/types/v5.d.ts +43 -4
  57. package/types/v6.d.ts +13 -0
  58. package/types/v6.js +1 -0
  59. package/utils/address.d.ts +30 -0
  60. package/utils/address.js +61 -0
  61. package/utils/ensure.d.ts +12 -0
  62. package/utils/ensure.js +59 -0
  63. package/utils/index.d.ts +5 -0
  64. package/utils/index.js +5 -0
  65. package/utils/proxy.d.ts +3 -0
  66. package/utils/proxy.js +7 -0
  67. package/utils/storage.d.ts +2 -0
  68. package/utils/storage.js +40 -0
  69. package/utils/types.d.ts +17 -0
  70. package/{utils.js → utils/types.js} +29 -30
  71. package/utils.d.ts +0 -13
@@ -27,15 +27,99 @@ exports.TypinkRegistry = void 0;
27
27
  const codecs_1 = require("@dedot/codecs");
28
28
  const $ = __importStar(require("@dedot/shape"));
29
29
  const utils_1 = require("@dedot/utils");
30
- const utils_js_1 = require("./utils.js");
30
+ const index_js_1 = require("./storage/index.js");
31
+ const index_js_2 = require("./utils/index.js");
31
32
  class TypinkRegistry extends codecs_1.TypeRegistry {
32
- #metadata;
33
- constructor(metadata) {
34
- super((0, utils_js_1.extractContractTypes)(metadata));
35
- this.#metadata = metadata;
33
+ metadata;
34
+ options;
35
+ constructor(metadata, options) {
36
+ (0, index_js_2.ensureSupportedContractMetadataVersion)(metadata);
37
+ super((0, index_js_2.extractContractTypes)(metadata));
38
+ this.metadata = metadata;
39
+ this.options = options;
36
40
  }
37
- get metadata() {
38
- return this.#metadata;
41
+ findCodec(typeId) {
42
+ const types = this.metadata.types;
43
+ const typeDef = types.find(({ id }) => id == typeId);
44
+ const $codec = super.findCodec(typeId);
45
+ const lazyType = (0, index_js_2.isLazyType)(typeDef.type.path);
46
+ if (lazyType === index_js_2.KnownLazyType.MAPPING) {
47
+ return this.#createLazyCodec(index_js_1.LazyMapping, $codec, typeDef);
48
+ }
49
+ else if (lazyType === index_js_2.KnownLazyType.LAZY) {
50
+ return this.#createLazyCodec(index_js_1.LazyObject, $codec, typeDef);
51
+ }
52
+ else if (lazyType === index_js_2.KnownLazyType.STORAGE_VEC) {
53
+ return this.#createLazyCodec(index_js_1.LazyStorageVec, $codec, typeDef);
54
+ }
55
+ return $codec;
56
+ }
57
+ /**
58
+ * Creates a lazy codec for a given type ID, extracting only storage types that is lazy/non-packed storage.
59
+ *
60
+ * @param typeId The type ID to create a lazy codec for
61
+ * @returns A shape codec containing only lazy/non-packed storage fields, or null if no lazy fields exist
62
+ */
63
+ createLazyCodec(typeId) {
64
+ const typeDef = this.findType(typeId);
65
+ // Check if this is a lazy storage type we want to keep
66
+ const lazyType = (0, index_js_2.isLazyType)(typeDef.path);
67
+ if (lazyType)
68
+ return this.findCodec(typeId);
69
+ // For non-lazy types, we need to recursively process the structure
70
+ const { typeDef: def } = typeDef;
71
+ // We only support Struct type for now.
72
+ if (def.type === 'Struct') {
73
+ const { fields } = def.value;
74
+ if (fields.length === 0) {
75
+ return null;
76
+ }
77
+ // Handle tuple-like structs (fields with undefined names)
78
+ if (fields[0].name === undefined) {
79
+ if (fields.length === 1) {
80
+ // Single unnamed field - check if it contains lazy types
81
+ return this.createLazyCodec(fields[0].typeId);
82
+ }
83
+ else {
84
+ return null; // Unsupported Lazy Codec Structure
85
+ }
86
+ }
87
+ else {
88
+ // Named struct fields - create struct with only lazy fields
89
+ const lazyFields = {};
90
+ let hasLazyFields = false;
91
+ for (const field of fields) {
92
+ // Skip fields with undefined names in named structs
93
+ if (field.name === undefined)
94
+ continue;
95
+ // Recursively check if this field contains lazy types
96
+ const fieldCodec = this.createLazyCodec(field.typeId);
97
+ // Only include fields that have lazy types
98
+ if (fieldCodec) {
99
+ lazyFields[field.name] = fieldCodec;
100
+ hasLazyFields = true;
101
+ }
102
+ }
103
+ // If no lazy fields, return null
104
+ if (!hasLazyFields) {
105
+ return null;
106
+ }
107
+ return $.Struct(lazyFields);
108
+ }
109
+ }
110
+ return null;
111
+ }
112
+ #enhanceLazyCodec($codec, typeDef) {
113
+ const $Codec = $.Tuple($codec);
114
+ // @ts-ignore
115
+ $Codec.subDecode = () => {
116
+ return [typeDef, this];
117
+ };
118
+ return $Codec;
119
+ }
120
+ #createLazyCodec(LazyClazz, $codec, typeDef) {
121
+ // @ts-ignore
122
+ return $.instance(LazyClazz, this.#enhanceLazyCodec($codec, typeDef), () => ({}));
39
123
  }
40
124
  decodeEvents(records, contract) {
41
125
  return records
@@ -44,27 +128,35 @@ class TypinkRegistry extends codecs_1.TypeRegistry {
44
128
  }
45
129
  decodeEvent(eventRecord, contract) {
46
130
  (0, utils_1.assert)(this.#isContractEmittedEvent(eventRecord.event, contract), 'Invalid ContractEmitted Event');
47
- const { version } = this.#metadata;
131
+ const { version } = this.metadata;
48
132
  switch (version) {
133
+ case 6:
49
134
  case 5:
50
- return this.#decodeEventV5(eventRecord);
135
+ return this.isRevive() // --
136
+ ? this.#decodeEventV6(eventRecord)
137
+ : this.#decodeEventV5(eventRecord);
51
138
  case '4':
52
139
  return this.#decodeEventV4(eventRecord);
53
140
  default:
54
141
  throw new utils_1.DedotError('Unsupported metadata version!');
55
142
  }
56
143
  }
57
- #isContractEmittedEvent(event, contract) {
58
- const eventMatched = event.pallet === 'Contracts' &&
144
+ #isContractEmittedEvent(event, contractAddress) {
145
+ const eventMatched = (this.isRevive() // --
146
+ ? event.pallet === 'Revive'
147
+ : event.pallet === 'Contracts') &&
59
148
  typeof event.palletEvent === 'object' &&
60
149
  event.palletEvent.name === 'ContractEmitted';
61
150
  if (!eventMatched)
62
151
  return false;
63
- if (contract) {
152
+ if (contractAddress) {
64
153
  // @ts-ignore
65
154
  const emittedContract = event.palletEvent.data?.contract;
66
- if (emittedContract instanceof codecs_1.AccountId32) {
67
- return emittedContract.eq(contract);
155
+ if (this.isRevive()) {
156
+ return emittedContract === contractAddress;
157
+ }
158
+ else if (emittedContract instanceof codecs_1.AccountId32) {
159
+ return emittedContract.eq(contractAddress);
68
160
  }
69
161
  else {
70
162
  return false;
@@ -73,32 +165,51 @@ class TypinkRegistry extends codecs_1.TypeRegistry {
73
165
  return true;
74
166
  }
75
167
  #decodeEventV4(eventRecord) {
76
- (0, utils_1.assert)(this.#metadata.version === '4', 'Invalid metadata version!');
168
+ (0, utils_1.assert)(this.metadata.version === '4', 'Invalid metadata version!');
77
169
  (0, utils_1.assert)(this.#isContractEmittedEvent(eventRecord.event), 'Invalid ContractEmitted Event');
78
170
  const data = (0, utils_1.hexToU8a)(eventRecord.event.palletEvent.data.data);
79
171
  const index = data.at(0);
80
172
  (0, utils_1.assert)(index !== undefined, 'Unable to decode event index!');
81
- const event = this.#metadata.spec.events[index];
173
+ const event = this.metadata.spec.events[index];
82
174
  (0, utils_1.assert)(event, `Event index not found: ${index.toString()}`);
83
175
  return this.#tryDecodeEvent(event, data.subarray(1));
84
176
  }
85
- #decodeEventV5(eventRecord) {
86
- (0, utils_1.assert)(this.#metadata.version === 5, 'Invalid metadata version!');
87
- (0, utils_1.assert)(this.#isContractEmittedEvent(eventRecord.event), 'Invalid ContractEmitted Event');
88
- const data = (0, utils_1.hexToU8a)(eventRecord.event.palletEvent.data.data);
89
- const signatureTopic = eventRecord.topics.at(0);
177
+ #detectEventMeta(signatureTopics) {
178
+ // Only version >= 5 metadata supports signature topics
179
+ (0, utils_1.assert)(+this.metadata.version >= 5, 'Invalid metadata version!');
90
180
  let eventMeta;
91
- if (signatureTopic) {
92
- eventMeta = this.#metadata.spec.events.find((one) => one.signature_topic === signatureTopic);
181
+ const targetSignatureTopic = signatureTopics.at(0);
182
+ if (targetSignatureTopic) {
183
+ // @ts-ignore
184
+ eventMeta = this.metadata.spec.events.find((one) => one.signature_topic === targetSignatureTopic);
93
185
  }
94
186
  // TODO: Handle multiple anonymous events
95
187
  // If `event` does not exist, it means it's an anonymous event
96
188
  // that does not contain a signature topic in the metadata.
97
189
  if (!eventMeta) {
98
- const potentialEvents = this.#metadata.spec.events.filter((one) => !one.signature_topic && one.args.filter((arg) => arg.indexed).length === eventRecord.topics.length);
190
+ const potentialEvents = this.metadata.spec.events.filter(
191
+ // @ts-ignore
192
+ (one) => !one.signature_topic && one.args.filter((arg) => arg.indexed).length === signatureTopics.length);
99
193
  (0, utils_1.assert)(potentialEvents.length === 1, 'Unable to determine event!');
100
194
  eventMeta = potentialEvents[0];
101
195
  }
196
+ return eventMeta;
197
+ }
198
+ #decodeEventV5(eventRecord) {
199
+ (0, utils_1.assert)(this.metadata.version === 5, 'Invalid metadata version!');
200
+ (0, utils_1.assert)(this.#isContractEmittedEvent(eventRecord.event), 'Invalid ContractEmitted Event');
201
+ const data = (0, utils_1.hexToU8a)(eventRecord.event.palletEvent.data.data);
202
+ const signatureTopics = eventRecord.topics;
203
+ const eventMeta = this.#detectEventMeta(signatureTopics);
204
+ return this.#tryDecodeEvent(eventMeta, data);
205
+ }
206
+ #decodeEventV6(eventRecord) {
207
+ (0, utils_1.assert)(this.isRevive(), 'Invalid metadata version!');
208
+ (0, utils_1.assert)(this.#isContractEmittedEvent(eventRecord.event), 'Invalid ContractEmitted Event');
209
+ const eventData = eventRecord.event.palletEvent.data;
210
+ const data = (0, utils_1.hexToU8a)(eventData.data);
211
+ const signatureTopics = eventData.topics;
212
+ const eventMeta = this.#detectEventMeta(signatureTopics);
102
213
  return this.#tryDecodeEvent(eventMeta, data);
103
214
  }
104
215
  #tryDecodeEvent(eventMeta, raw) {
@@ -114,5 +225,14 @@ class TypinkRegistry extends codecs_1.TypeRegistry {
114
225
  const name = (0, utils_1.stringPascalCase)(label);
115
226
  return args.length ? { name, data } : { name };
116
227
  }
228
+ /**
229
+ * Check if the contract is Revive Compatible (ink!v6)
230
+ */
231
+ isRevive() {
232
+ return (+this.metadata.version >= 6 ||
233
+ this.metadata.source.language // TODO remove this
234
+ .toLowerCase() // --
235
+ .startsWith('ink! 6.'));
236
+ }
117
237
  }
118
238
  exports.TypinkRegistry = TypinkRegistry;
package/cjs/errors.js CHANGED
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
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
4
  const utils_1 = require("@dedot/utils");
5
- const utils_js_1 = require("./utils.js");
5
+ const index_js_1 = require("./utils/index.js");
6
6
  /**
7
7
  * Represents an error that occurred during the instantiation of a smart contract.
8
8
  * This class extends the base `DedotError` and includes a `raw` property of type `ContractInstantiateResult`.
@@ -85,7 +85,7 @@ class ContractInstantiateLangError extends ContractInstantiateError {
85
85
  (0, utils_1.assert)(raw.result.isOk, 'Should not throw DispatchError!');
86
86
  super(raw);
87
87
  this.langError = err;
88
- this.flags = (0, utils_js_1.toReturnFlags)(raw.result.value.result.flags.bits);
88
+ this.flags = (0, index_js_1.toReturnFlags)(raw.result.value.result.flags.bits);
89
89
  this.message = `Lange error: ${JSON.stringify(err)}`;
90
90
  }
91
91
  }
@@ -172,7 +172,7 @@ class ContractLangError extends ContractExecutionError {
172
172
  (0, utils_1.assert)(raw.result.isOk, 'Should not throw DispatchError!');
173
173
  super(raw);
174
174
  this.langError = err;
175
- this.flags = (0, utils_js_1.toReturnFlags)(raw.result.value.flags.bits);
175
+ this.flags = (0, index_js_1.toReturnFlags)(raw.result.value.flags.bits);
176
176
  this.message = `Lange error: ${JSON.stringify(err)}`;
177
177
  }
178
178
  }
@@ -3,9 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ConstructorQueryExecutor = void 0;
4
4
  const utils_1 = require("@dedot/utils");
5
5
  const errors_js_1 = require("../errors.js");
6
- const utils_js_1 = require("../utils.js");
7
- const index_js_1 = require("./abstract/index.js");
8
- class ConstructorQueryExecutor extends index_js_1.DeployerExecutor {
6
+ const index_js_1 = require("../utils/index.js");
7
+ const index_js_2 = require("./abstract/index.js");
8
+ class ConstructorQueryExecutor extends index_js_2.DeployerExecutor {
9
9
  doExecute(constructor) {
10
10
  const meta = this.findConstructorMeta(constructor);
11
11
  (0, utils_1.assert)(meta, `Constructor message not found: ${constructor}`);
@@ -14,16 +14,55 @@ class ConstructorQueryExecutor extends index_js_1.DeployerExecutor {
14
14
  (0, utils_1.assertFalse)(params.length < args.length, `Expected at least ${args.length} arguments, got ${params.length}`);
15
15
  (0, utils_1.assertFalse)(params.length > args.length + 1, `Expected at most ${args.length + 1} arguments, got ${params.length}`);
16
16
  const callOptions = (params[args.length] || {});
17
- const { caller = this.options.defaultCaller, value = 0n, gasLimit, storageDepositLimit, salt = '0x', } = callOptions;
17
+ const { caller = this.options.defaultCaller, // --
18
+ value = 0n, gasLimit, storageDepositLimit, salt, } = callOptions;
18
19
  (0, utils_1.assert)(caller, 'Expected a valid caller address in ConstructorCallOptions');
19
- (0, utils_1.assertFalse)((0, utils_1.isNull)(salt) || (0, utils_1.isUndefined)(salt), 'Expected a salt in ConstructorCallOptions');
20
20
  const formattedInputs = args.map((arg, index) => this.tryEncode(arg, params[index]));
21
21
  const bytes = (0, utils_1.u8aToHex)((0, utils_1.concatU8a)((0, utils_1.hexToU8a)(meta.selector), ...formattedInputs));
22
+ const isUpload = (0, utils_1.isPvm)(this.code) || (0, utils_1.isWasm)(this.code);
22
23
  const code = {
23
- type: (0, utils_1.isWasm)(this.code) ? 'Upload' : 'Existing',
24
+ type: isUpload ? 'Upload' : 'Existing',
24
25
  value: this.code,
25
26
  };
26
- const raw = await this.client.call.contractsApi.instantiate(caller, value, gasLimit, storageDepositLimit, code, bytes, salt);
27
+ const client = this.client;
28
+ const raw = await (async () => {
29
+ if (this.registry.isRevive()) {
30
+ (0, utils_1.assert)((0, utils_1.isUndefined)(salt) || (0, utils_1.toU8a)(salt).byteLength == 32, 'Invalid salt provided in ConstructorCallOptions: expected a 32-byte value as a hex string or a Uint8Array');
31
+ const raw = await client.call.reviveApi.instantiate(caller, // --
32
+ value, gasLimit, storageDepositLimit, code, bytes, salt ? (0, utils_1.toHex)(salt) : undefined);
33
+ const result = raw.result;
34
+ if (result.isOk) {
35
+ // @ts-ignore
36
+ result.value.address = result.value.addr;
37
+ // @ts-ignore
38
+ delete result.value.addr;
39
+ }
40
+ return {
41
+ gasConsumed: raw.gasConsumed,
42
+ gasRequired: raw.gasRequired,
43
+ storageDeposit: raw.storageDeposit,
44
+ result,
45
+ };
46
+ }
47
+ else {
48
+ const raw = await client.call.contractsApi.instantiate(caller, // --
49
+ value, gasLimit, storageDepositLimit, code, bytes, salt || '0x');
50
+ const result = raw.result;
51
+ if (result.isOk) {
52
+ // @ts-ignore
53
+ result.value.address = result.value.accountId.address();
54
+ // @ts-ignore
55
+ delete result.value.accountId;
56
+ }
57
+ return {
58
+ gasConsumed: raw.gasConsumed,
59
+ gasRequired: raw.gasRequired,
60
+ storageDeposit: raw.storageDeposit,
61
+ debugMessage: raw.debugMessage,
62
+ result,
63
+ };
64
+ }
65
+ })();
27
66
  if (raw.result.isErr) {
28
67
  throw new errors_js_1.ContractInstantiateDispatchError(raw.result.err, raw);
29
68
  }
@@ -35,8 +74,9 @@ class ConstructorQueryExecutor extends index_js_1.DeployerExecutor {
35
74
  return {
36
75
  data: data.value,
37
76
  raw,
38
- address: raw.result.value.accountId,
39
- flags: (0, utils_js_1.toReturnFlags)(bits),
77
+ address: raw.result.value.address,
78
+ flags: (0, index_js_1.toReturnFlags)(bits),
79
+ inputData: bytes,
40
80
  };
41
81
  };
42
82
  callFn.meta = meta;
@@ -11,17 +11,28 @@ class ConstructorTxExecutor extends index_js_1.DeployerExecutor {
11
11
  const { args } = meta;
12
12
  (0, utils_1.assert)(params.length === args.length + 1, `Expected ${args.length + 1} arguments, got ${params.length}`);
13
13
  const txCallOptions = params[args.length];
14
- const { value = 0n, gasLimit, storageDepositLimit, salt = '0x' } = txCallOptions;
14
+ const { value = 0n, gasLimit, storageDepositLimit, salt } = txCallOptions;
15
15
  (0, utils_1.assert)(gasLimit, 'Expected a gas limit in ConstructorTxOptions');
16
- (0, utils_1.assertFalse)((0, utils_1.isNull)(salt) || (0, utils_1.isUndefined)(salt), 'Expected a salt in ConstructorCallOptions');
17
16
  const formattedInputs = args.map((arg, index) => this.tryEncode(arg, params[index]));
18
17
  const bytes = (0, utils_1.u8aToHex)((0, utils_1.concatU8a)((0, utils_1.hexToU8a)(meta.selector), ...formattedInputs));
19
- if ((0, utils_1.isWasm)(this.code)) {
20
- return this.client.tx.contracts.instantiateWithCode(value, gasLimit, storageDepositLimit, this.code, bytes, salt);
18
+ const client = this.client;
19
+ if (this.registry.isRevive()) {
20
+ (0, utils_1.assert)((0, utils_1.isUndefined)(salt) || (0, utils_1.toU8a)(salt).byteLength == 32, 'Invalid salt provided in ConstructorCallOptions: expected a 32-byte value as a hex string or a Uint8Array');
21
+ (0, utils_1.assert)(!(0, utils_1.isUndefined)(storageDepositLimit), 'Expected a storage deposit limit in ConstructorTxOptions');
22
+ if ((0, utils_1.isPvm)(this.code)) {
23
+ return client.tx.revive.instantiateWithCode(value, gasLimit, storageDepositLimit, this.code, bytes, salt ? (0, utils_1.toHex)(salt) : undefined);
24
+ }
25
+ else {
26
+ return client.tx.revive.instantiate(value, gasLimit, storageDepositLimit, (0, utils_1.toHex)(this.code), bytes, salt ? (0, utils_1.toHex)(salt) : undefined);
27
+ }
21
28
  }
22
29
  else {
23
- return this.client.tx.contracts.instantiate(value, // prettier-end-here
24
- gasLimit, storageDepositLimit, this.code, bytes, salt);
30
+ if ((0, utils_1.isWasm)(this.code)) {
31
+ return client.tx.contracts.instantiateWithCode(value, gasLimit, storageDepositLimit, this.code, bytes, salt || '0x');
32
+ }
33
+ else {
34
+ return client.tx.contracts.instantiate(value, gasLimit, storageDepositLimit, (0, utils_1.toHex)(this.code), bytes, salt || '0x');
35
+ }
25
36
  }
26
37
  };
27
38
  callFn.meta = meta;
@@ -3,9 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.QueryExecutor = void 0;
4
4
  const utils_1 = require("@dedot/utils");
5
5
  const errors_js_1 = require("../errors.js");
6
- const utils_js_1 = require("../utils.js");
7
- const index_js_1 = require("./abstract/index.js");
8
- class QueryExecutor extends index_js_1.ContractExecutor {
6
+ const index_js_1 = require("../utils/index.js");
7
+ const index_js_2 = require("./abstract/index.js");
8
+ class QueryExecutor extends index_js_2.ContractExecutor {
9
9
  doExecute(message) {
10
10
  const meta = this.findMessage(message);
11
11
  (0, utils_1.assert)(meta, `Query message not found: ${message}`);
@@ -18,7 +18,31 @@ class QueryExecutor extends index_js_1.ContractExecutor {
18
18
  (0, utils_1.assert)(caller, 'Expected a valid caller address in ContractCallOptions');
19
19
  const formattedInputs = args.map((arg, index) => this.tryEncode(arg, params[index]));
20
20
  const bytes = (0, utils_1.u8aToHex)((0, utils_1.concatU8a)((0, utils_1.hexToU8a)(meta.selector), ...formattedInputs));
21
- const raw = await this.client.call.contractsApi.call(caller, this.address, value, gasLimit, storageDepositLimit, bytes);
21
+ const client = this.client;
22
+ await (0, index_js_1.ensureContractPresence)(client, this.registry, this.address);
23
+ const raw = await (async () => {
24
+ if (this.registry.isRevive()) {
25
+ const raw = await client.call.reviveApi.call(caller, // --
26
+ this.address, value, gasLimit, storageDepositLimit, bytes);
27
+ return {
28
+ gasConsumed: raw.gasConsumed,
29
+ gasRequired: raw.gasRequired,
30
+ storageDeposit: raw.storageDeposit,
31
+ result: raw.result,
32
+ };
33
+ }
34
+ else {
35
+ const raw = await client.call.contractsApi.call(caller, // --
36
+ this.address, value, gasLimit, storageDepositLimit, bytes);
37
+ return {
38
+ gasConsumed: raw.gasConsumed,
39
+ gasRequired: raw.gasRequired,
40
+ storageDeposit: raw.storageDeposit,
41
+ debugMessage: raw.debugMessage,
42
+ result: raw.result,
43
+ };
44
+ }
45
+ })();
22
46
  if (raw.result.isErr) {
23
47
  throw new errors_js_1.ContractDispatchError(raw.result.err, raw);
24
48
  }
@@ -30,7 +54,8 @@ class QueryExecutor extends index_js_1.ContractExecutor {
30
54
  return {
31
55
  data: data.value,
32
56
  raw,
33
- flags: (0, utils_js_1.toReturnFlags)(bits),
57
+ flags: (0, index_js_1.toReturnFlags)(bits),
58
+ inputData: bytes,
34
59
  };
35
60
  };
36
61
  callFn.meta = meta;
@@ -15,7 +15,16 @@ class TxExecutor extends index_js_1.ContractExecutor {
15
15
  (0, utils_1.assert)(gasLimit, 'Expected a gas limit in ContractTxOptions');
16
16
  const formattedInputs = args.map((arg, index) => this.tryEncode(arg, params[index]));
17
17
  const bytes = (0, utils_1.u8aToHex)((0, utils_1.concatU8a)((0, utils_1.hexToU8a)(meta.selector), ...formattedInputs));
18
- return this.client.tx.contracts.call(this.address, value, gasLimit, storageDepositLimit, bytes);
18
+ const client = this.client;
19
+ if (this.registry.isRevive()) {
20
+ (0, utils_1.assert)(!(0, utils_1.isUndefined)(storageDepositLimit), 'Expected a storage deposit limit in ContractTxOptions');
21
+ return client.tx.revive.call(this.address, // --
22
+ value, gasLimit, storageDepositLimit || 0n, bytes);
23
+ }
24
+ else {
25
+ return client.tx.contracts.call(this.address, // --
26
+ value, gasLimit, storageDepositLimit, bytes);
27
+ }
19
28
  };
20
29
  callFn.meta = meta;
21
30
  return callFn;
@@ -1,20 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ContractExecutor = void 0;
4
- const codecs_1 = require("@dedot/codecs");
5
- const utils_js_1 = require("../../utils.js");
4
+ const index_js_1 = require("../../utils/index.js");
6
5
  const Executor_js_1 = require("./Executor.js");
7
6
  class ContractExecutor extends Executor_js_1.Executor {
8
7
  address;
9
8
  constructor(client, registry, address, options) {
10
9
  super(client, registry, options);
11
- this.address = new codecs_1.AccountId32(address);
10
+ this.address = address;
12
11
  }
13
12
  findMessage(message) {
14
- return this.metadata.spec.messages.find((one) => (0, utils_js_1.normalizeLabel)(one.label) === message);
13
+ return this.metadata.spec.messages.find((one) => (0, index_js_1.normalizeLabel)(one.label) === message);
15
14
  }
16
15
  findTxMessage(message) {
17
- return this.metadata.spec.messages.find((one) => one.mutates && (0, utils_js_1.normalizeLabel)(one.label) === message);
16
+ return this.metadata.spec.messages.find((one) => one.mutates && (0, index_js_1.normalizeLabel)(one.label) === message);
18
17
  }
19
18
  }
20
19
  exports.ContractExecutor = ContractExecutor;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DeployerExecutor = void 0;
4
- const utils_js_1 = require("../../utils.js");
4
+ const index_js_1 = require("../../utils/index.js");
5
5
  const Executor_js_1 = require("./Executor.js");
6
6
  class DeployerExecutor extends Executor_js_1.Executor {
7
7
  code;
@@ -10,7 +10,7 @@ class DeployerExecutor extends Executor_js_1.Executor {
10
10
  this.code = code;
11
11
  }
12
12
  findConstructorMeta(constructor) {
13
- return this.metadata.spec.constructors.find((one) => (0, utils_js_1.normalizeLabel)(one.label) === constructor);
13
+ return this.metadata.spec.constructors.find((one) => (0, index_js_1.normalizeLabel)(one.label) === constructor);
14
14
  }
15
15
  }
16
16
  exports.DeployerExecutor = DeployerExecutor;
package/cjs/index.js CHANGED
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./utils.js"), exports);
17
+ __exportStar(require("./utils/index.js"), exports);
18
18
  __exportStar(require("./errors.js"), exports);
19
19
  __exportStar(require("./executor/index.js"), exports);
20
20
  __exportStar(require("./types/index.js"), exports);
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BaseLazyObject = void 0;
4
+ const utils_1 = require("@dedot/utils");
5
+ const index_js_1 = require("../utils/index.js");
6
+ class BaseLazyObject {
7
+ typeDef;
8
+ registry;
9
+ constructor(typeDef, registry) {
10
+ this.typeDef = typeDef;
11
+ this.registry = registry;
12
+ }
13
+ findStorageRootLayout(typeId) {
14
+ const rootLayout = this.registry.metadata.storage;
15
+ const rootKey = (0, index_js_1.findStorageRootKey)(rootLayout, typeId);
16
+ (0, utils_1.assert)(rootKey, 'Root Key Not Found');
17
+ return rootKey;
18
+ }
19
+ getContractStorage(key) {
20
+ const { getStorage } = this.registry.options || {};
21
+ (0, utils_1.assert)(getStorage, 'getStorage is not available');
22
+ return getStorage((0, utils_1.toU8a)(key));
23
+ }
24
+ }
25
+ exports.BaseLazyObject = BaseLazyObject;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LazyMapping = void 0;
4
+ const utils_1 = require("@dedot/utils");
5
+ const BaseLazyObject_js_1 = require("./BaseLazyObject.js");
6
+ class LazyMapping extends BaseLazyObject_js_1.BaseLazyObject {
7
+ async get(key) {
8
+ const { id, type: { params }, } = this.typeDef;
9
+ const [keyType, valueType] = params;
10
+ const $Key = this.registry.findCodec(keyType.type);
11
+ const $Value = this.registry.findCodec(valueType.type);
12
+ const rootKey = this.findStorageRootLayout(id);
13
+ const encodedKey = $Key.tryEncode(key);
14
+ const storageKey = (0, utils_1.concatU8a)((0, utils_1.toU8a)(rootKey), encodedKey);
15
+ const rawValue = await this.getContractStorage(storageKey);
16
+ if (rawValue) {
17
+ return $Value.tryDecode(rawValue);
18
+ }
19
+ return undefined;
20
+ }
21
+ }
22
+ exports.LazyMapping = LazyMapping;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LazyObject = void 0;
4
+ const BaseLazyObject_js_1 = require("./BaseLazyObject.js");
5
+ class LazyObject extends BaseLazyObject_js_1.BaseLazyObject {
6
+ async get() {
7
+ const { id, type: { params }, } = this.typeDef;
8
+ const [valueType] = params;
9
+ const $Value = this.registry.findCodec(valueType.type);
10
+ const rootKey = this.findStorageRootLayout(id);
11
+ const rawValue = await this.getContractStorage(rootKey);
12
+ if (rawValue) {
13
+ return $Value.tryDecode(rawValue);
14
+ }
15
+ return undefined;
16
+ }
17
+ }
18
+ exports.LazyObject = LazyObject;
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.LazyStorageVec = void 0;
27
+ const $ = __importStar(require("@dedot/shape"));
28
+ const utils_1 = require("@dedot/utils");
29
+ const BaseLazyObject_js_1 = require("./BaseLazyObject.js");
30
+ class LazyStorageVec extends BaseLazyObject_js_1.BaseLazyObject {
31
+ async len() {
32
+ const { id } = this.typeDef;
33
+ const rootKey = this.findStorageRootLayout(id);
34
+ const rawValue = await this.getContractStorage(rootKey);
35
+ if (rawValue) {
36
+ return $.u32.tryDecode(rawValue);
37
+ }
38
+ return 0;
39
+ }
40
+ async get(index) {
41
+ const { id, type: { params }, } = this.typeDef;
42
+ const [valueType] = params;
43
+ const $Value = this.registry.findCodec(valueType.type);
44
+ const rootKey = this.findStorageRootLayout(id);
45
+ const encodedKey = $.u32.tryEncode(index);
46
+ const storageKey = (0, utils_1.concatU8a)((0, utils_1.toU8a)(rootKey), encodedKey);
47
+ const rawValue = await this.getContractStorage(storageKey);
48
+ if (rawValue) {
49
+ return $Value.tryDecode(rawValue);
50
+ }
51
+ return undefined;
52
+ }
53
+ }
54
+ exports.LazyStorageVec = LazyStorageVec;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./LazyObject.js"), exports);
18
+ __exportStar(require("./LazyMapping.js"), exports);
19
+ __exportStar(require("./LazyStorageVec.js"), exports);