1

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

1 Answer 1

0

owner.call{value: msg.value}() will never send ether to receiver. The <address>.call() function sends an internal transaction to the given address. To send ether to receiver, type: receiver.call{value: value}().

When a variable is created without a value being assigned, the variable will get a default value. The default value for type address is 0x0000000000000000000000000000000000000000. This means if you call receiver.call{value: value}() without receiver being set, the ether is send to the 0x0 address.

To get user inputs you can use function parameters.

2
  • Another thing to note: The Ether will be send from the contract to the receiver (not from the owner). So if you want to send Ether from the owner to the receiver through the contract, then the owner has to send the Ether along with the method call to the contract.
    – Richard
    Dec 26, 2021 at 10:07
  • Ok, I updated my code, but I'm failing to understand something. Wait a sec I'll post it Dec 30, 2021 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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