-
Notifications
You must be signed in to change notification settings - Fork 0
/
CounterAllowance.sol
50 lines (43 loc) · 1.82 KB
/
CounterAllowance.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
46
47
48
49
50
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
abstract contract CounterAllowance {
event CounterAllowanceInitialized(
address indexed account, bytes32 indexed permissionHash, CounterAllowanceDetails allowance
);
error CounterAllowanceExceeded();
struct CounterAllowanceDetails {
/// @dev Start time of the allowance (unix seconds).
uint48 start;
/// @dev Time duration (seconds).
uint48 period;
/// @dev Maximum allowed value to spend
uint160 allowance;
}
/// @notice Packed recurring allowance values (start, period) for the permission.
mapping(address account => mapping(bytes32 permissionHash => CounterAllowanceDetails)) internal _recurringAllowances;
function getCounterAllowanceDetails(address account, bytes32 permissionHash)
public
view
returns (CounterAllowanceDetails memory)
{
return _recurringAllowances[account][permissionHash];
}
function _initializeCounterAllowance(
address account,
bytes32 permissionHash,
CounterAllowanceDetails memory allowance
) internal {
_recurringAllowances[account][permissionHash] = allowance;
emit CounterAllowanceInitialized(account, permissionHash, allowance);
}
function _useCounterAllowance(address account, bytes32 permissionHash) internal {
CounterAllowanceDetails storage allowance = _recurringAllowances[account][permissionHash];
require(allowance.start <= block.timestamp, "CounterAllowance: allowance not started");
require(allowance.start + allowance.period >= block.timestamp, "CounterAllowance: allowance expired");
if (allowance.allowance > 0) {
allowance.allowance--;
} else {
revert CounterAllowanceExceeded();
}
}
}