From e1d91f9f3c0d9f9e92d9b08946b536bbd13ec5dd Mon Sep 17 00:00:00 2001 From: Etienne Donneger Date: Wed, 30 Oct 2024 17:17:47 -0400 Subject: [PATCH] Fix `graph add` Etherscan lookups when using `localhost` network The command will now skip Etherscan lookups if detecting the network as `localhost`. User will be prompted to enter all relevant information. Closes #1748 --- .changeset/cold-snails-bake.md | 5 ++++ packages/cli/src/commands/add.ts | 41 +++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 .changeset/cold-snails-bake.md diff --git a/.changeset/cold-snails-bake.md b/.changeset/cold-snails-bake.md new file mode 100644 index 000000000..e8e22abb7 --- /dev/null +++ b/.changeset/cold-snails-bake.md @@ -0,0 +1,5 @@ +--- +'@graphprotocol/graph-cli': patch +--- + +Using `graph add` with `localhost` network now prompts the user for input diff --git a/packages/cli/src/commands/add.ts b/packages/cli/src/commands/add.ts index 2bbd5f9cd..86420c124 100644 --- a/packages/cli/src/commands/add.ts +++ b/packages/cli/src/commands/add.ts @@ -77,6 +77,9 @@ export default class AddCommand extends Command { const manifest = await Subgraph.load(manifestPath, { protocol }); const network = manifest.result.getIn(['dataSources', 0, 'network']) as any; const result = manifest.result.asMutable(); + const isLocalHost = network === 'localhost'; // This flag prevent Etherscan lookups in case the network selected is `localhost` + + if (isLocalHost) this.warn('`localhost` network detected, prompting user for inputs'); let startBlock = startBlockFlag; let contractName = contractNameFlag; @@ -96,10 +99,44 @@ export default class AddCommand extends Command { } else if (network === 'poa-core') { ethabi = await loadAbiFromBlockScout(EthereumABI, network, address); } else { - ethabi = await loadAbiFromEtherscan(EthereumABI, network, address); + try { + if (isLocalHost) throw Error; // Triggers user prompting without waiting for Etherscan lookup to fail + + ethabi = await loadAbiFromEtherscan(EthereumABI, network, address); + } catch (error) { + // we cannot ask user to do prompt in test environment + if (process.env.NODE_ENV !== 'test') { + const { abi: abiFromFile } = await prompt.ask<{ abi: EthereumABI }>([ + { + type: 'input', + name: 'abi', + message: 'ABI file (path)', + initial: ethabi, + validate: async (value: string) => { + try { + EthereumABI.load(contractName, value); + return true; + } catch (e) { + this.error(e.message); + } + }, + result: async (value: string) => { + try { + return EthereumABI.load(contractName, value); + } catch (e) { + return e.message; + } + }, + }, + ]); + ethabi = abiFromFile; + } + } } try { + if (isLocalHost) throw Error; // Triggers user prompting without waiting for Etherscan lookup to fail + startBlock ||= Number(await loadStartBlockForContract(network, address)).toString(); } catch (error) { // we cannot ask user to do prompt in test environment @@ -122,6 +159,8 @@ export default class AddCommand extends Command { } try { + if (isLocalHost) throw Error; // Triggers user prompting without waiting for Etherscan lookup to fail + contractName = await loadContractNameForAddress(network, address); } catch (error) { // not asking user to do prompt in test environment