Skip to content

Commit dc7abf8

Browse files
authored
Add functions to fetch funding rate data from market (#552)
* Add functions to fetch funding rate data from market * Remove invalid test function
1 parent 4a82de2 commit dc7abf8

5 files changed

+243
-0
lines changed

v2/futures/client.go

+10
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,16 @@ func (c *Client) NewFundingRateService() *FundingRateService {
546546
return &FundingRateService{c: c}
547547
}
548548

549+
// NewFundingRateHistoryService init funding rate history service
550+
func (c *Client) NewFundingRateHistoryService() *FundingRateHistoryService {
551+
return &FundingRateHistoryService{c: c}
552+
}
553+
554+
// NewFundingRateInfoService init funding rate info service
555+
func (c *Client) NewFundingRateInfoService() *FundingRateInfoService {
556+
return &FundingRateInfoService{c: c}
557+
}
558+
549559
// NewListUserLiquidationOrdersService init list user's liquidation orders service
550560
func (c *Client) NewListUserLiquidationOrdersService() *ListUserLiquidationOrdersService {
551561
return &ListUserLiquidationOrdersService{c: c}

v2/futures/funding_rate_history.go

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package futures
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"net/http"
7+
8+
"github.com/adshao/go-binance/v2/common"
9+
)
10+
11+
// FundingRateHistoryService gets funding rate history
12+
type FundingRateHistoryService struct {
13+
c *Client
14+
symbol *string
15+
startTime *int64
16+
endTime *int64
17+
limit *int
18+
}
19+
20+
// Symbol set symbol
21+
func (s *FundingRateHistoryService) Symbol(symbol string) *FundingRateHistoryService {
22+
s.symbol = &symbol
23+
return s
24+
}
25+
26+
// StartTime set startTime
27+
func (s *FundingRateHistoryService) StartTime(startTime int64) *FundingRateHistoryService {
28+
s.startTime = &startTime
29+
return s
30+
}
31+
32+
// EndTime set startTime
33+
func (s *FundingRateHistoryService) EndTime(endTime int64) *FundingRateHistoryService {
34+
s.endTime = &endTime
35+
return s
36+
}
37+
38+
// Limit set limit
39+
func (s *FundingRateHistoryService) Limit(limit int) *FundingRateHistoryService {
40+
s.limit = &limit
41+
return s
42+
}
43+
44+
// Do send request
45+
func (s *FundingRateHistoryService) Do(ctx context.Context, opts ...RequestOption) (res []*FundingRateHistory, err error) {
46+
r := &request{
47+
method: http.MethodGet,
48+
endpoint: "/fapi/v1/fundingRate",
49+
secType: secTypeNone,
50+
}
51+
if s.symbol != nil {
52+
r.setParam("symbol", *s.symbol)
53+
}
54+
if s.startTime != nil {
55+
r.setParam("startTime", *s.startTime)
56+
}
57+
if s.endTime != nil {
58+
r.setParam("endTime", *s.endTime)
59+
}
60+
if s.limit != nil {
61+
r.setParam("limit", *s.limit)
62+
}
63+
data, _, err := s.c.callAPI(ctx, r, opts...)
64+
data = common.ToJSONList(data)
65+
if err != nil {
66+
return []*FundingRateHistory{}, err
67+
}
68+
res = make([]*FundingRateHistory, 0)
69+
err = json.Unmarshal(data, &res)
70+
if err != nil {
71+
return []*FundingRateHistory{}, err
72+
}
73+
return res, nil
74+
}
75+
76+
// FundingRateHistory defines funding rate history data
77+
type FundingRateHistory struct {
78+
Symbol string `json:"symbol"`
79+
FundingTime int64 `json:"fundingTime"`
80+
FundingRate string `json:"fundingRate"`
81+
MarkPrice string `json:"markPrice"`
82+
}
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package futures
2+
3+
import (
4+
"github.com/stretchr/testify/suite"
5+
"testing"
6+
)
7+
8+
type fundingRateHistoryServiceTestSuite struct {
9+
baseTestSuite
10+
}
11+
12+
func TestFundingRateHistoryService(t *testing.T) {
13+
suite.Run(t, new(fundingRateHistoryServiceTestSuite))
14+
}
15+
16+
func (s *fundingRateHistoryServiceTestSuite) TestGetFundingRateHistory() {
17+
data := []byte(`[{
18+
"symbol": "BTCUSDT",
19+
"fundingTime": 1698768000000,
20+
"fundingRate": "0.00010000",
21+
"markPrice": "34287.54619963"
22+
}]`)
23+
s.mockDo(data, nil)
24+
defer s.assertDo()
25+
26+
symbol := "BTCUSDT"
27+
startTime := int64(1576566020000)
28+
endTime := int64(1676566020000)
29+
limit := 10
30+
s.assertReq(func(r *request) {
31+
e := newRequest().setParams(params{
32+
"symbol": symbol,
33+
"startTime": startTime,
34+
"endTime": endTime,
35+
"limit": limit,
36+
})
37+
s.assertRequestEqual(e, r)
38+
})
39+
40+
res, err := s.client.NewFundingRateHistoryService().Symbol(symbol).StartTime(startTime).EndTime(endTime).Limit(limit).Do(newContext())
41+
s.r().NoError(err)
42+
e := []*FundingRateHistory{
43+
{
44+
Symbol: symbol,
45+
FundingTime: 1698768000000,
46+
FundingRate: "0.00010000",
47+
MarkPrice: "34287.54619963",
48+
},
49+
}
50+
s.assertFundingRateHistoryEqual(e, res)
51+
}
52+
53+
func (s *fundingRateHistoryServiceTestSuite) assertFundingRateHistoryEqual(e, a []*FundingRateHistory) {
54+
r := s.r()
55+
r.Equal(e[0].Symbol, a[0].Symbol, "Symbol")
56+
r.Equal(e[0].FundingRate, a[0].FundingRate, "FundingRate")
57+
r.Equal(e[0].FundingTime, a[0].FundingTime, "FundingTime")
58+
r.Equal(e[0].MarkPrice, a[0].MarkPrice, "MarkPrice")
59+
}

v2/futures/funding_rate_info.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package futures
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"net/http"
7+
8+
"github.com/adshao/go-binance/v2/common"
9+
)
10+
11+
// FundingRateInfoService gets funding rate info
12+
type FundingRateInfoService struct {
13+
c *Client
14+
}
15+
16+
// Do sends request
17+
func (s *FundingRateInfoService) Do(ctx context.Context, opts ...RequestOption) (res []*FundingRateInfo, err error) {
18+
r := &request{
19+
method: http.MethodGet,
20+
endpoint: "/fapi/v1/fundingInfo",
21+
secType: secTypeNone,
22+
}
23+
data, _, err := s.c.callAPI(ctx, r, opts...)
24+
data = common.ToJSONList(data)
25+
if err != nil {
26+
return []*FundingRateInfo{}, err
27+
}
28+
res = make([]*FundingRateInfo, 0)
29+
err = json.Unmarshal(data, &res)
30+
if err != nil {
31+
return []*FundingRateInfo{}, err
32+
}
33+
return res, nil
34+
}
35+
36+
// FundingRateInfo defines funding rate info for symbols
37+
type FundingRateInfo struct {
38+
Symbol string `json:"symbol"`
39+
AdjustedFundingRateCap string `json:"adjustedFundingRateCap"`
40+
AdjustedFundingRateFloor string `json:"adjustedFundingRateFloor"`
41+
FundingIntervalHours int64 `json:"fundingIntervalHours"`
42+
}

