-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathmain.rs
132 lines (117 loc) · 3.55 KB
/
main.rs
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
use anyhow::anyhow;
use clap::Parser;
use tokio::{self, select, signal, sync::mpsc::unbounded_channel};
use tracing::{error, info};
use crate::file::Writer;
mod binance;
mod binancefuturescm;
mod binancefuturesum;
mod bybit;
mod error;
mod file;
mod throttler;
mod bitmart;
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
/// Path for the files where collected data will be written.
path: String,
/// Name of the exchange
exchange: String,
/// Symbols for which data will be collected.
symbols: Vec<String>,
}
#[tokio::main(flavor = "multi_thread")]
async fn main() -> Result<(), anyhow::Error> {
let args = Args::parse();
tracing_subscriber::fmt::init();
let (writer_tx, mut writer_rx) = unbounded_channel();
let handle = match args.exchange.as_str() {
"binancefutures" | "binancefuturesum" => {
let streams = [
"$symbol@trade",
"$symbol@bookTicker",
"$symbol@depth@0ms",
// "$symbol@@markPrice@1s"
]
.iter()
.map(|stream| stream.to_string())
.collect();
tokio::spawn(binancefuturesum::run_collection(
streams,
args.symbols,
writer_tx,
))
}
"binancefuturescm" => {
let streams = [
"$symbol@trade",
"$symbol@bookTicker",
"$symbol@depth@0ms",
// "$symbol@@markPrice@1s"
]
.iter()
.map(|stream| stream.to_string())
.collect();
tokio::spawn(binancefuturescm::run_collection(
streams,
args.symbols,
writer_tx,
))
}
"binance" | "binancespot" => {
let streams = ["$symbol@trade", "$symbol@bookTicker", "$symbol@depth@100ms"]
.iter()
.map(|stream| stream.to_string())
.collect();
tokio::spawn(binance::run_collection(streams, args.symbols, writer_tx))
}
"bybit" => {
let topics = [
"orderbook.1.$symbol",
"orderbook.50.$symbol",
"orderbook.500.$symbol",
"publicTrade.$symbol",
]
.iter()
.map(|topic| topic.to_string())
.collect();
tokio::spawn(bybit::run_collection(topics, args.symbols, writer_tx))
}
"bitmart" => {
let topics = [
"futures/depthIncrease50:$symbol@100ms",
"futures/trade:$symbol",
]
.iter()
.map(|topic| topic.to_string())
.collect();
tokio::spawn(bitmart::run_collection(topics, args.symbols, writer_tx))
}
exchange => {
return Err(anyhow!("{exchange} is not supported."));
}
};
let mut writer = Writer::new(&args.path);
loop {
select! {
_ = signal::ctrl_c() => {
info!("ctrl-c received");
break;
}
r = writer_rx.recv() => match r {
Some((recv_time, symbol, data)) => {
if let Err(error) = writer.write(recv_time, symbol, data) {
error!(?error, "write error");
break;
}
}
None => {
break;
}
}
}
}
// let _ = handle.await;
Ok(())
}