@defisaver/positions-sdk 2.1.79 → 2.1.82

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.
Files changed (75) hide show
  1. package/cjs/aaveV4/index.d.ts +7 -0
  2. package/cjs/aaveV4/index.js +243 -0
  3. package/cjs/aaveV4/lend.d.ts +55 -0
  4. package/cjs/aaveV4/lend.js +131 -0
  5. package/cjs/config/contracts.d.ts +1551 -0
  6. package/cjs/config/contracts.js +9 -0
  7. package/cjs/contracts.d.ts +32741 -0
  8. package/cjs/contracts.js +2 -1
  9. package/cjs/eulerV2/index.d.ts +1 -2
  10. package/cjs/eulerV2/index.js +4 -5
  11. package/cjs/helpers/aaveV4Helpers/index.d.ts +25 -0
  12. package/cjs/helpers/aaveV4Helpers/index.js +233 -0
  13. package/cjs/helpers/index.d.ts +1 -0
  14. package/cjs/helpers/index.js +2 -1
  15. package/cjs/index.d.ts +2 -1
  16. package/cjs/index.js +3 -1
  17. package/cjs/markets/aaveV4/index.d.ts +34 -0
  18. package/cjs/markets/aaveV4/index.js +182 -0
  19. package/cjs/markets/index.d.ts +1 -0
  20. package/cjs/markets/index.js +3 -1
  21. package/cjs/moneymarket/moneymarketCommonService.js +1 -1
  22. package/cjs/portfolio/index.js +20 -0
  23. package/cjs/services/utils.d.ts +1 -1
  24. package/cjs/services/utils.js +3 -3
  25. package/cjs/types/aaveV4.d.ts +159 -0
  26. package/cjs/types/aaveV4.js +22 -0
  27. package/cjs/types/index.d.ts +1 -0
  28. package/cjs/types/index.js +1 -0
  29. package/cjs/types/portfolio.d.ts +4 -0
  30. package/esm/aaveV4/index.d.ts +7 -0
  31. package/esm/aaveV4/index.js +200 -0
  32. package/esm/aaveV4/lend.d.ts +55 -0
  33. package/esm/aaveV4/lend.js +124 -0
  34. package/esm/config/contracts.d.ts +1551 -0
  35. package/esm/config/contracts.js +8 -0
  36. package/esm/contracts.d.ts +32741 -0
  37. package/esm/contracts.js +1 -0
  38. package/esm/eulerV2/index.d.ts +1 -2
  39. package/esm/eulerV2/index.js +5 -6
  40. package/esm/helpers/aaveV4Helpers/index.d.ts +25 -0
  41. package/esm/helpers/aaveV4Helpers/index.js +221 -0
  42. package/esm/helpers/index.d.ts +1 -0
  43. package/esm/helpers/index.js +1 -0
  44. package/esm/index.d.ts +2 -1
  45. package/esm/index.js +2 -1
  46. package/esm/markets/aaveV4/index.d.ts +34 -0
  47. package/esm/markets/aaveV4/index.js +161 -0
  48. package/esm/markets/index.d.ts +1 -0
  49. package/esm/markets/index.js +1 -0
  50. package/esm/moneymarket/moneymarketCommonService.js +1 -1
  51. package/esm/portfolio/index.js +21 -1
  52. package/esm/services/utils.d.ts +1 -1
  53. package/esm/services/utils.js +1 -1
  54. package/esm/types/aaveV4.d.ts +159 -0
  55. package/esm/types/aaveV4.js +19 -0
  56. package/esm/types/index.d.ts +1 -0
  57. package/esm/types/index.js +1 -0
  58. package/esm/types/portfolio.d.ts +4 -0
  59. package/package.json +2 -2
  60. package/src/aaveV4/index.ts +221 -0
  61. package/src/aaveV4/lend.ts +185 -0
  62. package/src/config/contracts.ts +8 -0
  63. package/src/contracts.ts +2 -0
  64. package/src/eulerV2/index.ts +12 -7
  65. package/src/helpers/aaveV4Helpers/index.ts +291 -0
  66. package/src/helpers/index.ts +1 -0
  67. package/src/index.ts +2 -0
  68. package/src/markets/aaveV4/index.ts +191 -0
  69. package/src/markets/index.ts +6 -1
  70. package/src/moneymarket/moneymarketCommonService.ts +1 -1
  71. package/src/portfolio/index.ts +20 -0
  72. package/src/services/utils.ts +1 -1
  73. package/src/types/aaveV4.ts +175 -0
  74. package/src/types/index.ts +2 -1
  75. package/src/types/portfolio.ts +4 -0
@@ -40,6 +40,7 @@ const spark_2 = require("../claiming/spark");
40
40
  const morphoBlue_2 = require("../claiming/morphoBlue");
41
41
  const king_1 = require("../claiming/king");
42
42
  const ethena_1 = require("../claiming/ethena");
