@rev-net/core-v6 0.0.11 → 0.0.13
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.
- package/ADMINISTRATION.md +7 -7
- package/ARCHITECTURE.md +11 -11
- package/AUDIT_INSTRUCTIONS.md +295 -0
- package/CHANGE_LOG.md +316 -0
- package/README.md +9 -6
- package/RISKS.md +180 -35
- package/SKILLS.md +9 -11
- package/STYLE_GUIDE.md +14 -1
- package/USER_JOURNEYS.md +489 -0
- package/package.json +9 -9
- package/script/Deploy.s.sol +124 -40
- package/script/helpers/RevnetCoreDeploymentLib.sol +19 -6
- package/src/REVDeployer.sol +183 -175
- package/src/REVLoans.sol +65 -28
- package/src/interfaces/IREVDeployer.sol +25 -23
- package/src/structs/REV721TiersHookFlags.sol +1 -0
- package/src/structs/REVAutoIssuance.sol +1 -0
- package/src/structs/REVBaseline721HookConfig.sol +1 -0
- package/src/structs/REVConfig.sol +1 -0
- package/src/structs/REVCroptopAllowedPost.sol +1 -0
- package/src/structs/REVDeploy721TiersHookConfig.sol +13 -14
- package/src/structs/REVDescription.sol +1 -0
- package/src/structs/REVLoan.sol +1 -0
- package/src/structs/REVLoanSource.sol +1 -0
- package/src/structs/REVStageConfig.sol +1 -0
- package/src/structs/REVSuckerDeploymentConfig.sol +1 -0
- package/test/REV.integrations.t.sol +148 -19
- package/test/REVAutoIssuanceFuzz.t.sol +31 -6
- package/test/REVDeployerRegressions.t.sol +47 -9
- package/test/REVInvincibility.t.sol +83 -19
- package/test/REVInvincibilityHandler.sol +29 -0
- package/test/REVLifecycle.t.sol +36 -6
- package/test/REVLoans.invariants.t.sol +64 -10
- package/test/REVLoansAttacks.t.sol +54 -9
- package/test/REVLoansFeeRecovery.t.sol +61 -15
- package/test/REVLoansFindings.t.sol +42 -9
- package/test/REVLoansRegressions.t.sol +33 -6
- package/test/REVLoansSourceFeeRecovery.t.sol +491 -0
- package/test/REVLoansSourced.t.sol +79 -17
- package/test/REVLoansUnSourced.t.sol +61 -10
- package/test/TestBurnHeldTokens.t.sol +47 -11
- package/test/TestCEIPattern.t.sol +37 -6
- package/test/TestCashOutCallerValidation.t.sol +41 -8
- package/test/TestConversionDocumentation.t.sol +50 -13
- package/test/TestCrossCurrencyReclaim.t.sol +584 -0
- package/test/TestCrossSourceReallocation.t.sol +37 -6
- package/test/TestERC2771MetaTx.t.sol +557 -0
- package/test/TestEmptyBuybackSpecs.t.sol +45 -10
- package/test/TestFlashLoanSurplus.t.sol +39 -7
- package/test/TestHookArrayOOB.t.sol +42 -13
- package/test/TestLiquidationBehavior.t.sol +37 -7
- package/test/TestLoanSourceRotation.t.sol +525 -0
- package/test/TestLongTailEconomics.t.sol +651 -0
- package/test/TestLowFindings.t.sol +80 -8
- package/test/TestMixedFixes.t.sol +43 -9
- package/test/TestPermit2Signatures.t.sol +657 -0
- package/test/TestReallocationSandwich.t.sol +384 -0
- package/test/TestRevnetRegressions.t.sol +324 -0
- package/test/TestSplitWeightAdjustment.t.sol +52 -13
- package/test/TestSplitWeightE2E.t.sol +53 -18
- package/test/TestSplitWeightFork.t.sol +66 -21
- package/test/TestStageTransitionBorrowable.t.sol +38 -6
- package/test/TestSwapTerminalPermission.t.sol +37 -7
- package/test/TestUint112Overflow.t.sol +39 -6
- package/test/TestZeroRepayment.t.sol +37 -6
- package/test/fork/ForkTestBase.sol +66 -17
- package/test/fork/TestCashOutFork.t.sol +9 -3
- package/test/fork/TestLoanBorrowFork.t.sol +1 -0
- package/test/fork/TestLoanCrossRulesetFork.t.sol +11 -3
- package/test/fork/TestLoanLiquidationFork.t.sol +1 -0
- package/test/fork/TestLoanReallocateFork.t.sol +1 -0
- package/test/fork/TestLoanRepayFork.t.sol +1 -0
- package/test/fork/TestLoanTransferFork.t.sol +133 -0
- package/test/fork/TestSplitWeightFork.t.sol +3 -0
- package/test/helpers/REVEmpty721Config.sol +46 -0
- package/test/mock/MockBuybackDataHook.sol +1 -0
- package/test/regression/TestBurnPermissionRequired.t.sol +267 -0
- package/test/regression/TestCrossRevnetLiquidation.t.sol +228 -0
- package/test/regression/TestCumulativeLoanCounter.t.sol +38 -8
- package/test/regression/TestLiquidateGapHandling.t.sol +40 -8
- package/test/regression/TestZeroPriceFeed.t.sol +396 -0
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
pragma solidity 0.8.26;
|
|
3
|
+
|
|
4
|
+
// forge-lint: disable-next-line(unaliased-plain-import)
|
|
5
|
+
import "forge-std/Test.sol";
|
|
6
|
+
// forge-lint: disable-next-line(unaliased-plain-import)
|
|
7
|
+
import /* {*} from */ "@bananapus/core-v6/test/helpers/TestBaseWorkflow.sol";
|
|
8
|
+
// forge-lint: disable-next-line(unaliased-plain-import)
|
|
9
|
+
import /* {*} from */ "./../src/REVDeployer.sol";
|
|
10
|
+
// forge-lint: disable-next-line(unaliased-plain-import)
|
|
11
|
+
import "@croptop/core-v6/src/CTPublisher.sol";
|
|
12
|
+
import {MockBuybackDataHook} from "./mock/MockBuybackDataHook.sol";
|
|
13
|
+
// forge-lint: disable-next-line(unaliased-plain-import)
|
|
14
|
+
import "@bananapus/core-v6/script/helpers/CoreDeploymentLib.sol";
|
|
15
|
+
// forge-lint: disable-next-line(unaliased-plain-import)
|
|
16
|
+
import "@bananapus/721-hook-v6/script/helpers/Hook721DeploymentLib.sol";
|
|
17
|
+
// forge-lint: disable-next-line(unaliased-plain-import)
|
|
18
|
+
import "@bananapus/suckers-v6/script/helpers/SuckerDeploymentLib.sol";
|
|
19
|
+
// forge-lint: disable-next-line(unaliased-plain-import)
|
|
20
|
+
import "@croptop/core-v6/script/helpers/CroptopDeploymentLib.sol";
|
|
21
|
+
// forge-lint: disable-next-line(unaliased-plain-import)
|
|
22
|
+
import "@bananapus/router-terminal-v6/script/helpers/RouterTerminalDeploymentLib.sol";
|
|
23
|
+
import {JBConstants} from "@bananapus/core-v6/src/libraries/JBConstants.sol";
|
|
24
|
+
import {JBAccountingContext} from "@bananapus/core-v6/src/structs/JBAccountingContext.sol";
|
|
25
|
+
import {JBPermissionIds} from "@bananapus/permission-ids-v6/src/JBPermissionIds.sol";
|
|
26
|
+
import {REVLoans} from "../src/REVLoans.sol";
|
|
27
|
+
import {REVLoan} from "../src/structs/REVLoan.sol";
|
|
28
|
+
import {REVStageConfig, REVAutoIssuance} from "../src/structs/REVStageConfig.sol";
|
|
29
|
+
import {REVLoanSource} from "../src/structs/REVLoanSource.sol";
|
|
30
|
+
import {REVDescription} from "../src/structs/REVDescription.sol";
|
|
31
|
+
import {IREVLoans} from "./../src/interfaces/IREVLoans.sol";
|
|
32
|
+
import {JBSuckerDeployerConfig} from "@bananapus/suckers-v6/src/structs/JBSuckerDeployerConfig.sol";
|
|
33
|
+
import {JBSuckerRegistry} from "@bananapus/suckers-v6/src/JBSuckerRegistry.sol";
|
|
34
|
+
import {JB721TiersHookDeployer} from "@bananapus/721-hook-v6/src/JB721TiersHookDeployer.sol";
|
|
35
|
+
import {JB721TiersHook} from "@bananapus/721-hook-v6/src/JB721TiersHook.sol";
|
|
36
|
+
import {JB721TiersHookStore} from "@bananapus/721-hook-v6/src/JB721TiersHookStore.sol";
|
|
37
|
+
import {JBAddressRegistry} from "@bananapus/address-registry-v6/src/JBAddressRegistry.sol";
|
|
38
|
+
import {IJBAddressRegistry} from "@bananapus/address-registry-v6/src/interfaces/IJBAddressRegistry.sol";
|
|
39
|
+
import {REVEmpty721Config} from "./helpers/REVEmpty721Config.sol";
|
|
40
|
+
|
|
41
|
+
/// @notice Deterministic test for the reallocation sandwich at stage boundaries.
|
|
42
|
+
/// Documents that a borrower can extract additional value by calling `reallocateCollateralFromLoan` immediately
|
|
43
|
+
/// after a stage transition that lowers the `cashOutTaxRate`. This is intentional design behavior:
|
|
44
|
+
/// loan value tracks the current bonding curve parameters, and stage boundaries are immutable and predictable.
|
|
45
|
+
/// See RISKS.md section 3 ("reallocateCollateralFromLoan sandwich potential") for details.
|
|
46
|
+
contract TestReallocationSandwich is TestBaseWorkflow {
|
|
47
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
48
|
+
bytes32 REV_DEPLOYER_SALT = "REVDeployer";
|
|
49
|
+
|
|
50
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
51
|
+
REVDeployer REV_DEPLOYER;
|
|
52
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
53
|
+
JB721TiersHook EXAMPLE_HOOK;
|
|
54
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
55
|
+
IJB721TiersHookDeployer HOOK_DEPLOYER;
|
|
56
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
57
|
+
IJB721TiersHookStore HOOK_STORE;
|
|
58
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
59
|
+
IJBAddressRegistry ADDRESS_REGISTRY;
|
|
60
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
61
|
+
IREVLoans LOANS_CONTRACT;
|
|
62
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
63
|
+
IJBSuckerRegistry SUCKER_REGISTRY;
|
|
64
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
65
|
+
CTPublisher PUBLISHER;
|
|
66
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
67
|
+
MockBuybackDataHook MOCK_BUYBACK;
|
|
68
|
+
|
|
69
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
70
|
+
uint256 FEE_PROJECT_ID;
|
|
71
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
72
|
+
uint256 REVNET_ID;
|
|
73
|
+
|
|
74
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
75
|
+
address BORROWER = makeAddr("borrower");
|
|
76
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
77
|
+
address OTHER_PAYER = makeAddr("otherPayer");
|
|
78
|
+
|
|
79
|
+
address private constant TRUSTED_FORWARDER = 0xB2b5841DBeF766d4b521221732F9B618fCf34A87;
|
|
80
|
+
|
|
81
|
+
/// @notice Stage 1 starts now with 60% cashOutTaxRate, stage 2 starts after 30 days with 20% cashOutTaxRate.
|
|
82
|
+
function _buildConfig()
|
|
83
|
+
internal
|
|
84
|
+
view
|
|
85
|
+
returns (REVConfig memory cfg, JBTerminalConfig[] memory tc, REVSuckerDeploymentConfig memory sdc)
|
|
86
|
+
{
|
|
87
|
+
JBAccountingContext[] memory acc = new JBAccountingContext[](1);
|
|
88
|
+
acc[0] = JBAccountingContext({
|
|
89
|
+
token: JBConstants.NATIVE_TOKEN, decimals: 18, currency: uint32(uint160(JBConstants.NATIVE_TOKEN))
|
|
90
|
+
});
|
|
91
|
+
tc = new JBTerminalConfig[](1);
|
|
92
|
+
tc[0] = JBTerminalConfig({terminal: jbMultiTerminal(), accountingContextsToAccept: acc});
|
|
93
|
+
|
|
94
|
+
REVStageConfig[] memory stages = new REVStageConfig[](2);
|
|
95
|
+
JBSplit[] memory splits = new JBSplit[](1);
|
|
96
|
+
splits[0].beneficiary = payable(multisig());
|
|
97
|
+
splits[0].percent = 10_000;
|
|
98
|
+
|
|
99
|
+
// Stage 1: high cashOutTaxRate (60%)
|
|
100
|
+
stages[0] = REVStageConfig({
|
|
101
|
+
startsAtOrAfter: uint40(block.timestamp),
|
|
102
|
+
autoIssuances: new REVAutoIssuance[](0),
|
|
103
|
+
splitPercent: 0,
|
|
104
|
+
splits: splits,
|
|
105
|
+
initialIssuance: uint112(1000e18),
|
|
106
|
+
issuanceCutFrequency: 0,
|
|
107
|
+
issuanceCutPercent: 0,
|
|
108
|
+
cashOutTaxRate: 6000, // 60%
|
|
109
|
+
extraMetadata: 0
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
// Stage 2: low cashOutTaxRate (20%) -- starts after 30 days
|
|
113
|
+
stages[1] = REVStageConfig({
|
|
114
|
+
startsAtOrAfter: uint40(block.timestamp + 30 days),
|
|
115
|
+
autoIssuances: new REVAutoIssuance[](0),
|
|
116
|
+
splitPercent: 0,
|
|
117
|
+
splits: splits,
|
|
118
|
+
initialIssuance: uint112(1000e18),
|
|
119
|
+
issuanceCutFrequency: 0,
|
|
120
|
+
issuanceCutPercent: 0,
|
|
121
|
+
cashOutTaxRate: 2000, // 20%
|
|
122
|
+
extraMetadata: 0
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
cfg = REVConfig({
|
|
126
|
+
// forge-lint: disable-next-line(named-struct-fields)
|
|
127
|
+
description: REVDescription("SandwichTest", "SWT", "ipfs://sandwich", "SWT_SALT"),
|
|
128
|
+
baseCurrency: uint32(uint160(JBConstants.NATIVE_TOKEN)),
|
|
129
|
+
splitOperator: multisig(),
|
|
130
|
+
stageConfigurations: stages
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
sdc = REVSuckerDeploymentConfig({
|
|
134
|
+
deployerConfigurations: new JBSuckerDeployerConfig[](0), salt: keccak256(abi.encodePacked("SWT"))
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function setUp() public override {
|
|
139
|
+
super.setUp();
|
|
140
|
+
FEE_PROJECT_ID = jbProjects().createFor(multisig());
|
|
141
|
+
SUCKER_REGISTRY = new JBSuckerRegistry(jbDirectory(), jbPermissions(), multisig(), address(0));
|
|
142
|
+
HOOK_STORE = new JB721TiersHookStore();
|
|
143
|
+
EXAMPLE_HOOK = new JB721TiersHook(
|
|
144
|
+
jbDirectory(), jbPermissions(), jbPrices(), jbRulesets(), HOOK_STORE, jbSplits(), multisig()
|
|
145
|
+
);
|
|
146
|
+
ADDRESS_REGISTRY = new JBAddressRegistry();
|
|
147
|
+
HOOK_DEPLOYER = new JB721TiersHookDeployer(EXAMPLE_HOOK, HOOK_STORE, ADDRESS_REGISTRY, multisig());
|
|
148
|
+
PUBLISHER = new CTPublisher(jbDirectory(), jbPermissions(), FEE_PROJECT_ID, multisig());
|
|
149
|
+
MOCK_BUYBACK = new MockBuybackDataHook();
|
|
150
|
+
LOANS_CONTRACT = new REVLoans({
|
|
151
|
+
controller: jbController(),
|
|
152
|
+
projects: jbProjects(),
|
|
153
|
+
revId: FEE_PROJECT_ID,
|
|
154
|
+
owner: address(this),
|
|
155
|
+
permit2: permit2(),
|
|
156
|
+
trustedForwarder: TRUSTED_FORWARDER
|
|
157
|
+
});
|
|
158
|
+
REV_DEPLOYER = new REVDeployer{salt: REV_DEPLOYER_SALT}(
|
|
159
|
+
jbController(),
|
|
160
|
+
SUCKER_REGISTRY,
|
|
161
|
+
FEE_PROJECT_ID,
|
|
162
|
+
HOOK_DEPLOYER,
|
|
163
|
+
PUBLISHER,
|
|
164
|
+
IJBBuybackHookRegistry(address(MOCK_BUYBACK)),
|
|
165
|
+
address(LOANS_CONTRACT),
|
|
166
|
+
TRUSTED_FORWARDER
|
|
167
|
+
);
|
|
168
|
+
vm.prank(multisig());
|
|
169
|
+
jbProjects().approve(address(REV_DEPLOYER), FEE_PROJECT_ID);
|
|
170
|
+
|
|
171
|
+
// Deploy the fee project first.
|
|
172
|
+
(REVConfig memory feeCfg, JBTerminalConfig[] memory feeTc, REVSuckerDeploymentConfig memory feeSdc) =
|
|
173
|
+
_buildConfig();
|
|
174
|
+
vm.prank(multisig());
|
|
175
|
+
REV_DEPLOYER.deployFor({
|
|
176
|
+
revnetId: FEE_PROJECT_ID,
|
|
177
|
+
configuration: feeCfg,
|
|
178
|
+
terminalConfigurations: feeTc,
|
|
179
|
+
suckerDeploymentConfiguration: feeSdc,
|
|
180
|
+
tiered721HookConfiguration: REVEmpty721Config.empty721Config(uint32(uint160(JBConstants.NATIVE_TOKEN))),
|
|
181
|
+
allowedPosts: REVEmpty721Config.emptyAllowedPosts()
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
// Deploy the test revnet.
|
|
185
|
+
(REVConfig memory cfg, JBTerminalConfig[] memory tc, REVSuckerDeploymentConfig memory sdc) = _buildConfig();
|
|
186
|
+
// forge-lint: disable-next-line(named-struct-fields)
|
|
187
|
+
cfg.description = REVDescription("SandwichTest2", "SW2", "ipfs://sandwich2", "SWT_SALT_2");
|
|
188
|
+
(REVNET_ID,) = REV_DEPLOYER.deployFor({
|
|
189
|
+
revnetId: 0,
|
|
190
|
+
configuration: cfg,
|
|
191
|
+
terminalConfigurations: tc,
|
|
192
|
+
suckerDeploymentConfiguration: sdc,
|
|
193
|
+
tiered721HookConfiguration: REVEmpty721Config.empty721Config(uint32(uint160(JBConstants.NATIVE_TOKEN))),
|
|
194
|
+
allowedPosts: REVEmpty721Config.emptyAllowedPosts()
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
vm.deal(BORROWER, 100 ether);
|
|
198
|
+
vm.deal(OTHER_PAYER, 100 ether);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/// @notice Helper: grant BURN_TOKENS permission to the loans contract for a given account and revnet.
|
|
202
|
+
function _grantBurnPermission(address account, uint256 revnetId) internal {
|
|
203
|
+
uint8[] memory permissionIds = new uint8[](1);
|
|
204
|
+
permissionIds[0] = JBPermissionIds.BURN_TOKENS;
|
|
205
|
+
|
|
206
|
+
JBPermissionsData memory permissionsData = JBPermissionsData({
|
|
207
|
+
// forge-lint: disable-next-line(unsafe-typecast)
|
|
208
|
+
operator: address(LOANS_CONTRACT),
|
|
209
|
+
// forge-lint: disable-next-line(unsafe-typecast)
|
|
210
|
+
projectId: uint56(revnetId),
|
|
211
|
+
permissionIds: permissionIds
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
vm.prank(account);
|
|
215
|
+
jbPermissions().setPermissionsFor(account, permissionsData);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/// @notice BY DESIGN: A borrower can extract additional value by reallocating collateral immediately after
|
|
219
|
+
/// a stage transition that lowers the cashOutTaxRate.
|
|
220
|
+
///
|
|
221
|
+
/// The sandwich works as follows:
|
|
222
|
+
/// 1. Borrower takes a loan in Stage 1 (60% cashOutTaxRate) — collateral is locked at Stage 1's rate
|
|
223
|
+
/// 2. Time warps past the stage boundary to Stage 2 (20% cashOutTaxRate)
|
|
224
|
+
/// 3. At Stage 2 rates, the same collateral is worth MORE on the bonding curve, so LESS collateral is
|
|
225
|
+
/// needed to support the original loan amount. The borrower frees the excess collateral.
|
|
226
|
+
/// 4. The freed collateral is used to open a NEW loan at Stage 2 rates — extracting additional funds.
|
|
227
|
+
/// 5. The borrower now holds TWO loans: the original (with reduced collateral) and a new one (with the
|
|
228
|
+
/// freed collateral). The total borrowed across both loans exceeds what was possible at Stage 1.
|
|
229
|
+
///
|
|
230
|
+
/// This is intentional: stage boundaries are immutable and predictable, and loan value tracks
|
|
231
|
+
/// the current bonding curve parameters. See RISKS.md section 3.
|
|
232
|
+
function test_reallocationSandwich_extractsValueAtStageBoundary() public {
|
|
233
|
+
// -- Step 1: Create liquidity from another payer so the bonding curve tax rate has a visible effect.
|
|
234
|
+
vm.prank(OTHER_PAYER);
|
|
235
|
+
jbMultiTerminal().pay{value: 10 ether}({
|
|
236
|
+
projectId: REVNET_ID,
|
|
237
|
+
token: JBConstants.NATIVE_TOKEN,
|
|
238
|
+
amount: 10 ether,
|
|
239
|
+
beneficiary: OTHER_PAYER,
|
|
240
|
+
minReturnedTokens: 0,
|
|
241
|
+
memo: "",
|
|
242
|
+
metadata: ""
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
// -- Step 2: Borrower pays in and receives tokens during Stage 1.
|
|
246
|
+
vm.prank(BORROWER);
|
|
247
|
+
uint256 borrowerTokens = jbMultiTerminal().pay{value: 10 ether}({
|
|
248
|
+
projectId: REVNET_ID,
|
|
249
|
+
token: JBConstants.NATIVE_TOKEN,
|
|
250
|
+
amount: 10 ether,
|
|
251
|
+
beneficiary: BORROWER,
|
|
252
|
+
minReturnedTokens: 0,
|
|
253
|
+
memo: "",
|
|
254
|
+
metadata: ""
|
|
255
|
+
});
|
|
256
|
+
assertGt(borrowerTokens, 0, "Borrower should receive tokens");
|
|
257
|
+
|
|
258
|
+
// -- Step 3: Grant BURN_TOKENS permission and take a loan during Stage 1.
|
|
259
|
+
_grantBurnPermission(BORROWER, REVNET_ID);
|
|
260
|
+
|
|
261
|
+
REVLoanSource memory source = REVLoanSource({token: JBConstants.NATIVE_TOKEN, terminal: jbMultiTerminal()});
|
|
262
|
+
|
|
263
|
+
// Use the minimum prepaid fee (2.5%) so fees don't obscure the comparison.
|
|
264
|
+
uint256 prepaidFeePercent = LOANS_CONTRACT.MIN_PREPAID_FEE_PERCENT();
|
|
265
|
+
|
|
266
|
+
vm.prank(BORROWER);
|
|
267
|
+
(uint256 stage1LoanId, REVLoan memory stage1Loan) = LOANS_CONTRACT.borrowFrom({
|
|
268
|
+
revnetId: REVNET_ID,
|
|
269
|
+
source: source,
|
|
270
|
+
minBorrowAmount: 0,
|
|
271
|
+
collateralCount: borrowerTokens,
|
|
272
|
+
beneficiary: payable(BORROWER),
|
|
273
|
+
prepaidFeePercent: prepaidFeePercent
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
uint256 stage1BorrowedAmount = stage1Loan.amount;
|
|
277
|
+
uint256 stage1Collateral = stage1Loan.collateral;
|
|
278
|
+
assertGt(stage1BorrowedAmount, 0, "Stage 1 loan should have positive borrowed amount");
|
|
279
|
+
assertEq(stage1Collateral, borrowerTokens, "All tokens should be collateral");
|
|
280
|
+
|
|
281
|
+
// Record the borrowable amount for the same collateral count at Stage 1 rates (for comparison).
|
|
282
|
+
uint256 borrowableStage1 = LOANS_CONTRACT.borrowableAmountFrom(
|
|
283
|
+
REVNET_ID, borrowerTokens, 18, uint32(uint160(JBConstants.NATIVE_TOKEN))
|
|
284
|
+
);
|
|
285
|
+
|
|
286
|
+
// -- Step 4: Warp past the stage boundary to Stage 2 (lower cashOutTaxRate).
|
|
287
|
+
vm.warp(block.timestamp + 31 days);
|
|
288
|
+
|
|
289
|
+
// Verify borrowable amount increased at Stage 2 rates.
|
|
290
|
+
uint256 borrowableStage2 = LOANS_CONTRACT.borrowableAmountFrom(
|
|
291
|
+
REVNET_ID, borrowerTokens, 18, uint32(uint160(JBConstants.NATIVE_TOKEN))
|
|
292
|
+
);
|
|
293
|
+
assertGt(borrowableStage2, borrowableStage1, "Borrowable should increase with lower cashOutTaxRate");
|
|
294
|
+
|
|
295
|
+
// -- Step 5: Determine how much collateral can be freed.
|
|
296
|
+
// At Stage 2's lower cashOutTaxRate, less collateral is needed to support the original loan amount.
|
|
297
|
+
// Use binary search to find the maximum transferable collateral: the largest amount that can be
|
|
298
|
+
// removed while leaving enough for the remaining collateral to cover the original loan amount.
|
|
299
|
+
uint256 collateralToTransfer;
|
|
300
|
+
{
|
|
301
|
+
uint256 lo = 0;
|
|
302
|
+
uint256 hi = stage1Collateral;
|
|
303
|
+
uint256 currency = uint32(uint160(JBConstants.NATIVE_TOKEN));
|
|
304
|
+
while (lo < hi) {
|
|
305
|
+
uint256 mid = (lo + hi + 1) / 2;
|
|
306
|
+
uint256 remaining = stage1Collateral - mid;
|
|
307
|
+
uint256 borrowable = LOANS_CONTRACT.borrowableAmountFrom(REVNET_ID, remaining, 18, currency);
|
|
308
|
+
if (borrowable >= stage1BorrowedAmount) {
|
|
309
|
+
lo = mid;
|
|
310
|
+
} else {
|
|
311
|
+
hi = mid - 1;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
collateralToTransfer = lo;
|
|
315
|
+
}
|
|
316
|
+
assertGt(collateralToTransfer, 0, "Should be able to free some collateral after tax rate decrease");
|
|
317
|
+
|
|
318
|
+
// Verify that the remaining collateral at Stage 2 rates supports the original loan amount.
|
|
319
|
+
uint256 borrowableWithRemaining = LOANS_CONTRACT.borrowableAmountFrom(
|
|
320
|
+
REVNET_ID, stage1Collateral - collateralToTransfer, 18, uint32(uint160(JBConstants.NATIVE_TOKEN))
|
|
321
|
+
);
|
|
322
|
+
assertGe(
|
|
323
|
+
borrowableWithRemaining,
|
|
324
|
+
stage1BorrowedAmount,
|
|
325
|
+
"Remaining collateral should cover original loan at Stage 2 rates"
|
|
326
|
+
);
|
|
327
|
+
|
|
328
|
+
// -- Step 6: Borrower reallocates collateral from the Stage 1 loan to extract the increased value.
|
|
329
|
+
// Transfer half the collateral from the old loan. The remaining half still covers the original
|
|
330
|
+
// loan amount at Stage 2's improved rates. The freed collateral goes into a new loan.
|
|
331
|
+
vm.prank(BORROWER);
|
|
332
|
+
(
|
|
333
|
+
, // reallocatedLoanId
|
|
334
|
+
, // newLoanId
|
|
335
|
+
REVLoan memory reallocatedLoan,
|
|
336
|
+
REVLoan memory newLoan
|
|
337
|
+
) = LOANS_CONTRACT.reallocateCollateralFromLoan({
|
|
338
|
+
loanId: stage1LoanId,
|
|
339
|
+
collateralCountToTransfer: collateralToTransfer,
|
|
340
|
+
source: source,
|
|
341
|
+
minBorrowAmount: 0,
|
|
342
|
+
collateralCountToAdd: 0,
|
|
343
|
+
beneficiary: payable(BORROWER),
|
|
344
|
+
prepaidFeePercent: prepaidFeePercent
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
// -- Step 7: Verify the sandwich extracted additional value.
|
|
348
|
+
|
|
349
|
+
// The reallocated loan retains the original borrowed amount with reduced collateral.
|
|
350
|
+
assertEq(reallocatedLoan.amount, stage1BorrowedAmount, "Reallocated loan should keep original amount");
|
|
351
|
+
assertEq(
|
|
352
|
+
reallocatedLoan.collateral,
|
|
353
|
+
stage1Collateral - collateralToTransfer,
|
|
354
|
+
"Reallocated loan should have reduced collateral"
|
|
355
|
+
);
|
|
356
|
+
|
|
357
|
+
// The new loan was created from the freed collateral at Stage 2 rates.
|
|
358
|
+
uint256 newBorrowedAmount = newLoan.amount;
|
|
359
|
+
assertGt(newBorrowedAmount, 0, "New loan should have positive borrowed amount");
|
|
360
|
+
assertEq(newLoan.collateral, collateralToTransfer, "New loan should hold the transferred collateral");
|
|
361
|
+
|
|
362
|
+
// The total borrowed across both loans exceeds the original Stage 1 borrowed amount.
|
|
363
|
+
// This is the sandwich profit: the borrower extracted additional value from the same total collateral.
|
|
364
|
+
uint256 totalBorrowedAfterSandwich = uint256(reallocatedLoan.amount) + uint256(newLoan.amount);
|
|
365
|
+
assertGt(
|
|
366
|
+
totalBorrowedAfterSandwich,
|
|
367
|
+
stage1BorrowedAmount,
|
|
368
|
+
"Total borrowed after sandwich should exceed original Stage 1 amount"
|
|
369
|
+
);
|
|
370
|
+
|
|
371
|
+
// Quantify the extracted delta.
|
|
372
|
+
uint256 extractedDelta = totalBorrowedAfterSandwich - stage1BorrowedAmount;
|
|
373
|
+
assertGt(extractedDelta, 0, "Extracted delta should be positive");
|
|
374
|
+
|
|
375
|
+
// Log the sandwich economics for visibility.
|
|
376
|
+
emit log_named_uint("Stage 1 borrowed amount", stage1BorrowedAmount);
|
|
377
|
+
emit log_named_uint("Reallocated loan amount (unchanged)", reallocatedLoan.amount);
|
|
378
|
+
emit log_named_uint("New loan amount (from freed collateral)", newBorrowedAmount);
|
|
379
|
+
emit log_named_uint("Total borrowed after sandwich", totalBorrowedAfterSandwich);
|
|
380
|
+
emit log_named_uint("Extracted delta (sandwich profit)", extractedDelta);
|
|
381
|
+
emit log_named_uint("Borrowable at Stage 1 rates (full collateral)", borrowableStage1);
|
|
382
|
+
emit log_named_uint("Borrowable at Stage 2 rates (full collateral)", borrowableStage2);
|
|
383
|
+
}
|
|
384
|
+
}
|