@bananapus/core-v6 0.0.16 → 0.0.18

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 (140) hide show
  1. package/ADMINISTRATION.md +1 -1
  2. package/ARCHITECTURE.md +2 -1
  3. package/AUDIT_INSTRUCTIONS.md +342 -0
  4. package/CHANGE_LOG.md +400 -0
  5. package/README.md +4 -4
  6. package/RISKS.md +171 -50
  7. package/SKILLS.md +9 -6
  8. package/USER_JOURNEYS.md +622 -0
  9. package/package.json +2 -2
  10. package/script/DeployPeriphery.s.sol +7 -1
  11. package/src/JBController.sol +5 -0
  12. package/src/JBDeadline.sol +3 -0
  13. package/src/JBDirectory.sol +2 -1
  14. package/src/JBMultiTerminal.sol +50 -9
  15. package/src/JBPermissions.sol +2 -0
  16. package/src/JBPrices.sol +8 -2
  17. package/src/JBRulesets.sol +3 -0
  18. package/src/JBSplits.sol +9 -5
  19. package/src/JBTerminalStore.sol +54 -47
  20. package/src/JBTokens.sol +3 -0
  21. package/src/interfaces/IJBTerminalStore.sol +3 -0
  22. package/src/libraries/JBFees.sol +2 -0
  23. package/src/libraries/JBMetadataResolver.sol +17 -4
  24. package/src/structs/JBBeforeCashOutRecordedContext.sol +4 -0
  25. package/test/TestAuditResponseDesignProofs.sol +434 -0
  26. package/test/TestDataHookFuzzing.sol +520 -0
  27. package/test/TestFeeFreeCashOutBypass.sol +617 -0
  28. package/test/TestL2SequencerPriceFeed.sol +292 -0
  29. package/test/TestMetadataOffsetOverflow.sol +179 -0
  30. package/test/TestMultiTerminalSurplus.sol +348 -0
  31. package/test/TestPermit2DataHook.t.sol +360 -0
  32. package/test/TestRulesetQueueing.sol +1 -2
  33. package/test/TestRulesetWeightCaching.sol +122 -124
  34. package/test/WeirdTokenTests.t.sol +37 -0
  35. package/test/regression/HoldFeesCashOutReserved.t.sol +415 -0
  36. package/test/regression/WeightCacheBoundary.t.sol +291 -0
  37. package/test/units/static/JBMultiTerminal/TestAddToBalanceOf.sol +2 -2
  38. package/test/units/static/JBMultiTerminal/TestCashOutTokensOf.sol +18 -17
  39. package/test/units/static/JBMultiTerminal/TestPay.sol +6 -4
  40. package/test/units/static/JBMultiTerminal/TestProcessHeldFeesOf.sol +206 -18
  41. package/test/units/static/JBMultiTerminal/TestUseAllowanceOf.sol +280 -0
  42. package/test/units/static/JBSplits/TestSelfManagedSplitGroups.sol +55 -12
  43. package/test/units/static/JBTerminalStore/TestRecordCashOutsFor.sol +72 -0
  44. package/docs/book.css +0 -13
  45. package/docs/book.toml +0 -12
  46. package/docs/solidity.min.js +0 -74
  47. package/docs/src/README.md +0 -703
  48. package/docs/src/SUMMARY.md +0 -94
  49. package/docs/src/src/JBChainlinkV3PriceFeed.sol/contract.JBChainlinkV3PriceFeed.md +0 -83
  50. package/docs/src/src/JBChainlinkV3SequencerPriceFeed.sol/contract.JBChainlinkV3SequencerPriceFeed.md +0 -88
  51. package/docs/src/src/JBController.sol/contract.JBController.md +0 -1121
  52. package/docs/src/src/JBDeadline.sol/contract.JBDeadline.md +0 -84
  53. package/docs/src/src/JBDirectory.sol/contract.JBDirectory.md +0 -294
  54. package/docs/src/src/JBERC20.sol/contract.JBERC20.md +0 -190
  55. package/docs/src/src/JBFeelessAddresses.sol/contract.JBFeelessAddresses.md +0 -80
  56. package/docs/src/src/JBFundAccessLimits.sol/contract.JBFundAccessLimits.md +0 -253
  57. package/docs/src/src/JBMultiTerminal.sol/contract.JBMultiTerminal.md +0 -1472
  58. package/docs/src/src/JBPermissions.sol/contract.JBPermissions.md +0 -199
  59. package/docs/src/src/JBPrices.sol/contract.JBPrices.md +0 -154
  60. package/docs/src/src/JBProjects.sol/contract.JBProjects.md +0 -131
  61. package/docs/src/src/JBRulesets.sol/contract.JBRulesets.md +0 -677
  62. package/docs/src/src/JBSplits.sol/contract.JBSplits.md +0 -237
  63. package/docs/src/src/JBTerminalStore.sol/contract.JBTerminalStore.md +0 -591
  64. package/docs/src/src/JBTokens.sol/contract.JBTokens.md +0 -353
  65. package/docs/src/src/README.md +0 -25
  66. package/docs/src/src/abstract/JBControlled.sol/abstract.JBControlled.md +0 -64
  67. package/docs/src/src/abstract/JBPermissioned.sol/abstract.JBPermissioned.md +0 -84
  68. package/docs/src/src/abstract/README.md +0 -5
  69. package/docs/src/src/enums/JBApprovalStatus.sol/enum.JBApprovalStatus.md +0 -17
  70. package/docs/src/src/enums/README.md +0 -4
  71. package/docs/src/src/interfaces/IJBCashOutHook.sol/interface.IJBCashOutHook.md +0 -29
  72. package/docs/src/src/interfaces/IJBCashOutTerminal.sol/interface.IJBCashOutTerminal.md +0 -57
  73. package/docs/src/src/interfaces/IJBControlled.sol/interface.IJBControlled.md +0 -12
  74. package/docs/src/src/interfaces/IJBController.sol/interface.IJBController.md +0 -334
  75. package/docs/src/src/interfaces/IJBDirectory.sol/interface.IJBDirectory.md +0 -108
  76. package/docs/src/src/interfaces/IJBDirectoryAccessControl.sol/interface.IJBDirectoryAccessControl.md +0 -19
  77. package/docs/src/src/interfaces/IJBFeeTerminal.sol/interface.IJBFeeTerminal.md +0 -91
  78. package/docs/src/src/interfaces/IJBFeelessAddresses.sol/interface.IJBFeelessAddresses.md +0 -26
  79. package/docs/src/src/interfaces/IJBFundAccessLimits.sol/interface.IJBFundAccessLimits.md +0 -88
  80. package/docs/src/src/interfaces/IJBMigratable.sol/interface.IJBMigratable.md +0 -29
  81. package/docs/src/src/interfaces/IJBMultiTerminal.sol/interface.IJBMultiTerminal.md +0 -50
  82. package/docs/src/src/interfaces/IJBPayHook.sol/interface.IJBPayHook.md +0 -28
  83. package/docs/src/src/interfaces/IJBPayoutTerminal.sol/interface.IJBPayoutTerminal.md +0 -105
  84. package/docs/src/src/interfaces/IJBPermissioned.sol/interface.IJBPermissioned.md +0 -12
  85. package/docs/src/src/interfaces/IJBPermissions.sol/interface.IJBPermissions.md +0 -74
  86. package/docs/src/src/interfaces/IJBPermitTerminal.sol/interface.IJBPermitTerminal.md +0 -15
  87. package/docs/src/src/interfaces/IJBPriceFeed.sol/interface.IJBPriceFeed.md +0 -12
  88. package/docs/src/src/interfaces/IJBPrices.sol/interface.IJBPrices.md +0 -74
  89. package/docs/src/src/interfaces/IJBProjectUriRegistry.sol/interface.IJBProjectUriRegistry.md +0 -19
  90. package/docs/src/src/interfaces/IJBProjects.sol/interface.IJBProjects.md +0 -49
  91. package/docs/src/src/interfaces/IJBRulesetApprovalHook.sol/interface.IJBRulesetApprovalHook.md +0 -35
  92. package/docs/src/src/interfaces/IJBRulesetDataHook.sol/interface.IJBRulesetDataHook.md +0 -97
  93. package/docs/src/src/interfaces/IJBRulesets.sol/interface.IJBRulesets.md +0 -165
  94. package/docs/src/src/interfaces/IJBSplitHook.sol/interface.IJBSplitHook.md +0 -31
  95. package/docs/src/src/interfaces/IJBSplits.sol/interface.IJBSplits.md +0 -35
  96. package/docs/src/src/interfaces/IJBTerminal.sol/interface.IJBTerminal.md +0 -141
  97. package/docs/src/src/interfaces/IJBTerminalStore.sol/interface.IJBTerminalStore.md +0 -198
  98. package/docs/src/src/interfaces/IJBToken.sol/interface.IJBToken.md +0 -54
  99. package/docs/src/src/interfaces/IJBTokenUriResolver.sol/interface.IJBTokenUriResolver.md +0 -12
  100. package/docs/src/src/interfaces/IJBTokens.sol/interface.IJBTokens.md +0 -151
  101. package/docs/src/src/interfaces/README.md +0 -33
  102. package/docs/src/src/libraries/JBCashOuts.sol/library.JBCashOuts.md +0 -40
  103. package/docs/src/src/libraries/JBConstants.sol/library.JBConstants.md +0 -52
  104. package/docs/src/src/libraries/JBCurrencyIds.sol/library.JBCurrencyIds.md +0 -19
  105. package/docs/src/src/libraries/JBFees.sol/library.JBFees.md +0 -52
  106. package/docs/src/src/libraries/JBFixedPointNumber.sol/library.JBFixedPointNumber.md +0 -12
  107. package/docs/src/src/libraries/JBMetadataResolver.sol/library.JBMetadataResolver.md +0 -242
  108. package/docs/src/src/libraries/JBRulesetMetadataResolver.sol/library.JBRulesetMetadataResolver.md +0 -180
  109. package/docs/src/src/libraries/JBSplitGroupIds.sol/library.JBSplitGroupIds.md +0 -14
  110. package/docs/src/src/libraries/JBSurplus.sol/library.JBSurplus.md +0 -44
  111. package/docs/src/src/libraries/README.md +0 -12
  112. package/docs/src/src/periphery/JBDeadline1Day.sol/contract.JBDeadline1Day.md +0 -15
  113. package/docs/src/src/periphery/JBDeadline3Days.sol/contract.JBDeadline3Days.md +0 -15
  114. package/docs/src/src/periphery/JBDeadline3Hours.sol/contract.JBDeadline3Hours.md +0 -15
  115. package/docs/src/src/periphery/JBDeadline7Days.sol/contract.JBDeadline7Days.md +0 -15
  116. package/docs/src/src/periphery/JBMatchingPriceFeed.sol/contract.JBMatchingPriceFeed.md +0 -22
  117. package/docs/src/src/periphery/README.md +0 -8
  118. package/docs/src/src/structs/JBAccountingContext.sol/struct.JBAccountingContext.md +0 -20
  119. package/docs/src/src/structs/JBAfterCashOutRecordedContext.sol/struct.JBAfterCashOutRecordedContext.md +0 -43
  120. package/docs/src/src/structs/JBAfterPayRecordedContext.sol/struct.JBAfterPayRecordedContext.md +0 -42
  121. package/docs/src/src/structs/JBBeforeCashOutRecordedContext.sol/struct.JBBeforeCashOutRecordedContext.md +0 -45
  122. package/docs/src/src/structs/JBBeforePayRecordedContext.sol/struct.JBBeforePayRecordedContext.md +0 -41
  123. package/docs/src/src/structs/JBCashOutHookSpecification.sol/struct.JBCashOutHookSpecification.md +0 -22
  124. package/docs/src/src/structs/JBCurrencyAmount.sol/struct.JBCurrencyAmount.md +0 -17
  125. package/docs/src/src/structs/JBFee.sol/struct.JBFee.md +0 -20
  126. package/docs/src/src/structs/JBFundAccessLimitGroup.sol/struct.JBFundAccessLimitGroup.md +0 -39
  127. package/docs/src/src/structs/JBPayHookSpecification.sol/struct.JBPayHookSpecification.md +0 -22
  128. package/docs/src/src/structs/JBPermissionsData.sol/struct.JBPermissionsData.md +0 -21
  129. package/docs/src/src/structs/JBRuleset.sol/struct.JBRuleset.md +0 -55
  130. package/docs/src/src/structs/JBRulesetConfig.sol/struct.JBRulesetConfig.md +0 -51
  131. package/docs/src/src/structs/JBRulesetMetadata.sol/struct.JBRulesetMetadata.md +0 -79
  132. package/docs/src/src/structs/JBRulesetWeightCache.sol/struct.JBRulesetWeightCache.md +0 -16
  133. package/docs/src/src/structs/JBRulesetWithMetadata.sol/struct.JBRulesetWithMetadata.md +0 -16
  134. package/docs/src/src/structs/JBSingleAllowance.sol/struct.JBSingleAllowance.md +0 -26
  135. package/docs/src/src/structs/JBSplit.sol/struct.JBSplit.md +0 -49
  136. package/docs/src/src/structs/JBSplitGroup.sol/struct.JBSplitGroup.md +0 -17
  137. package/docs/src/src/structs/JBSplitHookContext.sol/struct.JBSplitHookContext.md +0 -29
  138. package/docs/src/src/structs/JBTerminalConfig.sol/struct.JBTerminalConfig.md +0 -16
  139. package/docs/src/src/structs/JBTokenAmount.sol/struct.JBTokenAmount.md +0 -23
  140. package/docs/src/src/structs/README.md +0 -25
@@ -0,0 +1,348 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity ^0.8.6;
3
+
4
+ import {TestBaseWorkflow} from "./helpers/TestBaseWorkflow.sol";
5
+ import {JBMultiTerminal} from "../src/JBMultiTerminal.sol";
6
+ import {IJBController} from "../src/interfaces/IJBController.sol";
7
+ import {IJBRulesetApprovalHook} from "../src/interfaces/IJBRulesetApprovalHook.sol";
8
+ import {IJBTokens} from "../src/interfaces/IJBTokens.sol";
9
+ import {JBConstants} from "../src/libraries/JBConstants.sol";
10
+ import {JBAccountingContext} from "../src/structs/JBAccountingContext.sol";
11
+ import {JBFundAccessLimitGroup} from "../src/structs/JBFundAccessLimitGroup.sol";
12
+ import {JBRulesetConfig} from "../src/structs/JBRulesetConfig.sol";
13
+ import {JBRulesetMetadata} from "../src/structs/JBRulesetMetadata.sol";
14
+ import {JBSplitGroup} from "../src/structs/JBSplitGroup.sol";
15
+ import {JBTerminalConfig} from "../src/structs/JBTerminalConfig.sol";
16
+ import {MockPriceFeed} from "./mock/MockPriceFeed.sol";
17
+ import {MockERC20} from "./mock/MockERC20.sol";
18
+
19
+ /// @notice Tests for multi-terminal surplus aggregation edge cases, including cross-terminal
20
+ /// surplus with useTotalSurplusForCashOuts, cash out balance limits, and price conversion.
21
+ contract TestMultiTerminalSurplus_Local is TestBaseWorkflow {
22
+ IJBController private _controller;
23
+ JBMultiTerminal private _terminal1;
24
+ JBMultiTerminal private _terminal2;
25
+ IJBTokens private _tokens;
26
+ address private _projectOwner;
27
+ address private _user;
28
+
29
+ MockPriceFeed private _ethToUsdcFeed;
30
+ MockERC20 private _usdc;
31
+
32
+ uint32 private _nativeCurrency;
33
+ uint32 private _usdcCurrency;
34
+
35
+ uint256 private _projectId;
36
+
37
+ function setUp() public override {
38
+ super.setUp();
39
+
40
+ _projectOwner = multisig();
41
+ _user = beneficiary();
42
+ _controller = jbController();
43
+ _terminal1 = jbMultiTerminal();
44
+ _terminal2 = jbMultiTerminal2();
45
+ _tokens = jbTokens();
46
+ _usdc = usdcToken();
47
+
48
+ _nativeCurrency = uint32(uint160(JBConstants.NATIVE_TOKEN));
49
+ _usdcCurrency = uint32(uint160(address(_usdc)));
50
+
51
+ // Price feed: 1 ETH = 2000 USDC (6 decimals).
52
+ _ethToUsdcFeed = new MockPriceFeed(2000e6, 6);
53
+
54
+ // Metadata with useTotalSurplusForCashOuts = true.
55
+ JBRulesetMetadata memory _metadata = JBRulesetMetadata({
56
+ reservedPercent: 0,
57
+ cashOutTaxRate: 0,
58
+ baseCurrency: _nativeCurrency,
59
+ pausePay: false,
60
+ pauseCreditTransfers: false,
61
+ allowOwnerMinting: true,
62
+ allowSetCustomToken: true,
63
+ allowTerminalMigration: false,
64
+ allowSetTerminals: false,
65
+ allowSetController: false,
66
+ allowAddAccountingContext: true,
67
+ allowAddPriceFeed: true,
68
+ ownerMustSendPayouts: false,
69
+ holdFees: false,
70
+ useTotalSurplusForCashOuts: true,
71
+ useDataHookForPay: false,
72
+ useDataHookForCashOut: false,
73
+ dataHook: address(0),
74
+ metadata: 0
75
+ });
76
+
77
+ JBRulesetConfig[] memory _rulesetConfig = new JBRulesetConfig[](1);
78
+ _rulesetConfig[0].mustStartAtOrAfter = 0;
79
+ _rulesetConfig[0].duration = 0;
80
+ _rulesetConfig[0].weight = 1000 * 10 ** 18;
81
+ _rulesetConfig[0].weightCutPercent = 0;
82
+ _rulesetConfig[0].approvalHook = IJBRulesetApprovalHook(address(0));
83
+ _rulesetConfig[0].metadata = _metadata;
84
+ _rulesetConfig[0].splitGroups = new JBSplitGroup[](0);
85
+ _rulesetConfig[0].fundAccessLimitGroups = new JBFundAccessLimitGroup[](0);
86
+
87
+ // Terminal 1 accepts ETH; Terminal 2 accepts USDC.
88
+ JBAccountingContext[] memory _ethContext = new JBAccountingContext[](1);
89
+ _ethContext[0] = JBAccountingContext({token: JBConstants.NATIVE_TOKEN, decimals: 18, currency: _nativeCurrency});
90
+
91
+ JBAccountingContext[] memory _usdcContext = new JBAccountingContext[](1);
92
+ _usdcContext[0] = JBAccountingContext({token: address(_usdc), decimals: 6, currency: _usdcCurrency});
93
+
94
+ JBTerminalConfig[] memory _terminalConfigs = new JBTerminalConfig[](2);
95
+ _terminalConfigs[0] = JBTerminalConfig({terminal: _terminal1, accountingContextsToAccept: _ethContext});
96
+ _terminalConfigs[1] = JBTerminalConfig({terminal: _terminal2, accountingContextsToAccept: _usdcContext});
97
+
98
+ // Launch project with both terminals.
99
+ _projectId = _controller.launchProjectFor({
100
+ owner: _projectOwner,
101
+ projectUri: "multi-terminal-surplus-test",
102
+ rulesetConfigurations: _rulesetConfig,
103
+ terminalConfigurations: _terminalConfigs,
104
+ memo: ""
105
+ });
106
+
107
+ // Add price feed: USDC priced in native token terms.
108
+ vm.prank(_projectOwner);
109
+ _controller.addPriceFeed({
110
+ projectId: _projectId, pricingCurrency: _usdcCurrency, unitCurrency: _nativeCurrency, feed: _ethToUsdcFeed
111
+ });
112
+
113
+ // Deploy ERC-20 for the project.
114
+ vm.prank(_projectOwner);
115
+ _controller.deployERC20For(_projectId, "MultiTermToken", "MTT", bytes32(0));
116
+ }
117
+
118
+ /// @notice Helper: pay ETH into terminal 1.
119
+ function _payEth(address payer, uint256 amount) internal returns (uint256 tokensReceived) {
120
+ vm.deal(payer, amount);
121
+ vm.prank(payer);
122
+ tokensReceived = _terminal1.pay{value: amount}({
123
+ projectId: _projectId,
124
+ amount: amount,
125
+ token: JBConstants.NATIVE_TOKEN,
126
+ beneficiary: payer,
127
+ minReturnedTokens: 0,
128
+ memo: "",
129
+ metadata: ""
130
+ });
131
+ }
132
+
133
+ /// @notice Helper: pay USDC into terminal 2.
134
+ function _payUsdc(address payer, uint256 amount) internal returns (uint256 tokensReceived) {
135
+ _usdc.mint(payer, amount);
136
+ vm.prank(payer);
137
+ _usdc.approve(address(permit2()), amount);
138
+ vm.prank(payer);
139
+ // forge-lint: disable-next-line(unsafe-typecast)
140
+ permit2().approve(address(_usdc), address(_terminal2), uint160(amount), type(uint48).max);
141
+
142
+ vm.prank(payer);
143
+ tokensReceived = _terminal2.pay({
144
+ projectId: _projectId,
145
+ amount: amount,
146
+ token: address(_usdc),
147
+ beneficiary: payer,
148
+ minReturnedTokens: 0,
149
+ memo: "",
150
+ metadata: ""
151
+ });
152
+ }
153
+
154
+ /// @notice Surplus aggregation across 2 terminals with different tokens.
155
+ function test_surplusAcrossTwoTerminals() public {
156
+ uint256 ethAmount = 2 ether;
157
+ uint256 usdcAmount = 4000e6; // $4000 = 2 ETH
158
+
159
+ _payEth(_user, ethAmount);
160
+ _payUsdc(_user, usdcAmount);
161
+
162
+ // Check ETH-only surplus from terminal 1.
163
+ JBAccountingContext[] memory ethCtx = new JBAccountingContext[](1);
164
+ ethCtx[0] = JBAccountingContext({token: JBConstants.NATIVE_TOKEN, decimals: 18, currency: _nativeCurrency});
165
+ uint256 ethSurplus = _terminal1.currentSurplusOf(_projectId, ethCtx, 18, _nativeCurrency);
166
+ assertEq(ethSurplus, ethAmount, "terminal1 ETH surplus should match payment");
167
+
168
+ // Check USDC-only surplus from terminal 2.
169
+ JBAccountingContext[] memory usdcCtx = new JBAccountingContext[](1);
170
+ usdcCtx[0] = JBAccountingContext({token: address(_usdc), decimals: 6, currency: _usdcCurrency});
171
+ uint256 usdcSurplus = _terminal2.currentSurplusOf(_projectId, usdcCtx, 6, _usdcCurrency);
172
+ assertEq(usdcSurplus, usdcAmount, "terminal2 USDC surplus should match payment");
173
+
174
+ // Check total surplus in ETH terms from the store.
175
+ uint256 totalSurplusEth = jbTerminalStore().currentTotalSurplusOf(_projectId, 18, _nativeCurrency);
176
+ // Should be ETH amount + USDC-converted-to-ETH.
177
+ // The total should be greater than just the ETH amount.
178
+ assertGt(totalSurplusEth, ethAmount, "total surplus should include converted USDC value");
179
+ }
180
+
181
+ /// @notice With useTotalSurplusForCashOuts enabled, cash out from terminal 1 uses combined surplus.
182
+ function test_cashOutUsesTotalSurplusWhenEnabled() public {
183
+ uint256 ethAmount = 1 ether;
184
+ uint256 usdcAmount = 2000e6; // $2000 = 1 ETH
185
+
186
+ uint256 userEthTokens = _payEth(_user, ethAmount);
187
+ _payUsdc(_user, usdcAmount);
188
+
189
+ // User cashes out from terminal 1 (ETH).
190
+ // useTotalSurplusForCashOuts is enabled, so the bonding curve should consider
191
+ // the total surplus across both terminals, not just terminal 1's ETH balance.
192
+ uint256 userBalanceBefore = _user.balance;
193
+
194
+ vm.prank(_user);
195
+ uint256 reclaimed = _terminal1.cashOutTokensOf({
196
+ holder: _user,
197
+ projectId: _projectId,
198
+ cashOutCount: userEthTokens / 4, // cash out 25% of tokens from ETH payment
199
+ tokenToReclaim: JBConstants.NATIVE_TOKEN,
200
+ minTokensReclaimed: 0,
201
+ beneficiary: payable(_user),
202
+ metadata: new bytes(0)
203
+ });
204
+
205
+ // Reclaim amount should be > 0.
206
+ assertGt(reclaimed, 0, "should reclaim some ETH");
207
+
208
+ // Verify the user received ETH.
209
+ assertGt(_user.balance, userBalanceBefore, "user balance should increase");
210
+ }
211
+
212
+ /// @notice Cash out from terminal 1 cannot extract more ETH than terminal 1 holds,
213
+ /// even with useTotalSurplusForCashOuts enabled.
214
+ function test_cashOutCannotExceedTerminalBalance() public {
215
+ uint256 ethAmount = 1 ether;
216
+ uint256 usdcAmount = 20_000e6; // $20,000 = 10 ETH -- much more value than in terminal 1
217
+
218
+ uint256 userEthTokens = _payEth(_user, ethAmount);
219
+ _payUsdc(_user, usdcAmount);
220
+
221
+ // The total surplus in ETH terms is ~11 ETH, but terminal 1 only holds 1 ETH.
222
+ // Cashing out all tokens from terminal 1 should be bounded by the terminal's ETH balance.
223
+ // With cashOutTaxRate=0 and useTotalSurplusForCashOuts=true, the reclaim amount
224
+ // = totalSurplus * cashOutCount / totalSupply.
225
+ // The total token supply includes tokens from both payments.
226
+
227
+ // Get total token balance.
228
+ uint256 totalTokenBalance = _tokens.totalBalanceOf(_user, _projectId);
229
+ assertGt(totalTokenBalance, userEthTokens, "total tokens should include USDC payment tokens");
230
+
231
+ // Cash out a large fraction -- the reclaim may be limited by what terminal 1 actually holds.
232
+ // If reclaimAmount + hookAmounts > terminal balance, the store should revert.
233
+ // Let us try cashing out enough tokens that the calculated reclaim exceeds terminal 1's balance.
234
+ // With 1 ETH in terminal 1 and ~11 ETH total surplus, cashing out all tokens
235
+ // would try to reclaim ~11 ETH from a terminal that only has 1 ETH.
236
+
237
+ vm.prank(_user);
238
+ vm.expectRevert(); // Should revert with InadequateTerminalStoreBalance
239
+ _terminal1.cashOutTokensOf({
240
+ holder: _user,
241
+ projectId: _projectId,
242
+ cashOutCount: totalTokenBalance,
243
+ tokenToReclaim: JBConstants.NATIVE_TOKEN,
244
+ minTokensReclaimed: 0,
245
+ beneficiary: payable(_user),
246
+ metadata: new bytes(0)
247
+ });
248
+ }
249
+
250
+ /// @notice Surplus aggregation with price conversion rounding: small amounts.
251
+ function test_surplusAggregationRounding() public {
252
+ // Pay a very small amount of ETH.
253
+ uint256 ethAmount = 1 wei;
254
+ _payEth(_user, ethAmount);
255
+
256
+ // Pay a very small amount of USDC.
257
+ uint256 usdcAmount = 1; // 1e-6 USDC = $0.000001
258
+ _payUsdc(_user, usdcAmount);
259
+
260
+ // Total surplus should be >= the ETH amount (even if USDC rounds to 0 when converted).
261
+ uint256 totalSurplus = jbTerminalStore().currentTotalSurplusOf(_projectId, 18, _nativeCurrency);
262
+ assertGe(totalSurplus, ethAmount, "total surplus should be at least the ETH amount");
263
+ }
264
+
265
+ /// @notice Per-terminal balance tracking is independent across terminals.
266
+ function test_perTerminalBalanceIndependence() public {
267
+ uint256 ethAmount = 5 ether;
268
+ uint256 usdcAmount = 3000e6;
269
+
270
+ _payEth(_user, ethAmount);
271
+ _payUsdc(_user, usdcAmount);
272
+
273
+ // Terminal 1 balance (ETH) should be unaffected by terminal 2's USDC.
274
+ uint256 t1EthBalance = jbTerminalStore().balanceOf(address(_terminal1), _projectId, JBConstants.NATIVE_TOKEN);
275
+ assertEq(t1EthBalance, ethAmount, "terminal 1 ETH balance should match");
276
+
277
+ // Terminal 2 balance (USDC) should be unaffected by terminal 1's ETH.
278
+ uint256 t2UsdcBalance = jbTerminalStore().balanceOf(address(_terminal2), _projectId, address(_usdc));
279
+ assertEq(t2UsdcBalance, usdcAmount, "terminal 2 USDC balance should match");
280
+
281
+ // Terminal 1 should have no USDC balance.
282
+ uint256 t1UsdcBalance = jbTerminalStore().balanceOf(address(_terminal1), _projectId, address(_usdc));
283
+ assertEq(t1UsdcBalance, 0, "terminal 1 should have no USDC balance");
284
+
285
+ // Terminal 2 should have no ETH balance.
286
+ uint256 t2EthBalance = jbTerminalStore().balanceOf(address(_terminal2), _projectId, JBConstants.NATIVE_TOKEN);
287
+ assertEq(t2EthBalance, 0, "terminal 2 should have no ETH balance");
288
+ }
289
+
290
+ /// @notice Small cash out from terminal with most funds -- verifiable amount.
291
+ function test_smallCashOutFromTerminalWithFunds() public {
292
+ uint256 ethAmount = 10 ether;
293
+ uint256 usdcAmount = 2000e6; // = 1 ETH equivalent
294
+
295
+ uint256 userTokens = _payEth(_user, ethAmount);
296
+ _payUsdc(_user, usdcAmount);
297
+
298
+ // Cash out a small fraction of tokens from the ETH terminal.
299
+ uint256 cashOutCount = userTokens / 100; // 1% of ETH-minted tokens
300
+
301
+ uint256 balanceBefore = _user.balance;
302
+ vm.prank(_user);
303
+ uint256 reclaimed = _terminal1.cashOutTokensOf({
304
+ holder: _user,
305
+ projectId: _projectId,
306
+ cashOutCount: cashOutCount,
307
+ tokenToReclaim: JBConstants.NATIVE_TOKEN,
308
+ minTokensReclaimed: 0,
309
+ beneficiary: payable(_user),
310
+ metadata: new bytes(0)
311
+ });
312
+
313
+ assertGt(reclaimed, 0, "should reclaim something for 1% cash out");
314
+ assertEq(_user.balance - balanceBefore, reclaimed, "user balance increase should match reclaimed amount");
315
+
316
+ // After small cash out, terminal 1 should still have most of its ETH.
317
+ uint256 remainingBalance =
318
+ jbTerminalStore().balanceOf(address(_terminal1), _projectId, JBConstants.NATIVE_TOKEN);
319
+ assertGt(remainingBalance, 9 ether, "most of the ETH should remain");
320
+ }
321
+
322
+ /// @notice Total surplus query from terminal store matches sum of individual terminal surpluses.
323
+ function test_totalSurplusConsistency() public {
324
+ uint256 ethAmount = 3 ether;
325
+ uint256 usdcAmount = 6000e6; // $6000 = 3 ETH
326
+
327
+ _payEth(_user, ethAmount);
328
+ _payUsdc(_user, usdcAmount);
329
+
330
+ // Get individual surpluses in ETH terms.
331
+ JBAccountingContext[] memory ethCtx = new JBAccountingContext[](1);
332
+ ethCtx[0] = JBAccountingContext({token: JBConstants.NATIVE_TOKEN, decimals: 18, currency: _nativeCurrency});
333
+ uint256 t1Surplus = _terminal1.currentSurplusOf(_projectId, ethCtx, 18, _nativeCurrency);
334
+
335
+ JBAccountingContext[] memory usdcCtx = new JBAccountingContext[](1);
336
+ usdcCtx[0] = JBAccountingContext({token: address(_usdc), decimals: 6, currency: _usdcCurrency});
337
+ // Get terminal 2 surplus in ETH terms.
338
+ uint256 t2SurplusInEth = _terminal2.currentSurplusOf(_projectId, usdcCtx, 18, _nativeCurrency);
339
+
340
+ // Get total surplus from the store.
341
+ uint256 totalSurplus = jbTerminalStore().currentTotalSurplusOf(_projectId, 18, _nativeCurrency);
342
+
343
+ // Total should equal sum of individual surpluses (both converted to ETH).
344
+ assertEq(totalSurplus, t1Surplus + t2SurplusInEth, "total surplus should equal sum of terminal surpluses");
345
+ }
346
+
347
+ receive() external payable {}
348
+ }