Skip to Content
Courses

Permissions: Allow only the Owner to Withdraw Ether

View the Full Course Now 

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 updated on