pub struct WssMarketClient { /* private fields */ }Expand description
Reconnecting client for the market channel.
Implementations§
Source§impl WssMarketClient
impl WssMarketClient
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new instance using the default Polymarket WSS base.
Examples found in repository?
examples/wss_market.rs (line 44)
12async fn main() -> Result<()> {
13 let base_url =
14 env::var("POLY_API_URL").unwrap_or_else(|_| "https://clob.polymarket.com".to_string());
15 let clob = ClobClient::new(&base_url);
16
17 let min_liquidity = env::var("POLY_WSS_MIN_LIQUIDITY")
18 .ok()
19 .and_then(|value| Decimal::from_str(&value).ok())
20 .unwrap_or_else(|| Decimal::from(1_000_000));
21
22 let params = GammaListParams {
23 limit: Some(50),
24 liquidity_num_min: Some(min_liquidity),
25 ..Default::default()
26 };
27
28 let response = clob.get_markets(None, Some(¶ms)).await?;
29 let market = pick_liquid_market(&response.data, min_liquidity)?;
30
31 println!(
32 "Selected market {} (liquidity={:?})",
33 market.condition_id, market.liquidity_num
34 );
35
36 let asset_ids = derive_asset_ids(market).unwrap_or_else(|| Vec::new());
37
38 if asset_ids.is_empty() {
39 return Err(PolyError::validation(
40 "failed to derive asset IDs for the selected market",
41 ));
42 }
43
44 let mut client = WssMarketClient::new();
45 client.subscribe(asset_ids.clone()).await?;
46
47 println!("Subscribed to market channel for assets={:?}", asset_ids);
48
49 for _ in 0..20 {
50 match client.next_event().await {
51 Ok(WssMarketEvent::PriceChange(change)) => {
52 println!(
53 "price_change for {}: {:?}",
54 change.market, change.price_changes
55 );
56 }
57 Ok(WssMarketEvent::Book(book)) => {
58 println!(
59 "book {} bids={} asks={}",
60 book.market,
61 book.bids.len(),
62 book.asks.len()
63 );
64 }
65 Ok(WssMarketEvent::TickSizeChange(change)) => {
66 println!(
67 "tick size change {} from {} to {}",
68 change.market, change.old_tick_size, change.new_tick_size
69 );
70 }
71 Ok(WssMarketEvent::LastTrade(trade)) => {
72 println!(
73 "last_trade {} {:?}@{}",
74 trade.market, trade.side, trade.price
75 );
76 }
77 Err(err) => {
78 eprintln!("stream error: {}", err);
79 break;
80 }
81 }
82 }
83
84 Ok(())
85}Sourcepub fn with_url(url: &str) -> Self
pub fn with_url(url: &str) -> Self
Create a new client against a custom endpoint (useful for tests).
Sourcepub async fn subscribe(&mut self, asset_ids: Vec<String>) -> Result<()>
pub async fn subscribe(&mut self, asset_ids: Vec<String>) -> Result<()>
Subscribe to the market channel for the provided token/market IDs.
Examples found in repository?
examples/wss_market.rs (line 45)
12async fn main() -> Result<()> {
13 let base_url =
14 env::var("POLY_API_URL").unwrap_or_else(|_| "https://clob.polymarket.com".to_string());
15 let clob = ClobClient::new(&base_url);
16
17 let min_liquidity = env::var("POLY_WSS_MIN_LIQUIDITY")
18 .ok()
19 .and_then(|value| Decimal::from_str(&value).ok())
20 .unwrap_or_else(|| Decimal::from(1_000_000));
21
22 let params = GammaListParams {
23 limit: Some(50),
24 liquidity_num_min: Some(min_liquidity),
25 ..Default::default()
26 };
27
28 let response = clob.get_markets(None, Some(¶ms)).await?;
29 let market = pick_liquid_market(&response.data, min_liquidity)?;
30
31 println!(
32 "Selected market {} (liquidity={:?})",
33 market.condition_id, market.liquidity_num
34 );
35
36 let asset_ids = derive_asset_ids(market).unwrap_or_else(|| Vec::new());
37
38 if asset_ids.is_empty() {
39 return Err(PolyError::validation(
40 "failed to derive asset IDs for the selected market",
41 ));
42 }
43
44 let mut client = WssMarketClient::new();
45 client.subscribe(asset_ids.clone()).await?;
46
47 println!("Subscribed to market channel for assets={:?}", asset_ids);
48
49 for _ in 0..20 {
50 match client.next_event().await {
51 Ok(WssMarketEvent::PriceChange(change)) => {
52 println!(
53 "price_change for {}: {:?}",
54 change.market, change.price_changes
55 );
56 }
57 Ok(WssMarketEvent::Book(book)) => {
58 println!(
59 "book {} bids={} asks={}",
60 book.market,
61 book.bids.len(),
62 book.asks.len()
63 );
64 }
65 Ok(WssMarketEvent::TickSizeChange(change)) => {
66 println!(
67 "tick size change {} from {} to {}",
68 change.market, change.old_tick_size, change.new_tick_size
69 );
70 }
71 Ok(WssMarketEvent::LastTrade(trade)) => {
72 println!(
73 "last_trade {} {:?}@{}",
74 trade.market, trade.side, trade.price
75 );
76 }
77 Err(err) => {
78 eprintln!("stream error: {}", err);
79 break;
80 }
81 }
82 }
83
84 Ok(())
85}Sourcepub async fn next_event(&mut self) -> Result<WssMarketEvent>
pub async fn next_event(&mut self) -> Result<WssMarketEvent>
Read the next market channel event, reconnecting transparently when the socket drops.
Examples found in repository?
examples/wss_market.rs (line 50)
12async fn main() -> Result<()> {
13 let base_url =
14 env::var("POLY_API_URL").unwrap_or_else(|_| "https://clob.polymarket.com".to_string());
15 let clob = ClobClient::new(&base_url);
16
17 let min_liquidity = env::var("POLY_WSS_MIN_LIQUIDITY")
18 .ok()
19 .and_then(|value| Decimal::from_str(&value).ok())
20 .unwrap_or_else(|| Decimal::from(1_000_000));
21
22 let params = GammaListParams {
23 limit: Some(50),
24 liquidity_num_min: Some(min_liquidity),
25 ..Default::default()
26 };
27
28 let response = clob.get_markets(None, Some(¶ms)).await?;
29 let market = pick_liquid_market(&response.data, min_liquidity)?;
30
31 println!(
32 "Selected market {} (liquidity={:?})",
33 market.condition_id, market.liquidity_num
34 );
35
36 let asset_ids = derive_asset_ids(market).unwrap_or_else(|| Vec::new());
37
38 if asset_ids.is_empty() {
39 return Err(PolyError::validation(
40 "failed to derive asset IDs for the selected market",
41 ));
42 }
43
44 let mut client = WssMarketClient::new();
45 client.subscribe(asset_ids.clone()).await?;
46
47 println!("Subscribed to market channel for assets={:?}", asset_ids);
48
49 for _ in 0..20 {
50 match client.next_event().await {
51 Ok(WssMarketEvent::PriceChange(change)) => {
52 println!(
53 "price_change for {}: {:?}",
54 change.market, change.price_changes
55 );
56 }
57 Ok(WssMarketEvent::Book(book)) => {
58 println!(
59 "book {} bids={} asks={}",
60 book.market,
61 book.bids.len(),
62 book.asks.len()
63 );
64 }
65 Ok(WssMarketEvent::TickSizeChange(change)) => {
66 println!(
67 "tick size change {} from {} to {}",
68 change.market, change.old_tick_size, change.new_tick_size
69 );
70 }
71 Ok(WssMarketEvent::LastTrade(trade)) => {
72 println!(
73 "last_trade {} {:?}@{}",
74 trade.market, trade.side, trade.price
75 );
76 }
77 Err(err) => {
78 eprintln!("stream error: {}", err);
79 break;
80 }
81 }
82 }
83
84 Ok(())
85}Auto Trait Implementations§
impl !Freeze for WssMarketClient
impl !RefUnwindSafe for WssMarketClient
impl Send for WssMarketClient
impl Sync for WssMarketClient
impl Unpin for WssMarketClient
impl !UnwindSafe for WssMarketClient
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more