-
Notifications
You must be signed in to change notification settings - Fork 296
Expand file tree
/
Copy pathmarkets.rs
More file actions
201 lines (168 loc) · 5.21 KB
/
markets.rs
File metadata and controls
201 lines (168 loc) · 5.21 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use anyhow::Result;
use chrono::{DateTime, Utc};
use clap::{Args, Subcommand};
use polymarket_client_sdk::gamma::{
self,
types::{
request::{
MarketByIdRequest, MarketBySlugRequest, MarketTagsRequest, MarketsRequest,
SearchRequest,
},
response::Market,
},
};
use rust_decimal::Decimal;
use super::is_numeric_id;
use crate::output::OutputFormat;
use crate::output::markets::{print_market, print_markets};
use crate::output::tags::print_tags;
#[derive(Args)]
pub struct MarketsArgs {
#[command(subcommand)]
pub command: MarketsCommand,
}
#[derive(Subcommand)]
pub enum MarketsCommand {
/// List markets with optional filters
List {
/// Filter by active status
#[arg(long)]
active: Option<bool>,
/// Filter by closed status
#[arg(long)]
closed: Option<bool>,
/// Max results
#[arg(long, default_value = "25")]
limit: i32,
/// Pagination offset
#[arg(long)]
offset: Option<i32>,
/// Sort field (e.g. `volume_num`, `liquidity_num`)
#[arg(long)]
order: Option<String>,
/// Sort ascending instead of descending
#[arg(long)]
ascending: bool,
/// Minimum trading volume (e.g. 1000000)
#[arg(long)]
volume_min: Option<Decimal>,
/// Maximum trading volume
#[arg(long)]
volume_max: Option<Decimal>,
/// Minimum liquidity
#[arg(long)]
liquidity_min: Option<Decimal>,
/// Maximum liquidity
#[arg(long)]
liquidity_max: Option<Decimal>,
/// Only markets starting after this date (e.g. 2026-03-01T00:00:00Z)
#[arg(long)]
start_date_min: Option<DateTime<Utc>>,
/// Only markets starting before this date
#[arg(long)]
start_date_max: Option<DateTime<Utc>>,
/// Only markets ending after this date
#[arg(long)]
end_date_min: Option<DateTime<Utc>>,
/// Only markets ending before this date
#[arg(long)]
end_date_max: Option<DateTime<Utc>>,
/// Filter by tag ID
#[arg(long)]
tag: Option<String>,
},
/// Get a single market by ID or slug
Get {
/// Market ID (numeric) or slug
id: String,
},
/// Search markets
Search {
/// Search query string
query: String,
/// Results per type
#[arg(long, default_value = "10")]
limit: i32,
},
/// Get tags for a market
Tags {
/// Market ID
id: String,
},
}
pub async fn execute(
client: &gamma::Client,
args: MarketsArgs,
output: OutputFormat,
) -> Result<()> {
match args.command {
MarketsCommand::List {
active,
closed,
limit,
offset,
order,
ascending,
volume_min,
volume_max,
liquidity_min,
liquidity_max,
start_date_min,
start_date_max,
end_date_min,
end_date_max,
tag,
} => {
let resolved_closed = closed.or_else(|| active.map(|a| !a));
let request = MarketsRequest::builder()
.limit(limit)
.maybe_closed(resolved_closed)
.maybe_offset(offset)
.maybe_order(order)
.ascending(ascending)
.maybe_volume_num_min(volume_min)
.maybe_volume_num_max(volume_max)
.maybe_liquidity_num_min(liquidity_min)
.maybe_liquidity_num_max(liquidity_max)
.maybe_start_date_min(start_date_min)
.maybe_start_date_max(start_date_max)
.maybe_end_date_min(end_date_min)
.maybe_end_date_max(end_date_max)
.maybe_tag_id(tag)
.build();
let markets = client.markets(&request).await?;
print_markets(&markets, &output)?;
}
MarketsCommand::Get { id } => {
let is_numeric = is_numeric_id(&id);
let market = if is_numeric {
let req = MarketByIdRequest::builder().id(id).build();
client.market_by_id(&req).await?
} else {
let req = MarketBySlugRequest::builder().slug(id).build();
client.market_by_slug(&req).await?
};
print_market(&market, &output)?;
}
MarketsCommand::Search { query, limit } => {
let request = SearchRequest::builder()
.q(query)
.limit_per_type(limit)
.build();
let results = client.search(&request).await?;
let markets: Vec<Market> = results
.events
.unwrap_or_default()
.into_iter()
.flat_map(|e| e.markets.unwrap_or_default())
.collect();
print_markets(&markets, &output)?;
}
MarketsCommand::Tags { id } => {
let req = MarketTagsRequest::builder().id(id).build();
let tags = client.market_tags(&req).await?;
print_tags(&tags, &output)?;
}
}
Ok(())
}