Merge pull request #231 from Davi0kProgramsThings/v3.0.0b5

Merge branch `Davi0kProgramsThings:v3.0.0b5` into branch `bitfinexcom:master`.
This commit is contained in:
Vigan Abdurrahmani
2023-12-14 19:29:30 +01:00
committed by GitHub
4 changed files with 48 additions and 27 deletions

View File

@@ -1 +1 @@
__version__ = "3.0.0b4" __version__ = "3.0.0b5"

View File

@@ -67,8 +67,9 @@ class BfxWebSocketBucket(Connection):
if isinstance(message, list): if isinstance(message, list):
if (chan_id := cast(int, message[0])) and \ if (chan_id := cast(int, message[0])) and \
(subscription := self.__subscriptions.get(chan_id)) and \
(message[1] != Connection._HEARTBEAT): (message[1] != Connection._HEARTBEAT):
self.__handler.handle(self.__subscriptions[chan_id], message[1:]) self.__handler.handle(subscription, message[1:])
def __on_subscribed(self, message: Dict[str, Any]) -> None: def __on_subscribed(self, message: Dict[str, Any]) -> None:
chan_id = cast(int, message["chan_id"]) chan_id = cast(int, message["chan_id"])

View File

@@ -2,7 +2,7 @@
import zlib import zlib
from collections import OrderedDict from collections import OrderedDict
from typing import Dict, List from typing import Any, Dict, List, cast
from bfxapi import Client from bfxapi import Client
from bfxapi.types import TradingPairBook from bfxapi.types import TradingPairBook
@@ -15,8 +15,6 @@ class OrderBook:
symbol: {"bids": OrderedDict(), "asks": OrderedDict()} for symbol in symbols symbol: {"bids": OrderedDict(), "asks": OrderedDict()} for symbol in symbols
} }
self.cooldown: Dict[str, bool] = {symbol: False for symbol in symbols}
def update(self, symbol: str, data: TradingPairBook) -> None: def update(self, symbol: str, data: TradingPairBook) -> None:
price, count, amount = data.price, data.count, data.amount price, count, amount = data.price, data.count, data.amount
@@ -66,6 +64,15 @@ class OrderBook:
return crc32 == checksum return crc32 == checksum
def is_verifiable(self, symbol: str) -> bool:
return (
len(self.__order_book[symbol]["bids"]) >= 25
and len(self.__order_book[symbol]["asks"]) >= 25
)
def clear(self, symbol: str) -> None:
self.__order_book[symbol] = {"bids": OrderedDict(), "asks": OrderedDict()}
SYMBOLS = ["tLTCBTC", "tETHUSD", "tETHBTC"] SYMBOLS = ["tLTCBTC", "tETHUSD", "tETHBTC"]
@@ -100,17 +107,20 @@ def on_t_book_update(subscription: Book, data: TradingPairBook):
async def on_checksum(subscription: Book, value: int): async def on_checksum(subscription: Book, value: int):
symbol = subscription["symbol"] symbol = subscription["symbol"]
if order_book.verify(symbol, value): if order_book.is_verifiable(symbol):
order_book.cooldown[symbol] = False if not order_book.verify(symbol, value):
elif not order_book.cooldown[symbol]:
print( print(
"Mismatch between local and remote checksums: " "Mismatch between local and remote checksums: "
f"restarting book for symbol <{symbol}>..." f"restarting book for symbol <{symbol}>..."
) )
await bfx.wss.resubscribe(sub_id=subscription["sub_id"]) _subscription = cast(Dict[str, Any], subscription.copy())
order_book.cooldown[symbol] = True await bfx.wss.unsubscribe(sub_id=_subscription.pop("sub_id"))
await bfx.wss.subscribe(**_subscription)
order_book.clear(symbol)
bfx.wss.run() bfx.wss.run()

View File

@@ -2,7 +2,7 @@
import zlib import zlib
from collections import OrderedDict from collections import OrderedDict
from typing import Dict, List from typing import Any, Dict, List, cast
from bfxapi import Client from bfxapi import Client
from bfxapi.types import TradingPairRawBook from bfxapi.types import TradingPairRawBook
@@ -15,8 +15,6 @@ class RawOrderBook:
symbol: {"bids": OrderedDict(), "asks": OrderedDict()} for symbol in symbols symbol: {"bids": OrderedDict(), "asks": OrderedDict()} for symbol in symbols
} }
self.cooldown: Dict[str, bool] = {symbol: False for symbol in symbols}
def update(self, symbol: str, data: TradingPairRawBook) -> None: def update(self, symbol: str, data: TradingPairRawBook) -> None:
order_id, price, amount = data.order_id, data.price, data.amount order_id, price, amount = data.order_id, data.price, data.amount
@@ -66,6 +64,15 @@ class RawOrderBook:
return crc32 == checksum return crc32 == checksum
def is_verifiable(self, symbol: str) -> bool:
return (
len(self.__raw_order_book[symbol]["bids"]) >= 25
and len(self.__raw_order_book[symbol]["asks"]) >= 25
)
def clear(self, symbol: str) -> None:
self.__raw_order_book[symbol] = {"bids": OrderedDict(), "asks": OrderedDict()}
SYMBOLS = ["tLTCBTC", "tETHUSD", "tETHBTC"] SYMBOLS = ["tLTCBTC", "tETHUSD", "tETHBTC"]
@@ -100,17 +107,20 @@ def on_t_raw_book_update(subscription: Book, data: TradingPairRawBook):
async def on_checksum(subscription: Book, value: int): async def on_checksum(subscription: Book, value: int):
symbol = subscription["symbol"] symbol = subscription["symbol"]
if raw_order_book.verify(symbol, value): if raw_order_book.is_verifiable(symbol):
raw_order_book.cooldown[symbol] = False if not raw_order_book.verify(symbol, value):
elif not raw_order_book.cooldown[symbol]:
print( print(
"Mismatch between local and remote checksums: " "Mismatch between local and remote checksums: "
f"restarting book for symbol <{symbol}>..." f"restarting book for symbol <{symbol}>..."
) )
await bfx.wss.resubscribe(sub_id=subscription["sub_id"]) _subscription = cast(Dict[str, Any], subscription.copy())
raw_order_book.cooldown[symbol] = True await bfx.wss.unsubscribe(sub_id=_subscription.pop("sub_id"))
await bfx.wss.subscribe(**_subscription)
raw_order_book.clear(symbol)
bfx.wss.run() bfx.wss.run()