Skip to content

Commit

Permalink
feat: Remove combined action withdratETHGainToTrove
Browse files Browse the repository at this point in the history
  • Loading branch information
bingen committed May 3, 2024
1 parent 08e0822 commit fdbc486
Show file tree
Hide file tree
Showing 11 changed files with 0 additions and 4,650 deletions.
16 changes: 0 additions & 16 deletions contracts/src/BorrowerOperations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -239,18 +239,6 @@ contract BorrowerOperations is LiquityBase, Ownable, CheckContract, IBorrowerOpe
assert(troveManager.getTroveEntireColl(_troveId) > oldColl);
}

// Send ETH as collateral to a trove. Called by only the Stability Pool.
function moveETHGainToTrove(address _sender, uint256 _troveId, uint256 _ETHAmount) external override {
ContractsCacheTMAPBT memory contractsCache = ContractsCacheTMAPBT(troveManager, activePool, boldToken);
_requireTroveIsActive(contractsCache.troveManager, _troveId);
// TODO: Use oldColl and assert in fuzzing, remove before deployment
uint256 oldColl = troveManager.getTroveEntireColl(_troveId);
_requireCallerIsStabilityPool();
// TODO: check owner?
_adjustTrove(_sender, _troveId, _ETHAmount, true, 0, false, 0, contractsCache);
assert(troveManager.getTroveEntireColl(_troveId) > oldColl);
}

// Withdraw ETH collateral from a trove
function withdrawColl(uint256 _troveId, uint256 _collWithdrawal) external override {
ContractsCacheTMAPBT memory contractsCache = ContractsCacheTMAPBT(troveManager, activePool, boldToken);
Expand Down Expand Up @@ -797,10 +785,6 @@ contract BorrowerOperations is LiquityBase, Ownable, CheckContract, IBorrowerOpe
);
}

function _requireCallerIsStabilityPool() internal view {
require(msg.sender == stabilityPoolAddress, "BorrowerOps: Caller is not Stability Pool");
}

function _requireSufficientBoldBalance(IBoldToken _boldToken, address _borrower, uint256 _debtRepayment)
internal
view
Expand Down
2 changes: 0 additions & 2 deletions contracts/src/Interfaces/IBorrowerOperations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ interface IBorrowerOperations is ILiquityBase {

function addColl(uint256 _troveId, uint256 _ETHAmount) external;

function moveETHGainToTrove(address _sender, uint256 _troveId, uint256 _ETHAmount) external;

function withdrawColl(uint256 _troveId, uint256 _amount) external;

function withdrawBold(uint256 _troveId, uint256 _maxFee, uint256 _amount) external;
Expand Down
7 changes: 0 additions & 7 deletions contracts/src/Interfaces/IStabilityPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,6 @@ interface IStabilityPool is ILiquityBase {
*/
function withdrawFromSP(uint256 _amount) external;

/* withdrawETHGainToTrove():
* - Transfers the depositor's entire ETH gain from the Stability Pool to the caller's trove
* - Leaves their compounded deposit in the Stability Pool
* - Takes new snapshots of accumulators P and S
*/
function withdrawETHGainToTrove(uint256 _troveId) external;

/*
* Initial checks:
* - Caller is TroveManager
Expand Down
41 changes: 0 additions & 41 deletions contracts/src/StabilityPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -334,38 +334,6 @@ contract StabilityPool is LiquityBase, Ownable, CheckContract, IStabilityPool {
_sendETHGainToDepositor(depositorETHGain);
}

/* withdrawETHGainToTrove():
* - Transfers the depositor's entire ETH gain from the Stability Pool to the caller's trove
* - Leaves their compounded deposit in the Stability Pool
* - Takes new snapshots of accumulators P and S
*/
function withdrawETHGainToTrove(uint256 _troveId) external override {
uint256 initialDeposit = deposits[msg.sender].initialValue;
_requireUserHasDeposit(initialDeposit);
_requireTroveIsOpen(_troveId);
_requireUserHasETHGain(msg.sender);

uint256 depositorETHGain = getDepositorETHGain(msg.sender);

uint256 compoundedBoldDeposit = getCompoundedBoldDeposit(msg.sender);
uint256 boldLoss = initialDeposit - compoundedBoldDeposit; // Needed only for event log

_updateDepositAndSnapshots(msg.sender, compoundedBoldDeposit);

/* Emit events before transferring ETH gain to Trove.
This lets the event log make more sense (i.e. so it appears that first the ETH gain is withdrawn
and then it is deposited into the Trove, not the other way around). */
emit ETHGainWithdrawn(msg.sender, depositorETHGain, boldLoss);
emit UserDepositChanged(msg.sender, compoundedBoldDeposit);

uint256 newETHBalance = ETHBalance - depositorETHGain;
ETHBalance = newETHBalance;
emit StabilityPoolETHBalanceUpdated(newETHBalance);
emit EtherSent(msg.sender, depositorETHGain);

borrowerOperations.moveETHGainToTrove(msg.sender, _troveId, depositorETHGain);
}

// --- Liquidation functions ---

/*
Expand Down Expand Up @@ -695,15 +663,6 @@ contract StabilityPool is LiquityBase, Ownable, CheckContract, IStabilityPool {
require(_amount > 0, "StabilityPool: Amount must be non-zero");
}

function _requireTroveIsOpen(uint256 _troveId) internal view {
require(troveManager.checkTroveIsOpen(_troveId), "StabilityPool: trove must be active to withdraw ETHGain to");
}

function _requireUserHasETHGain(address _depositor) internal view {
uint256 ETHGain = getDepositorETHGain(_depositor);
require(ETHGain > 0, "StabilityPool: caller must have non-zero ETH Gain");
}

function _requireValidKickbackRate(uint256 _kickbackRate) internal pure {
require(_kickbackRate <= DECIMAL_PRECISION, "StabilityPool: Kickback rate must be in range [0,1]");
}
Expand Down
6 changes: 0 additions & 6 deletions contracts/src/test/TestContracts/BaseTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,6 @@ contract BaseTest is Test {
vm.stopPrank();
}

function withdrawETHGainToTrove(address _from, uint256 _troveId) public {
vm.startPrank(_from);
stabilityPool.withdrawETHGainToTrove(_troveId);
vm.stopPrank();
}

function batchLiquidateTroves(address _from, uint256[] memory _trovesList) public {
vm.startPrank(_from);
console.log(_trovesList[0], "trove 0 to liq");
Expand Down
95 changes: 0 additions & 95 deletions contracts/src/test/borrowerOperationsOnBehalfTroveManagament.sol
Original file line number Diff line number Diff line change
Expand Up @@ -342,99 +342,4 @@ contract BorrowerOperationsOnBehalfTroveManagamentTest is DevTestSetup {
assertEq(boldToken.balanceOf(A), AInitialBoldBalance + 10e18, "Wrong owner balance");
assertEq(boldToken.balanceOf(B), BInitialBoldBalance, "Wrong manager balance");
}

// TODO: withdrawETHGainToTrove

function testWithdrawETHGainToTroveWithAddManager() public {
uint256 ATroveId = openTroveNoHints100pctMaxFee(A, 100 ether, 10000e18, 1e17);

// Set add manager
vm.startPrank(A);
borrowerOperations.setAddManager(ATroveId, B);
// A provides to SP
stabilityPool.provideToSP(10000e18);
vm.stopPrank();

// B provides to SP
deal(address(boldToken), B, 10000e18);
vm.startPrank(B);
stabilityPool.provideToSP(10000e18);
vm.stopPrank();

// C opens trove, price drops and gets liquidated
uint256 CTroveId = openTroveNoHints100pctMaxFee(C, 100 ether, 14000e18, 1e17);
priceFeed.setPrice(priceFeed.getPrice() * 3 / 4);
troveManager.liquidate(CTroveId);

// Owner can withdraw ETH gain to trove
vm.startPrank(A);
uint256 AInitialGain = stabilityPool.getDepositorETHGain(A);

stabilityPool.withdrawETHGainToTrove(ATroveId);
vm.stopPrank();

assertEq(troveManager.getTroveColl(ATroveId), 100 ether + AInitialGain, "Wrong trove coll");
assertEq(stabilityPool.getDepositorETHGain(A), 0, "Wrong owner SP ETH balance");

// Manager can withdraw ETH gain to trove
vm.startPrank(B);
uint256 BInitialGain = stabilityPool.getDepositorETHGain(B);

stabilityPool.withdrawETHGainToTrove(ATroveId);
vm.stopPrank();

assertEq(troveManager.getTroveColl(ATroveId), 100 ether + AInitialGain + BInitialGain, "Wrong trove coll");
assertEq(stabilityPool.getDepositorETHGain(B), 0, "Wrong manager balance");
}

function testWithdrawETHGainToTroveWithoutAddManager() public {
uint256 ATroveId = openTroveNoHints100pctMaxFee(A, 100 ether, 10000e18, 1e17);

// A provides to SP
vm.startPrank(A);
stabilityPool.provideToSP(10000e18);
vm.stopPrank();

// B provides to SP
deal(address(boldToken), B, 10000e18);
vm.startPrank(B);
stabilityPool.provideToSP(10000e18);
vm.stopPrank();

// C opens trove, price drops and gets liquidated
uint256 CTroveId = openTroveNoHints100pctMaxFee(C, 100 ether, 14000e18, 1e17);
priceFeed.setPrice(priceFeed.getPrice() * 3 / 4);
troveManager.liquidate(CTroveId);

// Owner can withdraw ETH gain to trove
vm.startPrank(A);
uint256 AInitialGain = stabilityPool.getDepositorETHGain(A);

stabilityPool.withdrawETHGainToTrove(ATroveId);
vm.stopPrank();

assertEq(troveManager.getTroveColl(ATroveId), 100 ether + AInitialGain, "Wrong trove coll");
assertEq(stabilityPool.getDepositorETHGain(A), 0, "Wrong owner SP ETH balance");

// Others can’t withdraw ETH gain to trove
vm.startPrank(B);
uint256 BInitialGain = stabilityPool.getDepositorETHGain(B);

vm.expectRevert("TroveManager: sender is not trove owner nor manager");
stabilityPool.withdrawETHGainToTrove(ATroveId);
vm.stopPrank();

// Set remove manager - still won’t work
vm.startPrank(A);
borrowerOperations.setRemoveManager(ATroveId, B);
vm.stopPrank();

vm.startPrank(B);
vm.expectRevert("TroveManager: sender is not trove owner nor manager");
stabilityPool.withdrawETHGainToTrove(ATroveId);
vm.stopPrank();

assertEq(troveManager.getTroveColl(ATroveId), 100 ether + AInitialGain, "Wrong trove coll");
assertEq(stabilityPool.getDepositorETHGain(B), BInitialGain, "Wrong manager SP ETH balance");
}
}
120 changes: 0 additions & 120 deletions contracts/src/test/interestRateAggregate.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1763,126 +1763,6 @@ contract InterestRateAggregate is DevTestSetup {

// TODO: more thorough invariant test

// --- withdrawETHGainToTrove ---

function testWithdrawETHGainToTroveIncreasesAggRecordedDebtByAggInterest() public {
(uint256 ATroveId,,) = _setupForWithdrawETHGainToTrove();

// fast-forward time so interest accrues
vm.warp(block.timestamp + 1 days);

uint256 aggRecordedDebt_1 = activePool.aggRecordedDebt();
assertGt(aggRecordedDebt_1, 0);
uint256 pendingAggInterest = activePool.calcPendingAggInterest();
assertGt(pendingAggInterest, 0);

// A withdraws ETH gain to Trove
withdrawETHGainToTrove(A, ATroveId);

assertEq(activePool.aggRecordedDebt(), aggRecordedDebt_1 + pendingAggInterest);
}

function testWithdrawETHGainToTroveReducesPendingAggInterestTo0() public {
(uint256 ATroveId,,) = _setupForWithdrawETHGainToTrove();

// fast-forward time so interest accrues
vm.warp(block.timestamp + 1 days);

// check there's pending agg. interest
assertGt(activePool.calcPendingAggInterest(), 0);

// A withdraws ETH gain to Trove
withdrawETHGainToTrove(A, ATroveId);

// Check pending agg. interest reduced to 0
assertEq(activePool.calcPendingAggInterest(), 0);
}

function testWithdrawETHGainToTroveMintsInterestToRouter() public {
(uint256 ATroveId,,) = _setupForWithdrawETHGainToTrove();

// fast-forward time so interest accrues
vm.warp(block.timestamp + 1 days);

// Get I-router balance
uint256 boldBalRouter_1 = boldToken.balanceOf(address(mockInterestRouter));
assertEq(boldBalRouter_1, 0);

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

// A withdraws ETH gain to Trove
withdrawETHGainToTrove(A, ATroveId);

// Check I-router Bold bal has increased as expected from SP deposit
uint256 boldBalRouter_2 = boldToken.balanceOf(address(mockInterestRouter));
assertEq(boldBalRouter_2, pendingAggInterest);
}

function testWithdrawETHGainToTroveUpdatesLastAggUpdateTimeToNow() public {
(uint256 ATroveId,,) = _setupForWithdrawETHGainToTrove();

// fast-forward time so interest accrues
vm.warp(block.timestamp + 1 days);

assertGt(activePool.lastAggUpdateTime(), 0);
assertLt(activePool.lastAggUpdateTime(), block.timestamp);

// A withdraws ETH gain to Trove
withdrawETHGainToTrove(A, ATroveId);

// Check last agg update time increased to now
assertEq(activePool.lastAggUpdateTime(), block.timestamp);
}

function testWithdrawETHGainToTroveChangesAggWeightedDebtSumCorrectly() public {
(uint256 ATroveId,,) = _setupForWithdrawETHGainToTrove();

// fast-forward time
vm.warp(block.timestamp + 1 days);

// Get weighted sum before
uint256 weightedDebtSum_1 = activePool.aggWeightedDebtSum();
assertGt(weightedDebtSum_1, 0);

uint256 oldRecordedWeightedDebt = troveManager.getTroveWeightedRecordedDebt(ATroveId);
assertGt(oldRecordedWeightedDebt, 0);

// A withdraws ETH gain to Trove
withdrawETHGainToTrove(A, ATroveId);

// Expect recorded weighted debt to have increased due to accrued Trove interest being applied
uint256 newRecordedWeightedDebt = troveManager.getTroveWeightedRecordedDebt(ATroveId);
assertGt(newRecordedWeightedDebt, oldRecordedWeightedDebt);

// Expect weighted sum decreases by the old and increases by the new individual weighted Trove debt.
assertEq(activePool.aggWeightedDebtSum(), weightedDebtSum_1 - oldRecordedWeightedDebt + newRecordedWeightedDebt);
}

function testWithdrawETHGainToTroveChangesRecordedDebtSumCorrectly() public {
(uint256 ATroveId,,) = _setupForWithdrawETHGainToTrove();

// fast-forward time
vm.warp(block.timestamp + 1 days);

// Get recorded sum before
uint256 recordedDebt_1 = activePool.getRecordedDebtSum();
assertGt(recordedDebt_1, 0);

uint256 oldTroveRecordedDebt = troveManager.getTroveDebt(ATroveId);
assertGt(oldTroveRecordedDebt, 0);

// A withdraws ETH gain to Trove
withdrawETHGainToTrove(A, ATroveId);

// Expect recorded debt to have increased due to accrued Trove interest being applied
uint256 newTroveRecordedDebt = troveManager.getTroveDebt(ATroveId);
assertGt(newTroveRecordedDebt, oldTroveRecordedDebt);

// Get recorded sum after, check no change
assertEq(activePool.getRecordedDebtSum(), recordedDebt_1 - oldTroveRecordedDebt + newTroveRecordedDebt);
}

// --- batchLiquidateTroves (Normal Mode, offset) ---

function testBatchLiquidateTrovesPureOffsetChangesAggRecordedInterestCorrectly() public {
Expand Down
Loading

0 comments on commit fdbc486

Please sign in to comment.