Skip to content

Instantly share code, notes, and snippets.

@amarachiugwu
Last active August 5, 2022 00:02
Show Gist options
  • Select an option

  • Save amarachiugwu/f42698ca1cb5c8e7424677fdf75532b6 to your computer and use it in GitHub Desktop.

Select an option

Save amarachiugwu/f42698ca1cb5c8e7424677fdf75532b6 to your computer and use it in GitHub Desktop.
a simple deposit contract
// 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