@pezkuwi/api-contract 16.5.5 → 16.5.6

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 (234) hide show
  1. package/Abi/index.js +359 -0
  2. package/Abi/toLatestCompatible.js +30 -0
  3. package/Abi/toV1.js +21 -0
  4. package/Abi/toV2.js +27 -0
  5. package/Abi/toV3.js +10 -0
  6. package/Abi/toV4.js +10 -0
  7. package/LICENSE +201 -0
  8. package/README.md +1 -1
  9. package/augment.js +1 -0
  10. package/base/Base.js +40 -0
  11. package/base/Blueprint.js +51 -0
  12. package/base/Code.js +74 -0
  13. package/base/Contract.js +127 -0
  14. package/{src/base/index.ts → base/index.js} +0 -3
  15. package/base/mock.js +38 -0
  16. package/{build/base → base}/util.d.ts +1 -1
  17. package/base/util.js +33 -0
  18. package/bundle-pezkuwi-api-contract.js +1238 -0
  19. package/{src/bundle.ts → bundle.js} +0 -6
  20. package/cjs/Abi/index.d.ts +31 -0
  21. package/cjs/Abi/index.js +363 -0
  22. package/cjs/Abi/toLatestCompatible.d.ts +15 -0
  23. package/cjs/Abi/toLatestCompatible.js +36 -0
  24. package/cjs/Abi/toV1.d.ts +3 -0
  25. package/cjs/Abi/toV1.js +24 -0
  26. package/cjs/Abi/toV2.d.ts +3 -0
  27. package/cjs/Abi/toV2.js +30 -0
  28. package/cjs/Abi/toV3.d.ts +3 -0
  29. package/cjs/Abi/toV3.js +13 -0
  30. package/cjs/Abi/toV4.d.ts +3 -0
  31. package/cjs/Abi/toV4.js +13 -0
  32. package/cjs/augment.d.ts +1 -0
  33. package/cjs/augment.js +3 -0
  34. package/cjs/base/Base.d.ts +13 -0
  35. package/cjs/base/Base.js +44 -0
  36. package/cjs/base/Blueprint.d.ts +24 -0
  37. package/cjs/base/Blueprint.js +57 -0
  38. package/cjs/base/Code.d.ts +22 -0
  39. package/cjs/base/Code.js +80 -0
  40. package/cjs/base/Contract.d.ts +25 -0
  41. package/cjs/base/Contract.js +133 -0
  42. package/cjs/base/index.d.ts +3 -0
  43. package/cjs/base/index.js +14 -0
  44. package/cjs/base/mock.d.ts +3 -0
  45. package/cjs/base/mock.js +41 -0
  46. package/{src/base/types.ts → cjs/base/types.d.ts} +6 -21
  47. package/cjs/base/types.js +2 -0
  48. package/cjs/base/util.d.ts +16 -0
  49. package/cjs/base/util.js +42 -0
  50. package/cjs/bundle.d.ts +4 -0
  51. package/cjs/bundle.js +10 -0
  52. package/cjs/index.js +5 -0
  53. package/cjs/package.json +3 -0
  54. package/cjs/packageDetect.d.ts +1 -0
  55. package/cjs/packageDetect.js +7 -0
  56. package/cjs/packageInfo.js +4 -0
  57. package/cjs/promise/index.js +23 -0
  58. package/cjs/promise/types.js +2 -0
  59. package/cjs/rx/index.js +23 -0
  60. package/cjs/rx/types.js +2 -0
  61. package/cjs/types.js +2 -0
  62. package/cjs/util.js +13 -0
  63. package/index.d.ts +2 -0
  64. package/index.js +2 -0
  65. package/package.json +384 -15
  66. package/packageDetect.d.ts +1 -0
  67. package/{src/packageDetect.ts → packageDetect.js} +0 -8
  68. package/packageInfo.d.ts +6 -0
  69. package/packageInfo.js +1 -0
  70. package/promise/index.d.ts +13 -0
  71. package/promise/index.js +17 -0
  72. package/{src/promise/types.ts → promise/types.d.ts} +0 -4
  73. package/promise/types.js +1 -0
  74. package/rx/index.d.ts +13 -0
  75. package/rx/index.js +17 -0
  76. package/{src/rx/types.ts → rx/types.d.ts} +0 -4
  77. package/rx/types.js +1 -0
  78. package/types.d.ts +79 -0
  79. package/types.js +1 -0
  80. package/util.d.ts +5 -0
  81. package/util.js +10 -0
  82. package/src/Abi/Abi.spec.ts +0 -235
  83. package/src/Abi/index.ts +0 -477
  84. package/src/Abi/toLatestCompatible.spec.ts +0 -219
  85. package/src/Abi/toLatestCompatible.ts +0 -52
  86. package/src/Abi/toV1.ts +0 -35
  87. package/src/Abi/toV2.ts +0 -58
  88. package/src/Abi/toV3.ts +0 -18
  89. package/src/Abi/toV4.ts +0 -21
  90. package/src/augment.ts +0 -4
  91. package/src/base/Base.ts +0 -52
  92. package/src/base/Blueprint.ts +0 -90
  93. package/src/base/Code.spec.ts +0 -47
  94. package/src/base/Code.ts +0 -142
  95. package/src/base/Contract.ts +0 -197
  96. package/src/base/mock.ts +0 -48
  97. package/src/base/util.ts +0 -56
  98. package/src/checkTypes.manual.ts +0 -45
  99. package/src/index.ts +0 -6
  100. package/src/mod.ts +0 -4
  101. package/src/packageInfo.ts +0 -6
  102. package/src/promise/index.ts +0 -28
  103. package/src/rx/index.ts +0 -28
  104. package/src/test/compare/ink_v0_delegator.test.json +0 -47
  105. package/src/test/compare/ink_v0_dns.test.json +0 -232
  106. package/src/test/compare/ink_v0_erc20.test.json +0 -253
  107. package/src/test/compare/ink_v0_erc721.test.json +0 -415
  108. package/src/test/compare/ink_v0_flipper.test.json +0 -9
  109. package/src/test/compare/ink_v0_flipperBundle.test.json +0 -9
  110. package/src/test/compare/ink_v0_incrementer.test.json +0 -9
  111. package/src/test/compare/ink_v0_multisigPlain.test.json +0 -562
  112. package/src/test/compare/ink_v1_flipper.test.json +0 -9
  113. package/src/test/compare/ink_v1_psp22.test.json +0 -531
  114. package/src/test/compare/ink_v2_erc20.test.json +0 -205
  115. package/src/test/compare/ink_v2_flipper.test.json +0 -9
  116. package/src/test/compare/ink_v3_flipper.test.json +0 -9
  117. package/src/test/compare/ink_v3_traitErc20.test.json +0 -205
  118. package/src/test/compare/ink_v4_erc20Contract.test.json +0 -253
  119. package/src/test/compare/ink_v4_erc20Metadata.test.json +0 -253
  120. package/src/test/compare/ink_v4_flipperContract.test.json +0 -155
  121. package/src/test/compare/ink_v4_flipperMetadata.test.json +0 -155
  122. package/src/test/compare/ink_v5_erc20.test.json +0 -370
  123. package/src/test/compare/ink_v5_erc20AnonymousTransferMetadata.test.json +0 -370
  124. package/src/test/compare/ink_v5_erc20Contract.test.json +0 -370
  125. package/src/test/compare/ink_v5_erc20Metadata.test.json +0 -370
  126. package/src/test/compare/ink_v5_flipperContract.test.json +0 -174
  127. package/src/test/compare/ink_v5_flipperMetadata.test.json +0 -174
  128. package/src/test/compare/ink_v6_erc20Contract.test.json +0 -418
  129. package/src/test/compare/ink_v6_erc20Metadata.test.json +0 -418
  130. package/src/test/compare/solang_v0_ints256.test.json +0 -9
  131. package/src/test/compare/user_v0_assetTransfer.test.json +0 -54
  132. package/src/test/compare/user_v0_enumExample.test.json +0 -303
  133. package/src/test/compare/user_v0_recursive.test.json +0 -27
  134. package/src/test/compare/user_v0_withString.test.json +0 -260
  135. package/src/test/compare/user_v3_ask.test.json +0 -71
  136. package/src/test/compare/user_v4_events.test.json +0 -1328
  137. package/src/test/contracts/index.ts +0 -20
  138. package/src/test/contracts/ink/index.ts +0 -13
  139. package/src/test/contracts/ink/v0/accumulator.wasm +0 -0
  140. package/src/test/contracts/ink/v0/adder.wasm +0 -0
  141. package/src/test/contracts/ink/v0/delegator.json +0 -252
  142. package/src/test/contracts/ink/v0/delegator.wasm +0 -0
  143. package/src/test/contracts/ink/v0/dns.json +0 -713
  144. package/src/test/contracts/ink/v0/dns.wasm +0 -0
  145. package/src/test/contracts/ink/v0/erc20.json +0 -704
  146. package/src/test/contracts/ink/v0/erc20.wasm +0 -0
  147. package/src/test/contracts/ink/v0/erc721.json +0 -1197
  148. package/src/test/contracts/ink/v0/erc721.wasm +0 -0
  149. package/src/test/contracts/ink/v0/flipper.contract.json +0 -107
  150. package/src/test/contracts/ink/v0/flipper.json +0 -106
  151. package/src/test/contracts/ink/v0/flipper.wasm +0 -0
  152. package/src/test/contracts/ink/v0/incrementer.json +0 -108
  153. package/src/test/contracts/ink/v0/incrementer.wasm +0 -0
  154. package/src/test/contracts/ink/v0/index.ts +0 -11
  155. package/src/test/contracts/ink/v0/multisig_plain.json +0 -1466
  156. package/src/test/contracts/ink/v0/multisig_plain.wasm +0 -0
  157. package/src/test/contracts/ink/v0/subber.wasm +0 -0
  158. package/src/test/contracts/ink/v0/trait-flipper.json +0 -103
  159. package/src/test/contracts/ink/v0/trait-flipper.wasm +0 -0
  160. package/src/test/contracts/ink/v1/flipper.contract.json +0 -111
  161. package/src/test/contracts/ink/v1/index.ts +0 -6
  162. package/src/test/contracts/ink/v1/psp22_minter_pauser.contract.json +0 -1722
  163. package/src/test/contracts/ink/v2/erc20.contract.json +0 -630
  164. package/src/test/contracts/ink/v2/flipper.contract.json +0 -103
  165. package/src/test/contracts/ink/v2/index.ts +0 -5
  166. package/src/test/contracts/ink/v3/flipper.contract.json +0 -105
  167. package/src/test/contracts/ink/v3/index.ts +0 -6
  168. package/src/test/contracts/ink/v3/trait_erc20.contract.json +0 -631
  169. package/src/test/contracts/ink/v4/erc20.contract.json +0 -1
  170. package/src/test/contracts/ink/v4/erc20.json +0 -821
  171. package/src/test/contracts/ink/v4/erc20.wasm +0 -0
  172. package/src/test/contracts/ink/v4/flipper.contract.json +0 -1
  173. package/src/test/contracts/ink/v4/flipper.json +0 -396
  174. package/src/test/contracts/ink/v4/flipper.wasm +0 -0
  175. package/src/test/contracts/ink/v4/index.ts +0 -7
  176. package/src/test/contracts/ink/v5/erc20.contract.json +0 -1
  177. package/src/test/contracts/ink/v5/erc20.json +0 -1025
  178. package/src/test/contracts/ink/v5/erc20.wasm +0 -0
  179. package/src/test/contracts/ink/v5/erc20_anonymous_transfer.json +0 -1025
  180. package/src/test/contracts/ink/v5/flipper.contract.json +0 -1
  181. package/src/test/contracts/ink/v5/flipper.json +0 -420
  182. package/src/test/contracts/ink/v5/flipper.wasm +0 -0
  183. package/src/test/contracts/ink/v5/index.ts +0 -8
  184. package/src/test/contracts/ink/v6/erc20.contract.json +0 -1
  185. package/src/test/contracts/ink/v6/erc20.json +0 -1081
  186. package/src/test/contracts/ink/v6/erc20.polkavm +0 -0
  187. package/src/test/contracts/ink/v6/index.ts +0 -5
  188. package/src/test/contracts/solang/index.ts +0 -7
  189. package/src/test/contracts/solang/v0/index.ts +0 -4
  190. package/src/test/contracts/solang/v0/ints256.json +0 -113
  191. package/src/test/contracts/solang/v0/ints256.sol +0 -13
  192. package/src/test/contracts/solang/v0/ints256.wasm +0 -0
  193. package/src/test/contracts/user/index.ts +0 -9
  194. package/src/test/contracts/user/v0/assetTransfer.json +0 -299
  195. package/src/test/contracts/user/v0/assetTransfer.wasm +0 -0
  196. package/src/test/contracts/user/v0/enumExample.json +0 -528
  197. package/src/test/contracts/user/v0/enumExample.wasm +0 -0
  198. package/src/test/contracts/user/v0/index.ts +0 -7
  199. package/src/test/contracts/user/v0/recursive.contract.json +0 -1
  200. package/src/test/contracts/user/v0/withString.json +0 -777
  201. package/src/test/contracts/user/v3/ask.json +0 -550
  202. package/src/test/contracts/user/v3/index.ts +0 -4
  203. package/src/test/contracts/user/v4/events.contract.json +0 -2990
  204. package/src/test/contracts/user/v4/index.ts +0 -4
  205. package/src/test/contracts/util.ts +0 -14
  206. package/src/types.ts +0 -98
  207. package/src/util.ts +0 -20
  208. package/tsconfig.build.json +0 -22
  209. package/tsconfig.build.tsbuildinfo +0 -1
  210. package/tsconfig.spec.json +0 -26
  211. /package/{build/Abi → Abi}/index.d.ts +0 -0
  212. /package/{build/Abi → Abi}/toLatestCompatible.d.ts +0 -0
  213. /package/{build/Abi → Abi}/toV1.d.ts +0 -0
  214. /package/{build/Abi → Abi}/toV2.d.ts +0 -0
  215. /package/{build/Abi → Abi}/toV3.d.ts +0 -0
  216. /package/{build/Abi → Abi}/toV4.d.ts +0 -0
  217. /package/{build/augment.d.ts → augment.d.ts} +0 -0
  218. /package/{build/base → base}/Base.d.ts +0 -0
  219. /package/{build/base → base}/Blueprint.d.ts +0 -0
  220. /package/{build/base → base}/Code.d.ts +0 -0
  221. /package/{build/base → base}/Contract.d.ts +0 -0
  222. /package/{build/base → base}/index.d.ts +0 -0
  223. /package/{build/base → base}/mock.d.ts +0 -0
  224. /package/{build/base → base}/types.d.ts +0 -0
  225. /package/{build/packageDetect.d.ts → base/types.js} +0 -0
  226. /package/{build/bundle.d.ts → bundle.d.ts} +0 -0
  227. /package/{build → cjs}/index.d.ts +0 -0
  228. /package/{build → cjs}/packageInfo.d.ts +0 -0
  229. /package/{build → cjs}/promise/index.d.ts +0 -0
  230. /package/{build → cjs}/promise/types.d.ts +0 -0
  231. /package/{build → cjs}/rx/index.d.ts +0 -0
  232. /package/{build → cjs}/rx/types.d.ts +0 -0
  233. /package/{build → cjs}/types.d.ts +0 -0
  234. /package/{build → cjs}/util.d.ts +0 -0
