Transfer/deposit ether on contract fails

ethersmart-contract-walletssoliditytransfer

I am trying to deposit Ether on a smart contract. Looking at the documentation
my approach first looked like this:

// SPDX-License-Identifier: MIT
pragma solidity ^0.0.8;

contract SendEther {

    function deposit(uint256 amount) public payable {
        require(msg.value == amount);
    }
}

However, that failed with the following error message:

[vm]

from: 0x5c6…21678

to: SendEther.deposit(uint256) 0xe63…4ddf5

value: 0 wei

data: 0xb6b…03039

logs: 0

hash: 0x50c…acc83

status false Transaction mined but execution failed

transaction
hash 0x50cb8aab1042aef79e8bf285346213a1edfa106b2a7421ee159da82b693acc83

from 0x5c6B0f7Bf3E7ce046039Bd8FABdfD3f9F5021678

to SendEther.deposit(uint256)
0xe63D12C600b87F5da3C32C7cB3C1b1A84944ddf5 gas 3000000 gas transaction
cost 21614 gas execution cost 21614 gas input 0xb6b…03039 decoded
input { "uint256 amount": "12345" } decoded output {} logs [] val 0
wei

transact to SendEther.deposit errored: VM error: revert.

revert The transaction has been reverted to the initial state.

Note: The called function should be payable if you send value and the
value you send should be less than your current balance.

Debug the transaction to get more information.

Then I tried to use the transfer or call method to send Ether.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract SendEther {

    receive() external payable {}

    function sendViaTransfer(address payable to, uint256 amount) public payable {
        to.transfer(amount);
    }
}

Which resulted in this error:

[vm]

from: 0x147…C160C

to: SendEther.sendViaTransfer(address,uint256) 0x70c…a304B

value: 0 wei

data: 0xd27…004d2

logs: 0

hash: 0xc29…0cf23

status false Transaction mined but execution failed

transaction
hash 0xc296ba642bca2e19c304a9e28381b3b523fdb86a94fcbc19e14ad5847a20cf23

from 0x14723A09ACff6D2A60DcdF7aA4AFf308FDDC160C

to SendEther.sendViaTransfer(address,uint256)
0x70c001c84C723e3EfbF852D3e6AdaEb69A8a304B

gas 3000000 gas

transaction cost 29070 gas

execution cost 29070 gas

input 0xd27…004d2

decoded input {

"address to": "0x70c001c84C723e3EfbF852D3e6AdaEb69A8a304B",

"uint256 amount": "1234"

}

decoded output {}

logs []

val 0 wei

transact to SendEther.sendViaTransfer errored: VM error: revert.

revert The transaction has been reverted to the initial state.

Trying to use "call" or "send" results in the same error.

What am I doing wrong?

Best Answer

the log says message value is 0. Make sure to set VALUE in remix before clicking on the functions so it sends value

set value to send when calling function

Related Topic