Skip to content

Permissions: Allow only the Owner to Withdraw Ether

In this step we restrict withdrawal to the owner of the wallet. How can we determine the owner? It's the user who deployed the smart contract.

//SPDX-License-Identifier: MIT

pragma solidity 0.8.1;

contract SharedWallet {

    address owner;

    constructor() {
        owner = msg.sender;
    }

    modifier onlyOwner() {
        require(msg.sender == owner, "You are not allowed");
        _;
    }

    function withdrawMoney(address payable _to, uint _amount) public onlyOwner {
        _to.transfer(_amount);
    }

    receive() external payable {

    }
}

Whatch out that you also add the "onlyOwner" modifier to the withdrawMoney function!


Last update: April 29, 2022