-
-
Save panprog/cbdc1658d63c30c9fe94127a4b4b7e72 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: ISC | |
pragma solidity ^0.8.15; | |
import "./BasePairTest.sol"; | |
import "forge-std/Test.sol"; | |
contract LiquidatePairTest is BasePairTest { | |
using OracleHelper for AggregatorV3Interface; | |
using SafeCast for uint256; | |
function testLiquidateBug() public { | |
// Setup contracts | |
defaultSetUp(); | |
// Sets price to 3200 USD per ETH | |
uint256 _amountToBorrow = 16e20; // 1.5k | |
uint256 _amountInPool = 15e23; // 1.5m | |
// collateral is 1.5 times borrow amount | |
oracleDivide.setPrice(3200, 1, vm); | |
mineBlocks(1); | |
(, uint256 _exchangeRate) = pair.exchangeRateInfo(); | |
uint256 _collateralAmount = (_amountToBorrow * _exchangeRate * 4) / (3 * 1e18); | |
faucetFunds(asset, _amountInPool); | |
faucetFunds(collateral, _collateralAmount); | |
faucetFunds(collateral, _collateralAmount * 10, users[3]); | |
lendTokenViaDeposit(_amountInPool, users[0]); | |
borrowToken(uint128(_amountToBorrow), _collateralAmount, users[2]); | |
borrowToken(uint128(_amountToBorrow*10), _collateralAmount*10, users[3]); // another borrow for the bug not to revert immediately | |
oracleDivide.setPrice(1000, 1, vm); | |
mineBlocks(1); | |
uint128 _shares = pair.userBorrowShares(users[2]).toUint128(); | |
for (uint256 i = 0; i < 1; i++) { | |
pair.addInterest(); | |
mineOneBlock(); | |
} | |
vm.startPrank(users[1]); | |
pair.addInterest(); | |
asset.approve(address(pair), toBorrowAmount(_shares, true)); | |
console.log('shares', _shares); | |
console.log('user collateral', _collateralAmount); | |
// due to bad debt, liquidate only 70% of shares, just to get collateral to 0 and write off bad debt | |
_shares = _shares * 7 / 10; | |
console.log('liquidate shares', _shares); | |
pair.liquidateClean(_shares, block.timestamp, users[2]); | |
_shares = pair.userBorrowShares(users[2]).toUint128(); | |
console.log('after liquidating:'); | |
console.log('remaining collateral', pair.userCollateralBalance(users[2])); | |
console.log('remaining shares', _shares); | |
uint128 _totalAssetAmount; | |
uint128 _totalAssetShares; | |
uint128 _totalBorrowAmount; | |
uint128 _totalBorrowShares; | |
(_totalAssetAmount, _totalAssetShares) = pair.totalAsset(); | |
(_totalBorrowAmount, _totalBorrowShares) = pair.totalBorrow(); | |
console.log('total asset', _totalAssetAmount); | |
console.log('total borrow', _totalBorrowAmount, _totalBorrowShares); | |
for (uint256 i = 0; i < 3; i++) { | |
// liquidate 0 shares (to write off bad debt again) | |
pair.liquidateClean(0, block.timestamp, users[2]); | |
_shares = pair.userBorrowShares(users[2]).toUint128(); | |
console.log('after liquidating 0 shares:'); | |
console.log('remaining collateral', pair.userCollateralBalance(users[2])); | |
console.log('remaining shares', _shares); | |
(_totalAssetAmount, _totalAssetShares) = pair.totalAsset(); | |
(_totalBorrowAmount, _totalBorrowShares) = pair.totalBorrow(); | |
console.log('total asset', _totalAssetAmount); | |
console.log('total borrow', _totalBorrowAmount, _totalBorrowShares); | |
} | |
//assertEq(pair.userBorrowShares(users[2]), 0); | |
vm.stopPrank(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment