Overview

## Contracts

We have four main contracts in our Protocol, and they are SavingAccount, Accounts, Bank, TokenRegistry, and GlobalConfig.
The first three contracts are upgradable contracts, and they contain the main business logic of our Protocol. The overall structure of our Protocol is monolithic, which means that SavingAccount, Accounts, and Bank contracts work closely with each other. We decoupled this into three contracts only for code size limit reasons. The naming here maybe a little counter intuitive. Normally when we want to deposit money to a bank, we first go to the bank and open a saving account, then we deposit money to this account. Here, SavingAccount contract behaves like a traditional bank, which is the entry point for you to deposit or borrow money from DeFiner. The Bank contract records the state of the pool of DeFiner's protocol, like the total deposit, total borrow, and the current rate. The Accounts contract records the deposit and borrow balances for each specific user.
The workflow of a transaction to DeFiner works as the following diagram. The user sends transactions by calling functions on SavingAccount contract, then the SavingAccount contract will call the Bank contract to create a rate index checkpoint, which is used to compute the accumulated interests for all the user. Then the Bank contract will call the functions in Account contract to compute the balance change in the user account.
The workflow of a transaction
The TokenRegistry and GlobalConfig contracts are used to save data that will be used among the three main contracts. Like all three contracts' addresses, so in these three contract, they only need to keep global config's address in contract's memory space.

## SavingAccount

This is the interface contract that our users will mainly interact with. Users will send transactions to this contract to do deposit, withdraw, borrow, repay, withdrawAll, and liquidate functions to this contract to interact with our protocol. For each operation, this contract is the contract that receives and sends out the tokens. Whenever there is a transaction sent to SavingAccount contract, it will emit an event to log this transaction.

### Borrowing Power

The borrowing power of a user is related to the collateral that the user deposited into our system. The price is obtained from Chainlink's oracle. If the LTV of a user is too large then it is at the risk of being liquidated. Currently, the liquidation threshold is set to around 0.8.

## Bank

This contract is used to create rate indexes whenever there is an interaction with our SavingAccount contract. We are using rate indexes to compute the borrow and deposit interests. Given the different indexes, we compute the user balances here. Whenever an index is created, this contract will emit an event.

### Rate Index

i and j here represent two different blocks.
$RateIndex_i = RateIndex_j * (1 + RatePerBlock_{ij} * (i - j))$

### Balance

i and j here represent two different blocks.
$Balance_i = Balance_j \times RateIndex_j \div RateIndex_i$
Borrow balances and deposit balances both use this method to compute.

## Accounts

This contract is used to record the balances of each user account for different tokens. This contract also contains a BitMap which will quickly show whether a user has depositings/borrowings for each token while costs less gas.

## GlobalConfig

This contract is used to store all the contract's addresses, so all other contracts can only keep GlobalConfig's address to call functions of other contracts. It also used to config the reserve ratio and community fund ratio of the protocol.

## TokenRegistry

This contract records the meta-data about each contract supported token. We can add new supported tokens using this contract.

1. 1.
DAI
1. 1.
2. 2.
2. 2.
USDC
1. 1.
2. 2.
3. 3.
USDT
1. 1.
2. 2.
4. 4.
TUSD
1. 1.
2. 2.
5. 5.
MKR
1. 1.
2. 2.
6. 6.
BAT
1. 1.
2. 2.
7. 7.
ZRX
1. 1.
2. 2.
8. 8.
REP
1. 1.
2. 2.
9. 9.
WBTC
1. 1.
2. 2.
10. 10.
FIN
1. 1.
2. 2.