Skip to content

Commit 81cc831

Browse files
authored
complete market interfaces unimplemented in futures (#586)
* complete interfaces unimplemented in futures * rename index_info_service.go * gofmt
1 parent a387a5b commit 81cc831

11 files changed

+1360
-0
lines changed

v2/futures/asset_index_service.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package futures
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"net/http"
7+
)
8+
9+
type AssetIndexService struct {
10+
c *Client
11+
symbol *string // for example, BTCUSD
12+
}
13+
14+
type AssetIndex struct {
15+
Symbol string `json:"symbol"`
16+
Time uint64 `json:"time"`
17+
Index string `json:"index"`
18+
BidBuffer string `json:"bidBuffer"`
19+
AskBuffer string `json:"askBuffer"`
20+
BidRate string `json:"bidRate"`
21+
AskRate string `json:"askRate"`
22+
AutoExchangeBidBuffer string `json:"autoExchangeBidBuffer"`
23+
AutoExchangeAskBuffer string `json:"autoExchangeAskBuffer"`
24+
AutoExchangeBidRate string `json:"autoExchangeBidRate"`
25+
AutoExchangeAskRate string `json:"autoExchangeAskRate"`
26+
}
27+
28+
func (s *AssetIndexService) Symbol(symbol string) *AssetIndexService {
29+
s.symbol = &symbol
30+
return s
31+
}
32+
33+
func (s *AssetIndexService) Do(ctx context.Context, opts ...RequestOption) (res []*AssetIndex, err error) {
34+
r := &request{
35+
method: http.MethodGet,
36+
endpoint: "/futures/v1/assetIndex",
37+
}
38+
if s.symbol != nil {
39+
r.setParam("symbol", *s.symbol)
40+
}
41+
42+
data, _, err := s.c.callAPI(ctx, r, opts...)
43+
if err != nil {
44+
return nil, err
45+
}
46+
res = make([]*AssetIndex, 0)
47+
err = json.Unmarshal(data, &res)
48+
if err != nil {
49+
return nil, err
50+
}
51+
52+
return res, nil
53+
}
+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package futures
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/suite"
7+
)
8+
9+
type assetIndexServiceTestSuite struct {
10+
baseTestSuite
11+
}
12+
13+
func TestAssetIndexService(t *testing.T) {
14+
suite.Run(t, new(assetIndexServiceTestSuite))
15+
}
16+
17+
func (s *assetIndexServiceTestSuite) TestAssetIndex() {
18+
data := []byte(`[
19+
{
20+
"symbol": "BTCUSD",
21+
"time": 1719554624000,
22+
"index": "61475.02720330",
23+
"bidBuffer": "0.05000000",
24+
"askBuffer": "0.05000000",
25+
"bidRate": "58401.27584314",
26+
"askRate": "64548.77856347",
27+
"autoExchangeBidBuffer": "0.02500000",
28+
"autoExchangeAskBuffer": "0.02500000",
29+
"autoExchangeBidRate": "59938.15152322",
30+
"autoExchangeAskRate": "63011.90288338"
31+
},
32+
{
33+
"symbol": "USDTUSD",
34+
"time": 1719554624000,
35+
"index": "0.99884101",
36+
"bidBuffer": "0.00010000",
37+
"askBuffer": "0.00010000",
38+
"bidRate": "0.99874113",
39+
"askRate": "0.99894089",
40+
"autoExchangeBidBuffer": "0.00010000",
41+
"autoExchangeAskBuffer": "0.00010000",
42+
"autoExchangeBidRate": "0.99874113",
43+
"autoExchangeAskRate": "0.99894089"
44+
}]`)
45+
s.mockDo(data, nil)
46+
defer s.assertDo()
47+
var symbol string = "BTCUSD"
48+
s.assertReq(func(r *request) {
49+
e := newRequest().setParam("symbol", symbol)
50+
s.assertRequestEqual(e, r)
51+
})
52+
res, err := s.client.NewAssetIndexService().Symbol(symbol).Do(newContext())
53+
s.r().NoError(err)
54+
55+
e := []*AssetIndex{
56+
{
57+
Symbol: "BTCUSD",
58+
Time: 1719554624000,
59+
Index: "61475.02720330",
60+
BidBuffer: "0.05000000",
61+
AskBuffer: "0.05000000",
62+
BidRate: "58401.27584314",
63+
AskRate: "64548.77856347",
64+
AutoExchangeBidBuffer: "0.02500000",
65+
AutoExchangeAskBuffer: "0.02500000",
66+
AutoExchangeBidRate: "59938.15152322",
67+
AutoExchangeAskRate: "63011.90288338"},
68+
{
69+
Symbol: "USDTUSD",
70+
Time: 1719554624000,
71+
Index: "0.99884101",
72+
BidBuffer: "0.00010000",
73+
AskBuffer: "0.00010000",
74+
BidRate: "0.99874113",
75+
AskRate: "0.99894089",
76+
AutoExchangeBidBuffer: "0.00010000",
77+
AutoExchangeAskBuffer: "0.00010000",
78+
AutoExchangeBidRate: "0.99874113",
79+
AutoExchangeAskRate: "0.99894089"}}
80+
81+
s.assertAssetIndexesEqual(e, res)
82+
}
83+
84+
func (s *assetIndexServiceTestSuite) assertAssetIndexEqual(e, a *AssetIndex) {
85+
r := s.r()
86+
r.Equal(e.Symbol, a.Symbol, "Symbol")
87+
r.Equal(e.Time, a.Time, "Time")
88+
r.Equal(e.Index, a.Index, "Index")
89+
r.Equal(e.BidBuffer, a.BidBuffer, "BidBuffer")
90+
r.Equal(e.AskBuffer, a.AskBuffer, "AskBuffer")
91+
r.Equal(e.BidRate, a.BidRate, "BidRate")
92+
r.Equal(e.AskRate, a.AskRate, "AskRate")
93+
r.Equal(e.AutoExchangeBidBuffer, a.AutoExchangeBidBuffer, "AutoExchangeBidBuffer")
94+
r.Equal(e.AutoExchangeAskBuffer, a.AutoExchangeAskBuffer, "AutoExchangeAskBuffer")
95+
r.Equal(e.AutoExchangeBidRate, a.AutoExchangeBidRate, "AutoExchangeBidRate")
96+
r.Equal(e.AutoExchangeAskRate, a.AutoExchangeAskRate, "AutoExchangeAskRate")
97+
}
98+
99+
func (s *assetIndexServiceTestSuite) assertAssetIndexesEqual(e, a []*AssetIndex) {
100+
s.r().Len(e, len(a))
101+
for i := range e {
102+
s.assertAssetIndexEqual(e[i], a[i])
103+
}
104+
}

