-
-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathNativeTokenTransferAmountEnforcer.sol
More file actions
70 lines (57 loc) · 2.63 KB
/
NativeTokenTransferAmountEnforcer.sol
File metadata and controls
70 lines (57 loc) · 2.63 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;
import { ExecutionLib } from "@erc7579/lib/ExecutionLib.sol";
import { CaveatEnforcer } from "./CaveatEnforcer.sol";
import { ModeCode } from "../utils/Types.sol";
/**
* @title NativeTokenTransferAmountEnforcer
* @notice This contract enforces an allowance of native currency (e.g., ETH) for a specific delegation.
* @dev This enforcer operates only in single execution call type and with default execution mode.
*/
contract NativeTokenTransferAmountEnforcer is CaveatEnforcer {
using ExecutionLib for bytes;
////////////////////////////// State //////////////////////////////
/// @notice Mapping to store used allowance for each delegation
mapping(address sender => mapping(bytes32 delegationHash => uint256 amount)) public spentMap;
////////////////////////////// Events //////////////////////////////
event IncreasedSpentMap(
address indexed sender, address indexed redeemer, bytes32 indexed delegationHash, uint256 limit, uint256 spent
);
////////////////////////////// External Functions //////////////////////////////
/**
* @notice Enforces the conditions that should hold before a transaction is performed.
* @param _terms The encoded amount of native token allowance.
* @param _mode The execution mode. (Must be Single callType, Default execType)
* @param _executionCallData The call data of the execution.
* @param _delegationHash The hash of the delegation.
*/
function beforeHook(
bytes calldata _terms,
bytes calldata,
ModeCode _mode,
bytes calldata _executionCallData,
bytes32 _delegationHash,
address,
address _redeemer
)
public
override
onlySingleCallTypeMode(_mode)
onlyDefaultExecutionMode(_mode)
{
// Decode the total allowance from _terms
uint256 allowance_ = getTermsInfo(_terms);
(, uint256 value_,) = _executionCallData.decodeSingle();
uint256 spent_ = spentMap[msg.sender][_delegationHash] += value_;
require(spent_ <= allowance_, "NativeTokenTransferAmountEnforcer:allowance-exceeded");
emit IncreasedSpentMap(msg.sender, _redeemer, _delegationHash, allowance_, spent_);
}
/**
* @notice Decodes the terms used in this CaveatEnforcer.
* @param _terms The encoded amount of native token allowance.
* @return allowance_ The maximum number of tokens that the delegate is allowed to transfer.
*/
function getTermsInfo(bytes calldata _terms) public pure returns (uint256 allowance_) {
allowance_ = abi.decode(_terms, (uint256));
}
}