@rev-net/core-v6 0.0.1

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 (92) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +65 -0
  3. package/REVNET_SECURITY_CHECKLIST.md +164 -0
  4. package/SECURITY.md +68 -0
  5. package/SKILLS.md +166 -0
  6. package/deployments/revnet-core-v5/arbitrum/REVDeployer.json +2821 -0
  7. package/deployments/revnet-core-v5/arbitrum/REVLoans.json +2260 -0
  8. package/deployments/revnet-core-v5/arbitrum_sepolia/REVDeployer.json +2821 -0
  9. package/deployments/revnet-core-v5/arbitrum_sepolia/REVLoans.json +2260 -0
  10. package/deployments/revnet-core-v5/base/REVDeployer.json +2825 -0
  11. package/deployments/revnet-core-v5/base/REVLoans.json +2264 -0
  12. package/deployments/revnet-core-v5/base_sepolia/REVDeployer.json +2825 -0
  13. package/deployments/revnet-core-v5/base_sepolia/REVLoans.json +2264 -0
  14. package/deployments/revnet-core-v5/ethereum/REVDeployer.json +2825 -0
  15. package/deployments/revnet-core-v5/ethereum/REVLoans.json +2264 -0
  16. package/deployments/revnet-core-v5/optimism/REVDeployer.json +2821 -0
  17. package/deployments/revnet-core-v5/optimism/REVLoans.json +2260 -0
  18. package/deployments/revnet-core-v5/optimism_sepolia/REVDeployer.json +2825 -0
  19. package/deployments/revnet-core-v5/optimism_sepolia/REVLoans.json +2264 -0
  20. package/deployments/revnet-core-v5/sepolia/REVDeployer.json +2825 -0
  21. package/deployments/revnet-core-v5/sepolia/REVLoans.json +2264 -0
  22. package/docs/book.css +13 -0
  23. package/docs/book.toml +13 -0
  24. package/docs/solidity.min.js +74 -0
  25. package/docs/src/README.md +88 -0
  26. package/docs/src/SUMMARY.md +20 -0
  27. package/docs/src/src/README.md +7 -0
  28. package/docs/src/src/REVDeployer.sol/contract.REVDeployer.md +968 -0
  29. package/docs/src/src/REVLoans.sol/contract.REVLoans.md +1047 -0
  30. package/docs/src/src/interfaces/IREVDeployer.sol/interface.IREVDeployer.md +243 -0
  31. package/docs/src/src/interfaces/IREVLoans.sol/interface.IREVLoans.md +296 -0
  32. package/docs/src/src/interfaces/README.md +5 -0
  33. package/docs/src/src/structs/README.md +14 -0
  34. package/docs/src/src/structs/REVAutoIssuance.sol/struct.REVAutoIssuance.md +19 -0
  35. package/docs/src/src/structs/REVBuybackHookConfig.sol/struct.REVBuybackHookConfig.md +19 -0
  36. package/docs/src/src/structs/REVBuybackPoolConfig.sol/struct.REVBuybackPoolConfig.md +21 -0
  37. package/docs/src/src/structs/REVConfig.sol/struct.REVConfig.md +35 -0
  38. package/docs/src/src/structs/REVCroptopAllowedPost.sol/struct.REVCroptopAllowedPost.md +28 -0
  39. package/docs/src/src/structs/REVDeploy721TiersHookConfig.sol/struct.REVDeploy721TiersHookConfig.md +34 -0
  40. package/docs/src/src/structs/REVDescription.sol/struct.REVDescription.md +23 -0
  41. package/docs/src/src/structs/REVLoan.sol/struct.REVLoan.md +28 -0
  42. package/docs/src/src/structs/REVLoanSource.sol/struct.REVLoanSource.md +16 -0
  43. package/docs/src/src/structs/REVStageConfig.sol/struct.REVStageConfig.md +44 -0
  44. package/docs/src/src/structs/REVSuckerDeploymentConfig.sol/struct.REVSuckerDeploymentConfig.md +16 -0
  45. package/foundry.lock +11 -0
  46. package/foundry.toml +23 -0
  47. package/package.json +31 -0
  48. package/remappings.txt +1 -0
  49. package/script/Deploy.s.sol +350 -0
  50. package/script/helpers/RevnetCoreDeploymentLib.sol +72 -0
  51. package/slither-ci.config.json +10 -0
  52. package/sphinx.lock +507 -0
  53. package/src/REVDeployer.sol +1257 -0
  54. package/src/REVLoans.sol +1333 -0
  55. package/src/interfaces/IREVDeployer.sol +198 -0
  56. package/src/interfaces/IREVLoans.sol +241 -0
  57. package/src/structs/REVAutoIssuance.sol +11 -0
  58. package/src/structs/REVConfig.sol +17 -0
  59. package/src/structs/REVCroptopAllowedPost.sol +20 -0
  60. package/src/structs/REVDeploy721TiersHookConfig.sol +25 -0
  61. package/src/structs/REVDescription.sol +14 -0
  62. package/src/structs/REVLoan.sol +19 -0
  63. package/src/structs/REVLoanSource.sol +11 -0
  64. package/src/structs/REVStageConfig.sol +34 -0
  65. package/src/structs/REVSuckerDeploymentConfig.sol +11 -0
  66. package/test/REV.integrations.t.sol +420 -0
  67. package/test/REVAutoIssuanceFuzz.t.sol +276 -0
  68. package/test/REVDeployerAuditRegressions.t.sol +328 -0
  69. package/test/REVInvincibility.t.sol +1275 -0
  70. package/test/REVInvincibilityHandler.sol +357 -0
  71. package/test/REVLifecycle.t.sol +364 -0
  72. package/test/REVLoans.invariants.t.sol +642 -0
  73. package/test/REVLoansAttacks.t.sol +739 -0
  74. package/test/REVLoansAuditRegressions.t.sol +314 -0
  75. package/test/REVLoansFeeRecovery.t.sol +704 -0
  76. package/test/REVLoansSourced.t.sol +1732 -0
  77. package/test/REVLoansUnSourced.t.sol +331 -0
  78. package/test/TestPR09_ConversionDocumentation.t.sol +304 -0
  79. package/test/TestPR10_LiquidationBehavior.t.sol +340 -0
  80. package/test/TestPR11_LowFindings.t.sol +571 -0
  81. package/test/TestPR12_FlashLoanSurplus.t.sol +305 -0
  82. package/test/TestPR13_CrossSourceReallocation.t.sol +302 -0
  83. package/test/TestPR15_CashOutCallerValidation.t.sol +320 -0
  84. package/test/TestPR16_ZeroRepayment.t.sol +297 -0
  85. package/test/TestPR21_Uint112Overflow.t.sol +251 -0
  86. package/test/TestPR22_HookArrayOOB.t.sol +221 -0
  87. package/test/TestPR26_BurnHeldTokens.t.sol +331 -0
  88. package/test/TestPR27_CEIPattern.t.sol +448 -0
  89. package/test/TestPR29_SwapTerminalPermission.t.sol +206 -0
  90. package/test/TestPR32_MixedFixes.t.sol +529 -0
  91. package/test/helpers/MaliciousContracts.sol +233 -0
  92. package/test/mock/MockBuybackDataHook.sol +61 -0
@@ -0,0 +1,642 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity 0.8.23;
3
+
4
+ import "forge-std/Test.sol";
5
+ import {StdInvariant} from "forge-std/StdInvariant.sol";
6
+ import /* {*} from */ "@bananapus/core-v6/test/helpers/TestBaseWorkflow.sol";
7
+ import /* {*} from "@bananapus/721-hook-v6/src/JB721TiersHookDeployer.sol";
8
+ import /* {*} from */ "./../src/REVDeployer.sol";
9
+ import /* {*} from */ "./../src/REVLoans.sol";
10
+ import "@croptop/core-v6/src/CTPublisher.sol";
11
+ import {MockBuybackDataHook} from "./mock/MockBuybackDataHook.sol";
12
+
13
+ import "@bananapus/core-v6/script/helpers/CoreDeploymentLib.sol";
14
+ import "@bananapus/721-hook-v6/script/helpers/Hook721DeploymentLib.sol";
15
+ import "@bananapus/suckers-v6/script/helpers/SuckerDeploymentLib.sol";
16
+ import "@croptop/core-v6/script/helpers/CroptopDeploymentLib.sol";
17
+ import "@bananapus/swap-terminal-v6/script/helpers/SwapTerminalDeploymentLib.sol";
18
+
19
+ import {JBCashOuts} from "@bananapus/core-v6/src/libraries/JBCashOuts.sol";
20
+ import {JBConstants} from "@bananapus/core-v6/src/libraries/JBConstants.sol";
21
+ import {JBAccountingContext} from "@bananapus/core-v6/src/structs/JBAccountingContext.sol";
22
+ import {REVLoans} from "../src/REVLoans.sol";
23
+ import {REVStageConfig, REVAutoIssuance} from "../src/structs/REVStageConfig.sol";
24
+ import {REVLoanSource} from "../src/structs/REVLoanSource.sol";
25
+ import {REVDescription} from "../src/structs/REVDescription.sol";
26
+ import {IREVLoans} from "./../src/interfaces/IREVLoans.sol";
27
+ import {JBSuckerDeployerConfig} from "@bananapus/suckers-v6/src/structs/JBSuckerDeployerConfig.sol";
28
+ import {JBSuckerRegistry} from "@bananapus/suckers-v6/src/JBSuckerRegistry.sol";
29
+ import {JB721TiersHookDeployer} from "@bananapus/721-hook-v6/src/JB721TiersHookDeployer.sol";
30
+ import {JB721TiersHook} from "@bananapus/721-hook-v6/src/JB721TiersHook.sol";
31
+ import {JB721TiersHookStore} from "@bananapus/721-hook-v6/src/JB721TiersHookStore.sol";
32
+ import {JBAddressRegistry} from "@bananapus/address-registry-v6/src/JBAddressRegistry.sol";
33
+ import {IJBAddressRegistry} from "@bananapus/address-registry-v6/src/interfaces/IJBAddressRegistry.sol";
34
+
35
+ struct FeeProjectConfig {
36
+ REVConfig configuration;
37
+ JBTerminalConfig[] terminalConfigurations;
38
+ REVSuckerDeploymentConfig suckerDeploymentConfiguration;
39
+ }
40
+
41
+ contract REVLoansCallHandler is JBTest {
42
+ uint256 public COLLATERAL_SUM;
43
+ uint256 public COLLATERAL_RETURNED;
44
+ uint256 public BORROWED_SUM;
45
+ uint256 public RUNS;
46
+ uint256 REVNET_ID;
47
+ uint256 LAST_LOAN_MODIFIED;
48
+ address USER;
49
+
50
+ IJBMultiTerminal TERMINAL;
51
+ IREVLoans LOANS;
52
+ IJBPermissions PERMS;
53
+ IJBTokens TOKENS;
54
+
55
+ constructor(
56
+ IJBMultiTerminal terminal,
57
+ IREVLoans loans,
58
+ IJBPermissions permissions,
59
+ IJBTokens tokens,
60
+ uint256 revnetId,
61
+ address beneficiary
62
+ ) {
63
+ TERMINAL = terminal;
64
+ LOANS = loans;
65
+ PERMS = permissions;
66
+ TOKENS = tokens;
67
+ REVNET_ID = revnetId;
68
+ USER = beneficiary;
69
+ }
70
+
71
+ modifier useActor() {
72
+ vm.startPrank(USER);
73
+ _;
74
+ vm.stopPrank();
75
+ }
76
+
77
+ function payBorrow(uint256 amount, uint16 prepaid) public virtual useActor {
78
+ uint256 payAmount = bound(amount, 1 ether, 10 ether);
79
+ uint256 prepaidFee = bound(uint256(prepaid), 25, 500);
80
+
81
+ vm.deal(USER, payAmount);
82
+
83
+ uint256 receivedTokens = TERMINAL.pay{value: payAmount}(REVNET_ID, JBConstants.NATIVE_TOKEN, 0, USER, 0, "", "");
84
+ uint256 borrowable =
85
+ LOANS.borrowableAmountFrom(REVNET_ID, receivedTokens, 18, uint32(uint160(JBConstants.NATIVE_TOKEN)));
86
+
87
+ // User must give the loans contract permission, similar to an "approve" call, we're just spoofing to save time.
88
+ mockExpect(
89
+ address(PERMS),
90
+ abi.encodeCall(IJBPermissions.hasPermission, (address(LOANS), USER, 2, 11, true, true)),
91
+ abi.encode(true)
92
+ );
93
+
94
+ REVLoanSource memory sauce = REVLoanSource({token: JBConstants.NATIVE_TOKEN, terminal: TERMINAL});
95
+ (, REVLoan memory lastLoan) =
96
+ LOANS.borrowFrom(REVNET_ID, sauce, borrowable, receivedTokens, payable(USER), prepaidFee);
97
+
98
+ COLLATERAL_SUM += receivedTokens;
99
+ BORROWED_SUM += lastLoan.amount;
100
+ ++RUNS;
101
+ }
102
+
103
+ function repayLoan(uint16 percentToPay, uint8 daysToFastForward) public virtual useActor {
104
+ // Skip this if there are no loans to pay down
105
+ if (RUNS == 0) {
106
+ return;
107
+ }
108
+
109
+ uint256 denominator = 10_000;
110
+ uint256 percentToPayDown = bound(percentToPay, 1000, denominator - 1);
111
+ uint256 daysToWarp = bound(daysToFastForward, 10, 100);
112
+ daysToWarp = daysToWarp * 1 days;
113
+
114
+ vm.warp(block.timestamp + daysToWarp);
115
+
116
+ // get the loan ID
117
+ uint256 id = (REVNET_ID * 1_000_000_000_000) + RUNS;
118
+ REVLoan memory latestLoan = LOANS.loanOf(id);
119
+
120
+ // skip if we don't find the loan
121
+ try IERC721(address(LOANS)).ownerOf(id) {}
122
+ catch {
123
+ return;
124
+ }
125
+
126
+ // skip if we don't find a loan
127
+ if (latestLoan.amount == 0) return;
128
+
129
+ // calc percentage to pay down
130
+ uint256 amountPaidDown;
131
+
132
+ uint256 collateralReturned = mulDiv(latestLoan.collateral, percentToPayDown, 10_000);
133
+
134
+ uint256 newCollateral = latestLoan.collateral - collateralReturned;
135
+ uint256 borrowableFromNewCollateral =
136
+ LOANS.borrowableAmountFrom(REVNET_ID, newCollateral, 18, uint32(uint160(JBConstants.NATIVE_TOKEN)));
137
+
138
+ // Needed for edge case seeds like 17721, 11407, 334
139
+ if (borrowableFromNewCollateral > 0) borrowableFromNewCollateral -= 1;
140
+
141
+ uint256 amountDiff =
142
+ borrowableFromNewCollateral > latestLoan.amount ? 0 : latestLoan.amount - borrowableFromNewCollateral;
143
+
144
+ amountPaidDown = amountDiff;
145
+
146
+ // Calculate the fee.
147
+ {
148
+ // Keep a reference to the time since the loan was created.
149
+ uint256 timeSinceLoanCreated = block.timestamp - latestLoan.createdAt;
150
+
151
+ // If the loan period has passed the prepaid time frame, take a fee.
152
+ if (timeSinceLoanCreated > latestLoan.prepaidDuration) {
153
+ // Calculate the prepaid fee for the amount being paid back.
154
+ uint256 prepaidAmount =
155
+ JBFees.feeAmountFrom({amountBeforeFee: amountDiff, feePercent: latestLoan.prepaidFeePercent});
156
+
157
+ // Calculate the fee as a linear proportion given the amount of time that has passed.
158
+ // sourceFeeAmount = mulDiv(amount, timeSinceLoanCreated, LOAN_LIQUIDATION_DURATION) - prepaidAmount;
159
+ amountPaidDown += JBFees.feeAmountFrom({
160
+ amountBeforeFee: amountDiff - prepaidAmount,
161
+ feePercent: mulDiv(timeSinceLoanCreated, JBConstants.MAX_FEE, 3650 days)
162
+ });
163
+ }
164
+ }
165
+
166
+ // empty allowance data
167
+ JBSingleAllowance memory allowance;
168
+
169
+ vm.deal(USER, type(uint256).max);
170
+ (, REVLoan memory adjustedOrNewLoan) =
171
+ LOANS.repayLoan{value: amountPaidDown}(id, amountPaidDown, collateralReturned, payable(USER), allowance);
172
+
173
+ COLLATERAL_RETURNED += collateralReturned;
174
+ COLLATERAL_SUM -= collateralReturned;
175
+ if (BORROWED_SUM >= amountDiff) BORROWED_SUM -= (latestLoan.amount - adjustedOrNewLoan.amount);
176
+ }
177
+
178
+ /// @notice Advance time by a random amount to test time-dependent behavior.
179
+ function advanceTime(uint8 daysToAdvance) public {
180
+ uint256 daysToWarp = bound(uint256(daysToAdvance), 1, 365);
181
+ vm.warp(block.timestamp + daysToWarp * 1 days);
182
+ }
183
+
184
+ /// @notice Attempt to liquidate expired loans.
185
+ function liquidateLoans(uint8 count) public {
186
+ uint256 loanCount = bound(uint256(count), 1, 10);
187
+ if (RUNS == 0) return;
188
+
189
+ uint256 startingLoanId = (REVNET_ID * 1_000_000_000_000) + 1;
190
+ try LOANS.liquidateExpiredLoansFrom(REVNET_ID, startingLoanId, loanCount) {} catch {}
191
+ }
192
+
193
+ function reallocateCollateralFromLoan(
194
+ uint16 collateralPercent,
195
+ uint256 amountToPay,
196
+ uint16 prepaid
197
+ )
198
+ public
199
+ virtual
200
+ useActor
201
+ {
202
+ // used later for the new borrow
203
+ uint256 prepaidFeePercent = bound(uint256(prepaid), 25, 500);
204
+
205
+ // Skip this if there are no loans to refinance
206
+ if (RUNS == 0) {
207
+ return;
208
+ }
209
+
210
+ // 0.0001-99%
211
+ uint256 collateralPercentToTransfer = bound(uint256(collateralPercent), 1, 9999);
212
+ amountToPay = bound(amountToPay, 10 ether, 1000e18);
213
+
214
+ // get the loan ID
215
+ uint256 id = (REVNET_ID * 1_000_000_000_000) + RUNS;
216
+
217
+ try IERC721(address(LOANS)).ownerOf(id) {}
218
+ catch {
219
+ return;
220
+ }
221
+
222
+ REVLoan memory latestLoan = LOANS.loanOf(id);
223
+
224
+ // skip if we don't find a loan
225
+ if (latestLoan.amount == 0) return;
226
+
227
+ // pay in
228
+ vm.deal(USER, amountToPay);
229
+ uint256 collateralToAdd =
230
+ TERMINAL.pay{value: amountToPay}(REVNET_ID, JBConstants.NATIVE_TOKEN, 0, USER, 0, "", "");
231
+
232
+ // 0.0001-100% in token terms
233
+ uint256 collateralToTransfer = mulDiv(latestLoan.collateral, collateralPercentToTransfer, 10_000);
234
+
235
+ // get the new amount to borrow
236
+ uint256 newAmountInFull = LOANS.borrowableAmountFrom(
237
+ REVNET_ID, collateralToTransfer + collateralToAdd, 18, uint32(uint160(JBConstants.NATIVE_TOKEN))
238
+ );
239
+
240
+ (,,, REVLoan memory newLoan) = LOANS.reallocateCollateralFromLoan(
241
+ id,
242
+ collateralToTransfer,
243
+ REVLoanSource({token: JBConstants.NATIVE_TOKEN, terminal: TERMINAL}),
244
+ newAmountInFull,
245
+ collateralToAdd,
246
+ payable(USER),
247
+ prepaidFeePercent
248
+ );
249
+
250
+ COLLATERAL_SUM += collateralToAdd;
251
+ BORROWED_SUM += (newLoan.amount);
252
+ }
253
+ }
254
+
255
+ contract InvariantREVLoansTests is StdInvariant, TestBaseWorkflow, JBTest {
256
+ // A library that parses the packed ruleset metadata into a friendlier format.
257
+ using JBRulesetMetadataResolver for JBRuleset;
258
+
259
+ /// @notice the salts that are used to deploy the contracts.
260
+ bytes32 REV_DEPLOYER_SALT = "REVDeployer";
261
+ bytes32 ERC20_SALT = "REV_TOKEN";
262
+
263
+ // Handlers
264
+ REVLoansCallHandler PAY_HANDLER;
265
+
266
+ REVDeployer REV_DEPLOYER;
267
+ JB721TiersHook EXAMPLE_HOOK;
268
+
269
+ /// @notice Deploys tiered ERC-721 hooks for revnets.
270
+ IJB721TiersHookDeployer HOOK_DEPLOYER;
271
+ IJB721TiersHookStore HOOK_STORE;
272
+ IJBAddressRegistry ADDRESS_REGISTRY;
273
+
274
+ IREVLoans LOANS_CONTRACT;
275
+
276
+ /// @notice Deploys and tracks suckers for revnets.
277
+ IJBSuckerRegistry SUCKER_REGISTRY;
278
+
279
+ CTPublisher PUBLISHER;
280
+ MockBuybackDataHook MOCK_BUYBACK;
281
+
282
+ // When the second project is deployed, track the block.timestamp.
283
+ uint256 INITIAL_TIMESTAMP;
284
+
285
+ uint256 FEE_PROJECT_ID;
286
+ uint256 REVNET_ID;
287
+
288
+ address USER = makeAddr("user");
289
+
290
+ /// @notice The address that is allowed to forward calls.
291
+ address private constant TRUSTED_FORWARDER = 0xB2b5841DBeF766d4b521221732F9B618fCf34A87;
292
+
293
+ function getFeeProjectConfig() internal view returns (FeeProjectConfig memory) {
294
+ // Define constants
295
+ string memory name = "Revnet";
296
+ string memory symbol = "$REV";
297
+ string memory projectUri = "ipfs://QmNRHT91HcDgMcenebYX7rJigt77cgNcosvuhX21wkF3tx";
298
+ uint8 decimals = 18;
299
+ uint256 decimalMultiplier = 10 ** decimals;
300
+
301
+ // The tokens that the project accepts and stores.
302
+ JBAccountingContext[] memory accountingContextsToAccept = new JBAccountingContext[](1);
303
+
304
+ // Accept the chain's native currency through the multi terminal.
305
+ accountingContextsToAccept[0] = JBAccountingContext({
306
+ token: JBConstants.NATIVE_TOKEN, decimals: 18, currency: uint32(uint160(JBConstants.NATIVE_TOKEN))
307
+ });
308
+
309
+ // The terminals that the project will accept funds through.
310
+ JBTerminalConfig[] memory terminalConfigurations = new JBTerminalConfig[](1);
311
+ terminalConfigurations[0] =
312
+ JBTerminalConfig({terminal: jbMultiTerminal(), accountingContextsToAccept: accountingContextsToAccept});
313
+
314
+ JBSplit[] memory splits = new JBSplit[](1);
315
+ splits[0].beneficiary = payable(multisig());
316
+ splits[0].percent = 10_000;
317
+
318
+ // The project's revnet stage configurations.
319
+ REVStageConfig[] memory stageConfigurations = new REVStageConfig[](3);
320
+
321
+ {
322
+ REVAutoIssuance[] memory issuanceConfs = new REVAutoIssuance[](1);
323
+ issuanceConfs[0] = REVAutoIssuance({
324
+ chainId: uint32(block.chainid), count: uint104(70_000 * decimalMultiplier), beneficiary: multisig()
325
+ });
326
+
327
+ stageConfigurations[0] = REVStageConfig({
328
+ startsAtOrAfter: uint40(block.timestamp),
329
+ autoIssuances: issuanceConfs,
330
+ splitPercent: 2000, // 20%
331
+ splits: splits,
332
+ initialIssuance: uint112(1000 * decimalMultiplier),
333
+ issuanceCutFrequency: 90 days,
334
+ issuanceCutPercent: JBConstants.MAX_WEIGHT_CUT_PERCENT / 2,
335
+ cashOutTaxRate: 6000, // 0.6
336
+ extraMetadata: 0
337
+ });
338
+ }
339
+
340
+ stageConfigurations[1] = REVStageConfig({
341
+ startsAtOrAfter: uint40(stageConfigurations[0].startsAtOrAfter + 720 days),
342
+ autoIssuances: new REVAutoIssuance[](0),
343
+ splitPercent: 2000, // 20%
344
+ splits: splits,
345
+ initialIssuance: 0, // inherit from previous cycle.
346
+ issuanceCutFrequency: 180 days,
347
+ issuanceCutPercent: JBConstants.MAX_WEIGHT_CUT_PERCENT / 2,
348
+ cashOutTaxRate: 1000, //0.1
349
+ extraMetadata: 0
350
+ });
351
+
352
+ stageConfigurations[2] = REVStageConfig({
353
+ startsAtOrAfter: uint40(stageConfigurations[1].startsAtOrAfter + (20 * 365 days)),
354
+ autoIssuances: new REVAutoIssuance[](0),
355
+ splitPercent: 0,
356
+ splits: splits,
357
+ initialIssuance: 1,
358
+ issuanceCutFrequency: 0,
359
+ issuanceCutPercent: 0,
360
+ cashOutTaxRate: 500, // 0.05
361
+ extraMetadata: 0
362
+ });
363
+
364
+ // The project's revnet configuration
365
+ REVConfig memory revnetConfiguration = REVConfig({
366
+ description: REVDescription(name, symbol, projectUri, ERC20_SALT),
367
+ baseCurrency: uint32(uint160(JBConstants.NATIVE_TOKEN)),
368
+ splitOperator: multisig(),
369
+ stageConfigurations: stageConfigurations
370
+ });
371
+
372
+ return FeeProjectConfig({
373
+ configuration: revnetConfiguration,
374
+ terminalConfigurations: terminalConfigurations,
375
+ suckerDeploymentConfiguration: REVSuckerDeploymentConfig({
376
+ deployerConfigurations: new JBSuckerDeployerConfig[](0), salt: keccak256(abi.encodePacked("REV"))
377
+ })
378
+ });
379
+ }
380
+
381
+ function getSecondProjectConfig() internal view returns (FeeProjectConfig memory) {
382
+ // Define constants
383
+ string memory name = "NANA";
384
+ string memory symbol = "$NANA";
385
+ string memory projectUri = "ipfs://QmNRHT91HcDgMcenebYX7rJigt77cgNxosvuhX21wkF3tx";
386
+ uint8 decimals = 18;
387
+ uint256 decimalMultiplier = 10 ** decimals;
388
+
389
+ // The tokens that the project accepts and stores.
390
+ JBAccountingContext[] memory accountingContextsToAccept = new JBAccountingContext[](1);
391
+
392
+ // Accept the chain's native currency through the multi terminal.
393
+ accountingContextsToAccept[0] = JBAccountingContext({
394
+ token: JBConstants.NATIVE_TOKEN, decimals: 18, currency: uint32(uint160(JBConstants.NATIVE_TOKEN))
395
+ });
396
+
397
+ // The terminals that the project will accept funds through.
398
+ JBTerminalConfig[] memory terminalConfigurations = new JBTerminalConfig[](1);
399
+ terminalConfigurations[0] =
400
+ JBTerminalConfig({terminal: jbMultiTerminal(), accountingContextsToAccept: accountingContextsToAccept});
401
+
402
+ JBSplit[] memory splits = new JBSplit[](1);
403
+ splits[0].beneficiary = payable(multisig());
404
+ splits[0].percent = 10_000;
405
+
406
+ // The project's revnet stage configurations.
407
+ REVStageConfig[] memory stageConfigurations = new REVStageConfig[](3);
408
+
409
+ {
410
+ REVAutoIssuance[] memory issuanceConfs = new REVAutoIssuance[](1);
411
+ issuanceConfs[0] = REVAutoIssuance({
412
+ chainId: uint32(block.chainid), count: uint104(70_000 * decimalMultiplier), beneficiary: multisig()
413
+ });
414
+
415
+ stageConfigurations[0] = REVStageConfig({
416
+ startsAtOrAfter: uint40(block.timestamp),
417
+ autoIssuances: issuanceConfs,
418
+ splitPercent: 2000, // 20%
419
+ splits: splits,
420
+ initialIssuance: uint112(1000 * decimalMultiplier),
421
+ issuanceCutFrequency: 90 days,
422
+ issuanceCutPercent: JBConstants.MAX_WEIGHT_CUT_PERCENT / 2,
423
+ cashOutTaxRate: 6000, // 0.6
424
+ extraMetadata: 0
425
+ });
426
+ }
427
+
428
+ stageConfigurations[1] = REVStageConfig({
429
+ startsAtOrAfter: uint40(stageConfigurations[0].startsAtOrAfter + 365 days),
430
+ autoIssuances: new REVAutoIssuance[](0),
431
+ splitPercent: 9000, // 90%
432
+ splits: splits,
433
+ initialIssuance: 0, // this is a special number that is as close to max price as we can get.
434
+ issuanceCutFrequency: 180 days,
435
+ issuanceCutPercent: JBConstants.MAX_WEIGHT_CUT_PERCENT / 2,
436
+ cashOutTaxRate: 0, // 0.0%
437
+ extraMetadata: 0
438
+ });
439
+
440
+ stageConfigurations[2] = REVStageConfig({
441
+ startsAtOrAfter: uint40(stageConfigurations[1].startsAtOrAfter + (20 * 365 days)),
442
+ autoIssuances: new REVAutoIssuance[](0),
443
+ splitPercent: 0,
444
+ splits: splits,
445
+ initialIssuance: 0, // this is a special number that is as close to max price as we can get.
446
+ issuanceCutFrequency: 0,
447
+ issuanceCutPercent: 0,
448
+ cashOutTaxRate: 500, // 0.05
449
+ extraMetadata: 0
450
+ });
451
+
452
+ // The project's revnet configuration
453
+ REVConfig memory revnetConfiguration = REVConfig({
454
+ description: REVDescription(name, symbol, projectUri, "NANA_TOKEN"),
455
+ baseCurrency: uint32(uint160(JBConstants.NATIVE_TOKEN)),
456
+ splitOperator: multisig(),
457
+ stageConfigurations: stageConfigurations
458
+ });
459
+
460
+ return FeeProjectConfig({
461
+ configuration: revnetConfiguration,
462
+ terminalConfigurations: terminalConfigurations,
463
+ suckerDeploymentConfiguration: REVSuckerDeploymentConfig({
464
+ deployerConfigurations: new JBSuckerDeployerConfig[](0), salt: keccak256(abi.encodePacked("NANA"))
465
+ })
466
+ });
467
+ }
468
+
469
+ function setUp() public override {
470
+ super.setUp();
471
+
472
+ FEE_PROJECT_ID = jbProjects().createFor(multisig());
473
+
474
+ SUCKER_REGISTRY = new JBSuckerRegistry(jbDirectory(), jbPermissions(), multisig(), address(0));
475
+
476
+ HOOK_STORE = new JB721TiersHookStore();
477
+
478
+ EXAMPLE_HOOK = new JB721TiersHook(jbDirectory(), jbPermissions(), jbRulesets(), HOOK_STORE, multisig());
479
+
480
+ ADDRESS_REGISTRY = new JBAddressRegistry();
481
+
482
+ HOOK_DEPLOYER = new JB721TiersHookDeployer(EXAMPLE_HOOK, HOOK_STORE, ADDRESS_REGISTRY, multisig());
483
+
484
+ PUBLISHER = new CTPublisher(jbDirectory(), jbPermissions(), FEE_PROJECT_ID, multisig());
485
+ MOCK_BUYBACK = new MockBuybackDataHook();
486
+
487
+ LOANS_CONTRACT = new REVLoans({
488
+ controller: jbController(),
489
+ projects: jbProjects(),
490
+ revId: FEE_PROJECT_ID,
491
+ owner: address(this),
492
+ permit2: permit2(),
493
+ trustedForwarder: TRUSTED_FORWARDER
494
+ });
495
+
496
+ REV_DEPLOYER = new REVDeployer{salt: REV_DEPLOYER_SALT}(
497
+ jbController(),
498
+ SUCKER_REGISTRY,
499
+ FEE_PROJECT_ID,
500
+ HOOK_DEPLOYER,
501
+ PUBLISHER,
502
+ IJBRulesetDataHook(address(MOCK_BUYBACK)),
503
+ address(LOANS_CONTRACT),
504
+ TRUSTED_FORWARDER
505
+ );
506
+
507
+ // Approve the basic deployer to configure the project.
508
+ vm.prank(address(multisig()));
509
+ jbProjects().approve(address(REV_DEPLOYER), FEE_PROJECT_ID);
510
+
511
+ // Build the config.
512
+ FeeProjectConfig memory feeProjectConfig = getFeeProjectConfig();
513
+
514
+ // Configure the project.
515
+ vm.prank(address(multisig()));
516
+ REVNET_ID = REV_DEPLOYER.deployFor({
517
+ revnetId: FEE_PROJECT_ID, // Zero to deploy a new revnet
518
+ configuration: feeProjectConfig.configuration,
519
+ terminalConfigurations: feeProjectConfig.terminalConfigurations,
520
+ suckerDeploymentConfiguration: feeProjectConfig.suckerDeploymentConfiguration
521
+ });
522
+
523
+ // Configure second revnet
524
+ FeeProjectConfig memory fee2Config = getSecondProjectConfig();
525
+
526
+ // Configure the second project.
527
+ REVNET_ID = REV_DEPLOYER.deployFor({
528
+ revnetId: 0, // Zero to deploy a new revnet
529
+ configuration: fee2Config.configuration,
530
+ terminalConfigurations: fee2Config.terminalConfigurations,
531
+ suckerDeploymentConfiguration: fee2Config.suckerDeploymentConfiguration
532
+ });
533
+
534
+ INITIAL_TIMESTAMP = block.timestamp;
535
+
536
+ // Deploy handlers and assign them as targets
537
+ PAY_HANDLER =
538
+ new REVLoansCallHandler(jbMultiTerminal(), LOANS_CONTRACT, jbPermissions(), jbTokens(), REVNET_ID, USER);
539
+
540
+ // Calls to perform via the handler
541
+ bytes4[] memory selectors = new bytes4[](5);
542
+ selectors[0] = REVLoansCallHandler.payBorrow.selector;
543
+ selectors[1] = REVLoansCallHandler.repayLoan.selector;
544
+ selectors[2] = REVLoansCallHandler.reallocateCollateralFromLoan.selector;
545
+ selectors[3] = REVLoansCallHandler.advanceTime.selector;
546
+ selectors[4] = REVLoansCallHandler.liquidateLoans.selector;
547
+
548
+ targetContract(address(PAY_HANDLER));
549
+ targetSelector(FuzzSelector({addr: address(PAY_HANDLER), selectors: selectors}));
550
+ }
551
+
552
+ function invariant_A_User_Balance_And_Collateral() public view {
553
+ IJBToken token = jbTokens().tokenOf(REVNET_ID);
554
+
555
+ uint256 userTokenBalance = token.balanceOf(USER);
556
+ if (PAY_HANDLER.RUNS() > 0) assertGe(userTokenBalance, PAY_HANDLER.COLLATERAL_RETURNED());
557
+
558
+ // Ensure REVLoans and our handler/user have the same provided collateral amounts.
559
+ assertEq(PAY_HANDLER.COLLATERAL_SUM(), LOANS_CONTRACT.totalCollateralOf(REVNET_ID));
560
+ }
561
+
562
+ function invariant_B_TotalBorrowed() public view {
563
+ uint256 expectedTotalBorrowed = PAY_HANDLER.BORROWED_SUM();
564
+
565
+ // Get the actual total borrowed amount from the contract
566
+ uint256 actualTotalBorrowed = _getTotalBorrowedFromContract(REVNET_ID);
567
+
568
+ // Assert that the expected and actual total borrowed amounts match
569
+ assertEq(actualTotalBorrowed, expectedTotalBorrowed, "Total borrowed amount mismatch");
570
+ }
571
+
572
+ function _calculateExpectedTotalBorrowed(uint256 _revnetId) internal view returns (uint256 totalBorrowed) {
573
+ // Access loan sources from the Loans contract
574
+ REVLoanSource[] memory sources = LOANS_CONTRACT.loanSourcesOf(_revnetId);
575
+
576
+ // Iterate through loan sources to calculate the total borrowed amount
577
+ for (uint256 i = 0; i < sources.length; i++) {
578
+ totalBorrowed += LOANS_CONTRACT.totalBorrowedFrom(_revnetId, sources[i].terminal, sources[i].token);
579
+ }
580
+ }
581
+
582
+ function _getTotalBorrowedFromContract(uint256 _revnetId) internal view returns (uint256) {
583
+ return LOANS_CONTRACT.totalBorrowedFrom(_revnetId, jbMultiTerminal(), JBConstants.NATIVE_TOKEN);
584
+ }
585
+
586
+ /// @notice INV-RL-3: loan.amount <= type(uint112).max for all active loans (C-1 regression).
587
+ /// @dev Verifies that no loan amount exceeds the uint112 storage boundary.
588
+ function invariant_C_LoanAmountFitsUint112() public view {
589
+ if (PAY_HANDLER.RUNS() == 0) return;
590
+
591
+ for (uint256 i = 1; i <= PAY_HANDLER.RUNS(); i++) {
592
+ uint256 loanId = (REVNET_ID * 1_000_000_000_000) + i;
593
+
594
+ // Skip if loan was liquidated/burned
595
+ try IERC721(address(LOANS_CONTRACT)).ownerOf(loanId) {}
596
+ catch {
597
+ continue;
598
+ }
599
+
600
+ REVLoan memory loan = LOANS_CONTRACT.loanOf(loanId);
601
+ if (loan.amount == 0) continue;
602
+
603
+ assertLe(uint256(loan.amount), uint256(type(uint112).max), "INV-RL-3: loan.amount must fit in uint112");
604
+ assertLe(
605
+ uint256(loan.collateral), uint256(type(uint112).max), "INV-RL-3: loan.collateral must fit in uint112"
606
+ );
607
+ }
608
+ }
609
+
610
+ /// @notice INV-RL-4: Active loans always have non-zero collateral.
611
+ /// @dev Borrowable amounts can decrease as the revnet evolves (new payments change the
612
+ /// cashout curve), so we only check that collateral > 0 for active loans.
613
+ function invariant_D_ActiveLoansHaveCollateral() public view {
614
+ if (PAY_HANDLER.RUNS() == 0) return;
615
+
616
+ for (uint256 i = 1; i <= PAY_HANDLER.RUNS(); i++) {
617
+ uint256 loanId = (REVNET_ID * 1_000_000_000_000) + i;
618
+
619
+ // Skip if loan was liquidated/burned
620
+ try IERC721(address(LOANS_CONTRACT)).ownerOf(loanId) {}
621
+ catch {
622
+ continue;
623
+ }
624
+
625
+ REVLoan memory loan = LOANS_CONTRACT.loanOf(loanId);
626
+ if (loan.amount == 0) continue;
627
+
628
+ // Active loans must have non-zero collateral backing them.
629
+ assertGt(uint256(loan.collateral), 0, "INV-RL-4: Active loan must have non-zero collateral");
630
+ }
631
+ }
632
+
633
+ /// @notice INV-RL-5: Total collateral tracked by handler matches contract state.
634
+ /// @dev This is already checked by invariant_A, but we add an explicit named check.
635
+ function invariant_E_CollateralConsistency() public view {
636
+ assertEq(
637
+ PAY_HANDLER.COLLATERAL_SUM(),
638
+ LOANS_CONTRACT.totalCollateralOf(REVNET_ID),
639
+ "INV-RL-5: Handler collateral sum must match contract totalCollateralOf"
640
+ );
641
+ }
642
+ }