Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
83 lines (63 sloc) 2.28 KB
from ethereum import abi
from google.appengine.ext import db, ndb
import logging
import urllib
import urllib2
import webapp2
from web3 import Web3, RPCProvider
import models
web3 = Web3(RPCProvider(host=''))
def sendRequest(url, body, contentType):
request = urllib2.Request(url, body, {'Content-Type': contentType})
response = urllib2.urlopen(request)
if 200 <= response.getcode() < 300:
logging.warn("POST to %r got response code %d: %r", url, response.getcode(),
else:"POST to %r", url)
def notifyHook(hook, log):
topics = [int(x[2:], 16) for x in log['topics']]
data = log['data'][2:].decode('hex')
event = hook.interface.decode_event(topics, data)
eventData = dict(log)
url = hook.uri % eventData
if hook.body:
body = {}
for k, v in hook.body.iteritems():
body[k] = v % eventData
if hook.contentType.lower() == 'text/x-www-form-urlencoded':
body = urllib.urlencode(body)
body = json.dumps(body)
body = ""
sendRequest(url, body, hook.contentType)
def processLog(log):
topics = log['topics']
eventId = topics[0].lower()
address = log['address'].lower()"Log for %s from %s", eventId, address)
if len(topics) == 0:
q = models.Hook.query(models.Hook.eventId == eventId, ndb.OR(
models.Hook.address == address, models.Hook.address == ""))
for hook in q:
notifyHook(hook, log)
class BlockLoadHandler(webapp2.RequestHandler):
def get(self):
lastBlock = web3.eth.blockNumber - NUM_CONFIRMATIONS
sync = models.SyncState.getInstance()
for blocknum in range(sync.lastBlock, lastBlock + 1):"Processing block %d", blocknum)
block = web3.eth.getBlock(blocknum)
for tx in block['transactions']:
receipt = web3.eth.getTransactionReceipt(tx)
for log in receipt['logs']:
processLog(log)"Done processing new blocks.")
sync.lastBlock = lastBlock
app = webapp2.WSGIApplication([
('/_tasks/blockLoader', BlockLoadHandler),
], debug=True)