use std::collections::BTreeMap;
use crate::venue::Balance;
pub const DUE_MIN_USD: f64 = 50.0;
fn stable(bal: &BTreeMap<String, Balance>, assets: &[&str]) -> f64 {
assets
.iter()
.filter_map(|a| bal.get(*a))
.map(|b| b.free + b.locked)
.sum()
}
pub fn revx_stable_from(bal: &BTreeMap<String, Balance>) -> f64 {
bal.iter()
.filter(|(a, _)| matches!(a.as_str(), "USD" | "USDC"))
.map(|(_, b)| b.free + b.locked)
.fold(0.0, |acc, x| acc + x)
}
pub fn gate_stable_from(bal: &BTreeMap<String, Balance>) -> f64 {
stable(bal, &["USDT", "USDC"])
}
pub fn gate_share(alloc: &[(String, f64)], routing: &[(String, String)]) -> f64 {
let mut by_exch: BTreeMap<&str, f64> = BTreeMap::new();
for (sym, exch) in routing {
let w = alloc
.iter()
.find(|(s, _)| s == sym)
.map_or(0.0, |(_, w)| *w);
*by_exch.entry(exch.as_str()).or_insert(0.0) += w;
}
let gate = by_exch.get("gate").copied().unwrap_or(0.0);
let working = by_exch.get("revx").copied().unwrap_or(0.0) + gate;
if working != 0.0 {
gate / working
} else {
0.0
}
}
pub fn gate_gap(
rx_stable: f64,
gate_stable: f64,
alloc: &[(String, f64)],
routing: &[(String, String)],
) -> f64 {
(rx_stable + gate_stable) * gate_share(alloc, routing) - gate_stable
}
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub struct FundingCard {
pub gate_stable: f64,
pub gate_target: f64,
pub gate_share_pct: f64,
pub gate_gap: f64,
pub revx_stable: f64,
pub deposit_headroom: f64,
pub reserved_on_revx: f64,
pub revx_sendable: f64,
pub in_flight: bool,
pub due: bool,
pub send_now: f64,
pub revx_usdc_free: f64,
}
pub fn card(
rx_stable: f64,
gate_stable: f64,
usdc_free: f64,
alloc: &[(String, f64)],
routing: &[(String, String)],
) -> FundingCard {
let share = gate_share(alloc, routing);
let gap = gate_gap(rx_stable, gate_stable, alloc, routing);
let usdc_free = rungbot_core::watch::pyfmt::round(usdc_free.max(0.0), 2);
let due = gap >= DUE_MIN_USD && usdc_free >= DUE_MIN_USD;
let headroom = if share != 0.0 && gap < DUE_MIN_USD {
(-gap + DUE_MIN_USD) / share
} else {
0.0
};
FundingCard {
gate_stable,
gate_target: gap + gate_stable,
gate_share_pct: share * 100.0,
gate_gap: gap,
revx_stable: rx_stable,
deposit_headroom: headroom,
reserved_on_revx: onramp_reserve(rx_stable, gate_stable, alloc, routing),
revx_sendable: usdc_free,
in_flight: gap >= DUE_MIN_USD && !due,
due,
send_now: if due { gap.min(usdc_free) } else { 0.0 },
revx_usdc_free: usdc_free,
}
}
pub fn onramp_reserve(
rx_stable: f64,
gate_stable: f64,
alloc: &[(String, f64)],
routing: &[(String, String)],
) -> f64 {
let gap = gate_gap(rx_stable, gate_stable, alloc, routing);
if gap >= DUE_MIN_USD {
gap
} else {
0.0
}
}
#[cfg(test)]
mod tests {
use super::*;
fn b(free: f64, locked: f64) -> Balance {
Balance { free, locked }
}
#[test]
fn the_reserve_is_the_gap_to_the_gate_share() {
let alloc = vec![("AAA".to_string(), 3.0), ("BBB".to_string(), 1.0)];
let routing = vec![
("AAA".to_string(), "revx".to_string()),
("BBB".to_string(), "gate".to_string()),
];
assert_eq!(gate_share(&alloc, &routing), 0.25);
assert_eq!(onramp_reserve(900.0, 100.0, &alloc, &routing), 150.0);
assert_eq!(onramp_reserve(750.0, 210.0, &alloc, &routing), 0.0);
let mut rx = BTreeMap::new();
rx.insert("USD".to_string(), b(10.0, 5.0));
rx.insert("USDC".to_string(), b(1.0, 0.0));
rx.insert("AAA".to_string(), b(9.0, 0.0));
assert_eq!(revx_stable_from(&rx), 16.0);
}
}