ethereum / web3.py

A python interface for interacting with the Ethereum blockchain and ecosystem.
http://web3py.readthedocs.io
MIT License
4.94k stars 1.68k forks source link

Error when listening to events and catching one #965

Closed elishadrion closed 11 months ago

elishadrion commented 6 years ago

What was wrong?

This is the code.

import time
from solc import compile_source
from web3 import Web3, TestRPCProvider

def handle_event(event):
    print(event)

def log_loop(filter, poll_interval):
    while True:
        for event in filter.get_new_entries():
            handle_event(event)
        time.sleep(poll_interval)

def main():
    contract_source_code = '''
    pragma solidity ^0.4.24;
    contract Test {
        event MoneyReceived (
            uint256 amount
        );
        function pay() payable {
            emit MoneyReceived(msg.value);
        }
    }
    '''

    w3 = Web3(TestRPCProvider())
    compiled_contract = compile_source(contract_source_code)
    contract_interface = compiled_contract['<stdin>:Test']
    Test = w3.eth.contract(abi=contract_interface['abi'], bytecode=contract_interface['bin'])
    tx_hash = Test.constructor().transact()
    tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)
    deployed_contract = w3.eth.contract(
        address=tx_receipt.contractAddress,
        abi=contract_interface['abi'],
    )
    w3.eth.waitForTransactionReceipt(tx_hash)
    event_filter = deployed_contract.eventFilter("MoneyReceived", {'fromBlock': 0, 'toBlock': 'latest'})
    tx_hash = deployed_contract.functions.pay().transact({"value":1000000000000000000})
    log_loop(event_filter, 2)

if __name__ == "__main__":
    main()

The issue is that when an event is caught, the following error systematically appears :

ValueError: Unknown format 0x000000000000000000000000c305c901078781c232a2a521c2af7980f8385ee9, attempted to normalize to 0x000000000000000000000000c305c901078781c232a2a521c2af7980f8385ee9

c305c901078781c232a2a521c2af7980f8385ee9 being the address of the contract on the testnet.

dylanjw commented 6 years ago

Can you paste the output of pip freeze?

elishadrion commented 6 years ago
attrdict==2.0.0
bitcoin==1.1.42
certifi==2018.4.16
cffi==1.11.5
chardet==3.0.4
click==6.7
cytoolz==0.9.0.1
eth-abi==1.1.1
eth-account==0.2.3
eth-hash==0.1.4
eth-keyfile==0.5.1
eth-keys==0.2.0b3
eth-rlp==0.1.2
eth-testrpc==1.3.5
eth-utils==1.0.3
ethereum==1.6.1
hexbytes==0.1.0
idna==2.7
json-rpc==1.11.0
lru-dict==1.1.6
parsimonious==0.8.0
pbkdf2==1.3
py-solc==3.1.0
pycparser==2.18
pycryptodome==3.6.4
pyethash==0.1.27
pysha3==1.0.2
PyYAML==3.13
repoze.lru==0.7
requests==2.19.1
rlp==0.6.0
scrypt==0.8.6
secp256k1==0.13.2
semantic-version==2.6.0
six==1.11.0
toolz==0.9.0
urllib3==1.23
web3==4.4.1
websockets==5.0.1
Werkzeug==0.14.1

I installed the following packages via pip : web3py, py-solc and eth-testrpc

dylanjw commented 6 years ago

There appears to be a bug with the provider. Try testing with EthereumTesterProvider which uses py-evm for its backend by default, which is better maintained than pyethereum.

I was able to recreate your issue using eth-testrpc, where the address field in the logs is malformed:

*** ValueError: Could not format value [{'type': 'mined', 'logIndex': '0x0', 'transactionIndex': '0x0', 'transactionHash': '0x39c738b0986d5048b08e04474df4f0c5b44e0d5eb780889d0b993dc46a84a392', 'blockHash': '0xd6bb105376ff213ff0baec6b7afb8ce63c11d691a53c23adc1a8f7320a7d71df', 'blockNumber': '0x1', 'address': '0x000000000000000000000000c305c901078781c232a2a521c2af7980f8385ee9', 'data': '0x0000000000000000000000000000000000000000000000000de0b6b3a7640000', 'topics': ['0x93a0151bd9ab4670fc26048645e2eb2e4255bdf34858297165d80d4404731af1']}] as field 'logs'

Using the py-evm backed EthereumTesterProvider does not have the same issue.

elishadrion commented 6 years ago

Did you simply replace w3 = Web3(TestRPCProvider()) by w3 = Web3(EthereumTesterProvider()) and successfully ran the script?

I systematically have the following error now, caused by web3.eth.waitForTransactionReceipt(tx_hash):

  File "/Users/ed/Desktop/abcd/venv3.6/lib/python3.6/site-packages/eth_tester/main.py", line 314, in get_transaction_receipt
    raise ValidationError('Invalid status value: only 0 or 1 are valid')
eth_tester.exceptions.ValidationError: Invalid status value: only 0 or 1 are valid

The same error is produced using the code here.

fselmo commented 11 months ago

stale