diff --git a/src/client.rs b/src/client.rs index 0034412..77d77ac 100644 --- a/src/client.rs +++ b/src/client.rs @@ -35,6 +35,17 @@ struct MarketByTokenResponse { condition_id: String, } +#[derive(Serialize)] +struct TokenRequest<'a> { + token_id: &'a str, +} + +#[derive(Serialize)] +struct PriceRequest<'a> { + token_id: &'a str, + side: &'a str, +} + fn polymarket_default_headers() -> HeaderMap { let mut headers = HeaderMap::new(); headers.insert( @@ -426,13 +437,9 @@ impl ClobClient { &self, token_ids: &[String], ) -> Result> { - let request_data: Vec> = token_ids + let request_data: Vec> = token_ids .iter() - .map(|id| { - let mut map = std::collections::HashMap::new(); - map.insert("token_id", id.clone()); - map - }) + .map(|id| TokenRequest { token_id: id }) .collect(); let response = self @@ -1610,13 +1617,9 @@ impl ClobClient { &self, token_ids: &[String], ) -> Result> { - let request_data: Vec> = token_ids + let request_data: Vec> = token_ids .iter() - .map(|id| { - let mut map = std::collections::HashMap::new(); - map.insert("token_id", id.clone()); - map - }) + .map(|id| TokenRequest { token_id: id }) .collect(); let response = self @@ -1648,13 +1651,11 @@ impl ClobClient { &self, book_params: &[crate::types::BookParams], ) -> Result>> { - let request_data: Vec> = book_params + let request_data: Vec> = book_params .iter() - .map(|params| { - let mut map = std::collections::HashMap::new(); - map.insert("token_id", params.token_id.clone()); - map.insert("side", params.side.as_str().to_string()); - map + .map(|params| PriceRequest { + token_id: ¶ms.token_id, + side: params.side.as_str(), }) .collect(); @@ -1679,13 +1680,9 @@ impl ClobClient { /// Get order book for multiple tokens (batch) - reference implementation compatible pub async fn get_order_books(&self, token_ids: &[String]) -> Result> { - let request_data: Vec> = token_ids + let request_data: Vec> = token_ids .iter() - .map(|id| { - let mut map = std::collections::HashMap::new(); - map.insert("token_id", id.clone()); - map - }) + .map(|id| TokenRequest { token_id: id }) .collect(); let response = self @@ -1755,13 +1752,9 @@ impl ClobClient { /// Get last trade prices for multiple tokens pub async fn get_last_trade_prices(&self, token_ids: &[String]) -> Result { - let request_data: Vec> = token_ids + let request_data: Vec> = token_ids .iter() - .map(|id| { - let mut map = std::collections::HashMap::new(); - map.insert("token_id", id.clone()); - map - }) + .map(|id| TokenRequest { token_id: id }) .collect(); let response = self @@ -3011,6 +3004,9 @@ mod tests { let mock = server .mock("POST", "/midpoints") + .match_body(Matcher::JsonString( + r#"[{"token_id":"0x123"},{"token_id":"0x456"}]"#.to_string(), + )) .with_header("content-type", "application/json") .with_status(200) .with_header("content-type", "application/json") @@ -3086,6 +3082,10 @@ mod tests { let mock = server .mock("POST", "/prices") + .match_body(Matcher::JsonString( + r#"[{"token_id":"0x123","side":"BUY"},{"token_id":"0x456","side":"SELL"}]"# + .to_string(), + )) .with_header("content-type", "application/json") .with_status(200) .with_body(mock_response) @@ -3245,6 +3245,7 @@ mod tests { let mock = server .mock("POST", "/books") + .match_body(Matcher::JsonString(r#"[{"token_id":"0x123"}]"#.to_string())) .with_header("content-type", "application/json") .with_status(200) .with_body(mock_response)