Skip to content

Commit

Permalink
Merge pull request #57 from latesun/master
Browse files Browse the repository at this point in the history
Support order in python/go
  • Loading branch information
Sinclair-Ni authored Apr 30, 2024
2 parents 083ef9d + 2a1c660 commit d0cd24f
Show file tree
Hide file tree
Showing 20 changed files with 1,306 additions and 136 deletions.
194 changes: 194 additions & 0 deletions go/_examples/futures_order_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package main

import (
"encoding/json"
"os"
"testing"
"time"

gate "github.com/gateio/gatews/go"
"github.com/gateio/gatews/go/model"
"github.com/gateio/gatews/go/resp"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/stretchr/testify/assert"
)

var (
testFuturesOrderParam = &model.FuturesOrder{
Contract: "BTC_USDT",
Size: 50,
Iceberg: 0,
Price: "30000",
Text: "t-my-custom-id",
}

testKeyVals = map[string]any{
"X-Gate-Channel-Id": "T-xxx",
"req_id": "test_req_id",
}
)

type futuresTester struct {
svc *gate.WsService
}

func newFuturesTester() (*futuresTester, error) {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})

tester, err := gate.NewWsService(nil, nil, gate.NewConnConfFromOption(&gate.ConfOptions{
// URL: "",
App: "futures", // required
Key: "", // required
Secret: "", // required
MaxRetryConn: 5,
}))
if err != nil {
return nil, err
}

return &futuresTester{tester}, nil
}

func (s *futuresTester) loginCallback() gate.CallBack {
return gate.NewCallBack(func(msg *gate.UpdateMsg) {
if msg.Data.Errs != nil {
log.Error().Msgf("[Login] label: %s, message: %s", msg.Data.Errs.Label, msg.Data.Errs.Message)
return
}
log.Info().Msgf("[Login] result: %s", msg.Data.Result)
})
}

func (s *futuresTester) createOrderCallback() gate.CallBack {
return gate.NewCallBack(func(msg *gate.UpdateMsg) {
if msg.Data.Errs != nil {
log.Error().Msgf("[Create] label: %s, message: %s", msg.Data.Errs.Label, msg.Data.Errs.Message)
return
}

var order resp.FutureOrder
if err := json.Unmarshal(msg.Data.Result, &order); err != nil {
log.Error().Msgf("[Create] failed to unmarshal response: %v, msg: %v", err, msg)
return
}

if order.Id == 0 {
return
}

log.Info().Msgf("[Create] order_id: %v, status: %s", order.Id, order.Status)
})
}

func (s *futuresTester) orderAmendCallback() gate.CallBack {
return gate.NewCallBack(func(msg *gate.UpdateMsg) {
if msg.Data.Errs != nil {
log.Error().Msgf("[Amend] label: %s, message: %s", msg.Data.Errs.Label, msg.Data.Errs.Message)
return
}

var order resp.FutureOrder
if err := json.Unmarshal(msg.Data.Result, &order); err != nil {
log.Error().Msgf("[Amend] failed to unmarshal response: %v, msg: %v", err, msg)
return
}

log.Info().Msgf("[Amend] order_id: %v, status: %s", order.Id, order.Status)
})
}

func (s *futuresTester) orderStatusCallback() gate.CallBack {
return gate.NewCallBack(func(msg *gate.UpdateMsg) {
if msg.Data.Errs != nil {
log.Info().Msgf("[Query] label: %s, message: %s", msg.Data.Errs.Label, msg.Data.Errs.Message)
return
}

var order resp.FutureOrder
if err := json.Unmarshal(msg.Data.Result, &order); err != nil {
log.Info().Msgf("[Query] failed to unmarshal response: %v, msg: %v", err, msg)
return
}

log.Info().Msgf("[Query] order: %#v", order)
})
}

func (s *futuresTester) orderCancelCallback() gate.CallBack {
return gate.NewCallBack(func(msg *gate.UpdateMsg) {
if msg.Data.Errs != nil {
log.Error().Msgf("[Cancel] failed to cancel order, label: %s, message: %s", msg.Data.Errs.Label, msg.Data.Errs.Message)
return
}

var orders *resp.FutureOrder
if err := json.Unmarshal(msg.Data.Result, &orders); err != nil {
log.Error().Msgf("[Cancel] failed to unmarshal response: %v, msg: %v", err, msg)
return
}

log.Info().Msgf("[Cancel] order_id: %v, status: %v", orders.Id, orders.Status)
})
}

