Skip to content

Add retry functionality to etherscan plugin #4530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chatty-apricots-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wagmi/cli": patch
---

Retry failed requests in etherscan plugin
31 changes: 17 additions & 14 deletions packages/cli/src/plugins/etherscan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Abi } from 'abitype'
import { Address as AddressSchema } from 'abitype/zod'
import { camelCase } from 'change-case'
import { join } from 'pathe'
import type { Address } from 'viem'
import { type Address, withRetry } from 'viem'
import { z } from 'zod'

import type { ContractConfig } from '../config.js'
Expand Down Expand Up @@ -107,19 +107,22 @@ export function etherscan<chainId extends ChainId>(
}

let abi: Abi | undefined
const implementationAddress = await (async () => {
if (!tryFetchProxyImplementation) return
const json = await globalThis
.fetch(buildUrl({ ...options, action: 'getsourcecode' }))
.then((res) => res.json())
const parsed = await GetSourceCodeResponse.safeParseAsync(json)
if (!parsed.success)
throw fromZodError(parsed.error, { prefix: 'Invalid response' })
if (parsed.data.status === '0') throw new Error(parsed.data.result)
if (!parsed.data.result[0]) return
abi = parsed.data.result[0].ABI
return parsed.data.result[0].Implementation as Address
})()
const implementationAddress = await withRetry(
async () => {
if (!tryFetchProxyImplementation) return
const json = await globalThis
.fetch(buildUrl({ ...options, action: 'getsourcecode' }))
.then((res) => res.json())
const parsed = await GetSourceCodeResponse.safeParseAsync(json)
if (!parsed.success)
throw fromZodError(parsed.error, { prefix: 'Invalid response' })
if (parsed.data.status === '0') throw new Error(parsed.data.result)
if (!parsed.data.result[0]) return
abi = parsed.data.result[0].ABI
return parsed.data.result[0].Implementation as Address
},
{ delay: 1000, retryCount: 2 },
)

if (abi) {
const cacheDir = getCacheDir()
Expand Down
28 changes: 18 additions & 10 deletions packages/cli/src/plugins/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { homedir } from 'node:os'
import { join } from 'pathe'

import type { Abi } from 'abitype'
import { withRetry } from 'viem'
import type { ContractConfig, Plugin } from '../config.js'
import type { Compute, RequiredBy } from '../types.js'

Expand Down Expand Up @@ -86,20 +87,27 @@ export function fetch(config: FetchConfig): FetchResult {
if (cachedFile?.timestamp > Date.now()) abi = cachedFile.abi
else {
try {
let aborted = false
const controller = new globalThis.AbortController()
const timeout = setTimeout(
() => controller.abort(),
timeoutDuration,
)
const timeout = setTimeout(() => {
aborted = true
controller.abort()
}, timeoutDuration)

const { url, init } = await request(contract)
const response = await globalThis.fetch(url, {
...init,
signal: controller.signal,
})
clearTimeout(timeout)
await withRetry(
async () => {
if (aborted) return
const response = await globalThis.fetch(url, {
...init,
signal: controller.signal,
})
clearTimeout(timeout)

abi = await parse({ response })
abi = await parse({ response })
},
{ delay: 1000, retryCount: 2 },
)
await writeFile(
cacheFilePath,
`${JSON.stringify({ abi, timestamp }, undefined, 2)}\n`,
Expand Down
Loading