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

Reduce liquidation penalty #157

Merged
merged 15 commits into from
May 15, 2024
Merged

Reduce liquidation penalty #157

merged 15 commits into from
May 15, 2024

Conversation

bingen
Copy link
Collaborator

@bingen bingen commented May 3, 2024

Closes #77

@bingen bingen self-assigned this May 3, 2024
@bingen bingen force-pushed the reduce_liquidation_penalty branch from 199b51b to 30171fa Compare May 3, 2024 15:21
@bingen bingen force-pushed the reduce_liquidation_penalty branch from 30171fa to b89f18e Compare May 3, 2024 15:32
@bingen bingen marked this pull request as draft May 3, 2024 15:35
@bingen bingen force-pushed the reduce_liquidation_penalty branch from c166491 to 8d74744 Compare May 3, 2024 16:06
@bingen bingen force-pushed the reduce_liquidation_penalty branch from 8d74744 to 57446c9 Compare May 3, 2024 16:07
@bingen bingen marked this pull request as ready for review May 3, 2024 16:19
Copy link
Collaborator

@RickGriff RickGriff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! A couple questions about the details.

constructor(address _ETHAddress) {
checkContract(_ETHAddress);
ETH = IERC20(_ETHAddress);
constructor(IERC20 _ETH, ITroveManager _troveManager) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is moving TM into constructor related to the reduced liq penalty, or an independent improvement? (fine if so, just curious)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the reason is to fetch MCR from TroveManager, thus avoiding to define it twice (and making sure it’s consistent).

contracts/src/BorrowerOperations.sol Outdated Show resolved Hide resolved
address stabilityPoolAddress;
address gasPoolAddress;
ICollSurplusPool collSurplusPool;
IBoldToken public boldToken;
// A doubly linked list of Troves, sorted by their collateral ratios
ISortedTroves public sortedTroves;

// Minimum collateral ratio for individual troves
uint256 public immutable MCR;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use a constant here - it would save (a bit) of gas when we read the MCR value right?

Though I guess this approach makes deployment more robust as we can pass those Trove params.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, specially because we plan to have different MCRs for different collaterals.

(collToRedistribute, collSurplus) = _getCollPenaltyAndSurplus(
collRedistributionPortion + collSurplus, // Coll surplus from offset can be eaten up by red. penalty
debtToRedistribute,
LIQUIDATION_PENALTY_REDISTRIBUTION,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're passing the redistribution penalty here as the penaltyRatio param, is that correct?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Should I change the name? For now I’m adding a comment to make it clearer.

if (debtToRedistribute > 0) {
uint256 collRedistributionPortion = _collToLiquidate - collSPPortion;
if (collRedistributionPortion > 0) {
(collToRedistribute, collSurplus) = _getCollPenaltyAndSurplus(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm what if there's a partial offset and redistribution?

We assign collSurplus above on L545, and then again here on L556. Does that leave us with the correct final collateral surplus?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope so 😉
The rationale is the following (and that’s what the comment in the line below means):

  • First priority is offset. It can result with an outstanding coll to be redistributed and some surplus.
  • When we proceed to redistribution, we assimilate the surplus into the pending coll, so implicitly we reset surplus to zero. This is because redistribution is harsher, so it may need to eat up part of the previous surplus, i.e., the outstanding collateral may not be enough for the redistribution penalty.
  • We do the math again for redistribution and get the final surplus, which may be lower or greater than the one coming from offsetting, depending if the redistribution net result was positive or negative for the borrower.

Does that make sense?
There are actually a couple of tests with mixed liquidations and redistributions, one with fuzzing. Besides all the old tests are passing.
But still this is new and sensitive, so let’s make sure my logic is sound.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that makes sense. Thanks!

contracts/src/TroveManager.sol Outdated Show resolved Hide resolved
@@ -575,7 +651,10 @@ contract TroveManager is ERC721, LiquityBase, Ownable, ITroveManager {
);

// Move liquidated ETH and Bold to the appropriate pools
stabilityPoolCached.offset(totals.totalDebtToOffset, totals.totalCollToSendToSP);
if (totals.totalDebtToOffset > 0 || totals.totalCollToSendToSP > 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it be that one is 0 and the other is not? (Ah, I guess because the redist. penalty can eat the coll to send to SP?)

assertEq(WETH.balanceOf(A) - AInitialETHBalance, collSurplusAmount, "A collateral balance mismatch");
}

function testLiquidationOffsetNoSurplus() public {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm why is there no surplus in this test, but there is one in the previous test?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the final price is 1,030, leaving the trove at 103% CR, so below the penalty threshold, while the previous one was 1,099.999..., so leaving the trove just below 110%.

@bingen bingen merged commit 13b2ab0 into main May 15, 2024
7 checks passed
@danielattilasimon danielattilasimon deleted the reduce_liquidation_penalty branch June 13, 2024 04:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Reduced liquidation penalty
2 participants