@typemove/move 1.0.0-rc.5 → 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/package.json +1 -1
- package/src/abstract-codegen.ts +55 -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
|
}
|
|
@@ -445,7 +429,7 @@ export abstract class AbstractCodegen<NetworkType, ModuleTypes, StructType> {
|
|
|
445
429
|
currentAddress: string
|
|
446
430
|
): string {
|
|
447
431
|
if (type.reference) {
|
|
448
|
-
return this.
|
|
432
|
+
return this.REFERENCE_TYPE
|
|
449
433
|
}
|
|
450
434
|
|
|
451
435
|
switch (type.qname) {
|
|
@@ -531,21 +515,33 @@ export abstract class AbstractCodegen<NetworkType, ModuleTypes, StructType> {
|
|
|
531
515
|
}
|
|
532
516
|
return '_' + parts.join('.')
|
|
533
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
|
+
}
|
|
534
530
|
}
|
|
535
531
|
|
|
536
|
-
export class AccountCodegen<
|
|
532
|
+
export class AccountCodegen<ModuleType, StructType> {
|
|
537
533
|
modules: InternalMoveModule[]
|
|
538
|
-
config: Config
|
|
534
|
+
config: Config
|
|
539
535
|
abi: ModuleType[]
|
|
540
536
|
loader: AccountRegister
|
|
541
|
-
moduleGen: AbstractCodegen<
|
|
537
|
+
moduleGen: AbstractCodegen<ModuleType, StructType>
|
|
542
538
|
|
|
543
539
|
constructor(
|
|
544
|
-
moduleGen: AbstractCodegen<
|
|
540
|
+
moduleGen: AbstractCodegen<ModuleType, StructType>,
|
|
545
541
|
loader: AccountRegister,
|
|
546
542
|
abi: ModuleType[],
|
|
547
543
|
modules: InternalMoveModule[],
|
|
548
|
-
config: Config
|
|
544
|
+
config: Config
|
|
549
545
|
) {
|
|
550
546
|
// const json = fs.readFileSync(config.srcFile, 'utf-8')
|
|
551
547
|
this.moduleGen = moduleGen
|
|
@@ -584,7 +580,8 @@ export class AccountCodegen<NetworkType, ModuleType, StructType> {
|
|
|
584
580
|
if (isFrameworkAccount(account) && !isFrameworkAccount(address)) {
|
|
585
581
|
// Decide where to find runtime library
|
|
586
582
|
moduleImports.push(
|
|
587
|
-
`import { _${account} } from "@typemove/${this.moduleGen.PREFIX.toLowerCase()}"`
|
|
583
|
+
`import { _${account} } from "@typemove/${this.moduleGen.PREFIX.toLowerCase()}/builtin"`
|
|
584
|
+
// `import _${account} = builtin._${account} `
|
|
588
585
|
)
|
|
589
586
|
} else {
|
|
590
587
|
moduleImports.push(
|
|
@@ -596,14 +593,11 @@ export class AccountCodegen<NetworkType, ModuleType, StructType> {
|
|
|
596
593
|
}
|
|
597
594
|
}
|
|
598
595
|
|
|
599
|
-
let loadAllTypes = `loadAllTypes(defaultMoveCoder(
|
|
600
|
-
this.moduleGen.PREFIX
|
|
601
|
-
}Network.${this.moduleGen.generateNetworkOption(this.config.network)}))`
|
|
596
|
+
let loadAllTypes = `loadAllTypes(defaultMoveCoder())`
|
|
602
597
|
|
|
603
598
|
if (this.moduleGen.SYSTEM_MODULES.has(address)) {
|
|
604
599
|
loadAllTypes = `
|
|
605
|
-
loadAllTypes(defaultMoveCoder(
|
|
606
|
-
loadAllTypes(defaultMoveCoder(${this.moduleGen.PREFIX}Network.TEST_NET))
|
|
600
|
+
loadAllTypes(defaultMoveCoder())
|
|
607
601
|
`
|
|
608
602
|
}
|
|
609
603
|
|
|
@@ -617,14 +611,12 @@ export class AccountCodegen<NetworkType, ModuleType, StructType> {
|
|
|
617
611
|
|
|
618
612
|
/* Generated modules for account ${address} */
|
|
619
613
|
|
|
620
|
-
${this.generateImports()}
|
|
614
|
+
${this.moduleGen.generateImports()}
|
|
621
615
|
|
|
622
616
|
${moduleImports.join('\n')}
|
|
623
617
|
|
|
624
618
|
${this.modules
|
|
625
|
-
.map((m) =>
|
|
626
|
-
this.moduleGen.generateModule(m, eventsMap, this.config.network)
|
|
627
|
-
)
|
|
619
|
+
.map((m) => this.moduleGen.generateModule(m, eventsMap))
|
|
628
620
|
.join('\n')}
|
|
629
621
|
|
|
630
622
|
const MODULES = JSON.parse('${JSON.stringify(this.abi)}')
|
|
@@ -646,19 +638,4 @@ export class AccountCodegen<NetworkType, ModuleType, StructType> {
|
|
|
646
638
|
},
|
|
647
639
|
]
|
|
648
640
|
}
|
|
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
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<
|
|
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 {
|