Skip to content

Commit

Permalink
Added README. More work on subscribe websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmcd committed Oct 6, 2019
1 parent cdcce01 commit 5e4a1a8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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']])
19 changes: 14 additions & 5 deletions infmon/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']

Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 5e4a1a8

Please sign in to comment.