solagent_rig_jupiter/
trade.rs1use serde::{Deserialize, Serialize};
2use solagent_core::{
3 rig::{completion::ToolDefinition, tool::Tool},
4 SolanaAgentKit,
5};
6use solagent_parameters::parameters;
7use solagent_plugin_jupiter::trade;
8use std::sync::Arc;
9
10#[derive(Deserialize)]
11pub struct TradeArgs {
12 output_mint: String,
13 input_amount: f64,
14 input_mint: Option<String>,
15 slippage_bps: Option<u32>,
16}
17
18#[derive(Deserialize, Serialize)]
19pub struct TradeOutput {
20 pub signature: String,
21}
22
23#[derive(Debug, thiserror::Error)]
24#[error("Trade error")]
25pub struct TradeError;
26
27pub struct Trade {
28 agent: Arc<SolanaAgentKit>,
29}
30
31impl Trade {
32 pub fn new(agent: Arc<SolanaAgentKit>) -> Self {
33 Trade { agent }
34 }
35}
36
37impl Tool for Trade {
38 const NAME: &'static str = "trade";
39
40 type Error = TradeError;
41 type Args = TradeArgs;
42 type Output = TradeOutput;
43
44 async fn definition(&self, _prompt: String) -> ToolDefinition {
45 ToolDefinition {
46 name: "trade".to_string(),
47 description: r#"
48 This tool can be used to swap tokens to another token (It uses Jupiter Exchange).
49
50 {
51 input: {
52 output_mint: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
53 input_amount: 1,
54 },
55 }
56
57 "#
58 .to_string(),
59 parameters: parameters!(
60 output_mint: String,
61 input_amount: f64,
62 input_mint: Option<String>,
63 slippage_bps: Option<u32>,
64 ),
65 }
66 }
67
68 async fn call(&self, args: Self::Args) -> Result<Self::Output, Self::Error> {
69 let signature = trade(
70 &self.agent,
71 &args.output_mint,
72 args.input_amount,
73 args.input_mint,
74 args.slippage_bps,
75 )
76 .await
77 .expect("trade");
78
79 Ok(TradeOutput { signature })
80 }
81}