Skip to content

Commit

Permalink
chore(sui): view function can only take pure types (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
zfy0701 authored Nov 12, 2024
1 parent 4a1c573 commit f230826
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions packages/sui/src/codegen/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,27 +121,33 @@ export class SuiCodegen extends AbstractCodegen<
`
}

private generateArgs(module: InternalMoveModule, func: InternalMoveFunction) {
private generateArgs(module: InternalMoveModule, func: InternalMoveFunction, isView: boolean) {
const args = []
const argsLen = func.params.length
for (const [idx, arg] of func.params.entries()) {
if (idx === argsLen - 1 && arg.qname === '0x2::tx_context::TxContext') {
// no op
} else if (arg.reference) {
args.push({
paramType: `${this.ADDRESS_TYPE} | TransactionObjectArgument | TransactionArgument`,
paramType: isView
? this.ADDRESS_TYPE
: `${this.ADDRESS_TYPE} | TransactionObjectArgument | TransactionArgument`,
callValue: `_args.push(transactionArgumentOrObject(args[${idx}], tx))`
})
} else if (arg.isVector()) {
// TODO fix pure vector
args.push({
paramType: `(${this.ADDRESS_TYPE} | TransactionObjectArgument)[] | TransactionArgument`,
paramType: isView
? `${this.ADDRESS_TYPE}[]`
: `(${this.ADDRESS_TYPE} | TransactionObjectArgument)[] | TransactionArgument`,
callValue: `_args.push(transactionArgumentOrVec(args[${idx}], tx))`
})
} else {
// Handle pure type
let pureFunction = ''
const paramType = `${this.generateTypeForDescriptor(arg, module.address)} | TransactionArgument`
const paramType = isView
? this.generateTypeForDescriptor(arg, module.address)
: `${this.generateTypeForDescriptor(arg, module.address)} | TransactionArgument`

switch (arg.qname.toLowerCase()) {
case 'u8':
Expand Down Expand Up @@ -200,7 +206,7 @@ export class SuiCodegen extends AbstractCodegen<
})
.join(',')

const args = this.generateArgs(module, func)
const args = this.generateArgs(module, func, true)
const returnType = `${this.generateFunctionReturnTypeParameters(func, module.address)}`

return `export async function ${camel(normalizeToJSName(func.name))}${genericString}(
Expand Down Expand Up @@ -229,7 +235,7 @@ export class SuiCodegen extends AbstractCodegen<
return ''
}

const args = this.generateArgs(module, func)
const args = this.generateArgs(module, func, false)

const genericString = this.generateFunctionTypeParameters(func)

Expand Down

0 comments on commit f230826

Please sign in to comment.