@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.
- package/LICENSE +21 -0
- package/README.md +65 -0
- package/REVNET_SECURITY_CHECKLIST.md +164 -0
- package/SECURITY.md +68 -0
- package/SKILLS.md +166 -0
- package/deployments/revnet-core-v5/arbitrum/REVDeployer.json +2821 -0
- package/deployments/revnet-core-v5/arbitrum/REVLoans.json +2260 -0
- package/deployments/revnet-core-v5/arbitrum_sepolia/REVDeployer.json +2821 -0
- package/deployments/revnet-core-v5/arbitrum_sepolia/REVLoans.json +2260 -0
- package/deployments/revnet-core-v5/base/REVDeployer.json +2825 -0
- package/deployments/revnet-core-v5/base/REVLoans.json +2264 -0
- package/deployments/revnet-core-v5/base_sepolia/REVDeployer.json +2825 -0
- package/deployments/revnet-core-v5/base_sepolia/REVLoans.json +2264 -0
- package/deployments/revnet-core-v5/ethereum/REVDeployer.json +2825 -0
- package/deployments/revnet-core-v5/ethereum/REVLoans.json +2264 -0
- package/deployments/revnet-core-v5/optimism/REVDeployer.json +2821 -0
- package/deployments/revnet-core-v5/optimism/REVLoans.json +2260 -0
- package/deployments/revnet-core-v5/optimism_sepolia/REVDeployer.json +2825 -0
- package/deployments/revnet-core-v5/optimism_sepolia/REVLoans.json +2264 -0
- package/deployments/revnet-core-v5/sepolia/REVDeployer.json +2825 -0
- package/deployments/revnet-core-v5/sepolia/REVLoans.json +2264 -0
- package/docs/book.css +13 -0
- package/docs/book.toml +13 -0
- package/docs/solidity.min.js +74 -0
- package/docs/src/README.md +88 -0
- package/docs/src/SUMMARY.md +20 -0
- package/docs/src/src/README.md +7 -0
- package/docs/src/src/REVDeployer.sol/contract.REVDeployer.md +968 -0
- package/docs/src/src/REVLoans.sol/contract.REVLoans.md +1047 -0
- package/docs/src/src/interfaces/IREVDeployer.sol/interface.IREVDeployer.md +243 -0
- package/docs/src/src/interfaces/IREVLoans.sol/interface.IREVLoans.md +296 -0
- package/docs/src/src/interfaces/README.md +5 -0
- package/docs/src/src/structs/README.md +14 -0
- package/docs/src/src/structs/REVAutoIssuance.sol/struct.REVAutoIssuance.md +19 -0
- package/docs/src/src/structs/REVBuybackHookConfig.sol/struct.REVBuybackHookConfig.md +19 -0
- package/docs/src/src/structs/REVBuybackPoolConfig.sol/struct.REVBuybackPoolConfig.md +21 -0
- package/docs/src/src/structs/REVConfig.sol/struct.REVConfig.md +35 -0
- package/docs/src/src/structs/REVCroptopAllowedPost.sol/struct.REVCroptopAllowedPost.md +28 -0
- package/docs/src/src/structs/REVDeploy721TiersHookConfig.sol/struct.REVDeploy721TiersHookConfig.md +34 -0
- package/docs/src/src/structs/REVDescription.sol/struct.REVDescription.md +23 -0
- package/docs/src/src/structs/REVLoan.sol/struct.REVLoan.md +28 -0
- package/docs/src/src/structs/REVLoanSource.sol/struct.REVLoanSource.md +16 -0
- package/docs/src/src/structs/REVStageConfig.sol/struct.REVStageConfig.md +44 -0
- package/docs/src/src/structs/REVSuckerDeploymentConfig.sol/struct.REVSuckerDeploymentConfig.md +16 -0
- package/foundry.lock +11 -0
- package/foundry.toml +23 -0
- package/package.json +31 -0
- package/remappings.txt +1 -0
- package/script/Deploy.s.sol +350 -0
- package/script/helpers/RevnetCoreDeploymentLib.sol +72 -0
- package/slither-ci.config.json +10 -0
- package/sphinx.lock +507 -0
- package/src/REVDeployer.sol +1257 -0
- package/src/REVLoans.sol +1333 -0
- package/src/interfaces/IREVDeployer.sol +198 -0
- package/src/interfaces/IREVLoans.sol +241 -0
- package/src/structs/REVAutoIssuance.sol +11 -0
- package/src/structs/REVConfig.sol +17 -0
- package/src/structs/REVCroptopAllowedPost.sol +20 -0
- package/src/structs/REVDeploy721TiersHookConfig.sol +25 -0
- package/src/structs/REVDescription.sol +14 -0
- package/src/structs/REVLoan.sol +19 -0
- package/src/structs/REVLoanSource.sol +11 -0
- package/src/structs/REVStageConfig.sol +34 -0
- package/src/structs/REVSuckerDeploymentConfig.sol +11 -0
- package/test/REV.integrations.t.sol +420 -0
- package/test/REVAutoIssuanceFuzz.t.sol +276 -0
- package/test/REVDeployerAuditRegressions.t.sol +328 -0
- package/test/REVInvincibility.t.sol +1275 -0
- package/test/REVInvincibilityHandler.sol +357 -0
- package/test/REVLifecycle.t.sol +364 -0
- package/test/REVLoans.invariants.t.sol +642 -0
- package/test/REVLoansAttacks.t.sol +739 -0
- package/test/REVLoansAuditRegressions.t.sol +314 -0
- package/test/REVLoansFeeRecovery.t.sol +704 -0
- package/test/REVLoansSourced.t.sol +1732 -0
- package/test/REVLoansUnSourced.t.sol +331 -0
- package/test/TestPR09_ConversionDocumentation.t.sol +304 -0
- package/test/TestPR10_LiquidationBehavior.t.sol +340 -0
- package/test/TestPR11_LowFindings.t.sol +571 -0
- package/test/TestPR12_FlashLoanSurplus.t.sol +305 -0
- package/test/TestPR13_CrossSourceReallocation.t.sol +302 -0
- package/test/TestPR15_CashOutCallerValidation.t.sol +320 -0
- package/test/TestPR16_ZeroRepayment.t.sol +297 -0
- package/test/TestPR21_Uint112Overflow.t.sol +251 -0
- package/test/TestPR22_HookArrayOOB.t.sol +221 -0
- package/test/TestPR26_BurnHeldTokens.t.sol +331 -0
- package/test/TestPR27_CEIPattern.t.sol +448 -0
- package/test/TestPR29_SwapTerminalPermission.t.sol +206 -0
- package/test/TestPR32_MixedFixes.t.sol +529 -0
- package/test/helpers/MaliciousContracts.sol +233 -0
- package/test/mock/MockBuybackDataHook.sol +61 -0
|
@@ -0,0 +1,529 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
pragma solidity 0.8.23;
|
|
3
|
+
|
|
4
|
+
import "forge-std/Test.sol";
|
|
5
|
+
import /* {*} from */ "@bananapus/core-v6/test/helpers/TestBaseWorkflow.sol";
|
|
6
|
+
import /* {*} from "@bananapus/721-hook-v6/src/JB721TiersHookDeployer.sol";
|
|
7
|
+
import /* {*} from */ "./../src/REVDeployer.sol";
|
|
8
|
+
import "@croptop/core-v6/src/CTPublisher.sol";
|
|
9
|
+
import {MockBuybackDataHook} from "./mock/MockBuybackDataHook.sol";
|
|
10
|
+
import "@bananapus/core-v6/script/helpers/CoreDeploymentLib.sol";
|
|
11
|
+
import "@bananapus/721-hook-v6/script/helpers/Hook721DeploymentLib.sol";
|
|
12
|
+
import "@bananapus/suckers-v6/script/helpers/SuckerDeploymentLib.sol";
|
|
13
|
+
import "@croptop/core-v6/script/helpers/CroptopDeploymentLib.sol";
|
|
14
|
+
import "@bananapus/swap-terminal-v6/script/helpers/SwapTerminalDeploymentLib.sol";
|
|
15
|
+
import {JBConstants} from "@bananapus/core-v6/src/libraries/JBConstants.sol";
|
|
16
|
+
import {JBAccountingContext} from "@bananapus/core-v6/src/structs/JBAccountingContext.sol";
|
|
17
|
+
import {MockPriceFeed} from "@bananapus/core-v6/test/mock/MockPriceFeed.sol";
|
|
18
|
+
import {MockERC20} from "@bananapus/core-v6/test/mock/MockERC20.sol";
|
|
19
|
+
import {REVLoans} from "../src/REVLoans.sol";
|
|
20
|
+
import {REVLoan} from "../src/structs/REVLoan.sol";
|
|
21
|
+
import {REVStageConfig, REVAutoIssuance} from "../src/structs/REVStageConfig.sol";
|
|
22
|
+
import {REVLoanSource} from "../src/structs/REVLoanSource.sol";
|
|
23
|
+
import {REVDescription} from "../src/structs/REVDescription.sol";
|
|
24
|
+
import {IREVLoans} from "./../src/interfaces/IREVLoans.sol";
|
|
25
|
+
import {JBSuckerDeployerConfig} from "@bananapus/suckers-v6/src/structs/JBSuckerDeployerConfig.sol";
|
|
26
|
+
import {JBSuckerRegistry} from "@bananapus/suckers-v6/src/JBSuckerRegistry.sol";
|
|
27
|
+
import {JB721TiersHookDeployer} from "@bananapus/721-hook-v6/src/JB721TiersHookDeployer.sol";
|
|
28
|
+
import {JB721TiersHook} from "@bananapus/721-hook-v6/src/JB721TiersHook.sol";
|
|
29
|
+
import {JB721TiersHookStore} from "@bananapus/721-hook-v6/src/JB721TiersHookStore.sol";
|
|
30
|
+
import {JBAddressRegistry} from "@bananapus/address-registry-v6/src/JBAddressRegistry.sol";
|
|
31
|
+
import {IJBAddressRegistry} from "@bananapus/address-registry-v6/src/interfaces/IJBAddressRegistry.sol";
|
|
32
|
+
|
|
33
|
+
/// @notice Tests for PR #32: liquidation boundary, reallocate msg.value, and decimal normalization fixes.
|
|
34
|
+
contract TestPR32_MixedFixes is TestBaseWorkflow, JBTest {
|
|
35
|
+
bytes32 REV_DEPLOYER_SALT = "REVDeployer";
|
|
36
|
+
|
|
37
|
+
REVDeployer REV_DEPLOYER;
|
|
38
|
+
JB721TiersHook EXAMPLE_HOOK;
|
|
39
|
+
IJB721TiersHookDeployer HOOK_DEPLOYER;
|
|
40
|
+
IJB721TiersHookStore HOOK_STORE;
|
|
41
|
+
IJBAddressRegistry ADDRESS_REGISTRY;
|
|
42
|
+
REVLoans LOANS_CONTRACT;
|
|
43
|
+
MockERC20 TOKEN;
|
|
44
|
+
IJBSuckerRegistry SUCKER_REGISTRY;
|
|
45
|
+
CTPublisher PUBLISHER;
|
|
46
|
+
MockBuybackDataHook MOCK_BUYBACK;
|
|
47
|
+
|
|
48
|
+
uint256 FEE_PROJECT_ID;
|
|
49
|
+
uint256 REVNET_ID;
|
|
50
|
+
|
|
51
|
+
address USER = makeAddr("user");
|
|
52
|
+
|
|
53
|
+
address private constant TRUSTED_FORWARDER = 0xB2b5841DBeF766d4b521221732F9B618fCf34A87;
|
|
54
|
+
|
|
55
|
+
function setUp() public override {
|
|
56
|
+
super.setUp();
|
|
57
|
+
FEE_PROJECT_ID = jbProjects().createFor(multisig());
|
|
58
|
+
SUCKER_REGISTRY = new JBSuckerRegistry(jbDirectory(), jbPermissions(), multisig(), address(0));
|
|
59
|
+
HOOK_STORE = new JB721TiersHookStore();
|
|
60
|
+
EXAMPLE_HOOK = new JB721TiersHook(jbDirectory(), jbPermissions(), jbRulesets(), HOOK_STORE, multisig());
|
|
61
|
+
ADDRESS_REGISTRY = new JBAddressRegistry();
|
|
62
|
+
HOOK_DEPLOYER = new JB721TiersHookDeployer(EXAMPLE_HOOK, HOOK_STORE, ADDRESS_REGISTRY, multisig());
|
|
63
|
+
PUBLISHER = new CTPublisher(jbDirectory(), jbPermissions(), FEE_PROJECT_ID, multisig());
|
|
64
|
+
MOCK_BUYBACK = new MockBuybackDataHook();
|
|
65
|
+
TOKEN = new MockERC20("1/2 ETH", "1/2");
|
|
66
|
+
MockPriceFeed priceFeed = new MockPriceFeed(1e21, 6);
|
|
67
|
+
vm.prank(multisig());
|
|
68
|
+
jbPrices()
|
|
69
|
+
.addPriceFeedFor(0, uint32(uint160(address(TOKEN))), uint32(uint160(JBConstants.NATIVE_TOKEN)), priceFeed);
|
|
70
|
+
LOANS_CONTRACT = new REVLoans({
|
|
71
|
+
controller: jbController(),
|
|
72
|
+
projects: jbProjects(),
|
|
73
|
+
revId: FEE_PROJECT_ID,
|
|
74
|
+
owner: address(this),
|
|
75
|
+
permit2: permit2(),
|
|
76
|
+
trustedForwarder: TRUSTED_FORWARDER
|
|
77
|
+
});
|
|
78
|
+
REV_DEPLOYER = new REVDeployer{salt: REV_DEPLOYER_SALT}(
|
|
79
|
+
jbController(),
|
|
80
|
+
SUCKER_REGISTRY,
|
|
81
|
+
FEE_PROJECT_ID,
|
|
82
|
+
HOOK_DEPLOYER,
|
|
83
|
+
PUBLISHER,
|
|
84
|
+
IJBRulesetDataHook(address(MOCK_BUYBACK)),
|
|
85
|
+
address(LOANS_CONTRACT),
|
|
86
|
+
TRUSTED_FORWARDER
|
|
87
|
+
);
|
|
88
|
+
vm.prank(multisig());
|
|
89
|
+
jbProjects().approve(address(REV_DEPLOYER), FEE_PROJECT_ID);
|
|
90
|
+
_deployFeeProject();
|
|
91
|
+
_deployRevnet();
|
|
92
|
+
vm.deal(USER, 1000e18);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function _deployFeeProject() internal {
|
|
96
|
+
JBAccountingContext[] memory acc = new JBAccountingContext[](2);
|
|
97
|
+
acc[0] = JBAccountingContext({
|
|
98
|
+
token: JBConstants.NATIVE_TOKEN, decimals: 18, currency: uint32(uint160(JBConstants.NATIVE_TOKEN))
|
|
99
|
+
});
|
|
100
|
+
acc[1] = JBAccountingContext({token: address(TOKEN), decimals: 6, currency: uint32(uint160(address(TOKEN)))});
|
|
101
|
+
JBTerminalConfig[] memory tc = new JBTerminalConfig[](1);
|
|
102
|
+
tc[0] = JBTerminalConfig({terminal: jbMultiTerminal(), accountingContextsToAccept: acc});
|
|
103
|
+
REVStageConfig[] memory stages = new REVStageConfig[](1);
|
|
104
|
+
JBSplit[] memory splits = new JBSplit[](1);
|
|
105
|
+
splits[0].beneficiary = payable(multisig());
|
|
106
|
+
splits[0].percent = 10_000;
|
|
107
|
+
REVAutoIssuance[] memory ai = new REVAutoIssuance[](1);
|
|
108
|
+
ai[0] = REVAutoIssuance({chainId: uint32(block.chainid), count: uint104(70_000e18), beneficiary: multisig()});
|
|
109
|
+
stages[0] = REVStageConfig({
|
|
110
|
+
startsAtOrAfter: uint40(block.timestamp),
|
|
111
|
+
autoIssuances: ai,
|
|
112
|
+
splitPercent: 2000,
|
|
113
|
+
splits: splits,
|
|
114
|
+
initialIssuance: uint112(1000e18),
|
|
115
|
+
issuanceCutFrequency: 90 days,
|
|
116
|
+
issuanceCutPercent: JBConstants.MAX_WEIGHT_CUT_PERCENT / 2,
|
|
117
|
+
cashOutTaxRate: 6000,
|
|
118
|
+
extraMetadata: 0
|
|
119
|
+
});
|
|
120
|
+
REVConfig memory cfg = REVConfig({
|
|
121
|
+
description: REVDescription("Revnet", "$REV", "ipfs://test", "REV_TOKEN"),
|
|
122
|
+
baseCurrency: uint32(uint160(JBConstants.NATIVE_TOKEN)),
|
|
123
|
+
splitOperator: multisig(),
|
|
124
|
+
stageConfigurations: stages
|
|
125
|
+
});
|
|
126
|
+
vm.prank(multisig());
|
|
127
|
+
REV_DEPLOYER.deployFor({
|
|
128
|
+
revnetId: FEE_PROJECT_ID,
|
|
129
|
+
configuration: cfg,
|
|
130
|
+
terminalConfigurations: tc,
|
|
131
|
+
suckerDeploymentConfiguration: REVSuckerDeploymentConfig({
|
|
132
|
+
deployerConfigurations: new JBSuckerDeployerConfig[](0), salt: keccak256("FEE")
|
|
133
|
+
})
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function _deployRevnet() internal {
|
|
138
|
+
JBAccountingContext[] memory acc = new JBAccountingContext[](2);
|
|
139
|
+
acc[0] = JBAccountingContext({
|
|
140
|
+
token: JBConstants.NATIVE_TOKEN, decimals: 18, currency: uint32(uint160(JBConstants.NATIVE_TOKEN))
|
|
141
|
+
});
|
|
142
|
+
acc[1] = JBAccountingContext({token: address(TOKEN), decimals: 6, currency: uint32(uint160(address(TOKEN)))});
|
|
143
|
+
JBTerminalConfig[] memory tc = new JBTerminalConfig[](1);
|
|
144
|
+
tc[0] = JBTerminalConfig({terminal: jbMultiTerminal(), accountingContextsToAccept: acc});
|
|
145
|
+
REVStageConfig[] memory stages = new REVStageConfig[](1);
|
|
146
|
+
JBSplit[] memory splits = new JBSplit[](1);
|
|
147
|
+
splits[0].beneficiary = payable(multisig());
|
|
148
|
+
splits[0].percent = 10_000;
|
|
149
|
+
REVAutoIssuance[] memory ai = new REVAutoIssuance[](1);
|
|
150
|
+
ai[0] = REVAutoIssuance({chainId: uint32(block.chainid), count: uint104(70_000e18), beneficiary: multisig()});
|
|
151
|
+
stages[0] = REVStageConfig({
|
|
152
|
+
startsAtOrAfter: uint40(block.timestamp),
|
|
153
|
+
autoIssuances: ai,
|
|
154
|
+
splitPercent: 2000,
|
|
155
|
+
splits: splits,
|
|
156
|
+
initialIssuance: uint112(1000e18),
|
|
157
|
+
issuanceCutFrequency: 90 days,
|
|
158
|
+
issuanceCutPercent: JBConstants.MAX_WEIGHT_CUT_PERCENT / 2,
|
|
159
|
+
cashOutTaxRate: 6000,
|
|
160
|
+
extraMetadata: 0
|
|
161
|
+
});
|
|
162
|
+
REVLoanSource[] memory ls = new REVLoanSource[](1);
|
|
163
|
+
ls[0] = REVLoanSource({token: JBConstants.NATIVE_TOKEN, terminal: jbMultiTerminal()});
|
|
164
|
+
REVConfig memory cfg = REVConfig({
|
|
165
|
+
description: REVDescription("NANA", "$NANA", "ipfs://test2", "NANA_TOKEN"),
|
|
166
|
+
baseCurrency: uint32(uint160(JBConstants.NATIVE_TOKEN)),
|
|
167
|
+
splitOperator: multisig(),
|
|
168
|
+
stageConfigurations: stages
|
|
169
|
+
});
|
|
170
|
+
REVNET_ID = REV_DEPLOYER.deployFor({
|
|
171
|
+
revnetId: 0,
|
|
172
|
+
configuration: cfg,
|
|
173
|
+
terminalConfigurations: tc,
|
|
174
|
+
suckerDeploymentConfiguration: REVSuckerDeploymentConfig({
|
|
175
|
+
deployerConfigurations: new JBSuckerDeployerConfig[](0), salt: keccak256("NANA")
|
|
176
|
+
})
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function _setupLoan(
|
|
181
|
+
address user,
|
|
182
|
+
uint256 ethAmount,
|
|
183
|
+
uint256 prepaidFee
|
|
184
|
+
)
|
|
185
|
+
internal
|
|
186
|
+
returns (uint256 loanId, uint256 tokenCount, uint256 borrowAmount)
|
|
187
|
+
{
|
|
188
|
+
vm.prank(user);
|
|
189
|
+
tokenCount =
|
|
190
|
+
jbMultiTerminal().pay{value: ethAmount}(REVNET_ID, JBConstants.NATIVE_TOKEN, ethAmount, user, 0, "", "");
|
|
191
|
+
borrowAmount =
|
|
192
|
+
LOANS_CONTRACT.borrowableAmountFrom(REVNET_ID, tokenCount, 18, uint32(uint160(JBConstants.NATIVE_TOKEN)));
|
|
193
|
+
if (borrowAmount == 0) return (0, tokenCount, 0);
|
|
194
|
+
mockExpect(
|
|
195
|
+
address(jbPermissions()),
|
|
196
|
+
abi.encodeCall(IJBPermissions.hasPermission, (address(LOANS_CONTRACT), user, REVNET_ID, 11, true, true)),
|
|
197
|
+
abi.encode(true)
|
|
198
|
+
);
|
|
199
|
+
REVLoanSource memory source = REVLoanSource({token: JBConstants.NATIVE_TOKEN, terminal: jbMultiTerminal()});
|
|
200
|
+
vm.prank(user);
|
|
201
|
+
(loanId,) = LOANS_CONTRACT.borrowFrom(REVNET_ID, source, 0, tokenCount, payable(user), prepaidFee);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/// @notice At exactly LOAN_LIQUIDATION_DURATION, determineSourceFeeAmount should revert with LoanExpired (>=
|
|
205
|
+
/// boundary).
|
|
206
|
+
function test_liquidationBoundary_exactDuration_isLiquidatable() public {
|
|
207
|
+
(uint256 loanId,,) = _setupLoan(USER, 10e18, 25);
|
|
208
|
+
require(loanId != 0, "Loan setup failed");
|
|
209
|
+
|
|
210
|
+
REVLoan memory loan = LOANS_CONTRACT.loanOf(loanId);
|
|
211
|
+
|
|
212
|
+
// Warp to exactly LOAN_LIQUIDATION_DURATION after creation
|
|
213
|
+
vm.warp(loan.createdAt + LOANS_CONTRACT.LOAN_LIQUIDATION_DURATION());
|
|
214
|
+
|
|
215
|
+
// With the >= fix, this should revert because timeSinceLoanCreated == LOAN_LIQUIDATION_DURATION
|
|
216
|
+
vm.expectRevert(
|
|
217
|
+
abi.encodeWithSelector(
|
|
218
|
+
REVLoans.REVLoans_LoanExpired.selector,
|
|
219
|
+
LOANS_CONTRACT.LOAN_LIQUIDATION_DURATION(),
|
|
220
|
+
LOANS_CONTRACT.LOAN_LIQUIDATION_DURATION()
|
|
221
|
+
)
|
|
222
|
+
);
|
|
223
|
+
LOANS_CONTRACT.determineSourceFeeAmount(loan, loan.amount);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/// @notice At LOAN_LIQUIDATION_DURATION - 1, the loan should still be manageable (not expired).
|
|
227
|
+
function test_liquidationBoundary_oneBefore_notLiquidatable() public {
|
|
228
|
+
(uint256 loanId,,) = _setupLoan(USER, 10e18, 25);
|
|
229
|
+
require(loanId != 0, "Loan setup failed");
|
|
230
|
+
|
|
231
|
+
REVLoan memory loan = LOANS_CONTRACT.loanOf(loanId);
|
|
232
|
+
|
|
233
|
+
// Warp to one second before the liquidation boundary
|
|
234
|
+
vm.warp(loan.createdAt + LOANS_CONTRACT.LOAN_LIQUIDATION_DURATION() - 1);
|
|
235
|
+
|
|
236
|
+
// This should NOT revert — the loan is still within the liquidation window
|
|
237
|
+
uint256 fee = LOANS_CONTRACT.determineSourceFeeAmount(loan, loan.amount);
|
|
238
|
+
|
|
239
|
+
// Fee should be > 0 since we're past the prepaid duration but before liquidation
|
|
240
|
+
assertTrue(fee > 0, "Fee should be nonzero for a loan past its prepaid period");
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/// @notice Sending ETH to the non-payable reallocateCollateralFromLoan should revert.
|
|
244
|
+
/// @dev Since the function is not payable, Solidity prevents sending ETH at compile time.
|
|
245
|
+
/// We use a low-level call to bypass this and verify the EVM-level revert.
|
|
246
|
+
function test_reallocate_withETHValue_reverts() public {
|
|
247
|
+
(uint256 loanId, uint256 tokenCount,) = _setupLoan(USER, 10e18, 25);
|
|
248
|
+
require(loanId != 0, "Loan setup failed");
|
|
249
|
+
|
|
250
|
+
// Encode the function call
|
|
251
|
+
bytes memory callData = abi.encodeWithSelector(
|
|
252
|
+
LOANS_CONTRACT.reallocateCollateralFromLoan.selector,
|
|
253
|
+
loanId,
|
|
254
|
+
tokenCount / 10,
|
|
255
|
+
REVLoanSource({token: JBConstants.NATIVE_TOKEN, terminal: jbMultiTerminal()}),
|
|
256
|
+
0,
|
|
257
|
+
0,
|
|
258
|
+
USER,
|
|
259
|
+
25
|
|
260
|
+
);
|
|
261
|
+
|
|
262
|
+
// Low-level call with msg.value to bypass Solidity's payable check
|
|
263
|
+
vm.prank(USER);
|
|
264
|
+
(bool success,) = address(LOANS_CONTRACT).call{value: 1 ether}(callData);
|
|
265
|
+
assertFalse(success, "Sending ETH to non-payable reallocate should revert");
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/// @notice Calling reallocateCollateralFromLoan without ETH should work (given valid params).
|
|
269
|
+
function test_reallocate_withoutETHValue_succeeds() public {
|
|
270
|
+
(uint256 loanId, uint256 tokenCount,) = _setupLoan(USER, 10e18, 25);
|
|
271
|
+
require(loanId != 0, "Loan setup failed");
|
|
272
|
+
|
|
273
|
+
// Inflate surplus so collateral removal is viable
|
|
274
|
+
address donor = makeAddr("donor");
|
|
275
|
+
vm.deal(donor, 500e18);
|
|
276
|
+
vm.prank(donor);
|
|
277
|
+
jbMultiTerminal().addToBalanceOf{value: 500e18}(REVNET_ID, JBConstants.NATIVE_TOKEN, 500e18, false, "", "");
|
|
278
|
+
|
|
279
|
+
// Get extra tokens to add as collateral to the new loan
|
|
280
|
+
vm.prank(USER);
|
|
281
|
+
uint256 extraTokens =
|
|
282
|
+
jbMultiTerminal().pay{value: 50e18}(REVNET_ID, JBConstants.NATIVE_TOKEN, 50e18, USER, 0, "", "");
|
|
283
|
+
|
|
284
|
+
uint256 collateralToTransfer = tokenCount / 10;
|
|
285
|
+
|
|
286
|
+
REVLoanSource memory source = REVLoanSource({token: JBConstants.NATIVE_TOKEN, terminal: jbMultiTerminal()});
|
|
287
|
+
|
|
288
|
+
// Mock burn permission
|
|
289
|
+
mockExpect(
|
|
290
|
+
address(jbPermissions()),
|
|
291
|
+
abi.encodeCall(IJBPermissions.hasPermission, (address(LOANS_CONTRACT), USER, REVNET_ID, 11, true, true)),
|
|
292
|
+
abi.encode(true)
|
|
293
|
+
);
|
|
294
|
+
|
|
295
|
+
// Call without msg.value — should succeed
|
|
296
|
+
vm.prank(USER);
|
|
297
|
+
(uint256 reallocatedLoanId, uint256 newLoanId,,) = LOANS_CONTRACT.reallocateCollateralFromLoan(
|
|
298
|
+
loanId, collateralToTransfer, source, 0, extraTokens, payable(USER), 25
|
|
299
|
+
);
|
|
300
|
+
|
|
301
|
+
assertTrue(reallocatedLoanId != 0, "Reallocated loan should exist");
|
|
302
|
+
assertTrue(newLoanId != 0, "New loan should exist");
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// ==================== Mixed-Decimal Normalization Tests ====================
|
|
306
|
+
|
|
307
|
+
uint256 MIXED_REVNET_ID;
|
|
308
|
+
|
|
309
|
+
/// @notice Deploy a revnet with BOTH ETH (18 dec) and TOKEN (6 dec) as loan sources.
|
|
310
|
+
function _deployMixedDecimalRevnet() internal {
|
|
311
|
+
JBAccountingContext[] memory acc = new JBAccountingContext[](2);
|
|
312
|
+
acc[0] = JBAccountingContext({
|
|
313
|
+
token: JBConstants.NATIVE_TOKEN, decimals: 18, currency: uint32(uint160(JBConstants.NATIVE_TOKEN))
|
|
314
|
+
});
|
|
315
|
+
acc[1] = JBAccountingContext({token: address(TOKEN), decimals: 6, currency: uint32(uint160(address(TOKEN)))});
|
|
316
|
+
JBTerminalConfig[] memory tc = new JBTerminalConfig[](1);
|
|
317
|
+
tc[0] = JBTerminalConfig({terminal: jbMultiTerminal(), accountingContextsToAccept: acc});
|
|
318
|
+
REVStageConfig[] memory stages = new REVStageConfig[](1);
|
|
319
|
+
JBSplit[] memory splits = new JBSplit[](1);
|
|
320
|
+
splits[0].beneficiary = payable(multisig());
|
|
321
|
+
splits[0].percent = 10_000;
|
|
322
|
+
REVAutoIssuance[] memory ai = new REVAutoIssuance[](1);
|
|
323
|
+
ai[0] = REVAutoIssuance({chainId: uint32(block.chainid), count: uint104(70_000e18), beneficiary: multisig()});
|
|
324
|
+
// cashOutTaxRate=0 for proportional math (simpler assertions).
|
|
325
|
+
stages[0] = REVStageConfig({
|
|
326
|
+
startsAtOrAfter: uint40(block.timestamp),
|
|
327
|
+
autoIssuances: ai,
|
|
328
|
+
splitPercent: 2000,
|
|
329
|
+
splits: splits,
|
|
330
|
+
initialIssuance: uint112(1000e18),
|
|
331
|
+
issuanceCutFrequency: 90 days,
|
|
332
|
+
issuanceCutPercent: JBConstants.MAX_WEIGHT_CUT_PERCENT / 2,
|
|
333
|
+
cashOutTaxRate: 0,
|
|
334
|
+
extraMetadata: 0
|
|
335
|
+
});
|
|
336
|
+
REVLoanSource[] memory ls = new REVLoanSource[](2);
|
|
337
|
+
ls[0] = REVLoanSource({token: JBConstants.NATIVE_TOKEN, terminal: jbMultiTerminal()});
|
|
338
|
+
ls[1] = REVLoanSource({token: address(TOKEN), terminal: jbMultiTerminal()});
|
|
339
|
+
REVConfig memory cfg = REVConfig({
|
|
340
|
+
description: REVDescription("Mixed", "$MIX", "ipfs://mix", "MIX_TOKEN"),
|
|
341
|
+
baseCurrency: uint32(uint160(JBConstants.NATIVE_TOKEN)),
|
|
342
|
+
splitOperator: multisig(),
|
|
343
|
+
stageConfigurations: stages
|
|
344
|
+
});
|
|
345
|
+
MIXED_REVNET_ID = REV_DEPLOYER.deployFor({
|
|
346
|
+
revnetId: 0,
|
|
347
|
+
configuration: cfg,
|
|
348
|
+
terminalConfigurations: tc,
|
|
349
|
+
suckerDeploymentConfiguration: REVSuckerDeploymentConfig({
|
|
350
|
+
deployerConfigurations: new JBSuckerDeployerConfig[](0), salt: keccak256("MIXED")
|
|
351
|
+
})
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/// @notice When only the ETH source has borrows, TOKEN source (0 borrowed) should be skipped via `continue`.
|
|
356
|
+
/// @dev Exercises the `if (tokensLoaned == 0) continue` path in _totalBorrowedFrom.
|
|
357
|
+
function test_mixedDecimals_zeroBorrowFromTokenSource_continuePath() public {
|
|
358
|
+
_deployMixedDecimalRevnet();
|
|
359
|
+
|
|
360
|
+
// Pay ETH to get project tokens.
|
|
361
|
+
vm.prank(USER);
|
|
362
|
+
uint256 tokenCount =
|
|
363
|
+
jbMultiTerminal().pay{value: 10e18}(MIXED_REVNET_ID, JBConstants.NATIVE_TOKEN, 10e18, USER, 0, "", "");
|
|
364
|
+
|
|
365
|
+
// Borrow from ETH source.
|
|
366
|
+
uint256 borrowable = LOANS_CONTRACT.borrowableAmountFrom(
|
|
367
|
+
MIXED_REVNET_ID, tokenCount, 18, uint32(uint160(JBConstants.NATIVE_TOKEN))
|
|
368
|
+
);
|
|
369
|
+
require(borrowable > 0, "Must have borrowable amount");
|
|
370
|
+
|
|
371
|
+
mockExpect(
|
|
372
|
+
address(jbPermissions()),
|
|
373
|
+
abi.encodeCall(
|
|
374
|
+
IJBPermissions.hasPermission, (address(LOANS_CONTRACT), USER, MIXED_REVNET_ID, 11, true, true)
|
|
375
|
+
),
|
|
376
|
+
abi.encode(true)
|
|
377
|
+
);
|
|
378
|
+
REVLoanSource memory ethSource = REVLoanSource({token: JBConstants.NATIVE_TOKEN, terminal: jbMultiTerminal()});
|
|
379
|
+
vm.prank(USER);
|
|
380
|
+
(uint256 loanId, REVLoan memory loan) =
|
|
381
|
+
LOANS_CONTRACT.borrowFrom(MIXED_REVNET_ID, ethSource, 0, tokenCount, payable(USER), 25);
|
|
382
|
+
|
|
383
|
+
// Verify loan created and TOKEN source has zero borrowed.
|
|
384
|
+
assertTrue(loanId != 0, "ETH loan should be created");
|
|
385
|
+
assertTrue(loan.amount > 0, "Loan amount should be nonzero");
|
|
386
|
+
assertEq(
|
|
387
|
+
LOANS_CONTRACT.totalBorrowedFrom(MIXED_REVNET_ID, jbMultiTerminal(), address(TOKEN)),
|
|
388
|
+
0,
|
|
389
|
+
"TOKEN source should have zero borrowed"
|
|
390
|
+
);
|
|
391
|
+
assertTrue(
|
|
392
|
+
LOANS_CONTRACT.totalBorrowedFrom(MIXED_REVNET_ID, jbMultiTerminal(), JBConstants.NATIVE_TOKEN) > 0,
|
|
393
|
+
"ETH source should have nonzero borrowed"
|
|
394
|
+
);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/// @notice Borrow from TOKEN (6-decimal) source, verify totalBorrowed tracked and borrowable works in 18-dec
|
|
398
|
+
/// context. @dev This is the core normalization test. Without decimal normalization, _totalBorrowedFrom would treat
|
|
399
|
+
/// the 6-decimal TOKEN amount as if it were in 18 decimals, making it ~10^12 times too small.
|
|
400
|
+
function test_mixedDecimals_borrowFromTokenSource_normalizedCorrectly() public {
|
|
401
|
+
_deployMixedDecimalRevnet();
|
|
402
|
+
|
|
403
|
+
// Pay with TOKEN to get project tokens AND create TOKEN surplus in the terminal.
|
|
404
|
+
// Using TOKEN (not ETH) ensures the borrowable amount from TOKEN source stays within
|
|
405
|
+
// the terminal's actual TOKEN balance.
|
|
406
|
+
deal(address(TOKEN), USER, 2_000_000e6);
|
|
407
|
+
vm.prank(USER);
|
|
408
|
+
TOKEN.approve(address(jbMultiTerminal()), type(uint256).max);
|
|
409
|
+
vm.prank(USER);
|
|
410
|
+
uint256 tokenCount = jbMultiTerminal().pay(MIXED_REVNET_ID, address(TOKEN), 1_000_000e6, USER, 0, "", "");
|
|
411
|
+
require(tokenCount > 0, "Must receive project tokens");
|
|
412
|
+
|
|
413
|
+
// Record borrowable BEFORE any borrow (queried in 18-decimal ETH context).
|
|
414
|
+
uint256 smallCollateral = tokenCount / 10;
|
|
415
|
+
uint256 borrowableBefore = LOANS_CONTRACT.borrowableAmountFrom(
|
|
416
|
+
MIXED_REVNET_ID, smallCollateral, 18, uint32(uint160(JBConstants.NATIVE_TOKEN))
|
|
417
|
+
);
|
|
418
|
+
assertTrue(borrowableBefore > 0, "Must have borrowable amount before borrow");
|
|
419
|
+
|
|
420
|
+
// Borrow from TOKEN source using a small portion of collateral.
|
|
421
|
+
mockExpect(
|
|
422
|
+
address(jbPermissions()),
|
|
423
|
+
abi.encodeCall(
|
|
424
|
+
IJBPermissions.hasPermission, (address(LOANS_CONTRACT), USER, MIXED_REVNET_ID, 11, true, true)
|
|
425
|
+
),
|
|
426
|
+
abi.encode(true)
|
|
427
|
+
);
|
|
428
|
+
REVLoanSource memory tokenSource = REVLoanSource({token: address(TOKEN), terminal: jbMultiTerminal()});
|
|
429
|
+
vm.prank(USER);
|
|
430
|
+
(uint256 loanId, REVLoan memory loan) =
|
|
431
|
+
LOANS_CONTRACT.borrowFrom(MIXED_REVNET_ID, tokenSource, 0, smallCollateral, payable(USER), 25);
|
|
432
|
+
|
|
433
|
+
assertTrue(loanId != 0, "TOKEN loan should be created");
|
|
434
|
+
assertTrue(loan.amount > 0, "Loan amount should be nonzero");
|
|
435
|
+
|
|
436
|
+
// Verify totalBorrowedFrom tracks the raw 6-decimal amount.
|
|
437
|
+
uint256 tokenBorrowed = LOANS_CONTRACT.totalBorrowedFrom(MIXED_REVNET_ID, jbMultiTerminal(), address(TOKEN));
|
|
438
|
+
assertEq(tokenBorrowed, loan.amount, "totalBorrowedFrom should match loan amount in source decimals");
|
|
439
|
+
|
|
440
|
+
// Query borrowable for more collateral in 18-decimal ETH context.
|
|
441
|
+
// This calls _totalBorrowedFrom which must normalize the 6-dec TOKEN amount to 18-dec.
|
|
442
|
+
// Without normalization: TOKEN borrow treated as near-zero → borrowable barely changes.
|
|
443
|
+
// With normalization: TOKEN borrow properly scaled → borrowable reflects the outstanding debt.
|
|
444
|
+
uint256 borrowableAfter = LOANS_CONTRACT.borrowableAmountFrom(
|
|
445
|
+
MIXED_REVNET_ID, smallCollateral, 18, uint32(uint160(JBConstants.NATIVE_TOKEN))
|
|
446
|
+
);
|
|
447
|
+
|
|
448
|
+
// borrowableAfter should still be nonzero (the revnet has surplus).
|
|
449
|
+
assertTrue(borrowableAfter > 0, "Should still have borrowable amount after TOKEN borrow");
|
|
450
|
+
|
|
451
|
+
// Also query in TOKEN's own 6-decimal context to verify the same-currency normalization path.
|
|
452
|
+
// Since only TOKEN source has borrows and ETH source has 0 (skipped via `continue`), this is safe.
|
|
453
|
+
uint256 borrowableInToken =
|
|
454
|
+
LOANS_CONTRACT.borrowableAmountFrom(MIXED_REVNET_ID, smallCollateral, 6, uint32(uint160(address(TOKEN))));
|
|
455
|
+
assertTrue(borrowableInToken > 0, "Borrowable in TOKEN terms should be nonzero");
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/// @notice Borrow from both ETH and TOKEN sources, verify both borrows are tracked and aggregated.
|
|
459
|
+
/// @dev TOKEN borrow MUST happen first: when borrowing from TOKEN source, _totalBorrowedFrom is queried
|
|
460
|
+
/// in 6-dec TOKEN context. If ETH had borrows, converting ETH→TOKEN at 6 decimals causes the inverse
|
|
461
|
+
/// price feed to truncate to 0 (mulDiv(1e6,1e6,1e21)=0). Borrowing TOKEN first ensures ETH=0→continue.
|
|
462
|
+
/// The subsequent ETH borrow then exercises cross-decimal normalization (TOKEN 6-dec → ETH 18-dec) using
|
|
463
|
+
/// the DIRECT TOKEN→ETH feed which works at 18 decimals.
|
|
464
|
+
function test_mixedDecimals_borrowFromBothSources_aggregatesCorrectly() public {
|
|
465
|
+
_deployMixedDecimalRevnet();
|
|
466
|
+
|
|
467
|
+
// STEP 1: Pay TOKEN ONLY to get project tokens and create TOKEN-only surplus.
|
|
468
|
+
// This avoids ETH surplus inflating the TOKEN borrowable beyond the terminal's TOKEN balance.
|
|
469
|
+
deal(address(TOKEN), USER, 10_000_000e6);
|
|
470
|
+
vm.prank(USER);
|
|
471
|
+
TOKEN.approve(address(jbMultiTerminal()), type(uint256).max);
|
|
472
|
+
vm.prank(USER);
|
|
473
|
+
uint256 tokenTokenCount = jbMultiTerminal().pay(MIXED_REVNET_ID, address(TOKEN), 5_000_000e6, USER, 0, "", "");
|
|
474
|
+
|
|
475
|
+
// STEP 2: Borrow from TOKEN source (ETH totalBorrowed=0 → skipped via `continue`, no inverse price needed).
|
|
476
|
+
uint256 smallCollateral = tokenTokenCount / 20;
|
|
477
|
+
mockExpect(
|
|
478
|
+
address(jbPermissions()),
|
|
479
|
+
abi.encodeCall(
|
|
480
|
+
IJBPermissions.hasPermission, (address(LOANS_CONTRACT), USER, MIXED_REVNET_ID, 11, true, true)
|
|
481
|
+
),
|
|
482
|
+
abi.encode(true)
|
|
483
|
+
);
|
|
484
|
+
REVLoanSource memory tokenSource = REVLoanSource({token: address(TOKEN), terminal: jbMultiTerminal()});
|
|
485
|
+
vm.prank(USER);
|
|
486
|
+
(uint256 tokenLoanId,) =
|
|
487
|
+
LOANS_CONTRACT.borrowFrom(MIXED_REVNET_ID, tokenSource, 0, smallCollateral, payable(USER), 25);
|
|
488
|
+
assertTrue(tokenLoanId != 0, "TOKEN loan should be created");
|
|
489
|
+
|
|
490
|
+
// STEP 3: Pay ETH to create ETH surplus.
|
|
491
|
+
vm.prank(USER);
|
|
492
|
+
uint256 ethTokenCount =
|
|
493
|
+
jbMultiTerminal().pay{value: 10e18}(MIXED_REVNET_ID, JBConstants.NATIVE_TOKEN, 10e18, USER, 0, "", "");
|
|
494
|
+
|
|
495
|
+
// STEP 4: Borrow from ETH source. Now _totalBorrowedFrom(revnetId, 18, ETH_CURRENCY) must normalize
|
|
496
|
+
// the TOKEN borrow (6 dec) to 18 dec using the DIRECT TOKEN→ETH feed. This is the key normalization test.
|
|
497
|
+
uint256 ethCollateral = ethTokenCount / 20;
|
|
498
|
+
mockExpect(
|
|
499
|
+
address(jbPermissions()),
|
|
500
|
+
abi.encodeCall(
|
|
501
|
+
IJBPermissions.hasPermission, (address(LOANS_CONTRACT), USER, MIXED_REVNET_ID, 11, true, true)
|
|
502
|
+
),
|
|
503
|
+
abi.encode(true)
|
|
504
|
+
);
|
|
505
|
+
REVLoanSource memory ethSource = REVLoanSource({token: JBConstants.NATIVE_TOKEN, terminal: jbMultiTerminal()});
|
|
506
|
+
vm.prank(USER);
|
|
507
|
+
(uint256 ethLoanId,) =
|
|
508
|
+
LOANS_CONTRACT.borrowFrom(MIXED_REVNET_ID, ethSource, 0, ethCollateral, payable(USER), 25);
|
|
509
|
+
assertTrue(ethLoanId != 0, "ETH loan should be created");
|
|
510
|
+
|
|
511
|
+
// Both sources should have tracked borrows.
|
|
512
|
+
uint256 ethBorrowed =
|
|
513
|
+
LOANS_CONTRACT.totalBorrowedFrom(MIXED_REVNET_ID, jbMultiTerminal(), JBConstants.NATIVE_TOKEN);
|
|
514
|
+
uint256 tokenBorrowed = LOANS_CONTRACT.totalBorrowedFrom(MIXED_REVNET_ID, jbMultiTerminal(), address(TOKEN));
|
|
515
|
+
assertTrue(ethBorrowed > 0, "ETH borrow should be tracked");
|
|
516
|
+
assertTrue(tokenBorrowed > 0, "TOKEN borrow should be tracked");
|
|
517
|
+
|
|
518
|
+
// The total number of loans should be 2.
|
|
519
|
+
assertEq(LOANS_CONTRACT.numberOfLoansFor(MIXED_REVNET_ID), 2, "Should have 2 loans");
|
|
520
|
+
|
|
521
|
+
// Query borrowable in 18-decimal ETH context. This exercises _totalBorrowedFrom's
|
|
522
|
+
// cross-decimal normalization: TOKEN borrows (6 dec) must be normalized to 18 dec before
|
|
523
|
+
// aggregation with the direct TOKEN→ETH price feed.
|
|
524
|
+
uint256 borrowableInEth = LOANS_CONTRACT.borrowableAmountFrom(
|
|
525
|
+
MIXED_REVNET_ID, ethCollateral, 18, uint32(uint160(JBConstants.NATIVE_TOKEN))
|
|
526
|
+
);
|
|
527
|
+
assertTrue(borrowableInEth > 0, "Should still have borrowable after both borrows (18-dec ETH)");
|
|
528
|
+
}
|
|
529
|
+
}
|