-
Notifications
You must be signed in to change notification settings - Fork 1
/
aggregator.rs
165 lines (157 loc) · 5.53 KB
/
aggregator.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
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
use crate::processor::web::SharedState;
use super::ftx::{self, Ftx};
use super::kucoin::{self, Kucoin};
use super::okex::{self, OkEx};
use super::sushiswap::{self, Sushiswap};
use super::{binance, coinbase, uniswapv2, uniswapv3, Exchange, PairInfo, PRECESIONS_REPRESENT};
use binance::Binance;
use coinbase::Coinbase;
use futures::future;
use log::{debug, info, warn};
use std::collections::BTreeMap;
use std::error::Error;
use std::vec::Vec;
use strum_macros::Display;
use uniswapv2::UniswapV2;
use uniswapv3::UniswapV3;
pub struct Aggregator {
data_sources: BTreeMap<String, Box<dyn Exchange>>,
mappings: BTreeMap<String, Vec<String>>,
s_state: SharedState,
}
pub fn new(mappings: BTreeMap<String, Vec<String>>, _s_state: SharedState) -> Aggregator {
let mut agg = Aggregator {
data_sources: BTreeMap::new(),
mappings: mappings,
s_state: _s_state,
};
agg.data_sources
.insert(binance::NAME.into(), Box::new(Binance::default()));
agg.data_sources
.insert(coinbase::NAME.into(), Box::new(Coinbase::default()));
agg.data_sources
.insert(uniswapv3::NAME.into(), Box::new(UniswapV3::default()));
agg.data_sources
.insert(uniswapv2::NAME.into(), Box::new(UniswapV2::default()));
agg.data_sources
.insert(ftx::NAME.into(), Box::new(Ftx::default()));
agg.data_sources
.insert(kucoin::NAME.into(), Box::new(Kucoin::default()));
agg.data_sources
.insert(okex::NAME.into(), Box::new(OkEx::default()));
agg.data_sources
.insert(sushiswap::NAME.into(), Box::new(Sushiswap::default()));
agg
}
#[derive(Debug, Display)]
pub enum AggError {
NoEnoughVolumes(f64),
NoPairs(String),
}
impl std::error::Error for AggError {}
impl Aggregator {
pub async fn get_price(&self) -> Result<u128, Box<dyn Error + Send + Sync>> {
let mut exchagne_names = Vec::<String>::new();
let tasks = self
.data_sources
.iter()
.filter(|item| self.mappings.contains_key(item.0))
.map(|(ex_name, exchange)| {
let symbols = self.mappings[ex_name].clone();
let result = exchange.get_pairs(symbols);
exchagne_names.push(ex_name.clone());
return result;
});
let all_exchanges: Vec<Result<Vec<PairInfo>, Box<dyn Error + Send + Sync>>> =
future::join_all(tasks).await;
let mut total_volume = 0 as f64;
let mut all_pairs = Vec::<&PairInfo>::new();
let mut offset: usize = 0;
for exchange in &all_exchanges {
match exchange {
Ok(pairs) => {
info!("*** {} ***", exchagne_names[offset]);
for pair in pairs {
total_volume += pair.volume;
all_pairs.push(pair);
info!(
" +--- {} -> {} vol:{}",
pair.symbol, pair.price, pair.volume
);
}
}
Err(err) => {
warn!("{}", err)
}
}
offset += 1;
}
if total_volume < 1.0 {
return Err(Box::new(AggError::NoEnoughVolumes(total_volume)));
}
{
let monitor_pairs = &mut self.s_state.lock().unwrap().exchange_pairs;
monitor_pairs.clear();
for p in all_pairs.clone() {
monitor_pairs.push(p.clone());
}
}
calc_weighted_price(all_pairs)
}
}
fn remove_outliers(mut all_pairs: Vec<&PairInfo>) -> Vec<&PairInfo> {
let mut result = Vec::<&PairInfo>::new();
all_pairs.sort_by(|a, b| a.price.partial_cmp(&b.price).unwrap());
let n = all_pairs.len();
let p25 = (n as f64 * 0.25) as usize;
let p75 = (n as f64 * 0.75) as usize;
let p25_price = all_pairs[p25].price;
let p75_price = all_pairs[p75].price;
let iqr = p75_price - p25_price;
let mut lower_bound = 0.0;
if p25_price > (iqr * 1.5) {
lower_bound = p25_price - (iqr * 1.5);
}
let upper_bound = p75_price + (iqr * 1.5);
info!("range acceptted: [{},{}]", lower_bound, upper_bound);
for pair_price in all_pairs {
if pair_price.price < lower_bound || pair_price.price > upper_bound {
warn!("outlier skipped: {:?}", pair_price);
continue;
}
result.push(pair_price);
}
result
}
fn calc_weighted_price(
all_pairs_original: Vec<&PairInfo>,
) -> Result<u128, Box<dyn Error + Send + Sync>> {
debug!("pairs count:{}", all_pairs_original.len());
let all_pairs = remove_outliers(all_pairs_original);
debug!("oufter remove outliers, pairs count:{}", all_pairs.len());
let mut avg_price = 0.0 as f64;
let total_volume = all_pairs
.iter()
.map(|a| a.volume)
.reduce(|a, b| a + b)
.unwrap();
for pair in all_pairs {
debug!(" ++ {}", pair.price);
avg_price += pair.price as f64 * pair.volume / total_volume;
}
debug!("avg : {}", avg_price);
Ok((avg_price * PRECESIONS_REPRESENT) as u128)
}
#[cfg(test)]
mod tests {
use crate::fetcher::aggregator;
use crate::flags;
use crate::processor::web::SharedState;
#[tokio::test]
async fn test_agg() {
let cfg = flags::Config::new("./config/node.yaml").unwrap();
let agg = aggregator::new(cfg.mappings, SharedState::default());
let weighted_price = agg.get_price().await.unwrap();
println!("weighted price:{}", weighted_price);
}
}