Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a few missing endpoints and bug fixes #20

Merged
merged 9 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ license = "Apache-2.0"
[dependencies]
reqwest = "0.11"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_json = "1.0"
rust_decimal = "1.34"
serde_qs = "0.12"
chrono = { version = "0.4", features = ["serde"] }
2 changes: 1 addition & 1 deletion src/client/epochs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl Maestro {
}

pub async fn specific_epoch(&self, epoch_no: i32) -> Result<EpochResp, Box<dyn Error>> {
let url = format!("/epochs/{}/info", epoch_no);
let url = format!("/epochs/{}", epoch_no);
let resp = self.get(&url).await?;
let specific_epoch =
serde_json::from_str(&resp).map_err(|e| Box::new(e) as Box<dyn Error>)?;
Expand Down
23 changes: 23 additions & 0 deletions src/client/markets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use crate::models::markets::{DexPairOHLC, DexPairOHLCParameters};

use super::maestro::Maestro;
use std::{error::Error, fmt::Display};

impl Maestro {
/// Returns market activity in candlestick OHLC format for a specific DEX and token pair
pub async fn markets_dex_pair_ohlc(
&self,
dex: impl Display,
pair: impl Display,
parameters: Option<DexPairOHLCParameters>,
) -> Result<Vec<DexPairOHLC>, Box<dyn Error>> {
let ps = parameters
.and_then(|p| serde_qs::to_string(&p).ok())
.map(|x| format!("?{x}"))
.unwrap_or_default();
let url = format!("/markets/dexs/ohlc/{dex}/{pair}{ps}");
let resp = self.get(&url).await?;

serde_json::from_str(&resp).map_err(|e| Box::new(e) as Box<dyn Error>)
}
}
1 change: 1 addition & 0 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod epochs;
pub mod general;
pub mod linear_vesting;
pub mod maestro;
pub mod markets;
pub mod pools;
pub mod scripts;
pub mod transactions;
Expand Down
22 changes: 20 additions & 2 deletions src/client/pools.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use super::maestro::Maestro;
use crate::{
models::pools::{
PoolMintedBlocks, RegisteredPools, StakePoolDelegators, StakePoolHistory,
StakePoolInformation, StakePoolMetadata, StakePoolRelays, StakePoolUpdates,
PoolMintedBlocks, RegisteredPools, StakePoolDelegatorHistory, StakePoolDelegators,
StakePoolHistory, StakePoolInformation, StakePoolMetadata, StakePoolRelays,
StakePoolUpdates,
},
utils::Parameters,
};
Expand Down Expand Up @@ -103,4 +104,21 @@ impl Maestro {
serde_json::from_str(&resp).map_err(|e| Box::new(e) as Box<dyn Error>)?;
Ok(stake_pool_updates)
}

pub async fn stake_pool_delegator_history(
&self,
pool_id: &str,
epoch_no: i64,
params: Option<Parameters>,
) -> Result<StakePoolDelegatorHistory, Box<dyn Error>> {
let formatted_params = params.map(|p| p.format()).unwrap_or_default();
let url = format!(
"/pools/{}/delegators/{}{}",
pool_id, epoch_no, formatted_params
);
let resp = self.get(&url).await?;
let stake_pool_delegator_history =
serde_json::from_str(&resp).map_err(|e| Box::new(e) as Box<dyn Error>)?;
Ok(stake_pool_delegator_history)
}
}
4 changes: 2 additions & 2 deletions src/models/epochs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use serde::Deserialize;
pub struct Epoch {
pub blk_count: i32,
pub epoch_no: i32,
pub fees: i32,
pub start_time: i32,
pub fees: String,
pub start_time: i64,
pub tx_count: i32,
}

Expand Down
60 changes: 60 additions & 0 deletions src/models/markets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize)]
pub struct DexPairOHLC {
pub coin_a_change_pct: Decimal,
pub coin_a_close: Decimal,
pub coin_a_high: Decimal,
pub coin_a_low: Decimal,
pub coin_a_open: Decimal,
pub coin_a_volume: Decimal,
pub coin_b_change_pct: Decimal,
pub coin_b_close: Decimal,
pub coin_b_high: Decimal,
pub coin_b_low: Decimal,
pub coin_b_open: Decimal,
pub coin_b_volume: Decimal,
pub count: i64,
pub timestamp: DateTime<Utc>,
}

#[derive(Clone, Copy, Debug, Serialize)]
pub enum Resolution {
#[serde(rename = "1m")]
OneMinute,
#[serde(rename = "5m")]
FiveMinutes,
#[serde(rename = "15m")]
FifteenMinutes,
#[serde(rename = "30m")]
ThirtyMinutes,
#[serde(rename = "1h")]
OneHour,
#[serde(rename = "4h")]
FourHours,
#[serde(rename = "1d")]
OneDay,
#[serde(rename = "1w")]
OneWeek,
#[serde(rename = "1mo")]
OneMonth,
}

#[derive(Clone, Copy, Debug, Serialize)]
pub enum Sort {
#[serde(rename = "asc")]
Asc,
#[serde(rename = "desc")]
Desc,
}

#[derive(Clone, Debug, Default, Serialize)]
pub struct DexPairOHLCParameters {
pub resolution: Option<Resolution>,
pub from: Option<String>,
pub to: Option<String>,
pub limit: Option<i64>,
pub sort: Option<Sort>,
}
1 change: 1 addition & 0 deletions src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod datum;
pub mod epochs;
pub mod general;
pub mod linear_vesting;
pub mod markets;
pub mod pools;
pub mod scripts;
pub mod transactions;
Expand Down
15 changes: 14 additions & 1 deletion src/models/pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub struct StakePoolHistoryData {
pub epoch_no: i64,
pub epoch_ros: String,
pub fixed_cost: i64,
pub margin: i64,
pub margin: serde_json::Value,
pub pool_fees: i64,
pub saturation_pct: serde_json::Value,
}
Expand Down Expand Up @@ -142,3 +142,16 @@ pub struct StakePoolUpdates {
pub data: Vec<StakePoolDetails>,
pub last_updated: utils::LastUpdated,
}

#[derive(Deserialize)]
pub struct StakePoolDelegatorHistoryData {
pub amount: String,
pub stake_address: String,
}

#[derive(Deserialize)]
pub struct StakePoolDelegatorHistory {
pub data: Vec<StakePoolDelegatorHistoryData>,
pub last_updated: utils::LastUpdated,
pub next_cursor: Option<String>,
}
Loading