Skip to content

Commit

Permalink
test: Adapt test to new SP pending yield changes
Browse files Browse the repository at this point in the history
  • Loading branch information
bingen committed Sep 12, 2024
1 parent a05c039 commit c9bdcfe
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 101 deletions.
6 changes: 3 additions & 3 deletions contracts/src/test/Invariants.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ contract InvariantsTest is Logging, BaseInvariantTest, BaseMultiCollateralTest {
"Wrong StabilityPool deposits"
);
assertEqDecimal(
c.stabilityPool.getYieldGainsOwed(), handler.spBoldYield(i), 18, "Wrong StabilityPool yield"
c.stabilityPool.getYieldGainsOwed() + c.stabilityPool.getYieldGainsPending(), handler.spBoldYield(i), 18, "Wrong StabilityPool yield"
);
assertEqDecimal(c.stabilityPool.getCollBalance(), handler.spColl(i), 18, "Wrong StabilityPool coll");

Expand Down Expand Up @@ -276,10 +276,10 @@ contract InvariantsTest is Logging, BaseInvariantTest, BaseMultiCollateralTest {
// This only holds as long as no one sends BOLD directly to the SP's address other than ActivePool
assertApproxEqAbsDecimal(
boldToken.balanceOf(address(stabilityPool)),
sumBoldDeposit + sumYieldGain + handler.spUnclaimableBoldYield(j),
sumBoldDeposit + sumYieldGain + stabilityPool.getYieldGainsPending(),
1e-3 ether,
18,
"SP BOLD balance !~= claimable + unclaimable BOLD"
"SP BOLD balance !~= claimable + pending"
);
}
}
Expand Down
16 changes: 16 additions & 0 deletions contracts/src/test/TestContracts/DevTestSetup.sol
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,22 @@ contract DevTestSetup is BaseTest {
assertEq(uint8(troveManager.getTroveStatus(troveIDs.C)), uint8(ITroveManager.Status.closedByLiquidation));
}

function _setupForSPDepositAdjustmentsWithoutOwedYieldRewards() internal returns (ABCDEF memory troveIDs) {
(troveIDs.A, troveIDs.B, troveIDs.C, troveIDs.D) = _setupForBatchLiquidateTrovesPureOffset();

// A claims yield rewards
makeSPWithdrawalAndClaim(A, 0);

// A liquidates C
liquidate(A, troveIDs.C);

// D sends BOLD to A and B so they have some to use in tests
transferBold(D, A, boldToken.balanceOf(D) / 2);
transferBold(D, B, boldToken.balanceOf(D));

assertEq(uint8(troveManager.getTroveStatus(troveIDs.C)), uint8(ITroveManager.Status.closedByLiquidation));
}

function _setupForPTests() internal returns (ABCDEF memory) {
ABCDEF memory troveIDs;
(troveIDs.A, troveIDs.B, troveIDs.C, troveIDs.D) = _setupForBatchLiquidateTrovesPureOffset();
Expand Down
98 changes: 57 additions & 41 deletions contracts/src/test/TestContracts/InvariantsTestHandler.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,21 @@ contract InvariantsTestHandler is BaseHandler, BaseMultiCollateralTest {
string errorString;
}

struct ProvideToSPContext {
TestDeployer.LiquityContractsDev c;
uint256 pendingInterest;
uint256 pendingYield;
uint256 initialBoldDeposit;
uint256 boldDeposit;
uint256 boldYield;
uint256 ethGain;
uint256 ethStash;
uint256 ethClaimed;
uint256 boldClaimed;
uint256 depositorPendingYield;
string errorString;
}

struct WithdrawFromSPContext {
TestDeployer.LiquityContractsDev c;
uint256 pendingInterest;
Expand Down Expand Up @@ -333,10 +348,6 @@ contract InvariantsTestHandler is BaseHandler, BaseMultiCollateralTest {
// Price per branch
mapping(uint256 branchIdx => uint256) _price;

// Bold yield sent to the SP at a time when there are no deposits is lost forever
// We keep track of the lost amount so we can use it in invariants
mapping(uint256 branchIdx => uint256) public spUnclaimableBoldYield;

// All free-floating BOLD is kept in the handler, to be dealt out to actors as needed
uint256 _handlerBold;

Expand Down Expand Up @@ -1529,24 +1540,33 @@ contract InvariantsTestHandler is BaseHandler, BaseMultiCollateralTest {
}

function provideToSP(uint256 i, uint256 amount, bool claim) external {
ProvideToSPContext memory v;

i = _bound(i, 0, branches.length - 1);
amount = _bound(amount, 0, _handlerBold);

TestDeployer.LiquityContractsDev memory c = branches[i];
uint256 pendingInterest = c.activePool.calcPendingAggInterest();
uint256 initialBoldDeposit = c.stabilityPool.deposits(msg.sender);
uint256 boldDeposit = c.stabilityPool.getCompoundedBoldDeposit(msg.sender);
uint256 boldYield = c.stabilityPool.getDepositorYieldGainWithPending(msg.sender);
uint256 ethGain = c.stabilityPool.getDepositorCollGain(msg.sender);
uint256 ethStash = c.stabilityPool.stashedColl(msg.sender);
uint256 ethClaimed = claim ? ethStash + ethGain : 0;
uint256 boldClaimed = claim ? boldYield : 0;

info("initial deposit: ", initialBoldDeposit.decimal());
info("compounded deposit: ", boldDeposit.decimal());
info("yield gain: ", boldYield.decimal());
info("coll gain: ", ethGain.decimal());
info("stashed coll: ", ethStash.decimal());
v.c = branches[i];
v.pendingInterest = v.c.activePool.calcPendingAggInterest();
v.pendingYield = v.c.stabilityPool.getYieldGainsPending();
v.initialBoldDeposit = v.c.stabilityPool.deposits(msg.sender);
v.boldDeposit = v.c.stabilityPool.getCompoundedBoldDeposit(msg.sender);
v.boldYield = v.c.stabilityPool.getDepositorYieldGainWithPending(msg.sender);
v.ethGain = v.c.stabilityPool.getDepositorCollGain(msg.sender);
v.ethStash = v.c.stabilityPool.stashedColl(msg.sender);
v.ethClaimed = claim ? v.ethStash + v.ethGain : 0;
v.boldClaimed = claim ? v.boldYield : 0;
uint256 totalBoldDepositsBefore = v.c.stabilityPool.getTotalBoldDeposits();
if (totalBoldDepositsBefore < DECIMAL_PRECISION && amount + totalBoldDepositsBefore >= DECIMAL_PRECISION) {
v.depositorPendingYield = v.pendingYield + SP_YIELD_SPLIT * v.pendingInterest / 1e18;
}

info("initial deposit: ", v.initialBoldDeposit.decimal());
info("compounded deposit: ", v.boldDeposit.decimal());
info("yield gain: ", v.boldYield.decimal());
info("coll gain: ", v.ethGain.decimal());
info("stashed coll: ", v.ethStash.decimal());
info("pendingYield: ", v.pendingYield.decimal());
info("pendingInterest: ", v.pendingInterest.decimal());
logCall("provideToSP", i.toString(), amount.decimal(), claim.toString());

// TODO: randomly deal less than amount?
Expand All @@ -1555,31 +1575,31 @@ contract InvariantsTestHandler is BaseHandler, BaseMultiCollateralTest {
string memory errorString;
vm.prank(msg.sender);

try c.stabilityPool.provideToSP(amount, claim) {
try v.c.stabilityPool.provideToSP(amount, claim) {
// Preconditions
assertGtDecimal(amount, 0, 18, "Should have failed as amount was zero");

// Effects (deposit)
ethStash += ethGain;
ethStash -= ethClaimed;
v.ethStash += v.ethGain;
v.ethStash -= v.ethClaimed;

boldDeposit += amount;
boldDeposit += boldYield;
boldDeposit -= boldClaimed;
v.boldDeposit += amount;
v.boldDeposit += v.boldYield;
v.boldDeposit -= v.boldClaimed;

assertEqDecimal(c.stabilityPool.getCompoundedBoldDeposit(msg.sender), boldDeposit, 18, "Wrong deposit");
assertEqDecimal(c.stabilityPool.getDepositorYieldGain(msg.sender), 0, 18, "Wrong yield gain");
assertEqDecimal(c.stabilityPool.getDepositorCollGain(msg.sender), 0, 18, "Wrong coll gain");
assertEqDecimal(c.stabilityPool.stashedColl(msg.sender), ethStash, 18, "Wrong stashed coll");
assertEqDecimal(v.c.stabilityPool.getCompoundedBoldDeposit(msg.sender), v.boldDeposit, 18, "Wrong deposit");
assertApproxEqAbsDecimal(v.c.stabilityPool.getDepositorYieldGain(msg.sender), v.depositorPendingYield, 1e6, 18, "Wrong yield gain");
assertEqDecimal(v.c.stabilityPool.getDepositorCollGain(msg.sender), 0, 18, "Wrong coll gain");
assertEqDecimal(v.c.stabilityPool.stashedColl(msg.sender), v.ethStash, 18, "Wrong stashed coll");

// Effects (system)
_mintYield(i, pendingInterest, 0);
_mintYield(i, v.pendingInterest, 0);

spColl[i] -= ethClaimed;
spColl[i] -= v.ethClaimed;
spBoldDeposits[i] += amount;
spBoldDeposits[i] += boldYield;
spBoldDeposits[i] -= boldClaimed;
spBoldYield[i] -= boldYield;
spBoldDeposits[i] += v.boldYield;
spBoldDeposits[i] -= v.boldClaimed;
spBoldYield[i] -= v.boldYield;
} catch Error(string memory reason) {
errorString = reason;

Expand All @@ -1601,8 +1621,8 @@ contract InvariantsTestHandler is BaseHandler, BaseMultiCollateralTest {
_sweepBold(msg.sender, amount); // Take back the BOLD that was dealt
} else {
// Cleanup (success)
_sweepBold(msg.sender, boldClaimed);
_sweepColl(i, msg.sender, ethClaimed);
_sweepBold(msg.sender, v.boldClaimed);
_sweepColl(i, msg.sender, v.ethClaimed);
}
}

Expand Down Expand Up @@ -2445,11 +2465,7 @@ contract InvariantsTestHandler is BaseHandler, BaseMultiCollateralTest {
uint256 mintedYield = pendingInterest + upfrontFee;
uint256 mintedSPBoldYield = mintedYield * SP_YIELD_SPLIT / DECIMAL_PRECISION;

if (spBoldDeposits[i] == 0) {
spUnclaimableBoldYield[i] += mintedSPBoldYield;
} else {
spBoldYield[i] += mintedSPBoldYield;
}
spBoldYield[i] += mintedSPBoldYield;

_pendingInterest[i] = 0;
}
Expand Down
1 change: 1 addition & 0 deletions contracts/src/test/events.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,7 @@ contract StabilityPoolEventsTest is EventsTest, IStabilityPoolEvents {

// Increase epoch
makeSPDepositNoClaim(A, liquidatedDebt);
makeSPWithdrawalAndClaim(A, 0); // Claim yield from first troves
troveManager.liquidate(liquidatedTroveId[0]);

current.epoch = stabilityPool.currentEpoch();
Expand Down
10 changes: 6 additions & 4 deletions contracts/src/test/interestRateAggregate.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,8 @@ contract InterestRateAggregate is DevTestSetup {
priceFeed.setPrice(2000e18);
openTroveNoHints100pct(A, 2 ether, troveDebtRequest, 25e16);
makeSPDepositAndClaim(A, sPdeposit);
// claim gains from first trove
makeSPWithdrawalAndClaim(A, 0);

// fast-forward time
vm.warp(block.timestamp + 1 days);
Expand Down Expand Up @@ -2148,7 +2150,7 @@ contract InterestRateAggregate is DevTestSetup {
// --- claimALLCollGains ---

function testClaimAllCollGainsIncreasesAggRecordedDebtByPendingAggInterest() public {
_setupForSPDepositAdjustments();
_setupForSPDepositAdjustmentsWithoutOwedYieldRewards();

// A withdraws depsoiit and stashes gain
uint256 deposit_A = stabilityPool.getCompoundedBoldDeposit(A);
Expand All @@ -2171,7 +2173,7 @@ contract InterestRateAggregate is DevTestSetup {
}

function testClaimAllCollGainsReducesPendingAggInterestTo0() public {
_setupForSPDepositAdjustments();
_setupForSPDepositAdjustmentsWithoutOwedYieldRewards();

// A withdraws depsoiit and stashes gain
uint256 deposit_A = stabilityPool.getCompoundedBoldDeposit(A);
Expand All @@ -2192,7 +2194,7 @@ contract InterestRateAggregate is DevTestSetup {

// // Update last agg. update time to now
function testClaimAllCollGainsUpdatesLastAggUpdateTimeToNow() public {
_setupForSPDepositAdjustments();
_setupForSPDepositAdjustmentsWithoutOwedYieldRewards();

// A withdraws deposit and stashes gain
uint256 deposit_A = stabilityPool.getCompoundedBoldDeposit(A);
Expand All @@ -2216,7 +2218,7 @@ contract InterestRateAggregate is DevTestSetup {
// mints interest to SP
function testClaimAllCollGainsMintsAggInterestToSP() public {
ABCDEF memory troveIDs;
troveIDs = _setupForSPDepositAdjustments();
troveIDs = _setupForSPDepositAdjustmentsWithoutOwedYieldRewards();

// A withdraws depsoiit and stashes gain
uint256 deposit_A = stabilityPool.getCompoundedBoldDeposit(A);
Expand Down
Loading

0 comments on commit c9bdcfe

Please sign in to comment.