Skip to content

Commit e9cef4d

Browse files
committed
fix: _
1 parent 62c8ff7 commit e9cef4d

File tree

7 files changed

+90
-35
lines changed

7 files changed

+90
-35
lines changed

v_exchanges/src/main.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::env;
2+
13
use v_exchanges_adapters::binance::{BinanceHttpUrl, BinanceOption};
24
use v_exchanges_deser::{binance::Client, core::Exchange};
35

@@ -7,8 +9,16 @@ async fn main() {
79
v_utils::utils::init_subscriber(v_utils::utils::LogDestination::xdg("v_exchanges"));
810

911
let mut client = Client::new();
10-
client.update_default_option(BinanceOption::HttpUrl(BinanceHttpUrl::FuturesUsdM));
11-
12+
//client.update_default_option(BinanceOption::HttpUrl(BinanceHttpUrl::FuturesUsdM));
1213
let klines = client.klines(("BTC", "USDT").into(), "1m".into(), None, None, None).await.unwrap();
14+
//let price = client.price(("BTC", "USDT").into()).await.unwrap();
1315
dbg!(&klines);
16+
17+
//let key = env::var("BINANCE_TIGER_READ_KEY").unwrap();
18+
//let secret = env::var("BINANCE_TIGER_READ_SECRET").unwrap();
19+
//client.update_default_option(BinanceOption::Key(key));
20+
//client.update_default_option(BinanceOption::Secret(secret));
21+
//let balance = client.futures_balance("USDT").await.unwrap();
22+
//
23+
//dbg!(&balance);
1424
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use color_eyre::eyre::Result;
2+
use serde::Deserialize;
3+
use v_exchanges_adapters::binance::{BinanceHttpUrl, BinanceOption};
4+
use v_utils::trades::{Kline, Ohlc, Pair, Timeframe};
5+
6+
//TODO: make a Coin type
7+
pub async fn futures_balance(client: &v_exchanges_adapters::Client, asset: &str) -> Result<f64> {
8+
#[derive(serde::Serialize)]
9+
pub struct BalanceParams {
10+
pub asset: String,
11+
}
12+
let mut params = BalanceParams { asset: asset.to_string() };
13+
14+
let r: AccountResponse = client.get("/fapi/v2/balance", Some(&params), [BinanceOption::Default]).await.unwrap();
15+
let balance = r.balance;
16+
Ok(balance)
17+
}
18+
19+
#[derive(Clone, Debug, Default, derive_new::new, Deserialize)]
20+
struct AccountResponse {
21+
pub balance: f64,
22+
}

v_exchanges_deser/src/binance/futures/market.rs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ use color_eyre::eyre::Result;
44
use serde::{Deserialize, Serialize};
55
use serde_json::Value;
66
use serde_with::{DisplayFromStr, serde_as};
7-
use v_exchanges_adapters::binance::BinanceOption;
7+
use v_exchanges_adapters::binance::{BinanceHttpUrl, BinanceOption};
88
use v_utils::trades::{Kline, Ohlc, Pair, Timeframe};
99

10-
use crate::{binance::futures::core::*, core::Klines};
10+
use crate::core::Klines;
1111

12+
// klines {{{
1213
pub async fn klines(client: &v_exchanges_adapters::Client, pair: Pair, tf: Timeframe, limit: Option<u32>, start_time: Option<u64>, end_time: Option<u64>) -> Result<Klines> {
1314
#[derive(Serialize)]
1415
pub struct KlineParams {
@@ -30,7 +31,7 @@ pub async fn klines(client: &v_exchanges_adapters::Client, pair: Pair, tf: Timef
3031
end_time,
3132
};
3233

33-
let kline_responses: Vec<KlineResponse> = client.get("/fapi/v1/klines", Some(&params), [BinanceOption::Default]).await.unwrap();
34+
let kline_responses: Vec<KlineResponse> = client.get("/fapi/v1/klines", Some(&params), [BinanceOption::HttpUrl(BinanceHttpUrl::FuturesUsdM)]).await.unwrap();
3435
let klines: Vec<Kline> = kline_responses.into_iter().map(Kline::from).collect();
3536

3637
Ok(Klines { v: klines, tf, oi: Vec::new() })
@@ -84,11 +85,45 @@ impl From<KlineResponse> for Kline {
8485
}
8586
}
8687
}
88+
//,}}}
89+
90+
// price {{{
91+
//HACK: not sure this is _the_ thing to use for that (throwing away A LOT of data)
92+
pub async fn price(client: &v_exchanges_adapters::Client, pair: Pair) -> Result<f64> {
93+
#[derive(Serialize)]
94+
pub struct PriceParams {
95+
pub symbol: String,
96+
}
97+
let mut params = PriceParams { symbol: pair.to_string() };
98+
99+
let r: MarkPriceResponse = client.get("/fapi/v1/premiumIndex", Some(&params), [BinanceOption::Default]).await.unwrap();
100+
let price = r.index_price; // when using this framework, we care for per-exchange price, obviously
101+
Ok(price)
102+
}
103+
104+
#[serde_as]
105+
#[derive(Debug, Serialize, Deserialize, Clone)]
106+
#[serde(rename_all = "camelCase")]
107+
pub struct MarkPriceResponse {
108+
pub symbol: String,
109+
#[serde_as(as = "DisplayFromStr")]
110+
pub mark_price: f64,
111+
#[serde_as(as = "DisplayFromStr")]
112+
pub index_price: f64,
113+
#[serde_as(as = "DisplayFromStr")]
114+
pub estimated_settle_price: f64,
115+
#[serde_as(as = "DisplayFromStr")]
116+
pub last_funding_rate: f64,
117+
pub next_funding_time: u64,
118+
pub time: u64,
119+
}
120+
121+
//,}}}
87122

88123
#[cfg(test)]
89124
mod tests {
90125
#[test]
91-
fn kline_core() {
126+
fn klines() {
92127
let raw_str = "[1731448080000,\"88591.90\",\"88630.90\",\"88560.00\",\"88574.10\",\"173.581\",1731448139999,\"15378315.48720\",2800,\"113.654\",\"10069629.84420\",\"0\"]";
93128
let _: super::Kline = serde_json::from_str(raw_str).unwrap();
94129
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod core;
22
pub use core::*;
33

4+
pub mod account;
45
pub mod general;
56
pub mod market;
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
pub mod futures;
22
use color_eyre::eyre::Result;
3-
use futures::market::klines;
3+
use futures::market::{klines, price};
44
pub use v_exchanges_adapters::Client; // re-export
55
use v_exchanges_adapters::binance;
66
use v_utils::trades::{Pair, Timeframe};
77

88
use crate::core::{Exchange, Klines};
99

10+
//? currently client ends up importing this from crate::binance, but could it be possible to lift the [Client] reexport up, and still have the ability to call all exchange methods right on it?
1011
impl Exchange<binance::BinanceOptions> for Client {
1112
async fn klines(&self, symbol: Pair, tf: Timeframe, limit: Option<u32>, start_time: Option<u64>, end_time: Option<u64>) -> Result<Klines> {
1213
klines(&self, symbol, tf, limit, start_time, end_time).await
1314
}
15+
16+
async fn price(&self, symbol: Pair) -> Result<f64> {
17+
price(&self, symbol).await
18+
}
19+
20+
//DO: async fn balance(&self,
1421
}

v_exchanges_deser/src/core.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ pub struct Klines {
2222
pub trait Exchange<O: HandlerOptions> {
2323
//? should I have Self::Pair too? Like to catch the non-existent ones immediately? Although this would increase the error surface on new listings.
2424
fn klines(&self, symbol: Pair, tf: Timeframe, limit: Option<u32>, start_time: Option<u64>, end_time: Option<u64>) -> impl std::future::Future<Output = Result<Klines>> + Send;
25+
fn price(&self, symbol: Pair) -> impl std::future::Future<Output = Result<f64>> + Send;
26+
2527
// Defined in terms of actors
2628
//TODO!!!: fn spawn_klines_listener(&self, symbol: Pair, tf: Timeframe) -> mpsc::Receiver<Kline>;
29+
30+
//DO: balances
31+
// balances are defined for each margin type: [futures_balance, spot_balance, margin_balance], but note that on some exchanges, (like bybit), some of these may point to the same exact call
32+
// to negate confusion could add a `total_balance` endpoint
33+
34+
//? could implement many things that are _explicitly_ combinatorial. I can imagine several cases, where knowing that say the specified limit for the klines is wayyy over the max and that you may be opting into a long wait by calling it, could be useful.
2735
}

v_exchanges_deser/src/main.rs

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)