@defisaver/positions-sdk 1.0.10 → 1.0.11-fluid-dev
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 +69 -69
- package/cjs/config/contracts.d.ts +113 -49
- package/cjs/config/contracts.js +10 -2
- package/cjs/contracts.d.ts +1 -0
- package/cjs/contracts.js +2 -1
- package/cjs/fluid/index.d.ts +2 -0
- package/cjs/fluid/index.js +636 -13
- package/cjs/helpers/fluidHelpers/index.d.ts +59 -2
- package/cjs/helpers/fluidHelpers/index.js +142 -4
- package/cjs/helpers/morphoBlueHelpers/index.js +66 -66
- package/cjs/markets/aave/marketAssets.js +3 -1
- package/cjs/markets/fluid/index.js +276 -245
- package/cjs/services/priceService.d.ts +23 -0
- package/cjs/services/priceService.js +44 -5
- package/cjs/types/contracts/generated/FluidView.d.ts +220 -3
- package/cjs/types/contracts/generated/WeETHPriceFeed.d.ts +135 -0
- package/cjs/types/contracts/generated/WeETHPriceFeed.js +5 -0
- package/cjs/types/contracts/generated/index.d.ts +1 -0
- package/cjs/types/fluid.d.ts +39 -10
- package/esm/config/contracts.d.ts +113 -49
- package/esm/config/contracts.js +10 -2
- package/esm/contracts.d.ts +1 -0
- package/esm/contracts.js +1 -0
- package/esm/fluid/index.d.ts +2 -0
- package/esm/fluid/index.js +639 -16
- package/esm/helpers/fluidHelpers/index.d.ts +59 -2
- package/esm/helpers/fluidHelpers/index.js +137 -3
- package/esm/helpers/morphoBlueHelpers/index.js +66 -66
- package/esm/markets/aave/marketAssets.js +3 -1
- package/esm/markets/fluid/index.js +276 -245
- package/esm/services/priceService.d.ts +23 -0
- package/esm/services/priceService.js +40 -5
- package/esm/types/contracts/generated/FluidView.d.ts +220 -3
- package/esm/types/contracts/generated/WeETHPriceFeed.d.ts +135 -0
- package/esm/types/contracts/generated/WeETHPriceFeed.js +4 -0
- package/esm/types/contracts/generated/index.d.ts +1 -0
- package/esm/types/fluid.d.ts +39 -10
- package/package.json +54 -54
- package/src/aaveV2/index.ts +227 -227
- package/src/aaveV3/index.ts +624 -624
- package/src/assets/index.ts +60 -60
- package/src/chickenBonds/index.ts +123 -123
- package/src/compoundV2/index.ts +220 -220
- package/src/compoundV3/index.ts +291 -291
- package/src/config/contracts.js +1155 -1147
- package/src/constants/index.ts +6 -6
- package/src/contracts.ts +135 -134
- package/src/curveUsd/index.ts +239 -239
- package/src/eulerV2/index.ts +303 -303
- package/src/exchange/index.ts +17 -17
- package/src/fluid/index.ts +1216 -354
- package/src/helpers/aaveHelpers/index.ts +203 -203
- package/src/helpers/chickenBondsHelpers/index.ts +23 -23
- package/src/helpers/compoundHelpers/index.ts +248 -248
- package/src/helpers/curveUsdHelpers/index.ts +40 -40
- package/src/helpers/eulerHelpers/index.ts +234 -234
- package/src/helpers/fluidHelpers/index.ts +295 -57
- package/src/helpers/index.ts +11 -11
- package/src/helpers/liquityV2Helpers/index.ts +80 -80
- package/src/helpers/llamaLendHelpers/index.ts +53 -53
- package/src/helpers/makerHelpers/index.ts +94 -94
- package/src/helpers/morphoBlueHelpers/index.ts +367 -367
- package/src/helpers/sparkHelpers/index.ts +154 -154
- package/src/index.ts +52 -52
- package/src/liquity/index.ts +116 -116
- package/src/liquityV2/index.ts +295 -295
- package/src/llamaLend/index.ts +275 -275
- package/src/maker/index.ts +117 -117
- package/src/markets/aave/index.ts +152 -152
- package/src/markets/aave/marketAssets.ts +46 -44
- package/src/markets/compound/index.ts +213 -213
- package/src/markets/compound/marketsAssets.ts +82 -82
- package/src/markets/curveUsd/index.ts +69 -69
- package/src/markets/euler/index.ts +26 -26
- package/src/markets/fluid/index.ts +2043 -2012
- package/src/markets/index.ts +27 -27
- package/src/markets/liquityV2/index.ts +54 -54
- package/src/markets/llamaLend/contractAddresses.ts +141 -141
- package/src/markets/llamaLend/index.ts +235 -235
- package/src/markets/morphoBlue/index.ts +895 -895
- package/src/markets/spark/index.ts +29 -29
- package/src/markets/spark/marketAssets.ts +10 -10
- package/src/moneymarket/moneymarketCommonService.ts +80 -80
- package/src/morphoAaveV2/index.ts +256 -256
- package/src/morphoAaveV3/index.ts +630 -630
- package/src/morphoBlue/index.ts +202 -202
- package/src/multicall/index.ts +33 -33
- package/src/services/priceService.ts +130 -91
- package/src/services/utils.ts +59 -59
- package/src/setup.ts +8 -8
- package/src/spark/index.ts +460 -460
- package/src/staking/staking.ts +217 -217
- package/src/types/aave.ts +275 -275
- package/src/types/chickenBonds.ts +45 -45
- package/src/types/common.ts +84 -84
- package/src/types/compound.ts +133 -133
- package/src/types/contracts/generated/FluidView.ts +263 -2
- package/src/types/contracts/generated/WeETHPriceFeed.ts +202 -0
- package/src/types/contracts/generated/index.ts +1 -0
- package/src/types/curveUsd.ts +119 -119
- package/src/types/euler.ts +173 -173
- package/src/types/fluid.ts +299 -268
- package/src/types/index.ts +11 -11
- package/src/types/liquity.ts +30 -30
- package/src/types/liquityV2.ts +119 -119
- package/src/types/llamaLend.ts +155 -155
- package/src/types/maker.ts +50 -50
- package/src/types/morphoBlue.ts +194 -194
- package/src/types/spark.ts +135 -135
package/esm/fluid/index.js
CHANGED
|
@@ -8,16 +8,16 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
10
|
import Dec from 'decimal.js';
|
|
11
|
-
import { getAssetInfo, getAssetInfoByAddress } from '@defisaver/tokens';
|
|
11
|
+
import { assetAmountInEth, getAssetInfo, getAssetInfoByAddress, } from '@defisaver/tokens';
|
|
12
12
|
import { NetworkNumber } from '../types/common';
|
|
13
13
|
import { FluidVaultType, } from '../types';
|
|
14
14
|
import { DFSFeedRegistryContract, FeedRegistryContract, FluidViewContract } from '../contracts';
|
|
15
15
|
import { getEthAmountForDecimals, isMainnetNetwork } from '../services/utils';
|
|
16
|
-
import { getFluidAggregatedData } from '../helpers/fluidHelpers';
|
|
16
|
+
import { getFluidAggregatedData, mergeAssetData, mergeUsedAssets, parseDexBorrowData, parseDexSupplyData, } from '../helpers/fluidHelpers';
|
|
17
17
|
import { chunkAndMulticall } from '../multicall';
|
|
18
18
|
import { getFluidMarketInfoById, getFluidVersionsDataForNetwork, getFTokenAddress } from '../markets';
|
|
19
19
|
import { USD_QUOTE } from '../constants';
|
|
20
|
-
import { getChainlinkAssetAddress, getWstETHPriceFluid } from '../services/priceService';
|
|
20
|
+
import { getChainlinkAssetAddress, getWeETHChainLinkPriceCalls, getWstETHChainLinkPriceCalls, getWstETHPriceFluid, parseWeETHPriceCalls, parseWstETHPriceCalls, } from '../services/priceService';
|
|
21
21
|
import { getStakingApy, STAKING_ASSETS } from '../staking';
|
|
22
22
|
export const EMPTY_USED_ASSET = {
|
|
23
23
|
isSupplied: false,
|
|
@@ -38,19 +38,67 @@ const parseVaultType = (vaultType) => {
|
|
|
38
38
|
default: return FluidVaultType.Unknown;
|
|
39
39
|
}
|
|
40
40
|
};
|
|
41
|
-
const
|
|
42
|
-
const collAsset = getAssetInfoByAddress(data.supplyToken0, network);
|
|
43
|
-
const debtAsset = getAssetInfoByAddress(data.borrowToken0, network);
|
|
44
|
-
const supplyRate = new Dec(data.supplyRateVault).div(100).toString();
|
|
45
|
-
const borrowRate = new Dec(data.borrowRateVault).div(100).toString();
|
|
46
|
-
const oracleScaleFactor = new Dec(27).add(debtAsset.decimals).sub(collAsset.decimals).toString();
|
|
47
|
-
const oracleScale = new Dec(10).pow(oracleScaleFactor).toString();
|
|
48
|
-
const oraclePrice = new Dec(data.oraclePriceOperate).div(oracleScale).toString();
|
|
49
|
-
const isTokenUSDA = debtAsset.symbol === 'USDA';
|
|
41
|
+
const getChainLinkPricesForTokens = (tokens, network, web3) => __awaiter(void 0, void 0, void 0, function* () {
|
|
50
42
|
const isMainnet = isMainnetNetwork(network);
|
|
51
|
-
const
|
|
43
|
+
const noDuplicateTokens = new Array(...new Set(tokens));
|
|
44
|
+
const calls = noDuplicateTokens.flatMap((address) => {
|
|
45
|
+
const assetInfo = getAssetInfoByAddress(address, network);
|
|
46
|
+
const isTokenUSDA = assetInfo.symbol === 'USDA';
|
|
47
|
+
if (isTokenUSDA)
|
|
48
|
+
return;
|
|
49
|
+
const chainLinkFeedAddress = getChainlinkAssetAddress(assetInfo.symbol, network);
|
|
50
|
+
if (assetInfo.symbol === 'wstETH')
|
|
51
|
+
return getWstETHChainLinkPriceCalls(web3, network);
|
|
52
|
+
if (assetInfo.symbol === 'weETH')
|
|
53
|
+
return getWeETHChainLinkPriceCalls(web3, network);
|
|
54
|
+
if (isMainnet) {
|
|
55
|
+
const feedRegistryContract = FeedRegistryContract(web3, NetworkNumber.Eth);
|
|
56
|
+
return ({
|
|
57
|
+
target: feedRegistryContract.options.address,
|
|
58
|
+
abiItem: feedRegistryContract.options.jsonInterface.find(({ name }) => name === 'latestAnswer'),
|
|
59
|
+
params: [chainLinkFeedAddress, USD_QUOTE],
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
const feedRegistryContract = DFSFeedRegistryContract(web3, network);
|
|
63
|
+
return ({
|
|
64
|
+
target: feedRegistryContract.options.address,
|
|
65
|
+
abiItem: feedRegistryContract.options.jsonInterface.find(({ name }) => name === 'latestRoundData'),
|
|
66
|
+
params: [chainLinkFeedAddress, USD_QUOTE],
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
const prices = yield chunkAndMulticall(calls, 10, 'latest', web3, network);
|
|
70
|
+
let offset = 0; // wstETH has 3 calls, while others have only 1, so we need to keep track
|
|
71
|
+
return noDuplicateTokens.reduce((acc, token, i) => {
|
|
72
|
+
const assetInfo = getAssetInfoByAddress(token, network);
|
|
73
|
+
switch (assetInfo.symbol) {
|
|
74
|
+
case 'USDA':
|
|
75
|
+
acc[token] = '100000000';
|
|
76
|
+
break;
|
|
77
|
+
case 'wstETH': {
|
|
78
|
+
const { ethPrice, wstETHRate, } = parseWstETHPriceCalls(prices[i + offset][0], prices[i + offset + 1], prices[i + offset + 2][0]);
|
|
79
|
+
offset += 2;
|
|
80
|
+
acc[token] = new Dec(ethPrice).mul(wstETHRate).toString();
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
case 'weETH': {
|
|
84
|
+
const { ethPrice, weETHRate, } = parseWeETHPriceCalls(prices[i + offset][0], prices[i + offset + 1], prices[i + offset + 2][0]);
|
|
85
|
+
offset += 2;
|
|
86
|
+
acc[token] = new Dec(ethPrice).mul(weETHRate).toString();
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
default:
|
|
90
|
+
acc[token] = new Dec(prices[i + offset].answer).div(1e8).toString();
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
return acc;
|
|
94
|
+
}, {});
|
|
95
|
+
});
|
|
96
|
+
const getTokenPriceFromChainlink = (asset, network, web3) => __awaiter(void 0, void 0, void 0, function* () {
|
|
97
|
+
const isTokenUSDA = asset.symbol === 'USDA';
|
|
98
|
+
const isMainnet = isMainnetNetwork(network);
|
|
99
|
+
const loanTokenFeedAddress = getChainlinkAssetAddress(asset.symbol, network);
|
|
52
100
|
let loanTokenPrice;
|
|
53
|
-
if (
|
|
101
|
+
if (asset.symbol === 'wstETH') {
|
|
54
102
|
// need to handle wstETH for l2s inside getWstETHPrice
|
|
55
103
|
loanTokenPrice = yield getWstETHPriceFluid(web3, network);
|
|
56
104
|
}
|
|
@@ -64,7 +112,17 @@ const parseMarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0,
|
|
|
64
112
|
const roundPriceData = isTokenUSDA ? { answer: '100000000' } : yield feedRegistryContract.methods.latestRoundData(loanTokenFeedAddress, USD_QUOTE).call();
|
|
65
113
|
loanTokenPrice = roundPriceData.answer;
|
|
66
114
|
}
|
|
67
|
-
|
|
115
|
+
return new Dec(loanTokenPrice).div(1e8).toString();
|
|
116
|
+
});
|
|
117
|
+
const parseT1MarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0, void 0, void 0, function* () {
|
|
118
|
+
const collAsset = getAssetInfoByAddress(data.supplyToken0, network);
|
|
119
|
+
const debtAsset = getAssetInfoByAddress(data.borrowToken0, network);
|
|
120
|
+
const supplyRate = new Dec(data.supplyRateVault).div(100).toString();
|
|
121
|
+
const borrowRate = new Dec(data.borrowRateVault).div(100).toString();
|
|
122
|
+
const oracleScaleFactor = new Dec(27).add(debtAsset.decimals).sub(collAsset.decimals).toString();
|
|
123
|
+
const oracleScale = new Dec(10).pow(oracleScaleFactor).toString();
|
|
124
|
+
const oraclePrice = new Dec(data.oraclePriceOperate).div(oracleScale).toString();
|
|
125
|
+
const debtPriceParsed = yield getTokenPriceFromChainlink(debtAsset, network, web3);
|
|
68
126
|
const collAssetData = {
|
|
69
127
|
symbol: collAsset.symbol,
|
|
70
128
|
address: collAsset.address,
|
|
@@ -95,6 +153,10 @@ const parseMarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0,
|
|
|
95
153
|
debtAssetData.incentiveBorrowApy = yield getStakingApy(debtAsset.symbol, mainnetWeb3);
|
|
96
154
|
debtAssetData.incentiveBorrowToken = debtAsset.symbol;
|
|
97
155
|
}
|
|
156
|
+
if (STAKING_ASSETS.includes(debtAssetData.symbol)) {
|
|
157
|
+
debtAssetData.incentiveBorrowApy = yield getStakingApy(debtAsset.symbol, mainnetWeb3);
|
|
158
|
+
debtAssetData.incentiveBorrowToken = debtAsset.symbol;
|
|
159
|
+
}
|
|
98
160
|
const assetsData = {
|
|
99
161
|
[collAsset.symbol]: collAssetData,
|
|
100
162
|
[debtAsset.symbol]: debtAssetData,
|
|
@@ -144,6 +206,398 @@ const parseMarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0,
|
|
|
144
206
|
marketData,
|
|
145
207
|
};
|
|
146
208
|
});
|
|
209
|
+
const parseT2MarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0, void 0, void 0, function* () {
|
|
210
|
+
const collAsset0 = getAssetInfoByAddress(data.supplyToken0, network);
|
|
211
|
+
const collAsset1 = getAssetInfoByAddress(data.supplyToken1, network);
|
|
212
|
+
const debtAsset = getAssetInfoByAddress(data.borrowToken0, network);
|
|
213
|
+
// 18 because collateral is represented in shares for which they use 18 decimals
|
|
214
|
+
const oracleScaleFactor = new Dec(27).add(debtAsset.decimals).sub(18).toString();
|
|
215
|
+
const oracleScale = new Dec(10).pow(oracleScaleFactor).toString();
|
|
216
|
+
const oraclePrice = new Dec(data.oraclePriceOperate).div(oracleScale).toString();
|
|
217
|
+
const prices = yield getChainLinkPricesForTokens([collAsset0.address, collAsset1.address, debtAsset.address], network, web3);
|
|
218
|
+
const { supplyDexFee, totalSupplyShares, supplyRate1, totalSupplyToken1, token0PerSupplyShare, token1PerSupplyShare, totalSupplyToken0, maxSupplyShares, withdrawableToken0, withdrawable0, withdrawableToken1, withdrawable1, supplyRate0, utilizationSupply0, utilizationSupply1, withdrawableShares, reservesSupplyToken0, reservesSupplyToken1, } = parseDexSupplyData(data.dexSupplyData, collAsset0.symbol, collAsset1.symbol);
|
|
219
|
+
const collFirstAssetData = {
|
|
220
|
+
symbol: collAsset0.symbol,
|
|
221
|
+
address: collAsset0.address,
|
|
222
|
+
price: prices[collAsset0.address],
|
|
223
|
+
totalSupply: new Dec(totalSupplyShares).mul(token0PerSupplyShare).toString(),
|
|
224
|
+
canBeSupplied: true,
|
|
225
|
+
supplyRate: supplyRate0,
|
|
226
|
+
utilization: utilizationSupply0,
|
|
227
|
+
withdrawable: withdrawable0,
|
|
228
|
+
tokenPerSupplyShare: token0PerSupplyShare,
|
|
229
|
+
supplyReserves: reservesSupplyToken0,
|
|
230
|
+
};
|
|
231
|
+
if (STAKING_ASSETS.includes(collFirstAssetData.symbol)) {
|
|
232
|
+
collFirstAssetData.incentiveSupplyApy = yield getStakingApy(collAsset0.symbol, mainnetWeb3);
|
|
233
|
+
collFirstAssetData.incentiveSupplyToken = collAsset0.symbol;
|
|
234
|
+
}
|
|
235
|
+
const collSecondAssetData = {
|
|
236
|
+
symbol: collAsset1.symbol,
|
|
237
|
+
address: collAsset1.address,
|
|
238
|
+
price: prices[collAsset1.address],
|
|
239
|
+
totalSupply: new Dec(totalSupplyShares).mul(token1PerSupplyShare).toString(),
|
|
240
|
+
canBeSupplied: true,
|
|
241
|
+
supplyRate: supplyRate1,
|
|
242
|
+
withdrawable: withdrawable1,
|
|
243
|
+
utilization: utilizationSupply1,
|
|
244
|
+
tokenPerSupplyShare: token1PerSupplyShare,
|
|
245
|
+
supplyReserves: reservesSupplyToken1,
|
|
246
|
+
};
|
|
247
|
+
if (STAKING_ASSETS.includes(collFirstAssetData.symbol)) {
|
|
248
|
+
collFirstAssetData.incentiveSupplyApy = yield getStakingApy(collAsset1.symbol, mainnetWeb3);
|
|
249
|
+
collFirstAssetData.incentiveSupplyToken = collAsset1.symbol;
|
|
250
|
+
}
|
|
251
|
+
const borrowRate = new Dec(data.borrowRateVault).div(100).toString();
|
|
252
|
+
const debtAssetData = {
|
|
253
|
+
symbol: debtAsset.symbol,
|
|
254
|
+
address: debtAsset.address,
|
|
255
|
+
price: prices[debtAsset.address],
|
|
256
|
+
totalBorrow: data.totalBorrowVault,
|
|
257
|
+
canBeBorrowed: true,
|
|
258
|
+
borrowRate,
|
|
259
|
+
};
|
|
260
|
+
if (STAKING_ASSETS.includes(debtAssetData.symbol)) {
|
|
261
|
+
debtAssetData.incentiveBorrowApy = yield getStakingApy(debtAsset.symbol, mainnetWeb3);
|
|
262
|
+
debtAssetData.incentiveBorrowToken = debtAsset.symbol;
|
|
263
|
+
}
|
|
264
|
+
const assetsData = [
|
|
265
|
+
[collAsset0.symbol, collFirstAssetData],
|
|
266
|
+
[collAsset1.symbol, collSecondAssetData],
|
|
267
|
+
[debtAsset.symbol, debtAssetData],
|
|
268
|
+
]
|
|
269
|
+
.reduce((acc, [symbol, partialData]) => (Object.assign(Object.assign({}, acc), { [symbol]: mergeAssetData(acc[symbol], partialData) })), {});
|
|
270
|
+
const marketInfo = getFluidMarketInfoById(+data.vaultId, network);
|
|
271
|
+
const totalBorrowVault = getEthAmountForDecimals(data.totalBorrowVault, debtAsset.decimals);
|
|
272
|
+
const liqRatio = new Dec(data.liquidationThreshold).div(100).toString();
|
|
273
|
+
const liquidationMaxLimit = new Dec(data.liquidationMaxLimit).div(100).toString();
|
|
274
|
+
const liqFactor = new Dec(data.liquidationThreshold).div(10000).toString();
|
|
275
|
+
const totalSupplySharesInVault = assetAmountInEth(data.totalSupplyVault);
|
|
276
|
+
const collSharePrice = new Dec(oraclePrice).mul(prices[debtAsset.address]).toString();
|
|
277
|
+
const totalSupplyVaultUsd = new Dec(totalSupplySharesInVault).mul(collSharePrice).toString();
|
|
278
|
+
const withdrawableUSD = new Dec(withdrawableShares).mul(collSharePrice).toString();
|
|
279
|
+
const marketData = {
|
|
280
|
+
vaultId: +data.vaultId,
|
|
281
|
+
vaultValue: marketInfo === null || marketInfo === void 0 ? void 0 : marketInfo.value,
|
|
282
|
+
isSmartColl: data.isSmartColl,
|
|
283
|
+
isSmartDebt: data.isSmartDebt,
|
|
284
|
+
marketAddress: data.vault,
|
|
285
|
+
vaultType: parseVaultType(+data.vaultType),
|
|
286
|
+
oracle: data.oracle,
|
|
287
|
+
liquidationPenaltyPercent: new Dec(data.liquidationPenalty).div(100).toString(),
|
|
288
|
+
collFactor: new Dec(data.collateralFactor).div(10000).toString(),
|
|
289
|
+
liquidationRatio: liqRatio,
|
|
290
|
+
liqFactor,
|
|
291
|
+
minRatio: new Dec(1).div(liqFactor).mul(100).toString(),
|
|
292
|
+
collAsset0: collAsset0.symbol,
|
|
293
|
+
collAsset1: collAsset1.symbol,
|
|
294
|
+
debtAsset0: debtAsset.symbol,
|
|
295
|
+
totalPositions: data.totalPositions,
|
|
296
|
+
totalSupplyVault: totalSupplyShares,
|
|
297
|
+
totalBorrowVault,
|
|
298
|
+
totalSupplyVaultUsd,
|
|
299
|
+
collSharePrice,
|
|
300
|
+
totalBorrowVaultUsd: new Dec(totalBorrowVault).mul(assetsData[debtAsset.symbol].price).toString(),
|
|
301
|
+
borrowLimit: getEthAmountForDecimals(data.borrowLimit, debtAsset.decimals),
|
|
302
|
+
borrowableUntilLimit: getEthAmountForDecimals(data.borrowableUntilLimit, debtAsset.decimals),
|
|
303
|
+
borrowable: getEthAmountForDecimals(data.borrowable, debtAsset.decimals),
|
|
304
|
+
borrowLimitUtilization: getEthAmountForDecimals(data.borrowLimitUtilization, debtAsset.decimals),
|
|
305
|
+
maxBorrowLimit: getEthAmountForDecimals(data.maxBorrowLimit, debtAsset.decimals),
|
|
306
|
+
baseBorrowLimit: getEthAmountForDecimals(data.baseBorrowLimit, debtAsset.decimals),
|
|
307
|
+
minimumBorrowing: getEthAmountForDecimals(data.minimumBorrowing, debtAsset.decimals),
|
|
308
|
+
liquidationMaxLimit,
|
|
309
|
+
borrowRate,
|
|
310
|
+
supplyRate: '0',
|
|
311
|
+
totalSupplyToken0,
|
|
312
|
+
totalSupplyToken1,
|
|
313
|
+
withdrawableToken0,
|
|
314
|
+
withdrawableToken1,
|
|
315
|
+
withdrawableUSD,
|
|
316
|
+
withdrawable: withdrawableShares,
|
|
317
|
+
widthdrawableDex: new Dec(maxSupplyShares).minus(totalSupplyShares).toString(),
|
|
318
|
+
maxSupplyShares,
|
|
319
|
+
collDexFee: supplyDexFee,
|
|
320
|
+
};
|
|
321
|
+
return {
|
|
322
|
+
assetsData,
|
|
323
|
+
marketData,
|
|
324
|
+
};
|
|
325
|
+
});
|
|
326
|
+
const parseT3MarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0, void 0, void 0, function* () {
|
|
327
|
+
const collAsset = getAssetInfoByAddress(data.supplyToken0, network);
|
|
328
|
+
const debtAsset0 = getAssetInfoByAddress(data.borrowToken0, network);
|
|
329
|
+
const debtAsset1 = getAssetInfoByAddress(data.borrowToken1, network);
|
|
330
|
+
const { borrowableShares, maxBorrowShares, borrowDexFee, utilizationBorrow0, utilizationBorrow1, borrowable0, borrowable1, borrowRate0, borrowRate1, totalBorrowShares, token0PerBorrowShare, token1PerBorrowShare, borrowableToken0, borrowableToken1, totalBorrowToken0, totalBorrowToken1, reservesBorrowToken0, reservesBorrowToken1, } = parseDexBorrowData(data.dexBorrowData, debtAsset0.symbol, debtAsset1.symbol);
|
|
331
|
+
// 18 because debt is represented in shares for which they use 18 decimals
|
|
332
|
+
const oracleScaleFactor = new Dec(27).add(18).sub(collAsset.decimals).toString();
|
|
333
|
+
const oracleScale = new Dec(10).pow(oracleScaleFactor).toString();
|
|
334
|
+
const oraclePrice = new Dec(1).div(new Dec(data.oraclePriceOperate).div(oracleScale)).toString();
|
|
335
|
+
const prices = yield getChainLinkPricesForTokens([collAsset.address, debtAsset0.address, debtAsset1.address], network, web3);
|
|
336
|
+
const supplyRate = new Dec(data.supplyRateVault).div(100).toString();
|
|
337
|
+
const collAssetData = {
|
|
338
|
+
symbol: collAsset.symbol,
|
|
339
|
+
address: collAsset.address,
|
|
340
|
+
price: prices[collAsset.address],
|
|
341
|
+
totalSupply: data.totalSupplyVault,
|
|
342
|
+
canBeSupplied: true,
|
|
343
|
+
supplyRate,
|
|
344
|
+
};
|
|
345
|
+
if (STAKING_ASSETS.includes(collAssetData.symbol)) {
|
|
346
|
+
collAssetData.incentiveSupplyApy = yield getStakingApy(collAsset.symbol, mainnetWeb3);
|
|
347
|
+
collAssetData.incentiveSupplyToken = collAsset.symbol;
|
|
348
|
+
}
|
|
349
|
+
const debtAsset0Data = {
|
|
350
|
+
symbol: debtAsset0.symbol,
|
|
351
|
+
address: debtAsset0.address,
|
|
352
|
+
price: prices[debtAsset0.address],
|
|
353
|
+
totalBorrow: new Dec(totalBorrowShares).mul(token0PerBorrowShare).toString(),
|
|
354
|
+
canBeBorrowed: true,
|
|
355
|
+
borrowRate: borrowRate0,
|
|
356
|
+
borrowable: borrowable0,
|
|
357
|
+
utilization: utilizationBorrow0,
|
|
358
|
+
tokenPerBorrowShare: token0PerBorrowShare,
|
|
359
|
+
borrowReserves: reservesBorrowToken0,
|
|
360
|
+
};
|
|
361
|
+
if (STAKING_ASSETS.includes(debtAsset0Data.symbol)) {
|
|
362
|
+
debtAsset0Data.incentiveSupplyApy = yield getStakingApy(debtAsset0.symbol, mainnetWeb3);
|
|
363
|
+
debtAsset0Data.incentiveSupplyToken = debtAsset0.symbol;
|
|
364
|
+
}
|
|
365
|
+
const debtAsset1Data = {
|
|
366
|
+
symbol: debtAsset1.symbol,
|
|
367
|
+
address: debtAsset1.address,
|
|
368
|
+
price: prices[debtAsset1.address],
|
|
369
|
+
totalBorrow: new Dec(totalBorrowShares).mul(token1PerBorrowShare).toString(),
|
|
370
|
+
canBeBorrowed: true,
|
|
371
|
+
borrowRate: borrowRate1,
|
|
372
|
+
borrowable: borrowable1,
|
|
373
|
+
utilization: utilizationBorrow1,
|
|
374
|
+
tokenPerBorrowShare: token1PerBorrowShare,
|
|
375
|
+
borrowReserves: reservesBorrowToken1,
|
|
376
|
+
};
|
|
377
|
+
if (STAKING_ASSETS.includes(debtAsset1Data.symbol)) {
|
|
378
|
+
debtAsset1Data.incentiveSupplyApy = yield getStakingApy(debtAsset1.symbol, mainnetWeb3);
|
|
379
|
+
debtAsset1Data.incentiveSupplyToken = debtAsset1.symbol;
|
|
380
|
+
}
|
|
381
|
+
const assetsData = [
|
|
382
|
+
[collAsset.symbol, collAssetData],
|
|
383
|
+
[debtAsset0.symbol, debtAsset0Data],
|
|
384
|
+
[debtAsset1.symbol, debtAsset1Data],
|
|
385
|
+
]
|
|
386
|
+
.reduce((acc, [symbol, partialData]) => (Object.assign(Object.assign({}, acc), { [symbol]: mergeAssetData(acc[symbol], partialData) })), {});
|
|
387
|
+
const marketInfo = getFluidMarketInfoById(+data.vaultId, network);
|
|
388
|
+
const totalSupplyVault = getEthAmountForDecimals(data.totalSupplyVault, collAsset.decimals);
|
|
389
|
+
const liqRatio = new Dec(data.liquidationThreshold).div(100).toString();
|
|
390
|
+
const liquidationMaxLimit = new Dec(data.liquidationMaxLimit).div(100).toString();
|
|
391
|
+
const liqFactor = new Dec(data.liquidationThreshold).div(10000).toString();
|
|
392
|
+
const debtSharePrice = new Dec(oraclePrice).mul(prices[collAsset.address]).toString();
|
|
393
|
+
const totalBorrowSharesInVault = assetAmountInEth(data.totalBorrowVault);
|
|
394
|
+
const totalBorrowVaultUsd = new Dec(totalBorrowSharesInVault).mul(debtSharePrice).toString();
|
|
395
|
+
const borrowableUSD = new Dec(borrowableShares).mul(debtSharePrice).toString();
|
|
396
|
+
const marketData = {
|
|
397
|
+
vaultId: +data.vaultId,
|
|
398
|
+
vaultValue: marketInfo === null || marketInfo === void 0 ? void 0 : marketInfo.value,
|
|
399
|
+
isSmartColl: data.isSmartColl,
|
|
400
|
+
isSmartDebt: data.isSmartDebt,
|
|
401
|
+
marketAddress: data.vault,
|
|
402
|
+
vaultType: parseVaultType(+data.vaultType),
|
|
403
|
+
oracle: data.oracle,
|
|
404
|
+
liquidationPenaltyPercent: new Dec(data.liquidationPenalty).div(100).toString(),
|
|
405
|
+
collFactor: new Dec(data.collateralFactor).div(10000).toString(),
|
|
406
|
+
liquidationRatio: liqRatio,
|
|
407
|
+
liqFactor,
|
|
408
|
+
minRatio: new Dec(1).div(liqFactor).mul(100).toString(),
|
|
409
|
+
collAsset0: collAsset.symbol,
|
|
410
|
+
debtAsset0: debtAsset0.symbol,
|
|
411
|
+
debtAsset1: debtAsset1.symbol,
|
|
412
|
+
totalPositions: data.totalPositions,
|
|
413
|
+
totalSupplyVault,
|
|
414
|
+
totalBorrowVault: totalBorrowShares,
|
|
415
|
+
totalSupplyVaultUsd: new Dec(totalSupplyVault).mul(assetsData[collAsset.symbol].price).toString(),
|
|
416
|
+
totalBorrowVaultUsd,
|
|
417
|
+
withdrawalLimit: getEthAmountForDecimals(data.withdrawalLimit, collAsset.decimals),
|
|
418
|
+
withdrawableUntilLimit: getEthAmountForDecimals(data.withdrawableUntilLimit, collAsset.decimals),
|
|
419
|
+
withdrawable: getEthAmountForDecimals(data.withdrawable, collAsset.decimals),
|
|
420
|
+
liquidationMaxLimit,
|
|
421
|
+
borrowRate: '0',
|
|
422
|
+
supplyRate,
|
|
423
|
+
borrowableToken0,
|
|
424
|
+
borrowableToken1,
|
|
425
|
+
totalBorrowToken0,
|
|
426
|
+
totalBorrowToken1,
|
|
427
|
+
borrowableUSD,
|
|
428
|
+
borrowable: borrowableShares,
|
|
429
|
+
borrowableDex: new Dec(maxBorrowShares).minus(totalBorrowShares).toString(),
|
|
430
|
+
maxBorrowShares,
|
|
431
|
+
borrowDexFee,
|
|
432
|
+
debtSharePrice,
|
|
433
|
+
};
|
|
434
|
+
return {
|
|
435
|
+
assetsData,
|
|
436
|
+
marketData,
|
|
437
|
+
};
|
|
438
|
+
});
|
|
439
|
+
const parseT4MarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0, void 0, void 0, function* () {
|
|
440
|
+
const collAsset0 = getAssetInfoByAddress(data.supplyToken0, network);
|
|
441
|
+
const collAsset1 = getAssetInfoByAddress(data.supplyToken1, network);
|
|
442
|
+
const debtAsset0 = getAssetInfoByAddress(data.borrowToken0, network);
|
|
443
|
+
const debtAsset1 = getAssetInfoByAddress(data.borrowToken1, network);
|
|
444
|
+
const quoteToken = getAssetInfoByAddress(data.dexBorrowData.quoteToken, network);
|
|
445
|
+
// 27 - 18 + 18
|
|
446
|
+
const oracleScaleFactor = new Dec(27).toString();
|
|
447
|
+
const oracleScale = new Dec(10).pow(oracleScaleFactor).toString();
|
|
448
|
+
const oraclePrice = new Dec(data.oraclePriceOperate).div(oracleScale).toString();
|
|
449
|
+
const prices = yield getChainLinkPricesForTokens([collAsset0.address, collAsset1.address, debtAsset0.address, debtAsset1.address], network, web3);
|
|
450
|
+
const { supplyDexFee, totalSupplyShares, supplyRate1, token0PerSupplyShare, token1PerSupplyShare, totalSupplyToken0, totalSupplyToken1, maxSupplyShares, withdrawableToken0, withdrawable0, withdrawableToken1, withdrawable1, supplyRate0, utilizationSupply0, utilizationSupply1, withdrawableShares, reservesSupplyToken0, reservesSupplyToken1, } = parseDexSupplyData(data.dexSupplyData, collAsset0.symbol, collAsset1.symbol);
|
|
451
|
+
const { borrowableShares, maxBorrowShares, borrowDexFee, utilizationBorrow0, utilizationBorrow1, borrowable0, borrowable1, borrowRate0, borrowRate1, totalBorrowShares, token0PerBorrowShare, token1PerBorrowShare, borrowableToken0, borrowableToken1, totalBorrowToken0, totalBorrowToken1, quoteTokensPerShare, reservesBorrowToken0, reservesBorrowToken1, } = parseDexBorrowData(data.dexBorrowData, debtAsset0.symbol, debtAsset1.symbol);
|
|
452
|
+
const collAsset0Data = {
|
|
453
|
+
symbol: collAsset0.symbol,
|
|
454
|
+
address: collAsset0.address,
|
|
455
|
+
price: prices[collAsset0.address],
|
|
456
|
+
totalSupply: new Dec(totalSupplyShares).mul(token0PerSupplyShare).toString(),
|
|
457
|
+
canBeSupplied: true,
|
|
458
|
+
supplyRate: supplyRate0,
|
|
459
|
+
utilization: utilizationSupply0,
|
|
460
|
+
withdrawable: withdrawable0,
|
|
461
|
+
tokenPerSupplyShare: token0PerSupplyShare,
|
|
462
|
+
supplyReserves: reservesSupplyToken0,
|
|
463
|
+
};
|
|
464
|
+
if (STAKING_ASSETS.includes(collAsset0Data.symbol)) {
|
|
465
|
+
collAsset0Data.incentiveSupplyApy = yield getStakingApy(collAsset0.symbol, mainnetWeb3);
|
|
466
|
+
collAsset0Data.incentiveSupplyToken = collAsset0.symbol;
|
|
467
|
+
}
|
|
468
|
+
const collAsset1Data = {
|
|
469
|
+
symbol: collAsset1.symbol,
|
|
470
|
+
address: collAsset1.address,
|
|
471
|
+
price: prices[collAsset1.address],
|
|
472
|
+
totalSupply: new Dec(totalSupplyShares).mul(token1PerSupplyShare).toString(),
|
|
473
|
+
canBeSupplied: true,
|
|
474
|
+
supplyRate: supplyRate1,
|
|
475
|
+
withdrawable: withdrawable1,
|
|
476
|
+
utilization: utilizationSupply1,
|
|
477
|
+
tokenPerSupplyShare: token1PerSupplyShare,
|
|
478
|
+
supplyReserves: reservesSupplyToken1,
|
|
479
|
+
};
|
|
480
|
+
if (STAKING_ASSETS.includes(collAsset1Data.symbol)) {
|
|
481
|
+
collAsset1Data.incentiveSupplyApy = yield getStakingApy(collAsset1.symbol, mainnetWeb3);
|
|
482
|
+
collAsset1Data.incentiveSupplyToken = collAsset1.symbol;
|
|
483
|
+
}
|
|
484
|
+
const debtAsset0Data = {
|
|
485
|
+
symbol: debtAsset0.symbol,
|
|
486
|
+
address: debtAsset0.address,
|
|
487
|
+
price: prices[debtAsset0.address],
|
|
488
|
+
totalBorrow: new Dec(totalBorrowShares).mul(token0PerBorrowShare).toString(),
|
|
489
|
+
canBeBorrowed: true,
|
|
490
|
+
borrowRate: borrowRate0,
|
|
491
|
+
borrowable: borrowable0,
|
|
492
|
+
utilization: utilizationBorrow0,
|
|
493
|
+
tokenPerBorrowShare: token0PerBorrowShare,
|
|
494
|
+
borrowReserves: reservesBorrowToken0,
|
|
495
|
+
};
|
|
496
|
+
if (STAKING_ASSETS.includes(debtAsset0Data.symbol)) {
|
|
497
|
+
debtAsset0Data.incentiveSupplyApy = yield getStakingApy(debtAsset0.symbol, mainnetWeb3);
|
|
498
|
+
debtAsset0Data.incentiveSupplyToken = debtAsset0.symbol;
|
|
499
|
+
}
|
|
500
|
+
const debtAsset1Data = {
|
|
501
|
+
symbol: debtAsset1.symbol,
|
|
502
|
+
address: debtAsset1.address,
|
|
503
|
+
price: prices[debtAsset1.address],
|
|
504
|
+
totalBorrow: new Dec(totalBorrowShares).mul(token1PerBorrowShare).toString(),
|
|
505
|
+
canBeBorrowed: true,
|
|
506
|
+
borrowRate: borrowRate1,
|
|
507
|
+
borrowable: borrowable1,
|
|
508
|
+
utilization: utilizationBorrow1,
|
|
509
|
+
tokenPerBorrowShare: token1PerBorrowShare,
|
|
510
|
+
borrowReserves: reservesBorrowToken1,
|
|
511
|
+
};
|
|
512
|
+
if (STAKING_ASSETS.includes(debtAsset1Data.symbol)) {
|
|
513
|
+
debtAsset1Data.incentiveSupplyApy = yield getStakingApy(debtAsset1.symbol, mainnetWeb3);
|
|
514
|
+
debtAsset1Data.incentiveSupplyToken = debtAsset1.symbol;
|
|
515
|
+
}
|
|
516
|
+
const assetsData = [
|
|
517
|
+
[collAsset0.symbol, collAsset0Data],
|
|
518
|
+
[collAsset1.symbol, collAsset1Data],
|
|
519
|
+
[debtAsset0.symbol, debtAsset0Data],
|
|
520
|
+
[debtAsset1.symbol, debtAsset1Data],
|
|
521
|
+
]
|
|
522
|
+
.reduce((acc, [symbol, partialData]) => (Object.assign(Object.assign({}, acc), { [symbol]: mergeAssetData(acc[symbol], partialData) })), {});
|
|
523
|
+
const marketInfo = getFluidMarketInfoById(+data.vaultId, network);
|
|
524
|
+
const liqRatio = new Dec(data.liquidationThreshold).div(100).toString();
|
|
525
|
+
const liquidationMaxLimit = new Dec(data.liquidationMaxLimit).div(100).toString();
|
|
526
|
+
const liqFactor = new Dec(data.liquidationThreshold).div(10000).toString();
|
|
527
|
+
const totalBorrowSharesInVault = assetAmountInEth(data.totalBorrowVault);
|
|
528
|
+
const debtSharePrice = new Dec(quoteTokensPerShare).mul(prices[quoteToken.address]).toString();
|
|
529
|
+
const totalBorrowVaultUsd = new Dec(totalBorrowSharesInVault).mul(debtSharePrice).toString();
|
|
530
|
+
const borrowableUSD = new Dec(borrowableShares).mul(debtSharePrice).toString();
|
|
531
|
+
const totalSupplySharesInVault = assetAmountInEth(data.totalSupplyVault);
|
|
532
|
+
const collSharePrice = new Dec(oraclePrice).mul(debtSharePrice).toString();
|
|
533
|
+
const totalSupplyVaultUsd = new Dec(totalSupplySharesInVault).mul(collSharePrice).toString();
|
|
534
|
+
const withdrawableUSD = new Dec(withdrawableShares).mul(collSharePrice).toString();
|
|
535
|
+
const marketData = {
|
|
536
|
+
vaultId: +data.vaultId,
|
|
537
|
+
vaultValue: marketInfo === null || marketInfo === void 0 ? void 0 : marketInfo.value,
|
|
538
|
+
isSmartColl: data.isSmartColl,
|
|
539
|
+
isSmartDebt: data.isSmartDebt,
|
|
540
|
+
marketAddress: data.vault,
|
|
541
|
+
vaultType: parseVaultType(+data.vaultType),
|
|
542
|
+
oracle: data.oracle,
|
|
543
|
+
liquidationPenaltyPercent: new Dec(data.liquidationPenalty).div(100).toString(),
|
|
544
|
+
collFactor: new Dec(data.collateralFactor).div(10000).toString(),
|
|
545
|
+
liquidationRatio: liqRatio,
|
|
546
|
+
liqFactor,
|
|
547
|
+
minRatio: new Dec(1).div(liqFactor).mul(100).toString(),
|
|
548
|
+
collAsset0: collAsset0.symbol,
|
|
549
|
+
collAsset1: collAsset1.symbol,
|
|
550
|
+
debtAsset0: debtAsset0.symbol,
|
|
551
|
+
debtAsset1: debtAsset1.symbol,
|
|
552
|
+
totalPositions: data.totalPositions,
|
|
553
|
+
totalSupplyVault: totalSupplyShares,
|
|
554
|
+
totalBorrowVault: totalBorrowShares,
|
|
555
|
+
totalSupplyVaultUsd,
|
|
556
|
+
totalBorrowVaultUsd,
|
|
557
|
+
liquidationMaxLimit,
|
|
558
|
+
borrowRate: '0',
|
|
559
|
+
supplyRate: '0',
|
|
560
|
+
borrowableToken0,
|
|
561
|
+
borrowableToken1,
|
|
562
|
+
totalBorrowToken0,
|
|
563
|
+
totalBorrowToken1,
|
|
564
|
+
borrowableUSD,
|
|
565
|
+
borrowable: borrowableShares,
|
|
566
|
+
borrowableDex: new Dec(maxBorrowShares).minus(totalBorrowShares).toString(),
|
|
567
|
+
maxBorrowShares,
|
|
568
|
+
borrowDexFee,
|
|
569
|
+
totalSupplyToken0,
|
|
570
|
+
totalSupplyToken1,
|
|
571
|
+
withdrawableToken0,
|
|
572
|
+
withdrawableToken1,
|
|
573
|
+
withdrawableUSD,
|
|
574
|
+
withdrawable: withdrawableShares,
|
|
575
|
+
widthdrawableDex: new Dec(maxSupplyShares).minus(totalSupplyShares).toString(),
|
|
576
|
+
maxSupplyShares,
|
|
577
|
+
collDexFee: supplyDexFee,
|
|
578
|
+
collSharePrice,
|
|
579
|
+
debtSharePrice,
|
|
580
|
+
};
|
|
581
|
+
return {
|
|
582
|
+
assetsData,
|
|
583
|
+
marketData,
|
|
584
|
+
};
|
|
585
|
+
});
|
|
586
|
+
const parseMarketData = (web3, data, network, mainnetWeb3) => __awaiter(void 0, void 0, void 0, function* () {
|
|
587
|
+
const vaultType = parseVaultType(+data.vaultType);
|
|
588
|
+
switch (vaultType) {
|
|
589
|
+
case FluidVaultType.T1:
|
|
590
|
+
return parseT1MarketData(web3, data, network, mainnetWeb3);
|
|
591
|
+
case FluidVaultType.T2:
|
|
592
|
+
return parseT2MarketData(web3, data, network, mainnetWeb3);
|
|
593
|
+
case FluidVaultType.T3:
|
|
594
|
+
return parseT3MarketData(web3, data, network, mainnetWeb3);
|
|
595
|
+
case FluidVaultType.T4:
|
|
596
|
+
return parseT4MarketData(web3, data, network, mainnetWeb3);
|
|
597
|
+
default:
|
|
598
|
+
throw new Error(`Unknown vault type: ${vaultType}`);
|
|
599
|
+
}
|
|
600
|
+
});
|
|
147
601
|
export const EMPTY_FLUID_DATA = {
|
|
148
602
|
usedAssets: {},
|
|
149
603
|
suppliedUsd: '0',
|
|
@@ -159,11 +613,12 @@ export const EMPTY_FLUID_DATA = {
|
|
|
159
613
|
automationResubscribeRequired: false,
|
|
160
614
|
lastUpdated: Date.now(),
|
|
161
615
|
};
|
|
162
|
-
const
|
|
616
|
+
const parseT1UserData = (userPositionData, vaultData) => {
|
|
163
617
|
const { assetsData, marketData, } = vaultData;
|
|
164
618
|
const payload = Object.assign(Object.assign({ owner: userPositionData.owner, vaultId: marketData.vaultId }, EMPTY_FLUID_DATA), { lastUpdated: Date.now() });
|
|
165
619
|
const collAsset = getAssetInfo(marketData.collAsset0);
|
|
166
620
|
const debtAsset = getAssetInfo(marketData.debtAsset0);
|
|
621
|
+
// for T2 and T4 - this is the number of shares
|
|
167
622
|
const supplied = getEthAmountForDecimals(userPositionData.supply, collAsset.decimals);
|
|
168
623
|
const borrowed = getEthAmountForDecimals(userPositionData.borrow, debtAsset.decimals);
|
|
169
624
|
const collUsedAsset = Object.assign(Object.assign({}, EMPTY_USED_ASSET), { symbol: collAsset.symbol, collateral: true, supplied, suppliedUsd: new Dec(supplied).mul(assetsData[collAsset.symbol].price).toString(), isSupplied: new Dec(supplied).gt(0) });
|
|
@@ -178,6 +633,174 @@ const parseUserData = (userPositionData, vaultData) => {
|
|
|
178
633
|
marketData,
|
|
179
634
|
}));
|
|
180
635
|
};
|
|
636
|
+
const parseT2UserData = (userPositionData, vaultData) => {
|
|
637
|
+
const { assetsData, marketData, } = vaultData;
|
|
638
|
+
const payload = Object.assign(Object.assign({ owner: userPositionData.owner, vaultId: marketData.vaultId }, EMPTY_FLUID_DATA), { lastUpdated: Date.now() });
|
|
639
|
+
const collAsset0 = getAssetInfo(marketData.collAsset0);
|
|
640
|
+
const collAsset1 = getAssetInfo(marketData.collAsset1);
|
|
641
|
+
const debtAsset = getAssetInfo(marketData.debtAsset0);
|
|
642
|
+
const supplyShares = getEthAmountForDecimals(userPositionData.supply, 18); // this is supplied in coll shares
|
|
643
|
+
const borrowed = getEthAmountForDecimals(userPositionData.borrow, debtAsset.decimals); // this is actual token borrow
|
|
644
|
+
const supplied0 = new Dec(supplyShares).mul(assetsData[collAsset0.symbol].tokenPerSupplyShare).toString();
|
|
645
|
+
const supplied1 = new Dec(supplyShares).mul(assetsData[collAsset1.symbol].tokenPerSupplyShare).toString();
|
|
646
|
+
const collUsedAsset0 = {
|
|
647
|
+
symbol: collAsset0.symbol,
|
|
648
|
+
collateral: true,
|
|
649
|
+
supplied: supplied0,
|
|
650
|
+
suppliedUsd: new Dec(supplied0).mul(assetsData[collAsset0.symbol].price).toString(),
|
|
651
|
+
isSupplied: new Dec(supplied0).gt(0),
|
|
652
|
+
};
|
|
653
|
+
const collUsedAsset1 = {
|
|
654
|
+
symbol: collAsset1.symbol,
|
|
655
|
+
collateral: true,
|
|
656
|
+
supplied: supplied1,
|
|
657
|
+
suppliedUsd: new Dec(supplied1).mul(assetsData[collAsset1.symbol].price).toString(),
|
|
658
|
+
isSupplied: new Dec(supplied1).gt(0),
|
|
659
|
+
};
|
|
660
|
+
const debtUsedAsset = {
|
|
661
|
+
symbol: debtAsset.symbol,
|
|
662
|
+
collateral: false,
|
|
663
|
+
borrowed,
|
|
664
|
+
borrowedUsd: new Dec(borrowed).mul(assetsData[debtAsset.symbol].price).toString(),
|
|
665
|
+
isBorrowed: new Dec(borrowed).gt(0),
|
|
666
|
+
};
|
|
667
|
+
const usedAssets = [
|
|
668
|
+
[collAsset0.symbol, collUsedAsset0],
|
|
669
|
+
[collAsset1.symbol, collUsedAsset1],
|
|
670
|
+
[debtAsset.symbol, debtUsedAsset],
|
|
671
|
+
]
|
|
672
|
+
.reduce((acc, [symbol, partialData]) => {
|
|
673
|
+
acc[symbol] = mergeUsedAssets(acc[symbol], partialData);
|
|
674
|
+
return acc;
|
|
675
|
+
}, {});
|
|
676
|
+
return Object.assign(Object.assign(Object.assign({}, payload), { usedAssets,
|
|
677
|
+
supplyShares }), getFluidAggregatedData({
|
|
678
|
+
usedAssets,
|
|
679
|
+
assetsData,
|
|
680
|
+
marketData,
|
|
681
|
+
}, supplyShares));
|
|
682
|
+
};
|
|
683
|
+
const parseT3UserData = (userPositionData, vaultData) => {
|
|
684
|
+
const { assetsData, marketData, } = vaultData;
|
|
685
|
+
const payload = Object.assign(Object.assign({ owner: userPositionData.owner, vaultId: marketData.vaultId }, EMPTY_FLUID_DATA), { lastUpdated: Date.now() });
|
|
686
|
+
const collAsset = getAssetInfo(marketData.collAsset0);
|
|
687
|
+
const debtAsset0 = getAssetInfo(marketData.debtAsset0);
|
|
688
|
+
const debtAsset1 = getAssetInfo(marketData.debtAsset1);
|
|
689
|
+
const supplied = getEthAmountForDecimals(userPositionData.supply, collAsset.decimals); // this is actual token supply
|
|
690
|
+
const borrowShares = getEthAmountForDecimals(userPositionData.borrow, 18); // this is actual token borrow
|
|
691
|
+
const borrowed0 = new Dec(borrowShares).mul(assetsData[debtAsset0.symbol].tokenPerBorrowShare).toString();
|
|
692
|
+
const borrowed1 = new Dec(borrowShares).mul(assetsData[debtAsset1.symbol].tokenPerBorrowShare).toString();
|
|
693
|
+
const collUsedAsset = {
|
|
694
|
+
symbol: collAsset.symbol,
|
|
695
|
+
collateral: true,
|
|
696
|
+
supplied,
|
|
697
|
+
suppliedUsd: new Dec(supplied).mul(assetsData[collAsset.symbol].price).toString(),
|
|
698
|
+
isSupplied: new Dec(supplied).gt(0),
|
|
699
|
+
};
|
|
700
|
+
const debtUsedAsset0 = {
|
|
701
|
+
symbol: debtAsset0.symbol,
|
|
702
|
+
collateral: false,
|
|
703
|
+
borrowed: borrowed0,
|
|
704
|
+
borrowedUsd: new Dec(borrowed0).mul(assetsData[debtAsset0.symbol].price).toString(),
|
|
705
|
+
isBorrowed: new Dec(borrowed0).gt(0),
|
|
706
|
+
};
|
|
707
|
+
const debtUsedAsset1 = {
|
|
708
|
+
symbol: debtAsset1.symbol,
|
|
709
|
+
collateral: false,
|
|
710
|
+
borrowed: borrowed1,
|
|
711
|
+
borrowedUsd: new Dec(borrowed1).mul(assetsData[debtAsset1.symbol].price).toString(),
|
|
712
|
+
isBorrowed: new Dec(borrowed1).gt(0),
|
|
713
|
+
};
|
|
714
|
+
const usedAssets = [
|
|
715
|
+
[collAsset.symbol, collUsedAsset],
|
|
716
|
+
[debtAsset0.symbol, debtUsedAsset0],
|
|
717
|
+
[debtAsset1.symbol, debtUsedAsset1],
|
|
718
|
+
]
|
|
719
|
+
.reduce((acc, [symbol, partialData]) => {
|
|
720
|
+
acc[symbol] = mergeUsedAssets(acc[symbol], partialData);
|
|
721
|
+
return acc;
|
|
722
|
+
}, {});
|
|
723
|
+
return Object.assign(Object.assign(Object.assign({}, payload), { usedAssets,
|
|
724
|
+
borrowShares }), getFluidAggregatedData({
|
|
725
|
+
usedAssets,
|
|
726
|
+
assetsData,
|
|
727
|
+
marketData,
|
|
728
|
+
}, '', borrowShares));
|
|
729
|
+
};
|
|
730
|
+
const parseT4UserData = (userPositionData, vaultData) => {
|
|
731
|
+
const { assetsData, marketData, } = vaultData;
|
|
732
|
+
const payload = Object.assign(Object.assign({ owner: userPositionData.owner, vaultId: marketData.vaultId }, EMPTY_FLUID_DATA), { lastUpdated: Date.now() });
|
|
733
|
+
const collAsset0 = getAssetInfo(marketData.collAsset0);
|
|
734
|
+
const collAsset1 = getAssetInfo(marketData.collAsset1);
|
|
735
|
+
const debtAsset0 = getAssetInfo(marketData.debtAsset0);
|
|
736
|
+
const debtAsset1 = getAssetInfo(marketData.debtAsset1);
|
|
737
|
+
const supplyShares = getEthAmountForDecimals(userPositionData.supply, 18); // this is actual token supply
|
|
738
|
+
const borrowShares = getEthAmountForDecimals(userPositionData.borrow, 18); // this is actual token borrow
|
|
739
|
+
const supplied0 = new Dec(supplyShares).mul(assetsData[collAsset0.symbol].tokenPerSupplyShare).toString();
|
|
740
|
+
const supplied1 = new Dec(supplyShares).mul(assetsData[collAsset1.symbol].tokenPerSupplyShare).toString();
|
|
741
|
+
const borrowed0 = new Dec(borrowShares).mul(assetsData[debtAsset0.symbol].tokenPerBorrowShare).toString();
|
|
742
|
+
const borrowed1 = new Dec(borrowShares).mul(assetsData[debtAsset1.symbol].tokenPerBorrowShare).toString();
|
|
743
|
+
const collUsedAsset0 = {
|
|
744
|
+
symbol: collAsset0.symbol,
|
|
745
|
+
collateral: true,
|
|
746
|
+
supplied: supplied0,
|
|
747
|
+
suppliedUsd: new Dec(supplied0).mul(assetsData[collAsset0.symbol].price).toString(),
|
|
748
|
+
isSupplied: new Dec(supplied0).gt(0),
|
|
749
|
+
};
|
|
750
|
+
const collUsedAsset1 = {
|
|
751
|
+
symbol: collAsset1.symbol,
|
|
752
|
+
collateral: true,
|
|
753
|
+
supplied: supplied1,
|
|
754
|
+
suppliedUsd: new Dec(supplied1).mul(assetsData[collAsset1.symbol].price).toString(),
|
|
755
|
+
isSupplied: new Dec(supplied1).gt(0),
|
|
756
|
+
};
|
|
757
|
+
const debtUsedAsset0 = {
|
|
758
|
+
symbol: debtAsset0.symbol,
|
|
759
|
+
collateral: false,
|
|
760
|
+
borrowed: borrowed0,
|
|
761
|
+
borrowedUsd: new Dec(borrowed0).mul(assetsData[debtAsset0.symbol].price).toString(),
|
|
762
|
+
isBorrowed: new Dec(borrowed0).gt(0),
|
|
763
|
+
};
|
|
764
|
+
const debtUsedAsset1 = {
|
|
765
|
+
symbol: debtAsset1.symbol,
|
|
766
|
+
collateral: false,
|
|
767
|
+
borrowed: borrowed1,
|
|
768
|
+
borrowedUsd: new Dec(borrowed1).mul(assetsData[debtAsset1.symbol].price).toString(),
|
|
769
|
+
isBorrowed: new Dec(borrowed1).gt(0),
|
|
770
|
+
};
|
|
771
|
+
const usedAssets = [
|
|
772
|
+
[collAsset0.symbol, collUsedAsset0],
|
|
773
|
+
[collAsset1.symbol, collUsedAsset1],
|
|
774
|
+
[debtAsset0.symbol, debtUsedAsset0],
|
|
775
|
+
[debtAsset1.symbol, debtUsedAsset1],
|
|
776
|
+
]
|
|
777
|
+
.reduce((acc, [symbol, partialData]) => {
|
|
778
|
+
acc[symbol] = mergeUsedAssets(acc[symbol], partialData);
|
|
779
|
+
return acc;
|
|
780
|
+
}, {});
|
|
781
|
+
return Object.assign(Object.assign(Object.assign({}, payload), { usedAssets,
|
|
782
|
+
supplyShares,
|
|
783
|
+
borrowShares }), getFluidAggregatedData({
|
|
784
|
+
usedAssets,
|
|
785
|
+
assetsData,
|
|
786
|
+
marketData,
|
|
787
|
+
}, supplyShares, borrowShares));
|
|
788
|
+
};
|
|
789
|
+
const parseUserData = (userPositionData, vaultData) => {
|
|
790
|
+
const vaultType = vaultData.marketData.vaultType;
|
|
791
|
+
switch (vaultType) {
|
|
792
|
+
case FluidVaultType.T1:
|
|
793
|
+
return parseT1UserData(userPositionData, vaultData);
|
|
794
|
+
case FluidVaultType.T2:
|
|
795
|
+
return parseT2UserData(userPositionData, vaultData);
|
|
796
|
+
case FluidVaultType.T3:
|
|
797
|
+
return parseT3UserData(userPositionData, vaultData);
|
|
798
|
+
case FluidVaultType.T4:
|
|
799
|
+
return parseT4UserData(userPositionData, vaultData);
|
|
800
|
+
default:
|
|
801
|
+
throw new Error(`Unknown vault type: ${vaultType}`);
|
|
802
|
+
}
|
|
803
|
+
};
|
|
181
804
|
export const getFluidMarketData = (web3, network, market, mainnetWeb3) => __awaiter(void 0, void 0, void 0, function* () {
|
|
182
805
|
const view = FluidViewContract(web3, network);
|
|
183
806
|
const data = yield view.methods.getVaultData(market.marketAddress).call();
|