Withdraw All Ether - Become Ethereum Blockchain Developer (2024)

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.0pragma 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:

  1. Deploy the new version and send again 1 Ether to the Smart Contract.
  2. To avoid confusion I recommend you close the previous Instance, we won't need it anymore

Withdraw All Ether - Become Ethereum Blockchain Developer (1)

At the end you should end up with one active Instance of your Smart Contract.

The same procedure as before:

  1. Put in "1 Ether" into the value input box
  2. hit "receiveMoney" in your new contract Instance

Withdraw All Ether - Become Ethereum Blockchain Developer (2)

Withdraw All Ether - Become Ethereum Blockchain Developer (3)

Your balance should be 1 Ether again:

Withdraw All Ether - Become Ethereum Blockchain Developer (4)

Not 1 Ether?

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:

Withdraw All Ether - Become Ethereum Blockchain Developer (5)

Then hit the "withdrawMoney" button:

Withdraw All Ether - Become Ethereum Blockchain Developer (6)

Observe the amount of Ether you have now in your Account:

Withdraw All Ether - Become Ethereum Blockchain Developer (7)

It's more than the previous 100 Ether! We got our 1 Ether through our Smart Contract into another Account! AWESOME!

Why not 101 Ether?

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!

Try yourself first?

If you want to try yourself first, then do the following:

  1. Create a new function that takes one address as argument
  2. 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!

Last update: March 28, 2022

Withdraw All Ether - Become Ethereum Blockchain Developer (2024)
Top Articles
Latest Posts
Article information

Author: Sen. Emmett Berge

Last Updated:

Views: 6130

Rating: 5 / 5 (80 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Sen. Emmett Berge

Birthday: 1993-06-17

Address: 787 Elvis Divide, Port Brice, OH 24507-6802

Phone: +9779049645255

Job: Senior Healthcare Specialist

Hobby: Cycling, Model building, Kitesurfing, Origami, Lapidary, Dance, Basketball

Introduction: My name is Sen. Emmett Berge, I am a funny, vast, charming, courageous, enthusiastic, jolly, famous person who loves writing and wants to share my knowledge and understanding with you.