Skip to content

Commit

Permalink
Deribit: Add Subscription configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Aug 30, 2024
1 parent cbf98c0 commit bba8757
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 462 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ Binaries will be published once the codebase reaches a stable condition.

|User|Contribution Amount|
|--|--|
| [thrasher-](https://github.com/thrasher-) | 692 |
| [shazbert](https://github.com/shazbert) | 333 |
| [dependabot[bot]](https://github.com/apps/dependabot) | 293 |
| [thrasher-](https://github.com/thrasher-) | 696 |
| [shazbert](https://github.com/shazbert) | 337 |
| [dependabot[bot]](https://github.com/apps/dependabot) | 299 |
| [gloriousCode](https://github.com/gloriousCode) | 234 |
| [dependabot-preview[bot]](https://github.com/apps/dependabot-preview) | 88 |
| [gbjk](https://github.com/gbjk) | 80 |
| [gbjk](https://github.com/gbjk) | 82 |
| [xtda](https://github.com/xtda) | 47 |
| [lrascao](https://github.com/lrascao) | 27 |
| [Beadko](https://github.com/Beadko) | 17 |
Expand Down
21 changes: 16 additions & 5 deletions cmd/documentation/exchanges_templates/deribit.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,23 @@ if err != nil {
}
```

### How to do Websocket public/private calls
### Subscriptions

```go
// Exchanges will be abstracted out in further updates and examples will be
// supplied then
```
All default subscriptions are for all enabled assets.

Default Public Subscriptions:
- Candles ( Timeframe: 1 day )
- Orderbook ( Full depth @ Interval: 100ms )
- Ticker ( Interval: 100ms )
- All Trades ( Interval: 100ms )

Default Authenticated Subscriptions:
- My Account Orders
- My Account Trades

kline.Raw Interval configurable for a raw orderbook subscription when authenticated

Subscriptions are subject to enabled assets and pairs.

### Please click GoDocs chevron above to view current GoDoc information for this package
{{template "contributions"}}
Expand Down
21 changes: 16 additions & 5 deletions exchanges/deribit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,23 @@ if err != nil {
}
```

### How to do Websocket public/private calls
### Subscriptions

```go
// Exchanges will be abstracted out in further updates and examples will be
// supplied then
```
All default subscriptions are for all enabled assets.

Default Public Subscriptions:
- Candles ( Timeframe: 1 day )
- Orderbook ( Full depth @ Interval: 100ms )
- Ticker ( Interval: 100ms )
- All Trades ( Interval: 100ms )

Default Authenticated Subscriptions:
- My Account Orders
- My Account Trades

kline.Raw Interval configurable for a raw orderbook subscription when authenticated

Subscriptions are subject to enabled assets and pairs.

### Please click GoDocs chevron above to view current GoDoc information for this package

Expand Down
76 changes: 73 additions & 3 deletions exchanges/deribit/deribit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ import (
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-corp/gocryptotrader/exchanges/sharedtestvalues"
"github.com/thrasher-corp/gocryptotrader/exchanges/subscription"
"github.com/thrasher-corp/gocryptotrader/exchanges/ticker"
"github.com/thrasher-corp/gocryptotrader/exchanges/trade"
testexch "github.com/thrasher-corp/gocryptotrader/internal/testing/exchange"
testsubs "github.com/thrasher-corp/gocryptotrader/internal/testing/subscriptions"
"github.com/thrasher-corp/gocryptotrader/portfolio/withdraw"
)

Expand Down Expand Up @@ -3268,9 +3270,77 @@ func setupWs() {

func TestGenerateSubscriptions(t *testing.T) {
t.Parallel()
result, err := d.generateSubscriptions()
require.NoError(t, err)
assert.NotNil(t, result)

d := new(Deribit) //nolint:govet // Intentional lexical scope shadow
require.NoError(t, testexch.Setup(d), "Test instance Setup must not error")

d.Websocket.SetCanUseAuthenticatedEndpoints(true)
subs, err := d.generateSubscriptions()
require.NoError(t, err)
exp := subscription.List{}
for _, s := range d.Features.Subscriptions {
for _, a := range d.GetAssetTypes(true) {
pairs, err := d.GetEnabledPairs(a)
require.NoErrorf(t, err, "GetEnabledPairs %s must not error", a)
s := s.Clone() //nolint:govet // Intentional lexical scope shadow
s.Asset = a
if isSymbolChannel(s) {
for i, p := range pairs {
s := s.Clone() //nolint:govet // Intentional lexical scope shadow
s.QualifiedChannel = channelName(s) + "." + p.String()
if s.Interval != 0 {
s.QualifiedChannel += "." + channelInterval(s)
}
s.Pairs = pairs[i : i+1]
exp = append(exp, s)
}
} else {
s.Pairs = pairs
s.QualifiedChannel = channelName(s)
exp = append(exp, s)
}
}
}
testsubs.EqualLists(t, exp, subs)
}

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

for _, i := range []int{1, 3, 5, 10, 15, 30, 60, 120, 180, 360, 720} {
a := channelInterval(&subscription.Subscription{Channel: subscription.CandlesChannel, Interval: kline.Interval(i * int(time.Minute))})
assert.Equal(t, strconv.Itoa(i), a)
}

a := channelInterval(&subscription.Subscription{Channel: subscription.CandlesChannel, Interval: kline.OneDay})
assert.Equal(t, "1D", a)

assert.Panics(t, func() {
channelInterval(&subscription.Subscription{Channel: subscription.CandlesChannel, Interval: kline.OneMonth})
})

a = channelInterval(&subscription.Subscription{Channel: subscription.OrderbookChannel, Interval: kline.ThousandMilliseconds})
assert.Equal(t, "agg2", a, "1 second should expand to agg2")

a = channelInterval(&subscription.Subscription{Channel: subscription.OrderbookChannel, Interval: kline.HundredMilliseconds})
assert.Equal(t, "100ms", a, "100ms should expand correctly")

a = channelInterval(&subscription.Subscription{Channel: subscription.OrderbookChannel, Interval: kline.Raw})
assert.Equal(t, "raw", a, "raw should expand correctly")

assert.Panics(t, func() {
channelInterval(&subscription.Subscription{Channel: subscription.OrderbookChannel, Interval: kline.OneMonth})
})

a = channelInterval(&subscription.Subscription{Channel: userAccessLogChannel})
assert.Empty(t, a, "Anything else should return empty")
}

func TestChannelName(t *testing.T) {
t.Parallel()
assert.Equal(t, tickerChannel, channelName(&subscription.Subscription{Channel: subscription.TickerChannel}))
assert.Equal(t, userLockChannel, channelName(&subscription.Subscription{Channel: userLockChannel}))
assert.Panics(t, func() { channelName(&subscription.Subscription{Channel: "wibble"}) }, "Unknown channels should panic")
}

func TestFetchTicker(t *testing.T) {
Expand Down
Loading

0 comments on commit bba8757

Please sign in to comment.