@defisaver/positions-sdk 2.1.109-ltv-0-automations-dev → 2.1.110

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.
@@ -1,9 +1,11 @@
1
1
  import { Client } from 'viem';
2
- import { AaveV4AccountData, AaveV4SpokeData, AaveV4SpokeInfo, EthAddress, EthereumProvider, NetworkNumber } from '../types';
2
+ import { AaveV4AccountData, AaveV4SpokeData, AaveV4SpokeInfo, EthAddress, EthereumProvider, NetworkNumber, Blockish, PositionBalances } from '../types';
3
3
  export * as lend from './lend';
4
4
  export { getAaveV4MerkleCampaigns } from './merkl';
5
5
  export declare function _getAaveV4SpokeData(provider: Client, network: NetworkNumber, market: AaveV4SpokeInfo, blockNumber?: 'latest' | number): Promise<AaveV4SpokeData>;
6
6
  export declare function getAaveV4SpokeData(provider: EthereumProvider, network: NetworkNumber, spoke: AaveV4SpokeInfo, blockNumber?: 'latest' | number): Promise<AaveV4SpokeData>;
7
7
  export declare function _getAaveV4AccountData(provider: Client, network: NetworkNumber, spokeData: AaveV4SpokeData, address: EthAddress, blockNumber?: 'latest' | number): Promise<AaveV4AccountData>;
8
8
  export declare function getAaveV4AccountData(provider: EthereumProvider, network: NetworkNumber, marketData: AaveV4SpokeData, address: EthAddress, blockNumber?: 'latest' | number): Promise<any>;
9
+ export declare const _getAaveV4AccountBalances: (provider: Client, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress, spokeAddress: EthAddress, spokeData?: AaveV4SpokeData) => Promise<PositionBalances>;
10
+ export declare const getAaveV4AccountBalances: (provider: EthereumProvider, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress, spokeAddress: EthAddress, spokeData?: AaveV4SpokeData) => Promise<PositionBalances>;
9
11
  export declare function getAaveV4UnderlyingFromReserveId(provider: EthereumProvider, network: NetworkNumber, spoke: EthAddress, reserveId: number): Promise<any>;
@@ -45,7 +45,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
45
45
  return (mod && mod.__esModule) ? mod : { "default": mod };
46
46
  };
47
47
  Object.defineProperty(exports, "__esModule", { value: true });
48
- exports.getAaveV4MerkleCampaigns = exports.lend = void 0;
48
+ exports.getAaveV4AccountBalances = exports._getAaveV4AccountBalances = exports.getAaveV4MerkleCampaigns = exports.lend = void 0;
49
49
  exports._getAaveV4SpokeData = _getAaveV4SpokeData;
50
50
  exports.getAaveV4SpokeData = getAaveV4SpokeData;
51
51
  exports._getAaveV4AccountData = _getAaveV4AccountData;
@@ -285,6 +285,46 @@ function getAaveV4AccountData(provider_1, network_1, marketData_1, address_1) {
285
285
  return _getAaveV4AccountData((0, viem_1.getViemProvider)(provider, network), network, marketData, address, blockNumber);
286
286
  });
287
287
  }
