manifold_markets/
lib.rs

1//! Unofficial, shallowly-typed client for the Manifold Markets API.
2//!
3//! Based on the docs at [https://docs.manifold.markets/api][docs]. Tested, but not thorougly.
4//!
5//! [docs]: https://docs.manifold.markets/api
6//!
7//! See [`ManifoldClient`](ManifoldClient) for usage.
8
9#![feature(iterator_try_collect)]
10
11mod client;
12pub mod error;
13pub mod streams;
14pub mod types;
15pub use client::{ManifoldAuthorization, ManifoldClient};
16
17#[cfg(test)]
18mod tests {
19    use futures_util::{StreamExt, TryStreamExt};
20    use rand::seq::SliceRandom;
21
22    use crate::types::Market;
23
24    use super::*;
25
26    #[tokio::test]
27    async fn it_works() -> anyhow::Result<()> {
28        dotenv::dotenv().ok();
29
30        let manifold = ManifoldClient::from_api_key(&std::env::var("MANIFOLD_API_KEY").expect(
31            "The test requires a MANIFOLD_API_KEY environment variable (you can use .env)",
32        ))?;
33
34        let r = manifold
35            .stream_markets()
36            .take(510)
37            .try_collect::<Vec<_>>()
38            .await?;
39
40        println!("{:#?}", r[509]);
41
42        let mut r = r.iter().filter(|m| m.is_active()).collect::<Vec<_>>();
43
44        r.shuffle(&mut rand::thread_rng());
45
46        {
47            println!("testing yes/no");
48            let yes_no = r
49                .iter().find(|m| m.outcome_type() == types::OutcomeType::Binary)
50                .unwrap();
51
52            println!("will bet on {yes_no:#?}");
53
54            let pool = yes_no.pool();
55
56            let winning = pool
57                .iter()
58                .max_by(|a, b| a.1.partial_cmp(&b.1).unwrap())
59                .unwrap();
60
61            println!("will bet on {winning:#?}");
62
63            let bet = manifold
64                .post_bet(1, yes_no.id(), winning.0.clone(), None)
65                .await?;
66
67            println!("bet: {bet:#?}");
68        }
69        {
70            println!("now for a FreeResponse");
71
72            let free_response = r
73                .iter().find(|m| m.outcome_type() == types::OutcomeType::FreeResponse)
74                .unwrap();
75
76            println!("will bet on {free_response:#?}");
77
78            let pool = free_response.pool();
79            let winning = pool
80                .iter()
81                .max_by(|a, b| a.1.partial_cmp(&b.1).unwrap())
82                .unwrap();
83
84            println!("will bet on {winning:#?}");
85
86            let bet = manifold
87                .post_bet(1, free_response.id(), winning.0.clone(), None)
88                .await?;
89
90            println!("bet: {bet:#?}");
91        }
92        {
93            println!("now for a MultipleChoice");
94
95            let multiple_choice = r
96                .iter().find(|m| m.outcome_type() == types::OutcomeType::MultipleChoice)
97                .unwrap();
98
99            println!("will bet on {multiple_choice:#?}");
100
101            let pool = multiple_choice.pool();
102            let winning = pool
103                .iter()
104                .max_by(|a, b| a.1.partial_cmp(&b.1).unwrap())
105                .unwrap();
106
107            println!("will bet on {winning:#?}");
108
109            let bet = manifold
110                .post_bet(1, multiple_choice.id(), winning.0.clone(), None)
111                .await?;
112
113            println!("bet: {bet:#?}");
114        }
115        {
116            println!("now for a PseudoNumeric");
117
118            let pseudo_numeric = r
119                .iter().find(|m| m.outcome_type() == types::OutcomeType::PseudoNumeric)
120                .unwrap();
121
122            println!("will bet on {pseudo_numeric:#?}");
123
124            let pool = pseudo_numeric.pool();
125            let winning = pool
126                .iter()
127                .max_by(|a, b| a.1.partial_cmp(&b.1).unwrap())
128                .unwrap();
129
130            println!("will bet on {winning:#?}");
131
132            let bet = manifold
133                .post_bet(1, pseudo_numeric.id(), winning.0.clone(), None)
134                .await?;
135
136            println!("bet: {bet:#?}");
137        }
138
139        let me = manifold.get_me().await?;
140
141        println!("me: {me:#?}");
142
143        println!("test streaming my bets...");
144        let bets = manifold.stream_bets(Some(me.id()), None, None, None);
145
146        let bets = bets.take(10).try_collect::<Vec<_>>().await?;
147
148        println!("bet: {:#?}", bets[0]);
149
150        Ok(())
151    }
152}