@defisaver/positions-sdk 1.0.11-fluid-dev11 → 1.0.11-fluid-dev12
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/cjs/fluid/index.js +53 -28
- package/cjs/helpers/fluidHelpers/index.js +21 -2
- package/cjs/types/fluid.d.ts +4 -0
- package/esm/fluid/index.js +53 -28
- package/esm/helpers/fluidHelpers/index.js +21 -2
- package/esm/types/fluid.d.ts +4 -0
- package/package.json +1 -1
- package/src/fluid/index.ts +65 -37
- package/src/helpers/fluidHelpers/index.ts +33 -2
- package/src/types/fluid.ts +5 -0
package/cjs/fluid/index.js
CHANGED
|
@@ -120,6 +120,30 @@ const getTokenPriceFromChainlink = (asset, network, web3) => __awaiter(void 0, v
|
|
|
120
120
|
}
|
|
121
121
|
return new decimal_js_1.default(loanTokenPrice).div(1e8).toString();
|
|
122
122
|
});
|
|
123
|
+
const getMarketRateForDex = (token1PerShare, token0PerShare, rate0, rate1, price0, price1) => {
|
|
124
|
+
const token0PerShareUsd = new decimal_js_1.default(token0PerShare).mul(price0).toString();
|
|
125
|
+
const token1PerShareUsd = new decimal_js_1.default(token1PerShare).mul(price1).toString();
|
|
126
|
+
const sharesCombinedUsd = new decimal_js_1.default(token0PerShareUsd).plus(token1PerShareUsd);
|
|
127
|
+
const rate0PerShare = new decimal_js_1.default(rate0).mul(token0PerShareUsd).div(sharesCombinedUsd).toString();
|
|
128
|
+
const rate1PerShare = new decimal_js_1.default(rate1).mul(token1PerShareUsd).div(sharesCombinedUsd).toString();
|
|
129
|
+
return new decimal_js_1.default(rate0PerShare).plus(rate1PerShare).toString();
|
|
130
|
+
};
|
|
131
|
+
const getAdditionalMarketRateForDex = (token1PerShare, token0PerShare, incentiveSupplyRate0, incentiveSupplyRate1, price0, price1) => {
|
|
132
|
+
const token0PerShareUsd = new decimal_js_1.default(token0PerShare).mul(price0).toString();
|
|
133
|
+
const token1PerShareUsd = new decimal_js_1.default(token1PerShare).mul(price1).toString();
|
|
134
|
+
const sharesCombinedUsd = new decimal_js_1.default(token0PerShareUsd).plus(token1PerShareUsd);
|
|
135
|
+
const rate0PerShare = incentiveSupplyRate0 ? new decimal_js_1.default(incentiveSupplyRate0).mul(token0PerShareUsd).div(sharesCombinedUsd).toString() : 0;
|
|
136
|
+
const rate1PerShare = incentiveSupplyRate1 ? new decimal_js_1.default(incentiveSupplyRate1).mul(token1PerShareUsd).div(sharesCombinedUsd).toString() : 0;
|
|
137
|
+
return new decimal_js_1.default(rate0PerShare).plus(rate1PerShare).toString();
|
|
138
|
+
};
|
|
139
|
+
const getTradingApy = (poolAddress) => __awaiter(void 0, void 0, void 0, function* () {
|
|
140
|
+
const res = yield fetch(`https://api.fluid.instadapp.io/v2/1/dexes/${poolAddress}/apy`);
|
|
141
|
+
if (!res.ok) {
|
|
142
|
+
return '0';
|
|
143
|
+
}
|
|
144
|
+
const data = yield res.json();
|
|
145
|
+
return new decimal_js_1.default(data.tradingApy).div(100).toString();
|
|
146
|
+
});
|
|
123
147
|
const parseT1MarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0, void 0, void 0, function* () {
|
|
124
148
|
const collAsset = (0, tokens_1.getAssetInfoByAddress)(data.supplyToken0, network);
|
|
125
149
|
const debtAsset = (0, tokens_1.getAssetInfoByAddress)(data.borrowToken0, network);
|
|
@@ -144,6 +168,7 @@ const parseT1MarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0
|
|
|
144
168
|
collAssetData.incentiveSupplyApy = yield (0, staking_1.getStakingApy)(collAsset.symbol, mainnetWeb3);
|
|
145
169
|
collAssetData.incentiveSupplyToken = collAsset.symbol;
|
|
146
170
|
}
|
|
171
|
+
const incentiveSupplyRate = collAssetData.incentiveSupplyApy;
|
|
147
172
|
const debtAssetData = {
|
|
148
173
|
symbol: debtAsset.symbol,
|
|
149
174
|
address: debtAsset.address,
|
|
@@ -159,10 +184,7 @@ const parseT1MarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0
|
|
|
159
184
|
debtAssetData.incentiveBorrowApy = yield (0, staking_1.getStakingApy)(debtAsset.symbol, mainnetWeb3);
|
|
160
185
|
debtAssetData.incentiveBorrowToken = debtAsset.symbol;
|
|
161
186
|
}
|
|
162
|
-
|
|
163
|
-
debtAssetData.incentiveBorrowApy = yield (0, staking_1.getStakingApy)(debtAsset.symbol, mainnetWeb3);
|
|
164
|
-
debtAssetData.incentiveBorrowToken = debtAsset.symbol;
|
|
165
|
-
}
|
|
187
|
+
const incentiveBorrowRate = debtAssetData.incentiveBorrowApy;
|
|
166
188
|
const assetsData = {
|
|
167
189
|
[collAsset.symbol]: collAssetData,
|
|
168
190
|
[debtAsset.symbol]: debtAssetData,
|
|
@@ -206,6 +228,8 @@ const parseT1MarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0
|
|
|
206
228
|
liquidationMaxLimit,
|
|
207
229
|
borrowRate,
|
|
208
230
|
supplyRate,
|
|
231
|
+
incentiveSupplyRate,
|
|
232
|
+
incentiveBorrowRate,
|
|
209
233
|
oraclePrice,
|
|
210
234
|
};
|
|
211
235
|
return {
|
|
@@ -213,19 +237,6 @@ const parseT1MarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0
|
|
|
213
237
|
marketData,
|
|
214
238
|
};
|
|
215
239
|
});
|
|
216
|
-
const getMarketRateForDex = (token1PerShare, token0PerShare, rate0, rate1) => {
|
|
217
|
-
const sharesCombined = new decimal_js_1.default(token1PerShare).plus(token0PerShare);
|
|
218
|
-
const rate0PerShare = new decimal_js_1.default(rate0).mul(token0PerShare).div(sharesCombined).toString();
|
|
219
|
-
const rate1PerShare = new decimal_js_1.default(rate1).mul(token1PerShare).div(sharesCombined).toString();
|
|
220
|
-
return new decimal_js_1.default(rate0PerShare).plus(rate1PerShare).toString();
|
|
221
|
-
};
|
|
222
|
-
const getAdditionalMarketRateForDex = (token1PerShare, token0PerShare, incentiveSupplyRate0, incentiveSupplyRate1) => {
|
|
223
|
-
console.log(incentiveSupplyRate0, incentiveSupplyRate1);
|
|
224
|
-
const sharesCombined = new decimal_js_1.default(token1PerShare).plus(token0PerShare);
|
|
225
|
-
const rate0PerShare = incentiveSupplyRate0 ? new decimal_js_1.default(incentiveSupplyRate0).mul(token0PerShare).div(sharesCombined).toString() : 0;
|
|
226
|
-
const rate1PerShare = incentiveSupplyRate1 ? new decimal_js_1.default(incentiveSupplyRate1).mul(token1PerShare).div(sharesCombined).toString() : 0;
|
|
227
|
-
return new decimal_js_1.default(rate0PerShare).plus(rate1PerShare).toString();
|
|
228
|
-
};
|
|
229
240
|
const parseT2MarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0, void 0, void 0, function* () {
|
|
230
241
|
const collAsset0 = (0, tokens_1.getAssetInfoByAddress)(data.supplyToken0, network);
|
|
231
242
|
const collAsset1 = (0, tokens_1.getAssetInfoByAddress)(data.supplyToken1, network);
|
|
@@ -268,8 +279,9 @@ const parseT2MarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0
|
|
|
268
279
|
collSecondAssetData.incentiveSupplyApy = yield (0, staking_1.getStakingApy)(collAsset1.symbol, mainnetWeb3);
|
|
269
280
|
collSecondAssetData.incentiveSupplyToken = collAsset1.symbol;
|
|
270
281
|
}
|
|
271
|
-
const marketSupplyRate = getMarketRateForDex(token1PerSupplyShare, token0PerSupplyShare, supplyRate0, supplyRate1);
|
|
272
|
-
const incentiveSupplyRate = getAdditionalMarketRateForDex(token1PerSupplyShare, token0PerSupplyShare, collFirstAssetData.incentiveSupplyApy, collSecondAssetData.incentiveSupplyApy);
|
|
282
|
+
const marketSupplyRate = getMarketRateForDex(token1PerSupplyShare, token0PerSupplyShare, supplyRate0, supplyRate1, collFirstAssetData.price, collSecondAssetData.price);
|
|
283
|
+
const incentiveSupplyRate = getAdditionalMarketRateForDex(token1PerSupplyShare, token0PerSupplyShare, collFirstAssetData.incentiveSupplyApy, collSecondAssetData.incentiveSupplyApy, collFirstAssetData.price, collSecondAssetData.price);
|
|
284
|
+
const tradingSupplyRate = yield getTradingApy(data.dexSupplyData.dexPool);
|
|
273
285
|
const borrowRate = new decimal_js_1.default(data.borrowRateVault).div(100).toString();
|
|
274
286
|
const debtAssetData = {
|
|
275
287
|
symbol: debtAsset.symbol,
|
|
@@ -283,6 +295,7 @@ const parseT2MarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0
|
|
|
283
295
|
debtAssetData.incentiveBorrowApy = yield (0, staking_1.getStakingApy)(debtAsset.symbol, mainnetWeb3);
|
|
284
296
|
debtAssetData.incentiveBorrowToken = debtAsset.symbol;
|
|
285
297
|
}
|
|
298
|
+
const incentiveBorrowRate = debtAssetData.incentiveBorrowApy;
|
|
286
299
|
const assetsData = [
|
|
287
300
|
[collAsset0.symbol, collFirstAssetData],
|
|
288
301
|
[collAsset1.symbol, collSecondAssetData],
|
|
@@ -332,17 +345,20 @@ const parseT2MarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0
|
|
|
332
345
|
borrowRate,
|
|
333
346
|
supplyRate: marketSupplyRate,
|
|
334
347
|
incentiveSupplyRate,
|
|
348
|
+
incentiveBorrowRate,
|
|
335
349
|
totalSupplyToken0,
|
|
336
350
|
totalSupplyToken1,
|
|
337
351
|
withdrawableToken0,
|
|
338
352
|
withdrawableToken1,
|
|
339
353
|
withdrawableUSD,
|
|
340
354
|
withdrawable: withdrawableShares,
|
|
341
|
-
|
|
355
|
+
withdrawableDex: new decimal_js_1.default(maxSupplyShares).minus(totalSupplyShares).toString(),
|
|
342
356
|
maxSupplyShares,
|
|
343
357
|
maxSupplySharesUsd,
|
|
344
358
|
collDexFee: supplyDexFee,
|
|
345
359
|
oraclePrice,
|
|
360
|
+
tradingSupplyRate,
|
|
361
|
+
tradingBorrowRate: '0',
|
|
346
362
|
};
|
|
347
363
|
return {
|
|
348
364
|
assetsData,
|
|
@@ -372,6 +388,7 @@ const parseT3MarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0
|
|
|
372
388
|
collAssetData.incentiveSupplyApy = yield (0, staking_1.getStakingApy)(collAsset.symbol, mainnetWeb3);
|
|
373
389
|
collAssetData.incentiveSupplyToken = collAsset.symbol;
|
|
374
390
|
}
|
|
391
|
+
const incentiveSupplyRate = collAssetData.incentiveSupplyApy;
|
|
375
392
|
const debtAsset0Data = {
|
|
376
393
|
symbol: debtAsset0.symbol,
|
|
377
394
|
address: debtAsset0.address,
|
|
@@ -404,8 +421,9 @@ const parseT3MarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0
|
|
|
404
421
|
debtAsset1Data.incentiveSupplyApy = yield (0, staking_1.getStakingApy)(debtAsset1.symbol, mainnetWeb3);
|
|
405
422
|
debtAsset1Data.incentiveSupplyToken = debtAsset1.symbol;
|
|
406
423
|
}
|
|
407
|
-
const marketBorrowRate = getMarketRateForDex(token1PerBorrowShare, token0PerBorrowShare, borrowRate0, borrowRate1);
|
|
408
|
-
const incentiveBorrowRate = getAdditionalMarketRateForDex(token1PerBorrowShare, token0PerBorrowShare, debtAsset0Data.incentiveSupplyApy, debtAsset1Data.incentiveSupplyApy);
|
|
424
|
+
const marketBorrowRate = getMarketRateForDex(token1PerBorrowShare, token0PerBorrowShare, borrowRate0, borrowRate1, debtAsset0Data.price, debtAsset1Data.price);
|
|
425
|
+
const incentiveBorrowRate = getAdditionalMarketRateForDex(token1PerBorrowShare, token0PerBorrowShare, debtAsset0Data.incentiveSupplyApy, debtAsset1Data.incentiveSupplyApy, debtAsset0Data.price, debtAsset1Data.price);
|
|
426
|
+
const tradingBorrowRate = yield getTradingApy(data.dexBorrowData.dexPool);
|
|
409
427
|
const assetsData = [
|
|
410
428
|
[collAsset.symbol, collAssetData],
|
|
411
429
|
[debtAsset0.symbol, debtAsset0Data],
|
|
@@ -450,6 +468,9 @@ const parseT3MarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0
|
|
|
450
468
|
borrowRate: marketBorrowRate,
|
|
451
469
|
supplyRate,
|
|
452
470
|
incentiveBorrowRate,
|
|
471
|
+
incentiveSupplyRate,
|
|
472
|
+
tradingBorrowRate,
|
|
473
|
+
tradingSupplyRate: '0',
|
|
453
474
|
borrowableToken0,
|
|
454
475
|
borrowableToken1,
|
|
455
476
|
totalBorrowToken0,
|
|
@@ -545,10 +566,13 @@ const parseT4MarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0
|
|
|
545
566
|
debtAsset1Data.incentiveSupplyApy = yield (0, staking_1.getStakingApy)(debtAsset1.symbol, mainnetWeb3);
|
|
546
567
|
debtAsset1Data.incentiveSupplyToken = debtAsset1.symbol;
|
|
547
568
|
}
|
|
548
|
-
const
|
|
549
|
-
const
|
|
550
|
-
const
|
|
551
|
-
const
|
|
569
|
+
const marketInfo = (0, markets_1.getFluidMarketInfoById)(+data.vaultId, network);
|
|
570
|
+
const marketBorrowRate = getMarketRateForDex(token1PerBorrowShare, token0PerBorrowShare, borrowRate0, borrowRate1, debtAsset0Data.price, debtAsset1Data.price);
|
|
571
|
+
const incentiveBorrowRate = getAdditionalMarketRateForDex(token1PerBorrowShare, token0PerBorrowShare, debtAsset0Data.incentiveSupplyApy, debtAsset1Data.incentiveSupplyApy, debtAsset0Data.price, debtAsset1Data.price);
|
|
572
|
+
const tradingBorrowRate = yield getTradingApy(data.dexBorrowData.dexPool);
|
|
573
|
+
const marketSupplyRate = getMarketRateForDex(token1PerSupplyShare, token0PerSupplyShare, supplyRate0, supplyRate1, collAsset0Data.price, collAsset1Data.price);
|
|
574
|
+
const incentiveSupplyRate = getAdditionalMarketRateForDex(token1PerSupplyShare, token0PerSupplyShare, collAsset0Data.incentiveSupplyApy, collAsset1Data.incentiveSupplyApy, collAsset0Data.price, collAsset1Data.price);
|
|
575
|
+
const tradingSupplyRate = yield getTradingApy(data.dexSupplyData.dexPool);
|
|
552
576
|
const assetsData = [
|
|
553
577
|
[collAsset0.symbol, collAsset0Data],
|
|
554
578
|
[collAsset1.symbol, collAsset1Data],
|
|
@@ -556,7 +580,6 @@ const parseT4MarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0
|
|
|
556
580
|
[debtAsset1.symbol, debtAsset1Data],
|
|
557
581
|
]
|
|
558
582
|
.reduce((acc, [symbol, partialData]) => (Object.assign(Object.assign({}, acc), { [symbol]: (0, fluidHelpers_1.mergeAssetData)(acc[symbol], partialData) })), {});
|
|
559
|
-
const marketInfo = (0, markets_1.getFluidMarketInfoById)(+data.vaultId, network);
|
|
560
583
|
const liqRatio = new decimal_js_1.default(data.liquidationThreshold).div(100).toString();
|
|
561
584
|
const liquidationMaxLimit = new decimal_js_1.default(data.liquidationMaxLimit).div(100).toString();
|
|
562
585
|
const liqFactor = new decimal_js_1.default(data.liquidationThreshold).div(10000).toString();
|
|
@@ -613,13 +636,15 @@ const parseT4MarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0
|
|
|
613
636
|
withdrawableToken1,
|
|
614
637
|
withdrawableUSD,
|
|
615
638
|
withdrawable: withdrawableShares,
|
|
616
|
-
|
|
639
|
+
withdrawableDex: new decimal_js_1.default(maxSupplyShares).minus(totalSupplyShares).toString(),
|
|
617
640
|
maxSupplyShares,
|
|
618
641
|
maxSupplySharesUsd,
|
|
619
642
|
collDexFee: supplyDexFee,
|
|
620
643
|
collSharePrice,
|
|
621
644
|
debtSharePrice,
|
|
622
645
|
oraclePrice,
|
|
646
|
+
tradingBorrowRate,
|
|
647
|
+
tradingSupplyRate,
|
|
623
648
|
};
|
|
624
649
|
return {
|
|
625
650
|
assetsData,
|
|
@@ -10,6 +10,24 @@ const types_1 = require("../../types");
|
|
|
10
10
|
const moneymarket_1 = require("../../moneymarket");
|
|
11
11
|
const staking_1 = require("../../staking");
|
|
12
12
|
const utils_1 = require("../../services/utils");
|
|
13
|
+
const calculateNetApyDex = ({ marketData, suppliedUsd, borrowedUsd }) => {
|
|
14
|
+
const { borrowRate, supplyRate, incentiveBorrowRate, incentiveSupplyRate, tradingBorrowRate, tradingSupplyRate, } = marketData;
|
|
15
|
+
const totalBorrowRate = new decimal_js_1.default(borrowRate).minus(tradingBorrowRate || '0').toString();
|
|
16
|
+
const totalSupplyRate = new decimal_js_1.default(supplyRate).add(tradingSupplyRate || '0').toString();
|
|
17
|
+
const borrowIncentive = new decimal_js_1.default(incentiveBorrowRate || '0').mul(borrowedUsd).div(100).toString();
|
|
18
|
+
const supplyIncentive = new decimal_js_1.default(incentiveSupplyRate || '0').mul(suppliedUsd).div(100).toString();
|
|
19
|
+
const incentiveUsd = new decimal_js_1.default(supplyIncentive).minus(borrowIncentive).toString();
|
|
20
|
+
const borrowInterest = new decimal_js_1.default(totalBorrowRate).mul(borrowedUsd).div(100).toString();
|
|
21
|
+
const supplyInterest = new decimal_js_1.default(totalSupplyRate).mul(suppliedUsd).div(100).toString();
|
|
22
|
+
const totalInterestUsd = new decimal_js_1.default(supplyInterest).add(incentiveUsd).minus(borrowInterest).toString();
|
|
23
|
+
const balance = new decimal_js_1.default(suppliedUsd).sub(borrowedUsd).toString();
|
|
24
|
+
const netApy = new decimal_js_1.default(totalInterestUsd).div(balance).times(100).toString();
|
|
25
|
+
return {
|
|
26
|
+
netApy,
|
|
27
|
+
incentiveUsd,
|
|
28
|
+
totalInterestUsd,
|
|
29
|
+
};
|
|
30
|
+
};
|
|
13
31
|
const getFluidAggregatedData = ({ usedAssets, assetsData, marketData, }, supplyShares, borrowShares) => {
|
|
14
32
|
const payload = {};
|
|
15
33
|
payload.suppliedUsd = [types_1.FluidVaultType.T1, types_1.FluidVaultType.T3].includes(marketData.vaultType)
|
|
@@ -18,7 +36,8 @@ const getFluidAggregatedData = ({ usedAssets, assetsData, marketData, }, supplyS
|
|
|
18
36
|
payload.borrowedUsd = [types_1.FluidVaultType.T1, types_1.FluidVaultType.T2].includes(marketData.vaultType)
|
|
19
37
|
? (0, moneymarket_1.getAssetsTotal)(usedAssets, ({ isBorrowed }) => isBorrowed, ({ borrowedUsd }) => borrowedUsd)
|
|
20
38
|
: new decimal_js_1.default(marketData.debtSharePrice).mul(borrowShares).toString();
|
|
21
|
-
const
|
|
39
|
+
const isDex = [types_1.FluidVaultType.T2, types_1.FluidVaultType.T3, types_1.FluidVaultType.T4].includes(marketData.vaultType);
|
|
40
|
+
const { netApy, incentiveUsd, totalInterestUsd } = isDex ? calculateNetApyDex({ marketData, suppliedUsd: payload.suppliedUsd, borrowedUsd: payload.borrowedUsd }) : (0, staking_1.calculateNetApy)({ usedAssets, assetsData: assetsData });
|
|
22
41
|
payload.netApy = netApy;
|
|
23
42
|
payload.incentiveUsd = incentiveUsd;
|
|
24
43
|
payload.totalInterestUsd = totalInterestUsd;
|
|
@@ -51,7 +70,7 @@ exports.getFluidAggregatedData = getFluidAggregatedData;
|
|
|
51
70
|
const parseDexSupplyData = (dexSupplyData, collAsset0, collAsset1) => {
|
|
52
71
|
const { dexPool, // address of the dex pool
|
|
53
72
|
dexId, // id of the dex pool
|
|
54
|
-
fee: _fee, // fee of the dex pool
|
|
73
|
+
fee: _fee, // fee of the dex pool (Only used as swap fees)
|
|
55
74
|
lastStoredPrice, // last stored price of the dex pool
|
|
56
75
|
centerPrice, // center price of the dex pool
|
|
57
76
|
token0Utilization, // token0 utilization
|
package/cjs/types/fluid.d.ts
CHANGED
|
@@ -241,6 +241,8 @@ export interface InnerFluidMarketData {
|
|
|
241
241
|
borrowRate: string;
|
|
242
242
|
liquidationMaxLimit: string;
|
|
243
243
|
oraclePrice: string;
|
|
244
|
+
incentiveBorrowRate?: string;
|
|
245
|
+
incentiveSupplyRate?: string;
|
|
244
246
|
collSharePrice?: string;
|
|
245
247
|
maxSupplyShares?: string;
|
|
246
248
|
maxSupplySharesUsd?: string;
|
|
@@ -259,6 +261,8 @@ export interface InnerFluidMarketData {
|
|
|
259
261
|
totalBorrowToken0?: string;
|
|
260
262
|
totalBorrowToken1?: string;
|
|
261
263
|
borrowDexFee?: string;
|
|
264
|
+
tradingBorrowRate?: string;
|
|
265
|
+
tradingSupplyRate?: string;
|
|
262
266
|
}
|
|
263
267
|
export interface FluidMarketData {
|
|
264
268
|
assetsData: FluidAssetsData;
|
package/esm/fluid/index.js
CHANGED
|
@@ -114,6 +114,30 @@ const getTokenPriceFromChainlink = (asset, network, web3) => __awaiter(void 0, v
|
|
|
114
114
|
}
|
|
115
115
|
return new Dec(loanTokenPrice).div(1e8).toString();
|
|
116
116
|
});
|
|
117
|
+
const getMarketRateForDex = (token1PerShare, token0PerShare, rate0, rate1, price0, price1) => {
|
|
118
|
+
const token0PerShareUsd = new Dec(token0PerShare).mul(price0).toString();
|
|
119
|
+
const token1PerShareUsd = new Dec(token1PerShare).mul(price1).toString();
|
|
120
|
+
const sharesCombinedUsd = new Dec(token0PerShareUsd).plus(token1PerShareUsd);
|
|
121
|
+
const rate0PerShare = new Dec(rate0).mul(token0PerShareUsd).div(sharesCombinedUsd).toString();
|
|
122
|
+
const rate1PerShare = new Dec(rate1).mul(token1PerShareUsd).div(sharesCombinedUsd).toString();
|
|
123
|
+
return new Dec(rate0PerShare).plus(rate1PerShare).toString();
|
|
124
|
+
};
|
|
125
|
+
const getAdditionalMarketRateForDex = (token1PerShare, token0PerShare, incentiveSupplyRate0, incentiveSupplyRate1, price0, price1) => {
|
|
126
|
+
const token0PerShareUsd = new Dec(token0PerShare).mul(price0).toString();
|
|
127
|
+
const token1PerShareUsd = new Dec(token1PerShare).mul(price1).toString();
|
|
128
|
+
const sharesCombinedUsd = new Dec(token0PerShareUsd).plus(token1PerShareUsd);
|
|
129
|
+
const rate0PerShare = incentiveSupplyRate0 ? new Dec(incentiveSupplyRate0).mul(token0PerShareUsd).div(sharesCombinedUsd).toString() : 0;
|
|
130
|
+
const rate1PerShare = incentiveSupplyRate1 ? new Dec(incentiveSupplyRate1).mul(token1PerShareUsd).div(sharesCombinedUsd).toString() : 0;
|
|
131
|
+
return new Dec(rate0PerShare).plus(rate1PerShare).toString();
|
|
132
|
+
};
|
|
133
|
+
const getTradingApy = (poolAddress) => __awaiter(void 0, void 0, void 0, function* () {
|
|
134
|
+
const res = yield fetch(`https://api.fluid.instadapp.io/v2/1/dexes/${poolAddress}/apy`);
|
|
135
|
+
if (!res.ok) {
|
|
136
|
+
return '0';
|
|
137
|
+
}
|
|
138
|
+
const data = yield res.json();
|
|
139
|
+
return new Dec(data.tradingApy).div(100).toString();
|
|
140
|
+
});
|
|
117
141
|
const parseT1MarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0, void 0, void 0, function* () {
|
|
118
142
|
const collAsset = getAssetInfoByAddress(data.supplyToken0, network);
|
|
119
143
|
const debtAsset = getAssetInfoByAddress(data.borrowToken0, network);
|
|
@@ -138,6 +162,7 @@ const parseT1MarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0
|
|
|
138
162
|
collAssetData.incentiveSupplyApy = yield getStakingApy(collAsset.symbol, mainnetWeb3);
|
|
139
163
|
collAssetData.incentiveSupplyToken = collAsset.symbol;
|
|
140
164
|
}
|
|
165
|
+
const incentiveSupplyRate = collAssetData.incentiveSupplyApy;
|
|
141
166
|
const debtAssetData = {
|
|
142
167
|
symbol: debtAsset.symbol,
|
|
143
168
|
address: debtAsset.address,
|
|
@@ -153,10 +178,7 @@ const parseT1MarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0
|
|
|
153
178
|
debtAssetData.incentiveBorrowApy = yield getStakingApy(debtAsset.symbol, mainnetWeb3);
|
|
154
179
|
debtAssetData.incentiveBorrowToken = debtAsset.symbol;
|
|
155
180
|
}
|
|
156
|
-
|
|
157
|
-
debtAssetData.incentiveBorrowApy = yield getStakingApy(debtAsset.symbol, mainnetWeb3);
|
|
158
|
-
debtAssetData.incentiveBorrowToken = debtAsset.symbol;
|
|
159
|
-
}
|
|
181
|
+
const incentiveBorrowRate = debtAssetData.incentiveBorrowApy;
|
|
160
182
|
const assetsData = {
|
|
161
183
|
[collAsset.symbol]: collAssetData,
|
|
162
184
|
[debtAsset.symbol]: debtAssetData,
|
|
@@ -200,6 +222,8 @@ const parseT1MarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0
|
|
|
200
222
|
liquidationMaxLimit,
|
|
201
223
|
borrowRate,
|
|
202
224
|
supplyRate,
|
|
225
|
+
incentiveSupplyRate,
|
|
226
|
+
incentiveBorrowRate,
|
|
203
227
|
oraclePrice,
|
|
204
228
|
};
|
|
205
229
|
return {
|
|
@@ -207,19 +231,6 @@ const parseT1MarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0
|
|
|
207
231
|
marketData,
|
|
208
232
|
};
|
|
209
233
|
});
|
|
210
|
-
const getMarketRateForDex = (token1PerShare, token0PerShare, rate0, rate1) => {
|
|
211
|
-
const sharesCombined = new Dec(token1PerShare).plus(token0PerShare);
|
|
212
|
-
const rate0PerShare = new Dec(rate0).mul(token0PerShare).div(sharesCombined).toString();
|
|
213
|
-
const rate1PerShare = new Dec(rate1).mul(token1PerShare).div(sharesCombined).toString();
|
|
214
|
-
return new Dec(rate0PerShare).plus(rate1PerShare).toString();
|
|
215
|
-
};
|
|
216
|
-
const getAdditionalMarketRateForDex = (token1PerShare, token0PerShare, incentiveSupplyRate0, incentiveSupplyRate1) => {
|
|
217
|
-
console.log(incentiveSupplyRate0, incentiveSupplyRate1);
|
|
218
|
-
const sharesCombined = new Dec(token1PerShare).plus(token0PerShare);
|
|
219
|
-
const rate0PerShare = incentiveSupplyRate0 ? new Dec(incentiveSupplyRate0).mul(token0PerShare).div(sharesCombined).toString() : 0;
|
|
220
|
-
const rate1PerShare = incentiveSupplyRate1 ? new Dec(incentiveSupplyRate1).mul(token1PerShare).div(sharesCombined).toString() : 0;
|
|
221
|
-
return new Dec(rate0PerShare).plus(rate1PerShare).toString();
|
|
222
|
-
};
|
|
223
234
|
const parseT2MarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0, void 0, void 0, function* () {
|
|
224
235
|
const collAsset0 = getAssetInfoByAddress(data.supplyToken0, network);
|
|
225
236
|
const collAsset1 = getAssetInfoByAddress(data.supplyToken1, network);
|
|
@@ -262,8 +273,9 @@ const parseT2MarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0
|
|
|
262
273
|
collSecondAssetData.incentiveSupplyApy = yield getStakingApy(collAsset1.symbol, mainnetWeb3);
|
|
263
274
|
collSecondAssetData.incentiveSupplyToken = collAsset1.symbol;
|
|
264
275
|
}
|
|
265
|
-
const marketSupplyRate = getMarketRateForDex(token1PerSupplyShare, token0PerSupplyShare, supplyRate0, supplyRate1);
|
|
266
|
-
const incentiveSupplyRate = getAdditionalMarketRateForDex(token1PerSupplyShare, token0PerSupplyShare, collFirstAssetData.incentiveSupplyApy, collSecondAssetData.incentiveSupplyApy);
|
|
276
|
+
const marketSupplyRate = getMarketRateForDex(token1PerSupplyShare, token0PerSupplyShare, supplyRate0, supplyRate1, collFirstAssetData.price, collSecondAssetData.price);
|
|
277
|
+
const incentiveSupplyRate = getAdditionalMarketRateForDex(token1PerSupplyShare, token0PerSupplyShare, collFirstAssetData.incentiveSupplyApy, collSecondAssetData.incentiveSupplyApy, collFirstAssetData.price, collSecondAssetData.price);
|
|
278
|
+
const tradingSupplyRate = yield getTradingApy(data.dexSupplyData.dexPool);
|
|
267
279
|
const borrowRate = new Dec(data.borrowRateVault).div(100).toString();
|
|
268
280
|
const debtAssetData = {
|
|
269
281
|
symbol: debtAsset.symbol,
|
|
@@ -277,6 +289,7 @@ const parseT2MarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0
|
|
|
277
289
|
debtAssetData.incentiveBorrowApy = yield getStakingApy(debtAsset.symbol, mainnetWeb3);
|
|
278
290
|
debtAssetData.incentiveBorrowToken = debtAsset.symbol;
|
|
279
291
|
}
|
|
292
|
+
const incentiveBorrowRate = debtAssetData.incentiveBorrowApy;
|
|
280
293
|
const assetsData = [
|
|
281
294
|
[collAsset0.symbol, collFirstAssetData],
|
|
282
295
|
[collAsset1.symbol, collSecondAssetData],
|
|
@@ -326,17 +339,20 @@ const parseT2MarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0
|
|
|
326
339
|
borrowRate,
|
|
327
340
|
supplyRate: marketSupplyRate,
|
|
328
341
|
incentiveSupplyRate,
|
|
342
|
+
incentiveBorrowRate,
|
|
329
343
|
totalSupplyToken0,
|
|
330
344
|
totalSupplyToken1,
|
|
331
345
|
withdrawableToken0,
|
|
332
346
|
withdrawableToken1,
|
|
333
347
|
withdrawableUSD,
|
|
334
348
|
withdrawable: withdrawableShares,
|
|
335
|
-
|
|
349
|
+
withdrawableDex: new Dec(maxSupplyShares).minus(totalSupplyShares).toString(),
|
|
336
350
|
maxSupplyShares,
|
|
337
351
|
maxSupplySharesUsd,
|
|
338
352
|
collDexFee: supplyDexFee,
|
|
339
353
|
oraclePrice,
|
|
354
|
+
tradingSupplyRate,
|
|
355
|
+
tradingBorrowRate: '0',
|
|
340
356
|
};
|
|
341
357
|
return {
|
|
342
358
|
assetsData,
|
|
@@ -366,6 +382,7 @@ const parseT3MarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0
|
|
|
366
382
|
collAssetData.incentiveSupplyApy = yield getStakingApy(collAsset.symbol, mainnetWeb3);
|
|
367
383
|
collAssetData.incentiveSupplyToken = collAsset.symbol;
|
|
368
384
|
}
|
|
385
|
+
const incentiveSupplyRate = collAssetData.incentiveSupplyApy;
|
|
369
386
|
const debtAsset0Data = {
|
|
370
387
|
symbol: debtAsset0.symbol,
|
|
371
388
|
address: debtAsset0.address,
|
|
@@ -398,8 +415,9 @@ const parseT3MarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0
|
|
|
398
415
|
debtAsset1Data.incentiveSupplyApy = yield getStakingApy(debtAsset1.symbol, mainnetWeb3);
|
|
399
416
|
debtAsset1Data.incentiveSupplyToken = debtAsset1.symbol;
|
|
400
417
|
}
|
|
401
|
-
const marketBorrowRate = getMarketRateForDex(token1PerBorrowShare, token0PerBorrowShare, borrowRate0, borrowRate1);
|
|
402
|
-
const incentiveBorrowRate = getAdditionalMarketRateForDex(token1PerBorrowShare, token0PerBorrowShare, debtAsset0Data.incentiveSupplyApy, debtAsset1Data.incentiveSupplyApy);
|
|
418
|
+
const marketBorrowRate = getMarketRateForDex(token1PerBorrowShare, token0PerBorrowShare, borrowRate0, borrowRate1, debtAsset0Data.price, debtAsset1Data.price);
|
|
419
|
+
const incentiveBorrowRate = getAdditionalMarketRateForDex(token1PerBorrowShare, token0PerBorrowShare, debtAsset0Data.incentiveSupplyApy, debtAsset1Data.incentiveSupplyApy, debtAsset0Data.price, debtAsset1Data.price);
|
|
420
|
+
const tradingBorrowRate = yield getTradingApy(data.dexBorrowData.dexPool);
|
|
403
421
|
const assetsData = [
|
|
404
422
|
[collAsset.symbol, collAssetData],
|
|
405
423
|
[debtAsset0.symbol, debtAsset0Data],
|
|
@@ -444,6 +462,9 @@ const parseT3MarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0
|
|
|
444
462
|
borrowRate: marketBorrowRate,
|
|
445
463
|
supplyRate,
|
|
446
464
|
incentiveBorrowRate,
|
|
465
|
+
incentiveSupplyRate,
|
|
466
|
+
tradingBorrowRate,
|
|
467
|
+
tradingSupplyRate: '0',
|
|
447
468
|
borrowableToken0,
|
|
448
469
|
borrowableToken1,
|
|
449
470
|
totalBorrowToken0,
|
|
@@ -539,10 +560,13 @@ const parseT4MarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0
|
|
|
539
560
|
debtAsset1Data.incentiveSupplyApy = yield getStakingApy(debtAsset1.symbol, mainnetWeb3);
|
|
540
561
|
debtAsset1Data.incentiveSupplyToken = debtAsset1.symbol;
|
|
541
562
|
}
|
|
542
|
-
const
|
|
543
|
-
const
|
|
544
|
-
const
|
|
545
|
-
const
|
|
563
|
+
const marketInfo = getFluidMarketInfoById(+data.vaultId, network);
|
|
564
|
+
const marketBorrowRate = getMarketRateForDex(token1PerBorrowShare, token0PerBorrowShare, borrowRate0, borrowRate1, debtAsset0Data.price, debtAsset1Data.price);
|
|
565
|
+
const incentiveBorrowRate = getAdditionalMarketRateForDex(token1PerBorrowShare, token0PerBorrowShare, debtAsset0Data.incentiveSupplyApy, debtAsset1Data.incentiveSupplyApy, debtAsset0Data.price, debtAsset1Data.price);
|
|
566
|
+
const tradingBorrowRate = yield getTradingApy(data.dexBorrowData.dexPool);
|
|
567
|
+
const marketSupplyRate = getMarketRateForDex(token1PerSupplyShare, token0PerSupplyShare, supplyRate0, supplyRate1, collAsset0Data.price, collAsset1Data.price);
|
|
568
|
+
const incentiveSupplyRate = getAdditionalMarketRateForDex(token1PerSupplyShare, token0PerSupplyShare, collAsset0Data.incentiveSupplyApy, collAsset1Data.incentiveSupplyApy, collAsset0Data.price, collAsset1Data.price);
|
|
569
|
+
const tradingSupplyRate = yield getTradingApy(data.dexSupplyData.dexPool);
|
|
546
570
|
const assetsData = [
|
|
547
571
|
[collAsset0.symbol, collAsset0Data],
|
|
548
572
|
[collAsset1.symbol, collAsset1Data],
|
|
@@ -550,7 +574,6 @@ const parseT4MarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0
|
|
|
550
574
|
[debtAsset1.symbol, debtAsset1Data],
|
|
551
575
|
]
|
|
552
576
|
.reduce((acc, [symbol, partialData]) => (Object.assign(Object.assign({}, acc), { [symbol]: mergeAssetData(acc[symbol], partialData) })), {});
|
|
553
|
-
const marketInfo = getFluidMarketInfoById(+data.vaultId, network);
|
|
554
577
|
const liqRatio = new Dec(data.liquidationThreshold).div(100).toString();
|
|
555
578
|
const liquidationMaxLimit = new Dec(data.liquidationMaxLimit).div(100).toString();
|
|
556
579
|
const liqFactor = new Dec(data.liquidationThreshold).div(10000).toString();
|
|
@@ -607,13 +630,15 @@ const parseT4MarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0
|
|
|
607
630
|
withdrawableToken1,
|
|
608
631
|
withdrawableUSD,
|
|
609
632
|
withdrawable: withdrawableShares,
|
|
610
|
-
|
|
633
|
+
withdrawableDex: new Dec(maxSupplyShares).minus(totalSupplyShares).toString(),
|
|
611
634
|
maxSupplyShares,
|
|
612
635
|
maxSupplySharesUsd,
|
|
613
636
|
collDexFee: supplyDexFee,
|
|
614
637
|
collSharePrice,
|
|
615
638
|
debtSharePrice,
|
|
616
639
|
oraclePrice,
|
|
640
|
+
tradingBorrowRate,
|
|
641
|
+
tradingSupplyRate,
|
|
617
642
|
};
|
|
618
643
|
return {
|
|
619
644
|
assetsData,
|
|
@@ -4,6 +4,24 @@ import { FluidVaultType, } from '../../types';
|
|
|
4
4
|
import { calcLeverageLiqPrice, getAssetsTotal, isLeveragedPos } from '../../moneymarket';
|
|
5
5
|
import { calculateNetApy } from '../../staking';
|
|
6
6
|
import { getEthAmountForDecimals } from '../../services/utils';
|
|
7
|
+
const calculateNetApyDex = ({ marketData, suppliedUsd, borrowedUsd }) => {
|
|
8
|
+
const { borrowRate, supplyRate, incentiveBorrowRate, incentiveSupplyRate, tradingBorrowRate, tradingSupplyRate, } = marketData;
|
|
9
|
+
const totalBorrowRate = new Dec(borrowRate).minus(tradingBorrowRate || '0').toString();
|
|
10
|
+
const totalSupplyRate = new Dec(supplyRate).add(tradingSupplyRate || '0').toString();
|
|
11
|
+
const borrowIncentive = new Dec(incentiveBorrowRate || '0').mul(borrowedUsd).div(100).toString();
|
|
12
|
+
const supplyIncentive = new Dec(incentiveSupplyRate || '0').mul(suppliedUsd).div(100).toString();
|
|
13
|
+
const incentiveUsd = new Dec(supplyIncentive).minus(borrowIncentive).toString();
|
|
14
|
+
const borrowInterest = new Dec(totalBorrowRate).mul(borrowedUsd).div(100).toString();
|
|
15
|
+
const supplyInterest = new Dec(totalSupplyRate).mul(suppliedUsd).div(100).toString();
|
|
16
|
+
const totalInterestUsd = new Dec(supplyInterest).add(incentiveUsd).minus(borrowInterest).toString();
|
|
17
|
+
const balance = new Dec(suppliedUsd).sub(borrowedUsd).toString();
|
|
18
|
+
const netApy = new Dec(totalInterestUsd).div(balance).times(100).toString();
|
|
19
|
+
return {
|
|
20
|
+
netApy,
|
|
21
|
+
incentiveUsd,
|
|
22
|
+
totalInterestUsd,
|
|
23
|
+
};
|
|
24
|
+
};
|
|
7
25
|
export const getFluidAggregatedData = ({ usedAssets, assetsData, marketData, }, supplyShares, borrowShares) => {
|
|
8
26
|
const payload = {};
|
|
9
27
|
payload.suppliedUsd = [FluidVaultType.T1, FluidVaultType.T3].includes(marketData.vaultType)
|
|
@@ -12,7 +30,8 @@ export const getFluidAggregatedData = ({ usedAssets, assetsData, marketData, },
|
|
|
12
30
|
payload.borrowedUsd = [FluidVaultType.T1, FluidVaultType.T2].includes(marketData.vaultType)
|
|
13
31
|
? getAssetsTotal(usedAssets, ({ isBorrowed }) => isBorrowed, ({ borrowedUsd }) => borrowedUsd)
|
|
14
32
|
: new Dec(marketData.debtSharePrice).mul(borrowShares).toString();
|
|
15
|
-
const
|
|
33
|
+
const isDex = [FluidVaultType.T2, FluidVaultType.T3, FluidVaultType.T4].includes(marketData.vaultType);
|
|
34
|
+
const { netApy, incentiveUsd, totalInterestUsd } = isDex ? calculateNetApyDex({ marketData, suppliedUsd: payload.suppliedUsd, borrowedUsd: payload.borrowedUsd }) : calculateNetApy({ usedAssets, assetsData: assetsData });
|
|
16
35
|
payload.netApy = netApy;
|
|
17
36
|
payload.incentiveUsd = incentiveUsd;
|
|
18
37
|
payload.totalInterestUsd = totalInterestUsd;
|
|
@@ -44,7 +63,7 @@ export const getFluidAggregatedData = ({ usedAssets, assetsData, marketData, },
|
|
|
44
63
|
export const parseDexSupplyData = (dexSupplyData, collAsset0, collAsset1) => {
|
|
45
64
|
const { dexPool, // address of the dex pool
|
|
46
65
|
dexId, // id of the dex pool
|
|
47
|
-
fee: _fee, // fee of the dex pool
|
|
66
|
+
fee: _fee, // fee of the dex pool (Only used as swap fees)
|
|
48
67
|
lastStoredPrice, // last stored price of the dex pool
|
|
49
68
|
centerPrice, // center price of the dex pool
|
|
50
69
|
token0Utilization, // token0 utilization
|
package/esm/types/fluid.d.ts
CHANGED
|
@@ -241,6 +241,8 @@ export interface InnerFluidMarketData {
|
|
|
241
241
|
borrowRate: string;
|
|
242
242
|
liquidationMaxLimit: string;
|
|
243
243
|
oraclePrice: string;
|
|
244
|
+
incentiveBorrowRate?: string;
|
|
245
|
+
incentiveSupplyRate?: string;
|
|
244
246
|
collSharePrice?: string;
|
|
245
247
|
maxSupplyShares?: string;
|
|
246
248
|
maxSupplySharesUsd?: string;
|
|
@@ -259,6 +261,8 @@ export interface InnerFluidMarketData {
|
|
|
259
261
|
totalBorrowToken0?: string;
|
|
260
262
|
totalBorrowToken1?: string;
|
|
261
263
|
borrowDexFee?: string;
|
|
264
|
+
tradingBorrowRate?: string;
|
|
265
|
+
tradingSupplyRate?: string;
|
|
262
266
|
}
|
|
263
267
|
export interface FluidMarketData {
|
|
264
268
|
assetsData: FluidAssetsData;
|
package/package.json
CHANGED
package/src/fluid/index.ts
CHANGED
|
@@ -155,6 +155,39 @@ const getTokenPriceFromChainlink = async (asset: AssetData, network: NetworkNumb
|
|
|
155
155
|
return new Dec(loanTokenPrice).div(1e8).toString();
|
|
156
156
|
};
|
|
157
157
|
|
|
158
|
+
const getMarketRateForDex = (token1PerShare: string, token0PerShare: string, rate0: string, rate1: string, price0: string, price1: string) => {
|
|
159
|
+
const token0PerShareUsd = new Dec(token0PerShare).mul(price0).toString();
|
|
160
|
+
const token1PerShareUsd = new Dec(token1PerShare).mul(price1).toString();
|
|
161
|
+
const sharesCombinedUsd = new Dec(token0PerShareUsd).plus(token1PerShareUsd);
|
|
162
|
+
|
|
163
|
+
const rate0PerShare = new Dec(rate0).mul(token0PerShareUsd).div(sharesCombinedUsd).toString();
|
|
164
|
+
|
|
165
|
+
const rate1PerShare = new Dec(rate1).mul(token1PerShareUsd).div(sharesCombinedUsd).toString();
|
|
166
|
+
|
|
167
|
+
return new Dec(rate0PerShare).plus(rate1PerShare).toString();
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
const getAdditionalMarketRateForDex = (token1PerShare: string, token0PerShare: string, incentiveSupplyRate0: string, incentiveSupplyRate1: string, price0: string, price1: string) => {
|
|
171
|
+
const token0PerShareUsd = new Dec(token0PerShare).mul(price0).toString();
|
|
172
|
+
const token1PerShareUsd = new Dec(token1PerShare).mul(price1).toString();
|
|
173
|
+
const sharesCombinedUsd = new Dec(token0PerShareUsd).plus(token1PerShareUsd);
|
|
174
|
+
|
|
175
|
+
const rate0PerShare = incentiveSupplyRate0 ? new Dec(incentiveSupplyRate0).mul(token0PerShareUsd).div(sharesCombinedUsd).toString() : 0;
|
|
176
|
+
|
|
177
|
+
const rate1PerShare = incentiveSupplyRate1 ? new Dec(incentiveSupplyRate1).mul(token1PerShareUsd).div(sharesCombinedUsd).toString() : 0;
|
|
178
|
+
|
|
179
|
+
return new Dec(rate0PerShare).plus(rate1PerShare).toString();
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
const getTradingApy = async (poolAddress: EthAddress) => {
|
|
183
|
+
const res = await fetch(`https://api.fluid.instadapp.io/v2/1/dexes/${poolAddress}/apy`);
|
|
184
|
+
if (!res.ok) {
|
|
185
|
+
return '0';
|
|
186
|
+
}
|
|
187
|
+
const data = await res.json();
|
|
188
|
+
return new Dec(data.tradingApy).div(100).toString();
|
|
189
|
+
};
|
|
190
|
+
|
|
158
191
|
const parseT1MarketData = async (web3: Web3, data: FluidView.VaultDataStructOutputStruct, network: NetworkNumber, mainnetWeb3: Web3) => {
|
|
159
192
|
const collAsset = getAssetInfoByAddress(data.supplyToken0, network);
|
|
160
193
|
const debtAsset = getAssetInfoByAddress(data.borrowToken0, network);
|
|
@@ -184,6 +217,8 @@ const parseT1MarketData = async (web3: Web3, data: FluidView.VaultDataStructOutp
|
|
|
184
217
|
collAssetData.incentiveSupplyToken = collAsset.symbol;
|
|
185
218
|
}
|
|
186
219
|
|
|
220
|
+
const incentiveSupplyRate = collAssetData.incentiveSupplyApy;
|
|
221
|
+
|
|
187
222
|
const debtAssetData: FluidAssetData = {
|
|
188
223
|
symbol: debtAsset.symbol,
|
|
189
224
|
address: debtAsset.address,
|
|
@@ -200,10 +235,7 @@ const parseT1MarketData = async (web3: Web3, data: FluidView.VaultDataStructOutp
|
|
|
200
235
|
debtAssetData.incentiveBorrowToken = debtAsset.symbol;
|
|
201
236
|
}
|
|
202
237
|
|
|
203
|
-
|
|
204
|
-
debtAssetData.incentiveBorrowApy = await getStakingApy(debtAsset.symbol, mainnetWeb3);
|
|
205
|
-
debtAssetData.incentiveBorrowToken = debtAsset.symbol;
|
|
206
|
-
}
|
|
238
|
+
const incentiveBorrowRate = debtAssetData.incentiveBorrowApy;
|
|
207
239
|
|
|
208
240
|
const assetsData = {
|
|
209
241
|
[collAsset.symbol]: collAssetData,
|
|
@@ -250,6 +282,8 @@ const parseT1MarketData = async (web3: Web3, data: FluidView.VaultDataStructOutp
|
|
|
250
282
|
liquidationMaxLimit,
|
|
251
283
|
borrowRate,
|
|
252
284
|
supplyRate,
|
|
285
|
+
incentiveSupplyRate,
|
|
286
|
+
incentiveBorrowRate,
|
|
253
287
|
oraclePrice,
|
|
254
288
|
};
|
|
255
289
|
|
|
@@ -259,27 +293,6 @@ const parseT1MarketData = async (web3: Web3, data: FluidView.VaultDataStructOutp
|
|
|
259
293
|
} as FluidMarketData;
|
|
260
294
|
};
|
|
261
295
|
|
|
262
|
-
const getMarketRateForDex = (token1PerShare: string, token0PerShare: string, rate0: string, rate1: string) => {
|
|
263
|
-
const sharesCombined = new Dec(token1PerShare).plus(token0PerShare);
|
|
264
|
-
|
|
265
|
-
const rate0PerShare = new Dec(rate0).mul(token0PerShare).div(sharesCombined).toString();
|
|
266
|
-
|
|
267
|
-
const rate1PerShare = new Dec(rate1).mul(token1PerShare).div(sharesCombined).toString();
|
|
268
|
-
|
|
269
|
-
return new Dec(rate0PerShare).plus(rate1PerShare).toString();
|
|
270
|
-
};
|
|
271
|
-
|
|
272
|
-
const getAdditionalMarketRateForDex = (token1PerShare: string, token0PerShare: string, incentiveSupplyRate0: string, incentiveSupplyRate1: string) => {
|
|
273
|
-
console.log(incentiveSupplyRate0, incentiveSupplyRate1);
|
|
274
|
-
const sharesCombined = new Dec(token1PerShare).plus(token0PerShare);
|
|
275
|
-
|
|
276
|
-
const rate0PerShare = incentiveSupplyRate0 ? new Dec(incentiveSupplyRate0).mul(token0PerShare).div(sharesCombined).toString() : 0;
|
|
277
|
-
|
|
278
|
-
const rate1PerShare = incentiveSupplyRate1 ? new Dec(incentiveSupplyRate1).mul(token1PerShare).div(sharesCombined).toString() : 0;
|
|
279
|
-
|
|
280
|
-
return new Dec(rate0PerShare).plus(rate1PerShare).toString();
|
|
281
|
-
};
|
|
282
|
-
|
|
283
296
|
const parseT2MarketData = async (web3: Web3, data: FluidView.VaultDataStructOutputStruct, network: NetworkNumber, mainnetWeb3: Web3) => {
|
|
284
297
|
const collAsset0 = getAssetInfoByAddress(data.supplyToken0, network);
|
|
285
298
|
const collAsset1 = getAssetInfoByAddress(data.supplyToken1, network);
|
|
@@ -347,8 +360,9 @@ const parseT2MarketData = async (web3: Web3, data: FluidView.VaultDataStructOutp
|
|
|
347
360
|
collSecondAssetData.incentiveSupplyToken = collAsset1.symbol;
|
|
348
361
|
}
|
|
349
362
|
|
|
350
|
-
const marketSupplyRate = getMarketRateForDex(token1PerSupplyShare, token0PerSupplyShare, supplyRate0, supplyRate1);
|
|
351
|
-
const incentiveSupplyRate = getAdditionalMarketRateForDex(token1PerSupplyShare, token0PerSupplyShare, collFirstAssetData.incentiveSupplyApy!, collSecondAssetData.incentiveSupplyApy!);
|
|
363
|
+
const marketSupplyRate = getMarketRateForDex(token1PerSupplyShare, token0PerSupplyShare, supplyRate0, supplyRate1, collFirstAssetData.price!, collSecondAssetData.price!);
|
|
364
|
+
const incentiveSupplyRate = getAdditionalMarketRateForDex(token1PerSupplyShare, token0PerSupplyShare, collFirstAssetData.incentiveSupplyApy!, collSecondAssetData.incentiveSupplyApy!, collFirstAssetData.price!, collSecondAssetData.price!);
|
|
365
|
+
const tradingSupplyRate = await getTradingApy(data.dexSupplyData.dexPool);
|
|
352
366
|
|
|
353
367
|
const borrowRate = new Dec(data.borrowRateVault).div(100).toString();
|
|
354
368
|
const debtAssetData: Partial<FluidAssetData> = {
|
|
@@ -364,6 +378,8 @@ const parseT2MarketData = async (web3: Web3, data: FluidView.VaultDataStructOutp
|
|
|
364
378
|
debtAssetData.incentiveBorrowToken = debtAsset.symbol;
|
|
365
379
|
}
|
|
366
380
|
|
|
381
|
+
const incentiveBorrowRate = debtAssetData.incentiveBorrowApy;
|
|
382
|
+
|
|
367
383
|
const assetsData: FluidAssetsData = ([
|
|
368
384
|
[collAsset0.symbol, collFirstAssetData],
|
|
369
385
|
[collAsset1.symbol, collSecondAssetData],
|
|
@@ -422,17 +438,20 @@ const parseT2MarketData = async (web3: Web3, data: FluidView.VaultDataStructOutp
|
|
|
422
438
|
borrowRate,
|
|
423
439
|
supplyRate: marketSupplyRate,
|
|
424
440
|
incentiveSupplyRate,
|
|
441
|
+
incentiveBorrowRate,
|
|
425
442
|
totalSupplyToken0,
|
|
426
443
|
totalSupplyToken1,
|
|
427
444
|
withdrawableToken0,
|
|
428
445
|
withdrawableToken1,
|
|
429
446
|
withdrawableUSD,
|
|
430
447
|
withdrawable: withdrawableShares,
|
|
431
|
-
|
|
448
|
+
withdrawableDex: new Dec(maxSupplyShares).minus(totalSupplyShares).toString(),
|
|
432
449
|
maxSupplyShares,
|
|
433
450
|
maxSupplySharesUsd,
|
|
434
451
|
collDexFee: supplyDexFee,
|
|
435
452
|
oraclePrice,
|
|
453
|
+
tradingSupplyRate,
|
|
454
|
+
tradingBorrowRate: '0',
|
|
436
455
|
};
|
|
437
456
|
|
|
438
457
|
return {
|
|
@@ -441,7 +460,6 @@ const parseT2MarketData = async (web3: Web3, data: FluidView.VaultDataStructOutp
|
|
|
441
460
|
} as FluidMarketData;
|
|
442
461
|
};
|
|
443
462
|
|
|
444
|
-
|
|
445
463
|
const parseT3MarketData = async (web3: Web3, data: FluidView.VaultDataStructOutputStruct, network: NetworkNumber, mainnetWeb3: Web3) => {
|
|
446
464
|
const collAsset = getAssetInfoByAddress(data.supplyToken0, network);
|
|
447
465
|
const debtAsset0 = getAssetInfoByAddress(data.borrowToken0, network);
|
|
@@ -489,6 +507,8 @@ const parseT3MarketData = async (web3: Web3, data: FluidView.VaultDataStructOutp
|
|
|
489
507
|
collAssetData.incentiveSupplyToken = collAsset.symbol;
|
|
490
508
|
}
|
|
491
509
|
|
|
510
|
+
const incentiveSupplyRate = collAssetData.incentiveSupplyApy;
|
|
511
|
+
|
|
492
512
|
const debtAsset0Data: Partial<FluidAssetData> = {
|
|
493
513
|
symbol: debtAsset0.symbol,
|
|
494
514
|
address: debtAsset0.address,
|
|
@@ -522,8 +542,9 @@ const parseT3MarketData = async (web3: Web3, data: FluidView.VaultDataStructOutp
|
|
|
522
542
|
debtAsset1Data.incentiveSupplyApy = await getStakingApy(debtAsset1.symbol, mainnetWeb3);
|
|
523
543
|
debtAsset1Data.incentiveSupplyToken = debtAsset1.symbol;
|
|
524
544
|
}
|
|
525
|
-
const marketBorrowRate = getMarketRateForDex(token1PerBorrowShare, token0PerBorrowShare, borrowRate0, borrowRate1);
|
|
526
|
-
const incentiveBorrowRate = getAdditionalMarketRateForDex(token1PerBorrowShare, token0PerBorrowShare, debtAsset0Data.incentiveSupplyApy!, debtAsset1Data.incentiveSupplyApy!);
|
|
545
|
+
const marketBorrowRate = getMarketRateForDex(token1PerBorrowShare, token0PerBorrowShare, borrowRate0, borrowRate1, debtAsset0Data.price!, debtAsset1Data.price!);
|
|
546
|
+
const incentiveBorrowRate = getAdditionalMarketRateForDex(token1PerBorrowShare, token0PerBorrowShare, debtAsset0Data.incentiveSupplyApy!, debtAsset1Data.incentiveSupplyApy!, debtAsset0Data.price!, debtAsset1Data.price!);
|
|
547
|
+
const tradingBorrowRate = await getTradingApy(data.dexBorrowData.dexPool);
|
|
527
548
|
|
|
528
549
|
const assetsData: FluidAssetsData = ([
|
|
529
550
|
[collAsset.symbol, collAssetData],
|
|
@@ -580,6 +601,9 @@ const parseT3MarketData = async (web3: Web3, data: FluidView.VaultDataStructOutp
|
|
|
580
601
|
borrowRate: marketBorrowRate,
|
|
581
602
|
supplyRate,
|
|
582
603
|
incentiveBorrowRate,
|
|
604
|
+
incentiveSupplyRate,
|
|
605
|
+
tradingBorrowRate,
|
|
606
|
+
tradingSupplyRate: '0',
|
|
583
607
|
borrowableToken0,
|
|
584
608
|
borrowableToken1,
|
|
585
609
|
totalBorrowToken0,
|
|
@@ -726,12 +750,15 @@ const parseT4MarketData = async (web3: Web3, data: FluidView.VaultDataStructOutp
|
|
|
726
750
|
debtAsset1Data.incentiveSupplyApy = await getStakingApy(debtAsset1.symbol, mainnetWeb3);
|
|
727
751
|
debtAsset1Data.incentiveSupplyToken = debtAsset1.symbol;
|
|
728
752
|
}
|
|
753
|
+
const marketInfo = getFluidMarketInfoById(+data.vaultId, network);
|
|
729
754
|
|
|
730
|
-
const marketBorrowRate = getMarketRateForDex(token1PerBorrowShare, token0PerBorrowShare, borrowRate0, borrowRate1);
|
|
731
|
-
const incentiveBorrowRate = getAdditionalMarketRateForDex(token1PerBorrowShare, token0PerBorrowShare, debtAsset0Data.incentiveSupplyApy!, debtAsset1Data.incentiveSupplyApy!);
|
|
755
|
+
const marketBorrowRate = getMarketRateForDex(token1PerBorrowShare, token0PerBorrowShare, borrowRate0, borrowRate1, debtAsset0Data.price!, debtAsset1Data.price!);
|
|
756
|
+
const incentiveBorrowRate = getAdditionalMarketRateForDex(token1PerBorrowShare, token0PerBorrowShare, debtAsset0Data.incentiveSupplyApy!, debtAsset1Data.incentiveSupplyApy!, debtAsset0Data.price!, debtAsset1Data.price!);
|
|
757
|
+
const tradingBorrowRate = await getTradingApy(data.dexBorrowData.dexPool);
|
|
732
758
|
|
|
733
|
-
const marketSupplyRate = getMarketRateForDex(token1PerSupplyShare, token0PerSupplyShare, supplyRate0, supplyRate1);
|
|
734
|
-
const incentiveSupplyRate = getAdditionalMarketRateForDex(token1PerSupplyShare, token0PerSupplyShare, collAsset0Data.incentiveSupplyApy!, collAsset1Data.incentiveSupplyApy!);
|
|
759
|
+
const marketSupplyRate = getMarketRateForDex(token1PerSupplyShare, token0PerSupplyShare, supplyRate0, supplyRate1, collAsset0Data.price!, collAsset1Data.price!);
|
|
760
|
+
const incentiveSupplyRate = getAdditionalMarketRateForDex(token1PerSupplyShare, token0PerSupplyShare, collAsset0Data.incentiveSupplyApy!, collAsset1Data.incentiveSupplyApy!, collAsset0Data.price!, collAsset1Data.price!);
|
|
761
|
+
const tradingSupplyRate = await getTradingApy(data.dexSupplyData.dexPool);
|
|
735
762
|
|
|
736
763
|
const assetsData: FluidAssetsData = ([
|
|
737
764
|
[collAsset0.symbol, collAsset0Data],
|
|
@@ -744,7 +771,6 @@ const parseT4MarketData = async (web3: Web3, data: FluidView.VaultDataStructOutp
|
|
|
744
771
|
[symbol]: mergeAssetData(acc[symbol], partialData),
|
|
745
772
|
}), {} as Record<string, FluidAssetData>) as FluidAssetsData;
|
|
746
773
|
|
|
747
|
-
const marketInfo = getFluidMarketInfoById(+data.vaultId, network);
|
|
748
774
|
|
|
749
775
|
const liqRatio = new Dec(data.liquidationThreshold).div(100).toString();
|
|
750
776
|
const liquidationMaxLimit = new Dec(data.liquidationMaxLimit).div(100).toString();
|
|
@@ -805,13 +831,15 @@ const parseT4MarketData = async (web3: Web3, data: FluidView.VaultDataStructOutp
|
|
|
805
831
|
withdrawableToken1,
|
|
806
832
|
withdrawableUSD,
|
|
807
833
|
withdrawable: withdrawableShares,
|
|
808
|
-
|
|
834
|
+
withdrawableDex: new Dec(maxSupplyShares).minus(totalSupplyShares).toString(),
|
|
809
835
|
maxSupplyShares,
|
|
810
836
|
maxSupplySharesUsd,
|
|
811
837
|
collDexFee: supplyDexFee,
|
|
812
838
|
collSharePrice,
|
|
813
839
|
debtSharePrice,
|
|
814
840
|
oraclePrice,
|
|
841
|
+
tradingBorrowRate,
|
|
842
|
+
tradingSupplyRate,
|
|
815
843
|
};
|
|
816
844
|
|
|
817
845
|
return {
|
|
@@ -13,6 +13,36 @@ import { MMAssetsData } from '../../types/common';
|
|
|
13
13
|
import { FluidView } from '../../types/contracts/generated';
|
|
14
14
|
import { getEthAmountForDecimals } from '../../services/utils';
|
|
15
15
|
|
|
16
|
+
const calculateNetApyDex = ({ marketData, suppliedUsd, borrowedUsd }: { marketData: InnerFluidMarketData, suppliedUsd: string, borrowedUsd: string }) => {
|
|
17
|
+
const {
|
|
18
|
+
borrowRate,
|
|
19
|
+
supplyRate,
|
|
20
|
+
incentiveBorrowRate,
|
|
21
|
+
incentiveSupplyRate,
|
|
22
|
+
tradingBorrowRate,
|
|
23
|
+
tradingSupplyRate,
|
|
24
|
+
} = marketData;
|
|
25
|
+
|
|
26
|
+
const totalBorrowRate = new Dec(borrowRate).minus(tradingBorrowRate || '0').toString();
|
|
27
|
+
const totalSupplyRate = new Dec(supplyRate).add(tradingSupplyRate || '0').toString();
|
|
28
|
+
|
|
29
|
+
const borrowIncentive = new Dec(incentiveBorrowRate || '0').mul(borrowedUsd).div(100).toString();
|
|
30
|
+
const supplyIncentive = new Dec(incentiveSupplyRate || '0').mul(suppliedUsd).div(100).toString();
|
|
31
|
+
const incentiveUsd = new Dec(supplyIncentive).minus(borrowIncentive).toString();
|
|
32
|
+
|
|
33
|
+
const borrowInterest = new Dec(totalBorrowRate).mul(borrowedUsd).div(100).toString();
|
|
34
|
+
const supplyInterest = new Dec(totalSupplyRate).mul(suppliedUsd).div(100).toString();
|
|
35
|
+
const totalInterestUsd = new Dec(supplyInterest).add(incentiveUsd).minus(borrowInterest).toString();
|
|
36
|
+
const balance = new Dec(suppliedUsd).sub(borrowedUsd).toString();
|
|
37
|
+
const netApy = new Dec(totalInterestUsd).div(balance).times(100).toString();
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
netApy,
|
|
41
|
+
incentiveUsd,
|
|
42
|
+
totalInterestUsd,
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
|
|
16
46
|
export const getFluidAggregatedData = ({
|
|
17
47
|
usedAssets,
|
|
18
48
|
assetsData,
|
|
@@ -34,7 +64,8 @@ borrowShares?: string,
|
|
|
34
64
|
? getAssetsTotal(usedAssets, ({ isBorrowed }: { isBorrowed: boolean }) => isBorrowed, ({ borrowedUsd }: { borrowedUsd: string }) => borrowedUsd)
|
|
35
65
|
: new Dec(marketData.debtSharePrice!).mul(borrowShares!).toString();
|
|
36
66
|
|
|
37
|
-
const
|
|
67
|
+
const isDex = [FluidVaultType.T2, FluidVaultType.T3, FluidVaultType.T4].includes(marketData.vaultType);
|
|
68
|
+
const { netApy, incentiveUsd, totalInterestUsd } = isDex ? calculateNetApyDex({ marketData, suppliedUsd: payload.suppliedUsd, borrowedUsd: payload.borrowedUsd }) : calculateNetApy({ usedAssets, assetsData: assetsData as unknown as MMAssetsData });
|
|
38
69
|
payload.netApy = netApy;
|
|
39
70
|
payload.incentiveUsd = incentiveUsd;
|
|
40
71
|
payload.totalInterestUsd = totalInterestUsd;
|
|
@@ -94,7 +125,7 @@ export const parseDexSupplyData = (dexSupplyData: FluidView.DexSupplyDataStructO
|
|
|
94
125
|
const {
|
|
95
126
|
dexPool, // address of the dex pool
|
|
96
127
|
dexId, // id of the dex pool
|
|
97
|
-
fee: _fee, // fee of the dex pool
|
|
128
|
+
fee: _fee, // fee of the dex pool (Only used as swap fees)
|
|
98
129
|
lastStoredPrice, // last stored price of the dex pool
|
|
99
130
|
centerPrice, // center price of the dex pool
|
|
100
131
|
token0Utilization, // token0 utilization
|
package/src/types/fluid.ts
CHANGED
|
@@ -252,6 +252,8 @@ export interface InnerFluidMarketData {
|
|
|
252
252
|
borrowRate: string,
|
|
253
253
|
liquidationMaxLimit: string,
|
|
254
254
|
oraclePrice: string,
|
|
255
|
+
incentiveBorrowRate?: string,
|
|
256
|
+
incentiveSupplyRate?: string,
|
|
255
257
|
// T2 and T4 vaults
|
|
256
258
|
collSharePrice?: string,
|
|
257
259
|
maxSupplyShares?: string,
|
|
@@ -272,6 +274,9 @@ export interface InnerFluidMarketData {
|
|
|
272
274
|
totalBorrowToken0?: string,
|
|
273
275
|
totalBorrowToken1?: string,
|
|
274
276
|
borrowDexFee?: string,
|
|
277
|
+
// Dex vault
|
|
278
|
+
tradingBorrowRate?: string,
|
|
279
|
+
tradingSupplyRate?: string,
|
|
275
280
|
}
|
|
276
281
|
|
|
277
282
|
export interface FluidMarketData {
|