@@ -1,235 +0,0 @@
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 type { Registry } from '@pezkuwi/types/types';
7
-
8
- import fs from 'node:fs';
9
- import process from 'node:process';
10
-
11
- import { TypeDefInfo } from '@pezkuwi/types/types';
12
- import rpcMetadata from '@pezkuwi/types-support/metadata/static-substrate-contracts-node';
13
- import { blake2AsHex } from '@pezkuwi/util-crypto';
14
-
15
- import { Metadata, TypeRegistry } from '../../../types/src/bundle.js';
16
- import abis from '../test/contracts/index.js';
17
- import { Abi } from './index.js';
18
-
19
- interface SpecDef {
20
- messages: {
21
- label: string;
22
- name: string[] | string
23
- }[]
24
- }
25
-
26
- interface JSONAbi {
27
- source: {
28
- compiler: string,
29
- hash: string,
30
- language: string,
31
- wasm: string
32
- },
33
- spec: SpecDef;
34
- V1: {
35
- spec: SpecDef;
36
- },
37
- V2: {
38
- spec: SpecDef;
39
- },
40
- V3: {
41
- spec: SpecDef;
42
- },
43
- V4: {
44
- spec: SpecDef;
45
- }
46
- }
47
-
48
- function stringifyInfo (key: string, value: unknown): unknown {
49
- return key === 'info' && typeof value === 'number'
50
- ? TypeDefInfo[value]
51
- : value;
52
- }
53
-
54
- function stringifyJson (registry: Registry): string {
55
- const defs = registry.lookup.types.map(({ id }) =>
56
- registry.lookup.getTypeDef(id)
57
- );
58
-
59
- return JSON.stringify(defs, stringifyInfo, 2);
60
- }
61
-
62
- describe('Abi', (): void => {
63
- describe('ABI', (): void => {
64
- Object.entries(abis).forEach(([abiName, _abi]) => {
65
- const abi = _abi as unknown as JSONAbi;
66
-
67
- it(`initializes from a contract ABI (${abiName})`, (): void => {
68
- try {
69
- const messageIds = (abi.V4 || abi.V3 || abi.V2 || abi.V1 || abi).spec.messages.map(({ label, name }) =>
70
- label || (
71
- Array.isArray(name)
72
- ? name.join('::')
73
- : name
74
- )
75
- );
76
- const inkAbi = new Abi(abis[abiName]);
77
-
78
- expect(inkAbi.messages.map(({ identifier }) => identifier)).toEqual(messageIds);
79
- } catch (error) {
80
- console.error(error);
81
-
82
- throw error;
83
- }
84
- });
85
- });
86
- });
87
-
88
- describe('TypeDef', (): void => {
89
- for (const [abiName, abiJson] of Object.entries(abis)) {
90
- it(`initializes from a contract ABI: ${abiName}`, (): void => {
91
- const abi = new Abi(abiJson);
92
- const registryJson = stringifyJson(abi.registry);
93
- const cmpFile = new URL(`../test/compare/${abiName}.test.json`, import.meta.url);
94
-
95
- try {
96
- expect(
97
- JSON.parse(registryJson)
98
- ).toEqual(
99
- JSON.parse(fs.readFileSync(cmpFile, 'utf-8'))
100
- );
101
- } catch (error) {
102
- if (process.env['GITHUB_REPOSITORY']) {
103
- console.error(registryJson);
104
-
105
- throw error;
106
- }
107
-
108
- fs.writeFileSync(cmpFile, registryJson, { flag: 'w' });
109
- }
110
- });
111
- }
112
- });
113
-
114
- it('has the correct hash for the source', (): void => {
115
- const abi = new Abi(abis['ink_v0_flipperBundle']);
116
- const bundle = abis['ink_v0_flipperBundle'] as unknown as JSONAbi;
117
-
118
- // manual
119
- expect(bundle.source.hash).toEqual(blake2AsHex(bundle.source.wasm));
120
-
121
- // the Codec hash
122
- expect(bundle.source.hash).toEqual(abi.info.source.wasm.hash.toHex());
123
-
124
- // the hash as per the actual Abi
125
- expect(bundle.source.hash).toEqual(abi.info.source.wasmHash.toHex());
126
- });
127
-
128
- describe('Events', (): void => {
129
- const registry = new TypeRegistry();
130
-
131
- beforeAll((): void => {
132
- const metadata = new Metadata(registry, rpcMetadata);
133
-
134
- registry.setMetadata(metadata);
135
- });
136
-
137
- it('decoding <=ink!v4 event', (): void => {
138
- const abiJson = abis['ink_v4_erc20Metadata'];
139
-
140
- expect(abiJson).toBeDefined();
141
- const abi = new Abi(abiJson);
142
-
143
- const eventRecordHex =
144
- '0x0001000000080360951b8baf569bca905a279c12d6ce17db7cdce23a42563870ef585129ce5dc64d010001d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d018eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a4800505a4f7e9f4eb106000000000000000c0045726332303a3a5472616e7366657200000000000000000000000000000000da2d695d3b5a304e0039e7fc4419c34fa0c1f239189c99bb72a6484f1634782b2b00c7d40fe6d84d660f3e6bed90f218e022a0909f7e1a7ea35ada8b6e003564';
145
- const record = registry.createType('EventRecord', eventRecordHex);
146
-
147
- const decodedEvent = abi.decodeEvent(record);
148
-
149
- expect(decodedEvent.event.args.length).toEqual(3);
150
- expect(decodedEvent.args.length).toEqual(3);
151
- expect(decodedEvent.event.identifier).toEqual('Transfer');
152
-
153
- const decodedEventHuman = decodedEvent.event.args.reduce((prev, cur, index) => {
154
- return {
155
- ...prev,
156
- [cur.name]: decodedEvent.args[index].toHuman()
157
- };
158
- }, {});
159
-
160
- const expectedEvent = {
161
- from: '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY',
162
- to: '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty',
163
- value: '123.4567 MUnit'
164
- };
165
-
166
- expect(decodedEventHuman).toEqual(expectedEvent);
167
- });
168
-
169
- it('decoding >=ink!v5 event', (): void => {
170
- const abiJson = abis['ink_v5_erc20Metadata'];
171
-
172
- expect(abiJson).toBeDefined();
173
- const abi = new Abi(abiJson);
174
-
175
- const eventRecordHex =
176
- '0x00010000000803da17150e96b3955a4db6ad35ddeb495f722f9c1d84683113bfb096bf3faa30f2490101d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d018eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a4800505a4f7e9f4eb106000000000000000cb5b61a3e6a21a16be4f044b517c28ac692492f73c5bfd3f60178ad98c767f4cbd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48';
177
- const record = registry.createType('EventRecord', eventRecordHex);
178
-
179
- const decodedEvent = abi.decodeEvent(record);
180
-
181
- expect(decodedEvent.event.args.length).toEqual(3);
182
- expect(decodedEvent.args.length).toEqual(3);
183
- expect(decodedEvent.event.identifier).toEqual('erc20::erc20::Transfer');
184
-
185
- const decodedEventHuman = decodedEvent.event.args.reduce((prev, cur, index) => {
186
- return {
187
- ...prev,
188
- [cur.name]: decodedEvent.args[index].toHuman()
189
- };
190
- }, {});
191
-
192
- const expectedEvent = {
193
- from: '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY',
194
- to: '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty',
195
- value: '123.4567 MUnit'
196
- };
197
-
198
- expect(decodedEventHuman).toEqual(expectedEvent);
199
- });
200
-
201
- it('decoding >=ink!v5 anonymous event', (): void => {
202
- const abiJson = abis['ink_v5_erc20AnonymousTransferMetadata'];
203
-
204
- expect(abiJson).toBeDefined();
205
- const abi = new Abi(abiJson);
206
-
207
- expect(abi.events[0].identifier).toEqual('erc20::erc20::Transfer');
208
- expect(abi.events[0].signatureTopic).toEqual(null);
209
-
210
- const eventRecordWithAnonymousEventHex = '0x00010000000803538e726248a9c155911e7d99f4f474c3408630a2f6275dd501d4471c7067ad2c490101d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d018eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a4800505a4f7e9f4eb1060000000000000008d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48';
211
- const record = registry.createType('EventRecord', eventRecordWithAnonymousEventHex);
212
-
213
- const decodedEvent = abi.decodeEvent(record);
214
-
215
- expect(decodedEvent.event.args.length).toEqual(3);
216
- expect(decodedEvent.args.length).toEqual(3);
217
- expect(decodedEvent.event.identifier).toEqual('erc20::erc20::Transfer');
218
-
219
- const decodedEventHuman = decodedEvent.event.args.reduce((prev, cur, index) => {
220
- return {
221
- ...prev,
222
- [cur.name]: decodedEvent.args[index].toHuman()
223
- };
224
- }, {});
225
-
226
- const expectedEvent = {
227
- from: '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY',
228
- to: '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty',
229
- value: '123.4567 MUnit'
230
- };
231
-
232
- expect(decodedEventHuman).toEqual(expectedEvent);
233
- });
234
- });
235
- });
package/src/Abi/index.ts DELETED
@@ -1,477 +0,0 @@
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
- }