Skip to content

Commit

Permalink
fix(sdk-aptos): fix codegen for js reserved keywords (#320)
Browse files Browse the repository at this point in the history
  • Loading branch information
zfy0701 authored Feb 2, 2023
1 parent 911db5d commit 1680df8
Show file tree
Hide file tree
Showing 5 changed files with 436 additions and 14 deletions.
1 change: 1 addition & 0 deletions .github/workflows/publish-sdk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
branches:
- main
- release
- v1
jobs:
release:
runs-on: ubuntu-latest
Expand Down
33 changes: 20 additions & 13 deletions packages/sdk-aptos/src/codegen/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as fs from 'fs'
import * as path from 'path'
import { format } from 'prettier'
import { MoveFunction, MoveModule, MoveModuleBytecode, MoveStruct } from '../move-types'
import { AccountModulesImportInfo, AccountRegister, generateType } from './typegen'
import { AccountModulesImportInfo, AccountRegister, generateType, normalizeToJSName } from './typegen'
import { getMeaningfulFunctionParams, isFrameworkAccount, moduleQname, SPLITTER } from '../utils'
import chalk from 'chalk'
import { AptosNetwork, getAptosChainName, getRpcClient } from '../network'
Expand Down Expand Up @@ -192,7 +192,7 @@ export class AccountCodegen {
${this.modules
.map((m) => {
return `_r.load(${m.abi?.name}.ABI)`
return `_r.load(${normalizeToJSName(m.abi!.name)}.ABI)`
})
.join('\n')}
}
Expand Down Expand Up @@ -231,9 +231,10 @@ function generateModule(moduleByteCode: MoveModuleBytecode, network: AptosNetwor
const structs = module.structs.map((s) => generateStructs(module, s, eventTypes))
const callArgs = module.exposed_functions.map((f) => generateCallArgsStructs(module, f))

const moduleName = normalizeToJSName(module.name)
let processor = ''
if (functions.length > 0 || events.length > 0) {
processor = `export class ${module.name} extends AptosBaseProcessor {
processor = `export class ${moduleName} extends AptosBaseProcessor {
constructor(options: AptosBindOptions) {
super("${module.name}", options)
Expand All @@ -243,8 +244,8 @@ function generateModule(moduleByteCode: MoveModuleBytecode, network: AptosNetwor
network: AptosNetwork.${generateNetworkOption(network)}
}
static bind(options: Partial<AptosBindOptions> = {}): ${module.name} {
return new ${module.name}({ ...${module.name}.DEFAULT_OPTIONS, ...options })
static bind(options: Partial<AptosBindOptions> = {}): ${moduleName} {
return new ${moduleName}({ ...${moduleName}.DEFAULT_OPTIONS, ...options })
}
${functions.join('\n')}
Expand All @@ -261,7 +262,7 @@ function generateModule(moduleByteCode: MoveModuleBytecode, network: AptosNetwor
return `
${processor}
export namespace ${module.name} {
export namespace ${moduleName} {
${structs.join('\n')}
${callArgs.join('\n')}
Expand All @@ -278,25 +279,27 @@ function generateStructs(module: MoveModule, struct: MoveStruct, events: Set<str
const genericString = generateStructTypeParameters(struct)
const genericStringAny = generateStructTypeParameters(struct, true)

const structName = normalizeToJSName(struct.name)

const fields = struct.fields.map((field) => {
return `${field.name}: ${generateType(field.type, module.address)}`
})

let eventPayload = ''
if (events.has(moduleQname(module) + SPLITTER + struct.name)) {
eventPayload = `
export interface ${struct.name}Instance extends
TypedEventInstance<${struct.name}${genericStringAny}> {
export interface ${structName}Instance extends
TypedEventInstance<${structName}${genericStringAny}> {
/** @deprecated use {@link data_decoded} instead */
data_typed: ${struct.name}${genericStringAny}
data_decoded: ${struct.name}${genericStringAny}
data_typed: ${structName}${genericStringAny}
data_decoded: ${structName}${genericStringAny}
type_arguments: [${struct.generic_type_params.map((_) => 'string').join(', ')}]
}
`
}

return `
export class ${struct.name}${genericString} {
export class ${structName}${genericString} {
static TYPE_QNAME = '${module.address}::${module.name}::${struct.name}'
${fields.join('\n')}
}
Expand Down Expand Up @@ -361,10 +364,11 @@ function generateOnEntryFunctions(module: MoveModule, func: MoveFunction) {
}

// const genericString = generateFunctionTypeParameters(func)
const moduleName = normalizeToJSName(module.name)

const camelFuncName = capitalizeFirstChar(camelize(func.name))
const source = `
onEntry${camelFuncName}(func: (call: ${module.name}.${camelFuncName}Payload, ctx: AptosContext) => void, filter?: CallFilter, fetchConfig?: AptosFetchConfig): ${module.name} {
onEntry${camelFuncName}(func: (call: ${moduleName}.${camelFuncName}Payload, ctx: AptosContext) => void, filter?: CallFilter, fetchConfig?: AptosFetchConfig): ${moduleName} {
this.onEntryFunctionCall(func, {
...filter,
function: '${module.name}::${func.name}'
Expand Down Expand Up @@ -409,8 +413,11 @@ function generateOnEvents(module: MoveModule, struct: MoveStruct): string {

// const genericString = generateStructTypeParameters(struct)

const moduleName = normalizeToJSName(module.name)
const source = `
onEvent${struct.name}(func: (event: ${module.name}.${struct.name}Instance, ctx: AptosContext) => void, fetchConfig?: AptosFetchConfig): ${module.name} {
onEvent${struct.name}(func: (event: ${moduleName}.${normalizeToJSName(
struct.name
)}Instance, ctx: AptosContext) => void, fetchConfig?: AptosFetchConfig): ${moduleName} {
this.onEvent(func, {
type: '${module.name}::${struct.name}'
},
Expand Down
12 changes: 12 additions & 0 deletions packages/sdk-aptos/src/codegen/typegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ function generateTypeForDescriptor(type: TypeDescriptor, currentAddress: string)

function generateSimpleType(type: string, currentAddress: string): string {
const parts = type.split(SPLITTER)

for (let i = 0; i < parts.length; i++) {
parts[i] = normalizeToJSName(parts[i])
}

if (parts.length < 2) {
return parts[0]
}
Expand Down Expand Up @@ -151,3 +156,10 @@ export class AccountRegister {
return accountModuleImports
}
}

export function normalizeToJSName(name: string) {
if (name === 'package' || name === 'volatile') {
return name + '_'
}
return name
}
Loading

0 comments on commit 1680df8

Please sign in to comment.