bing0
bing0

Reputation: 11

Sending GoerliETH to a contract return "out of gas" and "execution reverted"

I'm using the Remix IDE to Deploy a Solidity Smart Contract.

//SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.19;

contract sendMonetSolidity{
    uint public receiveBalance;

    
    function sendMoney()public payable{
        receiveBalance += msg.value;
    }

    function getBalance()public view returns(uint){
        return address(this).balance;
    }


    function withdrawMoney() public {
        payable(msg.sender).transfer(this.getBalance());
    }

    function sendMoneyToSpecidicAddress(address payable _to) public{
        _to.transfer(this.getBalance());
    }


    
}

Injected Provider was MetaMask, while I sent and withdraw anything was well in the same browser. Then I started to send Goerli from another account, it was fail with an error "out of gas".

https://goerli.etherscan.io/address/0x44433868cc8b2726ca98ebe9bb5339b7a7c8d945

If I increased the gas fee, return error of "execution reverted"

Upvotes: 0

Views: 229

Answers (1)

Petr Hejda
Petr Hejda

Reputation: 43581

Contracts reject all native token payments (ETH on Ethereum, MATIC on Polygon, ...) by default.

You need to implement either receive() external payable or fallback() external payable special functions in order to accept native token payments.

pragma solidity ^0.8.19;

contract sendMonetSolidity {
    receive() external payable {
        // can be empty - don't forget to implement separate function for withdrawal
        // or you can redirect the payment to some EOA here
    }

    // ... rest of your code
}

Upvotes: 0

Related Questions