jupiter-client 1.0.1

A type-safe, asynchronous Rust client for the Jupiter Aggregator API on Solana. Facilitates seamless token swaps, route finding, and quote generation.
Documentation
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
pub use reqwest;
pub use rust_decimal;
pub use solana_account_decoder;
pub use solana_sdk;

use anyhow::{Result, anyhow};
use reqwest::{Client, ClientBuilder};
use rust_decimal::Decimal;
use serde::{Deserialize, Deserializer, Serialize, Serializer, de::DeserializeOwned};
use serde_json::Value;
use solana_account_decoder::UiAccount;
use solana_sdk::{
    instruction::{AccountMeta, Instruction},
    pubkey::Pubkey,
};
use std::{collections::HashMap, str::FromStr, sync::Arc, time::Duration};

pub mod field_as_string {
    use {
        serde::{Deserialize, Serialize},
        serde::{Deserializer, Serializer, de},
        std::str::FromStr,
    };

    pub fn serialize<T, S>(t: &T, serializer: S) -> Result<S::Ok, S::Error>
    where
        T: ToString,
        S: Serializer,
    {
        t.to_string().serialize(serializer)
    }

    pub fn deserialize<'de, T, D>(deserializer: D) -> Result<T, D::Error>
    where
        T: FromStr,
        D: Deserializer<'de>,
        <T as FromStr>::Err: std::fmt::Debug,
    {
        let s: String = String::deserialize(deserializer)?;
        s.parse()
            .map_err(|e| de::Error::custom(format!("Parse error: {:?}", e)))
    }
}

pub mod option_field_as_string {
    use {
        serde::{Deserialize, Deserializer, Serialize, Serializer, de},
        std::str::FromStr,
    };

    pub fn serialize<T, S>(t: &Option<T>, serializer: S) -> Result<S::Ok, S::Error>
    where
        T: ToString,
        S: Serializer,
    {
        if let Some(t) = t {
            t.to_string().serialize(serializer)
        } else {
            serializer.serialize_none()
        }
    }

    pub fn deserialize<'de, T, D>(deserializer: D) -> Result<Option<T>, D::Error>
    where
        T: FromStr,
        D: Deserializer<'de>,
        <T as FromStr>::Err: std::fmt::Debug,
    {
        let opt: Option<String> = Option::deserialize(deserializer)?;
        match opt {
            Some(s) => s
                .parse()
                .map(Some)
                .map_err(|e| de::Error::custom(format!("Parse error: {:?}", e))),
            None => Ok(None),
        }
    }
}

pub mod base64_serialize_deserialize {
    use base64::{Engine, engine::general_purpose::STANDARD};
    use serde::{Deserializer, Serializer, de};

    use super::*;
    pub fn serialize<S: Serializer>(v: &Vec<u8>, s: S) -> Result<S::Ok, S::Error> {
        let base58 = STANDARD.encode(v);
        String::serialize(&base58, s)
    }

    pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
    where
        D: Deserializer<'de>,
    {
        let field_string = String::deserialize(deserializer)?;
        STANDARD
            .decode(field_string)
            .map_err(|e| de::Error::custom(format!("base64 decoding error: {:?}", e)))
    }
}

// ====================== ComputeUnitPrice & Priority ======================

#[derive(Deserialize, Serialize, Debug, PartialEq, Clone)]
#[serde(rename_all = "camelCase")]
#[serde(untagged)]
pub enum ComputeUnitPriceMicroLamports {
    MicroLamports(u64),
    #[serde(deserialize_with = "deserialize_auto")]
    Auto,
}

fn deserialize_auto<'de, D>(deserializer: D) -> Result<(), D::Error>
where
    D: Deserializer<'de>,
{
    #[derive(Deserialize)]
    enum Helper {
        #[serde(rename = "auto")]
        Variant,
    }
    Helper::deserialize(deserializer)?;
    Ok(())
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Copy, Clone)]
#[serde(rename_all = "camelCase")]
pub enum PriorityLevel {
    Medium,
    High,
    VeryHigh,
}

#[derive(Deserialize, Debug, PartialEq, Copy, Clone, Default)]
#[serde(rename_all = "camelCase")]
pub enum PrioritizationFeeLamports {
    AutoMultiplier(u32),
    JitoTipLamports(u64),
    #[serde(rename_all = "camelCase")]
    PriorityLevelWithMaxLamports {
        priority_level: PriorityLevel,
        max_lamports: u64,
        #[serde(default)]
        global: bool,
    },
    #[default]
    #[serde(untagged, deserialize_with = "deserialize_auto")]
    Auto,
    #[serde(untagged)]
    Lamports(u64),
    #[serde(untagged, deserialize_with = "deserialize_disabled")]
    Disabled,
}

