RGAlex

SimpleWallet.sol

May 19th, 2021 (edited)
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. //SPDX-License-Identifier: MIT
  2. pragma solidity 0.8.1;
  3.  
  4. import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol";
  5.  
  6. contract Allowance is Ownable {
  7.  
  8. event AllowanceChanged(address indexed _forWho, address indexed _byWhom, uint _oldAmount, uint _newAmount);
  9. mapping(address => uint) public allowance;
  10.  
  11. function isOwner() internal view returns(bool) {
  12. return owner() == msg.sender;
  13. }
  14.  
  15. function setAllowance(address _who, uint _amount) public onlyOwner {
  16. emit AllowanceChanged(_who, msg.sender, allowance[_who], _amount);
  17. allowance[_who] = _amount;
  18. }
  19.  
  20. modifier ownerOrAllowed(uint _amount) {
  21. require(isOwner() || allowance[msg.sender] >= _amount, "You are not allowed!");
  22. _;
  23. }
  24.  
  25. function reduceAllowance(address _who, uint _amount) internal ownerOrAllowed(_amount) {
  26. emit AllowanceChanged(_who, msg.sender, allowance[_who], allowance[_who] - _amount);
  27. allowance[_who] -= _amount;
  28. }
  29.  
  30. }
  31. contract SharedWallet is Allowance {
  32.  
  33. event MoneySent(address indexed _beneficiary, uint _amount);
  34. event MoneyReceived(address indexed _from, uint _amount);
  35.  
  36. function withdrawMoney(address payable _to, uint _amount) public ownerOrAllowed(_amount) {
  37. require(_amount <= address(this).balance, "Contract doesn't own enough money");
  38. if(!isOwner()) {
  39. reduceAllowance(msg.sender, _amount);
  40. }
  41. emit MoneySent(_to, _amount);
  42. _to.transfer(_amount);
  43.  
  44. }
  45.  
  46. receive() external payable {
  47. emit MoneyReceived(msg.sender, msg.value);
  48. }
  49. }
Add Comment
Please, Sign In to add comment