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

fix some issues #642

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions v2/account_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package binance

import (
"context"
"encoding/json"
"net/http"
)

Expand Down
1 change: 1 addition & 0 deletions v2/asset_detail_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package binance

import (
"context"
"encoding/json"
"net/http"
)

Expand Down
1 change: 1 addition & 0 deletions v2/asset_dividend_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package binance

import (
"context"
"encoding/json"
"net/http"
)

Expand Down
1 change: 1 addition & 0 deletions v2/bnb_burn_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package binance

import (
"context"
"encoding/json"
"net/http"
)

Expand Down
1 change: 1 addition & 0 deletions v2/c2c_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package binance

import (
"context"
"encoding/json"
"net/http"
)

Expand Down
9 changes: 5 additions & 4 deletions v2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import (
"net/http"
"net/url"
"os"
"strings"
"time"

"encoding/json"

"github.com/bitly/go-simplejson"
jsoniter "github.com/json-iterator/go"

"github.com/adshao/go-binance/v2/common"
"github.com/adshao/go-binance/v2/delivery"
Expand Down Expand Up @@ -133,9 +135,6 @@ type SelfTradePreventionMode string
// UseTestnet switch all the API endpoints from production to the testnet
var UseTestnet = false

// Redefining the standard package
var json = jsoniter.ConfigCompatibleWithStandardLibrary

// Global enums
const (
SideTypeBuy SideType = "BUY"
Expand Down Expand Up @@ -443,8 +442,10 @@ func (c *Client) parseRequest(r *request, opts ...RequestOption) (err error) {
r.setParam(timestampKey, currentTimestamp()-c.TimeOffset)
}
queryString := r.query.Encode()
queryString = strings.ReplaceAll(queryString, "%40", "@")
body := &bytes.Buffer{}
bodyString := r.form.Encode()
bodyString = strings.ReplaceAll(bodyString, "%40", "@")
header := http.Header{}
if r.header != nil {
header = r.header.Clone()
Expand Down
13 changes: 13 additions & 0 deletions v2/common/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ func AmountToLotSize(lot float64, precision int, amount float64) float64 {
return math.Trunc(math.Floor(amount/lot)*lot*math.Pow10(precision)) / math.Pow10(precision)
}

func RoundPriceToTickSize(price, tickSize float64) float64 {
if tickSize == 0 {
return price // To avoid division by zero
}
// Calculate the factor by which to multiply and divide the price to conform to the tick size.
factor := 1 / tickSize

// Round the price to the nearest tick size.
roundedPrice := math.Round(price*factor) / factor

return roundedPrice
}

// ToJSONList convert v to json list if v is a map
func ToJSONList(v []byte) []byte {
if len(v) > 0 && v[0] == '{' {
Expand Down
1 change: 1 addition & 0 deletions v2/convert_trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package binance

import (
"context"
"encoding/json"
"net/http"
)

Expand Down
2 changes: 1 addition & 1 deletion v2/delivery/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var wsServe = func(cfg *WsConfig, handler WsHandler, errHandler ErrHandler) (don
Dialer := websocket.Dialer{
Proxy: proxy,
HandshakeTimeout: 45 * time.Second,
EnableCompression: false,
EnableCompression: true,
}

c, _, err := Dialer.Dial(cfg.Endpoint, nil)
Expand Down
1 change: 1 addition & 0 deletions v2/deposit_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package binance

import (
"context"
"encoding/json"
"net/http"
)

Expand Down
1 change: 1 addition & 0 deletions v2/dust_log_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package binance

import (
"context"
"encoding/json"
"net/http"
)

Expand Down
1 change: 1 addition & 0 deletions v2/exchange_info_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package binance

import (
"context"
"encoding/json"
"net/http"

"github.com/adshao/go-binance/v2/common"
Expand Down
1 change: 1 addition & 0 deletions v2/fiat_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package binance

import (
"context"
"encoding/json"
"net/http"
)

Expand Down
5 changes: 4 additions & 1 deletion v2/futures/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,10 @@ func (c *Client) NewGetBalanceService() *GetBalanceService {
return &GetBalanceService{c: c}
}

// NewGetPositionRiskService init getting position risk service
func (c *Client) NewGetPositionRiskV2Service() *GetPositionRiskV2Service {
return &GetPositionRiskV2Service{c: c}
}

func (c *Client) NewGetPositionRiskService() *GetPositionRiskService {
return &GetPositionRiskService{c: c}
}
Expand Down
1 change: 1 addition & 0 deletions v2/futures/order_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,7 @@ func (s *CancelOrderService) Do(ctx context.Context, opts ...RequestOption) (res
if s.origClientOrderID != nil {
r.setFormParam("origClientOrderId", *s.origClientOrderID)
}

data, _, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
Expand Down
81 changes: 74 additions & 7 deletions v2/futures/position_risk.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import (
)

// GetPositionRiskService get account balance
type GetPositionRiskService struct {
type GetPositionRiskV2Service struct {
c *Client
symbol string
}

// Symbol set symbol
func (s *GetPositionRiskService) Symbol(symbol string) *GetPositionRiskService {
func (s *GetPositionRiskV2Service) Symbol(symbol string) *GetPositionRiskV2Service {
s.symbol = symbol
return s
}

// Do send request
func (s *GetPositionRiskService) Do(ctx context.Context, opts ...RequestOption) (res []*PositionRisk, err error) {
func (s *GetPositionRiskV2Service) Do(ctx context.Context, opts ...RequestOption) (res []*PositionRiskV2, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/fapi/v2/positionRisk",
Expand All @@ -30,18 +30,18 @@ func (s *GetPositionRiskService) Do(ctx context.Context, opts ...RequestOption)
}
data, _, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return []*PositionRisk{}, err
return []*PositionRiskV2{}, err
}
res = make([]*PositionRisk, 0)
res = make([]*PositionRiskV2, 0)
err = json.Unmarshal(data, &res)
if err != nil {
return []*PositionRisk{}, err
return []*PositionRiskV2{}, err
}
return res, nil
}

// PositionRisk define position risk info
type PositionRisk struct {
type PositionRiskV2 struct {
EntryPrice string `json:"entryPrice"`
BreakEvenPrice string `json:"breakEvenPrice"`
MarginType string `json:"marginType"`
Expand All @@ -58,3 +58,70 @@ type PositionRisk struct {
Notional string `json:"notional"`
IsolatedWallet string `json:"isolatedWallet"`
}

type GetPositionRiskService struct {
c *Client
symbol string
recvWindow *int64
}

// Symbol set symbol
func (s *GetPositionRiskService) Symbol(symbol string) *GetPositionRiskService {
s.symbol = symbol
return s
}

func (s *GetPositionRiskService) RecvWindow(rw int64) *GetPositionRiskService {
s.recvWindow = &rw
return s
}

// Do send request
func (s *GetPositionRiskService) Do(ctx context.Context, opts ...RequestOption) (res []*PositionRisk, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/fapi/v3/positionRisk",
secType: secTypeSigned,
}
if s.symbol != "" {
r.setParam("symbol", s.symbol)
}
if s.recvWindow != nil {
r.recvWindow = *s.recvWindow
}

data, _, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return []*PositionRisk{}, err
}
res = make([]*PositionRisk, 0)
err = json.Unmarshal(data, &res)
if err != nil {
return []*PositionRisk{}, err
}
return res, nil
}

// PositionRisk define position risk info
type PositionRisk struct {
Symbol string `json:"symbol"`
PositionSide string `json:"positionSide"`
PositionAmt string `json:"positionAmt"`
EntryPrice string `json:"entryPrice"`
BreakEvenPrice string `json:"breakEvenPrice"`
MarkPrice string `json:"markPrice"`
UnRealizedProfit string `json:"unRealizedProfit"`
LiquidationPrice string `json:"liquidationPrice"`
IsolatedMargin string `json:"isolatedMargin"`
Notional string `json:"notional"`
MarginAsset string `json:"marginAsset"`
IsolatedWallet string `json:"isolatedWallet"`
InitialMargin string `json:"initialMargin"`
MaintMargin string `json:"maintMargin"`
PositionInitialMargin string `json:"positionInitialMargin"`
OpenOrderInitialMargin string `json:"openOrderInitialMargin"`
Adl int64 `json:"adl"`
BidNotional string `json:"bidNotional"`
AskNotional string `json:"askNotional"`
UpdateTime int64 `json:"updateTime"`
}
Loading
Loading