@bananapus/core-v6 0.0.17 → 0.0.19

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 (107) hide show
  1. package/CHANGE_LOG.md +53 -13
  2. package/SKILLS.md +6 -0
  3. package/USER_JOURNEYS.md +4 -0
  4. package/package.json +1 -1
  5. package/src/JBTerminalStore.sol +357 -171
  6. package/src/interfaces/IJBTerminalStore.sol +70 -0
  7. package/test/fork/TestSequencerPriceFeedFork.sol +168 -0
  8. package/test/units/static/JBTerminalStore/TestCurrentReclaimableSurplusOf.sol +215 -0
  9. package/test/units/static/JBTerminalStore/TestPreviewCashOutFrom.sol +383 -0
  10. package/test/units/static/JBTerminalStore/TestPreviewPayFrom.sol +415 -0
  11. package/docs/book.css +0 -13
  12. package/docs/book.toml +0 -12
  13. package/docs/solidity.min.js +0 -74
  14. package/docs/src/README.md +0 -703
  15. package/docs/src/SUMMARY.md +0 -94
  16. package/docs/src/src/JBChainlinkV3PriceFeed.sol/contract.JBChainlinkV3PriceFeed.md +0 -83
  17. package/docs/src/src/JBChainlinkV3SequencerPriceFeed.sol/contract.JBChainlinkV3SequencerPriceFeed.md +0 -88
  18. package/docs/src/src/JBController.sol/contract.JBController.md +0 -1121
  19. package/docs/src/src/JBDeadline.sol/contract.JBDeadline.md +0 -84
  20. package/docs/src/src/JBDirectory.sol/contract.JBDirectory.md +0 -294
  21. package/docs/src/src/JBERC20.sol/contract.JBERC20.md +0 -190
  22. package/docs/src/src/JBFeelessAddresses.sol/contract.JBFeelessAddresses.md +0 -80
  23. package/docs/src/src/JBFundAccessLimits.sol/contract.JBFundAccessLimits.md +0 -253
  24. package/docs/src/src/JBMultiTerminal.sol/contract.JBMultiTerminal.md +0 -1472
  25. package/docs/src/src/JBPermissions.sol/contract.JBPermissions.md +0 -199
  26. package/docs/src/src/JBPrices.sol/contract.JBPrices.md +0 -154
  27. package/docs/src/src/JBProjects.sol/contract.JBProjects.md +0 -131
  28. package/docs/src/src/JBRulesets.sol/contract.JBRulesets.md +0 -677
  29. package/docs/src/src/JBSplits.sol/contract.JBSplits.md +0 -237
  30. package/docs/src/src/JBTerminalStore.sol/contract.JBTerminalStore.md +0 -591
  31. package/docs/src/src/JBTokens.sol/contract.JBTokens.md +0 -353
  32. package/docs/src/src/README.md +0 -25
  33. package/docs/src/src/abstract/JBControlled.sol/abstract.JBControlled.md +0 -64
  34. package/docs/src/src/abstract/JBPermissioned.sol/abstract.JBPermissioned.md +0 -84
  35. package/docs/src/src/abstract/README.md +0 -5
  36. package/docs/src/src/enums/JBApprovalStatus.sol/enum.JBApprovalStatus.md +0 -17
  37. package/docs/src/src/enums/README.md +0 -4
  38. package/docs/src/src/interfaces/IJBCashOutHook.sol/interface.IJBCashOutHook.md +0 -29
  39. package/docs/src/src/interfaces/IJBCashOutTerminal.sol/interface.IJBCashOutTerminal.md +0 -57
  40. package/docs/src/src/interfaces/IJBControlled.sol/interface.IJBControlled.md +0 -12
  41. package/docs/src/src/interfaces/IJBController.sol/interface.IJBController.md +0 -334
  42. package/docs/src/src/interfaces/IJBDirectory.sol/interface.IJBDirectory.md +0 -108
  43. package/docs/src/src/interfaces/IJBDirectoryAccessControl.sol/interface.IJBDirectoryAccessControl.md +0 -19
  44. package/docs/src/src/interfaces/IJBFeeTerminal.sol/interface.IJBFeeTerminal.md +0 -91
  45. package/docs/src/src/interfaces/IJBFeelessAddresses.sol/interface.IJBFeelessAddresses.md +0 -26
  46. package/docs/src/src/interfaces/IJBFundAccessLimits.sol/interface.IJBFundAccessLimits.md +0 -88
  47. package/docs/src/src/interfaces/IJBMigratable.sol/interface.IJBMigratable.md +0 -29
  48. package/docs/src/src/interfaces/IJBMultiTerminal.sol/interface.IJBMultiTerminal.md +0 -50
  49. package/docs/src/src/interfaces/IJBPayHook.sol/interface.IJBPayHook.md +0 -28
  50. package/docs/src/src/interfaces/IJBPayoutTerminal.sol/interface.IJBPayoutTerminal.md +0 -105
  51. package/docs/src/src/interfaces/IJBPermissioned.sol/interface.IJBPermissioned.md +0 -12
  52. package/docs/src/src/interfaces/IJBPermissions.sol/interface.IJBPermissions.md +0 -74
  53. package/docs/src/src/interfaces/IJBPermitTerminal.sol/interface.IJBPermitTerminal.md +0 -15
  54. package/docs/src/src/interfaces/IJBPriceFeed.sol/interface.IJBPriceFeed.md +0 -12
  55. package/docs/src/src/interfaces/IJBPrices.sol/interface.IJBPrices.md +0 -74
  56. package/docs/src/src/interfaces/IJBProjectUriRegistry.sol/interface.IJBProjectUriRegistry.md +0 -19
  57. package/docs/src/src/interfaces/IJBProjects.sol/interface.IJBProjects.md +0 -49
  58. package/docs/src/src/interfaces/IJBRulesetApprovalHook.sol/interface.IJBRulesetApprovalHook.md +0 -35
  59. package/docs/src/src/interfaces/IJBRulesetDataHook.sol/interface.IJBRulesetDataHook.md +0 -97
  60. package/docs/src/src/interfaces/IJBRulesets.sol/interface.IJBRulesets.md +0 -165
  61. package/docs/src/src/interfaces/IJBSplitHook.sol/interface.IJBSplitHook.md +0 -31
  62. package/docs/src/src/interfaces/IJBSplits.sol/interface.IJBSplits.md +0 -35
  63. package/docs/src/src/interfaces/IJBTerminal.sol/interface.IJBTerminal.md +0 -141
  64. package/docs/src/src/interfaces/IJBTerminalStore.sol/interface.IJBTerminalStore.md +0 -198
  65. package/docs/src/src/interfaces/IJBToken.sol/interface.IJBToken.md +0 -54
  66. package/docs/src/src/interfaces/IJBTokenUriResolver.sol/interface.IJBTokenUriResolver.md +0 -12
  67. package/docs/src/src/interfaces/IJBTokens.sol/interface.IJBTokens.md +0 -151
  68. package/docs/src/src/interfaces/README.md +0 -33
  69. package/docs/src/src/libraries/JBCashOuts.sol/library.JBCashOuts.md +0 -40
  70. package/docs/src/src/libraries/JBConstants.sol/library.JBConstants.md +0 -52
  71. package/docs/src/src/libraries/JBCurrencyIds.sol/library.JBCurrencyIds.md +0 -19
  72. package/docs/src/src/libraries/JBFees.sol/library.JBFees.md +0 -52
  73. package/docs/src/src/libraries/JBFixedPointNumber.sol/library.JBFixedPointNumber.md +0 -12
  74. package/docs/src/src/libraries/JBMetadataResolver.sol/library.JBMetadataResolver.md +0 -242
  75. package/docs/src/src/libraries/JBRulesetMetadataResolver.sol/library.JBRulesetMetadataResolver.md +0 -180
  76. package/docs/src/src/libraries/JBSplitGroupIds.sol/library.JBSplitGroupIds.md +0 -14
  77. package/docs/src/src/libraries/JBSurplus.sol/library.JBSurplus.md +0 -44
  78. package/docs/src/src/libraries/README.md +0 -12
  79. package/docs/src/src/periphery/JBDeadline1Day.sol/contract.JBDeadline1Day.md +0 -15
  80. package/docs/src/src/periphery/JBDeadline3Days.sol/contract.JBDeadline3Days.md +0 -15
  81. package/docs/src/src/periphery/JBDeadline3Hours.sol/contract.JBDeadline3Hours.md +0 -15
  82. package/docs/src/src/periphery/JBDeadline7Days.sol/contract.JBDeadline7Days.md +0 -15
  83. package/docs/src/src/periphery/JBMatchingPriceFeed.sol/contract.JBMatchingPriceFeed.md +0 -22
  84. package/docs/src/src/periphery/README.md +0 -8
  85. package/docs/src/src/structs/JBAccountingContext.sol/struct.JBAccountingContext.md +0 -20
  86. package/docs/src/src/structs/JBAfterCashOutRecordedContext.sol/struct.JBAfterCashOutRecordedContext.md +0 -43
  87. package/docs/src/src/structs/JBAfterPayRecordedContext.sol/struct.JBAfterPayRecordedContext.md +0 -42
  88. package/docs/src/src/structs/JBBeforeCashOutRecordedContext.sol/struct.JBBeforeCashOutRecordedContext.md +0 -45
  89. package/docs/src/src/structs/JBBeforePayRecordedContext.sol/struct.JBBeforePayRecordedContext.md +0 -41
  90. package/docs/src/src/structs/JBCashOutHookSpecification.sol/struct.JBCashOutHookSpecification.md +0 -22
  91. package/docs/src/src/structs/JBCurrencyAmount.sol/struct.JBCurrencyAmount.md +0 -17
  92. package/docs/src/src/structs/JBFee.sol/struct.JBFee.md +0 -20
  93. package/docs/src/src/structs/JBFundAccessLimitGroup.sol/struct.JBFundAccessLimitGroup.md +0 -39
  94. package/docs/src/src/structs/JBPayHookSpecification.sol/struct.JBPayHookSpecification.md +0 -22
  95. package/docs/src/src/structs/JBPermissionsData.sol/struct.JBPermissionsData.md +0 -21
  96. package/docs/src/src/structs/JBRuleset.sol/struct.JBRuleset.md +0 -55
  97. package/docs/src/src/structs/JBRulesetConfig.sol/struct.JBRulesetConfig.md +0 -51
  98. package/docs/src/src/structs/JBRulesetMetadata.sol/struct.JBRulesetMetadata.md +0 -79
  99. package/docs/src/src/structs/JBRulesetWeightCache.sol/struct.JBRulesetWeightCache.md +0 -16
  100. package/docs/src/src/structs/JBRulesetWithMetadata.sol/struct.JBRulesetWithMetadata.md +0 -16
  101. package/docs/src/src/structs/JBSingleAllowance.sol/struct.JBSingleAllowance.md +0 -26
  102. package/docs/src/src/structs/JBSplit.sol/struct.JBSplit.md +0 -49
  103. package/docs/src/src/structs/JBSplitGroup.sol/struct.JBSplitGroup.md +0 -17
  104. package/docs/src/src/structs/JBSplitHookContext.sol/struct.JBSplitHookContext.md +0 -29
  105. package/docs/src/src/structs/JBTerminalConfig.sol/struct.JBTerminalConfig.md +0 -16
  106. package/docs/src/src/structs/JBTokenAmount.sol/struct.JBTokenAmount.md +0 -23
  107. package/docs/src/src/structs/README.md +0 -25
@@ -184,99 +184,25 @@ contract JBTerminalStore is IJBTerminalStore {
184
184
  JBCashOutHookSpecification[] memory hookSpecifications
185
185
  )
186
186
  {
187
- // Get a reference to the project's current ruleset.
188
- ruleset = RULESETS.currentOf(projectId);
189
-
190
- // Store the current surplus in `reclaimAmount` temporarily to avoid allocating a separate local variable
191
- // (saves one stack slot, which is needed to fit the 7th parameter without hitting stack-too-deep).
192
- reclaimAmount = ruleset.useTotalSurplusForCashOuts()
193
- ? JBSurplus.currentSurplusOf({
194
- projectId: projectId,
195
- terminals: DIRECTORY.terminalsOf(projectId),
196
- accountingContexts: new JBAccountingContext[](0),
197
- decimals: accountingContext.decimals,
198
- currency: accountingContext.currency
199
- })
200
- : _surplusFrom({
201
- terminal: msg.sender,
202
- projectId: projectId,
203
- accountingContexts: balanceAccountingContexts,
204
- ruleset: ruleset,
205
- targetDecimals: accountingContext.decimals,
206
- targetCurrency: accountingContext.currency
207
- });
208
-
209
- // Scoped to keep `totalSupply` and `context` off the outer stack.
210
- {
211
- // Get the total number of outstanding project tokens.
212
- uint256 totalSupply = IJBController(address(DIRECTORY.controllerOf(projectId)))
213
- .totalTokenSupplyWithReservedTokensOf(projectId);
214
-
215
- // Can't cash out more tokens than are in the supply.
216
- if (cashOutCount > totalSupply) revert JBTerminalStore_InsufficientTokens(cashOutCount, totalSupply);
217
-
218
- // SECURITY NOTE: The data hook has absolute control over cash-out economics.
219
- // It can set totalSupply, cashOutCount, and cashOutTaxRate to arbitrary values,
220
- // completely overriding the terminal's bonding curve math. For example, setting
221
- // totalSupply = surplus makes reclaimAmount = cashOutCount, bypassing the curve.
222
- // Project owners MUST audit their data hooks with the same rigor as the terminal.
223
-
224
- // If the ruleset has a data hook which is enabled for cash outs, use it to derive a claim amount and memo.
225
- if (ruleset.useDataHookForCashOut() && ruleset.dataHook() != address(0)) {
226
- // Build the cash out context field-by-field to avoid stack-too-deep
227
- // (the struct has 11 fields — a struct literal would require all values on the stack at once).
228
- JBBeforeCashOutRecordedContext memory context;
229
- context.terminal = msg.sender;
230
- context.holder = holder;
231
- context.projectId = projectId;
232
- context.rulesetId = ruleset.id;
233
- context.cashOutCount = cashOutCount;
234
- context.totalSupply = totalSupply;
235
- context.surplus = JBTokenAmount({
236
- token: accountingContext.token,
237
- value: reclaimAmount, // reclaimAmount temporarily holds the current surplus.
238
- decimals: accountingContext.decimals,
239
- currency: accountingContext.currency
240
- });
241
- context.useTotalSurplus = ruleset.useTotalSurplusForCashOuts();
242
- context.cashOutTaxRate = ruleset.cashOutTaxRate();
243
- context.beneficiaryIsFeeless = beneficiaryIsFeeless;
244
- context.metadata = metadata;
245
-
246
- (cashOutTaxRate, cashOutCount, totalSupply, hookSpecifications) =
247
- IJBRulesetDataHook(ruleset.dataHook()).beforeCashOutRecordedWith(context);
248
- } else {
249
- cashOutTaxRate = ruleset.cashOutTaxRate();
250
- }
251
-
252
- // Calculate the reclaim amount. `reclaimAmount` currently holds the surplus — overwrite it with the
253
- // result.
254
- if (reclaimAmount != 0) {
255
- reclaimAmount = JBCashOuts.cashOutFrom({
256
- surplus: reclaimAmount,
257
- cashOutCount: cashOutCount,
258
- totalSupply: totalSupply,
259
- cashOutTaxRate: cashOutTaxRate
260
- });
261
- }
262
- }
187
+ (ruleset, reclaimAmount, cashOutTaxRate, hookSpecifications) = _computeCashOutFrom({
188
+ terminal: msg.sender,
189
+ holder: holder,
190
+ projectId: projectId,
191
+ cashOutCount: cashOutCount,
192
+ accountingContext: accountingContext,
193
+ balanceAccountingContexts: balanceAccountingContexts,
194
+ beneficiaryIsFeeless: beneficiaryIsFeeless,
195
+ metadata: metadata
196
+ });
263
197
 
264
- // Keep a reference to the amount that should be added to the project's balance.
198
+ // Compute the total amount to subtract from the project's balance.
265
199
  uint256 balanceDiff = reclaimAmount;
266
200
 
267
- // Ensure that the specifications have valid amounts.
268
201
  if (hookSpecifications.length != 0) {
269
- // Keep a reference to the number of cash out hooks specified.
270
202
  uint256 numberOfSpecifications = hookSpecifications.length;
271
-
272
- // Loop through each specification.
273
203
  for (uint256 i; i < numberOfSpecifications; i++) {
274
- // Get a reference to the specification's amount.
275
204
  uint256 specificationAmount = hookSpecifications[i].amount;
276
-
277
- // Ensure the amount is non-zero.
278
205
  if (specificationAmount != 0) {
279
- // Increment the total amount being subtracted from the balance.
280
206
  balanceDiff += specificationAmount;
281
207
  }
282
208
  }
@@ -323,98 +249,21 @@ contract JBTerminalStore is IJBTerminalStore {
323
249
  override
324
250
  returns (JBRuleset memory ruleset, uint256 tokenCount, JBPayHookSpecification[] memory hookSpecifications)
325
251
  {
326
- // Get a reference to the project's current ruleset.
327
- ruleset = RULESETS.currentOf(projectId);
328
-
329
- // The project must have a ruleset.
330
- if (ruleset.cycleNumber == 0) revert JBTerminalStore_RulesetNotFound(projectId);
331
-
332
- // The ruleset must not have payments paused.
333
- if (ruleset.pausePay()) revert JBTerminalStore_RulesetPaymentPaused();
334
-
335
- // The weight according to which new tokens are to be minted, as a fixed point number with 18 decimals.
336
- uint256 weight;
337
-
338
- // SECURITY NOTE: The data hook has absolute control over payment token minting.
339
- // It can return an arbitrary weight (overriding the ruleset's weight) and hook specifications
340
- // that divert payment funds to external hooks before they reach the project's balance.
341
- // Project owners MUST audit their data hooks with the same rigor as the terminal.
342
-
343
- // If the ruleset has a data hook enabled for payments, use it to derive a weight and memo.
344
- if (ruleset.useDataHookForPay() && ruleset.dataHook() != address(0)) {
345
- // Create the pay context that'll be sent to the data hook.
346
- JBBeforePayRecordedContext memory context = JBBeforePayRecordedContext({
347
- terminal: msg.sender,
348
- payer: payer,
349
- amount: amount,
350
- projectId: projectId,
351
- rulesetId: ruleset.id,
352
- beneficiary: beneficiary,
353
- weight: ruleset.weight,
354
- reservedPercent: ruleset.reservedPercent(),
355
- metadata: metadata
356
- });
357
-
358
- (weight, hookSpecifications) = IJBRulesetDataHook(ruleset.dataHook()).beforePayRecordedWith(context);
359
- }
360
- // Otherwise use the ruleset's weight
361
- else {
362
- weight = ruleset.weight;
363
- }
364
-
365
- // Keep a reference to the amount that should be added to the project's balance.
366
- uint256 balanceDiff = amount.value;
367
-
368
- // Scoped section preventing stack too deep.
369
- {
370
- // Keep a reference to the number of hook specifications.
371
- uint256 numberOfSpecifications = hookSpecifications.length;
372
-
373
- // Ensure that the specifications have valid amounts.
374
- for (uint256 i; i < numberOfSpecifications; i++) {
375
- // Get a reference to the specification's amount.
376
- uint256 specifiedAmount = hookSpecifications[i].amount;
377
-
378
- // Ensure the amount is non-zero.
379
- if (specifiedAmount != 0) {
380
- // Can't send more to hook than was paid.
381
- if (specifiedAmount > balanceDiff) {
382
- revert JBTerminalStore_InvalidAmountToForwardHook(specifiedAmount, balanceDiff);
383
- }
384
-
385
- // Decrement the total amount being added to the local balance.
386
- balanceDiff -= specifiedAmount;
387
- }
388
- }
389
- }
390
-
391
- // If there's no amount being recorded, there's nothing left to do.
392
- if (amount.value == 0) return (ruleset, 0, hookSpecifications);
252
+ uint256 balanceDiff;
253
+ (ruleset, tokenCount, hookSpecifications, balanceDiff) = _computePayFrom({
254
+ terminal: msg.sender,
255
+ payer: payer,
256
+ amount: amount,
257
+ projectId: projectId,
258
+ beneficiary: beneficiary,
259
+ metadata: metadata
260
+ });
393
261
 
394
262
  // Add the correct balance difference to the token balance of the project.
395
263
  if (balanceDiff != 0) {
396
264
  balanceOf[msg.sender][projectId][amount.token] =
397
265
  balanceOf[msg.sender][projectId][amount.token] + balanceDiff;
398
266
  }
399
-
400
- // If there's no weight, the token count must be 0, so there's nothing left to do.
401
- if (weight == 0) return (ruleset, 0, hookSpecifications);
402
-
403
- // If the terminal should base its weight on a currency other than the terminal's currency, determine the
404
- // factor. The weight is always a fixed point mumber with 18 decimals. To ensure this, the ratio should use the
405
- // same
406
- // number of decimals as the `amount`.
407
- uint256 weightRatio = amount.currency == ruleset.baseCurrency()
408
- ? 10 ** amount.decimals
409
- : PRICES.pricePerUnitOf({
410
- projectId: projectId,
411
- pricingCurrency: amount.currency,
412
- unitCurrency: ruleset.baseCurrency(),
413
- decimals: amount.decimals
414
- });
415
-
416
- // Find the number of tokens to mint, as a fixed point number with as many decimals as `weight` has.
417
- tokenCount = mulDiv(amount.value, weight, weightRatio);
418
267
  }
419
268
 
420
269
  /// @notice Records a payout from a project.
@@ -699,6 +548,34 @@ contract JBTerminalStore is IJBTerminalStore {
699
548
  });
700
549
  }
701
550
 
551
+ /// @notice Returns the number of surplus terminal tokens that would be reclaimed by cashing out a given number of
552
+ /// tokens across all of a project's terminals using all accounting contexts.
553
+ /// @param projectId The ID of the project whose tokens would be cashed out.
554
+ /// @param cashOutCount The number of tokens that would be cashed out, as a fixed point number with 18 decimals.
555
+ /// @param decimals The number of decimals to include in the resulting fixed point number.
556
+ /// @param currency The currency that the resulting number will be in terms of.
557
+ /// @return The amount of surplus terminal tokens that would be reclaimed by cashing out `cashOutCount` tokens.
558
+ function currentTotalReclaimableSurplusOf(
559
+ uint256 projectId,
560
+ uint256 cashOutCount,
561
+ uint256 decimals,
562
+ uint256 currency
563
+ )
564
+ external
565
+ view
566
+ override
567
+ returns (uint256)
568
+ {
569
+ return this.currentReclaimableSurplusOf({
570
+ projectId: projectId,
571
+ cashOutCount: cashOutCount,
572
+ terminals: new IJBTerminal[](0),
573
+ accountingContexts: new JBAccountingContext[](0),
574
+ decimals: decimals,
575
+ currency: currency
576
+ });
577
+ }
578
+
702
579
  /// @notice Gets the current surplus amount in a terminal for a specified project.
703
580
  /// @dev The surplus is the amount of funds a project has in a terminal in excess of its payout limit.
704
581
  /// @dev The surplus is represented as a fixed point number with the same amount of decimals as the specified
@@ -757,10 +634,319 @@ contract JBTerminalStore is IJBTerminalStore {
757
634
  });
758
635
  }
759
636
 
637
+ /// @notice Simulates a cash out without modifying state.
638
+ /// @dev Invokes data hooks if configured, but skips the balance sufficiency check (balance may change between
639
+ /// preview and execution).
640
+ /// @param terminal The terminal address to simulate the cash out from.
641
+ /// @param holder The address cashing out.
642
+ /// @param projectId The ID of the project being cashed out from.
643
+ /// @param cashOutCount The number of project tokens being cashed out.
644
+ /// @param accountingContext The accounting context of the token being reclaimed.
645
+ /// @param balanceAccountingContexts The accounting contexts to include in the balance calculation.
646
+ /// @param beneficiaryIsFeeless Whether the cash out's beneficiary is a feeless address.
647
+ /// @param metadata Extra data to pass along to the data hook.
648
+ /// @return ruleset The project's current ruleset.
649
+ /// @return reclaimAmount The amount that would be reclaimed.
650
+ /// @return cashOutTaxRate The cash out tax rate that would be applied.
651
+ /// @return hookSpecifications Any cash out hook specifications from the data hook.
652
+ function previewCashOutFrom(
653
+ address terminal,
654
+ address holder,
655
+ uint256 projectId,
656
+ uint256 cashOutCount,
657
+ JBAccountingContext calldata accountingContext,
658
+ JBAccountingContext[] calldata balanceAccountingContexts,
659
+ bool beneficiaryIsFeeless,
660
+ bytes calldata metadata
661
+ )
662
+ external
663
+ view
664
+ override
665
+ returns (
666
+ JBRuleset memory ruleset,
667
+ uint256 reclaimAmount,
668
+ uint256 cashOutTaxRate,
669
+ JBCashOutHookSpecification[] memory hookSpecifications
670
+ )
671
+ {
672
+ (ruleset, reclaimAmount, cashOutTaxRate, hookSpecifications) = _computeCashOutFrom({
673
+ terminal: terminal,
674
+ holder: holder,
675
+ projectId: projectId,
676
+ cashOutCount: cashOutCount,
677
+ accountingContext: accountingContext,
678
+ balanceAccountingContexts: balanceAccountingContexts,
679
+ beneficiaryIsFeeless: beneficiaryIsFeeless,
680
+ metadata: metadata
681
+ });
682
+ }
683
+
684
+ /// @notice Simulates a payment without modifying state.
685
+ /// @dev Invokes data hooks if configured. Returns the same token count and hook specifications that
686
+ /// `recordPaymentFrom` would produce.
687
+ /// @param terminal The terminal address to simulate the payment from.
688
+ /// @param payer The address of the payer.
689
+ /// @param amount The amount being paid.
690
+ /// @param projectId The ID of the project being paid.
691
+ /// @param beneficiary The address to mint project tokens to.
692
+ /// @param metadata Extra data to pass along to the data hook.
693
+ /// @return ruleset The project's current ruleset.
694
+ /// @return tokenCount The number of project tokens that would be minted.
695
+ /// @return hookSpecifications Any pay hook specifications from the data hook.
696
+ function previewPayFrom(
697
+ address terminal,
698
+ address payer,
699
+ JBTokenAmount memory amount,
700
+ uint256 projectId,
701
+ address beneficiary,
702
+ bytes calldata metadata
703
+ )
704
+ external
705
+ view
706
+ override
707
+ returns (JBRuleset memory ruleset, uint256 tokenCount, JBPayHookSpecification[] memory hookSpecifications)
708
+ {
709
+ (ruleset, tokenCount, hookSpecifications,) = _computePayFrom({
710
+ terminal: terminal,
711
+ payer: payer,
712
+ amount: amount,
713
+ projectId: projectId,
714
+ beneficiary: beneficiary,
715
+ metadata: metadata
716
+ });
717
+ }
718
+
760
719
  //*********************************************************************//
761
720
  // -------------------------- internal views ------------------------- //
762
721
  //*********************************************************************//
763
722
 
723
+ /// @notice Computes payment results without writing state.
724
+ /// @param terminal The terminal recording the payment.
725
+ /// @param payer The address that made the payment.
726
+ /// @param amount The amount of tokens being paid.
727
+ /// @param projectId The ID of the project being paid.
728
+ /// @param beneficiary The beneficiary of the payment.
729
+ /// @param metadata Bytes to send to the data hook.
730
+ /// @return ruleset The ruleset the payment would be made during.
731
+ /// @return tokenCount The number of project tokens that would be minted.
732
+ /// @return hookSpecifications Pay hook specifications from the data hook.
733
+ /// @return balanceDiff The amount that would be added to the project's balance.
734
+ function _computePayFrom(
735
+ address terminal,
736
+ address payer,
737
+ JBTokenAmount memory amount,
738
+ uint256 projectId,
739
+ address beneficiary,
740
+ bytes memory metadata
741
+ )
742
+ internal
743
+ view
744
+ returns (
745
+ JBRuleset memory ruleset,
746
+ uint256 tokenCount,
747
+ JBPayHookSpecification[] memory hookSpecifications,
748
+ uint256 balanceDiff
749
+ )
750
+ {
751
+ // Get a reference to the project's current ruleset.
752
+ ruleset = RULESETS.currentOf(projectId);
753
+
754
+ // The project must have a ruleset.
755
+ if (ruleset.cycleNumber == 0) revert JBTerminalStore_RulesetNotFound(projectId);
756
+
757
+ // The ruleset must not have payments paused.
758
+ if (ruleset.pausePay()) revert JBTerminalStore_RulesetPaymentPaused();
759
+
760
+ // The weight according to which new tokens are to be minted, as a fixed point number with 18 decimals.
761
+ uint256 weight;
762
+
763
+ // SECURITY NOTE: The data hook has absolute control over payment token minting.
764
+ // It can return an arbitrary weight (overriding the ruleset's weight) and hook specifications
765
+ // that divert payment funds to external hooks before they reach the project's balance.
766
+ // Project owners MUST audit their data hooks with the same rigor as the terminal.
767
+
768
+ // If the ruleset has a data hook enabled for payments, use it to derive a weight and memo.
769
+ if (ruleset.useDataHookForPay() && ruleset.dataHook() != address(0)) {
770
+ // Create the pay context that'll be sent to the data hook.
771
+ JBBeforePayRecordedContext memory context = JBBeforePayRecordedContext({
772
+ terminal: terminal,
773
+ payer: payer,
774
+ amount: amount,
775
+ projectId: projectId,
776
+ rulesetId: ruleset.id,
777
+ beneficiary: beneficiary,
778
+ weight: ruleset.weight,
779
+ reservedPercent: ruleset.reservedPercent(),
780
+ metadata: metadata
781
+ });
782
+
783
+ (weight, hookSpecifications) = IJBRulesetDataHook(ruleset.dataHook()).beforePayRecordedWith(context);
784
+ }
785
+ // Otherwise use the ruleset's weight
786
+ else {
787
+ weight = ruleset.weight;
788
+ }
789
+
790
+ // Keep a reference to the amount that should be added to the project's balance.
791
+ balanceDiff = amount.value;
792
+
793
+ // Scoped section preventing stack too deep.
794
+ {
795
+ // Keep a reference to the number of hook specifications.
796
+ uint256 numberOfSpecifications = hookSpecifications.length;
797
+
798
+ // Ensure that the specifications have valid amounts.
799
+ for (uint256 i; i < numberOfSpecifications; i++) {
800
+ // Get a reference to the specification's amount.
801
+ uint256 specifiedAmount = hookSpecifications[i].amount;
802
+
803
+ // Ensure the amount is non-zero.
804
+ if (specifiedAmount != 0) {
805
+ // Can't send more to hook than was paid.
806
+ if (specifiedAmount > balanceDiff) {
807
+ revert JBTerminalStore_InvalidAmountToForwardHook(specifiedAmount, balanceDiff);
808
+ }
809
+
810
+ // Decrement the total amount being added to the local balance.
811
+ balanceDiff -= specifiedAmount;
812
+ }
813
+ }
814
+ }
815
+
816
+ // If there's no amount being recorded, there's nothing left to do.
817
+ if (amount.value == 0) return (ruleset, 0, hookSpecifications, 0);
818
+
819
+ // If there's no weight, the token count must be 0, so there's nothing left to do.
820
+ if (weight == 0) return (ruleset, 0, hookSpecifications, balanceDiff);
821
+
822
+ // If the terminal should base its weight on a currency other than the terminal's currency, determine the
823
+ // factor. The weight is always a fixed point mumber with 18 decimals. To ensure this, the ratio should use the
824
+ // same
825
+ // number of decimals as the `amount`.
826
+ uint256 weightRatio = amount.currency == ruleset.baseCurrency()
827
+ ? 10 ** amount.decimals
828
+ : PRICES.pricePerUnitOf({
829
+ projectId: projectId,
830
+ pricingCurrency: amount.currency,
831
+ unitCurrency: ruleset.baseCurrency(),
832
+ decimals: amount.decimals
833
+ });
834
+
835
+ // Find the number of tokens to mint, as a fixed point number with as many decimals as `weight` has.
836
+ tokenCount = mulDiv(amount.value, weight, weightRatio);
837
+ }
838
+
839
+ /// @notice Computes cash out results without writing state.
840
+ /// @param terminal The terminal recording the cash out.
841
+ /// @param holder The account that is cashing out tokens.
842
+ /// @param projectId The ID of the project being cashed out from.
843
+ /// @param cashOutCount The number of project tokens to cash out.
844
+ /// @param accountingContext The accounting context of the token being reclaimed.
845
+ /// @param balanceAccountingContexts The accounting contexts of the tokens whose balances should contribute to the
846
+ /// surplus being reclaimed from.
847
+ /// @param beneficiaryIsFeeless Whether the cash out's beneficiary is a feeless address.
848
+ /// @param metadata Bytes to send to the data hook.
849
+ /// @return ruleset The ruleset during the cash out.
850
+ /// @return reclaimAmount The amount of tokens reclaimed.
851
+ /// @return cashOutTaxRate The cash out tax rate applied.
852
+ /// @return hookSpecifications Cash out hook specifications from the data hook.
853
+ function _computeCashOutFrom(
854
+ address terminal,
855
+ address holder,
856
+ uint256 projectId,
857
+ uint256 cashOutCount,
858
+ JBAccountingContext calldata accountingContext,
859
+ JBAccountingContext[] calldata balanceAccountingContexts,
860
+ bool beneficiaryIsFeeless,
861
+ bytes memory metadata
862
+ )
863
+ internal
864
+ view
865
+ returns (
866
+ JBRuleset memory ruleset,
867
+ uint256 reclaimAmount,
868
+ uint256 cashOutTaxRate,
869
+ JBCashOutHookSpecification[] memory hookSpecifications
870
+ )
871
+ {
872
+ // Get a reference to the project's current ruleset.
873
+ ruleset = RULESETS.currentOf(projectId);
874
+
875
+ // Store the current surplus in `reclaimAmount` temporarily to avoid allocating a separate local variable
876
+ // (saves one stack slot, which is needed to fit the 7th parameter without hitting stack-too-deep).
877
+ reclaimAmount = ruleset.useTotalSurplusForCashOuts()
878
+ ? JBSurplus.currentSurplusOf({
879
+ projectId: projectId,
880
+ terminals: DIRECTORY.terminalsOf(projectId),
881
+ accountingContexts: new JBAccountingContext[](0),
882
+ decimals: accountingContext.decimals,
883
+ currency: accountingContext.currency
884
+ })
885
+ : _surplusFrom({
886
+ terminal: terminal,
887
+ projectId: projectId,
888
+ accountingContexts: balanceAccountingContexts,
889
+ ruleset: ruleset,
890
+ targetDecimals: accountingContext.decimals,
891
+ targetCurrency: accountingContext.currency
892
+ });
893
+
894
+ // Scoped to keep `totalSupply` and `context` off the outer stack.
895
+ {
896
+ // Get the total number of outstanding project tokens.
897
+ uint256 totalSupply = IJBController(address(DIRECTORY.controllerOf(projectId)))
898
+ .totalTokenSupplyWithReservedTokensOf(projectId);
899
+
900
+ // Can't cash out more tokens than are in the supply.
901
+ if (cashOutCount > totalSupply) revert JBTerminalStore_InsufficientTokens(cashOutCount, totalSupply);
902
+
903
+ // SECURITY NOTE: The data hook has absolute control over cash-out economics.
904
+ // It can set totalSupply, cashOutCount, and cashOutTaxRate to arbitrary values,
905
+ // completely overriding the terminal's bonding curve math. For example, setting
906
+ // totalSupply = surplus makes reclaimAmount = cashOutCount, bypassing the curve.
907
+ // Project owners MUST audit their data hooks with the same rigor as the terminal.
908
+
909
+ // If the ruleset has a data hook which is enabled for cash outs, use it to derive a claim amount and memo.
910
+ if (ruleset.useDataHookForCashOut() && ruleset.dataHook() != address(0)) {
911
+ // Build the cash out context field-by-field to avoid stack-too-deep
912
+ // (the struct has 11 fields — a struct literal would require all values on the stack at once).
913
+ JBBeforeCashOutRecordedContext memory context;
914
+ context.terminal = terminal;
915
+ context.holder = holder;
916
+ context.projectId = projectId;
917
+ context.rulesetId = ruleset.id;
918
+ context.cashOutCount = cashOutCount;
919
+ context.totalSupply = totalSupply;
920
+ context.surplus = JBTokenAmount({
921
+ token: accountingContext.token,
922
+ value: reclaimAmount, // reclaimAmount temporarily holds the current surplus.
923
+ decimals: accountingContext.decimals,
924
+ currency: accountingContext.currency
925
+ });
926
+ context.useTotalSurplus = ruleset.useTotalSurplusForCashOuts();
927
+ context.cashOutTaxRate = ruleset.cashOutTaxRate();
928
+ context.beneficiaryIsFeeless = beneficiaryIsFeeless;
929
+ context.metadata = metadata;
930
+
931
+ (cashOutTaxRate, cashOutCount, totalSupply, hookSpecifications) =
932
+ IJBRulesetDataHook(ruleset.dataHook()).beforeCashOutRecordedWith(context);
933
+ } else {
934
+ cashOutTaxRate = ruleset.cashOutTaxRate();
935
+ }
936
+
937
+ // Calculate the reclaim amount. `reclaimAmount` currently holds the surplus — overwrite it with the
938
+ // result.
939
+ if (reclaimAmount != 0) {
940
+ reclaimAmount = JBCashOuts.cashOutFrom({
941
+ surplus: reclaimAmount,
942
+ cashOutCount: cashOutCount,
943
+ totalSupply: totalSupply,
944
+ cashOutTaxRate: cashOutTaxRate
945
+ });
946
+ }
947
+ }
948
+ }
949
+
764
950
  /// @notice Gets a project's surplus amount in a terminal as measured by a given ruleset, across multiple accounting
765
951
  /// contexts.
766
952
  /// @dev This amount changes as the value of the balance changes in relation to the currency being used to measure
@@ -65,6 +65,22 @@ interface IJBTerminalStore {
65
65
  view
66
66
  returns (uint256);
67
67
 
68
+ /// @notice Returns the reclaimable surplus for a project across all terminals using all accounting contexts.
69
+ /// @param projectId The ID of the project.
70
+ /// @param cashOutCount The number of tokens being cashed out.
71
+ /// @param decimals The number of decimals to express the result with.
72
+ /// @param currency The currency to express the result in.
73
+ /// @return The reclaimable surplus amount.
74
+ function currentTotalReclaimableSurplusOf(
75
+ uint256 projectId,
76
+ uint256 cashOutCount,
77
+ uint256 decimals,
78
+ uint256 currency
79
+ )
80
+ external
81
+ view
82
+ returns (uint256);
83
+
68
84
  /// @notice Returns the current surplus for a terminal and project.
69
85
  /// @param terminal The terminal to get the surplus of.
70
86
  /// @param projectId The ID of the project.
@@ -97,6 +113,60 @@ interface IJBTerminalStore {
97
113
  view
98
114
  returns (uint256);
99
115
 
116
+ /// @notice Simulates a cash out without modifying state.
117
+ /// @param terminal The terminal address to simulate the cash out from.
118
+ /// @param holder The address cashing out.
119
+ /// @param projectId The ID of the project being cashed out from.
120
+ /// @param cashOutCount The number of project tokens being cashed out.
121
+ /// @param accountingContext The accounting context of the token being reclaimed.
122
+ /// @param balanceAccountingContexts The accounting contexts to include in the balance calculation.
123
+ /// @param beneficiaryIsFeeless Whether the cash out's beneficiary is a feeless address.
124
+ /// @param metadata Extra data to pass along to the data hook.
125
+ /// @return ruleset The project's current ruleset.
126
+ /// @return reclaimAmount The amount that would be reclaimed.
127
+ /// @return cashOutTaxRate The cash out tax rate that would be applied.
128
+ /// @return hookSpecifications Any cash out hook specifications from the data hook.
129
+ function previewCashOutFrom(
130
+ address terminal,
131
+ address holder,
132
+ uint256 projectId,
133
+ uint256 cashOutCount,
134
+ JBAccountingContext calldata accountingContext,
135
+ JBAccountingContext[] calldata balanceAccountingContexts,
136
+ bool beneficiaryIsFeeless,
137
+ bytes calldata metadata
138
+ )
139
+ external
140
+ view
141
+ returns (
142
+ JBRuleset memory ruleset,
143
+ uint256 reclaimAmount,
144
+ uint256 cashOutTaxRate,
145
+ JBCashOutHookSpecification[] memory hookSpecifications
146
+ );
147
+
148
+ /// @notice Simulates a payment without modifying state.
149
+ /// @param terminal The terminal address to simulate the payment from.
150
+ /// @param payer The address of the payer.
151
+ /// @param amount The amount being paid.
152
+ /// @param projectId The ID of the project being paid.
153
+ /// @param beneficiary The address to mint project tokens to.
154
+ /// @param metadata Extra data to pass along to the data hook.
155
+ /// @return ruleset The project's current ruleset.
156
+ /// @return tokenCount The number of project tokens that would be minted.
157
+ /// @return hookSpecifications Any pay hook specifications from the data hook.
158
+ function previewPayFrom(
159
+ address terminal,
160
+ address payer,
161
+ JBTokenAmount memory amount,
162
+ uint256 projectId,
163
+ address beneficiary,
164
+ bytes calldata metadata
165
+ )
166
+ external
167
+ view
168
+ returns (JBRuleset memory ruleset, uint256 tokenCount, JBPayHookSpecification[] memory hookSpecifications);
169
+
100
170
  /// @notice Returns the amount of payout limit used by a terminal for a project in a given cycle.
101
171
  /// @param terminal The terminal to get the used payout limit of.
102
172
  /// @param projectId The ID of the project.