From 4e666dee98e4b49929e3686120be2a0d8de77c16 Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 1 Jun 2022 20:44:24 +0800 Subject: [PATCH] max: implement margin borrow and repay service on max --- pkg/exchange/max/exchange.go | 44 ++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/pkg/exchange/max/exchange.go b/pkg/exchange/max/exchange.go index 618774a9f..b9471b608 100644 --- a/pkg/exchange/max/exchange.go +++ b/pkg/exchange/max/exchange.go @@ -38,6 +38,7 @@ type Exchange struct { v3margin *v3.MarginService } + func New(key, secret string) *Exchange { baseURL := maxapi.ProductionAPIURL if override := os.Getenv("MAX_API_BASE_URL"); len(override) > 0 { @@ -925,3 +926,46 @@ func (e *Exchange) QueryAveragePrice(ctx context.Context, symbol string) (fixedp return fixedpoint.MustNewFromString(ticker.Sell). Add(fixedpoint.MustNewFromString(ticker.Buy)).Div(Two), nil } + + +func (e *Exchange) RepayMarginAsset(ctx context.Context, asset string, amount fixedpoint.Value) error { + req := e.v3margin.NewMarginRepayRequest() + req.Currency(toLocalCurrency(asset)) + req.Amount(amount.String()) + resp, err := req.Do(ctx) + if err != nil { + return err + } + + log.Infof("margin repay: %v", resp) + return nil +} + +func (e *Exchange) BorrowMarginAsset(ctx context.Context, asset string, amount fixedpoint.Value) error { + req := e.v3margin.NewMarginLoanRequest() + req.Currency(toLocalCurrency(asset)) + req.Amount(amount.String()) + resp, err := req.Do(ctx) + if err != nil { + return err + } + + log.Infof("margin borrow: %v", resp) + return nil +} + +func (e *Exchange) QueryMarginAssetMaxBorrowable(ctx context.Context, asset string) (amount fixedpoint.Value, err error) { + req := e.v3margin.NewGetMarginBorrowingLimitsRequest() + resp, err := req.Do(ctx) + if err != nil { + return fixedpoint.Zero, err + } + + limits := *resp + if limit, ok := limits[toLocalCurrency(asset)]; ok { + return limit, nil + } + + err = fmt.Errorf("borrowing limit of %s not found", asset) + return amount, err +}