@sentio/sdk 1.37.0-rc.2 → 1.37.0-rc.4

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