Skip to content

Commit

Permalink
Binance COIN-M websockets listen on correct address
Browse files Browse the repository at this point in the history
  • Loading branch information
nardew committed May 26, 2021
1 parent 870f7b0 commit 392338d
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 55 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ The project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html

## [Pending release]

## [5.1.1] - 2021-05-26

### Fixed

- `binance` COIN-M futures websockets listen on correct address

## [5.1.0] - 2021-05-25

### Added
Expand Down Expand Up @@ -157,7 +163,8 @@ bitvavo.enums.CandelstickInterval -> bitvavo.enums.CandlestickInterval

The official release of `cryptoxlib-aio`.

[Pending release]: https://github.com/nardew/cryptoxlib-aio/compare/5.1.0...HEAD
[Pending release]: https://github.com/nardew/cryptoxlib-aio/compare/5.1.1...HEAD
[5.1.1]: https://github.com/nardew/cryptoxlib-aio/compare/5.1.0...5.1.1
[5.1.0]: https://github.com/nardew/cryptoxlib-aio/compare/5.0.0...5.1.0
[5.0.0]: https://github.com/nardew/cryptoxlib-aio/compare/4.1.0...5.0.0
[4.1.0]: https://github.com/nardew/cryptoxlib-aio/compare/4.0.0...4.1.0
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# cryptoxlib-aio 5.1.0
# cryptoxlib-aio 5.1.1