v2/futures/client.go

+36
Original file line numberDiff line numberDiff line change
@@ -640,3 +640,39 @@ func (c *Client) NewOpenInterestStatisticsService() *OpenInterestStatisticsServi
640640
func (c *Client) NewLongShortRatioService() *LongShortRatioService {
641641
return &LongShortRatioService{c: c}
642642
}
643+
644+
func (c *Client) NewDeliveryPriceService() *DeliveryPriceService {
645+
return &DeliveryPriceService{c: c}
646+
}
647+
648+
func (c *Client) NewTopLongShortAccountRatioService() *TopLongShortAccountRatioService {
649+
return &TopLongShortAccountRatioService{c: c}
650+
}
651+
652+
func (c *Client) NewTopLongShortPositionRatioService() *TopLongShortPositionRatioService {
653+
return &TopLongShortPositionRatioService{c: c}
654+
}
655+
656+
func (c *Client) NewTakerLongShortRatioService() *TakerLongShortRatioService {
657+
return &TakerLongShortRatioService{c: c}
658+
}
659+
660+
func (c *Client) NewBasisService() *BasisService {
661+
return &BasisService{c: c}
662+
}
663+
664+
func (c *Client) NewIndexInfoService() *IndexInfoService {
665+
return &IndexInfoService{c: c}
666+
}
667+
668+
func (c *Client) NewAssetIndexService() *AssetIndexService {
669+
return &AssetIndexService{c: c}
670+
}
671+
672+
func (c *Client) NewConstituentsService() *ConstituentsService {
673+
return &ConstituentsService{c: c}
674+
}
675+
676+
func (c *Client) NewLvtKlinesService() *LvtKlinesService {
677+
return &LvtKlinesService{c: c}
678+
}

