Skip to content

Instantly share code, notes, and snippets.

@Oluwatobilobaoke
Created February 24, 2024 17:43
Show Gist options
  • Save Oluwatobilobaoke/782668e19f08984a873aacd27ead2445 to your computer and use it in GitHub Desktop.
Save Oluwatobilobaoke/782668e19f08984a873aacd27ead2445 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.24+commit.e11b9ed9.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
error INSUFFICIENT_FUNDS();
error AMOUNT_CANT_BE_ZERO();
error ADDRESS_CANT_BE_ZERO();
contract SaveEther {
mapping(address => uint256) savings;
event SavingSuccessful(address indexed user, uint256 indexed amount);
function deposit() external payable {
if(msg.sender == address(0))
revert ADDRESS_CANT_BE_ZERO();
if(msg.value <= 0)
revert AMOUNT_CANT_BE_ZERO();
savings[msg.sender] = savings[msg.sender] + msg.value;
emit SavingSuccessful(msg.sender, msg.value);
}
function withdraw() external {
if(msg.sender == address(0))
revert ADDRESS_CANT_BE_ZERO();
uint256 _userSavings = savings[msg.sender];
if(_userSavings <= 0)
revert INSUFFICIENT_FUNDS();
savings[msg.sender] -= _userSavings;
payable(msg.sender).transfer(_userSavings);
}
function checkSavings(address _user) external view returns (uint256) {
return savings[_user];
}
function checkContractBal() external view returns (uint256) {
return address(this).balance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment