Skip to main content

tycho_simulation/rfq/protocols/native/
client_builder.rs

1use std::collections::HashSet;
2
3use tokio::time::Duration;
4use tycho_common::{models::Chain, Bytes};
5
6use super::client::NativeClient;
7use crate::rfq::{
8    constants::get_native_auth, errors::RFQError, protocols::utils::default_quote_tokens_for_chain,
9};
10
11pub struct NativeClientBuilder {
12    chain: Chain,
13    api_key: String,
14    tokens: HashSet<Bytes>,
15    tvl: f64,
16    quote_tokens: Option<HashSet<Bytes>>,
17    poll_time: Duration,
18    quote_timeout: Duration,
19}
20
21impl NativeClientBuilder {
22    pub fn new(chain: Chain, api_key: String) -> Self {
23        Self {
24            chain,
25            api_key,
26            tokens: HashSet::new(),
27            tvl: 100.0,
28            quote_tokens: None,
29            poll_time: Duration::from_secs(5),
30            quote_timeout: Duration::from_secs(5),
31        }
32    }
33
34    pub fn from_env(chain: Chain) -> Result<Self, RFQError> {
35        let auth = get_native_auth()?;
36        Ok(Self::new(chain, auth.key))
37    }
38
39    pub fn tokens(mut self, tokens: HashSet<Bytes>) -> Self {
40        self.tokens = tokens;
41        self
42    }
43
44    pub fn tvl_threshold(mut self, tvl: f64) -> Self {
45        self.tvl = tvl;
46        self
47    }
48
49    pub fn quote_tokens(mut self, quote_tokens: HashSet<Bytes>) -> Self {
50        self.quote_tokens = Some(quote_tokens);
51        self
52    }
53
54    pub fn poll_time(mut self, poll_time: Duration) -> Self {
55        self.poll_time = poll_time;
56        self
57    }
58
59    pub fn quote_timeout(mut self, quote_timeout: Duration) -> Self {
60        self.quote_timeout = quote_timeout;
61        self
62    }
63
64    pub fn build(self) -> Result<NativeClient, RFQError> {
65        let quote_tokens = match self.quote_tokens {
66            Some(tokens) => tokens,
67            None => default_quote_tokens_for_chain(&self.chain)?,
68        };
69
70        NativeClient::new(
71            self.chain,
72            self.api_key,
73            self.tokens,
74            self.tvl,
75            quote_tokens,
76            self.poll_time,
77            self.quote_timeout,
78        )
79    }
80}