pkg/exchange: use time.Time instead of int64 to represent time

This commit is contained in:
Edwin 2023-11-15 22:20:26 +08:00
parent 579e8b0ae5
commit 687ffe985c
8 changed files with 88 additions and 34 deletions

View File

@ -34,15 +34,16 @@ func TestClient(t *testing.T) {
ctx := context.Background()
t.Run("GetUnfilledOrdersRequest", func(t *testing.T) {
req := client.NewGetUnfilledOrdersRequest().StartTime(1)
startTime := time.Now().Add(-30 * 24 * time.Hour)
req := client.NewGetUnfilledOrdersRequest().StartTime(startTime)
resp, err := req.Do(ctx)
assert.NoError(t, err)
t.Logf("resp: %+v", resp)
})
t.Run("GetHistoryOrdersRequest", func(t *testing.T) {
// market buy
req, err := client.NewGetHistoryOrdersRequest().Symbol("APEUSDT").Do(ctx)
startTime := time.Now().Add(-30 * 24 * time.Hour)
req, err := client.NewGetHistoryOrdersRequest().Symbol("APEUSDT").StartTime(startTime).Do(ctx)
assert.NoError(t, err)
t.Logf("place order resp: %+v", req)
@ -61,7 +62,8 @@ func TestClient(t *testing.T) {
})
t.Run("GetTradeFillsRequest", func(t *testing.T) {
req, err := client.NewGetTradeFillsRequest().Symbol("APEUSDT").Do(ctx)
startTime := time.Now().Add(-30 * 24 * time.Hour)
req, err := client.NewGetTradeFillsRequest().Symbol("APEUSDT").StartTime(startTime).Do(ctx)
assert.NoError(t, err)
t.Logf("get trade fills resp: %+v", req)

View File

@ -6,6 +6,7 @@ package bitgetapi
import (
"encoding/json"
"fmt"
"time"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
@ -91,10 +92,10 @@ type GetHistoryOrdersRequest struct {
// Limit number default 100 max 100
limit *string `param:"limit,query"`
// idLessThan requests the content on the page before this ID (older data), the value input should be the orderId of the corresponding interface.
idLessThan *string `param:"idLessThan,query"`
startTime *int64 `param:"startTime,query"`
endTime *int64 `param:"endTime,query"`
orderId *string `param:"orderId,query"`
idLessThan *string `param:"idLessThan,query"`
startTime *time.Time `param:"startTime,milliseconds,query"`
endTime *time.Time `param:"endTime,milliseconds,query"`
orderId *string `param:"orderId,query"`
}
func (c *Client) NewGetHistoryOrdersRequest() *GetHistoryOrdersRequest {

View File

@ -10,6 +10,8 @@ import (
"net/url"
"reflect"
"regexp"
"strconv"
"time"
)
func (g *GetHistoryOrdersRequest) Symbol(symbol string) *GetHistoryOrdersRequest {
@ -27,12 +29,12 @@ func (g *GetHistoryOrdersRequest) IdLessThan(idLessThan string) *GetHistoryOrder
return g
}
func (g *GetHistoryOrdersRequest) StartTime(startTime int64) *GetHistoryOrdersRequest {
func (g *GetHistoryOrdersRequest) StartTime(startTime time.Time) *GetHistoryOrdersRequest {
g.startTime = &startTime
return g
}
func (g *GetHistoryOrdersRequest) EndTime(endTime int64) *GetHistoryOrdersRequest {
func (g *GetHistoryOrdersRequest) EndTime(endTime time.Time) *GetHistoryOrdersRequest {
g.endTime = &endTime
return g
}
@ -74,7 +76,8 @@ func (g *GetHistoryOrdersRequest) GetQueryParameters() (url.Values, error) {
startTime := *g.startTime
// assign parameter of startTime
params["startTime"] = startTime
// convert time.Time to milliseconds time stamp
params["startTime"] = strconv.FormatInt(startTime.UnixNano()/int64(time.Millisecond), 10)
} else {
}
// check endTime field -> json key endTime
@ -82,7 +85,8 @@ func (g *GetHistoryOrdersRequest) GetQueryParameters() (url.Values, error) {
endTime := *g.endTime
// assign parameter of endTime
params["endTime"] = endTime
// convert time.Time to milliseconds time stamp
params["endTime"] = strconv.FormatInt(endTime.UnixNano()/int64(time.Millisecond), 10)
} else {
}
// check orderId field -> json key orderId

View File

@ -1,6 +1,8 @@
package bitgetapi
import (
"time"
"github.com/c9s/requestgen"
"github.com/c9s/bbgo/pkg/fixedpoint"
@ -59,10 +61,10 @@ type GetTradeFillsRequest struct {
// Limit number default 100 max 100
limit *string `param:"limit,query"`
// idLessThan requests the content on the page before this ID (older data), the value input should be the orderId of the corresponding interface.
idLessThan *string `param:"idLessThan,query"`
startTime *int64 `param:"startTime,query"`
endTime *int64 `param:"endTime,query"`
orderId *string `param:"orderId,query"`
idLessThan *string `param:"idLessThan,query"`
startTime *time.Time `param:"startTime,milliseconds,query"`
endTime *time.Time `param:"endTime,milliseconds,query"`
orderId *string `param:"orderId,query"`
}
func (s *Client) NewGetTradeFillsRequest() *GetTradeFillsRequest {

View File

@ -10,6 +10,8 @@ import (
"net/url"
"reflect"
"regexp"
"strconv"
"time"
)
func (s *GetTradeFillsRequest) Symbol(symbol string) *GetTradeFillsRequest {
@ -27,12 +29,12 @@ func (s *GetTradeFillsRequest) IdLessThan(idLessThan string) *GetTradeFillsReque
return s
}
func (s *GetTradeFillsRequest) StartTime(startTime int64) *GetTradeFillsRequest {
func (s *GetTradeFillsRequest) StartTime(startTime time.Time) *GetTradeFillsRequest {
s.startTime = &startTime
return s
}
func (s *GetTradeFillsRequest) EndTime(endTime int64) *GetTradeFillsRequest {
func (s *GetTradeFillsRequest) EndTime(endTime time.Time) *GetTradeFillsRequest {
s.endTime = &endTime
return s
}
@ -71,7 +73,8 @@ func (s *GetTradeFillsRequest) GetQueryParameters() (url.Values, error) {
startTime := *s.startTime
// assign parameter of startTime
params["startTime"] = startTime
// convert time.Time to milliseconds time stamp
params["startTime"] = strconv.FormatInt(startTime.UnixNano()/int64(time.Millisecond), 10)
} else {
}
// check endTime field -> json key endTime
@ -79,7 +82,8 @@ func (s *GetTradeFillsRequest) GetQueryParameters() (url.Values, error) {
endTime := *s.endTime
// assign parameter of endTime
params["endTime"] = endTime
// convert time.Time to milliseconds time stamp
params["endTime"] = strconv.FormatInt(endTime.UnixNano()/int64(time.Millisecond), 10)
} else {
}
// check orderId field -> json key orderId
@ -185,6 +189,12 @@ func (s *GetTradeFillsRequest) GetSlugsMap() (map[string]string, error) {
return slugs, nil
}
// GetPath returns the request path of the API
func (s *GetTradeFillsRequest) GetPath() string {
return "/api/v2/spot/trade/fills"
}
// Do generates the request object and send the request object to the API endpoint
func (s *GetTradeFillsRequest) Do(ctx context.Context) ([]Trade, error) {
// no body params
@ -194,7 +204,9 @@ func (s *GetTradeFillsRequest) Do(ctx context.Context) ([]Trade, error) {
return nil, err
}
apiURL := "/api/v2/spot/trade/fills"
var apiURL string
apiURL = s.GetPath()
req, err := s.client.NewAuthenticatedRequest(ctx, "GET", apiURL, query, params)
if err != nil {
@ -211,6 +223,15 @@ func (s *GetTradeFillsRequest) Do(ctx context.Context) ([]Trade, error) {
return nil, err
}
type responseValidator interface {
Validate() error
}
validator, ok := interface{}(apiResponse).(responseValidator)
if ok {
if err := validator.Validate(); err != nil {
return nil, err
}
}
var data []Trade
if err := json.Unmarshal(apiResponse.Data, &data); err != nil {
return nil, err

View File

@ -4,6 +4,8 @@ package bitgetapi
//go:generate -command PostRequest requestgen -method POST -responseType .APIResponse -responseDataField Data
import (
"time"
"github.com/c9s/requestgen"
"github.com/c9s/bbgo/pkg/fixedpoint"
@ -39,10 +41,10 @@ type GetUnfilledOrdersRequest struct {
// Limit number default 100 max 100
limit *string `param:"limit,query"`
// idLessThan requests the content on the page before this ID (older data), the value input should be the orderId of the corresponding interface.
idLessThan *string `param:"idLessThan,query"`
startTime *int64 `param:"startTime,query"`
endTime *int64 `param:"endTime,query"`
orderId *string `param:"orderId,query"`
idLessThan *string `param:"idLessThan,query"`
startTime *time.Time `param:"startTime,milliseconds,query"`
endTime *time.Time `param:"endTime,milliseconds,query"`
orderId *string `param:"orderId,query"`
}
func (c *Client) NewGetUnfilledOrdersRequest() *GetUnfilledOrdersRequest {

View File

@ -10,6 +10,8 @@ import (
"net/url"
"reflect"
"regexp"
"strconv"
"time"
)
func (g *GetUnfilledOrdersRequest) Symbol(symbol string) *GetUnfilledOrdersRequest {
@ -27,12 +29,12 @@ func (g *GetUnfilledOrdersRequest) IdLessThan(idLessThan string) *GetUnfilledOrd
return g
}
func (g *GetUnfilledOrdersRequest) StartTime(startTime int64) *GetUnfilledOrdersRequest {
func (g *GetUnfilledOrdersRequest) StartTime(startTime time.Time) *GetUnfilledOrdersRequest {
g.startTime = &startTime
return g
}
func (g *GetUnfilledOrdersRequest) EndTime(endTime int64) *GetUnfilledOrdersRequest {
func (g *GetUnfilledOrdersRequest) EndTime(endTime time.Time) *GetUnfilledOrdersRequest {
g.endTime = &endTime
return g
}
@ -74,7 +76,8 @@ func (g *GetUnfilledOrdersRequest) GetQueryParameters() (url.Values, error) {
startTime := *g.startTime
// assign parameter of startTime
params["startTime"] = startTime
// convert time.Time to milliseconds time stamp
params["startTime"] = strconv.FormatInt(startTime.UnixNano()/int64(time.Millisecond), 10)
} else {
}
// check endTime field -> json key endTime
@ -82,7 +85,8 @@ func (g *GetUnfilledOrdersRequest) GetQueryParameters() (url.Values, error) {
endTime := *g.endTime
// assign parameter of endTime
params["endTime"] = endTime
// convert time.Time to milliseconds time stamp
params["endTime"] = strconv.FormatInt(endTime.UnixNano()/int64(time.Millisecond), 10)
} else {
}
// check orderId field -> json key orderId
@ -188,6 +192,12 @@ func (g *GetUnfilledOrdersRequest) GetSlugsMap() (map[string]string, error) {
return slugs, nil
}
// GetPath returns the request path of the API
func (g *GetUnfilledOrdersRequest) GetPath() string {
return "/api/v2/spot/trade/unfilled-orders"
}
// Do generates the request object and send the request object to the API endpoint
func (g *GetUnfilledOrdersRequest) Do(ctx context.Context) ([]UnfilledOrder, error) {
// no body params
@ -197,7 +207,9 @@ func (g *GetUnfilledOrdersRequest) Do(ctx context.Context) ([]UnfilledOrder, err
return nil, err
}
apiURL := "/api/v2/spot/trade/unfilled-orders"
var apiURL string
apiURL = g.GetPath()
req, err := g.client.NewAuthenticatedRequest(ctx, "GET", apiURL, query, params)
if err != nil {
@ -213,6 +225,16 @@ func (g *GetUnfilledOrdersRequest) Do(ctx context.Context) ([]UnfilledOrder, err
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
type responseValidator interface {
Validate() error
}
validator, ok := interface{}(apiResponse).(responseValidator)
if ok {
if err := validator.Validate(); err != nil {
return nil, err
}
}
var data []UnfilledOrder
if err := json.Unmarshal(apiResponse.Data, &data); err != nil {
return nil, err

View File

@ -420,8 +420,8 @@ func (e *Exchange) QueryClosedOrders(ctx context.Context, symbol string, since,
res, err := e.v2client.NewGetHistoryOrdersRequest().
Symbol(symbol).
Limit(strconv.Itoa(queryLimit)).
StartTime(since.UnixMilli()).
EndTime(until.UnixMilli()).
StartTime(since).
EndTime(until).
Do(ctx)
if err != nil {
return nil, fmt.Errorf("failed to call get order histories error: %w", err)
@ -512,7 +512,7 @@ func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *type
if time.Since(*options.StartTime) > queryMaxDuration {
return nil, fmt.Errorf("start time from the last 90 days can be queried, got: %s", options.StartTime)
}
req.StartTime(options.StartTime.UnixMilli())
req.StartTime(*options.StartTime)
}
if options.EndTime != nil {
@ -525,7 +525,7 @@ func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *type
if options.EndTime.Sub(*options.StartTime) > queryMaxDuration {
return nil, fmt.Errorf("start time %s and end time %s cannot greater than 90 days", options.StartTime, options.EndTime)
}
req.EndTime(options.EndTime.UnixMilli())
req.EndTime(*options.EndTime)
}
limit := options.Limit