pub mod api_types;
mod client;
use api_types::{Amount, ExternalOrder, OrderSide};
#[allow(deprecated)]
pub use client::{
AssembleQuoteOptions, ExternalMatchClient, ExternalMatchOptions, RequestQuoteOptions,
};
mod error;
pub use error::ExternalMatchClientError;
pub const GAS_SPONSORSHIP_QUERY_PARAM: &str = "disable_gas_sponsorship";
pub const GAS_REFUND_ADDRESS_QUERY_PARAM: &str = "refund_address";
pub const GAS_REFUND_NATIVE_ETH_QUERY_PARAM: &str = "refund_native_eth";
#[derive(Debug, Clone, Default)]
pub struct ExternalOrderBuilder {
quote_mint: Option<String>,
base_mint: Option<String>,
base_amount: Option<Amount>,
quote_amount: Option<Amount>,
side: Option<OrderSide>,
min_fill_size: Option<Amount>,
}
impl ExternalOrderBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn quote_mint(mut self, quote_mint: &str) -> Self {
self.quote_mint = Some(quote_mint.to_string());
self
}
pub fn base_mint(mut self, base_mint: &str) -> Self {
self.base_mint = Some(base_mint.to_string());
self
}
#[deprecated(since = "0.1.0", note = "use base_amount() or quote_amount() instead")]
pub fn amount(self, amount: Amount) -> Self {
self.base_amount(amount)
}
pub fn base_amount(mut self, base_amount: Amount) -> Self {
self.base_amount = Some(base_amount);
self
}
pub fn quote_amount(mut self, quote_amount: Amount) -> Self {
self.quote_amount = Some(quote_amount);
self
}
pub fn side(mut self, side: OrderSide) -> Self {
self.side = Some(side);
self
}
pub fn min_fill_size(mut self, min_fill_size: Amount) -> Self {
self.min_fill_size = Some(min_fill_size);
self
}
pub fn build(self) -> Result<ExternalOrder, ExternalMatchClientError> {
let quote_mint =
self.quote_mint.ok_or(ExternalMatchClientError::invalid_order("invalid quote mint"))?;
let base_mint =
self.base_mint.ok_or(ExternalMatchClientError::invalid_order("invalid base mint"))?;
let (base_amount, quote_amount) = match (self.base_amount, self.quote_amount) {
(Some(base), None) => (base, 0),
(None, Some(quote)) => (0, quote),
(None, None) => {
return Err(ExternalMatchClientError::invalid_order(
"must set either `base_amount` or `quote_amount`",
))
},
(Some(_), Some(_)) => {
return Err(ExternalMatchClientError::invalid_order(
"cannot set both `base_amount` and `quote_amount`",
))
},
};
let side = self.side.ok_or(ExternalMatchClientError::invalid_order("invalid side"))?;
let min_fill_size = self.min_fill_size.unwrap_or_default();
Ok(ExternalOrder { quote_mint, base_mint, base_amount, quote_amount, side, min_fill_size })
}
}