Skip to content
Closed
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
2 changes: 1 addition & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ cargo run --release -q --example "cex-candle"
## DEX Trade

```bash
cargo run --release -q --example "dex-trade"
cargo run --release -q --example "dex"
```

## DEX Candle
Expand Down
2 changes: 1 addition & 1 deletion examples/cex-candle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn main() {

// CEX Candle Data
let candle_options = datamaxi::cex::CandleOptions::new();
let candle_response = candle.get("binance", "ETH-USDT", candle_options);
let candle_response = candle.get("binance", "BTC-USDT", "futures", candle_options);
match candle_response {
Ok(answer) => match serde_json::to_string(&answer) {
Ok(json) => println!("{}", json),
Expand Down
4 changes: 2 additions & 2 deletions examples/dex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn main() {
let trade_response = dex.trade(
"bsc_mainnet",
"pancakeswap",
"0xb24cd29e32FaCDDf9e73831d5cD1FFcd1e535423",
"0x6ee3eE9C3395BbD136B6076A70Cb6cFF241c0E24",
trade_options,
);
match trade_response {
Expand All @@ -55,7 +55,7 @@ fn main() {
let candle_response = dex.candle(
"bsc_mainnet",
"pancakeswap",
"0xb24cd29e32FaCDDf9e73831d5cD1FFcd1e535423",
"0x6ee3eE9C3395BbD136B6076A70Cb6cFF241c0E24",
params,
);
match candle_response {
Expand Down
9 changes: 6 additions & 3 deletions src/cex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,31 @@ impl Candle {
/// Retrieves candle data for a specified exchange and symbol. Additional parameters can be
/// provided to filter and sort the results. The response will contain an array of candle data
/// objects, each representing a single candle with open, high, low, close, and volume values.
pub fn get<E, S>(
pub fn get<E, S, M>(
&self,
exchange: E,
symbol: S,
market: M,
options: CandleOptions,
) -> Result<CandleResponse>
where
E: Into<String>,
S: Into<String>,
M: Into<String>,
{
let mut parameters = HashMap::new();

// required
parameters.insert("exchange".to_string(), exchange.into());
parameters.insert("market".to_string(), market.into());
parameters.insert("symbol".to_string(), symbol.into());

// optional
parameters.extend(
[
options
.market
.map(|market| ("market".to_string(), market.to_string())),
.currency
.map(|currency| ("currency".to_string(), currency.to_string())),
options
.interval
.map(|interval| ("interval".to_string(), interval.to_string())),
Expand Down
23 changes: 10 additions & 13 deletions src/dex.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::api::{Client, Config, Datamaxi, Result};
pub use crate::models::{CandleOptions, PoolsOptions, TradeOptions};
use crate::models::{CandleResponse, PoolsResponse, TradeResponse};
use crate::models::{DexCandleResponse, PoolsResponse, TradeResponse};
use std::collections::HashMap;

/// Provides methods for retrieving DEX candle data and related information.
Expand All @@ -19,7 +19,7 @@ impl Dex {
exchange: E,
pool: P,
options: CandleOptions,
) -> Result<CandleResponse>
) -> Result<DexCandleResponse>
where
C: Into<String>,
E: Into<String>,
Expand All @@ -35,22 +35,19 @@ impl Dex {
// optional
parameters.extend(
[
options
.market
.map(|market| ("market".to_string(), market.to_string())),
options
.interval
.map(|interval| ("interval".to_string(), interval.to_string())),
options
.from
.map(|from| ("from".to_string(), from.to_string())),
options.to.map(|to| ("to".to_string(), to.to_string())),
options
.page
.map(|page| ("page".to_string(), page.to_string())),
options
.limit
.map(|limit| ("limit".to_string(), limit.to_string())),
options
.from
.map(|from| ("from".to_string(), from.to_string())),
options.to.map(|to| ("to".to_string(), to.to_string())),
options
.sort
.map(|sort| ("sort".to_string(), sort.to_string())),
Expand Down Expand Up @@ -87,16 +84,16 @@ impl Dex {
// optional
parameters.extend(
[
options
.from
.map(|from| ("from".to_string(), from.to_string())),
options.to.map(|to| ("to".to_string(), to.to_string())),
options
.page
.map(|page| ("page".to_string(), page.to_string())),
options
.limit
.map(|limit| ("limit".to_string(), limit.to_string())),
options
.from
.map(|from| ("from".to_string(), from.to_string())),
options.to.map(|to| ("to".to_string(), to.to_string())),
options
.sort
.map(|sort| ("sort".to_string(), sort.to_string())),
Expand Down
104 changes: 75 additions & 29 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ use serde::Serialize;
pub struct CandleDetail {
/// The timestamp of the candle's open time.
#[serde(rename = "d")]
pub timestamp: String,
pub timestamp: f64,

/// The opening price of the asset at the beginning of the time frame.
#[serde(rename = "o")]
pub open: String,
pub open: f64,

/// The highest price of the asset during the time frame.
#[serde(rename = "h")]
pub high: String,
pub high: f64,

/// The lowest price of the asset during the time frame.
#[serde(rename = "l")]
pub low: String,
pub low: f64,

/// The closing price of the asset at the end of the time frame.
#[serde(rename = "c")]
pub close: String,
pub close: f64,

/// The total volume of trades (in the base currency) that occurred during the time frame.
#[serde(rename = "v")]
pub volume: String,
pub volume: f64,
}

/// Response containing candle data.
Expand All @@ -35,40 +35,82 @@ pub struct CandleResponse {
/// A vector containing detailed information about each candle.
pub data: Vec<CandleDetail>,

/// The current page number in the paginated response.
pub page: i32,
pub currency: String,

/// The maximum number of items per page in the response.
pub limit: i32,
pub interval: String,

/// The starting point of the time frame for the candle data.
pub from: String,
pub market: String,

/// The ending point of the time frame for the candle data.
pub to: String,
pub symbol: String,
}

/// Detailed information about a dex candle.
#[derive(Serialize, Deserialize, Debug)]
pub struct DexCandleDetail {
/// The timestamp of the candle's open time.
#[serde(rename = "d")]
pub timestamp: f64,

/// The opening price of the asset at the beginning of the time frame.
#[serde(rename = "o")]
pub open: f64,

/// The highest price of the asset during the time frame.
#[serde(rename = "h")]
pub high: f64,

/// The lowest price of the asset during the time frame.
#[serde(rename = "l")]
pub low: f64,

/// The closing price of the asset at the end of the time frame.
#[serde(rename = "c")]
pub close: f64,

/// Specifies trading volume (base token) of the candle.
#[serde(rename = "bv")]
pub basevolume: f64,

/// Specifies trading volume (quote token) of the candle.
#[serde(rename = "qv")]
pub quotevolume: f64,
}

/// Response containing dex candle data
#[derive(Serialize, Deserialize, Debug)]
pub struct DexCandleResponse {
/// A vector containing detailed information about each candle.
pub data: Vec<DexCandleDetail>,

pub exchange: String,

pub chain: String,

pub pool: String,

pub interval: String,

/// The sorting order for the candle data (e.g., "asc" or "desc").
pub sort: String,

pub page: i32,

pub limit: i32,
}

/// Detailed information about a trade.
#[derive(Serialize, Deserialize, Debug)]
pub struct TradeDetail {
/// The timestamp of the trade.
#[serde(rename = "d")]
pub timestamp: String,
pub timestamp: i64,

/// The block number in which the trade was recorded.
#[serde(rename = "b")]
pub block_number: i64,

/// The trading pool where the trade occurred.
#[serde(rename = "pool")]
pub pool: String,
pub base: String,

/// The trading symbol associated with the trade (e.g., BTC-USDT).
#[serde(rename = "s")]
pub symbol: String,
pub quote: String,

/// The hash of the transaction related to the trade.
#[serde(rename = "tx")]
Expand All @@ -84,15 +126,15 @@ pub struct TradeDetail {

/// The quantity of the base asset traded, in base unit.
#[serde(rename = "bq")]
pub base_quantity: String,
pub base_quantity: f64,

/// The quantity of the quote asset traded, in base unit.
#[serde(rename = "qq")]
pub quote_quantity: String,
pub quote_quantity: f64,

/// The price of the trade in the quote asset's unit.
#[serde(rename = "p")]
pub price: String,
pub price: f64,
}

/// Response containing trade data.
Expand All @@ -107,11 +149,11 @@ pub struct TradeResponse {
/// The maximum number of items per page in the response.
pub limit: i32,

/// The starting point of the time frame for the trade data.
pub from: String,
pub exchange: String,

pub chain: String,

/// The ending point of the time frame for the trade data.
pub to: String,
pub pool: String,

/// The sorting order for the trade data (e.g., "asc" or "desc").
pub sort: String,
Expand Down Expand Up @@ -139,6 +181,9 @@ pub struct CandleOptions {

/// The sorting order for the candle data (e.g., "asc" or "desc").
pub sort: Option<String>,

/// The currency to display (e.g. USD)
pub currency: Option<String>,
}

impl Default for CandleOptions {
Expand All @@ -159,6 +204,7 @@ impl CandleOptions {
from: None,
to: None,
sort: Some("desc".into()),
currency: None,
}
}

Expand Down
Loading