Skip to content

Commit

Permalink
Fix liquidation threshold calculation for multiple collaterals
Browse files Browse the repository at this point in the history
  • Loading branch information
konchunas committed Dec 20, 2023
1 parent 07389aa commit 90b8a2d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
7 changes: 3 additions & 4 deletions contracts/partial/yToken/helpers.ligo
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ function calcMaxCollateralInCU(
verifyInterestUpdated(token);

(* sum += collateralFactorF * exchangeRate * oraclePrice * balance *)
acc := acc + userBalance * token.lastPrice
* token.collateralFactorF * liquidityF / token.totalSupplyF / precision;
acc := acc + userBalance * token.lastPrice * token.collateralFactorF * liquidityF / token.totalSupplyF / precision;
}
else skip;

Expand Down Expand Up @@ -156,10 +155,10 @@ function calcLiquidateCollateral(
verifyInterestUpdated(token);

(* sum += balance * oraclePrice * exchangeRate *)
acc := acc + userBalance * token.lastPrice * liquidityF / token.totalSupplyF;
acc := acc + userBalance * token.lastPrice * token.threshold * liquidityF / token.totalSupplyF / precision;
}
else skip;
} with acc * token.threshold / precision;
} with acc;
} with Set.fold(oneToken, userMarkets, 0n)

function applyInterestToBorrows(
Expand Down
27 changes: 27 additions & 0 deletions integration_tests/test_quick.py
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,33 @@ def test_withraw_admin_rewards(self):
with self.assertRaises(MichelsonRuntimeError):
chain.execute(self.ct.withdrawReserve(1, 1), sender=admin)

def test_multicollateral_liquidate(self):
chain = LocalChain(storage=self.storage)
config = {
"collateral_factor": 0.5,
"reserve_factor": 0.5,
"price": 100,
"liquidity": INITIAL_LIQUIDITY,
"threshold": 0.5,
"reserve_liquidation_rate": 0.05,
}
self.add_token(chain, token_a, config)
self.add_token(chain, token_b, config)
self.add_token(chain, token_c, config)

chain.execute(self.ct.mint(0, 100_000, 1))
chain.execute(self.ct.mint(1, 100_000, 1))
chain.execute(self.ct.enterMarket(0))
chain.execute(self.ct.enterMarket(1))

with self.assertRaises(MichelsonRuntimeError):
chain.execute(self.ct.borrow(2, 100_001, chain.now + 2))

chain.execute(self.ct.borrow(2, 100_000, chain.now + 2))

with self.assertRaises(MichelsonRuntimeError):
chain.execute(self.ct.liquidate(2, 0, me, 1, 1, chain.now + 2), sender=bob)

def test_real_world_liquidation(self):
price_a = 5244313
price_b = 56307584485
Expand Down

0 comments on commit 90b8a2d

Please sign in to comment.