Skip to content

Commit fce4cf2

Browse files
vigodskyArtur Abelian
and
Artur Abelian
authored
feat: export urls and added self trade prev mode into spot/futures client (#632)
* feat: export urls and added self trade prevention mode for futures * feat: added self trade prevention mode to spot client * feat: added self trade prevention mode to futures ws client --------- Co-authored-by: Artur Abelian <[email protected]>
1 parent eb541b9 commit fce4cf2

9 files changed

+154
-102
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pkg/
22
.idea
33
*.iml
4+
coverage.txt

v2/client.go

+8
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ var (
127127
BaseAPITestnetURL = "https://testnet.binance.vision"
128128
)
129129

130+
// SelfTradePreventionMode define self trade prevention strategy
131+
type SelfTradePreventionMode string
132+
130133
// UseTestnet switch all the API endpoints from production to the testnet
131134
var UseTestnet = false
132135

@@ -315,6 +318,11 @@ const (
315318
FuturesAlgoOrderStatusTypeWorking FuturesAlgoOrderStatusType = "WORKING"
316319
FuturesAlgoOrderStatusTypeFinished FuturesAlgoOrderStatusType = "FINISHED"
317320
FuturesAlgoOrderStatusTypeCancelled FuturesAlgoOrderStatusType = "CANCELLED"
321+
322+
SelfTradePreventionModeNone SelfTradePreventionMode = "NONE"
323+
SelfTradePreventionModeExpireTaker SelfTradePreventionMode = "EXPIRE_TAKER"
324+
SelfTradePreventionModeExpireBoth SelfTradePreventionMode = "EXPIRE_BOTH"
325+
SelfTradePreventionModeExpireMaker SelfTradePreventionMode = "EXPIRE_MAKER"
318326
)
319327

320328
func currentTimestamp() int64 {

v2/delivery/client.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ type UserDataEventType string
6464
type UserDataEventReasonType string
6565

6666
// Endpoints
67-
const (
68-
baseApiMainUrl = "https://dapi.binance.com"
69-
baseApiTestnetUrl = "https://testnet.binancefuture.com"
67+
var (
68+
BaseApiMainUrl = "https://dapi.binance.com"
69+
BaseApiTestnetUrl = "https://testnet.binancefuture.com"
7070
)
7171

7272
// Global enums
@@ -178,9 +178,9 @@ func newJSON(data []byte) (j *simplejson.Json, err error) {
178178
// getApiEndpoint return the base endpoint of the WS according the UseTestnet flag
179179
func getApiEndpoint() string {
180180
if UseTestnet {
181-
return baseApiTestnetUrl
181+
return BaseApiTestnetUrl
182182
}
183-
return baseApiMainUrl
183+
return BaseApiMainUrl
184184
}
185185

186186
// NewClient initialize an API client instance with API key and secret key.

v2/delivery/websocket_service.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
)
1111

1212
// Endpoints
13-
const (
14-
baseWsMainUrl = "wss://dstream.binance.com/ws"
15-
baseWsTestnetUrl = "wss://dstream.binancefuture.com/ws"
13+
var (
14+
BaseWsMainUrl = "wss://dstream.binance.com/ws"
15+
BaseWsTestnetUrl = "wss://dstream.binancefuture.com/ws"
1616
)
1717

1818
var (
@@ -28,9 +28,9 @@ var (
2828
// getWsEndpoint return the base endpoint of the WS according the UseTestnet flag
2929
func getWsEndpoint() string {
3030
if UseTestnet {
31-
return baseWsTestnetUrl
31+
return BaseWsTestnetUrl
3232
}
33-
return baseWsMainUrl
33+
return BaseWsMainUrl
3434
}
3535

3636
func getWsProxyUrl() *string {

v2/futures/client.go

+8
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ type UserDataEventReasonType string
7373
// ForceOrderCloseType define reason type for force order
7474
type ForceOrderCloseType string
7575

76+
// SelfTradePreventionMode define self trade prevention strategy
77+
type SelfTradePreventionMode string
78+
7679
// Endpoints
7780
var (
7881
BaseApiMainUrl = "https://fapi.binance.com"
@@ -187,6 +190,11 @@ const (
187190
ForceOrderCloseTypeLiquidation ForceOrderCloseType = "LIQUIDATION"
188191
ForceOrderCloseTypeADL ForceOrderCloseType = "ADL"
189192

193+
SelfTradePreventionModeNone SelfTradePreventionMode = "NONE"
194+
SelfTradePreventionModeExpireTaker SelfTradePreventionMode = "EXPIRE_TAKER"
195+
SelfTradePreventionModeExpireBoth SelfTradePreventionMode = "EXPIRE_BOTH"
196+
SelfTradePreventionModeExpireMaker SelfTradePreventionMode = "EXPIRE_MAKER"
197+
190198
timestampKey = "timestamp"
191199
signatureKey = "signature"
192200
recvWindowKey = "recvWindow"

v2/futures/order_place_service_ws.go

+26-16
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,23 @@ type OrderPlaceWsService struct {
1515

1616
// OrderPlaceWsRequest parameters for 'order.place' websocket API
1717
type OrderPlaceWsRequest struct {
18-
symbol string
19-
side SideType
20-
positionSide *PositionSideType
21-
orderType OrderType
22-
timeInForce *TimeInForceType
23-
quantity string
24-
reduceOnly *bool
25-
price *string
26-
newClientOrderID *string
27-
stopPrice *string
28-
workingType *WorkingType
29-
activationPrice *string
30-
callbackRate *string
31-
priceProtect *bool
32-
newOrderRespType NewOrderRespType
33-
closePosition *bool
18+
symbol string
19+
side SideType
20+
positionSide *PositionSideType
21+
orderType OrderType
22+
timeInForce *TimeInForceType
23+
quantity string
24+
reduceOnly *bool
25+
price *string
26+
newClientOrderID *string
27+
stopPrice *string
28+
workingType *WorkingType
29+
activationPrice *string
30+
callbackRate *string
31+
priceProtect *bool
32+
newOrderRespType NewOrderRespType
33+
closePosition *bool
34+
selfTradePreventionMode *SelfTradePreventionMode
3435
}
3536

3637
// NewOrderPlaceWsRequest init OrderPlaceWsRequest
@@ -134,6 +135,12 @@ func (s *OrderPlaceWsRequest) ClosePosition(closePosition bool) *OrderPlaceWsReq
134135
return s
135136
}
136137

138+
// SelfTradePreventionMode set selfTradePreventionMode
139+
func (s *OrderPlaceWsRequest) SelfTradePreventionMode(selfTradePreventionMode SelfTradePreventionMode) *OrderPlaceWsRequest {
140+
s.selfTradePreventionMode = &selfTradePreventionMode
141+
return s
142+
}
143+
137144
// CreateOrderResult define order creation result
138145
type CreateOrderResult struct {
139146
CreateOrderResponse
@@ -193,6 +200,9 @@ func (s *OrderPlaceWsRequest) buildParams() params {
193200
if s.closePosition != nil {
194201
m["closePosition"] = *s.closePosition
195202
}
203+
if s.selfTradePreventionMode != nil {
204+
m["selfTradePreventionMode"] = *s.selfTradePreventionMode
205+
}
196206

197207
return m
198208
}

v2/futures/order_service.go

+49-38
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,24 @@ import (
1414

1515
// CreateOrderService create order
1616
type CreateOrderService struct {
17-
c *Client
18-
symbol string
19-
side SideType
20-
positionSide *PositionSideType
21-
orderType OrderType
22-
timeInForce *TimeInForceType
23-
quantity string
24-
reduceOnly *string
25-
price *string
26-
newClientOrderID *string
27-
stopPrice *string
28-
workingType *WorkingType
29-
activationPrice *string
30-
callbackRate *string
31-
priceProtect *string
32-
newOrderRespType NewOrderRespType
33-
closePosition *string
17+
c *Client
18+
symbol string
19+
side SideType
20+
positionSide *PositionSideType
21+
orderType OrderType
22+
timeInForce *TimeInForceType
23+
quantity string
24+
reduceOnly *string
25+
price *string
26+
newClientOrderID *string
27+
stopPrice *string
28+
workingType *WorkingType
29+
activationPrice *string
30+
callbackRate *string
31+
priceProtect *string
32+
newOrderRespType NewOrderRespType
33+
closePosition *string
34+
selfTradePreventionMode *SelfTradePreventionMode
3435
}
3536

3637
// Symbol set symbol
@@ -132,6 +133,12 @@ func (s *CreateOrderService) ClosePosition(closePosition bool) *CreateOrderServi
132133
return s
133134
}
134135

136+
// SelfTradePreventionMode set selfTradePreventionMode
137+
func (s *CreateOrderService) SelfTradePreventionMode(selfTradePreventionMode SelfTradePreventionMode) *CreateOrderService {
138+
s.selfTradePreventionMode = &selfTradePreventionMode
139+
return s
140+
}
141+
135142
func (s *CreateOrderService) createOrder(ctx context.Context, endpoint string, opts ...RequestOption) (data []byte, header *http.Header, err error) {
136143
r := &request{
137144
method: http.MethodPost,
@@ -180,6 +187,9 @@ func (s *CreateOrderService) createOrder(ctx context.Context, endpoint string, o
180187
if s.closePosition != nil {
181188
m["closePosition"] = *s.closePosition
182189
}
190+
if s.selfTradePreventionMode != nil {
191+
m["selfTradePreventionMode"] = *s.selfTradePreventionMode
192+
}
183193
r.setFormParams(m)
184194
data, header, err = s.c.callAPI(ctx, r, opts...)
185195
if err != nil {
@@ -668,27 +678,28 @@ func (s *CancelOrderService) Do(ctx context.Context, opts ...RequestOption) (res
668678

669679
// CancelOrderResponse define response of canceling order
670680
type CancelOrderResponse struct {
671-
ClientOrderID string `json:"clientOrderId"`
672-
CumQuantity string `json:"cumQty"` // deprecated: use ExecutedQuantity instead
673-
CumQuote string `json:"cumQuote"`
674-
ExecutedQuantity string `json:"executedQty"`
675-
OrderID int64 `json:"orderId"`
676-
OrigQuantity string `json:"origQty"`
677-
Price string `json:"price"`
678-
ReduceOnly bool `json:"reduceOnly"`
679-
Side SideType `json:"side"`
680-
Status OrderStatusType `json:"status"`
681-
StopPrice string `json:"stopPrice"`
682-
Symbol string `json:"symbol"`
683-
TimeInForce TimeInForceType `json:"timeInForce"`
684-
Type OrderType `json:"type"`
685-
UpdateTime int64 `json:"updateTime"`
686-
WorkingType WorkingType `json:"workingType"`
687-
ActivatePrice string `json:"activatePrice"`
688-
PriceRate string `json:"priceRate"`
689-
OrigType string `json:"origType"`
690-
PositionSide PositionSideType `json:"positionSide"`
691-
PriceProtect bool `json:"priceProtect"`
681+
ClientOrderID string `json:"clientOrderId"`
682+
CumQuantity string `json:"cumQty"` // deprecated: use ExecutedQuantity instead
683+
CumQuote string `json:"cumQuote"`
684+
ExecutedQuantity string `json:"executedQty"`
685+
OrderID int64 `json:"orderId"`
686+
OrigQuantity string `json:"origQty"`
687+
Price string `json:"price"`
688+
ReduceOnly bool `json:"reduceOnly"`
689+
Side SideType `json:"side"`
690+
Status OrderStatusType `json:"status"`
691+
StopPrice string `json:"stopPrice"`
692+
Symbol string `json:"symbol"`
693+
TimeInForce TimeInForceType `json:"timeInForce"`
694+
Type OrderType `json:"type"`
695+
UpdateTime int64 `json:"updateTime"`
696+
WorkingType WorkingType `json:"workingType"`
697+
ActivatePrice string `json:"activatePrice"`
698+
PriceRate string `json:"priceRate"`
699+
OrigType string `json:"origType"`
700+
PositionSide PositionSideType `json:"positionSide"`
701+
PriceProtect bool `json:"priceProtect"`
702+
SelfTradePreventionMode SelfTradePreventionMode `json:"selfTradePreventionMode"`
692703
}
693704

694705
// CancelAllOpenOrdersService cancel all open orders

v2/futures/websocket_service.go

+12-11
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7-
"github.com/bitly/go-simplejson"
8-
"github.com/gorilla/websocket"
97
"strings"
108
"time"
9+
10+
"github.com/bitly/go-simplejson"
11+
"github.com/gorilla/websocket"
1112
)
1213

1314
// Endpoints
14-
const (
15-
baseWsMainUrl = "wss://fstream.binance.com/ws"
16-
baseWsTestnetUrl = "wss://stream.binancefuture.com/ws"
17-
baseCombinedMainURL = "wss://fstream.binance.com/stream?streams="
18-
baseCombinedTestnetURL = "wss://stream.binancefuture.com/stream?streams="
15+
var (
16+
BaseWsMainUrl = "wss://fstream.binance.com/ws"
17+
BaseWsTestnetUrl = "wss://stream.binancefuture.com/ws"
18+
BaseCombinedMainURL = "wss://fstream.binance.com/stream?streams="
19+
BaseCombinedTestnetURL = "wss://stream.binancefuture.com/stream?streams="
1920
BaseWsApiMainURL = "wss://ws-fapi.binance.com/ws-fapi/v1"
2021
BaseWsApiTestnetURL = "wss://testnet.binancefuture.com/ws-fapi/v1"
2122
localhostWsApiURL = "ws://localhost:8080/ws"
@@ -50,17 +51,17 @@ func SetWsProxyUrl(url string) {
5051
// getWsEndpoint return the base endpoint of the WS according the UseTestnet flag
5152
func getWsEndpoint() string {
5253
if UseTestnet {
53-
return baseWsTestnetUrl
54+
return BaseWsTestnetUrl
5455
}
55-
return baseWsMainUrl
56+
return BaseWsMainUrl
5657
}
5758

5859
// getCombinedEndpoint return the base endpoint of the combined stream according the UseTestnet flag
5960
func getCombinedEndpoint() string {
6061
if UseTestnet {
61-
return baseCombinedTestnetURL
62+
return BaseCombinedTestnetURL
6263
}
63-
return baseCombinedMainURL
64+
return BaseCombinedMainURL
6465
}
6566

6667
// WsAggTradeEvent define websocket aggTrde event.

0 commit comments

Comments
 (0)