Merge pull request #1664 from c9s/feature/max/get-trades-api

FEATURE: update max api to latest version
This commit is contained in:
kbearXD 2024-07-01 17:01:50 +08:00 committed by GitHub
commit a6aef35393
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 57 additions and 52 deletions

View File

@ -985,15 +985,10 @@ func (e *Exchange) QueryDepositHistory(
// QueryTrades // QueryTrades
// For MAX API spec // For MAX API spec
// start_time and end_time need to be within 3 days // give from_id -> query trades from this id and order by asc
// without any parameters -> return trades within 24 hours // give timestamp and order is asc -> query trades after timestamp and order by asc
// give start_time or end_time -> ignore parameter from_id // give timestamp and order is desc -> query trades before timestamp and order by desc
// give start_time or from_id -> order by time asc
// give end_time -> order by time desc
// limit should b1 1~1000 // limit should b1 1~1000
// For this QueryTrades spec (to be compatible with batch.TradeBatchQuery)
// give LastTradeID -> ignore start_time (but still can filter the end_time)
// without any parameters -> return trades within 24 hours
func (e *Exchange) QueryTrades( func (e *Exchange) QueryTrades(
ctx context.Context, symbol string, options *types.TradeQueryOptions, ctx context.Context, symbol string, options *types.TradeQueryOptions,
) (trades []types.Trade, err error) { ) (trades []types.Trade, err error) {
@ -1020,23 +1015,15 @@ func (e *Exchange) QueryTrades(
// However, we want to use from_id as main parameter for batch.TradeBatchQuery // However, we want to use from_id as main parameter for batch.TradeBatchQuery
if options.LastTradeID > 0 { if options.LastTradeID > 0 {
// MAX uses inclusive last trade ID // MAX uses inclusive last trade ID
req.From(options.LastTradeID) req.FromID(options.LastTradeID)
req.Order("asc")
} else { } else {
// option's start_time and end_time need to be within 3 days if options.StartTime != nil {
// so if the start_time and end_time is over 3 days, we make end_time down to start_time + 3 days req.Timestamp(*options.StartTime)
if options.StartTime != nil && options.EndTime != nil { req.Order("asc")
endTime := *options.EndTime
startTime := *options.StartTime
if endTime.Sub(startTime) > 72*time.Hour {
startTime := *options.StartTime
endTime = startTime.Add(72 * time.Hour)
}
req.StartTime(startTime)
req.EndTime(endTime)
} else if options.StartTime != nil {
req.StartTime(*options.StartTime)
} else if options.EndTime != nil { } else if options.EndTime != nil {
req.EndTime(*options.EndTime) req.Timestamp(*options.EndTime)
req.Order("desc")
} }
} }
@ -1046,6 +1033,14 @@ func (e *Exchange) QueryTrades(
} }
for _, t := range maxTrades { for _, t := range maxTrades {
if options.StartTime != nil && options.StartTime.After(t.CreatedAt.Time()) {
continue
}
if options.EndTime != nil && options.EndTime.Before(t.CreatedAt.Time()) {
continue
}
localTrades, err := toGlobalTradeV3(t) localTrades, err := toGlobalTradeV3(t)
if err != nil { if err != nil {
log.WithError(err).Errorf("can not convert trade: %+v", t) log.WithError(err).Errorf("can not convert trade: %+v", t)

View File

@ -14,15 +14,15 @@ func (s *Client) NewGetWalletTradesRequest(walletType WalletType) *GetWalletTrad
return &GetWalletTradesRequest{client: s.Client, walletType: walletType} return &GetWalletTradesRequest{client: s.Client, walletType: walletType}
} }
//go:generate GetRequest -url "/api/v3/wallet/:walletType/trades" -type GetWalletTradesRequest -responseType []Trade //go:generate GetRequest -url "/api/v3/wallet/:walletType/new/trades" -type GetWalletTradesRequest -responseType []Trade
type GetWalletTradesRequest struct { type GetWalletTradesRequest struct {
client requestgen.AuthenticatedAPIClient client requestgen.AuthenticatedAPIClient
walletType WalletType `param:"walletType,slug,required"` walletType WalletType `param:"walletType,slug,required"`
market string `param:"market,required"` market string `param:"market,required"`
from *uint64 `param:"from_id"` timestamp *time.Time `param:"timestamp,milliseconds,omitempty"`
startTime *time.Time `param:"start_time,milliseconds"` fromID *uint64 `param:"from_id,omitempty"`
endTime *time.Time `param:"end_time,milliseconds"` order *string `param:"order,omitempty" validValues:"asc,desc"`
limit *uint64 `param:"limit"` limit *uint64 `param:"limit"`
} }

View File

@ -1,4 +1,4 @@
// Code generated by "requestgen -method GET -url /api/v3/wallet/:walletType/trades -type GetWalletTradesRequest -responseType []Trade"; DO NOT EDIT. // Code generated by "requestgen -debug -method GET -url /api/v3/wallet/:walletType/new/trades -type GetWalletTradesRequest -responseType []"github.com/c9s/bbgo/pkg/exchange/max/maxapi/v3".Trade"; DO NOT EDIT.
package v3 package v3
@ -18,18 +18,18 @@ func (g *GetWalletTradesRequest) Market(market string) *GetWalletTradesRequest {
return g return g
} }
func (g *GetWalletTradesRequest) From(from uint64) *GetWalletTradesRequest { func (g *GetWalletTradesRequest) Timestamp(timestamp time.Time) *GetWalletTradesRequest {
g.from = &from g.timestamp = &timestamp
return g return g
} }
func (g *GetWalletTradesRequest) StartTime(startTime time.Time) *GetWalletTradesRequest { func (g *GetWalletTradesRequest) FromID(fromID uint64) *GetWalletTradesRequest {
g.startTime = &startTime g.fromID = &fromID
return g return g
} }
func (g *GetWalletTradesRequest) EndTime(endTime time.Time) *GetWalletTradesRequest { func (g *GetWalletTradesRequest) Order(order string) *GetWalletTradesRequest {
g.endTime = &endTime g.order = &order
return g return g
} }
@ -69,30 +69,40 @@ func (g *GetWalletTradesRequest) GetParameters() (map[string]interface{}, error)
// assign parameter of market // assign parameter of market
params["market"] = market params["market"] = market
// check from field -> json key from_id // check timestamp field -> json key timestamp
if g.from != nil { if g.timestamp != nil {
from := *g.from timestamp := *g.timestamp
// assign parameter of from // assign parameter of timestamp
params["from_id"] = from // convert time.Time to milliseconds time stamp
params["timestamp"] = strconv.FormatInt(timestamp.UnixNano()/int64(time.Millisecond), 10)
} else { } else {
} }
// check startTime field -> json key start_time // check fromID field -> json key from_id
if g.startTime != nil { if g.fromID != nil {
startTime := *g.startTime fromID := *g.fromID
// assign parameter of startTime // assign parameter of fromID
// convert time.Time to milliseconds time stamp params["from_id"] = fromID
params["start_time"] = strconv.FormatInt(startTime.UnixNano()/int64(time.Millisecond), 10)
} else { } else {
} }
// check endTime field -> json key end_time // check order field -> json key order
if g.endTime != nil { if g.order != nil {
endTime := *g.endTime order := *g.order
// assign parameter of endTime // TEMPLATE check-valid-values
// convert time.Time to milliseconds time stamp switch order {
params["end_time"] = strconv.FormatInt(endTime.UnixNano()/int64(time.Millisecond), 10) case "asc", "desc":
params["order"] = order
default:
return nil, fmt.Errorf("order value %v is invalid", order)
}
// END TEMPLATE check-valid-values
// assign parameter of order
params["order"] = order
} else { } else {
} }
// check limit field -> json key limit // check limit field -> json key limit
@ -206,7 +216,7 @@ func (g *GetWalletTradesRequest) Do(ctx context.Context) ([]Trade, error) {
return nil, err return nil, err
} }
apiURL := "/api/v3/wallet/:walletType/trades" apiURL := "/api/v3/wallet/:walletType/new/trades"
slugs, err := g.GetSlugsMap() slugs, err := g.GetSlugsMap()
if err != nil { if err != nil {
return nil, err return nil, err