Skip to main content

scematica_executor/
lib.rs

1pub mod raydium;
2pub mod orca;
3pub mod meteora;
4pub mod jupiter;
5pub mod raydium_state;
6
7use anyhow::Result;
8use async_trait::async_trait;
9use scematica_core::types::DexKind;
10use solana_client::nonblocking::rpc_client::RpcClient;
11use solana_sdk::{instruction::Instruction, pubkey::Pubkey};
12use std::sync::Arc;
13
14/// Trait for decoding on-chain pool state
15pub trait StateDecoder: Send + Sync {
16    fn decode_pool_state(&self, data: &[u8]) -> Result<(u64, u64)>;
17}
18
19/// Trait for building DEX-specific swap instructions
20#[async_trait]
21pub trait SwapInstructionBuilder: Send + Sync {
22    fn dex(&self) -> DexKind;
23
24    /// Build swap instructions for this DEX
25    async fn build_swap(
26        &self,
27        pool: &Pubkey,
28        owner: &Pubkey,
29        token_in: &Pubkey,
30        token_out: &Pubkey,
31        ata_in: &Pubkey,
32        ata_out: &Pubkey,
33        amount_in: u64,
34        min_amount_out: u64,
35    ) -> Result<Vec<Instruction>>;
36}
37
38/// Factory: get the right builder for a DEX.
39/// `rpc` will be forwarded to each builder once they accept `Arc<RpcClient>` (tasks 2–4).
40pub fn get_builder(dex: DexKind, rpc: Arc<RpcClient>) -> Option<Box<dyn SwapInstructionBuilder>> {
41    match dex {
42        DexKind::Raydium => Some(Box::new(raydium::RaydiumBuilder::new(rpc.clone()))),
43        DexKind::Orca => Some(Box::new(orca::OrcaBuilder::new(rpc.clone()))),
44        DexKind::Meteora => Some(Box::new(meteora::MeteoraBuilder::new(rpc.clone()))),
45        DexKind::Jupiter => Some(Box::new(jupiter::JupiterBuilder::new())),
46        _ => None,
47    }
48}