diff --git a/README.md b/README.md new file mode 100644 index 0000000..1a12cb8 --- /dev/null +++ b/README.md @@ -0,0 +1,41 @@ +# Event Monitoring with Infura +Making use of Infura [improved eth_getLogs](https://blog.infura.io/faster-logs-and-events-e43e2fa13773) +to observe token transactions. + +Etherscan API is used to retrieve contract ABI. + +## Setup +Create file config.json and enter your [Infura](https://infura.io/) project ID, +and [Etherscan](https://etherscan.io) API key. +Can also enter a default contract address. + + + { + "contract_address": "0x10dB9941E65DA3B7FDB0Cd05B1fd434Cb8B18158", + "infura_project_id": "(your Infura project id)", + "etherscan_api_key": "(your Etherscan API key)" + } + + + +## Example usage +Example using [Tether stablecoin](https://etherscan.io/token/0xdac17f958d2ee523a2206206994597c13d831ec7). We get +the ABI, find the topic for Transfer event and get these. + + from infmon.io import (get_contract_abi, get_event_interface, + get_current_block, get_contract_events, subscribe) + token_address = '0xdAC17F958D2ee523a2206206994597C13D831ec7' # Tether + token_abi = get_contract_abi(token_address) + token_interface = get_event_interface(token_abi) + # RPC interface - get token transfers in last 10 blocks + transfers = get_contract_events( + token_address, + from_block=get_current_block() - 10, + topics=[token_interface['Transfer']['topic']]) + + # Display + [token_interface['Transfer']['decode'](t) for t in transfers] + + # Subscription interface - WIP not quite working yet + await subscribe(token_address, + topics=[token_interface['Transfer']['topic']]) \ No newline at end of file diff --git a/infmon/io.py b/infmon/io.py index 50d10e8..ed2ed53 100644 --- a/infmon/io.py +++ b/infmon/io.py @@ -26,7 +26,7 @@ def read_config(): config = read_config() -CONTRACT_ADDRESS = config['contract_address'] +CONTRACT_ADDRESS = config['contract_address'] if 'contract_address' in config else '' INFURA_PROJECT_ID = config['infura_project_id'] ETHERSCAN_API_KEY = config['etherscan_api_key'] @@ -80,10 +80,19 @@ async def subscribe(contract_address=CONTRACT_ADDRESS, topics=None, infura_proje 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)) + block_hashes = defaultdict(int) + last_block_hash = '' + while True: + message_str = await ws.recv() + message = json.loads(message_str)['params']['result'] + block_hashes[message['blockHash']] += 1 + if message['removed']: + print('REMOVED') + print(message) + else: + if message['blockHash'] != last_block_hash: + print(last_block_hash + ': ' + str(block_hashes[last_block_hash])) + last_block_hash = message['blockHash'] def get_contract_abi(