# web3 == 4.0.0b3
import web3
from web3 import Web3, HTTPProvider
from web3.providers.eth_tester import EthereumTesterProvider
from eth_tester import EthereumTester
from solc import compile_source
# Solidity source code
contract_source_code = '''
pragma solidity ^0.4.18;
contract C {
event Test(uint indexed x);
function test() {
Test(2);
}
}
'''
compiled_sol = compile_source(contract_source_code) # Compiled source code
contract_interface = compiled_sol['<stdin>:C']
print (contract_interface['abi'])
# web3.py instance
# eth_tester = EthereumTester()
# web3 = Web3(EthereumTesterProvider(eth_tester))
# accounts = eth_tester.get_accounts()
web3 = Web3(HTTPProvider("http://localhost:8545"))
accounts = web3.eth.accounts
# Instantiate and deploy contract
contract = web3.eth.contract(abi=contract_interface['abi'], bytecode=contract_interface['bin'])
# Get transaction hash from deployed contract
tx_hash = contract.deploy(transaction={'from': accounts[0], 'gas': 410000})
# Get tx receipt to get contract address
tx_receipt = web3.eth.getTransactionReceipt(tx_hash)
contract_address = tx_receipt['contractAddress']
# Contract instance in concise mode
contract_instance = web3.eth.contract(address=contract_address, abi=contract_interface['abi'])
transfer_filter = contract_instance.eventFilter('Test')
# Should be empty
print (transfer_filter.get_all_entries())
# Call function
tx_hash = contract_instance.transact({'from': accounts[0]}).test();
# Receipt's logs must have the event
receipt = web3.eth.getTransactionReceipt(tx_hash)
print (receipt)
# Should print the event
print (transfer_filter.get_all_entries())
# Geth - testrpc - eth-tester all give me different errors when run with this script.
Comments