max: split v3 api into files

This commit is contained in:
c9s 2023-05-04 14:37:19 +08:00
parent f686a6de83
commit 70e3f8ec5f
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
18 changed files with 176 additions and 137 deletions

View File

@ -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
@ -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),
@ -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
}
@ -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)
@ -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
}
@ -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
}
@ -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
@ -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)
@ -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)
@ -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 {
@ -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 {
@ -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).
@ -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
@ -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
@ -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 {
@ -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)
@ -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)
@ -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

View File

@ -6,10 +6,6 @@ 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
@ -17,3 +13,7 @@ type CancelOrderRequest struct {
id *uint64 `param:"id,omitempty"`
clientOrderID *string `param:"client_oid,omitempty"`
}
func (s *Client) NewCancelOrderRequest() *CancelOrderRequest {
return &CancelOrderRequest{client: s.Client}
}

View File

@ -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}
}

View File

@ -14,7 +14,7 @@ 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"`
@ -22,6 +22,6 @@ type CreateWalletOrderRequest struct {
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}
}

View File

@ -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"`
@ -24,3 +20,7 @@ type ADRatio struct {
type GetMarginADRatioRequest struct {
client requestgen.AuthenticatedAPIClient
}
func (s *Client) NewGetMarginADRatioRequest() *GetMarginADRatioRequest {
return &GetMarginADRatioRequest{client: s.Client}
}

View File

@ -0,0 +1,38 @@
package v3
import (
"time"
"github.com/c9s/requestgen"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
func (s *Client) NewGetMarginLiquidationHistoryRequest() *GetMarginLiquidationHistoryRequest {
return &GetMarginLiquidationHistoryRequest{client: s.Client}
}
type LiquidationRecord struct {
SN string `json:"sn"`
AdRatio fixedpoint.Value `json:"ad_ratio"`
ExpectedAdRatio fixedpoint.Value `json:"expected_ad_ratio"`
CreatedAt types.MillisecondTimestamp `json:"created_at"`
State LiquidationState `json:"state"`
}
type LiquidationState string
const (
LiquidationStateProcessing LiquidationState = "processing"
LiquidationStateDebt LiquidationState = "debt"
LiquidationStateLiquidated LiquidationState = "liquidated"
)
//go:generate GetRequest -url "/api/v3/wallet/m/liquidations" -type GetMarginLiquidationHistoryRequest -responseType []LiquidationRecord
type GetMarginLiquidationHistoryRequest struct {
client requestgen.AuthenticatedAPIClient
startTime *time.Time `param:"startTime,milliseconds"`
endTime *time.Time `param:"endTime,milliseconds"`
limit *int `param:"limit"`
}

View File

@ -0,0 +1,33 @@
package v3
import (
"time"
"github.com/c9s/requestgen"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
func (s *Client) NewGetMarginLoanHistoryRequest() *GetMarginLoanHistoryRequest {
return &GetMarginLoanHistoryRequest{client: s.Client}
}
type LoanRecord struct {
SN string `json:"sn"`
Currency string `json:"currency"`
Amount fixedpoint.Value `json:"amount"`
State string `json:"state"`
CreatedAt types.MillisecondTimestamp `json:"created_at"`
InterestRate fixedpoint.Value `json:"interest_rate"`
}
//go:generate GetRequest -url "/api/v3/wallet/m/loans/:currency" -type GetMarginLoanHistoryRequest -responseType []LoanRecord
type GetMarginLoanHistoryRequest struct {
client requestgen.AuthenticatedAPIClient
currency string `param:"currency,slug,required"`
startTime *time.Time `param:"startTime,milliseconds"`
endTime *time.Time `param:"endTime,milliseconds"`
limit *int `param:"limit"`
}

View File

@ -0,0 +1,30 @@
package v3
import (
"time"
"github.com/c9s/requestgen"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
type RepaymentRecord struct {
SN string `json:"sn"`
Currency string `json:"currency"`
Amount fixedpoint.Value `json:"amount"`
Principal fixedpoint.Value `json:"principal"`
Interest fixedpoint.Value `json:"interest"`
CreatedAt types.MillisecondTimestamp `json:"created_at"`
State string `json:"state"`
}
//go:generate GetRequest -url "/api/v3/wallet/m/repayments/:currency" -type GetMarginRepaymentHistoryRequest -responseType []RepaymentRecord
type GetMarginRepaymentHistoryRequest struct {
client requestgen.AuthenticatedAPIClient
currency string `param:"currency,slug,required"`
startTime *time.Time `param:"startTime,milliseconds"`
endTime *time.Time `param:"endTime,milliseconds"`
limit *int `param:"limit"`
}

View File

@ -6,7 +6,7 @@ package v3
import "github.com/c9s/requestgen"
func (s *OrderService) NewGetOrderRequest() *GetOrderRequest {
func (s *Client) NewGetOrderRequest() *GetOrderRequest {
return &GetOrderRequest{client: s.Client}
}

View File

@ -6,7 +6,7 @@ package v3
import "github.com/c9s/requestgen"
func (s *OrderService) NewGetOrderTradesRequest() *GetOrderTradesRequest {
func (s *Client) NewGetOrderTradesRequest() *GetOrderTradesRequest {
return &GetOrderTradesRequest{client: s.Client}
}

View File

@ -6,7 +6,7 @@ import "github.com/c9s/requestgen"
//go:generate -command PostRequest requestgen -method POST
//go:generate -command DeleteRequest requestgen -method DELETE
func (s *OrderService) NewGetWalletAccountsRequest(walletType WalletType) *GetWalletAccountsRequest {
func (s *Client) NewGetWalletAccountsRequest(walletType WalletType) *GetWalletAccountsRequest {
return &GetWalletAccountsRequest{client: s.Client, walletType: walletType}
}

View File

@ -6,7 +6,7 @@ import "github.com/c9s/requestgen"
//go:generate -command PostRequest requestgen -method POST
//go:generate -command DeleteRequest requestgen -method DELETE
func (s *OrderService) NewGetWalletOpenOrdersRequest(walletType WalletType) *GetWalletOpenOrdersRequest {
func (s *Client) NewGetWalletOpenOrdersRequest(walletType WalletType) *GetWalletOpenOrdersRequest {
return &GetWalletOpenOrdersRequest{client: s.Client, walletType: walletType}
}

View File

@ -6,7 +6,7 @@ import "github.com/c9s/requestgen"
//go:generate -command PostRequest requestgen -method POST
//go:generate -command DeleteRequest requestgen -method DELETE
func (s *OrderService) NewGetWalletOrderHistoryRequest(walletType WalletType) *GetWalletOrderHistoryRequest {
func (s *Client) NewGetWalletOrderHistoryRequest(walletType WalletType) *GetWalletOrderHistoryRequest {
return &GetWalletOrderHistoryRequest{client: s.Client, walletType: walletType}
}

View File

@ -10,7 +10,7 @@ import (
"github.com/c9s/requestgen"
)
func (s *OrderService) NewGetWalletTradesRequest(walletType WalletType) *GetWalletTradesRequest {
func (s *Client) NewGetWalletTradesRequest(walletType WalletType) *GetWalletTradesRequest {
return &GetWalletTradesRequest{client: s.Client, walletType: walletType}
}

View File

@ -18,34 +18,18 @@ type MarginService struct {
Client *maxapi.RestClient
}
func (s *MarginService) NewGetMarginInterestRatesRequest() *GetMarginInterestRatesRequest {
func (s *Client) NewGetMarginInterestRatesRequest() *GetMarginInterestRatesRequest {
return &GetMarginInterestRatesRequest{client: s.Client}
}
func (s *MarginService) NewGetMarginBorrowingLimitsRequest() *GetMarginBorrowingLimitsRequest {
func (s *Client) NewGetMarginBorrowingLimitsRequest() *GetMarginBorrowingLimitsRequest {
return &GetMarginBorrowingLimitsRequest{client: s.Client}
}
func (s *MarginService) NewGetMarginInterestHistoryRequest(currency string) *GetMarginInterestHistoryRequest {
func (s *Client) NewGetMarginInterestHistoryRequest(currency string) *GetMarginInterestHistoryRequest {
return &GetMarginInterestHistoryRequest{client: s.Client, currency: currency}
}
func (s *MarginService) NewGetMarginLiquidationHistoryRequest() *GetMarginLiquidationHistoryRequest {
return &GetMarginLiquidationHistoryRequest{client: s.Client}
}
func (s *MarginService) NewGetMarginLoanHistoryRequest() *GetMarginLoanHistoryRequest {
return &GetMarginLoanHistoryRequest{client: s.Client}
}
func (s *MarginService) NewMarginRepayRequest() *MarginRepayRequest {
return &MarginRepayRequest{client: s.Client}
}
func (s *MarginService) NewMarginLoanRequest() *MarginLoanRequest {
return &MarginLoanRequest{client: s.Client}
}
type MarginInterestRate struct {
HourlyInterestRate fixedpoint.Value `json:"hourly_interest_rate"`
NextHourlyInterestRate fixedpoint.Value `json:"next_hourly_interest_rate"`
@ -81,80 +65,3 @@ type GetMarginInterestHistoryRequest struct {
endTime *time.Time `param:"endTime,milliseconds"`
limit *int `param:"limit"`
}
type LiquidationRecord struct {
SN string `json:"sn"`
AdRatio fixedpoint.Value `json:"ad_ratio"`
ExpectedAdRatio fixedpoint.Value `json:"expected_ad_ratio"`
CreatedAt types.MillisecondTimestamp `json:"created_at"`
State LiquidationState `json:"state"`
}
type LiquidationState string
const (
LiquidationStateProcessing LiquidationState = "processing"
LiquidationStateDebt LiquidationState = "debt"
LiquidationStateLiquidated LiquidationState = "liquidated"
)
//go:generate GetRequest -url "/api/v3/wallet/m/liquidations" -type GetMarginLiquidationHistoryRequest -responseType []LiquidationRecord
type GetMarginLiquidationHistoryRequest struct {
client requestgen.AuthenticatedAPIClient
startTime *time.Time `param:"startTime,milliseconds"`
endTime *time.Time `param:"endTime,milliseconds"`
limit *int `param:"limit"`
}
type RepaymentRecord struct {
SN string `json:"sn"`
Currency string `json:"currency"`
Amount fixedpoint.Value `json:"amount"`
Principal fixedpoint.Value `json:"principal"`
Interest fixedpoint.Value `json:"interest"`
CreatedAt types.MillisecondTimestamp `json:"created_at"`
State string `json:"state"`
}
//go:generate GetRequest -url "/api/v3/wallet/m/repayments/:currency" -type GetMarginRepaymentHistoryRequest -responseType []RepaymentRecord
type GetMarginRepaymentHistoryRequest struct {
client requestgen.AuthenticatedAPIClient
currency string `param:"currency,slug,required"`
startTime *time.Time `param:"startTime,milliseconds"`
endTime *time.Time `param:"endTime,milliseconds"`
limit *int `param:"limit"`
}
type LoanRecord struct {
SN string `json:"sn"`
Currency string `json:"currency"`
Amount fixedpoint.Value `json:"amount"`
State string `json:"state"`
CreatedAt types.MillisecondTimestamp `json:"created_at"`
InterestRate fixedpoint.Value `json:"interest_rate"`
}
//go:generate GetRequest -url "/api/v3/wallet/m/loans/:currency" -type GetMarginLoanHistoryRequest -responseType []LoanRecord
type GetMarginLoanHistoryRequest struct {
client requestgen.AuthenticatedAPIClient
currency string `param:"currency,slug,required"`
startTime *time.Time `param:"startTime,milliseconds"`
endTime *time.Time `param:"endTime,milliseconds"`
limit *int `param:"limit"`
}
//go:generate PostRequest -url "/api/v3/wallet/m/loan/:currency" -type MarginLoanRequest -responseType .LoanRecord
type MarginLoanRequest struct {
client requestgen.AuthenticatedAPIClient
currency string `param:"currency,slug,required"`
amount string `param:"amount"`
}
//go:generate PostRequest -url "/api/v3/wallet/m/repayment/:currency" -type MarginRepayRequest -responseType .RepaymentRecord
type MarginRepayRequest struct {
client requestgen.AuthenticatedAPIClient
currency string `param:"currency,slug,required"`
amount string `param:"amount"`
}

View File

@ -0,0 +1,18 @@
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"
func (s *Client) NewMarginLoanRequest() *MarginLoanRequest {
return &MarginLoanRequest{client: s.Client}
}
//go:generate PostRequest -url "/api/v3/wallet/m/loan/:currency" -type MarginLoanRequest -responseType .LoanRecord
type MarginLoanRequest struct {
client requestgen.AuthenticatedAPIClient
currency string `param:"currency,slug,required"`
amount string `param:"amount"`
}

View File

@ -0,0 +1,18 @@
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"
func (s *Client) NewMarginRepayRequest() *MarginRepayRequest {
return &MarginRepayRequest{client: s.Client}
}
//go:generate PostRequest -url "/api/v3/wallet/m/repayment/:currency" -type MarginRepayRequest -responseType .RepaymentRecord
type MarginRepayRequest struct {
client requestgen.AuthenticatedAPIClient
currency string `param:"currency,slug,required"`
amount string `param:"amount"`
}

View File

@ -1,9 +1,5 @@
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"
@ -17,7 +13,6 @@ type OrderType = maxapi.OrderType
type Order = maxapi.Order
type Account = maxapi.Account
// OrderService manages the Order endpoint.
type OrderService struct {
type Client struct {
Client requestgen.AuthenticatedAPIClient
}