parser: add auto fetch price from main instance

This commit is contained in:
k
2024-07-22 15:58:02 +02:00
parent 232276d106
commit 9871fdffc9
9 changed files with 183 additions and 23 deletions
+2 -2
View File
@@ -107,7 +107,7 @@ impl Binance {
log("binance: fetch 1mn");
retry(
|| {
|_| {
let body: Value = reqwest::blocking::get(
"https://api.binance.com/api/v3/uiKlines?symbol=BTCUSDT&interval=1m&limit=1000",
)?
@@ -154,7 +154,7 @@ impl Binance {
log("binance: fetch 1d");
retry(
|| {
|_| {
let body: Value = reqwest::blocking::get(
"https://api.binance.com/api/v3/uiKlines?symbol=BTCUSDT&interval=1d",
)?
+2 -2
View File
@@ -16,7 +16,7 @@ impl Kraken {
log("kraken: fetch 1mn");
retry(
|| {
|_| {
let body: Value = reqwest::blocking::get(
"https://api.kraken.com/0/public/OHLC?pair=XBTUSD&interval=1",
)?
@@ -70,7 +70,7 @@ impl Kraken {
log("fetch kraken daily");
retry(
|| {
|_| {
let body: Value = reqwest::blocking::get(
"https://api.kraken.com/0/public/OHLC?pair=XBTUSD&interval=1440",
)?
+2
View File
@@ -1,5 +1,7 @@
mod binance;
mod kraken;
mod satonomics;
pub use binance::*;
pub use kraken::*;
pub use satonomics::*;
+113
View File
@@ -0,0 +1,113 @@
use std::{collections::BTreeMap, str::FromStr};
use chrono::NaiveDate;
use color_eyre::eyre::ContextCompat;
use itertools::Itertools;
use serde_json::Value;
use crate::{
datasets::OHLC,
structs::{Date, DateMapChunkId, HeightMapChunkId},
utils::{log, retry},
MapChunkId,
};
pub struct Satonomics;
const SATONOMICS_OFFICIAL_URL: &str = "https://api.satonomics.xyz";
const SATONOMICS_OFFICIAL_BACKUP_URL: &str = "https://api-bkp.satonomics.xyz";
const RETRIES: usize = 10;
impl Satonomics {
fn get_base_url(try_index: usize) -> &'static str {
if try_index < RETRIES / 2 {
SATONOMICS_OFFICIAL_URL
} else {
SATONOMICS_OFFICIAL_BACKUP_URL
}
}
pub fn fetch_height_prices(chunk_id: HeightMapChunkId) -> color_eyre::Result<Vec<OHLC>> {
log("satonomics: fetch height prices");
retry(
|try_index| {
let base_url = Self::get_base_url(try_index);
let body: Value = reqwest::blocking::get(format!(
"{base_url}/height-to-price?chunk={}",
chunk_id.to_usize()
))?
.json()?;
Ok(body
.as_object()
.context("Expect to be an object")?
.get("dataset")
.context("Expect object to have dataset")?
.as_object()
.context("Expect to be an object")?
.get("map")
.context("Expect to have map")?
.as_array()
.context("Expect to be an array")?
.iter()
.map(Self::value_to_ohlc)
.collect_vec())
},
10,
RETRIES,
)
}
pub fn fetch_date_prices(chunk_id: DateMapChunkId) -> color_eyre::Result<BTreeMap<Date, OHLC>> {
log("satonomics: date height prices");
retry(
|try_index| {
let base_url = Self::get_base_url(try_index);
let body: Value = reqwest::blocking::get(format!(
"{base_url}/date-to-price?chunk={}",
chunk_id.to_usize()
))?
.json()?;
Ok(body
.as_object()
.context("Expect to be an object")?
.get("dataset")
.context("Expect object to have dataset")?
.as_object()
.context("Expect to be an object")?
.get("map")
.context("Expect to have map")?
.as_object()
.context("Expect to be an object")?
.iter()
.map(|(serialized_date, value)| {
let date = Date::wrap(NaiveDate::from_str(serialized_date).unwrap());
(date, Self::value_to_ohlc(value))
})
.collect::<BTreeMap<_, _>>())
},
10,
RETRIES,
)
}
fn value_to_ohlc(value: &Value) -> OHLC {
let ohlc = value.as_object().unwrap();
let get_value = |key: &str| ohlc.get(key).unwrap().as_f64().unwrap() as f32;
OHLC {
open: get_value("open"),
high: get_value("high"),
low: get_value("low"),
close: get_value("close"),
}
}
}