# Developer docs

Below is a brief guide to TrueFi lending pool contracts.

For detailed questions, please reach out to the [Discord #dev](https://discord.com/channels/768936623684714517/881223601657901077) channel.

## Lending pool addresses

<table><thead><tr><th width="216">Contract Name</th><th>Address</th></tr></thead><tbody><tr><td>tfUSDC</td><td><a href="https://etherscan.io/address/0xA991356d261fbaF194463aF6DF8f0464F8f1c742">0xA991356d261fbaF194463aF6DF8f0464F8f1c742</a></td></tr><tr><td>tfUSDT</td><td><a href="https://etherscan.io/address/0x6002b1dcB26E7B1AA797A17551C6F487923299d7">0x6002b1dcB26E7B1AA797A17551C6F487923299d7</a></td></tr><tr><td>tfTUSD</td><td>​<a href="https://etherscan.io/address/0x97cE06c3e3D027715b2d6C22e67D5096000072E5">0x97cE06c3e3D027715b2d6C22e67D5096000072E5</a></td></tr><tr><td>tfBUSD</td><td><a href="https://etherscan.io/address/0x1Ed460D149D48FA7d91703bf4890F97220C09437">0x1Ed460D149D48FA7d91703bf4890F97220C09437</a></td></tr></tbody></table>

## ***TrueFi DAO Pool*****&#x20;contracts**

* [**TrueFiPool2**](#truefipool2-contract): 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**](#truelender2-contract): implements the lending strategy for the TrueFi pool, i.e. how loans are approved and funded
* [**LoanFactory2**](#what-are-loan-tokens): deploys LoanTokens, which represent details of each loan on-chain
* [**TrueRatingAgencyV2**](#trueratingagencyv2): loan applications are rated and approved by TRU stakers
* [**TrueMultiFarm**](#truemultifarm): lenders can stake lending pool tokens to earn TRU rewards (read more [here](https://docs.brila.finance/brila-protocol/other-concepts/legacy-dao-pools/pool/farming-liquidity-mining))
* **SAFU:** handles bad debt in TrueFi lending pools (read more [here](https://docs.brila.finance/brila-protocol/other-concepts/legacy-dao-pools/pool/safu-secure-asset-fund-for-users))

## TrueFiPool2 contract

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.&#x20;

<table><thead><tr><th width="216">Contract Name</th><th>Address</th></tr></thead><tbody><tr><td>TrueFiPool2</td><td><a href="https://etherscan.io/address/0x8d35372ea3e85c49a60f0a72edeff8629da3999a#code">0x8d35372ea3e85c49a60f0a72edeff8629da3999a</a></td></tr></tbody></table>

### **Lending & Withdrawing methods**

<table><thead><tr><th width="227">Method</th><th>Notes</th></tr></thead><tbody><tr><td><h4><code>join(uint256 amount)</code></h4></td><td>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.</td></tr><tr><td><h4><code>liquidExit(uint256 amount)</code></h4></td><td><p>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 </p><p><code>liquidExitPenalty()</code> described below.</p></td></tr></tbody></table>

### **Determining pool values**

#### Calculating LP token price

To calculate the price of the lending pool token ("LP token"), take `poolValue()` divided by `totalSupply()` .

#### Calculating pool APY

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](https://dune.com/queries/1325629).

<table><thead><tr><th width="235">Method</th><th>Notes</th></tr></thead><tbody><tr><td><h4><code>poolValue()</code></h4></td><td><p>Returns pool value in underlying token. </p><p>This is the virtual price of the entire pool, including values for loan tokens and liquid underlying tokens held by the pool. </p><p><em>Note: this assumes defaulted loans are worth their full value.</em></p></td></tr><tr><td><h4><code>liquidValue()</code></h4></td><td>Returns virtual value of liquid assets in the pool.</td></tr><tr><td><strong><code>totalSupply()</code></strong></td><td>Returns total number of pool LP tokens.</td></tr><tr><td><strong><code>liquidRatio()</code></strong></td><td>Ratio of liquid assets in the pool to the pool value, in basis points. For example, 4683 => 46.83%.</td></tr><tr><td><strong><code>utilization()</code></strong></td><td><p>Returns utilization in basis points. For example, 5316 => 53.16%.</p><p>Calculated as <code>1 - liquidRatio()</code>.</p></td></tr><tr><td><h4><code>liquidExitPenalty( uint256 amount )</code></h4></td><td><p>Calculates lender will pay to withdraw liquid funds from the pool. This returns a proportion to be applied if <code>liquidExit()</code> is performed with the given amount of LP tokens. </p><p></p><p>For example, when a pool is at ~75% utilization a small withdrawal would return <code>liquidExitPenalty</code> = ~0.9980, meaning that the lender would pay an exit penalty of ~0.20% (20 bps) to withdraw from the pool. </p><p>For more detail on liquid exit and how the penalty is calculated, see <a href="../pool#what-is-liquid-exit">here</a>.</p></td></tr><tr><td><strong><code>averageExitPenalty(uint256 from, uint256 to)</code></strong></td><td>Internal function for calculating exit penalty.</td></tr></tbody></table>

### **Default handling**

Read more about default handling in [#safu-default-handling](https://docs.brila.finance/brila-protocol/other-concepts/pool/safu-secure-asset-fund-for-users#safu-default-handling "mention")

<table><thead><tr><th width="312">Method</th><th>Notes</th></tr></thead><tbody><tr><td><h4><strong><code>liquidate(</code></strong><code>ILoanToken2 loan)</code></h4></td><td>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 <code>defaulted</code> state. If SAFU does not have enough funds, deficit is saved to be redeemed later.</td></tr><tr><td><strong><code>reclaimDeficit(ILoanToken2 loan, uint256 amount)</code></strong></td><td>After a defaulted loan's debt has been redeemed by the <a href="../pool/safu-secure-asset-fund-for-users#safu-default-handling">SAFU</a>, the lending pool can reclaim call this function to redeem "deficiency claim" tokens for underlying tokens held in the SAFU. </td></tr></tbody></table>

## TrueLender2 contract

Implements the lending strategy for the TrueFi pool, i.e. how loans are approved and funded.

<table><thead><tr><th width="216">Contract Name</th><th>Address</th></tr></thead><tbody><tr><td>TrueLender2</td><td><a href="https://etherscan.io/address/0xa606dd423dF7dFb65Efe14ab66f5fDEBf62FF583">0xa606dd423dF7dFb65Efe14ab66f5fDEBf62FF583</a></td></tr></tbody></table>

<table><thead><tr><th width="312">Method</th><th>Notes</th></tr></thead><tbody><tr><td><h4><code>fund()</code></h4></td><td>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.</td></tr><tr><td><strong><code>loanIsCredible(uint256 yesVotes, uint256 noVotes)</code></strong></td><td>Checks whether loan receives sufficient votes via <a href="loan-approval-process">stkTRU rating</a> to be funded. Checks whether loan receives minimum ratio of yes to no votes and hits threshold of minimum votes set by owner.</td></tr><tr><td><strong><code>reclaim(ILoanToken2 loanToken, bytes calldata data)</code></strong></td><td>Redeems LoanTokens held by the pool for underlying funds held in the loan token. Loan token must be settled before calling this function. </td></tr><tr><td><strong><code>reclaimDeficit(ILoanToken2 loan, uint256 amount)</code></strong></td><td>After a defaulted loan's debt has been redeemed by the <a href="../pool/safu-secure-asset-fund-for-users#safu-default-handling">SAFU</a>, the lending pool can reclaim call this function to redeem "deficiency claim" tokens for underlying tokens held in the SAFU. </td></tr></tbody></table>

## Loan Token contracts <a href="#what-are-loan-tokens" id="what-are-loan-tokens"></a>

Loan tokens are created by [LoanFactory2](https://etherscan.io/address/0x69d844fB5928d0e7Bc530cC6325A88e53d6685BC).

<table><thead><tr><th width="216">Contract Name</th><th>Address</th></tr></thead><tbody><tr><td>LoanFactory2</td><td><a href="https://etherscan.io/address/0x69d844fB5928d0e7Bc530cC6325A88e53d6685BC">0x69d844fB5928d0e7Bc530cC6325A88e53d6685BC</a></td></tr></tbody></table>

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.&#x20;
* `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<br>

<table><thead><tr><th width="312">Method</th><th>Notes</th></tr></thead><tbody><tr><td><strong><code>fund()</code></strong></td><td>Transfers tokens to loan token contract from pool. Can be called only by lender contract. Sets status to Funded, start time, lender.</td></tr><tr><td><strong><code>withdraw(address _beneficiary)</code></strong></td><td>Borrower calls this function to withdraw funds to address provided. This sets the status of the loan to <code>Withdrawn</code>.</td></tr><tr><td><strong><code>repayInFull(address _sender)</code></strong></td><td>Function for borrower to repay all of the remaining loan balance. Borrower should use this to ensure full repayment.</td></tr><tr><td><strong><code>_repay(address _sender, uint256 _amount)</code></strong></td><td>Internal function for loan repayment. If <code>_amount</code> is sufficient, then this also settles the loan.</td></tr><tr><td><strong><code>settle()</code></strong> </td><td>Moves loan to status = 'settled' if loan is fully repaid. </td></tr><tr><td><strong><code>reclaim()</code></strong></td><td>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.</td></tr></tbody></table>

Repaying loans to lending pools has two steps:

1. Borrower repays funds to loan token
2. `reclaim()` function is called on loan token, which burns the loan token and pays out fees to stkTRU.

## TrueRatingAgencyV2

As described [here](https://docs.brila.finance/brila-protocol/other-concepts/legacy-dao-pools/loan-approval-process), the `TrueRatingAgencyV2` contract determines whether a loan request should be funded by `TrueLender2`.

<table><thead><tr><th width="231">Contract Name</th><th>Address</th></tr></thead><tbody><tr><td>TrueRatingAgencyV2</td><td><a href="https://etherscan.io/address/0x05461334340568075bE35438b221A3a0D261Fb6b">0x05461334340568075bE35438b221A3a0D261Fb6b</a></td></tr></tbody></table>

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](https://etherscan.io/address/0x05461334340568075bE35438b221A3a0D261Fb6b#readProxyContract#F18) contract
  * `TRU distribution factor` is calculated as `remaining` divided by `amount` from the [RatingAgencyV2Distributor](https://etherscan.io/address/0x6151570934470214592AA051c28805cF4744BCA7#readProxyContract) contract

## **TrueMultiFarm**

As described [here](https://docs.brila.finance/brila-protocol/other-concepts/legacy-dao-pools/pool/farming-liquidity-mining), lenders can stake LP tokens into the TrueMultiFarm contract to get TRU rewards.&#x20;

<table><thead><tr><th width="231">Contract Name</th><th>Address</th></tr></thead><tbody><tr><td>TrueMultiFarm</td><td><a href="https://etherscan.io/address/0xec6c3FD795D6e6f202825Ddb56E01b3c128b0b10#readProxyContract">0xec6c3FD795D6e6f202825Ddb56E01b3c128b0b10</a></td></tr><tr><td>LinearTrueDistributor</td><td><a href="https://etherscan.io/address/0xc7AB606e551bebD69f7611CdA1Fc473f8E5b8f70">0xc7AB606e551bebD69f7611CdA1Fc473f8E5b8f70</a></td></tr></tbody></table>

#### How to calculate lender emissions per day

* Total TRU emissions per day can be calculated using `totalAmount()` and `duration()` found on the [distributor contract](https://etherscan.io/address/0xc7AB606e551bebD69f7611CdA1Fc473f8E5b8f70):
  * `TRU distribution per day = (totalAmount/10^8) / (duration/(24*3600))`
* To calculate farm rates for each token, use the formula below:&#x20;
  * `TRU rewards per farm per day = TRU distribution per day * getShare(IERC20 token) / shares()` as described below

<table><thead><tr><th width="312">Method</th><th>Notes</th></tr></thead><tbody><tr><td><strong><code>stake(IERC20 token, uint256 amount)</code></strong></td><td>Stake tokens to the farm. Upon staking, this will claim any claimable rewards.</td></tr><tr><td><strong><code>unstake(IERC20 token, uint256 amount)</code></strong></td><td>Remove staked tokens</td></tr><tr><td><strong><code>claim(IERC20[] calldata tokens)</code></strong></td><td>Claim TRU rewards</td></tr><tr><td><strong><code>exit(IERC20[] calldata tokens)</code></strong></td><td>Unstake amount and claim rewards</td></tr><tr><td><strong><code>claimable(IERC20 token, address account)</code></strong></td><td>Returns the claimable TRU reward for an account that is staking tokens.</td></tr><tr><td><strong><code>shares()</code></strong></td><td>Returns denominator for total farm rewards rate</td></tr><tr><td><strong><code>getShare(IERC20 token)</code></strong></td><td>Returns numerator for LP token's share of farm rewards</td></tr></tbody></table>