v2/futures/constituents_service.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package futures
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"net/http"
7+
)
8+
9+
type ConstituentsService struct {
10+
c *Client
11+
symbol string // for example, BTCUSDT
12+
}
13+
14+
type ConstituentsServiceRsp struct {
15+
Symbol string `json:"symbol"`
16+
Time uint64 `json:"time"`
17+
Constituents []*Constituents `json:"constituents"`
18+
}
19+
20+
type Constituents struct {
21+
Exchange string `json:"exchange"`
22+
Symbol string `json:"symbol"`
23+
}
24+
25+
func (s *ConstituentsService) Symbol(symbol string) *ConstituentsService {
26+
s.symbol = symbol
27+
return s
28+
}
29+
30+
func (s *ConstituentsService) Do(ctx context.Context, opts ...RequestOption) (res *ConstituentsServiceRsp, err error) {
31+
r := &request{
32+
method: http.MethodGet,
33+
endpoint: "/futures/v1/assetIndex",
34+
}
35+
r.setParam("symbol", s.symbol)
36+
37+
data, _, err := s.c.callAPI(ctx, r, opts...)
38+
if err != nil {
39+
return nil, err
40+
}
41+
res = new(ConstituentsServiceRsp)
42+
err = json.Unmarshal(data, res)
43+
if err != nil {
44+
return nil, err
45+
}
46+
47+
return res, nil
48+
}
+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package futures
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/suite"
7+
)
8+
9+
type constituentsServiceTestSuite struct {
10+
baseTestSuite
11+
}
12+
13+
func TestConstituentsService(t *testing.T) {
14+
suite.Run(t, new(constituentsServiceTestSuite))
15+
}
16+
17+
func (s *constituentsServiceTestSuite) TestConstituents() {
18+
data := []byte(`{
19+
"symbol": "BTCUSDT",
20+
"time": 1719554596473,
21+
"constituents": [
22+
{
23+
"exchange": "binance",
24+
"symbol": "BTCUSDT"
25+
},
26+
{
27+
"exchange": "okex",
28+
"symbol": "BTC-USDT"
29+
},
30+
{
31+
"exchange": "coinbase",
32+
"symbol": "BTC-USDT"
33+
},
34+
{
35+
"exchange": "gateio",
36+
"symbol": "BTC_USDT"
37+
},
38+
{
39+
"exchange": "kucoin",
40+
"symbol": "BTC-USDT"
41+
},
42+
{
43+
"exchange": "mxc",
44+
"symbol": "BTCUSDT"
45+
},
46+
{
47+
"exchange": "bitget",
48+
"symbol": "BTCUSDT"
49+
},
50+
{
51+
"exchange": "bybit",
52+
"symbol": "BTCUSDT"
53+
}
54+
]
55+
}`)
56+
s.mockDo(data, nil)
57+
defer s.assertDo()
58+
var symbol string = "BTCUSDT"
59+
s.assertReq(func(r *request) {
60+
e := newRequest().setParam("symbol", symbol)
61+
s.assertRequestEqual(e, r)
62+
})
63+
res, err := s.client.NewConstituentsService().Symbol(symbol).Do(newContext())
64+
s.r().NoError(err)
65+
66+
e := &ConstituentsServiceRsp{
67+
Symbol: "BTCUSDT",
68+
Time: 1719554596473,
69+
Constituents: []*Constituents{{
70+
Exchange: "binance",
71+
Symbol: "BTCUSDT"},
72+
{
73+
Exchange: "okex",
74+
Symbol: "BTC-USDT"},
75+
{
76+
Exchange: "coinbase",
77+
Symbol: "BTC-USDT"},
78+
{
79+
Exchange: "gateio",
80+
Symbol: "BTC_USDT"},
81+
{
82+
Exchange: "kucoin",
83+
Symbol: "BTC-USDT"},
84+
{
85+
Exchange: "mxc",
86+
Symbol: "BTCUSDT"},
87+
{
88+
Exchange: "bitget",
89+
Symbol: "BTCUSDT"},
90+
{
91+
Exchange: "bybit",
92+
Symbol: "BTCUSDT"}}}
93+
s.assertConstituentsServiceRspEqual(e, res)
94+
}
95+
96+
func (s *constituentsServiceTestSuite) assertConstituentsServiceRspEqual(e, a *ConstituentsServiceRsp) {
97+
r := s.r()
98+
r.Equal(e.Symbol, a.Symbol, "Symbol")
99+
r.Equal(e.Time, a.Time, "Time")
100+
r.Len(e.Constituents, len(a.Constituents))
101+
for i := range e.Constituents {
102+
r.Equal(e.Constituents[i].Exchange, a.Constituents[i].Exchange, "Constituents[i].Exchange")
103+
r.Equal(e.Constituents[i].Symbol, a.Constituents[i].Symbol, "Constituents[i].Symbol")
104+
}
105+
106+
}

0 commit comments

Comments
 (0)