Skip to content

Commit 3dd192a

Browse files
committed
feat: get symbol data, cancel all orders
1 parent 1ccd44f commit 3dd192a

3 files changed

Lines changed: 116 additions & 4 deletions

File tree

client.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"encoding/hex"
99
"encoding/json"
1010
"fmt"
11-
"io/ioutil"
11+
"io"
1212
"log"
1313
"net/http"
1414
"os"
@@ -213,7 +213,7 @@ func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption)
213213
if err != nil {
214214
return []byte{}, err
215215
}
216-
data, err = ioutil.ReadAll(res.Body)
216+
data, err = io.ReadAll(res.Body)
217217
if err != nil {
218218
return []byte{}, err
219219
}
@@ -300,3 +300,7 @@ func (c *Client) NewGetOpenOrdersService() *GetOpenOrdersService {
300300
func (c *Client) NewGetKlinesService() *GetKlinesService {
301301
return &GetKlinesService{c: c}
302302
}
303+
304+
func (c *Client) NewGetSymbolDataService() *GetSymbolDataService {
305+
return &GetSymbolDataService{c: c}
306+
}

markets_service.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package bingx
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"net/http"
7+
)
8+
9+
type GetSymbolDataService struct {
10+
c *Client
11+
symbol string
12+
}
13+
14+
func (s *GetSymbolDataService) Symbol(symbol string) *GetSymbolDataService {
15+
s.symbol = symbol
16+
return s
17+
}
18+
19+
type SymbolData struct {
20+
Symbol string `json:"symbol"`
21+
QuantityPrecision int `json:"quantityPrecision"`
22+
PricePrecision int `json:"pricePrecision"`
23+
TradeMinQuantity int `json:"tradeMinQuantity"`
24+
}
25+
26+
func (s *GetSymbolDataService) Do(ctx context.Context, opts ...RequestOption) (res *SymbolData, err error) {
27+
r := &request{method: http.MethodGet, endpoint: "/openApi/swap/v2/quote/contracts"}
28+
29+
if s.symbol != "" {
30+
r.addParam("symbol", s.symbol)
31+
}
32+
33+
data, err := s.c.callAPI(ctx, r, opts...)
34+
if err != nil {
35+
return nil, err
36+
}
37+
38+
resp := new(struct {
39+
Code int `json:"code"`
40+
Msg string `json:"msg"`
41+
Data *SymbolData `json:"data"`
42+
})
43+
44+
err = json.Unmarshal(data, &resp)
45+
res = resp.Data
46+
47+
if err != nil {
48+
return nil, err
49+
}
50+
51+
return res, nil
52+
}

order_service.go

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"net/http"
77
)
88

9-
// Only Market and Limit orders supported
9+
// CreateOrderService Only Market and Limit orders supported
1010
type CreateOrderService struct {
1111
c *Client
1212
symbol string
@@ -147,7 +147,7 @@ func (s *CancelOrderService) ClientOrderId(clientOrderID string) *CancelOrderSer
147147
return s
148148
}
149149

150-
// Define response of cancel order request
150+
// CancelOrderResponse Define response of cancel order request
151151
type CancelOrderResponse struct {
152152
Time int `json:"time"`
153153
Symbol string `json:"symbol"`
@@ -205,6 +205,62 @@ func (s *CancelOrderService) Do(ctx context.Context, opts ...RequestOption) (res
205205
return res, nil
206206
}
207207

208+
type CancelAllOrdersService struct {
209+
c *Client
210+
symbol string
211+
orderType OrderType
212+
}
213+
214+
func (s *CancelAllOrdersService) Symbol(symbol string) *CancelAllOrdersService {
215+
s.symbol = symbol
216+
return s
217+
}
218+
219+
func (s *CancelAllOrdersService) Type(orderType OrderType) *CancelAllOrdersService {
220+
s.orderType = orderType
221+
222+
return s
223+
}
224+
225+
// CancelAllOrdersResponse Define response of cancel order request
226+
type CancelAllOrdersResponse struct {
227+
Success []CancelOrderResponse `json:"success"`
228+
Failed []CancelOrderResponse `json:"failed"`
229+
}
230+
231+
func (s *CancelAllOrdersService) Do(ctx context.Context, opts ...RequestOption) (res *CancelAllOrdersResponse, err error) {
232+
r := &request{method: http.MethodDelete, endpoint: "/openApi/swap/v2/trade/allOpenOrders"}
233+
234+
if s.symbol != "" {
235+
r.addParam("symbol", s.symbol)
236+
}
237+
238+
if s.orderType != "" {
239+
r.addParam("type", s.orderType)
240+
}
241+
242+
data, err := s.c.callAPI(ctx, r, opts...)
243+
if err != nil {
244+
return nil, err
245+
}
246+
247+
resp := new(struct {
248+
Code int `json:"code"`
249+
Msg string `json:"msg"`
250+
Data map[string]*CancelAllOrdersResponse `json:"data"`
251+
})
252+
253+
err = json.Unmarshal(data, &resp)
254+
255+
if err != nil {
256+
return nil, err
257+
}
258+
259+
res = resp.Data["order"]
260+
261+
return res, nil
262+
}
263+
208264
type GetOrderService struct {
209265
c *Client
210266
symbol string

0 commit comments

Comments
 (0)