Skip to content

Commit

Permalink
change yield split and add scale change test
Browse files Browse the repository at this point in the history
  • Loading branch information
RickGriff committed May 20, 2024
1 parent 1e7593f commit 6dfb30c
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 115 deletions.
4 changes: 2 additions & 2 deletions contracts/src/ActivePool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ contract ActivePool is Ownable, CheckContract, IActivePool {

uint256 public constant SECONDS_IN_ONE_YEAR = 31536000; // 60 * 60 * 24 * 365,

// uint256 public constant SP_YIELD_SPLIT = 72e16;
uint256 public constant SP_YIELD_SPLIT = 1e18;
uint256 public constant SP_YIELD_SPLIT = 72e16;
// uint256 public constant SP_YIELD_SPLIT = 1e18;

uint256 internal ETHBalance; // deposited ether tracker

Expand Down
1 change: 0 additions & 1 deletion contracts/src/test/TestContracts/BaseTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@ contract BaseTest is Test {
function getShareofSPReward(address _depositor, uint256 _reward) public returns (uint256) {
return _reward * stabilityPool.getCompoundedBoldDeposit(_depositor) / stabilityPool.getTotalBoldDeposits();
}

function logContractAddresses() public view {
console.log("ActivePool addr: ", address(activePool));
console.log("BorrowerOps addr: ", address(borrowerOperations));
Expand Down
9 changes: 9 additions & 0 deletions contracts/src/test/TestContracts/DevTestSetup.sol
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ contract DevTestSetup is BaseTest {
BOLD_GAS_COMP = troveManager.BOLD_GAS_COMPENSATION();
MIN_NET_DEBT = troveManager.MIN_NET_DEBT();
SP_YIELD_SPLIT = activePool.SP_YIELD_SPLIT();
MCR = troveManager.MCR();
}

function _setupForWithdrawETHGainToTrove() internal returns (uint256, uint256, uint256) {
Expand Down Expand Up @@ -263,4 +264,12 @@ contract DevTestSetup is BaseTest {
assertEq(troveManager.getTroveStatus(_troveIDs.A), 5); // status 'unredeemable'
assertEq(troveManager.getTroveStatus(_troveIDs.B), 1); // status 'active'
}

function _getSPYield(uint256 _aggInterest) internal returns (uint256) {
uint256 spYield = SP_YIELD_SPLIT * _aggInterest / 1e18;
assertGt(spYield, 0);
assertLe(spYield, _aggInterest);
return spYield;
}

}
63 changes: 40 additions & 23 deletions contracts/src/test/interestRateAggregate.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -274,24 +274,28 @@ contract InterestRateAggregate is DevTestSetup {

uint256 pendingAggInterest_1 = activePool.calcPendingAggInterest();
assertGt(pendingAggInterest_1, 0);
uint256 expectedSPYield_1 = _getSPYield(pendingAggInterest_1);

// Open 2nd trove
openTroveNoHints100pct(B, 2 ether, 2000e18, 25e16);

// Check SP Bold bal has increased as expected from 2nd trove opening
uint256 boldBalSP_1 = boldToken.balanceOf(address(stabilityPool));
assertEq(boldBalSP_1, pendingAggInterest_1);
assertEq(boldBalSP_1, expectedSPYield_1);

vm.warp(block.timestamp + 1 days);

uint256 pendingAggInterest_2 = activePool.calcPendingAggInterest();
assertGt(pendingAggInterest_2, 0);
uint256 expectedSPYield_2 = _getSPYield(pendingAggInterest_2);


// Open 3rd trove
openTroveNoHints100pct(C, 2 ether, 2000e18, 25e16);

// Check SP Bold bal has increased as expected from 3rd trove opening
uint256 boldBalSP_2 = boldToken.balanceOf(address(stabilityPool));
assertEq(boldBalSP_2, pendingAggInterest_1 + pendingAggInterest_2);
assertEq(boldBalSP_2, expectedSPYield_1 + expectedSPYield_2);
}

function testOpenTroveIncreasesWeightedSumByCorrectWeightedDebt() public {
Expand Down Expand Up @@ -410,13 +414,14 @@ contract InterestRateAggregate is DevTestSetup {

uint256 pendingAggInterest = activePool.calcPendingAggInterest();
assertGt(pendingAggInterest, 0);
uint256 expectedSPYield = _getSPYield(pendingAggInterest);

// Make SP deposit
makeSPDepositAndClaim(A, sPdeposit);

// Check SP Bold bal has increased as expected from SP deposit
uint256 boldBalSP_2 = boldToken.balanceOf(address(stabilityPool));
assertEq(boldBalSP_2, boldBalSP_1 + sPdeposit + pendingAggInterest);
assertEq(boldBalSP_2, boldBalSP_1 + sPdeposit + expectedSPYield);
}

// Does not change the debt weighted sum
Expand Down Expand Up @@ -529,7 +534,7 @@ contract InterestRateAggregate is DevTestSetup {

uint256 pendingAggInterest = activePool.calcPendingAggInterest();
assertGt(pendingAggInterest, 0);
uint256 expectedSPYield_A = SP_YIELD_SPLIT * pendingAggInterest / 1e18;
uint256 expectedSPYield_A = _getSPYield(pendingAggInterest);

uint256 expectedBoldGain_A = getShareofSPReward(A, expectedSPYield_A);
assertGt(expectedBoldGain_A, 0);
Expand All @@ -539,13 +544,7 @@ contract InterestRateAggregate is DevTestSetup {

// Check SP Bold bal has increased as expected
uint256 boldBalSP_2 = boldToken.balanceOf(address(stabilityPool));
console.log(boldBalSP_2, "boldBalSP_2");
console.log(sPdeposit, "sPdeposit");
console.log(pendingAggInterest, "pendingAggInterest");
console.log(expectedBoldGain_A, "expectedBoldGain_A");
console.log(boldBalSP_1 - sPdeposit + pendingAggInterest - expectedBoldGain_A, "boldBalSP_1 - sPdeposit + pendingAggInterest - expectedBoldGain_A");

assertApproximatelyEqual(boldBalSP_2, boldBalSP_1 - sPdeposit + pendingAggInterest - expectedBoldGain_A, 1e3);
assertApproximatelyEqual(boldBalSP_2, boldBalSP_1 - sPdeposit + expectedSPYield_A - expectedBoldGain_A, 1e3);
}

function testSPWithdrawalDoesNotChangeAggWeightedDebtSum() public {
Expand Down Expand Up @@ -680,12 +679,16 @@ contract InterestRateAggregate is DevTestSetup {
uint256 pendingAggInterest = activePool.calcPendingAggInterest();
assertGt(pendingAggInterest, 0);

uint256 expectedSPYield = _getSPYield(pendingAggInterest);



// B closes Trove
closeTrove(B, BTroveId);

// Check SP Bold bal has increased as expected from 3rd trove opening
uint256 boldBalSP_2 = boldToken.balanceOf(address(stabilityPool));
assertEq(boldBalSP_2, pendingAggInterest);
assertEq(boldBalSP_2, expectedSPYield);
}

// Reduces agg. weighted sum by the Trove's recorded debt
Expand Down Expand Up @@ -813,12 +816,14 @@ contract InterestRateAggregate is DevTestSetup {
uint256 pendingAggInterest = activePool.calcPendingAggInterest();
assertGt(pendingAggInterest, 0);

uint256 expectedSPYield = _getSPYield(pendingAggInterest);

// A changes interest rate
changeInterestRateNoHints(A, ATroveId, 75e16);

// Check SP Bold bal has increased as expected
uint256 boldBalSP_2 = boldToken.balanceOf(address(stabilityPool));
assertEq(boldBalSP_2, pendingAggInterest);
assertEq(boldBalSP_2, expectedSPYield);
}

// updates weighted debt sum: removes old and adds new
Expand Down Expand Up @@ -910,11 +915,12 @@ contract InterestRateAggregate is DevTestSetup {

uint256 aggInterest = activePool.calcPendingAggInterest();
assertGt(aggInterest, 0);
uint256 expectedSPYield = _getSPYield(aggInterest);

// A draws more debt
withdrawBold100pct(A, ATroveId, debtIncrease);

assertEq(boldToken.balanceOf(address(stabilityPool)), aggInterest);
assertEq(boldToken.balanceOf(address(stabilityPool)), expectedSPYield);
}

// Updates last agg update time to now
Expand Down Expand Up @@ -1028,11 +1034,12 @@ contract InterestRateAggregate is DevTestSetup {

uint256 pendingAggInterest = activePool.calcPendingAggInterest();
assertGt(pendingAggInterest, 0);
uint256 expectedSPYield = _getSPYield(pendingAggInterest);

// A repays debt
repayBold(A, ATroveId, debtDecrease);

assertEq(boldToken.balanceOf(address(stabilityPool)), pendingAggInterest);
assertEq(boldToken.balanceOf(address(stabilityPool)), expectedSPYield);
}

function testRepayBoldUpdatesLastAggUpdateTimeToNow() public {
Expand Down Expand Up @@ -1144,10 +1151,12 @@ contract InterestRateAggregate is DevTestSetup {
uint256 aggInterest = activePool.calcPendingAggInterest();
assertGt(aggInterest, 0);

uint256 expectedSPYield = _getSPYield(aggInterest);

// A adds coll
addColl(A, ATroveId, collIncrease);

assertEq(boldToken.balanceOf(address(stabilityPool)), aggInterest);
assertEq(boldToken.balanceOf(address(stabilityPool)), expectedSPYield);
}

function testAddCollUpdatesLastAggUpdateTimeToNow() public {
Expand Down Expand Up @@ -1261,11 +1270,12 @@ contract InterestRateAggregate is DevTestSetup {

uint256 aggInterest = activePool.calcPendingAggInterest();
assertGt(aggInterest, 0);
uint256 expectedSPYield = _getSPYield(aggInterest);

// A withdraws coll
withdrawColl(A, ATroveId, collDecrease);

assertEq(boldToken.balanceOf(address(stabilityPool)), aggInterest);
assertEq(boldToken.balanceOf(address(stabilityPool)), expectedSPYield);
}

function testWithdrawCollUpdatesLastAggUpdateTimeToNow() public {
Expand Down Expand Up @@ -1387,11 +1397,13 @@ contract InterestRateAggregate is DevTestSetup {
uint256 pendingAggInterest = activePool.calcPendingAggInterest();
assertGt(pendingAggInterest, 0);

uint256 expectedSPYield = _getSPYield(pendingAggInterest);

// B applies A's pending interest
applyTroveInterestPermissionless(B, ATroveId);

// Check SP Bold bal has increased by the pending agg interest
assertEq(boldToken.balanceOf(address(stabilityPool)), pendingAggInterest);
assertEq(boldToken.balanceOf(address(stabilityPool)), expectedSPYield);
}

function testApplyTroveInterestPermissionlessUpdatesLastAggUpdateTimeToNow() public {
Expand Down Expand Up @@ -1597,6 +1609,8 @@ contract InterestRateAggregate is DevTestSetup {
uint256 pendingAggInterest = activePool.calcPendingAggInterest();
assertGt(pendingAggInterest, 0);

uint256 expectedSPYield = _getSPYield(pendingAggInterest);

uint256 debt_C = troveManager.getTroveEntireDebt(CTroveId);
uint256 debt_D = troveManager.getTroveEntireDebt(DTroveId);

Expand All @@ -1609,7 +1623,7 @@ contract InterestRateAggregate is DevTestSetup {
// Check SP Bold bal has increased as expected from liquidation: depleted by Trove debts C and D, increased by pending
// interest
uint256 boldBalSP_2 = boldToken.balanceOf(address(stabilityPool));
assertEq(boldBalSP_2, boldBalSP_1 - debt_C - debt_D + pendingAggInterest);
assertEq(boldBalSP_2, boldBalSP_1 - debt_C - debt_D + expectedSPYield);
}

function testBatchLiquidateTrovesPureOffsetUpdatesLastAggInterestUpdateTimeToNow() public {
Expand Down Expand Up @@ -1741,6 +1755,8 @@ contract InterestRateAggregate is DevTestSetup {
uint256 pendingAggInterest = activePool.calcPendingAggInterest();
assertGt(pendingAggInterest, 0);

uint256 expectedSPYield = _getSPYield(pendingAggInterest);

// A liquidates C and D
uint256[] memory trovesToLiq = new uint256[](2);
trovesToLiq[0] = CTroveId;
Expand All @@ -1751,7 +1767,7 @@ contract InterestRateAggregate is DevTestSetup {

// Check SP Bold bal has increased as expected from liquidation
uint256 boldBalSP_2 = boldToken.balanceOf(address(stabilityPool));
assertEq(boldBalSP_2, pendingAggInterest);
assertEq(boldBalSP_2, expectedSPYield);
}

function testBatchLiquidateTrovesPureRedistUpdatesLastAggInterestUpdateTimeToNow() public {
Expand Down Expand Up @@ -2062,13 +2078,14 @@ contract InterestRateAggregate is DevTestSetup {

uint256 pendingAggInterest = activePool.calcPendingAggInterest();
assertGt(pendingAggInterest, 0);
uint256 expectedSPYield = _getSPYield(pendingAggInterest);

uint256 debt_A = troveManager.getTroveEntireDebt(troveIDs.A);
// E redeems
redeem(E, debt_A);

// Check SP Bold bal has increased by the pending agg interest
assertEq(boldToken.balanceOf(address(stabilityPool)), pendingAggInterest);
assertEq(boldToken.balanceOf(address(stabilityPool)), expectedSPYield);
}

function testRedemptionUpdatesLastAggUpdateTimeToNow() public {
Expand Down Expand Up @@ -2245,7 +2262,7 @@ contract InterestRateAggregate is DevTestSetup {

uint256 pendingAggInterest = activePool.calcPendingAggInterest();
assertGt(pendingAggInterest, 0);
uint256 expectedSPYield = pendingAggInterest * SP_YIELD_SPLIT / 1e18;
uint256 expectedSPYield = _getSPYield(pendingAggInterest);
uint256 expectedBoldGain_A = getShareofSPReward(A, expectedSPYield);

// Check A has stashed ETH gains
Expand All @@ -2256,7 +2273,7 @@ contract InterestRateAggregate is DevTestSetup {

// Check SP Bold bal has changed as expected - by the pendingAggInterest, minus A's share of it which gets paid out
uint256 boldBalSP_2 = boldToken.balanceOf(address(stabilityPool));
assertApproximatelyEqual(boldBalSP_2, boldBalSP_1 + pendingAggInterest - expectedBoldGain_A, 1e3);
assertApproximatelyEqual(boldBalSP_2, boldBalSP_1 + expectedSPYield - expectedBoldGain_A, 1e3);
}

// TODO: mixed collateral & debt adjustment opps
Expand Down
Loading

0 comments on commit 6dfb30c

Please sign in to comment.