I'm starting to learn solidity, and I'm trying to build a sendEther contract where some address sends an amount of ether to another address. I'm building it on remix, and I'm having trouble while setting up the receiver's address. This is my code so far.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.4;
contract sendEther {
address payable public owner;
address payable public receiver;
uint256 public value;
error insufficientBalance();
error transferAlreadyCalled();
event Log(string message);
constructor() payable {
owner = payable(msg.sender);
}
modifier inBalance(address owner_) {
if (owner.balance < value) {
emit Log("Insufficient balance");
revert insufficientBalance();
_;
}
}
function transferEther() external payable {
owner = payable(msg.sender);
(
bool sent, /* bytes memory data */
) = owner.call{value: msg.value}("");
require(sent, "Failed to send Ether");
}
}
I'm struggling to understand how the owner.call()
will send ether to receiver since receiver wasn't set to any address. That said, how should I get the desired address input from the user?
UPDATE
This is my code right now:
//SPDX-License-Identifier: Unlicense pragma solidity ^0.8.0;
import "hardhat/console.sol";
contract donateMoney { address public owner; uint256 public totalDonations; uint256 public value;
constructor(uint256 _value) {
owner = payable(msg.sender);
value = _value;
}
function getTotalDonations() public view returns (uint256) {
return totalDonations;
}
function donate() public payable {
(bool success, ) = owner.call{value: value}("");
require(success, "Failed to send money");
}
}
My question now is: How can I "get" the eth from the user as input? This runs well on solidity, but on CMD i get error's because who is donating doesnt have ETH. How can I access user's money from there? Using a frontend i guess, but if anyone has some tip. Thanks