@@ -213,6 +213,68 @@ def calculate_limit_order_amounts(
213213 }
214214
215215
216+ def calculate_limit_order_info (
217+ making_amount : str ,
218+ taking_amount : str ,
219+ input_price_usd : str ,
220+ output_price_usd : str ,
221+ ) -> Dict [str , str ]:
222+ """
223+ Calculate display info for a limit order from its amounts.
224+
225+ Use this when listing orders to show USD values and trigger prices.
226+
227+ Args:
228+ making_amount: Human-readable amount of input token being sold
229+ taking_amount: Human-readable amount of output token to receive
230+ input_price_usd: Current market price of input token in USD
231+ output_price_usd: Current market price of output token in USD
232+
233+ Returns:
234+ Dict with USD values and trigger price info
235+ """
236+ try :
237+ making = Decimal (str (making_amount ))
238+ taking = Decimal (str (taking_amount ))
239+ input_price = Decimal (str (input_price_usd ))
240+ output_price = Decimal (str (output_price_usd ))
241+
242+ # Calculate USD values at current prices
243+ making_usd = making * input_price
244+ taking_usd_at_current = taking * output_price
245+
246+ # Calculate the trigger price (price per output token the order expects)
247+ # trigger_price = making_usd / taking_amount
248+ if taking > 0 :
249+ trigger_price = making_usd / taking
250+ else :
251+ trigger_price = Decimal ("0" )
252+
253+ # Calculate price difference from current market
254+ if output_price > 0 :
255+ price_diff_pct = ((trigger_price - output_price ) / output_price ) * 100
256+ else :
257+ price_diff_pct = Decimal ("0" )
258+
259+ # Determine if order should fill (for buy orders, trigger >= current means fill)
260+ # For a buy order: you want to buy when price drops TO trigger_price
261+ # Order fills when current_price <= trigger_price
262+ will_fill = output_price <= trigger_price
263+
264+ return {
265+ "making_amount" : str (making ),
266+ "taking_amount" : str (taking ),
267+ "making_usd" : str (making_usd ),
268+ "taking_usd_at_current" : str (taking_usd_at_current ),
269+ "trigger_price_usd" : str (trigger_price ),
270+ "current_output_price_usd" : str (output_price ),
271+ "price_difference_percent" : str (price_diff_pct ),
272+ "should_fill_now" : will_fill ,
273+ }
274+ except (InvalidOperation , ValueError ) as e :
275+ raise ValueError (f"Invalid limit order info calculation: { e } " )
276+
277+
216278class TokenMathTool (AutoTool ):
217279 """
218280 Reliable token math calculations for swaps, transfers, and limit orders.
@@ -229,6 +291,7 @@ def __init__(self, registry: Optional[ToolRegistry] = None):
229291 "Actions: 'swap' (for privy_ultra - returns smallest_units), "
230292 "'transfer' (for privy_transfer - returns human-readable amount), "
231293 "'limit_order' (for privy_trigger - returns making_amount and taking_amount), "
294+ "'limit_order_info' (for displaying order list - calculates trigger price and USD values), "
232295 "'to_smallest_units', 'to_human', 'usd_to_tokens'. "
233296 "ALWAYS use this tool for any calculation - never do math yourself!"
234297 ),
@@ -245,6 +308,7 @@ def get_schema(self) -> Dict[str, Any]:
245308 "swap" ,
246309 "transfer" ,
247310 "limit_order" ,
311+ "limit_order_info" ,
248312 "to_smallest_units" ,
249313 "to_human" ,
250314 "usd_to_tokens" ,
@@ -254,6 +318,7 @@ def get_schema(self) -> Dict[str, Any]:
254318 "'swap' - For privy_ultra: calculate smallest_units from USD amount, "
255319 "'transfer' - For privy_transfer: calculate human-readable token amount from USD, "
256320 "'limit_order' - For privy_trigger: calculate making_amount and taking_amount, "
321+ "'limit_order_info' - For displaying orders: calculate trigger price and USD values from order amounts, "
257322 "'to_smallest_units' - Convert human amount to smallest units, "
258323 "'to_human' - Convert smallest units to human readable, "
259324 "'usd_to_tokens' - Calculate token amount from USD value"
@@ -289,7 +354,7 @@ def get_schema(self) -> Dict[str, Any]:
289354 },
290355 "output_price_usd" : {
291356 "type" : "string" ,
292- "description" : "Output token price in USD (for 'limit_order'). Get from Birdeye. Pass empty string if not needed." ,
357+ "description" : "Output token price in USD (for 'limit_order', 'limit_order_info' ). Get from Birdeye. Pass empty string if not needed." ,
293358 },
294359 "output_decimals" : {
295360 "type" : "integer" ,
@@ -299,6 +364,14 @@ def get_schema(self) -> Dict[str, Any]:
299364 "type" : "string" ,
300365 "description" : "Price change percentage for limit order (for 'limit_order'). E.g., '-0.5' for 0.5% lower (buy dip), '10' for 10% higher (sell high). Pass '0' for current price." ,
301366 },
367+ "making_amount" : {
368+ "type" : "string" ,
369+ "description" : "Human-readable amount of input token being sold (for 'limit_order_info'). From order's makingAmount field. Pass empty string if not needed." ,
370+ },
371+ "taking_amount" : {
372+ "type" : "string" ,
373+ "description" : "Human-readable amount of output token to receive (for 'limit_order_info'). From order's takingAmount field. Pass empty string if not needed." ,
374+ },
302375 },
303376 "required" : [
304377 "action" ,
@@ -312,6 +385,8 @@ def get_schema(self) -> Dict[str, Any]:
312385 "output_price_usd" ,
313386 "output_decimals" ,
314387 "price_change_percentage" ,
388+ "making_amount" ,
389+ "taking_amount" ,
315390 ],
316391 "additionalProperties" : False ,
317392 }
@@ -333,6 +408,8 @@ async def execute(
333408 output_price_usd : str = "" ,
334409 output_decimals : int = 0 ,
335410 price_change_percentage : str = "0" ,
411+ making_amount : str = "" ,
412+ taking_amount : str = "" ,
336413 ) -> Dict [str , Any ]:
337414 """Execute the math calculation."""
338415 action = action .lower ().strip ()
@@ -449,10 +526,41 @@ async def execute(
449526 "message" : f"${ usd_amount } at price ${ token_price_usd } = { result } tokens" ,
450527 }
451528
529+ elif action == "limit_order_info" :
530+ # Calculate display info for a limit order from its amounts
531+ if not all (
532+ [making_amount , taking_amount , input_price_usd , output_price_usd ]
533+ ):
534+ return {
535+ "status" : "error" ,
536+ "message" : "Missing required params for 'limit_order_info': making_amount, taking_amount, input_price_usd, output_price_usd" ,
537+ }
538+ result = calculate_limit_order_info (
539+ making_amount = making_amount ,
540+ taking_amount = taking_amount ,
541+ input_price_usd = input_price_usd ,
542+ output_price_usd = output_price_usd ,
543+ )
544+ return {
545+ "status" : "success" ,
546+ "action" : "limit_order_info" ,
547+ "making_amount" : result ["making_amount" ],
548+ "taking_amount" : result ["taking_amount" ],
549+ "making_usd" : result ["making_usd" ],
550+ "taking_usd_at_current" : result ["taking_usd_at_current" ],
551+ "trigger_price_usd" : result ["trigger_price_usd" ],
552+ "current_output_price_usd" : result ["current_output_price_usd" ],
553+ "price_difference_percent" : result ["price_difference_percent" ],
554+ "should_fill_now" : result ["should_fill_now" ],
555+ "message" : f"Order: sell { result ['making_amount' ]} (${ result ['making_usd' ]} ) for { result ['taking_amount' ]} tokens. "
556+ f"Trigger price: ${ result ['trigger_price_usd' ]} , Current price: ${ result ['current_output_price_usd' ]} "
557+ f"({ result ['price_difference_percent' ]} % diff). Will fill now: { result ['should_fill_now' ]} " ,
558+ }
559+
452560 else :
453561 return {
454562 "status" : "error" ,
455- "message" : f"Unknown action: { action } . Valid: swap, limit_order, to_smallest_units, to_human, usd_to_tokens" ,
563+ "message" : f"Unknown action: { action } . Valid: swap, transfer, limit_order, limit_order_info , to_smallest_units, to_human, usd_to_tokens" ,
456564 }
457565
458566 except ValueError as e :
0 commit comments