@defisaver/positions-sdk 2.1.42 → 2.1.43-aave-v4-dev-2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/aaveV4/index.d.ts +7 -0
- package/cjs/aaveV4/index.js +156 -0
- package/cjs/config/contracts.d.ts +1069 -0
- package/cjs/config/contracts.js +9 -0
- package/cjs/contracts.d.ts +18265 -0
- package/cjs/contracts.js +2 -1
- package/cjs/helpers/aaveV4Helpers/index.d.ts +7 -0
- package/cjs/helpers/aaveV4Helpers/index.js +54 -0
- package/cjs/helpers/index.d.ts +1 -0
- package/cjs/helpers/index.js +2 -1
- package/cjs/index.d.ts +2 -1
- package/cjs/index.js +3 -1
- package/cjs/markets/aaveV4/index.d.ts +6 -0
- package/cjs/markets/aaveV4/index.js +20 -0
- package/cjs/markets/index.d.ts +1 -0
- package/cjs/markets/index.js +3 -1
- package/cjs/portfolio/index.js +20 -0
- package/cjs/savings/morphoVaults/options.js +1 -1
- package/cjs/types/aaveV4.d.ts +120 -0
- package/cjs/types/aaveV4.js +11 -0
- package/cjs/types/index.d.ts +1 -0
- package/cjs/types/index.js +1 -0
- package/cjs/types/portfolio.d.ts +4 -0
- package/esm/aaveV4/index.d.ts +7 -0
- package/esm/aaveV4/index.js +147 -0
- package/esm/config/contracts.d.ts +1069 -0
- package/esm/config/contracts.js +8 -0
- package/esm/contracts.d.ts +18265 -0
- package/esm/contracts.js +1 -0
- package/esm/helpers/aaveV4Helpers/index.d.ts +7 -0
- package/esm/helpers/aaveV4Helpers/index.js +47 -0
- package/esm/helpers/index.d.ts +1 -0
- package/esm/helpers/index.js +1 -0
- package/esm/index.d.ts +2 -1
- package/esm/index.js +2 -1
- package/esm/markets/aaveV4/index.d.ts +6 -0
- package/esm/markets/aaveV4/index.js +15 -0
- package/esm/markets/index.d.ts +1 -0
- package/esm/markets/index.js +1 -0
- package/esm/portfolio/index.js +21 -1
- package/esm/savings/morphoVaults/options.js +1 -1
- package/esm/types/aaveV4.d.ts +120 -0
- package/esm/types/aaveV4.js +8 -0
- package/esm/types/index.d.ts +1 -0
- package/esm/types/index.js +1 -0
- package/esm/types/portfolio.d.ts +4 -0
- package/package.json +2 -2
- package/src/aaveV4/index.ts +159 -0
- package/src/config/contracts.ts +8 -0
- package/src/contracts.ts +3 -1
- package/src/helpers/aaveV4Helpers/index.ts +66 -0
- package/src/helpers/index.ts +1 -0
- package/src/index.ts +2 -0
- package/src/markets/aaveV4/index.ts +17 -0
- package/src/markets/index.ts +1 -0
- package/src/portfolio/index.ts +20 -0
- package/src/savings/morphoVaults/options.ts +1 -1
- package/src/types/aaveV4.ts +133 -0
- package/src/types/index.ts +2 -1
- package/src/types/portfolio.ts +4 -0
package/esm/contracts.js
CHANGED
|
@@ -132,3 +132,4 @@ export const StkAAVEViem = createViemContractFromConfigFunc('StkAAVE');
|
|
|
132
132
|
export const YearnViewContractViem = createViemContractFromConfigFunc('YearnView');
|
|
133
133
|
export const MakerDsrContractViem = createViemContractFromConfigFunc('MakerDsr');
|
|
134
134
|
export const SkySavingsContractView = createViemContractFromConfigFunc('SkySavings');
|
|
135
|
+
export const AaveV4ViewContractViem = createViemContractFromConfigFunc('AaveV4View');
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { AaveV4AggregatedPositionData, AaveV4AssetsData, AaveV4UsedReserveAssets } from '../../types';
|
|
2
|
+
import { NetworkNumber } from '../../types/common';
|
|
3
|
+
export declare const aaveV4GetAggregatedPositionData: ({ usedAssets, assetsData, network, }: {
|
|
4
|
+
usedAssets: AaveV4UsedReserveAssets;
|
|
5
|
+
assetsData: AaveV4AssetsData;
|
|
6
|
+
network: NetworkNumber;
|
|
7
|
+
}) => AaveV4AggregatedPositionData;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import Dec from 'decimal.js';
|
|
2
|
+
import { calcLeverageLiqPrice, getAssetsTotal, isLeveragedPos } from '../../moneymarket';
|
|
3
|
+
export const aaveV4GetAggregatedPositionData = ({ usedAssets, assetsData, network, }) => {
|
|
4
|
+
const payload = {};
|
|
5
|
+
payload.suppliedUsd = getAssetsTotal(usedAssets, ({ isSupplied }) => isSupplied, ({ suppliedUsd }) => suppliedUsd);
|
|
6
|
+
payload.suppliedCollateralUsd = getAssetsTotal(usedAssets, ({ isSupplied, collateral }) => isSupplied && collateral, ({ suppliedUsd }) => suppliedUsd);
|
|
7
|
+
payload.borrowLimitUsd = getAssetsTotal(usedAssets, ({ isSupplied, collateral }) => isSupplied && collateral, ({ symbol, suppliedUsd }) => new Dec(suppliedUsd).mul(assetsData[symbol].collateralFactor));
|
|
8
|
+
payload.liquidationLimitUsd = getAssetsTotal(usedAssets, ({ isSupplied, collateral }) => isSupplied && collateral,
|
|
9
|
+
// TODO: Verify if liquidation factor is available in Aave V4, currently using collateralFactor as placeholder
|
|
10
|
+
({ symbol, suppliedUsd }) => new Dec(suppliedUsd).mul(assetsData[symbol].collateralFactor));
|
|
11
|
+
payload.borrowedUsd = getAssetsTotal(usedAssets, ({ isBorrowed }) => isBorrowed, ({ borrowedUsd }) => borrowedUsd);
|
|
12
|
+
payload.drawnUsd = getAssetsTotal(usedAssets, ({ isBorrowed }) => isBorrowed, ({ drawnUsd }) => drawnUsd);
|
|
13
|
+
payload.premiumUsd = getAssetsTotal(usedAssets, ({ isBorrowed }) => isBorrowed, ({ premiumUsd }) => premiumUsd);
|
|
14
|
+
const leftToBorrowUsd = new Dec(payload.borrowLimitUsd).sub(payload.borrowedUsd);
|
|
15
|
+
payload.leftToBorrowUsd = leftToBorrowUsd.lte('0') ? '0' : leftToBorrowUsd.toString();
|
|
16
|
+
payload.ratio = +payload.suppliedUsd ? new Dec(payload.borrowLimitUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
|
|
17
|
+
payload.collRatio = +payload.suppliedUsd ? new Dec(payload.suppliedCollateralUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
|
|
18
|
+
payload.liqRatio = new Dec(payload.borrowLimitUsd).div(payload.liquidationLimitUsd).toString();
|
|
19
|
+
payload.liqPercent = new Dec(payload.borrowLimitUsd).div(payload.liquidationLimitUsd).mul(100).toString();
|
|
20
|
+
const { leveragedType, leveragedAsset } = isLeveragedPos(usedAssets);
|
|
21
|
+
payload.leveragedType = leveragedType;
|
|
22
|
+
payload.leveragedAsset = leveragedAsset;
|
|
23
|
+
payload.liquidationPrice = '';
|
|
24
|
+
if (leveragedType !== '') {
|
|
25
|
+
let assetPrice = assetsData[leveragedAsset].price;
|
|
26
|
+
if (leveragedType === 'lsd-leverage') {
|
|
27
|
+
// Treat ETH like a stablecoin in a long stETH position
|
|
28
|
+
payload.leveragedLsdAssetRatio = new Dec(assetsData[leveragedAsset].price).div(assetsData.ETH.price).toDP(18).toString();
|
|
29
|
+
assetPrice = new Dec(assetPrice).div(assetsData.ETH.price).toString();
|
|
30
|
+
}
|
|
31
|
+
payload.liquidationPrice = calcLeverageLiqPrice(leveragedType, assetPrice, payload.borrowedUsd, payload.liquidationLimitUsd);
|
|
32
|
+
}
|
|
33
|
+
payload.minCollRatio = new Dec(payload.suppliedCollateralUsd).div(payload.borrowLimitUsd).mul(100).toString();
|
|
34
|
+
payload.collLiquidationRatio = new Dec(payload.suppliedCollateralUsd).div(payload.liquidationLimitUsd).mul(100).toString();
|
|
35
|
+
// payload.healthRatio = new Dec(payload.liquidationLimitUsd).div(payload.borrowedUsd).toDP(4).toString();
|
|
36
|
+
payload.minHealthRatio = new Dec(payload.liquidationLimitUsd).div(payload.borrowLimitUsd).toDP(4).toString();
|
|
37
|
+
// TODO: Re-implement netApy calculation
|
|
38
|
+
// const { netApy, incentiveUsd, totalInterestUsd } = calculateNetApy({
|
|
39
|
+
// usedAssets,
|
|
40
|
+
// assetsData,
|
|
41
|
+
// optionalData: { healthRatio: payload.healthRatio },
|
|
42
|
+
// });
|
|
43
|
+
payload.netApy = '0';
|
|
44
|
+
payload.incentiveUsd = '0';
|
|
45
|
+
payload.totalInterestUsd = '0';
|
|
46
|
+
return payload;
|
|
47
|
+
};
|
package/esm/helpers/index.d.ts
CHANGED
package/esm/helpers/index.js
CHANGED
package/esm/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import './setup';
|
|
2
2
|
import * as fluid from './fluid';
|
|
3
|
+
import * as aaveV4 from './aaveV4';
|
|
3
4
|
import * as aaveV3 from './aaveV3';
|
|
4
5
|
import * as aaveV2 from './aaveV2';
|
|
5
6
|
import * as compoundV3 from './compoundV3';
|
|
@@ -21,4 +22,4 @@ import * as portfolio from './portfolio';
|
|
|
21
22
|
import * as claiming from './claiming';
|
|
22
23
|
import * as savings from './savings';
|
|
23
24
|
export * from './types';
|
|
24
|
-
export { aaveV2, aaveV3, compoundV2, compoundV3, spark, curveUsd, liquity, liquityV2, maker, exchange, staking, moneymarket, markets, helpers, morphoBlue, llamaLend, eulerV2, fluid, portfolio, claiming, savings, };
|
|
25
|
+
export { aaveV2, aaveV3, aaveV4, compoundV2, compoundV3, spark, curveUsd, liquity, liquityV2, maker, exchange, staking, moneymarket, markets, helpers, morphoBlue, llamaLend, eulerV2, fluid, portfolio, claiming, savings, };
|
package/esm/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import './setup';
|
|
2
2
|
import * as fluid from './fluid';
|
|
3
|
+
import * as aaveV4 from './aaveV4';
|
|
3
4
|
import * as aaveV3 from './aaveV3';
|
|
4
5
|
import * as aaveV2 from './aaveV2';
|
|
5
6
|
import * as compoundV3 from './compoundV3';
|
|
@@ -21,4 +22,4 @@ import * as portfolio from './portfolio';
|
|
|
21
22
|
import * as claiming from './claiming';
|
|
22
23
|
import * as savings from './savings';
|
|
23
24
|
export * from './types';
|
|
24
|
-
export { aaveV2, aaveV3, compoundV2, compoundV3, spark, curveUsd, liquity, liquityV2, maker, exchange, staking, moneymarket, markets, helpers, morphoBlue, llamaLend, eulerV2, fluid, portfolio, claiming, savings, };
|
|
25
|
+
export { aaveV2, aaveV3, aaveV4, compoundV2, compoundV3, spark, curveUsd, liquity, liquityV2, maker, exchange, staking, moneymarket, markets, helpers, morphoBlue, llamaLend, eulerV2, fluid, portfolio, claiming, savings, };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { AaveV4SpokeInfo } from '../../types';
|
|
2
|
+
import { NetworkNumber } from '../../types/common';
|
|
3
|
+
export declare const AAVE_V4_CORE_SPOKE: (networkId: NetworkNumber) => AaveV4SpokeInfo;
|
|
4
|
+
export declare const AaveV4Spokes: (networkId: NetworkNumber) => {
|
|
5
|
+
readonly aave_v4_core_spoke: AaveV4SpokeInfo;
|
|
6
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { AaveV4SpokesType } from '../../types';
|
|
2
|
+
import { NetworkNumber } from '../../types/common';
|
|
3
|
+
export const AAVE_V4_CORE_SPOKE = (networkId) => ({
|
|
4
|
+
chainIds: [NetworkNumber.Eth],
|
|
5
|
+
label: 'Core Spoke',
|
|
6
|
+
value: AaveV4SpokesType.AaveV4CoreSpoke,
|
|
7
|
+
url: 'core',
|
|
8
|
+
address: '0xBa97c5E52cd5BC3D7950Ae70779F8FfE92d40CdC',
|
|
9
|
+
hubs: [
|
|
10
|
+
'0xaD905aD5EA5B98cD50AE40Cfe368344686a21366',
|
|
11
|
+
],
|
|
12
|
+
});
|
|
13
|
+
export const AaveV4Spokes = (networkId) => ({
|
|
14
|
+
[AaveV4SpokesType.AaveV4CoreSpoke]: AAVE_V4_CORE_SPOKE(networkId),
|
|
15
|
+
});
|
package/esm/markets/index.d.ts
CHANGED
|
@@ -7,3 +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';
|
package/esm/markets/index.js
CHANGED
|
@@ -7,3 +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';
|
package/esm/portfolio/index.js
CHANGED
|
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
};
|
|
10
10
|
import Dec from 'decimal.js';
|
|
11
11
|
import { NetworkNumber } from '../types/common';
|
|
12
|
-
import { AaveMarkets, CompoundMarkets, CrvUsdMarkets, EulerV2Markets, LiquityV2Markets, LlamaLendMarkets, MorphoBlueMarkets, SparkMarkets, } from '../markets';
|
|
12
|
+
import { AaveMarkets, AaveV4Spokes, CompoundMarkets, CrvUsdMarkets, EulerV2Markets, LiquityV2Markets, LlamaLendMarkets, MorphoBlueMarkets, SparkMarkets, } from '../markets';
|
|
13
13
|
import { _getMorphoBlueAccountData, _getMorphoBlueMarketData, getMorphoEarn } from '../morphoBlue';
|
|
14
14
|
import { AaveVersions, CompoundVersions, } from '../types';
|
|
15
15
|
import { _getCompoundV3AccountData, _getCompoundV3MarketsData } from '../compoundV3';
|
|
@@ -34,6 +34,7 @@ import { fetchSparkAirdropRewards, fetchSparkRewards } from '../claiming/spark';
|
|
|
34
34
|
import { fetchMorphoBlueRewards } from '../claiming/morphoBlue';
|
|
35
35
|
import { getKingRewards } from '../claiming/king';
|
|
36
36
|
import { fetchEthenaAirdropRewards } from '../claiming/ethena';
|
|
37
|
+
import { _getAaveV4AccountData, _getAaveV4SpokeData } from '../aaveV4';
|
|
37
38
|
export function getPortfolioData(provider, network, defaultProvider, addresses, summerFiAddresses) {
|
|
38
39
|
return __awaiter(this, void 0, void 0, function* () {
|
|
39
40
|
const isMainnet = network === NetworkNumber.Eth;
|
|
@@ -50,6 +51,7 @@ export function getPortfolioData(provider, network, defaultProvider, addresses,
|
|
|
50
51
|
const llamaLendMarkets = [NetworkNumber.Eth, NetworkNumber.Arb].includes(network) ? Object.values(LlamaLendMarkets(network)).filter((market) => market.chainIds.includes(network)) : [];
|
|
51
52
|
const liquityV2Markets = [NetworkNumber.Eth].includes(network) ? Object.values(LiquityV2Markets(network)) : [];
|
|
52
53
|
const liquityV2MarketsStaking = [NetworkNumber.Eth].includes(network) ? Object.values(LiquityV2Markets(network)).filter(market => !market.isLegacy) : [];
|
|
54
|
+
const aaveV4Spokes = Object.values(AaveV4Spokes(network)).filter((market) => market.chainIds.includes(network));
|
|
53
55
|
const client = getViemProvider(provider, network, {
|
|
54
56
|
batch: {
|
|
55
57
|
multicall: {
|
|
@@ -75,6 +77,7 @@ export function getPortfolioData(provider, network, defaultProvider, addresses,
|
|
|
75
77
|
const crvUsdMarketsData = {};
|
|
76
78
|
const llamaLendMarketsData = {};
|
|
77
79
|
const liquityV2MarketsData = {};
|
|
80
|
+
const aaveV4SpokesData = {};
|
|
78
81
|
const markets = {
|
|
79
82
|
morphoMarketsData,
|
|
80
83
|
compoundV3MarketsData,
|
|
@@ -86,6 +89,7 @@ export function getPortfolioData(provider, network, defaultProvider, addresses,
|
|
|
86
89
|
crvUsdMarketsData,
|
|
87
90
|
llamaLendMarketsData,
|
|
88
91
|
liquityV2MarketsData,
|
|
92
|
+
aaveV4SpokesData,
|
|
89
93
|
};
|
|
90
94
|
const positions = {};
|
|
91
95
|
const stakingPositions = {};
|
|
@@ -94,6 +98,7 @@ export function getPortfolioData(provider, network, defaultProvider, addresses,
|
|
|
94
98
|
for (const address of allAddresses) {
|
|
95
99
|
positions[address.toLowerCase()] = {
|
|
96
100
|
aaveV3: {},
|
|
101
|
+
aaveV4: {},
|
|
97
102
|
morphoBlue: {},
|
|
98
103
|
compoundV3: {},
|
|
99
104
|
spark: {},
|
|
@@ -159,6 +164,10 @@ export function getPortfolioData(provider, network, defaultProvider, addresses,
|
|
|
159
164
|
const marketData = yield _getAaveV3MarketData(client, network, market);
|
|
160
165
|
aaveV3MarketsData[market.value] = marketData;
|
|
161
166
|
})),
|
|
167
|
+
...aaveV4Spokes.map((spoke) => __awaiter(this, void 0, void 0, function* () {
|
|
168
|
+
const spokeData = yield _getAaveV4SpokeData(client, network, spoke);
|
|
169
|
+
aaveV4SpokesData[spoke.value] = spokeData;
|
|
170
|
+
})),
|
|
162
171
|
...aaveV2Markets.map((market) => __awaiter(this, void 0, void 0, function* () {
|
|
163
172
|
const marketData = yield _getAaveV2MarketsData(client, network, market);
|
|
164
173
|
aaveV2MarketsData[market.value] = marketData;
|
|
@@ -428,6 +437,17 @@ export function getPortfolioData(provider, network, defaultProvider, addresses,
|
|
|
428
437
|
positions[address.toLowerCase()].aaveV3[market.value] = { error: `Error fetching AaveV3 account data for address ${address} on market ${market.value}`, data: null };
|
|
429
438
|
}
|
|
430
439
|
}))).flat(),
|
|
440
|
+
...aaveV4Spokes.map((spoke) => allAddresses.map((address) => __awaiter(this, void 0, void 0, function* () {
|
|
441
|
+
try {
|
|
442
|
+
const accData = yield _getAaveV4AccountData(client, network, aaveV4SpokesData[spoke.value], address);
|
|
443
|
+
if (new Dec(accData.suppliedUsd).gt(0))
|
|
444
|
+
positions[address.toLowerCase()].aaveV4[spoke.value] = { error: '', data: accData };
|
|
445
|
+
}
|
|
446
|
+
catch (error) {
|
|
447
|
+
console.error(`Error fetching AaveV4 account data for address ${address} on spoke ${spoke.value}:`, error);
|
|
448
|
+
positions[address.toLowerCase()].aaveV4[spoke.value] = { error: `Error fetching AaveV4 account data for address ${address} on spoke ${spoke.value}`, data: null };
|
|
449
|
+
}
|
|
450
|
+
}))).flat(),
|
|
431
451
|
...morphoMarkets.map((market) => addresses.map((address) => __awaiter(this, void 0, void 0, function* () {
|
|
432
452
|
try {
|
|
433
453
|
const [accDataPromise, earnDataPromise] = yield Promise.allSettled([
|
|
@@ -115,7 +115,7 @@ export const MORPHO_VAULT_STEAKHOUSE_ETH = {
|
|
|
115
115
|
export const MORPHO_VAULT_STEAKHOUSE_PYUSD = {
|
|
116
116
|
type: MorphoVaultType.MorphoVaultSteakhousePYUSD,
|
|
117
117
|
name: 'Steakhouse PYUSD',
|
|
118
|
-
address: '
|
|
118
|
+
address: '0xbEEF02e5E13584ab96848af90261f0C8Ee04722a',
|
|
119
119
|
asset: 'PYUSD',
|
|
120
120
|
deploymentBlock: 19043398,
|
|
121
121
|
isLegacy: false,
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { EthAddress, IncentiveData, NetworkNumber } from './common';
|
|
2
|
+
export declare enum AaveV4SpokesType {
|
|
3
|
+
AaveV4CoreSpoke = "aave_v4_core_spoke"
|
|
4
|
+
}
|
|
5
|
+
export declare enum AaveV4HubsType {
|
|
6
|
+
AaveV4CoreHub = "aave_v4_core_hub"
|
|
7
|
+
}
|
|
8
|
+
export interface AaveV4SpokeInfo {
|
|
9
|
+
chainIds: NetworkNumber[];
|
|
10
|
+
label: string;
|
|
11
|
+
value: AaveV4SpokesType;
|
|
12
|
+
url: string;
|
|
13
|
+
address: EthAddress;
|
|
14
|
+
hubs: EthAddress[];
|
|
15
|
+
}
|
|
16
|
+
export interface AaveV4HubAssetOnChainData {
|
|
17
|
+
assetId: number;
|
|
18
|
+
drawnRate: bigint;
|
|
19
|
+
}
|
|
20
|
+
export interface AaveV4HubOnChainData {
|
|
21
|
+
assets: Record<number, AaveV4HubAssetOnChainData>;
|
|
22
|
+
}
|
|
23
|
+
export interface AaveV4ReserveAssetOnChain {
|
|
24
|
+
underlying: EthAddress;
|
|
25
|
+
hub: EthAddress;
|
|
26
|
+
assetId: number;
|
|
27
|
+
decimals: number;
|
|
28
|
+
paused: boolean;
|
|
29
|
+
frozen: boolean;
|
|
30
|
+
borrowable: boolean;
|
|
31
|
+
collateralRisk: number;
|
|
32
|
+
collateralFactor: number;
|
|
33
|
+
maxLiquidationBonus: number;
|
|
34
|
+
liquidationFee: number;
|
|
35
|
+
price: bigint;
|
|
36
|
+
totalSupplied: bigint;
|
|
37
|
+
totalDrawn: bigint;
|
|
38
|
+
totalPremium: bigint;
|
|
39
|
+
totalDebt: bigint;
|
|
40
|
+
supplyCap: bigint;
|
|
41
|
+
borrowCap: bigint;
|
|
42
|
+
deficitRay: bigint;
|
|
43
|
+
spokeActive: boolean;
|
|
44
|
+
spokePaused: boolean;
|
|
45
|
+
}
|
|
46
|
+
export interface AaveV4ReserveAssetData {
|
|
47
|
+
symbol: string;
|
|
48
|
+
underlying: EthAddress;
|
|
49
|
+
hub: EthAddress;
|
|
50
|
+
assetId: number;
|
|
51
|
+
paused: boolean;
|
|
52
|
+
frozen: boolean;
|
|
53
|
+
borrowable: boolean;
|
|
54
|
+
collateralRisk: number;
|
|
55
|
+
collateralFactor: number;
|
|
56
|
+
liquidationFee: number;
|
|
57
|
+
price: string;
|
|
58
|
+
totalSupplied: string;
|
|
59
|
+
totalDrawn: string;
|
|
60
|
+
totalPremium: string;
|
|
61
|
+
totalDebt: string;
|
|
62
|
+
supplyCap: string;
|
|
63
|
+
borrowCap: string;
|
|
64
|
+
spokeActive: boolean;
|
|
65
|
+
spokePaused: boolean;
|
|
66
|
+
drawnRate: string;
|
|
67
|
+
supplyRate: string;
|
|
68
|
+
supplyIncentives: IncentiveData[];
|
|
69
|
+
borrowIncentives: IncentiveData[];
|
|
70
|
+
}
|
|
71
|
+
export type AaveV4AssetsData = Record<string, AaveV4ReserveAssetData>;
|
|
72
|
+
export interface AaveV4SpokeData {
|
|
73
|
+
assetsData: AaveV4AssetsData;
|
|
74
|
+
oracle: EthAddress;
|
|
75
|
+
oracleDecimals: number;
|
|
76
|
+
address: EthAddress;
|
|
77
|
+
}
|
|
78
|
+
export interface AaveV4UsedReserveAsset {
|
|
79
|
+
symbol: string;
|
|
80
|
+
supplied: string;
|
|
81
|
+
suppliedUsd: string;
|
|
82
|
+
drawn: string;
|
|
83
|
+
drawnUsd: string;
|
|
84
|
+
premium: string;
|
|
85
|
+
premiumUsd: string;
|
|
86
|
+
borrowed: string;
|
|
87
|
+
borrowedUsd: string;
|
|
88
|
+
isSupplied: boolean;
|
|
89
|
+
isBorrowed: boolean;
|
|
90
|
+
collateral: boolean;
|
|
91
|
+
}
|
|
92
|
+
export interface AaveV4AggregatedPositionData {
|
|
93
|
+
suppliedUsd: string;
|
|
94
|
+
suppliedCollateralUsd: string;
|
|
95
|
+
borrowLimitUsd: string;
|
|
96
|
+
liquidationLimitUsd: string;
|
|
97
|
+
borrowedUsd: string;
|
|
98
|
+
drawnUsd: string;
|
|
99
|
+
premiumUsd: string;
|
|
100
|
+
leftToBorrowUsd: string;
|
|
101
|
+
ratio: string;
|
|
102
|
+
collRatio: string;
|
|
103
|
+
liqRatio: string;
|
|
104
|
+
liqPercent: string;
|
|
105
|
+
leveragedType: string;
|
|
106
|
+
leveragedAsset: string;
|
|
107
|
+
liquidationPrice: string;
|
|
108
|
+
leveragedLsdAssetRatio?: string;
|
|
109
|
+
minCollRatio: string;
|
|
110
|
+
collLiquidationRatio: string;
|
|
111
|
+
minHealthRatio: string;
|
|
112
|
+
netApy: string;
|
|
113
|
+
incentiveUsd: string;
|
|
114
|
+
totalInterestUsd: string;
|
|
115
|
+
}
|
|
116
|
+
export type AaveV4UsedReserveAssets = Record<string, AaveV4UsedReserveAsset>;
|
|
117
|
+
export interface AaveV4AccountData extends AaveV4AggregatedPositionData {
|
|
118
|
+
usedAssets: AaveV4UsedReserveAssets;
|
|
119
|
+
healthFactor: string;
|
|
120
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export var AaveV4SpokesType;
|
|
2
|
+
(function (AaveV4SpokesType) {
|
|
3
|
+
AaveV4SpokesType["AaveV4CoreSpoke"] = "aave_v4_core_spoke";
|
|
4
|
+
})(AaveV4SpokesType || (AaveV4SpokesType = {}));
|
|
5
|
+
export var AaveV4HubsType;
|
|
6
|
+
(function (AaveV4HubsType) {
|
|
7
|
+
AaveV4HubsType["AaveV4CoreHub"] = "aave_v4_core_hub";
|
|
8
|
+
})(AaveV4HubsType || (AaveV4HubsType = {}));
|
package/esm/types/index.d.ts
CHANGED
package/esm/types/index.js
CHANGED
package/esm/types/portfolio.d.ts
CHANGED
|
@@ -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;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@defisaver/positions-sdk",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.43-aave-v4-dev-2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./cjs/index.js",
|
|
6
6
|
"module": "./esm/index.js",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"dev": "tsc -p tsconfig.json --watch",
|
|
13
13
|
"lint": "eslint src/ --fix",
|
|
14
14
|
"lint-check": "eslint src/",
|
|
15
|
-
"test": "mocha tests
|
|
15
|
+
"test": "mocha tests/portfolio.ts",
|
|
16
16
|
"test-single": "mocha ./tests/$npm_config_name.ts",
|
|
17
17
|
"test:debugger": "mocha --inspect-brk tests/*",
|
|
18
18
|
"version-bump": "git commit -am \"Version bump to $(npm version patch | cut -c 2-)\""
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { Client } from 'viem';
|
|
2
|
+
import Dec from 'decimal.js';
|
|
3
|
+
import { assetAmountInEth, getAssetInfoByAddress } from '@defisaver/tokens';
|
|
4
|
+
import { getViemProvider } from '../services/viem';
|
|
5
|
+
import {
|
|
6
|
+
AaveV4AccountData,
|
|
7
|
+
AaveV4HubAssetOnChainData,
|
|
8
|
+
AaveV4HubOnChainData,
|
|
9
|
+
AaveV4ReserveAssetData, AaveV4ReserveAssetOnChain, AaveV4SpokeData, AaveV4SpokeInfo,
|
|
10
|
+
AaveV4UsedReserveAssets,
|
|
11
|
+
} from '../types';
|
|
12
|
+
import {
|
|
13
|
+
EthAddress, EthereumProvider, IncentiveData, IncentiveKind, NetworkNumber,
|
|
14
|
+
} from '../types/common';
|
|
15
|
+
import { AaveV4ViewContractViem } from '../contracts';
|
|
16
|
+
import { getStakingApy, STAKING_ASSETS } from '../staking';
|
|
17
|
+
import { wethToEth } from '../services/utils';
|
|
18
|
+
import { aaveV4GetAggregatedPositionData } from '../helpers/aaveV4Helpers';
|
|
19
|
+
|
|
20
|
+
const fetchHubData = async (viewContract: ReturnType<typeof AaveV4ViewContractViem>, hubAddress: EthAddress): Promise<AaveV4HubOnChainData> => {
|
|
21
|
+
const hubData = await viewContract.read.getHubAllAssetsData([hubAddress]);
|
|
22
|
+
return {
|
|
23
|
+
assets: hubData.reduce((acc: Record<number, AaveV4HubAssetOnChainData>, assetOnChainData) => {
|
|
24
|
+
acc[assetOnChainData.assetId] = {
|
|
25
|
+
assetId: assetOnChainData.assetId,
|
|
26
|
+
drawnRate: assetOnChainData.drawnRate,
|
|
27
|
+
};
|
|
28
|
+
return acc;
|
|
29
|
+
}, {}),
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const formatReserveAsset = async (reserveAsset: AaveV4ReserveAssetOnChain, hubAsset: AaveV4HubAssetOnChainData, oracleDecimals: number, network: NetworkNumber): Promise<AaveV4ReserveAssetData> => {
|
|
34
|
+
const assetInfo = getAssetInfoByAddress(reserveAsset.underlying, network);
|
|
35
|
+
const symbol = wethToEth(assetInfo.symbol);
|
|
36
|
+
|
|
37
|
+
const isStakingAsset = STAKING_ASSETS.includes(symbol);
|
|
38
|
+
const supplyIncentives: IncentiveData[] = [];
|
|
39
|
+
const borrowIncentives: IncentiveData[] = [];
|
|
40
|
+
|
|
41
|
+
if (isStakingAsset) {
|
|
42
|
+
const yieldApy = await getStakingApy(symbol, network as NetworkNumber);
|
|
43
|
+
supplyIncentives.push({
|
|
44
|
+
apy: yieldApy,
|
|
45
|
+
token: symbol,
|
|
46
|
+
incentiveKind: IncentiveKind.Staking,
|
|
47
|
+
description: `Native ${symbol} yield.`,
|
|
48
|
+
});
|
|
49
|
+
if (reserveAsset.borrowable) {
|
|
50
|
+
// when borrowing assets whose value increases over time
|
|
51
|
+
borrowIncentives.push({
|
|
52
|
+
apy: new Dec(yieldApy).mul(-1).toString(),
|
|
53
|
+
token: symbol,
|
|
54
|
+
incentiveKind: IncentiveKind.Reward,
|
|
55
|
+
description: `Due to the native yield of ${symbol}, the value of the debt would increase over time.`,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return ({
|
|
61
|
+
symbol,
|
|
62
|
+
underlying: reserveAsset.underlying,
|
|
63
|
+
hub: reserveAsset.hub,
|
|
64
|
+
assetId: reserveAsset.assetId,
|
|
65
|
+
paused: reserveAsset.paused,
|
|
66
|
+
frozen: reserveAsset.frozen,
|
|
67
|
+
borrowable: reserveAsset.borrowable,
|
|
68
|
+
collateralRisk: new Dec(reserveAsset.collateralRisk).div(10000).toNumber(),
|
|
69
|
+
collateralFactor: new Dec(reserveAsset.collateralFactor).div(10000).toNumber(),
|
|
70
|
+
liquidationFee: new Dec(reserveAsset.liquidationFee).div(10000).toNumber(),
|
|
71
|
+
price: new Dec(reserveAsset.price).div(new Dec(10).pow(oracleDecimals)).toString(),
|
|
72
|
+
totalSupplied: assetAmountInEth(reserveAsset.totalSupplied.toString(), symbol),
|
|
73
|
+
totalDrawn: assetAmountInEth(reserveAsset.totalDrawn.toString(), symbol),
|
|
74
|
+
totalPremium: assetAmountInEth(reserveAsset.totalPremium.toString(), symbol),
|
|
75
|
+
totalDebt: assetAmountInEth(reserveAsset.totalDebt.toString(), symbol),
|
|
76
|
+
supplyCap: assetAmountInEth(reserveAsset.supplyCap.toString(), symbol),
|
|
77
|
+
borrowCap: assetAmountInEth(reserveAsset.borrowCap.toString(), symbol),
|
|
78
|
+
spokeActive: reserveAsset.spokeActive,
|
|
79
|
+
spokePaused: reserveAsset.spokePaused,
|
|
80
|
+
drawnRate: new Dec(hubAsset.drawnRate).div(new Dec(10).pow(27)).toString(),
|
|
81
|
+
supplyRate: '0', // To be implemented
|
|
82
|
+
supplyIncentives,
|
|
83
|
+
borrowIncentives,
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
export async function _getAaveV4SpokeData(provider: Client, network: NetworkNumber, market: AaveV4SpokeInfo, blockNumber: 'latest' | number = 'latest'): Promise<AaveV4SpokeData> {
|
|
88
|
+
const viewContract = AaveV4ViewContractViem(provider, network, blockNumber);
|
|
89
|
+
|
|
90
|
+
const hubsData: Record<EthAddress, AaveV4HubOnChainData> = {};
|
|
91
|
+
const [spokeData] = await Promise.all([
|
|
92
|
+
viewContract.read.getSpokeDataFull([market.address]),
|
|
93
|
+
...market.hubs.map(async (hubAddress) => {
|
|
94
|
+
hubsData[hubAddress] = await fetchHubData(viewContract, hubAddress);
|
|
95
|
+
}),
|
|
96
|
+
]);
|
|
97
|
+
|
|
98
|
+
const reserveAssetsArray = await Promise.all(spokeData[1].map(async (reserveAssetOnChain: AaveV4ReserveAssetOnChain) => formatReserveAsset(reserveAssetOnChain, hubsData[reserveAssetOnChain.hub].assets[reserveAssetOnChain.assetId], +spokeData[0].oracleDecimals.toString(), network)));
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
assetsData: reserveAssetsArray.reduce((acc: Record<string, AaveV4ReserveAssetData>, reserveAsset: AaveV4ReserveAssetData) => {
|
|
102
|
+
acc[reserveAsset.symbol] = reserveAsset;
|
|
103
|
+
return acc;
|
|
104
|
+
}, {}),
|
|
105
|
+
oracle: spokeData[0].oracle,
|
|
106
|
+
oracleDecimals: +spokeData[0].oracleDecimals.toString(),
|
|
107
|
+
address: market.address,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export async function getAaveV4SpokeData(provider: EthereumProvider, network: NetworkNumber, spoke: AaveV4SpokeInfo, blockNumber: 'latest' | number = 'latest'): Promise<AaveV4SpokeData> {
|
|
112
|
+
return _getAaveV4SpokeData(getViemProvider(provider, network), network, spoke, blockNumber);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export async function _getAaveV4AccountData(provider: Client, network: NetworkNumber, spokeData: AaveV4SpokeData, address: EthAddress, blockNumber: 'latest' | number = 'latest'): Promise<AaveV4AccountData> {
|
|
116
|
+
const viewContract = AaveV4ViewContractViem(provider, network, blockNumber);
|
|
117
|
+
|
|
118
|
+
const loanData = await viewContract.read.getLoanData([spokeData.address, address]);
|
|
119
|
+
|
|
120
|
+
const healthFactor = new Dec(loanData.healthFactor).div(1e18).toString();
|
|
121
|
+
|
|
122
|
+
const usedAssets = loanData.reserves.reduce((acc: AaveV4UsedReserveAssets, usedReserveAsset) => {
|
|
123
|
+
const reserveData = spokeData.assetsData[wethToEth(getAssetInfoByAddress(usedReserveAsset.underlying, network).symbol)];
|
|
124
|
+
const price = reserveData.price;
|
|
125
|
+
const supplied = assetAmountInEth(usedReserveAsset.supplied.toString(), reserveData.symbol);
|
|
126
|
+
const drawn = assetAmountInEth(usedReserveAsset.drawn.toString(), reserveData.symbol);
|
|
127
|
+
const premium = assetAmountInEth(usedReserveAsset.premium.toString(), reserveData.symbol);
|
|
128
|
+
const borrowed = assetAmountInEth(usedReserveAsset.totalDebt.toString(), reserveData.symbol);
|
|
129
|
+
acc[reserveData.symbol] = {
|
|
130
|
+
symbol: reserveData.symbol,
|
|
131
|
+
supplied,
|
|
132
|
+
suppliedUsd: new Dec(supplied).mul(price).toString(),
|
|
133
|
+
drawn,
|
|
134
|
+
drawnUsd: new Dec(drawn).mul(price).toString(),
|
|
135
|
+
premium,
|
|
136
|
+
premiumUsd: new Dec(premium).mul(price).toString(),
|
|
137
|
+
borrowed,
|
|
138
|
+
borrowedUsd: new Dec(borrowed).mul(price).toString(),
|
|
139
|
+
isSupplied: !new Dec(supplied).eq(0),
|
|
140
|
+
isBorrowed: usedReserveAsset.isBorrowing,
|
|
141
|
+
collateral: usedReserveAsset.isUsingAsCollateral,
|
|
142
|
+
};
|
|
143
|
+
return acc;
|
|
144
|
+
}, {});
|
|
145
|
+
|
|
146
|
+
return {
|
|
147
|
+
usedAssets,
|
|
148
|
+
healthFactor,
|
|
149
|
+
...aaveV4GetAggregatedPositionData({
|
|
150
|
+
usedAssets,
|
|
151
|
+
assetsData: spokeData.assetsData,
|
|
152
|
+
network,
|
|
153
|
+
}),
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export async function getAaveV4AccountData(provider: EthereumProvider, network: NetworkNumber, marketData: AaveV4SpokeData, address: EthAddress, blockNumber: 'latest' | number = 'latest'): Promise<any> {
|
|
158
|
+
return _getAaveV4AccountData(getViemProvider(provider, network), network, marketData, address, blockNumber);
|
|
159
|
+
}
|