Skip to content

Commit 326b303

Browse files
Add retries to etherscan calls (#1491)
* add retries to etherscan calls ref #1447 * fix prettier * add changeset
1 parent 3860fb5 commit 326b303

File tree

3 files changed

+41
-16
lines changed

3 files changed

+41
-16
lines changed

.changeset/honest-shoes-rhyme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphprotocol/graph-cli': minor
3+
---
4+
5+
add etherscan api retries to `graph init` wizard

packages/cli/src/command-helpers/abi.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ export const fetchContractCreationHashWithRetry = async (
7272
/* empty */
7373
}
7474
}
75-
throw new Error(`Failed to fetch contract creation transaction hash
76-
`);
75+
throw new Error(`Failed to fetch contract creation transaction hash`);
7776
};
7877

7978
export const fetchTransactionByHashFromRPC = async (

packages/cli/src/commands/init.ts

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from 'fs';
22
import os from 'os';
33
import path from 'path';
44
import { filesystem, prompt, system } from 'gluegun';
5+
import * as toolbox from 'gluegun';
56
import { Args, Command, Flags, ux } from '@oclif/core';
67
import {
78
loadAbiFromBlockScout,
@@ -395,6 +396,26 @@ async function processFromExampleInitForm(
395396
}
396397
}
397398

399+
async function retryWithPrompt<T>(func: () => Promise<T>): Promise<T | undefined> {
400+
for (;;) {
401+
try {
402+
return await func();
403+
} catch (_) {
404+
const { retry } = await toolbox.prompt.ask({
405+
type: 'confirm',
406+
name: 'retry',
407+
message: 'Do you want to retry?',
408+
initial: true,
409+
});
410+
411+
if (!retry) {
412+
break;
413+
}
414+
}
415+
}
416+
return undefined;
417+
}
418+
398419
async function processInitForm(
399420
this: InitCommand,
400421
{
@@ -585,24 +606,24 @@ async function processInitForm(
585606

586607
// Try loading the ABI from Etherscan, if none was provided
587608
if (protocolInstance.hasABIs() && !initAbi) {
588-
try {
589-
if (network === 'poa-core') {
590-
// TODO: this variable is never used anywhere, what happens?
591-
// abiFromBlockScout = await loadAbiFromBlockScout(ABI, network, value)
592-
} else {
593-
abiFromEtherscan = await loadAbiFromEtherscan(ABI, network, value);
594-
}
595-
} catch (e) {
596-
// noop
609+
if (network === 'poa-core') {
610+
abiFromEtherscan = await retryWithPrompt(() =>
611+
loadAbiFromBlockScout(ABI, network, value),
612+
);
613+
} else {
614+
abiFromEtherscan = await retryWithPrompt(() =>
615+
loadAbiFromEtherscan(ABI, network, value),
616+
);
597617
}
598618
}
599619
// If startBlock is not set, try to load it.
600620
if (!initStartBlock) {
601-
try {
602-
// Load startBlock for this contract
603-
initStartBlock = Number(await loadStartBlockForContract(network, value)).toString();
604-
} catch (error) {
605-
// noop
621+
// Load startBlock for this contract
622+
const startBlock = await retryWithPrompt(() =>
623+
loadStartBlockForContract(network, value),
624+
);
625+
if (startBlock) {
626+
initStartBlock = Number(startBlock).toString();
606627
}
607628
}
608629
return value;

0 commit comments

Comments
 (0)