-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolicyEngine.sol
More file actions
270 lines (224 loc) · 6.7 KB
/
Copy pathPolicyEngine.sol
File metadata and controls
270 lines (224 loc) · 6.7 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
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "@openzeppelin/contracts/access/AccessControl.sol";
import "../interfaces/IPolicyEngine.sol";
import "../libraries/PolicyLib.sol";
/// @title PolicyEngine
/// @notice Enforces spending policies for agent wallets.
/// AgentWallet must call enforce() before every payment.
contract PolicyEngine is IPolicyEngine, AccessControl {
using PolicyLib for PolicyLib.Policy;
using PolicyLib for PolicyLib.SpendRecord;
bytes32 public constant POLICY_ADMIN_ROLE =
keccak256("POLICY_ADMIN_ROLE");
/// @dev agentId => Policy
mapping(bytes32 => PolicyLib.Policy)
private _policies;
/// @dev agentId => SpendRecord
mapping(bytes32 => PolicyLib.SpendRecord)
private _spendRecords;
constructor(address admin) {
_grantRole(DEFAULT_ADMIN_ROLE, admin);
_grantRole(POLICY_ADMIN_ROLE, admin);
}
// ============================================================
// WRITE FUNCTIONS
// ============================================================
/// @notice Create or update an agent policy
function setPolicy(
bytes32 agentId,
PolicyLib.Policy calldata policy
)
external
onlyRole(POLICY_ADMIN_ROLE)
{
// Basic sanity checks
if (
policy.maxPerTransaction >
policy.maxPerHour
) {
revert InvalidPolicy();
}
if (
policy.maxPerHour >
policy.maxPerDay
) {
revert InvalidPolicy();
}
_policies[agentId] = policy;
emit PolicySet(
agentId,
msg.sender
);
}
/// @notice Disable a policy
function revokePolicy(
bytes32 agentId
)
external
onlyRole(POLICY_ADMIN_ROLE)
{
PolicyLib.Policy storage p =
_policies[agentId];
if (
p.maxPerTransaction == 0 &&
!p.active
) {
revert PolicyNotFound(agentId);
}
p.active = false;
emit PolicyRevoked(agentId);
}
/// @notice Enforce policy before payment.
/// Reverts if payment violates rules.
function enforce(
bytes32 agentId,
uint128 amount,
address payee
)
external
returns (bool requiresApproval)
{
PolicyLib.Policy storage p =
_policies[agentId];
PolicyLib.SpendRecord storage r =
_spendRecords[agentId];
// --------------------------------------------------------
// 1. Policy must exist
// --------------------------------------------------------
if (
p.maxPerTransaction == 0 &&
!p.active
) {
revert PolicyNotFound(agentId);
}
// --------------------------------------------------------
// 2. Policy must be active
// --------------------------------------------------------
if (!p.active) {
revert PolicyInactive(agentId);
}
// --------------------------------------------------------
// 3. Policy must not be expired
// --------------------------------------------------------
if (p.isExpired()) {
revert PolicyExpired(agentId);
}
// --------------------------------------------------------
// 4. Payee must be allowed
// --------------------------------------------------------
if (
!p.isPayeeAllowed(payee)
) {
revert PayeeNotAllowed(
agentId,
payee
);
}
// --------------------------------------------------------
// 5. Per transaction limit
// --------------------------------------------------------
if (
amount >
p.maxPerTransaction
) {
revert ExceedsPerTxLimit(
agentId,
amount,
p.maxPerTransaction
);
}
// --------------------------------------------------------
// 6. Reset spend windows if needed
// --------------------------------------------------------
if (r.isNewHour()) {
r.hourlyAmount = 0;
r.hourBucket =
PolicyLib.currentHourBucket();
}
if (r.isNewDay()) {
r.dailyAmount = 0;
r.dayBucket =
PolicyLib.currentDayBucket();
}
// --------------------------------------------------------
// 7. Hourly limit
// --------------------------------------------------------
uint128 newHourly =
r.hourlyAmount + amount;
if (
newHourly >
p.maxPerHour
) {
revert ExceedsHourlyLimit(
agentId,
newHourly,
p.maxPerHour
);
}
// --------------------------------------------------------
// 8. Daily limit
// --------------------------------------------------------
uint128 newDaily =
r.dailyAmount + amount;
if (
newDaily >
p.maxPerDay
) {
revert ExceedsDailyLimit(
agentId,
newDaily,
p.maxPerDay
);
}
// --------------------------------------------------------
// 9. Commit spend
// --------------------------------------------------------
r.hourlyAmount = newHourly;
r.dailyAmount = newDaily;
// --------------------------------------------------------
// 10. Approval threshold
// --------------------------------------------------------
requiresApproval =
amount >=
p.requireApprovalAbove;
if (requiresApproval) {
emit ApprovalRequired(
agentId,
amount,
payee
);
} else {
emit PaymentEnforced(
agentId,
amount,
payee
);
}
}
// ============================================================
// READ FUNCTIONS
// ============================================================
function getPolicy(
bytes32 agentId
)
external
view
returns (
PolicyLib.Policy memory
)
{
return _policies[agentId];
}
function getSpendRecord(
bytes32 agentId
)
external
view
returns (
PolicyLib.SpendRecord memory
)
{
return _spendRecords[agentId];
}
}