@defisaver/positions-sdk 2.1.72-aave-v4-dev → 2.1.72-aave-v4-3-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.
@@ -10,12 +10,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  import Dec from 'decimal.js';
11
11
  import { assetAmountInEth, getAssetInfoByAddress } from '@defisaver/tokens';
12
12
  import { getViemProvider } from '../services/viem';
13
- import { IncentiveKind, } from '../types/common';
13
+ import { IncentiveKind, } from '../types';
14
14
  import { AaveV4ViewContractViem } from '../contracts';
15
15
  import { getStakingApy, STAKING_ASSETS } from '../staking';
16
16
  import { wethToEth } from '../services/utils';
17
17
  import { aaveV4GetAggregatedPositionData } from '../helpers/aaveV4Helpers';
18
18
  import { getAaveV4HubByAddress } from '../markets/aaveV4';
19
+ import { aprToApy } from '../moneymarket';
20
+ export * as lend from './lend';
19
21
  const fetchHubData = (viewContract, hubAddress) => __awaiter(void 0, void 0, void 0, function* () {
20
22
  const hubData = yield viewContract.read.getHubAllAssetsData([hubAddress]);
21
23
  return {
@@ -23,12 +25,71 @@ const fetchHubData = (viewContract, hubAddress) => __awaiter(void 0, void 0, voi
23
25
  acc[assetOnChainData.assetId] = {
24
26
  assetId: assetOnChainData.assetId,
25
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,
26
34
  };
27
35
  return acc;
28
36
  }, {}),
29
37
  };
30
38
  });
39
+ const calcUserRiskPremiumBps = (usedAssets, assetsData) => {
40
+ const collaterals = [];
41
+ const debts = [];
42
+ Object.entries(usedAssets).forEach(([identifier, asset]) => {
43
+ const reserveData = assetsData[identifier];
44
+ if (!reserveData)
45
+ return;
46
+ const borrowedUsdDec = new Dec(asset.borrowedUsd || '0');
47
+ if (asset.isBorrowed && borrowedUsdDec.gt(0)) {
48
+ debts.push({ valueUsd: borrowedUsdDec });
49
+ }
50
+ const suppliedUsdDec = new Dec(asset.suppliedUsd || '0');
51
+ const isActiveCollateral = asset.collateral
52
+ && asset.isSupplied
53
+ && asset.collateralFactor > 0
54
+ && suppliedUsdDec.gt(0);
55
+ if (isActiveCollateral) {
56
+ // collateralRisk is stored as a fraction (e.g. 0.25), convert back to bps
57
+ const riskBps = new Dec(reserveData.collateralRisk).mul(10000).toNumber();
58
+ collaterals.push({
59
+ riskBps,
60
+ valueUsd: suppliedUsdDec,
61
+ });
62
+ }
63
+ });
64
+ const totalDebtUsd = debts.reduce((sum, d) => sum.add(d.valueUsd), new Dec(0));
65
+ if (totalDebtUsd.lte(0)) {
66
+ return 0;
67
+ }
68
+ // sort by risk ASC, value DESC
69
+ collaterals.sort((a, b) => {
70
+ if (a.riskBps !== b.riskBps)
71
+ return a.riskBps - b.riskBps;
72
+ return b.valueUsd.comparedTo(a.valueUsd);
73
+ });
74
+ let debtLeftToCover = totalDebtUsd;
75
+ let numerator = new Dec(0); // sum(coveredUsd * riskBps)
76
+ let coveredDebt = new Dec(0); // sum(coveredUsd)
77
+ collaterals.forEach(({ riskBps, valueUsd }) => {
78
+ if (debtLeftToCover.lte(0))
79
+ return;
80
+ const coveredUsd = Dec.min(valueUsd, debtLeftToCover);
81
+ numerator = numerator.add(coveredUsd.mul(riskBps));
82
+ coveredDebt = coveredDebt.add(coveredUsd);
83
+ debtLeftToCover = debtLeftToCover.sub(coveredUsd);
84
+ });
85
+ if (coveredDebt.lte(0)) {
86
+ return 0;
87
+ }
88
+ const riskPremiumBps = numerator.div(coveredDebt);
89
+ return riskPremiumBps.toNumber();
90
+ };
31
91
  const formatReserveAsset = (reserveAsset, hubAsset, reserveId, oracleDecimals, network) => __awaiter(void 0, void 0, void 0, function* () {
92
+ var _a, _b, _c, _d, _e, _f;
32
93
  const assetInfo = getAssetInfoByAddress(reserveAsset.underlying, network);
33
94
  const symbol = wethToEth(assetInfo.symbol);
34
95
  const hubInfo = getAaveV4HubByAddress(network, reserveAsset.hub);
@@ -56,6 +117,26 @@ const formatReserveAsset = (reserveAsset, hubAsset, reserveId, oracleDecimals, n
56
117
  });
57
118
  }
