gakonst

Untitled

Dec 6th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | None | 0 0
  1. # web3 == 4.0.0b3
  2. import web3
  3. from web3 import Web3, HTTPProvider
  4. from web3.providers.eth_tester import EthereumTesterProvider
  5. from eth_tester import EthereumTester
  6. from solc import compile_source
  7.  
  8. # Solidity source code
  9. contract_source_code = '''
  10. pragma solidity ^0.4.18;
  11.  
  12. contract C {
  13.    event Test(uint indexed x);
  14.    function test()  {
  15.        Test(2);
  16.    }
  17. }
  18. '''
  19.  
  20. compiled_sol = compile_source(contract_source_code) # Compiled source code
  21. contract_interface = compiled_sol['<stdin>:C']
  22. print (contract_interface['abi'])
  23.  
  24. # web3.py instance
  25. # eth_tester = EthereumTester()
  26. # web3 = Web3(EthereumTesterProvider(eth_tester))
  27. # accounts = eth_tester.get_accounts()
  28. web3 = Web3(HTTPProvider("http://localhost:8545"))
  29. accounts = web3.eth.accounts
  30.  
  31. # Instantiate and deploy contract
  32. contract = web3.eth.contract(abi=contract_interface['abi'], bytecode=contract_interface['bin'])
  33.  
  34. # Get transaction hash from deployed contract
  35. tx_hash = contract.deploy(transaction={'from': accounts[0], 'gas': 410000})
  36.  
  37. # Get tx receipt to get contract address
  38. tx_receipt = web3.eth.getTransactionReceipt(tx_hash)
  39. contract_address = tx_receipt['contractAddress']
  40.  
  41. # Contract instance in concise mode
  42. contract_instance = web3.eth.contract(address=contract_address, abi=contract_interface['abi'])
  43. transfer_filter = contract_instance.eventFilter('Test')
  44.  
  45. # Should be empty
  46. print (transfer_filter.get_all_entries())
  47.    
  48. # Call function
  49. tx_hash = contract_instance.transact({'from': accounts[0]}).test();
  50. # Receipt's logs must have the event
  51. receipt = web3.eth.getTransactionReceipt(tx_hash)
  52. print (receipt)
  53.  
  54. # Should print the event
  55. print (transfer_filter.get_all_entries())
  56.  
  57. # Geth - testrpc - eth-tester all give me different errors when run with this script.
Advertisement
Add Comment
Please, Sign In to add comment