1

I'm a beginner of solidity. I just want to test view the event in etherscan but above error problems up.

contract class24{
    function get_time_now()public view returns(uint256,uint256){
        return (now,block.timestamp);
    }
    function get_block_info()public view returns(uint blockNumber,bytes32 blockHash,uint256  blockDifficulty){

        return (block.number,
                blockhash(block.number-1),
                block.difficulty);
    }
    function get_tx_info()public view returns(address msgSender,address origin,uint value){
        return (msg.sender,
                tx.origin,
                msg.value);
    }
    event setMoney (uint money);

    function buy () public payable {
        emit setMoney(msg.value);
    }
}

However, when I force to deploy the contract by matamask in Rinkeby test network.I'm 100% sure that my wallet has enough eth. The below warning prompt up. Could any tell me why and what can I do?

Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending? execution reverted

1 Answer 1

2

If this happens during deployment of the contract, you are probably trying to send some eth alongside the contract deployment transaction.

The "only" way to send eth to a contract is to call a function that has payable modifier: https://solidity-by-example.org/payable/

When you're deploying the contract, you are in fact calling a special function called constructor. Which is a function that can be called only once, during the contract deployment. But your contract doesn't have one, does it?

In fact it does. Every contract, if not manually defined, has a default empty constructor like this:

constructor() {
}

You can see it has no payable modifier, so that means, the contract cannot receive funds during its deployment. If you want to send some initial funds to the constract during deployment, you would have to manually define the constructor and add the payable modifier.

In your case, you're probably don't want to send any funds to the contract right now, so you'll need to remove the value amount from your transaction.

If you're working in Remix, the value is defined here and needs to be set to 0: enter image description here

4
  • The contract is fine to deploy if I don't put any value in remix. However, what I really want to do is, put 1eth in the value in remix and successful deploy the contract. However, when I put 1 eht in the value the warning message - "Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending? execution reverted" prompt up and remix shows "transaction execution failed".....
    – King Yip
    Commented Feb 14, 2022 at 12:26
  • 1
    Like I said, if you want to send eth to the contract during deployment, you need to define the constructor and add a payable modifier to it: constructor() payable { } Commented Feb 14, 2022 at 12:29
  • Do you have any idea what happen on it?
    – King Yip
    Commented Feb 14, 2022 at 12:31
  • What do you mean? You just cannot send eth to a function that doesn't have payable modifier. Commented Feb 14, 2022 at 12:47

Your Answer

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

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