@@ -5,7 +5,7 @@ import { parseReserve } from "../state/reserve";
5
5
import BN from "bn.js" ;
6
6
import { WAD , WANG } from "./constants" ;
7
7
import { ReserveConfigType , RewardsDataType , ReserveDataType } from "./shared" ;
8
- import { SLOTS_PER_YEAR } from "../core/constants " ;
8
+ import { calculateBorrowInterest , calculateSupplyInterest } from "../core" ;
9
9
10
10
type ParsedReserve = NonNullable < ReturnType < typeof parseReserve > > [ "info" ] ;
11
11
@@ -29,69 +29,19 @@ export class SolendReserve {
29
29
}
30
30
31
31
private calculateSupplyAPY = ( reserve : ParsedReserve ) => {
32
- const apr = this . calculateSupplyAPR ( reserve ) ;
33
- const apy =
34
- new BigNumber ( 1 )
35
- . plus ( new BigNumber ( apr ) . dividedBy ( SLOTS_PER_YEAR ) )
36
- . toNumber ( ) **
37
- SLOTS_PER_YEAR -
38
- 1 ;
39
- return apy ;
32
+ return calculateSupplyInterest ( reserve , true ) . toNumber ( ) ;
40
33
} ;
41
34
42
35
private calculateBorrowAPY = ( reserve : ParsedReserve ) => {
43
- const apr = this . calculateBorrowAPR ( reserve ) ;
44
- const apy =
45
- new BigNumber ( 1 )
46
- . plus ( new BigNumber ( apr ) . dividedBy ( SLOTS_PER_YEAR ) )
47
- . toNumber ( ) **
48
- SLOTS_PER_YEAR -
49
- 1 ;
50
- return apy ;
36
+ return calculateBorrowInterest ( reserve , true ) . toNumber ( ) ;
51
37
} ;
52
38
53
39
private calculateSupplyAPR ( reserve : ParsedReserve ) {
54
- const currentUtilization = this . calculateUtilizationRatio ( reserve ) ;
55
-
56
- const borrowAPY = this . calculateBorrowAPR ( reserve ) ;
57
- return currentUtilization * borrowAPY ;
58
- }
59
-
60
- private calculateUtilizationRatio ( reserve : ParsedReserve ) {
61
- const totalBorrowsWads = new BigNumber (
62
- reserve . liquidity . borrowedAmountWads . toString ( )
63
- ) . div ( WAD ) ;
64
- const currentUtilization = totalBorrowsWads
65
- . dividedBy (
66
- totalBorrowsWads . plus ( reserve . liquidity . availableAmount . toString ( ) )
67
- )
68
- . toNumber ( ) ;
69
-
70
- return currentUtilization ;
40
+ return calculateSupplyInterest ( reserve , false ) . toNumber ( ) ;
71
41
}
72
42
73
43
private calculateBorrowAPR ( reserve : ParsedReserve ) {
74
- const currentUtilization = this . calculateUtilizationRatio ( reserve ) ;
75
- const optimalUtilization = reserve . config . optimalUtilizationRate / 100 ;
76
-
77
- let borrowAPR ;
78
- if ( optimalUtilization === 1.0 || currentUtilization < optimalUtilization ) {
79
- const normalizedFactor = currentUtilization / optimalUtilization ;
80
- const optimalBorrowRate = reserve . config . optimalBorrowRate / 100 ;
81
- const minBorrowRate = reserve . config . minBorrowRate / 100 ;
82
- borrowAPR =
83
- normalizedFactor * ( optimalBorrowRate - minBorrowRate ) + minBorrowRate ;
84
- } else {
85
- const normalizedFactor =
86
- ( currentUtilization - optimalUtilization ) / ( 1 - optimalUtilization ) ;
87
- const optimalBorrowRate = reserve . config . optimalBorrowRate / 100 ;
88
- const maxBorrowRate = reserve . config . maxBorrowRate / 100 ;
89
- borrowAPR =
90
- normalizedFactor * ( maxBorrowRate - optimalBorrowRate ) +
91
- optimalBorrowRate ;
92
- }
93
-
94
- return borrowAPR ;
44
+ return calculateBorrowInterest ( reserve , false ) . toNumber ( ) ;
95
45
}
96
46
97
47
setBuffer ( buffer : AccountInfo < Buffer > | null ) {
@@ -105,23 +55,23 @@ export class SolendReserve {
105
55
if ( ! this . buffer ) {
106
56
this . buffer = await this . connection . getAccountInfo (
107
57
new PublicKey ( this . config . address ) ,
108
- "processed"
58
+ "processed" ,
109
59
) ;
110
60
}
111
61
112
62
if ( ! this . buffer ) {
113
63
throw Error (
114
- `Error requesting account info for ${ this . config . liquidityToken . name } `
64
+ `Error requesting account info for ${ this . config . liquidityToken . name } ` ,
115
65
) ;
116
66
}
117
67
118
68
const parsedData = parseReserve (
119
69
new PublicKey ( this . config . address ) ,
120
- this . buffer
70
+ this . buffer ,
121
71
) ?. info ;
122
72
if ( ! parsedData ) {
123
73
throw Error (
124
- `Unable to parse data of reserve ${ this . config . liquidityToken . name } `
74
+ `Unable to parse data of reserve ${ this . config . liquidityToken . name } ` ,
125
75
) ;
126
76
}
127
77
@@ -133,7 +83,7 @@ export class SolendReserve {
133
83
poolSize : string ,
134
84
rewardPrice : number ,
135
85
tokenPrice : number ,
136
- decimals : number
86
+ decimals : number ,
137
87
) {
138
88
const poolValueUSD = new BigNumber ( poolSize )
139
89
. times ( tokenPrice )
@@ -162,14 +112,14 @@ export class SolendReserve {
162
112
stats . totalDepositsWads . toString ( ) ,
163
113
reward . price ,
164
114
stats . assetPriceUSD ,
165
- this . config . liquidityToken . decimals
115
+ this . config . liquidityToken . decimals ,
166
116
) . toNumber ( ) ,
167
117
price : reward . price ,
168
118
} ) ) ;
169
119
170
120
const totalAPY = new BigNumber ( stats . supplyInterestAPY )
171
121
. plus (
172
- rewards . reduce ( ( acc , reward ) => acc . plus ( reward . apy ) , new BigNumber ( 0 ) )
122
+ rewards . reduce ( ( acc , reward ) => acc . plus ( reward . apy ) , new BigNumber ( 0 ) ) ,
173
123
)
174
124
. toNumber ( ) ;
175
125
@@ -196,14 +146,14 @@ export class SolendReserve {
196
146
stats . totalBorrowsWads . toString ( ) ,
197
147
reward . price ,
198
148
stats . assetPriceUSD ,
199
- this . config . liquidityToken . decimals
149
+ this . config . liquidityToken . decimals ,
200
150
) . toNumber ( ) ,
201
151
price : reward . price ,
202
152
} ) ) ;
203
153
204
154
const totalAPY = new BigNumber ( stats . borrowInterestAPY )
205
155
. minus (
206
- rewards . reduce ( ( acc , reward ) => acc . plus ( reward . apy ) , new BigNumber ( 0 ) )
156
+ rewards . reduce ( ( acc , reward ) => acc . plus ( reward . apy ) , new BigNumber ( 0 ) ) ,
207
157
)
208
158
. toNumber ( ) ;
209
159
@@ -215,11 +165,11 @@ export class SolendReserve {
215
165
}
216
166
217
167
private formatReserveData (
218
- parsedData : NonNullable < ReturnType < typeof parseReserve > > [ "info" ]
168
+ parsedData : NonNullable < ReturnType < typeof parseReserve > > [ "info" ] ,
219
169
) : ReserveDataType {
220
170
const totalBorrowsWads = parsedData . liquidity . borrowedAmountWads ;
221
171
const totalLiquidityWads = parsedData . liquidity . availableAmount . mul (
222
- new BN ( WAD )
172
+ new BN ( WAD ) ,
223
173
) ;
224
174
const totalDepositsWads = totalBorrowsWads . add ( totalLiquidityWads ) ;
225
175
const cTokenExchangeRate = new BigNumber ( totalDepositsWads . toString ( ) )
@@ -238,13 +188,13 @@ export class SolendReserve {
238
188
maxBorrowRate : parsedData . config . maxBorrowRate / 100 ,
239
189
protocolTakeRate : parsedData . config . protocolTakeRate / 100 ,
240
190
borrowFeePercentage : new BigNumber (
241
- parsedData . config . fees . borrowFeeWad . toString ( )
191
+ parsedData . config . fees . borrowFeeWad . toString ( ) ,
242
192
)
243
193
. dividedBy ( WAD )
244
194
. toNumber ( ) ,
245
195
hostFeePercentage : parsedData . config . fees . hostFeePercentage / 100 ,
246
196
flashLoanFeePercentage : new BigNumber (
247
- parsedData . config . fees . flashLoanFeeWad . toString ( )
197
+ parsedData . config . fees . flashLoanFeeWad . toString ( ) ,
248
198
)
249
199
. dividedBy ( WAD )
250
200
. toNumber ( ) ,
@@ -262,6 +212,8 @@ export class SolendReserve {
262
212
totalLiquidityWads,
263
213
supplyInterestAPY : this . calculateSupplyAPY ( parsedData ) ,
264
214
borrowInterestAPY : this . calculateBorrowAPY ( parsedData ) ,
215
+ supplyInterestAPR : this . calculateSupplyAPR ( parsedData ) ,
216
+ borrowInterestAPR : this . calculateBorrowAPR ( parsedData ) ,
265
217
assetPriceUSD : new BigNumber ( parsedData . liquidity . marketPrice . toString ( ) )
266
218
. div ( WAD )
267
219
. toNumber ( ) ,
0 commit comments