Skip to content

Commit

Permalink
Release: v1.0.10-alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
amir-the-h committed Sep 29, 2021
1 parent 12050c7 commit a6c60aa
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 23 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ Changelog
=========
All notable changes to this project will be documented in this file.

v1.0.10-alpha
------------

### Changed

- Fixed nil assignment on Ws client of Trade
- Added request `ID` to trade requests
- Added new event `Success` to handle trade success response

v1.0.9-alpha
------------

Expand Down
35 changes: 27 additions & 8 deletions api/ws/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,15 @@ import (
//
// https://www.okex.com/docs-v5/en/#websocket-api
type ClientWs struct {
AuthorizedUntil *time.Time
Cancel context.CancelFunc
DoneChan chan interface{}
StructuredEventChan chan interface{}
RawEventChan chan *events.Basic
ErrChan chan *events.Error
LoginChan chan *events.Login
StructuredEventChan chan interface{}
Private *Private
Public *Public
Trade *Trade
SubscribeChan chan *events.Subscribe
UnsubscribeCh chan *events.Unsubscribe
LoginChan chan *events.Login
SuccessChan chan *events.Success
sendChan map[bool]chan []byte
url map[bool]okex.BaseURL
conn map[bool]*websocket.Conn
Expand All @@ -40,6 +37,10 @@ type ClientWs struct {
lastTransmit map[bool]*time.Time
mu map[bool]*sync.Mutex
rmu map[bool]*sync.Mutex
AuthorizedUntil *time.Time
Private *Private
Public *Public
Trade *Trade
ctx context.Context
}

Expand Down Expand Up @@ -71,6 +72,7 @@ func NewClient(ctx context.Context, apiKey, secretKey, passphrase string, url ma
}
c.Private = NewPrivate(c)
c.Public = NewPublic(c)
c.Trade = NewTrade(c)

return c
}
Expand Down Expand Up @@ -162,7 +164,7 @@ func (c *ClientWs) Unsubscribe(p bool, ch []okex.ChannelName, args map[string]st
}

// Send message through either connections
func (c *ClientWs) Send(p bool, op okex.Operation, args []map[string]string) error {
func (c *ClientWs) Send(p bool, op okex.Operation, args []map[string]string, extras ...map[string]string) error {
err := c.Connect(p)
if err != nil {
return err
Expand All @@ -172,6 +174,11 @@ func (c *ClientWs) Send(p bool, op okex.Operation, args []map[string]string) err
"op": op,
"args": args,
}
for _, extra := range extras {
for k, v := range extra {
data[k] = v
}
}
j, err := json.Marshal(data)
if err != nil {
return err
Expand All @@ -183,11 +190,12 @@ func (c *ClientWs) Send(p bool, op okex.Operation, args []map[string]string) err
}

// SetChannels to receive certain events on separate channel
func (c *ClientWs) SetChannels(errCh chan *events.Error, subCh chan *events.Subscribe, unSub chan *events.Unsubscribe, lCh chan *events.Login) {
func (c *ClientWs) SetChannels(errCh chan *events.Error, subCh chan *events.Subscribe, unSub chan *events.Unsubscribe, lCh chan *events.Login, sCh chan *events.Success) {
c.ErrChan = errCh
c.SubscribeChan = subCh
c.UnsubscribeCh = unSub
c.LoginChan = lCh
c.SuccessChan = sCh
}

func (c *ClientWs) dial(p bool) error {
Expand Down Expand Up @@ -358,6 +366,17 @@ func (c *ClientWs) process(data []byte, e *events.Basic) bool {
}()
return true
}
if e.ID != "" {
e := events.Success{}
_ = json.Unmarshal(data, &e)
go func() {
if c.SuccessChan != nil {
c.SuccessChan <- &e
}
c.StructuredEventChan <- e
}()
return true
}
if c.Private.Process(data, e) {
return true
}
Expand Down
6 changes: 3 additions & 3 deletions api/ws/trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (c *Trade) PlaceOrder(req ...requests.PlaceOrder) error {
}
c.waitForAuthorization()

return c.Send(true, op, tmpArgs)
return c.Send(true, op, tmpArgs, map[string]string{"id": req[0].ID})
}

// CancelOrder
Expand All @@ -67,7 +67,7 @@ func (c *Trade) CancelOrder(req ...requests.CancelOrder) error {
}
c.waitForAuthorization()

return c.Send(true, op, tmpArgs)
return c.Send(true, op, tmpArgs, map[string]string{"id": req[0].ID})
}

// AmendOrder
Expand All @@ -93,7 +93,7 @@ func (c *Trade) AmendOrder(req ...requests.AmendOrder) error {
}
c.waitForAuthorization()

return c.Send(true, op, tmpArgs)
return c.Send(true, op, tmpArgs, map[string]string{"id": req[0].ID})
}

func (c *Trade) waitForAuthorization() {
Expand Down
10 changes: 9 additions & 1 deletion events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

type (
Basic struct {
ID string `json:"id,omitempty"`
Event string `json:"event"`
Code int `json:"code,omitempty,string"`
Msg string `json:"msg,omitempty"`
Expand All @@ -19,12 +20,19 @@ type (
arg map[string]interface{}
untypedArg []interface{}
}
Success struct {
Code int `json:"code,omitempty,string"`
Msg string `json:"msg,omitempty"`
ID string `json:"id,omitempty"`
Op okex.Operation `json:"op,omitempty"`
Data []*Argument `json:"data,omitempty"`
}
Error struct {
Event string `json:"event,omitempty"`
Msg string `json:"msg,omitempty"`
Op string `json:"op,omitempty"`
Code okex.JSONInt64 `json:"code"`
Id okex.JSONInt64 `json:"id,omitempty"`
ID string `json:"id,omitempty"`
}
Login struct {
Event string `json:"event"`
Expand Down
25 changes: 14 additions & 11 deletions requests/rest/trade/trade_requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,28 @@ import (

type (
PlaceOrder struct {
ID string `json:"-"`
InstID string `json:"instId"`
Ccy string `json:"ccy,omitempty"`
ClOrdID string `json:"clOrdId,omitempty"`
Tag string `json:"tag,omitempty"`
ReduceOnly bool `json:"reduceOnly,omitempty"`
Sz float64 `json:"sz,string"`
Px float64 `json:"px,omitempty,string"`
TdMode okex.TradeMode `json:"tdMode,string"`
Side okex.OrderSide `json:"side,string"`
PosSide okex.PositionSide `json:"posSide,string"`
OrdType okex.OrderType `json:"ordType,string"`
TgtCcy okex.QuantityType `json:"tgtCcy,string,omitempty"`
TdMode okex.TradeMode `json:"tdMode"`
Side okex.OrderSide `json:"side"`
PosSide okex.PositionSide `json:"posSide,omitempty"`
OrdType okex.OrderType `json:"ordType"`
TgtCcy okex.QuantityType `json:"tgtCcy,omitempty"`
}
CancelOrder struct {
ID string `json:"-"`
InstID string `json:"instId"`
OrdID string `json:"ordId,omitempty"`
ClOrdID string `json:"clOrdId,omitempty"`
}
AmendOrder struct {
ID string `json:"-"`
InstID string `json:"instId"`
OrdID string `json:"ordId,omitempty"`
ClOrdID string `json:"clOrdId,omitempty"`
Expand All @@ -36,8 +39,8 @@ type (
ClosePosition struct {
InstID string `json:"instId"`
Ccy string `json:"ccy,omitempty"`
PosSide okex.PositionSide `json:"posSide,omitempty,string"`
MgnMode okex.MarginMode `json:"mgnMode,string"`
PosSide okex.PositionSide `json:"posSide,omitempty"`
MgnMode okex.MarginMode `json:"mgnMode"`
}
OrderDetails struct {
InstID string `json:"instId"`
Expand All @@ -50,9 +53,9 @@ type (
After float64 `json:"after,omitempty,string"`
Before float64 `json:"before,omitempty,string"`
Limit float64 `json:"limit,omitempty,string"`
InstType okex.InstrumentType `json:"instType,omitempty,string"`
OrdType okex.OrderType `json:"ordType,omitempty,string"`
State okex.OrderState `json:"state,omitempty,string"`
InstType okex.InstrumentType `json:"instType,omitempty"`
OrdType okex.OrderType `json:"ordType,omitempty"`
State okex.OrderState `json:"state,omitempty"`
}
TransactionDetails struct {
Uly string `json:"uly,omitempty"`
Expand All @@ -61,6 +64,6 @@ type (
After float64 `json:"after,omitempty,string"`
Before float64 `json:"before,omitempty,string"`
Limit float64 `json:"limit,omitempty,string"`
InstType okex.InstrumentType `json:"instType,omitempty,string"`
InstType okex.InstrumentType `json:"instType,omitempty"`
}
)

0 comments on commit a6c60aa

Please sign in to comment.