How To Send Ether From One Smart Contract To Another Contract
I set out to challenge myself to write some solidity code on how to send ether from one smart contract to another. It turned out to be a bit more challenging than I thought. mostly because the resources on the internet left a lot of questions unanswered, so I had to figure things out myself, and I decided to share them with you. Let's dig in.
We need two smart contracts for this experiment. The sender contract and the receiver contract. It's pretty self-explanatory. One contract sends ether, a separate contract receives it.
sidenote: this isn't limited to just contracts. You can send ether from the sender contract to an Ethereum address.
Now, open up Remix, and create a new contract. Let’s call it “Sender.sol”. Copy and paste the code underneath into the newly created contract.
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Sender {
//ensures contract can receive ether
event Received(address, uint);
receive() external payable {
emit Received(msg.sender, msg.value);
}
//method to send ether
function senViaCall(address payable _to, uint256 _value) public payable {
(bool sent, bytes memory data) = _to.call{value: _value * 1 ether}('');
require(sent, "failed to send ether");
}
//gets ether balance
function getBalance() public view returns(uint256) {
return address(this).balance;
}
}
Now we create another contract. We’ll call it “Receiver.sol”. Copy and Paste the code underneath.
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Receiver{
//ensures contract can receive ether
event Received(address, uint);
receive() external payable {
emit Received(msg.sender, msg.value);
}
//gets contract ether balance
function getBalance() public view returns(uint256) {
return address(this).balance;
}
//withdraws to address that calls the function
function withdraw() public {
address payable to = payable(msg.sender);
to.transfer(getBalance());
}
}
i suggest you deploy the “Receiver.sol” contract first. Then we deploy the “Sender.sol”.
your deployed contracts should look like this:
Before we move any further, we need to fund our Sender.sol contract with some ether to run our test. Insert the value of ether you want to send in the input box, and then click on the “Transact” button on your deployed Sender contract.
Now proceed to copy the contract address of your Reciever contract. Then paste in your Sender contract. You’ll need to pass in two parameters, {contract address, amount_of_ether}. Should look like this: {0x474…., 4}.
The comma is important. Then click on the bright red “senViaCall” button. all things considered, your transaction should go through.
You can head over to your Receiver contract and click on the “getBalance” method to see your new balance. The number should be the equivalent of the amount of eth you just sent, but in WEI, that’s why it looks long.
Now, we’ve gotten some eth into our Receiver contract, time to withdraw it. Just click on the “withdraw” button, and all the deposited eth will be sent to your wallet address. And that’s it.
A few things to note: This isn’t a safe method to use and deploy to production. DO NOT DO IT. It simply means anyone can call the “withdraw” method, and get all the ether sent to their account. You could hardcode the address you’ll like to withdraw the eth to by replacing “msg.sender”, inside your “withdraw” method with the wallet/contract address. so from:
address payable to = payable(msg.sender);
to
address payable to = payable(0x448…);
And there we have it. You have a fully functional contract that receives and sends out ether! lfg 🚀