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
use super::{get_http, PriceFeed};
use crate::{config::CurrencyStore, currency::*, Error};
use async_trait::async_trait;
use clap::Parser;
use reqwest::Url;
use serde_json::Value;

const COINGECKO_API_KEY_PARAMETER: &str = "x_cg_pro_api_key";

#[derive(Parser, Debug, Clone)]
pub struct CoinGeckoCli {
    /// Fetch the exchange rate from CoinGecko (https://api.coingecko.com/api/v3/).
    #[clap(long)]
    coingecko_url: Option<Url>,

    /// Use a dedicated API key for coingecko pro URL (https://pro-api.coingecko.com/api/v3/)
    #[clap(long)]
    coingecko_api_key: Option<String>,
}

pub struct CoinGeckoApi {
    url: Url,
    api_key: Option<String>,
}

impl Default for CoinGeckoApi {
    fn default() -> Self {
        Self {
            url: Url::parse("https://api.coingecko.com/api/v3").unwrap(),
            api_key: None,
        }
    }
}

fn extract_response(value: Value, base: &str, quote: &str) -> Option<f64> {
    value.get(base)?.get(quote)?.as_f64()
}

impl CoinGeckoApi {
    pub fn from_opts(opts: CoinGeckoCli) -> Option<Self> {
        if let Some(url) = opts.coingecko_url {
            let mut api = Self::new(url);
            if let Some(api_key) = opts.coingecko_api_key {
                api.with_key(api_key)
            }
            Some(api)
        } else {
            None
        }
    }

    pub fn new(url: Url) -> Self {
        Self { url, api_key: None }
    }

    pub fn with_key(&mut self, api_key: String) {
        self.api_key = Some(api_key);
    }

    async fn get_exchange_rate(
        &self,
        currency_pair: CurrencyPair<Currency>,
        currency_store: &CurrencyStore<String>,
    ) -> Result<CurrencyPairAndPrice<Currency>, Error> {
        let base = currency_pair
            .base
            .path()
            .or_else(|| Some(currency_store.name(&currency_pair.base.symbol())?.to_lowercase()))
            .ok_or(Error::InvalidCurrency)?;
        let quote = currency_pair
            .quote
            .path()
            .unwrap_or_else(|| currency_pair.quote.symbol().to_lowercase());

        // https://www.coingecko.com/api/documentations/v3
        let mut url = self.url.clone();
        url.set_path(&format!("{}/simple/price", url.path()));
        url.set_query(Some(&format!("ids={base}&vs_currencies={quote}")));
        if let Some(api_key) = &self.api_key {
            url.query_pairs_mut().append_pair(COINGECKO_API_KEY_PARAMETER, api_key);
        }

        let data = get_http(url).await?;
        let exchange_rate = extract_response(data, &base, &quote).ok_or(Error::InvalidResponse)?;

        Ok(CurrencyPairAndPrice {
            pair: currency_pair,
            price: exchange_rate,
        })
    }
}

#[async_trait]
impl PriceFeed for CoinGeckoApi {
    async fn get_price(
        &self,
        currency_pair: CurrencyPair<Currency>,
        currency_store: &CurrencyStore<String>,
    ) -> Result<CurrencyPairAndPrice<Currency>, Error> {
        self.get_exchange_rate(currency_pair, currency_store).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn should_extract_response() {
        assert_eq!(
            extract_response(
                json!({
                    "bitcoin":{"usd":19148.24}
                }),
                "bitcoin",
                "usd"
            ),
            Some(19148.24)
        )
    }
}