From a9f964dd6a799d4f52134c930c363d9ef87b8bd1 Mon Sep 17 00:00:00 2001 From: taobig Date: Thu, 28 May 2026 17:23:09 +0800 Subject: [PATCH 1/2] fix: set symbol param only when isIsolated is true --- v2/margin_service.go | 9 ++++++--- v2/margin_service_test.go | 8 ++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/v2/margin_service.go b/v2/margin_service.go index 4c67e3e7..4e4f324d 100644 --- a/v2/margin_service.go +++ b/v2/margin_service.go @@ -242,9 +242,12 @@ func (s *MarginBorrowRepayService) Do(ctx context.Context, opts ...RequestOption m := params{ "asset": s.asset, "isIsolated": s.isIsolated, - "symbol": s.symbol, - "amount": s.amount, - "type": string(s._type), + //"symbol": s.symbol, + "amount": s.amount, + "type": string(s._type), + } + if s.isIsolated { + m["symbol"] = s.symbol // set symbol param only when isIsolated is true } r.setFormParams(m) res = new(TransactionResponse) diff --git a/v2/margin_service_test.go b/v2/margin_service_test.go index c1de34ab..a5caaa16 100644 --- a/v2/margin_service_test.go +++ b/v2/margin_service_test.go @@ -106,8 +106,8 @@ func (s *marginTestSuite) TestBorrowRepayBorrow() { "asset": asset, "amount": amount, "isIsolated": false, - "symbol": "", - "type": string(_type), + //"symbol": "", + "type": string(_type), }) s.assertRequestEqual(e, r) }) @@ -137,8 +137,8 @@ func (s *marginTestSuite) TestBorrowRepayRepay() { "asset": asset, "amount": amount, "isIsolated": false, - "symbol": "", - "type": string(_type), + //"symbol": "", + "type": string(_type), }) s.assertRequestEqual(e, r) }) From 52e7179ae37626eea4414a6f94fb88002d711834 Mon Sep 17 00:00:00 2001 From: taobig Date: Sun, 7 Jun 2026 16:50:18 +0800 Subject: [PATCH 2/2] fix: guard against sending empty symbol in isolated margin --- v2/margin_service.go | 18 +++++-------- v2/margin_service_test.go | 56 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 59 insertions(+), 15 deletions(-) diff --git a/v2/margin_service.go b/v2/margin_service.go index 4e4f324d..09ef9516 100644 --- a/v2/margin_service.go +++ b/v2/margin_service.go @@ -3,6 +3,7 @@ package binance import ( "context" "encoding/json" + "fmt" "net/http" "strings" ) @@ -242,12 +243,14 @@ func (s *MarginBorrowRepayService) Do(ctx context.Context, opts ...RequestOption m := params{ "asset": s.asset, "isIsolated": s.isIsolated, - //"symbol": s.symbol, - "amount": s.amount, - "type": string(s._type), + "amount": s.amount, + "type": string(s._type), } if s.isIsolated { - m["symbol"] = s.symbol // set symbol param only when isIsolated is true + if s.symbol == "" { + return nil, fmt.Errorf("symbol is required for isolated margin borrow/repay") + } + m["symbol"] = s.symbol // Only set symbol param when isIsolated is true } r.setFormParams(m) res = new(TransactionResponse) @@ -330,13 +333,6 @@ func (s *ListMarginBorrowRepayService) Do(ctx context.Context, opts ...RequestOp secType: secTypeSigned, } m := params{ - //"asset": s.asset, - //"isolatedSymbol": s.isolatedSymbol, - //"txId": s.txId, - //"startTime": s.startTime, - //"endTime": s.endTime, - //"current": s.current, - //"size": s.size, "type": string(s._type), } r.setParams(m) diff --git a/v2/margin_service_test.go b/v2/margin_service_test.go index a5caaa16..db133e0e 100644 --- a/v2/margin_service_test.go +++ b/v2/margin_service_test.go @@ -106,8 +106,7 @@ func (s *marginTestSuite) TestBorrowRepayBorrow() { "asset": asset, "amount": amount, "isIsolated": false, - //"symbol": "", - "type": string(_type), + "type": string(_type), }) s.assertRequestEqual(e, r) }) @@ -137,8 +136,7 @@ func (s *marginTestSuite) TestBorrowRepayRepay() { "asset": asset, "amount": amount, "isIsolated": false, - //"symbol": "", - "type": string(_type), + "type": string(_type), }) s.assertRequestEqual(e, r) }) @@ -154,6 +152,56 @@ func (s *marginTestSuite) TestBorrowRepayRepay() { s.assertTransactionResponseEqual(e, res) } +func (s *marginTestSuite) TestBorrowRepayBorrowIsolated() { + data := []byte(`{ + "tranId": 100000001 + }`) + s.mockDo(data, nil) + defer s.assertDo() + asset := "BTC" + amount := "1.000" + symbol := "BTCUSDT" + _type := MarginAccountBorrow + s.assertReq(func(r *request) { + e := newSignedRequest().setFormParams(params{ + "asset": asset, + "amount": amount, + "isIsolated": true, + "symbol": symbol, + "type": string(_type), + }) + s.assertRequestEqual(e, r) + }) + res, err := s.client.NewMarginBorrowRepayService(). + Asset(asset). + Amount(amount). + IsIsolated(true). + Symbol(symbol). + Type(_type). + Do(newContext()) + s.r().NoError(err) + e := &TransactionResponse{ + TranID: 100000001, + } + s.assertTransactionResponseEqual(e, res) +} + +func (s *marginTestSuite) TestBorrowRepayBorrowIsolatedWithoutSymbol() { + asset := "BTC" + amount := "1.000" + _type := MarginAccountBorrow + res, err := s.client.NewMarginBorrowRepayService(). + Asset(asset). + Amount(amount). + IsIsolated(true). + Type(_type). + Do(newContext()) + s.r().Error(err) + s.r().EqualError(err, "symbol is required for isolated margin borrow/repay") + s.r().Nil(res) + s.client.AssertNotCalled(s.T(), "do", anyHTTPRequest()) +} + func (s *marginTestSuite) TestListBorrowRepay() { data := []byte(`{ "rows": [