[Ethereum] Deposit Ether into a Contract

contract-developmentremix

I added the below three functions to my deployed contract as described in the article https://programtheblockchain.com/posts/2017/12/15/writing-a-contract-that-handles-ether/

I deployed using Remix on Rinkeby. When I enter 1 Ether into the Deposit transaction in Remix I get the "Gas Estimation Failed" Error.

Why is the Deposit function generating an error?

Thank youenter image description here

function withdraw() public {
msg.sender.transfer(address(this).balance);
}

function deposit(uint256 amount) payable public {
    require(msg.value == amount);
    // nothing else to do!
}

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

Update

I tried to use a fallback function but Remix is not liking it.

pragma solidity >=0.4.0 <0.7.0;

Thank you

enter image description here

Best Answer

  1. I used the below fallback function and it worked.

receive() external payable {}

  1. This post was what I was actually looking for in my original question. Everything works well now.

How do you send Ether as a function to a contract using Remix?

Related Topic