Skip to content

Commit

Permalink
Merge pull request #1525 from c9s/edwin/binance-update-api-changes-3
Browse files Browse the repository at this point in the history
FEATURE: [binance] add margin request
  • Loading branch information
bailantaotao authored Feb 6, 2024
2 parents 7cc3d1b + 836f1f9 commit 1c98e60
Show file tree
Hide file tree
Showing 6 changed files with 311 additions and 42 deletions.
49 changes: 48 additions & 1 deletion pkg/exchange/binance/binanceapi/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/stretchr/testify/assert"

"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/testutil"
)

Expand Down Expand Up @@ -168,7 +169,6 @@ func TestClient_NewTransferAssetRequest(t *testing.T) {
req.FromSymbol("BTCUSDT")
req.ToSymbol("BTCUSDT")
req.Amount("0.01")
req.Timestamp(time.Now())
req.TransferType(TransferAssetTypeIsolatedMarginToMain)
res, err := req.Do(ctx)
assert.NoError(t, err)
Expand Down Expand Up @@ -197,3 +197,50 @@ func TestClient_GetMarginBorrowRepayHistoryRequest(t *testing.T) {
assert.NotEmpty(t, res)
t.Logf("result: %+v", res)
}

func TestClient_NewPlaceMarginOrderRequest(t *testing.T) {
client := getTestClientOrSkip(t)
ctx := context.Background()

err := client.SetTimeOffsetFromServer(ctx)
assert.NoError(t, err)

res, err := client.NewPlaceMarginOrderRequest().
Asset("USDT").
Amount(fixedpoint.NewFromFloat(5)).
IsIsolated(true).
Symbol("BNBUSDT").
SetBorrowRepayType(BorrowRepayTypeBorrow).
Do(ctx)
assert.NoError(t, err)
assert.NotNil(t, res)
assert.NotEmpty(t, res)
t.Logf("result: %+v", res)

<-time.After(time.Second)
end := time.Now()
start := end.Add(-24 * time.Hour * 30)
histories, err := client.NewGetMarginBorrowRepayHistoryRequest().
StartTime(start).
EndTime(end).
Asset("BNB").
IsolatedSymbol("BNBUSDT").
SetBorrowRepayType(BorrowRepayTypeBorrow).
Do(ctx)
assert.NoError(t, err)
assert.NotNil(t, histories)
assert.NotEmpty(t, histories)
t.Logf("result: %+v", histories)

res, err = client.NewPlaceMarginOrderRequest().
Asset("USDT").
Amount(fixedpoint.NewFromFloat(5)).
IsIsolated(true).
Symbol("BNBUSDT").
SetBorrowRepayType(BorrowRepayTypeRepay).
Do(ctx)
assert.NoError(t, err)
assert.NotNil(t, res)
assert.NotEmpty(t, res)
t.Logf("result: %+v", res)
}
28 changes: 28 additions & 0 deletions pkg/exchange/binance/binanceapi/place_margin_order_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package binanceapi

import (
"github.com/c9s/requestgen"

"github.com/c9s/bbgo/pkg/fixedpoint"
)

//go:generate requestgen -method POST -url "/sapi/v1/margin/borrow-repay" -type PlaceMarginOrderRequest -responseType .TransferResponse
type PlaceMarginOrderRequest struct {
client requestgen.AuthenticatedAPIClient

asset string `param:"asset"`

// TRUE for Isolated Margin, FALSE for Cross Margin, Default FALSE
isIsolated bool `param:"isIsolated"`

// Only for Isolated margin
symbol *string `param:"symbol"`

amount fixedpoint.Value `param:"amount"`

BorrowRepayType BorrowRepayType `param:"type"`
}

func (c *RestClient) NewPlaceMarginOrderRequest() *PlaceMarginOrderRequest {
return &PlaceMarginOrderRequest{client: c}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions pkg/exchange/binance/binanceapi/transfer_asset_request.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package binanceapi

import (
"time"

"github.com/c9s/requestgen"
)

Expand All @@ -29,8 +27,6 @@ type TransferAssetRequest struct {

amount string `param:"amount"`

timestamp time.Time `param:"timestamp,milliseconds,query"`

fromSymbol *string `param:"fromSymbol"`
toSymbol *string `param:"toSymbol"`
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1c98e60

Please sign in to comment.