Skip to content

Pausing Smart Contract

A Smart Contract cannot be "paused" on a protocol-level on the Ethereum Blockchain. But we can add some logic "in code" to pause it.

Let's try this!

Smart Contract Pausing Functionality

Let us update our Smart Contract and add a simple Boolean variable to see if the functionality is paused or not.

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.1;

contract StartStopUpdateExample {

    address public owner;
    bool public paused;

    constructor() {
        owner = msg.sender;
    }

    function sendMoney() public payable {

    }

    function setPaused(bool _paused) public {
        require(msg.sender == owner, "You are not the owner");
        paused = _paused;
    }

    function withdrawAllMoney(address payable _to) public {
        require(owner == msg.sender, "You cannot withdraw.");
        require(paused == false, "Contract Paused");
        _to.transfer(address(this).balance);
    }
}

Content wise, there isn't much new. We have a function that can update the paused variable. That function can only be called by the owner. And withdrawAllMoney can only be called if the contract is not paused.

Of course, this doesn't make much sense, other than being some sort of academic example. BUT! It makes sense if you think of more complex functionality, like a Token Sale that can be paused. If you have require(paused == false) in all your customer facing functions, then you can easily pause a Smart Contract.

Try the Smart Contract

Of course we're also trying if our functionality works!

  1. Deploy a new Instance
  2. Update paused to true
  3. Try to withdrawAllMoney with any of your accounts, it won't work!

In the next and last exercise for this Lab I want to destroy our Smart Contract.