-
-
Notifications
You must be signed in to change notification settings - Fork 269
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
FEATURE: [bitget] support book stream on bitget #1344
FEATURE: [bitget] support book stream on bitget #1344
Conversation
Welcome back! @bailantaotao, This pull request may get 869 BBG. |
556329f
to
f7dcefd
Compare
Re-estimated karma: this pull request may get 861 BBG |
Codecov Report
@@ Coverage Diff @@
## main #1344 +/- ##
==========================================
+ Coverage 20.70% 20.81% +0.10%
==========================================
Files 560 565 +5
Lines 40025 40265 +240
==========================================
+ Hits 8287 8380 +93
- Misses 31136 31276 +140
- Partials 602 609 +7
... and 6 files with indirect coverage changes Continue to review full report in Codecov by Sentry.
|
pkg/exchange/bitget/stream.go
Outdated
|
||
func (s *Stream) handleBooksEvent(o BookEvent) { | ||
for _, book := range o.OrderBooks() { | ||
switch { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
switch o.Type ?
pkg/exchange/bitget/types.go
Outdated
Arg WebsocketArg `json:"arg"` | ||
// "op" and "channel" are exclusive. | ||
*WsOpEvent | ||
*WsChannelEvent |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
embed multiple struct like this may be dangerous?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that is fine because we have a sanity check before calling the pointer struct.
func (w *WsEvent) IsOp() bool {
return w.WsOpEvent != nil && w.WsChannelEvent == nil
}
func (w *WsEvent) IsChannel() bool {
return w.WsOpEvent == nil && w.WsChannelEvent != nil
}
This event is only used in parseWebSocketEvent. If it does not belong to any type, it will return an error.
func (s *Stream) parseWebSocketEvent(in []byte) (interface{}, error) {
var e WsEvent
err := json.Unmarshal(in, &e)
if err != nil {
return nil, err
}
switch {
case e.IsOp():
return e.WsOpEvent, nil
case e.IsChannel():
switch e.Arg.Channel {
case ChannelOrderBook, ChannelOrderBook5, ChannelOrderBook15:
...
return &books, nil
}
}
return nil, fmt.Errorf("unhandled websocket event: %+v", string(in))
}
pkg/exchange/bitget/types.go
Outdated
|
||
// internal use | ||
Type ActionType | ||
instId string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make this field public?
pkg/exchange/bitget/stream.go
Outdated
case types.DepthLevel15: | ||
w.Channel = ChannelOrderBook15 | ||
case types.DepthLevel200: | ||
log.Info("*** The subscription events for the order book may return fewer than 200 bids/asks at a depth of 200. ***") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better to add the exchange name "bitget" in this log.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The log already with the filed
bbgo/pkg/exchange/bitget/exchange.go
Lines 14 to 16 in 5f8bda7
var log = logrus.WithFields(logrus.Fields{ | |
"exchange": ID, | |
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好酷喔,現在可以顯示code了?
pkg/exchange/bitget/stream.go
Outdated
var books BookEvent | ||
err = json.Unmarshal(e.WsChannelEvent.Data, &books.Events) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to unmarshal data into BookEvent: %+v, err: %w", string(e.WsChannelEvent.Data), err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we log e.Arg.Channel
as well? This would allow us to immediately identify which channel is problematic upon encountering the error.
f7dcefd
to
3af0238
Compare
Re-estimated karma: this pull request may get 980 BBG |
3af0238
to
4e4e529
Compare
Re-estimated karma: this pull request may get 985 BBG |
for _, subscription := range s.Subscriptions { | ||
arg, err := convertSubscription(subscription) | ||
if err != nil { | ||
logger.WithError(err).Errorf("convert error, subscription: %+v", subscription) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can simply return the error here I think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, it cannot be. The reason is that the syncSubscriptions
function is invoked by both handleConnect
and Unsubscribe
. However, there is no error returned to the caller to inform them that an error has occurred.
handleConnect
stream.OnConnect(stream.handlerConnect)
func (s *Stream) handlerConnect() {
if s.PublicOnly {
// errors are handled in the syncSubscriptions, so they are skipped here.
_ = s.syncSubscriptions(WsEventSubscribe)
} else {
log.Error("*** PRIVATE API NOT IMPLEMENTED ***")
}
}
Unsubscribe
func (s *Stream) Unsubscribe() {
// errors are handled in the syncSubscriptions, so they are skipped here.
_ = s.syncSubscriptions(WsEventUnsubscribe)
s.Resubscribe(func(old []types.Subscription) (new []types.Subscription, err error) {
// clear the subscriptions
return []types.Subscription{}, nil
})
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 got it, thanks
return fmt.Errorf("unexpected subscription type: %v", opType) | ||
} | ||
|
||
logger := log.WithField("opType", opType) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
opType log tag might not be so helpful?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The op type represents a subscription or unsubscription. If an error occurs, we can identify the type that is causing the problem and address it.
4e4e529
to
843f683
Compare
Re-estimated karma: this pull request may get 1040 BBG |
Re-estimated karma: this pull request may get 1049 BBG |
6e48749
to
c4f651a
Compare
Re-estimated karma: this pull request may get 1054 BBG |
c4f651a
to
f17cf34
Compare
Re-estimated karma: this pull request may get 1059 BBG |
f17cf34
to
51d86ca
Compare
Re-estimated karma: this pull request may get 1072 BBG |
Hi @bailantaotao, Well done! 1077 BBG has been sent to your polygon wallet. Please check the following tx: https://polygonscan.com/tx/0xb4220e34afec278399f53a49ef0f8e72deb548a98aac82c55f2b94d870e4c3e1 Thank you for your contribution! |
books
channel might return less than 200 bids/asks as per symbol's orderbook various from each other; The number of bids/asks is not a fixed value and may vary in the future.Reference
https://bitgetlimited.github.io/apidoc/en/spot/#depth-channel