-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathbuilder_authenticated.rs
More file actions
59 lines (50 loc) · 2.23 KB
/
Copy pathbuilder_authenticated.rs
File metadata and controls
59 lines (50 loc) · 2.23 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
//! Demonstrates builder-attributed trading with the CLOB client.
//!
//! V2 authenticates builder operations with the same L2 credentials as any other user;
//! attribution is carried on the order's `builder_code` field and as a query parameter
//! on `builder_trades`.
//!
//! Run with tracing enabled:
//! ```sh
//! RUST_LOG=info,hyper_util=off,hyper=off,reqwest=off,h2=off,rustls=off cargo run --example builder_authenticated --features clob,tracing
//! ```
//!
//! Requires `POLYMARKET_PRIVATE_KEY` and `POLYMARKET_BUILDER_CODE` environment variables.
use std::str::FromStr as _;
use alloy::signers::Signer as _;
use alloy::signers::local::LocalSigner;
use polymarket_client_sdk_v2::clob::types::request::TradesRequest;
use polymarket_client_sdk_v2::clob::{Client, Config};
use polymarket_client_sdk_v2::types::{B256, U256};
use polymarket_client_sdk_v2::{POLYGON, PRIVATE_KEY_VAR};
use polymarket_client_sdk_v2::CLOB_HOST;
use tracing::{error, info};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt::init();
let private_key = std::env::var(PRIVATE_KEY_VAR).expect("Need POLYMARKET_PRIVATE_KEY");
let builder_code = B256::from_str(
&std::env::var("POLYMARKET_BUILDER_CODE").expect("Need POLYMARKET_BUILDER_CODE"),
)?;
let signer = LocalSigner::from_str(&private_key)?.with_chain_id(Some(POLYGON));
let config = Config::builder().builder_code(builder_code).build();
let client = Client::new(CLOB_HOST, config)?
.authentication_builder(&signer)
.authenticate()
.await?;
match client.builder_api_keys().await {
Ok(keys) => info!(endpoint = "builder_api_keys", count = keys.len()),
Err(e) => error!(endpoint = "builder_api_keys", error = %e),
}
let token_id = U256::from_str(
"15871154585880608648532107628464183779895785213830018178010423617714102767076",
)?;
let request = TradesRequest::builder().asset_id(token_id).build();
match client.builder_trades(builder_code, &request, None).await {
Ok(trades) => {
info!(endpoint = "builder_trades", token_id = %token_id, count = trades.data.len());
}
Err(e) => error!(endpoint = "builder_trades", token_id = %token_id, error = %e),
}
Ok(())
}