tycho_simulation/rfq/protocols/hashflow/
client_builder.rs1use std::collections::HashSet;
2
3use tokio::time::Duration;
4use tycho_common::{models::Chain, Bytes};
5
6use super::client::HashflowClient;
7use crate::rfq::{errors::RFQError, protocols::utils::default_quote_tokens_for_chain};
8
9pub struct HashflowClientBuilder {
34 chain: Chain,
35 auth_user: String,
36 auth_key: String,
37 tokens: HashSet<Bytes>,
38 tvl: f64,
39 quote_tokens: Option<HashSet<Bytes>>,
40 poll_time: Duration,
41 quote_timeout: Duration,
42 via_fallback_router: bool,
43}
44
45impl HashflowClientBuilder {
46 pub fn new(chain: Chain, auth_user: String, auth_key: String) -> Self {
47 Self {
48 chain,
49 auth_user,
50 auth_key,
51 tokens: HashSet::new(),
52 tvl: 100.0, quote_tokens: None,
54 poll_time: Duration::from_secs(5), quote_timeout: Duration::from_secs(5), via_fallback_router: false,
57 }
58 }
59
60 pub fn tokens(mut self, tokens: HashSet<Bytes>) -> Self {
62 self.tokens = tokens;
63 self
64 }
65
66 pub fn tvl_threshold(mut self, tvl: f64) -> Self {
68 self.tvl = tvl;
69 self
70 }
71
72 pub fn quote_tokens(mut self, quote_tokens: HashSet<Bytes>) -> Self {
75 self.quote_tokens = Some(quote_tokens);
76 self
77 }
78
79 pub fn poll_time(mut self, poll_time: Duration) -> Self {
81 self.poll_time = poll_time;
82 self
83 }
84
85 pub fn quote_timeout(mut self, timeout: Duration) -> Self {
87 self.quote_timeout = timeout;
88 self
89 }
90
91 pub fn with_fallback_router(mut self) -> Self {
93 self.via_fallback_router = true;
94 self
95 }
96
97 pub fn build(self) -> Result<HashflowClient, RFQError> {
98 let quote_tokens;
99 if let Some(tokens) = self.quote_tokens {
100 quote_tokens = tokens;
101 } else {
102 quote_tokens = default_quote_tokens_for_chain(&self.chain)?
103 }
104
105 let client = HashflowClient::new(
106 self.chain,
107 self.tokens,
108 self.tvl,
109 quote_tokens,
110 self.auth_user,
111 self.auth_key,
112 self.poll_time,
113 self.quote_timeout,
114 )?;
115 Ok(if self.via_fallback_router { client.via_fallback_router() } else { client })
116 }
117}