diff --git a/v2/client.go b/v2/client.go index 0814a634..f9026b1f 100644 --- a/v2/client.go +++ b/v2/client.go @@ -109,6 +109,9 @@ type UserUniversalTransferType string // UserUniversalTransferStatus define the user universal transfer status type UserUniversalTransferStatusType string +// FuturesOrderBookHistoryDataType define the futures order book history data types +type FuturesOrderBookHistoryDataType string + // Endpoints var ( BaseAPIMainURL = "https://api.binance.com" @@ -287,6 +290,9 @@ const ( UserUniversalTransferStatusTypePending UserUniversalTransferStatusType = "PENDING" UserUniversalTransferStatusTypeConfirmed UserUniversalTransferStatusType = "CONFIRMED" UserUniversalTransferStatusTypeFailed UserUniversalTransferStatusType = "FAILED" + + FuturesOrderBookHistoryDataTypeTDepth FuturesOrderBookHistoryDataType = "T_DEPTH" + FuturesOrderBookHistoryDataTypeSDepth FuturesOrderBookHistoryDataType = "S_DEPTH" ) func currentTimestamp() int64 { @@ -1291,3 +1297,8 @@ func (c *Client) NewSubAccountTransactionStatisticsService() *SubAccountTransact func (c *Client) NewSubAccountFuturesAccountV2Service() *SubAccountFuturesAccountV2Service { return &SubAccountFuturesAccountV2Service{c: c} } + +// Futures order book history service +func (c *Client) NewFuturesOrderBookHistoryService() *FuturesOrderBookHistoryService { + return &FuturesOrderBookHistoryService{c: c} +} diff --git a/v2/futures_service.go b/v2/futures_service.go index b11900a5..502e9c75 100644 --- a/v2/futures_service.go +++ b/v2/futures_service.go @@ -143,3 +143,64 @@ type FuturesTransfer struct { Timestamp int64 `json:"timestamp"` Status FuturesTransferStatusType `json:"status"` } + +type FuturesOrderBookHistoryService struct { + c *Client + symbol string + dataType string + startTime int64 + endTime int64 +} + +func (s *FuturesOrderBookHistoryService) Symbol(symbol string) *FuturesOrderBookHistoryService { + s.symbol = symbol + return s +} + +func (s *FuturesOrderBookHistoryService) DataType(dataType FuturesOrderBookHistoryDataType) *FuturesOrderBookHistoryService { + s.dataType = string(dataType) + return s +} + +func (s *FuturesOrderBookHistoryService) StartTime(startTime int64) *FuturesOrderBookHistoryService { + s.startTime = startTime + return s +} + +func (s *FuturesOrderBookHistoryService) EndTime(endTime int64) *FuturesOrderBookHistoryService { + s.endTime = endTime + return s +} + +func (s *FuturesOrderBookHistoryService) Do(ctx context.Context, opts ...RequestOption) (res *FuturesOrderBookHistory, err error) { + r := &request{ + method: http.MethodGet, + endpoint: "/sapi/v1/futures/histDataLink", + secType: secTypeSigned, + } + r.setParams(params{ + "symbol": s.symbol, + "dataType": s.dataType, + "startTime": s.startTime, + "endTime": s.endTime, + }) + data, err := s.c.callAPI(ctx, r, opts...) + if err != nil { + return nil, err + } + res = new(FuturesOrderBookHistory) + err = json.Unmarshal(data, &res) + if err != nil { + return nil, err + } + return res, nil +} + +type FuturesOrderBookHistoryItem struct { + Day string `json:"day"` + Url string `json:"url"` +} + +type FuturesOrderBookHistory struct { + Data []*FuturesOrderBookHistoryItem `json:"data"` +} diff --git a/v2/futures_service_test.go b/v2/futures_service_test.go index eb0012ec..04bf4d6c 100644 --- a/v2/futures_service_test.go +++ b/v2/futures_service_test.go @@ -105,3 +105,56 @@ func (s *futuresTransferTestSuite) assertFuturesTransferEqual(e, a FuturesTransf r.Equal(e.Timestamp, a.Timestamp, "Timestamp") r.Equal(e.Status, a.Status, "Status") } + +type futuresOrderBookHistoryTestSuite struct { + baseTestSuite +} + +func TestFuturesOrderBookHistoryService(t *testing.T) { + suite.Run(t, new(futuresOrderBookHistoryTestSuite)) +} + +func (s *futuresOrderBookHistoryTestSuite) TestFuturesOrderBookHistory() { + data := []byte(`{ + "data": [ + { + "day": "2023-06-30", + "url": "https://bin-prod-user-rebate-bucket.s3.ap-northeast-1.amazonaws.com/future-data-symbol-update/2023-06-30/BTCUSDT_T_DEPTH_2023-06-30.tar.gz?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20230925T025710Z&X-Amz-SignedHeaders=host&X-Amz-Expires=86399&X-Amz-Credential=AKIAVL364M5ZNFZ74IPP%2F20230925%2Fap-northeast-1%2Fs3%2Faws4_request&X-Amz-Signature=5fffcb390d10f34d71615726f81f99e42d80a11532edeac77b858c51a88cbf59" + } + ] + }`) + s.mockDo(data, nil) + defer s.assertDo() + symbol := "BTCUSDT" + dataType := FuturesOrderBookHistoryDataTypeTDepth + startTime := int64(1625040000000) + endTime := int64(1625126399999) + s.assertReq(func(r *request) { + e := newSignedRequest().setParams(params{ + "symbol": symbol, + "dataType": "T_DEPTH", + "startTime": startTime, + "endTime": endTime, + }) + s.assertRequestEqual(e, r) + }) + res, err := s.client.NewFuturesOrderBookHistoryService().Symbol(symbol). + DataType(dataType).StartTime(startTime).EndTime(endTime).Do(newContext()) + s.r().NoError(err) + e := &FuturesOrderBookHistory{ + Data: []*FuturesOrderBookHistoryItem{ + { + Day: "2023-06-30", + Url: "https://bin-prod-user-rebate-bucket.s3.ap-northeast-1.amazonaws.com/future-data-symbol-update/2023-06-30/BTCUSDT_T_DEPTH_2023-06-30.tar.gz?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20230925T025710Z&X-Amz-SignedHeaders=host&X-Amz-Expires=86399&X-Amz-Credential=AKIAVL364M5ZNFZ74IPP%2F20230925%2Fap-northeast-1%2Fs3%2Faws4_request&X-Amz-Signature=5fffcb390d10f34d71615726f81f99e42d80a11532edeac77b858c51a88cbf59", + }, + }, + } + s.assertFuturesOrderBookHistoryEqual(e, res) +} + +func (s *futuresOrderBookHistoryTestSuite) assertFuturesOrderBookHistoryEqual(a, e *FuturesOrderBookHistory) { + for index, v := range a.Data { + v.Day = e.Data[index].Day + v.Url = e.Data[index].Url + } +}