Skip to content

Commit

Permalink
Improve ABI file path validation (#1766)
Browse files Browse the repository at this point in the history
- Error message now shows within CLI interactive session instead of
  error crash
- Passing `--abi` will correctly pre-fill the interactive form
- Catch JSON parsing exception for `EthereumABI`.

Closes: #1765
  • Loading branch information
0237h authored Nov 12, 2024
1 parent 1d4217a commit d2fda94
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/soft-scissors-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphprotocol/graph-cli': patch
---

Improve ABI file path validation
25 changes: 6 additions & 19 deletions packages/cli/src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -623,47 +623,34 @@ async function processInitForm(
type: 'input',
name: 'abi',
message: 'ABI file (path)',
initial: initAbi,
initial: initAbiPath,
skip: () =>
!protocolInstance.hasABIs() ||
initFromExample !== undefined ||
abiFromEtherscan !== undefined ||
isSubstreams ||
!!initAbiPath,
isSubstreams,
validate: async (value: string) => {
if (initFromExample || abiFromEtherscan || !protocolInstance.hasABIs()) {
return true;
}

const ABI = protocolInstance.getABI();
if (initAbiPath) {
try {
loadAbiFromFile(ABI, initAbiPath);
return true;
} catch (e) {
this.error(e.message);
}
}
if (initAbiPath) value = initAbiPath;

try {
loadAbiFromFile(ABI, value);
return true;
} catch (e) {
this.error(e.message);
return e.message;
}
},
result: async (value: string) => {
if (initFromExample || abiFromEtherscan || !protocolInstance.hasABIs()) {
return null;
}

const ABI = protocolInstance.getABI();
if (initAbiPath) {
try {
return loadAbiFromFile(ABI, initAbiPath);
} catch (e) {
return e.message;
}
}
if (initAbiPath) value = initAbiPath;

try {
return loadAbiFromFile(ABI, value);
Expand Down
8 changes: 7 additions & 1 deletion packages/cli/src/protocols/ethereum/abi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,13 @@ export default class ABI {
}

static load(name: string, file: string) {
const data = JSON.parse(fs.readFileSync(file).toString());
let data;
try {
data = JSON.parse(fs.readFileSync(file).toString());
} catch (e) {
throw Error(`Could not parse ABI: ${e}`);
}

const abi = ABI.normalized(data);

if (abi === null || abi === undefined) {
Expand Down

0 comments on commit d2fda94

Please sign in to comment.