First, for the first example, I don't think you need to require(msg.value == amount)
. msg.value
is the eth value sent to this contract. The amount
parameter is not doing much there. And this function will revert if the msg.value
sent is not the same as the amount
. But anyways, doing this is redundant. But make sure that if you send an amount
parameter of 5
, then you should send 5
weis as well so this function does not fail.
function deposit(uint256 amount) public payable {
require(msg.value == amount);
}
The second example look fine. But your contract needs to have at least amount
of balance or you should send a balance (msg.value
) the same as the amount
parameter.
function sendViaTransfer(address payable to, uint256 amount) public payable {
to.transfer(amount);
}
I wrote some code with all the ways you can receive and send ether, with some comments, It might be helpful for you. All of the example work. Just remember to ether have enough balance in your SendEther
contract before trying to send ether to another address, or send enough ether to that contract so it can use the msg.value
and send that amount.
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Receiver {
receive() external payable {}
fallback() external payable {}
function receiveBalance() public payable {}
function getBalance() public view returns(uint256) {
return address(this).balance;
}
//.. Other functionalities here (withdraw, etc).
}
contract SendEther {
address public owner = msg.sender;
// Maybe you want to control who can withdraw the balance from this contract.
modifier onlyOnwer() {
require(msg.sender == owner);
_;
}
function deposit(uint256 amount) public payable {
require(msg.value == amount); // No need to send or check an amount here.
}
function deposit_v2() public payable {
// We have the option to do nothing here. As long as the function is `payable`, we receive the eth.
}
// If you want to only receive eth without doing any special logic, then you can use `receive` and/or `fallback`.
receive() external payable {}
fallback() external payable {}
// This function is `payable`, so this contract is receving some amount and sending `amount` to another address
function sendViaTransfer(address payable to, uint256 amount) public payable onlyOnwer {
// Maybe you want to check that this contract has enough balance to send the requested amount.
require(address(this).balance >= amount, "Not enough balance in the contract.");
to.transfer(amount);
}
// This function is `payable`, so this contract is receving some amount and sending that same amount (msg.value) to another address
function sendViaTransfer_v2(address payable to) public payable onlyOnwer {
to.transfer(msg.value);
}
// This function is `payable`, so this contract is receving some amount and sending `amount` to another address
function sendViaSend(address payable to, uint256 amount) public payable onlyOnwer {
// Maybe you want to check that this contract has enough balance to send the requested amount.
require(address(this).balance >= amount, "Not enough balance in the contract.");
bool success = to.send(amount);
require(success, "Error sending eth");
}
// This function is `payable`, so this contract is receving some amount and sending that same amount (msg.value) to another address
function sendViaSend_v2(address payable to) public payable onlyOnwer {
bool success = to.send(msg.value);
require(success, "Error sending eth");
}
// This function is `payable`, so this contract is receving some amount and sending `amount` to another address
function sendViaCall(address payable to, uint256 amount) public payable onlyOnwer {
// Maybe you want to check that this contract has enough balance to send the requested amount.
require(address(this).balance >= amount, "Not enough balance in the contract.");
(bool success, ) = to.call{value: amount}("");
require(success, "Error sending eth");
}
// This function is `payable`, so this contract is receving some amount and sending that same amount (msg.value) to another address
function sendViaCall_v2(address payable to) public payable onlyOnwer {
(bool success, ) = to.call{value: msg.value}("");
require(success, "Error sending eth");
}
// This function is `payable`, so this contract is receving some amount and sending `amount` to another address
function sendViaCustomFunction(address payable to, uint256 amount) public payable onlyOnwer {
// Maybe you want to check that this contract has enough balance to send the requested amount.
require(address(this).balance >= amount, "Not enough balance in the contract.");
Receiver(to).receiveBalance{value: amount}();
}
// This function is `payable`, so this contract is receving some amount and sending that same amount (msg.value) to another address
function sendViaCustomFunction_v2(address payable to) public payable onlyOnwer {
Receiver(to).receiveBalance{value: msg.value}();
}
function getBalance() public view returns(uint256) {
return address(this).balance;
}
}
value: 0 wei
. Are you using remix?