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

stream: force subscription store check as stop gap for wrapper side implementation #1717

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
14 changes: 14 additions & 0 deletions exchanges/deribit/deribit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4129,3 +4129,17 @@ func TestGetCurrencyTradeURL(t *testing.T) {
require.NoError(t, err)
assert.NotEmpty(t, resp)
}

func TestFormatPerpetualPairWithSettlement(t *testing.T) {
t.Parallel()

pair := currency.NewPair(currency.BTC, currency.NewCode("USDC-PERPETUAL"))
pair.Delimiter = "-"
result := formatPerpetualPairWithSettlement(pair)
assert.Equal(t, "BTC_USDC-PERPETUAL", result)

pair = currency.NewPair(currency.BTC, currency.NewCode("PERPETUAL"))
pair.Delimiter = "-"
result = formatPerpetualPairWithSettlement(pair)
assert.Equal(t, "BTC-PERPETUAL", result)
}
35 changes: 23 additions & 12 deletions exchanges/deribit/deribit_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ var (
}
)

var errSubscriptionFailureDetected = errors.New("subscription failure detected")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like it's stream.ErrSubscriptionFailure in disguise 🤠


// WsConnect starts a new connection with the websocket API
func (d *Deribit) WsConnect() error {
if !d.Websocket.IsEnabled() || !d.IsEnabled() {
Expand Down Expand Up @@ -779,6 +781,7 @@ func (d *Deribit) GetSubscriptionTemplate(_ *subscription.Subscription) (*templa
"channelName": channelName,
"interval": channelInterval,
"isSymbolChannel": isSymbolChannel,
"fmt": formatPerpetualPairWithSettlement,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Don't think we can call this fmt when it's specific to Futures.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It calls it against every pair, are you asking me to only call it against futures?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. But I think it's misleading that it's not really format for every pair, even if it's called for all of them.
So I guess I'd vote for: Rename the template func to fmtFuturePairs or rename the func called to formatChannelPair.
What's catching me is the disconnect between what's in the template and the actual func name.
Can also ignore.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}).
Parse(subTplText)
}
Expand All @@ -805,18 +808,19 @@ func (d *Deribit) handleSubscription(method string, subs subscription.List) erro
if err != nil || len(subs) == 0 {
return err
}

r := WsSubscriptionInput{
JSONRPCVersion: rpcVersion,
ID: d.Websocket.Conn.GenerateMessageID(false),
Method: method,
Params: map[string][]string{
"channels": subs.QualifiedChannels(),
},
Params: map[string][]string{"channels": subs.QualifiedChannels()},
}

data, err := d.Websocket.Conn.SendMessageReturnResponse(context.TODO(), request.Unset, r.ID, r)
if err != nil {
return err
}

var response wsSubscriptionResponse
err = json.Unmarshal(data, &response)
if err != nil {
Expand All @@ -827,23 +831,23 @@ func (d *Deribit) handleSubscription(method string, subs subscription.List) erro
subAck[c] = true
}
if len(subAck) != len(subs) {
err = common.ErrUnknownError
err = errSubscriptionFailureDetected
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, this would be covering ... any subs that come back that we didn't expect, which won't be reported in the for range below?
It feels like it'd be better to delete from subAck as we range subs, and then error to say "Unexpected channels in Result" or something.
Cos otherwise, this error will be impossible to diagnose and I'd probably say misleading.

}
for _, s := range subs {
if _, ok := subAck[s.QualifiedChannel]; ok {
err = common.AppendError(err, d.Websocket.AddSuccessfulSubscriptions(d.Websocket.Conn, s))
if strings.Contains(method, "subscribe") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't tested yet, but "unsubscribe" contains the string "subscribe" and so both would likely fall in here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

err = common.AppendError(err, d.Websocket.AddSuccessfulSubscriptions(d.Websocket.Conn, s))
} else {
err = common.AppendError(err, d.Websocket.RemoveSubscriptions(d.Websocket.Conn, s))
}
} else {
err = common.AppendError(err, errors.New(s.String()))
err = common.AppendError(err, errors.New(s.String()+"failed to"+method))
gbjk marked this conversation as resolved.
Show resolved Hide resolved
}
}
if err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if err != nil {
return err

return err
}

if method == "unsubscribe" {
return d.Websocket.RemoveSubscriptions(d.Websocket.Conn, subs...)
}
return d.Websocket.AddSubscriptions(d.Websocket.Conn, subs...)
return nil
}

func getValidatedCurrencyCode(pair currency.Pair) string {
Expand Down Expand Up @@ -906,11 +910,18 @@ func isSymbolChannel(s *subscription.Subscription) bool {
return false
}

func formatPerpetualPairWithSettlement(pair currency.Pair) string {
if str := pair.Quote.String(); strings.Contains(str, "PERPETUAL") && strings.Contains(str, "-") {
pair.Delimiter = "_"
}
return pair.String()
}

const subTplText = `
{{- if isSymbolChannel $.S -}}
{{- range $asset, $pairs := $.AssetPairs }}
{{- range $p := $pairs }}
{{- channelName $.S -}} . {{- $p }}
{{- channelName $.S -}} . {{- fmt $p }}
{{- with $i := interval $.S -}} . {{- $i }}{{ end }}
{{- $.PairSeparator }}
{{- end }}
Expand Down
Loading