mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 08:45:16 +00:00
max: implement margin borrow and repay service on max
This commit is contained in:
parent
01822eee28
commit
4e666dee98
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user