Skip to content

Commit

Permalink
chore(fuel): assign each logId as const variable (#981)
Browse files Browse the repository at this point in the history
  • Loading branch information
spacedragon authored Sep 25, 2024
1 parent 9aa4383 commit b7227ca
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions packages/sdk/src/fuel/codegen/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ ${
}
}
type LogIdFilter<T> = T | T[]
${getLogConstants(logByTypes)}
export class ${name}Processor extends FuelAbstractProcessor<${name}> {
static bind(options: Omit<FuelProcessorConfig, 'abi'>) {
if (!options.name) {
Expand Down Expand Up @@ -234,11 +237,33 @@ function collectImportedTypes(types: any[]): string[] {
return Array.from(ret)
}

function getLogConstants(logByTypes: Record<string, string[]>) {
return Object.entries(logByTypes)
.map(([t, ids]) => {
const name = getTypeName(t)
if (ids.length == 1) {
return `const Log${name}Id = "${ids[0]}"`
}
return ids.map((id, idx) => `const Log${name}Id${idx} = "${id}"`).join('\n')
})
.join('\n')
}

function genOnLogFunction(contractName: string, [type, ids]: [string, string[]]) {
const name = getTypeName(type)

if (ids.length == 1) {
return `
onLog${name}(handler: (log: FuelLog<${type}>, ctx: FuelContractContext<${contractName}>) => void | Promise<void>) {
return super.onLog<${type}>([Log${name}Id], (log, ctx) => handler(log, ctx))
}`
}
const logIdFilterValues = ids.map((_, idx) => `Log${name}Id${idx}`)

return `
onLog${name}(handler: (log: FuelLog<${type}>, ctx: FuelContractContext<${contractName}>) => void | Promise<void>, logIdFilter?: string | string[]) {
return super.onLog<${type}>(logIdFilter ?? [${ids.map((id) => `"${id}"`).join(', ')}], (log, ctx) => handler(log, ctx))
onLog${name}(handler: (log: FuelLog<${type}>, ctx: FuelContractContext<${contractName}>) => void | Promise<void>,
logIdFilter?: LogIdFilter<${logIdFilterValues.map((d) => `typeof ${d}`).join(' | ')}> ) {
return super.onLog<${type}>(logIdFilter ?? [${logIdFilterValues.join(', ')}], (log, ctx) => handler(log, ctx))
}`
}

Expand Down

0 comments on commit b7227ca

Please sign in to comment.