43
+ const aaveV4_1 = require("../aaveV4");
43
44
  function getPortfolioData(provider_1, network_1, defaultProvider_1, addresses_1) {
44
45
  return __awaiter(this, arguments, void 0, function* (provider, network, defaultProvider, addresses, isSim = false) {
45
46
  const isMainnet = network === common_1.NetworkNumber.Eth;
@@ -56,6 +57,7 @@ function getPortfolioData(provider_1, network_1, defaultProvider_1, addresses_1)
56
57
  const llamaLendMarkets = [common_1.NetworkNumber.Eth, common_1.NetworkNumber.Arb].includes(network) ? Object.values((0, markets_1.LlamaLendMarkets)(network)).filter((market) => market.chainIds.includes(network)) : [];
57
58
  const liquityV2Markets = [common_1.NetworkNumber.Eth].includes(network) ? Object.values((0, markets_1.LiquityV2Markets)(network)) : [];
58
59
  const liquityV2MarketsStaking = [common_1.NetworkNumber.Eth].includes(network) ? Object.values((0, markets_1.LiquityV2Markets)(network)).filter(market => !market.isLegacy) : [];
60
+ const aaveV4Spokes = Object.values((0, markets_1.AaveV4Spokes)(network)).filter((market) => market.chainIds.includes(network));
59
61
  const args = [network, { batch: { multicall: { batchSize: isSim ? 500000 : 2500000 } } }];
60
62
  const client = (0, viem_1.getViemProvider)(provider, ...args);
61
63
  const defaultClient = (0, viem_1.getViemProvider)(defaultProvider, ...args);
@@ -70,6 +72,7 @@ function getPortfolioData(provider_1, network_1, defaultProvider_1, addresses_1)
70
72
  const crvUsdMarketsData = {};
71
73
  const llamaLendMarketsData = {};
72
74
  const liquityV2MarketsData = {};
75
+ const aaveV4SpokesData = {};
73
76
  const markets = {
74
77
  morphoMarketsData,
75
78
  compoundV3MarketsData,
@@ -81,6 +84,7 @@ function getPortfolioData(provider_1, network_1, defaultProvider_1, addresses_1)
81
84
  crvUsdMarketsData,
82
85
  llamaLendMarketsData,
83
86
  liquityV2MarketsData,
87
+ aaveV4SpokesData,
84
88
  };
85
89
  const positions = {};
86
90
  const stakingPositions = {};
@@ -89,6 +93,7 @@ function getPortfolioData(provider_1, network_1, defaultProvider_1, addresses_1)
89
93
  for (const address of allAddresses) {
90
94
  positions[address.toLowerCase()] = {
91
95
  aaveV3: {},
96
+ aaveV4: {},
92
97
  morphoBlue: {},
93
98
  compoundV3: {},
94
99
  spark: {},
@@ -154,6 +159,10 @@ function getPortfolioData(provider_1, network_1, defaultProvider_1, addresses_1)
154
159
  const marketData = yield (0, aaveV3_1._getAaveV3MarketData)(client, network, market);
155
160
  aaveV3MarketsData[market.value] = marketData;
156
161
  })),
162
+ ...aaveV4Spokes.map((spoke) => __awaiter(this, void 0, void 0, function* () {
163
+ const spokeData = yield (0, aaveV4_1._getAaveV4SpokeData)(client, network, spoke);
164
+ aaveV4SpokesData[spoke.value] = spokeData;
165
+ })),
157
166
  ...aaveV2Markets.map((market) => __awaiter(this, void 0, void 0, function* () {
158
167
  const marketData = yield (0, aaveV2_1._getAaveV2MarketsData)(client, network, market);
159
168
  aaveV2MarketsData[market.value] = marketData;
@@ -423,6 +432,17 @@ function getPortfolioData(provider_1, network_1, defaultProvider_1, addresses_1)
423
432
  positions[address.toLowerCase()].aaveV3[market.value] = { error: `Error fetching AaveV3 account data for address ${address} on market ${market.value}`, data: null };
424
433
  }
425
434
  }))).flat(),
