Files
gocryptotrader/exchanges/okx/helpers.go
Adrian Gallagher a5b638bfb7 GHA: Add additional checks for common issues (#1922)
* GHA, tests: Add additional checks for common issues

These checks include:
- Ensuring that all testify funcs use their formatted variants (e.g., `assert.Equalf(t, expected, actual)` instead of `assert.Equal(t, expected, actual)`).
- Replacing `%s` with %q
- Enforcing consistent usage of should/must wording for testify assert/require messages

* Add support for checking backticked string format specifiers and fix issues

* tests: Fix error comparisons

* tests: Replace errors.Is(err, nil) usage with testify and automate check

* refactor: Rename ExtractPort to ExtractPortOrDefault

* tests: Replace assert with require for error handling in multiple test files

* tests: Replace assert with require for error handling and improve assertions in data tests

* tests: Fix typo in assertion message for StreamVol test

* OKX: Fix GetOpenInterestAndVolumeStrike test with instrument selection and improved assertions

* OKX: Revert intentional error check

* Improve error message for expiry time check in GetOpenInterestAndVolumeStrike test
2025-05-28 12:26:51 +10:00

168 lines
4.6 KiB
Go

package okx
import (
"fmt"
"strings"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
)
// orderTypeFromString returns the order Type and TimeInForce for okx order type strings
func orderTypeFromString(orderType string) (order.Type, order.TimeInForce, error) {
orderType = strings.ToLower(orderType)
switch orderType {
case orderMarket:
return order.Market, order.UnknownTIF, nil
case orderLimit:
return order.Limit, order.UnknownTIF, nil
case orderPostOnly:
return order.Limit, order.PostOnly, nil
case orderFOK:
return order.Limit, order.FillOrKill, nil
case orderIOC:
return order.Limit, order.ImmediateOrCancel, nil
case orderOptimalLimitIOC:
return order.OptimalLimitIOC, order.ImmediateOrCancel, nil
case "mmp":
return order.MarketMakerProtection, order.UnknownTIF, nil
case "mmp_and_post_only":
return order.MarketMakerProtectionAndPostOnly, order.PostOnly, nil
case "twap":
return order.TWAP, order.UnknownTIF, nil
case "move_order_stop":
return order.TrailingStop, order.UnknownTIF, nil
case "chase":
return order.Chase, order.UnknownTIF, nil
default:
return order.UnknownType, order.UnknownTIF, fmt.Errorf("%w %v", order.ErrTypeIsInvalid, orderType)
}
}
// orderTypeString returns a string representation of order.Type instance
func orderTypeString(orderType order.Type, tif order.TimeInForce) (string, error) {
switch tif {
case order.PostOnly:
return orderPostOnly, nil
case order.FillOrKill:
return orderFOK, nil
case order.ImmediateOrCancel:
return orderIOC, nil
}
switch orderType {
case order.Market,
order.Limit,
order.Trigger,
order.OptimalLimitIOC,
order.MarketMakerProtection,
order.MarketMakerProtectionAndPostOnly,
order.Chase,
order.TWAP,
order.OCO:
return orderType.Lower(), nil
case order.ConditionalStop:
return "conditional", nil
case order.TrailingStop:
return "move_order_stop", nil
default:
return "", fmt.Errorf("%w: `%v`", order.ErrUnsupportedOrderType, orderType)
}
}
// getAssetsFromInstrumentID parses an instrument ID and returns a list of assets types
// that the instrument is associated with
func (ok *Okx) getAssetsFromInstrumentID(instrumentID string) ([]asset.Item, error) {
if instrumentID == "" {
return nil, errMissingInstrumentID
}
pf, err := ok.CurrencyPairs.GetFormat(asset.Spot, true)
if err != nil {
return nil, err
}
splitSymbol := strings.Split(instrumentID, pf.Delimiter)
if len(splitSymbol) <= 1 {
return nil, fmt.Errorf("%w %v", currency.ErrCurrencyNotSupported, instrumentID)
}
pair, err := currency.NewPairDelimiter(instrumentID, pf.Delimiter)
if err != nil {
return nil, fmt.Errorf("%w: %q", err, instrumentID)
}
switch {
case len(splitSymbol) == 2:
resp := make([]asset.Item, 0, 2)
enabled, err := ok.IsPairEnabled(pair, asset.Spot)
if err != nil {
return nil, err
}
if enabled {
resp = append(resp, asset.Spot)
}
enabled, err = ok.IsPairEnabled(pair, asset.Margin)
if err != nil {
return nil, err
}
if enabled {
resp = append(resp, asset.Margin)
}
if len(resp) > 0 {
return resp, nil
}
case len(splitSymbol) > 2:
var aType asset.Item
switch strings.ToLower(splitSymbol[len(splitSymbol)-1]) {
case "swap":
aType = asset.PerpetualSwap
case "c", "p":
aType = asset.Options
default:
aType = asset.Futures
}
enabled, err := ok.IsPairEnabled(pair, aType)
if err != nil {
return nil, err
} else if enabled {
return []asset.Item{aType}, nil
}
}
return nil, fmt.Errorf("%w: no asset enabled with instrument ID `%v`", asset.ErrNotEnabled, instrumentID)
}
// assetTypeFromInstrumentType returns an asset Item instance given and Instrument Type string
func assetTypeFromInstrumentType(instrumentType string) (asset.Item, error) {
switch strings.ToUpper(instrumentType) {
case instTypeSwap, instTypeContract:
return asset.PerpetualSwap, nil
case instTypeSpot:
return asset.Spot, nil
case instTypeMargin:
return asset.Margin, nil
case instTypeFutures:
return asset.Futures, nil
case instTypeOption:
return asset.Options, nil
case "":
return asset.Empty, nil
default:
return asset.Empty, asset.ErrNotSupported
}
}
// assetTypeString returns a string representation of asset type
func assetTypeString(assetType asset.Item) (string, error) {
switch assetType {
case asset.Spot:
return "SPOT", nil
case asset.Margin:
return "MARGIN", nil
case asset.Futures:
return "FUTURES", nil
case asset.Options:
return "OPTION", nil
case asset.PerpetualSwap:
return "SWAP", nil
default:
return "", asset.ErrNotSupported
}
}