@bananapus/core-v6 0.0.18 → 0.0.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/ADMINISTRATION.md +3 -0
  2. package/ARCHITECTURE.md +24 -0
  3. package/AUDIT_INSTRUCTIONS.md +4 -2
  4. package/CHANGE_LOG.md +29 -1
  5. package/README.md +12 -2
  6. package/RISKS.md +10 -2
  7. package/SKILLS.md +9 -0
  8. package/USER_JOURNEYS.md +6 -0
  9. package/foundry.toml +1 -0
  10. package/package.json +1 -1
  11. package/src/JBController.sol +52 -5
  12. package/src/JBMultiTerminal.sol +197 -179
  13. package/src/JBTerminalStore.sol +367 -171
  14. package/src/interfaces/IJBCashOutTerminal.sol +30 -0
  15. package/src/interfaces/IJBController.sol +15 -0
  16. package/src/interfaces/IJBTerminal.sol +28 -0
  17. package/src/interfaces/IJBTerminalStore.sol +66 -0
  18. package/src/libraries/JBPayoutSplitGroupLib.sol +157 -0
  19. package/src/structs/JBCashOutHookSpecification.sol +2 -0
  20. package/src/structs/JBPayHookSpecification.sol +2 -0
  21. package/test/CoreExploitTests.t.sol +21 -10
  22. package/test/TestCashOutHooks.sol +6 -4
  23. package/test/TestDataHookFuzzing.sol +6 -2
  24. package/test/TestPayHooks.sol +1 -1
  25. package/test/TestRulesetQueueing.sol +4 -5
  26. package/test/TestRulesetQueuingStress.sol +5 -3
  27. package/test/TestTerminalPreviewParity.sol +208 -0
  28. package/test/fork/TestSequencerPriceFeedFork.sol +168 -0
  29. package/test/fork/TestTerminalPreviewParityFork.sol +109 -0
  30. package/test/units/static/JBController/TestPreviewMintOf.sol +116 -0
  31. package/test/units/static/JBMultiTerminal/TestCashOutTokensOf.sol +144 -25
  32. package/test/units/static/JBMultiTerminal/TestExecutePayout.sol +11 -1
  33. package/test/units/static/JBMultiTerminal/TestExecuteProcessFee.sol +15 -2
  34. package/test/units/static/JBMultiTerminal/TestPay.sol +64 -2
  35. package/test/units/static/JBMultiTerminal/TestPreviewCashOutFrom.sol +116 -0
  36. package/test/units/static/JBMultiTerminal/TestPreviewPayFor.sol +98 -0
  37. package/test/units/static/JBMultiTerminal/TestProcessHeldFeesOf.sol +11 -2
  38. package/test/units/static/JBRulesets/TestCurrentOf.sol +8 -6
  39. package/test/units/static/JBRulesets/TestRulesets.sol +25 -24
  40. package/test/units/static/JBRulesets/TestUpcomingRulesetOf.sol +4 -17
  41. package/test/units/static/JBSurplus/TestSurplusFuzz.sol +49 -2
  42. package/test/units/static/JBTerminalStore/TestCurrentReclaimableSurplusOf.sol +215 -0
  43. package/test/units/static/JBTerminalStore/TestPreviewCashOutFrom.sol +475 -0
  44. package/test/units/static/JBTerminalStore/TestPreviewPayFrom.sol +464 -0
  45. package/test/units/static/JBTerminalStore/TestRecordCashOutsFor.sol +113 -2
  46. package/test/units/static/JBTerminalStore/TestRecordPaymentFrom.sol +227 -5
