fix(ftx): support subaccount in websocket

This commit is contained in:
Jui-Nan Lin 2021-05-21 23:07:53 +08:00
parent 05bde543b7
commit fb47a4882f
4 changed files with 10 additions and 8 deletions

View File

@ -87,7 +87,7 @@ func (e *Exchange) PlatformFeeCurrency() string {
}
func (e *Exchange) NewStream() types.Stream {
return NewStream(e.key, e.secret, e)
return NewStream(e.key, e.secret, e.subAccount, e)
}
func (e *Exchange) QueryMarkets(ctx context.Context) (types.MarketMap, error) {

View File

@ -28,12 +28,13 @@ type Stream struct {
key string
secret string
subAccount string
// subscriptions are only accessed in single goroutine environment, so I don't use mutex to protect them
subscriptions []websocketRequest
}
func NewStream(key, secret string, e *Exchange) *Stream {
func NewStream(key, secret string, subAccount string, e *Exchange) *Stream {
s := &Stream{
exchange: e,
isConnected: false,
@ -47,7 +48,7 @@ func NewStream(key, secret string, e *Exchange) *Stream {
s.ws.OnMessage((&messageHandler{StandardStream: s.StandardStream}).handleMessage)
s.ws.OnConnected(func(conn *websocket.Conn) {
subs := []websocketRequest{newLoginRequest(s.key, s.secret, time.Now())}
subs := []websocketRequest{newLoginRequest(s.key, s.secret, time.Now(), s.subAccount)}
subs = append(subs, s.subscriptions...)
for _, sub := range subs {
if err := conn.WriteJSON(sub); err != nil {

View File

@ -61,7 +61,7 @@ type loginArgs struct {
SubAccount string `json:"subaccount"`
}
func newLoginRequest(key, secret string, t time.Time) websocketRequest {
func newLoginRequest(key, secret string, t time.Time, subsaccount string) websocketRequest {
millis := t.UnixNano() / int64(time.Millisecond)
return websocketRequest{
Operation: login,
@ -69,6 +69,7 @@ func newLoginRequest(key, secret string, t time.Time) websocketRequest {
Key: key,
Signature: sign(secret, loginBody(millis)),
Time: millis,
SubAccount: subsaccount,
},
}
}

View File

@ -180,7 +180,7 @@ func Test_insertAt(t *testing.T) {
func Test_newLoginRequest(t *testing.T) {
// From API doc: https://docs.ftx.com/?javascript#authentication-2
r := newLoginRequest("", "Y2QTHI23f23f23jfjas23f23To0RfUwX3H42fvN-", time.Unix(0, 1557246346499*int64(time.Millisecond)))
r := newLoginRequest("", "Y2QTHI23f23f23jfjas23f23To0RfUwX3H42fvN-", time.Unix(0, 1557246346499*int64(time.Millisecond)), "")
expectedSignature := "d10b5a67a1a941ae9463a60b285ae845cdeac1b11edc7da9977bef0228b96de9"
assert.Equal(t, expectedSignature, r.Login.Signature)
jsonStr, err := json.Marshal(r)