-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathasync.rs
More file actions
152 lines (128 loc) · 5.06 KB
/
Copy pathasync.rs
File metadata and controls
152 lines (128 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//! Demonstrates async concurrency patterns with the CLOB client.
//!
//! This example shows how to:
//! 1. Run multiple unauthenticated API calls concurrently
//! 2. Run multiple authenticated API calls concurrently
//! 3. Spawn background tasks that share the client
//!
//! Run with tracing enabled:
//! ```sh
//! RUST_LOG=info,hyper_util=off,hyper=off,reqwest=off,h2=off,rustls=off cargo run --example async --features clob,tracing
//! ```
//!
//! Optionally log to a file:
//! ```sh
//! LOG_FILE=async.log RUST_LOG=info,hyper_util=off,hyper=off,reqwest=off,h2=off,rustls=off cargo run --example async --features clob,tracing
//! ```
//!
//! For authenticated endpoints, set the `POLYMARKET_PRIVATE_KEY` environment variable.
use std::fs::File;
use std::str::FromStr as _;
use alloy::signers::Signer as _;
use alloy::signers::local::LocalSigner;
use polymarket_client_sdk_v2::clob::{Client, Config};
use polymarket_client_sdk_v2::types::U256;
use polymarket_client_sdk_v2::{POLYGON, PRIVATE_KEY_VAR};
use polymarket_client_sdk_v2::CLOB_HOST;
use tokio::join;
use tracing::{error, info};
use tracing_subscriber::EnvFilter;
use tracing_subscriber::layer::SubscriberExt as _;
use tracing_subscriber::util::SubscriberInitExt as _;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
if let Ok(path) = std::env::var("LOG_FILE") {
let file = File::create(path)?;
tracing_subscriber::registry()
.with(EnvFilter::from_default_env())
.with(
tracing_subscriber::fmt::layer()
.with_writer(file)
.with_ansi(false),
)
.init();
} else {
tracing_subscriber::fmt::init();
}
let (unauthenticated, authenticated) = join!(unauthenticated(), authenticated());
unauthenticated?;
authenticated
}
async fn unauthenticated() -> anyhow::Result<()> {
let client = Client::new(CLOB_HOST, Config::default())?;
let client_clone = client.clone();
let token_id = U256::from_str(
"42334954850219754195241248003172889699504912694714162671145392673031415571339",
)?;
let thread = tokio::spawn(async move {
let (ok_result, tick_result, neg_risk_result) = join!(
client_clone.ok(),
client_clone.tick_size(token_id),
client_clone.neg_risk(token_id)
);
match ok_result {
Ok(s) => info!(endpoint = "ok", thread = true, result = %s),
Err(e) => error!(endpoint = "ok", thread = true, error = %e),
}
match tick_result {
Ok(t) => info!(endpoint = "tick_size", thread = true, tick_size = ?t.minimum_tick_size),
Err(e) => error!(endpoint = "tick_size", thread = true, error = %e),
}
match neg_risk_result {
Ok(n) => info!(endpoint = "neg_risk", thread = true, neg_risk = n.neg_risk),
Err(e) => error!(endpoint = "neg_risk", thread = true, error = %e),
}
anyhow::Ok(())
});
match client.ok().await {
Ok(s) => info!(endpoint = "ok", result = %s),
Err(e) => error!(endpoint = "ok", error = %e),
}
match client.tick_size(token_id).await {
Ok(t) => {
info!(endpoint = "tick_size", token_id = %token_id, tick_size = ?t.minimum_tick_size);
}
Err(e) => error!(endpoint = "tick_size", token_id = %token_id, error = %e),
}
match client.neg_risk(token_id).await {
Ok(n) => info!(endpoint = "neg_risk", token_id = %token_id, neg_risk = n.neg_risk),
Err(e) => error!(endpoint = "neg_risk", token_id = %token_id, error = %e),
}
thread.await?
}
async fn authenticated() -> anyhow::Result<()> {
let Ok(private_key) = std::env::var(PRIVATE_KEY_VAR) else {
info!(
endpoint = "authenticated",
"skipped - POLYMARKET_PRIVATE_KEY not set"
);
return Ok(());
};
let signer = LocalSigner::from_str(&private_key)?.with_chain_id(Some(POLYGON));
let client = Client::new(CLOB_HOST, Config::default())?
.authentication_builder(&signer)
.authenticate()
.await?;
let client_clone = client.clone();
let thread = tokio::spawn(async move {
let (ok_result, api_keys_result) = join!(client_clone.ok(), client_clone.api_keys());
match ok_result {
Ok(s) => info!(endpoint = "ok", thread = true, authenticated = true, result = %s),
Err(e) => error!(endpoint = "ok", thread = true, authenticated = true, error = %e),
}
match api_keys_result {
Ok(keys) => info!(endpoint = "api_keys", thread = true, result = ?keys),
Err(e) => error!(endpoint = "api_keys", thread = true, error = %e),
}
anyhow::Ok(())
});
match client.ok().await {
Ok(s) => info!(endpoint = "ok", authenticated = true, result = %s),
Err(e) => error!(endpoint = "ok", authenticated = true, error = %e),
}
match client.api_keys().await {
Ok(keys) => info!(endpoint = "api_keys", result = ?keys),
Err(e) => error!(endpoint = "api_keys", error = %e),
}
thread.await?
}