@typemove/move 1.0.0-rc.9 → 1.0.1-rc.1

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 (81) hide show
  1. package/dist/cjs/abstract-codegen.d.ts +49 -0
  2. package/dist/cjs/abstract-codegen.d.ts.map +1 -0
  3. package/dist/cjs/abstract-codegen.js +405 -0
  4. package/dist/cjs/abstract-codegen.js.map +1 -0
  5. package/dist/cjs/abstract-move-coder.d.ts +26 -0
  6. package/dist/cjs/abstract-move-coder.d.ts.map +1 -0
  7. package/dist/cjs/abstract-move-coder.js +253 -0
  8. package/dist/cjs/abstract-move-coder.js.map +1 -0
  9. package/dist/cjs/account.d.ts +16 -0
  10. package/dist/cjs/account.d.ts.map +1 -0
  11. package/dist/cjs/account.js +76 -0
  12. package/dist/cjs/account.js.map +1 -0
  13. package/dist/cjs/chain-adapter.d.ts +14 -0
  14. package/dist/cjs/chain-adapter.d.ts.map +1 -0
  15. package/dist/cjs/chain-adapter.js +11 -0
  16. package/dist/cjs/chain-adapter.js.map +1 -0
  17. package/dist/cjs/index.d.ts +8 -0
  18. package/dist/cjs/index.d.ts.map +1 -0
  19. package/dist/cjs/index.js +11 -0
  20. package/dist/cjs/index.js.map +1 -0
  21. package/dist/cjs/internal-models.d.ts +36 -0
  22. package/dist/cjs/internal-models.d.ts.map +1 -0
  23. package/dist/cjs/internal-models.js +10 -0
  24. package/dist/cjs/internal-models.js.map +1 -0
  25. package/dist/cjs/package.json +3 -0
  26. package/dist/cjs/ts-type.test.d.ts +2 -0
  27. package/dist/cjs/ts-type.test.d.ts.map +1 -0
  28. package/dist/cjs/ts-type.test.js.map +1 -0
  29. package/dist/cjs/types.d.ts +43 -0
  30. package/dist/cjs/types.d.ts.map +1 -0
  31. package/dist/cjs/types.js +218 -0
  32. package/dist/cjs/types.js.map +1 -0
  33. package/dist/cjs/utils.d.ts +17 -0
  34. package/dist/cjs/utils.d.ts.map +1 -0
  35. package/dist/cjs/utils.js +82 -0
  36. package/dist/cjs/utils.js.map +1 -0
  37. package/dist/cjs/utils.test.d.ts +2 -0
  38. package/dist/cjs/utils.test.d.ts.map +1 -0
  39. package/dist/cjs/utils.test.js.map +1 -0
  40. package/dist/esm/abstract-codegen.d.ts +49 -0
  41. package/dist/esm/abstract-codegen.d.ts.map +1 -0
  42. package/dist/esm/abstract-codegen.js +399 -0
  43. package/dist/esm/abstract-codegen.js.map +1 -0
  44. package/dist/esm/abstract-move-coder.d.ts +26 -0
  45. package/dist/esm/abstract-move-coder.d.ts.map +1 -0
  46. package/dist/esm/abstract-move-coder.js +249 -0
  47. package/dist/esm/abstract-move-coder.js.map +1 -0
  48. package/dist/esm/account.d.ts +16 -0
  49. package/dist/esm/account.d.ts.map +1 -0
  50. package/dist/esm/account.js +71 -0
  51. package/dist/esm/account.js.map +1 -0
  52. package/dist/esm/chain-adapter.d.ts +14 -0
  53. package/dist/esm/chain-adapter.d.ts.map +1 -0
  54. package/dist/esm/chain-adapter.js +7 -0
  55. package/dist/esm/chain-adapter.js.map +1 -0
  56. package/dist/esm/index.d.ts +8 -0
  57. package/dist/esm/index.d.ts.map +1 -0
  58. package/dist/esm/index.js.map +1 -0
  59. package/dist/esm/internal-models.d.ts +36 -0
  60. package/dist/esm/internal-models.d.ts.map +1 -0
  61. package/dist/esm/internal-models.js +7 -0
  62. package/dist/esm/internal-models.js.map +1 -0
  63. package/dist/esm/ts-type.test.d.ts +2 -0
  64. package/dist/esm/ts-type.test.d.ts.map +1 -0
  65. package/dist/esm/ts-type.test.js.map +1 -0
  66. package/dist/esm/types.d.ts +43 -0
  67. package/dist/esm/types.d.ts.map +1 -0
  68. package/dist/esm/types.js +211 -0
  69. package/dist/esm/types.js.map +1 -0
  70. package/dist/esm/utils.d.ts +17 -0
  71. package/dist/esm/utils.d.ts.map +1 -0
  72. package/dist/esm/utils.js +69 -0
  73. package/dist/esm/utils.js.map +1 -0
  74. package/dist/esm/utils.test.d.ts +2 -0
  75. package/dist/esm/utils.test.d.ts.map +1 -0
  76. package/dist/esm/utils.test.js.map +1 -0
  77. package/package.json +12 -7
  78. package/src/abstract-codegen.ts +60 -224
  79. package/src/abstract-move-coder.ts +35 -144
  80. package/src/internal-models.ts +1 -0
  81. /package/dist/{index.js → esm/index.js} +0 -0
