qbtrade/pkg/exchange/max/margin.go
2024-06-27 22:42:38 +08:00

44 lines
1.3 KiB
Go

package max
import (
"context"
"errors"
"fmt"
v3 "git.qtrade.icu/lychiyu/qbtrade/pkg/exchange/max/maxapi/v3"
"git.qtrade.icu/lychiyu/qbtrade/pkg/fixedpoint"
"git.qtrade.icu/lychiyu/qbtrade/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)
}