Skip to content

Commit

Permalink
Fix gen encode fields function
Browse files Browse the repository at this point in the history
  • Loading branch information
Lbqds committed Apr 26, 2024
1 parent 5d1b2d5 commit 872b9e0
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions packages/cli/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,16 +241,20 @@ function genSubscribeAllEvents(contract: Contract): string {
`
}

function genEncodeFieldsFunc(contract: Contract): string {
function getStructs(): string {
const hasStruct = Project.currentProject.structs.length > 0
return hasStruct ? 'AllStructs' : '[]'
}

function genEncodeFieldsFunc(contract: Contract): string {
const hasFields = contract.fieldsSig.names.length > 0
const params = hasFields ? `fields: ${contractTypes(contract.name)}.Fields` : ''
return `
encodeFields(${params}) {
return encodeContractFields(
${hasFields ? `addStdIdToFields(this.contract, fields)` : '{}'},
this.contract.fieldsSig,
${hasStruct ? 'AllStructs' : ''}
${getStructs()}
)
}
`
Expand Down Expand Up @@ -416,7 +420,6 @@ function importStructs(): string {

function genContract(contract: Contract, artifactRelativePath: string): string {
const fieldsSig = getContractFields(contract)
const hasStruct = Project.currentProject.structs.length > 0
const projectArtifact = Project.currentProject.projectArtifact
const contractInfo = projectArtifact.infos.get(contract.name)
if (contractInfo === undefined) {
Expand Down Expand Up @@ -459,7 +462,7 @@ function genContract(contract: Contract, artifactRelativePath: string): string {
${contract.name}ContractJson,
'${contractInfo.bytecodeDebugPatch}',
'${contractInfo.codeHashDebug}',
${hasStruct ? 'AllStructs' : []}
${getStructs()}
))
// Use this class to interact with the blockchain
Expand All @@ -480,14 +483,13 @@ function genContract(contract: Contract, artifactRelativePath: string): string {

function genScript(script: Script): string {
console.log(`Generating code for script ${script.name}`)
const hasStruct = Project.currentProject.structs.length > 0
const fieldsType = script.fieldsSig.names.length > 0 ? `{${formatParameters(script.fieldsSig)}}` : '{}'
return `
export const ${script.name} = new ExecutableScript<${fieldsType}>(
Script.fromJson(
${script.name}ScriptJson,
'',
${hasStruct ? 'AllStructs' : []}
${getStructs()}
)
)
`
Expand Down

0 comments on commit 872b9e0

Please sign in to comment.