use std::{borrow::Cow, fmt, future::Future, pin::Pin, sync::Arc, time::Duration};
use reqwest::{Url, header::HeaderValue};
use alloy_signer::Signer;
use crate::{
Error, MakerCredential, Quoter, Result, SavedSession, Session, api,
model::Venue,
protocol::{checks::deployment::check_deployment, signing::SigningKey},
store::Journal,
transport::{self, Clock, Inner},
types::EscrowEnvironment,
};
const USER_AGENT: &str = concat!("lamarge-rust/", env!("CARGO_PKG_VERSION"));
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Environment {
Production,
Development,
Custom {
api_url: String,
app_origin: String,
},
}
impl Environment {
fn urls(&self) -> (&str, &str) {
match self {
Self::Production => ("https://api.lamarge.trade", "https://app.lamarge.trade"),
Self::Development => ("https://api.dev.lamarge.trade", "https://app.dev.lamarge.trade"),
Self::Custom { api_url, app_origin } => (api_url, app_origin),
}
}
fn escrow_environment(&self) -> Option<EscrowEnvironment> {
match self {
Self::Production => Some(EscrowEnvironment::Production),
Self::Development => Some(EscrowEnvironment::Development),
Self::Custom { .. } => None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RetryPolicy {
max_attempts: u32,
base_delay: Duration,
max_delay: Duration,
}
impl RetryPolicy {
pub fn new(max_attempts: u32, base_delay: Duration, max_delay: Duration) -> Self {
Self { max_attempts: max_attempts.max(1), base_delay, max_delay }
}
pub const fn none() -> Self {
Self { max_attempts: 1, base_delay: Duration::ZERO, max_delay: Duration::ZERO }
}
pub(crate) const fn max_attempts(&self) -> u32 {
self.max_attempts
}
pub(crate) const fn base_delay(&self) -> Duration {
self.base_delay
}
pub(crate) const fn max_delay(&self) -> Duration {
self.max_delay
}
}
impl Default for RetryPolicy {
fn default() -> Self {
Self::new(3, Duration::from_millis(500), Duration::from_secs(5))
}
}
#[derive(Clone)]
pub struct Client {
inner: Arc<Inner>,
}
impl Client {
pub fn new(environment: Environment) -> Result<Self> {
Self::builder(environment).build()
}
pub fn builder(environment: Environment) -> ClientBuilder {
ClientBuilder {
environment,
timeout: Duration::from_secs(30),
connect_timeout: Duration::from_secs(10),
retry: RetryPolicy::default(),
user_agent_suffix: None,
clock: None,
lighter_api: None,
lighter_reads: None,
}
}
pub fn environment(&self) -> &Environment {
&self.inner.environment
}
pub fn market(&self) -> api::Market<'_> {
api::Market { inner: &self.inner, auth: None }
}
pub fn explorer(&self) -> api::Explorer<'_> {
api::Explorer { inner: &self.inner, auth: None }
}
pub fn events(&self) -> api::PublicEvents<'_> {
api::PublicEvents { inner: &self.inner, auth: None }
}
pub async fn check_deployment(&self) -> Result<EscrowEnvironment> {
let venues: Vec<Venue> = self.market().venues().await?;
Ok(check_deployment(&venues, self.inner.environment.escrow_environment())?)
}
pub async fn sign_in<S, J>(&self, signer: S, journal: J) -> Result<Session>
where
S: Signer + Send + Sync + 'static,
J: Into<Journal>,
{
Session::sign_in(self, SigningKey::new(signer), journal.into()).await
}
pub async fn resume_session<S, J>(
&self,
saved: SavedSession,
signer: S,
journal: J,
) -> Result<Session>
where
S: Signer + Send + Sync + 'static,
J: Into<Journal>,
{
Session::resume(self, saved, SigningKey::new(signer), journal.into()).await
}
pub async fn quoter<S>(&self, credential: MakerCredential, quote_signer: S) -> Result<Quoter>
where
S: Signer + Send + Sync + 'static,
{
Quoter::start(self, credential, SigningKey::new(quote_signer)).await
}
pub(crate) fn inner(&self) -> &Arc<Inner> {
&self.inner
}
}
impl fmt::Debug for Client {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Client")
.field("environment", &self.inner.environment)
.field("api", &self.inner.api)
.finish_non_exhaustive()
}
}
pub trait LighterReads: Send + Sync {
fn get<'a>(&'a self, url: &'a str) -> LighterRead<'a>;
}
impl<T: LighterReads + ?Sized> LighterReads for Arc<T> {
fn get<'a>(&'a self, url: &'a str) -> LighterRead<'a> {
(**self).get(url)
}
}
pub type LighterRead<'a> = Pin<
Box<
dyn Future<
Output = std::result::Result<
(u16, Vec<u8>),
Box<dyn std::error::Error + Send + Sync>,
>,
> + Send
+ 'a,
>,
>;
#[must_use = "a builder does nothing until built"]
pub struct ClientBuilder {
environment: Environment,
timeout: Duration,
connect_timeout: Duration,
retry: RetryPolicy,
user_agent_suffix: Option<String>,
clock: Option<Clock>,
lighter_api: Option<String>,
lighter_reads: Option<Arc<dyn LighterReads>>,
}
impl ClientBuilder {
pub fn timeout(mut self, per_attempt: Duration) -> Self {
self.timeout = per_attempt;
self
}
pub fn connect_timeout(mut self, timeout: Duration) -> Self {
self.connect_timeout = timeout;
self
}
pub fn retry(mut self, policy: RetryPolicy) -> Self {
self.retry = policy;
self
}
pub fn user_agent_suffix(mut self, suffix: impl Into<String>) -> Self {
self.user_agent_suffix = Some(suffix.into());
self
}
pub fn lighter_reads(mut self, reads: impl LighterReads + 'static) -> Self {
self.lighter_reads = Some(Arc::new(reads));
self
}
#[cfg(test)]
pub(crate) fn clock(mut self, clock: Clock) -> Self {
self.clock = Some(clock);
self
}
#[cfg(test)]
pub(crate) fn lighter_api(mut self, base: impl Into<String>) -> Self {
self.lighter_api = Some(base.into());
self
}
pub fn build(self) -> Result<Client> {
let (api_url, app_origin) = self.environment.urls();
let api = api_base(api_url)?;
let origin = app_origin_of(app_origin)?;
let user_agent = match &self.user_agent_suffix {
Some(suffix) => HeaderValue::try_from(format!("{USER_AGENT} {suffix}")),
None => Ok(HeaderValue::from_static(USER_AGENT)),
}
.map_err(|_| Error::Config("user agent suffix is not a valid header value".into()))?;
let (http_tls, ws_tls) = transport::tls::configs()?;
let http = reqwest::Client::builder()
.tls_backend_preconfigured(http_tls)
.redirect(reqwest::redirect::Policy::none())
.retry(reqwest::retry::never())
.timeout(self.timeout)
.connect_timeout(self.connect_timeout)
.user_agent(user_agent)
.build()
.map_err(|error| Error::Config(format!("http client: {error}").into()))?;
let clock = self.clock.unwrap_or_else(|| Arc::new(chrono::Utc::now));
Ok(Client {
inner: Arc::new(Inner {
http,
api,
origin,
lighter_api: self.lighter_api.unwrap_or_else(|| transport::LIGHTER_API.to_owned()),
lighter_reads: self.lighter_reads,
ws_tls,
retry: self.retry,
environment: self.environment,
clock,
}),
})
}
}
impl fmt::Debug for ClientBuilder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ClientBuilder")
.field("environment", &self.environment)
.field("timeout", &self.timeout)
.field("connect_timeout", &self.connect_timeout)
.field("retry", &self.retry)
.field("user_agent_suffix", &self.user_agent_suffix)
.field("lighter_reads", &self.lighter_reads.is_some())
.finish_non_exhaustive()
}
}
fn guarded_url(text: &str, what: &str) -> Result<Url> {
let invalid = |reason: &str| Error::Config(Cow::Owned(format!("{what} {text:?}: {reason}")));
let url = Url::parse(text).map_err(|error| invalid(&error.to_string()))?;
let local = matches!(url.host_str(), Some("localhost" | "127.0.0.1" | "[::1]"));
match url.scheme() {
"https" => {}
"http" if local => {}
"http" => return Err(invalid("https is required outside localhost")),
_ => return Err(invalid("not an http(s) URL")),
}
if url.query().is_some()
|| url.fragment().is_some()
|| !url.username().is_empty()
|| url.password().is_some()
{
return Err(invalid("credentials, query and fragment are not allowed"));
}
Ok(url)
}
fn api_base(text: &str) -> Result<String> {
let url = guarded_url(text, "api url")?;
Ok(url.as_str().trim_end_matches('/').to_owned())
}
fn app_origin_of(text: &str) -> Result<String> {
let url = guarded_url(text, "app origin")?;
if url.path() != "/" {
return Err(Error::Config(format!("app origin {text:?}: an origin has no path").into()));
}
Ok(url.origin().ascii_serialization())
}
#[cfg(test)]
mod tests {
use super::*;
fn custom(api_url: &str, app_origin: &str) -> Result<Client> {
Client::new(Environment::Custom { api_url: api_url.into(), app_origin: app_origin.into() })
}
#[test]
fn named_environments_build() {
let client = Client::new(Environment::Production).unwrap();
assert_eq!(client.inner().api, "https://api.lamarge.trade");
assert_eq!(client.inner().origin, "https://app.lamarge.trade");
assert_eq!(client.environment(), &Environment::Production);
let client = Client::new(Environment::Development).unwrap();
assert_eq!(client.inner().api, "https://api.dev.lamarge.trade");
assert_eq!(client.inner().origin, "https://app.dev.lamarge.trade");
assert_eq!(client.inner().lighter_api, transport::LIGHTER_API);
}
#[test]
fn https_guard() {
assert!(custom("https://api.example.com/base/", "https://app.example.com").is_ok());
for local in ["http://localhost:8080", "http://127.0.0.1:1", "http://[::1]:9"] {
assert!(custom(local, local).is_ok(), "{local}");
}
for (api, origin) in [
("http://api.example.com", "https://app.example.com"),
("https://api.example.com", "http://app.example.com"),
("http://localhost.example.com", "https://app.example.com"),
("ftp://api.example.com", "https://app.example.com"),
("api.example.com", "https://app.example.com"),
("https://user:pass@api.example.com", "https://app.example.com"),
("https://api.example.com?x=1", "https://app.example.com"),
("https://api.example.com", "https://app.example.com/login"),
] {
assert!(matches!(custom(api, origin), Err(Error::Config(_))), "{api} {origin}");
}
}
#[test]
fn api_base_keeps_its_path_without_trailing_slash() {
let client = custom("https://api.example.com/base/", "https://app.example.com/").unwrap();
assert_eq!(client.inner().api, "https://api.example.com/base");
assert_eq!(client.inner().origin, "https://app.example.com");
let client =
custom("https://api.example.com:8443", "https://app.example.com:8443").unwrap();
assert_eq!(client.inner().api, "https://api.example.com:8443");
assert_eq!(client.inner().origin, "https://app.example.com:8443");
}
#[test]
fn an_invalid_user_agent_suffix_is_refused() {
let error =
Client::builder(Environment::Production).user_agent_suffix("bad\nsuffix").build();
assert!(matches!(error, Err(Error::Config(_))));
}
#[test]
fn retry_policy_counts_the_first_attempt() {
assert_eq!(RetryPolicy::new(0, Duration::ZERO, Duration::ZERO).max_attempts(), 1);
assert_eq!(RetryPolicy::none().max_attempts(), 1);
let default = RetryPolicy::default();
assert_eq!(default.max_attempts(), 3);
assert_eq!(default.base_delay(), Duration::from_millis(500));
assert_eq!(default.max_delay(), Duration::from_secs(5));
}
#[test]
fn injected_clock_and_websocket_tls_reach_the_transport() {
let fixed = chrono::DateTime::from_timestamp(1_800_000_000, 0).unwrap();
let client = Client::builder(Environment::Production)
.clock(Arc::new(move || fixed))
.build()
.unwrap();
assert_eq!(client.inner().now(), fixed);
assert_eq!(client.inner().ws_tls.alpn_protocols, [b"http/1.1".to_vec()]);
}
#[test]
fn client_is_send_sync_and_debug_shows_no_internals() {
fn check<T: Send + Sync + 'static>() {}
check::<Client>();
let client = Client::new(Environment::Production).unwrap();
assert!(format!("{client:?}").contains("api.lamarge.trade"));
}
}