Skip to content

Commit

Permalink
feat: added autoHealmax to clerk (centrifuge#609)
Browse files Browse the repository at this point in the history
  • Loading branch information
xmxanuel authored Nov 17, 2021
1 parent a6e9ec2 commit c1090fa
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
12 changes: 9 additions & 3 deletions src/lender/adapters/mkr/clerk.sol
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ contract Clerk is Auth, Interest {
// collateral tolerance accepted because of potential rounding problems
uint public collateralTolerance = 10;

// maximum amount which can be used to heal as part of a draw operation
// if the collateral deficit is higher a specific heal call is required
uint public autoHealMax = 100 ether;

// the debt is only repaid if amount is higher than the threshold
// repaying a lower amount would cause more cost in gas fees than the debt reduction
uint public wipeThreshold = 1 * WAD;
Expand Down Expand Up @@ -172,6 +176,8 @@ contract Clerk is Auth, Interest {
collateralTolerance = value;
} else if (what == "wipeThreshold") {
wipeThreshold = value;
} else if (what == "autoHealMax") {
autoHealMax = value;
} else { revert(); }
emit File(what, value);
}
Expand Down Expand Up @@ -231,9 +237,9 @@ contract Clerk is Auth, Interest {
function draw(uint amountDAI) public auth active {
// make sure to heal CDP before drawing new DAI
uint healAmountDAI = collatDeficit();
if (healAmountDAI > 0) {
require((validate(0, healAmountDAI, 0, 0) == 0), "violates-constraints");
}

require(healAmountDAI <= autoHealMax, "collateral-deficit-heal-needed");

require(amountDAI <= remainingCredit(), "not-enough-credit-left");
// collateral value that needs to be locked in vault to draw amountDAI
uint collateralDAI = safeAdd(calcOvercollAmount(amountDAI), healAmountDAI);
Expand Down
7 changes: 6 additions & 1 deletion src/lender/adapters/mkr/test/clerk.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ contract ClerkTest is Assertions, Interest {

jug.setReturn("base", 0);
assertEq(clerk.stabilityFee(), interestRatePerSecond);

uint base = ONE;
jug.setReturn("base", base);
assertEq(clerk.stabilityFee(), safeAdd(interestRatePerSecond, base));
Expand Down Expand Up @@ -682,4 +682,9 @@ contract ClerkTest is Assertions, Interest {
clerk.file("tolerance", 100);
assertEq(clerk.collateralTolerance(), 100);
}

function testFileAutoHealMax() public {
clerk.file("autoHealMax", 200 ether);
assertEq(clerk.autoHealMax(), 200 ether);
}
}
29 changes: 19 additions & 10 deletions src/test/system/lender/mkr/mkr_scenarios.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,22 @@ contract MKRLenderSystemTest is MKRTestBasis {
}

function testWipeAndDrawWithAutoHeal() public {
_wipeAndDrawWithAutoHeal(1 days);
}

function testFailAutoHeal() public {
clerk.file("autoHealMax", 0.1 * 1 ether);
_wipeAndDrawWithAutoHeal(1 days);
}

function _wipeAndDrawWithAutoHeal(uint timeUntilExecute) public {
root.relyContract(address(mkrAssessor), address(this));
mkrAssessor.file("minSeniorRatio", 0);

// initial junior & senior investments
uint seniorSupplyAmount = 800 ether;
uint juniorSupplyAmount = 400 ether;
juniorSupply(juniorSupplyAmount);
juniorSupply(juniorSupplyAmount);
seniorSupply(seniorSupplyAmount);
hevm.warp(block.timestamp + 1 days);
coordinator.closeEpoch();
Expand All @@ -384,7 +393,7 @@ contract MKRLenderSystemTest is MKRTestBasis {
clerk.raise(mkrCreditline);
assertEq(clerk.remainingCredit(), mkrCreditline);

// borrow loan & draw from maker
// borrow loan & draw from maker
uint borrowAmount = 1600 ether;
setupOngoingDefaultLoan(borrowAmount);
uint debt = safeSub(borrowAmount, safeAdd(seniorSupplyAmount, juniorSupplyAmount));
Expand All @@ -399,12 +408,12 @@ contract MKRLenderSystemTest is MKRTestBasis {
hevm.warp(block.timestamp + 1 days);
seniorSupply(200 ether);
juniorInvestor.redeemOrder(400 ether);
coordinator.closeEpoch();
coordinator.closeEpoch();
uint seniorTokenPriceClosing = mkrAssessor.calcSeniorTokenPrice();
assertTrue(coordinator.submissionPeriod() == true);

// 2. submit solution & execute epoch after 1 day so that
hevm.warp(block.timestamp + 1 days);
// 2. submit solution & execute epoch
hevm.warp(block.timestamp + timeUntilExecute);
// valid submission
ModelInput memory submission = ModelInput({
seniorSupply : 200 ether, // --> calls maker wipe
Expand All @@ -419,8 +428,8 @@ contract MKRLenderSystemTest is MKRTestBasis {
// check for DROP token price
uint seniorTokenPriceExecution = mkrAssessor.calcSeniorTokenPrice();
// drop price during epoch execution higher then during epoch closing -> requires healing
assertTrue(seniorTokenPriceClosing < seniorTokenPriceExecution);
assertTrue(coordinator.submissionPeriod() == false);
assertTrue(seniorTokenPriceClosing < seniorTokenPriceExecution);
assertTrue(coordinator.submissionPeriod() == false);
}

function testWipeAndDrawWithAutoHealSameBlock() public {
Expand All @@ -430,7 +439,7 @@ contract MKRLenderSystemTest is MKRTestBasis {
// initial junior & senior investments
uint seniorSupplyAmount = 800 ether;
uint juniorSupplyAmount = 400 ether;
juniorSupply(juniorSupplyAmount);
juniorSupply(juniorSupplyAmount);
seniorSupply(seniorSupplyAmount);
hevm.warp(block.timestamp + 1 days);
coordinator.closeEpoch();
Expand All @@ -446,7 +455,7 @@ contract MKRLenderSystemTest is MKRTestBasis {
clerk.raise(mkrCreditline);
assertEq(clerk.remainingCredit(), mkrCreditline);

// borrow loan & draw from maker
// borrow loan & draw from maker
uint borrowAmount = 1600 ether;
setupOngoingDefaultLoan(borrowAmount);
uint debt = safeSub(borrowAmount, safeAdd(seniorSupplyAmount, juniorSupplyAmount));
Expand All @@ -460,6 +469,6 @@ contract MKRLenderSystemTest is MKRTestBasis {
seniorSupply(200 ether);
juniorInvestor.redeemOrder(1 ether);
coordinator.closeEpoch(); // auto execute epoch in teh same block
assertTrue(coordinator.submissionPeriod() == false);
assertTrue(coordinator.submissionPeriod() == false);
}
}

0 comments on commit c1090fa

Please sign in to comment.