THORChain Lending Code Walkthrough notes

2 minute read

I am keeping an eye on THORFi lending and trying to push a plan for its release. There is a lot of discussion around the pros/cons of lending, so I would like to see a comprehensive testing and review process. Learn more abount the risks of lending here

The code is in review at moment across three branches.

MRs

  1. https://gitlab.com/thorchain/thornode/-/merge_requests/2713 (Lending)
  2. https://gitlab.com/thorchain/thornode/-/merge_requests/2658 (Virtual pool gets generate at the beginning of each swap)
  3. https://gitlab.com/thorchain/thornode/-/merge_requests/2723 (Circuit Breaker)

Code walkthrough nodes

Derived Asset Virtual Pool, recalc pre swap

  1. Has derived asset type (which is already used for TOR)
  2. V Pool spawned twice. 1. on begin block to allow API query (to allow lending quotes). 2. On v pool swap as required, calcs pool depth to determine the exact slip.
  3. Virtual Pool depts are re-calculated before every Virtual Pool swap, so no sandwich attacks or swap queue manipulation is possible.
  4. RUNE slip is fee is burnt.

Lending Open and Close Loans

  1. Min out will be added for min loan amount (open/close), like LIM for a swap. Will be in a different but provisions allowed for it in msg structures.
  2. 5 new mimirs
    1. Min CR
    2. Max CR
    3. Pause Loans - stop open and close loans
    4. LoanRepaymentMaturity - enable min time for loan open.
    5. CRPoolMultipler - cap collateral in a pool
    6. EnableDerivedAssets
    7. (MaxRuneSupply for circuit breaker but no in code)
  3. Loan Open message
    1. Has affiliate for first L1 swap.
    2. Has Dex aggregation support to taking out the debit (target asset)
  4. Loan Close message
    1. Can pay off with any asset (is converted to TOR)
  5. Data for total pool collateral is collected and published in pool and pools endpoints.
  6. New Memos for loan open and close with an example
  7. New events emitted on loan open and repayment.
  8. Has collateral up and down (13:45) - gives better data over time and allows credit rollover for new loan when overpaid. (though the 4 vars are not in the current code base)
  9. Anyone can payoff anyone else’s loan, no restrictions.
  10. Loan Close: Asset in calls LoanRepayment_handler that calls Swap handler (to swap L1 to TOR) that then calls LoanRepayment_handler after asset in has been swapped to TOR - thus and LoanRepayement_handler is recursive. Be good to map thatin a digram

Circuit Breaker Part 1

  1. Part 1: For loan close, will not mint Rune beyond 500 mil, checks native supply before minting new RUNE. If over, will move RUNE from the reserve instead.
  2. Part 2 is in the lending PR (2713) but not covered in the video
    1. If the supply of RUNE is 500M (MaxRuneSupply Mimir) or greater, don’t allow the creation of new loans.
    2. Can’t find Mimir value in constants
    3. I put comment on the MR about this