288
+ const _getAaveV4AccountBalances = (provider, network, block, addressMapping, address, spokeAddress, spokeData) => __awaiter(void 0, void 0, void 0, function* () {
289
+ const balances = {
290
+ collateral: {},
291
+ debt: {},
292
+ };
293
+ if (!address || !spokeAddress) {
294
+ return balances;
295
+ }
296
+ const blockNumber = block === 'latest' ? 'latest' : Number(block);
297
+ let resolvedSpokeData = spokeData;
298
+ if (!resolvedSpokeData) {
299
+ const spokeInfo = (0, aaveV4_1.findAaveV4SpokeByAddress)(network, spokeAddress);
300
+ if (!spokeInfo) {
301
+ return balances;
302
+ }
303
+ resolvedSpokeData = yield _getAaveV4SpokeData(provider, network, spokeInfo, blockNumber);
304
+ }
305
+ const accountData = yield _getAaveV4AccountData(provider, network, resolvedSpokeData, address, blockNumber);
306
+ const finalSpokeData = resolvedSpokeData;
307
+ Object.entries(accountData.usedAssets).forEach(([key, asset]) => {
308
+ const reserveData = finalSpokeData.assetsData[key];
309
+ if (!reserveData)
310
+ return;
311
+ const balanceKey = addressMapping
312
+ ? (0, utils_1.wethToEthByAddress)(reserveData.underlying, network).toLowerCase()
313
+ : (0, utils_1.wethToEth)(asset.symbol);
314
+ if (asset.isSupplied && new decimal_js_1.default(asset.supplied || 0).gt(0)) {
315
+ balances.collateral[balanceKey] = (0, tokens_1.assetAmountInWei)(asset.supplied, asset.symbol);
316
+ }
317
+ if (asset.isBorrowed && new decimal_js_1.default(asset.borrowed || 0).gt(0)) {
318
+ balances.debt[balanceKey] = (0, tokens_1.assetAmountInWei)(asset.borrowed, asset.symbol);
319
+ }
320
+ });
321
+ return balances;
322
+ });
323
+ exports._getAaveV4AccountBalances = _getAaveV4AccountBalances;
324
+ const getAaveV4AccountBalances = (provider, network, block, addressMapping, address, spokeAddress, spokeData) => __awaiter(void 0, void 0, void 0, function* () {
325
+ return (0, exports._getAaveV4AccountBalances)((0, viem_1.getViemProvider)(provider, network), network, block, addressMapping, address, spokeAddress, spokeData);
326
+ });
327
+ exports.getAaveV4AccountBalances = getAaveV4AccountBalances;
288
328
  const _getAaveV4UnderlyingFromReserveId = (provider, network, spoke, reserveId) => __awaiter(void 0, void 0, void 0, function* () {
289
329
  const viewContract = (0, contracts_1.AaveV4ViewContractViem)(provider, network);
290
330
  const reserveData = yield viewContract.read.getReserveData([spoke, BigInt(reserveId)]);
@@ -30,17 +30,6 @@ export declare const aaveAnyGetEmodeMutableProps: ({ eModeCategory, eModeCategor
30
30
  liquidationRatio: any;
31
31
  collateralFactor: any;
32
32
  };
33
- /**
34
- * @description Per-asset effective LTV and liquidation threshold (LLTV) for the user, eMode-aware.
35
- * Mirrors AaveV3View._getUserReserveLtvAndLltv: an asset in the active eMode's ltv-zero set has
36
- * ltv 0 but KEEPS the eMode liquidation threshold (liquidations only consider LLTV). The returned
37
- * `ltv` is identical to aaveAnyGetEmodeMutableProps().collateralFactor in every branch, so a ratio
38
- * built on this matches the regular safety ratio whenever no collateral is LTV-0.
39
- */
40
- export declare const aaveAnyGetUserReserveLtvAndLltv: ({ eModeCategory, eModeCategoriesData, assetsData, }: AaveHelperCommon, _asset: string) => {
41
- ltv: string;
42
- lltv: string;
43
- };
44
33
  export declare const aaveAnyGetAggregatedPositionData: ({ usedAssets, eModeCategory, assetsData, selectedMarket, network, ...rest }: AaveHelperCommon) => AaveV3AggregatedPositionData;
45
34
  export declare const getApyAfterValuesEstimation: (selectedMarket: AaveMarketInfo, actions: [{
46
35
  action: string;
@@ -23,7 +23,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
23
23
  return (mod && mod.__esModule) ? mod : { "default": mod };
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.getAaveUnderlyingSymbol = exports.getApyAfterValuesEstimation = exports.aaveAnyGetAggregatedPositionData = exports.aaveAnyGetUserReserveLtvAndLltv = exports.aaveAnyGetEmodeMutableProps = exports.aaveAnyGetSuppliableAsCollAssets = exports.aaveAnyGetSuppliableAssets = exports.aaveAnyGetCollSuppliedAssets = exports.aaveV3IsInSiloedMode = exports.aaveV3IsInIsolationMode = exports.isAaveV3 = exports.isAaveV2 = exports.AAVE_V3_MARKETS = void 0;
26
+ exports.getAaveUnderlyingSymbol = exports.getApyAfterValuesEstimation = exports.aaveAnyGetAggregatedPositionData = exports.aaveAnyGetEmodeMutableProps = exports.aaveAnyGetSuppliableAsCollAssets = exports.aaveAnyGetSuppliableAssets = exports.aaveAnyGetCollSuppliedAssets = exports.aaveV3IsInSiloedMode = exports.aaveV3IsInIsolationMode = exports.isAaveV3 = exports.isAaveV2 = exports.AAVE_V3_MARKETS = void 0;
27
27
  const decimal_js_1 = __importDefault(require("decimal.js"));
28
28
  const tokens_1 = require("@defisaver/tokens");
29
29
  const types_1 = require("../../types");
@@ -83,33 +83,6 @@ const aaveAnyGetEmodeMutableProps = ({ eModeCategory, eModeCategoriesData, asset
83
83
  return ({ liquidationRatio, collateralFactor });
84
84
  };
85
85
  exports.aaveAnyGetEmodeMutableProps = aaveAnyGetEmodeMutableProps;
86
- /**
87
- * @description Offset subtracted from the liquidation threshold (LLTV) when crediting LTV-0 collateral
88
- * in the safety-ratio fallback. Matches AaveV3View.getSafetyRatioWithLtvZeroFallback ('LLTV - 5%').
89
- * Values are fractions (e.g. 0.8), so 5% === 0.05.
90
- */
91
- const LTV_ZERO_FALLBACK_LLTV_OFFSET = '0.05';
92
- /**
93
- * @description Per-asset effective LTV and liquidation threshold (LLTV) for the user, eMode-aware.
94
- * Mirrors AaveV3View._getUserReserveLtvAndLltv: an asset in the active eMode's ltv-zero set has
95
- * ltv 0 but KEEPS the eMode liquidation threshold (liquidations only consider LLTV). The returned
96
- * `ltv` is identical to aaveAnyGetEmodeMutableProps().collateralFactor in every branch, so a ratio
97
- * built on this matches the regular safety ratio whenever no collateral is LTV-0.
98
- */
99
- const aaveAnyGetUserReserveLtvAndLltv = ({ eModeCategory, eModeCategoriesData, assetsData, }, _asset) => {
100
- const asset = (0, utils_1.getNativeAssetFromWrapped)(_asset);
101
- const assetData = assetsData[asset];
102
- const eModeCategoryData = eModeCategoriesData === null || eModeCategoriesData === void 0 ? void 0 : eModeCategoriesData[eModeCategory];
103
- if (eModeCategory === 0
104
- || !eModeCategoryData
105
- || !eModeCategoryData.collateralAssets.includes(asset)
106
- || new decimal_js_1.default(eModeCategoryData.collateralFactor || 0).eq(0)) {
107
- return { ltv: assetData.collateralFactor, lltv: assetData.liquidationRatio };
108
- }
109
- const ltv = eModeCategoryData.ltvZeroAssets.includes(asset) ? '0' : eModeCategoryData.collateralFactor;
110
- return { ltv, lltv: eModeCategoryData.liquidationRatio };
111
- };
112
- exports.aaveAnyGetUserReserveLtvAndLltv = aaveAnyGetUserReserveLtvAndLltv;
113
86
  const aaveAnyGetAggregatedPositionData = (_a) => {
114
87
  var { usedAssets, eModeCategory, assetsData, selectedMarket, network } = _a, rest = __rest(_a, ["usedAssets", "eModeCategory", "assetsData", "selectedMarket", "network"]);
115
88
  const data = Object.assign({ usedAssets, eModeCategory, assetsData, selectedMarket, network }, rest);
@@ -123,17 +96,6 @@ const aaveAnyGetAggregatedPositionData = (_a) => {
123
96
  payload.leftToBorrowUsd = leftToBorrowUsd.lte('0') ? '0' : leftToBorrowUsd.toString();
124
97
  payload.ratio = +payload.suppliedUsd ? new decimal_js_1.default(payload.borrowLimitUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
125
98
  payload.collRatio = +payload.suppliedUsd ? new decimal_js_1.default(payload.suppliedCollateralUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
126
- // Safety ratio as evaluated by the automation bots: LTV-0 collateral is credited at (LLTV - 5%)
127
- // instead of 0 (AaveV3View.getSafetyRatioWithLtvZeroFallback). Equals `ratio` when no collateral
128
- // is LTV-0. Computed off-chain here so it is available for after-value simulations too.
129
- payload.borrowLimitWithLtvZeroFallbackUsd = (0, moneymarket_1.getAssetsTotal)(usedAssets, ({ isSupplied, collateral }) => isSupplied && collateral, ({ symbol, suppliedUsd }) => {
130
- const { ltv, lltv } = (0, exports.aaveAnyGetUserReserveLtvAndLltv)(data, symbol);
131
- const effectiveLtv = new decimal_js_1.default(ltv).eq(0)
132
- ? decimal_js_1.default.max(0, new decimal_js_1.default(lltv).sub(LTV_ZERO_FALLBACK_LLTV_OFFSET))
133
- : new decimal_js_1.default(ltv);
134
- return new decimal_js_1.default(suppliedUsd).mul(effectiveLtv);
135
- });
136
- payload.safetyRatioWithLtvZeroFallback = +payload.suppliedUsd ? new decimal_js_1.default(payload.borrowLimitWithLtvZeroFallbackUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
137
99
  payload.liqRatio = new decimal_js_1.default(payload.borrowLimitUsd).div(payload.liquidationLimitUsd).toString();
138
100
  payload.liqPercent = new decimal_js_1.default(payload.borrowLimitUsd).div(payload.liquidationLimitUsd).mul(100).toString();
139
101
  const { leveragedType, leveragedAsset } = (0, moneymarket_1.isLeveragedPos)(usedAssets);
@@ -32,3 +32,4 @@ export declare const AaveV4Spokes: (networkId: NetworkNumber) => {
32
32
  readonly aave_v4_main_spoke: AaveV4SpokeInfo;
33
33
  };
34
34
  export declare const getAaveV4SpokeTypeInfo: (type: AaveV4SpokesType, network?: NetworkNumber) => AaveV4SpokeInfo;
35
+ export declare const findAaveV4SpokeByAddress: (networkId: NetworkNumber, address: string) => AaveV4SpokeInfo | undefined;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getAaveV4SpokeTypeInfo = exports.AaveV4Spokes = exports.AAVE_V4_MAIN_SPOKE = exports.AAVE_V4_LOMBARD_BTC_SPOKE = exports.AAVE_V4_LIDO_SPOKE = exports.AAVE_V4_KELP_SPOKE = exports.AAVE_V4_GOLD_SPOKE = exports.AAVE_V4_FOREX_SPOKE = exports.AAVE_V4_ETHERFI_SPOKE = exports.AAVE_V4_ETHENA_ECOSYSTEM_SPOKE = exports.AAVE_V4_ETHENA_CORRELATED_SPOKE = exports.AAVE_V4_BLUECHIP_SPOKE = exports.getAaveV4HubByAddress = exports.getAaveV4HubTypeInfo = exports.AaveV4Hubs = exports.AAVE_V4_PRIME_HUB = exports.AAVE_V4_PLUS_HUB = exports.AAVE_V4_CORE_HUB = void 0;
3
+ exports.findAaveV4SpokeByAddress = exports.getAaveV4SpokeTypeInfo = exports.AaveV4Spokes = exports.AAVE_V4_MAIN_SPOKE = exports.AAVE_V4_LOMBARD_BTC_SPOKE = exports.AAVE_V4_LIDO_SPOKE = exports.AAVE_V4_KELP_SPOKE = exports.AAVE_V4_GOLD_SPOKE = exports.AAVE_V4_FOREX_SPOKE = exports.AAVE_V4_ETHERFI_SPOKE = exports.AAVE_V4_ETHENA_ECOSYSTEM_SPOKE = exports.AAVE_V4_ETHENA_CORRELATED_SPOKE = exports.AAVE_V4_BLUECHIP_SPOKE = exports.getAaveV4HubByAddress = exports.getAaveV4HubTypeInfo = exports.AaveV4Hubs = exports.AAVE_V4_PRIME_HUB = exports.AAVE_V4_PLUS_HUB = exports.AAVE_V4_CORE_HUB = void 0;
4
4
  const types_1 = require("../../types");
5
5
  // HUBS
6
6
  const AAVE_V4_CORE_HUB = (networkId) => ({
@@ -180,3 +180,5 @@ const AaveV4Spokes = (networkId) => ({
180
180
  exports.AaveV4Spokes = AaveV4Spokes;
181
181
  const getAaveV4SpokeTypeInfo = (type, network) => (Object.assign({}, (0, exports.AaveV4Spokes)(network !== null && network !== void 0 ? network : types_1.NetworkNumber.Eth))[type]);
182
182
  exports.getAaveV4SpokeTypeInfo = getAaveV4SpokeTypeInfo;
183
+ const findAaveV4SpokeByAddress = (networkId, address) => Object.values((0, exports.AaveV4Spokes)(networkId)).find(spoke => spoke.address.toLowerCase() === address.toLowerCase());
184
+ exports.findAaveV4SpokeByAddress = findAaveV4SpokeByAddress;
@@ -7,4 +7,4 @@ export { LlamaLendMarkets } from './llamaLend';
7
7
  export { LiquityV2Markets, findLiquityV2MarketByAddress } from './liquityV2';
8
8
  export { EulerV2Markets } from './euler';
9
9
  export { FluidMarkets, getFluidVersionsDataForNetwork, getFluidMarketInfoById, getFTokenAddress, getFluidMarketInfoByAddress, } from './fluid';
10
- export { AaveV4Spokes } from './aaveV4';
10
+ export { AaveV4Spokes, findAaveV4SpokeByAddress } from './aaveV4';
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AaveV4Spokes = exports.getFluidMarketInfoByAddress = exports.getFTokenAddress = exports.getFluidMarketInfoById = exports.getFluidVersionsDataForNetwork = exports.FluidMarkets = exports.EulerV2Markets = exports.findLiquityV2MarketByAddress = exports.LiquityV2Markets = exports.LlamaLendMarkets = exports.findMorphoBlueMarket = exports.MorphoBlueMarkets = exports.CrvUsdMarkets = exports.SparkMarkets = exports.v3USDTCollAssets = exports.v3USDCeCollAssets = exports.v3USDCCollAssets = exports.v3USDbCCollAssets = exports.v3ETHCollAssets = exports.compoundV2CollateralAssets = exports.CompoundMarkets = exports.getAaveV3MarketByMarketAddress = exports.aaveV3AssetsDefaultMarket = exports.aaveV2AssetsDefaultMarket = exports.aaveV1AssetsDefaultMarket = exports.AaveMarkets = void 0;
3
+ exports.findAaveV4SpokeByAddress = exports.AaveV4Spokes = exports.getFluidMarketInfoByAddress = exports.getFTokenAddress = exports.getFluidMarketInfoById = exports.getFluidVersionsDataForNetwork = exports.FluidMarkets = exports.EulerV2Markets = exports.findLiquityV2MarketByAddress = exports.LiquityV2Markets = exports.LlamaLendMarkets = exports.findMorphoBlueMarket = exports.MorphoBlueMarkets = exports.CrvUsdMarkets = exports.SparkMarkets = exports.v3USDTCollAssets = exports.v3USDCeCollAssets = exports.v3USDCCollAssets = exports.v3USDbCCollAssets = exports.v3ETHCollAssets = exports.compoundV2CollateralAssets = exports.CompoundMarkets = exports.getAaveV3MarketByMarketAddress = exports.aaveV3AssetsDefaultMarket = exports.aaveV2AssetsDefaultMarket = exports.aaveV1AssetsDefaultMarket = exports.AaveMarkets = void 0;
4
4
  var aave_1 = require("./aave");
5
5
  Object.defineProperty(exports, "AaveMarkets", { enumerable: true, get: function () { return aave_1.AaveMarkets; } });
6
6
  Object.defineProperty(exports, "aaveV1AssetsDefaultMarket", { enumerable: true, get: function () { return aave_1.aaveV1AssetsDefaultMarket; } });
@@ -37,3 +37,4 @@ Object.defineProperty(exports, "getFTokenAddress", { enumerable: true, get: func
37
37
  Object.defineProperty(exports, "getFluidMarketInfoByAddress", { enumerable: true, get: function () { return fluid_1.getFluidMarketInfoByAddress; } });
38
38
  var aaveV4_1 = require("./aaveV4");
39
39
  Object.defineProperty(exports, "AaveV4Spokes", { enumerable: true, get: function () { return aaveV4_1.AaveV4Spokes; } });
40
+ Object.defineProperty(exports, "findAaveV4SpokeByAddress", { enumerable: true, get: function () { return aaveV4_1.findAaveV4SpokeByAddress; } });
@@ -125,7 +125,6 @@ export interface AavePositionData extends MMPositionData {
125
125
  ratio: string;
126
126
  minRatio: string;
127
127
  collRatio: string;
128
- safetyRatioWithLtvZeroFallback?: string;
129
128
  suppliedUsd: string;
130
129
  borrowedUsd: string;
131
130
  borrowLimitUsd: string;
@@ -157,8 +156,6 @@ export interface AaveV3AggregatedPositionData {
157
156
  leftToBorrowUsd: string;
158
157
  ratio: string;
159
158
  collRatio: string;
160
- borrowLimitWithLtvZeroFallbackUsd: string;
161
- safetyRatioWithLtvZeroFallback: string;
162
159
  netApy: string;
163
160
  incentiveUsd: string;
164
161
  totalInterestUsd: string;
@@ -1,9 +1,11 @@
1
1
  import { Client } from 'viem';
2
- import { AaveV4AccountData, AaveV4SpokeData, AaveV4SpokeInfo, EthAddress, EthereumProvider, NetworkNumber } from '../types';
2
+ import { AaveV4AccountData, AaveV4SpokeData, AaveV4SpokeInfo, EthAddress, EthereumProvider, NetworkNumber, Blockish, PositionBalances } from '../types';
3
3
  export * as lend from './lend';
4
4
  export { getAaveV4MerkleCampaigns } from './merkl';
5
5
  export declare function _getAaveV4SpokeData(provider: Client, network: NetworkNumber, market: AaveV4SpokeInfo, blockNumber?: 'latest' | number): Promise<AaveV4SpokeData>;
6
6
  export declare function getAaveV4SpokeData(provider: EthereumProvider, network: NetworkNumber, spoke: AaveV4SpokeInfo, blockNumber?: 'latest' | number): Promise<AaveV4SpokeData>;
7
7
  export declare function _getAaveV4AccountData(provider: Client, network: NetworkNumber, spokeData: AaveV4SpokeData, address: EthAddress, blockNumber?: 'latest' | number): Promise<AaveV4AccountData>;
8
8
  export declare function getAaveV4AccountData(provider: EthereumProvider, network: NetworkNumber, marketData: AaveV4SpokeData, address: EthAddress, blockNumber?: 'latest' | number): Promise<any>;
9
+ export declare const _getAaveV4AccountBalances: (provider: Client, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress, spokeAddress: EthAddress, spokeData?: AaveV4SpokeData) => Promise<PositionBalances>;
10
+ export declare const getAaveV4AccountBalances: (provider: EthereumProvider, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress, spokeAddress: EthAddress, spokeData?: AaveV4SpokeData) => Promise<PositionBalances>;
9
11
  export declare function getAaveV4UnderlyingFromReserveId(provider: EthereumProvider, network: NetworkNumber, spoke: EthAddress, reserveId: number): Promise<any>;
@@ -8,14 +8,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import Dec from 'decimal.js';
11
- import { assetAmountInEth, getAssetInfoByAddress } from '@defisaver/tokens';
11
+ import { assetAmountInEth, assetAmountInWei, getAssetInfoByAddress } from '@defisaver/tokens';
12
12
  import { getViemProvider } from '../services/viem';
13
13
  import { IncentiveKind, } from '../types';
14
14
  import { AaveV4ViewContractViem } from '../contracts';
15
15
  import { getStakingApy, STAKING_ASSETS } from '../staking';
16
- import { isMaxUint, wethToEth } from '../services/utils';
16
+ import { isMaxUint, wethToEth, wethToEthByAddress } from '../services/utils';
17
17
  import { aaveV4GetAggregatedPositionData, calcUserRiskPremiumBps } from '../helpers/aaveV4Helpers';
18
- import { getAaveV4HubByAddress } from '../markets/aaveV4';
18
+ import { findAaveV4SpokeByAddress, getAaveV4HubByAddress } from '../markets/aaveV4';
19
19
  import { aprToApy } from '../moneymarket';
20
20
  import { attachAaveV4MerklIncentives, getAaveV4MerkleCampaigns } from './merkl';
21
21
  export * as lend from './lend';
@@ -240,6 +240,44 @@ export function getAaveV4AccountData(provider_1, network_1, marketData_1, addres
240
240
  return _getAaveV4AccountData(getViemProvider(provider, network), network, marketData, address, blockNumber);
241
241
  });
242
242
  }
243
+ export const _getAaveV4AccountBalances = (provider, network, block, addressMapping, address, spokeAddress, spokeData) => __awaiter(void 0, void 0, void 0, function* () {
244
+ const balances = {
245
+ collateral: {},
246
+ debt: {},
247
+ };
248
+ if (!address || !spokeAddress) {
249
+ return balances;
250
+ }
251
+ const blockNumber = block === 'latest' ? 'latest' : Number(block);
252
+ let resolvedSpokeData = spokeData;
253
+ if (!resolvedSpokeData) {
254
+ const spokeInfo = findAaveV4SpokeByAddress(network, spokeAddress);
255
+ if (!spokeInfo) {
256
+ return balances;
257
+ }
258
+ resolvedSpokeData = yield _getAaveV4SpokeData(provider, network, spokeInfo, blockNumber);
259
+ }
260
+ const accountData = yield _getAaveV4AccountData(provider, network, resolvedSpokeData, address, blockNumber);
261
+ const finalSpokeData = resolvedSpokeData;
262
+ Object.entries(accountData.usedAssets).forEach(([key, asset]) => {
263
+ const reserveData = finalSpokeData.assetsData[key];
264
+ if (!reserveData)
265
+ return;
266
+ const balanceKey = addressMapping
267
+ ? wethToEthByAddress(reserveData.underlying, network).toLowerCase()
268
+ : wethToEth(asset.symbol);
269
+ if (asset.isSupplied && new Dec(asset.supplied || 0).gt(0)) {
270
+ balances.collateral[balanceKey] = assetAmountInWei(asset.supplied, asset.symbol);
271
+ }
272
+ if (asset.isBorrowed && new Dec(asset.borrowed || 0).gt(0)) {
273
+ balances.debt[balanceKey] = assetAmountInWei(asset.borrowed, asset.symbol);
274
+ }
275
+ });
276
+ return balances;
277
+ });
278
+ export const getAaveV4AccountBalances = (provider, network, block, addressMapping, address, spokeAddress, spokeData) => __awaiter(void 0, void 0, void 0, function* () {
279
+ return _getAaveV4AccountBalances(getViemProvider(provider, network), network, block, addressMapping, address, spokeAddress, spokeData);
280
+ });
243
281
  const _getAaveV4UnderlyingFromReserveId = (provider, network, spoke, reserveId) => __awaiter(void 0, void 0, void 0, function* () {
244
282
  const viewContract = AaveV4ViewContractViem(provider, network);
245
283
  const reserveData = yield viewContract.read.getReserveData([spoke, BigInt(reserveId)]);
@@ -30,17 +30,6 @@ export declare const aaveAnyGetEmodeMutableProps: ({ eModeCategory, eModeCategor
30
30
  liquidationRatio: any;
31
31
  collateralFactor: any;
32
32
  };
33
- /**
34
- * @description Per-asset effective LTV and liquidation threshold (LLTV) for the user, eMode-aware.
35
- * Mirrors AaveV3View._getUserReserveLtvAndLltv: an asset in the active eMode's ltv-zero set has
36
- * ltv 0 but KEEPS the eMode liquidation threshold (liquidations only consider LLTV). The returned
37
- * `ltv` is identical to aaveAnyGetEmodeMutableProps().collateralFactor in every branch, so a ratio
38
- * built on this matches the regular safety ratio whenever no collateral is LTV-0.
39
- */
40
- export declare const aaveAnyGetUserReserveLtvAndLltv: ({ eModeCategory, eModeCategoriesData, assetsData, }: AaveHelperCommon, _asset: string) => {
41
- ltv: string;
42
- lltv: string;
43
- };
44
33
  export declare const aaveAnyGetAggregatedPositionData: ({ usedAssets, eModeCategory, assetsData, selectedMarket, network, ...rest }: AaveHelperCommon) => AaveV3AggregatedPositionData;
45
34
  export declare const getApyAfterValuesEstimation: (selectedMarket: AaveMarketInfo, actions: [{
46
35
  action: string;
@@ -69,32 +69,6 @@ export const aaveAnyGetEmodeMutableProps = ({ eModeCategory, eModeCategoriesData
69
69
  const { liquidationRatio, collateralFactor } = eModeCategoryData;
70
70
  return ({ liquidationRatio, collateralFactor });
71
71
  };
72
- /**
73
- * @description Offset subtracted from the liquidation threshold (LLTV) when crediting LTV-0 collateral
74
- * in the safety-ratio fallback. Matches AaveV3View.getSafetyRatioWithLtvZeroFallback ('LLTV - 5%').
75
- * Values are fractions (e.g. 0.8), so 5% === 0.05.
76
- */
77
- const LTV_ZERO_FALLBACK_LLTV_OFFSET = '0.05';
78
- /**
79
- * @description Per-asset effective LTV and liquidation threshold (LLTV) for the user, eMode-aware.
80
- * Mirrors AaveV3View._getUserReserveLtvAndLltv: an asset in the active eMode's ltv-zero set has
81
- * ltv 0 but KEEPS the eMode liquidation threshold (liquidations only consider LLTV). The returned
82
- * `ltv` is identical to aaveAnyGetEmodeMutableProps().collateralFactor in every branch, so a ratio
83
- * built on this matches the regular safety ratio whenever no collateral is LTV-0.
84
- */
85
- export const aaveAnyGetUserReserveLtvAndLltv = ({ eModeCategory, eModeCategoriesData, assetsData, }, _asset) => {
86
- const asset = getNativeAssetFromWrapped(_asset);
87
- const assetData = assetsData[asset];
88
- const eModeCategoryData = eModeCategoriesData === null || eModeCategoriesData === void 0 ? void 0 : eModeCategoriesData[eModeCategory];
89
- if (eModeCategory === 0
90
- || !eModeCategoryData
91
- || !eModeCategoryData.collateralAssets.includes(asset)
92
- || new Dec(eModeCategoryData.collateralFactor || 0).eq(0)) {
93
- return { ltv: assetData.collateralFactor, lltv: assetData.liquidationRatio };
94
- }
95
- const ltv = eModeCategoryData.ltvZeroAssets.includes(asset) ? '0' : eModeCategoryData.collateralFactor;
96
- return { ltv, lltv: eModeCategoryData.liquidationRatio };
97
- };
98
72
  export const aaveAnyGetAggregatedPositionData = (_a) => {
99
73
  var { usedAssets, eModeCategory, assetsData, selectedMarket, network } = _a, rest = __rest(_a, ["usedAssets", "eModeCategory", "assetsData", "selectedMarket", "network"]);
100
74
  const data = Object.assign({ usedAssets, eModeCategory, assetsData, selectedMarket, network }, rest);
@@ -108,17 +82,6 @@ export const aaveAnyGetAggregatedPositionData = (_a) => {
108
82
  payload.leftToBorrowUsd = leftToBorrowUsd.lte('0') ? '0' : leftToBorrowUsd.toString();
109
83
  payload.ratio = +payload.suppliedUsd ? new Dec(payload.borrowLimitUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
110
84
  payload.collRatio = +payload.suppliedUsd ? new Dec(payload.suppliedCollateralUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
111
- // Safety ratio as evaluated by the automation bots: LTV-0 collateral is credited at (LLTV - 5%)
112
- // instead of 0 (AaveV3View.getSafetyRatioWithLtvZeroFallback). Equals `ratio` when no collateral
113
- // is LTV-0. Computed off-chain here so it is available for after-value simulations too.
114
- payload.borrowLimitWithLtvZeroFallbackUsd = getAssetsTotal(usedAssets, ({ isSupplied, collateral }) => isSupplied && collateral, ({ symbol, suppliedUsd }) => {
115
- const { ltv, lltv } = aaveAnyGetUserReserveLtvAndLltv(data, symbol);
116
- const effectiveLtv = new Dec(ltv).eq(0)
117
- ? Dec.max(0, new Dec(lltv).sub(LTV_ZERO_FALLBACK_LLTV_OFFSET))
118
- : new Dec(ltv);
119
- return new Dec(suppliedUsd).mul(effectiveLtv);
120
- });
121
- payload.safetyRatioWithLtvZeroFallback = +payload.suppliedUsd ? new Dec(payload.borrowLimitWithLtvZeroFallbackUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
122
85
  payload.liqRatio = new Dec(payload.borrowLimitUsd).div(payload.liquidationLimitUsd).toString();
123
86
  payload.liqPercent = new Dec(payload.borrowLimitUsd).div(payload.liquidationLimitUsd).mul(100).toString();
124
87
  const { leveragedType, leveragedAsset } = isLeveragedPos(usedAssets);
@@ -32,3 +32,4 @@ export declare const AaveV4Spokes: (networkId: NetworkNumber) => {
32
32
  readonly aave_v4_main_spoke: AaveV4SpokeInfo;
33
33
  };
34
34
  export declare const getAaveV4SpokeTypeInfo: (type: AaveV4SpokesType, network?: NetworkNumber) => AaveV4SpokeInfo;
35
+ export declare const findAaveV4SpokeByAddress: (networkId: NetworkNumber, address: string) => AaveV4SpokeInfo | undefined;
@@ -159,3 +159,4 @@ export const AaveV4Spokes = (networkId) => ({
159
159
  [AaveV4SpokesType.AaveV4MainSpoke]: AAVE_V4_MAIN_SPOKE(networkId),
160
160
  });
161
161
  export const getAaveV4SpokeTypeInfo = (type, network) => (Object.assign({}, AaveV4Spokes(network !== null && network !== void 0 ? network : NetworkNumber.Eth))[type]);
162
+ export const findAaveV4SpokeByAddress = (networkId, address) => Object.values(AaveV4Spokes(networkId)).find(spoke => spoke.address.toLowerCase() === address.toLowerCase());
@@ -7,4 +7,4 @@ export { LlamaLendMarkets } from './llamaLend';
7
7
  export { LiquityV2Markets, findLiquityV2MarketByAddress } from './liquityV2';
8
8
  export { EulerV2Markets } from './euler';
9
9
  export { FluidMarkets, getFluidVersionsDataForNetwork, getFluidMarketInfoById, getFTokenAddress, getFluidMarketInfoByAddress, } from './fluid';
10
- export { AaveV4Spokes } from './aaveV4';
10
+ export { AaveV4Spokes, findAaveV4SpokeByAddress } from './aaveV4';
@@ -7,4 +7,4 @@ export { LlamaLendMarkets } from './llamaLend';
7
7
  export { LiquityV2Markets, findLiquityV2MarketByAddress } from './liquityV2';
8
8
  export { EulerV2Markets } from './euler';
9
9
  export { FluidMarkets, getFluidVersionsDataForNetwork, getFluidMarketInfoById, getFTokenAddress, getFluidMarketInfoByAddress, } from './fluid';
10
- export { AaveV4Spokes } from './aaveV4';
10
+ export { AaveV4Spokes, findAaveV4SpokeByAddress } from './aaveV4';
@@ -125,7 +125,6 @@ export interface AavePositionData extends MMPositionData {
125
125
  ratio: string;
126
126
  minRatio: string;
127
127
  collRatio: string;
128
- safetyRatioWithLtvZeroFallback?: string;
129
128
  suppliedUsd: string;
130
129
  borrowedUsd: string;
131
130
  borrowLimitUsd: string;
@@ -157,8 +156,6 @@ export interface AaveV3AggregatedPositionData {
157
156
  leftToBorrowUsd: string;
158
157
  ratio: string;
159
158
  collRatio: string;
160
- borrowLimitWithLtvZeroFallbackUsd: string;
161
- safetyRatioWithLtvZeroFallback: string;
162
159
  netApy: string;
163
160
  incentiveUsd: string;
164
161
  totalInterestUsd: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defisaver/positions-sdk",
3
- "version": "2.1.109-ltv-0-automations-dev",
3
+ "version": "2.1.110",
4
4
  "description": "",
5
5
  "main": "./cjs/index.js",
6
6
  "module": "./esm/index.js",
@@ -1,6 +1,6 @@
1
1
  import { Client } from 'viem';
2
2
  import Dec from 'decimal.js';
3
- import { assetAmountInEth, getAssetInfoByAddress } from '@defisaver/tokens';
3
+ import { assetAmountInEth, assetAmountInWei, getAssetInfoByAddress } from '@defisaver/tokens';
4
4
  import { getViemProvider } from '../services/viem';
5
5
  import {
6
6
  AaveV4AccountData,
@@ -16,12 +16,14 @@ import {
16
16
  IncentiveData,
17
17
  IncentiveKind,
18
18
  NetworkNumber,
19
+ Blockish,
20
+ PositionBalances,
19
21
  } from '../types';
20
22
  import { AaveV4ViewContractViem } from '../contracts';
21
23
  import { getStakingApy, STAKING_ASSETS } from '../staking';
22
- import { isMaxUint, wethToEth } from '../services/utils';
24
+ import { isMaxUint, wethToEth, wethToEthByAddress } from '../services/utils';
23
25
  import { aaveV4GetAggregatedPositionData, calcUserRiskPremiumBps } from '../helpers/aaveV4Helpers';
24
- import { getAaveV4HubByAddress } from '../markets/aaveV4';
26
+ import { findAaveV4SpokeByAddress, getAaveV4HubByAddress } from '../markets/aaveV4';
25
27
  import { aprToApy } from '../moneymarket';
26
28
  import { attachAaveV4MerklIncentives, getAaveV4MerkleCampaigns } from './merkl';
27
29
 
@@ -259,6 +261,74 @@ export async function getAaveV4AccountData(provider: EthereumProvider, network:
259
261
  return _getAaveV4AccountData(getViemProvider(provider, network), network, marketData, address, blockNumber);
260
262
  }
261
263
 
264
+ export const _getAaveV4AccountBalances = async (
265
+ provider: Client,
266
+ network: NetworkNumber,
267
+ block: Blockish,
268
+ addressMapping: boolean,
269
+ address: EthAddress,
270
+ spokeAddress: EthAddress,
271
+ spokeData?: AaveV4SpokeData,
272
+ ): Promise<PositionBalances> => {
273
+ const balances: PositionBalances = {
274
+ collateral: {},
275
+ debt: {},
276
+ };
277
+
278
+ if (!address || !spokeAddress) {
279
+ return balances;
280
+ }
281
+
282
+ const blockNumber = block === 'latest' ? 'latest' : Number(block);
283
+ let resolvedSpokeData = spokeData;
284
+ if (!resolvedSpokeData) {
285
+ const spokeInfo = findAaveV4SpokeByAddress(network, spokeAddress);
286
+ if (!spokeInfo) {
287
+ return balances;
288
+ }
289
+ resolvedSpokeData = await _getAaveV4SpokeData(provider, network, spokeInfo, blockNumber);
290
+ }
291
+
292
+ const accountData = await _getAaveV4AccountData(provider, network, resolvedSpokeData, address, blockNumber);
293
+ const finalSpokeData = resolvedSpokeData;
294
+
295
+ Object.entries(accountData.usedAssets).forEach(([key, asset]) => {
296
+ const reserveData = finalSpokeData.assetsData[key];
297
+ if (!reserveData) return;
298
+
299
+ const balanceKey = addressMapping
300
+ ? wethToEthByAddress(reserveData.underlying, network).toLowerCase()
301
+ : wethToEth(asset.symbol);
302
+
303
+ if (asset.isSupplied && new Dec(asset.supplied || 0).gt(0)) {
304
+ balances.collateral![balanceKey] = assetAmountInWei(asset.supplied, asset.symbol);
305
+ }
306
+ if (asset.isBorrowed && new Dec(asset.borrowed || 0).gt(0)) {
307
+ balances.debt![balanceKey] = assetAmountInWei(asset.borrowed, asset.symbol);
308
+ }
309
+ });
310
+
311
+ return balances;
312
+ };
313
+
314
+ export const getAaveV4AccountBalances = async (
315
+ provider: EthereumProvider,
316
+ network: NetworkNumber,
317
+ block: Blockish,
318
+ addressMapping: boolean,
319
+ address: EthAddress,
320
+ spokeAddress: EthAddress,
321
+ spokeData?: AaveV4SpokeData,
322
+ ): Promise<PositionBalances> => _getAaveV4AccountBalances(
323
+ getViemProvider(provider, network),
324
+ network,
325
+ block,
326
+ addressMapping,
327
+ address,
328
+ spokeAddress,
329
+ spokeData,
330
+ );
331
+
262
332
  const _getAaveV4UnderlyingFromReserveId = async (provider: Client, network: NetworkNumber, spoke: EthAddress, reserveId: number): Promise<any> => {
263
333
  const viewContract = AaveV4ViewContractViem(provider, network);
264
334
 
@@ -80,45 +80,6 @@ export const aaveAnyGetEmodeMutableProps = (
80
80
  return ({ liquidationRatio, collateralFactor });
81
81
  };
82
82
 
83
- /**
84
- * @description Offset subtracted from the liquidation threshold (LLTV) when crediting LTV-0 collateral
85
- * in the safety-ratio fallback. Matches AaveV3View.getSafetyRatioWithLtvZeroFallback ('LLTV - 5%').
86
- * Values are fractions (e.g. 0.8), so 5% === 0.05.
87
- */
88
- const LTV_ZERO_FALLBACK_LLTV_OFFSET = '0.05';
89
-
90
- /**
91
- * @description Per-asset effective LTV and liquidation threshold (LLTV) for the user, eMode-aware.
92
- * Mirrors AaveV3View._getUserReserveLtvAndLltv: an asset in the active eMode's ltv-zero set has
93
- * ltv 0 but KEEPS the eMode liquidation threshold (liquidations only consider LLTV). The returned
94
- * `ltv` is identical to aaveAnyGetEmodeMutableProps().collateralFactor in every branch, so a ratio
95
- * built on this matches the regular safety ratio whenever no collateral is LTV-0.
96
- */
97
- export const aaveAnyGetUserReserveLtvAndLltv = (
98
- {
99
- eModeCategory,
100
- eModeCategoriesData,
101
- assetsData,
102
- }: AaveHelperCommon,
103
- _asset: string,
104
- ): { ltv: string, lltv: string } => {
105
- const asset = getNativeAssetFromWrapped(_asset);
106
- const assetData = assetsData[asset];
107
- const eModeCategoryData = eModeCategoriesData?.[eModeCategory];
108
-
109
- if (
110
- eModeCategory === 0
111
- || !eModeCategoryData
112
- || !eModeCategoryData.collateralAssets.includes(asset)
113
- || new Dec(eModeCategoryData.collateralFactor || 0).eq(0)
114
- ) {
115
- return { ltv: assetData.collateralFactor, lltv: assetData.liquidationRatio };
116
- }
117
-
118
- const ltv = eModeCategoryData.ltvZeroAssets.includes(asset) ? '0' : eModeCategoryData.collateralFactor;
119
- return { ltv, lltv: eModeCategoryData.liquidationRatio };
120
- };
121
-
122
83
  export const aaveAnyGetAggregatedPositionData = ({
123
84
  usedAssets,
124
85
  eModeCategory,
@@ -148,21 +109,6 @@ export const aaveAnyGetAggregatedPositionData = ({
148
109
  payload.leftToBorrowUsd = leftToBorrowUsd.lte('0') ? '0' : leftToBorrowUsd.toString();
149
110
  payload.ratio = +payload.suppliedUsd ? new Dec(payload.borrowLimitUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
150
111
  payload.collRatio = +payload.suppliedUsd ? new Dec(payload.suppliedCollateralUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
151
- // Safety ratio as evaluated by the automation bots: LTV-0 collateral is credited at (LLTV - 5%)
152
- // instead of 0 (AaveV3View.getSafetyRatioWithLtvZeroFallback). Equals `ratio` when no collateral
153
- // is LTV-0. Computed off-chain here so it is available for after-value simulations too.
154
- payload.borrowLimitWithLtvZeroFallbackUsd = getAssetsTotal(
155
- usedAssets,
156
- ({ isSupplied, collateral }: { isSupplied: boolean, collateral: string }) => isSupplied && collateral,
157
- ({ symbol, suppliedUsd }: { symbol: string, suppliedUsd: string }) => {
158
- const { ltv, lltv } = aaveAnyGetUserReserveLtvAndLltv(data, symbol);
159
- const effectiveLtv = new Dec(ltv).eq(0)
160
- ? Dec.max(0, new Dec(lltv).sub(LTV_ZERO_FALLBACK_LLTV_OFFSET))
161
- : new Dec(ltv);
162
- return new Dec(suppliedUsd).mul(effectiveLtv);
163
- },
164
- );
165
- payload.safetyRatioWithLtvZeroFallback = +payload.suppliedUsd ? new Dec(payload.borrowLimitWithLtvZeroFallbackUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
166
112
  payload.liqRatio = new Dec(payload.borrowLimitUsd).div(payload.liquidationLimitUsd).toString();
167
113
  payload.liqPercent = new Dec(payload.borrowLimitUsd).div(payload.liquidationLimitUsd).mul(100).toString();
168
114
  const { leveragedType, leveragedAsset } = isLeveragedPos(usedAssets);
@@ -188,4 +188,8 @@ export const AaveV4Spokes = (networkId: NetworkNumber) => ({
188
188
 
189
189
  export const getAaveV4SpokeTypeInfo = (type: AaveV4SpokesType, network?: NetworkNumber) => ({ ...AaveV4Spokes(network ?? NetworkNumber.Eth) }[type]);
190
190
 
191
+ export const findAaveV4SpokeByAddress = (networkId: NetworkNumber, address: string): AaveV4SpokeInfo | undefined => Object.values(AaveV4Spokes(networkId)).find(
192
+ spoke => spoke.address.toLowerCase() === address.toLowerCase(),
193
+ );
194
+
191
195
 
@@ -27,4 +27,4 @@ export {
27
27
  getFTokenAddress,
28
28
  getFluidMarketInfoByAddress,
29
29
  } from './fluid';
30
- export { AaveV4Spokes } from './aaveV4';
30
+ export { AaveV4Spokes, findAaveV4SpokeByAddress } from './aaveV4';
package/src/types/aave.ts CHANGED
@@ -141,8 +141,6 @@ export interface AavePositionData extends MMPositionData {
141
141
  ratio: string,
142
142
  minRatio: string,
143
143
  collRatio: string,
144
- // Safety ratio as evaluated by automation bots (LTV-0 collateral credited at LLTV - 5%). Aave v3 only.
145
- safetyRatioWithLtvZeroFallback?: string,
146
144
  suppliedUsd: string,
147
145
  borrowedUsd: string,
148
146
  borrowLimitUsd: string,
@@ -175,8 +173,6 @@ export interface AaveV3AggregatedPositionData {
175
173
  leftToBorrowUsd: string,
176
174
  ratio: string,
177
175
  collRatio: string,
178
- borrowLimitWithLtvZeroFallbackUsd: string,
179
- safetyRatioWithLtvZeroFallback: string,
180
176
  netApy: string,
181
177
  incentiveUsd: string,
182
178
  totalInterestUsd: string,