@defisaver/positions-sdk 2.1.55 → 2.1.56-dev-exposure
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.mocharc.json +4 -4
- package/.nvmrc +1 -1
- package/README.md +64 -64
- package/cjs/aaveV3/index.d.ts +1 -0
- package/cjs/aaveV3/index.js +1 -0
- package/cjs/compoundV2/index.d.ts +1 -0
- package/cjs/compoundV2/index.js +1 -0
- package/cjs/compoundV3/index.d.ts +1 -0
- package/cjs/compoundV3/index.js +1 -0
- package/cjs/eulerV2/index.d.ts +1 -0
- package/cjs/eulerV2/index.js +1 -0
- package/cjs/fluid/index.d.ts +3 -0
- package/cjs/helpers/aaveHelpers/index.js +1 -0
- package/cjs/helpers/compoundHelpers/index.js +2 -0
- package/cjs/helpers/curveUsdHelpers/index.js +1 -0
- package/cjs/helpers/eulerHelpers/index.js +1 -0
- package/cjs/helpers/fluidHelpers/index.js +1 -0
- package/cjs/helpers/liquityV2Helpers/index.js +1 -0
- package/cjs/helpers/llamaLendHelpers/index.js +1 -0
- package/cjs/helpers/morphoBlueHelpers/index.js +67 -66
- package/cjs/helpers/sparkHelpers/index.js +1 -0
- package/cjs/liquity/index.js +2 -0
- package/cjs/maker/index.js +2 -0
- package/cjs/markets/morphoBlue/index.d.ts +4 -0
- package/cjs/markets/morphoBlue/index.js +36 -2
- package/cjs/moneymarket/moneymarketCommonService.d.ts +1 -0
- package/cjs/moneymarket/moneymarketCommonService.js +8 -1
- package/cjs/savings/morphoVaults/index.js +17 -17
- package/cjs/spark/index.d.ts +1 -0
- package/cjs/spark/index.js +1 -0
- package/cjs/types/aave.d.ts +1 -0
- package/cjs/types/common.d.ts +1 -0
- package/cjs/types/compound.d.ts +1 -0
- package/cjs/types/curveUsd.d.ts +2 -0
- package/cjs/types/euler.d.ts +1 -0
- package/cjs/types/fluid.d.ts +1 -0
- package/cjs/types/liquity.d.ts +1 -0
- package/cjs/types/liquityV2.d.ts +2 -0
- package/cjs/types/llamaLend.d.ts +2 -0
- package/cjs/types/maker.d.ts +1 -0
- package/cjs/types/morphoBlue.d.ts +4 -0
- package/cjs/types/morphoBlue.js +2 -0
- package/cjs/types/spark.d.ts +1 -0
- package/esm/aaveV3/index.d.ts +1 -0
- package/esm/aaveV3/index.js +1 -0
- package/esm/compoundV2/index.d.ts +1 -0
- package/esm/compoundV2/index.js +1 -0
- package/esm/compoundV3/index.d.ts +1 -0
- package/esm/compoundV3/index.js +1 -0
- package/esm/eulerV2/index.d.ts +1 -0
- package/esm/eulerV2/index.js +1 -0
- package/esm/fluid/index.d.ts +3 -0
- package/esm/helpers/aaveHelpers/index.js +2 -1
- package/esm/helpers/compoundHelpers/index.js +3 -1
- package/esm/helpers/curveUsdHelpers/index.js +2 -1
- package/esm/helpers/eulerHelpers/index.js +2 -1
- package/esm/helpers/fluidHelpers/index.js +2 -1
- package/esm/helpers/liquityV2Helpers/index.js +2 -1
- package/esm/helpers/llamaLendHelpers/index.js +2 -1
- package/esm/helpers/morphoBlueHelpers/index.js +68 -67
- package/esm/helpers/sparkHelpers/index.js +2 -1
- package/esm/liquity/index.js +2 -0
- package/esm/maker/index.js +2 -0
- package/esm/markets/morphoBlue/index.d.ts +4 -0
- package/esm/markets/morphoBlue/index.js +32 -0
- package/esm/moneymarket/moneymarketCommonService.d.ts +1 -0
- package/esm/moneymarket/moneymarketCommonService.js +6 -0
- package/esm/savings/morphoVaults/index.js +17 -17
- package/esm/spark/index.d.ts +1 -0
- package/esm/spark/index.js +1 -0
- package/esm/types/aave.d.ts +1 -0
- package/esm/types/common.d.ts +1 -0
- package/esm/types/compound.d.ts +1 -0
- package/esm/types/curveUsd.d.ts +2 -0
- package/esm/types/euler.d.ts +1 -0
- package/esm/types/fluid.d.ts +1 -0
- package/esm/types/liquity.d.ts +1 -0
- package/esm/types/liquityV2.d.ts +2 -0
- package/esm/types/llamaLend.d.ts +2 -0
- package/esm/types/maker.d.ts +1 -0
- package/esm/types/morphoBlue.d.ts +4 -0
- package/esm/types/morphoBlue.js +2 -0
- package/esm/types/spark.d.ts +1 -0
- package/package.json +48 -48
- package/src/aaveV2/index.ts +240 -240
- package/src/aaveV3/index.ts +638 -637
- package/src/aaveV3/merit.ts +97 -97
- package/src/aaveV3/merkl.ts +74 -74
- package/src/claiming/aaveV3.ts +154 -154
- package/src/claiming/compV3.ts +22 -22
- package/src/claiming/ethena.ts +61 -61
- package/src/claiming/index.ts +12 -12
- package/src/claiming/king.ts +66 -66
- package/src/claiming/morphoBlue.ts +118 -118
- package/src/claiming/spark.ts +225 -225
- package/src/compoundV2/index.ts +245 -244
- package/src/compoundV3/index.ts +275 -274
- package/src/config/contracts.ts +1320 -1320
- package/src/constants/index.ts +10 -10
- package/src/contracts.ts +171 -171
- package/src/curveUsd/index.ts +254 -254
- package/src/eulerV2/index.ts +325 -324
- package/src/exchange/index.ts +25 -25
- package/src/fluid/index.ts +1800 -1800
- package/src/helpers/aaveHelpers/index.ts +203 -202
- package/src/helpers/compoundHelpers/index.ts +278 -276
- package/src/helpers/curveUsdHelpers/index.ts +44 -40
- package/src/helpers/eulerHelpers/index.ts +230 -229
- package/src/helpers/fluidHelpers/index.ts +338 -335
- package/src/helpers/index.ts +10 -10
- package/src/helpers/liquityV2Helpers/index.ts +85 -82
- package/src/helpers/llamaLendHelpers/index.ts +56 -53
- package/src/helpers/makerHelpers/index.ts +52 -52
- package/src/helpers/morphoBlueHelpers/index.ts +406 -405
- package/src/helpers/sparkHelpers/index.ts +170 -169
- package/src/index.ts +49 -49
- package/src/liquity/index.ts +161 -159
- package/src/liquityV2/index.ts +703 -703
- package/src/llamaLend/index.ts +305 -305
- package/src/maker/index.ts +225 -223
- package/src/markets/aave/index.ts +118 -118
- package/src/markets/aave/marketAssets.ts +54 -54
- package/src/markets/compound/index.ts +243 -243
- package/src/markets/compound/marketsAssets.ts +97 -97
- package/src/markets/curveUsd/index.ts +69 -69
- package/src/markets/euler/index.ts +26 -26
- package/src/markets/fluid/index.ts +2900 -2900
- package/src/markets/index.ts +25 -25
- package/src/markets/liquityV2/index.ts +102 -102
- package/src/markets/llamaLend/contractAddresses.ts +141 -141
- package/src/markets/llamaLend/index.ts +235 -235
- package/src/markets/morphoBlue/index.ts +1020 -988
- package/src/markets/spark/index.ts +29 -29
- package/src/markets/spark/marketAssets.ts +12 -12
- package/src/moneymarket/moneymarketCommonService.ts +90 -84
- package/src/morphoBlue/index.ts +274 -274
- package/src/portfolio/index.ts +586 -586
- package/src/savings/index.ts +95 -95
- package/src/savings/makerDsr/index.ts +53 -53
- package/src/savings/makerDsr/options.ts +9 -9
- package/src/savings/morphoVaults/index.ts +80 -80
- package/src/savings/morphoVaults/options.ts +193 -193
- package/src/savings/skyOptions/index.ts +95 -95
- package/src/savings/skyOptions/options.ts +10 -10
- package/src/savings/sparkSavingsVaults/index.ts +60 -60
- package/src/savings/sparkSavingsVaults/options.ts +35 -35
- package/src/savings/yearnV3Vaults/index.ts +61 -61
- package/src/savings/yearnV3Vaults/options.ts +55 -55
- package/src/savings/yearnVaults/index.ts +73 -73
- package/src/savings/yearnVaults/options.ts +32 -32
- package/src/services/priceService.ts +278 -278
- package/src/services/utils.ts +115 -115
- package/src/services/viem.ts +57 -57
- package/src/setup.ts +8 -8
- package/src/spark/index.ts +460 -459
- package/src/staking/eligibility.ts +53 -53
- package/src/staking/index.ts +1 -1
- package/src/staking/staking.ts +192 -192
- package/src/types/aave.ts +200 -199
- package/src/types/claiming.ts +114 -114
- package/src/types/common.ts +116 -115
- package/src/types/compound.ts +146 -145
- package/src/types/curveUsd.ts +125 -123
- package/src/types/euler.ts +177 -176
- package/src/types/fluid.ts +486 -485
- package/src/types/index.ts +15 -15
- package/src/types/liquity.ts +31 -30
- package/src/types/liquityV2.ts +130 -128
- package/src/types/llamaLend.ts +163 -161
- package/src/types/maker.ts +64 -63
- package/src/types/merit.ts +1 -1
- package/src/types/merkl.ts +70 -70
- package/src/types/morphoBlue.ts +206 -202
- package/src/types/portfolio.ts +60 -60
- package/src/types/savings/index.ts +23 -23
- package/src/types/savings/makerDsr.ts +13 -13
- package/src/types/savings/morphoVaults.ts +32 -32
- package/src/types/savings/sky.ts +14 -14
- package/src/types/savings/sparkSavingsVaults.ts +15 -15
- package/src/types/savings/yearnV3Vaults.ts +17 -17
- package/src/types/savings/yearnVaults.ts +14 -14
- package/src/types/spark.ts +136 -135
- package/src/umbrella/index.ts +69 -69
- package/src/umbrella/umbrellaUtils.ts +29 -29
|
@@ -1,336 +1,339 @@
|
|
|
1
|
-
import Dec from 'decimal.js';
|
|
2
|
-
import { assetAmountInEth } from '@defisaver/tokens';
|
|
3
|
-
import {
|
|
4
|
-
FluidAggregatedVaultData, FluidAssetData,
|
|
5
|
-
FluidAssetsData, FluidDexBorrowDataStructOutput, FluidDexSupplyDataStructOutput, FluidUsedAsset,
|
|
6
|
-
FluidUsedAssets,
|
|
7
|
-
FluidVaultType,
|
|
8
|
-
InnerFluidMarketData,
|
|
9
|
-
} from '../../types';
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
import {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
const
|
|
35
|
-
const
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
payload
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
payload.
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
payload.
|
|
78
|
-
|
|
79
|
-
payload.
|
|
80
|
-
payload.
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
const
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
assetPrice = new Dec(
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
//
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
const
|
|
161
|
-
const
|
|
162
|
-
|
|
163
|
-
const
|
|
164
|
-
const
|
|
165
|
-
|
|
166
|
-
const
|
|
167
|
-
|
|
168
|
-
const
|
|
169
|
-
const
|
|
170
|
-
|
|
171
|
-
const
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
const
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
const
|
|
178
|
-
const
|
|
179
|
-
|
|
180
|
-
const
|
|
181
|
-
const
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
const
|
|
256
|
-
const
|
|
257
|
-
|
|
258
|
-
const
|
|
259
|
-
const
|
|
260
|
-
|
|
261
|
-
const
|
|
262
|
-
|
|
263
|
-
const
|
|
264
|
-
const
|
|
265
|
-
|
|
266
|
-
const
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
const
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
const
|
|
273
|
-
const
|
|
274
|
-
|
|
275
|
-
const
|
|
276
|
-
const
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
1
|
+
import Dec from 'decimal.js';
|
|
2
|
+
import { assetAmountInEth } from '@defisaver/tokens';
|
|
3
|
+
import {
|
|
4
|
+
FluidAggregatedVaultData, FluidAssetData,
|
|
5
|
+
FluidAssetsData, FluidDexBorrowDataStructOutput, FluidDexSupplyDataStructOutput, FluidUsedAsset,
|
|
6
|
+
FluidUsedAssets,
|
|
7
|
+
FluidVaultType,
|
|
8
|
+
InnerFluidMarketData,
|
|
9
|
+
} from '../../types';
|
|
10
|
+
import {
|
|
11
|
+
calcLeverageLiqPrice, getAssetsTotal, getExposure, isLeveragedPos,
|
|
12
|
+
} from '../../moneymarket';
|
|
13
|
+
import { calculateNetApy } from '../../staking';
|
|
14
|
+
import { LeverageType, MMAssetsData } from '../../types/common';
|
|
15
|
+
import { getEthAmountForDecimals } from '../../services/utils';
|
|
16
|
+
|
|
17
|
+
const calculateNetApyDex = ({ marketData, suppliedUsd, borrowedUsd }: { marketData: InnerFluidMarketData, suppliedUsd: string, borrowedUsd: string }) => {
|
|
18
|
+
const {
|
|
19
|
+
borrowRate,
|
|
20
|
+
supplyRate,
|
|
21
|
+
incentiveBorrowRate,
|
|
22
|
+
incentiveSupplyRate,
|
|
23
|
+
tradingBorrowRate,
|
|
24
|
+
tradingSupplyRate,
|
|
25
|
+
} = marketData;
|
|
26
|
+
|
|
27
|
+
const totalBorrowRate = new Dec(borrowRate).minus(tradingBorrowRate || '0').toString();
|
|
28
|
+
const totalSupplyRate = new Dec(supplyRate).add(tradingSupplyRate || '0').toString();
|
|
29
|
+
|
|
30
|
+
const borrowIncentive = new Dec(incentiveBorrowRate || '0').mul(borrowedUsd).div(100).toString();
|
|
31
|
+
const supplyIncentive = new Dec(incentiveSupplyRate || '0').mul(suppliedUsd).div(100).toString();
|
|
32
|
+
const incentiveUsd = new Dec(supplyIncentive).minus(borrowIncentive).toString();
|
|
33
|
+
|
|
34
|
+
const borrowInterest = new Dec(totalBorrowRate).mul(borrowedUsd).div(100).toString();
|
|
35
|
+
const supplyInterest = new Dec(totalSupplyRate).mul(suppliedUsd).div(100).toString();
|
|
36
|
+
const totalInterestUsd = new Dec(supplyInterest).add(incentiveUsd).minus(borrowInterest).toString();
|
|
37
|
+
const balance = new Dec(suppliedUsd).sub(borrowedUsd).toString();
|
|
38
|
+
const netApy = new Dec(totalInterestUsd).div(balance).times(100).toString();
|
|
39
|
+
|
|
40
|
+
return {
|
|
41
|
+
netApy,
|
|
42
|
+
incentiveUsd,
|
|
43
|
+
totalInterestUsd,
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export const getFluidAggregatedData = ({
|
|
48
|
+
usedAssets,
|
|
49
|
+
assetsData,
|
|
50
|
+
marketData,
|
|
51
|
+
}: {
|
|
52
|
+
usedAssets: FluidUsedAssets,
|
|
53
|
+
marketData: InnerFluidMarketData,
|
|
54
|
+
assetsData: FluidAssetsData
|
|
55
|
+
},
|
|
56
|
+
supplyShares?: string,
|
|
57
|
+
borrowShares?: string,
|
|
58
|
+
): FluidAggregatedVaultData => {
|
|
59
|
+
const payload = {} as FluidAggregatedVaultData;
|
|
60
|
+
|
|
61
|
+
payload.suppliedUsd = [FluidVaultType.T1, FluidVaultType.T3].includes(marketData.vaultType)
|
|
62
|
+
? getAssetsTotal(usedAssets, ({ isSupplied }: { isSupplied: boolean }) => isSupplied, ({ suppliedUsd }: { suppliedUsd: string }) => suppliedUsd)
|
|
63
|
+
: new Dec(marketData.collSharePrice!).mul(supplyShares!).toString();
|
|
64
|
+
payload.borrowedUsd = [FluidVaultType.T1, FluidVaultType.T2].includes(marketData.vaultType)
|
|
65
|
+
? getAssetsTotal(usedAssets, ({ isBorrowed }: { isBorrowed: boolean }) => isBorrowed, ({ borrowedUsd }: { borrowedUsd: string }) => borrowedUsd)
|
|
66
|
+
: new Dec(marketData.debtSharePrice!).mul(borrowShares!).toString();
|
|
67
|
+
|
|
68
|
+
const isDex = [FluidVaultType.T2, FluidVaultType.T3, FluidVaultType.T4].includes(marketData.vaultType);
|
|
69
|
+
const { netApy, incentiveUsd, totalInterestUsd } = isDex ? calculateNetApyDex({ marketData, suppliedUsd: payload.suppliedUsd, borrowedUsd: payload.borrowedUsd }) : calculateNetApy({ usedAssets, assetsData: assetsData as unknown as MMAssetsData });
|
|
70
|
+
payload.netApy = netApy;
|
|
71
|
+
payload.incentiveUsd = incentiveUsd;
|
|
72
|
+
payload.totalInterestUsd = totalInterestUsd;
|
|
73
|
+
const collFactor = marketData.collFactor;
|
|
74
|
+
const liqRatio = marketData.liquidationRatio;
|
|
75
|
+
|
|
76
|
+
payload.borrowLimitUsd = new Dec(payload.suppliedUsd).mul(collFactor).toString();
|
|
77
|
+
payload.liquidationLimitUsd = new Dec(payload.suppliedUsd).mul(liqRatio).div(100).toString();
|
|
78
|
+
const leftToBorrowUsd = new Dec(payload.borrowLimitUsd).sub(payload.borrowedUsd);
|
|
79
|
+
payload.leftToBorrowUsd = leftToBorrowUsd.lte('0') ? '0' : leftToBorrowUsd.toString();
|
|
80
|
+
payload.ratio = +payload.suppliedUsd ? new Dec(payload.borrowLimitUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
|
|
81
|
+
payload.collRatio = +payload.suppliedUsd ? new Dec(payload.suppliedUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
|
|
82
|
+
payload.minRatio = marketData.minRatio;
|
|
83
|
+
const { leveragedType, leveragedAsset } = isLeveragedPos(usedAssets);
|
|
84
|
+
|
|
85
|
+
payload.leveragedType = leveragedType;
|
|
86
|
+
if (leveragedType !== '') {
|
|
87
|
+
payload.leveragedAsset = leveragedAsset;
|
|
88
|
+
let assetPrice = assetsData[leveragedAsset].price;
|
|
89
|
+
if (leveragedType === LeverageType.VolatilePair) {
|
|
90
|
+
const borrowedAsset = (Object.values(usedAssets) as FluidUsedAsset[]).find(({ borrowedUsd }: { borrowedUsd: string }) => +borrowedUsd > 0);
|
|
91
|
+
const borrowedAssetPrice = assetsData[borrowedAsset!.symbol].price;
|
|
92
|
+
const leveragedAssetPrice = assetsData[leveragedAsset].price;
|
|
93
|
+
const isReverse = new Dec(leveragedAssetPrice).lt(borrowedAssetPrice);
|
|
94
|
+
if (isReverse) {
|
|
95
|
+
payload.leveragedType = LeverageType.VolatilePairReverse;
|
|
96
|
+
payload.currentVolatilePairRatio = new Dec(borrowedAssetPrice).div(leveragedAssetPrice).toDP(18).toString();
|
|
97
|
+
assetPrice = new Dec(borrowedAssetPrice).div(assetPrice).toString();
|
|
98
|
+
} else {
|
|
99
|
+
assetPrice = new Dec(assetPrice).div(borrowedAssetPrice).toString();
|
|
100
|
+
payload.currentVolatilePairRatio = new Dec(leveragedAssetPrice).div(borrowedAssetPrice).toDP(18).toString();
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
payload.liquidationPrice = calcLeverageLiqPrice(payload.leveragedType, assetPrice, payload.borrowedUsd, payload.liquidationLimitUsd);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
payload.minCollRatio = new Dec(payload.suppliedUsd).div(payload.borrowLimitUsd).mul(100).toString();
|
|
107
|
+
payload.collLiquidationRatio = new Dec(payload.suppliedUsd).div(payload.liquidationLimitUsd).mul(100).toString();
|
|
108
|
+
payload.exposure = getExposure(payload.borrowedUsd, payload.suppliedUsd);
|
|
109
|
+
|
|
110
|
+
return payload;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
interface DexSupplyData {
|
|
115
|
+
maxSupplyShares: string
|
|
116
|
+
supplyDexFee: string
|
|
117
|
+
token0PerSupplyShare: string
|
|
118
|
+
token1PerSupplyShare: string
|
|
119
|
+
withdrawable0: string
|
|
120
|
+
withdrawable1: string
|
|
121
|
+
withdrawableShares: string
|
|
122
|
+
utilizationSupply0: string
|
|
123
|
+
utilizationSupply1: string
|
|
124
|
+
supplyRate0: string
|
|
125
|
+
supplyRate1: string
|
|
126
|
+
totalSupplyShares: string
|
|
127
|
+
withdrawableToken0: string
|
|
128
|
+
withdrawableToken1: string
|
|
129
|
+
totalSupplyToken0: string
|
|
130
|
+
totalSupplyToken1: string
|
|
131
|
+
reservesSupplyToken0: string
|
|
132
|
+
reservesSupplyToken1: string
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export const parseDexSupplyData = (dexSupplyData: FluidDexSupplyDataStructOutput, collAsset0: string, collAsset1: string): DexSupplyData => {
|
|
136
|
+
const {
|
|
137
|
+
dexPool, // address of the dex pool
|
|
138
|
+
dexId, // id of the dex pool
|
|
139
|
+
fee: _fee, // fee of the dex pool (Only used as swap fees)
|
|
140
|
+
lastStoredPrice, // last stored price of the dex pool
|
|
141
|
+
centerPrice, // center price of the dex pool
|
|
142
|
+
token0Utilization, // token0 utilization
|
|
143
|
+
token1Utilization, // token1 utilization
|
|
144
|
+
// ONLY FOR SUPPLY
|
|
145
|
+
totalSupplyShares: totalSupplySharesWei, // total supply shares, in 1e18
|
|
146
|
+
maxSupplyShares: maxSupplySharesWei, // max supply shares, in 1e18
|
|
147
|
+
token0Supplied, // token0 supplied, in token0 decimals
|
|
148
|
+
token1Supplied, // token1 supplied, in token1 decimals
|
|
149
|
+
sharesWithdrawable, // shares withdrawable, in 1e18
|
|
150
|
+
token0Withdrawable, // token0 withdrawable, in token0 decimals
|
|
151
|
+
token1Withdrawable, // token1 withdrawable, in token1 decimals
|
|
152
|
+
token0PerSupplyShare: token0PerSupplyShareWei, // token0 amount per 1e18 supply shares
|
|
153
|
+
token1PerSupplyShare: token1PerSupplyShareWei, // token1 amount per 1e18 supply shares
|
|
154
|
+
token0SupplyRate, // token0 supply rate. E.g 320 = 3.2% APR
|
|
155
|
+
token1SupplyRate, // token1 supply rate. E.g 320 = 3.2% APR
|
|
156
|
+
supplyToken0Reserves, // token0 reserves in the dex pool
|
|
157
|
+
supplyToken1Reserves, // token1 reserves in the dex pool
|
|
158
|
+
} = dexSupplyData;
|
|
159
|
+
|
|
160
|
+
const maxSupplyShares = getEthAmountForDecimals(maxSupplySharesWei.toString(), 18);
|
|
161
|
+
const fee = new Dec(_fee).div(100).toString();
|
|
162
|
+
|
|
163
|
+
const token0PerSupplyShare = assetAmountInEth(token0PerSupplyShareWei.toString(), collAsset0);
|
|
164
|
+
const token1PerSupplyShare = assetAmountInEth(token1PerSupplyShareWei.toString(), collAsset1);
|
|
165
|
+
|
|
166
|
+
const withdrawable0 = assetAmountInEth(token0Withdrawable.toString(), collAsset0);
|
|
167
|
+
const withdrawable1 = assetAmountInEth(token1Withdrawable.toString(), collAsset1);
|
|
168
|
+
const utilizationSupply0 = assetAmountInEth(token0Utilization.toString(), collAsset0);
|
|
169
|
+
const utilizationSupply1 = assetAmountInEth(token1Utilization.toString(), collAsset1);
|
|
170
|
+
|
|
171
|
+
const supplyRate0 = new Dec(token0SupplyRate).div(100).toString();
|
|
172
|
+
const supplyRate1 = new Dec(token1SupplyRate).div(100).toString();
|
|
173
|
+
|
|
174
|
+
const totalSupplyShares = getEthAmountForDecimals(totalSupplySharesWei.toString(), 18); // in shares
|
|
175
|
+
|
|
176
|
+
const withdrawableShares = getEthAmountForDecimals(sharesWithdrawable.toString(), 18);
|
|
177
|
+
const withdrawableToken0 = new Dec(withdrawableShares).mul(token0PerSupplyShare).div(1e18).toString();
|
|
178
|
+
const withdrawableToken1 = new Dec(withdrawableShares).mul(token1PerSupplyShare).div(1e18).toString();
|
|
179
|
+
|
|
180
|
+
const totalSupplyToken0 = assetAmountInEth(token0Supplied.toString(), collAsset0);
|
|
181
|
+
const totalSupplyToken1 = assetAmountInEth(token1Supplied.toString(), collAsset1);
|
|
182
|
+
|
|
183
|
+
const reservesSupplyToken0 = assetAmountInEth(supplyToken0Reserves.toString(), collAsset0);
|
|
184
|
+
const reservesSupplyToken1 = assetAmountInEth(supplyToken1Reserves.toString(), collAsset1);
|
|
185
|
+
|
|
186
|
+
return {
|
|
187
|
+
maxSupplyShares,
|
|
188
|
+
withdrawableShares,
|
|
189
|
+
supplyDexFee: fee,
|
|
190
|
+
token0PerSupplyShare,
|
|
191
|
+
token1PerSupplyShare,
|
|
192
|
+
withdrawable0,
|
|
193
|
+
withdrawable1,
|
|
194
|
+
utilizationSupply0,
|
|
195
|
+
utilizationSupply1,
|
|
196
|
+
supplyRate0,
|
|
197
|
+
supplyRate1,
|
|
198
|
+
totalSupplyShares,
|
|
199
|
+
withdrawableToken0,
|
|
200
|
+
withdrawableToken1,
|
|
201
|
+
totalSupplyToken0,
|
|
202
|
+
totalSupplyToken1,
|
|
203
|
+
reservesSupplyToken0,
|
|
204
|
+
reservesSupplyToken1,
|
|
205
|
+
};
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
interface DexBorrowData {
|
|
209
|
+
maxBorrowShares: string
|
|
210
|
+
borrowDexFee: string
|
|
211
|
+
token0PerBorrowShare: string
|
|
212
|
+
token1PerBorrowShare: string
|
|
213
|
+
borrowable0: string
|
|
214
|
+
borrowable1: string
|
|
215
|
+
utilizationBorrow0: string
|
|
216
|
+
utilizationBorrow1: string
|
|
217
|
+
borrowRate0: string
|
|
218
|
+
borrowRate1: string
|
|
219
|
+
totalBorrowShares: string
|
|
220
|
+
borrowableToken0: string
|
|
221
|
+
borrowableToken1: string
|
|
222
|
+
totalBorrowToken0: string
|
|
223
|
+
totalBorrowToken1: string
|
|
224
|
+
borrowableShares: string
|
|
225
|
+
quoteTokensPerShare: string
|
|
226
|
+
reservesBorrowToken0: string
|
|
227
|
+
reservesBorrowToken1: string
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export const parseDexBorrowData = (dexBorrowData: FluidDexBorrowDataStructOutput, debtAsset0: string, debtAsset1: string): DexBorrowData => {
|
|
231
|
+
const {
|
|
232
|
+
dexPool,
|
|
233
|
+
dexId,
|
|
234
|
+
fee: _fee,
|
|
235
|
+
lastStoredPrice,
|
|
236
|
+
centerPrice,
|
|
237
|
+
token0Utilization,
|
|
238
|
+
token1Utilization,
|
|
239
|
+
totalBorrowShares: totalBorrowSharesWei,
|
|
240
|
+
maxBorrowShares: maxBorrowSharesWei,
|
|
241
|
+
token0Borrowed,
|
|
242
|
+
token1Borrowed,
|
|
243
|
+
sharesBorrowable,
|
|
244
|
+
token0Borrowable,
|
|
245
|
+
token1Borrowable,
|
|
246
|
+
token0PerBorrowShare: token0PerBorrowShareWei,
|
|
247
|
+
token1PerBorrowShare: token1PerBorrowShareWei,
|
|
248
|
+
token0BorrowRate,
|
|
249
|
+
token1BorrowRate,
|
|
250
|
+
quoteTokensPerShare,
|
|
251
|
+
borrowToken0Reserves,
|
|
252
|
+
borrowToken1Reserves,
|
|
253
|
+
} = dexBorrowData;
|
|
254
|
+
|
|
255
|
+
const maxBorrowShares = getEthAmountForDecimals(maxBorrowSharesWei.toString(), 18);
|
|
256
|
+
const fee = new Dec(_fee).div(100).toString();
|
|
257
|
+
|
|
258
|
+
const token0PerBorrowShare = assetAmountInEth(token0PerBorrowShareWei.toString(), debtAsset0);
|
|
259
|
+
const token1PerBorrowShare = assetAmountInEth(token1PerBorrowShareWei.toString(), debtAsset1);
|
|
260
|
+
|
|
261
|
+
const borrowable0 = assetAmountInEth(token0Borrowable.toString(), debtAsset0);
|
|
262
|
+
const borrowable1 = assetAmountInEth(token1Borrowable.toString(), debtAsset1);
|
|
263
|
+
const utilizationBorrow0 = assetAmountInEth(token0Utilization.toString(), debtAsset0);
|
|
264
|
+
const utilizationBorrow1 = assetAmountInEth(token1Utilization.toString(), debtAsset1);
|
|
265
|
+
|
|
266
|
+
const borrowRate0 = new Dec(token0BorrowRate).div(100).toString();
|
|
267
|
+
const borrowRate1 = new Dec(token1BorrowRate).div(100).toString();
|
|
268
|
+
|
|
269
|
+
const totalBorrowShares = getEthAmountForDecimals(totalBorrowSharesWei.toString(), 18); // in shares
|
|
270
|
+
|
|
271
|
+
const borrowableShares = getEthAmountForDecimals(sharesBorrowable.toString(), 18);
|
|
272
|
+
const borrowableToken0 = new Dec(borrowableShares).mul(token0PerBorrowShare).div(1e18).toString();
|
|
273
|
+
const borrowableToken1 = new Dec(borrowableShares).mul(token1PerBorrowShare).div(1e18).toString();
|
|
274
|
+
|
|
275
|
+
const totalBorrowToken0 = assetAmountInEth(token0Borrowed.toString(), debtAsset0);
|
|
276
|
+
const totalBorrowToken1 = assetAmountInEth(token1Borrowed.toString(), debtAsset1);
|
|
277
|
+
|
|
278
|
+
const reservesBorrowToken0 = assetAmountInEth(borrowToken0Reserves.toString(), debtAsset0);
|
|
279
|
+
const reservesBorrowToken1 = assetAmountInEth(borrowToken1Reserves.toString(), debtAsset1);
|
|
280
|
+
|
|
281
|
+
return {
|
|
282
|
+
borrowableShares,
|
|
283
|
+
maxBorrowShares,
|
|
284
|
+
borrowDexFee: fee,
|
|
285
|
+
token0PerBorrowShare,
|
|
286
|
+
token1PerBorrowShare,
|
|
287
|
+
borrowable0,
|
|
288
|
+
borrowable1,
|
|
289
|
+
utilizationBorrow0,
|
|
290
|
+
utilizationBorrow1,
|
|
291
|
+
borrowRate0,
|
|
292
|
+
borrowRate1,
|
|
293
|
+
totalBorrowShares,
|
|
294
|
+
borrowableToken0,
|
|
295
|
+
borrowableToken1,
|
|
296
|
+
totalBorrowToken0,
|
|
297
|
+
totalBorrowToken1,
|
|
298
|
+
quoteTokensPerShare: getEthAmountForDecimals(quoteTokensPerShare.toString(), 27),
|
|
299
|
+
reservesBorrowToken0,
|
|
300
|
+
reservesBorrowToken1,
|
|
301
|
+
};
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
const EMPTY_ASSET_DATA = {
|
|
305
|
+
symbol: '',
|
|
306
|
+
address: '',
|
|
307
|
+
price: '0',
|
|
308
|
+
totalSupply: '',
|
|
309
|
+
totalBorrow: '0',
|
|
310
|
+
canBeSupplied: false,
|
|
311
|
+
canBeBorrowed: false,
|
|
312
|
+
supplyRate: '0',
|
|
313
|
+
borrowRate: '0',
|
|
314
|
+
supplyIncentives: [],
|
|
315
|
+
borrowIncentives: [],
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
export const mergeAssetData = (existing: Partial<FluidAssetData> = {}, additional: Partial<FluidAssetData>): FluidAssetData => ({
|
|
319
|
+
...EMPTY_ASSET_DATA,
|
|
320
|
+
...existing,
|
|
321
|
+
...additional,
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
export const EMPTY_USED_ASSET = {
|
|
325
|
+
isSupplied: false,
|
|
326
|
+
isBorrowed: false,
|
|
327
|
+
supplied: '0',
|
|
328
|
+
suppliedUsd: '0',
|
|
329
|
+
borrowed: '0',
|
|
330
|
+
borrowedUsd: '0',
|
|
331
|
+
symbol: '',
|
|
332
|
+
collateral: false,
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
export const mergeUsedAssets = (existing: Partial<FluidUsedAsset> = {}, additional: Partial<FluidUsedAsset>): FluidUsedAsset => ({
|
|
336
|
+
...EMPTY_USED_ASSET,
|
|
337
|
+
...existing,
|
|
338
|
+
...additional,
|
|
336
339
|
});
|