Skip to content

Commit

Permalink
Bitget websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
cranktakular committed Sep 10, 2024
1 parent 63157c6 commit 69a3b18
Show file tree
Hide file tree
Showing 8 changed files with 497 additions and 82 deletions.
3 changes: 3 additions & 0 deletions engine/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/thrasher-corp/gocryptotrader/exchanges/binanceus"
"github.com/thrasher-corp/gocryptotrader/exchanges/bitfinex"
"github.com/thrasher-corp/gocryptotrader/exchanges/bitflyer"
"github.com/thrasher-corp/gocryptotrader/exchanges/bitget"
"github.com/thrasher-corp/gocryptotrader/exchanges/bithumb"
"github.com/thrasher-corp/gocryptotrader/exchanges/bitmex"
"github.com/thrasher-corp/gocryptotrader/exchanges/bitstamp"
Expand Down Expand Up @@ -1002,6 +1003,8 @@ func NewSupportedExchangeByName(name string) (exchange.IBotExchange, error) {
return new(bitfinex.Bitfinex), nil
case "bitflyer":
return new(bitflyer.Bitflyer), nil
case "bitget":
return new(bitget.Bitget), nil
case "bithumb":
return new(bithumb.Bithumb), nil
case "bitmex":
Expand Down
18 changes: 18 additions & 0 deletions engine/websocketroutine_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,24 @@ func (m *WebsocketRoutineManager) websocketDataHandler(exchName string, data int
d)
}
}
case stream.IncompleteKline:
if m.verbose {
log.Infof(log.WebsocketMgr, "%s websocket %s %s incomplete kline updated %+v",
exchName,
m.FormatCurrency(d.Pair),
d.AssetType,
d)
}
case []stream.IncompleteKline:
for x := range d {
if m.verbose {
log.Infof(log.WebsocketMgr, "%s websocket %s %s incomplete kline updated %+v",
exchName,
m.FormatCurrency(d[x].Pair),
d[x].AssetType,
d)
}
}
case *orderbook.Depth:
base, err := d.Retrieve()
if err != nil {
Expand Down
28 changes: 26 additions & 2 deletions exchanges/bitget/bitget.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,14 @@ const (

// Websocket endpoints
// Unauthenticated
bitgetTickerChannel = "ticker"
bitgetTickerChannel = "ticker"
bitgetCandleDailyChannel = "candle1D" // There's one of these for each time period, but we'll ignore those for now

// Authenticated
bitgetAccountChannel = "account"

errIntervalNotSupported = "interval not supported"
errIntervalNotSupported = "interval not supported"
errAuthenticatedWebsocketDisabled = "%v AuthenticatedWebsocketAPISupport not enabled"
)

var (
Expand Down Expand Up @@ -4632,6 +4635,27 @@ func (t *UnixTimestamp) Time() time.Time {
return time.Time(*t)
}

// UnmarshalJSON unmarshals the JSON input into a UnixTimestampNumber type
func (t *UnixTimestampNumber) UnmarshalJSON(b []byte) error {
var timestampNum uint64
err := json.Unmarshal(b, &timestampNum)
if err != nil {
return err
}
*t = UnixTimestampNumber(time.UnixMilli(int64(timestampNum)).UTC())
return nil
}

// String implements the stringer interface
func (t *UnixTimestampNumber) String() string {
return t.Time().String()
}

// Time returns the time.Time representation of the UnixTimestampNumber
func (t *UnixTimestampNumber) Time() time.Time {
return time.Time(*t)
}

// UnmarshalJSON unmarshals the JSON input into a YesNoBool type
func (y *YesNoBool) UnmarshalJSON(b []byte) error {
var yn string
Expand Down
61 changes: 49 additions & 12 deletions exchanges/bitget/bitget_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ type Params struct {
// UnixTimestamp is a type used to unmarshal unix millisecond timestamps returned from the exchange
type UnixTimestamp time.Time

// UnixTimestampNumber is a type used to unmarshal unix millisecond timestamps returned from the exchange, when they
// aren't provided as strings
type UnixTimestampNumber time.Time

// AnnResp holds information on announcements
type AnnResp struct {
Data []struct {
Expand Down Expand Up @@ -2434,23 +2438,24 @@ type LiquidRecs struct {
// WsResponse contains information on a websocket response
type WsResponse struct {
Event string `json:"event"`
Code int `json:"code,string"`
Code int `json:"code"`
Message string `json:"msg"`
Arg struct {
InstrumentType string `json:"instType"`
Channel string `json:"channel"`
InstrumentID string `json:"instId"`
} `json:"arg"`
Action string `json:"action"`
Data json.RawMessage `json:"data"`
Timestamp UnixTimestamp `json:"ts"`
Action string `json:"action"`
Data json.RawMessage `json:"data"`
Timestamp UnixTimestampNumber `json:"ts"`
}

// WsArgument contains information used in a websocket request
type WsArgument struct {
InstrumentType string `json:"instType"`
Channel string `json:"channel"`
InstrumentID string `json:"instId"`
InstrumentID string `json:"instId,omitempty"`
Coin string `json:"coin,omitempty"`
}

// WsRequest contains information on a websocket request
Expand All @@ -2459,13 +2464,45 @@ type WsRequest struct {
Arguments []WsArgument `json:"args"`
}

// WsLoginArgument contains information usied in a websocket login request

Check failure on line 2467 in exchanges/bitget/bitget_types.go

View workflow job for this annotation

GitHub Actions / Spell checker

usied ==> used, busied
type WsLoginArgument struct {
APIKey string `json:"apiKey"`
Passphrase string `json:"passphrase"`
Timestamp string `json:"timestamp"`
Signature string `json:"sign"`
}

// WsLogin contains information on a websocket login request
type WsLogin struct {
Operation string `json:"op"`
Arguments []struct {
APIKey string `json:"apiKey"`
Passphrase string `json:"passphrase"`
Timestamp string `json:"timestamp"`
Signature string `json:"sign"`
} `json:"args"`
Operation string `json:"op"`
Arguments []WsLoginArgument `json:"args"`
}

// WsTickerSnapshot contains information on a ticker snapshot
type WsTickerSnapshot struct {
InstrumentID string `json:"instId"`
LastPrice float64 `json:"lastPr,string"`
Open24H float64 `json:"open24h,string"`
High24H float64 `json:"high24h,string"`
Low24H float64 `json:"low24h,string"`
Change24H float64 `json:"change24h,string"`
BidPrice float64 `json:"bidPr,string"`
AskPrice float64 `json:"askPr,string"`
BidSize float64 `json:"bidSz,string"`
AskSize float64 `json:"askSz,string"`
BaseVolume float64 `json:"baseVolume,string"`
QuoteVolume float64 `json:"quoteVolume,string"`
OpenUTC float64 `json:"openUtc,string"`
ChangeUTC24H float64 `json:"changeUtc24h,string"`
Timestamp UnixTimestamp `json:"ts"`
}

// WsAccountResponse contains information on an account response
type WsAccountResponse struct {
Coin string `json:"coin"`
Available float64 `json:"available,string"`
Frozen float64 `json:"frozen,string"`
Locked float64 `json:"locked,string"`
LimitAvailable float64 `json:"limitAvailable,string"`
UpdateTime UnixTimestamp `json:"uTime"`
}
Loading

0 comments on commit 69a3b18

Please sign in to comment.