Skip to content

Commit 63fcd98

Browse files
committedFeb 27, 2025··
chore: format
1 parent d186708 commit 63fcd98

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed
 

‎openagent/tools/arbitrum/compound_market_analysis.py

+29-16
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from openagent.agent.config import ModelConfig
1212
from openagent.core.tool import Tool
1313

14+
1415
@dataclass
1516
class CompoundMarketData:
1617
address: str
@@ -20,12 +21,14 @@ class CompoundMarketData:
2021
borrowAPRChange24h: float
2122
supplyAPRChange24h: float
2223

24+
2325
class CompoundMarketConfig(BaseModel):
2426
model: Optional[ModelConfig] = Field(
2527
default=None,
26-
description="Model configuration for LLM. If not provided, will use agent's core model",
28+
description="Model configuration for this tool. If not provided, will use agent's core model",
2729
)
2830

31+
2932
class ArbitrumCompoundMarketTool(Tool[CompoundMarketConfig]):
3033
def __init__(self, core_model=None):
3134
super().__init__()
@@ -107,20 +110,25 @@ async def __call__(self) -> str:
107110
logger.error(f"Error in {self.name} tool: {e}")
108111
return f"Error in {self.name} tool: {e}"
109112

110-
async def _fetch_compound_arbitrum_market_data(self) -> list[CompoundMarketData]:
113+
@staticmethod
114+
async def _fetch_compound_arbitrum_market_data() -> list[CompoundMarketData]:
111115
async with httpx.AsyncClient(timeout=30.0) as client:
112116
response = await client.get(
113117
"https://v3-api.compound.finance/market/all-networks/all-contracts/summary"
114118
)
115119

116120
if response.status_code != 200:
117-
raise Exception(f"Failed to fetch Compound market data: {response.text}, {response.status_code}")
121+
raise Exception(
122+
f"Failed to fetch Compound market data: {response.text}, {response.status_code}"
123+
)
118124

119125
results = response.json()
120126

121127
# Filter for Arbitrum markets (chain_id 42161)
122-
arbitrum_markets = [market for market in results if market["chain_id"] == 42161]
123-
128+
arbitrum_markets = [
129+
market for market in results if market["chain_id"] == 42161
130+
]
131+
124132
market_data = []
125133

126134
for market in arbitrum_markets:
@@ -130,16 +138,22 @@ async def _fetch_compound_arbitrum_market_data(self) -> list[CompoundMarketData]
130138
)
131139

132140
if historical_response.status_code != 200:
133-
logger.warning(f"Failed to fetch historical data for {market['comet']['address']}: {historical_response.text}, {historical_response.status_code}")
141+
logger.warning(
142+
f"Failed to fetch historical data for {market['comet']['address']}: {historical_response.text}, {historical_response.status_code}"
143+
)
134144
continue
135-
145+
136146
historical_data = historical_response.json()
137147

138148
# Sort historical data by timestamp in descending order (newest first)
139-
sorted_data = sorted(historical_data, key=lambda x: x['timestamp'], reverse=True)
140-
149+
sorted_data = sorted(
150+
historical_data, key=lambda x: x["timestamp"], reverse=True
151+
)
152+
141153
if len(sorted_data) < 2:
142-
logger.warning(f"Insufficient historical data for {market['comet']['address']}")
154+
logger.warning(
155+
f"Insufficient historical data for {market['comet']['address']}"
156+
)
143157
continue
144158

145159
# Convert string APRs to float
@@ -148,20 +162,19 @@ async def _fetch_compound_arbitrum_market_data(self) -> list[CompoundMarketData]
148162
yesterday_borrow_apr = float(sorted_data[1]["borrow_apr"])
149163
yesterday_supply_apr = float(sorted_data[1]["supply_apr"])
150164

151-
152165
# Calculate 24h changes
153166
borrow_apr_change_24h = current_borrow_apr - yesterday_borrow_apr
154167
supply_apr_change_24h = current_supply_apr - yesterday_supply_apr
155168

156169
market_data.append(
157170
CompoundMarketData(
158-
address=market['comet']['address'],
159-
collateralAssets=market['collateral_asset_symbols'],
171+
address=market["comet"]["address"],
172+
collateralAssets=market["collateral_asset_symbols"],
160173
borrowAPR=current_borrow_apr,
161174
supplyAPR=current_supply_apr,
162175
borrowAPRChange24h=borrow_apr_change_24h,
163-
supplyAPRChange24h=supply_apr_change_24h
176+
supplyAPRChange24h=supply_apr_change_24h,
164177
)
165178
)
166-
167-
return market_data
179+
180+
return market_data

0 commit comments

Comments
 (0)
Please sign in to comment.