1use serde::{Deserialize, Serialize};
2use solagent_core::rig::{
3 completion::ToolDefinition,
4 tool::Tool,
5};
6use solagent_parameters::parameters;
7use solagent_plugin_pyth::{fetch_price_by_pyth, fetch_pyth_price_feed_id};
8
9#[derive(Debug, Deserialize)]
10pub struct FetchPricePyThArgs {
11 token_symbol: String,
12}
13
14#[derive(Deserialize, Serialize)]
15pub struct FetchPricePyThOutput {
16 pub price: f64,
17}
18
19#[derive(Debug, thiserror::Error)]
20#[error("FetchPricePyTh error")]
21pub struct FetchPricePyThError;
22
23#[derive(Default)]
24pub struct FetchPricePyTh;
25impl FetchPricePyTh {
26 pub fn new() -> Self {
27 FetchPricePyTh {}
28 }
29}
30
31impl Tool for FetchPricePyTh {
32 const NAME: &'static str = "fetch_price_by_pyth";
33
34 type Error = FetchPricePyThError;
35 type Args = FetchPricePyThArgs;
36 type Output = FetchPricePyThOutput;
37
38 async fn definition(&self, _prompt: String) -> ToolDefinition {
39 ToolDefinition {
40 name: "fetch_price_by_pyth".to_string(),
41 description: r#"
42
43 Fetch the current price from a Pyth oracle price feed.
44
45 examples: [
46 [
47 {
48 input: {
49 token_symbol: "SOL", // SOL/USD price feed
50 },
51 output: {
52 status: "success",
53 price: "23.45",
54 message: "Current price: $23.45",
55 },
56 explanation: "Get the current SOL/USD price from Pyth oracle",
57 },
58 ],
59 ],
60
61 "#
62 .to_string(),
63 parameters: parameters!(
64 token_symbol: String,
65 ),
66 }
67 }
68
69 async fn call(&self, args: Self::Args) -> Result<Self::Output, Self::Error> {
70 let price_feed_id = fetch_pyth_price_feed_id(&args.token_symbol).await.expect("fetch_pyth_price_feed_id");
71 let price = fetch_price_by_pyth(&price_feed_id).await.expect("fetch_price_by_pyth");
72
73 Ok(FetchPricePyThOutput { price })
74 }
75}