0

Code is :

contract sendEther{
    //to send ether, we need to deposit some ether first in this contract
    //We can do it by declaring constructor as payable which enables us to deposit some ether shile deploying
    //OR we can declare a receive/fallback function to receive some ether after the contract is deployed
    //mainly we will use receive func coz we want to just receive ether
    //since we do not have a fallback func, any function called apart from sending ether will fail

    //constructor() payable {}

    receive() external payable {}

    function sendviaTransfer(address payable _to) external payable{
        _to.transfer(123); 
        
    }

    function sendviaSend(address payable _to) external payable{
        bool sent = _to.send(123);

        require(sent, "failed");
    }

    function sendviaCall(address payable _to) external payable{
        (bool sent, ) = _to.call{value : 123}(""); //will forward all available gas with 123 eth to execute this transaction
        require(sent, "failed");

    }
}

If I do not use constructor payable and only use receive payable then im unable to deposit ether and sendViaX functions fail.... Plz suggest a solution

I have picked this up from this video https://www.youtube.com/watch?v=mlPc3EW-nNA&list=PLO5VPQH6OWdVQwpQfw9rZ67O6Pjfo6q-p&index=39

1 Answer 1

0

In what way are you trying to send ethers?

once the contract is deployed you should be able to set the value in remix

enter image description here

and then click on the button transact, this should work, the contract should be able to receive the ethers because of the receive function

enter image description here

1
  • Yes .... i figured it out somehow, this was exactly how i wanted to do it .... Thnku for answering :)
    – shikamaru
    Oct 27, 2022 at 14:29

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.