58
119
  }
120
+ const totalSuppliedRaw = (_a = reserveAsset.totalSupplied) !== null && _a !== void 0 ? _a : 0;
121
+ const totalDrawnRaw = (_b = reserveAsset.totalDrawn) !== null && _b !== void 0 ? _b : 0;
122
+ const totalPremiumRaw = (_c = reserveAsset.totalPremium) !== null && _c !== void 0 ? _c : 0;
123
+ const totalDebtRaw = (_d = reserveAsset.totalDebt) !== null && _d !== void 0 ? _d : 0;
124
+ const supplyCapRaw = (_e = reserveAsset.supplyCap) !== null && _e !== void 0 ? _e : 0;
125
+ const borrowCapRaw = (_f = reserveAsset.borrowCap) !== null && _f !== void 0 ? _f : 0;
126
+ /** @DEV Hub related calculations */
127
+ const drawnRate = new Dec(hubAsset.drawnRate.toString()).div(new Dec(10).pow(27));
128
+ const borrowApr = drawnRate.mul(100);
129
+ const totalDrawn = new Dec(hubAsset.totalDrawn.toString());
130
+ const liquidity = new Dec(hubAsset.liquidity.toString());
131
+ const swept = new Dec(hubAsset.swept.toString());
132
+ const hubUtilization = totalDrawn.div(totalDrawn.add(swept).add(liquidity));
133
+ const liquidityFee = new Dec(hubAsset.liquidityFee.toString()).div(new Dec(10).pow(4));
134
+ const totalDrawnShares = new Dec(hubAsset.totalDrawnShares.toString());
135
+ const totalPremiumShares = new Dec(hubAsset.totalPremiumShares.toString());
136
+ // TODO JK@JK premiumMultiplier should be added to supplyApr calculation (.mul(premiumMultiplier)
137
+ // TODO JKJ@JK when we confirm that this is the right way to calculate it
138
+ const premiumMultiplier = totalDrawnShares.add(totalPremiumShares).div(totalDrawnShares);
139
+ const supplyApr = borrowApr.mul(hubUtilization).mul(new Dec(1).minus(liquidityFee));
59
140
  return ({
60
141
  symbol,
61
142
  underlying: reserveAsset.underlying,
@@ -70,16 +151,17 @@ const formatReserveAsset = (reserveAsset, hubAsset, reserveId, oracleDecimals, n
70
151
  collateralFactor: new Dec(reserveAsset.collateralFactor).div(10000).toNumber(),
71
152
  liquidationFee: new Dec(reserveAsset.liquidationFee).div(10000).toNumber(),
72
153
  price: new Dec(reserveAsset.price).div(new Dec(10).pow(oracleDecimals)).toString(),
73
- totalSupplied: assetAmountInEth(reserveAsset.totalSupplied.toString(), symbol),
74
- totalDrawn: assetAmountInEth(reserveAsset.totalDrawn.toString(), symbol),
75
- totalPremium: assetAmountInEth(reserveAsset.totalPremium.toString(), symbol),
76
- totalDebt: assetAmountInEth(reserveAsset.totalDebt.toString(), symbol),
77
- supplyCap: assetAmountInEth(reserveAsset.supplyCap.toString(), symbol),
78
- borrowCap: assetAmountInEth(reserveAsset.borrowCap.toString(), symbol),
154
+ totalSupplied: assetAmountInEth(totalSuppliedRaw.toString(), symbol),
155
+ totalDrawn: assetAmountInEth(totalDrawnRaw.toString(), symbol),
156
+ totalPremium: assetAmountInEth(totalPremiumRaw.toString(), symbol),
157
+ totalDebt: assetAmountInEth(totalDebtRaw.toString(), symbol),
158
+ supplyCap: assetAmountInEth(supplyCapRaw.toString(), symbol),
159
+ borrowCap: assetAmountInEth(borrowCapRaw.toString(), symbol),
79
160
  spokeActive: reserveAsset.spokeActive,
80
161
  spokeHalted: reserveAsset.spokeHalted,
81
- drawnRate: new Dec(hubAsset.drawnRate).div(new Dec(10).pow(27)).toString(),
82
- supplyRate: '0', // To be implemented
162
+ drawnRate: drawnRate.toString(),
163
+ borrowRate: aprToApy(borrowApr.toString()),
164
+ supplyRate: aprToApy(supplyApr.toString()),
83
165
  supplyIncentives,
84
166
  borrowIncentives,
85
167
  canBeBorrowed: reserveAsset.spokeActive && !reserveAsset.spokeHalted && !reserveAsset.paused && !reserveAsset.frozen,
@@ -149,13 +231,16 @@ export function _getAaveV4AccountData(provider_1, network_1, spokeData_1, addres
149
231
  };
150
232
  return acc;
151
233
  }, {});
152
- return Object.assign({ usedAssets,
153
- healthFactor }, aaveV4GetAggregatedPositionData({
234
+ const aggregated = aaveV4GetAggregatedPositionData({
154
235
  usedAssets,
155
236
  assetsData: spokeData.assetsData,
156
237
  network,
157
238
  useUserCollateralFactor: true,
158
- }));
239
+ });
240
+ const riskPremiumBps = calcUserRiskPremiumBps(usedAssets, spokeData.assetsData);
241
+ return Object.assign(Object.assign({}, aggregated), { usedAssets,
242
+ healthFactor,
243
+ riskPremiumBps });
159
244
  });
