Skip to content
This repository has been archived by the owner on Sep 8, 2024. It is now read-only.

Commit

Permalink
[24/28] 🐛 Fix : liquidate
Browse files Browse the repository at this point in the history
- cancel out collaterals
  • Loading branch information
ooMia committed Sep 8, 2024
1 parent 9bca51d commit a9351c7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
23 changes: 17 additions & 6 deletions src/DreamAcademyLending.sol
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ contract DreamAcademyLending is _Lending, Initializable, ReentrancyGuardTransien
function repay(address token, uint256 amount) external nonReentrant identity(msg.sender, Operation.REPAY) {
if (token == _ETH) {
revert("repay|ETH: Not payable");
} else {
transferFrom(msg.sender, _THIS, amount, token);
}
transferFrom(msg.sender, _THIS, amount, token);
cancelOutStorage(_USERS[msg.sender].loans, getPrice(token) * amount);
}

Expand All @@ -107,12 +106,18 @@ contract DreamAcademyLending is _Lending, Initializable, ReentrancyGuardTransien
nonReentrant
identity(borrower, Operation.LIQUIDATE)
{
transferFrom(msg.sender, _THIS, amount, token);
if (token == _ETH) {
transferETH(msg.sender, amount);
} else {
transfer(msg.sender, amount, token);
revert("liquidate|ETH: Not payable");
}
uint256 loanAmount = sumAmounts(_USERS[borrower].loans);
if (loanAmount > 100) {
require(amount * 100 <= loanAmount * 25, "liquidate|over 100: liquidation amount is over 25%");
}

transferFrom(msg.sender, _THIS, amount, token);
transfer(msg.sender, amount, token);

cancelOutStorage(_USERS[borrower].collaterals, getPrice(token) * amount);
cancelOutStorage(_USERS[borrower].loans, getPrice(token) * amount);
}

Expand Down Expand Up @@ -147,6 +152,12 @@ contract DreamAcademyLending is _Lending, Initializable, ReentrancyGuardTransien
}
}

function sumAmounts(Value[] storage values) internal view returns (uint256 res) {
for (uint256 i = 0; i < values.length; ++i) {
res += values[i].amount;
}
}

function createValue(address token, uint256 amount) internal view returns (Value memory) {
return Value(token, amount, amount * getPrice(token), block.number);
}
Expand Down
8 changes: 6 additions & 2 deletions src/Lending.sol
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ abstract contract _Lending {
} else if (op == Operation.BORROW) {
// require(preUserBalanceValue < postUserBalance, "identity|BORROW: Total value of user balance not increased");
// require(preThisBalanceValue > postThisBalance, "identity|BORROW: Total value of contract balance not decreased");
console.log(preLTV / 1e18);
console.log(postLTV / 1e18);
console.log("user%d | BORROW: preLTV: %d, postLTV: %d", getUserNumber(), preLTV / 1e18, postLTV / 1e18);
require(postLTV <= OC_RATE * 1e18, "identity|BORROW: OC RATE not satisfied");
require(preOpLoan < postOpLoan, "identity|BORROW: Loan not increased");
require(preOpCollateral == postOpCollateral, "identity|WITHDRAW: Collateral changed");
Expand All @@ -100,6 +99,7 @@ abstract contract _Lending {
// user: msg.sender, balanceType: value
// require(preUserBalanceValue >= postUserBalance, "identity|LIQUIDATE: Total value of user balance increased");
// require(preThisBalanceValue <= postThisBalance, "identity|LIQUIDATE: Total value of contract balance decreased");
console.log("user%d | LIQUIDATE: preLTV: %d, postLTV: %d", getUserNumber(), preLTV / 1e18, postLTV / 1e18);
require(preOpLoan > postOpLoan, "identity|LIQUIDATE: Loan not decreased");
require(preOpCollateral > postOpCollateral, "identity|LIQUIDATE: Collateral not decreased");
require(preLTV > postLTV || preLTV == 0, "identity|LIQUIDATE: LTV not decreased");
Expand Down Expand Up @@ -179,4 +179,8 @@ abstract contract _Lending {
(bool res,) = to.call{value: amount}("");
require(res, "transferETH: transfer failed");
}

function getUserNumber() internal view returns (uint256) {
return uint256(uint160(msg.sender) - uint160(address(0x1336)));
}
}

0 comments on commit a9351c7

Please sign in to comment.