Skip to content

Commit

Permalink
add: lib result and error types
Browse files Browse the repository at this point in the history
  • Loading branch information
negativefix committed May 30, 2022
1 parent d8dd101 commit 53aee2f
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 50 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
/Cargo.lock
/.vscode
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ readme = "README.md"

[dependencies]
serde = { version = "1.0.137", features = ["derive"] }
serde_json = "1.0.81"
tokio = { version = "1", features = ["full"] }
reqwest = { version = "0.11", features = ["json"] }
24 changes: 24 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use serde_json;
use reqwest;


pub type EGResult<T> = Result<T, Error>;

#[derive(Debug)]
pub enum Error {
Network(reqwest::Error),
Parse(serde_json::Error),
}


impl From<reqwest::Error> for Error {
fn from(e: reqwest::Error) -> Error {
Error::Network(e)
}
}

impl From<serde_json::Error> for Error {
fn from(e: serde_json::Error) -> Error {
Error::Parse(e)
}
}
98 changes: 51 additions & 47 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ extern crate serde;
extern crate reqwest;

use serde::Deserialize;
use reqwest::{Response, Result};
use reqwest::Response;

mod endpoints;

mod errors;
pub use errors::EGResult;

const ORIGIN: &str = "https://data.elrond.com/";

#[derive(Deserialize, Debug)]
Expand All @@ -16,7 +19,7 @@ pub struct DataPoint<T> {
pub value: T,
}

pub async fn fetch(endpoint: &str) -> Result<Response> {
pub async fn fetch(endpoint: &str) -> EGResult<Response> {
let url = format!("{}{}", ORIGIN, endpoint);
let res = reqwest::get(&url).await?;
println!("status: {:#?}", res.status());
Expand All @@ -26,24 +29,24 @@ pub async fn fetch(endpoint: &str) -> Result<Response> {
// Market value
pub mod market_value {

use super::Result;
use crate::EGResult;
use super::DataPoint;
use super::endpoints;
use super::fetch;

pub async fn price() -> Result<Vec<DataPoint<f64>>> {
pub async fn price() -> EGResult<Vec<DataPoint<f64>>> {
let res = fetch(endpoints::PRICE).await?;
let data_points = res.json().await?;
Ok(data_points)
}

pub async fn market_cap() -> Result<Vec<DataPoint<f64>>> {
pub async fn market_cap() -> EGResult<Vec<DataPoint<f64>>> {
let res = fetch(endpoints::MARKET_CAP).await?;
let data_points = res.json().await?;
Ok(data_points)
}

pub async fn exchange_volume_24h() -> Result<Vec<DataPoint<f64>>> {
pub async fn exchange_volume_24h() -> EGResult<Vec<DataPoint<f64>>> {
let res = fetch(endpoints::VOLUME_24H).await?;
let data_points = res.json().await?;
Ok(data_points)
Expand All @@ -55,24 +58,24 @@ pub mod market_value {
// Supply
pub mod supply {

use super::Result;
use crate::EGResult;
use super::DataPoint;
use super::endpoints;
use super::fetch;

pub async fn circulating_supply() -> Result<Vec<DataPoint<f64>>> {
pub async fn circulating_supply() -> EGResult<Vec<DataPoint<f64>>> {
let res = fetch(endpoints::CIRCULATING_SUPPLY).await?;
let data_points = res.json().await?;
Ok(data_points)
}

pub async fn floating_supply() -> Result<Vec<DataPoint<f64>>> {
pub async fn floating_supply() -> EGResult<Vec<DataPoint<f64>>> {
let res = fetch(endpoints::FLOATING_SUPPLY).await?;
let data_points = res.json().await?;
Ok(data_points)
}

pub async fn staked() -> Result<Vec<DataPoint<f64>>> {
pub async fn staked() -> EGResult<Vec<DataPoint<f64>>> {
let res = fetch(endpoints::STAKED).await?;
let data_points = res.json().await?;
Ok(data_points)
Expand All @@ -83,48 +86,48 @@ pub mod supply {
// User Adoption
pub mod user_adoption {

use super::Result;
use crate::EGResult;
use super::DataPoint;
use super::endpoints;
use super::fetch;

pub async fn user_count() -> Result<Vec<DataPoint<u64>>> {
pub async fn user_count() -> EGResult<Vec<DataPoint<u64>>> {
let res = fetch(endpoints::USER_COUNT).await?;
let data_points = res.json().await?;
Ok(data_points)
}

pub async fn user_count_gt_0() -> Result<Vec<DataPoint<u64>>> {
pub async fn user_count_gt_0() -> EGResult<Vec<DataPoint<u64>>> {
let res = fetch(endpoints::USER_COUNT_GT_0).await?;
let data_points = res.json().await?;
Ok(data_points)
}

pub async fn user_count_gt_0_1() -> Result<Vec<DataPoint<u64>>> {
pub async fn user_count_gt_0_1() -> EGResult<Vec<DataPoint<u64>>> {
let res = fetch(endpoints::USER_COUNT_GT_0_1).await?;
let data_points = res.json().await?;
Ok(data_points)
}

pub async fn user_count_gt_1() -> Result<Vec<DataPoint<u64>>> {
pub async fn user_count_gt_1() -> EGResult<Vec<DataPoint<u64>>> {
let res = fetch(endpoints::USER_COUNT_GT_1).await?;
let data_points = res.json().await?;
Ok(data_points)
}

pub async fn user_count_gt_10() -> Result<Vec<DataPoint<u64>>> {
pub async fn user_count_gt_10() -> EGResult<Vec<DataPoint<u64>>> {
let res = fetch(endpoints::USER_COUNT_GT_10).await?;
let data_points = res.json().await?;
Ok(data_points)
}

pub async fn user_count_gt_100() -> Result<Vec<DataPoint<u64>>> {
pub async fn user_count_gt_100() -> EGResult<Vec<DataPoint<u64>>> {
let res = fetch(endpoints::USER_COUNT_GT_100).await?;
let data_points = res.json().await?;
Ok(data_points)
}

pub async fn user_count_gt_1000() -> Result<Vec<DataPoint<u64>>> {
pub async fn user_count_gt_1000() -> EGResult<Vec<DataPoint<u64>>> {
let res = fetch(endpoints::USER_COUNT_GT_1000).await?;
let data_points = res.json().await?;
Ok(data_points)
Expand All @@ -135,18 +138,18 @@ pub mod user_adoption {
// Transactions
pub mod transactions {

use super::Result;
use crate::EGResult;
use super::DataPoint;
use super::endpoints;
use super::fetch;

pub async fn total_transactions() -> Result<Vec<DataPoint<f64>>> {
pub async fn total_transactions() -> EGResult<Vec<DataPoint<f64>>> {
let res = fetch(endpoints::TRANSACTIONS_TOTAL).await?;
let data_points = res.json().await?;
Ok(data_points)
}

pub async fn transactions_24h() -> Result<Vec<DataPoint<f64>>> {
pub async fn transactions_24h() -> EGResult<Vec<DataPoint<f64>>> {
let res = fetch(endpoints::TRANSACTIONS_24H).await?;
let data_points = res.json().await?;
Ok(data_points)
Expand All @@ -157,42 +160,42 @@ pub mod transactions {
// Staking Metrics
pub mod staking_metrics {

use super::Result;
use crate::EGResult;
use super::DataPoint;
use super::endpoints;
use super::fetch;

pub async fn queued_value_total() -> Result<Vec<DataPoint<u64>>> {
pub async fn queued_value_total() -> EGResult<Vec<DataPoint<u64>>> {
let res = fetch(endpoints::QUEUED_VALUE_TOTAL).await?;
let data_points = res.json().await?;
Ok(data_points)
}

pub async fn queued_user_total() -> Result<Vec<DataPoint<u64>>> {
pub async fn queued_user_total() -> EGResult<Vec<DataPoint<u64>>> {
let res = fetch(endpoints::QUEUED_USERS_TOTAL).await?;
let data_points = res.json().await?;
Ok(data_points)
}

pub async fn queued_staked_total() -> Result<Vec<DataPoint<u64>>> {
pub async fn queued_staked_total() -> EGResult<Vec<DataPoint<u64>>> {
let res = fetch(endpoints::QUEUED_STAKED_TOTAL).await?;
let data_points = res.json().await?;
Ok(data_points)
}

pub async fn queue_staking_total() -> Result<Vec<DataPoint<u64>>> {
pub async fn queue_staking_total() -> EGResult<Vec<DataPoint<u64>>> {
let res = fetch(endpoints::QUEUE_STAKING_TOTAL).await?;
let data_points = res.json().await?;
Ok(data_points)
}

pub async fn queued_delegated_total() -> Result<Vec<DataPoint<u64>>> {
pub async fn queued_delegated_total() -> EGResult<Vec<DataPoint<u64>>> {
let res = fetch(endpoints::QUEUE_DELEGATED_TOTAL).await?;
let data_points = res.json().await?;
Ok(data_points)
}

pub async fn queue_delegating_total() -> Result<Vec<DataPoint<f64>>> {
pub async fn queue_delegating_total() -> EGResult<Vec<DataPoint<f64>>> {
let res = fetch(endpoints::QUEUE_DELEGATING_TOTAL).await?;
let data_points = res.json().await?;
Ok(data_points)
Expand All @@ -202,73 +205,73 @@ pub mod staking_metrics {
// Exchanges
pub mod exchanges {

use super::Result;
use crate::EGResult;
use super::DataPoint;
use super::endpoints;
use super::fetch;

pub async fn exchanges_total_balance() -> Result<Vec<DataPoint<f64>>> {
pub async fn exchanges_total_balance() -> EGResult<Vec<DataPoint<f64>>> {
let res = fetch(super::endpoints::EXCHANGES_TOTAL_BALANCE).await?;
let data_points = res.json().await?;
Ok(data_points)
}

pub async fn bitmax_balance() -> Result<Vec<DataPoint<f64>>> {
pub async fn bitmax_balance() -> EGResult<Vec<DataPoint<f64>>> {
let res = fetch(endpoints::BITMAX_BALANCE).await?;
let data_points = res.json().await?;
Ok(data_points)
}

pub async fn binance_com_balance() -> Result<Vec<DataPoint<f64>>> {
pub async fn binance_com_balance() -> EGResult<Vec<DataPoint<f64>>> {
let res = fetch(endpoints::BINANCE_COM_BALANCE).await?;
let data_points = res.json().await?;
Ok(data_points)
}

pub async fn binance_us_balance() -> Result<Vec<DataPoint<f64>>> {
pub async fn binance_us_balance() -> EGResult<Vec<DataPoint<f64>>> {
let res = fetch(endpoints::BINANCE_US_BALANCE).await?;
let data_points = res.json().await?;
Ok(data_points)
}


pub async fn bitfinex_balance() -> Result<Vec<DataPoint<f64>>> {
pub async fn bitfinex_balance() -> EGResult<Vec<DataPoint<f64>>> {
let res = fetch(endpoints::BITFINEX_BALANCE).await?;
let data_points = res.json().await?;
Ok(data_points)
}

pub async fn bithumb_balance() -> Result<Vec<DataPoint<f64>>> {
pub async fn bithumb_balance() -> EGResult<Vec<DataPoint<f64>>> {
let res = fetch(endpoints::BITHUMB_BALANCE).await?;
let data_points = res.json().await?;
Ok(data_points)
}

pub async fn crypto_com_balance() -> Result<Vec<DataPoint<f64>>> {
pub async fn crypto_com_balance() -> EGResult<Vec<DataPoint<f64>>> {
let res = fetch(endpoints::CRYPTO_COM_BALANCE).await?;
let data_points = res.json().await?;
Ok(data_points)
}

pub async fn kucoin_balance() -> Result<Vec<DataPoint<f64>>> {
pub async fn kucoin_balance() -> EGResult<Vec<DataPoint<f64>>> {
let res = fetch(endpoints::KUCOIN_BALANCE).await?;
let data_points = res.json().await?;
Ok(data_points)
}

pub async fn okex_balance() -> Result<Vec<DataPoint<f64>>> {
pub async fn okex_balance() -> EGResult<Vec<DataPoint<f64>>> {
let res = fetch(endpoints::OKEX_BALANCE).await?;
let data_points = res.json().await?;
Ok(data_points)
}

pub async fn inflow_24h() -> Result<Vec<DataPoint<f64>>> {
pub async fn inflow_24h() -> EGResult<Vec<DataPoint<f64>>> {
let res = fetch(endpoints::INFLOW_24).await?;
let data_points = res.json().await?;
Ok(data_points)
}

pub async fn outflow_24h() -> Result<Vec<DataPoint<f64>>> {
pub async fn outflow_24h() -> EGResult<Vec<DataPoint<f64>>> {
let res = fetch(endpoints::OUTFLOW_24).await?;
let data_points = res.json().await?;
Ok(data_points)
Expand All @@ -280,40 +283,41 @@ pub mod exchanges {
// Socials
pub mod socials {

use super::Result;
use crate::EGResult;
use super::DataPoint;
use super::endpoints;
use super::fetch;

pub async fn mentions() -> Result<Vec<DataPoint<u64>>> {
pub async fn mentions() -> EGResult<Vec<DataPoint<u64>>> {
let res = fetch(endpoints::MENTIONS).await?;
let data_points = res.json().await?;
Ok(data_points)
}

pub async fn impressions() -> Result<Vec<DataPoint<f64>>> {
pub async fn impressions() -> EGResult<Vec<DataPoint<f64>>> {
let res = fetch(endpoints::IMPRESSIONS).await?;
let data_points = res.json().await?;
Ok(data_points)
}
}

pub mod dev_activity {
use super::{fetch, endpoints, Result, DataPoint};

pub async fn commits() -> Result<u32> {
use super::{fetch, endpoints, EGResult, DataPoint};

pub async fn commits() -> EGResult<u32> {
let res = fetch(endpoints::COMMITS).await?;
let commits = res.json().await?;
Ok(commits)
}

pub async fn stars() -> Result<u32> {
pub async fn stars() -> EGResult<u32> {
let res = fetch(endpoints::STARS).await?;
let stars = res.json().await?;
Ok(stars)
}

pub async fn commits_24h() -> Result<Vec<DataPoint<u64>>> {
pub async fn commits_24h() -> EGResult<Vec<DataPoint<u64>>> {
let res = fetch(endpoints::COMMITS_24H).await?;
let data_points = res.json().await?;
Ok(data_points)
Expand Down
8 changes: 5 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use elrond_growth_rust::market_value;
use reqwest::Result;
extern crate elrond_growth_rust;

use elrond_growth_rust::{market_value, EGResult};


#[tokio::main]
async fn main() -> Result<()> {
async fn main() -> EGResult<()> {
let price_data = market_value::price().await?;
println!("{:#?}", price_data.last().unwrap().value);
Ok(())
Expand Down

0 comments on commit 53aee2f

Please sign in to comment.