Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/pip/bal_tools/black-24.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
gosuto-inzasheru authored Dec 5, 2024
2 parents 79975e5 + fcc20e8 commit 7a273e2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
2 changes: 1 addition & 1 deletion bal_tools/ecosystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def get_votes_from_snapshot(self, snapshot_id: str):
"first": limit,
"skip": offset,
"snapshot_id": snapshot_id,
"voter": "multisigs/vote_incentive_recycling",
"voter": "multisigs/maxi_omni",
"space": "gauges.aurafinance.eth",
},
)
Expand Down
22 changes: 18 additions & 4 deletions bal_tools/subgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,15 @@ def __init__(self, chain: str = "mainnet", silence_warnings: bool = False):
self.chain = chain
self.subgraph_url = {}
if silence_warnings:
warnings.filterwarnings('ignore', module='bal_tools.subgraph')
self.set_silence_warnings(True)
self.custom_price_logic: Dict[str, Callable] = {}

def set_silence_warnings(self, silence_warnings: bool):
if silence_warnings:
warnings.filterwarnings("ignore", module="bal_tools.subgraph")
else:
warnings.filterwarnings("default", module="bal_tools.subgraph")

def get_subgraph_url(self, subgraph="core") -> str:
"""
perform some soup magic to determine the latest subgraph url
Expand Down Expand Up @@ -93,7 +99,10 @@ def get_subgraph_url_frontendv2(self, subgraph):
if urlparse(url).scheme in ["http", "https"]:
graph_api_key = os.getenv("GRAPH_API_KEY")
if "${keys.graph}" in url and not graph_api_key:
warnings.warn(f"`GRAPH_API_KEY` not set. may be rate limited or have stale data for subgraph:{subgraph} url:{url}", UserWarning)
warnings.warn(
f"`GRAPH_API_KEY` not set. may be rate limited or have stale data for subgraph:{subgraph} url:{url}",
UserWarning,
)
break
return url.replace("${keys.graph}", graph_api_key)
except AttributeError:
Expand Down Expand Up @@ -202,7 +211,10 @@ def fetch_graphql_data(
)

transport = RequestsHTTPTransport(
url=url or self.subgraph_url[subgraph], retries=retries
url=url or self.subgraph_url[subgraph],
retries=retries,
retry_backoff_factor=0.5,
retry_status_forcelist=[429, 500, 502, 503, 504, 520],
)
client = Client(transport=transport, fetch_schema_from_transport=False)

Expand Down Expand Up @@ -281,7 +293,9 @@ def calc_twap(address: str) -> TWAPResult:
if end_date_ts >= int(item["timestamp"]) >= start_date_ts
]
if not prices:
raise NoPricesFoundError(f"No prices found for {address} on {chain} between {start_date_ts} UTC and {end_date_ts} UTC")
raise NoPricesFoundError(
f"No prices found for {address} on {chain} between {start_date_ts} UTC and {end_date_ts} UTC"
)
return TWAPResult(address=address, twap_price=sum(prices) / len(prices))

results = [calc_twap(addr) for addr in addresses]
Expand Down
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def addr_book():

@pytest.fixture(scope="module")
def msig_name():
return "multisigs/vote_incentive_recycling"
return "multisigs/maxi_omni"


@pytest.fixture(scope="module")
Expand All @@ -79,6 +79,7 @@ def bribe_market_abi():
with open("tests/abi/bribe_market.json", "r") as file:
return json.load(file)


@pytest.fixture(scope="module")
def bridge_abi():
with open("tests/abi/bridge.json", "r") as file:
Expand Down
15 changes: 11 additions & 4 deletions tests/test_subgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,29 @@ def test_get_balancer_pool_snapshots(chain, subgraph_all_chains, pool_snapshot_b

@pytest.mark.parametrize("have_thegraph_key", [True, False])
@pytest.mark.parametrize("subgraph_type", ["core", "gauges", "blocks", "aura"])
def test_find_all_subgraph_urls(subgraph_all_chains, have_thegraph_key, subgraph_type):
def test_find_all_subgraph_urls(
subgraph_all_chains, have_thegraph_key, subgraph_type, monkeypatch
):
if subgraph_all_chains.chain in ["sepolia", "mode"] and subgraph_type in [
"aura",
"blocks",
]:
pytest.skip(
f"No {subgraph_type} subgraph exists on {subgraph_all_chains.chain}"
)
os.environ["GRAPH_API_KEY"] = (
os.getenv("GRAPH_API_KEY") if have_thegraph_key else ""
)

if not have_thegraph_key:
monkeypatch.setenv("GRAPH_API_KEY", "")
subgraph_all_chains.set_silence_warnings(True)

url = subgraph_all_chains.get_subgraph_url(subgraph_type)

assert url is not None
assert url is not ""

if not have_thegraph_key:
subgraph_all_chains.set_silence_warnings(False)


def test_warning_configuration(monkeypatch):
monkeypatch.setenv("GRAPH_API_KEY", "")
Expand Down

0 comments on commit 7a273e2

Please sign in to comment.