Skip to content

Commit

Permalink
feat: add exponential backoff factor for etherscan (#288)
Browse files Browse the repository at this point in the history
* add slight exponential backoff factor for etherscan fetches
  • Loading branch information
charles-cooper authored Aug 28, 2024
1 parent 2bcc2bd commit 3667459
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions boa/explorer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from time import sleep
import time
from typing import Optional

import requests
Expand All @@ -24,13 +24,14 @@ def _fetch_etherscan(
if api_key is not None:
params["apikey"] = api_key

for _ in range(num_retries):
for i in range(num_retries):
res = SESSION.get(uri, params=params)
res.raise_for_status()
data = res.json()
if not (data.get("status") == "0" and "rate limit" in data.get("result", "")):
break
sleep(backoff_ms / 1000)
backoff_factor = 1.1**i # 1.1**10 ~= 2.59
time.sleep(backoff_factor * backoff_ms / 1000)

if int(data["status"]) != 1:
raise ValueError(f"Failed to retrieve data from API: {data}")
Expand Down

0 comments on commit 3667459

Please sign in to comment.