@defisaver/positions-sdk 2.1.167 → 2.1.168-stable-pair-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.js +8 -29
- package/cjs/aaveV3/index.js +2 -31
- package/cjs/aaveV4/index.d.ts +25 -0
- package/cjs/aaveV4/index.js +4 -1
- package/cjs/compoundV2/index.d.ts +1 -1
- package/cjs/compoundV2/index.js +2 -2
- package/cjs/curveUsd/index.d.ts +1 -1
- package/cjs/curveUsd/index.js +6 -1
- package/cjs/fluid/index.js +13 -12
- package/cjs/helpers/aaveBalances.d.ts +5 -0
- package/cjs/helpers/aaveBalances.js +51 -0
- package/cjs/helpers/aaveV4Helpers/index.js +11 -1
- package/cjs/helpers/fluidBalances.d.ts +6 -0
- package/cjs/helpers/fluidBalances.js +36 -0
- package/cjs/liquityV2/index.d.ts +8 -0
- package/cjs/liquityV2/index.js +3 -3
- package/cjs/llamaLend/index.d.ts +1 -1
- package/cjs/llamaLend/index.js +6 -1
- package/cjs/moneymarket/moneymarketCommonService.js +12 -2
- package/cjs/morphoMidnight/index.d.ts +1 -1
- package/cjs/morphoMidnight/index.js +6 -1
- package/cjs/portfolio/index.d.ts +1 -0
- package/cjs/portfolio/index.js +3 -0
- package/cjs/portfolio/lendingSummaries.d.ts +7 -0
- package/cjs/portfolio/lendingSummaries.js +262 -0
- package/cjs/portfolio/lendingSummaryTransport.d.ts +8 -0
- package/cjs/portfolio/lendingSummaryTransport.js +109 -0
- package/cjs/spark/index.js +2 -31
- package/cjs/types/common.d.ts +1 -0
- package/cjs/types/common.js +1 -0
- package/cjs/types/index.d.ts +1 -0
- package/cjs/types/index.js +1 -0
- package/cjs/types/lendingSummary.d.ts +34 -0
- package/cjs/types/lendingSummary.js +2 -0
- package/esm/aaveV2/index.js +11 -32
- package/esm/aaveV3/index.js +3 -32
- package/esm/aaveV4/index.d.ts +25 -0
- package/esm/aaveV4/index.js +3 -1
- package/esm/compoundV2/index.d.ts +1 -1
- package/esm/compoundV2/index.js +2 -2
- package/esm/curveUsd/index.d.ts +1 -1
- package/esm/curveUsd/index.js +6 -1
- package/esm/fluid/index.js +13 -12
- package/esm/helpers/aaveBalances.d.ts +5 -0
- package/esm/helpers/aaveBalances.js +45 -0
- package/esm/helpers/aaveV4Helpers/index.js +11 -1
- package/esm/helpers/fluidBalances.d.ts +6 -0
- package/esm/helpers/fluidBalances.js +28 -0
- package/esm/liquityV2/index.d.ts +8 -0
- package/esm/liquityV2/index.js +1 -1
- package/esm/llamaLend/index.d.ts +1 -1
- package/esm/llamaLend/index.js +6 -1
- package/esm/moneymarket/moneymarketCommonService.js +12 -2
- package/esm/morphoMidnight/index.d.ts +1 -1
- package/esm/morphoMidnight/index.js +6 -1
- package/esm/portfolio/index.d.ts +1 -0
- package/esm/portfolio/index.js +1 -0
- package/esm/portfolio/lendingSummaries.d.ts +7 -0
- package/esm/portfolio/lendingSummaries.js +256 -0
- package/esm/portfolio/lendingSummaryTransport.d.ts +8 -0
- package/esm/portfolio/lendingSummaryTransport.js +104 -0
- package/esm/spark/index.js +4 -33
- package/esm/types/common.d.ts +1 -0
- package/esm/types/common.js +1 -0
- package/esm/types/index.d.ts +1 -0
- package/esm/types/index.js +1 -0
- package/esm/types/lendingSummary.d.ts +34 -0
- package/esm/types/lendingSummary.js +1 -0
- package/package.json +1 -1
- package/src/aaveV2/index.ts +8 -48
- package/src/aaveV3/index.ts +2 -48
- package/src/aaveV4/index.ts +6 -1
- package/src/compoundV2/index.ts +2 -2
- package/src/curveUsd/index.ts +6 -1
- package/src/fluid/index.ts +13 -12
- package/src/helpers/aaveBalances.ts +41 -0
- package/src/helpers/aaveV4Helpers/index.ts +12 -1
- package/src/helpers/fluidBalances.ts +34 -0
- package/src/liquityV2/index.ts +1 -1
- package/src/llamaLend/index.ts +6 -1
- package/src/moneymarket/moneymarketCommonService.ts +13 -2
- package/src/morphoMidnight/index.ts +6 -1
- package/src/portfolio/index.ts +2 -0
- package/src/portfolio/lendingSummaries.ts +216 -0
- package/src/portfolio/lendingSummaryTransport.ts +84 -0
- package/src/spark/index.ts +3 -49
- package/src/types/common.ts +1 -0
- package/src/types/index.ts +2 -1
- package/src/types/lendingSummary.ts +41 -0
package/src/aaveV4/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Client } from 'viem';
|
|
2
2
|
import Dec from 'decimal.js';
|
|
3
3
|
import { assetAmountInEth, assetAmountInWei, getAssetInfoByAddress } from '@defisaver/tokens';
|
|
4
|
-
import { getViemProvider } from '../services/viem';
|
|
4
|
+
import { getViemProvider, setViemBlockNumber } from '../services/viem';
|
|
5
5
|
import {
|
|
6
6
|
AaveV4AccountData,
|
|
7
7
|
AaveV4HubAssetOnChainData,
|
|
@@ -275,6 +275,11 @@ export async function _getAaveV4AccountData(provider: Client, network: NetworkNu
|
|
|
275
275
|
};
|
|
276
276
|
}
|
|
277
277
|
|
|
278
|
+
/** Shared raw read; no spoke metadata or market enrichment. */
|
|
279
|
+
export const _getAaveV4LoanBalances = (provider: Client, network: NetworkNumber, block: Blockish, address: EthAddress, spoke: EthAddress) => (
|
|
280
|
+
AaveV4ViewContractViem(provider, network, block).read.getLoanData([spoke, address], setViemBlockNumber(block))
|
|
281
|
+
);
|
|
282
|
+
|
|
278
283
|
/**
|
|
279
284
|
* Lightweight existence check for Loan Shifter / UI discovery.
|
|
280
285
|
* Reads only `getLoanData(spoke, user)` and returns whether any reserve has
|
package/src/compoundV2/index.ts
CHANGED
|
@@ -116,7 +116,7 @@ const getAllMarketAddresses = async (provider: Client, network: NetworkNumber, b
|
|
|
116
116
|
};
|
|
117
117
|
|
|
118
118
|
|
|
119
|
-
export const _getCompoundV2AccountBalances = async (provider: Client, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress): Promise<PositionBalances> => {
|
|
119
|
+
export const _getCompoundV2AccountBalances = async (provider: Client, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress, tokens?: EthAddress[]): Promise<PositionBalances> => {
|
|
120
120
|
let balances: PositionBalances = {
|
|
121
121
|
collateral: {},
|
|
122
122
|
debt: {},
|
|
@@ -126,7 +126,7 @@ export const _getCompoundV2AccountBalances = async (provider: Client, network: N
|
|
|
126
126
|
return balances;
|
|
127
127
|
}
|
|
128
128
|
|
|
129
|
-
const assets = await getAllMarketAddresses(provider, network, block);
|
|
129
|
+
const assets = tokens || await getAllMarketAddresses(provider, network, block);
|
|
130
130
|
const assetInfo = assets.map(a => getAssetInfoByAddress(a, network));
|
|
131
131
|
const loanInfoContract = CompoundLoanInfoContractViem(provider, network, block);
|
|
132
132
|
const loanInfo = await loanInfoContract.read.getTokenBalances([address, assets], setViemBlockNumber(block));
|
package/src/curveUsd/index.ts
CHANGED
|
@@ -128,7 +128,7 @@ const getStatusForUser = (bandRange: string[], activeBand: string, crvUSDSupplie
|
|
|
128
128
|
return CrvUSDStatus.Nonexistant;
|
|
129
129
|
};
|
|
130
130
|
|
|
131
|
-
export const _getCrvUsdAccountBalances = async (provider: Client, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress, controllerAddress: EthAddress): Promise<PositionBalances> => {
|
|
131
|
+
export const _getCrvUsdAccountBalances = async (provider: Client, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress, controllerAddress: EthAddress, includeDeposited = false): Promise<PositionBalances> => {
|
|
132
132
|
let balances: PositionBalances = {
|
|
133
133
|
collateral: {},
|
|
134
134
|
debt: {},
|
|
@@ -152,6 +152,11 @@ export const _getCrvUsdAccountBalances = async (provider: Client, network: Netwo
|
|
|
152
152
|
},
|
|
153
153
|
};
|
|
154
154
|
|
|
155
|
+
if (includeDeposited) {
|
|
156
|
+
balances.deposited = {
|
|
157
|
+
[addressMapping ? getAssetInfo(wethToEth(selectedMarket.baseAsset), network).address.toLowerCase() : wethToEth(selectedMarket.baseAsset)]: data.curveUsdCollateralAmount.toString(),
|
|
158
|
+
};
|
|
159
|
+
}
|
|
155
160
|
return balances;
|
|
156
161
|
};
|
|
157
162
|
|
package/src/fluid/index.ts
CHANGED
|
@@ -3,6 +3,7 @@ import {
|
|
|
3
3
|
assetAmountInEth, AssetData, getAssetInfo, getAssetInfoByAddress,
|
|
4
4
|
} from '@defisaver/tokens';
|
|
5
5
|
import { Client, PublicClient } from 'viem';
|
|
6
|
+
import { fluidTokenAmount } from '../helpers/fluidBalances';
|
|
6
7
|
import {
|
|
7
8
|
EthAddress, EthereumProvider, IncentiveKind, NetworkNumber,
|
|
8
9
|
} from '../types/common';
|
|
@@ -1178,8 +1179,8 @@ const parseT1UserData = (userPositionData: FluidUserPositionStructOutputStruct,
|
|
|
1178
1179
|
const debtAsset = getAssetInfo(marketData.debtAsset0);
|
|
1179
1180
|
|
|
1180
1181
|
// for T2 and T4 - this is the number of shares
|
|
1181
|
-
const supplied =
|
|
1182
|
-
const borrowed =
|
|
1182
|
+
const supplied = fluidTokenAmount(userPositionData.supply, collAsset.decimals);
|
|
1183
|
+
const borrowed = fluidTokenAmount(userPositionData.borrow, debtAsset.decimals);
|
|
1183
1184
|
|
|
1184
1185
|
const collUsedAsset: FluidUsedAsset = {
|
|
1185
1186
|
...EMPTY_USED_ASSET,
|
|
@@ -1234,10 +1235,10 @@ const parseT2UserData = (userPositionData: FluidUserPositionStructOutputStruct,
|
|
|
1234
1235
|
const debtAsset = getAssetInfo(marketData.debtAsset0);
|
|
1235
1236
|
|
|
1236
1237
|
const supplyShares = getEthAmountForDecimals(userPositionData.supply.toString(), 18); // this is supplied in coll shares
|
|
1237
|
-
const borrowed =
|
|
1238
|
+
const borrowed = fluidTokenAmount(userPositionData.borrow, debtAsset.decimals); // this is actual token borrow
|
|
1238
1239
|
|
|
1239
|
-
const supplied0 =
|
|
1240
|
-
const supplied1 =
|
|
1240
|
+
const supplied0 = fluidTokenAmount(userPositionData.supply, collAsset0.decimals, assetsData[collAsset0.symbol].tokenPerSupplyShare!);
|
|
1241
|
+
const supplied1 = fluidTokenAmount(userPositionData.supply, collAsset1.decimals, assetsData[collAsset1.symbol].tokenPerSupplyShare!);
|
|
1241
1242
|
|
|
1242
1243
|
const collUsedAsset0: Partial<FluidUsedAsset> = {
|
|
1243
1244
|
symbol: collAsset0.symbol,
|
|
@@ -1302,11 +1303,11 @@ const parseT3UserData = (userPositionData: FluidUserPositionStructOutputStruct,
|
|
|
1302
1303
|
const debtAsset0 = getAssetInfo(marketData.debtAsset0);
|
|
1303
1304
|
const debtAsset1 = getAssetInfo(marketData.debtAsset1);
|
|
1304
1305
|
|
|
1305
|
-
const supplied =
|
|
1306
|
+
const supplied = fluidTokenAmount(userPositionData.supply, collAsset.decimals); // this is actual token supply
|
|
1306
1307
|
const borrowShares = getEthAmountForDecimals(userPositionData.borrow.toString(), 18); // this is actual token borrow
|
|
1307
1308
|
|
|
1308
|
-
const borrowed0 =
|
|
1309
|
-
const borrowed1 =
|
|
1309
|
+
const borrowed0 = fluidTokenAmount(userPositionData.borrow, debtAsset0.decimals, assetsData[debtAsset0.symbol].tokenPerBorrowShare!);
|
|
1310
|
+
const borrowed1 = fluidTokenAmount(userPositionData.borrow, debtAsset1.decimals, assetsData[debtAsset1.symbol].tokenPerBorrowShare!);
|
|
1310
1311
|
|
|
1311
1312
|
const collUsedAsset: Partial<FluidUsedAsset> = {
|
|
1312
1313
|
symbol: collAsset.symbol,
|
|
@@ -1374,11 +1375,11 @@ const parseT4UserData = (userPositionData: FluidUserPositionStructOutputStruct,
|
|
|
1374
1375
|
const supplyShares = getEthAmountForDecimals(userPositionData.supply.toString(), 18); // this is actual token supply
|
|
1375
1376
|
const borrowShares = getEthAmountForDecimals(userPositionData.borrow.toString(), 18); // this is actual token borrow
|
|
1376
1377
|
|
|
1377
|
-
const supplied0 =
|
|
1378
|
-
const supplied1 =
|
|
1378
|
+
const supplied0 = fluidTokenAmount(userPositionData.supply, collAsset0.decimals, assetsData[collAsset0.symbol].tokenPerSupplyShare!);
|
|
1379
|
+
const supplied1 = fluidTokenAmount(userPositionData.supply, collAsset1.decimals, assetsData[collAsset1.symbol].tokenPerSupplyShare!);
|
|
1379
1380
|
|
|
1380
|
-
const borrowed0 =
|
|
1381
|
-
const borrowed1 =
|
|
1381
|
+
const borrowed0 = fluidTokenAmount(userPositionData.borrow, debtAsset0.decimals, assetsData[debtAsset0.symbol].tokenPerBorrowShare!);
|
|
1382
|
+
const borrowed1 = fluidTokenAmount(userPositionData.borrow, debtAsset1.decimals, assetsData[debtAsset1.symbol].tokenPerBorrowShare!);
|
|
1382
1383
|
|
|
1383
1384
|
const collUsedAsset0: Partial<FluidUsedAsset> = {
|
|
1384
1385
|
symbol: collAsset0.symbol,
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import Dec from 'decimal.js';
|
|
2
|
+
import { getAssetInfoByAddress } from '@defisaver/tokens';
|
|
3
|
+
import { Client } from 'viem';
|
|
4
|
+
import { AaveMarketInfo, SparkMarketData } from '../types';
|
|
5
|
+
import {
|
|
6
|
+
Blockish, EthAddress, NetworkNumber, PositionBalances,
|
|
7
|
+
} from '../types/common';
|
|
8
|
+
import { createViemContractFromConfigFunc } from '../contracts';
|
|
9
|
+
import { setViemBlockNumber } from '../services/viem';
|
|
10
|
+
import { wethToEth, wethToEthByAddress } from '../services/utils';
|
|
11
|
+
|
|
12
|
+
/** Raw token balances shared by Aave/Spark getters and lending previews. */
|
|
13
|
+
export async function getAaveLikeBalances(
|
|
14
|
+
client: Client, network: NetworkNumber, block: Blockish, addressMapping: boolean,
|
|
15
|
+
address: EthAddress, market: Pick<AaveMarketInfo | SparkMarketData, 'providerAddress' | 'protocolData' | 'protocolDataAddress'>,
|
|
16
|
+
view: 'AaveLoanInfoV2' | 'AaveV3View' | 'SparkView', tokens?: EthAddress[],
|
|
17
|
+
): Promise<PositionBalances> {
|
|
18
|
+
const balances: PositionBalances = { collateral: {}, debt: {} };
|
|
19
|
+
if (!address) return balances;
|
|
20
|
+
let reserves: { tokenAddress: EthAddress; symbol: string }[];
|
|
21
|
+
if (tokens) reserves = tokens.map(tokenAddress => ({ tokenAddress, symbol: getAssetInfoByAddress(tokenAddress, network).symbol }));
|
|
22
|
+
else {
|
|
23
|
+
if (!market.protocolData) throw new Error('Missing protocol data provider');
|
|
24
|
+
reserves = [...await createViemContractFromConfigFunc(market.protocolData, market.protocolDataAddress as EthAddress)(client, network)
|
|
25
|
+
.read.getAllReservesTokens(setViemBlockNumber(block))];
|
|
26
|
+
}
|
|
27
|
+
const addresses = reserves.map(token => token.tokenAddress);
|
|
28
|
+
const contract = createViemContractFromConfigFunc(view)(client, network, block);
|
|
29
|
+
const middle = tokens ? Math.ceil(addresses.length / 2) : Math.floor(addresses.length / 2);
|
|
30
|
+
// Preserve legacy V2 single-call and V3/Spark two-call behavior.
|
|
31
|
+
const parts = !tokens && view === 'AaveLoanInfoV2' ? [addresses] : [addresses.slice(0, middle), addresses.slice(middle)];
|
|
32
|
+
const data = (await Promise.all((tokens ? parts.filter(part => part.length) : parts)
|
|
33
|
+
.map(part => contract.read.getTokenBalances([market.providerAddress, address, part], setViemBlockNumber(block))))).flat();
|
|
34
|
+
data.forEach((token, i) => {
|
|
35
|
+
// The views leave token.token unset; input order identifies the reserve.
|
|
36
|
+
const key = addressMapping ? wethToEthByAddress(reserves[i].tokenAddress, network).toLowerCase() : wethToEth(reserves[i].symbol);
|
|
37
|
+
balances.collateral![key] = token.balance.toString();
|
|
38
|
+
balances.debt![key] = new Dec(token.borrowsStable.toString()).add(token.borrowsVariable.toString()).toString();
|
|
39
|
+
});
|
|
40
|
+
return balances;
|
|
41
|
+
}
|
|
@@ -198,13 +198,17 @@ export const isLeveragedPosAaveV4 = (usedAssets: AaveV4UsedReserveAssets, dustLi
|
|
|
198
198
|
let supplyUnstable = 0;
|
|
199
199
|
let longAsset = '';
|
|
200
200
|
let shortAsset = '';
|
|
201
|
+
let stableCollAsset = '';
|
|
201
202
|
Object.values(usedAssets).forEach(({
|
|
202
203
|
symbol, suppliedUsd, borrowedUsd, collateral, reserveId,
|
|
203
204
|
}) => {
|
|
204
205
|
const spokeAsset = `${symbol}-${reserveId}`;
|
|
205
206
|
const isSupplied = (+suppliedUsd) > dustLimit; // ignore dust like <limit leftover supply
|
|
206
207
|
const isBorrowed = (+borrowedUsd) > dustLimit; // ignore dust like <limit leftover supply
|
|
207
|
-
if (isSupplied && STABLE_ASSETS.includes(symbol) && collateral)
|
|
208
|
+
if (isSupplied && STABLE_ASSETS.includes(symbol) && collateral) {
|
|
209
|
+
supplyStable += 1;
|
|
210
|
+
stableCollAsset = spokeAsset;
|
|
211
|
+
}
|
|
208
212
|
if (isBorrowed && STABLE_ASSETS.includes(symbol)) borrowStable += 1;
|
|
209
213
|
if (isBorrowed && !STABLE_ASSETS.includes(symbol)) {
|
|
210
214
|
borrowUnstable += 1;
|
|
@@ -218,6 +222,7 @@ export const isLeveragedPosAaveV4 = (usedAssets: AaveV4UsedReserveAssets, dustLi
|
|
|
218
222
|
const isLong = borrowStable > 0 && borrowUnstable === 0 && supplyUnstable === 1 && supplyStable === 0;
|
|
219
223
|
const isShort = supplyStable > 0 && supplyUnstable === 0 && borrowUnstable === 1 && borrowStable === 0;
|
|
220
224
|
const isVolatilePair = supplyUnstable === 1 && borrowUnstable === 1 && supplyStable === 0 && borrowStable === 0;
|
|
225
|
+
const isStablePair = supplyStable === 1 && borrowStable === 1 && supplyUnstable === 0 && borrowUnstable === 0;
|
|
221
226
|
if (isLong) {
|
|
222
227
|
return {
|
|
223
228
|
leveragedType: LeverageType.Long,
|
|
@@ -236,6 +241,12 @@ export const isLeveragedPosAaveV4 = (usedAssets: AaveV4UsedReserveAssets, dustLi
|
|
|
236
241
|
leveragedAsset: longAsset,
|
|
237
242
|
};
|
|
238
243
|
}
|
|
244
|
+
if (isStablePair) {
|
|
245
|
+
return {
|
|
246
|
+
leveragedType: LeverageType.StablePair,
|
|
247
|
+
leveragedAsset: stableCollAsset,
|
|
248
|
+
};
|
|
249
|
+
}
|
|
239
250
|
return {
|
|
240
251
|
leveragedType: LeverageType.None,
|
|
241
252
|
leveragedAsset: '',
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import Dec from 'decimal.js';
|
|
2
|
+
import { getAssetInfoByAddress } from '@defisaver/tokens';
|
|
3
|
+
import { FluidUserPositionStructOutputStruct, FluidVaultDataStructOutputStruct } from '../types';
|
|
4
|
+
import { NetworkNumber, PositionBalances } from '../types/common';
|
|
5
|
+
|
|
6
|
+
/** Convert raw tokens, or raw 18-decimal shares with a human-readable token/share ratio. */
|
|
7
|
+
export const fluidTokenAmount = (amount: bigint, decimals: number, perShare?: string): string => (
|
|
8
|
+
new Dec(amount.toString()).div(new Dec(10).pow(perShare === undefined ? decimals : 18)).mul(perShare ?? 1).toString()
|
|
9
|
+
);
|
|
10
|
+
|
|
11
|
+
/** The same token/share conversion used by the full Fluid user parsers, without prices or yield. */
|
|
12
|
+
export function parseFluidBalances(position: FluidUserPositionStructOutputStruct, vault: FluidVaultDataStructOutputStruct, network: NetworkNumber): PositionBalances {
|
|
13
|
+
const type = Number(vault.vaultType);
|
|
14
|
+
if (![10000, 20000, 30000, 40000].includes(type)) throw new Error('Unsupported Fluid vault');
|
|
15
|
+
const side = (amount: bigint, tokens: string[], ratios: bigint[] | undefined) => {
|
|
16
|
+
const balances: Record<string, string> = {};
|
|
17
|
+
tokens.forEach((address, i) => {
|
|
18
|
+
const token = getAssetInfoByAddress(address, network);
|
|
19
|
+
if (!Number.isFinite(token.decimals) || !token.symbol) throw new Error('Fluid token metadata unavailable');
|
|
20
|
+
const perShare = ratios ? fluidTokenAmount(ratios[i], token.decimals) : undefined;
|
|
21
|
+
const value = fluidTokenAmount(amount, token.decimals, perShare);
|
|
22
|
+
balances[token.symbol] = new Dec(balances[token.symbol] || 0).add(value).toString();
|
|
23
|
+
});
|
|
24
|
+
return balances;
|
|
25
|
+
};
|
|
26
|
+
const smartSupply = type === 20000 || type === 40000;
|
|
27
|
+
const smartDebt = type === 30000 || type === 40000;
|
|
28
|
+
return {
|
|
29
|
+
collateral: side(position.supply, smartSupply ? [vault.supplyToken0, vault.supplyToken1] : [vault.supplyToken0],
|
|
30
|
+
smartSupply ? [vault.dexSupplyData.token0PerSupplyShare, vault.dexSupplyData.token1PerSupplyShare] : undefined),
|
|
31
|
+
debt: side(position.borrow, smartDebt ? [vault.borrowToken0, vault.borrowToken1] : [vault.borrowToken0],
|
|
32
|
+
smartDebt ? [vault.dexBorrowData.token0PerBorrowShare, vault.dexBorrowData.token1PerBorrowShare] : undefined),
|
|
33
|
+
};
|
|
34
|
+
}
|
package/src/liquityV2/index.ts
CHANGED
|
@@ -152,7 +152,7 @@ const getUserTroves = async (
|
|
|
152
152
|
return getUserTroves(provider, network, isLegacy, account, marketAddress, newStartIndex, newStartIndex + 100, existingTroves);
|
|
153
153
|
};
|
|
154
154
|
|
|
155
|
-
const nftContractCreationBlockMapping = {
|
|
155
|
+
export const nftContractCreationBlockMapping = {
|
|
156
156
|
[LiquityV2Versions.LiquityV2Eth]: 22516079,
|
|
157
157
|
[LiquityV2Versions.LiquityV2WstEth]: 22516099,
|
|
158
158
|
[LiquityV2Versions.LiquityV2REth]: 22516118,
|
package/src/llamaLend/index.ts
CHANGED
|
@@ -168,7 +168,7 @@ const getStatusForUser = (bandRange: string[], activeBand: string, debtSupplied:
|
|
|
168
168
|
return LlamaLendStatus.Nonexistant;
|
|
169
169
|
};
|
|
170
170
|
|
|
171
|
-
export const _getLlamaLendAccountBalances = async (provider: Client, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress, controllerAddress: EthAddress): Promise<PositionBalances> => {
|
|
171
|
+
export const _getLlamaLendAccountBalances = async (provider: Client, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress, controllerAddress: EthAddress, includeDeposited = false): Promise<PositionBalances> => {
|
|
172
172
|
let balances: PositionBalances = {
|
|
173
173
|
collateral: {},
|
|
174
174
|
debt: {},
|
|
@@ -192,6 +192,11 @@ export const _getLlamaLendAccountBalances = async (provider: Client, network: Ne
|
|
|
192
192
|
},
|
|
193
193
|
};
|
|
194
194
|
|
|
195
|
+
if (includeDeposited) {
|
|
196
|
+
balances.deposited = {
|
|
197
|
+
[addressMapping ? getAssetInfo(wethToEth(selectedMarket.baseAsset), network).address.toLowerCase() : wethToEth(selectedMarket.baseAsset)]: data.debtTokenCollateralAmount.toString(),
|
|
198
|
+
};
|
|
199
|
+
}
|
|
195
200
|
return balances;
|
|
196
201
|
};
|
|
197
202
|
|
|
@@ -13,7 +13,7 @@ export const calcShortLiqPrice = (assetPrice: string, borrowedUsd: string, borro
|
|
|
13
13
|
|
|
14
14
|
export const calcLeverageLiqPrice = (leverageType: LeverageType, assetPrice: string, borrowedUsd: string, borrowLimitUsd: string) => {
|
|
15
15
|
if (leverageType === LeverageType.Short || leverageType === LeverageType.VolatilePairReverse) return calcShortLiqPrice(assetPrice, borrowedUsd, borrowLimitUsd);
|
|
16
|
-
if (leverageType === LeverageType.Long || leverageType === LeverageType.VolatilePair) return calcLongLiqPrice(assetPrice, borrowedUsd, borrowLimitUsd);
|
|
16
|
+
if (leverageType === LeverageType.Long || leverageType === LeverageType.VolatilePair || leverageType === LeverageType.StablePair) return calcLongLiqPrice(assetPrice, borrowedUsd, borrowLimitUsd);
|
|
17
17
|
console.error('invalid leverageType', leverageType);
|
|
18
18
|
return '0';
|
|
19
19
|
};
|
|
@@ -37,12 +37,16 @@ export const isLeveragedPos = (usedAssets: MMUsedAssets, dustLimit = 5) => {
|
|
|
37
37
|
let supplyUnstable = 0;
|
|
38
38
|
let longAsset = '';
|
|
39
39
|
let shortAsset = '';
|
|
40
|
+
let stableCollAsset = '';
|
|
40
41
|
Object.values(usedAssets).forEach(({
|
|
41
42
|
symbol, suppliedUsd, borrowedUsd, collateral,
|
|
42
43
|
}) => {
|
|
43
44
|
const isSupplied = (+suppliedUsd) > dustLimit; // ignore dust like <limit leftover supply
|
|
44
45
|
const isBorrowed = (+borrowedUsd) > dustLimit; // ignore dust like <limit leftover supply
|
|
45
|
-
if (isSupplied && STABLE_ASSETS.includes(symbol) && collateral)
|
|
46
|
+
if (isSupplied && STABLE_ASSETS.includes(symbol) && collateral) {
|
|
47
|
+
supplyStable += 1;
|
|
48
|
+
stableCollAsset = symbol;
|
|
49
|
+
}
|
|
46
50
|
if (isBorrowed && STABLE_ASSETS.includes(symbol)) borrowStable += 1;
|
|
47
51
|
if (isBorrowed && !STABLE_ASSETS.includes(symbol)) {
|
|
48
52
|
borrowUnstable += 1;
|
|
@@ -56,6 +60,7 @@ export const isLeveragedPos = (usedAssets: MMUsedAssets, dustLimit = 5) => {
|
|
|
56
60
|
const isLong = borrowStable > 0 && borrowUnstable === 0 && supplyUnstable === 1 && supplyStable === 0;
|
|
57
61
|
const isShort = supplyStable > 0 && supplyUnstable === 0 && borrowUnstable === 1 && borrowStable === 0;
|
|
58
62
|
const isVolatilePair = supplyUnstable === 1 && borrowUnstable === 1 && supplyStable === 0 && borrowStable === 0;
|
|
63
|
+
const isStablePair = supplyStable === 1 && borrowStable === 1 && supplyUnstable === 0 && borrowUnstable === 0;
|
|
59
64
|
if (isLong) {
|
|
60
65
|
return {
|
|
61
66
|
leveragedType: LeverageType.Long,
|
|
@@ -74,6 +79,12 @@ export const isLeveragedPos = (usedAssets: MMUsedAssets, dustLimit = 5) => {
|
|
|
74
79
|
leveragedAsset: longAsset,
|
|
75
80
|
};
|
|
76
81
|
}
|
|
82
|
+
if (isStablePair) {
|
|
83
|
+
return {
|
|
84
|
+
leveragedType: LeverageType.StablePair,
|
|
85
|
+
leveragedAsset: stableCollAsset,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
77
88
|
return {
|
|
78
89
|
leveragedType: LeverageType.None,
|
|
79
90
|
leveragedAsset: '',
|
|
@@ -222,7 +222,7 @@ export async function getMorphoMidnightAccountData(provider: EthereumProvider, n
|
|
|
222
222
|
return _getMorphoMidnightAccountData(getViemProvider(provider, network), network, account, selectedMarket, marketInfo);
|
|
223
223
|
}
|
|
224
224
|
|
|
225
|
-
export const _getMorphoMidnightAccountBalances = async (provider: Client, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress, selectedMarket: MorphoMidnightMarketData): Promise<PositionBalances> => {
|
|
225
|
+
export const _getMorphoMidnightAccountBalances = async (provider: Client, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress, selectedMarket: MorphoMidnightMarketData, includeCredit = false): Promise<PositionBalances> => {
|
|
226
226
|
const balances: PositionBalances = { collateral: {}, debt: {} };
|
|
227
227
|
if (!address) return balances;
|
|
228
228
|
|
|
@@ -244,6 +244,11 @@ export const _getMorphoMidnightAccountBalances = async (provider: Client, networ
|
|
|
244
244
|
});
|
|
245
245
|
balances.collateral = collateral;
|
|
246
246
|
|
|
247
|
+
if (includeCredit) {
|
|
248
|
+
balances.deposited = {
|
|
249
|
+
[addressMapping ? loanTokenInfo.address.toLowerCase() : wethToEth(loanTokenInfo.symbol)]: assetAmountInEth(positionInfo.credit.toString(), wethToEth(loanTokenInfo.symbol)),
|
|
250
|
+
};
|
|
251
|
+
}
|
|
247
252
|
return balances;
|
|
248
253
|
};
|
|
249
254
|
|
package/src/portfolio/index.ts
CHANGED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
/* global BigInt */
|
|
2
|
+
import Dec from 'decimal.js';
|
|
3
|
+
import { getAssetInfo, getAssetInfoByAddress } from '@defisaver/tokens';
|
|
4
|
+
import {
|
|
5
|
+
Abi, custom, parseAbi, parseAbiItem,
|
|
6
|
+
} from 'viem';
|
|
7
|
+
import {
|
|
8
|
+
AaveMarkets, AaveV4Spokes, CompoundMarkets, compoundV2CollateralAssets, CrvUsdMarkets,
|
|
9
|
+
LiquityV2Markets, LlamaLendMarkets, MorphoBlueMarkets, MorphoMidnightMarkets, SparkMarkets,
|
|
10
|
+
} from '../markets';
|
|
11
|
+
import { AaveVersions, CompoundVersions } from '../types';
|
|
12
|
+
import {
|
|
13
|
+
EthAddress, EthereumProvider, NetworkNumber, PositionBalances, Amount,
|
|
14
|
+
} from '../types/common';
|
|
15
|
+
import { LendingSummaryOptions, WalletLendingSummaries, WalletLendingSummary } from '../types/lendingSummary';
|
|
16
|
+
import { ConfigKey, getConfigContractAbi, getConfigContractAddress } from '../contracts';
|
|
17
|
+
import { getViemProvider } from '../services/viem';
|
|
18
|
+
import { getWrappedNativeAssetFromUnwrapped } from '../services/utils';
|
|
19
|
+
import { getAaveLikeBalances } from '../helpers/aaveBalances';
|
|
20
|
+
import { parseFluidBalances } from '../helpers/fluidBalances';
|
|
21
|
+
import { _getAaveV4LoanBalances } from '../aaveV4';
|
|
22
|
+
import { _getCompoundV2AccountBalances } from '../compoundV2';
|
|
23
|
+
import { _getCompoundV3AccountBalances } from '../compoundV3';
|
|
24
|
+
import { _getCrvUsdAccountBalances } from '../curveUsd';
|
|
25
|
+
import { _getLlamaLendAccountBalances } from '../llamaLend';
|
|
26
|
+
import { _getMorphoBlueAccountBalances } from '../morphoBlue';
|
|
27
|
+
import { _getMorphoMidnightAccountBalances } from '../morphoMidnight';
|
|
28
|
+
import { _getLiquityAccountBalances } from '../liquity';
|
|
29
|
+
import { _getUserCdps } from '../maker';
|
|
30
|
+
import { nftContractCreationBlockMapping } from '../liquityV2';
|
|
31
|
+
import { lendingSummaryTransport } from './lendingSummaryTransport';
|
|
32
|
+
|
|
33
|
+
const nftAbi = parseAbi([
|
|
34
|
+
'function troveNFT() view returns (address)', 'function balanceOf(address) view returns (uint256)',
|
|
35
|
+
'function ownerOf(uint256) view returns (address)',
|
|
36
|
+
]);
|
|
37
|
+
const transfer = parseAbiItem('event Transfer(address indexed from, address indexed to, uint256 indexed tokenId)');
|
|
38
|
+
const units = (value: unknown, decimals = 18) => new Dec(String(value)).div(new Dec(10).pow(decimals)).toString();
|
|
39
|
+
const positive = (value: unknown) => new Dec(String(value)).gt(0);
|
|
40
|
+
const supported = <T extends { chainIds: NetworkNumber[] }>(list: Record<string, T>, network: NetworkNumber) => Object.values(list).filter(m => m.chainIds.includes(network));
|
|
41
|
+
|
|
42
|
+
/** Lending-only balances at one snapshot, valued with caller-supplied market prices.
|
|
43
|
+
* One shared client/limiter covers every wallet. Failed reads remain partial; cancellation
|
|
44
|
+
* discards results. Morpho Blue earn deposits, rewards and savings are excluded.
|
|
45
|
+
*/
|
|
46
|
+
export async function getWalletLendingSummaries(
|
|
47
|
+
provider: EthereumProvider, network: NetworkNumber, walletAddresses: EthAddress[], options: LendingSummaryOptions,
|
|
48
|
+
): Promise<WalletLendingSummaries> {
|
|
49
|
+
const addresses = [...new Set(walletAddresses.map(address => {
|
|
50
|
+
if (!/^0x[0-9a-f]{40}$/i.test(address)) throw new Error('Invalid wallet address');
|
|
51
|
+
return address.toLowerCase() as EthAddress;
|
|
52
|
+
}))];
|
|
53
|
+
const summaries: Record<string, WalletLendingSummary> = Object.fromEntries(addresses.map(address => [address, {
|
|
54
|
+
status: 'complete', positions: [], protocols: [], suppliedUsd: '0', valuationComplete: true, errors: [],
|
|
55
|
+
}]));
|
|
56
|
+
if (!addresses.length || options.signal?.aborted) return { summaries: {}, rateLimited: false };
|
|
57
|
+
const maxCalldataBytes = options.isSim ? 2_000 : 32_000;
|
|
58
|
+
const transport = lendingSummaryTransport(provider, options.signal, options.blockNumber, maxCalldataBytes);
|
|
59
|
+
const client = getViemProvider(transport.provider, network, {
|
|
60
|
+
transport: custom(transport.provider, { retryCount: 0 }), batch: { multicall: { batchSize: maxCalldataBytes } },
|
|
61
|
+
});
|
|
62
|
+
let snapshot: bigint;
|
|
63
|
+
try {
|
|
64
|
+
snapshot = options.blockNumber ?? await client.getBlockNumber();
|
|
65
|
+
transport.pinBlock(snapshot);
|
|
66
|
+
} catch (error) {
|
|
67
|
+
if (options.signal?.aborted) return { summaries: {}, rateLimited: transport.isRateLimited() };
|
|
68
|
+
for (const summary of Object.values(summaries)) { summary.status = 'failed'; summary.errors.push(String(error)); }
|
|
69
|
+
return { summaries, rateLimited: transport.isRateLimited() };
|
|
70
|
+
}
|
|
71
|
+
const block = Number(snapshot);
|
|
72
|
+
// Dynamic ABI boundary for user-scoped CDP/NFT reads; money markets use their existing readers.
|
|
73
|
+
const read = (name: ConfigKey, functionName: string, args: readonly unknown[] = []): Promise<any> => client.readContract({
|
|
74
|
+
address: getConfigContractAddress(name, network, block), abi: getConfigContractAbi(name, network, block) as Abi, functionName, args,
|
|
75
|
+
});
|
|
76
|
+
const nftRead = (address: EthAddress, functionName: 'troveNFT' | 'balanceOf' | 'ownerOf', args: readonly any[] = []): Promise<any> => client.readContract({
|
|
77
|
+
address, abi: nftAbi as Abi, functionName, args,
|
|
78
|
+
});
|
|
79
|
+
const cache = new Map<string, Promise<any>>();
|
|
80
|
+
const shared = <T>(key: string, fetch: () => Promise<T>): Promise<T> => {
|
|
81
|
+
if (!cache.has(key)) cache.set(key, fetch());
|
|
82
|
+
return cache.get(key)!;
|
|
83
|
+
};
|
|
84
|
+
const tasks: Promise<void>[] = [];
|
|
85
|
+
const tokens = new Set<EthAddress>();
|
|
86
|
+
const valuations: (() => void)[] = [];
|
|
87
|
+
let prices: Record<string, string> = {};
|
|
88
|
+
const run = async (address: string, protocol: string, key: string, work: () => Promise<void>) => {
|
|
89
|
+
try { if (!options.signal?.aborted) await work(); } catch (error) {
|
|
90
|
+
summaries[address].errors.push(`${protocol}:${key}: ${String(error).split('\n')[0]}`);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
const add = async (address: string, protocol: string, key: string, balances: PositionBalances, human = false) => {
|
|
94
|
+
const supply = Object.entries(balances.collateral || {}).concat(Object.entries(balances.deposited || {}));
|
|
95
|
+
const debt = Object.entries(balances.debt || {});
|
|
96
|
+
if (![...supply, ...debt].some(([, amount]) => positive(amount))) return;
|
|
97
|
+
const entry = {
|
|
98
|
+
id: `${protocol}:${key}`,
|
|
99
|
+
protocol,
|
|
100
|
+
suppliedUsd: (supply.some(([, amount]) => positive(amount)) ? null : '0') as string | null,
|
|
101
|
+
debtUsd: (debt.some(([, amount]) => positive(amount)) ? null : '0') as string | null,
|
|
102
|
+
};
|
|
103
|
+
summaries[address].positions.push(entry);
|
|
104
|
+
const normalize = (amounts: [string, Amount][]) => {
|
|
105
|
+
const result: Record<string, Dec> = {};
|
|
106
|
+
amounts.filter(([, amount]) => positive(amount)).forEach(([symbol, amount]) => {
|
|
107
|
+
const token = symbol.startsWith('0x') ? getAssetInfoByAddress(symbol, network) : getAssetInfo(getWrappedNativeAssetFromUnwrapped(symbol), network);
|
|
108
|
+
if (!token.address || !Number.isFinite(token.decimals)) throw new Error(`Token metadata unavailable: ${symbol}`);
|
|
109
|
+
const id = token.address.toLowerCase() as EthAddress;
|
|
110
|
+
tokens.add(id);
|
|
111
|
+
result[id] = (result[id] || new Dec(0)).add(human ? amount : units(amount, token.decimals));
|
|
112
|
+
});
|
|
113
|
+
return result;
|
|
114
|
+
};
|
|
115
|
+
const amounts = [normalize(supply), normalize(debt)];
|
|
116
|
+
valuations.push(() => {
|
|
117
|
+
(['suppliedUsd', 'debtUsd'] as const).forEach((field, i) => {
|
|
118
|
+
try {
|
|
119
|
+
entry[field] = Object.entries(amounts[i]).reduce((total, [token, amount]) => {
|
|
120
|
+
const price = new Dec(prices[token] || 0);
|
|
121
|
+
if (!price.isFinite() || !price.gt(0)) throw new Error(`Price unavailable: ${token}`);
|
|
122
|
+
return total.add(amount.mul(price));
|
|
123
|
+
}, new Dec(0)).toString();
|
|
124
|
+
} catch (error) { summaries[address].errors.push(`${entry.id}: ${String(error)}`); }
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
};
|
|
128
|
+
const balanceTask = (address: string, protocol: string, key: string, fetch: () => Promise<PositionBalances>, human = false) => tasks.push(run(address, protocol, key, async () => add(address, protocol, key, await fetch(), human)));
|
|
129
|
+
|
|
130
|
+
for (const address of addresses) {
|
|
131
|
+
const aaveMarkets = supported(AaveMarkets(network), network).filter(m => [AaveVersions.AaveV2, AaveVersions.AaveV3, AaveVersions.AaveV3Lido, AaveVersions.AaveV3Etherfi].includes(m.value));
|
|
132
|
+
for (const market of [...aaveMarkets, ...supported(SparkMarkets(network), network)]) {
|
|
133
|
+
const v2 = market.value === AaveVersions.AaveV2;
|
|
134
|
+
const spark = !aaveMarkets.some(m => m.value === market.value);
|
|
135
|
+
balanceTask(address, spark ? 'spark' : v2 ? 'aaveV2' : 'aaveV3', market.value, () => getAaveLikeBalances(client, network, block, false, address, market,
|
|
136
|
+
spark ? 'SparkView' : v2 ? 'AaveLoanInfoV2' : 'AaveV3View', market.assets.map(asset => getAssetInfo(getWrappedNativeAssetFromUnwrapped(asset), network).address as EthAddress)));
|
|
137
|
+
}
|
|
138
|
+
for (const spoke of supported(AaveV4Spokes(network), network)) {
|
|
139
|
+
balanceTask(address, 'aaveV4', spoke.value, async () => {
|
|
140
|
+
const { reserves } = await _getAaveV4LoanBalances(client, network, block, address, spoke.address);
|
|
141
|
+
const balances: PositionBalances = { collateral: {}, debt: {} };
|
|
142
|
+
reserves.forEach(r => {
|
|
143
|
+
balances.collateral![r.underlying] = new Dec(balances.collateral![r.underlying] || 0).add(r.supplied.toString()).toString();
|
|
144
|
+
balances.debt![r.underlying] = new Dec(balances.debt![r.underlying] || 0).add(r.totalDebt.toString()).toString();
|
|
145
|
+
});
|
|
146
|
+
return balances;
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
for (const market of supported(CompoundMarkets(network), network)) {
|
|
150
|
+
const v2 = market.value === CompoundVersions.CompoundV2;
|
|
151
|
+
balanceTask(address, v2 ? 'compoundV2' : 'compoundV3', market.value, () => (v2
|
|
152
|
+
? _getCompoundV2AccountBalances(client, network, block, false, address, compoundV2CollateralAssets.map(a => a.address as EthAddress))
|
|
153
|
+
: _getCompoundV3AccountBalances(client, network, block, false, address, market.baseMarketAddress)));
|
|
154
|
+
}
|
|
155
|
+
for (const market of supported(MorphoBlueMarkets(network), network)) balanceTask(address, 'morphoBlue', market.value, () => _getMorphoBlueAccountBalances(client, network, block, false, address, market), true);
|
|
156
|
+
for (const market of supported(MorphoMidnightMarkets(network), network)) balanceTask(address, 'morphoMidnight', market.value, () => _getMorphoMidnightAccountBalances(client, network, block, false, address, market, true), true);
|
|
157
|
+
if (network === NetworkNumber.Eth) for (const market of supported(CrvUsdMarkets(network), network)) balanceTask(address, 'crvUsd', market.value, () => _getCrvUsdAccountBalances(client, network, block, false, address, market.controllerAddress, true));
|
|
158
|
+
if ([NetworkNumber.Eth, NetworkNumber.Arb].includes(network)) for (const market of supported(LlamaLendMarkets(network), network)) balanceTask(address, 'llamaLend', market.value, () => _getLlamaLendAccountBalances(client, network, block, false, address, market.controllerAddress, true));
|
|
159
|
+
if (network === NetworkNumber.Eth) {
|
|
160
|
+
balanceTask(address, 'liquity', 'trove', () => _getLiquityAccountBalances(client, network, block, false, address));
|
|
161
|
+
tasks.push(run(address, 'maker', 'discovery', async () => {
|
|
162
|
+
const cdps = await _getUserCdps(client, network, address);
|
|
163
|
+
await Promise.all(cdps.map(cdp => run(address, 'maker', String(cdp.id), async () => {
|
|
164
|
+
const [collateral, debt] = await read('McdVat', 'urns', [cdp.ilk, cdp.urn]);
|
|
165
|
+
const rate = positive(debt) ? (await shared(`maker:${cdp.ilk}`, () => read('McdVat', 'ilks', [cdp.ilk])))[1] : 0;
|
|
166
|
+
await add(address, 'maker', String(cdp.id), { collateral: { [cdp.asset]: units(collateral) }, debt: { DAI: new Dec(units(debt)).mul(units(rate, 27)).toString() } }, true);
|
|
167
|
+
})));
|
|
168
|
+
}));
|
|
169
|
+
for (const market of supported(LiquityV2Markets(network), network)) {
|
|
170
|
+
tasks.push(run(address, 'liquityV2', market.value, async () => {
|
|
171
|
+
const nft = await shared(`nft:${market.value}`, () => nftRead(market.marketAddress, 'troveNFT'));
|
|
172
|
+
const count = await nftRead(nft, 'balanceOf', [address]);
|
|
173
|
+
if (count === BigInt(0)) return;
|
|
174
|
+
const logs = await client.getLogs({
|
|
175
|
+
address: nft, event: transfer, args: { to: address }, fromBlock: BigInt(nftContractCreationBlockMapping[market.value]), toBlock: snapshot,
|
|
176
|
+
});
|
|
177
|
+
const owned = (await Promise.all([...new Set(logs.map(log => log.args.tokenId!))].map(async id => {
|
|
178
|
+
try { return (await nftRead(nft, 'ownerOf', [id])).toLowerCase() === address ? id : null; } catch { return null; }
|
|
179
|
+
}))).filter((id): id is bigint => id !== null);
|
|
180
|
+
if (BigInt(owned.length) !== count) summaries[address].errors.push(`liquityV2:${market.value}: Not all owned troves could be discovered`);
|
|
181
|
+
await Promise.all(owned.map(id => run(address, 'liquityV2', `${market.value}:${id}`, async () => {
|
|
182
|
+
const data = await read(market.isLegacy ? 'LiquityV2LegacyView' : 'LiquityV2View', 'getTroveInfo', [market.marketAddress, id]);
|
|
183
|
+
if (data.owner.toLowerCase() !== address) throw new Error('Trove ownership changed');
|
|
184
|
+
await add(address, 'liquityV2', `${market.value}:${id}`, { collateral: { [market.collateralToken]: data.collAmount.toString() }, debt: { [market.debtToken]: data.debtAmount.toString() } });
|
|
185
|
+
})));
|
|
186
|
+
}));
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
if ([NetworkNumber.Eth, NetworkNumber.Arb, NetworkNumber.Base, NetworkNumber.Plasma].includes(network)) {
|
|
190
|
+
tasks.push(run(address, 'fluid', 'discovery', async () => {
|
|
191
|
+
const [positions, vaults] = await read('FluidView', 'getUserPositions', [address]);
|
|
192
|
+
await Promise.all(positions.map((p: any, i: number) => run(address, 'fluid', String(p.nftId), async () => {
|
|
193
|
+
if (positive(p.supply) || positive(p.borrow)) await add(address, 'fluid', String(p.nftId), parseFluidBalances(p, vaults[i], network), true);
|
|
194
|
+
})));
|
|
195
|
+
}));
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
await Promise.all(tasks);
|
|
199
|
+
if (options.signal?.aborted) return { summaries: {}, rateLimited: transport.isRateLimited() };
|
|
200
|
+
if (tokens.size) {
|
|
201
|
+
try { prices = await options.resolvePrices(network, [...tokens], options.signal); } catch (error) {
|
|
202
|
+
for (const summary of Object.values(summaries)) if (summary.positions.length) summary.errors.push(`Prices: ${String(error)}`);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
if (options.signal?.aborted) return { summaries: {}, rateLimited: transport.isRateLimited() };
|
|
206
|
+
valuations.forEach(value => value());
|
|
207
|
+
for (const summary of Object.values(summaries)) {
|
|
208
|
+
summary.positions.sort((a, b) => a.id.localeCompare(b.id));
|
|
209
|
+
summary.protocols = [...new Set(summary.positions.map(p => p.protocol))];
|
|
210
|
+
summary.suppliedUsd = summary.positions.reduce((total, p) => total.add(p.suppliedUsd || 0), new Dec(0)).toString();
|
|
211
|
+
summary.valuationComplete = summary.positions.every(p => p.suppliedUsd !== null && p.debtUsd !== null);
|
|
212
|
+
summary.errors = [...new Set(summary.errors)];
|
|
213
|
+
if (summary.errors.length) summary.status = 'partial';
|
|
214
|
+
}
|
|
215
|
+
return { summaries, rateLimited: transport.isRateLimited() };
|
|
216
|
+
}
|