1#[cfg(all(feature = "clob", feature = "gamma"))]
62use polyte_clob::{Chain, Clob, Credentials};
63#[cfg(all(feature = "clob", feature = "gamma"))]
64use polyte_gamma::Gamma;
65
66#[cfg(feature = "clob")]
67pub use polyte_clob;
68#[cfg(feature = "data")]
69pub use polyte_data;
70#[cfg(feature = "gamma")]
71pub use polyte_gamma;
72
73pub mod prelude {
75 #[cfg(all(feature = "clob", feature = "gamma", feature = "data"))]
76 pub use crate::{Polymarket, PolymarketBuilder, PolymarketError};
77
78 #[cfg(feature = "clob")]
79 pub use polyte_clob::{Chain, Clob, ClobError, CreateOrderParams, Credentials, OrderSide};
80
81 #[cfg(feature = "data")]
82 pub use polyte_data::{DataApi, DataApiError};
83
84 #[cfg(feature = "gamma")]
85 pub use polyte_gamma::{Gamma, GammaError};
86}
87
88#[derive(Debug, thiserror::Error)]
90pub enum PolymarketError {
91 #[cfg(feature = "clob")]
93 #[error("CLOB error: {0}")]
94 Clob(#[from] polyte_clob::ClobError),
95
96 #[cfg(feature = "data")]
98 #[error("Data error: {0}")]
99 Data(#[from] polyte_data::DataApiError),
100
101 #[cfg(feature = "gamma")]
103 #[error("Gamma error: {0}")]
104 Gamma(#[from] polyte_gamma::GammaError),
105
106 #[error("Configuration error: {0}")]
108 Config(String),
109}
110
111#[cfg(all(feature = "clob", feature = "gamma"))]
113#[derive(Clone)]
114pub struct Polymarket {
115 pub clob: Clob,
117 pub gamma: Gamma,
119}
120
121#[cfg(all(feature = "clob", feature = "gamma"))]
122impl Polymarket {
123 pub fn new(
125 private_key: impl Into<String>,
126 credentials: Credentials,
127 ) -> Result<Self, PolymarketError> {
128 PolymarketBuilder::new(private_key, credentials).build()
129 }
130
131 pub fn builder(private_key: impl Into<String>, credentials: Credentials) -> PolymarketBuilder {
133 PolymarketBuilder::new(private_key, credentials)
134 }
135}
136
137#[cfg(all(feature = "clob", feature = "gamma"))]
139pub struct PolymarketBuilder {
140 clob_base_url: Option<String>,
141 gamma_base_url: Option<String>,
142 timeout_ms: Option<u64>,
143 chain: Option<Chain>,
144 private_key: String,
145 credentials: Credentials,
146}
147
148#[cfg(all(feature = "clob", feature = "gamma"))]
149impl PolymarketBuilder {
150 fn new(private_key: impl Into<String>, credentials: Credentials) -> Self {
151 Self {
152 clob_base_url: None,
153 gamma_base_url: None,
154 timeout_ms: None,
155 chain: None,
156 private_key: private_key.into(),
157 credentials,
158 }
159 }
160
161 pub fn clob_base_url(mut self, url: impl Into<String>) -> Self {
163 self.clob_base_url = Some(url.into());
164 self
165 }
166
167 pub fn gamma_base_url(mut self, url: impl Into<String>) -> Self {
169 self.gamma_base_url = Some(url.into());
170 self
171 }
172
173 pub fn timeout_ms(mut self, timeout: u64) -> Self {
175 self.timeout_ms = Some(timeout);
176 self
177 }
178
179 pub fn chain(mut self, chain: Chain) -> Self {
181 self.chain = Some(chain);
182 self
183 }
184
185 pub fn build(self) -> Result<Polymarket, PolymarketError> {
187 let mut gamma_builder = Gamma::builder();
189
190 if let Some(url) = self.gamma_base_url {
191 gamma_builder = gamma_builder.base_url(url);
192 }
193 if let Some(timeout) = self.timeout_ms {
194 gamma_builder = gamma_builder.timeout_ms(timeout);
195 }
196
197 let gamma = gamma_builder.build()?;
198
199 let mut clob_builder = Clob::builder(self.private_key, self.credentials)?;
201
202 if let Some(url) = self.clob_base_url {
203 clob_builder = clob_builder.base_url(url);
204 }
205 if let Some(timeout) = self.timeout_ms {
206 clob_builder = clob_builder.timeout_ms(timeout);
207 }
208 if let Some(chain) = self.chain {
209 clob_builder = clob_builder.chain(chain);
210 }
211
212 let clob = clob_builder.build()?;
213
214 Ok(Polymarket { clob, gamma })
215 }
216}