Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: [binance] add margin request #1525

Merged
merged 3 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion pkg/exchange/binance/binanceapi/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,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 +196,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"`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use fixedpoint value?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK.

The previous type is because I simply copied the value from another file.
The caller uses req.Price(order.Market.FormatPrice(order.Price))


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
Loading