Skip to content

Commit

Permalink
Add retries to etherscan calls (#1491)
Browse files Browse the repository at this point in the history
* add retries to etherscan calls
ref #1447

* fix prettier

* add changeset
  • Loading branch information
YaroShkvorets committed Nov 6, 2023
1 parent 3860fb5 commit 326b303
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/honest-shoes-rhyme.md
@@ -0,0 +1,5 @@
---
'@graphprotocol/graph-cli': minor
---

add etherscan api retries to `graph init` wizard
3 changes: 1 addition & 2 deletions packages/cli/src/command-helpers/abi.ts
Expand Up @@ -72,8 +72,7 @@ export const fetchContractCreationHashWithRetry = async (
/* empty */
}
}
throw new Error(`Failed to fetch contract creation transaction hash
`);
throw new Error(`Failed to fetch contract creation transaction hash`);
};

export const fetchTransactionByHashFromRPC = async (
Expand Down
49 changes: 35 additions & 14 deletions packages/cli/src/commands/init.ts
Expand Up @@ -2,6 +2,7 @@ import fs from 'fs';
import os from 'os';
import path from 'path';
import { filesystem, prompt, system } from 'gluegun';
import * as toolbox from 'gluegun';
import { Args, Command, Flags, ux } from '@oclif/core';
import {
loadAbiFromBlockScout,
Expand Down Expand Up @@ -395,6 +396,26 @@ async function processFromExampleInitForm(
}
}

async function retryWithPrompt<T>(func: () => Promise<T>): Promise<T | undefined> {
for (;;) {
try {
return await func();
} catch (_) {
const { retry } = await toolbox.prompt.ask({
type: 'confirm',
name: 'retry',
message: 'Do you want to retry?',
initial: true,
});

if (!retry) {
break;
}
}
}
return undefined;
}

async function processInitForm(
this: InitCommand,
{
Expand Down Expand Up @@ -585,24 +606,24 @@ async function processInitForm(

// Try loading the ABI from Etherscan, if none was provided
if (protocolInstance.hasABIs() && !initAbi) {
try {
if (network === 'poa-core') {
// TODO: this variable is never used anywhere, what happens?
// abiFromBlockScout = await loadAbiFromBlockScout(ABI, network, value)
} else {
abiFromEtherscan = await loadAbiFromEtherscan(ABI, network, value);
}
} catch (e) {
// noop
if (network === 'poa-core') {
abiFromEtherscan = await retryWithPrompt(() =>
loadAbiFromBlockScout(ABI, network, value),
);
} else {
abiFromEtherscan = await retryWithPrompt(() =>
loadAbiFromEtherscan(ABI, network, value),
);
}
}
// If startBlock is not set, try to load it.
if (!initStartBlock) {
try {
// Load startBlock for this contract
initStartBlock = Number(await loadStartBlockForContract(network, value)).toString();
} catch (error) {
// noop
// Load startBlock for this contract
const startBlock = await retryWithPrompt(() =>
loadStartBlockForContract(network, value),
);
if (startBlock) {
initStartBlock = Number(startBlock).toString();
}
}
return value;
Expand Down

0 comments on commit 326b303

Please sign in to comment.