Files
gocryptotrader/exchanges/gateio/gateio_websocket_request_futures.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

179 lines
6.1 KiB
Go

package gateio
import (
"context"
"errors"
"fmt"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/exchange/websocket"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
)
var (
errInvalidAutoSize = errors.New("invalid auto size")
errStatusNotSet = errors.New("status not set")
)
// authenticateFutures sends an authentication message to the websocket connection
func (e *Exchange) authenticateFutures(ctx context.Context, conn websocket.Connection) error {
return e.websocketLogin(ctx, conn, "futures.login")
}
// WebsocketFuturesSubmitOrder submits an order via the websocket connection
func (e *Exchange) WebsocketFuturesSubmitOrder(ctx context.Context, a asset.Item, order *ContractOrderCreateParams) (*WebsocketFuturesOrderResponse, error) {
resps, err := e.WebsocketFuturesSubmitOrders(ctx, a, order)
if err != nil {
return nil, err
}
if len(resps) != 1 {
return nil, common.ErrInvalidResponse
}
return &resps[0], err
}
// WebsocketFuturesSubmitOrders submits orders via the websocket connection. All orders must be for the same asset.
func (e *Exchange) WebsocketFuturesSubmitOrders(ctx context.Context, a asset.Item, orders ...*ContractOrderCreateParams) ([]WebsocketFuturesOrderResponse, error) {
if len(orders) == 0 {
return nil, errOrdersEmpty
}
for _, o := range orders {
if err := validateFuturesPairAsset(o.Contract, a); err != nil {
return nil, err
}
if o.Price == "" && o.TimeInForce != "ioc" {
return nil, fmt.Errorf("%w: cannot be zero when time in force is not IOC", errInvalidPrice)
}
if o.Size == 0 && o.AutoSize == "" {
return nil, fmt.Errorf("%w: size cannot be zero", errInvalidAmount)
}
if o.AutoSize != "" {
if o.AutoSize != "close_long" && o.AutoSize != "close_short" {
return nil, fmt.Errorf("%w: %s", errInvalidAutoSize, o.AutoSize)
}
if o.Size != 0 {
return nil, fmt.Errorf("%w: size needs to be zero when auto size is set", errInvalidAmount)
}
}
}
if len(orders) == 1 {
var singleResponse WebsocketFuturesOrderResponse
err := e.SendWebsocketRequest(ctx, perpetualSubmitOrderEPL, "futures.order_place", a, orders[0], &singleResponse, 2)
return []WebsocketFuturesOrderResponse{singleResponse}, err
}
var resp []WebsocketFuturesOrderResponse
return resp, e.SendWebsocketRequest(ctx, perpetualSubmitBatchOrdersEPL, "futures.order_batch_place", a, orders, &resp, 2)
}
// WebsocketFuturesCancelOrder cancels an order via the websocket connection.
func (e *Exchange) WebsocketFuturesCancelOrder(ctx context.Context, orderID string, contract currency.Pair, a asset.Item) (*WebsocketFuturesOrderResponse, error) {
if orderID == "" {
return nil, order.ErrOrderIDNotSet
}
if err := validateFuturesPairAsset(contract, a); err != nil {
return nil, err
}
params := &struct {
OrderID string `json:"order_id"`
}{OrderID: orderID}
var resp WebsocketFuturesOrderResponse
return &resp, e.SendWebsocketRequest(ctx, perpetualCancelOrderEPL, "futures.order_cancel", a, params, &resp, 1)
}
// WebsocketFuturesCancelAllOpenFuturesOrders cancels multiple orders via the websocket.
func (e *Exchange) WebsocketFuturesCancelAllOpenFuturesOrders(ctx context.Context, contract currency.Pair, a asset.Item, side string) ([]WebsocketFuturesOrderResponse, error) {
if err := validateFuturesPairAsset(contract, a); err != nil {
return nil, err
}
if side != "" && side != "ask" && side != "bid" {
return nil, fmt.Errorf("%w: %s", order.ErrSideIsInvalid, side)
}
params := &struct {
Contract currency.Pair `json:"contract"`
Side string `json:"side,omitempty"`
}{Contract: contract, Side: side}
var resp []WebsocketFuturesOrderResponse
return resp, e.SendWebsocketRequest(ctx, perpetualCancelOpenOrdersEPL, "futures.order_cancel_cp", a, params, &resp, 2)
}
// WebsocketFuturesAmendOrder amends an order via the websocket connection
func (e *Exchange) WebsocketFuturesAmendOrder(ctx context.Context, amend *WebsocketFuturesAmendOrder) (*WebsocketFuturesOrderResponse, error) {
if amend == nil {
return nil, fmt.Errorf("%w: %T", common.ErrNilPointer, amend)
}
if amend.OrderID == "" {
return nil, order.ErrOrderIDNotSet
}
if err := validateFuturesPairAsset(amend.Contract, amend.Asset); err != nil {
return nil, err
}
if amend.Size == 0 && amend.Price == "" {
return nil, fmt.Errorf("%w: size or price must be set", errInvalidAmount)
}
var resp WebsocketFuturesOrderResponse
return &resp, e.SendWebsocketRequest(ctx, perpetualAmendOrderEPL, "futures.order_amend", amend.Asset, amend, &resp, 1)
}
// WebsocketFuturesOrderList fetches a list of orders via the websocket connection
func (e *Exchange) WebsocketFuturesOrderList(ctx context.Context, list *WebsocketFutureOrdersList) ([]WebsocketFuturesOrderResponse, error) {
if list == nil {
return nil, fmt.Errorf("%w: %T", common.ErrNilPointer, list)
}
if err := validateFuturesPairAsset(list.Contract, list.Asset); err != nil {
return nil, err
}
if list.Status == "" {
return nil, errStatusNotSet
}
var resp []WebsocketFuturesOrderResponse
return resp, e.SendWebsocketRequest(ctx, perpetualGetOrdersEPL, "futures.order_list", list.Asset, list, &resp, 1)
}
// WebsocketFuturesGetOrderStatus gets the status of an order via the websocket connection.
func (e *Exchange) WebsocketFuturesGetOrderStatus(ctx context.Context, contract currency.Pair, a asset.Item, orderID string) (*WebsocketFuturesOrderResponse, error) {
if err := validateFuturesPairAsset(contract, a); err != nil {
return nil, err
}
if orderID == "" {
return nil, order.ErrOrderIDNotSet
}
params := &struct {
OrderID string `json:"order_id"`
}{OrderID: orderID}
var resp WebsocketFuturesOrderResponse
return &resp, e.SendWebsocketRequest(ctx, perpetualFetchOrderEPL, "futures.order_status", a, params, &resp, 1)
}
// validateFuturesPairAsset enforces that a futures pair's quote currency matches the given asset
func validateFuturesPairAsset(pair currency.Pair, a asset.Item) error {
if pair.IsEmpty() {
return currency.ErrCurrencyPairEmpty
}
_, err := getSettlementCurrency(pair, a)
return err
}