Skip to content

Instantly share code, notes, and snippets.

@logankilpatrick
Created March 11, 2021 14:58
Show Gist options
  • Save logankilpatrick/f273c416232c2aaf517e45416b7496ee to your computer and use it in GitHub Desktop.
Save logankilpatrick/f273c416232c2aaf517e45416b7496ee 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.7.4+commit.3f05b770.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.8.0;
contract Bank {
address private owner;
mapping (address => uint) private balances;
event LogDeposit(address depositor, uint amount);
modifier onlyOwner() {
require(msg.sender == owner, "Caller is not owner");
_;
}
constructor() {
owner = msg.sender;
}
// function internal
function deposit() public payable returns (uint newBalance) {
balances[msg.sender] += msg.value;
emit LogDeposit(msg.sender, msg.value);
return balances[msg.sender];
}
function attemptSend(address payable account, uint withdrawAmount) internal {
bool sent = account.send(withdrawAmount);
if ( !sent ) {
balances[account] += withdrawAmount;
}
}
function withdraw(uint withdrawAmount) external returns (uint newBalance) {
if ( balances[msg.sender] >= withdrawAmount ) {
balances[msg.sender] -= withdrawAmount;
attemptSend(msg.sender, withdrawAmount);
}
return balances[msg.sender];
}
function balance() public view returns (uint) {
return balances[msg.sender];
}
function balance(address account) public view onlyOwner returns (uint) {
return balances[account];
}
fallback () external {
revert();
}
}
// this line is added to create a gist. Empty file is not allowed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment