Skip to content

Commit

Permalink
Add get_leverage_brackets api for future
Browse files Browse the repository at this point in the history
  • Loading branch information
liumiao committed Apr 4, 2024
1 parent 6a1eb43 commit 0c71fce
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ pub enum Futures {
OpenOrders,
UserDataStream,
Income,
LeverageBracket,
}

impl From<API> for String {
Expand Down Expand Up @@ -169,6 +170,7 @@ impl From<API> for String {
Futures::OpenOrders => "/fapi/v1/openOrders",
Futures::UserDataStream => "/fapi/v1/listenKey",
Futures::Income => "/fapi/v1/income",
Futures::LeverageBracket => "/fapi/v1/leverageBracket",
},
})
}
Expand Down
15 changes: 15 additions & 0 deletions src/futures/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -689,4 +689,19 @@ impl FuturesAccount {
self.client
.get_signed(API::Futures(Futures::Income), Some(request))
}

pub fn leverage_brackets<S>(
&self, symbol: S,
) -> Result<Vec<crate::futures::model::LeverageBrackets>>
where
S: Into<Option<String>>,
{
let mut parameters: BTreeMap<String, String> = BTreeMap::new();
if let Some(symbol) = symbol.into() {
parameters.insert("symbol".into(), symbol.into());
}
let request = build_signed_request(parameters, self.recv_window)?;
self.client
.get_signed(API::Futures(Futures::LeverageBracket), Some(request))
}
}
23 changes: 23 additions & 0 deletions src/futures/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -625,3 +625,26 @@ pub struct Income {
pub tran_id: u64,
pub trade_id: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct LeverageBrackets {
pub symbol: String,
pub notional_coef: Option<f64>,
pub brackets: Vec<Bracket>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Bracket {
pub bracket: i32,
pub initial_leverage: u8,
#[serde(with = "string_or_float")]
pub notional_cap: f64,
#[serde(with = "string_or_float")]
pub notional_floor: f64,
#[serde(with = "string_or_float")]
pub maint_margin_ratio: f64,
#[serde(with = "string_or_float")]
pub cum: f64,
}
4 changes: 2 additions & 2 deletions src/futures/websockets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,15 @@ impl<'a> FuturesWebSockets<'a> {
pub fn event_loop(&mut self, running: &AtomicBool) -> Result<()> {
while running.load(Ordering::Relaxed) {
if let Some(ref mut socket) = self.socket {
let message = socket.0.read_message()?;
let message = socket.0.read()?;
match message {
Message::Text(msg) => {
if let Err(e) = self.handle_msg(&msg) {
bail!(format!("Error on handling stream message: {}", e));
}
}
Message::Ping(payload) => {
socket.0.write_message(Message::Pong(payload)).unwrap();
socket.0.write(Message::Pong(payload)).unwrap();
}
Message::Pong(_) | Message::Binary(_) | Message::Frame(_) => (),
Message::Close(e) => bail!(format!("Disconnected {:?}", e)),
Expand Down
4 changes: 2 additions & 2 deletions src/websockets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,15 @@ impl<'a> WebSockets<'a> {
pub fn event_loop(&mut self, running: &AtomicBool) -> Result<()> {
while running.load(Ordering::Relaxed) {
if let Some(ref mut socket) = self.socket {
let message = socket.0.read_message()?;
let message = socket.0.read()?;
match message {
Message::Text(msg) => {
if let Err(e) = self.handle_msg(&msg) {
bail!(format!("Error on handling stream message: {}", e));
}
}
Message::Ping(payload) => {
socket.0.write_message(Message::Pong(payload)).unwrap();
socket.0.write(Message::Pong(payload)).unwrap();
}
Message::Pong(_) | Message::Binary(_) | Message::Frame(_) => (),
Message::Close(e) => bail!(format!("Disconnected {:?}", e)),
Expand Down
24 changes: 23 additions & 1 deletion tests/futures_account_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod tests {
use super::*;
use mockito::{Server, Matcher};
use float_cmp::*;
use binance::account::OrderSide;
use binance::account::{self, OrderSide};
use binance::futures::model::Transaction;

#[test]
Expand Down Expand Up @@ -254,4 +254,26 @@ mod tests {

mock.assert();
}

#[test]
fn get_leverage_brackets() {
let mut server = Server::new();
let mock = server
.mock("GET", "/fapi/v1/leverageBracket")
.with_header("content-type", "application/json;charset=UTF-8")
.match_query(Matcher::Regex(
"recvWindow=1234&timestamp=\\d+&signature=.*".into(),
))
.with_body_from_file("tests/mocks/futures/account/get_leverage_brackets.json")
.create();

let config = Config::default()
.set_futures_rest_api_endpoint(server.url())
.set_recv_window(1234);
let account: FuturesAccount = Binance::new_with_config(None, None, &config);
let _ = env_logger::try_init();
account.leverage_brackets(None).unwrap();

mock.assert();
}
}
24 changes: 24 additions & 0 deletions tests/mocks/futures/account/get_leverage_brackets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[
{
"symbol": "ETHUSDT",
"notionalCoef": 1.50,
"brackets": [
{
"bracket": 1,
"initialLeverage": 75,
"notionalCap": 10000,
"notionalFloor": 0,
"maintMarginRatio": 0.0065,
"cum": 0
},
{
"bracket": 2,
"initialLeverage": 50,
"notionalCap": 20000,
"notionalFloor": 10000,
"maintMarginRatio": 0.005,
"cum": 10
}
]
}
]

0 comments on commit 0c71fce

Please sign in to comment.