@@ -1,23 +1,10 @@
1
- import {
2
- InternalMoveFunction,
3
- InternalMoveFunctionVisibility,
4
- InternalMoveModule,
5
- InternalMoveStruct,
6
- } from './internal-models.js'
1
+ import { InternalMoveFunction, InternalMoveModule, InternalMoveStruct } from './internal-models.js'
7
2
  import path from 'path'
8
3
  import fs from 'fs'
9
4
  import { AccountModulesImportInfo, AccountRegister } from './account.js'
10
5
  import chalk from 'chalk'
11
6
  import { format } from 'prettier'
12
- import {
13
- isFrameworkAccount,
14
- moduleQname,
15
- normalizeToJSName,
16
- SPLITTER,
17
- upperFirst,
18
- VECTOR_STR,
19
- camel,
20
- } from './utils.js'
7
+ import { isFrameworkAccount, moduleQname, normalizeToJSName, SPLITTER, VECTOR_STR } from './utils.js'
21
8
  import { TypeDescriptor } from './types.js'
22
9
  import { ChainAdapter } from './chain-adapter.js'
23
10
 
@@ -34,14 +21,10 @@ interface Config {
34
21
 
35
22
  // TODO be able to generate cjs
36
23
  export abstract class AbstractCodegen<ModuleTypes, StructType> {
37
- // TEST_NET: NetworkType
38
- // MAIN_NET: NetworkType
39
24
  ADDRESS_TYPE: string
40
- REFERENCE_TYPE: string
25
+ SYSTEM_PACKAGE: string
41
26
  PREFIX: string
42
27
  STRUCT_FIELD_NAME: string = 'data'
43
- GENERATE_CLIENT = false
44
- GENERATE_ON_ENTRY = true
45
28
  PAYLOAD_OPTIONAL = false
46
29
  SYSTEM_MODULES = new Set(['0x1', '0x2', '0x3'])
47
30
  ESM = true
@@ -60,6 +43,14 @@ export abstract class AbstractCodegen<ModuleTypes, StructType> {
60
43
  return JSON.parse(fs.readFileSync(fullPath, 'utf-8'))
61
44
  }
62
45
 
46
+ protected defaultCoderPackage() {
47
+ return `@typemove/${this.PREFIX.toLowerCase()}`
48
+ }
49
+
50
+ generateLoadAll(isSystem: boolean): string {
51
+ return 'loadAllTypes(defaultMoveCoder())'
52
+ }
53
+
63
54
  async generate(
64
55
  srcDir: string,
65
56
  outputDir: string,
@@ -80,10 +71,7 @@ export abstract class AbstractCodegen<ModuleTypes, StructType> {
80
71
 
81
72
  // when generating user code, don't need to generate framework account
82
73
  for (const sysModule of this.SYSTEM_MODULES) {
83
- loader.accountImports.set(
84
- sysModule,
85
- new AccountModulesImportInfo(sysModule, sysModule)
86
- )
74
+ loader.accountImports.set(sysModule, new AccountModulesImportInfo(sysModule, sysModule))
87
75
  }
88
76
  // const client = getRpcClient(network)
89
77
 
@@ -100,7 +88,7 @@ export abstract class AbstractCodegen<ModuleTypes, StructType> {
100
88
  }
101
89
  const codeGen = new AccountCodegen(this, loader, abi, modules, {
102
90
  fileName: path.basename(file, '.json'),
103
- outputDir: outputDir,
91
+ outputDir: outputDir
104
92
  // network,
105
93
  })
106
94
 
@@ -109,9 +97,7 @@ export abstract class AbstractCodegen<ModuleTypes, StructType> {
109
97
 
110
98
  while (loader.pendingAccounts.size > 0) {
111
99
  for (const account of loader.pendingAccounts) {
112
- console.log(
113
- `download dependent module for account ${account} at ${this.chainAdapter.endpoint}`
114
- )
100
+ console.log(`download dependent module for account ${account} at ${this.chainAdapter.endpoint}`)
115
101
 
116
102
  try {
117
103
  const rawModules = await this.chainAdapter.fetchModules(
@@ -120,24 +106,15 @@ export abstract class AbstractCodegen<ModuleTypes, StructType> {
120
106
  )
121
107
  const modules = this.chainAdapter.toInternalModules(rawModules)
122
108
 
123
- fs.writeFileSync(
124
- path.resolve(srcDir, account + '.json'),
125
- JSON.stringify(rawModules, null, '\t')
126
- )
109
+ fs.writeFileSync(path.resolve(srcDir, account + '.json'), JSON.stringify(rawModules, null, '\t'))
127
110
  for (const module of modules) {
128
111
  loader.register(module, account)
129
112
  }
130
- const codeGen = new AccountCodegen(
131
- this,
132
- loader,
133
- rawModules,
134
- modules,
135
- {
136
- fileName: account,
137
- outputDir: outputDir,
138
- // network,
139
- }
140
- )
113
+ const codeGen = new AccountCodegen(this, loader, rawModules, modules, {
114
+ fileName: account,
115
+ outputDir: outputDir
116
+ // network,
117
+ })
141
118
 
142
119
  outputs.push(...codeGen.generate())
143
120
  } catch (e) {
@@ -154,7 +131,7 @@ export abstract class AbstractCodegen<ModuleTypes, StructType> {
154
131
 
155
132
  for (const output of outputs) {
156
133
  // const content = output.fileContent
157
- const content = format(output.fileContent, { parser: 'typescript' })
134
+ const content = await format(output.fileContent, { parser: 'typescript' })
158
135
  fs.writeFileSync(path.join(outputDir, output.fileName), content)
159
136
  }
160
137
 
@@ -165,33 +142,16 @@ export abstract class AbstractCodegen<ModuleTypes, StructType> {
165
142
  `
166
143
  for (const output of outputs) {
167
144
  const parsed = path.parse(output.fileName)
168
- rootFileContent += `export * as _${parsed.name.replaceAll(
169
- '-',
170
- '_'
171
- )} from './${parsed.name}${this.maybeEsmPrefix()}'\n`
145
+ rootFileContent += `export * as _${parsed.name.replaceAll('-', '_')} from './${
146
+ parsed.name
147
+ }${this.maybeEsmPrefix()}'\n`
172
148
  }
173
149
  fs.writeFileSync(rootFile, rootFileContent)
174
150
 
175
151
  return outputs.length + 1
176
152
  }
177
153
 
178
- // generateNetworkOption(network: NetworkType) {
179
- // switch (network) {
180
- // case this.TEST_NET:
181
- // return 'TEST_NET'
182
- // }
183
- // return 'MAIN_NET'
184
- // }
185
-
186
- protected generateModuleExtra(
187
- module: InternalMoveModule,
188
- allEventStructs: Map<string, InternalMoveStruct>
189
- // network: NetworkType
190
- ) {
191
- return ''
192
- }
193
-
194
- protected generateBuilder(module: InternalMoveModule) {
154
+ protected generateExtra(module: InternalMoveModule) {
195
155
  return ''
196
156
  }
197
157
 
@@ -201,16 +161,6 @@ export abstract class AbstractCodegen<ModuleTypes, StructType> {
201
161
  // network: NetworkType
202
162
  ) {
203
163
  const qname = moduleQname(module)
204
- // const functions = this.GENERATE_ON_ENTRY
205
- // ? module.exposedFunctions
206
- // .map((f) => this.generateForEntryFunctions(module, f))
207
- // .filter((s) => s !== '')
208
- // : []
209
- const clientFunctions = this.GENERATE_CLIENT
210
- ? module.exposedFunctions
211
- .map((f) => this.generateClientFunctions(module, f))
212
- .filter((s) => s !== '')
213
- : []
214
164
  const eventStructs = new Map<string, InternalMoveStruct>()
215
165
  for (const [type, struct] of allEventStructs.entries()) {
216
166
  if (type.startsWith(qname + SPLITTER)) {
@@ -222,43 +172,25 @@ export abstract class AbstractCodegen<ModuleTypes, StructType> {
222
172
  const events = Array.from(eventStructs.values())
223
173
  .map((e) => this.generateForEvents(module, e))
224
174
  .filter((s) => s !== '')
225
- const structs = module.structs.map((s) =>
226
- this.generateStructs(module, s, eventTypes)
227
- )
228
- const callArgs = module.exposedFunctions.map((f) =>
229
- this.generateCallArgsStructs(module, f)
230
- )
175
+ const structs = module.structs.map((s) => this.generateStructs(module, s, eventTypes))
176
+ const callArgs = module.exposedFunctions.map((f) => this.generateCallArgsStructs(module, f))
231
177
 
232
178
  const moduleName = normalizeToJSName(module.name)
233
- let client = ''
234
-
235
- if (clientFunctions.length > 0) {
236
- client = `
237
- export class Client extends ModuleClient {
238
- ${clientFunctions.join('\n')}
239
- }
240
- `
241
- }
242
-
243
- // TODO how to deal with callArgs
244
179
  return `
245
- ${this.generateModuleExtra(module, allEventStructs)}
246
180
 
247
181
  export namespace ${moduleName} {
248
182
  ${structs.join('\n')}
249
183
 
250
- ${client}
251
- ${this.generateBuilder(module)}
184
+ ${this.generateExtra(module)}
185
+
186
+ ${events.join('\n')}
187
+
188
+ ${callArgs.join('\n')}
252
189
  }
253
190
  `
254
191
  }
255
192
 
256
- generateStructs(
257
- module: InternalMoveModule,
258
- struct: InternalMoveStruct,
259
- events: Set<string>,
260
- typeOnly = false
261
- ) {
193
+ generateStructs(module: InternalMoveModule, struct: InternalMoveStruct, events: Set<string>, typeOnly = false) {
262
194
  const typeParams = struct.typeParams || []
263
195
  const genericString = this.generateStructTypeParameters(struct)
264
196
  const genericStringAny = this.generateStructTypeParameters(struct, true)
@@ -359,91 +291,17 @@ export abstract class AbstractCodegen<ModuleTypes, StructType> {
359
291
  // return genericString
360
292
  // }
361
293
 
362
- generateCallArgsStructs(
363
- module: InternalMoveModule,
364
- func: InternalMoveFunction
365
- ) {
366
- if (!func.isEntry) {
367
- return
368
- }
369
-
370
- const fields = this.chainAdapter
371
- .getMeaningfulFunctionParams(func.params)
372
- .map((param) => {
373
- return (
374
- this.generateTypeForDescriptor(param, module.address) +
375
- (this.PAYLOAD_OPTIONAL ? ' | undefined' : '')
376
- )
377
- })
378
-
379
- const camelFuncName = upperFirst(camel(func.name))
380
-
381
- const genericString = this.generateFunctionTypeParameters(func)
382
- return `
383
- export interface ${camelFuncName}Payload${genericString}
384
- extends TypedFunctionPayload<[${fields.join(',')}]> {
385
- arguments_decoded: [${fields.join(',')}],
386
- type_arguments: [${func.typeParams.map((_) => 'string').join(', ')}]
387
- }
388
- `
389
- }
390
-
391
- generateClientFunctions(
392
- module: InternalMoveModule,
393
- func: InternalMoveFunction
394
- ) {
395
- if (func.visibility === InternalMoveFunctionVisibility.PRIVATE) {
396
- return ''
397
- }
398
- if (func.isEntry) {
399
- return ''
400
- }
401
- // const moduleName = normalizeToJSName(module.name)
402
- const funcName = camel(func.name)
403
- const fields = this.chainAdapter
404
- .getMeaningfulFunctionParams(func.params)
405
- .map((param) => {
406
- return this.generateTypeForDescriptor(param, module.address)
407
- })
408
- const genericString = this.generateFunctionTypeParameters(func)
409
-
410
- const returns = func.return.map((param) => {
411
- return this.generateTypeForDescriptor(param, module.address)
412
- })
413
-
414
- const source = `
415
- ${funcName}${genericString}(type_arguments: [${func.typeParams
416
- .map((_) => 'string')
417
- .join(', ')}], args: [${fields.join(
418
- ','
419
- )}], version?: bigint): Promise<[${returns.join(',')}]> {
420
- return this.viewDecoded('${module.address}::${module.name}::${
421
- func.name
422
- }', type_arguments, args, version) as any
423
- }`
424
- return source
294
+ generateCallArgsStructs(module: InternalMoveModule, func: InternalMoveFunction) {
295
+ return ''
425
296
  }
426
297
 
427
- // generateForEntryFunctions(
428
- // module: InternalMoveModule,
429
- // func: InternalMoveFunction
430
- // ) {
431
- // return ''
432
- // }
433
-
434
- generateForEvents(
435
- module: InternalMoveModule,
436
- struct: InternalMoveStruct
437
- ): string {
298
+ generateForEvents(module: InternalMoveModule, struct: InternalMoveStruct): string {
438
299
  return ''
439
300
  }
440
301
 
441
- generateTypeForDescriptor(
442
- type: TypeDescriptor,
443
- currentAddress: string
444
- ): string {
302
+ generateTypeForDescriptor(type: TypeDescriptor, currentAddress: string): string {
445
303
  if (type.reference) {
446
- return this.REFERENCE_TYPE
304
+ return this.ADDRESS_TYPE
447
305
  }
448
306
 
449
307
  switch (type.qname) {
@@ -479,36 +337,23 @@ export abstract class AbstractCodegen<ModuleTypes, StructType> {
479
337
  // only for aptos
480
338
  return 'string'
481
339
  }
482
- if (
483
- elementTypeQname.startsWith('T') &&
484
- !elementTypeQname.includes(SPLITTER)
485
- ) {
340
+ if (elementTypeQname.startsWith('T') && !elementTypeQname.includes(SPLITTER)) {
486
341
  return `${elementTypeQname}[] | string`
487
342
  }
488
- return (
489
- this.generateTypeForDescriptor(type.typeArgs[0], currentAddress) + '[]'
490
- )
343
+ return this.generateTypeForDescriptor(type.typeArgs[0], currentAddress) + '[]'
491
344
  }
492
345
 
493
346
  const simpleName = this.generateSimpleType(type.qname, currentAddress)
494
347
  if (simpleName.length === 0) {
495
348
  console.error('unexpected error')
496
349
  }
497
- if (
498
- simpleName.toLowerCase() === VECTOR_STR ||
499
- simpleName.toLowerCase().startsWith(VECTOR_STR + SPLITTER)
500
- ) {
350
+ if (simpleName.toLowerCase() === VECTOR_STR || simpleName.toLowerCase().startsWith(VECTOR_STR + SPLITTER)) {
501
351
  console.error('unexpected vector type error')
502
352
  }
503
353
  if (type.typeArgs.length > 0) {
504
354
  // return simpleName
505
355
  return (
506
- simpleName +
507
- '<' +
508
- type.typeArgs
509
- .map((t) => this.generateTypeForDescriptor(t, currentAddress))
510
- .join(',') +
511
- '>'
356
+ simpleName + '<' + type.typeArgs.map((t) => this.generateTypeForDescriptor(t, currentAddress)).join(',') + '>'
512
357
  )
513
358
  }
514
359
  return simpleName
@@ -531,13 +376,19 @@ export abstract class AbstractCodegen<ModuleTypes, StructType> {
531
376
  }
532
377
 
533
378
  generateImports() {
379
+ let refImports = `import { ${this.ADDRESS_TYPE} } from "${this.SYSTEM_PACKAGE}"`
380
+ if (this.ADDRESS_TYPE === 'string') {
381
+ refImports = ''
382
+ }
383
+
534
384
  const imports = `
535
385
  import { TypeDescriptor, ANY_TYPE } from "@typemove/move"
536
386
  import {
537
- MoveCoder, defaultMoveCoder, TypedEventInstance } from "@typemove/${this.PREFIX.toLowerCase()}"
538
- import { ${this.ADDRESS_TYPE}, ${
539
- this.ADDRESS_TYPE === this.REFERENCE_TYPE ? '' : `${this.REFERENCE_TYPE},`
540
- } ModuleClient } from "@typemove/${this.PREFIX.toLowerCase()}"
387
+ MoveCoder, TypedEventInstance } from "@typemove/${this.PREFIX.toLowerCase()}"
388
+
389
+ import { defaultMoveCoder } from "${this.defaultCoderPackage()}"
390
+
391
+ ${refImports}
541
392
  `
542
393
  return imports
543
394
  }
@@ -588,9 +439,7 @@ export class AccountCodegen<ModuleType, StructType> {
588
439
  if (info) {
589
440
  for (const [account] of info.imports.entries()) {
590
441
  // Remap to user's filename if possible, TODO codepath not well tested
591
- const tsAccountModule =
592
- './' +
593
- (this.loader.accountImports.get(account)?.moduleName || account)
442
+ const tsAccountModule = './' + (this.loader.accountImports.get(account)?.moduleName || account)
594
443
  if (isFrameworkAccount(account) && !isFrameworkAccount(address)) {
595
444
  // Decide where to find runtime library
596
445
  moduleImports.push(
@@ -598,25 +447,14 @@ export class AccountCodegen<ModuleType, StructType> {
598
447
  // `import _${account} = builtin._${account} `
599
448
  )
600
449
  } else {
601
- moduleImports.push(
602
- `import * as _${account} from "${tsAccountModule}${this.moduleGen.maybeEsmPrefix()}"`
603
- )
450
+ moduleImports.push(`import * as _${account} from "${tsAccountModule}${this.moduleGen.maybeEsmPrefix()}"`)
604
451
  }
605
452
 
606
453
  dependedAccounts.push(account)
607
454
  }
608
455
  }
609
456
 
610
- let loadAllTypes = `loadAllTypes(defaultMoveCoder())`
611
-
612
- if (this.moduleGen.SYSTEM_MODULES.has(address)) {
613
- loadAllTypes = `
614
- loadAllTypes(defaultMoveCoder())
615
- `
616
- }
617
-
618
- const eventsMap: Map<string, InternalMoveStruct> =
619
- this.moduleGen.chainAdapter.getAllEventStructs(this.modules)
457
+ const eventsMap: Map<string, InternalMoveStruct> = this.moduleGen.chainAdapter.getAllEventStructs(this.modules)
620
458
 
621
459
  const source = `
622
460
  /* Autogenerated file. Do not edit manually. */
@@ -629,9 +467,7 @@ export class AccountCodegen<ModuleType, StructType> {
629
467
 
630
468
  ${moduleImports.join('\n')}
631
469
 
632
- ${this.modules
633
- .map((m) => this.moduleGen.generateModule(m, eventsMap))
634
- .join('\n')}
470
+ ${this.modules.map((m) => this.moduleGen.generateModule(m, eventsMap)).join('\n')}
635
471
 
636
472
  const MODULES = JSON.parse('${JSON.stringify(this.abi)}')
637
473
 
@@ -641,15 +477,15 @@ export class AccountCodegen<ModuleType, StructType> {
641
477
  coder.load(m as any)
642
478
  }
643
479
  }
644
-
645
- ${loadAllTypes}
480
+
481
+ ${this.moduleGen.generateLoadAll(this.moduleGen.SYSTEM_MODULES.has(address))}
646
482
  ` // source
647
483
 
648
484
  return [
649
485
  {
650
486
  fileName: this.config.fileName + '.ts',
651
- fileContent: source,
652
- },
487
+ fileContent: source
488
+ }
653
489
  ]
654
490
  }
655
491
  }