Skip to content

Commit 18737a5

Browse files
committed
Improved comments, and default boolean
1 parent b817bd6 commit 18737a5

8 files changed

Lines changed: 171 additions & 156 deletions

src/enforcers/ERC1155BalanceChangeEnforcer.sol

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@ import { ModeCode } from "../utils/Types.sol";
88

99
/**
1010
* @title ERC1155BalanceChangeEnforcer
11-
* @dev This contract enforces that the ERC1155 token balance of a recipient for a specific token ID
12-
* has changed by at least the specified amount after the execution, measured between the `beforeHook` and `afterHook` calls.
13-
* The change can be either an increase or decrease based on the `shouldBalanceIncrease` flag.
11+
* @dev This contract allows setting up some guardrails around balance changes. By specifying an amount and a direction
12+
* (decrease/increase), one can enforce a maximum decrease or minimum increase in after-execution balance.
13+
* The change can be either a decrease or increase based on the `isDecrease` flag.
14+
* @dev This contract has no enforcement of how the balance changes. It's meant to be used alongside additional enforcers to
15+
* create granular permissions.
1416
* @dev This enforcer operates only in default execution mode.
1517
* @dev Security Notice: This enforcer tracks balance changes by comparing the recipient's balance before and after execution. Since
1618
* enforcers watching the same recipient share state, a single balance modification may satisfy multiple enforcers simultaneously.
1719
* Users should avoid tracking the same recipient's balance on multiple enforcers in a single delegation chain to prevent unintended
18-
* behavior.
20+
* behavior. Given its potential for concurrent condition fulfillment, use this enforcer at your own risk and ensure it aligns with
21+
* your intended security model.
1922
*/
2023
contract ERC1155BalanceChangeEnforcer is CaveatEnforcer {
2124
////////////////////////////// State //////////////////////////////
@@ -53,11 +56,12 @@ contract ERC1155BalanceChangeEnforcer is CaveatEnforcer {
5356
/**
5457
* @notice This function caches the recipient's ERC1155 token balance before the delegation is executed.
5558
* @param _terms 105 bytes where:
56-
* - first byte: boolean indicating if the balance should increase
59+
* - first byte: boolean indicating if the balance should decrease
5760
* - next 20 bytes: address of the ERC1155 token
5861
* - next 20 bytes: address of the recipient
5962
* - next 32 bytes: token ID
60-
* - next 32 bytes: amount the balance should change by
63+
* - next 32 bytes: balance change guardrail amount (i.e., minimum increase OR maximum decrease, depending on
64+
* isDecrease)
6165
* @param _mode The execution mode. (Must be Default execType)
6266
* @param _delegationHash The hash of the delegation.
6367
*/
@@ -83,13 +87,14 @@ contract ERC1155BalanceChangeEnforcer is CaveatEnforcer {
8387
}
8488

8589
/**
86-
* @notice This function enforces that the recipient's ERC1155 token balance has changed by at least the amount provided.
90+
* @notice This function enforces that the recipient's ERC1155 token balance has changed by the expected amount.
8791
* @param _terms 105 bytes where:
88-
* - first byte: boolean indicating if the balance should increase
92+
* - first byte: boolean indicating if the balance should decrease
8993
* - next 20 bytes: address of the ERC1155 token
9094
* - next 20 bytes: address of the recipient
9195
* - next 32 bytes: token ID
92-
* - next 32 bytes: amount the balance should change by
96+
* - next 32 bytes: balance change guardrail amount (i.e., minimum increase OR maximum decrease, depending on
97+
* isDecrease)
9398
* @param _delegationHash The hash of the delegation.
9499
*/
95100
function afterHook(
@@ -104,33 +109,34 @@ contract ERC1155BalanceChangeEnforcer is CaveatEnforcer {
104109
public
105110
override
106111
{
107-
(bool shouldBalanceIncrease_, address token_, address recipient_, uint256 tokenId_, uint256 amount_) = getTermsInfo(_terms);
112+
(bool isDecrease_, address token_, address recipient_, uint256 tokenId_, uint256 amount_) = getTermsInfo(_terms);
108113
bytes32 hashKey_ = _getHashKey(msg.sender, token_, recipient_, tokenId_, _delegationHash);
109114
delete isLocked[hashKey_];
110115
uint256 balance_ = IERC1155(token_).balanceOf(recipient_, tokenId_);
111-
if (shouldBalanceIncrease_) {
112-
require(balance_ >= balanceCache[hashKey_] + amount_, "ERC1155BalanceChangeEnforcer:insufficient-balance-increase");
113-
} else {
116+
if (isDecrease_) {
114117
require(balance_ >= balanceCache[hashKey_] - amount_, "ERC1155BalanceChangeEnforcer:exceeded-balance-decrease");
118+
} else {
119+
require(balance_ >= balanceCache[hashKey_] + amount_, "ERC1155BalanceChangeEnforcer:insufficient-balance-increase");
115120
}
116121
}
117122

118123
/**
119124
* @notice Decodes the terms used in this CaveatEnforcer.
120125
* @param _terms Encoded data that is used during the execution hooks.
121-
* @return shouldBalanceIncrease_ Boolean indicating if the balance should increase (true) or decrease (false).
126+
* @return isDecrease_ Boolean indicating if the balance should decrease (true) or increase (false).
122127
* @return token_ The address of the ERC1155 token.
123128
* @return recipient_ The address of the recipient of the token.
124129
* @return tokenId_ The ID of the ERC1155 token.
125-
* @return amount_ The amount the balance should change by.
130+
* @return amount_ Balance change guardrail amount (i.e., minimum increase OR maximum decrease, depending on
131+
* isDecrease)
126132
*/
127133
function getTermsInfo(bytes calldata _terms)
128134
public
129135
pure
130-
returns (bool shouldBalanceIncrease_, address token_, address recipient_, uint256 tokenId_, uint256 amount_)
136+
returns (bool isDecrease_, address token_, address recipient_, uint256 tokenId_, uint256 amount_)
131137
{
132138
require(_terms.length == 105, "ERC1155BalanceChangeEnforcer:invalid-terms-length");
133-
shouldBalanceIncrease_ = _terms[0] != 0;
139+
isDecrease_ = _terms[0] != 0;
134140
token_ = address(bytes20(_terms[1:21]));
135141
recipient_ = address(bytes20(_terms[21:41]));
136142
tokenId_ = uint256(bytes32(_terms[41:73]));

src/enforcers/ERC20BalanceChangeEnforcer.sol

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@ import { ModeCode } from "../utils/Types.sol";
88

99
/**
1010
* @title ERC20BalanceChangeEnforcer
11-
* @dev This contract enforces that the delegator's ERC20 balance has changed by at least the specified amount
12-
* after the execution has been executed, measured between the `beforeHook` and `afterHook` calls, regardless of what the execution
13-
* is. The change can be either an increase or decrease based on the `shouldBalanceIncrease` flag.
11+
* @dev This contract allows setting up some guardrails around balance changes. By specifying an amount and a direction
12+
* (decrease/increase), one can enforce a maximum decrease or minimum increase in after-execution balance.
13+
* The change can be either a decrease or increase based on the `isDecrease` flag.
1414
* @dev This contract has no enforcement of how the balance changes. It's meant to be used alongside additional enforcers to
1515
* create granular permissions.
1616
* @dev This enforcer operates only in default execution mode.
1717
* @dev Security Notice: This enforcer tracks balance changes by comparing the recipient's balance before and after execution. Since
1818
* enforcers watching the same recipient share state, a single balance modification may satisfy multiple enforcers simultaneously.
1919
* Users should avoid tracking the same recipient's balance on multiple enforcers in a single delegation chain to prevent unintended
20-
* behavior.
20+
* behavior. Given its potential for concurrent condition fulfillment, use this enforcer at your own risk and ensure it aligns with
21+
* your intended security model.
2122
*/
2223
contract ERC20BalanceChangeEnforcer is CaveatEnforcer {
2324
////////////////////////////// State //////////////////////////////
@@ -43,10 +44,11 @@ contract ERC20BalanceChangeEnforcer is CaveatEnforcer {
4344
/**
4445
* @notice This function caches the delegators ERC20 balance before the delegation is executed.
4546
* @param _terms 73 packed bytes where:
46-
* - first byte: boolean indicating if the balance should increase
47+
* - first byte: boolean indicating if the balance should decrease
4748
* - next 20 bytes: address of the token
4849
* - next 20 bytes: address of the recipient
49-
* - next 32 bytes: amount the balance should change by
50+
* - next 32 bytes: balance change guardrail amount (i.e., minimum increase OR maximum decrease, depending on
51+
* isDecrease)
5052
* @param _mode The execution mode. (Must be Default execType)
5153
*/
5254
function beforeHook(
@@ -71,12 +73,13 @@ contract ERC20BalanceChangeEnforcer is CaveatEnforcer {
7173
}
7274

7375
/**
74-
* @notice This function enforces that the delegators ERC20 balance has changed by at least the amount provided.
76+
* @notice This function enforces that the delegators ERC20 balance has changed by the expected amount.
7577
* @param _terms 73 packed bytes where:
76-
* - first byte: boolean indicating if the balance should increase
78+
* - first byte: boolean indicating if the balance should decrease
7779
* - next 20 bytes: address of the token
7880
* - next 20 bytes: address of the recipient
79-
* - next 32 bytes: amount the balance should change by
81+
* - next 32 bytes: balance change guardrail amount (i.e., minimum increase OR maximum decrease, depending on
82+
* isDecrease)
8083
*/
8184
function afterHook(
8285
bytes calldata _terms,
@@ -90,32 +93,33 @@ contract ERC20BalanceChangeEnforcer is CaveatEnforcer {
9093
public
9194
override
9295
{
93-
(bool shouldBalanceIncrease_, address token_, address recipient_, uint256 amount_) = getTermsInfo(_terms);
96+
(bool isDecrease_, address token_, address recipient_, uint256 amount_) = getTermsInfo(_terms);
9497
bytes32 hashKey_ = _getHashKey(msg.sender, token_, _delegationHash);
9598
delete isLocked[hashKey_];
9699
uint256 balance_ = IERC20(token_).balanceOf(recipient_);
97-
if (shouldBalanceIncrease_) {
98-
require(balance_ >= balanceCache[hashKey_] + amount_, "ERC20BalanceChangeEnforcer:insufficient-balance-increase");
99-
} else {
100+
if (isDecrease_) {
100101
require(balance_ >= balanceCache[hashKey_] - amount_, "ERC20BalanceChangeEnforcer:exceeded-balance-decrease");
102+
} else {
103+
require(balance_ >= balanceCache[hashKey_] + amount_, "ERC20BalanceChangeEnforcer:insufficient-balance-increase");
101104
}
102105
}
103106

104107
/**
105108
* @notice Decodes the terms used in this CaveatEnforcer.
106109
* @param _terms encoded data that is used during the execution hooks.
107-
* @return shouldBalanceIncrease_ Boolean indicating if the balance should increase (true) or decrease (false).
110+
* @return isDecrease_ Boolean indicating if the balance should decrease (true) or increase (false).
108111
* @return token_ The address of the token.
109112
* @return recipient_ The address of the recipient.
110-
* @return amount_ The amount the balance should change by.
113+
* @return amount_ Balance change guardrail amount (i.e., minimum increase OR maximum decrease, depending on
114+
* isDecrease)
111115
*/
112116
function getTermsInfo(bytes calldata _terms)
113117
public
114118
pure
115-
returns (bool shouldBalanceIncrease_, address token_, address recipient_, uint256 amount_)
119+
returns (bool isDecrease_, address token_, address recipient_, uint256 amount_)
116120
{
117121
require(_terms.length == 73, "ERC20BalanceChangeEnforcer:invalid-terms-length");
118-
shouldBalanceIncrease_ = _terms[0] != 0;
122+
isDecrease_ = _terms[0] != 0;
119123
token_ = address(bytes20(_terms[1:21]));
120124
recipient_ = address(bytes20(_terms[21:41]));
121125
amount_ = uint256(bytes32(_terms[41:]));

src/enforcers/ERC721BalanceChangeEnforcer.sol

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@ import { ModeCode } from "../utils/Types.sol";
88

99
/**
1010
* @title ERC721BalanceChangeEnforcer
11-
* @dev This contract enforces that the ERC721 token balance of a recipient has changed by at least the specified amount
12-
* after the execution, measured between the `beforeHook` and `afterHook` calls, regardless of what the execution is.
13-
* The change can be either an increase or decrease based on the `shouldBalanceIncrease` flag.
11+
* @dev This contract allows setting up some guardrails around balance changes. By specifying an amount and a direction
12+
* (decrease/increase), one can enforce a maximum decrease or minimum increase in after-execution balance.
13+
* The change can be either a decrease or increase based on the `isDecrease` flag.
14+
* @dev This contract has no enforcement of how the balance changes. It's meant to be used alongside additional enforcers to
15+
* create granular permissions.
1416
* @dev This enforcer operates only in default execution mode.
15-
* @dev Security Notice: This enforcer tracks balance changes by comparing the recipient's balance before and after execution.
16-
* Since enforcers watching the same recipient share state, a single balance modification may satisfy multiple enforcers
17-
* simultaneously. Users should avoid tracking the same recipient's balance on multiple enforcers in a single delegation chain to
18-
* prevent unintended behavior.
17+
* @dev Security Notice: This enforcer tracks balance changes by comparing the recipient's balance before and after execution. Since
18+
* enforcers watching the same recipient share state, a single balance modification may satisfy multiple enforcers simultaneously.
19+
* Users should avoid tracking the same recipient's balance on multiple enforcers in a single delegation chain to prevent unintended
20+
* behavior. Given its potential for concurrent condition fulfillment, use this enforcer at your own risk and ensure it aligns with
21+
* your intended security model.
1922
*/
2023
contract ERC721BalanceChangeEnforcer is CaveatEnforcer {
2124
////////////////////////////// State //////////////////////////////
@@ -51,10 +54,11 @@ contract ERC721BalanceChangeEnforcer is CaveatEnforcer {
5154
/**
5255
* @notice This function caches the delegator's ERC721 token balance before the delegation is executed.
5356
* @param _terms 73 bytes where:
54-
* - first byte: boolean indicating if the balance should increase
57+
* - first byte: boolean indicating if the balance should decrease
5558
* - next 20 bytes: address of the ERC721 token
5659
* - next 20 bytes: address of the recipient
57-
* - next 32 bytes: amount the balance should change by
60+
* - next 32 bytes: balance change guardrail amount (i.e., minimum increase OR maximum decrease, depending on
61+
* isDecrease)
5862
* @param _mode The execution mode. (Must be Default execType)
5963
* @param _delegationHash The hash of the delegation.
6064
*/
@@ -80,12 +84,13 @@ contract ERC721BalanceChangeEnforcer is CaveatEnforcer {
8084
}
8185

8286
/**
83-
* @notice This function enforces that the delegator's ERC721 token balance has changed by at least the amount provided.
87+
* @notice This function enforces that the delegator's ERC721 token balance has changed by the expected amount.
8488
* @param _terms 73 bytes where:
85-
* - first byte: boolean indicating if the balance should increase
89+
* - first byte: boolean indicating if the balance should decrease
8690
* - next 20 bytes: address of the ERC721 token
8791
* - next 20 bytes: address of the recipient
88-
* - next 32 bytes: amount the balance should change by
92+
* - next 32 bytes: balance change guardrail amount (i.e., minimum increase OR maximum decrease, depending on
93+
* isDecrease)
8994
* @param _delegationHash The hash of the delegation.
9095
*/
9196
function afterHook(
@@ -100,32 +105,33 @@ contract ERC721BalanceChangeEnforcer is CaveatEnforcer {
100105
public
101106
override
102107
{
103-
(bool shouldBalanceIncrease_, address token_, address recipient_, uint256 amount_) = getTermsInfo(_terms);
108+
(bool isDecrease_, address token_, address recipient_, uint256 amount_) = getTermsInfo(_terms);
104109
bytes32 hashKey_ = _getHashKey(msg.sender, token_, recipient_, _delegationHash);
105110
delete isLocked[hashKey_];
106111
uint256 balance_ = IERC721(token_).balanceOf(recipient_);
107-
if (shouldBalanceIncrease_) {
108-
require(balance_ >= balanceCache[hashKey_] + amount_, "ERC721BalanceChangeEnforcer:insufficient-balance-increase");
109-
} else {
112+
if (isDecrease_) {
110113
require(balance_ >= balanceCache[hashKey_] - amount_, "ERC721BalanceChangeEnforcer:exceeded-balance-decrease");
114+
} else {
115+
require(balance_ >= balanceCache[hashKey_] + amount_, "ERC721BalanceChangeEnforcer:insufficient-balance-increase");
111116
}
112117
}
113118

114119
/**
115120
* @notice Decodes the terms used in this CaveatEnforcer.
116121
* @param _terms Encoded data that is used during the execution hooks.
117-
* @return shouldBalanceIncrease_ Boolean indicating if the balance should increase (true) or decrease (false).
122+
* @return isDecrease_ Boolean indicating if the balance should decrease (true) or increase (false).
118123
* @return token_ The address of the ERC721 token.
119124
* @return recipient_ The address of the recipient of the token.
120-
* @return amount_ The amount the balance should change by.
125+
* @return amount_ Balance change guardrail amount (i.e., minimum increase OR maximum decrease, depending on
126+
* isDecrease)
121127
*/
122128
function getTermsInfo(bytes calldata _terms)
123129
public
124130
pure
125-
returns (bool shouldBalanceIncrease_, address token_, address recipient_, uint256 amount_)
131+
returns (bool isDecrease_, address token_, address recipient_, uint256 amount_)
126132
{
127133
require(_terms.length == 73, "ERC721BalanceChangeEnforcer:invalid-terms-length");
128-
shouldBalanceIncrease_ = _terms[0] != 0;
134+
isDecrease_ = _terms[0] != 0;
129135
token_ = address(bytes20(_terms[1:21]));
130136
recipient_ = address(bytes20(_terms[21:41]));
131137
amount_ = uint256(bytes32(_terms[41:]));

0 commit comments

Comments
 (0)