add binance.us support

This commit is contained in:
c9s 2021-12-30 23:46:43 +08:00
parent 38cb4535f0
commit 3c2704c4ae
3 changed files with 28 additions and 11 deletions

View File

@ -88,6 +88,9 @@ Add your dotenv file:
BINANCE_API_KEY=
BINANCE_API_SECRET=
# if you want to use binance.us, change this to 1
BINANCE_US=0
# for MAX exchange, if you have one
MAX_API_KEY=
MAX_API_SECRET=

View File

@ -27,6 +27,12 @@ import (
const BNB = "BNB"
const BinanceUSBaseURL = "https://api.binance.us"
const BinanceUSWebSocketURL = "wss://stream.binance.us:9443"
const BinanceWebSocketURL = "wss://stream.binance.com:9443"
const BinanceFuturesWebSocketURL = "wss://fstream.binance.com"
// 50 per 10 seconds = 5 per second
var orderLimiter = rate.NewLimiter(5, 5)
@ -45,6 +51,11 @@ func init() {
}
}
func isBinanceUs() bool {
v, err := strconv.ParseBool(os.Getenv("BINANCE_US"))
return err == nil && v
}
type Exchange struct {
types.MarginSettings
types.FuturesSettings
@ -62,6 +73,10 @@ func New(key, secret string) *Exchange {
var futuresClient = binance.NewFuturesClient(key, secret)
futuresClient.HTTPClient = &http.Client{Timeout: 15 * time.Second}
if isBinanceUs() {
client.BaseURL = BinanceUSBaseURL
}
var err error
if len(key) > 0 && len(secret) > 0 {
_, err = client.NewSetServerTimeService().Do(context.Background())

View File

@ -300,18 +300,17 @@ func (s *Stream) handleOutboundAccountPositionEvent(e *OutboundAccountPositionEv
func (s *Stream) getEndpointUrl(listenKey string) string {
var url string
if s.PublicOnly {
if s.IsFutures {
url = "wss://fstream.binance.com/ws/"
} else {
url = "wss://stream.binance.com:9443/ws"
}
if s.IsFutures {
url = BinanceFuturesWebSocketURL + "/ws"
} else if isBinanceUs() {
url = BinanceUSWebSocketURL + "/ws"
} else {
if s.IsFutures {
url = "wss://fstream.binance.com/ws/" + listenKey
} else {
url = "wss://stream.binance.com:9443/ws/" + listenKey
}
url = BinanceWebSocketURL + "/ws"
}
if !s.PublicOnly {
url += "/" + listenKey
}
return url