-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathallowance.sol
45 lines (38 loc) · 1.21 KB
/
allowance.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//SPDX-License-Identifier: MIT
pragma solidity 0.8.1;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol";
contract Allowance is Ownable {
event AllowanceChanged(
address indexed _forWho,
address indexed _byWhom,
uint256 _oldAmount,
uint256 _newAmount
);
mapping(address => uint256) public allowance;
function isOwner() internal view returns (bool) {
return owner() == msg.sender;
}
function setAllowance(address _who, uint256 _amount) public onlyOwner {
emit AllowanceChanged(_who, msg.sender, allowance[_who], _amount);
allowance[_who] = _amount;
}
modifier ownerOrAllowed(uint256 _amount) {
require(
isOwner() || allowance[msg.sender] >= _amount,
"You are not allowed!"
);
_;
}
function reduceAllowance(address _who, uint256 _amount)
internal
ownerOrAllowed(_amount)
{
emit AllowanceChanged(
_who,
msg.sender,
allowance[_who],
allowance[_who] - _amount
);
allowance[_who] -= _amount;
}
}