@typemove/move 1.0.0-rc.5 → 1.0.0-rc.7
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/package.json +1 -1
- package/src/abstract-codegen.ts +69 -78
- package/src/abstract-move-coder.ts +11 -15
- package/src/chain-adapter.ts +9 -10
- package/src/types.ts +5 -1
package/package.json
CHANGED
package/src/abstract-codegen.ts
CHANGED
|
@@ -26,17 +26,18 @@ interface OutputFile {
|
|
|
26
26
|
fileContent: string
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
interface Config
|
|
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<
|
|
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<
|
|
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
|
-
|
|
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 ${
|
|
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
|
|
|
@@ -196,26 +175,30 @@ export abstract class AbstractCodegen<NetworkType, ModuleTypes, StructType> {
|
|
|
196
175
|
return outputs.length + 1
|
|
197
176
|
}
|
|
198
177
|
|
|
199
|
-
generateNetworkOption(network: NetworkType) {
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
}
|
|
178
|
+
// generateNetworkOption(network: NetworkType) {
|
|
179
|
+
// switch (network) {
|
|
180
|
+
// case this.TEST_NET:
|
|
181
|
+
// return 'TEST_NET'
|
|
182
|
+
// }
|
|
183
|
+
// return 'MAIN_NET'
|
|
184
|
+
// }
|
|
206
185
|
|
|
207
186
|
protected generateModuleExtra(
|
|
208
187
|
module: InternalMoveModule,
|
|
209
|
-
allEventStructs: Map<string, InternalMoveStruct
|
|
210
|
-
network: NetworkType
|
|
188
|
+
allEventStructs: Map<string, InternalMoveStruct>
|
|
189
|
+
// network: NetworkType
|
|
211
190
|
) {
|
|
212
191
|
return ''
|
|
213
192
|
}
|
|
214
193
|
|
|
194
|
+
protected generateBuilder(module: InternalMoveModule) {
|
|
195
|
+
return ''
|
|
196
|
+
}
|
|
197
|
+
|
|
215
198
|
generateModule(
|
|
216
199
|
module: InternalMoveModule,
|
|
217
|
-
allEventStructs: Map<string, InternalMoveStruct
|
|
218
|
-
network: NetworkType
|
|
200
|
+
allEventStructs: Map<string, InternalMoveStruct>
|
|
201
|
+
// network: NetworkType
|
|
219
202
|
) {
|
|
220
203
|
const qname = moduleQname(module)
|
|
221
204
|
// const functions = this.GENERATE_ON_ENTRY
|
|
@@ -259,12 +242,13 @@ export abstract class AbstractCodegen<NetworkType, ModuleTypes, StructType> {
|
|
|
259
242
|
|
|
260
243
|
// TODO how to deal with callArgs
|
|
261
244
|
return `
|
|
262
|
-
${this.generateModuleExtra(module, allEventStructs
|
|
245
|
+
${this.generateModuleExtra(module, allEventStructs)}
|
|
263
246
|
|
|
264
247
|
export namespace ${moduleName} {
|
|
265
248
|
${structs.join('\n')}
|
|
266
249
|
|
|
267
250
|
${client}
|
|
251
|
+
${this.generateBuilder(module)}
|
|
268
252
|
}
|
|
269
253
|
`
|
|
270
254
|
}
|
|
@@ -361,6 +345,20 @@ export abstract class AbstractCodegen<NetworkType, ModuleTypes, StructType> {
|
|
|
361
345
|
return genericString
|
|
362
346
|
}
|
|
363
347
|
|
|
348
|
+
// generateTypeParameters(struct: TypeDescriptor, useAny = false) {
|
|
349
|
+
// let genericString = ''
|
|
350
|
+
//
|
|
351
|
+
// if (struct.typeArgs && struct.typeArgs.length > 0) {
|
|
352
|
+
// const params = struct.typeArgs
|
|
353
|
+
// .map((_, idx) => {
|
|
354
|
+
// return useAny ? 'any' : 'T' + idx
|
|
355
|
+
// })
|
|
356
|
+
// .join(',')
|
|
357
|
+
// genericString = `<${params}>`
|
|
358
|
+
// }
|
|
359
|
+
// return genericString
|
|
360
|
+
// }
|
|
361
|
+
|
|
364
362
|
generateCallArgsStructs(
|
|
365
363
|
module: InternalMoveModule,
|
|
366
364
|
func: InternalMoveFunction
|
|
@@ -445,7 +443,7 @@ export abstract class AbstractCodegen<NetworkType, ModuleTypes, StructType> {
|
|
|
445
443
|
currentAddress: string
|
|
446
444
|
): string {
|
|
447
445
|
if (type.reference) {
|
|
448
|
-
return this.
|
|
446
|
+
return this.REFERENCE_TYPE
|
|
449
447
|
}
|
|
450
448
|
|
|
451
449
|
switch (type.qname) {
|
|
@@ -531,21 +529,33 @@ export abstract class AbstractCodegen<NetworkType, ModuleTypes, StructType> {
|
|
|
531
529
|
}
|
|
532
530
|
return '_' + parts.join('.')
|
|
533
531
|
}
|
|
532
|
+
|
|
533
|
+
generateImports() {
|
|
534
|
+
const imports = `
|
|
535
|
+
import { TypeDescriptor, ANY_TYPE } from "@typemove/move"
|
|
536
|
+
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()}"
|
|
541
|
+
`
|
|
542
|
+
return imports
|
|
543
|
+
}
|
|
534
544
|
}
|
|
535
545
|
|
|
536
|
-
export class AccountCodegen<
|
|
546
|
+
export class AccountCodegen<ModuleType, StructType> {
|
|
537
547
|
modules: InternalMoveModule[]
|
|
538
|
-
config: Config
|
|
548
|
+
config: Config
|
|
539
549
|
abi: ModuleType[]
|
|
540
550
|
loader: AccountRegister
|
|
541
|
-
moduleGen: AbstractCodegen<
|
|
551
|
+
moduleGen: AbstractCodegen<ModuleType, StructType>
|
|
542
552
|
|
|
543
553
|
constructor(
|
|
544
|
-
moduleGen: AbstractCodegen<
|
|
554
|
+
moduleGen: AbstractCodegen<ModuleType, StructType>,
|
|
545
555
|
loader: AccountRegister,
|
|
546
556
|
abi: ModuleType[],
|
|
547
557
|
modules: InternalMoveModule[],
|
|
548
|
-
config: Config
|
|
558
|
+
config: Config
|
|
549
559
|
) {
|
|
550
560
|
// const json = fs.readFileSync(config.srcFile, 'utf-8')
|
|
551
561
|
this.moduleGen = moduleGen
|
|
@@ -584,7 +594,8 @@ export class AccountCodegen<NetworkType, ModuleType, StructType> {
|
|
|
584
594
|
if (isFrameworkAccount(account) && !isFrameworkAccount(address)) {
|
|
585
595
|
// Decide where to find runtime library
|
|
586
596
|
moduleImports.push(
|
|
587
|
-
`import { _${account} } from "@typemove/${this.moduleGen.PREFIX.toLowerCase()}"`
|
|
597
|
+
`import { _${account} } from "@typemove/${this.moduleGen.PREFIX.toLowerCase()}/builtin"`
|
|
598
|
+
// `import _${account} = builtin._${account} `
|
|
588
599
|
)
|
|
589
600
|
} else {
|
|
590
601
|
moduleImports.push(
|
|
@@ -596,14 +607,11 @@ export class AccountCodegen<NetworkType, ModuleType, StructType> {
|
|
|
596
607
|
}
|
|
597
608
|
}
|
|
598
609
|
|
|
599
|
-
let loadAllTypes = `loadAllTypes(defaultMoveCoder(
|
|
600
|
-
this.moduleGen.PREFIX
|
|
601
|
-
}Network.${this.moduleGen.generateNetworkOption(this.config.network)}))`
|
|
610
|
+
let loadAllTypes = `loadAllTypes(defaultMoveCoder())`
|
|
602
611
|
|
|
603
612
|
if (this.moduleGen.SYSTEM_MODULES.has(address)) {
|
|
604
613
|
loadAllTypes = `
|
|
605
|
-
loadAllTypes(defaultMoveCoder(
|
|
606
|
-
loadAllTypes(defaultMoveCoder(${this.moduleGen.PREFIX}Network.TEST_NET))
|
|
614
|
+
loadAllTypes(defaultMoveCoder())
|
|
607
615
|
`
|
|
608
616
|
}
|
|
609
617
|
|
|
@@ -617,14 +625,12 @@ export class AccountCodegen<NetworkType, ModuleType, StructType> {
|
|
|
617
625
|
|
|
618
626
|
/* Generated modules for account ${address} */
|
|
619
627
|
|
|
620
|
-
${this.generateImports()}
|
|
628
|
+
${this.moduleGen.generateImports()}
|
|
621
629
|
|
|
622
630
|
${moduleImports.join('\n')}
|
|
623
631
|
|
|
624
632
|
${this.modules
|
|
625
|
-
.map((m) =>
|
|
626
|
-
this.moduleGen.generateModule(m, eventsMap, this.config.network)
|
|
627
|
-
)
|
|
633
|
+
.map((m) => this.moduleGen.generateModule(m, eventsMap))
|
|
628
634
|
.join('\n')}
|
|
629
635
|
|
|
630
636
|
const MODULES = JSON.parse('${JSON.stringify(this.abi)}')
|
|
@@ -646,19 +652,4 @@ export class AccountCodegen<NetworkType, ModuleType, StructType> {
|
|
|
646
652
|
},
|
|
647
653
|
]
|
|
648
654
|
}
|
|
649
|
-
|
|
650
|
-
generateImports() {
|
|
651
|
-
const imports = `
|
|
652
|
-
import { TypeDescriptor, ANY_TYPE } from "@typemove/move"
|
|
653
|
-
import {
|
|
654
|
-
MoveCoder, defaultMoveCoder, TypedEventInstance, ${
|
|
655
|
-
this.moduleGen.PREFIX
|
|
656
|
-
}Network } from "@typemove/${this.moduleGen.PREFIX.toLowerCase()}"
|
|
657
|
-
import { ${
|
|
658
|
-
this.moduleGen.ADDRESS_TYPE
|
|
659
|
-
}, ModuleClient } from "@typemove/${this.moduleGen.PREFIX.toLowerCase()}"
|
|
660
|
-
`
|
|
661
|
-
|
|
662
|
-
return imports
|
|
663
|
-
}
|
|
664
655
|
}
|
|
@@ -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<
|
|
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:
|
|
26
|
-
adapter: ChainAdapter<
|
|
25
|
+
// network: string
|
|
26
|
+
adapter: ChainAdapter<ModuleType, StructType>
|
|
27
27
|
|
|
28
|
-
protected constructor(
|
|
29
|
-
this.
|
|
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
|
-
|
|
94
|
-
|
|
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
|
-
|
|
123
|
-
|
|
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
|
package/src/chain-adapter.ts
CHANGED
|
@@ -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<
|
|
5
|
-
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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/types.ts
CHANGED
|
@@ -88,7 +88,7 @@ export class TypeDescriptor<T = any> {
|
|
|
88
88
|
return []
|
|
89
89
|
}
|
|
90
90
|
|
|
91
|
-
if (this.
|
|
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 {
|