@typemove/move 1.0.0-rc.4 → 1.0.0-rc.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+ export * from './types.js';
2
+ export * from './utils.js';
3
+ export * from './account.js';
4
+ export * from './chain-adapter.js';
5
+ export * from './abstract-codegen.js';
6
+ export * from './abstract-move-coder.js';
7
+ export * from './internal-models.js';
8
+ //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,13 +1,18 @@
1
1
  {
2
2
  "name": "@typemove/move",
3
- "version": "1.0.0-rc.4",
3
+ "version": "1.0.0-rc.6",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
+ "exports": {
7
+ ".": "./dist/index.js"
8
+ },
9
+ "main": "./dist/index.js",
6
10
  "files": [
7
11
  "{lib,src}",
8
12
  "!**/*.test.{js,ts}",
9
13
  "!{lib,src}/*/tests"
10
14
  ],
15
+ "types": "./dist/index.d.ts",
11
16
  "dependencies": {
12
17
  "chalk": "^5.2.0",
13
18
  "radash": "^11.0.0"
@@ -26,17 +26,18 @@ interface OutputFile {
26
26
  fileContent: string
27
27
  }
28
28
 
29
- interface Config<NetworkType> {
29
+ interface Config {
30
30
  fileName: string
31
31
  outputDir: string
32
- network: NetworkType
32
+ // network: NetworkType
33
33
  }
34
34
 
35
35
  // TODO be able to generate cjs
36
- export abstract class AbstractCodegen<NetworkType, ModuleTypes, StructType> {
37
- TEST_NET: NetworkType
38
- MAIN_NET: NetworkType
36
+ export abstract class AbstractCodegen<ModuleTypes, StructType> {
37
+ // TEST_NET: NetworkType
38
+ // MAIN_NET: NetworkType
39
39
  ADDRESS_TYPE: string
40
+ REFERENCE_TYPE: string
40
41
  PREFIX: string
41
42
  STRUCT_FIELD_NAME: string = 'data'
42
43
  GENERATE_CLIENT = false
@@ -45,11 +46,9 @@ export abstract class AbstractCodegen<NetworkType, ModuleTypes, StructType> {
45
46
  SYSTEM_MODULES = new Set(['0x1', '0x2', '0x3'])
46
47
  ESM = true
47
48
 
48
- chainAdapter: ChainAdapter<NetworkType, ModuleTypes, StructType>
49
+ chainAdapter: ChainAdapter<ModuleTypes, StructType>
49
50
 
50
- protected constructor(
51
- chainAdapter: ChainAdapter<NetworkType, ModuleTypes, StructType>
52
- ) {
51
+ protected constructor(chainAdapter: ChainAdapter<ModuleTypes, StructType>) {
53
52
  this.chainAdapter = chainAdapter
54
53
  }
55
54
 
@@ -64,27 +63,7 @@ export abstract class AbstractCodegen<NetworkType, ModuleTypes, StructType> {
64
63
  async generate(
65
64
  srcDir: string,
66
65
  outputDir: string,
67
- builtin = false
68
- ): Promise<number> {
69
- const num1 = await this.generateForNetwork(
70
- srcDir,
71
- outputDir,
72
- this.MAIN_NET,
73
- builtin
74
- )
75
- const num2 = await this.generateForNetwork(
76
- path.join(srcDir, 'testnet'),
77
- path.join(outputDir, 'testnet'),
78
- this.TEST_NET,
79
- builtin
80
- )
81
- return num1 + num2
82
- }
83
-
84
- async generateForNetwork(
85
- srcDir: string,
86
- outputDir: string,
87
- network: NetworkType,
66
+ // network: NetworkType,
88
67
  builtin = false
89
68
  ) {
90
69
  if (!fs.existsSync(srcDir)) {
@@ -122,7 +101,7 @@ export abstract class AbstractCodegen<NetworkType, ModuleTypes, StructType> {
122
101
  const codeGen = new AccountCodegen(this, loader, abi, modules, {
123
102
  fileName: path.basename(file, '.json'),
124
103
  outputDir: outputDir,
125
- network,
104
+ // network,
126
105
  })
127
106
 
128
107
  outputs.push(...codeGen.generate())
@@ -131,13 +110,13 @@ export abstract class AbstractCodegen<NetworkType, ModuleTypes, StructType> {
131
110
  while (loader.pendingAccounts.size > 0) {
132
111
  for (const account of loader.pendingAccounts) {
133
112
  console.log(
134
- `download dependent module for account ${account} at ${network}`
113
+ `download dependent module for account ${account} at ${this.chainAdapter.endpoint}`
135
114
  )
136
115
 
137
116
  try {
138
117
  const rawModules = await this.chainAdapter.fetchModules(
139
- account,
140
- network
118
+ account
119
+ // network
141
120
  )
142
121
  const modules = this.chainAdapter.toInternalModules(rawModules)
143
122
 
@@ -156,7 +135,7 @@ export abstract class AbstractCodegen<NetworkType, ModuleTypes, StructType> {
156
135
  {
157
136
  fileName: account,
158
137
  outputDir: outputDir,
159
- network,
138
+ // network,
160
139
  }
161
140
  )
162
141
 
@@ -189,32 +168,44 @@ export abstract class AbstractCodegen<NetworkType, ModuleTypes, StructType> {
189
168
  rootFileContent += `export * as _${parsed.name.replaceAll(
190
169
  '-',
191
170
  '_'
192
- )} from './${parsed.name}${this.maybeEsmPrefix()}\n`
171
+ )} from './${parsed.name}${this.maybeEsmPrefix()}'\n`
193
172
  }
194
173
  fs.writeFileSync(rootFile, rootFileContent)
195
174
 
196
175
  return outputs.length + 1
197
176
  }
198
177
 
199
- generateNetworkOption(network: NetworkType) {
200
- switch (network) {
201
- case this.TEST_NET:
202
- return 'TEST_NET'
203
- }
204
- return 'MAIN_NET'
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) {
195
+ return ''
205
196
  }
206
197
 
207
198
  generateModule(
208
199
  module: InternalMoveModule,
209
- allEventStructs: Map<string, InternalMoveStruct>,
210
- network: NetworkType
200
+ allEventStructs: Map<string, InternalMoveStruct>
201
+ // network: NetworkType
211
202
  ) {
212
203
  const qname = moduleQname(module)
213
- const functions = this.GENERATE_ON_ENTRY
214
- ? module.exposedFunctions
215
- .map((f) => this.generateForEntryFunctions(module, f))
216
- .filter((s) => s !== '')
217
- : []
204
+ // const functions = this.GENERATE_ON_ENTRY
205
+ // ? module.exposedFunctions
206
+ // .map((f) => this.generateForEntryFunctions(module, f))
207
+ // .filter((s) => s !== '')
208
+ // : []
218
209
  const clientFunctions = this.GENERATE_CLIENT
219
210
  ? module.exposedFunctions
220
211
  .map((f) => this.generateClientFunctions(module, f))
@@ -239,53 +230,26 @@ export abstract class AbstractCodegen<NetworkType, ModuleTypes, StructType> {
239
230
  )
240
231
 
241
232
  const moduleName = normalizeToJSName(module.name)
242
- let processor = ''
243
233
  let client = ''
244
234
 
245
235
  if (clientFunctions.length > 0) {
246
236
  client = `
247
- export class ${moduleName}_client extends ModuleClient {
237
+ export class Client extends ModuleClient {
248
238
  ${clientFunctions.join('\n')}
249
239
  }
250
240
  `
251
241
  }
252
242
 
253
- if (functions.length > 0 || events.length > 0) {
254
- processor = `export class ${moduleName} extends ${
255
- this.PREFIX
256
- }BaseProcessor {
257
-
258
- constructor(options: ${this.PREFIX}BindOptions) {
259
- super("${module.name}", options)
260
- }
261
- static DEFAULT_OPTIONS: ${this.PREFIX}BindOptions = {
262
- address: "${module.address}",
263
- network: ${this.PREFIX}Network.${this.generateNetworkOption(network)}
264
- }
265
-
266
- static bind(options: Partial<${
267
- this.PREFIX
268
- }BindOptions> = {}): ${moduleName} {
269
- return new ${moduleName}({ ...${moduleName}.DEFAULT_OPTIONS, ...options })
270
- }
271
-
272
- ${functions.join('\n')}
273
-
274
- ${events.join('\n')}
275
- }
276
- `
277
- }
278
-
243
+ // TODO how to deal with callArgs
279
244
  return `
280
- ${client}
281
-
282
- ${processor}
245
+ ${this.generateModuleExtra(module, allEventStructs)}
283
246
 
284
247
  export namespace ${moduleName} {
285
248
  ${structs.join('\n')}
286
-
287
- ${callArgs.join('\n')}
288
- }
249
+
250
+ ${client}
251
+ ${this.generateBuilder(module)}
252
+ }
289
253
  `
290
254
  }
291
255
 
@@ -446,12 +410,12 @@ export abstract class AbstractCodegen<NetworkType, ModuleTypes, StructType> {
446
410
  return source
447
411
  }
448
412
 
449
- generateForEntryFunctions(
450
- module: InternalMoveModule,
451
- func: InternalMoveFunction
452
- ) {
453
- return ''
454
- }
413
+ // generateForEntryFunctions(
414
+ // module: InternalMoveModule,
415
+ // func: InternalMoveFunction
416
+ // ) {
417
+ // return ''
418
+ // }
455
419
 
456
420
  generateForEvents(
457
421
  module: InternalMoveModule,
@@ -465,7 +429,7 @@ export abstract class AbstractCodegen<NetworkType, ModuleTypes, StructType> {
465
429
  currentAddress: string
466
430
  ): string {
467
431
  if (type.reference) {
468
- return this.ADDRESS_TYPE
432
+ return this.REFERENCE_TYPE
469
433
  }
470
434
 
471
435
  switch (type.qname) {
@@ -551,21 +515,33 @@ export abstract class AbstractCodegen<NetworkType, ModuleTypes, StructType> {
551
515
  }
552
516
  return '_' + parts.join('.')
553
517
  }
518
+
519
+ generateImports() {
520
+ const imports = `
521
+ import { TypeDescriptor, ANY_TYPE } from "@typemove/move"
522
+ import {
523
+ MoveCoder, defaultMoveCoder, TypedEventInstance } from "@typemove/${this.PREFIX.toLowerCase()}"
524
+ import { ${this.ADDRESS_TYPE}, ${
525
+ this.ADDRESS_TYPE === this.REFERENCE_TYPE ? '' : `${this.REFERENCE_TYPE},`
526
+ } ModuleClient } from "@typemove/${this.PREFIX.toLowerCase()}"
527
+ `
528
+ return imports
529
+ }
554
530
  }
555
531
 
556
- export class AccountCodegen<NetworkType, ModuleType, StructType> {
532
+ export class AccountCodegen<ModuleType, StructType> {
557
533
  modules: InternalMoveModule[]
558
- config: Config<NetworkType>
534
+ config: Config
559
535
  abi: ModuleType[]
560
536
  loader: AccountRegister
561
- moduleGen: AbstractCodegen<NetworkType, ModuleType, StructType>
537
+ moduleGen: AbstractCodegen<ModuleType, StructType>
562
538
 
563
539
  constructor(
564
- moduleGen: AbstractCodegen<NetworkType, ModuleType, StructType>,
540
+ moduleGen: AbstractCodegen<ModuleType, StructType>,
565
541
  loader: AccountRegister,
566
542
  abi: ModuleType[],
567
543
  modules: InternalMoveModule[],
568
- config: Config<NetworkType>
544
+ config: Config
569
545
  ) {
570
546
  // const json = fs.readFileSync(config.srcFile, 'utf-8')
571
547
  this.moduleGen = moduleGen
@@ -604,7 +580,8 @@ export class AccountCodegen<NetworkType, ModuleType, StructType> {
604
580
  if (isFrameworkAccount(account) && !isFrameworkAccount(address)) {
605
581
  // Decide where to find runtime library
606
582
  moduleImports.push(
607
- `import { _${account} } from "@typemove/${this.moduleGen.PREFIX.toLowerCase()}"`
583
+ `import { _${account} } from "@typemove/${this.moduleGen.PREFIX.toLowerCase()}/builtin"`
584
+ // `import _${account} = builtin._${account} `
608
585
  )
609
586
  } else {
610
587
  moduleImports.push(
@@ -616,14 +593,11 @@ export class AccountCodegen<NetworkType, ModuleType, StructType> {
616
593
  }
617
594
  }
618
595
 
619
- let loadAllTypes = `loadAllTypes(defaultMoveCoder(${
620
- this.moduleGen.PREFIX
621
- }Network.${this.moduleGen.generateNetworkOption(this.config.network)}))`
596
+ let loadAllTypes = `loadAllTypes(defaultMoveCoder())`
622
597
 
623
598
  if (this.moduleGen.SYSTEM_MODULES.has(address)) {
624
599
  loadAllTypes = `
625
- loadAllTypes(defaultMoveCoder(${this.moduleGen.PREFIX}Network.MAIN_NET))
626
- loadAllTypes(defaultMoveCoder(${this.moduleGen.PREFIX}Network.TEST_NET))
600
+ loadAllTypes(defaultMoveCoder())
627
601
  `
628
602
  }
629
603
 
@@ -637,14 +611,12 @@ export class AccountCodegen<NetworkType, ModuleType, StructType> {
637
611
 
638
612
  /* Generated modules for account ${address} */
639
613
 
640
- ${this.generateImports()}
614
+ ${this.moduleGen.generateImports()}
641
615
 
642
616
  ${moduleImports.join('\n')}
643
617
 
644
618
  ${this.modules
645
- .map((m) =>
646
- this.moduleGen.generateModule(m, eventsMap, this.config.network)
647
- )
619
+ .map((m) => this.moduleGen.generateModule(m, eventsMap))
648
620
  .join('\n')}
649
621
 
650
622
  const MODULES = JSON.parse('${JSON.stringify(this.abi)}')
@@ -666,19 +638,4 @@ export class AccountCodegen<NetworkType, ModuleType, StructType> {
666
638
  },
667
639
  ]
668
640
  }
669
-
670
- generateImports() {
671
- const imports = `
672
- import { TypeDescriptor, ANY_TYPE } from "@typemove/move"
673
- import {
674
- MoveCoder, defaultMoveCoder, ${
675
- this.moduleGen.PREFIX
676
- }Network } from "@typemove/${this.moduleGen.PREFIX.toLowerCase()}"
677
- import { ${
678
- this.moduleGen.ADDRESS_TYPE
679
- }, ModuleClient } from "@typemove/${this.moduleGen.PREFIX.toLowerCase()}"
680
- `
681
-
682
- return imports
683
- }
684
641
  }
@@ -18,15 +18,15 @@ import {
18
18
  // import { bytesToBigInt } from '../utils/index.js'
19
19
  import { ChainAdapter } from './chain-adapter.js'
20
20
 
21
- export abstract class AbstractMoveCoder<Network, ModuleType, StructType> {
21
+ export abstract class AbstractMoveCoder<ModuleType, StructType> {
22
22
  protected moduleMapping = new Map<string, InternalMoveModule>()
23
23
  private typeMapping = new Map<string, InternalMoveStruct>()
24
24
  private funcMapping = new Map<string, InternalMoveFunction>()
25
- network: Network
26
- adapter: ChainAdapter<Network, ModuleType, StructType>
25
+ // network: string
26
+ adapter: ChainAdapter<ModuleType, StructType>
27
27
 
28
- protected constructor(network: Network) {
29
- this.network = network
28
+ protected constructor(adapter: ChainAdapter<ModuleType, StructType>) {
29
+ this.adapter = adapter
30
30
  }
31
31
 
32
32
  contains(account: string, name: string) {
@@ -89,11 +89,9 @@ export abstract class AbstractMoveCoder<Network, ModuleType, StructType> {
89
89
  const key = account + SPLITTER + module
90
90
  let resp = this.requestMap.get(account + SPLITTER + module)
91
91
  if (!resp) {
92
- resp = this.adapter
93
- .fetchModule(account, module, this.network)
94
- .then((m) => {
95
- return this.load(m)
96
- })
92
+ resp = this.adapter.fetchModule(account, module).then((m) => {
93
+ return this.load(m)
94
+ })
97
95
  this.requestMap.set(key, resp)
98
96
  }
99
97
  await resp
@@ -118,11 +116,9 @@ export abstract class AbstractMoveCoder<Network, ModuleType, StructType> {
118
116
  const key = account + SPLITTER + module
119
117
  let resp = this.requestMap.get(account + SPLITTER + module)
120
118
  if (!resp) {
121
- resp = this.adapter
122
- .fetchModule(account, module, this.network)
123
- .then((m) => {
124
- return this.load(m)
125
- })
119
+ resp = this.adapter.fetchModule(account, module).then((m) => {
120
+ return this.load(m)
121
+ })
126
122
  this.requestMap.set(key, resp)
127
123
  }
128
124
  await resp
@@ -1,17 +1,16 @@
1
1
  import { InternalMoveModule, InternalMoveStruct } from './internal-models.js'
2
2
  import { TypeDescriptor } from './types.js'
3
3
 
4
- export abstract class ChainAdapter<NetworkType, ModuleType, StructType> {
5
- abstract fetchModule(
6
- account: string,
7
- module: string,
8
- network: NetworkType
9
- ): Promise<ModuleType>
4
+ export abstract class ChainAdapter<ModuleType, StructType> {
5
+ endpoint: string
10
6
 
11
- abstract fetchModules(
12
- account: string,
13
- network: NetworkType
14
- ): Promise<ModuleType[]>
7
+ constructor(endpoint: string) {
8
+ this.endpoint = endpoint
9
+ }
10
+
11
+ abstract fetchModule(account: string, module: string): Promise<ModuleType>
12
+
13
+ abstract fetchModules(account: string): Promise<ModuleType[]>
15
14
  abstract toInternalModules(modules: ModuleType[]): InternalMoveModule[]
16
15
 
17
16
  // Get all structs that represent Events
package/src/index.ts CHANGED
@@ -2,3 +2,6 @@ export * from './types.js'
2
2
  export * from './utils.js'
3
3
  export * from './account.js'
4
4
  export * from './chain-adapter.js'
5
+ export * from './abstract-codegen.js'
6
+ export * from './abstract-move-coder.js'
7
+ export * from './internal-models.js'
package/src/types.ts CHANGED
@@ -88,7 +88,7 @@ export class TypeDescriptor<T = any> {
88
88
  return []
89
89
  }
90
90
 
91
- if (this.qname.toLowerCase() === VECTOR_STR) {
91
+ if (this.isVector()) {
92
92
  return this.typeArgs[0].dependedTypes()
93
93
  }
94
94
 
@@ -117,6 +117,10 @@ export class TypeDescriptor<T = any> {
117
117
 
118
118
  return Array.from(types)
119
119
  }
120
+
121
+ isVector(): boolean {
122
+ return this.qname.toLowerCase() === VECTOR_STR
123
+ }
120
124
  }
121
125
 
122
126
  export function parseMoveType(type: string): TypeDescriptor {