@@ -0,0 +1,464 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity 0.8.26;
3
+
4
+ import {JBTerminalStore} from "../../../../src/JBTerminalStore.sol";
5
+ import {IJBPayHook} from "../../../../src/interfaces/IJBPayHook.sol";
6
+ import {IJBPrices} from "../../../../src/interfaces/IJBPrices.sol";
7
+ import {IJBRulesetApprovalHook} from "../../../../src/interfaces/IJBRulesetApprovalHook.sol";
8
+ import {IJBRulesetDataHook} from "../../../../src/interfaces/IJBRulesetDataHook.sol";
9
+ import {IJBRulesets} from "../../../../src/interfaces/IJBRulesets.sol";
10
+ import {IJBToken} from "../../../../src/interfaces/IJBToken.sol";
11
+ import {JBConstants} from "../../../../src/libraries/JBConstants.sol";
12
+ import {JBRulesetMetadataResolver} from "../../../../src/libraries/JBRulesetMetadataResolver.sol";
13
+ import {JBBeforePayRecordedContext} from "../../../../src/structs/JBBeforePayRecordedContext.sol";
14
+ import {JBPayHookSpecification} from "../../../../src/structs/JBPayHookSpecification.sol";
15
+ import {JBRuleset} from "../../../../src/structs/JBRuleset.sol";
16
+ import {JBRulesetMetadata} from "../../../../src/structs/JBRulesetMetadata.sol";
17
+ import {JBTokenAmount} from "../../../../src/structs/JBTokenAmount.sol";
18
+ import {mulDiv} from "@prb/math/src/Common.sol";
19
+ import {JBTerminalStoreSetup} from "./JBTerminalStoreSetup.sol";
20
+
21
+ contract TestPreviewPayFrom_Local is JBTerminalStoreSetup {
22
+ using JBRulesetMetadataResolver for JBRuleset;
23
+
24
+ uint64 _projectId = 1;
25
+ uint256 _defaultValue = 1e18;
26
+ uint8 _defaultDecimals = 18;
27
+
28
+ // Mocks
29
+ IJBToken _token = IJBToken(makeAddr("token"));
30
+ IJBRulesetDataHook _dataHook = IJBRulesetDataHook(makeAddr("dataHook"));
31
+ IJBPayHook _payHook = IJBPayHook(makeAddr("payHook"));
32
+ address _terminal = makeAddr("terminal");
33
+
34
+ uint32 _currency = uint32(uint160(address(_token)));
35
+ uint32 _nativeCurrency = uint32(uint160(JBConstants.NATIVE_TOKEN));
36
+
37
+ function setUp() public {
38
+ super.terminalStoreSetup();
39
+ }
40
+
41
+ function test_WhenCurrentRulesetCycleNumberIsZero() external {
42
+ // it will revert INVALID_RULESET
43
+
44
+ JBTokenAmount memory _tokenAmount = JBTokenAmount({
45
+ token: address(_token), value: _defaultValue, decimals: _defaultDecimals, currency: _currency
46
+ });
47
+
48
+ JBRuleset memory _returnedRuleset = JBRuleset({
49
+ cycleNumber: 0,
50
+ id: uint48(block.timestamp),
51
+ basedOnId: 0,
52
+ start: uint48(block.timestamp),
53
+ duration: uint32(block.timestamp + 1000),
54
+ weight: 1e18,
55
+ weightCutPercent: 0,
56
+ approvalHook: IJBRulesetApprovalHook(address(0)),
57
+ metadata: 0
58
+ });
59
+
60
+ mockExpect(address(rulesets), abi.encodeCall(IJBRulesets.currentOf, (_projectId)), abi.encode(_returnedRuleset));
61
+
62
+ vm.expectRevert(abi.encodeWithSelector(JBTerminalStore.JBTerminalStore_RulesetNotFound.selector, _projectId));
63
+ vm.prank(_terminal);
64
+ _store.previewPayFrom({
65
+ payer: address(this), amount: _tokenAmount, projectId: _projectId, beneficiary: address(this), metadata: ""
66
+ });
67
+ }
68
+
69
+ function test_WhenCurrentRulesetPausePayEqTrue() external {
70
+ // it will revert RULESET_PAYMENT_PAUSED
71
+
72
+ JBTokenAmount memory _tokenAmount = JBTokenAmount({
73
+ token: address(_token), value: _defaultValue, decimals: _defaultDecimals, currency: _currency
74
+ });
75
+
76
+ JBRulesetMetadata memory _metadata = JBRulesetMetadata({
77
+ reservedPercent: 0,
78
+ cashOutTaxRate: JBConstants.MAX_CASH_OUT_TAX_RATE,
79
+ baseCurrency: uint32(uint160(JBConstants.NATIVE_TOKEN)),
80
+ pausePay: true,
81
+ pauseCreditTransfers: false,
82
+ allowOwnerMinting: false,
83
+ allowSetCustomToken: false,
84
+ allowTerminalMigration: false,
85
+ allowSetTerminals: false,
86
+ ownerMustSendPayouts: false,
87
+ allowSetController: false,
88
+ allowAddAccountingContext: true,
89
+ allowAddPriceFeed: false,
90
+ holdFees: false,
91
+ useTotalSurplusForCashOuts: false,
92
+ useDataHookForPay: false,
93
+ useDataHookForCashOut: false,
94
+ dataHook: address(0),
95
+ metadata: 0
96
+ });
97
+
98
+ uint256 _packedMetadata = JBRulesetMetadataResolver.packRulesetMetadata(_metadata);
99
+
100
+ JBRuleset memory _returnedRuleset = JBRuleset({
101
+ cycleNumber: uint48(block.timestamp),
102
+ id: uint48(block.timestamp),
103
+ basedOnId: 0,
104
+ start: uint48(block.timestamp),
105
+ duration: uint32(block.timestamp + 1000),
106
+ weight: 1e18,
107
+ weightCutPercent: 0,
108
+ approvalHook: IJBRulesetApprovalHook(address(0)),
109
+ metadata: _packedMetadata
110
+ });
111
+
112
+ mockExpect(address(rulesets), abi.encodeCall(IJBRulesets.currentOf, (_projectId)), abi.encode(_returnedRuleset));
113
+
114
+ vm.expectRevert(JBTerminalStore.JBTerminalStore_RulesetPaymentPaused.selector);
115
+ vm.prank(_terminal);
116
+ _store.previewPayFrom({
117
+ payer: address(this), amount: _tokenAmount, projectId: _projectId, beneficiary: address(this), metadata: ""
118
+ });
119
+ }
120
+
121
+ function test_MatchesRecordPaymentFrom_NoDataHook() external {
122
+ // it returns the same tokenCount as recordPaymentFrom for a simple payment without a data hook
123
+
124
+ JBTokenAmount memory _tokenAmount = JBTokenAmount({
125
+ token: address(_token), value: _defaultValue, decimals: _defaultDecimals, currency: _currency
126
+ });
127
+
128
+ JBRulesetMetadata memory _metadata = JBRulesetMetadata({
129
+ reservedPercent: 0,
130
+ cashOutTaxRate: JBConstants.MAX_CASH_OUT_TAX_RATE,
131
+ baseCurrency: uint32(uint160(address(_token))),
132
+ pausePay: false,
133
+ pauseCreditTransfers: false,
134
+ allowOwnerMinting: false,
135
+ allowSetCustomToken: false,
136
+ allowTerminalMigration: false,
137
+ allowSetTerminals: false,
138
+ ownerMustSendPayouts: false,
139
+ allowSetController: false,
140
+ allowAddAccountingContext: true,
141
+ allowAddPriceFeed: false,
142
+ holdFees: false,
143
+ useTotalSurplusForCashOuts: false,
144
+ useDataHookForPay: false,
145
+ useDataHookForCashOut: false,
146
+ dataHook: address(0),
147
+ metadata: 0
148
+ });
149
+
150
+ uint256 _packedMetadata = JBRulesetMetadataResolver.packRulesetMetadata(_metadata);
151
+
152
+ JBRuleset memory _returnedRuleset = JBRuleset({
153
+ cycleNumber: uint48(block.timestamp),
154
+ id: uint48(block.timestamp),
155
+ basedOnId: 0,
156
+ start: uint48(block.timestamp),
157
+ duration: uint32(block.timestamp + 1000),
158
+ weight: 1e18,
159
+ weightCutPercent: 0,
160
+ approvalHook: IJBRulesetApprovalHook(address(0)),
161
+ metadata: _packedMetadata
162
+ });
163
+
164
+ // Mock for preview call
165
+ mockExpect(address(rulesets), abi.encodeCall(IJBRulesets.currentOf, (_projectId)), abi.encode(_returnedRuleset));
166
+
167
+ (, uint256 previewTokenCount, JBPayHookSpecification[] memory previewSpecs) = _store.previewPayFrom({
168
+ payer: address(this), amount: _tokenAmount, projectId: _projectId, beneficiary: address(this), metadata: ""
169
+ });
170
+
171
+ // Mock for record call
172
+ mockExpect(address(rulesets), abi.encodeCall(IJBRulesets.currentOf, (_projectId)), abi.encode(_returnedRuleset));
173
+
174
+ (, uint256 recordTokenCount, JBPayHookSpecification[] memory recordSpecs) = _store.recordPaymentFrom({
175
+ payer: address(this), amount: _tokenAmount, projectId: _projectId, beneficiary: address(this), metadata: ""
176
+ });
177
+
178
+ assertEq(previewTokenCount, recordTokenCount);
179
+ assertEq(previewSpecs.length, recordSpecs.length);
180
+ }
181
+
182
+ function test_MatchesRecordPaymentFrom_WithDataHook() external {
183
+ // it returns the same tokenCount and hook specs as recordPaymentFrom with a data hook
184
+
185
+ JBTokenAmount memory _tokenAmount = JBTokenAmount({
186
+ token: address(_token), value: _defaultValue, decimals: _defaultDecimals, currency: _currency
187
+ });
188
+
189
+ JBRulesetMetadata memory _metadata = JBRulesetMetadata({
190
+ reservedPercent: 0,
191
+ cashOutTaxRate: JBConstants.MAX_CASH_OUT_TAX_RATE,
192
+ baseCurrency: uint32(uint160(address(_token))),
193
+ pausePay: false,
194
+ pauseCreditTransfers: false,
195
+ allowOwnerMinting: false,
196
+ allowSetCustomToken: false,
197
+ allowTerminalMigration: false,
198
+ allowSetTerminals: false,
199
+ ownerMustSendPayouts: false,
200
+ allowSetController: false,
201
+ allowAddAccountingContext: true,
202
+ allowAddPriceFeed: false,
203
+ holdFees: false,
204
+ useTotalSurplusForCashOuts: false,
205
+ useDataHookForPay: true,
206
+ useDataHookForCashOut: false,
207
+ dataHook: address(_dataHook),
208
+ metadata: 0
209
+ });
210
+
211
+ uint256 _packedMetadata = JBRulesetMetadataResolver.packRulesetMetadata(_metadata);
212
+
213
+ JBRuleset memory _returnedRuleset = JBRuleset({
214
+ cycleNumber: uint48(block.timestamp),
215
+ id: uint48(block.timestamp),
216
+ basedOnId: 0,
217
+ start: uint48(block.timestamp),
218
+ duration: uint32(block.timestamp + 1000),
219
+ weight: 1e18,
220
+ weightCutPercent: 0,
221
+ approvalHook: IJBRulesetApprovalHook(address(0)),
222
+ metadata: _packedMetadata
223
+ });
224
+
225
+ JBPayHookSpecification[] memory _spec = new JBPayHookSpecification[](1);
226
+ _spec[0] = JBPayHookSpecification({hook: _payHook, noop: false, amount: _defaultValue / 2, metadata: ""});
227
+
228
+ // The data hook context will use the terminal address passed to preview / msg.sender for record.
229
+ // Since we call both from address(this), they match.
230
+ JBBeforePayRecordedContext memory _context = JBBeforePayRecordedContext({
231
+ terminal: address(this),
232
+ payer: address(this),
233
+ amount: _tokenAmount,
234
+ projectId: _projectId,
235
+ rulesetId: uint48(block.timestamp),
236
+ beneficiary: address(this),
237
+ weight: _returnedRuleset.weight,
238
+ reservedPercent: _returnedRuleset.reservedPercent(),
239
+ metadata: ""
240
+ });
241
+
242
+ // Mock for preview call
243
+ mockExpect(address(rulesets), abi.encodeCall(IJBRulesets.currentOf, (_projectId)), abi.encode(_returnedRuleset));
244
+ mockExpect(
245
+ address(_dataHook),
246
+ abi.encodeCall(IJBRulesetDataHook.beforePayRecordedWith, (_context)),
247
+ abi.encode(1e18 / 2, _spec)
248
+ );
249
+
250
+ (, uint256 previewTokenCount, JBPayHookSpecification[] memory previewSpecs) = _store.previewPayFrom({
251
+ payer: address(this), amount: _tokenAmount, projectId: _projectId, beneficiary: address(this), metadata: ""
252
+ });
253
+
254
+ // Mock for record call
255
+ mockExpect(address(rulesets), abi.encodeCall(IJBRulesets.currentOf, (_projectId)), abi.encode(_returnedRuleset));
256
+ mockExpect(
257
+ address(_dataHook),
258
+ abi.encodeCall(IJBRulesetDataHook.beforePayRecordedWith, (_context)),
259
+ abi.encode(1e18 / 2, _spec)
260
+ );
261
+
262
+ (, uint256 recordTokenCount, JBPayHookSpecification[] memory recordSpecs) = _store.recordPaymentFrom({
263
+ payer: address(this), amount: _tokenAmount, projectId: _projectId, beneficiary: address(this), metadata: ""
264
+ });
265
+
266
+ assertEq(previewTokenCount, recordTokenCount);
267
+ assertEq(previewSpecs.length, recordSpecs.length);
268
+ assertEq(previewSpecs[0].amount, recordSpecs[0].amount);
269
+ }
270
+
271
+ function test_DoesNotModifyState() external {
272
+ // it does not change balanceOf
273
+
274
+ JBTokenAmount memory _tokenAmount = JBTokenAmount({
275
+ token: address(_token), value: _defaultValue, decimals: _defaultDecimals, currency: _currency
276
+ });
277
+
278
+ JBRulesetMetadata memory _metadata = JBRulesetMetadata({
279
+ reservedPercent: 0,
280
+ cashOutTaxRate: JBConstants.MAX_CASH_OUT_TAX_RATE,
281
+ baseCurrency: uint32(uint160(address(_token))),
282
+ pausePay: false,
283
+ pauseCreditTransfers: false,
284
+ allowOwnerMinting: false,
285
+ allowSetCustomToken: false,
286
+ allowTerminalMigration: false,
287
+ allowSetTerminals: false,
288
+ ownerMustSendPayouts: false,
289
+ allowSetController: false,
290
+ allowAddAccountingContext: true,
291
+ allowAddPriceFeed: false,
292
+ holdFees: false,
293
+ useTotalSurplusForCashOuts: false,
294
+ useDataHookForPay: false,
295
+ useDataHookForCashOut: false,
296
+ dataHook: address(0),
297
+ metadata: 0
298
+ });
299
+
300
+ uint256 _packedMetadata = JBRulesetMetadataResolver.packRulesetMetadata(_metadata);
301
+
302
+ JBRuleset memory _returnedRuleset = JBRuleset({
303
+ cycleNumber: uint48(block.timestamp),
304
+ id: uint48(block.timestamp),
305
+ basedOnId: 0,
306
+ start: uint48(block.timestamp),
307
+ duration: uint32(block.timestamp + 1000),
308
+ weight: 1e18,
309
+ weightCutPercent: 0,
310
+ approvalHook: IJBRulesetApprovalHook(address(0)),
311
+ metadata: _packedMetadata
312
+ });
313
+
314
+ mockExpect(address(rulesets), abi.encodeCall(IJBRulesets.currentOf, (_projectId)), abi.encode(_returnedRuleset));
315
+
316
+ uint256 balanceBefore = _store.balanceOf(_terminal, _projectId, address(_token));
317
+
318
+ vm.prank(_terminal);
319
+ _store.previewPayFrom({
320
+ payer: address(this), amount: _tokenAmount, projectId: _projectId, beneficiary: address(this), metadata: ""
321
+ });
322
+
323
+ uint256 balanceAfter = _store.balanceOf(_terminal, _projectId, address(_token));
324
+
325
+ assertEq(balanceBefore, balanceAfter);
326
+ }
327
+
328
+ function test_WithDifferentBaseCurrency() external {
329
+ // it returns the correct tokenCount when baseCurrency differs from payment currency
330
+
331
+ JBTokenAmount memory _tokenAmount = JBTokenAmount({
332
+ token: address(_token), value: _defaultValue, decimals: _defaultDecimals, currency: _currency
333
+ });
334
+
335
+ JBRulesetMetadata memory _metadata = JBRulesetMetadata({
336
+ reservedPercent: 0,
337
+ cashOutTaxRate: JBConstants.MAX_CASH_OUT_TAX_RATE,
338
+ baseCurrency: _nativeCurrency,
339
+ pausePay: false,
340
+ pauseCreditTransfers: false,
341
+ allowOwnerMinting: false,
342
+ allowSetCustomToken: false,
343
+ allowTerminalMigration: false,
344
+ allowSetTerminals: false,
345
+ ownerMustSendPayouts: false,
346
+ allowSetController: false,
347
+ allowAddAccountingContext: true,
348
+ allowAddPriceFeed: false,
349
+ holdFees: false,
350
+ useTotalSurplusForCashOuts: false,
351
+ useDataHookForPay: false,
352
+ useDataHookForCashOut: false,
353
+ dataHook: address(0),
354
+ metadata: 0
355
+ });
356
+
357
+ uint256 _packedMetadata = JBRulesetMetadataResolver.packRulesetMetadata(_metadata);
358
+
359
+ JBRuleset memory _returnedRuleset = JBRuleset({
360
+ cycleNumber: uint48(block.timestamp),
361
+ id: uint48(block.timestamp),
362
+ basedOnId: 0,
363
+ start: uint48(block.timestamp),
364
+ duration: uint32(block.timestamp + 1000),
365
+ weight: 1e18,
366
+ weightCutPercent: 0,
367
+ approvalHook: IJBRulesetApprovalHook(address(0)),
368
+ metadata: _packedMetadata
369
+ });
370
+
371
+ mockExpect(address(rulesets), abi.encodeCall(IJBRulesets.currentOf, (_projectId)), abi.encode(_returnedRuleset));
372
+
373
+ // mock call to JBPrices pricePerUnitOf
374
+ mockExpect(
375
+ address(prices),
376
+ abi.encodeCall(IJBPrices.pricePerUnitOf, (_projectId, _currency, _nativeCurrency, 18)),
377
+ abi.encode(2e18)
378
+ );
379
+
380
+ uint256 expectedCount = mulDiv(_defaultValue, 1e18, 2e18);
381
+
382
+ vm.prank(_terminal);
383
+ (, uint256 tokenCount,) = _store.previewPayFrom({
384
+ payer: address(this), amount: _tokenAmount, projectId: _projectId, beneficiary: address(this), metadata: ""
385
+ });
386
+
387
+ assertEq(tokenCount, expectedCount);
388
+ }
389
+
390
+ function test_WithDataHookAndZeroAmountNoopSpec() external {
391
+ JBTokenAmount memory _tokenAmount = JBTokenAmount({
392
+ token: address(_token), value: _defaultValue, decimals: _defaultDecimals, currency: _currency
393
+ });
394
+
395
+ JBRulesetMetadata memory _metadata = JBRulesetMetadata({
396
+ reservedPercent: 0,
397
+ cashOutTaxRate: JBConstants.MAX_CASH_OUT_TAX_RATE,
398
+ baseCurrency: uint32(uint160(address(_token))),
399
+ pausePay: false,
400
+ pauseCreditTransfers: false,
401
+ allowOwnerMinting: false,
402
+ allowSetCustomToken: false,
403
+ allowTerminalMigration: false,
404
+ allowSetTerminals: false,
405
+ ownerMustSendPayouts: false,
406
+ allowSetController: false,
407
+ allowAddAccountingContext: true,
408
+ allowAddPriceFeed: false,
409
+ holdFees: false,
410
+ useTotalSurplusForCashOuts: false,
411
+ useDataHookForPay: true,
412
+ useDataHookForCashOut: false,
413
+ dataHook: address(_dataHook),
414
+ metadata: 0
415
+ });
416
+
417
+ uint256 _packedMetadata = JBRulesetMetadataResolver.packRulesetMetadata(_metadata);
418
+
419
+ JBRuleset memory _returnedRuleset = JBRuleset({
420
+ cycleNumber: uint48(block.timestamp),
421
+ id: uint48(block.timestamp),
422
+ basedOnId: 0,
423
+ start: uint48(block.timestamp),
424
+ duration: uint32(block.timestamp + 1000),
425
+ weight: 1e18,
426
+ weightCutPercent: 0,
427
+ approvalHook: IJBRulesetApprovalHook(address(0)),
428
+ metadata: _packedMetadata
429
+ });
430
+
431
+ JBPayHookSpecification[] memory _spec = new JBPayHookSpecification[](1);
432
+ _spec[0] = JBPayHookSpecification({hook: _payHook, noop: true, amount: 0, metadata: "info"});
433
+
434
+ JBBeforePayRecordedContext memory _context = JBBeforePayRecordedContext({
435
+ terminal: _terminal,
436
+ payer: address(this),
437
+ amount: _tokenAmount,
438
+ projectId: _projectId,
439
+ rulesetId: uint48(block.timestamp),
440
+ beneficiary: address(this),
441
+ weight: _returnedRuleset.weight,
442
+ reservedPercent: _returnedRuleset.reservedPercent(),
443
+ metadata: ""
444
+ });
445
+
446
+ mockExpect(address(rulesets), abi.encodeCall(IJBRulesets.currentOf, (_projectId)), abi.encode(_returnedRuleset));
447
+ mockExpect(
448
+ address(_dataHook),
449
+ abi.encodeCall(IJBRulesetDataHook.beforePayRecordedWith, (_context)),
450
+ abi.encode(1e18 / 2, _spec)
451
+ );
452
+
453
+ vm.prank(_terminal);
454
+ (, uint256 tokenCount, JBPayHookSpecification[] memory hookSpecifications) = _store.previewPayFrom({
455
+ payer: address(this), amount: _tokenAmount, projectId: _projectId, beneficiary: address(this), metadata: ""
456
+ });
457
+
458
+ assertEq(tokenCount, 1e18 / 2);
459
+ assertEq(hookSpecifications.length, 1);
460
+ assertEq(hookSpecifications[0].amount, 0);
461
+ assertEq(hookSpecifications[0].noop, true);
462
+ assertEq(hookSpecifications[0].metadata, bytes("info"));
463
+ }
464
+ }
@@ -428,7 +428,7 @@ contract TestRecordCashOutsFor_Local is JBTerminalStoreSetup {
428
428
 
429
429
  // return data
430
430
  JBCashOutHookSpecification[] memory _spec = new JBCashOutHookSpecification[](1);
431
- _spec[0] = JBCashOutHookSpecification({hook: _cashOutHook, amount: 0, metadata: ""});
431
+ _spec[0] = JBCashOutHookSpecification({hook: _cashOutHook, noop: false, amount: 0, metadata: ""});
432
432
 
433
433
  // mock call to data hook beforeCashOutRecordedWith
434
434
  mockExpect(
@@ -541,7 +541,7 @@ contract TestRecordCashOutsFor_Local is JBTerminalStoreSetup {
541
541
 
542
542
  // return data
543
543
  JBCashOutHookSpecification[] memory _spec = new JBCashOutHookSpecification[](1);
544
- _spec[0] = JBCashOutHookSpecification({hook: _cashOutHook, amount: 0, metadata: ""});
544
+ _spec[0] = JBCashOutHookSpecification({hook: _cashOutHook, noop: false, amount: 0, metadata: ""});
545
545
 
546
546
  // The mock will only match if the context has beneficiaryIsFeeless=true.
547
547
  mockExpect(
@@ -563,6 +563,117 @@ contract TestRecordCashOutsFor_Local is JBTerminalStoreSetup {
563
563
  assertEq(expectedCashOuts, reclaimed);
564
564
  }
565
565
 
566
+ function test_GivenTheHookReturnsANoopSpecWithAmount()
567
+ external
568
+ whenCurrentRulesetUseTotalSurplusForCashOutsEqTrueWithHook
569
+ {
570
+ mockExpect(
571
+ address(_controller),
572
+ abi.encodeCall(IJBController.totalTokenSupplyWithReservedTokensOf, (_projectId)),
573
+ abi.encode(_totalSupply)
574
+ );
575
+
576
+ JBAccountingContext memory _accountingContexts =
577
+ JBAccountingContext({token: address(_token), decimals: 18, currency: _currency});
578
+ JBAccountingContext[] memory _balanceContexts = new JBAccountingContext[](1);
579
+ _balanceContexts[0] = JBAccountingContext({token: address(_token), decimals: 18, currency: _currency});
580
+
581
+ JBBeforeCashOutRecordedContext memory _context = JBBeforeCashOutRecordedContext({
582
+ terminal: address(this),
583
+ holder: address(this),
584
+ projectId: _projectId,
585
+ rulesetId: uint48(block.timestamp),
586
+ cashOutCount: 1e18,
587
+ totalSupply: _totalSupply,
588
+ surplus: JBTokenAmount({
589
+ token: _accountingContexts.token,
590
+ value: 3e18,
591
+ decimals: _accountingContexts.decimals,
592
+ currency: _accountingContexts.currency
593
+ }),
594
+ useTotalSurplus: true,
595
+ cashOutTaxRate: 0,
596
+ beneficiaryIsFeeless: false,
597
+ metadata: ""
598
+ });
599
+
600
+ JBCashOutHookSpecification[] memory _spec = new JBCashOutHookSpecification[](1);
601
+ _spec[0] = JBCashOutHookSpecification({hook: _cashOutHook, noop: true, amount: 1, metadata: ""});
602
+
603
+ mockExpect(
604
+ address(_dataHook),
605
+ abi.encodeCall(IJBRulesetDataHook.beforeCashOutRecordedWith, (_context)),
606
+ abi.encode(0, 1e18, _totalSupply, _spec)
607
+ );
608
+
609
+ vm.expectRevert(abi.encodeWithSelector(JBTerminalStore.JBTerminalStore_NoopHookSpecHasAmount.selector, 1));
610
+ _store.recordCashOutFor({
611
+ holder: address(this),
612
+ projectId: _projectId,
613
+ cashOutCount: 1e18,
614
+ accountingContext: _accountingContexts,
615
+ balanceAccountingContexts: _balanceContexts,
616
+ beneficiaryIsFeeless: false,
617
+ metadata: ""
618
+ });
619
+ }
620
+
621
+ function test_GivenALaterHookReturnsANoopSpecWithAmount()
622
+ external
623
+ whenCurrentRulesetUseTotalSurplusForCashOutsEqTrueWithHook
624
+ {
625
+ mockExpect(
626
+ address(_controller),
627
+ abi.encodeCall(IJBController.totalTokenSupplyWithReservedTokensOf, (_projectId)),
628
+ abi.encode(_totalSupply)
629
+ );
630
+
631
+ JBAccountingContext memory _accountingContexts =
632
+ JBAccountingContext({token: address(_token), decimals: 18, currency: _currency});
633
+ JBAccountingContext[] memory _balanceContexts = new JBAccountingContext[](1);
634
+ _balanceContexts[0] = JBAccountingContext({token: address(_token), decimals: 18, currency: _currency});
635
+
636
+ JBBeforeCashOutRecordedContext memory _context = JBBeforeCashOutRecordedContext({
637
+ terminal: address(this),
638
+ holder: address(this),
639
+ projectId: _projectId,
640
+ rulesetId: uint48(block.timestamp),
641
+ cashOutCount: 1e18,
642
+ totalSupply: _totalSupply,
643
+ surplus: JBTokenAmount({
644
+ token: _accountingContexts.token,
645
+ value: 3e18,
646
+ decimals: _accountingContexts.decimals,
647
+ currency: _accountingContexts.currency
648
+ }),
649
+ useTotalSurplus: true,
650
+ cashOutTaxRate: 0,
651
+ beneficiaryIsFeeless: false,
652
+ metadata: ""
653
+ });
654
+
655
+ JBCashOutHookSpecification[] memory _spec = new JBCashOutHookSpecification[](2);
656
+ _spec[0] = JBCashOutHookSpecification({hook: _cashOutHook, noop: false, amount: 0, metadata: ""});
657
+ _spec[1] = JBCashOutHookSpecification({hook: _cashOutHook, noop: true, amount: 1, metadata: ""});
658
+
659
+ mockExpect(
660
+ address(_dataHook),
661
+ abi.encodeCall(IJBRulesetDataHook.beforeCashOutRecordedWith, (_context)),
662
+ abi.encode(0, 1e18, _totalSupply, _spec)
663
+ );
664
+
665
+ vm.expectRevert(abi.encodeWithSelector(JBTerminalStore.JBTerminalStore_NoopHookSpecHasAmount.selector, 1));
666
+ _store.recordCashOutFor({
667
+ holder: address(this),
668
+ projectId: _projectId,
669
+ cashOutCount: 1e18,
670
+ accountingContext: _accountingContexts,
671
+ balanceAccountingContexts: _balanceContexts,
672
+ beneficiaryIsFeeless: false,
673
+ metadata: ""
674
+ });
675
+ }
676
+
566
677
  // Probably unnecessary even though it may give us a bit of cov %.. skipping for now
567
678
  /* function test_WhenTheCurrentRulesetUseTotalSurplusForCashOutsEqFalse() external {
568
679
  // it will use the standard surplus calculation