A borrower has the opportunity to move their loan to another pool under new lending conditions.
During refinancing interest from the loan is transferred to the old pool.
The debt is then transferred to the new pool however this subraction occurs twice, resulting in loss of funds to the Lenders pool.
Pool balances are reduced twice by the transferred debt. The larger the loan the greater the loss of funds to the new pool.
function test_Refinance() public {
vm.startPrank(lender1);
Pool memory p1 = Pool({
lender: lender1,
loanToken: address(loanToken),
collateralToken: address(collateralToken),
minLoanSize: 100 * 10 ** 18,
poolBalance: POOL_LOAN_TOKEN_BALANCE,
maxLoanRatio: 2 * 10 ** 18,
auctionLength: 1 days,
interestRate: 1000,
outstandingLoans: 0
});
Pool memory p2 = Pool({
lender: lender2,
loanToken: address(loanToken),
collateralToken: address(collateralToken),
minLoanSize: 100 * 10 ** 18,
poolBalance: POOL_B_LOAN_TOKEN_BALANCE,
maxLoanRatio: 2 * 10 ** 18,
auctionLength: 1 days,
interestRate: 1000,
outstandingLoans: 0
});
bytes32 poolIdOne = lender.setPool(p1);
vm.startPrank(lender2);
bytes32 poolIdTwo = lender.setPool(p2);
bytes32[] memory poolIds = new bytes32[](3);
poolIds[0] = poolIdOne;
poolIds[1] = poolIdTwo;
uint256[] memory loansIds = new uint256[](1);
loansIds[0] = 0;
vm.startPrank(borrower);
Borrow memory b = Borrow({poolId: poolIdOne, debt: LOAN_AMOUNT, collateral: 1000 * 10 ** 18});
Borrow[] memory borrows = new Borrow[](1);
borrows[0] = b;
lender.borrow(borrows);
Refinance memory r =
Refinance({loanId: 0, poolId: poolIdTwo, debt: 1000 * 10 ** 18, collateral: 1000 * 10 ** 18});
Refinance[] memory refinances = new Refinance[](1);
refinances[0] = r;
vm.warp(10 days);
(,,,, uint256 poolBalance,,,,) = lender.pools(poolIdTwo);
assertEq(poolBalance, (POOL_B_LOAN_TOKEN_BALANCE));
lender.refinance(refinances);
(,,,, poolBalance,,,,) = lender.pools(poolIdTwo);
assertEq(poolBalance, (POOL_B_LOAN_TOKEN_BALANCE) - (2 * 1000 * 10 ** 18));
}