1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
pub mod jupiter_v6;
pub mod sanctum;
pub mod sanctum_state;

use anchor_lang::prelude::*;
use std::str::FromStr;

use crate::{MangoClient, TransactionBuilder};
use fixed::types::I80F48;

#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Version {
    Mock,
    V6,
    Sanctum,
}

#[derive(Clone)]
#[allow(clippy::large_enum_variant)]
pub enum RawQuote {
    Mock,
    V6(jupiter_v6::QuoteResponse),
    Sanctum(sanctum::QuoteResponse),
}

#[derive(Clone)]
pub struct Quote {
    pub input_mint: Pubkey,
    pub output_mint: Pubkey,
    pub price_impact_pct: f64,
    pub in_amount: u64,
    pub out_amount: u64,
    pub raw: RawQuote,
}

impl Quote {
    pub fn try_from_v6(query: jupiter_v6::QuoteResponse) -> anyhow::Result<Self> {
        Ok(Quote {
            input_mint: Pubkey::from_str(&query.input_mint)?,
            output_mint: Pubkey::from_str(&query.output_mint)?,
            price_impact_pct: query.price_impact_pct.parse()?,
            in_amount: query
                .in_amount
                .as_ref()
                .map(|a| a.parse())
                .unwrap_or(Ok(0))?,
            out_amount: query.out_amount.parse()?,
            raw: RawQuote::V6(query),
        })
    }

    pub fn try_from_sanctum(
        input_mint: Pubkey,
        output_mint: Pubkey,
        query: sanctum::QuoteResponse,
    ) -> anyhow::Result<Self> {
        Ok(Quote {
            input_mint: input_mint,
            output_mint: output_mint,
            price_impact_pct: query.fee_pct.parse()?,
            in_amount: query
                .in_amount
                .as_ref()
                .map(|a| a.parse())
                .unwrap_or(Ok(0))?,
            out_amount: query.out_amount.parse()?,
            raw: RawQuote::Sanctum(query),
        })
    }

    pub fn first_route_label(&self) -> String {
        let label_maybe = match &self.raw {
            RawQuote::Mock => Some("mock".into()),
            RawQuote::V6(raw) => raw
                .route_plan
                .first()
                .and_then(|v| v.swap_info.as_ref())
                .and_then(|v| v.label.as_ref())
                .cloned(),
            RawQuote::Sanctum(raw) => Some(raw.swap_src.clone()),
        };
        label_maybe.unwrap_or_else(|| "unknown".into())
    }
}

pub struct Swap<'a> {
    pub mango_client: &'a MangoClient,
}

impl<'a> Swap<'a> {
    async fn quote_mock(
        &self,
        input_mint: Pubkey,
        output_mint: Pubkey,
        amount: u64,
    ) -> anyhow::Result<Quote> {
        let input_token_index = self
            .mango_client
            .context
            .token_by_mint(&input_mint)?
            .token_index;
        let output_token_index = self
            .mango_client
            .context
            .token_by_mint(&output_mint)?
            .token_index;
        let input_price = self
            .mango_client
            .bank_oracle_price(input_token_index)
            .await?;
        let output_price = self
            .mango_client
            .bank_oracle_price(output_token_index)
            .await?;
        let in_amount = amount;
        let out_amount = (I80F48::from(amount) * input_price / output_price).to_num::<u64>();
        Ok(Quote {
            input_mint,
            output_mint,
            price_impact_pct: 0.0,
            in_amount,
            out_amount,
            raw: RawQuote::Mock,
        })
    }

    pub async fn quote(
        &self,
        input_mint: Pubkey,
        output_mint: Pubkey,
        amount: u64,
        slippage_bps: u64,
        only_direct_routes: bool,
        version: Version,
    ) -> anyhow::Result<Quote> {
        Ok(match version {
            Version::Mock => self.quote_mock(input_mint, output_mint, amount).await?,
            Version::V6 => Quote::try_from_v6(
                self.mango_client
                    .jupiter_v6()
                    .quote(
                        input_mint,
                        output_mint,
                        amount,
                        slippage_bps,
                        only_direct_routes,
                    )
                    .await?,
            )?,
            Version::Sanctum => Quote::try_from_sanctum(
                input_mint,
                output_mint,
                self.mango_client
                    .sanctum()
                    .quote(input_mint, output_mint, amount)
                    .await?,
            )?,
        })
    }

    pub async fn prepare_swap_transaction(
        &self,
        quote: &Quote,
    ) -> anyhow::Result<TransactionBuilder> {
        match &quote.raw {
            RawQuote::Mock => anyhow::bail!("can't prepare jupiter swap for the mock"),
            RawQuote::V6(raw) => {
                self.mango_client
                    .jupiter_v6()
                    .prepare_swap_transaction(raw)
                    .await
            }
            RawQuote::Sanctum(raw) => {
                let max_slippage_bps = (quote.price_impact_pct * 100.0).ceil() as u64;
                self.mango_client
                    .sanctum()
                    .prepare_swap_transaction(
                        quote.input_mint,
                        quote.output_mint,
                        max_slippage_bps,
                        raw,
                    )
                    .await
            }
        }
    }
}