![](https://img.shields.io/badge/python-3.6-blue.svg) ![](https://img.shields.io/badge/python-3.7-blue.svg) ![](https://img.shields.io/badge/python-3.8-blue.svg) ![](https://img.shields.io/badge/python-3.9-blue.svg)

Expand Down
10 changes: 8 additions & 2 deletions cryptoxlib/WebsocketMgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,19 @@ def get_websocket(self) -> Websocket:
return self.get_full_websocket()

def get_full_websocket(self) -> Websocket:
return FullWebsocket(websocket_uri = self.websocket_uri + self.get_websocket_uri_variable_part(),
uri = self.websocket_uri + self.get_websocket_uri_variable_part()
LOG.debug(f"Websocket URI: {uri}")

return FullWebsocket(websocket_uri = uri,
builtin_ping_interval = self.builtin_ping_interval,
max_message_size = self.max_message_size,
ssl_context = self.ssl_context)

def get_aiohttp_websocket(self) -> Websocket:
return AiohttpWebsocket(websocket_uri = self.websocket_uri + self.get_websocket_uri_variable_part(),
uri = self.websocket_uri + self.get_websocket_uri_variable_part()
LOG.debug(f"Websocket URI: {uri}")

return AiohttpWebsocket(websocket_uri = uri,
builtin_ping_interval = self.builtin_ping_interval,
max_message_size = self.max_message_size,
ssl_context = self.ssl_context)
Expand Down
2 changes: 1 addition & 1 deletion cryptoxlib/clients/binance/BinanceFuturesClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ async def get_commission_rate(self, symbol: PairSymbolType, recv_window_ms: Opti
async def get_listen_key(self):
return await self._create_post("listenKey", headers = self._get_header(), api_variable_path = self.get_api_v1())

async def keep_alive_listen_key(self, listen_key: str):
async def keep_alive_listen_key(self):
return await self._create_put("listenKey", headers = self._get_header(), api_variable_path = self.get_api_v1())


Expand Down
4 changes: 2 additions & 2 deletions cryptoxlib/clients/binance/BinanceFuturesWebsocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class BinanceCOINMFuturesWebsocket(BinanceCommonWebsocket):
def __init__(self, subscriptions: List[Subscription], binance_client, api_key: str = None, sec_key: str = None,
ssl_context = None) -> None:
super().__init__(subscriptions = subscriptions, binance_client = binance_client, api_key = api_key,
sec_key = sec_key, websocket_uri = BinanceUSDSMFuturesWebsocket.WEBSOCKET_URI,
sec_key = sec_key, websocket_uri = BinanceCOINMFuturesWebsocket.WEBSOCKET_URI,
ssl_context = ssl_context)


Expand All @@ -48,7 +48,7 @@ class BinanceCOINMFuturesTestnetWebsocket(BinanceCommonWebsocket):
def __init__(self, subscriptions: List[Subscription], binance_client, api_key: str = None, sec_key: str = None,
ssl_context = None) -> None:
super().__init__(subscriptions = subscriptions, binance_client = binance_client, api_key = api_key,
sec_key = sec_key, websocket_uri = BinanceUSDSMFuturesTestnetWebsocket.WEBSOCKET_URI,
sec_key = sec_key, websocket_uri = BinanceCOINMFuturesTestnetWebsocket.WEBSOCKET_URI,
ssl_context = ssl_context)


Expand Down
4 changes: 2 additions & 2 deletions examples/binance_coin_m_futures_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ async def run():
# Bundle several subscriptions into a single websocket
client.compose_subscriptions([
OrderBookTickerSubscription(callbacks = [orderbook_ticker_update]),
OrderBookSymbolTickerSubscription(symbol = Pair("BTC", "USDT"), callbacks = [orderbook_ticker_update]),
CandlestickSubscription(interval = Interval.I_1MIN, symbol = Pair('BTC', 'USDT'), callbacks = [candlestick_update])
OrderBookSymbolTickerSubscription(symbol = "BTCUSD_PERP", callbacks = [orderbook_ticker_update]),
CandlestickSubscription(interval = Interval.I_1MIN, symbol = "BTCUSD_PERP", callbacks = [candlestick_update])
])

# Bundle another subscriptions into a separate websocket
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setuptools.setup(
name="cryptoxlib-aio",
version="5.1.0",
version="5.1.1",
author="nardew",
author_email="cryptoxlib.aio@gmail.com",
description="Cryptoexchange asynchronous python client",
Expand Down
60 changes: 15 additions & 45 deletions tests/e2e/binance_coin_m_futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
AllMarketTickersSubscription, MiniTickerSubscription, OrderBookTickerSubscription, \
OrderBookSymbolTickerSubscription, LiquidationOrdersSubscription, BlvtCandlestickSubscription, \
BlvtSubscription, CompositeIndexSubscription, DepthSubscription, CandlestickSubscription, \
ContContractCandlestickSubscription, TickerSubscription, AccountSubscription
ContContractCandlestickSubscription, TickerSubscription
from cryptoxlib.clients.binance.exceptions import BinanceRestException
from cryptoxlib.Pair import Pair

Expand Down Expand Up @@ -138,10 +138,6 @@ def check_positive_response(self, response):
def check_error_code(self, e: BinanceRestException, status: str, code: str):
return str(e.status_code) == status and str(e.body['code']) == code

async def test_get_ping(self):
response = await self.client.ping()
self.assertTrue(self.check_positive_response(response))

async def test_change_position_type(self):
# make sure some position exists in order for the change of position to fail
await self.client.create_order(symbol = "BTCUSD_PERP", side = enums.OrderSide.BUY,
Expand Down Expand Up @@ -304,15 +300,15 @@ async def init_test(self):
async def test_aggregate_trade(self):
message_counter = WsMessageCounter()
self.client.compose_subscriptions([
AggregateTradeSubscription(symbol = "BTCUSDT", callbacks = [message_counter.generate_callback(1)])
AggregateTradeSubscription(symbol = "BTCUSD_PERP", callbacks = [message_counter.generate_callback(1)])
])

await self.assertWsMessageCount(message_counter)

async def test_mark_price(self):
message_counter = WsMessageCounter()
self.client.compose_subscriptions([
MarkPriceSubscription(symbol = Pair("BTC", "USDT"), frequency1sec = True, callbacks = [message_counter.generate_callback(1)])
MarkPriceSubscription(symbol = "BTCUSD_PERP", frequency1sec = True, callbacks = [message_counter.generate_callback(1)])
])

await self.assertWsMessageCount(message_counter)
Expand All @@ -328,15 +324,15 @@ async def test_mark_price_all(self):
async def test_candlestick(self):
message_counter = WsMessageCounter()
self.client.compose_subscriptions([
CandlestickSubscription(symbol = Pair("BTC", "USDT"), interval = enums.Interval.I_1MIN, callbacks = [message_counter.generate_callback(1)])
CandlestickSubscription(symbol = "BTCUSD_PERP", interval = enums.Interval.I_1MIN, callbacks = [message_counter.generate_callback(1)])
])

await self.assertWsMessageCount(message_counter)

async def test_cont_contract_candlestick(self):
message_counter = WsMessageCounter()
self.client.compose_subscriptions([
ContContractCandlestickSubscription(Pair("BTC", "USDT"), enums.Interval.I_1MIN, enums.ContractType.PERPETUAL,
ContContractCandlestickSubscription(Pair("BTC", "USD"), enums.Interval.I_1MIN, enums.ContractType.PERPETUAL,
callbacks = [message_counter.generate_callback(1)])
])

Expand All @@ -353,7 +349,7 @@ async def test_all_market_mini_ticker(self):
async def test_mini_ticker(self):
message_counter = WsMessageCounter()
self.client.compose_subscriptions([
MiniTickerSubscription(Pair('BTC', 'USDT'), callbacks = [message_counter.generate_callback(1)])
MiniTickerSubscription(symbol = "BTCUSD_PERP", callbacks = [message_counter.generate_callback(1)])
])

await self.assertWsMessageCount(message_counter)
Expand All @@ -369,7 +365,7 @@ async def test_all_market_ticker(self):
async def test_ticker(self):
message_counter = WsMessageCounter()
self.client.compose_subscriptions([
TickerSubscription(Pair('BTC', 'USDT'), callbacks = [message_counter.generate_callback(1)])
TickerSubscription(symbol = "BTCUSD_PERP", callbacks = [message_counter.generate_callback(1)])
])

await self.assertWsMessageCount(message_counter)
Expand All @@ -385,20 +381,21 @@ async def test_best_orderbook_ticker(self):
async def test_best_orderbook_symbol_ticker(self):
message_counter = WsMessageCounter()
self.client.compose_subscriptions([
OrderBookSymbolTickerSubscription(Pair('BTC', 'USDT'), callbacks = [message_counter.generate_callback(1)])
OrderBookSymbolTickerSubscription(symbol = "BTCUSD_PERP", callbacks = [message_counter.generate_callback(1)])
])

await self.assertWsMessageCount(message_counter)

# fails since normally there are no liquidation orders
async def liquidation_orders(self):
async def test_liquidation_orders(self):
message_counter = WsMessageCounter()
self.client.compose_subscriptions([
LiquidationOrdersSubscription(Pair('BTC', 'USDT'), callbacks = [message_counter.generate_callback(1)])
LiquidationOrdersSubscription(symbol = "BTCUSD_PERP", callbacks = [message_counter.generate_callback(1)])
])

await self.assertWsMessageCount(message_counter)

# fails since normally there are no liquidation orders
async def test_all_liquidation_orders(self):
message_counter = WsMessageCounter()
self.client.compose_subscriptions([
Expand All @@ -410,58 +407,31 @@ async def test_all_liquidation_orders(self):
async def test_partial_detph(self):
message_counter = WsMessageCounter()
self.client.compose_subscriptions([
DepthSubscription(symbol = Pair('BTC', 'USDT'), level = 5, frequency = 100, callbacks = [message_counter.generate_callback(1)])
DepthSubscription(symbol = "BTCUSD_PERP", level = 5, frequency = 100, callbacks = [message_counter.generate_callback(1)])
])

await self.assertWsMessageCount(message_counter)

async def test_partial_detph2(self):
message_counter = WsMessageCounter()
self.client.compose_subscriptions([
DepthSubscription(symbol = Pair('BTC', 'USDT'), level = 5, callbacks = [message_counter.generate_callback(1)])
DepthSubscription(symbol = "BTCUSD_PERP", level = 5, callbacks = [message_counter.generate_callback(1)])
])

await self.assertWsMessageCount(message_counter)

async def test_detph(self):
message_counter = WsMessageCounter()
self.client.compose_subscriptions([
DepthSubscription(symbol = Pair('BTC', 'USDT'), level = 0, frequency = 100, callbacks = [message_counter.generate_callback(1)])
DepthSubscription(symbol = "BTCUSD_PERP", level = 0, frequency = 100, callbacks = [message_counter.generate_callback(1)])
])

await self.assertWsMessageCount(message_counter)

async def test_detph2(self):
message_counter = WsMessageCounter()
self.client.compose_subscriptions([
DepthSubscription(symbol = Pair('BTC', 'USDT'), level = 0, callbacks = [message_counter.generate_callback(1)])
])

await self.assertWsMessageCount(message_counter)

async def test_blvt(self):
message_counter = WsMessageCounter()
self.client.compose_subscriptions([
BlvtSubscription(Pair('BTC', 'UP'), callbacks = [message_counter.generate_callback(1)])
])

await self.assertWsMessageCount(message_counter)

# typically no data are received
async def blvt_candlesticks(self):
message_counter = WsMessageCounter()
self.client.compose_subscriptions([
BlvtCandlestickSubscription(Pair('BTC', 'UP'), enums.Interval.I_1MIN,
callbacks = [message_counter.generate_callback(1)])
])

await self.assertWsMessageCount(message_counter, timeout = 60)

async def test_composite_index(self):
message_counter = WsMessageCounter()
self.client.compose_subscriptions([
CompositeIndexSubscription(Pair('DEFI', 'USDT'),
callbacks = [message_counter.generate_callback(1)])
DepthSubscription(symbol = "BTCUSD_PERP", level = 0, callbacks = [message_counter.generate_callback(1)])
])

await self.assertWsMessageCount(message_counter)
Expand Down

0 comments on commit 392338d

Please sign in to comment.