-
Notifications
You must be signed in to change notification settings - Fork 296
Expand file tree
/
Copy pathmarkets.rs
More file actions
158 lines (133 loc) · 4.08 KB
/
markets.rs
File metadata and controls
158 lines (133 loc) · 4.08 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
use anyhow::Result;
use clap::{Args, Subcommand};
use polymarket_client_sdk::gamma::{
self,
types::{
request::{
MarketByIdRequest, MarketBySlugRequest, MarketTagsRequest, MarketsRequest,
SearchRequest,
},
response::Market,
},
};
use super::is_numeric_id;
use crate::output::markets::{print_market_detail, print_markets_table};
use crate::output::tags::print_tags_table;
use crate::output::{OutputFormat, print_json};
#[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,
},
/// 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,
} => {
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)
.maybe_ascending(Some(ascending))
.build();
let markets = client.markets(&request).await?;
match output {
OutputFormat::Table => print_markets_table(&markets),
OutputFormat::Json => print_json(&markets)?,
}
}
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?
};
match output {
OutputFormat::Table => print_market_detail(&market),
OutputFormat::Json => print_json(&market)?,
}
}
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();
match output {
OutputFormat::Table => print_markets_table(&markets),
OutputFormat::Json => print_json(&markets)?,
}
}
MarketsCommand::Tags { id } => {
let req = MarketTagsRequest::builder().id(id).build();
let tags = client.market_tags(&req).await?;
match output {
OutputFormat::Table => print_tags_table(&tags),
OutputFormat::Json => print_json(&tags)?,
}
}
}
Ok(())
}