[Legacy] TrueFi DAO pools contracts
Below is a brief guide to TrueFi lending pool contracts.
For detailed questions, please reach out to the Discord #dev channel.
Contract Name | Address |
---|---|
TrueFiPool2: lenders provide and withdraw liquidity to the pool, and can get lending pool details, such as pool value, pool token price, etc. from this contract
TrueLender2: implements the lending strategy for the TrueFi pool, i.e. how loans are approved and funded
LoanFactory2: deploys LoanTokens, which represent details of each loan on-chain
TrueRatingAgencyV2: loan applications are rated and approved by TRU stakers
TrueMultiFarm: lenders can stake lending pool tokens to earn TRU rewards (read more here)
SAFU: handles bad debt in TrueFi lending pools (read more here)
The TrueFiPool2
contract is used by TrueFi DAO lending pools -- tfUSDC / tfUSDT / tfBUSD / tfTUSD. This contract enables accounts to pool tokens with the goal of earning yields on underlying tokens.
To calculate the price of the lending pool token ("LP token"), take poolValue()
divided by totalSupply()
.
To calculate yield for a pool, one must calculate the weighted average of rates across each active loan in the pool and idle funds held in the pool.
pool_apy = SUM(loan_1_amount
*loan_1_apy + ... + loan_n_amount*loan_n_value) / poolValue()
For an example of how this can be done via SQL, see this Dune query.
Read more about default handling in #safu-default-handling
Implements the lending strategy for the TrueFi pool, i.e. how loans are approved and funded.
Loan tokens are created by LoanFactory2.
Each LoanToken has the following parameters:
borrower()
: address of borrower
amount()
: principal amount of fixed term loan
term()
: loan term in seconds
apy()
: loan APR in basis points (i.e. 971 = 9.71%)
status()
: returns loan's status, which progresses through the following states:
0
--> Awaiting:
Waiting for funding to meet capital requirements
1
-->Funded:
Capital requirements met, borrower can withdraw
2
-->Withdrawn:
Borrower withdraws money, loan waiting to be repaid
3
-->Settled:
Loan has been paid back in full with interest
4
-->Defaulted:
Loan has not been paid back in full
5
-->Liquidated:
Loan has Defaulted and stakers have been Liquidated
canTransfer()
: returns LoanTokens are non-transferable except for whitelisted addresses
debt()
: returns amount
+ interest
. Note that this does not take into account whether the loan has been repaid.
isRepaid()
: returns boolean value, indicating whether a loan has been repaid in full.
profit()
: returns difference between debt()
and amount()
, the anticipated interest to be paid on the loan
Repaying loans to lending pools has two steps:
Borrower repays funds to loan token
reclaim()
function is called on loan token, which burns the loan token and pays out fees to stkTRU.
As described here, the TrueRatingAgencyV2
contract determines whether a loan request should be funded by TrueLender2
.
stkTRU holders can vote yes()
or no()
on each loan. The TrueLender2
contract then checks to see if each loan satisfies threshold conditions in order to be funded.
stkTRU voters receive rewards in the form of TRU tokens for voting on loan requests. claimable()
reward tokens are calculated as follows:
claimable() = (# TRU voted by user / # total TRU votes) * (Total TRU Reward)
, where
Total TRU Reward = (Loan interest * TRU distribution factor * rewardMultiplier)
where Loan interest = (loan APR * term in days * principal) /365
. Loan APR, term, and principal can be obtained from the respective loan token contract
rewardMultiplier
can be found from the TrueRatingAgencyV2 contract
TRU distribution factor
is calculated as remaining
divided by amount
from the RatingAgencyV2Distributor contract
As described here, lenders can stake LP tokens into the TrueMultiFarm contract to get TRU rewards.
Total TRU emissions per day can be calculated using totalAmount()
and duration()
found on the distributor contract:
TRU distribution per day = (totalAmount/10^8) / (duration/(24*3600))
To calculate farm rates for each token, use the formula below:
TRU rewards per farm per day = TRU distribution per day * getShare(IERC20 token) / shares()
as described below
Contract Name | Address |
---|---|
Method | Notes |
---|---|
Method | Notes |
---|---|
Method | Notes |
---|---|
Contract Name | Address |
---|---|
Method | Notes |
---|---|
Contract Name | Address |
---|---|
Method | Notes |
---|---|
Contract Name | Address |
---|---|
Contract Name | Address |
---|---|
Method | Notes |
---|---|
tfUSDC
tfUSDT
tfTUSD
tfBUSD
TrueFiPool2
Lends a certain amount of underlying tokens to the portfolio, and mints and transfers corresponding amount of ERC-20 lending pool ("LP") tokens to the lender.
Withdraws liquidity from the lending pool. Lender transfers pool tokens to pool and receives underlying token, but with a small penalty for liquidity as calculated by
liquidExitPenalty()
described below.
Returns pool value in underlying token.
This is the virtual price of the entire pool, including values for loan tokens and liquid underlying tokens held by the pool.
Note: this assumes defaulted loans are worth their full value.
Returns virtual value of liquid assets in the pool.
totalSupply()
Returns total number of pool LP tokens.
liquidRatio()
Ratio of liquid assets in the pool to the pool value, in basis points. For example, 4683 => 46.83%.
utilization()
Returns utilization in basis points. For example, 5316 => 53.16%.
Calculated as 1 - liquidRatio()
.
Calculates lender will pay to withdraw liquid funds from the pool. This returns a proportion to be applied if liquidExit()
is performed with the given amount of LP tokens.
For example, when a pool is at ~75% utilization a small withdrawal would return liquidExitPenalty
= ~0.9980, meaning that the lender would pay an exit penalty of ~0.20% (20 bps) to withdraw from the pool.
For more detail on liquid exit and how the penalty is calculated, see here.
averageExitPenalty(uint256 from, uint256 to)
Internal function for calculating exit penalty.
Calls SAFU function to liquidate bad debt. Liquidates a defaulted Loan, withdraws a portion of TRU from staking pool then tries to cover the loan with own funds, to compensate lending pool. Loan must be in defaulted
state. If SAFU does not have enough funds, deficit is saved to be redeemed later.
reclaimDeficit(ILoanToken2 loan, uint256 amount)
After a defaulted loan's debt has been redeemed by the SAFU, the lending pool can reclaim call this function to redeem "deficiency claim" tokens for underlying tokens held in the SAFU.
TrueLender2
When called, sends funds from the pool to the loan token contract. Pool receives and holds loan tokens. Must be called by the loan borrower.
loanIsCredible(uint256 yesVotes, uint256 noVotes)
Checks whether loan receives sufficient votes via stkTRU rating to be funded. Checks whether loan receives minimum ratio of yes to no votes and hits threshold of minimum votes set by owner.
reclaim(ILoanToken2 loanToken, bytes calldata data)
Redeems LoanTokens held by the pool for underlying funds held in the loan token. Loan token must be settled before calling this function.
reclaimDeficit(ILoanToken2 loan, uint256 amount)
After a defaulted loan's debt has been redeemed by the SAFU, the lending pool can reclaim call this function to redeem "deficiency claim" tokens for underlying tokens held in the SAFU.
LoanFactory2
fund()
Transfers tokens to loan token contract from pool. Can be called only by lender contract. Sets status to Funded, start time, lender.
withdraw(address _beneficiary)
Borrower calls this function to withdraw funds to address provided. This sets the status of the loan to Withdrawn
.
repayInFull(address _sender)
Function for borrower to repay all of the remaining loan balance. Borrower should use this to ensure full repayment.
_repay(address _sender, uint256 _amount)
Internal function for loan repayment. If _amount
is sufficient, then this also settles the loan.
settle()
Moves loan to status = 'settled' if loan is fully repaid.
reclaim()
Function for borrower to reclaim any underlying currency stuck in the loan token contract. Only the borrower can call this function after the loan has been settled, defaulted, or liquidated.
TrueRatingAgencyV2
TrueMultiFarm
LinearTrueDistributor
stake(IERC20 token, uint256 amount)
Stake tokens to the farm. Upon staking, this will claim any claimable rewards.
unstake(IERC20 token, uint256 amount)
Remove staked tokens
claim(IERC20[] calldata tokens)
Claim TRU rewards
exit(IERC20[] calldata tokens)
Unstake amount and claim rewards
claimable(IERC20 token, address account)
Returns the claimable TRU reward for an account that is staking tokens.
shares()
Returns denominator for total farm rewards rate
getShare(IERC20 token)
Returns numerator for LP token's share of farm rewards