Withdraw Ether From Smart Contract
So far we have sent Ether to our Smart Contract. But there is currently no way to get Ether back out again! So, whatās next? Yes! A function to withdraw Ether would be good, ey?!
Add a Withdraw Function
Letās add the following function to the Smart Contract:
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.1;
contract SendMoneyExample {
uint public balanceReceived;
function receiveMoney() public payable {
balanceReceived += msg.value;
}
function getBalance() public view returns(uint) {
return address(this).balance;
}
function withdrawMoney() public {
address payable to = payable(msg.sender);
to.transfer(getBalance());
}
}
This function will send all funds stored in the Smart Contract to the person who calls the āwithdrawMoney()ā function.
Deploy the new Smart Contract
Letās try this:
- Deploy the new version and send again 1 Ether to the Smart Contract.
- To avoid confusion I recommend you close the previous Instance, we wonāt need it anymore
At the end you should end up with one active Instance of your Smart Contract.
The same procedure as before:
- Put in ā1 Etherā into the value input box
- hit āreceiveMoneyā in your new contract Instance
Your balance should be 1 Ether again:
If your balance is 0, then double check the value field
If your balance is 2 Ether, then double check the contract Instance you are interacting with!
Withdraw Funds from the Smart Contract
Now itās time we use our new function! But to make things more exciting, weāre going to withdraw to a different Account.
Select the second Account from the Accounts dropdown:
Then hit the āwithdrawMoneyā button:
Observe the amount of Ether you have now in your Account:
Itās more than the previous 100 Ether! We got our 1 Ether through our Smart Contract into another Account! AWESOME!
Are you wondering why you donāt have 101 Ether in your Account? After all, you had 100 Ether before, and now you added 1 Ether, so, why is it not 101 Ether? Is the Math you learned in school worthless?
No, the Math you learned in School comes in handy actually.
What you can observe here is the concept of āGasā on the Ethereum Blockchain. Every transaction on Ethereum costs a little bit. And itās not different here on a simulated chain. Same principles apply. How much is the Gas you paid, youāre wondering? Well, you can open the transaction details and see for yourself. Weāre covering this - in depth - later on in the course. I also made a dedicated video and blog postā about this if you want to deep dive right now.
While we can withdraw our funds now, the whole function itself is pretty useless, isnāt it?! Anyone can withdraw funds to his Account. There are no fractions of the Amount - all in all, pretty insecure.
I still hope the concept is a bit clearer now!
Letās to another function, which allows us the send the full amount to a specific Address! It will still be insecure, but at least teaches a new concept - one at a time!
If you want to try yourself first, then do the following:
- Create a new function that takes one address as argument
- The full amount of Ether stored on the Smart Contract will be sent to this address
Alright, letās do this on the next page!