Skip to content

Commit 4110eb8

Browse files
committed
add lendinhg project
1 parent 7274ed8 commit 4110eb8

28 files changed

+1799
-23
lines changed

project-10-lending/.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

project-10-lending/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.anchor
2+
.DS_Store
3+
target
4+
**/*.rs.bk
5+
node_modules
6+
test-ledger
7+
.yarn

project-10-lending/.prettierignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.anchor
2+
.DS_Store
3+
target
4+
node_modules
5+
dist
6+
build
7+
test-ledger

project-10-lending/Anchor.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[toolchain]
2+
3+
[features]
4+
resolution = true
5+
skip-lint = false
6+
7+
[programs.localnet]
8+
lending = "CdZeD33fXsAHfZYS8jdxg4qHgXYJwBQ1Bv6GJyETtLST"
9+
10+
[registry]
11+
url = "https://api.apr.dev"
12+
13+
[provider]
14+
cluster = "Localnet"
15+
wallet = "~/.config/solana/id.json"
16+
17+
[scripts]
18+
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"

project-10-lending/Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[workspace]
2+
members = [
3+
"programs/*"
4+
]
5+
resolver = "2"
6+
7+
[profile.release]
8+
overflow-checks = true
9+
lto = "fat"
10+
codegen-units = 1
11+
[profile.release.build-override]
12+
opt-level = 3
13+
incremental = false
14+
codegen-units = 1

project-10-lending/README.md

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,78 @@
1-
# developer-bootcamp-2024
1+
# Simplified Lending Protocol
22

3-
## Lending Project
3+
### Disclaimer: This code is not audited and should not be used in production. There are several edge cases that have not been addressed and error handling that has not been implemented. This is for education purposes only.
44

5-
### Program state
5+
## Program Terminology
66

7-
Solana program states are stored in data accounts. There are three accounts involved here which are used for storing the state of the lending program.
7+
1. Health Factor -
8+
2. Max Loan to Value Ratio (LTV) - Max percentage of collateral that can be borrowed
9+
3. Collateral -
10+
4. Liquidation Threshold -
11+
5. Shares -
12+
When users deposit assets into a lending protocol, these assets typically earn interest over time. If the protocol only recorded the initial deposit amount without a mechanism for shares:
813

9-
1. `LendingProgram` -> This account stores the owner which has authority to manage tokens of reserve.
10-
2. `Reserve` -> The reserve holds tokens of specific mint which are deposited by the users. It has state which keeps tracks of liquidity tokens and collateral tokens of users.
11-
3. `Obligation` -> This account is a contract between user and the lending program. It keeps track of users deposits and borrows from reserve.
14+
It would need to continuously update every user's balance as interest is added, which is computationally expensive and inefficient on a blockchain where each state update consumes gas and resources.
1215

13-
### Program Instructions
16+
By using shares:
1417

15-
Instructions are functions where logic of the program is stored. We can create new accounts, create tokens, mint and transfer tokens with instructions. Instructions can be called from client programs.
18+
The total amount of deposited assets and the total number of shares issued are tracked.
19+
When a user deposits assets, they receive a proportion of total shares relative to their deposit.
20+
The value of each share increases over time as interest accrues to the pool, but the number of shares owned by each depositor remains constant. This way, the system only needs to update the total balance and the share value, not each individual's balance.
1621

17-
There are seven instructions in this program.
22+
Interest Rate Model for implementing a dynamic APY:
1823

19-
1. `init_lending_market` -> Create new lending market account.
20-
2. `init_reserve` -> Create new reserve account. Also create liquidity and collateral token accounts.
21-
3. `init_obligation` -> Create new obligation for user.
22-
4. `supply` -> Users can lend tokens by calling this instruction with token mint and token account.
23-
5. `borrow` -> Borrow tokens from the reserve
24-
6. `repay` -> Repay borrowed tokens
25-
7. `withdraw` -> Withdraw all the deposited tokens.
24+
## Formulas
25+
26+
**Variable Interest Rate Model:**
27+
28+
- $Utilization Ratio = total Borrows/total Deposits$
29+
30+
- $Deposit Interest Rate = d_b + min(UR, 1) * (d_m, d_b)$
31+
32+
- $Borrow Interest Rate = b_b + UR * (b_m - b_b)$
33+
34+
- $Accrued Deposit Interest = total Deposits * deposit Rate * (now - last Interest Update)$
35+
36+
- $Accrued Borrow Interest = total Borrows * borrow Rate (now - late Interest Update)$
37+
38+
where,
39+
40+
- $UR = Utilization Ratio$
41+
- $d_b = Deposit Base Rate$
42+
- $b_b = Borrow Base Rate$
43+
- $d_m = Deposit Max Rate$
44+
- $b_m = Borrow Max Rate$
45+
46+
Note: This is to be used if you are attempting the challenge to implement a dynamic apy into the program
47+
48+
**Share Value**
49+
50+
- $TotalAssets = TotalDeposits + AccruedInterest$
51+
52+
- $Share Price = TotalAssets / TotalShares$
53+
54+
- $WithdrawableAssets = UserShares * SharePrice$
55+
56+
**Deposit and Borrow Shares**
57+
58+
- $Deposit Ratio = amountDeposited / totalBankDeposits$
59+
60+
- $User Deposit Shares = totalBankDepositShares * depositRatio$
61+
62+
- $Borrow Ratio = amountBorrowed / totalBankBorrowed$
63+
64+
- $User Borrowed Shares = totalBankBorrowedShares * borrowRatio$
65+
66+
**Health Factor**
67+
68+
- $Total Collateral = ∑ (assetPrice_i * assetDepositAmount_i)$
69+
70+
- $Total Borrowed = ∑ (assetPrice_i * assetBorrowAmount_i)$
71+
72+
- $Health Factor =
73+
74+
Note: Health Factor should be updated dynamically based on key triggers:
75+
76+
- **Deposits/Withdrawals:** Recalculate when users deposit or withdraw collateral.
77+
- **Borrow/Repay:** Recalculate when users borrow new funds or repay existing debts.
78+
- **Price Updates:** Recalculate when the price feed updates the value of any assets involved.

0 commit comments

Comments
 (0)