Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: [max] replace deprecated max v3 API #1166

Merged
merged 6 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions pkg/exchange/max/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Exchange struct {
key, secret string
client *maxapi.RestClient

v3order *v3.OrderService
v3client *v3.Client
v3margin *v3.MarginService

submitOrderLimiter, queryTradeLimiter, accountQueryLimiter, closedOrderQueryLimiter, marketDataLimiter *rate.Limiter
Expand All @@ -47,7 +47,7 @@ func New(key, secret string) *Exchange {
key: key,
// pragma: allowlist nextline secret
secret: secret,
v3order: &v3.OrderService{Client: client},
v3client: &v3.Client{Client: client},
v3margin: &v3.MarginService{Client: client},

queryTradeLimiter: rate.NewLimiter(rate.Every(1*time.Second), 2),
Expand Down Expand Up @@ -182,7 +182,7 @@ func (e *Exchange) QueryOrderTrades(ctx context.Context, q types.OrderQuery) ([]
return nil, err
}

maxTrades, err := e.v3order.NewGetOrderTradesRequest().OrderID(uint64(orderID)).Do(ctx)
maxTrades, err := e.v3client.NewGetOrderTradesRequest().OrderID(uint64(orderID)).Do(ctx)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -218,7 +218,7 @@ func (e *Exchange) QueryOrder(ctx context.Context, q types.OrderQuery) (*types.O
return nil, errors.New("max.QueryOrder: only accept one parameter of OrderID/ClientOrderID")
}

request := e.v3order.NewGetOrderRequest()
request := e.v3client.NewGetOrderRequest()

if len(q.OrderID) != 0 {
orderID, err := strconv.ParseInt(q.OrderID, 10, 64)
Expand Down Expand Up @@ -248,7 +248,7 @@ func (e *Exchange) QueryOpenOrders(ctx context.Context, symbol string) (orders [
walletType = maxapi.WalletTypeMargin
}

maxOrders, err := e.v3order.NewGetWalletOpenOrdersRequest(walletType).Market(market).Do(ctx)
maxOrders, err := e.v3client.NewGetWalletOpenOrdersRequest(walletType).Market(market).Do(ctx)
if err != nil {
return orders, err
}
Expand Down Expand Up @@ -282,7 +282,7 @@ func (e *Exchange) queryClosedOrdersByLastOrderID(ctx context.Context, symbol st
walletType = maxapi.WalletTypeMargin
}

req := e.v3order.NewGetWalletOrderHistoryRequest(walletType).Market(market)
req := e.v3client.NewGetWalletOrderHistoryRequest(walletType).Market(market)
if lastOrderID == 0 {
lastOrderID = 1
}
Expand Down Expand Up @@ -315,7 +315,7 @@ func (e *Exchange) CancelAllOrders(ctx context.Context) ([]types.Order, error) {
walletType = maxapi.WalletTypeMargin
}

req := e.v3order.NewCancelWalletOrderAllRequest(walletType)
req := e.v3client.NewCancelWalletOrderAllRequest(walletType)
var orderResponses, err = req.Do(ctx)
if err != nil {
return nil, err
Expand All @@ -338,7 +338,7 @@ func (e *Exchange) CancelOrdersBySymbol(ctx context.Context, symbol string) ([]t
walletType = maxapi.WalletTypeMargin
}

req := e.v3order.NewCancelWalletOrderAllRequest(walletType)
req := e.v3client.NewCancelWalletOrderAllRequest(walletType)
req.Market(market)

var orderResponses, err = req.Do(ctx)
Expand All @@ -362,7 +362,7 @@ func (e *Exchange) CancelOrdersByGroupID(ctx context.Context, groupID uint32) ([
walletType = maxapi.WalletTypeMargin
}

req := e.v3order.NewCancelWalletOrderAllRequest(walletType)
req := e.v3client.NewCancelWalletOrderAllRequest(walletType)
req.GroupID(groupID)

var orderResponses, err = req.Do(ctx)
Expand Down Expand Up @@ -398,7 +398,7 @@ func (e *Exchange) CancelOrders(ctx context.Context, orders ...types.Order) (err

if len(groupIDs) > 0 {
for groupID := range groupIDs {
req := e.v3order.NewCancelWalletOrderAllRequest(walletType)
req := e.v3client.NewCancelWalletOrderAllRequest(walletType)
req.GroupID(groupID)

if _, err := req.Do(ctx); err != nil {
Expand All @@ -409,7 +409,7 @@ func (e *Exchange) CancelOrders(ctx context.Context, orders ...types.Order) (err
}

for _, o := range orphanOrders {
req := e.v3order.NewCancelOrderRequest()
req := e.v3client.NewCancelOrderRequest()
if o.OrderID > 0 {
req.Id(o.OrderID)
} else if len(o.ClientOrderID) > 0 && o.ClientOrderID != types.NoClientOrderID {
Expand Down Expand Up @@ -498,7 +498,7 @@ func (e *Exchange) SubmitOrder(ctx context.Context, order types.SubmitOrder) (cr

clientOrderID := NewClientOrderID(o.ClientOrderID)

req := e.v3order.NewCreateWalletOrderRequest(walletType)
req := e.v3client.NewCreateWalletOrderRequest(walletType)
req.Market(toLocalSymbol(o.Symbol)).
Side(toLocalSideType(o.Side)).
Volume(quantityString).
Expand Down Expand Up @@ -590,7 +590,7 @@ func (e *Exchange) QueryAccount(ctx context.Context) (*types.Account, error) {
if e.MarginSettings.IsMargin {
a.AccountType = types.AccountTypeMargin

req := e.v3margin.NewGetMarginADRatioRequest()
req := e.v3client.NewGetMarginADRatioRequest()
adRatio, err := req.Do(ctx)
if err != nil {
return a, err
Expand All @@ -613,7 +613,7 @@ func (e *Exchange) QueryAccountBalances(ctx context.Context) (types.BalanceMap,
walletType = maxapi.WalletTypeMargin
}

req := e.v3order.NewGetWalletAccountsRequest(walletType)
req := e.v3client.NewGetWalletAccountsRequest(walletType)
accounts, err := req.Do(ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -818,7 +818,7 @@ func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *type
walletType = maxapi.WalletTypeMargin
}

req := e.v3order.NewGetWalletTradesRequest(walletType)
req := e.v3client.NewGetWalletTradesRequest(walletType)
req.Market(market)

if options.Limit > 0 {
Expand Down Expand Up @@ -977,7 +977,7 @@ func (e *Exchange) QueryAveragePrice(ctx context.Context, symbol string) (fixedp
}

func (e *Exchange) RepayMarginAsset(ctx context.Context, asset string, amount fixedpoint.Value) error {
req := e.v3margin.NewMarginRepayRequest()
req := e.v3client.NewMarginRepayRequest()
req.Currency(toLocalCurrency(asset))
req.Amount(amount.String())
resp, err := req.Do(ctx)
Expand All @@ -990,7 +990,7 @@ func (e *Exchange) RepayMarginAsset(ctx context.Context, asset string, amount fi
}

func (e *Exchange) BorrowMarginAsset(ctx context.Context, asset string, amount fixedpoint.Value) error {
req := e.v3margin.NewMarginLoanRequest()
req := e.v3client.NewMarginLoanRequest()
req.Currency(toLocalCurrency(asset))
req.Amount(amount.String())
resp, err := req.Do(ctx)
Expand All @@ -1003,7 +1003,7 @@ func (e *Exchange) BorrowMarginAsset(ctx context.Context, asset string, amount f
}

func (e *Exchange) QueryMarginAssetMaxBorrowable(ctx context.Context, asset string) (amount fixedpoint.Value, err error) {
req := e.v3margin.NewGetMarginBorrowingLimitsRequest()
req := e.v3client.NewGetMarginBorrowingLimitsRequest()
resp, err := req.Do(ctx)
if err != nil {
return fixedpoint.Zero, err
Expand Down
8 changes: 4 additions & 4 deletions pkg/exchange/max/maxapi/v3/cancel_order_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ package v3

import "github.com/c9s/requestgen"

func (s *OrderService) NewCancelOrderRequest() *CancelOrderRequest {
return &CancelOrderRequest{client: s.Client}
}

//go:generate DeleteRequest -url "/api/v3/order" -type CancelOrderRequest -responseType .Order
type CancelOrderRequest struct {
client requestgen.AuthenticatedAPIClient

id *uint64 `param:"id,omitempty"`
clientOrderID *string `param:"client_oid,omitempty"`
}

func (s *Client) NewCancelOrderRequest() *CancelOrderRequest {
return &CancelOrderRequest{client: s.Client}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type OrderCancelResponse struct {
Error *string
}

func (s *OrderService) NewCancelWalletOrderAllRequest(walletType WalletType) *CancelWalletOrderAllRequest {
func (s *Client) NewCancelWalletOrderAllRequest(walletType WalletType) *CancelWalletOrderAllRequest {
return &CancelWalletOrderAllRequest{client: s.Client, walletType: walletType}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/exchange/max/maxapi/v3/create_wallet_order_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ type CreateWalletOrderRequest struct {
market string `param:"market,required"`
side string `param:"side,required"`
volume string `param:"volume,required"`
orderType OrderType `param:"ord_type"`
orderType OrderType `param:"ord_type"`

price *string `param:"price"`
stopPrice *string `param:"stop_price"`
clientOrderID *string `param:"client_oid"`
groupID *string `param:"group_id"`
}

func (s *OrderService) NewCreateWalletOrderRequest(walletType WalletType) *CreateWalletOrderRequest {
func (s *Client) NewCreateWalletOrderRequest(walletType WalletType) *CreateWalletOrderRequest {
return &CreateWalletOrderRequest{client: s.Client, walletType: walletType}
}
8 changes: 4 additions & 4 deletions pkg/exchange/max/maxapi/v3/get_margin_ad_ratio_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ import (
//go:generate -command PostRequest requestgen -method POST
//go:generate -command DeleteRequest requestgen -method DELETE

func (s *MarginService) NewGetMarginADRatioRequest() *GetMarginADRatioRequest {
return &GetMarginADRatioRequest{client: s.Client}
}

type ADRatio struct {
AdRatio fixedpoint.Value `json:"ad_ratio"`
AssetInUsdt fixedpoint.Value `json:"asset_in_usdt"`
Expand All @@ -24,3 +20,7 @@ type ADRatio struct {
type GetMarginADRatioRequest struct {
client requestgen.AuthenticatedAPIClient
}

func (s *Client) NewGetMarginADRatioRequest() *GetMarginADRatioRequest {
return &GetMarginADRatioRequest{client: s.Client}
}
35 changes: 35 additions & 0 deletions pkg/exchange/max/maxapi/v3/get_margin_interest_history_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package v3

//go:generate -command GetRequest requestgen -method GET
//go:generate -command PostRequest requestgen -method POST
//go:generate -command DeleteRequest requestgen -method DELETE

import (
"time"

"github.com/c9s/requestgen"

"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)

type MarginInterestRecord struct {
Currency string `json:"currency"`
Amount fixedpoint.Value `json:"amount"`
InterestRate fixedpoint.Value `json:"interest_rate"`
CreatedAt types.MillisecondTimestamp `json:"created_at"`
}

//go:generate GetRequest -url "/api/v3/wallet/m/interests" -type GetMarginInterestHistoryRequest -responseType []MarginInterestRecord
type GetMarginInterestHistoryRequest struct {
client requestgen.AuthenticatedAPIClient

currency string `param:"currency,required"`
startTime *time.Time `param:"startTime,milliseconds"`
endTime *time.Time `param:"endTime,milliseconds"`
limit *int `param:"limit"`
}

func (s *Client) NewGetMarginInterestHistoryRequest(currency string) *GetMarginInterestHistoryRequest {
return &GetMarginInterestHistoryRequest{client: s.Client, currency: currency}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions pkg/exchange/max/maxapi/v3/get_margin_interest_rates_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package v3

//go:generate -command GetRequest requestgen -method GET
//go:generate -command PostRequest requestgen -method POST
//go:generate -command DeleteRequest requestgen -method DELETE

import (
"github.com/c9s/requestgen"

"github.com/c9s/bbgo/pkg/fixedpoint"
)

type MarginInterestRate struct {
HourlyInterestRate fixedpoint.Value `json:"hourly_interest_rate"`
NextHourlyInterestRate fixedpoint.Value `json:"next_hourly_interest_rate"`
}

type MarginInterestRateMap map[string]MarginInterestRate

//go:generate GetRequest -url "/api/v3/wallet/m/interest_rates" -type GetMarginInterestRatesRequest -responseType .MarginInterestRateMap
type GetMarginInterestRatesRequest struct {
client requestgen.APIClient
}

func (s *Client) NewGetMarginInterestRatesRequest() *GetMarginInterestRatesRequest {
return &GetMarginInterestRatesRequest{client: s.Client}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading