Repository navigation
Expand file tree
/
Copy pathmarket_refresh.rs
More file actions
91 lines (81 loc) · 2.84 KB
/
Copy pathmarket_refresh.rs
File metadata and controls
91 lines (81 loc) · 2.84 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
//! Observe market price/tag refresh through `MoonClient` snapshots and events.
//!
//! Run:
//! cargo run --example market_refresh --release -- "<key_base64>" [host:port] [market] [watch_seconds]
use std::env;
use std::time::{Duration, Instant};
use moonproto::state::MarketsEvent;
use moonproto::{Event, RefreshConfig};
mod common;
fn print_market(client: &moonproto::MoonClient, market: &str) {
let Some(snapshot) = client.snapshot() else {
println!("[state] no snapshot");
return;
};
let state = snapshot.markets();
let price = state.price(market);
let tags = state.tags(market);
match price {
Some(price) => println!(
"[state] {market} bid={} ask={} mark={} funding={} tags=0x{:x}",
price.bid,
price.ask,
price.mark_price,
price.funding_rate,
tags.bits()
),
None => println!(
"[state] {market} not found yet; markets={} tags=0x{:x}",
state.market_count(),
tags.bits()
),
}
}
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() < 2 {
eprintln!("Usage: market_refresh <key_base64> [host:port] [market] [watch_seconds]");
std::process::exit(1);
}
let market = args.get(3).map(String::as_str).unwrap_or("BTCUSDT");
let watch_secs: u64 = args.get(4).and_then(|s| s.parse().ok()).unwrap_or(75);
let client = match common::connect_with_refresh(
&args[1],
args.get(2),
common::init_config(),
RefreshConfig {
update_markets_every: Some(Duration::from_secs(2)),
check_tags_every: Some(Duration::from_secs(60)),
},
) {
Ok(client) => client,
Err(err) => {
eprintln!("[connect/init] failed: {err}");
std::process::exit(2);
}
};
print_market(&client, market);
let mut prices = 0u64;
let mut tags = 0u64;
let deadline = Instant::now() + Duration::from_secs(watch_secs);
while Instant::now() < deadline {
for event in client.drain_events() {
match event {
Event::Markets(MarketsEvent::PricesUpdated { count, .. }) => {
prices += 1;
println!("[event] prices updated: {count}");
print_market(&client, market);
}
Event::Markets(MarketsEvent::TokenTagsUpdated { count }) => {
tags += 1;
println!("[event] token tags updated: {count}");
print_market(&client, market);
}
Event::Markets(other) => println!("[event] markets: {other:?}"),
_ => {}
}
}
std::thread::sleep(Duration::from_millis(50));
}
println!("[done] price updates={prices} tag updates={tags}");
}