There is a double deduction of the pool balance in the refinance method in lender.sol
The first deduction is here.
the 2nd occurs after the loan is updated.
function test_double_deduction() public {
vm.startPrank(lender1);
uint256 initPoolBalance = 1000*10**18;
uint256 loanSize = 500*10**18;
Pool memory p = Pool({
lender: lender1,
loanToken: address(loanToken),
collateralToken: address(collateralToken),
minLoanSize: 100*10**18,
poolBalance: initPoolBalance,
maxLoanRatio: 2*10**18,
auctionLength: 1 days,
interestRate: 1000,
outstandingLoans: 0
});
bytes32 poolId = lender.setPool(p);
(,,,,uint256 poolBalance,,,,) = lender.pools(poolId);
assertEq(poolBalance, initPoolBalance);
vm.startPrank(borrower);
collateralToken.approve(address(lender), loanSize);
Borrow memory b = Borrow({
poolId: poolId,
debt: loanSize,
collateral: loanSize
});
Borrow[] memory borrows = new Borrow[](1);
borrows[0] = b;
lender.borrow(borrows);
vm.startPrank(lender2);
Pool memory p2 = Pool({
lender: lender2,
loanToken: address(loanToken),
collateralToken: address(collateralToken),
minLoanSize: 100*10**18,
poolBalance: 1000*10**18,
maxLoanRatio: 2*10**18,
auctionLength: 1 days,
interestRate: 1000,
outstandingLoans: 0
});
bytes32 poolId2 = lender.setPool(p2);
vm.startPrank(borrower);
Refinance memory r = Refinance({
loanId: 0,
poolId: keccak256(
abi.encode(
address(lender2),
address(loanToken),
address(collateralToken)
)
),
debt: loanSize,
collateral: loanSize
});
Refinance[] memory rs = new Refinance[](1);
rs[0] = r;
lender.refinance(rs);
(,,,,uint256 poolBalance2,,,,) = lender.pools(poolId2);
assertEq(poolBalance2, initPoolBalance - loanSize);
}
High. The owner of the pool can lose up to half the funds if the pool is size 2*debt.
Remove the following lines of code from refinance.