Skip to content

Commit

Permalink
Add change_margin_type api of future
Browse files Browse the repository at this point in the history
  • Loading branch information
liumiao committed Feb 26, 2024
1 parent 909e14b commit ebd6bb4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ pub enum Futures {
LvtKlines,
IndexInfo,
ChangeInitialLeverage,
ChangeMarginType,
Account,
OpenOrders,
UserDataStream,
Expand Down Expand Up @@ -161,6 +162,7 @@ impl From<API> for String {
Futures::LvtKlines => "/fapi/v1/lvtKlines",
Futures::IndexInfo => "/fapi/v1/indexInfo",
Futures::ChangeInitialLeverage => "/fapi/v1/leverage",
Futures::ChangeMarginType => "/fapi/v1/marginType",
Futures::Account => "/fapi/v2/account",
Futures::OpenOrders => "/fapi/v1/openOrders",
Futures::UserDataStream => "/fapi/v1/listenKey",
Expand Down
19 changes: 18 additions & 1 deletion src/futures/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,9 @@ impl FuturesAccount {
}

// Custom order for for professional traders
pub fn custom_batch_orders(&self, _order_count: u64, order_requests: Vec<CustomOrderRequest>) -> Result<Transaction> {
pub fn custom_batch_orders(
&self, _order_count: u64, order_requests: Vec<CustomOrderRequest>,
) -> Result<Transaction> {
let request = String::from("");
for order_request in order_requests {
let order = OrderRequest {
Expand Down Expand Up @@ -595,6 +597,21 @@ impl FuturesAccount {
.post_signed(API::Futures(Futures::ChangeInitialLeverage), request)
}

pub fn change_margin_type<S>(&self, symbol: S, isolated: bool) -> Result<()>
where
S: Into<String>,
{
let mut parameters: BTreeMap<String, String> = BTreeMap::new();
let margin_type = if isolated { "ISOLATED" } else { "CROSSED" };
parameters.insert("symbol".into(), symbol.into());
parameters.insert("marginType".into(), margin_type.into());

let request = build_signed_request(parameters, self.recv_window)?;
self.client
.post_signed::<Empty>(API::Futures(Futures::ChangeMarginType), request)
.map(|_| ())
}

pub fn change_position_mode(&self, dual_side_position: bool) -> Result<()> {
let mut parameters: BTreeMap<String, String> = BTreeMap::new();
let dual_side = if dual_side_position { "true" } else { "false" };
Expand Down
21 changes: 21 additions & 0 deletions tests/futures_account_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,27 @@ mod tests {
));
}

#[test]
fn change_margin_type() {
let mut server = Server::new();
let mock = server.mock("POST", "/fapi/v1/marginType")
.with_header("content-type", "application/json;charset=UTF-8")
.match_query(Matcher::Regex(
"marginType=ISOLATED&recvWindow=1234&symbol=BTCUSDT&timestamp=\\d+&signature=.*".into(),
))
.with_body_from_file("tests/mocks/futures/account/change_margin_type.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.change_margin_type("BTCUSDT", true).unwrap();

mock.assert();
}

#[test]
fn cancel_all_open_orders() {
let mut server = Server::new();
Expand Down
4 changes: 4 additions & 0 deletions tests/mocks/futures/account/change_margin_type.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"code": 200,
"msg": "success"
}

0 comments on commit ebd6bb4

Please sign in to comment.