0

Following is the smart contract code, setMessage method always fail with revert while estimating gas using web3.py, even when the address user is same as msg.sender. when the contract is deployed on Ganache [windows application] and the transaction is build using web3.py "buildTransaction" method, the revert statement error is always returned while estimating the Gas for transaction. can someone please suggest what is the issue with this code?

''' pragma solidity ^0.8.13; contract EchoMyName { address owner; string message = "EchoMyName world";

function setMessage( string memory msg_ , address user) public
{
    if( msg.sender == user)
    {
    message = msg_;
    }
    else
    {
        revert("Sorry");
    }
    
}
 
function getMessage() public view returns ( string memory )
{
    return message;
}
 
constructor() 
{
    owner = msg.sender;
}

} '''

Python Code is below:

contract_func = contract.functions.setMessage("Hello There", "0x4628df168f80c9BE779c0dC1aBaC36b070F662AC")
transaction = contract_func.buildTransaction(transaction = {'nonce' : nonce,'gasPrice': w3.eth.gas_price})

This is the Exception Returned, even if the message sender and user Address is same, and Test Ethers are also in account:

Exception: execution reverted: VM Exception while processing transaction: revert Sorry

2 Answers 2

1

Although the code and logic is fine and works in remix, Prefer using require statements instead of if-else-revert() statements. They are more costly then require statements and also preferred by solidity official documentation.

Use:

require(msg.sender==user, "You are not the user");
message = msg_; 

The transaction will automatically be reverted if msg.sender!=user;

1
  • Thanks Hanif, will take that into account, i have updated the question, issue is the "buildTransaction" method returns this error, not the actual transaction. Apr 4, 2022 at 12:43
1

I reproduced your contract on web3.py/ Ganache and it seems to work well.

I think your issue is with the buildTransaction() function. I don't see "from" parameter for example (or chainId) even though you are modifying the state of the blockchain.

Also, I believe your nonce, should be 'nonce' = nonce +1 , as you already used the value of nonce for the transaction creating your contract.

Here is a snippet of code that worked:

transaction = mycontract.functions.setMessage(
    "Hello There", my_address
).buildTransaction(
    {
        "chainId": w3.eth.chain_id,
        "gasPrice": w3.eth.gas_price,
        "from": my_address,
        "nonce": nonce + 1,
    }
2
  • It's also working with brownie...I believe you have either an account configuration issue or not enough eth on your test account Apr 4, 2022 at 9:10
  • Thanks Daniel, missed some importatnt information in question while posting, apologies. Yes the code works fine in remix, when the code is deployed on Ganache [windows application] and the transaction is build using web3.py "buildTransaction" method, the revert statement error is always returned while estimating the Gas for transaction. I have Updated the Question. Apr 4, 2022 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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