eth-monitor

Monitor and cache ethereum transactions with match filters
git clone git://git.defalsify.org/eth-monitor.git
Info | Log | Files | Refs | README | LICENSE

etherscan.py (1521B)


      1 # standard imports
      2 import urllib.request
      3 import json
      4 
      5 # external imports
      6 from hexathon import add_0x
      7 from chainlib.eth.block import (
      8         block_by_hash,
      9         Block,
     10         )
     11 from chainlib.eth.tx import (
     12         Tx,
     13         receipt,
     14         )
     15 
     16 
     17 class Importer:
     18 
     19     def __init__(self, rpc, api_key, filters=[], block_callback=None):
     20         self.api_key = api_key
     21         self.filters = filters
     22         self.rpc = rpc
     23         self.block_callback = block_callback
     24 
     25 
     26     def get(self, address):
     27         #f = open('sample_import.json', 'r')
     28         #o = json.load(f)
     29         #f.close()
     30         o = self.get_api(address)
     31 
     32         for v in o['result']:
     33             o = block_by_hash(v['blockHash'])
     34             r = self.rpc.do(o)
     35             block = Block(r)
     36 
     37             if self.block_callback != None:
     38                 self.block_callback(None, block)
     39 
     40             tx_src = block.txs[int(v['transactionIndex'])]
     41 
     42             o = receipt(tx_src['hash'])
     43             r = self.rpc.do(o)
     44 
     45             tx = Tx.from_src(tx_src, block=block, rcpt=r)
     46 
     47             for fltr in self.filters:
     48                 fltr.filter(self.rpc, block, tx)
     49 
     50 
     51     def get_api(self, address):
     52         a = add_0x(address)
     53         req = urllib.request.Request(url='https://api.etherscan.io/api?module=account&action=txlist&address={}&tag=latest&api_key={}'.format(a, self.api_key))
     54         req.add_header('Content-Length', 0)
     55         req.add_header('Accept', 'application/json')
     56         r = urllib.request.urlopen(req)
     57         return json.load(r)