Skip to content

Commit

Permalink
Add get price task (#20)
Browse files Browse the repository at this point in the history
* implement get price task

* let get_price task into metrics system

* update readme

* update description
  • Loading branch information
tommady authored Aug 31, 2022
1 parent b0e10d0 commit 6c36839
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This exporter has below custom metrics right now!
| BridgedBalance | the specific token balance of reserving safe on source chain ( this metric displays with 6 digits as the fractional part so need to divide the metric value by 10 to the power of 6 ) |
| BridgedSupply | the token supply total minted on the destination chain ( this metric displays with 6 digits as the fractional part so need to divide the metric value by 10 to the power of 6 ) |
| NavtiveBalance | the native balance of reserving safe on source chain ( this metric displays with 6 digits as the fractional part so need to divide the metric value by 10 to the power of 6 ) |
| GetPrice | the close price of the related currency pair from gate.io ( this metric displays with 6 digits as the fractional part so need to divide the metric value by 10 to the power of 6 ) |

## Installation

Expand Down
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pub(crate) enum TaskName {
BridgedBalance,
BridgedSupply,
NativeBalance,
GetPrice,
}

impl Default for TaskName {
Expand Down
8 changes: 8 additions & 0 deletions src/crawler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ impl Crawler {
target.extra_opts.clone(),
crate::tasks::native_balance,
),
TaskName::GetPrice => Task::new(
"get_price".to_string(),
// in this case the host_addr is the currency_pair
target.host_addr.clone(),
metric,
target.extra_opts.clone(),
crate::tasks::get_price,
),
};

tasks.push(Arc::new(task));
Expand Down
5 changes: 5 additions & 0 deletions src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ where
"the native balance of reserving safe on source chain",
)
.context("new native_balance failed")?,
TaskName::GetPrice => GenericGauge::new(
"get_price",
"the close price of the related currency pair from gate.io",
)
.context("new get_price failed")?,
};

registry
Expand Down
81 changes: 81 additions & 0 deletions src/tasks/get_price.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// curl -H 'Accept: application/json' -X GET https://api.gateio.ws/api/v4/spot/candlesticks\?currency_pair\=FRA_USDT\&interval\=15m\&limit\=1
// [[unix_timestamp, trading_volume, close_price, highest_price, lowest_price, open_price]]
// [["1645749900","2839.79160470986265","0.01815","0.01897","0.01793","0.01889"]]
use crate::config::ExtraOpts;

use anyhow::{bail, Context, Result};
use prometheus::core::Number;
use serde_json::Value;

pub(crate) fn get_price<N: Number>(pair: &str, _opts: &Option<ExtraOpts>) -> Result<N> {
// https://api.gateio.ws/api/v4/spot/candlesticks\?currency_pair\=FRA_USDT\&interval\=15m\&limit\=1
let path = format!(
"https://api.gateio.ws/api/v4/spot/candlesticks?interval=15m&limit=1&currency_pair={}",
pair
);

let data: Value = ureq::get(&path)
.set("Accept", "application/json")
.call()
.with_context(|| format!("requesting gate.io call failed, pair:{:?}", pair))?
.into_json()
.with_context(|| format!("requesting gate.io json failed, pair:{:?}", pair))?;

if !data.is_array() || !data[0].is_array() {
bail!(
"the data is not an array which is:{:?}, pair:{:?}",
data,
pair
)
}

let d = data
.as_array()
.with_context(|| format!("data as_array failed, data:{:?}, pair:{:?}", data, pair))?;

if d.len() != 1 {
bail!(
"first level of data length is not 1: data:{:?}, pair:{:?}",
data,
pair
);
}

let info = d[0]
.as_array()
.with_context(|| format!("info as_array failed, data:{:?}, pair:{:?}", data, pair))?;

if info.len() < 3 {
bail!(
"second level of data length is smaller than 3: data:{:?}, pair:{:?}",
data,
pair
);
}

let price = info[2].as_str().with_context(|| {
format!(
"close price as_str failed, price:{:?}, pair:{:?}",
info[2], pair
)
})?;

let price = price.parse::<f64>().with_context(|| {
format!(
"close price parse into f64 failed, price:{:?}, pair:{:?}",
price, pair,
)
})?;

Ok(N::from_i64((price * 1000000.0).round() as i64))
}

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

#[test]
fn test_get_price() {
assert!(get_price::<u64>("FRA_USDT", &None).is_ok())
}
}
2 changes: 2 additions & 0 deletions src/tasks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ mod bridged_supply;
pub(crate) use bridged_supply::bridged_supply;
mod native_balance;
pub(crate) use native_balance::native_balance;
mod get_price;
pub(crate) use get_price::get_price;
10 changes: 0 additions & 10 deletions src/tasks/native_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,6 @@ mod tests {

#[test]
fn test_native_balance() {
println!(
"{:?}",
native_balance::<u64>(
"https://data-seed-prebsc-1-s1.binance.org:8545",
&Some(ExtraOpts::NativeBalance {
native_address: "0xae13d989dac2f0debff460ac112a837c89baa7cd".to_string(),
decimal: 18,
}),
)
);
assert!(native_balance::<u64>(
"https://data-seed-prebsc-1-s1.binance.org:8545",
&Some(ExtraOpts::NativeBalance {
Expand Down

0 comments on commit 6c36839

Please sign in to comment.