assigned beneficiary can withdraw based on allowance variable
This is what we cooked up initially. We coded our own basic owner functionality with the variable and require statements.
//SPDX-License-Identifier: MIT pragmasolidity ^0.8.1; contract SharedWallet {address owner;uint balanceaddress receiver;uint allowance;constructor(){ owner = msg.sender; //attribute owner on deployment }functiondeposit() payablepublic{ balance += msg.value; }// OR :receive () externalpayable{ balance += msg.value; }// centralising permissions:modifieronlyOwner(){require(msg.sender == owner,"you are not the owner"); _; }functionwithdraw(uint_w_amt,address payable _to) publiconlyOwner {require(_w_amt <= balance,"Not enough balance"); balance -= _w_amt; _to.transfer(_w_amt); }functionreceiveAllowance(uint_w_amt,address payable _to) public {require(msg.sender == receiver,"must be beneficiary");require(_w_amt <= allowance,"Cannot exceed allowance or balance"); balance -= _w_amt; _to.transfer(_w_amt); }}
However, OpenZeppelin has already built a comprehensive ownership functionality, allowing for transfers, renouncement and much more. We can simply re-use it, by importing it.
Our modifier onlyOwner should be removed as Ownable.sol also defines onlyOwner (line 42). This will cause an error, with Solidity expecting the override specifier.
//SPDX-License-Identifier: MITpragmasolidity ^0.8.1;// for Owner functionality - no need to built youselfimport"https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol";contractSharedWalletisOwnable{ //to extend base Ownableuint balance;address receiver;uint allowance;receive () externalpayable{ //receive eth balance += msg.value; }//check if you are the ownerfunctionisOwner() internalviewreturns(bool){returnowner() == msg.sender; }functionwithdraw(uint_w_amt,address payable _to) publiconlyOwner {require(_w_amt <= balance,"Not enough balance"); balance -= _w_amt; _to.transfer(_w_amt); }functionreceiveAllowance(uint_w_amt,address payable _to) public {require(msg.sender == receiver,"must be beneficiary");require(_w_amt <= allowance,"Cannot exceed allowance or balance"); balance -= _w_amt; _to.transfer(_w_amt); }}
The latest OpenZeppelin contract does not have an isOwner() function anymore, so we have to create our own.
Note that the owner() is a function from the Ownable.sol contract.