|
10 | 10 | from pydantic import BaseModel, Field |
11 | 11 | from openagent.agent.config import ModelConfig |
12 | 12 | from openagent.core.tool import Tool |
| 13 | +from openagent.core.utils.fetch_json import fetch_json |
13 | 14 |
|
14 | 15 |
|
15 | 16 | @dataclass |
@@ -112,69 +113,51 @@ async def __call__(self) -> str: |
112 | 113 |
|
113 | 114 | @staticmethod |
114 | 115 | async def _fetch_compound_arbitrum_market_data() -> list[CompoundMarketData]: |
115 | | - async with httpx.AsyncClient(timeout=30.0) as client: |
116 | | - response = await client.get( |
117 | | - "https://v3-api.compound.finance/market/all-networks/all-contracts/summary" |
118 | | - ) |
119 | | - |
120 | | - if response.status_code != 200: |
121 | | - raise Exception( |
122 | | - f"Failed to fetch Compound market data: {response.text}, {response.status_code}" |
123 | | - ) |
124 | | - |
125 | | - results = response.json() |
| 116 | + results = await fetch_json( |
| 117 | + url="https://v3-api.compound.finance/market/all-networks/all-contracts/summary" |
| 118 | + ) |
126 | 119 |
|
127 | | - # Filter for Arbitrum markets (chain_id 42161) |
128 | | - arbitrum_markets = [ |
129 | | - market for market in results if market["chain_id"] == 42161 |
130 | | - ] |
| 120 | + # Filter for Arbitrum markets (chain_id 42161) |
| 121 | + arbitrum_markets = [market for market in results if market["chain_id"] == 42161] |
131 | 122 |
|
132 | | - market_data = [] |
| 123 | + market_data = [] |
133 | 124 |
|
134 | | - for market in arbitrum_markets: |
135 | | - # Fetch historical data for each address |
136 | | - historical_response = await client.get( |
137 | | - f"https://v3-api.compound.finance/market/arbitrum-mainnet/{market['comet']['address']}/historical/summary" |
138 | | - ) |
139 | | - |
140 | | - if historical_response.status_code != 200: |
141 | | - logger.warning( |
142 | | - f"Failed to fetch historical data for {market['comet']['address']}: {historical_response.text}, {historical_response.status_code}" |
143 | | - ) |
144 | | - continue |
| 125 | + for market in arbitrum_markets: |
| 126 | + # Fetch historical data for each address |
| 127 | + historical_data = await fetch_json( |
| 128 | + f"https://v3-api.compound.finance/market/arbitrum-mainnet/{market['comet']['address']}/historical/summary" |
| 129 | + ) |
145 | 130 |
|
146 | | - historical_data = historical_response.json() |
| 131 | + # Sort historical data by timestamp in descending order (newest first) |
| 132 | + sorted_data = sorted( |
| 133 | + historical_data, key=lambda x: x["timestamp"], reverse=True |
| 134 | + ) |
147 | 135 |
|
148 | | - # Sort historical data by timestamp in descending order (newest first) |
149 | | - sorted_data = sorted( |
150 | | - historical_data, key=lambda x: x["timestamp"], reverse=True |
| 136 | + if len(sorted_data) < 2: |
| 137 | + logger.warning( |
| 138 | + f"Insufficient historical data for {market['comet']['address']}" |
151 | 139 | ) |
152 | | - |
153 | | - if len(sorted_data) < 2: |
154 | | - logger.warning( |
155 | | - f"Insufficient historical data for {market['comet']['address']}" |
156 | | - ) |
157 | | - continue |
158 | | - |
159 | | - # Convert string APRs to float |
160 | | - current_borrow_apr = float(sorted_data[0]["borrow_apr"]) |
161 | | - current_supply_apr = float(sorted_data[0]["supply_apr"]) |
162 | | - yesterday_borrow_apr = float(sorted_data[1]["borrow_apr"]) |
163 | | - yesterday_supply_apr = float(sorted_data[1]["supply_apr"]) |
164 | | - |
165 | | - # Calculate 24h changes |
166 | | - borrow_apr_change_24h = current_borrow_apr - yesterday_borrow_apr |
167 | | - supply_apr_change_24h = current_supply_apr - yesterday_supply_apr |
168 | | - |
169 | | - market_data.append( |
170 | | - CompoundMarketData( |
171 | | - address=market["comet"]["address"], |
172 | | - collateralAssets=market["collateral_asset_symbols"], |
173 | | - borrowAPR=current_borrow_apr, |
174 | | - supplyAPR=current_supply_apr, |
175 | | - borrowAPRChange24h=borrow_apr_change_24h, |
176 | | - supplyAPRChange24h=supply_apr_change_24h, |
177 | | - ) |
| 140 | + continue |
| 141 | + |
| 142 | + # Convert string APRs to float |
| 143 | + current_borrow_apr = float(sorted_data[0]["borrow_apr"]) |
| 144 | + current_supply_apr = float(sorted_data[0]["supply_apr"]) |
| 145 | + yesterday_borrow_apr = float(sorted_data[1]["borrow_apr"]) |
| 146 | + yesterday_supply_apr = float(sorted_data[1]["supply_apr"]) |
| 147 | + |
| 148 | + # Calculate 24h changes |
| 149 | + borrow_apr_change_24h = current_borrow_apr - yesterday_borrow_apr |
| 150 | + supply_apr_change_24h = current_supply_apr - yesterday_supply_apr |
| 151 | + |
| 152 | + market_data.append( |
| 153 | + CompoundMarketData( |
| 154 | + address=market["comet"]["address"], |
| 155 | + collateralAssets=market["collateral_asset_symbols"], |
| 156 | + borrowAPR=current_borrow_apr, |
| 157 | + supplyAPR=current_supply_apr, |
| 158 | + borrowAPRChange24h=borrow_apr_change_24h, |
| 159 | + supplyAPRChange24h=supply_apr_change_24h, |
178 | 160 | ) |
| 161 | + ) |
179 | 162 |
|
180 | | - return market_data |
| 163 | + return market_data |
0 commit comments