ethereum / web3.py

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

Events: myContract.events method does not get the events #2662

Closed Kato-Official closed 3 months ago

Kato-Official commented 2 years ago

I have tried to get the events from a simple function from the smart contract. I have waited for the transaction to be fully mined and received the transaction receipt.

This is the contract address in Ropsten:

0x631E9882773B5f93cC575B57c0058Be7F46aE46c

This is the link:

https://ropsten.etherscan.io/address/0x631E9882773B5f93cC575B57c0058Be7F46aE46c#code

This is the event/emit in solidity:

  event gCreated(uint, string, uint, string);  
  emit gCreated((gCount-1), _gName, block.timestamp, "Will This Display?");

This is the python function:

def create(title_name):
    nonce_tx = w3.eth.getTransactionCount(account_0)
    tx = contract.functions.creatG(title_name).buildTransaction({
        'chainId': 3,
        'gas': 200000,
        'maxPriorityFeePerGas': w3.toWei('1', 'gwei'),
        'nonce': nonce_tx,
    })
    signed_tx = w3.eth.account.sign_transaction(tx, private_key=private_key_0)
    tx_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
    tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)

    # THIS IS THE PART THAT DOES NOT WORK
    event = contract.events.gCreated().processReceipt(tx_receipt)
    print(event)
    print(event[0]['args'])

create('Test')

The event works great in the chain explorer as well as in remix, but with web3.py I get this:

AttributeDict({'': 'Will This Display?'})

What happened to the other 3 elements of the event? Since when contract.events have stopped working? Did it stopped from the Ethereum merge? Did anyone found a solution to this?

BZAghalarov commented 3 months ago

@Kato-Official , I've been looking into this issue and tried reproducing it. Unfortunately, Ropsten completely stopped working so I switched to Sepolia as a Testnet.

Version: 7.0.0b Python: 3.12.3 OS: Windows 10 Pro

This is contract address in Sepolia:

0x43AD75490C5245881fA4FdE5b1Bd9d9Fa4b5B9CE

This is the link :

https://sepolia.etherscan.io/address/0x43ad75490c5245881fa4fde5b1bd9d9fa4b5b9ce#code

Event and emit in solidity are the same with yours, but I'd prefer to share the entire code below:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract MyCon {
    uint public count = 0;
    event ItemCreated(uint id, string name, uint timestamp, string message);

    function createItem(string memory _name) public {
        emit ItemCreated(count, _name, block.timestamp, "Will This Display?");
        count++;
    }
}

This is the python function :

def create(title_name):
    w3 = Web3(Web3.HTTPProvider("https://sepolia.infura.io/v3/INFURA_PROJECT_ID"))
    contract = w3.eth.contract(address=CONTRACT_ADDRESS, abi=CONTRACT_ABI)
    nonce_tx = w3.eth.get_transaction_count(account_0)
    tx = contract.functions.createItem(title_name).build_transaction({
        'chainId': 11155111,
        'gas': 200000,
        'maxPriorityFeePerGas': w3.to_wei('1', 'gwei'),
        'nonce': nonce_tx,
    })
    signed_tx = w3.eth.account.sign_transaction(tx,
                                                private_key="PRIVATE_KEY")

    tx_hash = w3.eth.send_raw_transaction(signed_tx.raw_transaction)
    tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)

    event = contract.events.ItemCreated().process_receipt(tx_receipt)

    print(event)
    print(event[0]['args'])

create('Test')

However, as expected, I got AttributeDict({'id': 5, 'name': 'Test', 'timestamp': 1721205420, 'message': 'Will This Display?'}) which differs from the result you mentioned. I think the reason for not being able to reproduce the issue could be either the problems with Ropsten or the version of web3.py.

fselmo commented 3 months ago

It seems like this can't be reproduced and an attempt at reproducing by @BZAghalarov found an expected outcome. I'm going to close this but if we believe there's still an issue here let's keep discussing and we can re-open.

Thanks for the help @BZAghalarov !