-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduces a in-memory price cache (#329)
- Loading branch information
Showing
7 changed files
with
68 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use std::collections::HashMap; | ||
use std::sync::RwLock; | ||
use serde::Deserialize; | ||
use anyhow::Result; | ||
use tracing::info; | ||
use once_cell::sync::Lazy; | ||
|
||
const YADIO_API_URL: &str = "https://api.yadio.io/exrates/BTC"; | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct YadioResponse { | ||
#[serde(rename = "BTC")] | ||
btc: HashMap<String, f64>, | ||
} | ||
|
||
static BITCOIN_PRICES: Lazy<RwLock<HashMap<String, f64>>> = Lazy::new(|| RwLock::new(HashMap::new())); | ||
|
||
pub struct BitcoinPriceManager; | ||
|
||
impl BitcoinPriceManager { | ||
pub async fn update_prices() -> Result<()> { | ||
let response = reqwest::get(YADIO_API_URL).await?; | ||
let yadio_response: YadioResponse = response.json().await?; | ||
info!( | ||
"Bitcoin prices updated. Got BTC price in {} fiat currencies", | ||
yadio_response.btc.keys().collect::<Vec<&String>>().len() | ||
); | ||
|
||
let mut prices_write = BITCOIN_PRICES.write().unwrap(); | ||
*prices_write = yadio_response.btc; | ||
Ok(()) | ||
} | ||
|
||
pub fn get_price(currency: &str) -> Option<f64> { | ||
let prices_read = BITCOIN_PRICES.read().unwrap(); | ||
prices_read.get(currency).cloned() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters