3

I am trying to create a python filter to read events from Smart contract but I am not successful so far. below is the sample python code and I have used "Instructor" event in solidity and trying to define "my_callback" function below whenever instructor event is fired from solidity program. Can you please help ?

import random
import sys
import os

from web3 import Web3, HTTPProvider, IPCProvider
from web3.contract import ConciseContract
import json
from web3.providers.rpc import HTTPProvider


def my_callback():
    print ('Instructor event  is fired from contract')

contractAddress = '0x2818129b6306be2a337b31d83ff9bdf6da067a41'
web3 = Web3(HTTPProvider('http://localhost:8545'))
#open("C:/Users/XXX/Documents/50_states.csv",mode='w')
with open("C:/Users/XXX/Documents/ourfirsttoken.json", mode='r') as abi_definition:
    abi = json.load(abi_definition)

fContract = web3.eth.contract(abi,contractAddress)

transfer_filter = fContract.on('Instructor',my_callback())

fContract.transact({'from': web3.eth.accounts[0]}).setInstructor('XYsdgdsZ',3255)
print('contract get instructor function ' + str(fContract.call().getInstructor()[0]))
0

event_filter = fContract.events.Instructor.createFilter(fromBlock='latest') while True: for event in event_filter.get_new_entries(): my_callback(event) time.sleep(2)

For more check out the documentation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.