11
11
from openagent .agent .config import ModelConfig
12
12
from openagent .core .tool import Tool
13
13
14
+
14
15
@dataclass
15
16
class CompoundMarketData :
16
17
address : str
@@ -20,12 +21,14 @@ class CompoundMarketData:
20
21
borrowAPRChange24h : float
21
22
supplyAPRChange24h : float
22
23
24
+
23
25
class CompoundMarketConfig (BaseModel ):
24
26
model : Optional [ModelConfig ] = Field (
25
27
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" ,
27
29
)
28
30
31
+
29
32
class ArbitrumCompoundMarketTool (Tool [CompoundMarketConfig ]):
30
33
def __init__ (self , core_model = None ):
31
34
super ().__init__ ()
@@ -107,20 +110,25 @@ async def __call__(self) -> str:
107
110
logger .error (f"Error in { self .name } tool: { e } " )
108
111
return f"Error in { self .name } tool: { e } "
109
112
110
- async def _fetch_compound_arbitrum_market_data (self ) -> list [CompoundMarketData ]:
113
+ @staticmethod
114
+ async def _fetch_compound_arbitrum_market_data () -> list [CompoundMarketData ]:
111
115
async with httpx .AsyncClient (timeout = 30.0 ) as client :
112
116
response = await client .get (
113
117
"https://v3-api.compound.finance/market/all-networks/all-contracts/summary"
114
118
)
115
119
116
120
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
+ )
118
124
119
125
results = response .json ()
120
126
121
127
# 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
+
124
132
market_data = []
125
133
126
134
for market in arbitrum_markets :
@@ -130,16 +138,22 @@ async def _fetch_compound_arbitrum_market_data(self) -> list[CompoundMarketData]
130
138
)
131
139
132
140
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
+ )
134
144
continue
135
-
145
+
136
146
historical_data = historical_response .json ()
137
147
138
148
# 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
+
141
153
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
+ )
143
157
continue
144
158
145
159
# Convert string APRs to float
@@ -148,20 +162,19 @@ async def _fetch_compound_arbitrum_market_data(self) -> list[CompoundMarketData]
148
162
yesterday_borrow_apr = float (sorted_data [1 ]["borrow_apr" ])
149
163
yesterday_supply_apr = float (sorted_data [1 ]["supply_apr" ])
150
164
151
-
152
165
# Calculate 24h changes
153
166
borrow_apr_change_24h = current_borrow_apr - yesterday_borrow_apr
154
167
supply_apr_change_24h = current_supply_apr - yesterday_supply_apr
155
168
156
169
market_data .append (
157
170
CompoundMarketData (
158
- address = market [' comet' ][ ' address' ],
159
- collateralAssets = market [' collateral_asset_symbols' ],
171
+ address = market [" comet" ][ " address" ],
172
+ collateralAssets = market [" collateral_asset_symbols" ],
160
173
borrowAPR = current_borrow_apr ,
161
174
supplyAPR = current_supply_apr ,
162
175
borrowAPRChange24h = borrow_apr_change_24h ,
163
- supplyAPRChange24h = supply_apr_change_24h
176
+ supplyAPRChange24h = supply_apr_change_24h ,
164
177
)
165
178
)
166
-
167
- return market_data
179
+
180
+ return market_data
0 commit comments