Skip to content

Commit fe75415

Browse files
committed
Merge branch 'jac/dataclass'
2 parents f3abb64 + 18e9f90 commit fe75415

File tree

5 files changed

+44
-15
lines changed

5 files changed

+44
-15
lines changed

.devcontainer/devcontainer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
// Uncomment the next line to run commands after the container is created - for example installing curl.
2525
// "postCreateCommand": "apt-get update && apt-get install -y curl",
2626

27-
// "runArgs": [ ],
27+
"runArgs": [ "-e", "BN_API_KEY=EXAMPLEAPIKEY" ],
2828

2929
// Uncomment to use the Docker CLI from inside the container. See https://aka.ms/vscode-remote/samples/docker-from-docker.
3030
// "mounts": [ "source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind" ],

blocknative/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.2.2'
1+
__version__ = '0.2.3'

blocknative/stream.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
BN_BASE_URL = 'wss://api.blocknative.com/v0'
3434
BN_ETHEREUM = 'ethereum'
35+
BN_ETHEREUM_ID = 1
36+
BN_STREAM_CLASS_VERSION = '1.1'
3537

3638
Callback = Callable[[dict, Callable], None]
3739

@@ -84,23 +86,23 @@ def as_dict(self) -> dict:
8486
}
8587

8688

87-
@dataclass
8889
class Stream:
8990
"""Stream class used to connect to Blocknative's WebSocket API."""
9091

9192
api_key: str
9293
blockchain: str = BN_ETHEREUM
93-
network_id: int = 1
94-
version: str = '1'
94+
network_id: int = BN_ETHEREUM_ID
95+
version: str = BN_STREAM_CLASS_VERSION
9596
global_filters: List[dict] = None
9697
valid_session: bool = True
97-
_ws: WebSocketConnection = field(default=None, init=False)
98-
_message_queue: Queue = field(default=Queue(), init=False)
98+
_ws: WebSocketConnection = None
99+
_message_queue: Queue = Queue()
100+
_subscription_registry: Mapping[str, Subscription] = {}
99101

100-
# Registry of active subscriptions.
101-
_subscription_registry: Mapping[str, Subscription] = field(
102-
default_factory=dict, init=False
103-
)
102+
def __init__(self, api_key: str, blockchain: str = BN_ETHEREUM, network_id: int = BN_ETHEREUM_ID):
103+
self.api_key=api_key
104+
self.blockchain=blockchain
105+
self.network_id=network_id
104106

105107
def subscribe_address(
106108
self,
@@ -156,7 +158,7 @@ def subscribe_txn(self, tx_hash: str, callback: Callback, status: str = 'sent'):
156158
if self._is_connected():
157159
self._send_txn_watch_message(tx_hash, status)
158160

159-
def connect(self, base_url:str = BN_BASE_URL):
161+
def connect(self, base_url: str = BN_BASE_URL):
160162
"""Initializes the connection to the WebSocket server."""
161163
try:
162164
return trio.run(self._connect, base_url)
@@ -170,8 +172,9 @@ def send_message(self, message: str):
170172
Args:
171173
message: The message to send.
172174
"""
173-
logging.debug('Sending: {}' % message)
174175
self._message_queue.put(message)
176+
logging.debug('Sending: %s', message)
177+
175178

176179
async def _message_dispatcher(self):
177180
"""In a loop: Polls send message queue for latest messages to send to server.

examples/subscribe.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ async def txn_handler(txn, unsubscribe):
1616
with open(apikeyfile, 'r') as apikey:
1717
keystring = apikey.readline().rstrip().lstrip()
1818
stream = BNStream(keystring)
19-
stream.subscribe_address(monitor_address, txn_handler)
19+
filter = {'network': 'main'}
20+
stream.subscribe_address(monitor_address, txn_handler, filters=[filter])
2021
stream.connect()
2122
except Exception as e:
22-
print('API Failed: %s' % str(e))
23+
logging.error('API Failed: %s', str(e))
2324
traceback.print_exc(e)

examples/subscribe_bsc.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from blocknative.stream import Stream as BNStream
2+
import json,sys,traceback,logging
3+
4+
NETWORK_ID = 56
5+
6+
async def txn_handler(txn, unsubscribe):
7+
# Output the transaction data to the console
8+
print(json.dumps(txn, indent=4))
9+
10+
if __name__ == '__main__':
11+
if len(sys.argv) == 1:
12+
print('%s apikey' % sys.argv[0])
13+
else:
14+
try:
15+
logging.basicConfig(level=logging.INFO)
16+
apikeyfile = sys.argv[1]
17+
with open(apikeyfile, 'r') as apikey:
18+
keystring = apikey.readline().rstrip().lstrip()
19+
stream = BNStream(keystring, network_id=NETWORK_ID)
20+
pancakeswap_v2_address = '0x10ed43c718714eb63d5aa57b78b54704e256024e'
21+
stream.subscribe_address(pancakeswap_v2_address, txn_handler)
22+
stream.connect()
23+
except Exception as e:
24+
logging.error('API Failed: %s', str(e))
25+
traceback.print_exc(e)

0 commit comments

Comments
 (0)