160
245
  }
161
246
  export function getAaveV4AccountData(provider_1, network_1, marketData_1, address_1) {
@@ -0,0 +1,45 @@
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
+ userSuppliedAssetsEth: string;
34
+ userSuppliedSharesEth: string;
35
+ userSuppliedAssets: string;
36
+ userSuppliedShares: string;
37
+ underlyingAsset: EthAddress;
38
+ spoke: EthAddress;
39
+ decimals: number;
40
+ }
41
+ /**
42
+ * Fetches tokenization vault data for the given user via getTokenizationSpokesData.
43
+ * Returns parsed data including userSuppliedAssets in human-readable form for each vault.
44
+ */
45
+ export declare function getAaveV4TokenizationSpokesData(provider: EthereumProvider, network: NetworkNumber, userAddress: EthAddress): Promise<AaveV4TokenizationSpokeDataParsed[]>;
@@ -0,0 +1,134 @@
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 { assetAmountInEth, getAssetInfoByAddress } from '@defisaver/tokens';
11
+ import { AaveV4ViewContractViem } from '../contracts';
12
+ import { getViemProvider } from '../services/viem';
13
+ import { wethToEth } from '../services/utils';
14
+ import { NetworkNumber, } from '../types/common';
15
+ export const AAVE_V4_TOKENIZED_SPOKES = {
16
+ AAVE_CORE: '0x08309234884cF7E015b07Bf22569017Aa035cdeF',
17
+ EURC_CORE: '0x73596dED4B2Eb0aC85e477b3c8dB56FC427E6774',
18
+ GHO_CORE: '0xf7E1f1b43922527e5054bD77E7f863Cf182b194D',
19
+ GHO_PRIME: '0xeF1cAd5c6a2C9cb83c952b4B96bbD35b3F61F18f',
20
+ LBTC_CORE: '0x8f4D423590F22833131e3493bf67A27213398f8e',
21
+ LINK_CORE: '0xBE1197750b423e30137E97d9183065d33E903BE6',
22
+ PT_USDe_PLUS: '0x8cA27Ab284F2aA2BcF33D9129e11c101aD2d16de',
23
+ PT_sUSDe_PLUS: '0xb8A464EC56071a98c854f30fE19CfeCc41FA6233',
24
+ PYUSD_CORE: '0x203FB463087005698d50768FcA837047f738632d',
25
+ RLUSD_CORE: '0xa9afdd0c54fb153CaE39cE86E49626B5e9d15513',
26
+ USDC_CORE: '0xa2e476f4cbB06C7bFA8Ad51bCcbF198cd32CfD35',
27
+ USDC_PLUS: '0x320Bec4fB7a25e64c003A007D0AeF7AB3D6C30d7',
28
+ USDC_PRIME: '0x0A0507F7A1129892b5cf74b8B4e911442c466b87',
29
+ USDG_CORE: '0x87c224256f09a014C1BC3e9FbB094C3AdD8fBaCC',
30
+ USDT_CORE: '0x3f12BD5999b9172550893FF52691c980676f9E73',
31
+ USDT_PLUS: '0xa4E74a78bED2d3ab8971e8AB26fb39f26DD8eEd9',
32
+ USDT_PRIME: '0xF565fB55bc96d65561887898bfeb25C1dE7e06d2',
33
+ USDe_PLUS: '0xA0d346ab2699B689AC67aba5174164A84206BB73',
34
+ WBTC_CORE: '0x837Ab872A665e0CF467d41cF56a054031b4A38bc',
35
+ WBTC_PRIME: '0xeae98b8a1798738182B2123DF1eB93d97BD29F34',
36
+ WETH_CORE: '0x3961a75099E986F59a1a31c6f945061641dFD2b2',
37
+ WETH_PRIME: '0xa411826a6ef5d289c0FAa7d5B45FE8aAB52257F6',
38
+ XAUt_CORE: '0x470341bC0e2B833C54D0120642713BdF762A494F',
39
+ cbBTC_CORE: '0xe8D5E595d5b6b5EFf84B7064765fd0e8DfD214C9',
40
+ cbBTC_PRIME: '0x0E986545150DcDDe46Ea9df355D0fD2af33bd75D',
41
+ frxUSD_CORE: '0x00C8A6a42947Cc4E7B6f27963Cab0143ccaaD2B5',
42
+ frxUSD_PLUS: '0xCAB288d37CAb5a9db7b503F086455276Dcde61F1',
43
+ rsETH_CORE: '0x6eEce89caE2163584bA7Ff9743861B9633c245E0',
44
+ sUSDe_PLUS: '0xdf47fc43c88B06edC47753b7d647ff18037D2F3d',
45
+ weETH_CORE: '0xB67F20bFF413C8E5d633B54BD28899c4c9e33ed0',
46
+ wstETH_CORE: '0x474602394d0B02F43AC3D7C8c5cFc0814b03fd40',
47
+ wstETH_PRIME: '0xAcCdAb49ECB9A801CfF62a92fc80D52339b33770',
48
+ };
49
+ export const AAVE_V4_TOKENIZED_SPOKE_ADDRESSES = {
50
+ [NetworkNumber.Eth]: Object.values(AAVE_V4_TOKENIZED_SPOKES),
51
+ };
52
+ export const aaveV4GetTokenizedHubKey = (hubNameOrKey) => {
53
+ if (!hubNameOrKey)
54
+ return null;
55
+ const normalized = hubNameOrKey.trim().toUpperCase();
56
+ // TODO AaveV4 Maybe turn into constants
57
+ if (normalized === 'CORE' || normalized === 'CORE HUB')
58
+ return 'CORE';
59
+ if (normalized === 'PLUS' || normalized === 'PLUS HUB')
60
+ return 'PLUS';
61
+ if (normalized === 'PRIME' || normalized === 'PRIME HUB')
62
+ return 'PRIME';
63
+ if (normalized.includes('CORE'))
64
+ return 'CORE';
65
+ if (normalized.includes('PLUS'))
66
+ return 'PLUS';
67
+ if (normalized.includes('PRIME'))
68
+ return 'PRIME';
69
+ return null;
70
+ };
71
+ export const aaveV4GetTokenizedVaultKey = (symbol, hubNameOrKey) => {
72
+ if (!symbol)
73
+ return null;
74
+ const hubKey = aaveV4GetTokenizedHubKey(hubNameOrKey);
75
+ if (!hubKey)
76
+ return null;
77
+ const normalizedSymbol = symbol.trim().replace(/-/g, '_');
78
+ return `${normalizedSymbol}_${hubKey}`;
79
+ };
80
+ export const aaveV4GetTokenizedVaultAddress = (network, symbol, hubNameOrKey) => {
81
+ if (network !== NetworkNumber.Eth)
82
+ return undefined;
83
+ const key = aaveV4GetTokenizedVaultKey(symbol, hubNameOrKey);
84
+ if (!key)
85
+ return undefined;
86
+ return AAVE_V4_TOKENIZED_SPOKES[key];
87
+ };
88
+ const AAVE_V4_TOKENIZED_SPOKE_ADDRESS_TO_KEY = Object.entries(AAVE_V4_TOKENIZED_SPOKES).reduce((acc, [k, v]) => {
89
+ acc[v.toLowerCase()] = k;
90
+ return acc;
91
+ }, {});
92
+ /**
93
+ * Fetches tokenization vault data for the given user via getTokenizationSpokesData.
94
+ * Returns parsed data including userSuppliedAssets in human-readable form for each vault.
95
+ */
96
+ export function getAaveV4TokenizationSpokesData(provider, network, userAddress) {
97
+ return __awaiter(this, void 0, void 0, function* () {
98
+ var _a;
99
+ const spokes = (_a = AAVE_V4_TOKENIZED_SPOKE_ADDRESSES[network]) !== null && _a !== void 0 ? _a : [];
100
+ if (spokes.length === 0)
101
+ return [];
102
+ const client = getViemProvider(provider, network);
103
+ const viewContract = AaveV4ViewContractViem(client, network);
104
+ const raw = yield viewContract.read.getTokenizationSpokesData([spokes, userAddress]);
105
+ return raw.map((r, i) => {
106
+ var _a, _b, _c, _d, _e;
107
+ const vaultAddress = spokes[i];
108
+ const key = (_a = AAVE_V4_TOKENIZED_SPOKE_ADDRESS_TO_KEY[vaultAddress.toLowerCase()]) !== null && _a !== void 0 ? _a : null;
109
+ const symbol = wethToEth(getAssetInfoByAddress(r.underlyingAsset, network).symbol);
110
+ if (symbol === '?') { // unsupported asset
111
+ return null;
112
+ }
113
+ const hubKey = key ? (_b = key.split('_').pop()) !== null && _b !== void 0 ? _b : '' : '';
114
+ const decimals = Number((_c = r.decimals) !== null && _c !== void 0 ? _c : 18);
115
+ const userSuppliedAssetsRaw = (_d = r.userSuppliedAssets) !== null && _d !== void 0 ? _d : 0;
116
+ const userSuppliedSharesRaw = (_e = r.userSuppliedShares) !== null && _e !== void 0 ? _e : 0;
117
+ const userSuppliedAssetsEth = assetAmountInEth(userSuppliedAssetsRaw.toString(), symbol);
118
+ const userSuppliedSharesEth = assetAmountInEth(userSuppliedSharesRaw.toString(), symbol);
119
+ return {
120
+ vaultAddress,
121
+ key,
122
+ symbol,
123
+ hubKey,
124
+ userSuppliedAssetsEth,
125
+ userSuppliedSharesEth,
126
+ userSuppliedAssets: userSuppliedAssetsRaw.toString(),
127
+ userSuppliedShares: userSuppliedSharesRaw.toString(),
128
+ underlyingAsset: r.underlyingAsset,
129
+ spoke: r.spoke,
130
+ decimals,
131
+ };
132
+ }).filter(item => item != null);
133
+ });
134
+ }
@@ -1,5 +1,4 @@
1
- import { AaveV4AggregatedPositionData, AaveV4AssetsData, AaveV4ReserveAssetData, AaveV4UsedReserveAsset, AaveV4UsedReserveAssets } from '../../types';
2
- import { LeverageType, NetworkNumber } from '../../types/common';
1
+ import { AaveV4AggregatedPositionData, AaveV4AssetsData, AaveV4ReserveAssetData, AaveV4UsedReserveAsset, AaveV4UsedReserveAssets, LeverageType, NetworkNumber } from '../../types';
3
2
  export declare const aaveV4GetCollateralFactor: (assetData: AaveV4ReserveAssetData, usedAssetData: AaveV4UsedReserveAsset, useUserCollateralFactor?: boolean) => number;
4
3
  export declare const isLeveragedPosAaveV4: (usedAssets: AaveV4UsedReserveAssets, dustLimit?: number) => {
5
4
  leveragedType: LeverageType;
@@ -1,6 +1,6 @@
1
1
  import Dec from 'decimal.js';
2
2
  import { calcLeverageLiqPrice, getAssetsTotal, STABLE_ASSETS } from '../../moneymarket';
3
- import { LeverageType } from '../../types/common';
3
+ import { LeverageType, } from '../../types';
4
4
  export const aaveV4GetCollateralFactor = (assetData, usedAssetData, useUserCollateralFactor = false) => (useUserCollateralFactor ? usedAssetData.collateralFactor : assetData.collateralFactor);
5
5
  export const isLeveragedPosAaveV4 = (usedAssets, dustLimit = 5) => {
6
6
  let borrowUnstable = 0;
@@ -22,6 +22,12 @@ export interface AaveV4HubInfo {
22
22
  export interface AaveV4HubAssetOnChainData {
23
23
  assetId: number;
24
24
  drawnRate: bigint;
25
+ liquidity: bigint;
26
+ liquidityFee: number;
27
+ swept: bigint;
28
+ totalDrawn: bigint;
29
+ totalDrawnShares: bigint;
30
+ totalPremiumShares: bigint;
25
31
  }
26
32
  export interface AaveV4HubOnChainData {
27
33
  assets: Record<number, AaveV4HubAssetOnChainData>;
@@ -87,6 +93,7 @@ export interface AaveV4ReserveAssetData {
87
93
  spokeHalted: boolean;
88
94
  drawnRate: string;
89
95
  supplyRate: string;
96
+ borrowRate: string;
90
97
  supplyIncentives: IncentiveData[];
91
98
  borrowIncentives: IncentiveData[];
92
99
  canBeBorrowed: boolean;
@@ -142,4 +149,5 @@ export type AaveV4UsedReserveAssets = Record<string, AaveV4UsedReserveAsset>;
142
149
  export interface AaveV4AccountData extends AaveV4AggregatedPositionData {
143
150
  usedAssets: AaveV4UsedReserveAssets;
144
151
  healthFactor: string;
152
+ riskPremiumBps: number;
145
153
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defisaver/positions-sdk",
3
- "version": "2.1.72-aave-v4-dev",
3
+ "version": "2.1.72-aave-v4-3-dev",
4
4
  "description": "",
5
5
  "main": "./cjs/index.js",
6
6
  "module": "./esm/index.js",
@@ -6,17 +6,26 @@ import {
6
6
  AaveV4AccountData,
7
7
  AaveV4HubAssetOnChainData,
8
8
  AaveV4HubOnChainData,
9
- AaveV4ReserveAssetData, AaveV4ReserveAssetOnChain, AaveV4SpokeData, AaveV4SpokeInfo,
9
+ AaveV4ReserveAssetData,
10
+ AaveV4ReserveAssetOnChain,
11
+ AaveV4SpokeData,
12
+ AaveV4SpokeInfo,
10
13
  AaveV4UsedReserveAssets,
14
+ AaveV4AssetsData,
15
+ EthAddress,
16
+ EthereumProvider,
17
+ IncentiveData,
18
+ IncentiveKind,
19
+ NetworkNumber,
11
20
  } from '../types';
12
- import {
13
- EthAddress, EthereumProvider, IncentiveData, IncentiveKind, NetworkNumber,
14
- } from '../types/common';
15
21
  import { AaveV4ViewContractViem } from '../contracts';
16
22
  import { getStakingApy, STAKING_ASSETS } from '../staking';
17
23
  import { wethToEth } from '../services/utils';
18
24
  import { aaveV4GetAggregatedPositionData } from '../helpers/aaveV4Helpers';
19
25
  import { getAaveV4HubByAddress } from '../markets/aaveV4';
26
+ import { aprToApy } from '../moneymarket';
27
+
28
+ export * as lend from './lend';
20
29
 
21
30
  const fetchHubData = async (viewContract: ReturnType<typeof AaveV4ViewContractViem>, hubAddress: EthAddress): Promise<AaveV4HubOnChainData> => {
22
31
  const hubData = await viewContract.read.getHubAllAssetsData([hubAddress]);
@@ -25,12 +34,85 @@ const fetchHubData = async (viewContract: ReturnType<typeof AaveV4ViewContractVi
25
34
  acc[assetOnChainData.assetId] = {
26
35
  assetId: assetOnChainData.assetId,
27
36
  drawnRate: assetOnChainData.drawnRate,
37
+ liquidity: assetOnChainData.liquidity,
38
+ liquidityFee: assetOnChainData.liquidityFee,
39
+ swept: assetOnChainData.swept,
40
+ totalDrawn: assetOnChainData.totalDrawn,
41
+ totalDrawnShares: assetOnChainData.totalDrawnShares,
42
+ totalPremiumShares: assetOnChainData.totalPremiumShares,
28
43
  };
29
44
  return acc;
30
45
  }, {}),
31
46
  };
32
47
  };
33
48
 
49
+ const calcUserRiskPremiumBps = (usedAssets: AaveV4UsedReserveAssets, assetsData: AaveV4AssetsData): number => {
50
+ type CollateralInfo = { riskBps: number; valueUsd: Dec };
51
+ type DebtInfo = { valueUsd: Dec };
52
+
53
+ const collaterals: CollateralInfo[] = [];
54
+ const debts: DebtInfo[] = [];
55
+
56
+ Object.entries(usedAssets).forEach(([identifier, asset]) => {
57
+ const reserveData = assetsData[identifier];
58
+ if (!reserveData) return;
59
+
60
+ const borrowedUsdDec = new Dec(asset.borrowedUsd || '0');
61
+ if (asset.isBorrowed && borrowedUsdDec.gt(0)) {
62
+ debts.push({ valueUsd: borrowedUsdDec });
63
+ }
64
+
65
+ const suppliedUsdDec = new Dec(asset.suppliedUsd || '0');
66
+ const isActiveCollateral = asset.collateral
67
+ && asset.isSupplied
68
+ && asset.collateralFactor > 0
69
+ && suppliedUsdDec.gt(0);
70
+
71
+ if (isActiveCollateral) {
72
+ // collateralRisk is stored as a fraction (e.g. 0.25), convert back to bps
73
+ const riskBps = new Dec(reserveData.collateralRisk).mul(10000).toNumber();
74
+ collaterals.push({
75
+ riskBps,
76
+ valueUsd: suppliedUsdDec,
77
+ });
78
+ }
79
+ });
80
+
81
+ const totalDebtUsd = debts.reduce((sum, d) => sum.add(d.valueUsd), new Dec(0));
82
+
83
+ if (totalDebtUsd.lte(0)) {
84
+ return 0;
85
+ }
86
+
87
+ // sort by risk ASC, value DESC
88
+ collaterals.sort((a, b) => {
89
+ if (a.riskBps !== b.riskBps) return a.riskBps - b.riskBps;
90
+ return b.valueUsd.comparedTo(a.valueUsd);
91
+ });
92
+
93
+ let debtLeftToCover = totalDebtUsd;
94
+ let numerator = new Dec(0); // sum(coveredUsd * riskBps)
95
+ let coveredDebt = new Dec(0); // sum(coveredUsd)
96
+
97
+ collaterals.forEach(({ riskBps, valueUsd }) => {
98
+ if (debtLeftToCover.lte(0)) return;
99
+
100
+ const coveredUsd = Dec.min(valueUsd, debtLeftToCover);
101
+
102
+ numerator = numerator.add(coveredUsd.mul(riskBps));
103
+ coveredDebt = coveredDebt.add(coveredUsd);
104
+
105
+ debtLeftToCover = debtLeftToCover.sub(coveredUsd);
106
+ });
107
+
108
+ if (coveredDebt.lte(0)) {
109
+ return 0;
110
+ }
111
+
112
+ const riskPremiumBps = numerator.div(coveredDebt);
113
+ return riskPremiumBps.toNumber();
114
+ };
115
+
34
116
  const formatReserveAsset = async (reserveAsset: AaveV4ReserveAssetOnChain, hubAsset: AaveV4HubAssetOnChainData, reserveId: number, oracleDecimals: number, network: NetworkNumber): Promise<AaveV4ReserveAssetData> => {
35
117
  const assetInfo = getAssetInfoByAddress(reserveAsset.underlying, network);
36
118
  const symbol = wethToEth(assetInfo.symbol);
@@ -62,6 +144,28 @@ const formatReserveAsset = async (reserveAsset: AaveV4ReserveAssetOnChain, hubAs
62
144
  }
63
145
  }
64
146
 
147
+ const totalSuppliedRaw = reserveAsset.totalSupplied ?? 0;
148
+ const totalDrawnRaw = reserveAsset.totalDrawn ?? 0;
149
+ const totalPremiumRaw = reserveAsset.totalPremium ?? 0;
150
+ const totalDebtRaw = reserveAsset.totalDebt ?? 0;
151
+ const supplyCapRaw = reserveAsset.supplyCap ?? 0;
152
+ const borrowCapRaw = reserveAsset.borrowCap ?? 0;
153
+
154
+ /** @DEV Hub related calculations */
155
+ const drawnRate = new Dec(hubAsset.drawnRate.toString()).div(new Dec(10).pow(27));
156
+ const borrowApr = drawnRate.mul(100);
157
+ const totalDrawn = new Dec(hubAsset.totalDrawn.toString());
158
+ const liquidity = new Dec(hubAsset.liquidity.toString());
159
+ const swept = new Dec(hubAsset.swept.toString());
160
+ const hubUtilization = totalDrawn.div(totalDrawn.add(swept).add(liquidity));
161
+ const liquidityFee = new Dec(hubAsset.liquidityFee.toString()).div(new Dec(10).pow(4));
162
+ const totalDrawnShares = new Dec(hubAsset.totalDrawnShares.toString());
163
+ const totalPremiumShares = new Dec(hubAsset.totalPremiumShares.toString());
164
+ // TODO JK@JK premiumMultiplier should be added to supplyApr calculation (.mul(premiumMultiplier)
165
+ // TODO JKJ@JK when we confirm that this is the right way to calculate it
166
+ const premiumMultiplier = totalDrawnShares.add(totalPremiumShares).div(totalDrawnShares);
167
+ const supplyApr = borrowApr.mul(hubUtilization).mul(new Dec(1).minus(liquidityFee));
168
+
65
169
  return ({
66
170
  symbol,
67
171
  underlying: reserveAsset.underlying,
@@ -76,16 +180,17 @@ const formatReserveAsset = async (reserveAsset: AaveV4ReserveAssetOnChain, hubAs
76
180
  collateralFactor: new Dec(reserveAsset.collateralFactor).div(10000).toNumber(),
77
181
  liquidationFee: new Dec(reserveAsset.liquidationFee).div(10000).toNumber(),
78
182
  price: new Dec(reserveAsset.price).div(new Dec(10).pow(oracleDecimals)).toString(),
79
- totalSupplied: assetAmountInEth(reserveAsset.totalSupplied.toString(), symbol),
80
- totalDrawn: assetAmountInEth(reserveAsset.totalDrawn.toString(), symbol),
81
- totalPremium: assetAmountInEth(reserveAsset.totalPremium.toString(), symbol),
82
- totalDebt: assetAmountInEth(reserveAsset.totalDebt.toString(), symbol),
83
- supplyCap: assetAmountInEth(reserveAsset.supplyCap.toString(), symbol),
84
- borrowCap: assetAmountInEth(reserveAsset.borrowCap.toString(), symbol),
183
+ totalSupplied: assetAmountInEth(totalSuppliedRaw.toString(), symbol),
184
+ totalDrawn: assetAmountInEth(totalDrawnRaw.toString(), symbol),
185
+ totalPremium: assetAmountInEth(totalPremiumRaw.toString(), symbol),
186
+ totalDebt: assetAmountInEth(totalDebtRaw.toString(), symbol),
187
+ supplyCap: assetAmountInEth(supplyCapRaw.toString(), symbol),
188
+ borrowCap: assetAmountInEth(borrowCapRaw.toString(), symbol),
85
189
  spokeActive: reserveAsset.spokeActive,
86
190
  spokeHalted: reserveAsset.spokeHalted,
87
- drawnRate: new Dec(hubAsset.drawnRate).div(new Dec(10).pow(27)).toString(),
88
- supplyRate: '0', // To be implemented
191
+ drawnRate: drawnRate.toString(),
192
+ borrowRate: aprToApy(borrowApr.toString()),
193
+ supplyRate: aprToApy(supplyApr.toString()),
89
194
  supplyIncentives,
90
195
  borrowIncentives,
91
196
  canBeBorrowed: reserveAsset.spokeActive && !reserveAsset.spokeHalted && !reserveAsset.paused && !reserveAsset.frozen,
@@ -159,15 +264,20 @@ export async function _getAaveV4AccountData(provider: Client, network: NetworkNu
159
264
  return acc;
160
265
  }, {});
161
266
 
267
+ const aggregated = aaveV4GetAggregatedPositionData({
268
+ usedAssets,
269
+ assetsData: spokeData.assetsData,
270
+ network,
271
+ useUserCollateralFactor: true,
272
+ });
273
+
274
+ const riskPremiumBps = calcUserRiskPremiumBps(usedAssets, spokeData.assetsData);
275
+
162
276
  return {
277
+ ...aggregated,
163
278
  usedAssets,
164
279
  healthFactor,
165
- ...aaveV4GetAggregatedPositionData({
166
- usedAssets,
167
- assetsData: spokeData.assetsData,
168
- network,
169
- useUserCollateralFactor: true,
170
- }),
280
+ riskPremiumBps,
171
281
  };
172
282
  }
173
283