Skip to content

Commit

Permalink
Merge pull request #184 from unioncredit/feature/uni-2213-interest-pa…
Browse files Browse the repository at this point in the history
…yment-on-base-is-wrong

fix: interest calc error when borrow more than once
  • Loading branch information
maxweng authored Dec 9, 2024
2 parents 7a0a034 + a340007 commit 063829e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
5 changes: 3 additions & 2 deletions contracts/market/UToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -640,8 +640,9 @@ contract UToken is IUToken, Controller, ERC20PermitUpgradeable, ReentrancyGuardU
if (totalBorrowsNew > _debtCeiling) revert AmountExceedGlobalMax();

// Update internal balances
uint256 newPrincipal = actualAmount + fee;
accountBorrows[msg.sender].principal += newPrincipal;
accountBorrows[msg.sender].principal += actualAmount + fee;
uint256 newPrincipal = _getBorrowed(msg.sender);

accountBorrows[msg.sender].interest = accountBorrowsNew - newPrincipal;
accountBorrows[msg.sender].interestIndex = borrowIndex;
_totalBorrows = totalBorrowsNew;
Expand Down
15 changes: 15 additions & 0 deletions test/foundry/uToken/TestBorrowRepay.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@ contract TestBorrowRepay is TestUTokenBase {
assertEq(borrowed, realBorrowAmount + fees);
}

function testBorrowTwoTimes() public {
vm.startPrank(ALICE);
uint256 originationFee = uToken.originationFee();
uint256 borrowRatePerSecond = uToken.borrowRatePerSecond();
uToken.borrow(ALICE, 1 * UNIT);
uToken.borrow(ALICE, 1 * UNIT);
skip(block.timestamp);
uint256 interest = uToken.calculatingInterest(ALICE);
uint256 borrowed = 2 * UNIT;
uint256 fee = (borrowed * 10 ** (18 - tokenDecimals) * originationFee) / 1e18;
uint256 expectInterest = ((borrowed + fee) * borrowRatePerSecond) / 1e18; //1 sec
assertEq(interest, expectInterest);
vm.stopPrank();
}

function testRepayBorrow(uint256 borrowAmount) public {
vm.assume(borrowAmount >= MIN_BORROW && borrowAmount < MAX_BORROW - (MAX_BORROW * ORIGINATION_FEE) / 1e18);

Expand Down

0 comments on commit 063829e

Please sign in to comment.