Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes adshao/go-binance#612 #620

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions v2/futures/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ const (
UserDataEventTypeAccountUpdate UserDataEventType = "ACCOUNT_UPDATE"
UserDataEventTypeOrderTradeUpdate UserDataEventType = "ORDER_TRADE_UPDATE"
UserDataEventTypeAccountConfigUpdate UserDataEventType = "ACCOUNT_CONFIG_UPDATE"
UserDataEventTypeTradeLite UserDataEventType = "TRADE_LITE"

UserDataEventReasonTypeDeposit UserDataEventReasonType = "DEPOSIT"
UserDataEventReasonTypeWithdraw UserDataEventReasonType = "WITHDRAW"
Expand Down
128 changes: 92 additions & 36 deletions v2/futures/websocket_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"time"

"github.com/bitly/go-simplejson"
)

// Endpoints
Expand Down Expand Up @@ -995,50 +996,105 @@ func WsCompositiveIndexServe(symbol string, handler WsCompositeIndexHandler, err

// WsUserDataEvent define user data event
type WsUserDataEvent struct {
Event UserDataEventType `json:"e"`
Time int64 `json:"E"`
CrossWalletBalance string `json:"cw"`
MarginCallPositions []WsPosition `json:"p"`
TransactionTime int64 `json:"T"`
AccountUpdate WsAccountUpdate `json:"a"`
OrderTradeUpdate WsOrderTradeUpdate `json:"o"`
Event UserDataEventType `json:"e"`
Time int64 `json:"E"`
TransactionTime int64 `json:"T"`

// listenKeyExpired only have Event and Time
//

// MARGIN_CALL
WsUserDataMarginCall

// ACCOUNT_UPDATE
WsUserDataAccountUpdate

// ORDER_TRADE_UPDATE
WsUserDataOrderTradeUpdate

// ACCOUNT_CONFIG_UPDATE
WsUserDataAccountConfigUpdate

// TRADE_LITE
WsUserDataTradeLite
}

type WsUserDataAccountConfigUpdate struct {
AccountConfigUpdate WsAccountConfigUpdate `json:"ac"`
}

type WsUserDataAccountUpdate struct {
AccountUpdate WsAccountUpdate `json:"a"`
}

type WsUserDataMarginCall struct {
CrossWalletBalance string `json:"cw"`
MarginCallPositions []WsPosition `json:"p"`
}

type WsUserDataOrderTradeUpdate struct {
OrderTradeUpdate WsOrderTradeUpdate `json:"o"`
}

type WsUserDataTradeLite struct {
Symbol string `json:"s"`
OriginalQty string `json:"q"`
OriginalPrice string //`json:"p"`
IsMaker bool `json:"m"`
ClientOrderID string `json:"c"`
Side SideType `json:"S"`
LastFilledPrice string `json:"L"`
LastFilledQty string `json:"l"`
TradeID int64 `json:"t"`
OrderID int64 `json:"i"`
}

func (w *WsUserDataTradeLite) fromSimpleJson(j *simplejson.Json) (err error) {
w.Symbol = j.Get("s").MustString()
w.OriginalQty = j.Get("q").MustString()
w.OriginalPrice = j.Get("p").MustString()
w.IsMaker = j.Get("m").MustBool()
w.ClientOrderID = j.Get("c").MustString()
w.Side = SideType(j.Get("S").MustString())
w.LastFilledPrice = j.Get("L").MustString()
w.LastFilledQty = j.Get("l").MustString()
w.TradeID = j.Get("t").MustInt64()
w.OrderID = j.Get("i").MustInt64()
return nil
}

func (e *WsUserDataEvent) UnmarshalJSON(data []byte) error {
var tmp struct {
Event UserDataEventType `json:"e"`
Time interface{} `json:"E"`
CrossWalletBalance string `json:"cw"`
MarginCallPositions []WsPosition `json:"p"`
TransactionTime int64 `json:"T"`
AccountUpdate WsAccountUpdate `json:"a"`
OrderTradeUpdate WsOrderTradeUpdate `json:"o"`
AccountConfigUpdate WsAccountConfigUpdate `json:"ac"`
}
if err := json.Unmarshal(data, &tmp); err != nil {
j, err := newJSON(data)
if err != nil {
return err
}
e.Event = UserDataEventType(j.Get("e").MustString())
e.Time = j.Get("E").MustInt64()
if v, ok := j.CheckGet("T"); ok {
e.TransactionTime = v.MustInt64()
}

e.Event = tmp.Event
switch v := tmp.Time.(type) {
case float64:
e.Time = int64(v)
case string:
parsedTime, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return err
}
e.Time = parsedTime
eventMaps := map[UserDataEventType]any{
UserDataEventTypeMarginCall: &e.WsUserDataMarginCall,
UserDataEventTypeAccountUpdate: &e.WsUserDataAccountUpdate,
UserDataEventTypeOrderTradeUpdate: &e.WsUserDataOrderTradeUpdate,
UserDataEventTypeAccountConfigUpdate: &e.WsUserDataAccountConfigUpdate,
}

switch e.Event {
case UserDataEventTypeTradeLite:
return e.WsUserDataTradeLite.fromSimpleJson(j)
case UserDataEventTypeListenKeyExpired:
// noting
default:
return fmt.Errorf("unexpected type for E: %T", tmp.Time)
if v, ok := eventMaps[e.Event]; ok {
if err := json.Unmarshal(data, v); err != nil {
return err
}
} else {
return fmt.Errorf("unexpected event type: %v", e.Event)
}
}
e.CrossWalletBalance = tmp.CrossWalletBalance
e.MarginCallPositions = tmp.MarginCallPositions
e.TransactionTime = tmp.TransactionTime
e.AccountUpdate = tmp.AccountUpdate
e.OrderTradeUpdate = tmp.OrderTradeUpdate
e.AccountConfigUpdate = tmp.AccountConfigUpdate
return nil
}

Expand Down
Loading
Loading