@typemove/aptos 1.0.0-rc.13 → 1.0.0-rc.15

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.
@@ -3,7 +3,7 @@ import { Event, MoveModuleBytecode, MoveResource } from '../move-types.js'
3
3
  import chalk from 'chalk'
4
4
  import { join } from 'path'
5
5
  import { AptosChainAdapter } from '../aptos-chain-adapter.js'
6
- import { AbstractCodegen } from '@typemove/move'
6
+ import { AbstractCodegen, camel, InternalMoveFunction, InternalMoveModule, normalizeToJSName } from '@typemove/move'
7
7
 
8
8
  export async function codegen(
9
9
  abisDir: string,
@@ -20,10 +20,7 @@ export async function codegen(
20
20
  console.log(chalk.green(`Generated ${numFiles} for Aptos`))
21
21
  }
22
22
 
23
- class AptosCodegen extends AbstractCodegen<
24
- MoveModuleBytecode,
25
- Event | MoveResource
26
- > {
23
+ class AptosCodegen extends AbstractCodegen<MoveModuleBytecode, Event | MoveResource> {
27
24
  ADDRESS_TYPE = 'Address'
28
25
  REFERENCE_TYPE = 'Address'
29
26
  PREFIX = 'Aptos'
@@ -33,10 +30,62 @@ class AptosCodegen extends AbstractCodegen<
33
30
  super(new AptosChainAdapter(endpoint))
34
31
  }
35
32
 
36
- // generateImports(): string {
37
- // return `
38
- // ${super.generateImports()}
39
- // import { Address } from '@typemove/aptos'
40
- // `
41
- // }
33
+ generateImports(): string {
34
+ return `
35
+ ${super.generateImports()}
36
+ import { AptosClient } from 'aptos'
37
+ `
38
+ }
39
+ protected generateExtra(module: InternalMoveModule) {
40
+ // const funcs = module.exposedFunctions.map((f) =>
41
+ // this.generateBuilderForFunction(module, f)
42
+ // )
43
+
44
+ const viewFuncs = module.exposedFunctions.map((f) => this.generateViewFunction(module, f))
45
+
46
+ // export namespace builder {
47
+ // ${funcs.join('\n')}
48
+ // }
49
+
50
+ return `
51
+ export namespace view {
52
+ ${viewFuncs.join('\n')}
53
+ }
54
+ `
55
+ }
56
+
57
+ protected generateViewFunction(module: InternalMoveModule, func: InternalMoveFunction): string {
58
+ if (!func.isView) {
59
+ return ''
60
+ }
61
+ const genericString = this.generateFunctionTypeParameters(func)
62
+ const fields = this.chainAdapter.getMeaningfulFunctionParams(func.params).map((param) => {
63
+ return this.generateTypeForDescriptor(param, module.address)
64
+ })
65
+
66
+ const returns = func.return.map((param) => {
67
+ return this.generateTypeForDescriptor(param, module.address)
68
+ })
69
+
70
+ const typeParamArg = func.typeParams
71
+ .map((v, idx) => {
72
+ return `TypeDescriptor<T${idx}> | string`
73
+ })
74
+ .join(',')
75
+
76
+ // const args = this.generateArgs(module, func)
77
+
78
+ return `export async function ${camel(normalizeToJSName(func.name))}${genericString}(
79
+ client: AptosClient,
80
+ request: {
81
+ type_arguments: [${func.typeParams.map((_) => 'string').join(', ')}],
82
+ arguments: [${fields.join(',')}]},
83
+ version?: bigint): Promise<[${returns.join(',')}]> {
84
+ const data = { ...request, function: "${module.address}::${module.name}::${func.name}", }
85
+ const res = await client.view(data, version?.toString())
86
+ const coder = defaultMoveCoder(client.nodeUrl)
87
+ const type = await coder.getMoveFunction("${module.address}::${module.name}::${func.name}")
88
+ return await coder.decodeArray(res, type.return) as any
89
+ }`
90
+ }
42
91
  }
@@ -7,16 +7,9 @@ import {
7
7
  parseMoveType,
8
8
  } from '@typemove/move'
9
9
  import { Types } from 'aptos'
10
- import {
11
- MoveFunction,
12
- MoveModuleBytecode,
13
- MoveStruct,
14
- MoveStructField,
15
- } from './move-types'
10
+ import { MoveFunction, MoveModuleBytecode, MoveStruct, MoveStructField } from './move-types'
16
11
 
17
- export function toInternalModule(
18
- module: MoveModuleBytecode
19
- ): InternalMoveModule {
12
+ export function toInternalModule(module: MoveModuleBytecode): InternalMoveModule {
20
13
  if (!module.abi) {
21
14
  throw Error('module with no ABI found')
22
15
  }
@@ -45,6 +38,7 @@ export function toInternalFunction(func: MoveFunction): InternalMoveFunction {
45
38
  return {
46
39
  typeParams: func.generic_type_params,
47
40
  isEntry: func.is_entry,
41
+ isView: func.is_view,
48
42
  name: func.name,
49
43
  params: func.params.map(parseMoveType),
50
44
  return: func.return.map(parseMoveType),
@@ -62,9 +56,7 @@ export function toInternalStruct(struct: MoveStruct): InternalMoveStruct {
62
56
  }
63
57
  }
64
58
 
65
- export function toInternalField(
66
- module: MoveStructField
67
- ): InternalMoveStructField {
59
+ export function toInternalField(module: MoveStructField): InternalMoveStructField {
68
60
  return {
69
61
  name: module.name,
70
62
  type: parseMoveType(module.type),