THORChain Lending Code Walkthrough notes
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
- https://gitlab.com/thorchain/thornode/-/merge_requests/2713 (Lending)
- https://gitlab.com/thorchain/thornode/-/merge_requests/2658 (Virtual pool gets generate at the beginning of each swap)
- https://gitlab.com/thorchain/thornode/-/merge_requests/2723 (Circuit Breaker)
Code walkthrough nodes
Derived Asset Virtual Pool, recalc pre swap
- Has derived asset type (which is already used for TOR)
- 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.
- Virtual Pool depts are re-calculated before every Virtual Pool swap, so no sandwich attacks or swap queue manipulation is possible.
- RUNE slip is fee is burnt.
Lending Open and Close Loans
- 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.
- 5 new mimirs
- Min CR
- Max CR
- Pause Loans - stop open and close loans
- LoanRepaymentMaturity - enable min time for loan open.
- CRPoolMultipler - cap collateral in a pool
- EnableDerivedAssets
- (MaxRuneSupply for circuit breaker but no in code)
- Loan Open message
- Has affiliate for first L1 swap.
- Has Dex aggregation support to taking out the debit (target asset)
- Loan Close message
- Can pay off with any asset (is converted to TOR)
- Data for total pool collateral is collected and published in pool and pools endpoints.
- New Memos for loan open and close with an example
- New events emitted on loan open and repayment.
- 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)
- Anyone can payoff anyone else’s loan, no restrictions.
- 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
- 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.
- Part 2 is in the lending PR (2713) but not covered in the video
- If the supply of RUNE is 500M (MaxRuneSupply Mimir) or greater, don’t allow the creation of new loans.
- Can’t find Mimir value in constants
- I put comment on the MR about this