|
| 1 | +//! Solve loop: quote each auction order and assemble single-order solutions. |
| 2 | +
|
| 3 | +use { |
| 4 | + super::{auction::Auction, solution::Solution}, |
| 5 | + crate::dex::{self, Dex}, |
| 6 | + futures::future::join_all, |
| 7 | + solana_sdk::pubkey::Pubkey, |
| 8 | + std::future::Future, |
| 9 | +}; |
| 10 | + |
| 11 | +/// Quotes one order into a swap. A seam over [`Dex`] so the loop is testable |
| 12 | +/// without the network. |
| 13 | +pub trait Quote { |
| 14 | + fn quote( |
| 15 | + &self, |
| 16 | + order: &dex::Order, |
| 17 | + taker: &Pubkey, |
| 18 | + ) -> impl Future<Output = Result<dex::Swap, dex::jupiter::Error>> + Send; |
| 19 | +} |
| 20 | + |
| 21 | +impl Quote for Dex { |
| 22 | + fn quote( |
| 23 | + &self, |
| 24 | + order: &dex::Order, |
| 25 | + taker: &Pubkey, |
| 26 | + ) -> impl Future<Output = Result<dex::Swap, dex::jupiter::Error>> + Send { |
| 27 | + self.swap(order, taker) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +/// Quote every order concurrently and return one single-order solution per |
| 32 | +/// routable order. Buys (when disabled) and orders the aggregator cannot route |
| 33 | +/// yield no candidate, the rest of the auction still proceeds. |
| 34 | +/// |
| 35 | +/// Order counts are small (bounded by the settlement account budget), so every |
| 36 | +/// order is quoted at once. |
| 37 | +pub async fn solve<Q: Quote>(quoter: &Q, auction: &Auction) -> Vec<Solution> { |
| 38 | + let candidates = auction.orders.iter().enumerate().map(|(index, order)| { |
| 39 | + let dex_order = order.to_dex_order(); |
| 40 | + async move { |
| 41 | + let swap = quoter.quote(&dex_order, &auction.taker).await.ok()?; |
| 42 | + Solution::single(index as u64, order.uid, &dex_order, swap).ok() |
| 43 | + } |
| 44 | + }); |
| 45 | + join_all(candidates).await.into_iter().flatten().collect() |
| 46 | +} |
| 47 | + |
| 48 | +#[cfg(test)] |
| 49 | +mod tests { |
| 50 | + use { |
| 51 | + super::*, |
| 52 | + crate::{ |
| 53 | + config::JupiterConfig, |
| 54 | + domain::{auction, order::OrderUid}, |
| 55 | + }, |
| 56 | + std::str::FromStr, |
| 57 | + }; |
| 58 | + |
| 59 | + // USDC and wrapped SOL mints for the live test. |
| 60 | + const USDC: &str = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"; |
| 61 | + const WSOL: &str = "So11111111111111111111111111111111111111112"; |
| 62 | + |
| 63 | + fn pubkey(byte: u8) -> Pubkey { |
| 64 | + Pubkey::new_from_array([byte; 32]) |
| 65 | + } |
| 66 | + |
| 67 | + fn order(uid: u8, side: dex::Side, sell_mint: Pubkey) -> auction::Order { |
| 68 | + auction::Order { |
| 69 | + uid: OrderUid([uid; 32]), |
| 70 | + sell_mint, |
| 71 | + buy_mint: pubkey(2), |
| 72 | + buy_destination: pubkey(3), |
| 73 | + amount: 1_000, |
| 74 | + side, |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /// Routes any sell except the `0xff` sell mint, rejects buys. |
| 79 | + struct MockQuote; |
| 80 | + |
| 81 | + impl Quote for MockQuote { |
| 82 | + fn quote( |
| 83 | + &self, |
| 84 | + order: &dex::Order, |
| 85 | + _taker: &Pubkey, |
| 86 | + ) -> impl Future<Output = Result<dex::Swap, dex::jupiter::Error>> + Send { |
| 87 | + let result = match order.side { |
| 88 | + dex::Side::Buy => Err(dex::jupiter::Error::OrderNotSupported), |
| 89 | + dex::Side::Sell if order.sell_mint == pubkey(0xff) => { |
| 90 | + Err(dex::jupiter::Error::NotFound) |
| 91 | + } |
| 92 | + dex::Side::Sell => Ok(dex::Swap { |
| 93 | + in_amount: 1_000, |
| 94 | + out_amount: 2_000, |
| 95 | + instructions: vec![], |
| 96 | + address_lookup_tables: vec![], |
| 97 | + }), |
| 98 | + }; |
| 99 | + async move { result } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + #[tokio::test] |
| 104 | + async fn emits_one_solution_per_routable_order() { |
| 105 | + let auction = Auction { |
| 106 | + id: 1, |
| 107 | + taker: pubkey(1), |
| 108 | + orders: vec![ |
| 109 | + order(0x01, dex::Side::Sell, pubkey(0x10)), // routable |
| 110 | + order(0x02, dex::Side::Sell, pubkey(0xff)), // no route |
| 111 | + order(0x03, dex::Side::Buy, pubkey(0x11)), // buys disabled |
| 112 | + ], |
| 113 | + }; |
| 114 | + |
| 115 | + let solutions = solve(&MockQuote, &auction).await; |
| 116 | + |
| 117 | + assert_eq!(solutions.len(), 1); |
| 118 | + assert_eq!(solutions[0].trades[0].order_uid, OrderUid([0x01; 32])); |
| 119 | + } |
| 120 | + |
| 121 | + #[tokio::test] |
| 122 | + async fn empty_auction_yields_no_solutions() { |
| 123 | + let auction = Auction { |
| 124 | + id: 1, |
| 125 | + taker: pubkey(1), |
| 126 | + orders: vec![], |
| 127 | + }; |
| 128 | + assert!(solve(&MockQuote, &auction).await.is_empty()); |
| 129 | + } |
| 130 | + |
| 131 | + /// Live Jupiter API. Needs network. Keyless works, set `JUPITER_API_KEY` |
| 132 | + /// for headroom. |
| 133 | + #[tokio::test] |
| 134 | + #[ignore] |
| 135 | + async fn jupiter_live_solve() { |
| 136 | + let dex = Dex::Jupiter( |
| 137 | + dex::jupiter::Jupiter::new(&JupiterConfig { |
| 138 | + endpoint: "https://api.jup.ag".parse().unwrap(), |
| 139 | + api_key: std::env::var("JUPITER_API_KEY").ok(), |
| 140 | + slippage_bps: 50, |
| 141 | + enable_buy_orders: false, |
| 142 | + }) |
| 143 | + .unwrap(), |
| 144 | + ); |
| 145 | + let auction = Auction { |
| 146 | + id: 1, |
| 147 | + taker: Pubkey::from_str(WSOL).unwrap(), |
| 148 | + orders: vec![auction::Order { |
| 149 | + uid: OrderUid([7; 32]), |
| 150 | + sell_mint: Pubkey::from_str(USDC).unwrap(), |
| 151 | + buy_mint: Pubkey::from_str(WSOL).unwrap(), |
| 152 | + buy_destination: Pubkey::from_str(WSOL).unwrap(), |
| 153 | + amount: 1_000_000, |
| 154 | + side: dex::Side::Sell, |
| 155 | + }], |
| 156 | + }; |
| 157 | + |
| 158 | + let solutions = solve(&dex, &auction).await; |
| 159 | + |
| 160 | + assert_eq!(solutions.len(), 1); |
| 161 | + assert!(!solutions[0].interactions.is_empty()); |
| 162 | + } |
| 163 | +} |
0 commit comments