@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,491 @@
|
|
|
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
|
+
|
|
14
|
+
// forge-lint: disable-next-line(unaliased-plain-import)
|
|
15
|
+
import "@bananapus/core-v6/script/helpers/CoreDeploymentLib.sol";
|
|
16
|
+
// forge-lint: disable-next-line(unaliased-plain-import)
|
|
17
|
+
import "@bananapus/721-hook-v6/script/helpers/Hook721DeploymentLib.sol";
|
|
18
|
+
// forge-lint: disable-next-line(unaliased-plain-import)
|
|
19
|
+
import "@bananapus/suckers-v6/script/helpers/SuckerDeploymentLib.sol";
|
|
20
|
+
// forge-lint: disable-next-line(unaliased-plain-import)
|
|
21
|
+
import "@croptop/core-v6/script/helpers/CroptopDeploymentLib.sol";
|
|
22
|
+
// forge-lint: disable-next-line(unaliased-plain-import)
|
|
23
|
+
import "@bananapus/router-terminal-v6/script/helpers/RouterTerminalDeploymentLib.sol";
|
|
24
|
+
|
|
25
|
+
import {JBConstants} from "@bananapus/core-v6/src/libraries/JBConstants.sol";
|
|
26
|
+
import {JBAccountingContext} from "@bananapus/core-v6/src/structs/JBAccountingContext.sol";
|
|
27
|
+
import {REVLoans} from "../src/REVLoans.sol";
|
|
28
|
+
import {REVLoan} from "../src/structs/REVLoan.sol";
|
|
29
|
+
import {REVStageConfig, REVAutoIssuance} from "../src/structs/REVStageConfig.sol";
|
|
30
|
+
import {REVLoanSource} from "../src/structs/REVLoanSource.sol";
|
|
31
|
+
import {REVDescription} from "../src/structs/REVDescription.sol";
|
|
32
|
+
import {IREVLoans} from "./../src/interfaces/IREVLoans.sol";
|
|
33
|
+
import {JBSuckerDeployerConfig} from "@bananapus/suckers-v6/src/structs/JBSuckerDeployerConfig.sol";
|
|
34
|
+
import {JBSuckerRegistry} from "@bananapus/suckers-v6/src/JBSuckerRegistry.sol";
|
|
35
|
+
import {JB721TiersHookDeployer} from "@bananapus/721-hook-v6/src/JB721TiersHookDeployer.sol";
|
|
36
|
+
import {JB721TiersHook} from "@bananapus/721-hook-v6/src/JB721TiersHook.sol";
|
|
37
|
+
import {JB721TiersHookStore} from "@bananapus/721-hook-v6/src/JB721TiersHookStore.sol";
|
|
38
|
+
import {JBAddressRegistry} from "@bananapus/address-registry-v6/src/JBAddressRegistry.sol";
|
|
39
|
+
import {IJBAddressRegistry} from "@bananapus/address-registry-v6/src/interfaces/IJBAddressRegistry.sol";
|
|
40
|
+
import {REVEmpty721Config} from "./helpers/REVEmpty721Config.sol";
|
|
41
|
+
|
|
42
|
+
struct SourceFeeProjectConfig {
|
|
43
|
+
REVConfig configuration;
|
|
44
|
+
JBTerminalConfig[] terminalConfigurations;
|
|
45
|
+
REVSuckerDeploymentConfig suckerDeploymentConfiguration;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/// @title REVLoansSourceFeeRecovery
|
|
49
|
+
/// @notice Tests for the source fee try-catch in REVLoans._adjust().
|
|
50
|
+
/// @dev When loan.source.terminal.pay() reverts during source fee payment, the borrower
|
|
51
|
+
/// should receive the source fee amount back instead of losing it.
|
|
52
|
+
contract REVLoansSourceFeeRecovery is TestBaseWorkflow {
|
|
53
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
54
|
+
bytes32 REV_DEPLOYER_SALT = "REVDeployer";
|
|
55
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
56
|
+
bytes32 ERC20_SALT = "REV_TOKEN";
|
|
57
|
+
|
|
58
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
59
|
+
REVDeployer REV_DEPLOYER;
|
|
60
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
61
|
+
JB721TiersHook EXAMPLE_HOOK;
|
|
62
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
63
|
+
IJB721TiersHookDeployer HOOK_DEPLOYER;
|
|
64
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
65
|
+
IJB721TiersHookStore HOOK_STORE;
|
|
66
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
67
|
+
IJBAddressRegistry ADDRESS_REGISTRY;
|
|
68
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
69
|
+
IREVLoans LOANS_CONTRACT;
|
|
70
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
71
|
+
IJBSuckerRegistry SUCKER_REGISTRY;
|
|
72
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
73
|
+
CTPublisher PUBLISHER;
|
|
74
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
75
|
+
MockBuybackDataHook MOCK_BUYBACK;
|
|
76
|
+
|
|
77
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
78
|
+
uint256 FEE_PROJECT_ID;
|
|
79
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
80
|
+
uint256 REVNET_ID;
|
|
81
|
+
|
|
82
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
83
|
+
address USER = makeAddr("user");
|
|
84
|
+
address private constant TRUSTED_FORWARDER = 0xB2b5841DBeF766d4b521221732F9B618fCf34A87;
|
|
85
|
+
|
|
86
|
+
function _getFeeProjectConfig() internal view returns (SourceFeeProjectConfig memory) {
|
|
87
|
+
uint8 decimals = 18;
|
|
88
|
+
uint256 decimalMultiplier = 10 ** decimals;
|
|
89
|
+
|
|
90
|
+
JBAccountingContext[] memory accountingContextsToAccept = new JBAccountingContext[](1);
|
|
91
|
+
accountingContextsToAccept[0] = JBAccountingContext({
|
|
92
|
+
token: JBConstants.NATIVE_TOKEN, decimals: 18, currency: uint32(uint160(JBConstants.NATIVE_TOKEN))
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
JBTerminalConfig[] memory terminalConfigurations = new JBTerminalConfig[](1);
|
|
96
|
+
terminalConfigurations[0] =
|
|
97
|
+
JBTerminalConfig({terminal: jbMultiTerminal(), accountingContextsToAccept: accountingContextsToAccept});
|
|
98
|
+
|
|
99
|
+
REVStageConfig[] memory stageConfigurations = new REVStageConfig[](1);
|
|
100
|
+
JBSplit[] memory splits = new JBSplit[](1);
|
|
101
|
+
splits[0].beneficiary = payable(multisig());
|
|
102
|
+
splits[0].percent = 10_000;
|
|
103
|
+
|
|
104
|
+
REVAutoIssuance[] memory issuanceConfs = new REVAutoIssuance[](1);
|
|
105
|
+
issuanceConfs[0] = REVAutoIssuance({
|
|
106
|
+
// forge-lint: disable-next-line(unsafe-typecast)
|
|
107
|
+
chainId: uint32(block.chainid),
|
|
108
|
+
// forge-lint: disable-next-line(unsafe-typecast)
|
|
109
|
+
count: uint104(70_000 * decimalMultiplier),
|
|
110
|
+
beneficiary: multisig()
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
stageConfigurations[0] = REVStageConfig({
|
|
114
|
+
startsAtOrAfter: uint40(block.timestamp),
|
|
115
|
+
autoIssuances: issuanceConfs,
|
|
116
|
+
splitPercent: 2000,
|
|
117
|
+
splits: splits,
|
|
118
|
+
// forge-lint: disable-next-line(unsafe-typecast)
|
|
119
|
+
initialIssuance: uint112(1000 * decimalMultiplier),
|
|
120
|
+
issuanceCutFrequency: 90 days,
|
|
121
|
+
issuanceCutPercent: JBConstants.MAX_WEIGHT_CUT_PERCENT / 2,
|
|
122
|
+
cashOutTaxRate: 6000,
|
|
123
|
+
extraMetadata: 0
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
REVConfig memory revnetConfiguration = REVConfig({
|
|
127
|
+
description: REVDescription({
|
|
128
|
+
name: "Revnet",
|
|
129
|
+
ticker: "$REV",
|
|
130
|
+
uri: "ipfs://QmNRHT91HcDgMcenebYX7rJigt77cgNcosvuhX21wkF3tx",
|
|
131
|
+
salt: ERC20_SALT
|
|
132
|
+
}),
|
|
133
|
+
baseCurrency: uint32(uint160(JBConstants.NATIVE_TOKEN)),
|
|
134
|
+
splitOperator: multisig(),
|
|
135
|
+
stageConfigurations: stageConfigurations
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
return SourceFeeProjectConfig({
|
|
139
|
+
configuration: revnetConfiguration,
|
|
140
|
+
terminalConfigurations: terminalConfigurations,
|
|
141
|
+
suckerDeploymentConfiguration: REVSuckerDeploymentConfig({
|
|
142
|
+
deployerConfigurations: new JBSuckerDeployerConfig[](0), salt: keccak256(abi.encodePacked("REV"))
|
|
143
|
+
})
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function _getRevnetConfig() internal view returns (SourceFeeProjectConfig memory) {
|
|
148
|
+
uint8 decimals = 18;
|
|
149
|
+
uint256 decimalMultiplier = 10 ** decimals;
|
|
150
|
+
|
|
151
|
+
JBAccountingContext[] memory accountingContextsToAccept = new JBAccountingContext[](1);
|
|
152
|
+
accountingContextsToAccept[0] = JBAccountingContext({
|
|
153
|
+
token: JBConstants.NATIVE_TOKEN, decimals: 18, currency: uint32(uint160(JBConstants.NATIVE_TOKEN))
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
JBTerminalConfig[] memory terminalConfigurations = new JBTerminalConfig[](1);
|
|
157
|
+
terminalConfigurations[0] =
|
|
158
|
+
JBTerminalConfig({terminal: jbMultiTerminal(), accountingContextsToAccept: accountingContextsToAccept});
|
|
159
|
+
|
|
160
|
+
JBSplit[] memory splits = new JBSplit[](1);
|
|
161
|
+
splits[0].beneficiary = payable(multisig());
|
|
162
|
+
splits[0].percent = 10_000;
|
|
163
|
+
|
|
164
|
+
REVStageConfig[] memory stageConfigurations = new REVStageConfig[](1);
|
|
165
|
+
REVAutoIssuance[] memory issuanceConfs = new REVAutoIssuance[](1);
|
|
166
|
+
issuanceConfs[0] = REVAutoIssuance({
|
|
167
|
+
// forge-lint: disable-next-line(unsafe-typecast)
|
|
168
|
+
chainId: uint32(block.chainid),
|
|
169
|
+
// forge-lint: disable-next-line(unsafe-typecast)
|
|
170
|
+
count: uint104(70_000 * decimalMultiplier),
|
|
171
|
+
beneficiary: multisig()
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
stageConfigurations[0] = REVStageConfig({
|
|
175
|
+
startsAtOrAfter: uint40(block.timestamp),
|
|
176
|
+
autoIssuances: issuanceConfs,
|
|
177
|
+
splitPercent: 2000,
|
|
178
|
+
splits: splits,
|
|
179
|
+
// forge-lint: disable-next-line(unsafe-typecast)
|
|
180
|
+
initialIssuance: uint112(1000 * decimalMultiplier),
|
|
181
|
+
issuanceCutFrequency: 90 days,
|
|
182
|
+
issuanceCutPercent: JBConstants.MAX_WEIGHT_CUT_PERCENT / 2,
|
|
183
|
+
cashOutTaxRate: 6000,
|
|
184
|
+
extraMetadata: 0
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
REVLoanSource[] memory _loanSources = new REVLoanSource[](1);
|
|
188
|
+
_loanSources[0] = REVLoanSource({token: JBConstants.NATIVE_TOKEN, terminal: jbMultiTerminal()});
|
|
189
|
+
|
|
190
|
+
REVConfig memory revnetConfiguration = REVConfig({
|
|
191
|
+
description: REVDescription({
|
|
192
|
+
name: "NANA",
|
|
193
|
+
ticker: "$NANA",
|
|
194
|
+
uri: "ipfs://QmNRHT91HcDgMcenebYX7rJigt77cgNxosvuhX21wkF3tx",
|
|
195
|
+
salt: "NANA_TOKEN"
|
|
196
|
+
}),
|
|
197
|
+
baseCurrency: uint32(uint160(JBConstants.NATIVE_TOKEN)),
|
|
198
|
+
splitOperator: multisig(),
|
|
199
|
+
stageConfigurations: stageConfigurations
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
return SourceFeeProjectConfig({
|
|
203
|
+
configuration: revnetConfiguration,
|
|
204
|
+
terminalConfigurations: terminalConfigurations,
|
|
205
|
+
suckerDeploymentConfiguration: REVSuckerDeploymentConfig({
|
|
206
|
+
deployerConfigurations: new JBSuckerDeployerConfig[](0), salt: keccak256(abi.encodePacked("NANA"))
|
|
207
|
+
})
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function setUp() public override {
|
|
212
|
+
super.setUp();
|
|
213
|
+
|
|
214
|
+
FEE_PROJECT_ID = jbProjects().createFor(multisig());
|
|
215
|
+
|
|
216
|
+
SUCKER_REGISTRY = new JBSuckerRegistry(jbDirectory(), jbPermissions(), multisig(), address(0));
|
|
217
|
+
HOOK_STORE = new JB721TiersHookStore();
|
|
218
|
+
EXAMPLE_HOOK = new JB721TiersHook(
|
|
219
|
+
jbDirectory(), jbPermissions(), jbPrices(), jbRulesets(), HOOK_STORE, jbSplits(), multisig()
|
|
220
|
+
);
|
|
221
|
+
ADDRESS_REGISTRY = new JBAddressRegistry();
|
|
222
|
+
HOOK_DEPLOYER = new JB721TiersHookDeployer(EXAMPLE_HOOK, HOOK_STORE, ADDRESS_REGISTRY, multisig());
|
|
223
|
+
PUBLISHER = new CTPublisher(jbDirectory(), jbPermissions(), FEE_PROJECT_ID, multisig());
|
|
224
|
+
MOCK_BUYBACK = new MockBuybackDataHook();
|
|
225
|
+
|
|
226
|
+
LOANS_CONTRACT = new REVLoans({
|
|
227
|
+
controller: jbController(),
|
|
228
|
+
projects: jbProjects(),
|
|
229
|
+
revId: FEE_PROJECT_ID,
|
|
230
|
+
owner: address(this),
|
|
231
|
+
permit2: permit2(),
|
|
232
|
+
trustedForwarder: TRUSTED_FORWARDER
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
REV_DEPLOYER = new REVDeployer{salt: REV_DEPLOYER_SALT}(
|
|
236
|
+
jbController(),
|
|
237
|
+
SUCKER_REGISTRY,
|
|
238
|
+
FEE_PROJECT_ID,
|
|
239
|
+
HOOK_DEPLOYER,
|
|
240
|
+
PUBLISHER,
|
|
241
|
+
IJBBuybackHookRegistry(address(MOCK_BUYBACK)),
|
|
242
|
+
address(LOANS_CONTRACT),
|
|
243
|
+
TRUSTED_FORWARDER
|
|
244
|
+
);
|
|
245
|
+
|
|
246
|
+
// Deploy fee project.
|
|
247
|
+
vm.prank(multisig());
|
|
248
|
+
jbProjects().approve(address(REV_DEPLOYER), FEE_PROJECT_ID);
|
|
249
|
+
|
|
250
|
+
SourceFeeProjectConfig memory feeProjectConfig = _getFeeProjectConfig();
|
|
251
|
+
vm.prank(multisig());
|
|
252
|
+
REV_DEPLOYER.deployFor({
|
|
253
|
+
revnetId: FEE_PROJECT_ID,
|
|
254
|
+
configuration: feeProjectConfig.configuration,
|
|
255
|
+
terminalConfigurations: feeProjectConfig.terminalConfigurations,
|
|
256
|
+
suckerDeploymentConfiguration: feeProjectConfig.suckerDeploymentConfiguration,
|
|
257
|
+
tiered721HookConfiguration: REVEmpty721Config.empty721Config(uint32(uint160(JBConstants.NATIVE_TOKEN))),
|
|
258
|
+
allowedPosts: REVEmpty721Config.emptyAllowedPosts()
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
// Deploy revnet with loans enabled.
|
|
262
|
+
SourceFeeProjectConfig memory revnetConfig = _getRevnetConfig();
|
|
263
|
+
(REVNET_ID,) = REV_DEPLOYER.deployFor({
|
|
264
|
+
revnetId: 0,
|
|
265
|
+
configuration: revnetConfig.configuration,
|
|
266
|
+
terminalConfigurations: revnetConfig.terminalConfigurations,
|
|
267
|
+
suckerDeploymentConfiguration: revnetConfig.suckerDeploymentConfiguration,
|
|
268
|
+
tiered721HookConfiguration: REVEmpty721Config.empty721Config(uint32(uint160(JBConstants.NATIVE_TOKEN))),
|
|
269
|
+
allowedPosts: REVEmpty721Config.emptyAllowedPosts()
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
vm.deal(USER, 1000e18);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// =========================================================================
|
|
276
|
+
// Helpers
|
|
277
|
+
// =========================================================================
|
|
278
|
+
|
|
279
|
+
function _mockLoanPermission(address user) internal {
|
|
280
|
+
// Use vm.mockCall (not mockExpect) to avoid enforcing call counts — repay doesn't always
|
|
281
|
+
// trigger a permission check, but borrow does.
|
|
282
|
+
vm.mockCall(
|
|
283
|
+
address(jbPermissions()),
|
|
284
|
+
abi.encodeCall(IJBPermissions.hasPermission, (address(LOANS_CONTRACT), user, REVNET_ID, 11, true, true)),
|
|
285
|
+
abi.encode(true)
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function _borrowLoan(address user, uint256 ethAmount) internal returns (uint256 loanId, uint256 tokenCount) {
|
|
290
|
+
vm.prank(user);
|
|
291
|
+
tokenCount =
|
|
292
|
+
jbMultiTerminal().pay{value: ethAmount}(REVNET_ID, JBConstants.NATIVE_TOKEN, ethAmount, user, 0, "", "");
|
|
293
|
+
|
|
294
|
+
_mockLoanPermission(user);
|
|
295
|
+
REVLoanSource memory source = REVLoanSource({token: JBConstants.NATIVE_TOKEN, terminal: jbMultiTerminal()});
|
|
296
|
+
|
|
297
|
+
vm.prank(user);
|
|
298
|
+
(loanId,) = LOANS_CONTRACT.borrowFrom(REVNET_ID, source, 0, tokenCount, payable(user), 25);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// =========================================================================
|
|
302
|
+
// Test: Source fee try-catch during repay — terminal pay reverts
|
|
303
|
+
// =========================================================================
|
|
304
|
+
|
|
305
|
+
/// @notice When the source terminal's pay() reverts during repay, the source fee is returned
|
|
306
|
+
/// to the borrower and the repayment still succeeds.
|
|
307
|
+
function test_sourceFeeRecovery_repay_nativeToken() public {
|
|
308
|
+
// Step 1: Borrow normally.
|
|
309
|
+
(uint256 loanId,) = _borrowLoan(USER, 10e18);
|
|
310
|
+
|
|
311
|
+
// Step 2: Read the loan to get prepaid duration.
|
|
312
|
+
REVLoan memory loan = LOANS_CONTRACT.loanOf(loanId);
|
|
313
|
+
|
|
314
|
+
// Step 3: Warp past the prepaid duration so the source fee accrues.
|
|
315
|
+
vm.warp(block.timestamp + loan.prepaidDuration + 30 days);
|
|
316
|
+
|
|
317
|
+
// Step 4: Mock the source terminal's pay() to revert.
|
|
318
|
+
// This affects only `pay` calls, not `addToBalanceOf` (used by _removeFrom).
|
|
319
|
+
vm.mockCallRevert(
|
|
320
|
+
address(jbMultiTerminal()), abi.encodeWithSelector(IJBTerminal.pay.selector), "Source fee terminal failed"
|
|
321
|
+
);
|
|
322
|
+
|
|
323
|
+
// Step 5: Repay the full loan. The source fee try-catch should handle the reverting terminal.
|
|
324
|
+
uint256 repayAmount = loan.amount * 2; // Send more than enough to cover loan + fee.
|
|
325
|
+
vm.deal(USER, repayAmount);
|
|
326
|
+
|
|
327
|
+
vm.prank(USER);
|
|
328
|
+
LOANS_CONTRACT.repayLoan{value: repayAmount}({
|
|
329
|
+
loanId: loanId,
|
|
330
|
+
maxRepayBorrowAmount: repayAmount,
|
|
331
|
+
collateralCountToReturn: loan.collateral,
|
|
332
|
+
beneficiary: payable(USER),
|
|
333
|
+
allowance: JBSingleAllowance({amount: 0, expiration: 0, nonce: 0, sigDeadline: 0, signature: ""})
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
// Repay succeeded — the loan should be closed.
|
|
337
|
+
REVLoan memory postRepay = LOANS_CONTRACT.loanOf(loanId);
|
|
338
|
+
assertEq(postRepay.createdAt, 0, "Loan should be cleared after repay");
|
|
339
|
+
|
|
340
|
+
// No ETH stuck in loans contract.
|
|
341
|
+
assertEq(address(LOANS_CONTRACT).balance, 0, "No ETH stuck in loans contract");
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/// @notice When the source terminal's pay() works normally during repay, the source fee is
|
|
345
|
+
/// deducted as expected (regression to confirm baseline).
|
|
346
|
+
function test_sourceFeePayment_repay_normalOperation() public {
|
|
347
|
+
// Borrow normally.
|
|
348
|
+
(uint256 loanId,) = _borrowLoan(USER, 10e18);
|
|
349
|
+
|
|
350
|
+
REVLoan memory loan = LOANS_CONTRACT.loanOf(loanId);
|
|
351
|
+
|
|
352
|
+
// Warp past prepaid duration.
|
|
353
|
+
vm.warp(block.timestamp + loan.prepaidDuration + 30 days);
|
|
354
|
+
|
|
355
|
+
// Repay normally (no mocking — terminal pay should succeed).
|
|
356
|
+
uint256 repayAmount = loan.amount * 2;
|
|
357
|
+
vm.deal(USER, repayAmount);
|
|
358
|
+
|
|
359
|
+
vm.prank(USER);
|
|
360
|
+
LOANS_CONTRACT.repayLoan{value: repayAmount}({
|
|
361
|
+
loanId: loanId,
|
|
362
|
+
maxRepayBorrowAmount: repayAmount,
|
|
363
|
+
collateralCountToReturn: loan.collateral,
|
|
364
|
+
beneficiary: payable(USER),
|
|
365
|
+
allowance: JBSingleAllowance({amount: 0, expiration: 0, nonce: 0, sigDeadline: 0, signature: ""})
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
// Loan closed.
|
|
369
|
+
REVLoan memory postRepay = LOANS_CONTRACT.loanOf(loanId);
|
|
370
|
+
assertEq(postRepay.createdAt, 0, "Loan should be cleared after normal repay");
|
|
371
|
+
|
|
372
|
+
// No ETH stuck.
|
|
373
|
+
assertEq(address(LOANS_CONTRACT).balance, 0, "No ETH stuck");
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/// @notice When the source terminal reverts, the borrower receives more ETH back than when
|
|
377
|
+
/// it succeeds, because the source fee is returned to them.
|
|
378
|
+
function test_sourceFeeRecovery_borrowerGetsMoreThanNormal() public {
|
|
379
|
+
// Borrow a loan.
|
|
380
|
+
(uint256 loanId,) = _borrowLoan(USER, 10e18);
|
|
381
|
+
|
|
382
|
+
REVLoan memory loan = LOANS_CONTRACT.loanOf(loanId);
|
|
383
|
+
|
|
384
|
+
// Warp past prepaid duration.
|
|
385
|
+
vm.warp(block.timestamp + loan.prepaidDuration + 30 days);
|
|
386
|
+
|
|
387
|
+
// Compute the expected source fee.
|
|
388
|
+
uint256 sourceFee = LOANS_CONTRACT.determineSourceFeeAmount(loan, loan.amount);
|
|
389
|
+
assertGt(sourceFee, 0, "Source fee should be nonzero after prepaid duration");
|
|
390
|
+
|
|
391
|
+
// Snapshot state to compare normal vs. failed scenarios.
|
|
392
|
+
uint256 snap = vm.snapshotState();
|
|
393
|
+
|
|
394
|
+
// --- Normal repay ---
|
|
395
|
+
uint256 repayAmount = loan.amount * 2;
|
|
396
|
+
vm.deal(USER, repayAmount);
|
|
397
|
+
|
|
398
|
+
uint256 balBefore = USER.balance;
|
|
399
|
+
vm.prank(USER);
|
|
400
|
+
LOANS_CONTRACT.repayLoan{value: repayAmount}({
|
|
401
|
+
loanId: loanId,
|
|
402
|
+
maxRepayBorrowAmount: repayAmount,
|
|
403
|
+
collateralCountToReturn: loan.collateral,
|
|
404
|
+
beneficiary: payable(USER),
|
|
405
|
+
allowance: JBSingleAllowance({amount: 0, expiration: 0, nonce: 0, sigDeadline: 0, signature: ""})
|
|
406
|
+
});
|
|
407
|
+
uint256 normalBalance = USER.balance;
|
|
408
|
+
|
|
409
|
+
// --- Revert and try with failing terminal ---
|
|
410
|
+
vm.revertToState(snap);
|
|
411
|
+
|
|
412
|
+
vm.mockCallRevert(
|
|
413
|
+
address(jbMultiTerminal()), abi.encodeWithSelector(IJBTerminal.pay.selector), "Source fee terminal failed"
|
|
414
|
+
);
|
|
415
|
+
|
|
416
|
+
vm.deal(USER, repayAmount);
|
|
417
|
+
|
|
418
|
+
balBefore = USER.balance;
|
|
419
|
+
vm.prank(USER);
|
|
420
|
+
LOANS_CONTRACT.repayLoan{value: repayAmount}({
|
|
421
|
+
loanId: loanId,
|
|
422
|
+
maxRepayBorrowAmount: repayAmount,
|
|
423
|
+
collateralCountToReturn: loan.collateral,
|
|
424
|
+
beneficiary: payable(USER),
|
|
425
|
+
allowance: JBSingleAllowance({amount: 0, expiration: 0, nonce: 0, sigDeadline: 0, signature: ""})
|
|
426
|
+
});
|
|
427
|
+
uint256 failBalance = USER.balance;
|
|
428
|
+
|
|
429
|
+
// The borrower should have more ETH when the source fee terminal fails,
|
|
430
|
+
// because the source fee is returned to them instead of being paid.
|
|
431
|
+
assertGt(failBalance, normalBalance, "Borrower should receive more when source fee terminal fails");
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/// @notice Source fee is zero during the prepaid window — no try-catch path is hit.
|
|
435
|
+
function test_noSourceFeeDuringPrepaidWindow() public {
|
|
436
|
+
(uint256 loanId,) = _borrowLoan(USER, 10e18);
|
|
437
|
+
|
|
438
|
+
REVLoan memory loan = LOANS_CONTRACT.loanOf(loanId);
|
|
439
|
+
|
|
440
|
+
// Stay within prepaid duration — no source fee accrues.
|
|
441
|
+
vm.warp(block.timestamp + loan.prepaidDuration / 2);
|
|
442
|
+
|
|
443
|
+
uint256 sourceFee = LOANS_CONTRACT.determineSourceFeeAmount(loan, loan.amount);
|
|
444
|
+
assertEq(sourceFee, 0, "No source fee during prepaid window");
|
|
445
|
+
|
|
446
|
+
// Repay during prepaid window.
|
|
447
|
+
uint256 repayAmount = loan.amount * 2;
|
|
448
|
+
vm.deal(USER, repayAmount);
|
|
449
|
+
|
|
450
|
+
vm.prank(USER);
|
|
451
|
+
LOANS_CONTRACT.repayLoan{value: repayAmount}({
|
|
452
|
+
loanId: loanId,
|
|
453
|
+
maxRepayBorrowAmount: repayAmount,
|
|
454
|
+
collateralCountToReturn: loan.collateral,
|
|
455
|
+
beneficiary: payable(USER),
|
|
456
|
+
allowance: JBSingleAllowance({amount: 0, expiration: 0, nonce: 0, sigDeadline: 0, signature: ""})
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
REVLoan memory postRepay = LOANS_CONTRACT.loanOf(loanId);
|
|
460
|
+
assertEq(postRepay.createdAt, 0, "Loan should be cleared");
|
|
461
|
+
assertEq(address(LOANS_CONTRACT).balance, 0, "No ETH stuck");
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/// @notice The source fee try-catch during initial borrow (prepaid source fee) also recovers
|
|
465
|
+
/// gracefully when the source terminal reverts.
|
|
466
|
+
function test_sourceFeeRecovery_initialBorrow() public {
|
|
467
|
+
// Pay into the revnet first.
|
|
468
|
+
vm.prank(USER);
|
|
469
|
+
uint256 tokenCount =
|
|
470
|
+
jbMultiTerminal().pay{value: 10e18}(REVNET_ID, JBConstants.NATIVE_TOKEN, 10e18, USER, 0, "", "");
|
|
471
|
+
|
|
472
|
+
// Now mock the source terminal's pay to revert (for source fee payment).
|
|
473
|
+
// This will also cause the REV fee payment to fail (same terminal), but both are try-caught.
|
|
474
|
+
vm.mockCallRevert(
|
|
475
|
+
address(jbMultiTerminal()), abi.encodeWithSelector(IJBTerminal.pay.selector), "Terminal pay failed"
|
|
476
|
+
);
|
|
477
|
+
|
|
478
|
+
_mockLoanPermission(USER);
|
|
479
|
+
REVLoanSource memory source = REVLoanSource({token: JBConstants.NATIVE_TOKEN, terminal: jbMultiTerminal()});
|
|
480
|
+
|
|
481
|
+
uint256 balBefore = USER.balance;
|
|
482
|
+
vm.prank(USER);
|
|
483
|
+
LOANS_CONTRACT.borrowFrom(REVNET_ID, source, 0, tokenCount, payable(USER), 25);
|
|
484
|
+
|
|
485
|
+
uint256 received = USER.balance - balBefore;
|
|
486
|
+
assertGt(received, 0, "Borrower should receive ETH even when source fee terminal fails");
|
|
487
|
+
|
|
488
|
+
// No ETH stuck.
|
|
489
|
+
assertEq(address(LOANS_CONTRACT).balance, 0, "No ETH stuck in loans contract");
|
|
490
|
+
}
|
|
491
|
+
}
|