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