Skip to content

Commit

Permalink
Update unit tests and endpoint functions
Browse files Browse the repository at this point in the history
  • Loading branch information
samuael committed Nov 3, 2024
1 parent 82a77d0 commit 6c08ec3
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 92 deletions.
2 changes: 2 additions & 0 deletions exchanges/collateral/collateral.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func (t Mode) String() string {
return multiCollateralStr
case PortfolioMode:
return portfolioCollateralStr
case SpotFuturesMode:
return spotFuturesCollateralStr
case UnknownMode:
return unknownCollateralStr
}
Expand Down
3 changes: 3 additions & 0 deletions exchanges/collateral/collateral_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ func TestValidCollateralType(t *testing.T) {
if !PortfolioMode.Valid() {
t.Fatal("expected 'true', received 'false'")
}
if !SpotFuturesMode.Valid() {
t.Fatal("expected 'true', received 'false'")
}
if UnsetMode.Valid() {
t.Fatal("expected 'false', received 'true'")
}
Expand Down
15 changes: 9 additions & 6 deletions exchanges/collateral/collateral_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,23 @@ const (
PortfolioMode
// UnknownMode has collateral allocated in an unknown manner at present, but is not unset
UnknownMode
// SpotFuturesMode has collateral allocated across spot and futures accounts
SpotFuturesMode
)

const (
unsetCollateralStr = "unset"
singleCollateralStr = "single"
multiCollateralStr = "multi"
portfolioCollateralStr = "portfolio"
unknownCollateralStr = "unknown"
unsetCollateralStr = "unset"
singleCollateralStr = "single"
multiCollateralStr = "multi"
portfolioCollateralStr = "portfolio"
spotFuturesCollateralStr = "spot_futures"
unknownCollateralStr = "unknown"
)

// ErrInvalidCollateralMode is returned when converting invalid string to collateral mode
var ErrInvalidCollateralMode = errors.New("invalid collateral mode")

var supportedCollateralModes = SingleMode | MultiMode | PortfolioMode
var supportedCollateralModes = SingleMode | MultiMode | PortfolioMode | SpotFuturesMode

// ByPosition shows how much collateral is used
// from positions
Expand Down
21 changes: 11 additions & 10 deletions exchanges/okx/okx.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ func (ok *Okx) OrderTypeString(orderType order.Type) (string, error) {

// PlaceOrder place an order only if you have sufficient funds.
func (ok *Okx) PlaceOrder(ctx context.Context, arg *PlaceOrderRequestParam) (*OrderData, error) {
if arg == nil || *arg == (PlaceOrderRequestParam{}) {
return nil, common.ErrNilPointer
}
err := ok.validatePlaceOrderParams(arg)
if err != nil {
return nil, err
Expand Down Expand Up @@ -1734,10 +1737,16 @@ func (ok *Okx) SetPositionMode(ctx context.Context, positionMode string) (*Posit
}

// SetLeverageRate sets a leverage setting for instrument id.
func (ok *Okx) SetLeverageRate(ctx context.Context, arg SetLeverageInput) (*SetLeverageResponse, error) {
func (ok *Okx) SetLeverageRate(ctx context.Context, arg *SetLeverageInput) (*SetLeverageResponse, error) {
if arg.InstrumentID == "" && arg.Currency.IsEmpty() {
return nil, errEitherInstIDOrCcyIsRequired
}
switch arg.AssetType {
case asset.Futures, asset.PerpetualSwap:
if arg.PositionSide == "" && arg.MarginMode == "isolated" {
return nil, fmt.Errorf("%w, position side is required", order.ErrSideIsInvalid)
}
}
arg.PositionSide = strings.ToLower(arg.PositionSide)
var resp *SetLeverageResponse
return resp, ok.SendHTTPRequest(ctx, exchange.RestSpot, setLeverageEPL, http.MethodPost, "account/set-leverage", &arg, &resp, request.AuthenticatedRequest)
Expand Down Expand Up @@ -2205,14 +2214,6 @@ func (ok *Okx) GetBorrowInterestAndLimit(ctx context.Context, loanType int64, cc
return resp, ok.SendHTTPRequest(ctx, exchange.RestSpot, getBorrowInterestAndLimitEPL, http.MethodGet, common.EncodeURLValues("account/interest-limits", params), nil, &resp, request.AuthenticatedRequest)
}

// PositionBuilder calculates portfolio margin information for simulated position or current position of the user. You can add up to 200 simulated positions in one request.
// Instrument type SWAP FUTURES, and OPTION are supported
func (ok *Okx) PositionBuilder(ctx context.Context, arg PositionBuilderInput) ([]PositionBuilderResponse, error) {
arg.InstrumentType = strings.ToUpper(arg.InstrumentType)
var resp []PositionBuilderResponse
return resp, ok.SendHTTPRequest(ctx, exchange.RestSpot, positionBuilderEPL, http.MethodPost, "account/simulated_margin", &arg, &resp, request.AuthenticatedRequest)
}

// GetGreeks retrieves a greeks list of all assets in the account.
func (ok *Okx) GetGreeks(ctx context.Context, ccy currency.Code) ([]GreeksItem, error) {
params := url.Values{}
Expand Down Expand Up @@ -3235,7 +3236,7 @@ func (ok *Okx) PlaceLeadingStopOrder(ctx context.Context, arg *TPSLOrderParam) (
return nil, errSubPositionIDRequired
}
var resp *PositionIDInfo
return resp, ok.SendHTTPRequest(ctx, exchange.RestSpot, placeLeadingStopOrderEPL, http.MethodGet, "copytrading/algo-order", arg, &resp, request.AuthenticatedRequest)
return resp, ok.SendHTTPRequest(ctx, exchange.RestSpot, placeLeadingStopOrderEPL, http.MethodPost, "copytrading/algo-order", arg, &resp, request.AuthenticatedRequest)
}

// CloseLeadingPosition close a leading position once a time.
Expand Down
Loading

0 comments on commit 6c08ec3

Please sign in to comment.