0

I'm studying smart contracts and so far perceived smart contracts as a backend of a cloud application. So my understanding is that any interaction should be carried out via SM methods call. That means that if I want to deposit some ether to a smart contract I should call deposit(1 ether). That implies that in implementation of that function I should be able to transfer ether from caller's account to SM's one. However I haven't found how to do that. Functions transfer() and send() send ether to it's address object, not from.

So, my question is - how do I send ether from address object? Is the only way to have such a call a deposit method: contract.deposit.value(1 ether)(); ?

Update: I tried to run this code:

    function deposit(uint256 amount) public {
        require(amount > 0, "Amount must be greater than 0");
        require(amount <= address(msg.sender).balance, "Sender must have enough ether");
        
        // Transfer the specified amount from the sender's account to the contract's account
        (bool sent, ) = payable(address(this)).call{value: amount}("");
        require(sent, "Couldn't transfer Ether");
        
        // Increase the balance of the sender
        balances[msg.sender] += amount;
    }

It fails silently and sent is false. I checked that sender's balance is sufficient to send 1 wei.

1 Answer 1

1

Transfer Ether from the contract to a specified address

You can do that by using call method, inside the corresponding function of your contract having ether balance, like:

// Function to transfer Ether from the contract to a specified address
function transferEther(address payable _to, uint256 _amount) public {
    require(address(this).balance >= _amount, "Insufficient ether balance in contract");
    (bool sent, ) = _to.call{value: _amount}("");
    require(sent, "Failed to send Ether");
}

Transfer Ether from the sender to the contract

If you want to transfer ether from the sender to the contract, then the deposit function should be made as payable and you've pass the amount as msg.value while calling the deposit function.

If you're doing it in Remix IDE, then you can pass the amount in the VALUE field:

enter image description here

And, the deposit function should be like:

// Function to transfer Ether from the msg.sender to the contract
function deposit() public payable {
    require(msg.value > 0, "Amount must be greater than 0");

    // This condition will be checked by default
    // require(msg.value <= address(msg.sender).balance, "Sender must have enough ether"); 

    // Increase the balance of the sender
    balances[msg.sender] += msg.value;
}

P.S., Since you're expecting your contract to receive funds, so if you would like to allow the senders to send funds directly to the contract address (i.e., without interacting with the contract) then you can also add receive() and fallback() functions and call deposit() in both of them, like:

// Function to receive Ether. msg.data must be empty
receive() external payable {
    deposit();
}

// Fallback function is called when msg.data is not empty
fallback() external payable {
    deposit();
}

You can read more about these functions in this Medium article.

7
  • I've added a code I tried to run (with call())
    – Ralfeus
    Commented Jun 25 at 7:40
  • Okay. Let me check. Commented Jun 25 at 7:42
  • payable(sender).call{value: amount}("") is basically transferring amount from the contract to the sender. But, in the comment, you've mentioned "// Transfer the specified amount from the sender's account to the contract's account". So, do you want to transfer from sender (i.e., wallet address) to contract? Commented Jun 25 at 7:51
  • Yeah, you're right. I replaced that line with: (bool sent, ) = payable(address(this)).call{value: amount}("");. It gives same result
    – Ralfeus
    Commented Jun 25 at 8:01
  • Actually here comes a question - when I define a recipient's wallet to transfer ether to, what wallet will be used to transfer from? Caller of the function or smart contract's?
    – Ralfeus
    Commented Jun 25 at 8:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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