Skip to content

Commit

Permalink
HitBTC: Add subscription configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Dec 12, 2024
1 parent 9917af9 commit f835c80
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 112 deletions.
22 changes: 17 additions & 5 deletions cmd/documentation/exchanges_templates/hitbtc.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,24 @@ 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
```
Subscriptions are for [v2 api](https://hitbtc-com.github.io/hitbtc-api/#socket-api-reference)

All subscriptions are for spot.

Default Public Subscriptions:
- Ticker
- Orderbook
- Candles ( Interval: 30 minutes, History: 100 )
- All Trades ( History: 100 )

Default Authenticated Subscriptions:
- My Account events

Subscriptions are subject to enabled assets and pairs.

Configure Levels for number of history entries to return for applicable APIs.

### Please click GoDocs chevron above to view current GoDoc information for this package
{{template "contributions"}}
Expand Down
22 changes: 17 additions & 5 deletions exchanges/hitbtc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,24 @@ 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
```
Subscriptions are for [v2 api](https://hitbtc-com.github.io/hitbtc-api/#socket-api-reference)

All subscriptions are for spot.

Default Public Subscriptions:
- Ticker
- Orderbook
- Candles ( Interval: 30 minutes, History: 100 )
- All Trades ( History: 100 )

Default Authenticated Subscriptions:
- My Account events

Subscriptions are subject to enabled assets and pairs.

Configure Levels for number of history entries to return for applicable APIs.

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

Expand Down
68 changes: 67 additions & 1 deletion exchanges/hitbtc/hitbtc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import (
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/sharedtestvalues"
"github.com/thrasher-corp/gocryptotrader/exchanges/stream"
"github.com/thrasher-corp/gocryptotrader/exchanges/subscription"
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 @@ -1007,7 +1009,7 @@ func Test_FormatExchangeKlineInterval(t *testing.T) {
test := testCases[x]
t.Run(test.name, func(t *testing.T) {
t.Parallel()
ret, err := h.FormatExchangeKlineInterval(test.interval)
ret, err := formatExchangeKlineInterval(test.interval)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1090,3 +1092,67 @@ func TestGetCurrencyTradeURL(t *testing.T) {
assert.NotEmpty(t, resp)
}
}

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

h := new(HitBTC)
require.NoError(t, testexch.Setup(h), "Test instance Setup must not error")

h.Websocket.SetCanUseAuthenticatedEndpoints(true)
require.True(t, h.Websocket.CanUseAuthenticatedEndpoints(), "CanUseAuthenticatedEndpoints must return true")
subs, err := h.generateSubscriptions()
require.NoError(t, err, "generateSubscriptions must not error")
exp := subscription.List{}
pairs, err := h.GetEnabledPairs(asset.Spot)
require.NoErrorf(t, err, "GetEnabledPairs must not error")
for _, s := range h.Features.Subscriptions {
for _, p := range pairs.Format(currency.PairFormat{Uppercase: true}) {
s = s.Clone()
s.Pairs = currency.Pairs{p}
n := subscriptionNames[s.Channel]
switch s.Channel {
case subscription.MyAccountChannel:
s.QualifiedChannel = `{"method":"` + n + `"}`
case subscription.CandlesChannel:
s.QualifiedChannel = `{"method":"` + n + `","params":{"symbol":"` + p.String() + `","period":"M30","limit":100}}`
case subscription.AllTradesChannel:
s.QualifiedChannel = `{"method":"` + n + `","params":{"symbol":"` + p.String() + `","limit":100}}`
default:
s.QualifiedChannel = `{"method":"` + n + `","params":{"symbol":"` + p.String() + `"}}`
}
exp = append(exp, s)
}
}
testsubs.EqualLists(t, exp, subs)
}

func TestIsSymbolChannel(t *testing.T) {
t.Parallel()
assert.True(t, isSymbolChannel(&subscription.Subscription{Channel: subscription.TickerChannel}))
assert.False(t, isSymbolChannel(&subscription.Subscription{Channel: subscription.MyAccountChannel}))
}

func TestSubToReq(t *testing.T) {
t.Parallel()
p := currency.NewPairWithDelimiter("BTC", "USD", "-")
r := subToReq(&subscription.Subscription{Channel: subscription.TickerChannel}, p)
assert.Equal(t, "Ticker", r.Method)
assert.Equal(t, "BTC-USD", (r.Params.Symbol))

r = subToReq(&subscription.Subscription{Channel: subscription.CandlesChannel, Levels: 4, Interval: kline.OneHour}, p)
assert.Equal(t, "Candles", r.Method)
assert.Equal(t, "H1", r.Params.Period)
assert.Equal(t, 4, r.Params.Limit)
assert.Equal(t, "BTC-USD", (r.Params.Symbol))

r = subToReq(&subscription.Subscription{Channel: subscription.AllTradesChannel, Levels: 150})
assert.Equal(t, "Trades", r.Method)
assert.Equal(t, 150, r.Params.Limit)

assert.PanicsWithError(t,
"subscription channel not supported: myTrades",
func() { subToReq(&subscription.Subscription{Channel: subscription.MyTradesChannel}, p) },
"should panic on invalid channel",
)
}
17 changes: 5 additions & 12 deletions exchanges/hitbtc/hitbtc_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,24 +294,17 @@ type ResponseError struct {

// WsRequest defines a request obj for the JSON-RPC and gets a websocket response
type WsRequest struct {
Method string `json:"method"`
Params WsParams `json:"params,omitempty"`
ID int64 `json:"id"`
}

// WsNotification defines a notification obj for the JSON-RPC this does not get
// a websocket response
type WsNotification struct {
JSONRPCVersion string `json:"jsonrpc,omitempty"`
Method string `json:"method"`
Params WsParams `json:"params"`
JSONRPCVersion string `json:"jsonrpc,omitempty"`
Method string `json:"method"`
Params *WsParams `json:"params,omitempty"`
ID int64 `json:"id,omitempty"`
}

// WsParams are websocket params for a request
type WsParams struct {
Symbol string `json:"symbol,omitempty"`
Period string `json:"period,omitempty"`
Limit int64 `json:"limit,omitempty"`
Limit int `json:"limit,omitempty"`
Symbols []string `json:"symbols,omitempty"`
}

Expand Down
Loading

0 comments on commit f835c80

Please sign in to comment.