0

contract MyContract {
    
    address payable[] recipients;
 
    function sendEther(address payable recipient) external payable {
        recipient.transfer(1 ether);
    }
}

Error- transact to MyContract.sendEther 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.

1

Contract cannot take ethers from wallet address and transfer it, you need to pass the amount you wish to transfer as VALUE. Then your code should look like this:

function sendEther(address payable recipient) public payable {
    recipient.transfer(msg.value);
}

Then if you want to transfer only exact one ether you can have the following condition in your logic right above the ether transfer:

function sendEther(address payable recipient) public payable {
    require(msg.value == 1 ether, 'Only 1 ether allowed to be transfered.');
    recipient.transfer(msg.value);
}
3

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.