#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
use std::time::{SystemTime, UNIX_EPOCH};
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
use web_time::{SystemTime, UNIX_EPOCH};
use super::{ClassicSwapReader, wire};
use crate::{
config::HttpConfig,
domain::{
NetworkId, Source, Timestamp,
oneinch::{
Context, Limits, LiquiditySources, Method, PreparedSwap, Quote, QuoteRequest,
ReturnPolicy, Spender, SwapRequest,
},
},
error::Error,
transport::{HttpClient, OperationBudget},
};
use std::fmt;
#[derive(Clone, Debug)]
pub struct OneinchHttpConfig {
http: HttpConfig,
network: NetworkId,
limits: Limits,
}
impl OneinchHttpConfig {
pub fn new(http: HttpConfig, network: NetworkId, limits: Limits) -> Result<Self, Error> {
let auth = http
.endpoint()
.headers
.get(reqwest::header::AUTHORIZATION)
.and_then(|v| v.to_str().ok());
if http.limits().max_response_bytes() > wire::MAX_DOCUMENT_BYTES
|| http.endpoint().url.query().is_some()
|| network.chain_id().value().is_zero()
|| !auth.is_some_and(|v| {
v.strip_prefix("Bearer ").is_some_and(|token| {
!token.is_empty() && token.bytes().all(|b| b.is_ascii_graphic())
})
})
{
return Err(Error::Configuration);
}
Ok(Self {
http,
network,
limits,
})
}
#[must_use]
pub const fn http_config(&self) -> &HttpConfig {
&self.http
}
#[must_use]
pub const fn network(&self) -> &NetworkId {
&self.network
}
#[must_use]
pub const fn limits(&self) -> Limits {
self.limits
}
}
pub struct OneinchClient {
config: OneinchHttpConfig,
http: HttpClient,
}
impl fmt::Debug for OneinchClient {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("OneinchClient")
.field("config", &self.config)
.finish_non_exhaustive()
}
}
impl OneinchClient {
pub fn new(config: OneinchHttpConfig) -> Result<Self, Error> {
let http = HttpClient::new(&config.http)?;
Ok(Self { config, http })
}
#[must_use]
pub const fn config(&self) -> &OneinchHttpConfig {
&self.config
}
fn context(&self, method: Method) -> Result<Context, Error> {
let tag = match method {
Method::Quote => "classic-v6.1-quote",
Method::Swap => "classic-v6.1-swap",
Method::LiquiditySources => "classic-v6.1-liquidity-sources",
Method::Spender => "classic-v6.1-spender",
};
Ok(Context {
network: self.config.network.clone(),
method,
source: Source::new(
self.config.http.provider_id(),
tag,
env!("CARGO_PKG_VERSION"),
)?,
retrieved_at: Timestamp::from_unix_seconds(
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_err(|_| Error::Configuration)?
.as_secs(),
),
})
}
fn check(&self, r: &QuoteRequest) -> Result<(), Error> {
if r.network() != &self.config.network {
return Err(Error::Configuration);
}
Ok(())
}
async fn read(
&self,
operation: &[&str],
query: &[(&str, &str)],
budget: &OperationBudget,
) -> Result<Vec<u8>, Error> {
let chain = self.config.network.chain_id().value().to_string();
let mut path = vec!["v6.1", chain.as_str()];
path.extend_from_slice(operation);
self.http
.read(&path, query, None, budget)
.await?
.into_success()
}
pub async fn quote_exact_input(&self, request: QuoteRequest) -> Result<Quote, Error> {
let budget = OperationBudget::new(self.config.http.limits())?;
budget
.run(async {
self.check(&request)?;
let fields = quote_query(&request);
let query = borrow(&fields);
let bytes = self.read(&["quote"], &query, &budget).await?;
wire::quote(
&bytes,
request,
self.context(Method::Quote)?,
self.config.limits,
)
})
.await
}
pub async fn get_liquidity_sources(&self) -> Result<LiquiditySources, Error> {
let budget = OperationBudget::new(self.config.http.limits())?;
budget
.run(async {
let bytes = self.read(&["liquidity-sources"], &[], &budget).await?;
wire::sources(
&bytes,
self.context(Method::LiquiditySources)?,
self.config.limits,
)
})
.await
}
async fn spender(&self, budget: &OperationBudget) -> Result<Spender, Error> {
let bytes = self.read(&["approve", "spender"], &[], budget).await?;
wire::spender(&bytes, self.context(Method::Spender)?)
}
pub async fn get_spender(&self) -> Result<Spender, Error> {
let budget = OperationBudget::new(self.config.http.limits())?;
budget.run(self.spender(&budget)).await
}
pub async fn prepare_swap(&self, request: SwapRequest) -> Result<PreparedSwap, Error> {
let budget = OperationBudget::new(self.config.http.limits())?;
budget
.run(async {
self.check(request.selection().request())?;
let spender = self.spender(&budget).await?;
if spender.address() != request.settings().spender {
return Err(wire::invalid());
}
let mut fields = quote_query(request.selection().request());
let s = request.settings();
fields.extend([
("from", s.from.to_string()),
("origin", s.origin.to_string()),
("receiver", s.recipient.to_string()),
("disableEstimate", s.disable_estimate.to_string()),
("allowPartialFill", "false".into()),
("compatibility", "false".into()),
("usePatching", "false".into()),
("usePermit2", "false".into()),
("forceApprove", "false".into()),
("createAccessList", "false".into()),
]);
match s.return_policy {
ReturnPolicy::Minimum(q) => fields.push(("minReturn", q.value().to_string())),
ReturnPolicy::Slippage(p) => fields.push(("slippage", p.percent())),
}
let query = borrow(&fields);
let bytes = self.read(&["swap"], &query, &budget).await?;
let (quote, tx) = wire::swap(
&bytes,
request.selection().request().clone(),
self.context(Method::Swap)?,
self.config.limits,
)?;
PreparedSwap::new(request, quote, tx, spender).map_err(|e| {
if e == Error::UnsupportedCapability {
e
} else {
wire::invalid()
}
})
})
.await
}
}
fn quote_query(r: &QuoteRequest) -> Vec<(&'static str, String)> {
let mut fields = vec![
("src", r.source().provider_address().to_string()),
("dst", r.destination().provider_address().to_string()),
("amount", r.amount().value().to_string()),
("includeTokensInfo", "true".into()),
("includeProtocols", "true".into()),
("includeGas", "true".into()),
];
let s = r.settings();
if !s.protocols.is_empty() {
fields.push((
"protocols",
s.protocols
.iter()
.map(crate::domain::oneinch::ProtocolId::as_str)
.collect::<Vec<_>>()
.join(","),
));
}
if !s.excluded_protocols.is_empty() {
fields.push((
"excludedProtocols",
s.excluded_protocols
.iter()
.map(crate::domain::oneinch::ProtocolId::as_str)
.collect::<Vec<_>>()
.join(","),
));
}
if !s.connector_tokens.is_empty() {
fields.push((
"connectorTokens",
s.connector_tokens
.iter()
.map(|a| a.provider_address().to_string())
.collect::<Vec<_>>()
.join(","),
));
}
if let Some(v) = s.gas_price {
fields.push(("gasPrice", v.value().to_string()));
}
if let Some(v) = s.complexity_level {
fields.push(("complexityLevel", v.to_string()));
}
if let Some(v) = s.parts {
fields.push(("parts", v.to_string()));
}
if let Some(v) = s.main_route_parts {
fields.push(("mainRouteParts", v.to_string()));
}
if let Some(v) = s.gas_limit {
fields.push(("gasLimit", v.to_string()));
}
fields
}
fn borrow<'a>(fields: &'a [(&'static str, String)]) -> Vec<(&'a str, &'a str)> {
fields.iter().map(|(k, v)| (*k, v.as_str())).collect()
}
impl ClassicSwapReader for OneinchClient {
async fn quote_exact_input(&self, request: QuoteRequest) -> Result<Quote, Error> {
Self::quote_exact_input(self, request).await
}
async fn get_liquidity_sources(&self) -> Result<LiquiditySources, Error> {
Self::get_liquidity_sources(self).await
}
async fn get_spender(&self) -> Result<Spender, Error> {
Self::get_spender(self).await
}
async fn prepare_swap(&self, request: SwapRequest) -> Result<PreparedSwap, Error> {
Self::prepare_swap(self, request).await
}
}