fn deserialize_disabled<'de, D>(deserializer: D) -> Result<(), D::Error>
where
    D: Deserializer<'de>,
{
    #[derive(Deserialize)]
    enum Helper {
        #[serde(rename = "disabled")]
        Variant,
    }
    Helper::deserialize(deserializer)?;
    Ok(())
}

impl Serialize for PrioritizationFeeLamports {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        #[derive(Serialize)]
        #[serde(rename_all = "camelCase")]
        struct AutoMultiplier {
            auto_multiplier: u32,
        }

        #[derive(Serialize)]
        #[serde(rename_all = "camelCase")]
        struct JitoTip {
            jito_tip_lamports: u64,
        }

        #[derive(Serialize)]
        #[serde(rename_all = "camelCase")]
        struct PriorityWrapper<'a> {
            priority_level_with_max_lamports: PriorityLevelWithMaxLamports<'a>,
        }

        #[derive(Serialize)]
        #[serde(rename_all = "camelCase")]
        struct PriorityLevelWithMaxLamports<'a> {
            priority_level: &'a PriorityLevel,
            max_lamports: &'a u64,
            global: &'a bool,
        }

        match self {
            Self::AutoMultiplier(v) => AutoMultiplier {
                auto_multiplier: *v,
            }
            .serialize(serializer),
            Self::JitoTipLamports(v) => JitoTip {
                jito_tip_lamports: *v,
            }
            .serialize(serializer),
            Self::Auto => serializer.serialize_str("auto"),
            Self::Lamports(v) => serializer.serialize_u64(*v),
            Self::Disabled => serializer.serialize_str("disabled"),
            Self::PriorityLevelWithMaxLamports {
                priority_level,
                max_lamports,
                global,
            } => PriorityWrapper {
                priority_level_with_max_lamports: PriorityLevelWithMaxLamports {
                    priority_level,
                    max_lamports,
                    global,
                },
            }
            .serialize(serializer),
        }
    }
}

// ====================== TransactionConfig ======================

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
#[serde(rename_all = "camelCase")]
pub struct DynamicSlippageSettings {
    pub min_bps: Option<u16>,
    pub max_bps: Option<u16>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct TransactionConfig {
    pub wrap_and_unwrap_sol: bool,
    pub allow_optimized_wrapped_sol_token_account: bool,
    #[serde(with = "option_field_as_string")]
    pub fee_account: Option<Pubkey>,
    #[serde(with = "option_field_as_string")]
    pub destination_token_account: Option<Pubkey>,
    #[serde(with = "option_field_as_string")]
    pub tracking_account: Option<Pubkey>,
    pub compute_unit_price_micro_lamports: Option<ComputeUnitPriceMicroLamports>,
    pub prioritization_fee_lamports: Option<PrioritizationFeeLamports>,
    pub dynamic_compute_unit_limit: bool,
    pub as_legacy_transaction: bool,
    pub use_shared_accounts: bool,
    pub use_token_ledger: bool,
    pub skip_user_accounts_rpc_calls: bool,
    pub keyed_ui_accounts: Option<Vec<KeyedUiAccount>>,
    pub program_authority_id: Option<u8>,
    pub dynamic_slippage: Option<DynamicSlippageSettings>,
}

impl Default for TransactionConfig {
    fn default() -> Self {
        Self {
            wrap_and_unwrap_sol: true,
            allow_optimized_wrapped_sol_token_account: false,
            fee_account: None,
            destination_token_account: None,
            tracking_account: None,
            compute_unit_price_micro_lamports: None,
            prioritization_fee_lamports: Some(
                PrioritizationFeeLamports::PriorityLevelWithMaxLamports {
                    priority_level: PriorityLevel::VeryHigh,
                    max_lamports: 4_000_000,
                    global: false,
                },
            ),
            dynamic_compute_unit_limit: false,
            as_legacy_transaction: false,
            use_shared_accounts: true,
            use_token_ledger: false,
            skip_user_accounts_rpc_calls: false,
            keyed_ui_accounts: None,
            program_authority_id: None,
            dynamic_slippage: None,
        }
    }
}

#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
pub struct KeyedUiAccount {
    pub pubkey: String,
    #[serde(flatten)]
    pub ui_account: UiAccount,
    pub params: Option<Value>,
}

#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct SwapInfo {
    #[serde(with = "field_as_string")]
    pub amm_key: Pubkey,
    pub label: String,
    #[serde(with = "field_as_string")]
    pub input_mint: Pubkey,
    #[serde(with = "field_as_string")]
    pub output_mint: Pubkey,
    #[serde(with = "field_as_string")]
    pub in_amount: u64,
    #[serde(with = "field_as_string")]
    pub out_amount: u64,
    #[serde(default, with = "option_field_as_string")]
    pub fee_amount: Option<u64>,
    #[serde(default, with = "option_field_as_string")]
    pub fee_mint: Option<Pubkey>,
    // deprecated
    //
    // #[serde(default, with = "field_as_string")]
    // pub fee_amount: u64,
    // #[serde(default, with = "field_as_string")]
    // pub fee_mint: Pubkey,
}

