Lender - Refinance a loan

New lender: refinance a called loan (Dutch auction)

This function lets a new lender buy out a called loan during the auction window.

function transfer(uint256 _loanId, uint256 _newRate) external;

Note

  • Collateral stays inside the PolyLend contract for the entire refinancing; only ownership of the loan (and USDC) changes.

Flow

  • Check loan is valid.

  • Check loan is called.

  • Check that AUCTION_DURATION has not elapsed since the auction started

  • Check _newRate is between InterestLib.ONE and MAX_INTEREST.

  • Compute target auction rate based on how much of the 24-hour window has elapsed. In the original PolyLend spec, the interest rate linearly increases from 0% to a maximum over 24 hours; here, MAX_INTEREST caps the per-second rate.

  • Enforce that _newRate is at least the target auction rate.

  • Verify msg.sender has sufficient USDC and allowance to pay amountOwed to the old lender plus protocol fee.

  • Calculate amountOwed.

  • Create and store a new Loan for the same borrower with the new lender

    • loanAmount = amountOwed (principal rolls into new loan).

    • rate = _newRate.

    • startTime = block.timestamp.

    • minimumDuration = 0 (the new lender can call after 0 seconds unless you change semantics)

  • Cancel the old loan by zeroing loan.borrower.

  • Pay the previous lender and fee:

    • Calcualte protocol fee.

    • Calculate lenderAmount = amountOwed - fee.

    • Transfer lenderAmount from new lender to old lender.

    • Transfer fee to protocol fee recipient.

  • Emit LoanTransferred.

Last updated