Skip to content

Commit

Permalink
Start on trying to use websocket subscribe API
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmcd committed Oct 6, 2019
1 parent 851120d commit cdcce01
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions infmon/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from collections import defaultdict
from functools import partial
from itertools import chain
import asyncio
import websockets
from etherscan.contracts import Contract as EsContract
from web3 import Web3
import eth_abi
Expand Down Expand Up @@ -64,6 +66,26 @@ def get_current_block(infura_project_id=INFURA_PROJECT_ID):
return int(resp['result'], 16)


async def subscribe(contract_address=CONTRACT_ADDRESS, topics=None, infura_project_id=INFURA_PROJECT_ID):
if topics is None:
topics = []
subscribe_args = {
"jsonrpc": "2.0",
"method": "eth_subscribe",
"params": ["logs", {"address": contract_address.lower(), "topics": topics}],
"id": 1
}
ws_url = f'wss://mainnet.infura.io/ws/{infura_project_id}'
async with websockets.connect(ws_url) as ws:
await ws.send(json.dumps(subscribe_args))
subscribe_id = await ws.recv()
print(subscribe_id)
# while True:
# message_str = await ws.recv()
# message = json.loads(message_str)
# print(len(message))


def get_contract_abi(
contract_address=CONTRACT_ADDRESS,
etherscan_api_key=ETHERSCAN_API_KEY
Expand Down

0 comments on commit cdcce01

Please sign in to comment.