@sentio/sdk 2.0.0-rc.14 → 2.0.0-rc.16

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 (199) hide show
  1. package/lib/aptos/api.d.ts +5 -0
  2. package/lib/aptos/api.js +21 -0
  3. package/lib/aptos/api.js.map +1 -0
  4. package/lib/aptos/aptos-plugin.d.ts +14 -0
  5. package/lib/aptos/aptos-plugin.js +146 -0
  6. package/lib/aptos/aptos-plugin.js.map +1 -0
  7. package/lib/aptos/aptos-processor.d.ts +72 -0
  8. package/lib/aptos/aptos-processor.js +201 -0
  9. package/lib/aptos/aptos-processor.js.map +1 -0
  10. package/lib/aptos/builtin/0x1.d.ts +2140 -0
  11. package/lib/aptos/builtin/0x1.js +2727 -0
  12. package/lib/aptos/builtin/0x1.js.map +1 -0
  13. package/lib/aptos/builtin/0x3.d.ts +573 -0
  14. package/lib/aptos/builtin/0x3.js +496 -0
  15. package/lib/aptos/builtin/0x3.js.map +1 -0
  16. package/lib/aptos/builtin/index.d.ts +2 -0
  17. package/lib/aptos/builtin/index.js +6 -0
  18. package/lib/aptos/builtin/index.js.map +1 -0
  19. package/lib/aptos/codegen/codegen.d.ts +23 -0
  20. package/lib/aptos/codegen/codegen.js +388 -0
  21. package/lib/aptos/codegen/codegen.js.map +1 -0
  22. package/lib/aptos/codegen/index.d.ts +1 -0
  23. package/lib/aptos/codegen/index.js +2 -0
  24. package/lib/aptos/codegen/index.js.map +1 -0
  25. package/lib/aptos/codegen/typegen.d.ts +18 -0
  26. package/lib/aptos/codegen/typegen.js +145 -0
  27. package/lib/aptos/codegen/typegen.js.map +1 -0
  28. package/lib/aptos/codegen/typegen.test.d.ts +1 -0
  29. package/lib/aptos/codegen/typegen.test.js.map +1 -0
  30. package/lib/aptos/context.d.ts +21 -0
  31. package/lib/aptos/context.js +59 -0
  32. package/lib/aptos/context.js.map +1 -0
  33. package/lib/aptos/index.d.ts +9 -0
  34. package/lib/aptos/index.js +8 -0
  35. package/lib/aptos/index.js.map +1 -0
  36. package/lib/aptos/models.d.ts +37 -0
  37. package/lib/aptos/models.js +2 -0
  38. package/lib/aptos/models.js.map +1 -0
  39. package/lib/aptos/move-coder.d.ts +22 -0
  40. package/lib/aptos/move-coder.js +164 -0
  41. package/lib/aptos/move-coder.js.map +1 -0
  42. package/lib/aptos/move-types.d.ts +10 -0
  43. package/lib/aptos/move-types.js +2 -0
  44. package/lib/aptos/move-types.js.map +1 -0
  45. package/lib/aptos/network.d.ts +11 -0
  46. package/lib/aptos/network.js +27 -0
  47. package/lib/aptos/network.js.map +1 -0
  48. package/lib/aptos/run-codegen.d.ts +1 -0
  49. package/lib/aptos/run-codegen.js +12 -0
  50. package/lib/aptos/run-codegen.js.map +1 -0
  51. package/lib/aptos/tests/aptos.test.d.ts +1 -0
  52. package/lib/aptos/tests/aptos.test.js.map +1 -0
  53. package/lib/aptos/tests/souffl3.d.ts +1 -0
  54. package/lib/aptos/tests/souffl3.js +44 -0
  55. package/lib/aptos/tests/souffl3.js.map +1 -0
  56. package/lib/aptos/tests/types/index.d.ts +3 -0
  57. package/lib/aptos/tests/types/index.js +7 -0
  58. package/lib/aptos/tests/types/index.js.map +1 -0
  59. package/lib/aptos/tests/types/reserved.d.ts +445 -0
  60. package/lib/aptos/tests/types/reserved.js +301 -0
  61. package/lib/aptos/tests/types/reserved.js.map +1 -0
  62. package/lib/aptos/tests/types/soffl3.d.ts +1065 -0
  63. package/lib/aptos/tests/types/soffl3.js +548 -0
  64. package/lib/aptos/tests/types/soffl3.js.map +1 -0
  65. package/lib/aptos/tests/types/souffle.d.ts +440 -0
  66. package/lib/aptos/tests/types/souffle.js +295 -0
  67. package/lib/aptos/tests/types/souffle.js.map +1 -0
  68. package/lib/aptos/types.d.ts +9 -0
  69. package/lib/aptos/types.js +130 -0
  70. package/lib/aptos/types.js.map +1 -0
  71. package/lib/aptos/types.test.d.ts +1 -0
  72. package/lib/aptos/types.test.js.map +1 -0
  73. package/lib/aptos/utils.d.ts +7 -0
  74. package/lib/aptos/utils.js +20 -0
  75. package/lib/aptos/utils.js.map +1 -0
  76. package/lib/{utils/chain.d.ts → chain.d.ts} +0 -0
  77. package/lib/{utils/chain.js → chain.js} +0 -0
  78. package/lib/chain.js.map +1 -0
  79. package/lib/core/context.js +1 -1
  80. package/lib/core/context.js.map +1 -1
  81. package/lib/core/sui-plugin.js +1 -1
  82. package/lib/core/sui-plugin.js.map +1 -1
  83. package/lib/index.d.ts +1 -1
  84. package/lib/index.js +1 -1
  85. package/lib/index.js.map +1 -1
  86. package/lib/solana/builtin/index.d.ts +1 -0
  87. package/lib/solana/builtin/index.js +2 -0
  88. package/lib/solana/builtin/index.js.map +1 -0
  89. package/lib/solana/builtin/spl-token-processor.d.ts +39 -0
  90. package/lib/solana/builtin/spl-token-processor.js +254 -0
  91. package/lib/solana/builtin/spl-token-processor.js.map +1 -0
  92. package/lib/solana/builtin/types.d.ts +427 -0
  93. package/lib/solana/builtin/types.js +200 -0
  94. package/lib/solana/builtin/types.js.map +1 -0
  95. package/lib/solana/codegen/codegen.d.ts +1 -0
  96. package/lib/solana/codegen/codegen.js +122 -0
  97. package/lib/solana/codegen/codegen.js.map +1 -0
  98. package/lib/solana/codegen/index.d.ts +1 -0
  99. package/lib/solana/codegen/index.js +2 -0
  100. package/lib/solana/codegen/index.js.map +1 -0
  101. package/lib/solana/index.d.ts +4 -0
  102. package/lib/solana/index.js +5 -0
  103. package/lib/solana/index.js.map +1 -0
  104. package/lib/solana/run-codegen.d.ts +2 -0
  105. package/lib/solana/run-codegen.js +12 -0
  106. package/lib/solana/run-codegen.js.map +1 -0
  107. package/lib/solana/solana-context.d.ts +9 -0
  108. package/lib/solana/solana-context.js +28 -0
  109. package/lib/solana/solana-context.js.map +1 -0
  110. package/lib/solana/solana-options.d.ts +10 -0
  111. package/lib/solana/solana-options.js +10 -0
  112. package/lib/solana/solana-options.js.map +1 -0
  113. package/lib/solana/solana-plugin.d.ts +9 -0
  114. package/lib/solana/solana-plugin.js +87 -0
  115. package/lib/solana/solana-plugin.js.map +1 -0
  116. package/lib/solana/solana-processor.d.ts +43 -0
  117. package/lib/solana/solana-processor.js +72 -0
  118. package/lib/solana/solana-processor.js.map +1 -0
  119. package/lib/solana/tests/solana.test.d.ts +1 -0
  120. package/lib/solana/tests/solana.test.js.map +1 -0
  121. package/lib/solana/tests/types/basic_1.d.ts +26 -0
  122. package/lib/solana/tests/types/basic_1.js +63 -0
  123. package/lib/solana/tests/types/basic_1.js.map +1 -0
  124. package/lib/solana/tests/types/basic_1_processor.d.ts +21 -0
  125. package/lib/solana/tests/types/basic_1_processor.js +35 -0
  126. package/lib/solana/tests/types/basic_1_processor.js.map +1 -0
  127. package/lib/solana/tests/types/token_bridge.d.ts +29 -0
  128. package/lib/solana/tests/types/token_bridge.js +938 -0
  129. package/lib/solana/tests/types/token_bridge.js.map +1 -0
  130. package/lib/solana/tests/types/token_bridge_processor.d.ts +212 -0
  131. package/lib/solana/tests/types/token_bridge_processor.js +134 -0
  132. package/lib/solana/tests/types/token_bridge_processor.js.map +1 -0
  133. package/lib/solana/tests/wormhole-token-bridge.d.ts +15 -0
  134. package/lib/solana/tests/wormhole-token-bridge.js +79 -0
  135. package/lib/solana/tests/wormhole-token-bridge.js.map +1 -0
  136. package/lib/testing/test-processor-server.js +1 -1
  137. package/lib/testing/test-processor-server.js.map +1 -1
  138. package/lib/testing/test-provider.js +1 -1
  139. package/lib/testing/test-provider.js.map +1 -1
  140. package/lib/utils/index.d.ts +0 -1
  141. package/lib/utils/index.js +0 -1
  142. package/lib/utils/index.js.map +1 -1
  143. package/package.json +33 -11
  144. package/src/aptos/abis/0x1.json +9205 -0
  145. package/src/aptos/abis/0x3.json +1515 -0
  146. package/src/aptos/api.ts +23 -0
  147. package/src/aptos/aptos-plugin.ts +183 -0
  148. package/src/aptos/aptos-processor.ts +331 -0
  149. package/src/aptos/builtin/0x1.ts +4911 -0
  150. package/src/aptos/builtin/0x3.ts +1092 -0
  151. package/src/aptos/builtin/index.ts +5 -0
  152. package/src/aptos/codegen/codegen.ts +470 -0
  153. package/src/aptos/codegen/index.ts +1 -0
  154. package/src/aptos/codegen/tsconfig.json +8 -0
  155. package/src/aptos/codegen/typegen.ts +165 -0
  156. package/src/aptos/context.ts +72 -0
  157. package/src/aptos/index.ts +10 -0
  158. package/src/aptos/models.ts +47 -0
  159. package/src/aptos/move-coder.ts +201 -0
  160. package/src/aptos/move-types.ts +11 -0
  161. package/src/aptos/network.ts +29 -0
  162. package/src/aptos/run-codegen.ts +13 -0
  163. package/src/aptos/tests/abis/reserved.json +402 -0
  164. package/src/aptos/tests/abis/soffl3.json +1411 -0
  165. package/src/aptos/tests/abis/souffle.json +389 -0
  166. package/src/aptos/tests/souffl3.ts +57 -0
  167. package/src/aptos/tests/types/index.ts +6 -0
  168. package/src/aptos/tests/types/reserved.ts +881 -0
  169. package/src/aptos/tests/types/soffl3.ts +1820 -0
  170. package/src/aptos/tests/types/souffle.ts +880 -0
  171. package/src/aptos/types.ts +149 -0
  172. package/src/aptos/utils.ts +26 -0
  173. package/src/{utils/chain.ts → chain.ts} +0 -0
  174. package/src/core/context.ts +1 -1
  175. package/src/core/sui-plugin.ts +1 -1
  176. package/src/index.ts +1 -1
  177. package/src/solana/builtin/index.ts +1 -0
  178. package/src/solana/builtin/spl-token-processor.ts +298 -0
  179. package/src/solana/builtin/types.ts +279 -0
  180. package/src/solana/codegen/codegen.ts +140 -0
  181. package/src/solana/codegen/index.ts +1 -0
  182. package/src/solana/codegen/tsconfig.json +8 -0
  183. package/src/solana/index.ts +4 -0
  184. package/src/solana/run-codegen.ts +13 -0
  185. package/src/solana/solana-context.ts +30 -0
  186. package/src/solana/solana-options.ts +11 -0
  187. package/src/solana/solana-plugin.ts +103 -0
  188. package/src/solana/solana-processor.ts +102 -0
  189. package/src/solana/tests/abis/basic_1.json +62 -0
  190. package/src/solana/tests/abis/token_bridge.json +937 -0
  191. package/src/solana/tests/types/basic_1.ts +62 -0
  192. package/src/solana/tests/types/basic_1_processor.ts +42 -0
  193. package/src/solana/tests/types/token_bridge.ts +937 -0
  194. package/src/solana/tests/types/token_bridge_processor.ts +150 -0
  195. package/src/solana/tests/wormhole-token-bridge.ts +96 -0
  196. package/src/testing/test-processor-server.ts +1 -1
  197. package/src/testing/test-provider.ts +1 -1
  198. package/src/utils/index.ts +0 -1
  199. package/lib/utils/chain.js.map +0 -1
@@ -0,0 +1,5 @@
1
+ /* Autogenerated file. Do not edit manually. */
2
+ /* tslint:disable */
3
+ /* eslint-disable */
4
+ export * as _0x1 from './0x1.js'
5
+ export * as _0x3 from './0x3.js'
@@ -0,0 +1,470 @@
1
+ import * as fs from 'fs'
2
+ import * as path from 'path'
3
+ import { format } from 'prettier'
4
+ import { MoveFunction, MoveModule, MoveModuleBytecode, MoveStruct } from '../move-types.js'
5
+ import { AccountModulesImportInfo, AccountRegister, generateType, normalizeToJSName } from './typegen.js'
6
+ import { getMeaningfulFunctionParams, isFrameworkAccount, moduleQname, SPLITTER } from '../utils.js'
7
+ import chalk from 'chalk'
8
+ import { AptosNetwork, getAptosChainName } from '../network.js'
9
+ import { parseMoveType } from '../types.js'
10
+ import { AptosClient } from 'aptos-sdk'
11
+
12
+ function getRpcEndpoint(network: AptosNetwork): string {
13
+ switch (network) {
14
+ case AptosNetwork.TEST_NET:
15
+ return 'https://testnet.aptoslabs.com/'
16
+ }
17
+ return 'https://mainnet.aptoslabs.com/'
18
+ }
19
+
20
+ function getRpcClient(network: AptosNetwork): AptosClient {
21
+ return new AptosClient(getRpcEndpoint(network))
22
+ }
23
+
24
+ export async function codeGenAptosProcessor(abisDir: string, outDir = 'src/types/aptos') {
25
+ if (!fs.existsSync(abisDir)) {
26
+ return
27
+ }
28
+ console.log(chalk.green('Generated Types for Aptos'))
29
+ await generate(abisDir, outDir)
30
+ }
31
+
32
+ export async function generate(srcDir: string, outputDir: string) {
33
+ await generateForNetwork(srcDir, outputDir, AptosNetwork.MAIN_NET)
34
+ await generateForNetwork(path.join(srcDir, 'testnet'), path.join(outputDir, 'testnet'), AptosNetwork.TEST_NET)
35
+ }
36
+
37
+ export async function generateForNetwork(srcDir: string, outputDir: string, network: AptosNetwork) {
38
+ if (!fs.existsSync(srcDir)) {
39
+ return
40
+ }
41
+ if (network === AptosNetwork.TEST_NET) {
42
+ console.log('Found testnet directory, generate code for testnet modules')
43
+ }
44
+ const files = fs.readdirSync(srcDir)
45
+ outputDir = path.resolve(outputDir)
46
+ const outputs: OutputFile[] = []
47
+
48
+ fs.mkdirSync(outputDir, { recursive: true })
49
+
50
+ const loader = new AccountRegister()
51
+
52
+ // when generating user code, don't need to generate framework account
53
+ loader.accountImports.set('0x1', new AccountModulesImportInfo('0x1', '0x1'))
54
+ loader.accountImports.set('0x2', new AccountModulesImportInfo('0x2', '0x2'))
55
+ loader.accountImports.set('0x3', new AccountModulesImportInfo('0x3', '0x3'))
56
+ const client = getRpcClient(network)
57
+
58
+ for (const file of files) {
59
+ if (!file.endsWith('.json')) {
60
+ continue
61
+ }
62
+ const fullPath = path.resolve(srcDir, file)
63
+ const modules = JSON.parse(fs.readFileSync(fullPath, 'utf-8'))
64
+
65
+ for (const module of modules) {
66
+ if (module.abi) {
67
+ loader.register(module.abi, path.basename(file, '.json'))
68
+ }
69
+ }
70
+ const codeGen = new AccountCodegen(loader, modules, {
71
+ fileName: path.basename(file, '.json'),
72
+ outputDir: outputDir,
73
+ network,
74
+ })
75
+
76
+ outputs.push(...codeGen.generate())
77
+ }
78
+
79
+ while (loader.pendingAccounts.size > 0) {
80
+ for (const account of loader.pendingAccounts) {
81
+ console.log(`download dependent module for account ${account} at ${getAptosChainName(network)}`)
82
+
83
+ try {
84
+ const modules = await client.getAccountModules(account)
85
+ fs.writeFileSync(path.resolve(srcDir, account + '.json'), JSON.stringify(modules, null, '\t'))
86
+
87
+ for (const module of modules) {
88
+ if (module.abi) {
89
+ loader.register(module.abi, account)
90
+ }
91
+ }
92
+ const codeGen = new AccountCodegen(loader, modules, {
93
+ fileName: account,
94
+ outputDir: outputDir,
95
+ network,
96
+ })
97
+
98
+ outputs.push(...codeGen.generate())
99
+ } catch (e) {
100
+ console.error(
101
+ chalk.red(
102
+ 'Error downloading account module, check if you choose the right network,or download account modules manually into your director'
103
+ )
104
+ )
105
+ console.error(e)
106
+ process.exit(1)
107
+ }
108
+ }
109
+ }
110
+
111
+ for (const output of outputs) {
112
+ const content = format(output.fileContent, { parser: 'typescript' })
113
+ fs.writeFileSync(path.join(outputDir, output.fileName), content)
114
+ }
115
+
116
+ const rootFile = path.join(outputDir, 'index.ts')
117
+ fs.writeFileSync(
118
+ rootFile,
119
+ `/* Autogenerated file. Do not edit manually. */
120
+ /* tslint:disable */
121
+ /* eslint-disable */
122
+ `
123
+ )
124
+ for (const output of outputs) {
125
+ const parsed = path.parse(output.fileName)
126
+ const content = `export * as _${parsed.name} from './${parsed.name}.js'\n`
127
+ fs.appendFileSync(rootFile, content)
128
+ }
129
+ }
130
+
131
+ interface OutputFile {
132
+ fileName: string
133
+ fileContent: string
134
+ }
135
+
136
+ interface Config {
137
+ fileName: string
138
+ outputDir: string
139
+ network: AptosNetwork
140
+ }
141
+
142
+ export class AccountCodegen {
143
+ modules: MoveModuleBytecode[]
144
+ config: Config
145
+ loader: AccountRegister
146
+
147
+ constructor(loader: AccountRegister, modules: MoveModuleBytecode[], config: Config) {
148
+ // const json = fs.readFileSync(config.srcFile, 'utf-8')
149
+ this.modules = modules
150
+ this.config = config
151
+ this.loader = loader
152
+ }
153
+
154
+ generate(): OutputFile[] {
155
+ if (!this.modules) {
156
+ return []
157
+ }
158
+ // const baseName = path.basename(this.config.fileName, '.json')
159
+
160
+ let address: string | undefined
161
+ for (const module of this.modules) {
162
+ if (module.abi && module.abi.address) {
163
+ address = module.abi.address
164
+ }
165
+ }
166
+ if (!address) {
167
+ return []
168
+ }
169
+
170
+ const imports = `
171
+ import {
172
+ MoveCoder, AptosBindOptions, AptosBaseProcessor,
173
+ TypedEventInstance, AptosNetwork, TypedEntryFunctionPayload,
174
+ AptosContext, CallFilter
175
+ } from "@sentio/sdk/aptos"
176
+ import { AptosFetchConfig } from "@sentio/protos"
177
+ import { Address, MoveModule } from "@sentio/sdk/aptos"
178
+ `
179
+
180
+ const dependedAccounts: string[] = []
181
+
182
+ const moduleImports: string[] = []
183
+
184
+ const info = this.loader.accountImports.get(address)
185
+
186
+ if (info) {
187
+ for (const [account] of info.imports.entries()) {
188
+ // Remap to user's filename if possible, TODO codepath not well tested
189
+ const tsAccountModule = './' + (this.loader.accountImports.get(account)?.moduleName || account)
190
+ if (isFrameworkAccount(account) && !isFrameworkAccount(address)) {
191
+ // Decide where to find runtime library
192
+ moduleImports.push(`import { _${account} } from "@sentio/sdk/aptos/builtin"`)
193
+ } else {
194
+ moduleImports.push(`import * as _${account} from "${tsAccountModule}.js"`)
195
+ }
196
+
197
+ dependedAccounts.push(account)
198
+ }
199
+ }
200
+
201
+ const source = `
202
+ /* Autogenerated file. Do not edit manually. */
203
+ /* tslint:disable */
204
+ /* eslint-disable */
205
+
206
+ /* Generated modules for account ${address} */
207
+
208
+ ${imports}
209
+
210
+ ${moduleImports.join('\n')}
211
+
212
+ ${this.modules.map((m) => generateModule(m, this.config.network)).join('\n')}
213
+
214
+ export function loadAllTypes(_r: MoveCoder) {
215
+ ${dependedAccounts.map((a) => `_${a}.loadAllTypes(_r)`).join('\n')}
216
+
217
+ ${this.modules
218
+ .map((m) => {
219
+ return `_r.load(${normalizeToJSName(m.abi!.name)}.ABI)`
220
+ })
221
+ .join('\n')}
222
+ }
223
+ ` // source
224
+
225
+ return [
226
+ {
227
+ fileName: this.config.fileName + '.ts',
228
+ fileContent: source,
229
+ },
230
+ ]
231
+ }
232
+ }
233
+
234
+ function generateNetworkOption(network: AptosNetwork) {
235
+ switch (network) {
236
+ case AptosNetwork.TEST_NET:
237
+ return 'TEST_NET'
238
+ }
239
+ return 'MAIN_NET'
240
+ }
241
+
242
+ function generateModule(moduleByteCode: MoveModuleBytecode, network: AptosNetwork) {
243
+ if (!moduleByteCode.abi) {
244
+ return ''
245
+ }
246
+ const module = moduleByteCode.abi
247
+
248
+ const functions = module.exposed_functions.map((f) => generateOnEntryFunctions(module, f)).filter((s) => s !== '')
249
+
250
+ const eventStructs = getEventStructs(module)
251
+ const eventTypes = new Set(eventStructs.keys())
252
+ const events = Array.from(eventStructs.values())
253
+ .map((e) => generateOnEvents(module, e))
254
+ .filter((s) => s !== '')
255
+ const structs = module.structs.map((s) => generateStructs(module, s, eventTypes))
256
+ const callArgs = module.exposed_functions.map((f) => generateCallArgsStructs(module, f))
257
+
258
+ const moduleName = normalizeToJSName(module.name)
259
+ let processor = ''
260
+ if (functions.length > 0 || events.length > 0) {
261
+ processor = `export class ${moduleName} extends AptosBaseProcessor {
262
+
263
+ constructor(options: AptosBindOptions) {
264
+ super("${module.name}", options)
265
+ }
266
+ static DEFAULT_OPTIONS: AptosBindOptions = {
267
+ address: "${module.address}",
268
+ network: AptosNetwork.${generateNetworkOption(network)}
269
+ }
270
+
271
+ static bind(options: Partial<AptosBindOptions> = {}): ${moduleName} {
272
+ return new ${moduleName}({ ...${moduleName}.DEFAULT_OPTIONS, ...options })
273
+ }
274
+
275
+ ${functions.join('\n')}
276
+
277
+ ${events.join('\n')}
278
+
279
+ loadTypesInternal(registry: MoveCoder) {
280
+ loadAllTypes(registry)
281
+ }
282
+ }
283
+ `
284
+ }
285
+
286
+ return `
287
+ ${processor}
288
+
289
+ export namespace ${moduleName} {
290
+ ${structs.join('\n')}
291
+
292
+ ${callArgs.join('\n')}
293
+
294
+ export function loadTypes(_r: MoveCoder) {
295
+ loadAllTypes(_r)
296
+ }
297
+ export const ABI: MoveModule = JSON.parse('${JSON.stringify(module)}')
298
+ }
299
+ `
300
+ }
301
+
302
+ function generateStructs(module: MoveModule, struct: MoveStruct, events: Set<string>) {
303
+ const genericString = generateStructTypeParameters(struct)
304
+ const genericStringAny = generateStructTypeParameters(struct, true)
305
+
306
+ const structName = normalizeToJSName(struct.name)
307
+
308
+ const fields = struct.fields.map((field) => {
309
+ return `${field.name}: ${generateType(field.type, module.address)}`
310
+ })
311
+
312
+ let eventPayload = ''
313
+ if (events.has(moduleQname(module) + SPLITTER + struct.name)) {
314
+ eventPayload = `
315
+ export interface ${structName}Instance extends
316
+ TypedEventInstance<${structName}${genericStringAny}> {
317
+ /** @deprecated use {@link data_decoded} instead */
318
+ data_typed: ${structName}${genericStringAny}
319
+ data_decoded: ${structName}${genericStringAny}
320
+ type_arguments: [${struct.generic_type_params.map((_) => 'string').join(', ')}]
321
+ }
322
+ `
323
+ }
324
+
325
+ return `
326
+ export class ${structName}${genericString} {
327
+ static TYPE_QNAME = '${module.address}::${module.name}::${struct.name}'
328
+ ${fields.join('\n')}
329
+ }
330
+
331
+ ${eventPayload}
332
+ `
333
+ }
334
+
335
+ function generateFunctionTypeParameters(func: MoveFunction) {
336
+ let genericString = ''
337
+ if (func.generic_type_params && func.generic_type_params.length > 0) {
338
+ const params = func.generic_type_params
339
+ .map((v, idx) => {
340
+ return `T${idx}=any`
341
+ })
342
+ .join(',')
343
+ genericString = `<${params}>`
344
+ }
345
+ return genericString
346
+ }
347
+
348
+ function generateStructTypeParameters(struct: MoveStruct, useAny = false) {
349
+ let genericString = ''
350
+
351
+ if (struct.generic_type_params && struct.generic_type_params.length > 0) {
352
+ const params = struct.generic_type_params
353
+ .map((v, idx) => {
354
+ return useAny ? 'any' : 'T' + idx
355
+ })
356
+ .join(',')
357
+ genericString = `<${params}>`
358
+ }
359
+ return genericString
360
+ }
361
+
362
+ function generateCallArgsStructs(module: MoveModule, func: MoveFunction) {
363
+ if (!func.is_entry) {
364
+ return
365
+ }
366
+
367
+ const fields = getMeaningfulFunctionParams(func).map((param) => {
368
+ return `${generateType(param, module.address)}`
369
+ })
370
+
371
+ const camelFuncName = capitalizeFirstChar(camelize(func.name))
372
+
373
+ const genericString = generateFunctionTypeParameters(func)
374
+ return `
375
+ export interface ${camelFuncName}Payload${genericString}
376
+ extends TypedEntryFunctionPayload<[${fields.join(',')}]> {
377
+ /** @deprecated use {@link arguments_decoded} instead */
378
+ arguments_typed: [${fields.join(',')}],
379
+ arguments_decoded: [${fields.join(',')}],
380
+ type_arguments: [${func.generic_type_params.map((_) => 'string').join(', ')}]
381
+ }
382
+ `
383
+ }
384
+
385
+ function generateOnEntryFunctions(module: MoveModule, func: MoveFunction) {
386
+ if (!func.is_entry) {
387
+ return ''
388
+ }
389
+
390
+ // const genericString = generateFunctionTypeParameters(func)
391
+ const moduleName = normalizeToJSName(module.name)
392
+
393
+ const camelFuncName = capitalizeFirstChar(camelize(func.name))
394
+ const source = `
395
+ onEntry${camelFuncName}(func: (call: ${moduleName}.${camelFuncName}Payload, ctx: AptosContext) => void, filter?: CallFilter, fetchConfig?: AptosFetchConfig): ${moduleName} {
396
+ this.onEntryFunctionCall(func, {
397
+ ...filter,
398
+ function: '${module.name}::${func.name}'
399
+ },
400
+ fetchConfig)
401
+ return this
402
+ }`
403
+
404
+ return source
405
+ }
406
+
407
+ function getEventStructs(module: MoveModule) {
408
+ const qname = moduleQname(module)
409
+ const structMap = new Map<string, MoveStruct>()
410
+ const eventMap = new Map<string, MoveStruct>()
411
+
412
+ for (const struct of module.structs) {
413
+ structMap.set(qname + SPLITTER + struct.name, struct)
414
+ }
415
+
416
+ for (const struct of module.structs) {
417
+ for (const field of struct.fields) {
418
+ const t = parseMoveType(field.type)
419
+ if (t.qname === '0x1::event::EventHandle') {
420
+ const event = t.typeArgs[0].qname
421
+ const eventStruct = structMap.get(event)
422
+ if (eventStruct) {
423
+ eventMap.set(event, eventStruct)
424
+ }
425
+ }
426
+ }
427
+ }
428
+
429
+ return eventMap
430
+ }
431
+
432
+ function generateOnEvents(module: MoveModule, struct: MoveStruct): string {
433
+ // for struct that has drop + store
434
+ // if (!isEvent(struct, module)) {
435
+ // return ''
436
+ // }
437
+
438
+ // const genericString = generateStructTypeParameters(struct)
439
+
440
+ const moduleName = normalizeToJSName(module.name)
441
+ const source = `
442
+ onEvent${struct.name}(func: (event: ${moduleName}.${normalizeToJSName(
443
+ struct.name
444
+ )}Instance, ctx: AptosContext) => void, fetchConfig?: AptosFetchConfig): ${moduleName} {
445
+ this.onEvent(func, {
446
+ type: '${module.name}::${struct.name}'
447
+ },
448
+ fetchConfig)
449
+ return this
450
+ }
451
+ `
452
+ return source
453
+ }
454
+
455
+ function camelize(input: string): string {
456
+ return input
457
+ .split('_')
458
+ .reduce(
459
+ (res, word, i) =>
460
+ i === 0 ? word.toLowerCase() : `${res}${word.charAt(0).toUpperCase()}${word.slice(1).toLowerCase()}`,
461
+ ''
462
+ )
463
+ }
464
+
465
+ function capitalizeFirstChar(input: string): string {
466
+ if (!input) {
467
+ return input
468
+ }
469
+ return input[0].toUpperCase() + (input.length > 1 ? input.substring(1) : '')
470
+ }
@@ -0,0 +1 @@
1
+ export * from './codegen.js'
@@ -0,0 +1,8 @@
1
+ {
2
+ "compilerOptions": {
3
+ "rootDir": "..",
4
+ "outDir": "../../../lib/aptos"
5
+ },
6
+ "extends": "../../../tsconfig.json",
7
+ "include": ["./"]
8
+ }
@@ -0,0 +1,165 @@
1
+ import { MoveModule } from '../move-types.js'
2
+ import { parseMoveType, TypeDescriptor } from '../types.js'
3
+ import { moduleQname, moduleQnameForType, SPLITTER, VECTOR_STR } from '../utils.js'
4
+
5
+ function generateTypeForDescriptor(type: TypeDescriptor, currentAddress: string): string {
6
+ // TODO &signer is defintely an address, but what if &OTHER_TYPE?
7
+ if (type.qname.startsWith('&')) {
8
+ return 'Address'
9
+ }
10
+
11
+ switch (type.qname) {
12
+ case 'signer': // TODO check this
13
+ case 'address':
14
+ return 'Address'
15
+ case '0x1::string::String':
16
+ return 'string'
17
+ case 'bool':
18
+ return 'Boolean'
19
+ case 'u8':
20
+ case 'u16':
21
+ case 'u32':
22
+ return 'number'
23
+ case 'u64':
24
+ case 'u128':
25
+ return 'bigint'
26
+ }
27
+
28
+ if (type.qname === VECTOR_STR) {
29
+ // vector<u8> as hex string
30
+ const elementTypeQname = type.typeArgs[0].qname
31
+ if (elementTypeQname === 'u8') {
32
+ return 'string'
33
+ }
34
+ if (elementTypeQname.startsWith('T') && !elementTypeQname.includes(SPLITTER)) {
35
+ return `${elementTypeQname}[] | string`
36
+ }
37
+ return generateTypeForDescriptor(type.typeArgs[0], currentAddress) + '[]'
38
+ }
39
+
40
+ const simpleName = generateSimpleType(type.qname, currentAddress)
41
+ if (simpleName.length === 0) {
42
+ console.error('unexpected error')
43
+ }
44
+ if (simpleName.includes('vector')) {
45
+ console.error('unexpected error')
46
+ }
47
+ if (type.typeArgs.length > 0) {
48
+ // return simpleName
49
+ return simpleName + '<' + type.typeArgs.map((t) => generateTypeForDescriptor(t, currentAddress)).join(',') + '>'
50
+ }
51
+ return simpleName
52
+ }
53
+
54
+ function generateSimpleType(type: string, currentAddress: string): string {
55
+ const parts = type.split(SPLITTER)
56
+
57
+ for (let i = 0; i < parts.length; i++) {
58
+ parts[i] = normalizeToJSName(parts[i])
59
+ }
60
+
61
+ if (parts.length < 2) {
62
+ return parts[0]
63
+ }
64
+ if (parts[0] === currentAddress) {
65
+ return parts.slice(1).join('.')
66
+ }
67
+ return '_' + parts.join('.')
68
+ }
69
+
70
+ // TODO ctx need to have type parameters
71
+ export function generateType(type: string, currentAddress: string): string {
72
+ return generateTypeForDescriptor(parseMoveType(type), currentAddress)
73
+ }
74
+
75
+ export class AccountModulesImportInfo {
76
+ // account to module
77
+ imports: Map<string, Set<string>>
78
+ account: string
79
+ moduleName: string
80
+
81
+ constructor(account: string, tsModuleName: string) {
82
+ this.account = account
83
+ this.moduleName = tsModuleName
84
+ this.imports = new Map<string, Set<string>>()
85
+ }
86
+
87
+ addImport(account: string, module: string) {
88
+ if (account === this.account) {
89
+ return
90
+ }
91
+ let accountModules = this.imports.get(account)
92
+ if (!accountModules) {
93
+ accountModules = new Set<string>()
94
+ this.imports.set(account, accountModules)
95
+ }
96
+ accountModules.add(module)
97
+ }
98
+ }
99
+
100
+ export class AccountRegister {
101
+ accountImports = new Map<string, AccountModulesImportInfo>()
102
+ pendingAccounts = new Set<string>()
103
+
104
+ // loadedAccount = new Set<string>()
105
+ typeDescriptors = new Map<string, TypeDescriptor>()
106
+
107
+ private loadTypeDescriptor(type: string) {
108
+ let descriptor = this.typeDescriptors.get(type)
109
+
110
+ // const descriptparseMoveType(type)
111
+ if (!descriptor) {
112
+ descriptor = parseMoveType(type)
113
+ this.typeDescriptors.set(type, descriptor)
114
+ }
115
+ return descriptor
116
+ }
117
+
118
+ register(module: MoveModule, tsModuleName: string): AccountModulesImportInfo {
119
+ const currentModuleFqn = moduleQname(module)
120
+
121
+ let accountModuleImports = this.accountImports.get(module.address)
122
+ if (!accountModuleImports) {
123
+ accountModuleImports = new AccountModulesImportInfo(module.address, tsModuleName)
124
+ this.accountImports.set(module.address, accountModuleImports)
125
+ // the account has already be processed, delete pending task
126
+ this.pendingAccounts.delete(module.address)
127
+ }
128
+
129
+ for (const struct of module.structs) {
130
+ for (const field of struct.fields) {
131
+ for (const type of this.loadTypeDescriptor(field.type).dependedTypes()) {
132
+ const [account, module] = moduleQnameForType(type)
133
+ accountModuleImports.addImport(account, module)
134
+ if (!this.accountImports.has(account)) {
135
+ this.pendingAccounts.add(account)
136
+ }
137
+ }
138
+ }
139
+ }
140
+
141
+ for (const func of module.exposed_functions) {
142
+ if (!func.is_entry) {
143
+ continue
144
+ }
145
+ for (const param of func.params) {
146
+ for (const type of this.loadTypeDescriptor(param).dependedTypes()) {
147
+ const [account, module] = moduleQnameForType(type)
148
+ accountModuleImports.addImport(account, module)
149
+ if (!this.accountImports.has(account)) {
150
+ this.pendingAccounts.add(account)
151
+ }
152
+ }
153
+ }
154
+ }
155
+ this.accountImports.set(currentModuleFqn, accountModuleImports)
156
+ return accountModuleImports
157
+ }
158
+ }
159
+
160
+ export function normalizeToJSName(name: string) {
161
+ if (name === 'package' || name === 'volatile') {
162
+ return name + '_'
163
+ }
164
+ return name
165
+ }