Skip to content

Instantly share code, notes, and snippets.

@jha-adrs
Created May 16, 2023 05:05
Show Gist options
  • Save jha-adrs/8ed1d47a73a09466874547909799cc0a to your computer and use it in GitHub Desktop.
Save jha-adrs/8ed1d47a73a09466874547909799cc0a to your computer and use it in GitHub Desktop.
Solidity AllorNothing
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
contract AllorNothing {
address public owner;
mapping (address => uint256) public balances;
event Deposit(address indexed depositor, uint256 amount);
event Withdraw(address indexed withdrawer, uint256 amount);
error ZeroBalance();
constructor() {
owner = msg.sender;
balances[msg.sender] = 0;
}
function checkBal(address x) public view returns (uint256){
return balances[x];
}
function deposit() public payable {
require(msg.value >0, "Amount should be more than 0");
balances[msg.sender]+= msg.value;
emit Deposit(msg.sender, msg.value);
}
function withdraw() public {
uint256 amount = balances[msg.sender];
require(amount >0, "Cannot withdraw, Zero Balance");
balances[msg.sender] =0;
(bool success, ) = msg.sender.call{value:amount}("");
require(success, "Sorry amount could not be withdrawn");
emit Withdraw(msg.sender, amount);
}
}
//End
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment