Skip to content

Commit

Permalink
Merge pull request #1923 from c9s/dboy/coinbase-stream-pr2
Browse files Browse the repository at this point in the history
FEAT: [coinbase] add more message parsing, enhance stream functionality
  • Loading branch information
dboyliao authored Mar 6, 2025
2 parents baee9a8 + 548b333 commit 874c7f8
Show file tree
Hide file tree
Showing 6 changed files with 725 additions and 134 deletions.
12 changes: 10 additions & 2 deletions pkg/exchange/coinbase/exchage.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,21 @@ var log = logrus.WithField("exchange", ID)

type Exchange struct {
client *api.RestAPIClient

// api keys
apiKey string
apiSecret string
apiPassphrase string
}

func New(key, secret, passphrase string, timeout time.Duration) *Exchange {
client := api.NewClient(key, secret, passphrase, timeout)
return &Exchange{
client: client,

apiKey: key,
apiSecret: secret,
apiPassphrase: passphrase,
}
}

Expand Down Expand Up @@ -261,8 +270,7 @@ func (e *Exchange) CancelOrders(ctx context.Context, orders ...types.Order) erro

// ExchangeMarketDataService
func (e *Exchange) NewStream() types.Stream {
// TODO: implement stream
return nil
return NewStream(e, e.apiKey, e.apiPassphrase, e.apiSecret)
}

func (e *Exchange) QueryMarkets(ctx context.Context) (types.MarketMap, error) {
Expand Down
23 changes: 22 additions & 1 deletion pkg/exchange/coinbase/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

// See https://docs.cdp.coinbase.com/exchange/docs/websocket-channels for message types
func (s *Stream) parseMessage(data []byte) (interface{}, error) {
func parseMessage(data []byte) (interface{}, error) {
var baseMsg messageBaseType
err := json.Unmarshal(data, &baseMsg)
if err != nil {
Expand Down Expand Up @@ -91,6 +91,27 @@ func (s *Stream) parseMessage(data []byte) (interface{}, error) {
return nil, err
}
return &activeMsg, nil
case "balance":
var balanceMsg BalanceMessage
err = json.Unmarshal(data, &balanceMsg)
if err != nil {
return nil, err
}
return &balanceMsg, nil
case "snapshot":
var snapshotMsg OrderBookSnapshotMessage
err = json.Unmarshal(data, &snapshotMsg)
if err != nil {
return nil, err
}
return &snapshotMsg, nil
case "l2update":
var updateMsg OrderBookUpdateMessage
err = json.Unmarshal(data, &updateMsg)
if err != nil {
return nil, err
}
return &updateMsg, nil
}
return nil, fmt.Errorf("unknown message type: %s", baseMsg.Type)
}
Loading

0 comments on commit 874c7f8

Please sign in to comment.