func TestFuturesCreateOrder(t *testing.T) {
s, err := newFuturesTester()
assert.NoError(t, err)

s.svc.SetCallBack(gate.ChannelFutureLogin, s.loginCallback())
s.svc.SetCallBack(gate.ChannelFutureOrderPlace, s.createOrderCallback())
assert.NoError(t, s.svc.APIRequest(gate.ChannelFutureOrderPlace, testFuturesOrderParam, testKeyVals))

time.Sleep(5 * time.Second)
}

func TestFuturesAmendOrder(t *testing.T) {
s, err := newFuturesTester()
assert.NoError(t, err)

s.svc.SetCallBack(gate.ChannelFutureLogin, s.loginCallback())
s.svc.SetCallBack(gate.ChannelFutureOrderAmend, s.orderAmendCallback())

order := &model.AmendFuturesOrder{
OrderId: "order_id",
Settle: "USDT",
Price: "40000",
AmendText: "",
Size: 1,
}
assert.NoError(t, s.svc.APIRequest(gate.ChannelFutureOrderAmend, order, testKeyVals))
}

func TestFuturesQueryOrderStatus(t *testing.T) {
s, err := newFuturesTester()
assert.NoError(t, err)

s.svc.SetCallBack(gate.ChannelFutureLogin, s.loginCallback())
s.svc.SetCallBack(gate.ChannelFutureOrderStatus, s.orderStatusCallback())

order := &model.StatusFuturesOrder{
Settle: "usdt",
OrderId: "order_id",
}

assert.NoError(t, s.svc.APIRequest(gate.ChannelFutureOrderStatus, order, testKeyVals))

time.Sleep(5 * time.Second)
}

func TestFuturesCancelOrder(t *testing.T) {
s, err := newFuturesTester()
assert.NoError(t, err)

s.svc.SetCallBack(gate.ChannelFutureLogin, s.loginCallback())
s.svc.SetCallBack(gate.ChannelFutureOrderCancel, s.orderCancelCallback())

order := &model.CancelFuturesOrder{
Settle: "usdt",
OrderId: "order_id",
}
assert.NoError(t, s.svc.APIRequest(gate.ChannelFutureOrderCancel, order, testKeyVals))

time.Sleep(5 * time.Second)
}
25 changes: 25 additions & 0 deletions go/_examples/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module example

go 1.18

require (
github.com/gansidui/skiplist v0.0.0-20141121051332-c6a909ce563b
github.com/gateio/gatews/go v0.0.0-00010101000000-000000000000
github.com/rs/zerolog v1.32.0
github.com/shopspring/decimal v1.3.1
github.com/stretchr/testify v1.9.0
github.com/yireyun/go-queue v0.0.0-20220725040158-a4dd64810e1e
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set v1.7.1 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.12.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/gateio/gatews/go => ../
35 changes: 35 additions & 0 deletions go/_examples/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deckarep/golang-set v1.7.1 h1:SCQV0S6gTtp6itiFrTqI+pfmJ4LN85S1YzhDf9rTHJQ=
github.com/deckarep/golang-set v1.7.1/go.mod h1:93vsz/8Wt4joVM7c2AVqh+YRMiUSc14yDtF28KmMOgQ=
github.com/gansidui/skiplist v0.0.0-20141121051332-c6a909ce563b h1:MAoeneEI/UCOxABHa7aU2+8dqM89Uaj6tSMFxr1wbe0=
github.com/gansidui/skiplist v0.0.0-20141121051332-c6a909ce563b/go.mod h1:8VKNiVGGPIhJE0qomrfsTKscI5iypji4r9wt4gEUDWE=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0=
github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/yireyun/go-queue v0.0.0-20220725040158-a4dd64810e1e h1:6BhwJMDB4lTjHfNKdf1O/IuhVS3Mw85/mnqpoQlngRk=
github.com/yireyun/go-queue v0.0.0-20220725040158-a4dd64810e1e/go.mod h1:NS8O3p7NiPwC1Yw9xTd9DnDBguxFJG/BL1VPTSfJ5Gw=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading

0 comments on commit d0cd24f

Please sign in to comment.