Last active
August 15, 2020 17:23
-
-
Save MXmaster2s/b53a2822bd876cd8bd11830a24ac2477 to your computer and use it in GitHub Desktop.
SharedWaller.sol allows the contract-creator to set allowance limit to any address. The users can withdraw money from the wallet based on the set allowance. 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.6.1+commit.e6f7d5a4…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.6.1; | |
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol"; | |
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/SafeMath.sol"; | |
contract Allowance is Ownable { | |
using SafeMath for uint256; | |
event AllowanceChanged(address indexed _forWho, address indexed _fromWho, uint256 _oldAmount, uint256 _newAmount); | |
mapping (address => uint256) public allowance; | |
function setAllowance(address payable _who, uint256 _amount) public onlyOwner { | |
emit AllowanceChanged(_who, msg.sender, allowance[_who], allowance[_who].add(_amount)); | |
allowance[_who] = _amount; | |
} | |
function isOwner() internal view returns (bool) { | |
return owner() == msg.sender; | |
} | |
//Modifier to check if the user is the Owner or if the amount he is withdrawing is less or equal to how much he has in his account | |
modifier ownerOrAllowed(uint256 _amount) { | |
require(isOwner() || allowance[msg.sender] >= _amount, "You are not allowed!"); | |
_; | |
} | |
//reduce allowance of user after he withdraws money | |
function reduceAllowance(address _who, uint256 _amount) internal { | |
emit AllowanceChanged(_who, msg.sender, allowance[_who], allowance[_who].sub(_amount)); | |
allowance[_who] = allowance[_who].sub(_amount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment