@typemove/move 1.2.3 → 1.2.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 (85) hide show
  1. package/package.json +1 -1
  2. package/dist/cjs/abstract-codegen.d.ts +0 -50
  3. package/dist/cjs/abstract-codegen.d.ts.map +0 -1
  4. package/dist/cjs/abstract-codegen.js +0 -417
  5. package/dist/cjs/abstract-codegen.js.map +0 -1
  6. package/dist/cjs/abstract-move-coder.d.ts +0 -26
  7. package/dist/cjs/abstract-move-coder.d.ts.map +0 -1
  8. package/dist/cjs/abstract-move-coder.js +0 -260
  9. package/dist/cjs/abstract-move-coder.js.map +0 -1
  10. package/dist/cjs/account.d.ts +0 -16
  11. package/dist/cjs/account.d.ts.map +0 -1
  12. package/dist/cjs/account.js +0 -76
  13. package/dist/cjs/account.js.map +0 -1
  14. package/dist/cjs/chain-adapter.d.ts +0 -13
  15. package/dist/cjs/chain-adapter.d.ts.map +0 -1
  16. package/dist/cjs/chain-adapter.js +0 -7
  17. package/dist/cjs/chain-adapter.js.map +0 -1
  18. package/dist/cjs/index.d.ts +0 -8
  19. package/dist/cjs/index.d.ts.map +0 -1
  20. package/dist/cjs/index.js +0 -11
  21. package/dist/cjs/index.js.map +0 -1
  22. package/dist/cjs/internal-models.d.ts +0 -36
  23. package/dist/cjs/internal-models.d.ts.map +0 -1
  24. package/dist/cjs/internal-models.js +0 -10
  25. package/dist/cjs/internal-models.js.map +0 -1
  26. package/dist/cjs/ts-type.test.d.ts +0 -2
  27. package/dist/cjs/ts-type.test.d.ts.map +0 -1
  28. package/dist/cjs/ts-type.test.js.map +0 -1
  29. package/dist/cjs/types.d.ts +0 -46
  30. package/dist/cjs/types.d.ts.map +0 -1
  31. package/dist/cjs/types.js +0 -250
  32. package/dist/cjs/types.js.map +0 -1
  33. package/dist/cjs/utils.d.ts +0 -17
  34. package/dist/cjs/utils.d.ts.map +0 -1
  35. package/dist/cjs/utils.js +0 -82
  36. package/dist/cjs/utils.js.map +0 -1
  37. package/dist/cjs/utils.test.d.ts +0 -2
  38. package/dist/cjs/utils.test.d.ts.map +0 -1
  39. package/dist/cjs/utils.test.js.map +0 -1
  40. package/dist/esm/abstract-codegen.d.ts +0 -50
  41. package/dist/esm/abstract-codegen.d.ts.map +0 -1
  42. package/dist/esm/abstract-codegen.js +0 -411
  43. package/dist/esm/abstract-codegen.js.map +0 -1
  44. package/dist/esm/abstract-move-coder.d.ts +0 -26
  45. package/dist/esm/abstract-move-coder.d.ts.map +0 -1
  46. package/dist/esm/abstract-move-coder.js +0 -256
  47. package/dist/esm/abstract-move-coder.js.map +0 -1
  48. package/dist/esm/account.d.ts +0 -16
  49. package/dist/esm/account.d.ts.map +0 -1
  50. package/dist/esm/account.js +0 -71
  51. package/dist/esm/account.js.map +0 -1
  52. package/dist/esm/chain-adapter.d.ts +0 -13
  53. package/dist/esm/chain-adapter.d.ts.map +0 -1
  54. package/dist/esm/chain-adapter.js +0 -3
  55. package/dist/esm/chain-adapter.js.map +0 -1
  56. package/dist/esm/index.d.ts +0 -8
  57. package/dist/esm/index.d.ts.map +0 -1
  58. package/dist/esm/index.js +0 -8
  59. package/dist/esm/index.js.map +0 -1
  60. package/dist/esm/internal-models.d.ts +0 -36
  61. package/dist/esm/internal-models.d.ts.map +0 -1
  62. package/dist/esm/internal-models.js +0 -7
  63. package/dist/esm/internal-models.js.map +0 -1
  64. package/dist/esm/ts-type.test.d.ts +0 -2
  65. package/dist/esm/ts-type.test.d.ts.map +0 -1
  66. package/dist/esm/ts-type.test.js.map +0 -1
  67. package/dist/esm/types.d.ts +0 -46
  68. package/dist/esm/types.d.ts.map +0 -1
  69. package/dist/esm/types.js +0 -243
  70. package/dist/esm/types.js.map +0 -1
  71. package/dist/esm/utils.d.ts +0 -17
  72. package/dist/esm/utils.d.ts.map +0 -1
  73. package/dist/esm/utils.js +0 -69
  74. package/dist/esm/utils.js.map +0 -1
  75. package/dist/esm/utils.test.d.ts +0 -2
  76. package/dist/esm/utils.test.d.ts.map +0 -1
  77. package/dist/esm/utils.test.js.map +0 -1
  78. package/src/abstract-codegen.ts +0 -504
  79. package/src/abstract-move-coder.ts +0 -288
  80. package/src/account.ts +0 -93
  81. package/src/chain-adapter.ts +0 -26
  82. package/src/index.ts +0 -7
  83. package/src/internal-models.ts +0 -41
  84. package/src/types.ts +0 -284
  85. package/src/utils.ts +0 -91
@@ -1,504 +0,0 @@
1
- import { InternalMoveFunction, InternalMoveModule, InternalMoveStruct } from './internal-models.js'
2
- import path from 'path'
3
- import fs from 'fs'
4
- import { AccountModulesImportInfo, AccountRegister } from './account.js'
5
- import chalk from 'chalk'
6
- import { format } from 'prettier'
7
- import { isFrameworkAccount, moduleQname, normalizeToJSName, SPLITTER, VECTOR_STR } from './utils.js'
8
- import { TypeDescriptor } from './types.js'
9
- import { ChainAdapter } from './chain-adapter.js'
10
-
11
- interface OutputFile {
12
- fileName: string
13
- fileContent: string
14
- }
15
-
16
- interface Config {
17
- fileName: string
18
- outputDir: string
19
- // network: NetworkType
20
- }
21
-
22
- // TODO be able to generate cjs
23
- export abstract class AbstractCodegen<ModuleTypes, StructType> {
24
- ADDRESS_TYPE: string
25
- SYSTEM_PACKAGE: string
26
- PREFIX: string
27
- STRUCT_FIELD_NAME: string = 'data'
28
- PAYLOAD_OPTIONAL = false
29
- SYSTEM_MODULES = new Set(['0x1', '0x2', '0x3'])
30
- ESM = true
31
-
32
- chainAdapter: ChainAdapter<ModuleTypes, StructType>
33
-
34
- protected constructor(chainAdapter: ChainAdapter<ModuleTypes, StructType>) {
35
- this.chainAdapter = chainAdapter
36
- }
37
-
38
- public maybeEsmPrefix() {
39
- return this.ESM ? '.js' : ''
40
- }
41
-
42
- readModulesFile(fullPath: string) {
43
- return JSON.parse(fs.readFileSync(fullPath, 'utf-8'))
44
- }
45
-
46
- protected defaultCoderPackage() {
47
- return `@typemove/${this.PREFIX.toLowerCase()}`
48
- }
49
-
50
- generateLoadAll(isSystem: boolean): string {
51
- return 'loadAllTypes(defaultMoveCoder())'
52
- }
53
-
54
- async generate(
55
- srcDir: string,
56
- outputDir: string,
57
- // network: NetworkType,
58
- builtin = false
59
- ) {
60
- if (!fs.existsSync(srcDir)) {
61
- return 0
62
- }
63
-
64
- const files = fs.readdirSync(srcDir)
65
- outputDir = path.resolve(outputDir)
66
- const outputs: OutputFile[] = []
67
-
68
- fs.mkdirSync(outputDir, { recursive: true })
69
-
70
- const loader = new AccountRegister()
71
-
72
- // when generating user code, don't need to generate framework account
73
- for (const sysModule of this.SYSTEM_MODULES) {
74
- loader.accountImports.set(sysModule, new AccountModulesImportInfo(sysModule, sysModule))
75
- }
76
- // const client = getRpcClient(network)
77
-
78
- for (const file of files) {
79
- if (!file.endsWith('.json')) {
80
- continue
81
- }
82
- const fullPath = path.resolve(srcDir, file)
83
- const abi = this.readModulesFile(fullPath)
84
- const modules = this.chainAdapter.toInternalModules(abi)
85
-
86
- for (const module of modules) {
87
- loader.register(module, path.basename(file, '.json'))
88
- }
89
- const codeGen = new AccountCodegen(this, loader, abi, modules, {
90
- fileName: path.basename(file, '.json'),
91
- outputDir: outputDir
92
- // network,
93
- })
94
-
95
- outputs.push(...codeGen.generate())
96
- }
97
-
98
- while (loader.pendingAccounts.size > 0) {
99
- for (const account of loader.pendingAccounts) {
100
- const id = await this.chainAdapter.getChainId()
101
- console.log(`download dependent module for account ${account} at ${id}`)
102
-
103
- try {
104
- const rawModules = await this.chainAdapter.fetchModules(
105
- account
106
- // network
107
- )
108
- const modules = this.chainAdapter.toInternalModules(rawModules)
109
-
110
- fs.writeFileSync(path.resolve(srcDir, account + '.json'), JSON.stringify(rawModules, null, '\t'))
111
- for (const module of modules) {
112
- loader.register(module, account)
113
- }
114
- const codeGen = new AccountCodegen(this, loader, rawModules, modules, {
115
- fileName: account,
116
- outputDir: outputDir
117
- // network,
118
- })
119
-
120
- outputs.push(...codeGen.generate())
121
- } catch (e) {
122
- console.error(
123
- chalk.red(
124
- 'Error downloading account module, check if you choose the right network,or download account modules manually into your director'
125
- )
126
- )
127
- console.error(e)
128
- process.exit(1)
129
- }
130
- }
131
- }
132
-
133
- for (const output of outputs) {
134
- let content = output.fileContent
135
- content = await format(output.fileContent, { parser: 'typescript' })
136
- fs.writeFileSync(path.join(outputDir, output.fileName), content)
137
- }
138
-
139
- const rootFile = path.join(outputDir, 'index.ts')
140
- let rootFileContent = `/* Autogenerated file. Do not edit manually. */
141
- /* tslint:disable */
142
- /* eslint-disable */
143
- `
144
- for (const output of outputs) {
145
- const parsed = path.parse(output.fileName)
146
- rootFileContent += `export * as _${parsed.name.replaceAll('-', '_')} from './${
147
- parsed.name
148
- }${this.maybeEsmPrefix()}'\n`
149
- }
150
- fs.writeFileSync(rootFile, rootFileContent)
151
-
152
- return outputs.length + 1
153
- }
154
-
155
- protected generateExtra(module: InternalMoveModule) {
156
- return ''
157
- }
158
-
159
- generateModule(
160
- module: InternalMoveModule,
161
- allEventStructs: Map<string, InternalMoveStruct>
162
- // network: NetworkType
163
- ) {
164
- const qname = moduleQname(module)
165
- const eventStructs = new Map<string, InternalMoveStruct>()
166
- for (const [type, struct] of allEventStructs.entries()) {
167
- if (type.startsWith(qname + SPLITTER)) {
168
- eventStructs.set(type, struct)
169
- }
170
- }
171
-
172
- const eventTypes = new Set(eventStructs.keys())
173
- const events = Array.from(eventStructs.values())
174
- .map((e) => this.generateForEvents(module, e))
175
- .filter((s) => s !== '')
176
- const structs = module.structs.map((s) => this.generateStructs(module, s, eventTypes))
177
- const callArgs = module.exposedFunctions.map((f) => this.generateCallArgsStructs(module, f))
178
-
179
- const moduleName = normalizeToJSName(module.name)
180
- return `
181
-
182
- export namespace ${moduleName} {
183
- ${structs.join('\n')}
184
-
185
- ${this.generateExtra(module)}
186
-
187
- ${events.join('\n')}
188
-
189
- ${callArgs.join('\n')}
190
- }
191
- `
192
- }
193
-
194
- generateStructs(module: InternalMoveModule, struct: InternalMoveStruct, events: Set<string>, typeOnly = false) {
195
- const typeParams = struct.typeParams || []
196
- const genericString = this.generateStructTypeParameters(struct)
197
- const genericStringAny = this.generateStructTypeParameters(struct, true)
198
-
199
- const structName = normalizeToJSName(struct.name)
200
-
201
- const fields = struct.fields.map((field) => {
202
- const type = this.generateTypeForDescriptor(field.type, module.address)
203
- return `${field.name}: ${type}`
204
- })
205
-
206
- const typeParamApplyArg = typeParams
207
- .map((v, idx) => {
208
- return `arg${idx}: TypeDescriptor<T${idx}> = ANY_TYPE`
209
- })
210
- .join(',')
211
- const typeParamApply = typeParams
212
- .map((v, idx) => {
213
- return `arg${idx}`
214
- })
215
- .join(',')
216
-
217
- const typeDescriptor = `
218
- export namespace ${structName}{
219
- export const TYPE_QNAME = '${module.address}::${module.name}::${struct.name}'
220
-
221
- const TYPE = new TypeDescriptor<${structName}${genericStringAny}>(${structName}.TYPE_QNAME)
222
-
223
- export function type${genericString}(${typeParamApplyArg}): TypeDescriptor<${structName}${genericString}> {
224
- return TYPE.apply(${typeParamApply})
225
- }
226
- }
227
- `
228
- if (typeOnly) {
229
- return typeDescriptor
230
- }
231
-
232
- let eventPayload = ''
233
- if (events.has(moduleQname(module) + SPLITTER + struct.name)) {
234
- eventPayload = `
235
- export interface ${structName}Instance extends
236
- TypedEventInstance<${structName}${genericStringAny}> {
237
- ${this.STRUCT_FIELD_NAME}_decoded: ${structName}${genericStringAny}
238
- type_arguments: [${struct.typeParams.map((_) => 'string').join(', ')}]
239
- }
240
- `
241
- }
242
-
243
- return `
244
- export interface ${structName}${genericString} {
245
- ${fields.join('\n')}
246
- }
247
-
248
- ${typeDescriptor}
249
-
250
- ${eventPayload}
251
- `
252
- }
253
-
254
- generateFunctionTypeParameters(func: InternalMoveFunction) {
255
- let genericString = ''
256
- if (func.typeParams && func.typeParams.length > 0) {
257
- const params = func.typeParams
258
- .map((v, idx) => {
259
- return `T${idx}=any`
260
- })
261
- .join(',')
262
- genericString = `<${params}>`
263
- }
264
- return genericString
265
- }
266
-
267
- generateFunctionReturnTypeParameters(func: InternalMoveFunction, currentAddress: string) {
268
- let returnType = ''
269
- if (func.return && func.return.length > 0) {
270
- returnType = func.return
271
- .map((v, idx) => {
272
- return this.generateTypeForDescriptor(v, currentAddress)
273
- })
274
- .join(',')
275
- }
276
- return '[' + returnType + ']'
277
- }
278
-
279
- generateStructTypeParameters(struct: InternalMoveStruct, useAny = false) {
280
- let genericString = ''
281
-
282
- if (struct.typeParams && struct.typeParams.length > 0) {
283
- const params = struct.typeParams
284
- .map((v, idx) => {
285
- return useAny ? 'any' : 'T' + idx
286
- })
287
- .join(',')
288
- genericString = `<${params}>`
289
- }
290
- return genericString
291
- }
292
-
293
- // generateTypeParameters(struct: TypeDescriptor, useAny = false) {
294
- // let genericString = ''
295
- //
296
- // if (struct.typeArgs && struct.typeArgs.length > 0) {
297
- // const params = struct.typeArgs
298
- // .map((_, idx) => {
299
- // return useAny ? 'any' : 'T' + idx
300
- // })
301
- // .join(',')
302
- // genericString = `<${params}>`
303
- // }
304
- // return genericString
305
- // }
306
-
307
- generateCallArgsStructs(module: InternalMoveModule, func: InternalMoveFunction) {
308
- return ''
309
- }
310
-
311
- generateForEvents(module: InternalMoveModule, struct: InternalMoveStruct): string {
312
- return ''
313
- }
314
-
315
- generateTypeForDescriptor(type: TypeDescriptor, currentAddress: string): string {
316
- if (type.reference) {
317
- return this.ADDRESS_TYPE
318
- }
319
-
320
- switch (type.qname) {
321
- case 'signer': // TODO check this
322
- case 'address':
323
- case 'Address':
324
- return this.ADDRESS_TYPE
325
- case '0x1::string::String':
326
- return 'string'
327
- case 'bool':
328
- case 'Bool':
329
- return 'Boolean'
330
- case 'u8':
331
- case 'U8':
332
- case 'u16':
333
- case 'U16':
334
- case 'u32':
335
- case 'U32':
336
- return 'number'
337
- case 'u64':
338
- case 'U64':
339
- case 'u128':
340
- case 'U128':
341
- case 'u256':
342
- case 'U256':
343
- return 'bigint'
344
- }
345
-
346
- if (type.qname.toLowerCase() === VECTOR_STR) {
347
- // vector<u8> as hex string
348
- const elementTypeQname = type.typeArgs[0].qname
349
- if (elementTypeQname === 'u8') {
350
- // only for aptos
351
- return 'string'
352
- }
353
- if (elementTypeQname.startsWith('T') && !elementTypeQname.includes(SPLITTER)) {
354
- return `${elementTypeQname}[] | string`
355
- }
356
- return this.generateTypeForDescriptor(type.typeArgs[0], currentAddress) + '[]'
357
- }
358
-
359
- const simpleName = this.generateSimpleType(type.qname, currentAddress)
360
- if (simpleName.length === 0) {
361
- console.error('unexpected error')
362
- }
363
- if (simpleName.toLowerCase() === VECTOR_STR || simpleName.toLowerCase().startsWith(VECTOR_STR + SPLITTER)) {
364
- console.error('unexpected vector type error')
365
- }
366
- if (type.typeArgs.length > 0) {
367
- // return simpleName
368
- return (
369
- simpleName + '<' + type.typeArgs.map((t) => this.generateTypeForDescriptor(t, currentAddress)).join(',') + '>'
370
- )
371
- }
372
- return simpleName
373
- }
374
-
375
- generateSimpleType(type: string, currentAddress: string): string {
376
- const parts = type.split(SPLITTER)
377
-
378
- for (let i = 0; i < parts.length; i++) {
379
- parts[i] = normalizeToJSName(parts[i])
380
- }
381
-
382
- if (parts.length < 2) {
383
- return parts[0]
384
- }
385
- if (parts[0] === currentAddress) {
386
- return parts.slice(1).join('.')
387
- }
388
- return '_' + parts.join('.')
389
- }
390
-
391
- generateImports() {
392
- let refImports = `import { ${this.ADDRESS_TYPE} } from "${this.SYSTEM_PACKAGE}"`
393
- if (this.ADDRESS_TYPE === 'string') {
394
- refImports = ''
395
- }
396
-
397
- const imports = `
398
- import { TypeDescriptor, ANY_TYPE } from "@typemove/move"
399
- import {
400
- MoveCoder, TypedEventInstance } from "@typemove/${this.PREFIX.toLowerCase()}"
401
-
402
- import { defaultMoveCoder } from "${this.defaultCoderPackage()}"
403
-
404
- ${refImports}
405
- `
406
- return imports
407
- }
408
- }
409
-
410
- export class AccountCodegen<ModuleType, StructType> {
411
- modules: InternalMoveModule[]
412
- config: Config
413
- abi: ModuleType[]
414
- loader: AccountRegister
415
- moduleGen: AbstractCodegen<ModuleType, StructType>
416
-
417
- constructor(
418
- moduleGen: AbstractCodegen<ModuleType, StructType>,
419
- loader: AccountRegister,
420
- abi: ModuleType[],
421
- modules: InternalMoveModule[],
422
- config: Config
423
- ) {
424
- // const json = fs.readFileSync(config.srcFile, 'utf-8')
425
- this.moduleGen = moduleGen
426
- this.abi = abi
427
- this.modules = modules
428
- this.config = config
429
- this.loader = loader
430
- }
431
-
432
- generate(): OutputFile[] {
433
- if (!this.modules) {
434
- return []
435
- }
436
- // const baseName = path.basename(this.config.fileName, '.json')
437
-
438
- let address: string | undefined
439
- for (const module of this.modules) {
440
- address = module.address
441
- }
442
- if (!address) {
443
- return []
444
- }
445
-
446
- const dependedAccounts: string[] = []
447
-
448
- const moduleImports: string[] = []
449
-
450
- const info = this.loader.accountImports.get(address)
451
-
452
- if (info) {
453
- for (const [account] of info.imports.entries()) {
454
- // Remap to user's filename if possible, TODO codepath not well tested
455
- const tsAccountModule = './' + (this.loader.accountImports.get(account)?.moduleName || account)
456
- if (isFrameworkAccount(account) && !isFrameworkAccount(address)) {
457
- // Decide where to find runtime library
458
- moduleImports.push(
459
- `import { _${account} } from "@typemove/${this.moduleGen.PREFIX.toLowerCase()}/builtin"`
460
- // `import _${account} = builtin._${account} `
461
- )
462
- } else {
463
- moduleImports.push(`import * as _${account} from "${tsAccountModule}${this.moduleGen.maybeEsmPrefix()}"`)
464
- }
465
-
466
- dependedAccounts.push(account)
467
- }
468
- }
469
-
470
- const eventsMap: Map<string, InternalMoveStruct> = this.moduleGen.chainAdapter.getAllEventStructs(this.modules)
471
-
472
- const source = `
473
- /* Autogenerated file. Do not edit manually. */
474
- /* tslint:disable */
475
- /* eslint-disable */
476
-
477
- /* Generated modules for account ${address} */
478
-
479
- ${this.moduleGen.generateImports()}
480
-
481
- ${moduleImports.join('\n')}
482
-
483
- ${this.modules.map((m) => this.moduleGen.generateModule(m, eventsMap)).join('\n')}
484
-
485
- const MODULES = JSON.parse('${JSON.stringify(this.abi)}')
486
-
487
- export function loadAllTypes(coder: MoveCoder) {
488
- ${dependedAccounts.map((a) => `_${a}.loadAllTypes(coder)`).join('\n')}
489
- for (const m of Object.values(MODULES)) {
490
- coder.load(m as any)
491
- }
492
- }
493
-
494
- ${this.moduleGen.generateLoadAll(this.moduleGen.SYSTEM_MODULES.has(address))}
495
- ` // source
496
-
497
- return [
498
- {
499
- fileName: this.config.fileName + '.ts',
500
- fileContent: source
501
- }
502
- ]
503
- }
504
- }