Integration guide
We plan to improve this section dramatically. For now, introduce yourself at our dev-intro channel on discord to gain access to dev-support! Our developers will be there to assist you with your questions.
Or checkout our dev portal here.

Introduction

Solend has 3 types of accounts: lending market, reserve, and obligation.
There's only one instance of a lending market for now. There's a one-to-one mapping from listed asset to reserve. There's a one-to-one mapping from user to obligations (which tracks user's collateral/borrows).

Calculating TVL from on-chain data example

Deriving obligation address

1
const seed = LENDING_MARKET_ADDRESS.slice(0, 32);
2
const obligationAddress = await PublicKey.createWithSeed(
3
new PublicKey(address),
4
seed,
5
new PublicKey(SOLEND_PROGRAM_ID),
6
);
Copied!

Calculating APY

The following typescript snippet is extracted from our codebase
1
export const calculateSupplyAPY = (reserve: Reserve) => {
2
const currentUtilization = calculateUtilizationRatio(reserve);
3
const borrowAPY = calculateBorrowAPY(reserve);
4
return currentUtilization * borrowAPY;
5
};
6
7
export const calculateUtilizationRatio = (reserve: Reserve) => {
8
const borrowedAmount = reserve.liquidity.borrowedAmountWads
9
.div(new BN(`1${''.padEnd(18, '0')}`))
10
.toNumber();
11
const availableAmount = reserve.liquidity.availableAmount.toNumber();
12
const currentUtilization =
13
borrowedAmount / (availableAmount + borrowedAmount);
14
return currentUtilization;
15
};
16
17
export const calculateBorrowAPY = (reserve: Reserve) => {
18
const currentUtilization = calculateUtilizationRatio(reserve);
19
const optimalUtilization = reserve.config.optimalUtilizationRate / 100;
20
let borrowAPY;
21
if (optimalUtilization === 1.0 || currentUtilization < optimalUtilization) {
22
const normalizedFactor = currentUtilization / optimalUtilization;
23
const optimalBorrowRate = reserve.config.optimalBorrowRate / 100;
24
const minBorrowRate = reserve.config.minBorrowRate / 100;
25
borrowAPY =
26
normalizedFactor * (optimalBorrowRate - minBorrowRate) + minBorrowRate;
27
} else {
28
const normalizedFactor =
29
(currentUtilization - optimalUtilization) / (1 - optimalUtilization);
30
const optimalBorrowRate = reserve.config.optimalBorrowRate / 100;
31
const maxBorrowRate = reserve.config.maxBorrowRate / 100;
32
borrowAPY =
33
normalizedFactor * (maxBorrowRate - optimalBorrowRate) +
34
optimalBorrowRate;
35
}
36
37
return borrowAPY;
38
};
Copied!