max: implement TransferMarginAccountAsset on max

This commit is contained in:
c9s 2023-08-08 13:16:11 +08:00
parent 5460ebdbf4
commit 25298720d0
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
4 changed files with 69 additions and 5 deletions

View File

@ -1051,3 +1051,13 @@ func (e *Exchange) IsSupportedInterval(interval types.Interval) bool {
_, ok := SupportedIntervals[interval]
return ok
}
func logResponse(resp interface{}, err error, req interface{}) error {
if err != nil {
log.WithError(err).Errorf("%T: error %+v", req, resp)
return err
}
log.Infof("%T: response: %+v", req, resp)
return nil
}

View File

@ -0,0 +1,43 @@
package max
import (
"context"
"errors"
"fmt"
v3 "github.com/c9s/bbgo/pkg/exchange/max/maxapi/v3"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
// TransferMarginAccountAsset transfers the asset into/out from the margin account
//
// types.TransferIn => Spot to Margin
// types.TransferOut => Margin to Spot
//
// to call this method, you must set the IsMargin = true
func (e *Exchange) TransferMarginAccountAsset(ctx context.Context, asset string, amount fixedpoint.Value, io types.TransferDirection) error {
if e.IsIsolatedMargin {
return errors.New("isolated margin is not supported")
}
return e.transferCrossMarginAccountAsset(ctx, asset, amount, io)
}
// transferCrossMarginAccountAsset transfer asset to the cross margin account or to the main account
func (e *Exchange) transferCrossMarginAccountAsset(ctx context.Context, asset string, amount fixedpoint.Value, io types.TransferDirection) error {
req := e.v3margin.NewMarginTransferRequest()
req.Currency(toLocalCurrency(asset))
req.Amount(amount.String())
if io == types.TransferIn {
req.Side(v3.MarginTransferSideIn)
} else if io == types.TransferOut {
req.Side(v3.MarginTransferSideOut)
} else {
return fmt.Errorf("unexpected transfer direction: %d given", io)
}
resp, err := req.Do(ctx)
return logResponse(resp, err, req)
}

View File

@ -11,7 +11,7 @@ import (
"github.com/c9s/bbgo/pkg/types"
)
func (s *Client) NewMarginTransferRequest() *MarginTransferRequest {
func (s *MarginService) NewMarginTransferRequest() *MarginTransferRequest {
return &MarginTransferRequest{client: s.Client}
}
@ -35,7 +35,7 @@ type MarginTransferResponse struct {
type MarginTransferRequest struct {
client requestgen.AuthenticatedAPIClient
currency string `param:"currency,required"`
amount string `param:"amount"`
side string `param:"side"`
currency string `param:"currency,required"`
amount string `param:"amount"`
side MarginTransferSide `param:"side"`
}

View File

@ -21,7 +21,7 @@ func (m *MarginTransferRequest) Amount(amount string) *MarginTransferRequest {
return m
}
func (m *MarginTransferRequest) Side(side string) *MarginTransferRequest {
func (m *MarginTransferRequest) Side(side MarginTransferSide) *MarginTransferRequest {
m.side = side
return m
}
@ -60,6 +60,17 @@ func (m *MarginTransferRequest) GetParameters() (map[string]interface{}, error)
// check side field -> json key side
side := m.side
// TEMPLATE check-valid-values
switch side {
case MarginTransferSideIn, MarginTransferSideOut:
params["side"] = side
default:
return nil, fmt.Errorf("side value %v is invalid", side)
}
// END TEMPLATE check-valid-values
// assign parameter of side
params["side"] = side