@pezkuwi/api-contract 16.5.5

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 (162) hide show
  1. package/README.md +3 -0
  2. package/build/Abi/index.d.ts +31 -0
  3. package/build/Abi/toLatestCompatible.d.ts +15 -0
  4. package/build/Abi/toV1.d.ts +3 -0
  5. package/build/Abi/toV2.d.ts +3 -0
  6. package/build/Abi/toV3.d.ts +3 -0
  7. package/build/Abi/toV4.d.ts +3 -0
  8. package/build/augment.d.ts +1 -0
  9. package/build/base/Base.d.ts +13 -0
  10. package/build/base/Blueprint.d.ts +24 -0
  11. package/build/base/Code.d.ts +22 -0
  12. package/build/base/Contract.d.ts +25 -0
  13. package/build/base/index.d.ts +3 -0
  14. package/build/base/mock.d.ts +3 -0
  15. package/build/base/types.d.ts +25 -0
  16. package/build/base/util.d.ts +16 -0
  17. package/build/bundle.d.ts +4 -0
  18. package/build/index.d.ts +2 -0
  19. package/build/packageDetect.d.ts +1 -0
  20. package/build/packageInfo.d.ts +6 -0
  21. package/build/promise/index.d.ts +13 -0
  22. package/build/promise/types.d.ts +3 -0
  23. package/build/rx/index.d.ts +13 -0
  24. package/build/rx/types.d.ts +3 -0
  25. package/build/types.d.ts +79 -0
  26. package/build/util.d.ts +5 -0
  27. package/package.json +39 -0
  28. package/src/Abi/Abi.spec.ts +235 -0
  29. package/src/Abi/index.ts +477 -0
  30. package/src/Abi/toLatestCompatible.spec.ts +219 -0
  31. package/src/Abi/toLatestCompatible.ts +52 -0
  32. package/src/Abi/toV1.ts +35 -0
  33. package/src/Abi/toV2.ts +58 -0
  34. package/src/Abi/toV3.ts +18 -0
  35. package/src/Abi/toV4.ts +21 -0
  36. package/src/augment.ts +4 -0
  37. package/src/base/Base.ts +52 -0
  38. package/src/base/Blueprint.ts +90 -0
  39. package/src/base/Code.spec.ts +47 -0
  40. package/src/base/Code.ts +142 -0
  41. package/src/base/Contract.ts +197 -0
  42. package/src/base/index.ts +6 -0
  43. package/src/base/mock.ts +48 -0
  44. package/src/base/types.ts +40 -0
  45. package/src/base/util.ts +56 -0
  46. package/src/bundle.ts +10 -0
  47. package/src/checkTypes.manual.ts +45 -0
  48. package/src/index.ts +6 -0
  49. package/src/mod.ts +4 -0
  50. package/src/packageDetect.ts +13 -0
  51. package/src/packageInfo.ts +6 -0
  52. package/src/promise/index.ts +28 -0
  53. package/src/promise/types.ts +7 -0
  54. package/src/rx/index.ts +28 -0
  55. package/src/rx/types.ts +7 -0
  56. package/src/test/compare/ink_v0_delegator.test.json +47 -0
  57. package/src/test/compare/ink_v0_dns.test.json +232 -0
  58. package/src/test/compare/ink_v0_erc20.test.json +253 -0
  59. package/src/test/compare/ink_v0_erc721.test.json +415 -0
  60. package/src/test/compare/ink_v0_flipper.test.json +9 -0
  61. package/src/test/compare/ink_v0_flipperBundle.test.json +9 -0
  62. package/src/test/compare/ink_v0_incrementer.test.json +9 -0
  63. package/src/test/compare/ink_v0_multisigPlain.test.json +562 -0
  64. package/src/test/compare/ink_v1_flipper.test.json +9 -0
  65. package/src/test/compare/ink_v1_psp22.test.json +531 -0
  66. package/src/test/compare/ink_v2_erc20.test.json +205 -0
  67. package/src/test/compare/ink_v2_flipper.test.json +9 -0
  68. package/src/test/compare/ink_v3_flipper.test.json +9 -0
  69. package/src/test/compare/ink_v3_traitErc20.test.json +205 -0
  70. package/src/test/compare/ink_v4_erc20Contract.test.json +253 -0
  71. package/src/test/compare/ink_v4_erc20Metadata.test.json +253 -0
  72. package/src/test/compare/ink_v4_flipperContract.test.json +155 -0
  73. package/src/test/compare/ink_v4_flipperMetadata.test.json +155 -0
  74. package/src/test/compare/ink_v5_erc20.test.json +370 -0
  75. package/src/test/compare/ink_v5_erc20AnonymousTransferMetadata.test.json +370 -0
  76. package/src/test/compare/ink_v5_erc20Contract.test.json +370 -0
  77. package/src/test/compare/ink_v5_erc20Metadata.test.json +370 -0
  78. package/src/test/compare/ink_v5_flipperContract.test.json +174 -0
  79. package/src/test/compare/ink_v5_flipperMetadata.test.json +174 -0
  80. package/src/test/compare/ink_v6_erc20Contract.test.json +418 -0
  81. package/src/test/compare/ink_v6_erc20Metadata.test.json +418 -0
  82. package/src/test/compare/solang_v0_ints256.test.json +9 -0
  83. package/src/test/compare/user_v0_assetTransfer.test.json +54 -0
  84. package/src/test/compare/user_v0_enumExample.test.json +303 -0
  85. package/src/test/compare/user_v0_recursive.test.json +27 -0
  86. package/src/test/compare/user_v0_withString.test.json +260 -0
  87. package/src/test/compare/user_v3_ask.test.json +71 -0
  88. package/src/test/compare/user_v4_events.test.json +1328 -0
  89. package/src/test/contracts/index.ts +20 -0
  90. package/src/test/contracts/ink/index.ts +13 -0
  91. package/src/test/contracts/ink/v0/accumulator.wasm +0 -0
  92. package/src/test/contracts/ink/v0/adder.wasm +0 -0
  93. package/src/test/contracts/ink/v0/delegator.json +252 -0
  94. package/src/test/contracts/ink/v0/delegator.wasm +0 -0
  95. package/src/test/contracts/ink/v0/dns.json +713 -0
  96. package/src/test/contracts/ink/v0/dns.wasm +0 -0
  97. package/src/test/contracts/ink/v0/erc20.json +704 -0
  98. package/src/test/contracts/ink/v0/erc20.wasm +0 -0
  99. package/src/test/contracts/ink/v0/erc721.json +1197 -0
  100. package/src/test/contracts/ink/v0/erc721.wasm +0 -0
  101. package/src/test/contracts/ink/v0/flipper.contract.json +107 -0
  102. package/src/test/contracts/ink/v0/flipper.json +106 -0
  103. package/src/test/contracts/ink/v0/flipper.wasm +0 -0
  104. package/src/test/contracts/ink/v0/incrementer.json +108 -0
  105. package/src/test/contracts/ink/v0/incrementer.wasm +0 -0
  106. package/src/test/contracts/ink/v0/index.ts +11 -0
  107. package/src/test/contracts/ink/v0/multisig_plain.json +1466 -0
  108. package/src/test/contracts/ink/v0/multisig_plain.wasm +0 -0
  109. package/src/test/contracts/ink/v0/subber.wasm +0 -0
  110. package/src/test/contracts/ink/v0/trait-flipper.json +103 -0
  111. package/src/test/contracts/ink/v0/trait-flipper.wasm +0 -0
  112. package/src/test/contracts/ink/v1/flipper.contract.json +111 -0
  113. package/src/test/contracts/ink/v1/index.ts +6 -0
  114. package/src/test/contracts/ink/v1/psp22_minter_pauser.contract.json +1722 -0
  115. package/src/test/contracts/ink/v2/erc20.contract.json +630 -0
  116. package/src/test/contracts/ink/v2/flipper.contract.json +103 -0
  117. package/src/test/contracts/ink/v2/index.ts +5 -0
  118. package/src/test/contracts/ink/v3/flipper.contract.json +105 -0
  119. package/src/test/contracts/ink/v3/index.ts +6 -0
  120. package/src/test/contracts/ink/v3/trait_erc20.contract.json +631 -0
  121. package/src/test/contracts/ink/v4/erc20.contract.json +1 -0
  122. package/src/test/contracts/ink/v4/erc20.json +821 -0
  123. package/src/test/contracts/ink/v4/erc20.wasm +0 -0
  124. package/src/test/contracts/ink/v4/flipper.contract.json +1 -0
  125. package/src/test/contracts/ink/v4/flipper.json +396 -0
  126. package/src/test/contracts/ink/v4/flipper.wasm +0 -0
  127. package/src/test/contracts/ink/v4/index.ts +7 -0
  128. package/src/test/contracts/ink/v5/erc20.contract.json +1 -0
  129. package/src/test/contracts/ink/v5/erc20.json +1025 -0
  130. package/src/test/contracts/ink/v5/erc20.wasm +0 -0
  131. package/src/test/contracts/ink/v5/erc20_anonymous_transfer.json +1025 -0
  132. package/src/test/contracts/ink/v5/flipper.contract.json +1 -0
  133. package/src/test/contracts/ink/v5/flipper.json +420 -0
  134. package/src/test/contracts/ink/v5/flipper.wasm +0 -0
  135. package/src/test/contracts/ink/v5/index.ts +8 -0
  136. package/src/test/contracts/ink/v6/erc20.contract.json +1 -0
  137. package/src/test/contracts/ink/v6/erc20.json +1081 -0
  138. package/src/test/contracts/ink/v6/erc20.polkavm +0 -0
  139. package/src/test/contracts/ink/v6/index.ts +5 -0
  140. package/src/test/contracts/solang/index.ts +7 -0
  141. package/src/test/contracts/solang/v0/index.ts +4 -0
  142. package/src/test/contracts/solang/v0/ints256.json +113 -0
  143. package/src/test/contracts/solang/v0/ints256.sol +13 -0
  144. package/src/test/contracts/solang/v0/ints256.wasm +0 -0
  145. package/src/test/contracts/user/index.ts +9 -0
  146. package/src/test/contracts/user/v0/assetTransfer.json +299 -0
  147. package/src/test/contracts/user/v0/assetTransfer.wasm +0 -0
  148. package/src/test/contracts/user/v0/enumExample.json +528 -0
  149. package/src/test/contracts/user/v0/enumExample.wasm +0 -0
  150. package/src/test/contracts/user/v0/index.ts +7 -0
  151. package/src/test/contracts/user/v0/recursive.contract.json +1 -0
  152. package/src/test/contracts/user/v0/withString.json +777 -0
  153. package/src/test/contracts/user/v3/ask.json +550 -0
  154. package/src/test/contracts/user/v3/index.ts +4 -0
  155. package/src/test/contracts/user/v4/events.contract.json +2990 -0
  156. package/src/test/contracts/user/v4/index.ts +4 -0
  157. package/src/test/contracts/util.ts +14 -0
  158. package/src/types.ts +98 -0
  159. package/src/util.ts +20 -0
  160. package/tsconfig.build.json +22 -0
  161. package/tsconfig.build.tsbuildinfo +1 -0
  162. package/tsconfig.spec.json +26 -0
@@ -0,0 +1,477 @@
1
+ // Copyright 2017-2025 @polkadot/api-contract authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { Bytes, Vec } from '@pezkuwi/types';
5
+ import type { ChainProperties, ContractConstructorSpecLatest, ContractEventParamSpecLatest, ContractMessageParamSpecLatest, ContractMessageSpecLatest, ContractMetadata, ContractMetadataV4, ContractMetadataV5, ContractMetadataV6, ContractProjectInfo, ContractTypeSpec, EventRecord } from '@pezkuwi/types/interfaces';
6
+ import type { Codec, Registry, TypeDef } from '@pezkuwi/types/types';
7
+ import type { AbiConstructor, AbiEvent, AbiEventParam, AbiMessage, AbiMessageParam, AbiParam, DecodedEvent, DecodedMessage } from '../types.js';
8
+
9
+ import { Option, TypeRegistry } from '@pezkuwi/types';
10
+ import { TypeDefInfo } from '@pezkuwi/types-create';
11
+ import { assertReturn, compactAddLength, compactStripLength, isBn, isNumber, isObject, isString, isUndefined, logger, stringCamelCase, stringify, u8aConcat, u8aToHex } from '@pezkuwi/util';
12
+
13
+ import { convertVersions, enumVersions } from './toLatestCompatible.js';
14
+
15
+ interface AbiJson {
16
+ version?: string;
17
+
18
+ [key: string]: unknown;
19
+ }
20
+
21
+ type EventOf<M> = M extends {spec: { events: Vec<infer E>}} ? E : never
22
+ export type ContractMetadataSupported = ContractMetadataV4 | ContractMetadataV5 | ContractMetadataV6;
23
+ type ContractEventSupported = EventOf<ContractMetadataSupported>;
24
+
25
+ const l = logger('Abi');
26
+
27
+ const PRIMITIVE_ALWAYS = ['AccountId', 'AccountId20', 'AccountIndex', 'Address', 'Balance'];
28
+
29
+ function findMessage <T extends AbiMessage> (list: T[], messageOrId: T | string | number): T {
30
+ const message = isNumber(messageOrId)
31
+ ? list[messageOrId]
32
+ : isString(messageOrId)
33
+ ? list.find(({ identifier }) => [identifier, stringCamelCase(identifier)].includes(messageOrId.toString()))
34
+ : messageOrId;
35
+
36
+ return assertReturn(message, () => `Attempted to call an invalid contract interface, ${stringify(messageOrId)}`);
37
+ }
38
+
39
+ function getMetadata (registry: Registry, json: AbiJson): ContractMetadataSupported {
40
+ // this is for V1, V2, V3
41
+ const vx = enumVersions.find((v) => isObject(json[v]));
42
+
43
+ // this was added in V4
44
+ const jsonVersion = json.version;
45
+
46
+ if (!vx && jsonVersion && !enumVersions.find((v) => v === `V${jsonVersion}`)) {
47
+ throw new Error(`Unable to handle version ${jsonVersion}`);
48
+ }
49
+
50
+ const metadata = registry.createType<ContractMetadata>('ContractMetadata',
51
+ vx
52
+ ? { [vx]: json[vx] }
53
+ : jsonVersion
54
+ ? { [`V${jsonVersion}`]: json }
55
+ : { V0: json }
56
+ );
57
+
58
+ const converter = convertVersions.find(([v]) => metadata[`is${v}`]);
59
+
60
+ if (!converter) {
61
+ throw new Error(`Unable to convert ABI with version ${metadata.type} to a supported version`);
62
+ }
63
+
64
+ const upgradedMetadata = converter[1](registry, metadata[`as${converter[0]}`]);
65
+
66
+ return upgradedMetadata;
67
+ }
68
+
69
+ function isRevive (json: Record<string, unknown>): boolean {
70
+ const source = json['source'];
71
+ const version = json['version'];
72
+
73
+ const hasContractBinary =
74
+ typeof source === 'object' &&
75
+ source !== null &&
76
+ 'contract_binary' in source;
77
+
78
+ const hasVersion =
79
+ typeof version === 'number' && version >= 6;
80
+
81
+ return hasContractBinary || hasVersion;
82
+ }
83
+
84
+ function parseJson (json: Record<string, unknown>, chainProperties?: ChainProperties): [Record<string, unknown>, Registry, ContractMetadataSupported, ContractProjectInfo, boolean] {
85
+ const registry = new TypeRegistry();
86
+
87
+ const revive = isRevive(json);
88
+ const typeName = revive ? 'ContractReviveProjectInfo' : 'ContractProjectInfo';
89
+
90
+ const info = registry.createType(typeName, json) as unknown as ContractProjectInfo;
91
+ const metadata = getMetadata(registry, json as unknown as AbiJson);
92
+ const lookup = registry.createType('PortableRegistry', { types: metadata.types }, true);
93
+
94
+ // attach the lookup to the registry - now the types are known
95
+ registry.setLookup(lookup);
96
+
97
+ if (chainProperties) {
98
+ registry.setChainProperties(chainProperties);
99
+ }
100
+
101
+ // warm-up the actual type, pre-use
102
+ lookup.types.forEach(({ id }) =>
103
+ lookup.getTypeDef(id)
104
+ );
105
+
106
+ return [json, registry, metadata, info, revive];
107
+ }
108
+
109
+ /**
110
+ * @internal
111
+ * Determines if the given input value is a ContractTypeSpec
112
+ */
113
+ function isTypeSpec (value: Codec): value is ContractTypeSpec {
114
+ return !!value && value instanceof Map && !isUndefined((value as ContractTypeSpec).type) && !isUndefined((value as ContractTypeSpec).displayName);
115
+ }
116
+
117
+ /**
118
+ * @internal
119
+ * Determines if the given input value is an Option
120
+ */
121
+ function isOption (value: Codec): value is Option<Codec> {
122
+ return !!value && value instanceof Option;
123
+ }
124
+
125
+ export class Abi {
126
+ readonly events: AbiEvent[];
127
+ readonly constructors: AbiConstructor[];
128
+ readonly info: ContractProjectInfo;
129
+ readonly json: Record<string, unknown>;
130
+ readonly messages: AbiMessage[];
131
+ readonly metadata: ContractMetadataSupported;
132
+ readonly registry: Registry;
133
+ readonly environment = new Map<string, TypeDef | Codec>();
134
+ readonly isRevive: boolean;
135
+
136
+ constructor (abiJson: Record<string, unknown> | string, chainProperties?: ChainProperties) {
137
+ [this.json, this.registry, this.metadata, this.info, this.isRevive] = parseJson(
138
+ isString(abiJson)
139
+ ? JSON.parse(abiJson) as Record<string, unknown>
140
+ : abiJson,
141
+ chainProperties
142
+ );
143
+ this.constructors = this.metadata.spec.constructors.map((spec: ContractConstructorSpecLatest, index) =>
144
+ this.#createMessage(spec, index, {
145
+ isConstructor: true,
146
+ isDefault: spec.default.isTrue,
147
+ isPayable: spec.payable.isTrue,
148
+ returnType: spec.returnType.isSome
149
+ ? this.registry.lookup.getTypeDef(spec.returnType.unwrap().type)
150
+ : null
151
+ })
152
+ );
153
+ this.events = this.metadata.spec.events.map((_: ContractEventSupported, index: number) =>
154
+ this.#createEvent(index)
155
+ );
156
+ this.messages = this.metadata.spec.messages.map((spec: ContractMessageSpecLatest, index): AbiMessage =>
157
+ this.#createMessage(spec, index, {
158
+ isDefault: spec.default.isTrue,
159
+ isMutating: spec.mutates.isTrue,
160
+ isPayable: spec.payable.isTrue,
161
+ returnType: spec.returnType.isSome
162
+ ? this.registry.lookup.getTypeDef(spec.returnType.unwrap().type)
163
+ : null
164
+ })
165
+ );
166
+
167
+ // NOTE See the rationale for having Option<...> values in the actual
168
+ // ContractEnvironmentV4 structure definition in interfaces/contractsAbi
169
+ // (Due to conversions, the fields may not exist)
170
+ for (const [key, opt] of this.metadata.spec.environment.entries()) {
171
+ if (isOption(opt)) {
172
+ if (opt.isSome) {
173
+ const value = opt.unwrap();
174
+
175
+ if (isBn(value)) {
176
+ this.environment.set(key, value);
177
+ } else if (isTypeSpec(value)) {
178
+ this.environment.set(key, this.registry.lookup.getTypeDef(value.type));
179
+ } else {
180
+ throw new Error(`Invalid environment definition for ${key}:: Expected either Number or ContractTypeSpec`);
181
+ }
182
+ }
183
+ } else {
184
+ throw new Error(`Expected Option<*> definition for ${key} in ContractEnvironment`);
185
+ }
186
+ }
187
+ }
188
+
189
+ /**
190
+ * Warning: Unstable API, bound to change
191
+ */
192
+ public decodeEvent (record: EventRecord): DecodedEvent {
193
+ switch (this.metadata.version.toString()) {
194
+ // earlier version are hoisted to v4
195
+ case '4':
196
+ return this.#decodeEventV4(record);
197
+ case '5':
198
+ return this.#decodeEventV5(record);
199
+ // Latest
200
+ default:
201
+ return this.#decodeEventV6(record);
202
+ }
203
+ }
204
+
205
+ #decodeEventV6 = (record: EventRecord): DecodedEvent => {
206
+ const topics = record.event.data[2] as unknown as { toHex: () => string }[];
207
+ // Try to match by signature topic (first topic)
208
+ const signatureTopic = topics[0];
209
+ const data = record.event.data[1] as Bytes;
210
+
211
+ if (signatureTopic) {
212
+ const event = this.events.find((e) => e.signatureTopic !== undefined && e.signatureTopic !== null && e.signatureTopic === signatureTopic.toHex());
213
+
214
+ // Early return if event found by signature topic
215
+ if (event) {
216
+ return event.fromU8a(data);
217
+ }
218
+ }
219
+
220
+ // If no event returned yet, it might be anonymous
221
+ const amountOfTopics = topics.length;
222
+ const potentialEvents = this.events.filter((e) => {
223
+ // event can't have a signature topic
224
+ if (e.signatureTopic !== null && e.signatureTopic !== undefined) {
225
+ return false;
226
+ }
227
+
228
+ // event should have same amount of indexed fields as emitted topics
229
+ const amountIndexed = e.args.filter((a) => a.indexed).length;
230
+
231
+ if (amountIndexed !== amountOfTopics) {
232
+ return false;
233
+ }
234
+
235
+ // If all conditions met, it's a potential event
236
+ return true;
237
+ });
238
+
239
+ if (potentialEvents.length === 1) {
240
+ return potentialEvents[0].fromU8a(data);
241
+ }
242
+
243
+ throw new Error('Unable to determine event');
244
+ };
245
+
246
+ #decodeEventV5 = (record: EventRecord): DecodedEvent => {
247
+ // Find event by first topic, which potentially is the signature_topic
248
+ const signatureTopic = record.topics[0];
249
+ const data = record.event.data[1] as Bytes;
250
+
251
+ if (signatureTopic) {
252
+ const event = this.events.find((e) => e.signatureTopic !== undefined && e.signatureTopic !== null && e.signatureTopic === signatureTopic.toHex());
253
+
254
+ // Early return if event found by signature topic
255
+ if (event) {
256
+ return event.fromU8a(data);
257
+ }
258
+ }
259
+
260
+ // If no event returned yet, it might be anonymous
261
+ const amountOfTopics = record.topics.length;
262
+ const potentialEvents = this.events.filter((e) => {
263
+ // event can't have a signature topic
264
+ if (e.signatureTopic !== null && e.signatureTopic !== undefined) {
265
+ return false;
266
+ }
267
+
268
+ // event should have same amount of indexed fields as emitted topics
269
+ const amountIndexed = e.args.filter((a) => a.indexed).length;
270
+
271
+ if (amountIndexed !== amountOfTopics) {
272
+ return false;
273
+ }
274
+
275
+ // If all conditions met, it's a potential event
276
+ return true;
277
+ });
278
+
279
+ if (potentialEvents.length === 1) {
280
+ return potentialEvents[0].fromU8a(data);
281
+ }
282
+
283
+ throw new Error('Unable to determine event');
284
+ };
285
+
286
+ #decodeEventV4 = (record: EventRecord): DecodedEvent => {
287
+ const data = record.event.data[1] as Bytes;
288
+ const index = data[0];
289
+ const event = this.events[index];
290
+
291
+ if (!event) {
292
+ throw new Error(`Unable to find event with index ${index}`);
293
+ }
294
+
295
+ return event.fromU8a(data.subarray(1));
296
+ };
297
+
298
+ /**
299
+ * Warning: Unstable API, bound to change
300
+ */
301
+ public decodeConstructor (data: Uint8Array): DecodedMessage {
302
+ return this.#decodeMessage('message', this.constructors, data);
303
+ }
304
+
305
+ /**
306
+ * Warning: Unstable API, bound to change
307
+ */
308
+ public decodeMessage (data: Uint8Array): DecodedMessage {
309
+ return this.#decodeMessage('message', this.messages, data);
310
+ }
311
+
312
+ public findConstructor (constructorOrId: AbiConstructor | string | number): AbiConstructor {
313
+ return findMessage(this.constructors, constructorOrId);
314
+ }
315
+
316
+ public findMessage (messageOrId: AbiMessage | string | number): AbiMessage {
317
+ return findMessage(this.messages, messageOrId);
318
+ }
319
+
320
+ #createArgs = (args: ContractMessageParamSpecLatest[] | ContractEventParamSpecLatest[], spec: unknown): AbiParam[] => {
321
+ return args.map(({ label, type }, index): AbiParam => {
322
+ try {
323
+ if (!isObject(type)) {
324
+ throw new Error('Invalid type definition found');
325
+ }
326
+
327
+ const displayName = type.displayName.length
328
+ ? type.displayName[type.displayName.length - 1].toString()
329
+ : undefined;
330
+ const camelName = stringCamelCase(label);
331
+
332
+ if (displayName && PRIMITIVE_ALWAYS.includes(displayName)) {
333
+ return {
334
+ name: camelName,
335
+ type: {
336
+ info: TypeDefInfo.Plain,
337
+ type: displayName
338
+ }
339
+ };
340
+ }
341
+
342
+ const typeDef = this.registry.lookup.getTypeDef(type.type);
343
+
344
+ return {
345
+ name: camelName,
346
+ type: displayName && !typeDef.type.startsWith(displayName)
347
+ ? { displayName, ...typeDef }
348
+ : typeDef
349
+ };
350
+ } catch (error) {
351
+ l.error(`Error expanding argument ${index} in ${stringify(spec)}`);
352
+
353
+ throw error;
354
+ }
355
+ });
356
+ };
357
+
358
+ #createMessageParams = (args: ContractMessageParamSpecLatest[], spec: unknown): AbiMessageParam[] => {
359
+ return this.#createArgs(args, spec);
360
+ };
361
+
362
+ #createEventParams = (args: ContractEventParamSpecLatest[], spec: unknown): AbiEventParam[] => {
363
+ const params = this.#createArgs(args, spec);
364
+
365
+ return params.map((p, index): AbiEventParam => ({ ...p, indexed: args[index].indexed.toPrimitive() }));
366
+ };
367
+
368
+ #createEvent = (index: number): AbiEvent => {
369
+ // TODO TypeScript would narrow this type to the correct version,
370
+ // but version is `Text` so I need to call `toString()` here,
371
+ // which breaks the type inference.
372
+ switch (this.metadata.version.toString()) {
373
+ case '4':
374
+ return this.#createEventV4((this.metadata as ContractMetadataV4).spec.events[index], index);
375
+ default:
376
+ return this.#createEventV5((this.metadata as ContractMetadataV5).spec.events[index], index);
377
+ }
378
+ };
379
+
380
+ #createEventV5 = (spec: EventOf<ContractMetadataV5>, index: number): AbiEvent => {
381
+ const args = this.#createEventParams(spec.args, spec);
382
+ const event = {
383
+ args,
384
+ docs: spec.docs.map((d) => d.toString()),
385
+ fromU8a: (data: Uint8Array): DecodedEvent => ({
386
+ args: this.#decodeArgs(args, data),
387
+ event
388
+ }),
389
+ identifier: [spec.module_path, spec.label].join('::'),
390
+ index,
391
+ signatureTopic: spec.signature_topic.isSome ? spec.signature_topic.unwrap().toHex() : null
392
+ };
393
+
394
+ return event;
395
+ };
396
+
397
+ #createEventV4 = (spec: EventOf<ContractMetadataV4>, index: number): AbiEvent => {
398
+ const args = this.#createEventParams(spec.args, spec);
399
+ const event = {
400
+ args,
401
+ docs: spec.docs.map((d) => d.toString()),
402
+ fromU8a: (data: Uint8Array): DecodedEvent => ({
403
+ args: this.#decodeArgs(args, data),
404
+ event
405
+ }),
406
+ identifier: spec.label.toString(),
407
+ index
408
+ };
409
+
410
+ return event;
411
+ };
412
+
413
+ #createMessage = (spec: ContractMessageSpecLatest | ContractConstructorSpecLatest, index: number, add: Partial<AbiMessage> = {}): AbiMessage => {
414
+ const args = this.#createMessageParams(spec.args, spec);
415
+ const identifier = spec.label.toString();
416
+ const message = {
417
+ ...add,
418
+ args,
419
+ docs: spec.docs.map((d) => d.toString()),
420
+ fromU8a: (data: Uint8Array): DecodedMessage => ({
421
+ args: this.#decodeArgs(args, data),
422
+ message
423
+ }),
424
+ identifier,
425
+ index,
426
+ isDefault: spec.default.isTrue,
427
+ method: stringCamelCase(identifier),
428
+ path: identifier.split('::').map((s) => stringCamelCase(s)),
429
+ selector: spec.selector,
430
+ toU8a: (params: unknown[]) =>
431
+ this.#encodeMessageArgs(spec, args, params)
432
+ };
433
+
434
+ return message;
435
+ };
436
+
437
+ #decodeArgs = (args: AbiParam[], data: Uint8Array): Codec[] => {
438
+ // for decoding we expect the input to be just the arg data, no selectors
439
+ // no length added (this allows use with events as well)
440
+ let offset = 0;
441
+
442
+ return args.map(({ type: { lookupName, type } }): Codec => {
443
+ const value = this.registry.createType(lookupName || type, data.subarray(offset));
444
+
445
+ offset += value.encodedLength;
446
+
447
+ return value;
448
+ });
449
+ };
450
+
451
+ #decodeMessage = (type: 'constructor' | 'message', list: AbiMessage[], data: Uint8Array): DecodedMessage => {
452
+ const [, trimmed] = compactStripLength(data);
453
+ const selector = trimmed.subarray(0, 4);
454
+ const message = list.find((m) => m.selector.eq(selector));
455
+
456
+ if (!message) {
457
+ throw new Error(`Unable to find ${type} with selector ${u8aToHex(selector)}`);
458
+ }
459
+
460
+ return message.fromU8a(trimmed.subarray(4));
461
+ };
462
+
463
+ #encodeMessageArgs = ({ label, selector }: ContractMessageSpecLatest | ContractConstructorSpecLatest, args: AbiMessageParam[], data: unknown[]): Uint8Array => {
464
+ if (data.length !== args.length) {
465
+ throw new Error(`Expected ${args.length} arguments to contract message '${label.toString()}', found ${data.length}`);
466
+ }
467
+
468
+ return compactAddLength(
469
+ u8aConcat(
470
+ this.registry.createType('ContractSelector', selector).toU8a(),
471
+ ...args.map(({ type: { lookupName, type } }, index) =>
472
+ this.registry.createType(lookupName || type, data[index]).toU8a()
473
+ )
474
+ )
475
+ );
476
+ };
477
+ }
@@ -0,0 +1,219 @@
1
+ // Copyright 2017-2025 @polkadot/api-contract authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /// <reference types="@pezkuwi/dev-test/globals.d.ts" />
5
+
6
+ import { TypeRegistry } from '@pezkuwi/types';
7
+
8
+ import abis from '../test/contracts/index.js';
9
+ import { v0ToLatestCompatible, v1ToLatestCompatible, v2ToLatestCompatible, v3ToLatestCompatible, v4ToLatestCompatible, v5ToLatestCompatible, v6ToLatestCompatible } from './toLatestCompatible.js';
10
+
11
+ describe('v0ToLatestCompatible', (): void => {
12
+ const registry = new TypeRegistry();
13
+ const contract = registry.createType('ContractMetadata', { V0: abis['ink_v0_erc20'] });
14
+ const latest = v0ToLatestCompatible(registry, contract.asV0);
15
+
16
+ it('has the correct constructors', (): void => {
17
+ expect(
18
+ latest.spec.constructors.map(({ label }) => label.toString())
19
+ ).toEqual(['new']);
20
+ });
21
+
22
+ it('has the correct messages', (): void => {
23
+ expect(
24
+ latest.spec.messages.map(({ label }) => label.toString())
25
+ ).toEqual(['total_supply', 'balance_of', 'allowance', 'transfer', 'approve', 'transfer_from']);
26
+ });
27
+
28
+ it('has the correct events', (): void => {
29
+ expect(
30
+ latest.spec.events.map(({ label }) => label.toString())
31
+ ).toEqual(['Transfer', 'Approval']);
32
+ });
33
+
34
+ it('has the correct constructor arguments', (): void => {
35
+ expect(
36
+ latest.spec.constructors[0].args.map(({ label }) => label.toString())
37
+ ).toEqual(['initial_supply']);
38
+ });
39
+
40
+ it('has the correct message arguments', (): void => {
41
+ expect(
42
+ latest.spec.messages[1].args.map(({ label }) => label.toString())
43
+ ).toEqual(['owner']);
44
+ });
45
+
46
+ it('has the correct event arguments', (): void => {
47
+ expect(
48
+ latest.spec.events[0].args.map(({ label }) => label.toString())
49
+ ).toEqual(['from', 'to', 'value']);
50
+ });
51
+
52
+ it('has the latest compatible version number', (): void => {
53
+ expect(latest.version.toString()).toEqual('4');
54
+ });
55
+ });
56
+
57
+ describe('v1ToLatestCompatible', (): void => {
58
+ const registry = new TypeRegistry();
59
+ const contract = registry.createType('ContractMetadata', { V1: abis['ink_v1_flipper']['V1'] });
60
+ const latest = v1ToLatestCompatible(registry, contract.asV1);
61
+
62
+ it('has the correct constructors', (): void => {
63
+ expect(
64
+ latest.spec.constructors.map(({ label }) => label.toString())
65
+ ).toEqual(['new', 'default']);
66
+ });
67
+
68
+ it('has the correct messages', (): void => {
69
+ expect(
70
+ latest.spec.messages.map(({ label }) => label.toString())
71
+ ).toEqual(['flip', 'get']);
72
+ });
73
+
74
+ it('has the correct messages with namespaced method name', (): void => {
75
+ const contract = registry.createType('ContractMetadata', { V1: abis['ink_v1_psp22']['V1'] });
76
+ const latest = v1ToLatestCompatible(registry, contract.asV1);
77
+
78
+ expect(
79
+ latest.spec.messages.map(({ label }) => label.toString())
80
+ ).toEqual([
81
+ 'PSP22Metadata::token_name', 'PSP22Metadata::token_symbol', 'PSP22Metadata::token_decimals', 'PSP22Mintable::mint', 'PSP22::decrease_allowance', 'PSP22::transfer', 'PSP22::approve', 'PSP22::allowance', 'PSP22::transfer_from', 'PSP22::balance_of', 'PSP22::increase_allowance', 'PSP22::total_supply', 'pause', 'unpause'
82
+ ]);
83
+ });
84
+
85
+ it('has the correct constructor arguments', (): void => {
86
+ expect(
87
+ latest.spec.constructors[0].args.map(({ label }) => label.toString())
88
+ ).toEqual(['init_value']);
89
+ });
90
+
91
+ it('has the latest compatible version number', (): void => {
92
+ expect(latest.version.toString()).toEqual('4');
93
+ });
94
+ });
95
+
96
+ describe('v2ToLatestCompatible', (): void => {
97
+ const registry = new TypeRegistry();
98
+ const contract = registry.createType('ContractMetadata', { V2: abis['ink_v2_flipper']['V2'] });
99
+ const latest = v2ToLatestCompatible(registry, contract.asV2);
100
+
101
+ it('has the correct constructor flag', (): void => {
102
+ expect(
103
+ latest.spec.constructors[0].payable.isTrue
104
+ ).toEqual(true);
105
+ });
106
+
107
+ it('has the latest compatible version number', (): void => {
108
+ expect(latest.version.toString()).toEqual('4');
109
+ });
110
+ });
111
+
112
+ describe('v3ToLatestCompatible', (): void => {
113
+ const registry = new TypeRegistry();
114
+ const contract = registry.createType('ContractMetadata', { V3: abis['ink_v3_flipper']['V3'] });
115
+ const latest = v3ToLatestCompatible(registry, contract.asV3);
116
+
117
+ it('has the correct constructor flags', (): void => {
118
+ expect(
119
+ latest.spec.constructors[0].payable.isTrue
120
+ ).toEqual(false);
121
+ expect(
122
+ latest.spec.constructors[1].payable.isTrue
123
+ ).toEqual(true);
124
+ });
125
+
126
+ it('has the correct messages', (): void => {
127
+ const contract = registry.createType('ContractMetadata', { V3: abis['ink_v3_traitErc20']['V3'] });
128
+ const latest = v3ToLatestCompatible(registry, contract.asV3);
129
+
130
+ expect(
131
+ latest.spec.messages.map(({ label }) => label.toString())
132
+ ).toEqual([
133
+ 'BaseErc20::total_supply', 'BaseErc20::balance_of', 'BaseErc20::allowance', 'BaseErc20::transfer', 'BaseErc20::approve', 'BaseErc20::transfer_from'
134
+ ]);
135
+ });
136
+
137
+ it('has the latest compatible version number', (): void => {
138
+ expect(latest.version.toString()).toEqual('4');
139
+ });
140
+ });
141
+
142
+ describe('v4ToLatestCompatible', (): void => {
143
+ const registry = new TypeRegistry();
144
+ const contract = registry.createType('ContractMetadata', { V4: abis['ink_v4_flipperContract'] });
145
+ const latest = v4ToLatestCompatible(registry, contract.asV4);
146
+
147
+ it('has the correct constructor flags', (): void => {
148
+ expect(
149
+ latest.spec.constructors[0].payable.isTrue
150
+ ).toEqual(false);
151
+ expect(
152
+ latest.spec.constructors[1].payable.isTrue
153
+ ).toEqual(false);
154
+ });
155
+
156
+ it('has the latest compatible version number', (): void => {
157
+ expect(latest.version.toString()).toEqual('4');
158
+ });
159
+ });
160
+
161
+ describe('v5ToLatestCompatible', (): void => {
162
+ const registry = new TypeRegistry();
163
+ const contract = registry.createType('ContractMetadata', { V5: abis['ink_v5_erc20Metadata'] });
164
+ const latest = v5ToLatestCompatible(registry, contract.asV5);
165
+
166
+ it('has the correct messages', (): void => {
167
+ expect(
168
+ latest.spec.messages.map(({ label }) => label.toString())
169
+ ).toEqual(['total_supply', 'balance_of', 'allowance', 'transfer', 'approve', 'transfer_from']);
170
+ });
171
+
172
+ it('has new event fields', (): void => {
173
+ expect(
174
+ latest.spec.events.length
175
+ ).toEqual(2);
176
+
177
+ expect(
178
+ latest.spec.events.every((e) => e.has('module_path'))
179
+ ).toEqual(true);
180
+
181
+ expect(latest.spec.events[0].module_path.toString()).toEqual('erc20::erc20');
182
+
183
+ expect(
184
+ latest.spec.events.every((e) => e.has('signature_topic'))
185
+ ).toEqual(true);
186
+
187
+ expect(latest.spec.events[0].signature_topic.toHex()).toEqual('0xb5b61a3e6a21a16be4f044b517c28ac692492f73c5bfd3f60178ad98c767f4cb');
188
+ });
189
+
190
+ it('has the latest compatible version number', (): void => {
191
+ expect(latest.version.toString()).toEqual('5');
192
+ });
193
+ });
194
+
195
+ describe('v6ToLatestCompatible', (): void => {
196
+ const registry = new TypeRegistry();
197
+ const contract = registry.createType('ContractMetadata', { V6: abis['ink_v6_erc20Metadata'] });
198
+ const latest = v6ToLatestCompatible(registry, contract.asV6);
199
+
200
+ it('has the correct messages', (): void => {
201
+ expect(
202
+ latest.spec.messages.map(({ label }) => label.toString())
203
+ ).toEqual(['total_supply', 'balance_of', 'allowance', 'transfer', 'approve', 'transfer_from']);
204
+ });
205
+
206
+ it('has H160 as the type of balance_of argument', (): void => {
207
+ const arg = latest.spec.messages.find(
208
+ (m) => m.label.toString() === 'balance_of'
209
+ )?.args[0];
210
+
211
+ const name = arg?.type.displayName?.[0]?.toString();
212
+
213
+ expect(name).toBe('H160');
214
+ });
215
+
216
+ it('has the latest compatible version number', (): void => {
217
+ expect(latest.version.toString()).toEqual('6');
218
+ });
219
+ });