FEATURE: update max api to latest version

This commit is contained in:
kbearXD 2024-06-28 16:32:24 +08:00
parent ad5674d9cb
commit e63158f5fa
3 changed files with 57 additions and 52 deletions

View File

@ -985,15 +985,10 @@ func (e *Exchange) QueryDepositHistory(
// QueryTrades
// For MAX API spec
// start_time and end_time need to be within 3 days
// without any parameters -> return trades within 24 hours
// give start_time or end_time -> ignore parameter from_id
// give start_time or from_id -> order by time asc
// give end_time -> order by time desc
// give from_id -> query trades from this id and order by asc
// give timestamp and order is asc -> query trades after timestamp and order by asc
// give timestamp and order is desc -> query trades before timestamp and order by desc
// 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(
ctx context.Context, symbol string, options *types.TradeQueryOptions,
) (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
if options.LastTradeID > 0 {
// MAX uses inclusive last trade ID
req.From(options.LastTradeID)
req.FromID(options.LastTradeID)
req.Order("asc")
} else {
// option's start_time and end_time need to be within 3 days
// so if the start_time and end_time is over 3 days, we make end_time down to start_time + 3 days
if options.StartTime != nil && options.EndTime != nil {
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)
if options.StartTime != nil {
req.Timestamp(*options.StartTime)
req.Order("asc")
} 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 {
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)
if err != nil {
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}
}
//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 {
client requestgen.AuthenticatedAPIClient
walletType WalletType `param:"walletType,slug,required"`
market string `param:"market,required"`
from *uint64 `param:"from_id"`
startTime *time.Time `param:"start_time,milliseconds"`
endTime *time.Time `param:"end_time,milliseconds"`
timestamp *time.Time `param:"timestamp,milliseconds,omitempty"`
fromID *uint64 `param:"from_id,omitempty"`
order *string `param:"order,omitempty" validValues:"asc,desc"`
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
@ -18,18 +18,18 @@ func (g *GetWalletTradesRequest) Market(market string) *GetWalletTradesRequest {
return g
}
func (g *GetWalletTradesRequest) From(from uint64) *GetWalletTradesRequest {
g.from = &from
func (g *GetWalletTradesRequest) Timestamp(timestamp time.Time) *GetWalletTradesRequest {
g.timestamp = &timestamp
return g
}
func (g *GetWalletTradesRequest) StartTime(startTime time.Time) *GetWalletTradesRequest {
g.startTime = &startTime
func (g *GetWalletTradesRequest) FromID(fromID uint64) *GetWalletTradesRequest {
g.fromID = &fromID
return g
}
func (g *GetWalletTradesRequest) EndTime(endTime time.Time) *GetWalletTradesRequest {
g.endTime = &endTime
func (g *GetWalletTradesRequest) Order(order string) *GetWalletTradesRequest {
g.order = &order
return g
}
@ -69,30 +69,40 @@ func (g *GetWalletTradesRequest) GetParameters() (map[string]interface{}, error)
// assign parameter of market
params["market"] = market
// check from field -> json key from_id
if g.from != nil {
from := *g.from
// check timestamp field -> json key timestamp
if g.timestamp != nil {
timestamp := *g.timestamp
// assign parameter of from
params["from_id"] = from
// assign parameter of timestamp
// convert time.Time to milliseconds time stamp
params["timestamp"] = strconv.FormatInt(timestamp.UnixNano()/int64(time.Millisecond), 10)
} else {
}
// check startTime field -> json key start_time
if g.startTime != nil {
startTime := *g.startTime
// check fromID field -> json key from_id
if g.fromID != nil {
fromID := *g.fromID
// assign parameter of startTime
// convert time.Time to milliseconds time stamp
params["start_time"] = strconv.FormatInt(startTime.UnixNano()/int64(time.Millisecond), 10)
// assign parameter of fromID
params["from_id"] = fromID
} else {
}
// check endTime field -> json key end_time
if g.endTime != nil {
endTime := *g.endTime
// check order field -> json key order
if g.order != nil {
order := *g.order
// assign parameter of endTime
// convert time.Time to milliseconds time stamp
params["end_time"] = strconv.FormatInt(endTime.UnixNano()/int64(time.Millisecond), 10)
// TEMPLATE check-valid-values
switch order {
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 {
}
// check limit field -> json key limit
@ -206,7 +216,7 @@ func (g *GetWalletTradesRequest) Do(ctx context.Context) ([]Trade, error) {
return nil, err
}
apiURL := "/api/v3/wallet/:walletType/trades"
apiURL := "/api/v3/wallet/:walletType/new/trades"
slugs, err := g.GetSlugsMap()
if err != nil {
return nil, err