Skip to main content

vaea_flash_sdk/
profitability.rs

1/// VAEA Flash — Profitability Calculator (Rust)
2///
3/// Calculates if a flash loan strategy is profitable after all fees.
4/// Uses data from our own QuoteResponse — zero external dependencies.
5
6use crate::types::QuoteResponse;
7
8/// Parameters for profitability check.
9pub struct ProfitabilityParams {
10    /// Token symbol or mint
11    pub token: String,
12    /// Borrow amount in human units
13    pub amount: f64,
14    /// Expected gross profit from strategy in SOL (e.g. arb profit after repay)
15    pub expected_revenue: f64,
16    /// Optional Jito tip in SOL
17    pub jito_tip: f64,
18    /// Optional priority fee in SOL
19    pub priority_fee: f64,
20}
21
22/// Profitability analysis result.
23pub struct ProfitabilityResult {
24    /// Whether the strategy is net-profitable
25    pub profitable: bool,
26    /// Net profit in SOL (negative = loss)
27    pub net_profit: f64,
28    /// Detailed cost breakdown
29    pub breakdown: ProfitabilityBreakdown,
30    /// Recommendation: "send" if profit > 2x costs, "wait" if marginal, "abort" if unprofitable
31    pub recommendation: Recommendation,
32}
33
34pub struct ProfitabilityBreakdown {
35    pub revenue: f64,
36    pub vaea_fee: f64,
37    pub network_fee: f64,
38    pub priority_fee: f64,
39    pub jito_tip: f64,
40    pub total_cost: f64,
41}
42
43#[derive(Debug, Clone, Copy, PartialEq)]
44pub enum Recommendation {
45    /// Clear profit (> 2x costs)
46    Send,
47    /// Marginal profit
48    Wait,
49    /// Unprofitable
50    Abort,
51}
52
53/// Calculate profitability from a QuoteResponse.
54pub fn calculate_profitability(
55    quote: &QuoteResponse,
56    params: &ProfitabilityParams,
57) -> ProfitabilityResult {
58    let vaea_fee = quote.fee_breakdown.vaea_fee;
59    let network_fee = 0.000005; // ~5000 lamports base fee
60    let priority_fee = params.priority_fee;
61    let jito_tip = params.jito_tip;
62
63    let total_cost = vaea_fee + network_fee + priority_fee + jito_tip;
64    let net_profit = params.expected_revenue - total_cost;
65
66    let recommendation = if net_profit > total_cost * 2.0 {
67        Recommendation::Send
68    } else if net_profit > 0.0 {
69        Recommendation::Wait
70    } else {
71        Recommendation::Abort
72    };
73
74    ProfitabilityResult {
75        profitable: net_profit > 0.0,
76        net_profit,
77        breakdown: ProfitabilityBreakdown {
78            revenue: params.expected_revenue,
79            vaea_fee,
80            network_fee,
81            priority_fee,
82            jito_tip,
83            total_cost,
84        },
85        recommendation,
86    }
87}