@rev-net/core-v6 0.0.36 → 0.0.39

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