Skip to content

Commit 0ba99dc

Browse files
authored
Fix graph add Etherscan lookups when using localhost network (#1751)
The command will now skip Etherscan lookups if detecting the network as `localhost`. User will be prompted to enter all relevant information. Closes #1748
1 parent e37ce8d commit 0ba99dc

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

.changeset/cold-snails-bake.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphprotocol/graph-cli': patch
3+
---
4+
5+
Using `graph add` with `localhost` network now prompts the user for input

packages/cli/src/commands/add.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ export default class AddCommand extends Command {
7777
const manifest = await Subgraph.load(manifestPath, { protocol });
7878
const network = manifest.result.getIn(['dataSources', 0, 'network']) as any;
7979
const result = manifest.result.asMutable();
80+
const isLocalHost = network === 'localhost'; // This flag prevent Etherscan lookups in case the network selected is `localhost`
81+
82+
if (isLocalHost) this.warn('`localhost` network detected, prompting user for inputs');
8083

8184
let startBlock = startBlockFlag;
8285
let contractName = contractNameFlag;
@@ -96,10 +99,44 @@ export default class AddCommand extends Command {
9699
} else if (network === 'poa-core') {
97100
ethabi = await loadAbiFromBlockScout(EthereumABI, network, address);
98101
} else {
99-
ethabi = await loadAbiFromEtherscan(EthereumABI, network, address);
102+
try {
103+
if (isLocalHost) throw Error; // Triggers user prompting without waiting for Etherscan lookup to fail
104+
105+
ethabi = await loadAbiFromEtherscan(EthereumABI, network, address);
106+
} catch (error) {
107+
// we cannot ask user to do prompt in test environment
108+
if (process.env.NODE_ENV !== 'test') {
109+
const { abi: abiFromFile } = await prompt.ask<{ abi: EthereumABI }>([
110+
{
111+
type: 'input',
112+
name: 'abi',
113+
message: 'ABI file (path)',
114+
initial: ethabi,
115+
validate: async (value: string) => {
116+
try {
117+
EthereumABI.load(contractName, value);
118+
return true;
119+
} catch (e) {
120+
this.error(e.message);
121+
}
122+
},
123+
result: async (value: string) => {
124+
try {
125+
return EthereumABI.load(contractName, value);
126+
} catch (e) {
127+
return e.message;
128+
}
129+
},
130+
},
131+
]);
132+
ethabi = abiFromFile;
133+
}
134+
}
100135
}
101136

102137
try {
138+
if (isLocalHost) throw Error; // Triggers user prompting without waiting for Etherscan lookup to fail
139+
103140
startBlock ||= Number(await loadStartBlockForContract(network, address)).toString();
104141
} catch (error) {
105142
// we cannot ask user to do prompt in test environment
@@ -122,6 +159,8 @@ export default class AddCommand extends Command {
122159
}
123160

124161
try {
162+
if (isLocalHost) throw Error; // Triggers user prompting without waiting for Etherscan lookup to fail
163+
125164
contractName = await loadContractNameForAddress(network, address);
126165
} catch (error) {
127166
// not asking user to do prompt in test environment

0 commit comments

Comments
 (0)