ftx: define subscribed msg

This commit is contained in:
ycdesu 2021-02-27 19:27:26 +08:00
parent 73d05fe7bb
commit d9ad022a81
2 changed files with 61 additions and 0 deletions

View File

@ -1,5 +1,7 @@
package ftx
import "encoding/json"
type operation string
const subscribe operation = "subscribe"
@ -17,3 +19,44 @@ type SubscribeRequest struct {
Channel channel `json:"channel"`
Market string `json:"market"`
}
type respType string
const errRespType respType = "error"
const subscribedRespType respType = "subscribed"
const unsubscribedRespType respType = "unsubscribed"
const infoRespType respType = "info"
const partialRespType respType = "partial"
const updateRespType respType = "update"
type mandatoryFields struct {
Type respType `json:"type"`
// Channel is mandatory
Channel channel `json:"channel"`
// Market is mandatory
Market string `json:"market"`
}
// doc: https://docs.ftx.com/#response-format
type rawResponse struct {
mandatoryFields
// The following fields are optional.
// Example 1: {"type": "error", "code": 404, "msg": "No such market: BTCUSDT"}
Code int64 `json:"code"`
Message string `json:"msg"`
Data map[string]json.RawMessage `json:"data"`
}
func (r rawResponse) toSubscribedResp() subscribedResponse {
return subscribedResponse{
mandatoryFields: r.mandatoryFields,
}
}
// {"type": "subscribed", "channel": "orderbook", "market": "BTC/USDT"}
type subscribedResponse struct {
mandatoryFields
}

View File

@ -0,0 +1,18 @@
package ftx
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_rawResponse_toSubscribedResp(t *testing.T) {
input := `{"type": "subscribed", "channel": "orderbook", "market": "BTC/USDT"}`
var m rawResponse
assert.NoError(t, json.Unmarshal([]byte(input), &m))
r := m.toSubscribedResp()
assert.Equal(t, subscribedRespType, r.Type)
assert.Equal(t, orderbook, r.Channel)
assert.Equal(t, "BTC/USDT", r.Market)
}