diff --git a/.changeset/null-fallback-parameters.md b/.changeset/null-fallback-parameters.md new file mode 100644 index 000000000..a7d4e4316 --- /dev/null +++ b/.changeset/null-fallback-parameters.md @@ -0,0 +1,5 @@ +--- +"@graphprotocol/graph-cli": patch +--- + +Handle explorer ABIs with null input/output arrays on nameless fallback functions during code generation. diff --git a/packages/cli/src/protocols/ethereum/abi.test.ts b/packages/cli/src/protocols/ethereum/abi.test.ts new file mode 100644 index 000000000..81a59e7ef --- /dev/null +++ b/packages/cli/src/protocols/ethereum/abi.test.ts @@ -0,0 +1,45 @@ +import os from 'node:os'; +import path from 'node:path'; +import fs from 'fs-extra'; +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; +import ABI from './abi.js'; + +describe('ABI fallback parameters', () => { + let dir: string; + beforeEach(async () => { + dir = await fs.mkdtemp(path.join(os.tmpdir(), 'graph-abi-')); + }); + afterEach(async () => { + await fs.remove(dir); + }); + + it.each([null, undefined, []])( + 'generates a nameless fallback with parameters %s', + async parameters => { + const file = path.join(dir, 'abi.json'); + await fs.writeJson(file, [ + { + type: 'fallback', + name: '', + inputs: parameters, + outputs: parameters, + stateMutability: 'payable', + }, + { + type: 'function', + name: 'withdraw', + inputs: [{ name: 'wad', type: 'uint256' }], + outputs: [], + stateMutability: 'nonpayable', + }, + ]); + const abi = ABI.load('Contract', file); + expect(() => abi.callFunctionSignatures()).not.toThrow(); + const types = await abi.codeGenerator().generateTypes(); + const code = types.map(type => type.toString()).join('\n'); + expect(code).toContain('class DefaultCall'); + expect(code).toContain('class WithdrawCall'); + expect(code).toContain('get wad()'); + }, + ); +}); diff --git a/packages/cli/src/protocols/ethereum/abi.ts b/packages/cli/src/protocols/ethereum/abi.ts index bd77fe4de..b74fc6f5d 100644 --- a/packages/cli/src/protocols/ethereum/abi.ts +++ b/packages/cli/src/protocols/ethereum/abi.ts @@ -154,6 +154,12 @@ export default class ABI { throw Error(`No valid ABI in file: ${path.relative(process.cwd(), file)}`); } - return new ABI(name, file, immutable.fromJS(abi)); + // Some explorer ABIs encode the parameterless fallback with null arrays. + const entries = abi.map((entry: any) => + entry.type === 'fallback' + ? { ...entry, inputs: entry.inputs ?? [], outputs: entry.outputs ?? [] } + : entry, + ); + return new ABI(name, file, immutable.fromJS(entries)); } }