@rev-net/core-v6 0.0.21 → 0.0.22
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/USER_JOURNEYS.md +5 -4
- package/package.json +1 -1
- package/script/Deploy.s.sol +27 -7
- package/src/REVLoans.sol +14 -4
- package/test/TestZeroAmountLoanGuard.t.sol +365 -0
package/USER_JOURNEYS.md
CHANGED
|
@@ -321,8 +321,9 @@ This is a standard Juicebox payment, but REVOwner intervenes as the data hook.
|
|
|
321
321
|
**What happens:**
|
|
322
322
|
|
|
323
323
|
1. **Authorization:** `_ownerOf(loanId) == _msgSender()`
|
|
324
|
-
2. **
|
|
325
|
-
3.
|
|
324
|
+
2. **Expiry check:** If the loan has expired (`block.timestamp - createdAt > LOAN_LIQUIDATION_DURATION`), reverts with `REVLoans_LoanExpired`. Expired loans can only be liquidated, not reallocated.
|
|
325
|
+
3. **Source match:** New loan source must match existing loan source (prevents cross-source value extraction)
|
|
326
|
+
4. **`_reallocateCollateralFromLoan`:**
|
|
326
327
|
- Burns original loan NFT
|
|
327
328
|
- Validates `collateralCountToTransfer <= loan.collateral`
|
|
328
329
|
- Computes `newCollateralCount = loan.collateral - collateralCountToTransfer`
|
|
@@ -332,8 +333,8 @@ This is a standard Juicebox payment, but REVOwner intervenes as the data hook.
|
|
|
332
333
|
- Calls `_adjust` to reduce collateral (returns excess tokens to caller via `_returnCollateralFrom`)
|
|
333
334
|
- Mints replacement loan NFT to caller
|
|
334
335
|
- Deletes original loan data
|
|
335
|
-
|
|
336
|
-
- The `collateralCountToTransfer` tokens were just re-minted to the caller in step
|
|
336
|
+
5. **`borrowFrom`:** Opens a new loan with `collateralCountToTransfer + collateralCountToAdd` as collateral
|
|
337
|
+
- The `collateralCountToTransfer` tokens were just re-minted to the caller in step 4
|
|
337
338
|
- The `collateralCountToAdd` tokens come from the caller's existing balance
|
|
338
339
|
- Both are burned as collateral for the new loan
|
|
339
340
|
|
package/package.json
CHANGED
package/script/Deploy.s.sol
CHANGED
|
@@ -398,7 +398,8 @@ contract DeployScript is Script, Sphinx {
|
|
|
398
398
|
_singletonsExist = true;
|
|
399
399
|
|
|
400
400
|
// Also predict and verify the owner.
|
|
401
|
-
|
|
401
|
+
bool _ownerDeployed;
|
|
402
|
+
(_existingOwnerAddr, _ownerDeployed) = _isDeployed({
|
|
402
403
|
salt: REVOWNER_SALT,
|
|
403
404
|
creationCode: type(REVOwner).creationCode,
|
|
404
405
|
arguments: abi.encode(
|
|
@@ -411,7 +412,8 @@ contract DeployScript is Script, Sphinx {
|
|
|
411
412
|
});
|
|
412
413
|
|
|
413
414
|
// Also predict and verify the deployer.
|
|
414
|
-
|
|
415
|
+
bool _deployerDeployed;
|
|
416
|
+
(_existingDeployerAddr, _deployerDeployed) = _isDeployed({
|
|
415
417
|
salt: DEPLOYER_SALT,
|
|
416
418
|
creationCode: type(REVDeployer).creationCode,
|
|
417
419
|
arguments: abi.encode(
|
|
@@ -426,6 +428,9 @@ contract DeployScript is Script, Sphinx {
|
|
|
426
428
|
_existingOwnerAddr
|
|
427
429
|
)
|
|
428
430
|
});
|
|
431
|
+
|
|
432
|
+
// If either singleton is missing, we cannot reuse the set.
|
|
433
|
+
if (!_ownerDeployed || !_deployerDeployed) _singletonsExist = false;
|
|
429
434
|
// Stop searching — we found the deployed singletons.
|
|
430
435
|
break;
|
|
431
436
|
}
|
|
@@ -434,8 +439,21 @@ contract DeployScript is Script, Sphinx {
|
|
|
434
439
|
|
|
435
440
|
// Only create a new fee project if no singletons were found.
|
|
436
441
|
if (!_singletonsExist) {
|
|
437
|
-
//
|
|
438
|
-
|
|
442
|
+
// Check if safeAddress() already owns a blank project (from a previous interrupted run).
|
|
443
|
+
bool _foundExisting;
|
|
444
|
+
for (uint256 i = _nextProjectId - 1; i >= 1; i--) {
|
|
445
|
+
if (core.projects.ownerOf(i) == safeAddress()) {
|
|
446
|
+
if (address(core.controller.DIRECTORY().controllerOf(i)) == address(0)) {
|
|
447
|
+
FEE_PROJECT_ID = i;
|
|
448
|
+
_foundExisting = true;
|
|
449
|
+
break;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
if (!_foundExisting) {
|
|
454
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
455
|
+
FEE_PROJECT_ID = core.projects.createFor(safeAddress());
|
|
456
|
+
}
|
|
439
457
|
}
|
|
440
458
|
|
|
441
459
|
// Deploy REVLoans first — it only depends on the controller.
|
|
@@ -491,9 +509,11 @@ contract DeployScript is Script, Sphinx {
|
|
|
491
509
|
owner: address(revOwner)
|
|
492
510
|
});
|
|
493
511
|
|
|
494
|
-
// Only configure the fee project if
|
|
495
|
-
//
|
|
496
|
-
|
|
512
|
+
// Only configure the fee project if it doesn't already have a controller.
|
|
513
|
+
// This handles both fresh deploys and restarts where singletons exist but the fee project
|
|
514
|
+
// was not yet configured.
|
|
515
|
+
bool _feeProjectConfigured = address(core.controller.DIRECTORY().controllerOf(FEE_PROJECT_ID)) != address(0);
|
|
516
|
+
if (!_feeProjectConfigured) {
|
|
497
517
|
// Approve the basic deployer to configure the project.
|
|
498
518
|
core.projects.approve({to: address(_basicDeployer), tokenId: FEE_PROJECT_ID});
|
|
499
519
|
|
package/src/REVLoans.sol
CHANGED
|
@@ -759,6 +759,11 @@ contract REVLoans is ERC721, ERC2771Context, Ownable, IREVLoans {
|
|
|
759
759
|
// Make sure only the loan's owner can manage it.
|
|
760
760
|
if (_ownerOf(loanId) != _msgSender()) revert REVLoans_Unauthorized(_msgSender(), _ownerOf(loanId));
|
|
761
761
|
|
|
762
|
+
// Make sure the loan hasn't expired.
|
|
763
|
+
if (block.timestamp - _loanOf[loanId].createdAt > LOAN_LIQUIDATION_DURATION) {
|
|
764
|
+
revert REVLoans_LoanExpired(block.timestamp - _loanOf[loanId].createdAt, LOAN_LIQUIDATION_DURATION);
|
|
765
|
+
}
|
|
766
|
+
|
|
762
767
|
// Make sure the new loan's source matches the existing loan's source to prevent cross-source value extraction.
|
|
763
768
|
{
|
|
764
769
|
REVLoanSource storage existingSource = _loanOf[loanId].source;
|
|
@@ -831,6 +836,11 @@ contract REVLoans is ERC721, ERC2771Context, Ownable, IREVLoans {
|
|
|
831
836
|
loan: loan, revnetId: revnetId, collateralCount: loan.collateral - collateralCountToReturn
|
|
832
837
|
});
|
|
833
838
|
|
|
839
|
+
// If the remaining collateral yields zero borrow amount, treat as full repay.
|
|
840
|
+
if (newBorrowAmount == 0) {
|
|
841
|
+
collateralCountToReturn = loan.collateral;
|
|
842
|
+
}
|
|
843
|
+
|
|
834
844
|
// Make sure the new borrow amount is less than the loan's amount.
|
|
835
845
|
if (newBorrowAmount > loan.amount) {
|
|
836
846
|
revert REVLoans_NewBorrowAmountGreaterThanLoanAmount(newBorrowAmount, loan.amount);
|
|
@@ -1362,7 +1372,10 @@ contract REVLoans is ERC721, ERC2771Context, Ownable, IREVLoans {
|
|
|
1362
1372
|
paidOffLoan.prepaidDuration = loan.prepaidDuration;
|
|
1363
1373
|
paidOffLoan.source = loan.source;
|
|
1364
1374
|
|
|
1365
|
-
//
|
|
1375
|
+
// Mint the replacement loan FIRST so it exists before _adjust writes data.
|
|
1376
|
+
_mint({to: _msgSender(), tokenId: paidOffLoanId});
|
|
1377
|
+
|
|
1378
|
+
// Then adjust the loan data.
|
|
1366
1379
|
_adjust({
|
|
1367
1380
|
loan: paidOffLoan,
|
|
1368
1381
|
revnetId: revnetId,
|
|
@@ -1372,9 +1385,6 @@ contract REVLoans is ERC721, ERC2771Context, Ownable, IREVLoans {
|
|
|
1372
1385
|
beneficiary: beneficiary
|
|
1373
1386
|
});
|
|
1374
1387
|
|
|
1375
|
-
// Mint the replacement loan.
|
|
1376
|
-
_mint({to: _msgSender(), tokenId: paidOffLoanId});
|
|
1377
|
-
|
|
1378
1388
|
emit RepayLoan({
|
|
1379
1389
|
loanId: loanId,
|
|
1380
1390
|
revnetId: revnetId,
|
|
@@ -0,0 +1,365 @@
|
|
|
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
|
+
// 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
|
+
import {REVEmpty721Config} from "./helpers/REVEmpty721Config.sol";
|
|
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
|
+
import {JBConstants} from "@bananapus/core-v6/src/libraries/JBConstants.sol";
|
|
25
|
+
import {JBAccountingContext} from "@bananapus/core-v6/src/structs/JBAccountingContext.sol";
|
|
26
|
+
import {JBSingleAllowance} from "@bananapus/core-v6/src/structs/JBSingleAllowance.sol";
|
|
27
|
+
import {MockPriceFeed} from "@bananapus/core-v6/test/mock/MockPriceFeed.sol";
|
|
28
|
+
import {MockERC20} from "@bananapus/core-v6/test/mock/MockERC20.sol";
|
|
29
|
+
import {REVLoans} from "../src/REVLoans.sol";
|
|
30
|
+
import {REVLoan} from "../src/structs/REVLoan.sol";
|
|
31
|
+
import {REVStageConfig, REVAutoIssuance} from "../src/structs/REVStageConfig.sol";
|
|
32
|
+
import {REVLoanSource} from "../src/structs/REVLoanSource.sol";
|
|
33
|
+
import {REVDescription} from "../src/structs/REVDescription.sol";
|
|
34
|
+
import {IREVLoans} from "./../src/interfaces/IREVLoans.sol";
|
|
35
|
+
import {JBSuckerDeployerConfig} from "@bananapus/suckers-v6/src/structs/JBSuckerDeployerConfig.sol";
|
|
36
|
+
import {JBSuckerRegistry} from "@bananapus/suckers-v6/src/JBSuckerRegistry.sol";
|
|
37
|
+
import {JB721TiersHookDeployer} from "@bananapus/721-hook-v6/src/JB721TiersHookDeployer.sol";
|
|
38
|
+
import {JB721TiersHook} from "@bananapus/721-hook-v6/src/JB721TiersHook.sol";
|
|
39
|
+
import {JB721TiersHookStore} from "@bananapus/721-hook-v6/src/JB721TiersHookStore.sol";
|
|
40
|
+
import {JBAddressRegistry} from "@bananapus/address-registry-v6/src/JBAddressRegistry.sol";
|
|
41
|
+
import {IJBAddressRegistry} from "@bananapus/address-registry-v6/src/interfaces/IJBAddressRegistry.sol";
|
|
42
|
+
import {REVOwner} from "../src/REVOwner.sol";
|
|
43
|
+
import {IREVDeployer} from "../src/interfaces/IREVDeployer.sol";
|
|
44
|
+
import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol";
|
|
45
|
+
|
|
46
|
+
/// @notice Tests for PR #105: zero-amount loan guard and mint-before-adjust ordering.
|
|
47
|
+
contract TestZeroAmountLoanGuard is TestBaseWorkflow {
|
|
48
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
49
|
+
bytes32 REV_DEPLOYER_SALT = "REVDeployer";
|
|
50
|
+
|
|
51
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
52
|
+
REVDeployer REV_DEPLOYER;
|
|
53
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
54
|
+
REVOwner REV_OWNER;
|
|
55
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
56
|
+
JB721TiersHook EXAMPLE_HOOK;
|
|
57
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
58
|
+
IJB721TiersHookDeployer HOOK_DEPLOYER;
|
|
59
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
60
|
+
IJB721TiersHookStore HOOK_STORE;
|
|
61
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
62
|
+
IJBAddressRegistry ADDRESS_REGISTRY;
|
|
63
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
64
|
+
IREVLoans LOANS_CONTRACT;
|
|
65
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
66
|
+
MockERC20 TOKEN;
|
|
67
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
68
|
+
IJBSuckerRegistry SUCKER_REGISTRY;
|
|
69
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
70
|
+
CTPublisher PUBLISHER;
|
|
71
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
72
|
+
MockBuybackDataHook MOCK_BUYBACK;
|
|
73
|
+
|
|
74
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
75
|
+
uint256 FEE_PROJECT_ID;
|
|
76
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
77
|
+
uint256 REVNET_ID;
|
|
78
|
+
|
|
79
|
+
// forge-lint: disable-next-line(mixed-case-variable)
|
|
80
|
+
address USER = makeAddr("user");
|
|
81
|
+
|
|
82
|
+
address private constant TRUSTED_FORWARDER = 0xB2b5841DBeF766d4b521221732F9B618fCf34A87;
|
|
83
|
+
|
|
84
|
+
function setUp() public override {
|
|
85
|
+
super.setUp();
|
|
86
|
+
FEE_PROJECT_ID = jbProjects().createFor(multisig());
|
|
87
|
+
SUCKER_REGISTRY = new JBSuckerRegistry(jbDirectory(), jbPermissions(), multisig(), address(0));
|
|
88
|
+
HOOK_STORE = new JB721TiersHookStore();
|
|
89
|
+
EXAMPLE_HOOK = new JB721TiersHook(
|
|
90
|
+
jbDirectory(), jbPermissions(), jbPrices(), jbRulesets(), HOOK_STORE, jbSplits(), multisig()
|
|
91
|
+
);
|
|
92
|
+
ADDRESS_REGISTRY = new JBAddressRegistry();
|
|
93
|
+
HOOK_DEPLOYER = new JB721TiersHookDeployer(EXAMPLE_HOOK, HOOK_STORE, ADDRESS_REGISTRY, multisig());
|
|
94
|
+
PUBLISHER = new CTPublisher(jbDirectory(), jbPermissions(), FEE_PROJECT_ID, multisig());
|
|
95
|
+
MOCK_BUYBACK = new MockBuybackDataHook();
|
|
96
|
+
TOKEN = new MockERC20("1/2 ETH", "1/2");
|
|
97
|
+
MockPriceFeed priceFeed = new MockPriceFeed(1e21, 6);
|
|
98
|
+
vm.prank(multisig());
|
|
99
|
+
jbPrices()
|
|
100
|
+
.addPriceFeedFor(0, uint32(uint160(address(TOKEN))), uint32(uint160(JBConstants.NATIVE_TOKEN)), priceFeed);
|
|
101
|
+
LOANS_CONTRACT = new REVLoans({
|
|
102
|
+
controller: jbController(),
|
|
103
|
+
projects: jbProjects(),
|
|
104
|
+
revId: FEE_PROJECT_ID,
|
|
105
|
+
owner: address(this),
|
|
106
|
+
permit2: permit2(),
|
|
107
|
+
trustedForwarder: TRUSTED_FORWARDER
|
|
108
|
+
});
|
|
109
|
+
REV_OWNER = new REVOwner(
|
|
110
|
+
IJBBuybackHookRegistry(address(MOCK_BUYBACK)),
|
|
111
|
+
jbDirectory(),
|
|
112
|
+
FEE_PROJECT_ID,
|
|
113
|
+
SUCKER_REGISTRY,
|
|
114
|
+
address(LOANS_CONTRACT)
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
REV_DEPLOYER = new REVDeployer{salt: REV_DEPLOYER_SALT}(
|
|
118
|
+
jbController(),
|
|
119
|
+
SUCKER_REGISTRY,
|
|
120
|
+
FEE_PROJECT_ID,
|
|
121
|
+
HOOK_DEPLOYER,
|
|
122
|
+
PUBLISHER,
|
|
123
|
+
IJBBuybackHookRegistry(address(MOCK_BUYBACK)),
|
|
124
|
+
address(LOANS_CONTRACT),
|
|
125
|
+
TRUSTED_FORWARDER,
|
|
126
|
+
address(REV_OWNER)
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
vm.prank(multisig());
|
|
130
|
+
jbProjects().approve(address(REV_DEPLOYER), FEE_PROJECT_ID);
|
|
131
|
+
_deployFeeProject();
|
|
132
|
+
_deployRevnet();
|
|
133
|
+
vm.deal(USER, 1000e18);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function _deployFeeProject() internal {
|
|
137
|
+
JBAccountingContext[] memory acc = new JBAccountingContext[](2);
|
|
138
|
+
acc[0] = JBAccountingContext({
|
|
139
|
+
token: JBConstants.NATIVE_TOKEN, decimals: 18, currency: uint32(uint160(JBConstants.NATIVE_TOKEN))
|
|
140
|
+
});
|
|
141
|
+
acc[1] = JBAccountingContext({token: address(TOKEN), decimals: 6, currency: uint32(uint160(address(TOKEN)))});
|
|
142
|
+
JBTerminalConfig[] memory tc = new JBTerminalConfig[](1);
|
|
143
|
+
tc[0] = JBTerminalConfig({terminal: jbMultiTerminal(), accountingContextsToAccept: acc});
|
|
144
|
+
REVStageConfig[] memory stages = new REVStageConfig[](1);
|
|
145
|
+
JBSplit[] memory splits = new JBSplit[](1);
|
|
146
|
+
splits[0].beneficiary = payable(multisig());
|
|
147
|
+
splits[0].percent = 10_000;
|
|
148
|
+
REVAutoIssuance[] memory ai = new REVAutoIssuance[](1);
|
|
149
|
+
ai[0] = REVAutoIssuance({chainId: uint32(block.chainid), count: uint104(70_000e18), beneficiary: multisig()});
|
|
150
|
+
stages[0] = REVStageConfig({
|
|
151
|
+
startsAtOrAfter: uint40(block.timestamp),
|
|
152
|
+
autoIssuances: ai,
|
|
153
|
+
splitPercent: 2000,
|
|
154
|
+
splits: splits,
|
|
155
|
+
initialIssuance: uint112(1000e18),
|
|
156
|
+
issuanceCutFrequency: 90 days,
|
|
157
|
+
issuanceCutPercent: JBConstants.MAX_WEIGHT_CUT_PERCENT / 2,
|
|
158
|
+
cashOutTaxRate: 6000,
|
|
159
|
+
extraMetadata: 0
|
|
160
|
+
});
|
|
161
|
+
REVConfig memory cfg = REVConfig({
|
|
162
|
+
// forge-lint: disable-next-line(named-struct-fields)
|
|
163
|
+
description: REVDescription("Revnet", "$REV", "ipfs://test", "REV_TOKEN"),
|
|
164
|
+
baseCurrency: uint32(uint160(JBConstants.NATIVE_TOKEN)),
|
|
165
|
+
splitOperator: multisig(),
|
|
166
|
+
stageConfigurations: stages
|
|
167
|
+
});
|
|
168
|
+
vm.prank(multisig());
|
|
169
|
+
REV_DEPLOYER.deployFor({
|
|
170
|
+
revnetId: FEE_PROJECT_ID,
|
|
171
|
+
configuration: cfg,
|
|
172
|
+
terminalConfigurations: tc,
|
|
173
|
+
suckerDeploymentConfiguration: REVSuckerDeploymentConfig({
|
|
174
|
+
deployerConfigurations: new JBSuckerDeployerConfig[](0), salt: keccak256("FEE")
|
|
175
|
+
}),
|
|
176
|
+
tiered721HookConfiguration: REVEmpty721Config.empty721Config(uint32(uint160(JBConstants.NATIVE_TOKEN))),
|
|
177
|
+
allowedPosts: REVEmpty721Config.emptyAllowedPosts()
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function _deployRevnet() internal {
|
|
182
|
+
JBAccountingContext[] memory acc = new JBAccountingContext[](2);
|
|
183
|
+
acc[0] = JBAccountingContext({
|
|
184
|
+
token: JBConstants.NATIVE_TOKEN, decimals: 18, currency: uint32(uint160(JBConstants.NATIVE_TOKEN))
|
|
185
|
+
});
|
|
186
|
+
acc[1] = JBAccountingContext({token: address(TOKEN), decimals: 6, currency: uint32(uint160(address(TOKEN)))});
|
|
187
|
+
JBTerminalConfig[] memory tc = new JBTerminalConfig[](1);
|
|
188
|
+
tc[0] = JBTerminalConfig({terminal: jbMultiTerminal(), accountingContextsToAccept: acc});
|
|
189
|
+
REVStageConfig[] memory stages = new REVStageConfig[](1);
|
|
190
|
+
JBSplit[] memory splits = new JBSplit[](1);
|
|
191
|
+
splits[0].beneficiary = payable(multisig());
|
|
192
|
+
splits[0].percent = 10_000;
|
|
193
|
+
REVAutoIssuance[] memory ai = new REVAutoIssuance[](1);
|
|
194
|
+
ai[0] = REVAutoIssuance({chainId: uint32(block.chainid), count: uint104(70_000e18), beneficiary: multisig()});
|
|
195
|
+
stages[0] = REVStageConfig({
|
|
196
|
+
startsAtOrAfter: uint40(block.timestamp),
|
|
197
|
+
autoIssuances: ai,
|
|
198
|
+
splitPercent: 2000,
|
|
199
|
+
splits: splits,
|
|
200
|
+
initialIssuance: uint112(1000e18),
|
|
201
|
+
issuanceCutFrequency: 90 days,
|
|
202
|
+
issuanceCutPercent: JBConstants.MAX_WEIGHT_CUT_PERCENT / 2,
|
|
203
|
+
cashOutTaxRate: 6000,
|
|
204
|
+
extraMetadata: 0
|
|
205
|
+
});
|
|
206
|
+
REVLoanSource[] memory ls = new REVLoanSource[](1);
|
|
207
|
+
ls[0] = REVLoanSource({token: JBConstants.NATIVE_TOKEN, terminal: jbMultiTerminal()});
|
|
208
|
+
REVConfig memory cfg = REVConfig({
|
|
209
|
+
// forge-lint: disable-next-line(named-struct-fields)
|
|
210
|
+
description: REVDescription("NANA", "$NANA", "ipfs://test2", "NANA_TOKEN"),
|
|
211
|
+
baseCurrency: uint32(uint160(JBConstants.NATIVE_TOKEN)),
|
|
212
|
+
splitOperator: multisig(),
|
|
213
|
+
stageConfigurations: stages
|
|
214
|
+
});
|
|
215
|
+
(REVNET_ID,) = REV_DEPLOYER.deployFor({
|
|
216
|
+
revnetId: 0,
|
|
217
|
+
configuration: cfg,
|
|
218
|
+
terminalConfigurations: tc,
|
|
219
|
+
suckerDeploymentConfiguration: REVSuckerDeploymentConfig({
|
|
220
|
+
deployerConfigurations: new JBSuckerDeployerConfig[](0), salt: keccak256("NANA")
|
|
221
|
+
}),
|
|
222
|
+
tiered721HookConfiguration: REVEmpty721Config.empty721Config(uint32(uint160(JBConstants.NATIVE_TOKEN))),
|
|
223
|
+
allowedPosts: REVEmpty721Config.emptyAllowedPosts()
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function _setupLoan(
|
|
228
|
+
address user,
|
|
229
|
+
uint256 ethAmount,
|
|
230
|
+
uint256 prepaidFee
|
|
231
|
+
)
|
|
232
|
+
internal
|
|
233
|
+
returns (uint256 loanId, uint256 tokenCount, uint256 borrowAmount)
|
|
234
|
+
{
|
|
235
|
+
vm.prank(user);
|
|
236
|
+
tokenCount =
|
|
237
|
+
jbMultiTerminal().pay{value: ethAmount}(REVNET_ID, JBConstants.NATIVE_TOKEN, ethAmount, user, 0, "", "");
|
|
238
|
+
borrowAmount =
|
|
239
|
+
LOANS_CONTRACT.borrowableAmountFrom(REVNET_ID, tokenCount, 18, uint32(uint160(JBConstants.NATIVE_TOKEN)));
|
|
240
|
+
if (borrowAmount == 0) return (0, tokenCount, 0);
|
|
241
|
+
mockExpect(
|
|
242
|
+
address(jbPermissions()),
|
|
243
|
+
abi.encodeCall(IJBPermissions.hasPermission, (address(LOANS_CONTRACT), user, REVNET_ID, 11, true, true)),
|
|
244
|
+
abi.encode(true)
|
|
245
|
+
);
|
|
246
|
+
REVLoanSource memory source = REVLoanSource({token: JBConstants.NATIVE_TOKEN, terminal: jbMultiTerminal()});
|
|
247
|
+
vm.prank(user);
|
|
248
|
+
(loanId,) = LOANS_CONTRACT.borrowFrom(REVNET_ID, source, 0, tokenCount, payable(user), prepaidFee);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// -----------------------------------------------------------------------
|
|
252
|
+
// 1. Zero-amount loan guard: partial repay that rounds to zero triggers full repay
|
|
253
|
+
// -----------------------------------------------------------------------
|
|
254
|
+
|
|
255
|
+
/// @notice When a partial repay would create a zero-amount loan (newBorrowAmount rounds to 0),
|
|
256
|
+
/// the guard should automatically return ALL collateral (full repay path).
|
|
257
|
+
/// Without the guard, a zero-amount loan with leftover collateral would be unrepayable.
|
|
258
|
+
function test_zeroAmountGuard_partialRepayBecomesFullRepay() public {
|
|
259
|
+
// Setup: borrow against 10 ETH
|
|
260
|
+
(uint256 loanId,,) = _setupLoan(USER, 10e18, 25);
|
|
261
|
+
require(loanId != 0, "Loan setup failed");
|
|
262
|
+
|
|
263
|
+
REVLoan memory loan = LOANS_CONTRACT.loanOf(loanId);
|
|
264
|
+
uint256 originalCollateral = loan.collateral;
|
|
265
|
+
|
|
266
|
+
// Drain most surplus so remaining collateral yields ~0 borrow amount.
|
|
267
|
+
// Cash out most of the surplus from other participants to reduce it drastically.
|
|
268
|
+
// Instead, we'll return nearly all collateral, leaving just 1 token.
|
|
269
|
+
// With very low remaining collateral and bonding curve rounding, newBorrowAmount = 0.
|
|
270
|
+
uint256 collateralToReturn = originalCollateral - 1; // Return all but 1 token
|
|
271
|
+
|
|
272
|
+
JBSingleAllowance memory allowance;
|
|
273
|
+
|
|
274
|
+
vm.prank(USER);
|
|
275
|
+
(uint256 paidOffLoanId, REVLoan memory paidOffLoan) = LOANS_CONTRACT.repayLoan{value: loan.amount}(
|
|
276
|
+
loanId,
|
|
277
|
+
loan.amount, // generous max repay
|
|
278
|
+
collateralToReturn,
|
|
279
|
+
payable(USER),
|
|
280
|
+
allowance
|
|
281
|
+
);
|
|
282
|
+
|
|
283
|
+
// Check: if newBorrowAmount was 0, the guard should have triggered full repayment.
|
|
284
|
+
// This means paidOffLoanId == loanId (original ID returned for full repays)
|
|
285
|
+
// AND paidOffLoan.collateral == 0 AND paidOffLoan.amount == 0.
|
|
286
|
+
if (paidOffLoanId == loanId) {
|
|
287
|
+
// Full repay path was taken — guard worked
|
|
288
|
+
assertEq(paidOffLoan.collateral, 0, "Full repay should have zero collateral");
|
|
289
|
+
assertEq(paidOffLoan.amount, 0, "Full repay should have zero amount");
|
|
290
|
+
} else {
|
|
291
|
+
// Partial repay path — newBorrowAmount was nonzero (rounding didn't hit zero)
|
|
292
|
+
// This is also valid, just means the guard wasn't needed in this case.
|
|
293
|
+
assertTrue(paidOffLoan.amount > 0, "Partial repay should have nonzero amount");
|
|
294
|
+
assertTrue(paidOffLoan.collateral > 0, "Partial repay should have nonzero collateral");
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// -----------------------------------------------------------------------
|
|
299
|
+
// 2. Mint-before-adjust: replacement loan NFT exists before external calls
|
|
300
|
+
// -----------------------------------------------------------------------
|
|
301
|
+
|
|
302
|
+
/// @notice A partial repay (not returning all collateral) should produce a valid new loan NFT
|
|
303
|
+
/// with correct ownership established before any external calls from _adjust.
|
|
304
|
+
function test_mintBeforeAdjust_partialRepayCreatesValidLoan() public {
|
|
305
|
+
// Setup: borrow against 10 ETH
|
|
306
|
+
(uint256 loanId,,) = _setupLoan(USER, 10e18, 25);
|
|
307
|
+
require(loanId != 0, "Loan setup failed");
|
|
308
|
+
|
|
309
|
+
REVLoan memory loan = LOANS_CONTRACT.loanOf(loanId);
|
|
310
|
+
uint256 collateralToReturn = loan.collateral / 2;
|
|
311
|
+
|
|
312
|
+
JBSingleAllowance memory allowance;
|
|
313
|
+
|
|
314
|
+
vm.prank(USER);
|
|
315
|
+
(uint256 paidOffLoanId, REVLoan memory paidOffLoan) = LOANS_CONTRACT.repayLoan{value: loan.amount}(
|
|
316
|
+
loanId, loan.amount, collateralToReturn, payable(USER), allowance
|
|
317
|
+
);
|
|
318
|
+
|
|
319
|
+
// Partial repay should create a new loan (different ID)
|
|
320
|
+
assertTrue(paidOffLoanId != loanId, "Partial repay should create new loan ID");
|
|
321
|
+
|
|
322
|
+
// New loan should be owned by the caller
|
|
323
|
+
assertEq(
|
|
324
|
+
IERC721(address(LOANS_CONTRACT)).ownerOf(paidOffLoanId), USER, "New loan NFT should be owned by caller"
|
|
325
|
+
);
|
|
326
|
+
|
|
327
|
+
// New loan should have reduced collateral and amount
|
|
328
|
+
assertTrue(paidOffLoan.collateral < loan.collateral, "New loan should have less collateral");
|
|
329
|
+
assertTrue(paidOffLoan.amount < loan.amount, "New loan should have less borrow amount");
|
|
330
|
+
assertTrue(paidOffLoan.collateral > 0, "New loan should have nonzero collateral");
|
|
331
|
+
assertTrue(paidOffLoan.amount > 0, "New loan should have nonzero borrow amount");
|
|
332
|
+
|
|
333
|
+
// Original loan NFT should be burned
|
|
334
|
+
vm.expectRevert();
|
|
335
|
+
IERC721(address(LOANS_CONTRACT)).ownerOf(loanId);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// -----------------------------------------------------------------------
|
|
339
|
+
// 3. Full repay still works correctly (returns original loanId)
|
|
340
|
+
// -----------------------------------------------------------------------
|
|
341
|
+
|
|
342
|
+
/// @notice Full repayment (returning all collateral) should return the original loanId
|
|
343
|
+
/// and zero out the loan. This path doesn't create a new NFT.
|
|
344
|
+
function test_fullRepay_returnsOriginalLoanId() public {
|
|
345
|
+
(uint256 loanId,,) = _setupLoan(USER, 10e18, 25);
|
|
346
|
+
require(loanId != 0, "Loan setup failed");
|
|
347
|
+
|
|
348
|
+
REVLoan memory loan = LOANS_CONTRACT.loanOf(loanId);
|
|
349
|
+
|
|
350
|
+
JBSingleAllowance memory allowance;
|
|
351
|
+
|
|
352
|
+
vm.prank(USER);
|
|
353
|
+
(uint256 paidOffLoanId, REVLoan memory paidOffLoan) = LOANS_CONTRACT.repayLoan{value: loan.amount}(
|
|
354
|
+
loanId,
|
|
355
|
+
loan.amount,
|
|
356
|
+
loan.collateral, // return ALL collateral
|
|
357
|
+
payable(USER),
|
|
358
|
+
allowance
|
|
359
|
+
);
|
|
360
|
+
|
|
361
|
+
assertEq(paidOffLoanId, loanId, "Full repay should return original loan ID");
|
|
362
|
+
assertEq(paidOffLoan.amount, 0, "Full repay should zero out amount");
|
|
363
|
+
assertEq(paidOffLoan.collateral, 0, "Full repay should zero out collateral");
|
|
364
|
+
}
|
|
365
|
+
}
|