Files
gocryptotrader/exchanges/okx/ws_requests.go
Samuael A. 3f534a15f1 cmd/exchange_template, exchanges: Update templates and propogate to exchanges (#1777)
* Added TimeInForce type and updated related files

* Linter issue fix and minor coinbasepro type update

* Bitrex consts update

* added unit test and minor changes in bittrex

* Unit tests update

* Fix minor linter issues

* Update TestStringToTimeInForce unit test

* Exchange test template change

* A different approach

* fix conflict with gateio timeInForce

* minor exchange template update

* Minor fix to test_files template

* Update order tests

* Complete updating the order unit tests

* Updating exchange wrapper and test template files

* update kucoin and deribit wrapper to match the time in force change

* minor comment update

* fix time-in-force related test errors

* linter issue fix

* ADD_NEW_EXCHANGE documentation update

* time in force constants, functions and unit tests update

* shift tif policies to TimeInForce

* Update time-in-force, related functions, and unit tests

* fix linter issue and time-in-force processing

* added a good till crossing tif value

* order type fix and fix related tim-in-force entries

* update time-in-force unmarshaling and unit test

* consistency guideline added

* fix time-in-force error in gateio

* linter issue fix

* update based on review comments

* add unit test and fix missing issues

* minor fix and added benchmark unit test

* change GTT to GTC for limit

* fix linter issue

* added time-in-force value to place order param

* fix minor issues based on review comment and move tif code to separate files

* update on exchanges linked to time-in-force

* resolve missing review comments

* minor linter issues fix

* added time-in-force handler and update timeInForce parametered endpoint

* minor fixes based on review

* nits fix

* update based on review

* linter fix

* rm getTimeInForce func and minor change to time-in-force

* minor change

* update based on review comments

* wrappers and time-in-force calling approach

* minor change

* update gateio string to timeInForce conversion and unit test

* update exchange template

* update wrapper template file

* policy comments, and template files update

* rename all exchange types name to Exchange

* update on template files and template generation

* templates and generation code and other updates

* linter issue fix

* added subscriptions and websocket templates

* update ADD_NEW_EXCHANGE.md with recent binance functions and implementations

* rename template files and update unit tests

* minor template and unit test fix

* rename templates and fix on unit tests

* update on template files and documentation

* removed unnecessary tag fix and update templates

* fix Add_NEW_EXCHANGE.md doc file

* formatting, comments, and error checks update on template files

* rename exchange receivers to e and ex for consistency

* rename unit test exchange receiver and minor updates

* linter issues fix

* fix deribit issue and minor style update

* fix test issues caused by receiver change

* raname local variables exchange declaration variables

* update templates comments

* update templates and related comments

* renamed ex to e

* update template comments

* toggle WS to false to improve coverage

* template comments update

* added test coverage to Ws enabled and minor changes

---------

Co-authored-by: Samuel Reid <43227667+cranktakular@users.noreply.github.com>
2025-07-17 10:46:36 +10:00

345 lines
10 KiB
Go

package okx
import (
"context"
"errors"
"fmt"
"reflect"
"strconv"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/encoding/json"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
)
var (
errInvalidWebsocketRequest = errors.New("invalid websocket request")
errOperationFailed = errors.New("operation failed")
errPartialSuccess = errors.New("bulk operation partially succeeded")
errMassCancelFailed = errors.New("mass cancel failed")
errCancelAllSpreadOrdersFailed = errors.New("cancel all spread orders failed")
errMultipleItemsReturned = errors.New("multiple items returned")
)
// WSPlaceOrder submits an order
func (e *Exchange) WSPlaceOrder(ctx context.Context, arg *PlaceOrderRequestParam) (*OrderData, error) {
if err := arg.Validate(); err != nil {
return nil, err
}
id := strconv.FormatInt(e.Websocket.AuthConn.GenerateMessageID(false), 10)
var resp []*OrderData
if err := e.SendAuthenticatedWebsocketRequest(ctx, placeOrderEPL, id, "order", []PlaceOrderRequestParam{*arg}, &resp); err != nil {
return nil, err
}
return singleItem(resp)
}
// WSPlaceMultipleOrders submits multiple orders
func (e *Exchange) WSPlaceMultipleOrders(ctx context.Context, args []PlaceOrderRequestParam) ([]*OrderData, error) {
if len(args) == 0 {
return nil, fmt.Errorf("%T: %w", args, order.ErrSubmissionIsNil)
}
for i := range args {
if err := args[i].Validate(); err != nil {
return nil, err
}
}
id := strconv.FormatInt(e.Websocket.AuthConn.GenerateMessageID(false), 10)
var resp []*OrderData
return resp, e.SendAuthenticatedWebsocketRequest(ctx, placeMultipleOrdersEPL, id, "batch-orders", args, &resp)
}
// WSCancelOrder cancels an order
func (e *Exchange) WSCancelOrder(ctx context.Context, arg *CancelOrderRequestParam) (*OrderData, error) {
if arg == nil {
return nil, fmt.Errorf("%T: %w", arg, common.ErrNilPointer)
}
if arg.InstrumentID == "" {
return nil, errMissingInstrumentID
}
if arg.OrderID == "" && arg.ClientOrderID == "" {
return nil, order.ErrOrderIDNotSet
}
id := strconv.FormatInt(e.Websocket.AuthConn.GenerateMessageID(false), 10)
var resp []*OrderData
if err := e.SendAuthenticatedWebsocketRequest(ctx, cancelOrderEPL, id, "cancel-order", []CancelOrderRequestParam{*arg}, &resp); err != nil {
return nil, err
}
return singleItem(resp)
}
// WSCancelMultipleOrders cancels multiple orders
func (e *Exchange) WSCancelMultipleOrders(ctx context.Context, args []CancelOrderRequestParam) ([]*OrderData, error) {
if len(args) == 0 {
return nil, fmt.Errorf("%T: %w", args, order.ErrSubmissionIsNil)
}
for i := range args {
if args[i].InstrumentID == "" {
return nil, errMissingInstrumentID
}
if args[i].OrderID == "" && args[i].ClientOrderID == "" {
return nil, order.ErrOrderIDNotSet
}
}
id := strconv.FormatInt(e.Websocket.AuthConn.GenerateMessageID(false), 10)
var resp []*OrderData
return resp, e.SendAuthenticatedWebsocketRequest(ctx, cancelMultipleOrdersEPL, id, "batch-cancel-orders", args, &resp)
}
// WSAmendOrder amends an order
func (e *Exchange) WSAmendOrder(ctx context.Context, arg *AmendOrderRequestParams) (*OrderData, error) {
if arg == nil {
return nil, fmt.Errorf("%T: %w", arg, common.ErrNilPointer)
}
if arg.InstrumentID == "" {
return nil, errMissingInstrumentID
}
if arg.ClientOrderID == "" && arg.OrderID == "" {
return nil, order.ErrOrderIDNotSet
}
if arg.NewQuantity <= 0 && arg.NewPrice <= 0 {
return nil, errInvalidNewSizeOrPriceInformation
}
id := strconv.FormatInt(e.Websocket.AuthConn.GenerateMessageID(false), 10)
var resp []*OrderData
if err := e.SendAuthenticatedWebsocketRequest(ctx, amendOrderEPL, id, "amend-order", []AmendOrderRequestParams{*arg}, &resp); err != nil {
return nil, err
}
return singleItem(resp)
}
// WSAmendMultipleOrders amends multiple orders
func (e *Exchange) WSAmendMultipleOrders(ctx context.Context, args []AmendOrderRequestParams) ([]*OrderData, error) {
if len(args) == 0 {
return nil, fmt.Errorf("%T: %w", args, order.ErrSubmissionIsNil)
}
for x := range args {
if args[x].InstrumentID == "" {
return nil, errMissingInstrumentID
}
if args[x].ClientOrderID == "" && args[x].OrderID == "" {
return nil, order.ErrOrderIDNotSet
}
if args[x].NewQuantity <= 0 && args[x].NewPrice <= 0 {
return nil, errInvalidNewSizeOrPriceInformation
}
}
id := strconv.FormatInt(e.Websocket.AuthConn.GenerateMessageID(false), 10)
var resp []*OrderData
return resp, e.SendAuthenticatedWebsocketRequest(ctx, amendMultipleOrdersEPL, id, "batch-amend-orders", args, &resp)
}
// WSMassCancelOrders cancels all MMP pending orders of an instrument family. Only applicable to Option in Portfolio Margin mode, and MMP privilege is required.
func (e *Exchange) WSMassCancelOrders(ctx context.Context, args []CancelMassReqParam) error {
if len(args) == 0 {
return fmt.Errorf("%T: %w", args, order.ErrSubmissionIsNil)
}
for x := range args {
if args[x].InstrumentType == "" {
return fmt.Errorf("%w, instrument type can not be empty", errInvalidInstrumentType)
}
if args[x].InstrumentFamily == "" {
return errInstrumentFamilyRequired
}
}
id := strconv.FormatInt(e.Websocket.AuthConn.GenerateMessageID(false), 10)
var resps []*struct {
Result bool `json:"result"`
}
if err := e.SendAuthenticatedWebsocketRequest(ctx, amendOrderEPL, id, "mass-cancel", args, &resps); err != nil {
return err
}
resp, err := singleItem(resps)
if err != nil {
return err
}
if !resp.Result {
return errMassCancelFailed
}
return nil
}
// WSPlaceSpreadOrder submits a spread order
func (e *Exchange) WSPlaceSpreadOrder(ctx context.Context, arg *SpreadOrderParam) (*SpreadOrderResponse, error) {
if err := arg.Validate(); err != nil {
return nil, err
}
id := strconv.FormatInt(e.Websocket.AuthConn.GenerateMessageID(false), 10)
var resp []*SpreadOrderResponse
if err := e.SendAuthenticatedWebsocketRequest(ctx, placeSpreadOrderEPL, id, "sprd-order", []SpreadOrderParam{*arg}, &resp); err != nil {
return nil, err
}
return singleItem(resp)
}
// WSAmendSpreadOrder amends a spread order
func (e *Exchange) WSAmendSpreadOrder(ctx context.Context, arg *AmendSpreadOrderParam) (*SpreadOrderResponse, error) {
if arg == nil {
return nil, fmt.Errorf("%T: %w", arg, common.ErrNilPointer)
}
if arg.OrderID == "" && arg.ClientOrderID == "" {
return nil, order.ErrOrderIDNotSet
}
if arg.NewPrice == 0 && arg.NewSize == 0 {
return nil, errSizeOrPriceIsRequired
}
id := strconv.FormatInt(e.Websocket.AuthConn.GenerateMessageID(false), 10)
var resp []*SpreadOrderResponse
if err := e.SendAuthenticatedWebsocketRequest(ctx, amendSpreadOrderEPL, id, "sprd-amend-order", []AmendSpreadOrderParam{*arg}, &resp); err != nil {
return nil, err
}
return singleItem(resp)
}
// WSCancelSpreadOrder cancels an incomplete spread order through the websocket connection.
func (e *Exchange) WSCancelSpreadOrder(ctx context.Context, orderID, clientOrderID string) (*SpreadOrderResponse, error) {
if orderID == "" && clientOrderID == "" {
return nil, order.ErrOrderIDNotSet
}
arg := make(map[string]string)
if orderID != "" {
arg["ordId"] = orderID
}
if clientOrderID != "" {
arg["clOrdId"] = clientOrderID
}
id := strconv.FormatInt(e.Websocket.AuthConn.GenerateMessageID(false), 10)
var resp []*SpreadOrderResponse
if err := e.SendAuthenticatedWebsocketRequest(ctx, cancelSpreadOrderEPL, id, "sprd-cancel-order", []map[string]string{arg}, &resp); err != nil {
return nil, err
}
return singleItem(resp)
}
// WSCancelAllSpreadOrders cancels all spread orders and return success message through the websocket channel.
func (e *Exchange) WSCancelAllSpreadOrders(ctx context.Context, spreadID string) error {
arg := make(map[string]string, 1)
if spreadID != "" {
arg["sprdId"] = spreadID
}
id := strconv.FormatInt(e.Websocket.AuthConn.GenerateMessageID(false), 10)
var resps []*ResponseResult
if err := e.SendAuthenticatedWebsocketRequest(ctx, cancelAllSpreadOrderEPL, id, "sprd-mass-cancel", []map[string]string{arg}, &resps); err != nil {
return err
}
resp, err := singleItem(resps)
if err != nil {
return err
}
if !resp.Result {
return errCancelAllSpreadOrdersFailed
}
return nil
}
// SendAuthenticatedWebsocketRequest sends a websocket request to the server
func (e *Exchange) SendAuthenticatedWebsocketRequest(ctx context.Context, epl request.EndpointLimit, id, operation string, payload, result any) error {
if operation == "" || payload == nil {
return errInvalidWebsocketRequest
}
outbound := &struct {
ID string `json:"id"`
Operation string `json:"op"`
Arguments any `json:"args"`
// TODO: Add ExpTime to the struct, the struct should look like this:
// ExpTime string `json:"expTime,omitempty"` so a deadline can be set
// Request effective deadline. Unix timestamp format in milliseconds, e.g. 1597026383085
}{
ID: id,
Operation: operation,
Arguments: payload,
}
incoming, err := e.Websocket.AuthConn.SendMessageReturnResponse(ctx, epl, id, outbound)
if err != nil {
return err
}
intermediary := struct {
ID string `json:"id"`
Operation string `json:"op"`
Code int64 `json:"code,string"`
Message string `json:"msg"`
Data any `json:"data"`
InTime string `json:"inTime"`
OutTime string `json:"outTime"`
}{
Data: result,
}
if err := json.Unmarshal(incoming, &intermediary); err != nil {
return err
}
switch intermediary.Code {
case 0:
return nil
case 1:
return parseWSResponseErrors(result, errOperationFailed)
case 2:
return parseWSResponseErrors(result, errPartialSuccess)
default:
return getStatusError(intermediary.Code, intermediary.Message)
}
}
func parseWSResponseErrors(result any, err error) error {
s := reflect.ValueOf(result).Elem()
for i := range s.Len() {
v := s.Index(i)
if subErr, ok := v.Interface().(interface{ Error() error }); ok && subErr.Error() != nil {
err = common.AppendError(err, fmt.Errorf("%s[%d]: %w", v.Type(), i+1, subErr.Error()))
}
}
return err
}
func singleItem[T any](resp []*T) (*T, error) {
if len(resp) == 0 {
return nil, common.ErrNoResponse
}
if len(resp) > 1 {
return nil, fmt.Errorf("%w, received %d", errMultipleItemsReturned, len(resp))
}
return resp[0], nil
}