Created
June 16, 2022 06:32
-
-
Save jw122/dae119c10dd3cc1b74d5ac8e9b06e726 to your computer and use it in GitHub Desktop.
Simple smart contract wallet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity >=0.7.0 <0.9.0; | |
// From Block Explorer's tutorial video: https://www.youtube.com/watch?v=UoGzV094jhE | |
// A smart contract acts like a wallet that can receive funds | |
contract EtherWallet { | |
// a state variable for the owner. Only the owner will be able to withdraw funds from the wallet | |
address payable public owner; | |
constructor() { | |
// the constructor is called 1 time: when the contract is deployed. Run any initialization code here | |
owner = payable(msg.sender); // msg is a global variable. msg.sender gives us the address that is deploying this contract. By default, msg.sender is not payable so we cast it to payable | |
} | |
// receive ether coming in (a default function). Doesn't require the function keyword | |
receive() external payable {} // invoked when ether is sent to the contract | |
function withdraw(uint _amount) external { | |
// put a check in place ot make sure the sender is the owner | |
require(msg.sender == owner, "Only the owner can call this method"); | |
// transfer funds to sender | |
payable(msg.sender).transfer(_amount); | |
} | |
// query the balance of the smart contract. External means this function can be involved by external parties. View means it is read-only | |
function getBalance() external view returns (uint) { | |
return address(this).balance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment