Skip to content

Instantly share code, notes, and snippets.

@jremi
Created September 13, 2021 06:57
Show Gist options
  • Save jremi/baf4c9ca7c6a10cc5744af2ce820b7d3 to your computer and use it in GitHub Desktop.
Save jremi/baf4c9ca7c6a10cc5744af2ce820b7d3 to your computer and use it in GitHub Desktop.
SimpleWallet.sol
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
contract SimpleWallet {
address private owner;
struct AccountAllowancePermission {
uint maxWithdrawalAmount;
}
mapping(address => AccountAllowancePermission) private AccountAllowance;
modifier isOwner () {
require(msg.sender == owner, 'Not contract owner!');
_;
}
constructor() {
owner = msg.sender;
}
function getContractBalance() public view returns (uint) {
return address(this).balance;
}
function getRegularAccountMaxWithdrawalAmount() private view returns (uint) {
uint regularAccountDefaultMax = 1 ether;
uint regularAccount = AccountAllowance[msg.sender].maxWithdrawalAmount;
return regularAccount > 0 ? regularAccount : regularAccountDefaultMax;
}
function getMaxWithdrawalAllowed() public view returns (uint) {
uint ownerMax = getContractBalance();
uint regularMax = getRegularAccountMaxWithdrawalAmount();
return msg.sender == owner ? ownerMax : regularMax;
}
function withdraw(uint _amount) public payable {
uint maxAmountAllowed = getMaxWithdrawalAllowed();
if (_amount <= maxAmountAllowed) {
address payable _to = payable(msg.sender);
_to.transfer(_amount);
} else {
revert('Amount exceeds max withdrawal amount!');
}
}
function changeAllowance(address _address, uint _amount) isOwner public {
AccountAllowance[_address].maxWithdrawalAmount = _amount;
}
receive() external payable {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment