Sending ether from one contract to another?

Hi,
I have two contracts and I am trying to send ether from on contract to another. This is the code I tried to use.

(bool secondary, ) = payable(otherContractAddress).call{value: amountToSend}("");
require(secondary, "Failed");

This code fails and returns "Failed" when called with the address of my smart contract as the otherContractAddress. When I change otherContractAddress to a simple wallet address the function works properly. I assume it is an issue with callback functionality on the other contract.

Is there an easier way to send money from contract to contract?

Have you tried the following?

import "@openzeppelin/contracts/utils/Address.sol";
....
Address.sendValue(address, amount);
1 Like

Did you add receive() function in receiving contract?

1 Like

I did not try that yet. I will look into it thanks.

No I did not. I am assuming that I should have added a receive() function. I will look into that also. Thanks

    event Received(address, uint);
    receive() external payable {
        emit Received(msg.sender, msg.value);
    }

I added this into the receiving contract and the problem is solved. Thanks.

1 Like