435
+ ...aaveV4Spokes.map((spoke) => allAddresses.map((address) => __awaiter(this, void 0, void 0, function* () {
436
+ try {
437
+ const accData = yield (0, aaveV4_1._getAaveV4AccountData)(client, network, aaveV4SpokesData[spoke.value], address);
438
+ if (new decimal_js_1.default(accData.suppliedUsd).gt(0))
439
+ positions[address.toLowerCase()].aaveV4[spoke.value] = { error: '', data: accData };
440
+ }
441
+ catch (error) {
442
+ console.error(`Error fetching AaveV4 account data for address ${address} on spoke ${spoke.value}:`, error);
443
+ positions[address.toLowerCase()].aaveV4[spoke.value] = { error: `Error fetching AaveV4 account data for address ${address} on spoke ${spoke.value}`, data: null };
444
+ }
445
+ }))).flat(),
426
446
  ...morphoMarkets.map((market) => addresses.map((address) => __awaiter(this, void 0, void 0, function* () {
427
447
  try {
428
448
  const [accDataPromise, earnDataPromise] = yield Promise.allSettled([
@@ -18,7 +18,7 @@ export declare const bytesToString: (hex: string) => string;
18
18
  export declare const mapRange: (input: number | string, minInput: number | string, maxInput: number | string, minOutput: number | string, maxOutput: number | string) => number;
19
19
  export declare const isEnabledOnBitmap: (bitmap: number, assetId: number) => bigint;
20
20
  export declare const MAXUINT: string;
21
- export declare const isMaxuint: (amount: string) => boolean;
21
+ export declare const isMaxUint: (amount: string) => boolean;
22
22
  export declare const isMainnetNetwork: (network: NetworkNumber) => network is NetworkNumber.Eth;
23
23
  export declare const DEFAULT_TIMEOUT = 2000;
24
24
  export declare const LONGER_TIMEOUT = 5000;
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getNativeAssetFromWrapped = exports.getWrappedNativeAssetFromUnwrapped = exports.wxplToXpl = exports.xplToWxpl = exports.ethToWethByAddress = exports.wethToEthByAddress = exports.wethToEth = exports.ethToWeth = exports.convertHybridArraysToObjects = exports.LONGER_TIMEOUT = exports.DEFAULT_TIMEOUT = exports.isMainnetNetwork = exports.isMaxuint = exports.MAXUINT = exports.isEnabledOnBitmap = exports.mapRange = exports.bytesToString = exports.handleWbtcLegacy = exports.getEthAmountForDecimals = exports.getWeiAmountForDecimals = exports.compareAddresses = exports.isAddress = exports.ADDRESS_REGEX = exports.getAbiItem = exports.wstEthToStEth = exports.stEthToWstEth = exports.addToArrayIf = exports.addToObjectIf = exports.isLayer2Network = void 0;
6
+ exports.getNativeAssetFromWrapped = exports.getWrappedNativeAssetFromUnwrapped = exports.wxplToXpl = exports.xplToWxpl = exports.ethToWethByAddress = exports.wethToEthByAddress = exports.wethToEth = exports.ethToWeth = exports.convertHybridArraysToObjects = exports.LONGER_TIMEOUT = exports.DEFAULT_TIMEOUT = exports.isMainnetNetwork = exports.isMaxUint = exports.MAXUINT = exports.isEnabledOnBitmap = exports.mapRange = exports.bytesToString = exports.handleWbtcLegacy = exports.getEthAmountForDecimals = exports.getWeiAmountForDecimals = exports.compareAddresses = exports.isAddress = exports.ADDRESS_REGEX = exports.getAbiItem = exports.wstEthToStEth = exports.stEthToWstEth = exports.addToArrayIf = exports.addToObjectIf = exports.isLayer2Network = void 0;
7
7
  const decimal_js_1 = __importDefault(require("decimal.js"));
8
8
  const tokens_1 = require("@defisaver/tokens");
9
9
  const common_1 = require("../types/common");
@@ -51,8 +51,8 @@ exports.mapRange = mapRange;
51
51
  const isEnabledOnBitmap = (bitmap, assetId) => (BigInt(bitmap) >> BigInt(assetId)) & BigInt(1);
52
52
  exports.isEnabledOnBitmap = isEnabledOnBitmap;
53
53
  exports.MAXUINT = '115792089237316195423570985008687907853269984665640564039457584007913129639935';
54
- const isMaxuint = (amount) => (0, exports.compareAddresses)(exports.MAXUINT, amount);
55
- exports.isMaxuint = isMaxuint;
54
+ const isMaxUint = (amount) => (0, exports.compareAddresses)(exports.MAXUINT, amount);
55
+ exports.isMaxUint = isMaxUint;
56
56
  const isMainnetNetwork = (network) => network === common_1.NetworkNumber.Eth;
57
57
  exports.isMainnetNetwork = isMainnetNetwork;
58
58
  exports.DEFAULT_TIMEOUT = 2000; // 2 seconds
@@ -0,0 +1,159 @@
1
+ import { EthAddress, IncentiveData, LeverageType, NetworkNumber } from './common';
2
+ export declare enum AaveV4HubsType {
3
+ AaveV4CoreHub = "aave_v4_core_hub",
4
+ AaveV4PlusHub = "aave_v4_plus_hub",
5
+ AaveV4PrimeHub = "aave_v4_prime_hub"
6
+ }
7
+ export declare enum AaveV4SpokesType {
8
+ AaveV4BluechipSpoke = "aave_v4_bluechip_spoke",
9
+ AaveV4EthenaCorrelatedSpoke = "aave_v4_ethena_correlated_spoke",
10
+ AaveV4EthenaEcosystemSpoke = "aave_v4_ethena_ecosystem_spoke",
11
+ AaveV4EtherfiSpoke = "aave_v4_etherfi_spoke",
12
+ AaveV4ForexSpoke = "aave_v4_forex_spoke",
13
+ AaveV4GoldSpoke = "aave_v4_gold_spoke",
14
+ AaveV4KelpSpoke = "aave_v4_kelp_spoke",
15
+ AaveV4LidoSpoke = "aave_v4_lido_spoke",
16
+ AaveV4LombardBtcSpoke = "aave_v4_lombard_btc_spoke",
17
+ AaveV4MainSpoke = "aave_v4_main_spoke"
18
+ }
19
+ export interface AaveV4HubInfo {
20
+ chainIds: NetworkNumber[];
21
+ label: string;
22
+ value: AaveV4HubsType;
23
+ address: EthAddress;
24
+ }
25
+ export interface AaveV4HubAssetOnChainData {
26
+ assetId: number;
27
+ drawnRate: bigint;
28
+ liquidity: bigint;
29
+ liquidityFee: number;
30
+ swept: bigint;
31
+ totalDrawn: bigint;
32
+ totalDrawnShares: bigint;
33
+ totalPremiumShares: bigint;
34
+ }
35
+ export interface AaveV4HubOnChainData {
36
+ assets: Record<number, AaveV4HubAssetOnChainData>;
37
+ }
38
+ export interface AaveV4SpokeInfo {
39
+ chainIds: NetworkNumber[];
40
+ label: string;
41
+ value: AaveV4SpokesType;
42
+ url: string;
43
+ address: EthAddress;
44
+ hubs: EthAddress[];
45
+ }
46
+ export interface AaveV4SpokeData {
47
+ assetsData: AaveV4AssetsData;
48
+ oracle: EthAddress;
49
+ oracleDecimals: number;
50
+ address: EthAddress;
51
+ }
52
+ export interface AaveV4ReserveAssetOnChain {
53
+ underlying: EthAddress;
54
+ hub: EthAddress;
55
+ assetId: number;
56
+ decimals: number;
57
+ paused: boolean;
58
+ frozen: boolean;
59
+ borrowable: boolean;
60
+ collateralRisk: number;
61
+ collateralFactor: number;
62
+ maxLiquidationBonus: number;
63
+ liquidationFee: number;
64
+ price: bigint;
65
+ totalSupplied: bigint;
66
+ totalDrawn: bigint;
67
+ totalPremium: bigint;
68
+ totalDebt: bigint;
69
+ supplyCap: bigint;
70
+ borrowCap: bigint;
71
+ deficitRay: bigint;
72
+ spokeActive: boolean;
73
+ spokeHalted: boolean;
74
+ }
75
+ export interface AaveV4ReserveAssetData {
76
+ symbol: string;
77
+ underlying: EthAddress;
78
+ hub: EthAddress;
79
+ hubName: string;
80
+ assetId: number;
81
+ reserveId: number;
82
+ paused: boolean;
83
+ frozen: boolean;
84
+ borrowable: boolean;
85
+ collateralRisk: number;
86
+ collateralFactor: number;
87
+ liquidationFee: number;
88
+ maxLiquidationBonus: number;
89
+ price: string;
90
+ totalSupplied: string;
91
+ totalDrawn: string;
92
+ totalPremium: string;
93
+ totalDebt: string;
94
+ supplyCap: string;
95
+ borrowCap: string;
96
+ spokeActive: boolean;
97
+ spokeHalted: boolean;
98
+ drawnRate: string;
99
+ supplyRate: string;
100
+ borrowRate: string;
101
+ supplyIncentives: IncentiveData[];
102
+ borrowIncentives: IncentiveData[];
103
+ canBeBorrowed: boolean;
104
+ canBeSupplied: boolean;
105
+ canBeWithdrawn: boolean;
106
+ canBePayBacked: boolean;
107
+ utilization: string;
108
+ }
109
+ export type AaveV4AssetsData = Record<string, AaveV4ReserveAssetData>;
110
+ export interface AaveV4UsedReserveAsset {
111
+ symbol: string;
112
+ hubName: string;
113
+ assetId: number;
114
+ reserveId: number;
115
+ supplied: string;
116
+ suppliedUsd: string;
117
+ drawn: string;
118
+ drawnUsd: string;
119
+ premium: string;
120
+ premiumUsd: string;
121
+ borrowed: string;
122
+ borrowedUsd: string;
123
+ isSupplied: boolean;
124
+ isBorrowed: boolean;
125
+ collateral: boolean;
126
+ collateralFactor: number;
127
+ }
128
+ export interface AaveV4AggregatedPositionData {
129
+ suppliedUsd: string;
130
+ suppliedCollateralUsd: string;
131
+ borrowLimitUsd: string;
132
+ liquidationLimitUsd: string;
133
+ borrowedUsd: string;
134
+ drawnUsd: string;
135
+ premiumUsd: string;
136
+ leftToBorrowUsd: string;
137
+ ratio: string;
138
+ collRatio: string;
139
+ liqRatio: string;
140
+ liqPercent: string;
141
+ leveragedType: LeverageType;
142
+ leveragedAsset: string;
143
+ liquidationPrice: string;
144
+ minCollRatio: string;
145
+ collLiquidationRatio: string;
146
+ minHealthRatio: string;
147
+ healthRatio: string;
148
+ netApy: string;
149
+ incentiveUsd: string;
150
+ totalInterestUsd: string;
151
+ currentVolatilePairRatio?: string;
152
+ minRatio: string;
153
+ }
154
+ export type AaveV4UsedReserveAssets = Record<string, AaveV4UsedReserveAsset>;
155
+ export interface AaveV4AccountData extends AaveV4AggregatedPositionData {
156
+ usedAssets: AaveV4UsedReserveAssets;
157
+ healthFactor: string;
158
+ riskPremiumBps: number;
159
+ }
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AaveV4SpokesType = exports.AaveV4HubsType = void 0;
4
+ var AaveV4HubsType;
5
+ (function (AaveV4HubsType) {
6
+ AaveV4HubsType["AaveV4CoreHub"] = "aave_v4_core_hub";
7
+ AaveV4HubsType["AaveV4PlusHub"] = "aave_v4_plus_hub";
8
+ AaveV4HubsType["AaveV4PrimeHub"] = "aave_v4_prime_hub";
9
+ })(AaveV4HubsType || (exports.AaveV4HubsType = AaveV4HubsType = {}));
10
+ var AaveV4SpokesType;
11
+ (function (AaveV4SpokesType) {
12
+ AaveV4SpokesType["AaveV4BluechipSpoke"] = "aave_v4_bluechip_spoke";
13
+ AaveV4SpokesType["AaveV4EthenaCorrelatedSpoke"] = "aave_v4_ethena_correlated_spoke";
14
+ AaveV4SpokesType["AaveV4EthenaEcosystemSpoke"] = "aave_v4_ethena_ecosystem_spoke";
15
+ AaveV4SpokesType["AaveV4EtherfiSpoke"] = "aave_v4_etherfi_spoke";
16
+ AaveV4SpokesType["AaveV4ForexSpoke"] = "aave_v4_forex_spoke";
17
+ AaveV4SpokesType["AaveV4GoldSpoke"] = "aave_v4_gold_spoke";
18
+ AaveV4SpokesType["AaveV4KelpSpoke"] = "aave_v4_kelp_spoke";
19
+ AaveV4SpokesType["AaveV4LidoSpoke"] = "aave_v4_lido_spoke";
20
+ AaveV4SpokesType["AaveV4LombardBtcSpoke"] = "aave_v4_lombard_btc_spoke";
21
+ AaveV4SpokesType["AaveV4MainSpoke"] = "aave_v4_main_spoke";
22
+ })(AaveV4SpokesType || (exports.AaveV4SpokesType = AaveV4SpokesType = {}));
@@ -14,3 +14,4 @@ export * from './merit';
14
14
  export * from './merkl';
15
15
  export * from './savings';
16
16
  export * from './common';
17
+ export * from './aaveV4';
@@ -30,3 +30,4 @@ __exportStar(require("./merit"), exports);
30
30
  __exportStar(require("./merkl"), exports);
31
31
  __exportStar(require("./savings"), exports);
32
32
  __exportStar(require("./common"), exports);
33
+ __exportStar(require("./aaveV4"), exports);
@@ -1,4 +1,5 @@
1
1
  import { AaveV2PositionData, AaveV3PositionData, AaveVersions } from './aave';
2
+ import { AaveV4AccountData, AaveV4SpokesType } from './aaveV4';
2
3
  import { EthAddress } from './common';
3
4
  import { CompoundV2PositionData, CompoundV3PositionData, CompoundVersions } from './compound';
4
5
  import { CrvUSDUserData, CrvUSDVersions } from './curveUsd';
@@ -51,6 +52,9 @@ export interface PortfolioPositionsDataForAddress {
51
52
  [key: string]: FluidVaultData;
52
53
  };
53
54
  };
55
+ aaveV4: {
56
+ [key in AaveV4SpokesType]?: PortfolioProtocolData<AaveV4AccountData>;
57
+ };
54
58
  }
55
59
  export interface PortfolioPositionsData {
56
60
  [key: EthAddress]: PortfolioPositionsDataForAddress;
@@ -0,0 +1,7 @@
1
+ import { Client } from 'viem';
2
+ import { AaveV4AccountData, AaveV4SpokeData, AaveV4SpokeInfo, EthAddress, EthereumProvider, NetworkNumber } from '../types';
3
+ export * as lend from './lend';
4
+ export declare function _getAaveV4SpokeData(provider: Client, network: NetworkNumber, market: AaveV4SpokeInfo, blockNumber?: 'latest' | number): Promise<AaveV4SpokeData>;
5
+ export declare function getAaveV4SpokeData(provider: EthereumProvider, network: NetworkNumber, spoke: AaveV4SpokeInfo, blockNumber?: 'latest' | number): Promise<AaveV4SpokeData>;
6
+ export declare function _getAaveV4AccountData(provider: Client, network: NetworkNumber, spokeData: AaveV4SpokeData, address: EthAddress, blockNumber?: 'latest' | number): Promise<AaveV4AccountData>;
7
+ export declare function getAaveV4AccountData(provider: EthereumProvider, network: NetworkNumber, marketData: AaveV4SpokeData, address: EthAddress, blockNumber?: 'latest' | number): Promise<any>;
@@ -0,0 +1,200 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import Dec from 'decimal.js';
11
+ import { assetAmountInEth, getAssetInfoByAddress } from '@defisaver/tokens';
12
+ import { getViemProvider } from '../services/viem';
13
+ import { IncentiveKind, } from '../types';
14
+ import { AaveV4ViewContractViem } from '../contracts';
15
+ import { getStakingApy, STAKING_ASSETS } from '../staking';
16
+ import { isMaxUint, wethToEth } from '../services/utils';
17
+ import { aaveV4GetAggregatedPositionData, calcUserRiskPremiumBps } from '../helpers/aaveV4Helpers';
18
+ import { getAaveV4HubByAddress } from '../markets/aaveV4';
19
+ import { aprToApy } from '../moneymarket';
20
+ export * as lend from './lend';
21
+ const fetchHubData = (viewContract, hubAddress) => __awaiter(void 0, void 0, void 0, function* () {
22
+ const hubData = yield viewContract.read.getHubAllAssetsData([hubAddress]);
23
+ return {
24
+ assets: hubData.reduce((acc, assetOnChainData) => {
25
+ acc[assetOnChainData.assetId] = {
26
+ assetId: assetOnChainData.assetId,
27
+ drawnRate: assetOnChainData.drawnRate,
28
+ liquidity: assetOnChainData.liquidity,
29
+ liquidityFee: assetOnChainData.liquidityFee,
30
+ swept: assetOnChainData.swept,
31
+ totalDrawn: assetOnChainData.totalDrawn,
32
+ totalDrawnShares: assetOnChainData.totalDrawnShares,
33
+ totalPremiumShares: assetOnChainData.totalPremiumShares,
34
+ };
35
+ return acc;
36
+ }, {}),
37
+ };
38
+ });
39
+ const formatReserveAsset = (reserveAsset, hubAsset, reserveId, oracleDecimals, network) => __awaiter(void 0, void 0, void 0, function* () {
40
+ var _a, _b, _c, _d, _e, _f;
41
+ const assetInfo = getAssetInfoByAddress(reserveAsset.underlying, network);
42
+ const symbol = wethToEth(assetInfo.symbol);
43
+ const hubInfo = getAaveV4HubByAddress(network, reserveAsset.hub);
44
+ if (!hubInfo) {
45
+ throw new Error(`Hub not found with address: ${reserveAsset.hub}`);
46
+ }
47
+ const isStakingAsset = STAKING_ASSETS.includes(symbol);
48
+ const supplyIncentives = [];
49
+ const borrowIncentives = [];
50
+ if (isStakingAsset) {
51
+ const yieldApy = yield getStakingApy(symbol, network);
52
+ supplyIncentives.push({
53
+ apy: yieldApy,
54
+ token: symbol,
55
+ incentiveKind: IncentiveKind.Staking,
56
+ description: `Native ${symbol} yield.`,
57
+ });
58
+ if (reserveAsset.borrowable) {
59
+ // When borrowing assets whose value increases over time
60
+ borrowIncentives.push({
61
+ apy: new Dec(yieldApy).mul(-1).toString(),
62
+ token: symbol,
63
+ incentiveKind: IncentiveKind.Reward,
64
+ description: `Due to the native yield of ${symbol}, the value of the debt would increase over time.`,
65
+ });
66
+ }
67
+ }
68
+ const totalSuppliedRaw = (_a = reserveAsset.totalSupplied) !== null && _a !== void 0 ? _a : 0;
69
+ const totalDrawnRaw = (_b = reserveAsset.totalDrawn) !== null && _b !== void 0 ? _b : 0;
70
+ const totalPremiumRaw = (_c = reserveAsset.totalPremium) !== null && _c !== void 0 ? _c : 0;
71
+ const totalDebtRaw = (_d = reserveAsset.totalDebt) !== null && _d !== void 0 ? _d : 0;
72
+ const supplyCapRaw = (_e = reserveAsset.supplyCap) !== null && _e !== void 0 ? _e : 0;
73
+ const borrowCapRaw = (_f = reserveAsset.borrowCap) !== null && _f !== void 0 ? _f : 0;
74
+ /** @DEV Hub related calculations */
75
+ const drawnRate = new Dec(hubAsset.drawnRate.toString()).div(new Dec(10).pow(27));
76
+ const borrowApr = drawnRate.mul(100);
77
+ const totalDrawn = new Dec(hubAsset.totalDrawn.toString());
78
+ const liquidity = new Dec(hubAsset.liquidity.toString());
79
+ const swept = new Dec(hubAsset.swept.toString());
80
+ const hubUtilizationDenominator = totalDrawn.add(swept).add(liquidity);
81
+ const hubUtilization = hubUtilizationDenominator.isZero() ? new Dec(0) : totalDrawn.div(hubUtilizationDenominator);
82
+ const liquidityFee = new Dec(hubAsset.liquidityFee.toString()).div(new Dec(10).pow(4));
83
+ const totalDrawnShares = new Dec(hubAsset.totalDrawnShares.toString());
84
+ const totalPremiumShares = new Dec(hubAsset.totalPremiumShares.toString());
85
+ const premiumMultiplier = totalDrawnShares.isZero() ? new Dec(1) : totalDrawnShares.add(totalPremiumShares).div(totalDrawnShares);
86
+ const supplyApr = borrowApr.mul(hubUtilization).mul(premiumMultiplier).mul(new Dec(1).minus(liquidityFee));
87
+ const utilization = hubUtilization.times(100).toString();
88
+ return ({
89
+ symbol,
90
+ underlying: reserveAsset.underlying,
91
+ hub: hubInfo.address,
92
+ hubName: hubInfo === null || hubInfo === void 0 ? void 0 : hubInfo.label,
93
+ assetId: reserveAsset.assetId,
94
+ reserveId,
95
+ paused: reserveAsset.paused,
96
+ frozen: reserveAsset.frozen,
97
+ borrowable: reserveAsset.borrowable,
98
+ collateralRisk: new Dec(reserveAsset.collateralRisk).div(10000).toNumber(),
99
+ collateralFactor: new Dec(reserveAsset.collateralFactor).div(10000).toNumber(),
100
+ liquidationFee: new Dec(reserveAsset.liquidationFee).div(10000).toNumber(),
101
+ maxLiquidationBonus: new Dec(reserveAsset.maxLiquidationBonus).div(10000).toNumber(),
102
+ price: new Dec(reserveAsset.price).div(new Dec(10).pow(oracleDecimals)).toString(),
103
+ totalSupplied: isMaxUint(totalSuppliedRaw.toString()) ? totalSuppliedRaw.toString() : assetAmountInEth(totalSuppliedRaw.toString(), symbol),
104
+ totalDrawn: isMaxUint(totalDrawnRaw.toString()) ? totalDrawnRaw.toString() : assetAmountInEth(totalDrawnRaw.toString(), symbol),
105
+ totalPremium: isMaxUint(totalPremiumRaw.toString()) ? totalPremiumRaw.toString() : assetAmountInEth(totalPremiumRaw.toString(), symbol),
106
+ totalDebt: isMaxUint(totalDebtRaw.toString()) ? totalDebtRaw.toString() : assetAmountInEth(totalDebtRaw.toString(), symbol),
107
+ supplyCap: isMaxUint(supplyCapRaw.toString()) ? supplyCapRaw.toString() : assetAmountInEth(supplyCapRaw.toString(), symbol),
108
+ borrowCap: isMaxUint(borrowCapRaw.toString()) ? borrowCapRaw.toString() : assetAmountInEth(borrowCapRaw.toString(), symbol),
109
+ spokeActive: reserveAsset.spokeActive,
110
+ spokeHalted: reserveAsset.spokeHalted,
111
+ drawnRate: drawnRate.toString(),
112
+ borrowRate: aprToApy(borrowApr.toString()),
113
+ supplyRate: aprToApy(supplyApr.toString()),
114
+ supplyIncentives,
115
+ borrowIncentives,
116
+ canBeBorrowed: reserveAsset.spokeActive && !reserveAsset.spokeHalted && !reserveAsset.paused && !reserveAsset.frozen && reserveAsset.borrowable,
117
+ canBeSupplied: reserveAsset.spokeActive && !reserveAsset.spokeHalted && !reserveAsset.paused && !reserveAsset.frozen,
118
+ canBeWithdrawn: reserveAsset.spokeActive && !reserveAsset.spokeHalted && !reserveAsset.paused,
119
+ canBePayBacked: reserveAsset.spokeActive && !reserveAsset.spokeHalted && !reserveAsset.paused,
120
+ utilization,
121
+ });
122
+ });
123
+ export function _getAaveV4SpokeData(provider_1, network_1, market_1) {
124
+ return __awaiter(this, arguments, void 0, function* (provider, network, market, blockNumber = 'latest') {
125
+ const viewContract = AaveV4ViewContractViem(provider, network, blockNumber);
126
+ const hubsData = {};
127
+ const [spokeData] = yield Promise.all([
128
+ viewContract.read.getSpokeDataFull([market.address]),
129
+ ...market.hubs.map((hubAddress) => __awaiter(this, void 0, void 0, function* () {
130
+ hubsData[hubAddress] = yield fetchHubData(viewContract, hubAddress);
131
+ })),
132
+ ]);
133
+ const reserveAssetsArray = yield Promise.all(spokeData[1].map((reserveAssetOnChain, index) => __awaiter(this, void 0, void 0, function* () { return formatReserveAsset(reserveAssetOnChain, hubsData[reserveAssetOnChain.hub].assets[reserveAssetOnChain.assetId], index, +spokeData[0].oracleDecimals.toString(), network); })));
134
+ return {
135
+ assetsData: reserveAssetsArray.reduce((acc, reserveAsset) => {
136
+ acc[`${reserveAsset.symbol}-${reserveAsset.reserveId}`] = reserveAsset;
137
+ return acc;
138
+ }, {}),
139
+ oracle: spokeData[0].oracle,
140
+ oracleDecimals: +spokeData[0].oracleDecimals.toString(),
141
+ address: market.address,
142
+ };
143
+ });
144
+ }
145
+ export function getAaveV4SpokeData(provider_1, network_1, spoke_1) {
146
+ return __awaiter(this, arguments, void 0, function* (provider, network, spoke, blockNumber = 'latest') {
147
+ return _getAaveV4SpokeData(getViemProvider(provider, network), network, spoke, blockNumber);
148
+ });
149
+ }
150
+ export function _getAaveV4AccountData(provider_1, network_1, spokeData_1, address_1) {
151
+ return __awaiter(this, arguments, void 0, function* (provider, network, spokeData, address, blockNumber = 'latest') {
152
+ const viewContract = AaveV4ViewContractViem(provider, network, blockNumber);
153
+ const loanData = yield viewContract.read.getLoanData([spokeData.address, address]);
154
+ const healthFactorFromContract = new Dec(loanData.healthFactor.toString());
155
+ const healthFactor = isMaxUint(healthFactorFromContract.toString()) ? 'Infinity' : healthFactorFromContract.div(1e18).toString();
156
+ const usedAssets = loanData.reserves.reduce((acc, usedReserveAsset) => {
157
+ const identifier = `${wethToEth(getAssetInfoByAddress(usedReserveAsset.underlying, network).symbol)}-${+usedReserveAsset.reserveId.toString()}`;
158
+ const reserveData = spokeData.assetsData[identifier];
159
+ const price = reserveData.price;
160
+ const supplied = isMaxUint(usedReserveAsset.supplied.toString()) ? usedReserveAsset.supplied.toString() : assetAmountInEth(usedReserveAsset.supplied.toString(), reserveData.symbol);
161
+ const drawn = isMaxUint(usedReserveAsset.drawn.toString()) ? usedReserveAsset.drawn.toString() : assetAmountInEth(usedReserveAsset.drawn.toString(), reserveData.symbol);
162
+ const premium = isMaxUint(usedReserveAsset.premium.toString()) ? usedReserveAsset.premium.toString() : assetAmountInEth(usedReserveAsset.premium.toString(), reserveData.symbol);
163
+ const borrowed = isMaxUint(usedReserveAsset.totalDebt.toString()) ? usedReserveAsset.totalDebt.toString() : assetAmountInEth(usedReserveAsset.totalDebt.toString(), reserveData.symbol);
164
+ acc[identifier] = {
165
+ symbol: reserveData.symbol,
166
+ hubName: reserveData.hubName,
167
+ assetId: reserveData.assetId,
168
+ reserveId: +usedReserveAsset.reserveId.toString(),
169
+ supplied,
170
+ suppliedUsd: new Dec(supplied).mul(price).toString(),
171
+ drawn,
172
+ drawnUsd: new Dec(drawn).mul(price).toString(),
173
+ premium,
174
+ premiumUsd: new Dec(premium).mul(price).toString(),
175
+ borrowed,
176
+ borrowedUsd: new Dec(borrowed).mul(price).toString(),
177
+ isSupplied: !new Dec(supplied).eq(0),
178
+ isBorrowed: usedReserveAsset.isBorrowing,
179
+ collateral: usedReserveAsset.isUsingAsCollateral,
180
+ collateralFactor: new Dec(usedReserveAsset.collateralFactor).div(10000).toNumber(),
181
+ };
182
+ return acc;
183
+ }, {});
184
+ const aggregated = aaveV4GetAggregatedPositionData({
185
+ usedAssets,
186
+ assetsData: spokeData.assetsData,
187
+ network,
188
+ useUserCollateralFactor: true,
189
+ });
190
+ const riskPremiumBps = calcUserRiskPremiumBps(usedAssets, spokeData.assetsData);
191
+ return Object.assign(Object.assign({}, aggregated), { usedAssets,
192
+ healthFactor,
193
+ riskPremiumBps });
194
+ });
195
+ }
196
+ export function getAaveV4AccountData(provider_1, network_1, marketData_1, address_1) {
197
+ return __awaiter(this, arguments, void 0, function* (provider, network, marketData, address, blockNumber = 'latest') {
198
+ return _getAaveV4AccountData(getViemProvider(provider, network), network, marketData, address, blockNumber);
199
+ });
200
+ }
@@ -0,0 +1,55 @@
1
+ import { EthAddress, EthereumProvider, NetworkNumber } from '../types/common';
2
+ export interface AaveV4TokenizationSpokeData {
3
+ underlyingAsset: EthAddress;
4
+ assetId: string;
5
+ decimals: number;
6
+ spoke: EthAddress;
7
+ spokeActive: boolean;
8
+ spokeHalted: boolean;
9
+ spokeDepositCap: string;
10
+ spokeTotalAssets: string;
11
+ spokeTotalShares: string;
12
+ hub: EthAddress;
13
+ hubLiquidity: string;
14
+ hubDrawnRate: string;
15
+ convertToShares: string;
16
+ convertToAssets: string;
17
+ user: EthAddress;
18
+ userSuppliedAssets: string;
19
+ userSuppliedShares: string;
20
+ }
21
+ export declare const AAVE_V4_TOKENIZED_SPOKES: Record<string, EthAddress>;
22
+ export declare const AAVE_V4_TOKENIZED_SPOKE_ADDRESSES: Partial<Record<NetworkNumber, EthAddress[]>>;
23
+ export type AaveV4TokenizedHubKey = 'CORE' | 'PLUS' | 'PRIME';
24
+ export declare const aaveV4GetTokenizedHubKey: (hubNameOrKey?: string | null) => AaveV4TokenizedHubKey | null;
25
+ export declare const aaveV4GetTokenizedVaultKey: (symbol: string, hubNameOrKey?: string | null) => string | null;
26
+ export declare const aaveV4GetTokenizedVaultAddress: (network: NetworkNumber, symbol: string, hubNameOrKey?: string | null) => EthAddress | undefined;
27
+ /** Parsed tokenization spoke data with human-readable supplied amounts for display */
28
+ export interface AaveV4TokenizationSpokeDataParsed {
29
+ vaultAddress: EthAddress;
30
+ key: string | null;
31
+ symbol: string;
32
+ hubKey: string;
33
+ spokeActive: boolean;
34
+ spokeHalted: boolean;
35
+ /** Deposit cap in asset units (wei string) */
36
+ spokeDepositCap: string;
37
+ /** Total assets currently in spoke in asset units (wei string) */
38
+ spokeTotalAssets: string;
39
+ /** Available hub liquidity in asset units (wei string) */
40
+ hubLiquidity: string;
41
+ /** The conversion rate from assets to shares expressed in asset units. */
42
+ convertToShares: string;
43
+ userSuppliedAssetsEth: string;
44
+ userSuppliedSharesEth: string;
45
+ userSuppliedAssets: string;
46
+ userSuppliedShares: string;
47
+ underlyingAsset: EthAddress;
48
+ spoke: EthAddress;
49
+ decimals: number;
50
+ }
51
+ /**
52
+ * Fetches tokenization vault data for the given user via getTokenizationSpokesData.
53
+ * Returns parsed data including userSuppliedAssets in human-readable form for each vault.
54
+ */
55
+ export declare function getAaveV4TokenizationSpokesData(provider: EthereumProvider, network: NetworkNumber, userAddress: EthAddress): Promise<AaveV4TokenizationSpokeDataParsed[]>;