use std::fmt;
use std::time::Duration;
use reqwest::Method;
use reqwest::header::HeaderValue;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize, Serializer};
use serde_json::{Map, Number, Value};
use crate::error::{Error, ErrorKind, Result, Secret};
use crate::http::{Call, base_url, http_json, segment, sensitive, url};
use crate::transport::http_client;
fn decimal<S: Serializer>(v: &u64, s: S) -> std::result::Result<S::Ok, S::Error> {
s.serialize_str(&v.to_string())
}
fn decimal_opt<S: Serializer>(v: &Option<u64>, s: S) -> std::result::Result<S::Ok, S::Error> {
match v {
Some(v) => decimal(v, s),
None => s.serialize_none(),
}
}
fn decimal_or_null<S: Serializer>(
v: &Option<Option<u64>>,
s: S,
) -> std::result::Result<S::Ok, S::Error> {
match v {
Some(Some(v)) => decimal(v, s),
_ => s.serialize_none(),
}
}
pub const ROUTES: &[&str] = &[
"POST /open",
"POST /update",
"POST /withdraw",
"POST /close",
"POST /send",
"GET /quote",
"GET /positions",
"GET /position/{address}",
"GET /history",
"GET /health",
];
const RETRYABLE_CODES: [&str; 5] = [
"rate_limited",
"rate_limited_open",
"rpc_unavailable",
"quote_unavailable",
"door_busy",
];
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum DcaFrequency {
Hourly,
Daily,
Weekly,
Biweekly,
Monthly,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum DcaAmount {
#[serde(rename = "amountPerCycle", serialize_with = "decimal")]
PerCycle(u64),
#[serde(rename = "totalAmount", serialize_with = "decimal")]
Total(u64),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum DcaInterval {
#[serde(rename = "intervalSecs")]
Seconds(u64),
#[serde(rename = "frequency")]
Frequency(DcaFrequency),
}
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DcaOpenRequest {
pub owner: String,
pub input_mint: String,
pub output_mint: String,
pub cycles: u32,
#[serde(flatten)]
pub amount: DcaAmount,
#[serde(flatten)]
pub interval: DcaInterval,
#[serde(skip_serializing_if = "Option::is_none")]
pub max_slippage_bps: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub start_at: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub index: Option<u8>,
#[serde(
skip_serializing_if = "Option::is_none",
serialize_with = "decimal_opt"
)]
pub min_out_e6: Option<u64>,
#[serde(
skip_serializing_if = "Option::is_none",
serialize_with = "decimal_opt"
)]
pub max_price: Option<u64>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct DcaUpdateRequest {
pub owner: String,
pub position: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub paused: Option<bool>,
#[serde(
skip_serializing_if = "Option::is_none",
serialize_with = "decimal_opt"
)]
pub min_out_e6: Option<u64>,
#[serde(
skip_serializing_if = "Option::is_none",
serialize_with = "decimal_or_null"
)]
pub max_price: Option<Option<u64>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub max_slippage_bps: Option<u32>,
}
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct DcaWithdrawRequest {
pub owner: String,
pub position: String,
#[serde(
skip_serializing_if = "Option::is_none",
serialize_with = "decimal_opt"
)]
pub amount: Option<u64>,
}
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct DcaCloseRequest {
pub owner: String,
pub position: String,
}
#[derive(Debug, Clone, PartialEq, Serialize, Default)]
pub struct DcaSendRequest {
pub txn_base64: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub built_txn_base64: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct DcaQuoteRequest {
pub input_mint: String,
pub output_mint: String,
pub amount: u64,
pub slippage_bps: Option<u32>,
pub owner: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DcaBuild {
pub ok: bool,
pub txn_base64: String,
pub blockhash: String,
pub last_valid_block_height: String,
pub position: String,
pub cost: Map<String, Value>,
pub summary: Map<String, Value>,
#[serde(flatten)]
pub extra: Map<String, Value>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DcaOpenBuild {
pub authority: String,
pub escrow_account: String,
pub index: Number,
pub plan: Map<String, Value>,
#[serde(flatten)]
pub build: DcaBuild,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DcaSent {
pub ok: bool,
pub kind: String,
pub signature: String,
pub sent: bool,
pub position: String,
#[serde(flatten)]
pub extra: Map<String, Value>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DcaQuote {
pub ok: bool,
pub out: String,
pub min_out: String,
pub rate_e6: String,
pub fee_bps: Number,
pub in_decimals: Number,
pub out_decimals: Number,
pub amount: String,
pub value_usdc: String,
pub limits: Map<String, Value>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub cost: Option<Map<String, Value>>,
#[serde(flatten)]
pub extra: Map<String, Value>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DcaBuy {
pub signature: String,
pub cycle: Number,
pub in_amount: String,
pub out_amount: String,
pub ts: String,
pub slot: Option<Number>,
pub opened_slot: Number,
pub verdict: String,
pub detail: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DcaPosition {
pub address: String,
pub owner: String,
pub in_mint: String,
pub out_mint: String,
pub cycles_total: Number,
pub cycles_done: Number,
pub interval_secs: Number,
pub amount_per_cycle: String,
pub active: bool,
pub status: String,
pub opened_slot: Option<Number>,
pub opened_signature: Option<String>,
#[serde(flatten)]
pub fields: Map<String, Value>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DcaPositionDetail {
pub owner_in: Option<String>,
pub owner_out: Option<String>,
pub fills: Vec<DcaBuy>,
#[serde(flatten)]
pub position: DcaPosition,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DcaClosedRecord {
pub address: String,
pub closed: bool,
pub closed_slot: Number,
pub opened_slot: Number,
pub opened_signature: Option<String>,
pub fills: Vec<DcaBuy>,
#[serde(flatten)]
pub fields: Map<String, Value>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum DcaPositionAnswer {
Closed(DcaClosedRecord),
Open(DcaPositionDetail),
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DcaPositions {
pub ok: bool,
pub owner: String,
pub positions: Vec<DcaPosition>,
pub next: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DcaHistoryOrder {
pub address: String,
pub in_mint: String,
pub out_mint: String,
pub cycles_done: Number,
pub cycles_total: Number,
pub opened_slot: Number,
pub closed_slot: Number,
pub ended: String,
#[serde(flatten)]
pub fields: Map<String, Value>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DcaHistory {
pub ok: bool,
pub owner: String,
pub orders: Vec<DcaHistoryOrder>,
pub next: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DcaHealth {
pub ok: bool,
pub keeper: String,
#[serde(flatten)]
pub extra: Map<String, Value>,
}
#[derive(Clone)]
pub struct DcaOptions {
pub base_url: String,
pub api_key: Option<String>,
pub timeout: Duration,
}
impl Default for DcaOptions {
fn default() -> Self {
Self {
base_url: "https://dex.carbium.io/api/dca".into(),
api_key: None,
timeout: Duration::from_secs(30),
}
}
}
impl fmt::Debug for DcaOptions {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("DcaOptions")
.field("base_url", &self.base_url)
.field("api_key", &self.api_key.clone().map(Secret::new))
.field("timeout", &self.timeout)
.finish()
}
}
pub struct CarbiumDca {
base_url: String,
key: Option<(Secret, HeaderValue)>,
http: reqwest::Client,
timeout: Duration,
}
impl fmt::Debug for CarbiumDca {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("CarbiumDca")
.field("base_url", &self.base_url)
.field("api_key", &self.key.as_ref().map(|k| &k.0))
.field("timeout", &self.timeout)
.finish()
}
}
impl CarbiumDca {
pub fn new(options: DcaOptions) -> Result<Self> {
let key = match options.api_key.filter(|k| !k.is_empty()) {
Some(k) => Some((Secret::new(k.clone()), sensitive(&k)?)),
None => None,
};
Ok(Self {
base_url: base_url(&options.base_url)?,
key,
http: http_client()?,
timeout: options.timeout,
})
}
pub fn base_url(&self) -> &str {
&self.base_url
}
async fn call<T: DeserializeOwned>(
&self,
name: &str,
method: Method,
path: &str,
pairs: &[(&str, Option<String>)],
body: Option<Value>,
) -> Result<T> {
let headers: Vec<(&'static str, HeaderValue)> = self
.key
.iter()
.map(|(_, v)| ("x-api-key", v.clone()))
.collect();
let a = http_json(
&self.http,
method,
&url(&self.base_url, path, pairs)?,
&headers,
body.as_ref(),
Call {
kind: ErrorKind::Dca,
name,
timeout: self.timeout,
},
)
.await?;
let ok = a
.json
.as_ref()
.is_some_and(|j| j.is_object() && j.get("ok") != Some(&Value::Bool(false)));
if a.status >= 400 || !ok {
let busy = a
.str_field("err")
.is_some_and(|c| RETRYABLE_CODES.contains(&c));
return Err(a
.error(ErrorKind::Dca, name, "err", &["detail", "err"])
.with_retryable(matches!(a.status, 429 | 502 | 503 | 504) || busy)
.with_retry_after(a.retry_after_ms()));
}
a.read(ErrorKind::Dca, name)
}
async fn post<T: DeserializeOwned>(
&self,
name: &str,
path: &str,
body: &impl Serialize,
) -> Result<T> {
let body = serde_json::to_value(body)
.map_err(|e| Error::new(ErrorKind::Request, e.to_string()))?;
self.call(name, Method::POST, path, &[], Some(body)).await
}
pub async fn quote(&self, request: &DcaQuoteRequest) -> Result<DcaQuote> {
let pairs = [
("inputMint", Some(request.input_mint.clone())),
("outputMint", Some(request.output_mint.clone())),
("amount", Some(request.amount.to_string())),
("slippageBps", request.slippage_bps.map(|v| v.to_string())),
("owner", request.owner.clone()),
];
self.call("quote", Method::GET, "/quote", &pairs, None)
.await
}
pub async fn positions(&self, owner: &str, after: Option<&str>) -> Result<DcaPositions> {
self.call(
"positions",
Method::GET,
"/positions",
&[
("owner", Some(owner.into())),
("after", after.map(String::from)),
],
None,
)
.await
}
pub async fn history(&self, owner: &str, after: Option<&str>) -> Result<DcaHistory> {
self.call(
"history",
Method::GET,
"/history",
&[
("owner", Some(owner.into())),
("after", after.map(String::from)),
],
None,
)
.await
}
pub async fn position(
&self,
address: &str,
opened_slot: Option<u64>,
) -> Result<DcaPositionAnswer> {
self.call(
"position",
Method::GET,
&format!("/position/{}", segment(address)),
&[("openedSlot", opened_slot.map(|v| v.to_string()))],
None,
)
.await
}
pub async fn health(&self) -> Result<DcaHealth> {
self.call("health", Method::GET, "/health", &[], None).await
}
pub async fn open(&self, request: &DcaOpenRequest) -> Result<DcaOpenBuild> {
self.post("open", "/open", request).await
}
pub async fn update(&self, request: &DcaUpdateRequest) -> Result<DcaBuild> {
self.post("update", "/update", request).await
}
pub async fn withdraw(&self, request: &DcaWithdrawRequest) -> Result<DcaBuild> {
self.post("withdraw", "/withdraw", request).await
}
pub async fn close(&self, request: &DcaCloseRequest) -> Result<DcaBuild> {
self.post("close", "/close", request).await
}
pub async fn send(&self, request: &DcaSendRequest) -> Result<DcaSent> {
self.post("send", "/send", request).await
}
}