Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disallow ramping the LTV up #62

Merged
merged 3 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/EVault/modules/Governance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,13 @@ abstract contract GovernanceModule is IGovernance, Base, BalanceUtils, BorrowUti
// self-collateralization is not allowed
if (collateral == address(this)) revert E_InvalidLTVAsset();

ConfigAmount newLTVAmount = ltv.toConfigAmount();
LTVConfig memory origLTV = marketStorage.ltvLookup[collateral];
LTVConfig memory newLTV = origLTV.setLTV(ltv.toConfigAmount(), rampDuration);

// If new LTV is higher than the previous, or the same, it should take effect immediately
if (!(newLTVAmount < origLTV.getLTV(LTVType.LIQUIDATION)) && rampDuration > 0) revert E_LTVRamp();

LTVConfig memory newLTV = origLTV.setLTV(newLTVAmount, rampDuration);

marketStorage.ltvLookup[collateral] = newLTV;

Expand Down
1 change: 1 addition & 0 deletions src/EVault/shared/Errors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ contract Errors {
error E_InvalidConfigAmount();
error E_BadAssetReceiver();
error E_BadSharesReceiver();
error E_LTVRamp();
}
12 changes: 6 additions & 6 deletions src/EVault/shared/types/LTVConfig.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ library LTVConfigLib {
}

function getLTV(LTVConfig memory self, LTVType ltvType) internal view returns (ConfigAmount) {
if (ltvType == LTVType.BORROWING || block.timestamp >= self.targetTimestamp) return self.targetLTV;
if (
ltvType == LTVType.BORROWING || block.timestamp >= self.targetTimestamp || self.targetLTV > self.originalLTV
) {
return self.targetLTV;
}

uint256 ltv = self.originalLTV.toUint16();

unchecked {
uint256 timeElapsed = self.rampDuration - (self.targetTimestamp - block.timestamp);

if (self.targetLTV > self.originalLTV) {
ltv += ((self.targetLTV.toUint16() - self.originalLTV.toUint16()) * timeElapsed / self.rampDuration);
} else {
ltv -= ((self.originalLTV.toUint16() - self.targetLTV.toUint16()) * timeElapsed / self.rampDuration);
}
ltv = ltv - ((ltv - self.targetLTV.toUint16()) * timeElapsed / self.rampDuration);
}

return ConfigAmount.wrap(uint16(ltv));
Expand Down
56 changes: 27 additions & 29 deletions test/unit/evault/modules/Vault/ltv.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,52 +50,50 @@ contract VaultTest_LTV is EVaultTestBase {
}

function test_rampUp() public {
// ramping up is not allowed
vm.expectRevert(Errors.E_LTVRamp.selector);
eTST.setLTV(address(eTST2), 0.8e4, 1000);

assertEq(eTST.borrowingLTV(address(eTST2)), 0.8e4);
assertEq(eTST.liquidationLTV(address(eTST2)), 0.0e4);

skip(250);
eTST.setLTV(address(eTST2), 0.8e4, 0);

assertEq(eTST.borrowingLTV(address(eTST2)), 0.8e4);
assertEq(eTST.liquidationLTV(address(eTST2)), 0.2e4);
// ramping to stay the same is not allowed
vm.expectRevert(Errors.E_LTVRamp.selector);
eTST.setLTV(address(eTST2), 0.8e4, 1000);

skip(500);
eTST.setLTV(address(eTST2), 0.1e4, 1000);

assertEq(eTST.borrowingLTV(address(eTST2)), 0.8e4);
assertEq(eTST.liquidationLTV(address(eTST2)), 0.6e4);
skip(250);

skip(5000);
assertEq(eTST.borrowingLTV(address(eTST2)), 0.1e4);
assertEq(eTST.liquidationLTV(address(eTST2)), 0.625e4);

assertEq(eTST.borrowingLTV(address(eTST2)), 0.8e4);
assertEq(eTST.liquidationLTV(address(eTST2)), 0.8e4);
}
// ramp up on a way down is not allowed
vm.expectRevert(Errors.E_LTVRamp.selector);
eTST.setLTV(address(eTST2), 0.65e4, 1000);

function test_rampRetarget() public {
eTST.setLTV(address(eTST2), 0.8e4, 1000);
// can jump immediatelly
eTST.setLTV(address(eTST2), 0.65e4, 0);

assertEq(eTST.borrowingLTV(address(eTST2)), 0.8e4);
assertEq(eTST.liquidationLTV(address(eTST2)), 0.0e4);
// ramp down again
eTST.setLTV(address(eTST2), 0.1e4, 1000);

skip(250);

assertEq(eTST.borrowingLTV(address(eTST2)), 0.8e4);
assertEq(eTST.liquidationLTV(address(eTST2)), 0.2e4);

eTST.setLTV(address(eTST2), 0.1e4, 1000);
assertEq(eTST.liquidationLTV(address(eTST2)), 0.5125e4);

assertEq(eTST.borrowingLTV(address(eTST2)), 0.1e4);
assertEq(eTST.liquidationLTV(address(eTST2)), 0.2e4);
// can retarget - set a lower LTV with a new ramp
eTST.setLTV(address(eTST2), 0.5e4, 100);

skip(500);
skip(50);

assertEq(eTST.borrowingLTV(address(eTST2)), 0.1e4);
assertEq(eTST.liquidationLTV(address(eTST2)), 0.15e4);
assertEq(eTST.borrowingLTV(address(eTST2)), 0.5e4);
assertEq(eTST.liquidationLTV(address(eTST2)), 0.5063e4);

skip(600);
skip(50);

assertEq(eTST.borrowingLTV(address(eTST2)), 0.1e4);
assertEq(eTST.liquidationLTV(address(eTST2)), 0.1e4);
// on new target
assertEq(eTST.borrowingLTV(address(eTST2)), 0.5e4);
assertEq(eTST.liquidationLTV(address(eTST2)), 0.5e4);
}

function test_ltvRange() public {
Expand Down
Loading