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