Skip to content

Commit

Permalink
refactor: use path syntax for dia fair price
Browse files Browse the repository at this point in the history
Signed-off-by: Gregory Hill <[email protected]>
  • Loading branch information
gregdhill authored and sander2 committed Sep 11, 2023
1 parent edfa087 commit c1fa4f1
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 35 deletions.
5 changes: 1 addition & 4 deletions oracle/examples/kintsugi-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,7 @@
"feeds": {
"dia_fair_price": [
[
{
"symbol": "BTC",
"alias": "KBTC"
},
"BTC=KBTC",
"USD"
],
[
Expand Down
26 changes: 7 additions & 19 deletions oracle/src/currency.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(clippy::upper_case_acronyms)]

use crate::{feeds::DiaFairPriceExt, CurrencyStore, Error};
use crate::{CurrencyStore, Error};
use runtime::{FixedPointNumber, FixedPointTraits::*, FixedU128};
use serde::Deserialize;
use std::fmt::{self, Debug};
Expand All @@ -20,21 +20,13 @@ pub trait CurrencyInfo<Currency> {
fn decimals(&self, id: &Currency) -> Option<u32>;
}

#[derive(Deserialize, Debug, Clone)]
pub struct Extended<Ext> {
symbol: String,
#[serde(flatten)]
pub(crate) ext: Option<Ext>,
}

#[derive(Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum Currency {
#[serde(deserialize_with = "deserialize_as_string")]
Symbol(String),
#[serde(deserialize_with = "deserialize_as_tuple")]
Path(String, String),
DiaFairPrice(Extended<DiaFairPriceExt>),
}

fn deserialize_as_string<'de, D>(deserializer: D) -> Result<String, D::Error>
Expand Down Expand Up @@ -66,23 +58,13 @@ impl Currency {
match self {
Self::Symbol(symbol) => symbol.to_owned(),
Self::Path(symbol, _) => symbol.to_owned(),
Self::DiaFairPrice(extended) => extended.symbol.to_owned(),
}
}

pub fn path(&self) -> Option<String> {
match self {
Self::Symbol(_) => None,
Self::Path(_, path) => Some(path.to_owned()),
Self::DiaFairPrice(_) => None,
}
}

pub fn dia_fair_price_ext(&self) -> Option<DiaFairPriceExt> {
match self {
Self::Symbol(_) => None,
Self::Path(_, _) => None,
Self::DiaFairPrice(extended) => extended.ext.to_owned(),
}
}
}
Expand Down Expand Up @@ -300,6 +282,12 @@ mod tests {

// BTC/USD * DOT/BTC = USD/DOT
assert_reduce!(("BTC" / "USD" @ 19184.24) * ("DOT" / "BTC" @ 0.00032457) = ("USD" / "DOT" @ 0.16060054900429147));

// BTC/USD * KSM/USD = BTC/KSM
assert_reduce!(("BTC" / "USD" @ 27356.159557758947) * ("KSM" / "USD" @ 19.743996225593296) = ("BTC" / "KSM" @ 1385.5431922286498));

// USD/BTC * USD/KSM = BTC/KSM
assert_reduce!(("USD" / "BTC" @ 3.655107115877481e-5) * ("USD" / "KSM" @ 0.05052177613811538) = ("BTC" / "KSM" @ 1382.2242286321239));
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion oracle/src/feeds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ 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, DiaFairPriceExt};
pub use dia_fair_price::{DiaFairPriceApi, DiaFairPriceCli};
pub use gateio::{GateIoApi, GateIoCli};
pub use kraken::{KrakenApi, KrakenCli};

Expand Down
12 changes: 1 addition & 11 deletions oracle/src/feeds/dia_fair_price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@ use crate::{config::CurrencyStore, currency::*, Error};
use async_trait::async_trait;
use clap::Parser;
use reqwest::Url;
use serde::Deserialize;
use serde_json::Value;

#[derive(Deserialize, Debug, Clone)]
pub struct DiaFairPriceExt {
alias: Option<String>,
}

#[derive(Parser, Debug, Clone)]
pub struct DiaFairPriceCli {
/// Fetch the exchange rate from Dia xLSD feed
Expand Down Expand Up @@ -60,11 +54,7 @@ impl DiaFairPriceApi {
// this allows us to override the expected token name
// which is helpful when using the xlsd feed of a wrapped token
// but we want to submit the currency as the underlying (e.g. KBTC -> BTC)
let alias = currency_pair
.base
.dia_fair_price_ext()
.and_then(|ext| ext.alias)
.unwrap_or(currency_pair.base.symbol());
let alias = currency_pair.base.path().unwrap_or(currency_pair.base.symbol());

let url = self.url.clone();
let data = get_http(url).await?;
Expand Down

0 comments on commit c1fa4f1

Please sign in to comment.