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.