Last active
August 5, 2022 00:02
-
-
Save amarachiugwu/f42698ca1cb5c8e7424677fdf75532b6 to your computer and use it in GitHub Desktop.
a simple deposit contract
This file contains hidden or 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.8.0; | |
| contract simpleDepositContract { | |
| // emit an event when an end users deposits | |
| event depositEvent(address user, uint amount); | |
| // track individual balances | |
| mapping(address => uint) balances; | |
| // Collect funds here like deposit | |
| function Deposit() public payable { | |
| balances[msg.sender] += msg.value; | |
| } | |
| // returns the balance of a user in the pool | |
| function userBalance(address _user) external view returns(uint) { | |
| return balances[_user]; | |
| } | |
| // special receive function that receives eth and calls deposit() | |
| receive() external payable { | |
| Deposit(); | |
| emit depositEvent(msg.sender, msg.value); | |
| } | |
| // allows a user to withdral | |
| function withdraw() public { | |
| require(balances[msg.sender] > 0, "No deposit"); | |
| balances[msg.sender] = 0; | |
| payable(msg.sender).transfer(balances[msg.sender]); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment