Last active
March 7, 2021 07:49
-
-
Save krittawatcode/c5c1efd37e8cca7bcd1f2dff4903a1a4 to your computer and use it in GitHub Desktop.
For ETH Shared wallet app
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
// "SPDX-License-Identifier: UNLICENSED" | |
pragma solidity ^0.8.1; | |
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol"; | |
contract Allowance is Ownable { | |
// using SafeMath for uint; | |
event AllowanceChanged(address indexed _forWho, address indexed _byWhom, uint _oldAmount, uint _newAmount); | |
function isOwner() internal view returns(bool) { | |
return owner() == msg.sender; | |
} | |
mapping(address => uint) public allowance; | |
function setAllowance(address _who, uint _amount) public onlyOwner { | |
emit AllowanceChanged(_who, msg.sender, allowance[_who], _amount); | |
allowance[_who] = _amount; | |
} | |
modifier ownerOrAllowed(uint _amount) { | |
require(isOwner() || allowance[msg.sender] >= _amount, "You are not allowed!"); | |
_; | |
} | |
function reduceAllowance(address _who, uint _amount) internal ownerOrAllowed(_amount) { | |
emit AllowanceChanged(_who, msg.sender, allowance[_who], allowance[_who] - _amount); | |
allowance[_who] -= _amount; | |
} | |
} |
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
// "SPDX-License-Identifier: UNLICENSED" | |
pragma solidity ^0.8.1; | |
import "./Allowance.sol"; | |
contract SharedWallet is Allowance { | |
event MoneySent(address indexed _beneficiary, uint _amount); | |
event MoneyReceived(address indexed _from, uint _amount); | |
function renounceOwnership() public view override onlyOwner { | |
revert("can't renounceOwnership here"); //not possible with this smart contract | |
} | |
function withdrawMoney(address payable _to, uint _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); | |
} | |
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