Skip to content

Commit

Permalink
pkg/exchange: add margin transfer request
Browse files Browse the repository at this point in the history
  • Loading branch information
bailantaotao committed Feb 5, 2024
1 parent f77d03d commit c73fc65
Show file tree
Hide file tree
Showing 4 changed files with 305 additions and 19 deletions.
47 changes: 47 additions & 0 deletions pkg/exchange/binance/binanceapi/client_test.go
Original file line number Diff line number Diff line change
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("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("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)
}
26 changes: 26 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,26 @@
package binanceapi

import (
"github.com/c9s/requestgen"
)

//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 string `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.

32 changes: 13 additions & 19 deletions pkg/exchange/binance/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,43 +328,37 @@ func (e *Exchange) QueryMarginAssetMaxBorrowable(ctx context.Context, asset stri
return resp.Amount, nil
}

func (e *Exchange) RepayMarginAsset(ctx context.Context, asset string, amount fixedpoint.Value) error {
req := e.client.NewMarginRepayService()
func (e *Exchange) borrowRepayAsset(ctx context.Context, asset string, amount fixedpoint.Value, marginType binanceapi.BorrowRepayType) error {
req := e.client2.NewPlaceMarginOrderRequest()
req.Asset(asset)
req.Amount(amount.String())
req.SetBorrowRepayType(marginType)
if e.IsIsolatedMargin {
req.IsIsolated(e.IsIsolatedMargin)
req.Symbol(e.IsolatedMarginSymbol)
}

log.Infof("repaying margin asset %s amount %f", asset, amount.Float64())
log.Infof("%s margin asset %s amount %f", marginType, asset, amount.Float64())
resp, err := req.Do(ctx)
if err != nil {
return err
}

log.Debugf("margin repayed %f %s, transaction id = %d", amount.Float64(), asset, resp.TranID)
log.Debugf("margin %s %f %s, transaction id = %d", marginType, amount.Float64(), asset, resp.TranId)
return err
}

func (e *Exchange) BorrowMarginAsset(ctx context.Context, asset string, amount fixedpoint.Value) error {
req := e.client.NewMarginLoanService()
req.Asset(asset)
req.Amount(amount.String())
if e.IsIsolatedMargin {
req.Symbol(e.IsolatedMarginSymbol)
}
func (e *Exchange) RepayMarginAsset(ctx context.Context, asset string, amount fixedpoint.Value) error {
return e.borrowRepayAsset(ctx, asset, amount, binanceapi.BorrowRepayTypeRepay)
}

log.Infof("borrowing margin asset %s amount %f", asset, amount.Float64())
resp, err := req.Do(ctx)
if err != nil {
return err
}
log.Debugf("margin borrowed %f %s, transaction id = %d", amount.Float64(), asset, resp.TranID)
return err
func (e *Exchange) BorrowMarginAsset(ctx context.Context, asset string, amount fixedpoint.Value) error {
return e.borrowRepayAsset(ctx, asset, amount, binanceapi.BorrowRepayTypeBorrow)
}

func (e *Exchange) QueryMarginBorrowHistory(ctx context.Context, asset string) error {
req := e.client.NewListMarginLoansService()
req := e.client2.NewGetMarginBorrowRepayHistoryRequest()
req.SetBorrowRepayType(binanceapi.BorrowRepayTypeBorrow)
req.Asset(asset)
history, err := req.Do(ctx)
if err != nil {
Expand Down

0 comments on commit c73fc65

Please sign in to comment.