-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTransferLimitPlugin.sol
281 lines (224 loc) · 10.3 KB
/
TransferLimitPlugin.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {
ManifestAssociatedFunction,
ManifestAssociatedFunctionType,
ManifestFunction,
ManifestExecutionHook,
PluginManifest,
PluginMetadata,
SelectorPermission
} from "modular-account-libs/interfaces/IPlugin.sol";
import {BasePlugin} from "modular-account-libs/plugins/BasePlugin.sol";
import {IMultiOwnerPlugin} from "modular-account/plugins/owner/IMultiOwnerPlugin.sol";
import {IStandardExecutor, Call} from "modular-account/interfaces/IStandardExecutor.sol";
import {
AssociatedLinkedListSet,
AssociatedLinkedListSetLib,
SetValue
} from "modular-account/libraries/AssociatedLinkedListSetLib.sol";
import {ITransferLimitPlugin} from "src/interfaces/ITransferLimitPlugin.sol";
contract TransferLimitPlugin is ITransferLimitPlugin, BasePlugin {
using AssociatedLinkedListSetLib for AssociatedLinkedListSet;
enum FunctionId {
EXECUTE_FUNCTION,
EXECUTE_BATCH_FUNCTION
}
struct ERC20SpendLimit {
address token;
uint256 limit;
}
uint256 internal constant _MANIFEST_DEPENDENCY_INDEX_MULTI_OWNER_USER_OP_VALIDATION = 0;
string internal constant _NAME = "ERC20 Transfer Limit Plugin";
string internal constant _VERSION = "1.0.0";
string internal constant _AUTHOR = "Metalamp";
AssociatedLinkedListSet private _tokenList;
mapping(address account => mapping(address token => uint256 limit)) private _limits;
/*//////////////////////////////////////////////////////////////
ERRORS
//////////////////////////////////////////////////////////////*/
error EndBalanceLessThanLimit(address token, uint256 limit);
error InsufficientBalance(address token, uint256 balance);
/*//////////////////////////////////////////////////////////////
EVENTS
//////////////////////////////////////////////////////////////*/
event LimitUpdated(address indexed token, uint256 limit);
/*//////////////////////////////////////////////////////////////
VALIDATION FUNCTIONS
//////////////////////////////////////////////////////////////*/
// region - Validation
function preExecutionHook(uint8 functionId, address sender, uint256 value, bytes calldata data)
external
view
override
returns (bytes memory)
{
(sender, value);
if (functionId == uint8(FunctionId.EXECUTE_FUNCTION)) {
(address token,, bytes memory innerCalldata) = abi.decode(data[4:], (address, uint256, bytes));
if (_tokenList.contains(msg.sender, SetValue.wrap(bytes30(bytes20(token))))) {
_checkLimit(token, innerCalldata);
}
} else if (functionId == uint8(FunctionId.EXECUTE_BATCH_FUNCTION)) {
Call[] memory calls = abi.decode(data[4:], (Call[]));
uint256 length = calls.length;
for (uint8 i = 0; i < length; i++) {
if (_tokenList.contains(msg.sender, SetValue.wrap(bytes30(bytes20(calls[i].target))))) {
_checkLimit(calls[i].target, calls[i].data);
}
}
}
return "";
}
function _checkLimit(address token, bytes memory innerCalldata) private view {
bytes4 selector;
uint256 spend;
assembly {
selector := mload(add(innerCalldata, 32)) // 0:32 is arr len, 32:36 is selector
spend := mload(add(innerCalldata, 68)) // 36:68 is recipient, 68:100 is spend
}
if (selector == IERC20.transfer.selector || selector == IERC20.approve.selector) {
uint256 limit = _limits[msg.sender][token];
uint256 accountBalance = IERC20(token).balanceOf(msg.sender);
uint256 endBalance;
if (accountBalance >= spend) {
endBalance = accountBalance - spend;
} else {
revert InsufficientBalance(token, accountBalance);
}
if (endBalance < limit) {
revert EndBalanceLessThanLimit(token, limit);
}
}
}
// endregion
/*//////////////////////////////////////////////////////////////
EXECUTION FUNCTIONS
//////////////////////////////////////////////////////////////*/
// region - Execution functions
function updateLimit(address token, uint256 limit) external {
_tokenList.tryAdd(msg.sender, SetValue.wrap(bytes30(bytes20(token))));
_limits[msg.sender][token] = limit;
emit LimitUpdated(token, limit);
}
// endregion
/*//////////////////////////////////////////////////////////////
INSTALLATION
//////////////////////////////////////////////////////////////*/
// region - Installation
function onInstall(bytes calldata data) external override {
(ERC20SpendLimit[] memory spendLimits) = abi.decode(data, (ERC20SpendLimit[]));
uint256 length = spendLimits.length;
for (uint8 i = 0; i < length; i++) {
_tokenList.tryAdd(msg.sender, SetValue.wrap(bytes30(bytes20(spendLimits[i].token))));
_limits[msg.sender][spendLimits[i].token] = spendLimits[i].limit;
}
}
function onUninstall(bytes calldata data) external override {
(address[] memory tokens) = abi.decode(data, (address[]));
uint256 length = tokens.length;
for (uint8 i = 0; i < length; i++) {
delete _limits[msg.sender][tokens[i]];
}
_tokenList.clear(msg.sender);
}
// endregion
/*//////////////////////////////////////////////////////////////
VIEW FUNCTIONS
//////////////////////////////////////////////////////////////*/
// region - View functions
function getCurrentLimit(address account, address token) external view returns (uint256) {
return _limits[account][token];
}
function getTokensForAccount(address account) external view returns (address[] memory tokens) {
SetValue[] memory set = _tokenList.getAll(account);
tokens = new address[](set.length);
for (uint256 i = 0; i < tokens.length; i++) {
tokens[i] = address(bytes20(bytes32(SetValue.unwrap(set[i]))));
}
return tokens;
}
function pluginMetadata() external pure override returns (PluginMetadata memory) {
PluginMetadata memory metadata;
metadata.name = _NAME;
metadata.version = _VERSION;
metadata.author = _AUTHOR;
// Permission strings
string memory modifyTokenLimitsPermission = "Modify ERC20 token limits";
// Permission descriptions
metadata.permissionDescriptors = new SelectorPermission[](1);
metadata.permissionDescriptors[0] = SelectorPermission({
functionSelector: this.updateLimit.selector,
permissionDescription: modifyTokenLimitsPermission
});
return metadata;
}
function supportsInterface(bytes4 interfaceId) public view override returns (bool) {
return interfaceId == type(ITransferLimitPlugin).interfaceId || super.supportsInterface(interfaceId);
}
// endregion
/*//////////////////////////////////////////////////////////////
MANIFEST
//////////////////////////////////////////////////////////////*/
// region - Manifest
function pluginManifest() external pure override returns (PluginManifest memory) {
PluginManifest memory manifest;
// dependency
manifest.dependencyInterfaceIds = new bytes4[](1);
manifest.dependencyInterfaceIds[0] = type(IMultiOwnerPlugin).interfaceId;
// runtime execution functions
manifest.executionFunctions = new bytes4[](3);
manifest.executionFunctions[0] = this.updateLimit.selector;
manifest.executionFunctions[1] = this.getTokensForAccount.selector;
manifest.executionFunctions[2] = this.getCurrentLimit.selector;
// runtime validation functions
ManifestFunction memory runtimeAlwaysAllow = ManifestFunction({
functionType: ManifestAssociatedFunctionType.RUNTIME_VALIDATION_ALWAYS_ALLOW,
functionId: 0,
dependencyIndex: 0
});
manifest.runtimeValidationFunctions = new ManifestAssociatedFunction[](3);
manifest.runtimeValidationFunctions[0] = ManifestAssociatedFunction({
executionSelector: this.updateLimit.selector,
associatedFunction: ManifestFunction({
functionType: ManifestAssociatedFunctionType.DEPENDENCY,
functionId: 0,
dependencyIndex: _MANIFEST_DEPENDENCY_INDEX_MULTI_OWNER_USER_OP_VALIDATION
})
});
manifest.runtimeValidationFunctions[1] = ManifestAssociatedFunction({
executionSelector: this.getTokensForAccount.selector,
associatedFunction: runtimeAlwaysAllow
});
manifest.runtimeValidationFunctions[2] = ManifestAssociatedFunction({
executionSelector: this.getCurrentLimit.selector,
associatedFunction: runtimeAlwaysAllow
});
// pre execution functions
ManifestFunction memory none =
ManifestFunction({functionType: ManifestAssociatedFunctionType.NONE, functionId: 0, dependencyIndex: 0});
// pre execution validation
manifest.executionHooks = new ManifestExecutionHook[](2);
manifest.executionHooks[0] = ManifestExecutionHook({
executionSelector: IStandardExecutor.execute.selector,
preExecHook: ManifestFunction({
functionType: ManifestAssociatedFunctionType.SELF,
functionId: uint8(FunctionId.EXECUTE_FUNCTION),
dependencyIndex: 0
}),
postExecHook: none
});
manifest.executionHooks[1] = ManifestExecutionHook({
executionSelector: IStandardExecutor.executeBatch.selector,
preExecHook: ManifestFunction({
functionType: ManifestAssociatedFunctionType.SELF,
functionId: uint8(FunctionId.EXECUTE_BATCH_FUNCTION),
dependencyIndex: 0
}),
postExecHook: none
});
return manifest;
}
// endregion
}