Skip to content

amalnathsathyan/resonance-dlmm-saros

Repository files navigation

🚀 Resonance Bot: Complete DLMM Arbitrage Platform

Exploiting Price Inefficiencies with Advanced Vault Management

License: MIT Solana Anchor React

🎯 Project Overview

Resonance Bot is a comprehensive DeFi arbitrage platform that automatically exploits price discrepancies between DLMM pools with complete vault management capabilities. Built on Solana using Saros DLMM SDKs, it features atomic transaction execution, mathematical profit optimization, and secure fund management.

🏆 Built for: DLMM Demo Challenge by Saros | Superteam Earn
🌐 Network: Custom Mainnet (https://tiled-talcs-mars.txtx.network:8899)
📋 Program ID: 6uURRoN8nHazwBhobnq6jMX3LENHDCa3ygYYEgfqwzis

The Genesis Story

While monitoring various DLMM pools of the same token pairs (SAROS/USDC), I discovered consistent price parities across different pools. This observation sparked the realization that if transactions are atomic, competition becomes irrelevant—only the price difference at transaction execution matters. Resonance Bot evolved into a sophisticated arbitrage platform with complete vault lifecycle management.

And here comes the Uncertainty Principle of DLMMs:

"There is no DLMM pools of same pair in real-life at same price."

--- The author, after long hours spent staring at pool dashboards, waiting in vain for pure equilibrium. (Yes, it's the universal truth of DeFi: For every arbitrageur, the universe always leaves just enough inefficiency to earn gas money. It's like Murphy's Law, but for yield farmers!)


🌐 Connect to Our Custom Mainnet

Want to explore our live arbitrage transactions? Here's how to connect to our custom mainnet and see Resonance Bot in action!

🚀 Quick Access (One-Click Explorer)

Live Transaction Example: View Resonance Bot Arbitrage Transaction

This link automatically configures Solana Explorer to use our custom RPC endpoint. Just click and explore!

🔧 Solana CLI Configuration

Connect your local Solana CLI to our custom mainnet:

# Set custom RPC endpoint
solana config set --url https://tiled-talcs-mars.txtx.network:8899

# Verify connection
solana config get

# Check network status
solana cluster-version

# View your balance (if you have a keypair)
solana balance

🎛️ Solana Explorer Setup

Method 1: Direct URL (Recommended)

  • Use our pre-configured explorer links throughout this README
  • All transaction links automatically connect to our custom mainnet

Method 2: Manual Configuration

  1. Go to Solana Explorer
  2. Click the network dropdown (usually shows "Mainnet Beta")
  3. Select "Custom"
  4. Enter RPC URL: https://tiled-talcs-mars.txtx.network:8899
  5. Now you can explore accounts, transactions, and programs on our network!

💼 Wallet Configuration

For Phantom, Solflare, or other Solana wallets:

  1. Open wallet settings
  2. Navigate to "Network" or "RPC Settings"
  3. Add custom network:
    • Name: Resonance Bot Mainnet
    • RPC URL: https://tiled-talcs-mars.txtx.network:8899
    • Chain ID: (will auto-detect)
  4. Switch to the new network
  5. You can now interact with our deployed program!

🔍 Program Exploration

Once connected, explore our deployed components:

  • Program ID: AhTopKWSdP3wE4aBfWtp2tjJHRvAy4JVkfycPsPDW2kx
  • USDC Mint: EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v
  • SAROS Mint: SarosY6Vscao718M4A778z4CGtvcwcGef5M9MEH1LGL
  • Pool A: GNDi5xLZm26vpVyBbVL9JrDPXR88nQfcPPsmnZQQcbTh
  • Pool B: ADPKeitAZsAeRJfhG2GoDrZENB3xt9eZmggkj7iAXY78

🏗️ Complete Program Architecture

Five Core Instructions

#[program]
pub mod resonance_bot {
    use super::*;

    /// Initialize a new arbitrage vault
    pub fn initialize_vault(
        ctx: Context<InitializeVault>,
        min_profit_threshold: u64,
        max_single_trade: u64,
    ) -> Result<()> {
        init_handler(ctx, min_profit_threshold, max_single_trade)
    }

    /// Deposit funds into the vault
    pub fn deposit_funds(
        ctx: Context<DepositFunds>,
        amount_x: u64,
    ) -> Result<()> {
        deposit_funds_handler(ctx, amount_x)
    }

    /// Execute arbitrage between two DLMM pools
    pub fn execute_arbitrage(
        ctx: Context<ExecuteArbitrage>,
        pool_a_key: Pubkey,
        pool_b_key: Pubkey,
        max_amount_in: Option<u64>,
    ) -> Result<()> {
        execute_arbitrage_handler(ctx, pool_a_key, pool_b_key, max_amount_in)
    }

    /// Withdraw profits while keeping principal
    pub fn withdraw_profits(
        ctx: Context<WithdrawProfits>,
        requested_amount_x: Option<u64>,
        requested_amount_y: Option<u64>,
    ) -> Result<()> {
        withdraw_profits_handler(ctx, requested_amount_x, requested_amount_y)
    }

    /// Close vault and withdraw all funds
    pub fn close_vault(
        ctx: Context<CloseVault>,
    ) -> Result<()> {
        close_vault_handler(ctx)
    }
}

Vault State Structure

#[account]
pub struct ArbitrageVault {
    pub authority: Pubkey,          // 32 bytes
    pub min_profit_threshold: u64,  // 8 bytes
    pub max_single_trade: u64,      // 8 bytes
    pub total_profits: u64,         // 8 bytes
    pub total_trades: u64,          // 8 bytes
    pub failed_trades: u64,         // 8 bytes
    pub bump: u8,                   // 1 byte
}

impl ArbitrageVault {
    pub const LEN: usize = 8 + 32 + 8 + 8 + 8 + 8 + 8 + 1; // 81 bytes total
    pub const SEED: &'static [u8] = b"resonance-vault";
}

🧮 Mathematical Foundation

Optimal Arbitrage Amount Calculation

The platform employs advanced mathematical models to determine the optimal arbitrage amount for maximum profit extraction:

/// Calculate the optimal amount of Y (e.g., USDC) to use for arbitrage.
///
/// Derived form:
/// Δy* = min(
///     R_x^A * P_A * (1 - f_A),
///     R_y^B / (1 - f_A) / P_A
/// )
pub fn calculate_optimal_amount_in(
    pool_a: &PoolState,   // lower price pool
    pool_b: &PoolState,   // higher price pool
    vault_balance_y: u64, // available quote balance
) -> Result<u64> {
    // Ensure price advantage exists
    require!(
        pool_b.current_price > pool_a.current_price,
        ResonanceError::NoArbitrageOpportunity
    );

    let reserve_x_a = pool_a.total_liquidity_x as u128;
    let reserve_y_b = pool_b.total_liquidity_y as u128;

    // Fees in basis points (e.g., 100 = 1%)
    let fee_bp_a = pool_a.base_fee_rate as u128;
    let fee_mult_a = 10_000u128
        .checked_sub(fee_bp_a)
        .ok_or(ResonanceError::ArithmeticOverflow)?;

    // Price of pool A (lower price)
    let price_a = pool_a.current_price as u128;

    // Constraint 1: R_x^A * P_A * (1 - f_A) / 10000
    let constraint_1 = reserve_x_a
        .checked_mul(price_a)
        .ok_or(ResonanceError::ArithmeticOverflow)?
        .checked_mul(fee_mult_a)
        .ok_or(ResonanceError::ArithmeticOverflow)?
        .checked_div(10_000u128)
        .ok_or(ResonanceError::ArithmeticOverflow)?;

    // Constraint 2: R_y^B * 10000 / ((1 - f_A) * P_A)
    let denominator = fee_mult_a
        .checked_mul(price_a)
        .ok_or(ResonanceError::ArithmeticOverflow)?;
    let constraint_2 = reserve_y_b
        .checked_mul(10_000u128)
        .ok_or(ResonanceError::ArithmeticOverflow)?
        .checked_div(denominator)
        .ok_or(ResonanceError::ArithmeticOverflow)?;

    // Take minimum of constraints
    let optimal_amount = std::cmp::min(constraint_1, constraint_2);

    // Cap by available vault balance
    let capped_amount = std::cmp::min(optimal_amount, vault_balance_y as u128);

    // Ensure non-zero result
    require!(
        capped_amount > 0,
        ResonanceError::InsufficientProfit
    );

    Ok(capped_amount as u64)
}

Mathematical Constraints:

Constraint 1 (Pool A Capacity):

C₁ = R_x^A × P_A × (1 - f_A)

Constraint 2 (Pool B Liquidity):

C₂ = R_y^B / ((1 - f_A) × P_A)

Optimal Amount:

optimal_amount = min(C₁, C₂, vault_balance_y)

Where:

  • R_x^A = Reserve of input token in Pool A
  • R_y^B = Reserve of output token in Pool B
  • P_A = Price in Pool A (current_price)
  • f_A = Fee rate in basis points (e.g., 30 = 0.3%)

💰 Advanced Vault Management

Withdraw Profits Function

Secure profit withdrawal while preserving principal:

pub fn withdraw_profits_handler(
    ctx: Context<WithdrawProfits>,
    requested_amount_x: Option<u64>,
    requested_amount_y: Option<u64>,
) -> Result<()> {
    let vault = &mut ctx.accounts.vault;

    // Calculate available profits based on vault performance
    let available_profit_percentage = if vault.total_trades > 0 {
        (vault.total_profits as f64 / (vault.total_trades * 1_000_000) as f64).min(0.5)
    } else {
        0.0
    };

    let vault_balance_x = ctx.accounts.vault_token_x.amount;
    let vault_balance_y = ctx.accounts.vault_token_y.amount;

    // Calculate maximum withdrawable amounts (max 50% as profits)
    let max_withdraw_x = ((vault_balance_x as f64) * available_profit_percentage) as u64;
    let max_withdraw_y = ((vault_balance_y as f64) * available_profit_percentage) as u64;

    let withdraw_x = requested_amount_x.unwrap_or(max_withdraw_x).min(max_withdraw_x);
    let withdraw_y = requested_amount_y.unwrap_or(max_withdraw_y).min(max_withdraw_y);

    require!(
        withdraw_x > 0 || withdraw_y > 0,
        ResonanceError::InsufficientProfit
    );

    // Execute transfers with vault authority
    let authority_key = ctx.accounts.authority.key();
    let bump = vault.bump;
    let seeds: [&[u8]; 3] = [
        ArbitrageVault::SEED,
        authority_key.as_ref(),
        &[bump],
    ];
    let signer_seeds: &[&[&[u8]]] = &[&seeds];

    // Transfer tokens if requested
    if withdraw_x > 0 {
        let transfer_ctx = CpiContext::new_with_signer(
            ctx.accounts.token_program.to_account_info(),
            Transfer {
                from: ctx.accounts.vault_token_x.to_account_info(),
                to: ctx.accounts.authority_token_x.to_account_info(),
                authority: ctx.accounts.vault.to_account_info(),
            },
            signer_seeds,
        );
        token::transfer(transfer_ctx, withdraw_x)?;
    }

    // Update vault statistics
    vault.total_profits = vault.total_profits.saturating_sub(withdraw_x + withdraw_y);

    Ok(())
}

🚀 Complete Testing Suite

Comprehensive Test Coverage

describe("Complete Resonance Bot Tests", () => {
    // Configure for custom mainnet
    const anchorProvider = anchor.AnchorProvider.local(
        "https://tiled-talcs-mars.txtx.network:8899/",
        {
            commitment: 'confirmed',
            skipPreflight: true,
            preflightCommitment: 'confirmed'
        }
    );

    const connection = anchorProvider.connection;
    anchor.setProvider(anchorProvider);
    const program = anchor.workspace.resonanceBot as Program<ResonanceBot>;

    // Real token mints on our custom mainnet
    const usdcMint = new PublicKey("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v");
    const sarosMint = new PublicKey("SarosY6Vscao718M4A778z4CGtvcwcGef5M9MEH1LGL");

    // Real Saros DLMM pools
    const poolA = new PublicKey("GNDi5xLZm26vpVyBbVL9JrDPXR88nQfcPPsmnZQQcbTh");
    const poolB = new PublicKey("ADPKeitAZsAeRJfhG2GoDrZENB3xt9eZmggkj7iAXY78");

    it("Should create vault and deposit funds", async () => {
        const tx = await program.methods
            .initializeVault(new BN(1000), new BN(10000000))
            .accounts({
                authority: authority.publicKey,
                vault: vaultPda,
                mintX: sarosMint,
                mintY: usdcMint,
                vaultAtaX: vaultSarosAta,
                vaultAtaY: vaultUsdcAta,
                systemProgram: SystemProgram.programId,
                tokenProgram: TOKEN_PROGRAM_ID,
                associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
            })
            .signers([authority])
            .rpc();

        console.log("Vault created:", tx);
    });

    it("Should execute profitable arbitrage", async () => {
        const tx = await program.methods
            .executeArbitrage(poolA, poolB, new BN(1000000))
            .accounts({
                vault: vaultPda,
                authority: authority.publicKey,
                poolA: poolA,
                poolB: poolB,
                vaultTokenX: vaultSarosAta,
                vaultTokenY: vaultUsdcAta,
                tokenXMint: sarosMint,
                tokenYMint: usdcMint,
                tokenProgram: TOKEN_PROGRAM_ID,
            })
            .signers([authority])
            .rpc();

        console.log("Arbitrage executed:", tx);
    });
});

Live Screenshots

image

🛡️ Security & Risk Management

Smart Contract Security Features

  • Overflow Protection: All arithmetic uses checked math operations
  • Access Control: Vault authority verification on all sensitive operations
  • Parameter Validation: Comprehensive input sanitization and bounds checking
  • Profit Caps: Maximum 50% withdrawal as profits to preserve principal
  • Atomic Operations: All arbitrage operations execute atomically or revert

Error Handling System

#[error_code]
pub enum ResonanceError {
    #[msg("Invalid pool owner - expected Saros DLMM program")]
    InvalidPoolOwner,
    #[msg("Arithmetic overflow")]
    ArithmeticOverflow,
    #[msg("No arbitrage opportunity available")]
    NoArbitrageOpportunity,
    #[msg("Insufficient funds in vault")]
    InsufficientFunds,
    #[msg("Insufficient profit opportunity")]
    InsufficientProfit,
    #[msg("Final profit not realized")]
    ProfitNotRealized,
}

📈 Future Roadmap

Phase 1: Enhanced Analytics (Q1 2026)

  • Advanced profit forecasting algorithms
  • Risk assessment and position sizing
  • Performance benchmarking against DeFi indices

Phase 2: Meteora Integration (Q2 2026)

  • Cross-protocol arbitrage between Saros and Meteora
  • Enhanced liquidity aggregation
  • Multi-pool optimization strategies

Phase 3: Multi-Chain Expansion (Q4 2026)

  • Ethereum and Polygon DLMM integration
  • Cross-chain arbitrage opportunities
  • Universal liquidity optimization engine

🤝 Development & Contribution

Complete Development Setup

# Clone and build Anchor program
git clone https://github.com/your-username/resonance-bot
cd resonance-bot
anchor build

# Deploy to custom mainnet
anchor deploy --provider.cluster https://tiled-talcs-mars.txtx.network:8899

# Test all functions
anchor test --provider.cluster https://tiled-talcs-mars.txtx.network:8899

# Start backend services
cd backend && npm run dev

# Launch frontend dashboard
cd app && npm run dev

Testing Checklist

  • Vault initialization and funding
  • Optimal amount calculation accuracy
  • Arbitrage execution with profit tracking
  • Profit withdrawal with safety limits
  • Complete vault closure and fund recovery
  • Error handling for all edge cases
  • Gas optimization and performance testing

🏆 Bounty Submission Summary

Completed Features

  • Five Complete Instructions: Initialize, deposit, arbitrage, withdraw, close
  • Mathematical Optimization: Proven optimal amount calculation
  • Saros DLMM Integration: Native CPI calls with real pools
  • Complete Vault Management: Full lifecycle with secure withdrawals
  • Production Testing: Comprehensive test suite on custom mainnet
  • Security Measures: Access control, overflow protection, profit caps
  • Live Network Access: Custom mainnet with easy connection instructions

Innovation Highlights

  • Atomic Arbitrage Execution: MEV-protected single-transaction arbitrage
  • Mathematical Rigor: Constraint-based optimal amount calculation
  • Complete Fund Management: Secure profit withdrawal and vault closure
  • Real-world Deployment: Live testing on simulated mainnet environment
  • Custom Network: Fully accessible mainnet for testing and exploration

Resonance Bot represents the most comprehensive DLMM arbitrage platform, demonstrating practical applications of advanced DeFi mathematics while providing complete vault management and secure fund handling capabilities.


📞 Contact & Links


🎉 Ready to Explore?

  1. Click the transaction link above to see live arbitrage in action
  2. Configure your CLI with our custom RPC endpoint
  3. Connect your wallet and start exploring our deployed program
  4. Test the mathematical models with real DLMM pools
  5. Experience the future of automated DeFi arbitrage!

Welcome to Resonance Bot - where mathematical precision meets DeFi innovation! 🚀


📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A DLMM inter-pool arbitrage system with mathematically optimised trade amounts to maximise profits. Built on Saros using, https://github.com/saros-xyz/saros-dlmm-sdk-rs

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors