@defisaver/positions-sdk 2.1.174 → 2.1.175-looping-2-dev-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/cjs/aaveV2/index.d.ts +4 -4
- package/cjs/aaveV2/index.js +7 -7
- package/cjs/aaveV3/index.d.ts +5 -14
- package/cjs/aaveV3/index.js +14 -10
- package/cjs/aaveV3/merkl.d.ts +5 -1
- package/cjs/aaveV3/merkl.js +8 -3
- package/cjs/aaveV3/sgho.d.ts +2 -16
- package/cjs/aaveV4/index.d.ts +4 -3
- package/cjs/aaveV4/index.js +19 -16
- package/cjs/aaveV4/lend.d.ts +2 -43
- package/cjs/aaveV4/merkl.d.ts +10 -2
- package/cjs/aaveV4/merkl.js +15 -6
- package/cjs/claiming/aaveV3.d.ts +3 -3
- package/cjs/claiming/aaveV3.js +5 -4
- package/cjs/claiming/ethena.d.ts +2 -1
- package/cjs/claiming/king.d.ts +3 -2
- package/cjs/claiming/king.js +3 -2
- package/cjs/claiming/spark.d.ts +4 -4
- package/cjs/claiming/spark.js +8 -7
- package/cjs/claiming/uniswap.d.ts +2 -2
- package/cjs/claiming/uniswap.js +3 -2
- package/cjs/compoundV2/index.d.ts +4 -4
- package/cjs/compoundV2/index.js +9 -9
- package/cjs/compoundV3/index.d.ts +5 -4
- package/cjs/compoundV3/index.js +17 -13
- package/cjs/compoundV3/merkl.d.ts +1 -1
- package/cjs/compoundV3/merkl.js +2 -5
- package/cjs/curveUsd/index.d.ts +5 -5
- package/cjs/curveUsd/index.js +17 -17
- package/cjs/fluid/index.d.ts +23 -66
- package/cjs/fluid/index.js +75 -72
- package/cjs/fluid/merkl.d.ts +5 -3
- package/cjs/fluid/merkl.js +11 -6
- package/cjs/helpers/aaveHelpers/index.js +1 -1
- package/cjs/helpers/aaveV4Helpers/index.js +14 -2
- package/cjs/helpers/compoundHelpers/index.js +2 -2
- package/cjs/helpers/curveUsdHelpers/index.js +1 -1
- package/cjs/helpers/fluidHelpers/index.js +1 -1
- package/cjs/helpers/liquityV2Helpers/index.js +1 -1
- package/cjs/helpers/llamaLendHelpers/index.js +1 -1
- package/cjs/helpers/morphoBlueHelpers/index.js +1 -1
- package/cjs/helpers/morphoMidnightHelpers/index.js +1 -1
- package/cjs/helpers/sparkHelpers/index.js +1 -1
- package/cjs/liquity/index.d.ts +5 -15
- package/cjs/liquity/index.js +23 -23
- package/cjs/liquityV2/index.d.ts +16 -31
- package/cjs/liquityV2/index.js +96 -78
- package/cjs/llamaLend/index.d.ts +5 -5
- package/cjs/llamaLend/index.js +13 -13
- package/cjs/maker/index.d.ts +6 -6
- package/cjs/maker/index.js +16 -16
- package/cjs/moneymarket/moneymarketCommonService.js +13 -2
- package/cjs/morphoBlue/index.d.ts +7 -7
- package/cjs/morphoBlue/index.js +28 -26
- package/cjs/morphoBlue/merkl.d.ts +5 -3
- package/cjs/morphoBlue/merkl.js +6 -8
- package/cjs/morphoMidnight/index.d.ts +5 -5
- package/cjs/morphoMidnight/index.js +18 -18
- package/cjs/portfolio/customDeFiData.d.ts +3 -0
- package/cjs/portfolio/customDeFiData.js +267 -0
- package/cjs/portfolio/index.d.ts +17 -8
- package/cjs/portfolio/index.js +459 -520
- package/cjs/portfolio/lendingProtocols.d.ts +184 -0
- package/cjs/portfolio/lendingProtocols.js +196 -0
- package/cjs/savings/index.d.ts +8 -2
- package/cjs/savings/index.js +33 -4
- package/cjs/services/merkl.d.ts +46 -2
- package/cjs/services/merkl.js +190 -19
- package/cjs/services/priceService.d.ts +12 -12
- package/cjs/services/priceService.js +30 -29
- package/cjs/services/viem.d.ts +10 -2
- package/cjs/services/viem.js +27 -1
- package/cjs/spark/index.d.ts +4 -4
- package/cjs/spark/index.js +9 -9
- package/cjs/types/aave.d.ts +98 -2
- package/cjs/types/aaveV4.d.ts +45 -0
- package/cjs/types/common.d.ts +1 -0
- package/cjs/types/common.js +1 -0
- package/cjs/types/compound.d.ts +26 -2
- package/cjs/types/curveUsd.d.ts +9 -1
- package/cjs/types/fluid.d.ts +13 -0
- package/cjs/types/index.d.ts +1 -0
- package/cjs/types/index.js +1 -0
- package/cjs/types/liquity.d.ts +12 -0
- package/cjs/types/liquityV2.d.ts +24 -0
- package/cjs/types/llamaLend.d.ts +12 -1
- package/cjs/types/merkl.d.ts +18 -0
- package/cjs/types/morphoBlue.d.ts +2 -0
- package/cjs/types/portfolio.d.ts +162 -5
- package/cjs/types/portfolio.js +18 -0
- package/cjs/types/savings/index.d.ts +9 -1
- package/cjs/types/spark.d.ts +21 -1
- package/cjs/umbrella/index.d.ts +3 -4
- package/cjs/umbrella/index.js +5 -4
- package/esm/aaveV2/index.d.ts +4 -4
- package/esm/aaveV2/index.js +8 -8
- package/esm/aaveV3/index.d.ts +5 -14
- package/esm/aaveV3/index.js +15 -11
- package/esm/aaveV3/merkl.d.ts +5 -1
- package/esm/aaveV3/merkl.js +9 -4
- package/esm/aaveV3/sgho.d.ts +2 -16
- package/esm/aaveV4/index.d.ts +4 -3
- package/esm/aaveV4/index.js +20 -17
- package/esm/aaveV4/lend.d.ts +2 -43
- package/esm/aaveV4/merkl.d.ts +10 -2
- package/esm/aaveV4/merkl.js +16 -7
- package/esm/claiming/aaveV3.d.ts +3 -3
- package/esm/claiming/aaveV3.js +6 -5
- package/esm/claiming/ethena.d.ts +2 -1
- package/esm/claiming/king.d.ts +3 -2
- package/esm/claiming/king.js +3 -2
- package/esm/claiming/spark.d.ts +4 -4
- package/esm/claiming/spark.js +9 -8
- package/esm/claiming/uniswap.d.ts +2 -2
- package/esm/claiming/uniswap.js +3 -2
- package/esm/compoundV2/index.d.ts +4 -4
- package/esm/compoundV2/index.js +9 -9
- package/esm/compoundV3/index.d.ts +5 -4
- package/esm/compoundV3/index.js +19 -15
- package/esm/compoundV3/merkl.d.ts +1 -1
- package/esm/compoundV3/merkl.js +3 -6
- package/esm/curveUsd/index.d.ts +5 -5
- package/esm/curveUsd/index.js +17 -17
- package/esm/fluid/index.d.ts +23 -66
- package/esm/fluid/index.js +76 -73
- package/esm/fluid/merkl.d.ts +5 -3
- package/esm/fluid/merkl.js +12 -7
- package/esm/helpers/aaveHelpers/index.js +1 -1
- package/esm/helpers/aaveV4Helpers/index.js +15 -3
- package/esm/helpers/compoundHelpers/index.js +2 -2
- package/esm/helpers/curveUsdHelpers/index.js +1 -1
- package/esm/helpers/fluidHelpers/index.js +1 -1
- package/esm/helpers/liquityV2Helpers/index.js +1 -1
- package/esm/helpers/llamaLendHelpers/index.js +1 -1
- package/esm/helpers/morphoBlueHelpers/index.js +1 -1
- package/esm/helpers/morphoMidnightHelpers/index.js +1 -1
- package/esm/helpers/sparkHelpers/index.js +1 -1
- package/esm/liquity/index.d.ts +5 -15
- package/esm/liquity/index.js +23 -23
- package/esm/liquityV2/index.d.ts +16 -31
- package/esm/liquityV2/index.js +95 -78
- package/esm/llamaLend/index.d.ts +5 -5
- package/esm/llamaLend/index.js +13 -13
- package/esm/maker/index.d.ts +6 -6
- package/esm/maker/index.js +16 -16
- package/esm/moneymarket/moneymarketCommonService.js +13 -2
- package/esm/morphoBlue/index.d.ts +7 -7
- package/esm/morphoBlue/index.js +29 -27
- package/esm/morphoBlue/merkl.d.ts +5 -3
- package/esm/morphoBlue/merkl.js +7 -9
- package/esm/morphoMidnight/index.d.ts +5 -5
- package/esm/morphoMidnight/index.js +18 -18
- package/esm/portfolio/customDeFiData.d.ts +3 -0
- package/esm/portfolio/customDeFiData.js +261 -0
- package/esm/portfolio/index.d.ts +17 -8
- package/esm/portfolio/index.js +459 -522
- package/esm/portfolio/lendingProtocols.d.ts +184 -0
- package/esm/portfolio/lendingProtocols.js +189 -0
- package/esm/savings/index.d.ts +8 -2
- package/esm/savings/index.js +31 -3
- package/esm/services/merkl.d.ts +46 -2
- package/esm/services/merkl.js +180 -18
- package/esm/services/priceService.d.ts +12 -12
- package/esm/services/priceService.js +31 -30
- package/esm/services/viem.d.ts +10 -2
- package/esm/services/viem.js +25 -0
- package/esm/spark/index.d.ts +4 -4
- package/esm/spark/index.js +10 -10
- package/esm/types/aave.d.ts +98 -2
- package/esm/types/aaveV4.d.ts +45 -0
- package/esm/types/common.d.ts +1 -0
- package/esm/types/common.js +1 -0
- package/esm/types/compound.d.ts +26 -2
- package/esm/types/curveUsd.d.ts +9 -1
- package/esm/types/fluid.d.ts +13 -0
- package/esm/types/index.d.ts +1 -0
- package/esm/types/index.js +1 -0
- package/esm/types/liquity.d.ts +12 -0
- package/esm/types/liquityV2.d.ts +24 -0
- package/esm/types/llamaLend.d.ts +12 -1
- package/esm/types/merkl.d.ts +18 -0
- package/esm/types/morphoBlue.d.ts +2 -0
- package/esm/types/portfolio.d.ts +162 -5
- package/esm/types/portfolio.js +17 -1
- package/esm/types/savings/index.d.ts +9 -1
- package/esm/types/spark.d.ts +21 -1
- package/esm/umbrella/index.d.ts +3 -4
- package/esm/umbrella/index.js +5 -4
- package/package.json +1 -1
- package/src/aaveV2/index.ts +7 -7
- package/src/aaveV3/index.ts +16 -11
- package/src/aaveV3/merkl.ts +9 -4
- package/src/aaveV3/sgho.ts +2 -17
- package/src/aaveV4/index.ts +19 -16
- package/src/aaveV4/lend.ts +2 -48
- package/src/aaveV4/merkl.ts +16 -7
- package/src/claiming/aaveV3.ts +9 -5
- package/src/claiming/ethena.ts +5 -5
- package/src/claiming/king.ts +8 -5
- package/src/claiming/spark.ts +15 -10
- package/src/claiming/uniswap.ts +4 -3
- package/src/compoundV2/index.ts +10 -9
- package/src/compoundV3/index.ts +20 -14
- package/src/compoundV3/merkl.ts +5 -4
- package/src/curveUsd/index.ts +19 -17
- package/src/fluid/index.ts +91 -72
- package/src/fluid/merkl.ts +12 -7
- package/src/helpers/aaveHelpers/index.ts +1 -1
- package/src/helpers/aaveV4Helpers/index.ts +16 -2
- package/src/helpers/compoundHelpers/index.ts +2 -2
- package/src/helpers/curveUsdHelpers/index.ts +1 -1
- package/src/helpers/fluidHelpers/index.ts +1 -1
- package/src/helpers/liquityV2Helpers/index.ts +1 -1
- package/src/helpers/llamaLendHelpers/index.ts +1 -1
- package/src/helpers/morphoBlueHelpers/index.ts +1 -1
- package/src/helpers/morphoMidnightHelpers/index.ts +1 -1
- package/src/helpers/sparkHelpers/index.ts +1 -1
- package/src/liquity/index.ts +24 -24
- package/src/liquityV2/index.ts +110 -76
- package/src/llamaLend/index.ts +15 -13
- package/src/maker/index.ts +19 -16
- package/src/moneymarket/moneymarketCommonService.ts +14 -2
- package/src/morphoBlue/index.ts +23 -20
- package/src/morphoBlue/merkl.ts +9 -6
- package/src/morphoMidnight/index.ts +13 -13
- package/src/portfolio/customDeFiData.ts +385 -0
- package/src/portfolio/index.ts +307 -331
- package/src/portfolio/lendingProtocols.ts +281 -0
- package/src/savings/index.ts +41 -4
- package/src/services/merkl.ts +209 -19
- package/src/services/priceService.ts +33 -30
- package/src/services/viem.ts +16 -0
- package/src/spark/index.ts +11 -8
- package/src/types/aave.ts +104 -1
- package/src/types/aaveV4.ts +50 -0
- package/src/types/common.ts +1 -0
- package/src/types/compound.ts +38 -2
- package/src/types/curveUsd.ts +17 -1
- package/src/types/fluid.ts +14 -0
- package/src/types/index.ts +1 -0
- package/src/types/liquity.ts +13 -1
- package/src/types/liquityV2.ts +26 -1
- package/src/types/llamaLend.ts +19 -1
- package/src/types/merkl.ts +20 -0
- package/src/types/morphoBlue.ts +3 -0
- package/src/types/portfolio.ts +191 -5
- package/src/types/savings/index.ts +12 -1
- package/src/types/spark.ts +25 -1
- package/src/umbrella/index.ts +7 -5
package/src/fluid/index.ts
CHANGED
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
FluidAggregatedVaultData,
|
|
12
12
|
FluidAssetData,
|
|
13
13
|
FluidAssetsData,
|
|
14
|
+
FluidEarnPositionData,
|
|
14
15
|
FluidFTokenDataStructOutput,
|
|
15
16
|
FluidMarketData,
|
|
16
17
|
FluidMarketInfo,
|
|
@@ -22,6 +23,7 @@ import {
|
|
|
22
23
|
FluidVaultDataStructOutputStruct,
|
|
23
24
|
FluidVaultType,
|
|
24
25
|
InnerFluidMarketData,
|
|
26
|
+
Blockish,
|
|
25
27
|
} from '../types';
|
|
26
28
|
import {
|
|
27
29
|
BTCPriceFeedContractViem,
|
|
@@ -66,7 +68,7 @@ import {
|
|
|
66
68
|
parseWstUSRPriceCalls,
|
|
67
69
|
} from '../services/priceService';
|
|
68
70
|
import { getStakingApy, STAKING_ASSETS } from '../staking';
|
|
69
|
-
import { getViemProvider } from '../services/viem';
|
|
71
|
+
import { getBlockTimestamp, getViemProvider, setViemBlockNumber } from '../services/viem';
|
|
70
72
|
import { attachFluidMerklIncentives, getFluidMerklCampaigns } from './merkl';
|
|
71
73
|
|
|
72
74
|
export {
|
|
@@ -101,6 +103,7 @@ const getChainLinkPricesForTokens = async (
|
|
|
101
103
|
tokens: string[],
|
|
102
104
|
network: NetworkNumber,
|
|
103
105
|
client: PublicClient,
|
|
106
|
+
blockNumber: Blockish = 'latest',
|
|
104
107
|
): Promise<{ [key: string]: string }> => {
|
|
105
108
|
const isMainnet = isMainnetNetwork(network);
|
|
106
109
|
|
|
@@ -158,7 +161,7 @@ const getChainLinkPricesForTokens = async (
|
|
|
158
161
|
|
|
159
162
|
const calls = [...staticCalls, ..._calls].filter((call) => call);
|
|
160
163
|
// @ts-ignore
|
|
161
|
-
const results = await client.multicall({ contracts: calls });
|
|
164
|
+
const results = await client.multicall({ contracts: calls, ...setViemBlockNumber(blockNumber) });
|
|
162
165
|
|
|
163
166
|
const ethPriceChainlink = new Dec(results[0].result as string).div(1e8).toString();
|
|
164
167
|
const btcPriceChainlink = new Dec(results[1].result as string).div(1e8).toString();
|
|
@@ -294,7 +297,7 @@ const getChainLinkPricesForTokens = async (
|
|
|
294
297
|
};
|
|
295
298
|
|
|
296
299
|
|
|
297
|
-
const getTokenPriceFromChainlink = async (asset: AssetData, network: NetworkNumber, provider: PublicClient) => {
|
|
300
|
+
const getTokenPriceFromChainlink = async (asset: AssetData, network: NetworkNumber, provider: PublicClient, blockNumber: Blockish = 'latest') => {
|
|
298
301
|
if (asset.symbol === 'sUSDS') {
|
|
299
302
|
return new Dec('105276929').div(1e8).toString();
|
|
300
303
|
}
|
|
@@ -305,15 +308,15 @@ const getTokenPriceFromChainlink = async (asset: AssetData, network: NetworkNumb
|
|
|
305
308
|
let loanTokenPrice;
|
|
306
309
|
if (asset.symbol === 'wstETH') {
|
|
307
310
|
// need to handle wstETH for l2s inside getWstETHPrice
|
|
308
|
-
loanTokenPrice = await getWstETHPriceFluid(provider, network);
|
|
311
|
+
loanTokenPrice = await getWstETHPriceFluid(provider, network, blockNumber);
|
|
309
312
|
} else if (isMainnet) {
|
|
310
313
|
const feedRegistryContract = FeedRegistryContractViem(provider, NetworkNumber.Eth);
|
|
311
|
-
loanTokenPrice = isTokenUSDA ? '100000000' : await feedRegistryContract.read.latestAnswer([loanTokenFeedAddress, USD_QUOTE]);
|
|
314
|
+
loanTokenPrice = isTokenUSDA ? '100000000' : await feedRegistryContract.read.latestAnswer([loanTokenFeedAddress, USD_QUOTE], setViemBlockNumber(blockNumber));
|
|
312
315
|
} else {
|
|
313
316
|
// Currently only base network is supported
|
|
314
317
|
const feedRegistryContract = DFSFeedRegistryContractViem(provider, network);
|
|
315
318
|
try {
|
|
316
|
-
const roundPriceData = isTokenUSDA ? [0, '100000000'] : await feedRegistryContract.read.latestRoundData([loanTokenFeedAddress, USD_QUOTE]);
|
|
319
|
+
const roundPriceData = isTokenUSDA ? [0, '100000000'] : await feedRegistryContract.read.latestRoundData([loanTokenFeedAddress, USD_QUOTE], setViemBlockNumber(blockNumber));
|
|
317
320
|
loanTokenPrice = roundPriceData[1].toString();
|
|
318
321
|
} catch (err) {
|
|
319
322
|
console.error(`Error fetching price for ${asset.symbol} on ${network}: ${err}`);
|
|
@@ -364,7 +367,7 @@ const getTradingApy = async (poolAddress: EthAddress) => {
|
|
|
364
367
|
return new Dec(data.tradingApy).div(100).toString();
|
|
365
368
|
};
|
|
366
369
|
|
|
367
|
-
const parseT1MarketData = async (provider: PublicClient, data: FluidVaultDataStructOutputStruct, network: NetworkNumber, tokenPrices: Record<string, string> | null = null) => {
|
|
370
|
+
const parseT1MarketData = async (provider: PublicClient, data: FluidVaultDataStructOutputStruct, network: NetworkNumber, tokenPrices: Record<string, string> | null = null, blockNumber: Blockish = 'latest') => {
|
|
368
371
|
const collAssetContract = getAssetInfoByAddress(data.supplyToken0, network);
|
|
369
372
|
const collAsset = getAssetInfo(getNativeAssetFromWrapped(collAssetContract.symbol), network);
|
|
370
373
|
const debtAssetContract = getAssetInfoByAddress(data.borrowToken0, network);
|
|
@@ -380,7 +383,7 @@ const parseT1MarketData = async (provider: PublicClient, data: FluidVaultDataStr
|
|
|
380
383
|
if (tokenPrices) {
|
|
381
384
|
debtPriceParsed = tokenPrices[debtAsset.symbol] || '0';
|
|
382
385
|
} else {
|
|
383
|
-
debtPriceParsed = await getTokenPriceFromChainlink(debtAssetContract, network, provider);
|
|
386
|
+
debtPriceParsed = await getTokenPriceFromChainlink(debtAssetContract, network, provider, blockNumber);
|
|
384
387
|
}
|
|
385
388
|
|
|
386
389
|
const collAssetData: FluidAssetData = {
|
|
@@ -484,7 +487,7 @@ const parseT1MarketData = async (provider: PublicClient, data: FluidVaultDataStr
|
|
|
484
487
|
} as FluidMarketData;
|
|
485
488
|
};
|
|
486
489
|
|
|
487
|
-
const parseT2MarketData = async (provider: PublicClient, data: FluidVaultDataStructOutputStruct, network: NetworkNumber, tokenPrices: Record<string, string> | null = null) => {
|
|
490
|
+
const parseT2MarketData = async (provider: PublicClient, data: FluidVaultDataStructOutputStruct, network: NetworkNumber, tokenPrices: Record<string, string> | null = null, blockNumber: Blockish = 'latest') => {
|
|
488
491
|
const collAsset0Contract = getAssetInfoByAddress(data.supplyToken0, network);
|
|
489
492
|
const collAsset0 = getAssetInfo(getNativeAssetFromWrapped(collAsset0Contract.symbol), network);
|
|
490
493
|
const collAsset1Contract = getAssetInfoByAddress(data.supplyToken1, network);
|
|
@@ -501,7 +504,7 @@ const parseT2MarketData = async (provider: PublicClient, data: FluidVaultDataStr
|
|
|
501
504
|
if (tokenPrices) {
|
|
502
505
|
prices = tokenPrices;
|
|
503
506
|
} else {
|
|
504
|
-
prices = await getChainLinkPricesForTokens([collAsset0Contract.address, collAsset1Contract.address, debtAssetContract.address], network, provider);
|
|
507
|
+
prices = await getChainLinkPricesForTokens([collAsset0Contract.address, collAsset1Contract.address, debtAssetContract.address], network, provider, blockNumber);
|
|
505
508
|
}
|
|
506
509
|
|
|
507
510
|
const {
|
|
@@ -677,7 +680,7 @@ const parseT2MarketData = async (provider: PublicClient, data: FluidVaultDataStr
|
|
|
677
680
|
} as FluidMarketData;
|
|
678
681
|
};
|
|
679
682
|
|
|
680
|
-
const parseT3MarketData = async (provider: PublicClient, data: FluidVaultDataStructOutputStruct, network: NetworkNumber, tokenPrices: Record<string, string> | null = null) => {
|
|
683
|
+
const parseT3MarketData = async (provider: PublicClient, data: FluidVaultDataStructOutputStruct, network: NetworkNumber, tokenPrices: Record<string, string> | null = null, blockNumber: Blockish = 'latest') => {
|
|
681
684
|
const collAssetContract = getAssetInfoByAddress(data.supplyToken0, network);
|
|
682
685
|
const collAsset = getAssetInfo(getNativeAssetFromWrapped(collAssetContract.symbol), network);
|
|
683
686
|
const debtAsset0Contract = getAssetInfoByAddress(data.borrowToken0, network);
|
|
@@ -715,7 +718,7 @@ const parseT3MarketData = async (provider: PublicClient, data: FluidVaultDataStr
|
|
|
715
718
|
if (tokenPrices) {
|
|
716
719
|
prices = tokenPrices;
|
|
717
720
|
} else {
|
|
718
|
-
prices = await getChainLinkPricesForTokens([collAssetContract.address, debtAsset0Contract.address, debtAsset1Contract.address], network, provider);
|
|
721
|
+
prices = await getChainLinkPricesForTokens([collAssetContract.address, debtAsset0Contract.address, debtAsset1Contract.address], network, provider, blockNumber);
|
|
719
722
|
}
|
|
720
723
|
|
|
721
724
|
const supplyRate = new Dec(data.supplyRateVault).div(100).toString();
|
|
@@ -867,7 +870,7 @@ const parseT3MarketData = async (provider: PublicClient, data: FluidVaultDataStr
|
|
|
867
870
|
} as FluidMarketData;
|
|
868
871
|
};
|
|
869
872
|
|
|
870
|
-
const parseT4MarketData = async (provider: PublicClient, data: FluidVaultDataStructOutputStruct, network: NetworkNumber, tokenPrices: Record<string, string> | null = null) => {
|
|
873
|
+
const parseT4MarketData = async (provider: PublicClient, data: FluidVaultDataStructOutputStruct, network: NetworkNumber, tokenPrices: Record<string, string> | null = null, blockNumber: Blockish = 'latest') => {
|
|
871
874
|
const collAsset0Contract = getAssetInfoByAddress(data.supplyToken0, network);
|
|
872
875
|
const collAsset0 = getAssetInfo(getNativeAssetFromWrapped(collAsset0Contract.symbol), network);
|
|
873
876
|
const collAsset1Contract = getAssetInfoByAddress(data.supplyToken1, network);
|
|
@@ -889,7 +892,7 @@ const parseT4MarketData = async (provider: PublicClient, data: FluidVaultDataStr
|
|
|
889
892
|
} else {
|
|
890
893
|
prices = await getChainLinkPricesForTokens(
|
|
891
894
|
[collAsset0Contract.address, collAsset1Contract.address, debtAsset0Contract.address, debtAsset1Contract.address],
|
|
892
|
-
network, provider);
|
|
895
|
+
network, provider, blockNumber);
|
|
893
896
|
}
|
|
894
897
|
|
|
895
898
|
const {
|
|
@@ -1124,7 +1127,7 @@ const parseT4MarketData = async (provider: PublicClient, data: FluidVaultDataStr
|
|
|
1124
1127
|
} as FluidMarketData;
|
|
1125
1128
|
};
|
|
1126
1129
|
|
|
1127
|
-
const parseMarketData = async (provider: PublicClient, data: FluidVaultDataStructOutputStruct, network: NetworkNumber, tokenPrices: Record<string, string> | null = null) => {
|
|
1130
|
+
const parseMarketData = async (provider: PublicClient, data: FluidVaultDataStructOutputStruct, network: NetworkNumber, tokenPrices: Record<string, string> | null = null, blockNumber: Blockish = 'latest') => {
|
|
1128
1131
|
const marketInfo = getFluidMarketInfoById(+(data.vaultId.toString()), network);
|
|
1129
1132
|
if (!marketInfo) {
|
|
1130
1133
|
console.error(`Fluid market with address ${data.vault} not supported.`);
|
|
@@ -1133,13 +1136,13 @@ const parseMarketData = async (provider: PublicClient, data: FluidVaultDataStruc
|
|
|
1133
1136
|
const vaultType = parseVaultType(+(data.vaultType.toString()));
|
|
1134
1137
|
switch (vaultType) {
|
|
1135
1138
|
case FluidVaultType.T1:
|
|
1136
|
-
return parseT1MarketData(provider, data, network, tokenPrices);
|
|
1139
|
+
return parseT1MarketData(provider, data, network, tokenPrices, blockNumber);
|
|
1137
1140
|
case FluidVaultType.T2:
|
|
1138
|
-
return parseT2MarketData(provider, data, network, tokenPrices);
|
|
1141
|
+
return parseT2MarketData(provider, data, network, tokenPrices, blockNumber);
|
|
1139
1142
|
case FluidVaultType.T3:
|
|
1140
|
-
return parseT3MarketData(provider, data, network, tokenPrices);
|
|
1143
|
+
return parseT3MarketData(provider, data, network, tokenPrices, blockNumber);
|
|
1141
1144
|
case FluidVaultType.T4:
|
|
1142
|
-
return parseT4MarketData(provider, data, network, tokenPrices);
|
|
1145
|
+
return parseT4MarketData(provider, data, network, tokenPrices, blockNumber);
|
|
1143
1146
|
default:
|
|
1144
1147
|
throw new Error(`Unknown vault type: ${vaultType}`);
|
|
1145
1148
|
}
|
|
@@ -1450,15 +1453,15 @@ const parseUserData = (userPositionData: FluidUserPositionStructOutputStruct, va
|
|
|
1450
1453
|
}
|
|
1451
1454
|
};
|
|
1452
1455
|
|
|
1453
|
-
export const _getFluidMarketData = async (provider: PublicClient, network: NetworkNumber, market: FluidMarketInfo) => {
|
|
1456
|
+
export const _getFluidMarketData = async (provider: PublicClient, network: NetworkNumber, market: FluidMarketInfo, blockNumber: Blockish = 'latest') => {
|
|
1454
1457
|
const view = FluidViewContractViem(provider, network);
|
|
1455
1458
|
|
|
1456
1459
|
const [data, merklCampaigns] = await Promise.all([
|
|
1457
|
-
view.read.getVaultData([market.marketAddress]),
|
|
1460
|
+
view.read.getVaultData([market.marketAddress], setViemBlockNumber(blockNumber)),
|
|
1458
1461
|
getFluidMerklCampaigns(network),
|
|
1459
1462
|
]);
|
|
1460
1463
|
|
|
1461
|
-
const marketData = await parseMarketData(provider, data, network);
|
|
1464
|
+
const marketData = await parseMarketData(provider, data, network, null, blockNumber);
|
|
1462
1465
|
return marketData ? attachFluidMerklIncentives(marketData, merklCampaigns) : marketData;
|
|
1463
1466
|
};
|
|
1464
1467
|
|
|
@@ -1466,23 +1469,26 @@ export const getFluidMarketData = async (
|
|
|
1466
1469
|
provider: EthereumProvider,
|
|
1467
1470
|
network: NetworkNumber,
|
|
1468
1471
|
market: FluidMarketInfo,
|
|
1469
|
-
|
|
1472
|
+
blockNumber: Blockish = 'latest',
|
|
1473
|
+
) => _getFluidMarketData(getViemProvider(provider, network), network, market, blockNumber);
|
|
1470
1474
|
|
|
1471
1475
|
export const _getFluidVaultIdsForUser = async (
|
|
1472
1476
|
provider: Client,
|
|
1473
1477
|
network: NetworkNumber,
|
|
1474
1478
|
user: EthAddress,
|
|
1479
|
+
blockNumber: Blockish = 'latest',
|
|
1475
1480
|
): Promise<string[]> => {
|
|
1476
1481
|
const view = FluidViewContractViem(provider, network);
|
|
1477
1482
|
|
|
1478
|
-
return (await view.read.getUserNftIds([user])).map((item: bigint) => item.toString());
|
|
1483
|
+
return (await view.read.getUserNftIds([user], setViemBlockNumber(blockNumber))).map((item: bigint) => item.toString());
|
|
1479
1484
|
};
|
|
1480
1485
|
|
|
1481
1486
|
export const getFluidVaultIdsForUser = async (
|
|
1482
1487
|
provider: EthereumProvider,
|
|
1483
1488
|
network: NetworkNumber,
|
|
1484
1489
|
user: EthAddress,
|
|
1485
|
-
|
|
1490
|
+
blockNumber: Blockish = 'latest',
|
|
1491
|
+
): Promise<string[]> => _getFluidVaultIdsForUser(getViemProvider(provider, network), network, user, blockNumber);
|
|
1486
1492
|
|
|
1487
1493
|
|
|
1488
1494
|
export const _getFluidPosition = async (
|
|
@@ -1493,10 +1499,11 @@ export const _getFluidPosition = async (
|
|
|
1493
1499
|
assetsData: FluidAssetsData
|
|
1494
1500
|
marketData: InnerFluidMarketData,
|
|
1495
1501
|
},
|
|
1502
|
+
blockNumber: Blockish = 'latest',
|
|
1496
1503
|
): Promise<FluidVaultData> => {
|
|
1497
1504
|
const view = FluidViewContractViem(provider, network);
|
|
1498
1505
|
|
|
1499
|
-
const data = await view.read.getPositionByNftId([BigInt(vaultId)]);
|
|
1506
|
+
const data = await view.read.getPositionByNftId([BigInt(vaultId)], setViemBlockNumber(blockNumber));
|
|
1500
1507
|
|
|
1501
1508
|
const userPositionData = data[0];
|
|
1502
1509
|
|
|
@@ -1511,15 +1518,16 @@ export const getFluidPosition = async (
|
|
|
1511
1518
|
assetsData: FluidAssetsData
|
|
1512
1519
|
marketData: InnerFluidMarketData,
|
|
1513
1520
|
},
|
|
1514
|
-
|
|
1521
|
+
blockNumber: Blockish = 'latest',
|
|
1522
|
+
): Promise<FluidVaultData> => _getFluidPosition(getViemProvider(provider, network), network, vaultId, extractedState, blockNumber);
|
|
1515
1523
|
|
|
1516
|
-
export const _getFluidPositionWithMarket = async (provider: PublicClient, network: NetworkNumber, vaultId: string) => {
|
|
1524
|
+
export const _getFluidPositionWithMarket = async (provider: PublicClient, network: NetworkNumber, vaultId: string, blockNumber: Blockish = 'latest') => {
|
|
1517
1525
|
const view = FluidViewContractViem(provider, network);
|
|
1518
1526
|
const [data, merklCampaigns] = await Promise.all([
|
|
1519
|
-
view.read.getPositionByNftId([BigInt(vaultId)]),
|
|
1527
|
+
view.read.getPositionByNftId([BigInt(vaultId)], setViemBlockNumber(blockNumber)),
|
|
1520
1528
|
getFluidMerklCampaigns(network),
|
|
1521
1529
|
]);
|
|
1522
|
-
const parsedMarketData = await parseMarketData(provider, data[1], network);
|
|
1530
|
+
const parsedMarketData = await parseMarketData(provider, data[1], network, null, blockNumber);
|
|
1523
1531
|
if (!parsedMarketData) {
|
|
1524
1532
|
return;
|
|
1525
1533
|
}
|
|
@@ -1536,17 +1544,18 @@ export const getFluidPositionWithMarket = async (
|
|
|
1536
1544
|
provider: EthereumProvider,
|
|
1537
1545
|
network: NetworkNumber,
|
|
1538
1546
|
vaultId: string,
|
|
1539
|
-
|
|
1547
|
+
blockNumber: Blockish = 'latest',
|
|
1548
|
+
) => _getFluidPositionWithMarket(getViemProvider(provider, network), network, vaultId, blockNumber);
|
|
1540
1549
|
|
|
1541
|
-
export const _getAllFluidMarketDataChunked = async (network: NetworkNumber, provider: PublicClient) => {
|
|
1550
|
+
export const _getAllFluidMarketDataChunked = async (network: NetworkNumber, provider: PublicClient, blockNumber: Blockish = 'latest') => {
|
|
1542
1551
|
const versions = getFluidVersionsDataForNetwork(network);
|
|
1543
1552
|
const view = FluidViewContractViem(provider, network);
|
|
1544
1553
|
const [data, merklCampaigns] = await Promise.all([
|
|
1545
|
-
Promise.all(versions.map((version) => view.read.getVaultData([version.marketAddress]))),
|
|
1554
|
+
Promise.all(versions.map((version) => view.read.getVaultData([version.marketAddress], setViemBlockNumber(blockNumber)))),
|
|
1546
1555
|
getFluidMerklCampaigns(network),
|
|
1547
1556
|
]);
|
|
1548
1557
|
return Promise.all(data.map(async (item) => {
|
|
1549
|
-
const marketData = await parseMarketData(provider, item, network);
|
|
1558
|
+
const marketData = await parseMarketData(provider, item, network, null, blockNumber);
|
|
1550
1559
|
return marketData ? attachFluidMerklIncentives(marketData, merklCampaigns) : marketData;
|
|
1551
1560
|
}));
|
|
1552
1561
|
};
|
|
@@ -1554,16 +1563,17 @@ export const _getAllFluidMarketDataChunked = async (network: NetworkNumber, prov
|
|
|
1554
1563
|
export const getAllFluidMarketDataChunked = async (
|
|
1555
1564
|
network: NetworkNumber,
|
|
1556
1565
|
provider: EthereumProvider,
|
|
1557
|
-
|
|
1566
|
+
blockNumber: Blockish = 'latest',
|
|
1567
|
+
) => _getAllFluidMarketDataChunked(network, getViemProvider(provider, network, { batch: { multicall: true } }), blockNumber);
|
|
1558
1568
|
|
|
1559
|
-
export const _getFluidTokenData = async (provider: Client, network: NetworkNumber, token: string) => {
|
|
1569
|
+
export const _getFluidTokenData = async (provider: Client, network: NetworkNumber, token: string, blockNumber: Blockish = 'latest') => {
|
|
1560
1570
|
const view = FluidViewContractViem(provider, network);
|
|
1561
1571
|
const fTokenAddress = getFTokenAddress(token, network);
|
|
1562
1572
|
const [
|
|
1563
1573
|
data,
|
|
1564
1574
|
rewardsApiResponse,
|
|
1565
1575
|
] = await Promise.all([
|
|
1566
|
-
await view.read.getFTokenData([fTokenAddress]),
|
|
1576
|
+
await view.read.getFTokenData([fTokenAddress], setViemBlockNumber(blockNumber)),
|
|
1567
1577
|
fetch(`https://api.fluid.instadapp.io/v2/lending/${network}/tokens/${fTokenAddress}`),
|
|
1568
1578
|
]);
|
|
1569
1579
|
let rewardsData = { rewards: [] } as any;
|
|
@@ -1596,9 +1606,10 @@ export const getFluidTokenData = async (
|
|
|
1596
1606
|
provider: EthereumProvider,
|
|
1597
1607
|
network: NetworkNumber,
|
|
1598
1608
|
token: string,
|
|
1599
|
-
|
|
1609
|
+
blockNumber: Blockish = 'latest',
|
|
1610
|
+
) => _getFluidTokenData(getViemProvider(provider, network), network, token, blockNumber);
|
|
1600
1611
|
|
|
1601
|
-
const parseFDepositTokenData = (fTokenData: FluidFTokenDataStructOutput, userPosition: FluidUserEarnPositionStructOutput, apiData: any, fTokenAddress?: string) => {
|
|
1612
|
+
const parseFDepositTokenData = (fTokenData: FluidFTokenDataStructOutput, userPosition: FluidUserEarnPositionStructOutput, apiData: any, fTokenAddress?: string): FluidEarnPositionData => {
|
|
1602
1613
|
const decimals = fTokenData.decimals.toString();
|
|
1603
1614
|
const depositRate = new Dec(getEthAmountForDecimals(fTokenData.convertToShares.toString(), decimals)).toString();
|
|
1604
1615
|
const withdrawRate = new Dec(getEthAmountForDecimals(fTokenData.convertToAssets.toString(), decimals)).toString();
|
|
@@ -1619,14 +1630,14 @@ const parseFDepositTokenData = (fTokenData: FluidFTokenDataStructOutput, userPos
|
|
|
1619
1630
|
};
|
|
1620
1631
|
};
|
|
1621
1632
|
|
|
1622
|
-
export const _getFluidDepositData = async (provider: Client, network: NetworkNumber, token: string, address: EthAddress) => {
|
|
1633
|
+
export const _getFluidDepositData = async (provider: Client, network: NetworkNumber, token: string, address: EthAddress, blockNumber: Blockish = 'latest') => {
|
|
1623
1634
|
const view = FluidViewContractViem(provider, network);
|
|
1624
1635
|
const fTokenAddress = getFTokenAddress(token, network);
|
|
1625
1636
|
const [
|
|
1626
1637
|
[userPosition, fTokenData],
|
|
1627
1638
|
rewardsApiResponse,
|
|
1628
1639
|
] = await Promise.all([
|
|
1629
|
-
view.read.getUserEarnPositionWithFToken([fTokenAddress, address]),
|
|
1640
|
+
view.read.getUserEarnPositionWithFToken([fTokenAddress, address], setViemBlockNumber(blockNumber)),
|
|
1630
1641
|
fetch(`https://api.fluid.instadapp.io/v2/lending/${network}/tokens/${fTokenAddress}`),
|
|
1631
1642
|
]);
|
|
1632
1643
|
let rewardsData = { rewards: [] };
|
|
@@ -1644,15 +1655,16 @@ export const getFluidDepositData = async (
|
|
|
1644
1655
|
network: NetworkNumber,
|
|
1645
1656
|
token: string,
|
|
1646
1657
|
address: EthAddress,
|
|
1647
|
-
|
|
1658
|
+
blockNumber: Blockish = 'latest',
|
|
1659
|
+
) => _getFluidDepositData(getViemProvider(provider, network), network, token, address, blockNumber);
|
|
1648
1660
|
|
|
1649
|
-
export const _getAllUserEarnPositionsWithFTokens = async (provider: Client, network: NetworkNumber, user: EthAddress) => {
|
|
1661
|
+
export const _getAllUserEarnPositionsWithFTokens = async (provider: Client, network: NetworkNumber, user: EthAddress, blockNumber: Blockish = 'latest'): Promise<FluidEarnPositionData[]> => {
|
|
1650
1662
|
const view = FluidViewContractViem(provider, network);
|
|
1651
1663
|
const [
|
|
1652
1664
|
[userPositions, fTokensData],
|
|
1653
1665
|
rewardsApiResponse,
|
|
1654
1666
|
] = await Promise.all([
|
|
1655
|
-
view.read.getAllUserEarnPositionsWithFTokens([user]),
|
|
1667
|
+
view.read.getAllUserEarnPositionsWithFTokens([user], setViemBlockNumber(blockNumber)),
|
|
1656
1668
|
fetch(`https://api.fluid.instadapp.io/v2/lending/${network}/tokens`),
|
|
1657
1669
|
]);
|
|
1658
1670
|
|
|
@@ -1665,7 +1677,7 @@ export const _getAllUserEarnPositionsWithFTokens = async (provider: Client, netw
|
|
|
1665
1677
|
rewardsData = await rewardsApiResponse.json();
|
|
1666
1678
|
}
|
|
1667
1679
|
|
|
1668
|
-
const parsedRes = fTokensData.reduce<
|
|
1680
|
+
const parsedRes = fTokensData.reduce<FluidEarnPositionData[]>((acc, fTokenData, i) => {
|
|
1669
1681
|
const userPosition = userPositions[i];
|
|
1670
1682
|
const deposited = userPosition?.underlyingAssets;
|
|
1671
1683
|
|
|
@@ -1685,17 +1697,18 @@ export const getAllUserEarnPositionsWithFTokens = async (
|
|
|
1685
1697
|
provider: EthereumProvider,
|
|
1686
1698
|
network: NetworkNumber,
|
|
1687
1699
|
user: EthAddress,
|
|
1688
|
-
|
|
1700
|
+
blockNumber: Blockish = 'latest',
|
|
1701
|
+
) => _getAllUserEarnPositionsWithFTokens(getViemProvider(provider, network), network, user, blockNumber);
|
|
1689
1702
|
|
|
1690
|
-
export const _getUserPositions = async (provider: PublicClient, network: NetworkNumber, user: EthAddress) => {
|
|
1703
|
+
export const _getUserPositions = async (provider: PublicClient, network: NetworkNumber, user: EthAddress, blockNumber: Blockish = 'latest') => {
|
|
1691
1704
|
const view = FluidViewContractViem(provider, network);
|
|
1692
1705
|
|
|
1693
1706
|
const [data, merklCampaigns] = await Promise.all([
|
|
1694
|
-
view.read.getUserPositions([user]),
|
|
1707
|
+
view.read.getUserPositions([user], setViemBlockNumber(blockNumber)),
|
|
1695
1708
|
getFluidMerklCampaigns(network),
|
|
1696
1709
|
]);
|
|
1697
1710
|
|
|
1698
|
-
const parsedMarketData = (await Promise.all(data[1].map(async (vaultData) => parseMarketData(provider, vaultData, network))))
|
|
1711
|
+
const parsedMarketData = (await Promise.all(data[1].map(async (vaultData) => parseMarketData(provider, vaultData, network, null, blockNumber))))
|
|
1699
1712
|
.map(marketData => (marketData ? attachFluidMerklIncentives(marketData, merklCampaigns) : marketData));
|
|
1700
1713
|
|
|
1701
1714
|
const userData = data[0].map((position, i) => (parsedMarketData[i] && { ...parseUserData(position, parsedMarketData[i]) }));
|
|
@@ -1710,45 +1723,46 @@ export const getUserPositions = async (
|
|
|
1710
1723
|
provider: EthereumProvider,
|
|
1711
1724
|
network: NetworkNumber,
|
|
1712
1725
|
user: EthAddress,
|
|
1713
|
-
|
|
1726
|
+
blockNumber: Blockish = 'latest',
|
|
1727
|
+
) => _getUserPositions(getViemProvider(provider, network), network, user, blockNumber);
|
|
1714
1728
|
|
|
1715
|
-
const getTokenPricePortfolio = async (token: string, provider: PublicClient, network: NetworkNumber) => {
|
|
1729
|
+
const getTokenPricePortfolio = async (token: string, provider: PublicClient, network: NetworkNumber, blockNumber: Blockish = 'latest') => {
|
|
1716
1730
|
if (token === 'ETH') {
|
|
1717
1731
|
const ethFeedContract = ETHPriceFeedContractViem(provider, network);
|
|
1718
|
-
return ethFeedContract.read.latestAnswer();
|
|
1732
|
+
return ethFeedContract.read.latestAnswer(setViemBlockNumber(blockNumber));
|
|
1719
1733
|
}
|
|
1720
1734
|
if (token === 'WBTC') {
|
|
1721
1735
|
const wbtcFeedContract = BTCPriceFeedContractViem(provider, network);
|
|
1722
|
-
return wbtcFeedContract.read.latestAnswer();
|
|
1736
|
+
return wbtcFeedContract.read.latestAnswer(setViemBlockNumber(blockNumber));
|
|
1723
1737
|
}
|
|
1724
1738
|
if (token === 'wstETH') {
|
|
1725
|
-
return getWstETHPrice(provider, network);
|
|
1739
|
+
return getWstETHPrice(provider, network, blockNumber);
|
|
1726
1740
|
}
|
|
1727
1741
|
if (token === 'weETH' && network !== NetworkNumber.Plasma) {
|
|
1728
|
-
return getWeETHPrice(provider, network);
|
|
1742
|
+
return getWeETHPrice(provider, network, blockNumber);
|
|
1729
1743
|
}
|
|
1730
1744
|
|
|
1731
1745
|
if (token === 'wrsETH') {
|
|
1732
|
-
return getWsrETHPrice(provider, network);
|
|
1746
|
+
return getWsrETHPrice(provider, network, blockNumber);
|
|
1733
1747
|
}
|
|
1734
1748
|
|
|
1735
1749
|
if (token === 'syrupUSDT') {
|
|
1736
|
-
return getSyrupUSDTPrice(provider, network);
|
|
1750
|
+
return getSyrupUSDTPrice(provider, network, blockNumber);
|
|
1737
1751
|
}
|
|
1738
1752
|
|
|
1739
1753
|
if (token === 'wstUSR') {
|
|
1740
|
-
return getWstUSRPrice(provider, network);
|
|
1754
|
+
return getWstUSRPrice(provider, network, blockNumber);
|
|
1741
1755
|
}
|
|
1742
1756
|
|
|
1743
1757
|
const isMainnet = isMainnetNetwork(network);
|
|
1744
1758
|
const chainLinkFeedAddress = getChainlinkAssetAddress(token, network);
|
|
1745
1759
|
if (isMainnet) {
|
|
1746
1760
|
const feedRegistryContract = FeedRegistryContractViem(provider, NetworkNumber.Eth);
|
|
1747
|
-
return feedRegistryContract.read.latestAnswer([chainLinkFeedAddress, USD_QUOTE]);
|
|
1761
|
+
return feedRegistryContract.read.latestAnswer([chainLinkFeedAddress, USD_QUOTE], setViemBlockNumber(blockNumber));
|
|
1748
1762
|
}
|
|
1749
1763
|
|
|
1750
1764
|
const feedRegistryContract = DFSFeedRegistryContractViem(provider, network);
|
|
1751
|
-
return feedRegistryContract.read.latestRoundData([chainLinkFeedAddress, USD_QUOTE]);
|
|
1765
|
+
return feedRegistryContract.read.latestRoundData([chainLinkFeedAddress, USD_QUOTE], setViemBlockNumber(blockNumber));
|
|
1752
1766
|
};
|
|
1753
1767
|
|
|
1754
1768
|
const tokensWithoutChainlinkPrices = ['sUSDS', 'USDA', 'ezETH', 'rsETH', 'weETHs', 'LBTC'];
|
|
@@ -1778,12 +1792,12 @@ const handleTokenWithoutChainlinkPrice = (token: string, prices: Record<string,
|
|
|
1778
1792
|
return '0';
|
|
1779
1793
|
};
|
|
1780
1794
|
|
|
1781
|
-
const getTokensPricesForPortfolio = async (tokens: string[], provider: PublicClient, network: NetworkNumber) => {
|
|
1795
|
+
const getTokensPricesForPortfolio = async (tokens: string[], provider: PublicClient, network: NetworkNumber, blockNumber: Blockish = 'latest') => {
|
|
1782
1796
|
const tokensWithChainlinkPrices = tokens.filter((token) => !tokensWithoutChainlinkPrices.includes(token));
|
|
1783
1797
|
const pricesFromChainlink: Record<string, string> = {};
|
|
1784
1798
|
await Promise.all(tokensWithChainlinkPrices.map(async (token) => {
|
|
1785
1799
|
try {
|
|
1786
|
-
const price = await getTokenPricePortfolio(token, provider, network);
|
|
1800
|
+
const price = await getTokenPricePortfolio(token, provider, network, blockNumber);
|
|
1787
1801
|
if (typeof price === 'string') pricesFromChainlink[token] = price;
|
|
1788
1802
|
else if (typeof price === 'bigint') pricesFromChainlink[token] = new Dec(price).div(1e8).toString();
|
|
1789
1803
|
else pricesFromChainlink[token] = new Dec(price[1]!.toString() as string).div(1e8).toString();
|
|
@@ -1800,10 +1814,10 @@ const getTokensPricesForPortfolio = async (tokens: string[], provider: PublicCli
|
|
|
1800
1814
|
return pricesFromChainlink;
|
|
1801
1815
|
};
|
|
1802
1816
|
|
|
1803
|
-
export const _getUserPositionsPortfolio = async (provider: PublicClient, network: NetworkNumber, user: EthAddress) => {
|
|
1817
|
+
export const _getUserPositionsPortfolio = async (provider: PublicClient, network: NetworkNumber, user: EthAddress, blockNumber: Blockish = 'latest') => {
|
|
1804
1818
|
const view = FluidViewContractViem(provider, network);
|
|
1805
1819
|
|
|
1806
|
-
const data = await view.read.getUserPositions([user]);
|
|
1820
|
+
const data = await view.read.getUserPositions([user], setViemBlockNumber(blockNumber));
|
|
1807
1821
|
const tokens = Array.from(new Set(data[1].map((vaultData) => {
|
|
1808
1822
|
const vaultTokens = [getAssetInfoByAddress(vaultData.supplyToken0, network).symbol, getAssetInfoByAddress(vaultData.borrowToken0, network).symbol];
|
|
1809
1823
|
if (vaultData.supplyToken1 && !compareAddresses(ZERO_ADDRESS, vaultData.supplyToken1)) vaultTokens.push(getAssetInfoByAddress(vaultData.supplyToken1, network).symbol);
|
|
@@ -1817,11 +1831,11 @@ export const _getUserPositionsPortfolio = async (provider: PublicClient, network
|
|
|
1817
1831
|
if (!tokens.includes('WBTC')) tokens.push('WBTC');
|
|
1818
1832
|
|
|
1819
1833
|
const [tokenPrices, merklCampaigns] = await Promise.all([
|
|
1820
|
-
getTokensPricesForPortfolio(tokens, provider, network),
|
|
1834
|
+
getTokensPricesForPortfolio(tokens, provider, network, blockNumber),
|
|
1821
1835
|
getFluidMerklCampaigns(network),
|
|
1822
1836
|
]);
|
|
1823
1837
|
|
|
1824
|
-
const parsedMarketData = (await Promise.all(data[1].map(async (vaultData) => parseMarketData(provider, vaultData, network, tokenPrices))))
|
|
1838
|
+
const parsedMarketData = (await Promise.all(data[1].map(async (vaultData) => parseMarketData(provider, vaultData, network, tokenPrices, blockNumber))))
|
|
1825
1839
|
.map(marketData => (marketData ? attachFluidMerklIncentives(marketData, merklCampaigns) : marketData));
|
|
1826
1840
|
|
|
1827
1841
|
const userData = data[0].map((position, i) => (parsedMarketData[i] && { ...parseUserData(position, parsedMarketData[i]) }));
|
|
@@ -1833,12 +1847,13 @@ export const _getUserPositionsPortfolio = async (provider: PublicClient, network
|
|
|
1833
1847
|
};
|
|
1834
1848
|
|
|
1835
1849
|
|
|
1836
|
-
|
|
1850
|
+
/** `blockTimestamp` (unix seconds) is the historical block's timestamp when already known; otherwise it is read from the block. */
|
|
1851
|
+
export const _getAllFluidMarketDataPortfolio = async (provider: PublicClient, network: NetworkNumber, blockNumber: Blockish = 'latest', blockTimestamp?: number): Promise<Record<string, FluidMarketData>> => {
|
|
1837
1852
|
const versions = getFluidVersionsDataForNetwork(network);
|
|
1838
1853
|
if (versions.length === 0) return {};
|
|
1839
1854
|
|
|
1840
1855
|
const view = FluidViewContractViem(provider, network);
|
|
1841
|
-
const vaultsData = await Promise.all(versions.map((version) => view.read.getVaultData([version.marketAddress])));
|
|
1856
|
+
const vaultsData = await Promise.all(versions.map((version) => view.read.getVaultData([version.marketAddress], setViemBlockNumber(blockNumber))));
|
|
1842
1857
|
|
|
1843
1858
|
const tokens = Array.from(new Set(vaultsData.map((vaultData) => {
|
|
1844
1859
|
const vaultTokens = [getAssetInfoByAddress(vaultData.supplyToken0, network).symbol, getAssetInfoByAddress(vaultData.borrowToken0, network).symbol];
|
|
@@ -1852,11 +1867,13 @@ export const _getAllFluidMarketDataPortfolio = async (provider: PublicClient, ne
|
|
|
1852
1867
|
if (!tokens.includes('WBTC')) tokens.push('WBTC');
|
|
1853
1868
|
|
|
1854
1869
|
const [tokenPrices, merklCampaigns] = await Promise.all([
|
|
1855
|
-
getTokensPricesForPortfolio(tokens, provider, network),
|
|
1856
|
-
|
|
1870
|
+
getTokensPricesForPortfolio(tokens, provider, network, blockNumber),
|
|
1871
|
+
// Merkl rewards are keyed by time: a historical block resolves to its timestamp first (one RPC
|
|
1872
|
+
// call unless given, none for 'latest') so the rewards in force at that moment are attached.
|
|
1873
|
+
getBlockTimestamp(provider, blockNumber, blockTimestamp).then((asOfTimestamp) => getFluidMerklCampaigns(network, asOfTimestamp)),
|
|
1857
1874
|
]);
|
|
1858
1875
|
|
|
1859
|
-
const parsedMarketsData = await Promise.all(vaultsData.map(async (vaultData) => parseMarketData(provider, vaultData, network, tokenPrices)));
|
|
1876
|
+
const parsedMarketsData = await Promise.all(vaultsData.map(async (vaultData) => parseMarketData(provider, vaultData, network, tokenPrices, blockNumber)));
|
|
1860
1877
|
|
|
1861
1878
|
const marketsData: Record<string, FluidMarketData> = {};
|
|
1862
1879
|
parsedMarketsData.forEach((marketData, i) => {
|
|
@@ -1870,4 +1887,6 @@ export const _getAllFluidMarketDataPortfolio = async (provider: PublicClient, ne
|
|
|
1870
1887
|
export const getAllFluidMarketDataPortfolio = async (
|
|
1871
1888
|
provider: EthereumProvider,
|
|
1872
1889
|
network: NetworkNumber,
|
|
1873
|
-
|
|
1890
|
+
blockNumber: Blockish = 'latest',
|
|
1891
|
+
blockTimestamp?: number,
|
|
1892
|
+
): Promise<Record<string, FluidMarketData>> => _getAllFluidMarketDataPortfolio(getViemProvider(provider, network, { batch: { multicall: true } }), network, blockNumber, blockTimestamp);
|
package/src/fluid/merkl.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import memoize from 'memoizee';
|
|
2
2
|
import { aprToApy } from '../moneymarket';
|
|
3
|
-
import {
|
|
3
|
+
import { fetchMerklOpportunities } from '../services/merkl';
|
|
4
4
|
import {
|
|
5
5
|
FluidMarketData,
|
|
6
6
|
FluidMerklRewardMap,
|
|
@@ -66,22 +66,27 @@ export const buildFluidMerklRewardMap = (opportunities: MerklOpportunity[], chai
|
|
|
66
66
|
};
|
|
67
67
|
|
|
68
68
|
/**
|
|
69
|
-
* Fetches live vault-scoped Fluid campaigns for the chain, keyed by vault address.
|
|
70
|
-
*
|
|
69
|
+
* Fetches live vault-scoped Fluid campaigns for the chain, keyed by vault address. With
|
|
70
|
+
* `asOfTimestamp` (unix seconds) the campaigns and APRs are the ones Merkl had at that moment
|
|
71
|
+
* instead. Never throws — on any failure it returns an empty map, so Merkl being down can't break
|
|
72
|
+
* market data.
|
|
71
73
|
*/
|
|
72
|
-
export const getFluidMerklCampaigns = memoize(async (chainId: NetworkNumber): Promise<FluidMerklRewardMap> => {
|
|
74
|
+
export const getFluidMerklCampaigns = memoize(async (chainId: NetworkNumber, asOfTimestamp?: number): Promise<FluidMerklRewardMap> => {
|
|
73
75
|
try {
|
|
74
|
-
const opportunities = await
|
|
76
|
+
const opportunities = await fetchMerklOpportunities({
|
|
75
77
|
mainProtocolId: 'fluid',
|
|
78
|
+
chainId: chainId.toString(),
|
|
76
79
|
type: Object.values(FluidMerklOpportunityType).join(','),
|
|
77
80
|
status: OpportunityStatus.LIVE,
|
|
78
|
-
});
|
|
81
|
+
}, asOfTimestamp);
|
|
79
82
|
return buildFluidMerklRewardMap(opportunities, chainId);
|
|
80
83
|
} catch (e) {
|
|
81
84
|
console.error('Failed to fetch Fluid Merkl campaigns', e);
|
|
82
85
|
return {};
|
|
83
86
|
}
|
|
84
|
-
|
|
87
|
+
// memoizee keys on the declared arity, which excludes optional params — `length: 2` keeps the
|
|
88
|
+
// timestamp in the cache key so a historical lookup never serves (or is served by) the live one
|
|
89
|
+
}, { promise: true, maxAge: 60 * 1000, length: 2 });
|
|
85
90
|
|
|
86
91
|
/**
|
|
87
92
|
* Appends the vault's Merkl campaigns to its assets' incentive arrays — supply rewards on every
|
|
@@ -195,7 +195,7 @@ export const aaveAnyGetAggregatedPositionData = ({
|
|
|
195
195
|
payload.leveragedType = leveragedType;
|
|
196
196
|
payload.leveragedAsset = leveragedAsset;
|
|
197
197
|
payload.liquidationPrice = '';
|
|
198
|
-
if (
|
|
198
|
+
if (leveragedAsset !== '') {
|
|
199
199
|
let assetPrice = data.assetsData[leveragedAsset].price;
|
|
200
200
|
if (leveragedType === LeverageType.VolatilePair) {
|
|
201
201
|
const borrowedAsset = (Object.values(usedAssets) as AaveV3UsedAsset[]).find(({ borrowedUsd }: { borrowedUsd: string }) => +borrowedUsd > 0);
|
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
aprToApy,
|
|
6
6
|
calcLeverageLiqPrice,
|
|
7
7
|
getAssetsTotal,
|
|
8
|
+
getExposure,
|
|
8
9
|
STABLE_ASSETS,
|
|
9
10
|
} from '../../moneymarket';
|
|
10
11
|
import { calculateInterestEarned } from '../../staking';
|
|
@@ -198,13 +199,17 @@ export const isLeveragedPosAaveV4 = (usedAssets: AaveV4UsedReserveAssets, dustLi
|
|
|
198
199
|
let supplyUnstable = 0;
|
|
199
200
|
let longAsset = '';
|
|
200
201
|
let shortAsset = '';
|
|
202
|
+
let stableCollAsset = '';
|
|
201
203
|
Object.values(usedAssets).forEach(({
|
|
202
204
|
symbol, suppliedUsd, borrowedUsd, collateral, reserveId,
|
|
203
205
|
}) => {
|
|
204
206
|
const spokeAsset = `${symbol}-${reserveId}`;
|
|
205
207
|
const isSupplied = (+suppliedUsd) > dustLimit; // ignore dust like <limit leftover supply
|
|
206
208
|
const isBorrowed = (+borrowedUsd) > dustLimit; // ignore dust like <limit leftover supply
|
|
207
|
-
if (isSupplied && STABLE_ASSETS.includes(symbol) && collateral)
|
|
209
|
+
if (isSupplied && STABLE_ASSETS.includes(symbol) && collateral) {
|
|
210
|
+
supplyStable += 1;
|
|
211
|
+
stableCollAsset = spokeAsset;
|
|
212
|
+
}
|
|
208
213
|
if (isBorrowed && STABLE_ASSETS.includes(symbol)) borrowStable += 1;
|
|
209
214
|
if (isBorrowed && !STABLE_ASSETS.includes(symbol)) {
|
|
210
215
|
borrowUnstable += 1;
|
|
@@ -218,6 +223,7 @@ export const isLeveragedPosAaveV4 = (usedAssets: AaveV4UsedReserveAssets, dustLi
|
|
|
218
223
|
const isLong = borrowStable > 0 && borrowUnstable === 0 && supplyUnstable === 1 && supplyStable === 0;
|
|
219
224
|
const isShort = supplyStable > 0 && supplyUnstable === 0 && borrowUnstable === 1 && borrowStable === 0;
|
|
220
225
|
const isVolatilePair = supplyUnstable === 1 && borrowUnstable === 1 && supplyStable === 0 && borrowStable === 0;
|
|
226
|
+
const isStablePair = supplyStable > 0 && borrowStable > 0 && supplyUnstable === 0 && borrowUnstable === 0;
|
|
221
227
|
if (isLong) {
|
|
222
228
|
return {
|
|
223
229
|
leveragedType: LeverageType.Long,
|
|
@@ -236,6 +242,13 @@ export const isLeveragedPosAaveV4 = (usedAssets: AaveV4UsedReserveAssets, dustLi
|
|
|
236
242
|
leveragedAsset: longAsset,
|
|
237
243
|
};
|
|
238
244
|
}
|
|
245
|
+
if (isStablePair) {
|
|
246
|
+
return {
|
|
247
|
+
leveragedType: LeverageType.StablePair,
|
|
248
|
+
// with multiple stable collaterals there is no single leveraged asset (and no liquidation price)
|
|
249
|
+
leveragedAsset: supplyStable === 1 ? stableCollAsset : '',
|
|
250
|
+
};
|
|
251
|
+
}
|
|
239
252
|
return {
|
|
240
253
|
leveragedType: LeverageType.None,
|
|
241
254
|
leveragedAsset: '',
|
|
@@ -277,7 +290,7 @@ export const aaveV4GetAggregatedPositionData = ({
|
|
|
277
290
|
payload.leveragedType = leveragedType;
|
|
278
291
|
payload.leveragedAsset = leveragedAsset;
|
|
279
292
|
payload.liquidationPrice = '';
|
|
280
|
-
if (
|
|
293
|
+
if (leveragedAsset !== '') {
|
|
281
294
|
const leveragedAssetData = assetsData[leveragedAsset];
|
|
282
295
|
let assetPrice = leveragedAssetData?.price || '0';
|
|
283
296
|
if (leveragedType === LeverageType.VolatilePair) {
|
|
@@ -300,6 +313,7 @@ export const aaveV4GetAggregatedPositionData = ({
|
|
|
300
313
|
payload.collLiquidationRatio = new Dec(payload.suppliedCollateralUsd).div(payload.liquidationLimitUsd).mul(100).toString();
|
|
301
314
|
payload.healthRatio = new Dec(payload.liquidationLimitUsd).div(payload.borrowedUsd).toDP(4).toString();
|
|
302
315
|
payload.minHealthRatio = new Dec(payload.liquidationLimitUsd).div(payload.borrowLimitUsd).toDP(4).toString();
|
|
316
|
+
payload.exposure = getExposure(payload.borrowedUsd, payload.suppliedUsd);
|
|
303
317
|
|
|
304
318
|
const { netApy, incentiveUsd, totalInterestUsd } = calculateNetApyAaveV4({ usedAssets, assetsData });
|
|
305
319
|
payload.netApy = netApy;
|
|
@@ -157,7 +157,7 @@ export const getCompoundV2AggregatedData = ({
|
|
|
157
157
|
const { leveragedType, leveragedAsset } = isLeveragedPos(usedAssets);
|
|
158
158
|
payload.leveragedType = leveragedType;
|
|
159
159
|
payload.liquidationPrice = '';
|
|
160
|
-
if (
|
|
160
|
+
if (leveragedAsset !== '') {
|
|
161
161
|
payload.leveragedAsset = leveragedAsset;
|
|
162
162
|
const assetPrice = assetsData[handleWbtcLegacy(leveragedAsset)].price;
|
|
163
163
|
payload.liquidationPrice = calcLeverageLiqPrice(leveragedType, assetPrice, payload.borrowedUsd, payload.liquidationLimitUsd);
|
|
@@ -193,7 +193,7 @@ export const getCompoundV3AggregatedData = ({
|
|
|
193
193
|
const { leveragedType, leveragedAsset } = isLeveragedPos(usedAssets, selectedMarket.value === CompoundVersions.CompoundV3ETH ? 0.001 : 5);
|
|
194
194
|
payload.leveragedType = leveragedType;
|
|
195
195
|
payload.liquidationPrice = '';
|
|
196
|
-
if (
|
|
196
|
+
if (leveragedAsset !== '') {
|
|
197
197
|
payload.leveragedAsset = leveragedAsset;
|
|
198
198
|
let assetPrice = assetsData[leveragedAsset].price;
|
|
199
199
|
if (leveragedType === LeverageType.VolatilePair) {
|