Skip to content

Instantly share code, notes, and snippets.

@MXmaster2s
Last active August 15, 2020 17:23
Show Gist options
  • Save MXmaster2s/b53a2822bd876cd8bd11830a24ac2477 to your computer and use it in GitHub Desktop.
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…
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);
}
}
pragma solidity ^0.6.1;
import "./Allowance.sol";
contract SharedWallet is Allowance {
event MoneySent(address indexed _benificiary, uint256 _amount);
event MoneyReceived(address indexed _from, uint256 _amount);
function withdrawMoney(address payable _to, uint256 _amount) public ownerOrAllowed(_amount) {
require(_amount <= address(this).balance, "Contract doesn't own enough money!");
if(!isOwner()){
reduceAllowance(msg.sender, _amount);
}
emit MoneySent(_to, _amount);
_to.transfer(_amount);
}
function renounceOwnership() override public onlyOwner {
revert("Cannot renounce Ownership here!");
}
receive() external payable {
emit MoneyReceived(msg.sender, msg.value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment