Skip to content

Commit

Permalink
fix: issue-51
Browse files Browse the repository at this point in the history
  • Loading branch information
fann95 committed Oct 30, 2023
1 parent 955d742 commit 97f30b9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
13 changes: 12 additions & 1 deletion contracts/LiquidityBorrowingManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -391,11 +391,22 @@ contract LiquidityBorrowingManager is
* Emits a `TakeOverDebt` event.
* @param borrowingKey The unique key associated with the borrowing to be taken over
* @param collateralAmt The amount of collateral to be provided by the new borrower
* @param minBorrowedAmount The minimum borrowed amount required to take over the debt.
* @param deadline The deadline timestamp after which the transaction is considered invalid.
*/
function takeOverDebt(bytes32 borrowingKey, uint256 collateralAmt) external nonReentrant {
function takeOverDebt(
bytes32 borrowingKey,
uint256 collateralAmt,
uint256 minBorrowedAmount,
uint256 deadline
) external nonReentrant checkDeadline(deadline) {
BorrowingInfo memory oldBorrowing = borrowingsInfo[borrowingKey];
// Ensure that the borrowed position exists
(oldBorrowing.borrowedAmount == 0).revertError(ErrLib.ErrorCode.INVALID_BORROWING_KEY);
// Ensure that the borrowedAmount hasn't changed
(oldBorrowing.borrowedAmount < minBorrowedAmount).revertError(
ErrLib.ErrorCode.UNEXPECTED_CHANGES
);

uint256 accLoanRatePerSeconds;
uint256 minPayment;
Expand Down
3 changes: 2 additions & 1 deletion contracts/libraries/ErrLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ library ErrLib {
ERC20_APPROVE_DID_NOT_SUCCEED, // 9
SWAP_TARGET_NOT_APPROVED, // 10
INVALID_SWAP, //11
INVALID_CALLER //12
INVALID_CALLER, //12
UNEXPECTED_CHANGES //13
}

error RevertErrorCode(ErrorCode code);
Expand Down
4 changes: 3 additions & 1 deletion docs/LiquidityBorrowingManager.md
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ function setSwapCallToWhitelist(address swapTarget, bytes4 funcSelector, bool is
### takeOverDebt

```solidity
function takeOverDebt(bytes32 borrowingKey, uint256 collateralAmt) external nonpayable
function takeOverDebt(bytes32 borrowingKey, uint256 collateralAmt, uint256 minBorrowedAmount, uint256 deadline) external nonpayable
```

Take over debt by transferring ownership of a borrowing to the current caller
Expand All @@ -593,6 +593,8 @@ Take over debt by transferring ownership of a borrowing to the current caller
|---|---|---|
| borrowingKey | bytes32 | The unique key associated with the borrowing to be taken over |
| collateralAmt | uint256 | The amount of collateral to be provided by the new borrower |
| minBorrowedAmount | uint256 | The minimum borrowed amount required to take over the debt. |
| deadline | uint256 | The deadline timestamp after which the transaction is considered invalid. |

### tokenIdToBorrowingKeys

Expand Down
8 changes: 5 additions & 3 deletions test/WagmiLeverageTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1199,20 +1199,22 @@ describe("WagmiLeverageTests", () => {
it("takeOverDebt should be correct if the collateral is depleted", async () => {
snapshot_global.restore();
const aliceBorrowingsCount = await borrowingManager.getBorrowerDebtsCount(alice.address);
let deadline = (await time.latest()) + 60;

const bobBorrowingsCount = await borrowingManager.getBorrowerDebtsCount(bob.address);
let debt: LiquidityBorrowingManager.BorrowingInfoExtStructOutput = (
await borrowingManager.getBorrowerDebtsInfo(bob.address)
)[0];
expect(debt.collateralBalance).to.be.gte(0);
let collateralDebt = debt.collateralBalance.div(COLLATERAL_BALANCE_PRECISION);
await expect(borrowingManager.connect(alice).takeOverDebt(debt.key, collateralDebt)).to.be.reverted; // forbidden
await expect(borrowingManager.connect(alice).takeOverDebt(debt.key, collateralDebt, debt.info.borrowedAmount, deadline)).to.be.reverted; // forbidden
await time.increase(debt.estimatedLifeTime.toNumber() + 10);
deadline = (await time.latest()) + 60;
debt = (await borrowingManager.getBorrowerDebtsInfo(bob.address))[0];
expect(debt.collateralBalance).to.be.lt(0);
collateralDebt = debt.collateralBalance.abs().div(COLLATERAL_BALANCE_PRECISION).add(1);
await expect(borrowingManager.connect(alice).takeOverDebt(debt.key, collateralDebt)).to.be.reverted; //collateralAmt is not enough
await borrowingManager.connect(alice).takeOverDebt(debt.key, collateralDebt.add(5));
await expect(borrowingManager.connect(alice).takeOverDebt(debt.key, collateralDebt, debt.info.borrowedAmount, deadline)).to.be.reverted; //collateralAmt is not enough
await borrowingManager.connect(alice).takeOverDebt(debt.key, collateralDebt.add(5), debt.info.borrowedAmount, deadline);
expect(await borrowingManager.getBorrowerDebtsCount(bob.address)).to.be.equal(bobBorrowingsCount.sub(1));
expect(await borrowingManager.getBorrowerDebtsCount(alice.address)).to.be.equal(aliceBorrowingsCount.add(1));//new
let loansInfo: LiquidityManager.LoanInfoStructOutput[] = await borrowingManager.getLoansInfo(debt.key);
Expand Down

0 comments on commit 97f30b9

Please sign in to comment.