pub type RoutePlanWithMetadata = Vec<RoutePlanStep>;

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
#[serde(rename_all = "camelCase")]
pub struct RoutePlanStep {
    pub swap_info: SwapInfo,
    pub percent: u8,
}

#[derive(Serialize, Deserialize, Default, PartialEq, Clone, Debug)]
pub enum SwapMode {
    #[default]
    ExactIn,
    ExactOut,
}

impl FromStr for SwapMode {
    type Err = anyhow::Error;
    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        match s {
            "ExactIn" => Ok(Self::ExactIn),
            "ExactOut" => Ok(Self::ExactOut),
            _ => Err(anyhow!("Invalid SwapMode: {}", s)),
        }
    }
}

#[derive(Serialize, Debug, Default, Clone)]
#[serde(rename_all = "camelCase")]
pub struct QuoteRequest {
    #[serde(with = "field_as_string")]
    pub input_mint: Pubkey,
    #[serde(with = "field_as_string")]
    pub output_mint: Pubkey,
    #[serde(with = "field_as_string")]
    pub amount: u64,
    pub swap_mode: Option<SwapMode>,
    pub slippage_bps: u16,
    pub auto_slippage: Option<bool>,
    pub max_auto_slippage_bps: Option<u16>,
    pub compute_auto_slippage: bool,
    pub auto_slippage_collision_usd_value: Option<u32>,
    pub minimize_slippage: Option<bool>,
    pub platform_fee_bps: Option<u8>,
    pub dexes: Option<String>,
    pub excluded_dexes: Option<String>,
    pub only_direct_routes: Option<bool>,
    pub as_legacy_transaction: Option<bool>,
    pub restrict_intermediate_tokens: Option<bool>,
    pub max_accounts: Option<usize>,
    pub quote_type: Option<String>,
    pub quote_args: Option<HashMap<String, String>>,
    pub prefer_liquid_dexes: Option<bool>,
}

#[derive(Serialize, Debug, Default, Clone)]
#[serde(rename_all = "camelCase")]
pub struct InternalQuoteRequest {
    #[serde(with = "field_as_string")]
    pub input_mint: Pubkey,
    #[serde(with = "field_as_string")]
    pub output_mint: Pubkey,
    #[serde(with = "field_as_string")]
    pub amount: u64,
    pub swap_mode: Option<SwapMode>,
    pub slippage_bps: u16,
    pub auto_slippage: Option<bool>,
    pub max_auto_slippage_bps: Option<u16>,
    pub compute_auto_slippage: bool,
    pub auto_slippage_collision_usd_value: Option<u32>,
    pub minimize_slippage: Option<bool>,
    pub platform_fee_bps: Option<u8>,
    pub dexes: Option<String>,
    pub excluded_dexes: Option<String>,
    pub only_direct_routes: Option<bool>,
    pub as_legacy_transaction: Option<bool>,
    pub restrict_intermediate_tokens: Option<bool>,
    pub max_accounts: Option<usize>,
    pub quote_type: Option<String>,
    pub prefer_liquid_dexes: Option<bool>,
}

impl From<QuoteRequest> for InternalQuoteRequest {
    fn from(req: QuoteRequest) -> Self {
        Self {
            input_mint: req.input_mint,
            output_mint: req.output_mint,
            amount: req.amount,
            swap_mode: req.swap_mode,
            slippage_bps: req.slippage_bps,
            auto_slippage: req.auto_slippage,
            max_auto_slippage_bps: req.max_auto_slippage_bps,
            compute_auto_slippage: req.compute_auto_slippage,
            auto_slippage_collision_usd_value: req.auto_slippage_collision_usd_value,
            minimize_slippage: req.minimize_slippage,
            platform_fee_bps: req.platform_fee_bps,
            dexes: req.dexes,
            excluded_dexes: req.excluded_dexes,
            only_direct_routes: req.only_direct_routes,
            as_legacy_transaction: req.as_legacy_transaction,
            restrict_intermediate_tokens: req.restrict_intermediate_tokens,
            max_accounts: req.max_accounts,
            quote_type: req.quote_type,
            prefer_liquid_dexes: req.prefer_liquid_dexes,
        }
    }
}

#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct PlatformFee {
    #[serde(with = "field_as_string")]
    pub amount: u64,
    pub fee_bps: u8,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct QuoteResponse {
    #[serde(with = "field_as_string")]
    pub input_mint: Pubkey,
    #[serde(with = "field_as_string")]
    pub in_amount: u64,
    #[serde(with = "field_as_string")]
    pub output_mint: Pubkey,
    #[serde(with = "field_as_string")]
    pub out_amount: u64,
    #[serde(with = "field_as_string")]
    pub other_amount_threshold: u64,
    pub swap_mode: SwapMode,
    pub slippage_bps: u16,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub computed_auto_slippage: Option<u16>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub uses_quote_minimizing_slippage: Option<bool>,
    pub platform_fee: Option<PlatformFee>,
    pub price_impact_pct: Decimal,
    pub route_plan: RoutePlanWithMetadata,
    #[serde(default)]
    pub context_slot: u64,
    #[serde(default)]
    pub time_taken: f64,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct SwapRequest {
    #[serde(with = "field_as_string")]
    pub user_public_key: Pubkey,
    pub quote_response: QuoteResponse,
    #[serde(flatten)]
    pub config: TransactionConfig,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub enum PrioritizationType {
    #[serde(rename_all = "camelCase")]
    Jito { lamports: u64 },
    #[serde(rename_all = "camelCase")]
    ComputeBudget {
        micro_lamports: u64,
        estimated_micro_lamports: Option<u64>,
    },
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct DynamicSlippageReport {
    pub slippage_bps: u16,
    pub other_amount: Option<u64>,
    pub simulated_incurred_slippage_bps: Option<i16>,
    pub amplification_ratio: Option<Decimal>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct UiSimulationError {
    error_code: String,
    error: String,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct SwapResponse {
    #[serde(with = "base64_serialize_deserialize")]
    pub swap_transaction: Vec<u8>,
    pub last_valid_block_height: u64,
    pub prioritization_fee_lamports: u64,
    pub compute_unit_limit: u32,
    pub prioritization_type: Option<PrioritizationType>,
    pub dynamic_slippage_report: Option<DynamicSlippageReport>,
    pub simulation_error: Option<UiSimulationError>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct SwapInstructionsResponse {
    pub token_ledger_instruction: Option<Instruction>,
    pub compute_budget_instructions: Vec<Instruction>,
    pub setup_instructions: Vec<Instruction>,
    pub swap_instruction: Instruction,
    pub cleanup_instruction: Option<Instruction>,
    pub other_instructions: Vec<Instruction>,
    pub address_lookup_table_addresses: Vec<Pubkey>,
    pub prioritization_fee_lamports: u64,
    pub compute_unit_limit: u32,
    pub prioritization_type: Option<PrioritizationType>,
    pub dynamic_slippage_report: Option<DynamicSlippageReport>,
    pub simulation_error: Option<UiSimulationError>,
}

// Internal structs for deserialization
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
struct InstructionInternal {
    #[serde(with = "field_as_string")]
    program_id: Pubkey,
    accounts: Vec<AccountMetaInternal>,
    #[serde(with = "base64_serialize_deserialize")]
    data: Vec<u8>,
}

#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
struct AccountMetaInternal {
    #[serde(with = "field_as_string")]
    pubkey: Pubkey,
    is_signer: bool,
    is_writable: bool,
}

impl From<AccountMetaInternal> for AccountMeta {
    fn from(a: AccountMetaInternal) -> Self {
        AccountMeta {
            pubkey: a.pubkey,
            is_signer: a.is_signer,
            is_writable: a.is_writable,
        }
    }
}

impl From<InstructionInternal> for Instruction {
    fn from(i: InstructionInternal) -> Self {
        Instruction {
            program_id: i.program_id,
            accounts: i.accounts.into_iter().map(Into::into).collect(),
            data: i.data,
        }
    }
}

#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
struct SwapInstructionsResponseInternal {
    token_ledger_instruction: Option<InstructionInternal>,
    compute_budget_instructions: Vec<InstructionInternal>,
    setup_instructions: Vec<InstructionInternal>,
    swap_instruction: InstructionInternal,
    cleanup_instruction: Option<InstructionInternal>,
    other_instructions: Vec<InstructionInternal>,
    address_lookup_table_addresses: Vec<PubkeyInternal>,
    prioritization_fee_lamports: u64,
    compute_unit_limit: u32,
    prioritization_type: Option<PrioritizationType>,
    dynamic_slippage_report: Option<DynamicSlippageReport>,
    simulation_error: Option<UiSimulationError>,
}

#[derive(Deserialize, Debug, Clone)]
struct PubkeyInternal(#[serde(with = "field_as_string")] Pubkey);

impl From<SwapInstructionsResponseInternal> for SwapInstructionsResponse {
    fn from(v: SwapInstructionsResponseInternal) -> Self {
        Self {
            token_ledger_instruction: v.token_ledger_instruction.map(Into::into),
            compute_budget_instructions: v
                .compute_budget_instructions
                .into_iter()
                .map(Into::into)
                .collect(),
            setup_instructions: v.setup_instructions.into_iter().map(Into::into).collect(),
            swap_instruction: v.swap_instruction.into(),
            cleanup_instruction: v.cleanup_instruction.map(Into::into),
            other_instructions: v.other_instructions.into_iter().map(Into::into).collect(),
            address_lookup_table_addresses: v
                .address_lookup_table_addresses
                .into_iter()
                .map(|p| p.0)
                .collect(),
            prioritization_fee_lamports: v.prioritization_fee_lamports,
            compute_unit_limit: v.compute_unit_limit,
            prioritization_type: v.prioritization_type,
            dynamic_slippage_report: v.dynamic_slippage_report,
            simulation_error: v.simulation_error,
        }
    }
}

#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct OrderResponse {
    pub route_plan: RoutePlanWithMetadata,
    #[serde(with = "field_as_string")]
    pub input_mint: Pubkey,
    #[serde(with = "field_as_string")]
    pub output_mint: Pubkey,
    #[serde(with = "field_as_string")]
    pub in_amount: u64,
    #[serde(with = "field_as_string")]
    pub out_amount: u64,
    #[serde(with = "field_as_string")]
    pub other_amount_threshold: u64,
    pub swap_mode: SwapMode,
    pub transaction: String,
    pub request_id: String,
    pub error_message: Option<String>,
    pub router: String,
    pub slippage_bps: u64,
}

#[derive(Clone)]
struct JupiterClientRef {
    client: Client,
    base_path: String,
    api_key: Option<String>,
}

#[derive(Clone)]
pub struct JupiterClient {
    inner: Arc<JupiterClientRef>,
}

async fn check_is_success(response: reqwest::Response) -> Result<reqwest::Response> {
    if !response.status().is_success() {
        let status = response.status();
        let text = response.text().await.ok();
        return Err(anyhow!("Request failed: {}, body: {:?}", status, text));
    }

    Ok(response)
}

async fn check_and_deserialize<T: DeserializeOwned>(response: reqwest::Response) -> Result<T> {
    check_is_success(response)
        .await?
        .json::<T>()
        .await
        .map_err(Into::into)
}

impl JupiterClient {
    fn build_inner(base_path: impl AsRef<str>, client: Client, api_key: Option<String>) -> Self {
        Self {
            inner: Arc::new(JupiterClientRef {
                client,
                base_path: base_path.as_ref().to_string(),
                api_key,
            }),
        }
    }

    pub fn new(base_path: impl AsRef<str>) -> anyhow::Result<Self> {
        Ok(Self::build_inner(
            base_path,
            ClientBuilder::new().build()?,
            None,
        ))
    }

    pub fn new_with_apikey(
        base_path: impl AsRef<str>,
        api_key: impl AsRef<str>,
    ) -> anyhow::Result<Self> {
        Ok(Self::build_inner(
            base_path,
            ClientBuilder::new().build()?,
            Some(api_key.as_ref().to_string()),
        ))
    }

    pub fn new_with_timeout(base_path: impl AsRef<str>, timeout: Duration) -> anyhow::Result<Self> {
        let client = ClientBuilder::new().timeout(timeout).build()?;

        Ok(Self::build_inner(base_path, client, None))
    }

    pub fn new_with_timeout_and_apikey(
        base_path: impl AsRef<str>,
        timeout: Duration,
        api_key: impl AsRef<str>,
    ) -> anyhow::Result<Self> {
        let client = ClientBuilder::new().timeout(timeout).build()?;

        Ok(Self::build_inner(
            base_path,
            client,
            Some(api_key.as_ref().to_string()),
        ))
    }

    pub fn new_with_client(base_path: impl AsRef<str>, client: Client) -> Self {
        Self::build_inner(base_path, client, None)
    }

    pub fn new_with_client_and_apikey(
        base_path: impl AsRef<str>,
        client: Client,
        api_key: impl AsRef<str>,
    ) -> Self {
        Self::build_inner(base_path, client, Some(api_key.as_ref().to_string()))
    }

    pub fn base_path(&self) -> &str {
        &self.inner.base_path
    }

    pub fn api_key(&self) -> Option<&str> {
        self.inner.api_key.as_deref()
    }

    pub async fn request(
        &self,
        method: reqwest::Method,
        path: impl AsRef<str>,
    ) -> Result<reqwest::Response> {
        let url = format!("{}{}", self.inner.base_path, path.as_ref());
        let mut builder = self.inner.client.request(method, &url);

        if let Some(ref api_key) = self.inner.api_key {
            builder = builder.header("x-api-key", api_key);
        }

        Ok(builder.send().await?)
    }

    pub async fn quote(&self, request: &QuoteRequest) -> Result<QuoteResponse> {
        let url = format!("{}/quote", self.inner.base_path);
        let internal = InternalQuoteRequest::from(request.clone());

        let mut builder = self
            .inner
            .client
            .get(&url)
            .query(&internal)
            .query(&request.quote_args);

        if let Some(ref api_key) = self.inner.api_key {
            builder = builder.header("x-api-key", api_key);
        }

        check_and_deserialize(builder.send().await?).await
    }

    pub async fn quote_raw(&self, request: &QuoteRequest) -> Result<reqwest::Response> {
        let url = format!("{}/quote", self.inner.base_path);
        let internal = InternalQuoteRequest::from(request.clone());

        let mut builder = self
            .inner
            .client
            .get(&url)
            .query(&internal)
            .query(&request.quote_args);

        if let Some(ref api_key) = self.inner.api_key {
            builder = builder.header("x-api-key", api_key);
        }

        Ok(builder.send().await?)
    }

    pub async fn swap(
        &self,
        swap_request: &SwapRequest,
        extra_args: Option<HashMap<String, String>>,
    ) -> Result<SwapResponse> {
        let mut builder = self
            .inner
            .client
            .post(format!("{}/swap", self.inner.base_path))
            .query(&extra_args)
            .json(swap_request);

        if let Some(ref api_key) = self.inner.api_key {
            builder = builder.header("x-api-key", api_key);
        }

        check_and_deserialize(builder.send().await?).await
    }

    pub async fn swap_instructions(
        &self,
        swap_request: &SwapRequest,
    ) -> Result<SwapInstructionsResponse> {
        let mut builder = self
            .inner
            .client
            .post(format!("{}/swap-instructions", self.inner.base_path))
            .json(swap_request);

        if let Some(ref api_key) = self.inner.api_key {
            builder = builder.header("x-api-key", api_key);
        }

        check_and_deserialize::<SwapInstructionsResponseInternal>(builder.send().await?)
            .await
            .map(Into::into)
    }
}

#[cfg(test)]
mod tests {
    use super::solana_sdk::pubkey;
    use super::*;

    #[tokio::test]
    async fn test_swap() {
        let client = JupiterClient::new_with_apikey(
            "https://api.jup.ag/swap/v1",
            "6fec26c0-9178-4d63-abe2-e29f8a10107f",
        )
        .unwrap();

        let quote = client
            .quote(&QuoteRequest {
                input_mint: pubkey!("So11111111111111111111111111111111111111112"),
                output_mint: pubkey!("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"),
                amount: 1_000_000_000,
                ..Default::default()
            })
            .await
            .unwrap();

        println!("{:#?}", quote);
    }
}