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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
mod blockcypher;
mod blockstream;
mod coingecko;
mod dia;
mod dia_fair_price;
mod gateio;
mod kraken;

use crate::{
    config::{CurrencyStore, PriceConfig},
    currency::*,
    Error,
};
use async_trait::async_trait;
use futures::future::join_all;
use reqwest::Url;
use serde::Deserialize;
use serde_json::Value;
use statrs::statistics::{Data, OrderStatistics};
use std::{collections::BTreeMap, fmt};

pub use blockcypher::{BlockCypherApi, BlockCypherCli};
pub use blockstream::{BlockstreamApi, BlockstreamCli};
pub use coingecko::{CoinGeckoApi, CoinGeckoCli};
pub use dia::{DiaApi, DiaCli};
pub use dia_fair_price::{DiaFairPriceApi, DiaFairPriceCli};
pub use gateio::{GateIoApi, GateIoCli};
pub use kraken::{KrakenApi, KrakenCli};

pub async fn get_http(url: Url) -> Result<Value, Error> {
    log::debug!("{}", url);
    // TODO: share http client
    Ok(reqwest::get(url).await?.error_for_status()?.json::<Value>().await?)
}

#[derive(Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
#[serde(rename_all = "lowercase")]
pub enum FeedName {
    Kraken,
    GateIo,
    CoinGecko,
    Dia,
    #[serde(rename = "dia_fair_price")]
    DiaFairPrice,
}

impl fmt::Display for FeedName {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{self:?}")
    }
}

#[async_trait]
trait PriceFeed {
    async fn get_price(
        &self,
        currency_pair: CurrencyPair<Currency>,
        currency_store: &CurrencyStore<String>,
    ) -> Result<CurrencyPairAndPrice<Currency>, Error>;
}

#[derive(Default)]
pub struct PriceFeeds {
    currency_store: CurrencyStore<String>,
    feeds: BTreeMap<FeedName, Box<dyn PriceFeed>>,
}

impl PriceFeeds {
    pub fn new(currency_store: CurrencyStore<String>) -> Self {
        Self {
            currency_store,
            ..Default::default()
        }
    }

    pub fn maybe_add_coingecko(&mut self, opts: CoinGeckoCli) {
        if let Some(api) = CoinGeckoApi::from_opts(opts) {
            log::info!("🔗 CoinGecko");
            self.feeds.insert(FeedName::CoinGecko, Box::new(api));
        }
    }

    pub fn maybe_add_dia(&mut self, opts: DiaCli) {
        if let Some(api) = DiaApi::from_opts(opts) {
            log::info!("🔗 Dia");
            self.feeds.insert(FeedName::Dia, Box::new(api));
        }
    }

    pub fn maybe_add_dia_fair_price(&mut self, opts: DiaFairPriceCli) {
        if let Some(api) = DiaFairPriceApi::from_opts(opts) {
            log::info!("🔗 DiaFairPrice");
            self.feeds.insert(FeedName::DiaFairPrice, Box::new(api));
        }
    }

    pub fn maybe_add_gateio(&mut self, opts: GateIoCli) {
        if let Some(api) = GateIoApi::from_opts(opts) {
            log::info!("🔗 gate.io");
            self.feeds.insert(FeedName::GateIo, Box::new(api));
        }
    }

    pub fn maybe_add_kraken(&mut self, opts: KrakenCli) {
        if let Some(api) = KrakenApi::from_opts(opts) {
            log::info!("🔗 Kraken");
            self.feeds.insert(FeedName::Kraken, Box::new(api));
        }
    }

    async fn get_prices(
        &self,
        price_config: PriceConfig<Currency>,
    ) -> Result<Vec<CurrencyPairAndPrice<Currency>>, Error> {
        let currency_pair = price_config.pair;
        let currency_store = &self.currency_store;
        Ok(join_all(
            price_config
                .feeds
                .into_iter()
                .map(|x| {
                    x.into_iter()
                        .map(|(name, pair)| {
                            self.feeds
                                .get(&name)
                                .map(|feed| (name.clone(), pair, feed))
                                .ok_or(Error::NotConfigured(name))
                        })
                        .collect::<Result<Vec<_>, Error>>()
                })
                .collect::<Result<Vec<_>, Error>>()?
                .into_iter()
                .map(|route| {
                    let currency_pair = currency_pair.clone();
                    async move {
                        let mut currency_pair_and_price = if let Some(currency_pair_and_price) = join_all(
                            route
                                .into_iter()
                                .map(|(name, currency_pair, feed)| feed.get_price(currency_pair, currency_store)),
                        )
                        .await
                        .into_iter()
                        .collect::<Result<Vec<_>, Error>>()?
                        .into_iter()
                        .reduce(|left, right| left.reduce(right))
                        {
                            currency_pair_and_price
                        } else {
                            return Ok(None);
                        };

                        if currency_pair_and_price.pair.base != currency_pair.base {
                            currency_pair_and_price = currency_pair_and_price.invert()
                        }

                        Ok(Some(currency_pair_and_price))
                    }
                }),
        )
        .await
        .into_iter()
        .collect::<Result<Vec<_>, Error>>()?
        .into_iter()
        .flatten()
        .collect::<Vec<_>>())
    }

    pub async fn get_value_or_median(
        &self,
        price_config: PriceConfig<Currency>,
    ) -> Result<CurrencyPairAndPrice<Currency>, Error> {
        let pair = price_config.pair.clone();
        let price = if let Some(value) = price_config.value {
            value
        } else {
            Data::new(
                self.get_prices(price_config)
                    .await?
                    .into_iter()
                    .map(|cup| cup.price)
                    .collect::<Vec<f64>>(),
            )
            .median()
        };
        Ok(CurrencyPairAndPrice { pair, price })
    }
}

#[async_trait]
trait BitcoinFeed {
    async fn get_fee_estimate(&self, confirmation_target: u32) -> Result<f64, Error>;
}

pub struct BitcoinFeeds(Vec<Box<dyn BitcoinFeed>>);

impl BitcoinFeeds {
    pub fn new() -> Self {
        Self(vec![])
    }

    pub fn maybe_add_blockstream(&mut self, opts: BlockstreamCli) {
        if let Some(api) = BlockstreamApi::from_opts(opts) {
            log::info!("🔗 Blockstream");
            self.0.push(Box::new(api));
        }
    }

    pub fn maybe_add_blockcypher(&mut self, opts: BlockCypherCli) {
        if let Some(api) = BlockCypherApi::from_opts(opts) {
            log::info!("🔗 BlockCypher");
            self.0.push(Box::new(api));
        }
    }

    async fn get_fee_estimates(&self, confirmation_target: u32) -> Result<Vec<f64>, Error> {
        join_all(self.0.iter().map(|feed| feed.get_fee_estimate(confirmation_target)))
            .await
            .into_iter()
            .collect::<Result<Vec<_>, Error>>()
    }

    pub async fn maybe_get_median(&self, confirmation_target: u32) -> Result<Option<f64>, Error> {
        Ok(if self.0.is_empty() {
            None
        } else {
            Some(Data::new(self.get_fee_estimates(confirmation_target).await?).median())
        })
    }
}