v2/futures/funding_rate_info_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package futures
2+
3+
import (
4+
"github.com/stretchr/testify/suite"
5+
"testing"
6+
)
7+
8+
type fundingRateInfoServiceTestSuite struct {
9+
baseTestSuite
10+
}
11+
12+
func TestFundingRateInfoService(t *testing.T) {
13+
suite.Run(t, new(fundingRateInfoServiceTestSuite))
14+
}
15+
16+
func (s *fundingRateInfoServiceTestSuite) TestGetFundingRateInfo() {
17+
data := []byte(`[{
18+
"symbol": "BTCUSDT",
19+
"adjustedFundingRateCap": "0.02500000",
20+
"adjustedFundingRateFloor": "-0.02500000",
21+
"fundingIntervalHours": 8
22+
}]`)
23+
s.mockDo(data, nil)
24+
defer s.assertDo()
25+
26+
s.assertReq(func(r *request) {
27+
e := newRequest()
28+
s.assertRequestEqual(e, r)
29+
})
30+
31+
res, err := s.client.NewFundingRateInfoService().Do(newContext())
32+
s.r().NoError(err)
33+
e := []*FundingRateInfo{
34+
{
35+
Symbol: "BTCUSDT",
36+
AdjustedFundingRateCap: "0.02500000",
37+
AdjustedFundingRateFloor: "-0.02500000",
38+
FundingIntervalHours: 8,
39+
},
40+
}
41+
s.assertFundingRateInfoEqual(e, res)
42+
}
43+
44+
func (s *fundingRateInfoServiceTestSuite) assertFundingRateInfoEqual(e, a []*FundingRateInfo) {
45+
r := s.r()
46+
r.Equal(e[0].Symbol, a[0].Symbol, "Symbol")
47+
r.Equal(e[0].AdjustedFundingRateCap, a[0].AdjustedFundingRateCap, "AdjustedFundingRateCap")
48+
r.Equal(e[0].AdjustedFundingRateFloor, a[0].AdjustedFundingRateFloor, "AdjustedFundingRateFloor")
49+
r.Equal(e[0].FundingIntervalHours, a[0].FundingIntervalHours, "FundingIntervalHours")
50+
}

0 commit comments

Comments
 (0)