Skip to content

Instantly share code, notes, and snippets.

@ShahirZain
Created May 13, 2022 12:28
Show Gist options
  • Save ShahirZain/bc733536f8202142819688aa72509f49 to your computer and use it in GitHub Desktop.
Save ShahirZain/bc733536f8202142819688aa72509f49 to your computer and use it in GitHub Desktop.
Shared Wallet in Solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol';
import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/math/SafeMath.sol';
contract Allowance is Ownable {
using SafeMath for uint;
mapping(address => uint) public allowance;
event AllowanceChange(address indexed _forWho , address indexed _fromWhom , uint _oldAmount , uint newAmount);
modifier ownerOrAllowed (uint _amount){
require(owner() == msg.sender || allowance[msg.sender] > _amount, "You are not allowed" );
_;
}
function addAllowance (address _who , uint _amount) public onlyOwner{
emit AllowanceChange(_who , msg.sender , allowance[_who] , _amount);
allowance[_who] = _amount;
}
function reduceAllowance(address _account , uint _amount) internal {
emit AllowanceChange(_account , msg.sender , allowance[_account] , allowance[_account].sub(_amount));
allowance[_account] = allowance[_account].sub(_amount);
}
}
contract SimpleWallet is Allowance{
// address public owner;
// constructor (){
// owner = msg.sender;
// }
// modifier onlyOwner (){
// require(owner == msg.sender, "You are not allowed") ;
// _;
// }
event MoneySent (address indexed beneficiary , uint amount);
event MoneyReceived (address indexed _from , uint amount);
function withdrawMoney (address payable _to, uint _amount) public ownerOrAllowed(_amount) {
if(!(owner() == msg.sender)){
reduceAllowance(msg.sender , _amount);
}
emit MoneySent(_to , _amount);
_to.transfer(_amount);
}
function renounceOwnership () public pure override {
revert ("Can't renounce Ownership");
}
receive () external payable {
emit MoneySent(msg.sender , msg.value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment