Skip to content

Withdraw To Specific Account

Previously we had our Smart Contract just blindly send the Ether to whoever called the Smart Contracts "withdrawMoney" function. Let's extend this a bit so that the Funds can be send to a specific Account.

It's still not secure, as basically anyone could interact with that function, but it's one step closer!

// 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);

    function withdrawMoneyTo(address payable _to) public {

As you can see, we can now specify an Address the money will be transferred to! Let's give this a try!

Redeploy our Smart Contract

Of course, we need to re-deploy our Smart Contract. There are no live-updates (yet?!). Same procedure as before:

  1. Deploy the Smart Contract
  2. Close the old Instance
  3. Send 1 Ether to the Smart Contract (don't forget the value input field!)
  4. Make sure the Balance shows up correctly.

Test the "withdrawMoneyTo" function

Now it's time to test the new function. We're going to make things a bit more exciting ✨: We're going to use our first account to send all funds to the third account. Why? Because we can πŸ™„. And because it's important to understand how gas fees are working - who is paying for the transaction.

  1. Select the third account from the dropdown

  1. Hit the little "copy" icon:

  1. Switch back to the first Account:

  1. Paste the Account you copied into the input field next to "withdrawMoneyTo":

No function with that name?

If there's no function with that name, then you are most likely still interacting with the old Instance. Re-Deploy or even reload the whole page.

  1. Hit the "withdrawMoneyTo" button and see what happens! Wow, nothing 🀣. Well, only on the surface!

  2. Now open the Accounts dropdown. See the balance of your third Account? 101 Ether!!!

Why is there 101 Ether and not 100.999999999some? Because we sent a transaction from Account #1 to the Smart Contract, instructing the Smart Contract to send all funds stored on the Address of the Smart Contract to the third Account in your Account-List. Gas fees were paid by Account #1. Account #3 got 1 full Ether!

Time-Locked Withdrawals

That's all cool and fun so far, but let's go one step further and introduce block.timestamp. This gobal object contains the timestamp when a block was mined. It's not necessarily the current timestamp when the execution happens. It might be a few seconds off. But it's still enough to do some locking.

Next up I want to write a short Smart Contract that only allows withdrawal if the last deposit was more than 1 Minute ago.

Try yourself first?

If you want to try yourself first, then we extend the Smart Contract and store the "block.timestamp" somewhere. Withdrawals can only happen if the "block.timestamp" during withdrawal is greater than the previously stored timestamp + 1 minutes (that is a globally available constant in Solidity)

This is potentially not in the course videos. This exercise is optional.

Last update: March 28, 2022