0

I know how to make a smart contract send money to an address, and I know how to send money from an address to a smart contract in Remix, but when I try to have one contract send to another my require function catches it and it doesn't work. I have a second contract, and here is the code that's not working:

function sendMoney(uint amount) public{
        (bool success,) = casino.call{value: amount}("");
        require(success, "Fail!");
    }

casino is defined as a specific address of another smart contract earlier in my code.

2 Answers 2

2

In order to send ethers to another smart contract the address should be payable or you can cast the address on the following way:

(bool success,) = payable(casino).call{value: amount}("");

additionally the other smart contract should have a fallback or a receive function in order to be able to receibe the funds

0

To build up a bit on Javier's answer, here are 2 simple contracts.


contract Receiver {

    receive() external payable {}
}

contract Sender {

    Receiver public receiver;

    constructor(address payable _receiverAddress) payable {
        receiver = Receiver(_receiverAddress);
    }

    function preferedMethod() external returns(bool){
        (bool success,) = payable(receiver).call{value: address(this).balance}("");
        return success;
    }

    //Works but not recommended
    function transferEth() external {
        payable(receiver).transfer(address(this).balance);
    }

    //Works but not recommended
    function sendEth() external returns(bool){
        bool success = payable(receiver).send(address(this).balance);
        return(success);
    }
}

As you can see, the receiver contract has a receive payable function, and the Sender contract has 3 different methods of sending Eth.

I think 'call' is the preferred method now but do realize that 'call' opens the door to reentrancy hacks.

And here is a link of Solidity by Examples that might help: https://solidity-by-example.org/sending-ether/

Hope this helps!

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.