@defisaver/positions-sdk 2.1.50 → 2.1.51-aave-v4-dev
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/aaveV4/index.d.ts +7 -0
- package/cjs/aaveV4/index.js +167 -0
- package/cjs/config/contracts.d.ts +1277 -0
- package/cjs/config/contracts.js +9 -0
- package/cjs/contracts.d.ts +23120 -0
- package/cjs/contracts.js +2 -1
- package/cjs/helpers/aaveV4Helpers/index.d.ts +13 -0
- package/cjs/helpers/aaveV4Helpers/index.js +109 -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 +7 -0
- package/cjs/markets/aaveV4/index.js +22 -0
- package/cjs/markets/index.d.ts +1 -0
- package/cjs/markets/index.js +3 -1
- package/cjs/markets/morphoBlue/index.d.ts +2 -0
- package/cjs/markets/morphoBlue/index.js +19 -2
- package/cjs/portfolio/index.js +20 -0
- package/cjs/types/aaveV4.d.ts +129 -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/morphoBlue.d.ts +1 -0
- package/cjs/types/morphoBlue.js +1 -0
- package/cjs/types/portfolio.d.ts +4 -0
- package/esm/aaveV4/index.d.ts +7 -0
- package/esm/aaveV4/index.js +158 -0
- package/esm/config/contracts.d.ts +1277 -0
- package/esm/config/contracts.js +8 -0
- package/esm/contracts.d.ts +23120 -0
- package/esm/contracts.js +1 -0
- package/esm/helpers/aaveV4Helpers/index.d.ts +13 -0
- package/esm/helpers/aaveV4Helpers/index.js +100 -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 +7 -0
- package/esm/markets/aaveV4/index.js +16 -0
- package/esm/markets/index.d.ts +1 -0
- package/esm/markets/index.js +1 -0
- package/esm/markets/morphoBlue/index.d.ts +2 -0
- package/esm/markets/morphoBlue/index.js +16 -0
- package/esm/portfolio/index.js +21 -1
- package/esm/types/aaveV4.d.ts +129 -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/morphoBlue.d.ts +1 -0
- package/esm/types/morphoBlue.js +1 -0
- package/esm/types/portfolio.d.ts +4 -0
- package/package.json +1 -1
- package/src/aaveV4/index.ts +169 -0
- package/src/config/contracts.ts +8 -0
- package/src/contracts.ts +3 -1
- package/src/helpers/aaveV4Helpers/index.ts +121 -0
- package/src/helpers/index.ts +1 -0
- package/src/index.ts +2 -0
- package/src/markets/aaveV4/index.ts +19 -0
- package/src/markets/index.ts +1 -0
- package/src/markets/morphoBlue/index.ts +17 -0
- package/src/portfolio/index.ts +20 -0
- package/src/types/aaveV4.ts +142 -0
- package/src/types/index.ts +2 -1
- package/src/types/morphoBlue.ts +2 -0
- package/src/types/portfolio.ts +4 -0
|
@@ -0,0 +1,169 @@
|
|
|
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, reserveId: number, 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
|
+
reserveId,
|
|
66
|
+
paused: reserveAsset.paused,
|
|
67
|
+
frozen: reserveAsset.frozen,
|
|
68
|
+
borrowable: reserveAsset.borrowable,
|
|
69
|
+
collateralRisk: new Dec(reserveAsset.collateralRisk).div(10000).toNumber(),
|
|
70
|
+
collateralFactor: new Dec(reserveAsset.collateralFactor).div(10000).toNumber(),
|
|
71
|
+
liquidationFee: new Dec(reserveAsset.liquidationFee).div(10000).toNumber(),
|
|
72
|
+
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),
|
|
79
|
+
spokeActive: reserveAsset.spokeActive,
|
|
80
|
+
spokePaused: reserveAsset.spokePaused,
|
|
81
|
+
drawnRate: new Dec(hubAsset.drawnRate).div(new Dec(10).pow(27)).toString(),
|
|
82
|
+
supplyRate: '0', // To be implemented
|
|
83
|
+
supplyIncentives,
|
|
84
|
+
borrowIncentives,
|
|
85
|
+
canBeBorrowed: reserveAsset.spokeActive && !reserveAsset.spokePaused && !reserveAsset.paused && !reserveAsset.frozen,
|
|
86
|
+
canBeSupplied: reserveAsset.spokeActive && !reserveAsset.spokePaused && !reserveAsset.paused && !reserveAsset.frozen,
|
|
87
|
+
canBeWithdrawn: reserveAsset.spokeActive && !reserveAsset.spokePaused && !reserveAsset.paused,
|
|
88
|
+
canBePayBacked: reserveAsset.spokeActive && !reserveAsset.spokePaused && !reserveAsset.paused,
|
|
89
|
+
utilization: new Dec(reserveAsset.totalDrawn.toString()).times(100).div(new Dec(reserveAsset.totalSupplied.toString())).toString(),
|
|
90
|
+
});
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export async function _getAaveV4SpokeData(provider: Client, network: NetworkNumber, market: AaveV4SpokeInfo, blockNumber: 'latest' | number = 'latest'): Promise<AaveV4SpokeData> {
|
|
94
|
+
const viewContract = AaveV4ViewContractViem(provider, network, blockNumber);
|
|
95
|
+
|
|
96
|
+
const hubsData: Record<EthAddress, AaveV4HubOnChainData> = {};
|
|
97
|
+
const [spokeData] = await Promise.all([
|
|
98
|
+
viewContract.read.getSpokeDataFull([market.address]),
|
|
99
|
+
...market.hubs.map(async (hubAddress) => {
|
|
100
|
+
hubsData[hubAddress] = await fetchHubData(viewContract, hubAddress);
|
|
101
|
+
}),
|
|
102
|
+
]);
|
|
103
|
+
|
|
104
|
+
const reserveAssetsArray = await Promise.all(spokeData[1].map(async (reserveAssetOnChain: AaveV4ReserveAssetOnChain, index: number) => formatReserveAsset(reserveAssetOnChain, hubsData[reserveAssetOnChain.hub].assets[reserveAssetOnChain.assetId], index, +spokeData[0].oracleDecimals.toString(), network)));
|
|
105
|
+
|
|
106
|
+
return {
|
|
107
|
+
assetsData: reserveAssetsArray.reduce((acc: Record<string, AaveV4ReserveAssetData>, reserveAsset: AaveV4ReserveAssetData) => {
|
|
108
|
+
acc[`${reserveAsset.symbol}-${reserveAsset.reserveId}`] = reserveAsset;
|
|
109
|
+
return acc;
|
|
110
|
+
}, {}),
|
|
111
|
+
oracle: spokeData[0].oracle,
|
|
112
|
+
oracleDecimals: +spokeData[0].oracleDecimals.toString(),
|
|
113
|
+
address: market.address,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export async function getAaveV4SpokeData(provider: EthereumProvider, network: NetworkNumber, spoke: AaveV4SpokeInfo, blockNumber: 'latest' | number = 'latest'): Promise<AaveV4SpokeData> {
|
|
118
|
+
return _getAaveV4SpokeData(getViemProvider(provider, network), network, spoke, blockNumber);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export async function _getAaveV4AccountData(provider: Client, network: NetworkNumber, spokeData: AaveV4SpokeData, address: EthAddress, blockNumber: 'latest' | number = 'latest'): Promise<AaveV4AccountData> {
|
|
122
|
+
const viewContract = AaveV4ViewContractViem(provider, network, blockNumber);
|
|
123
|
+
|
|
124
|
+
const loanData = await viewContract.read.getLoanData([spokeData.address, address]);
|
|
125
|
+
|
|
126
|
+
const healthFactor = new Dec(loanData.healthFactor).div(1e18).toString();
|
|
127
|
+
const usedAssets = loanData.reserves.reduce((acc: AaveV4UsedReserveAssets, usedReserveAsset) => {
|
|
128
|
+
const identifier = `${wethToEth(getAssetInfoByAddress(usedReserveAsset.underlying, network).symbol)}-${+usedReserveAsset.reserveId.toString()}`;
|
|
129
|
+
const reserveData = spokeData.assetsData[identifier];
|
|
130
|
+
const price = reserveData.price;
|
|
131
|
+
const supplied = assetAmountInEth(usedReserveAsset.supplied.toString(), reserveData.symbol);
|
|
132
|
+
const drawn = assetAmountInEth(usedReserveAsset.drawn.toString(), reserveData.symbol);
|
|
133
|
+
const premium = assetAmountInEth(usedReserveAsset.premium.toString(), reserveData.symbol);
|
|
134
|
+
const borrowed = assetAmountInEth(usedReserveAsset.totalDebt.toString(), reserveData.symbol);
|
|
135
|
+
acc[identifier] = {
|
|
136
|
+
symbol: reserveData.symbol,
|
|
137
|
+
assetId: reserveData.assetId,
|
|
138
|
+
reserveId: +usedReserveAsset.reserveId.toString(),
|
|
139
|
+
supplied,
|
|
140
|
+
suppliedUsd: new Dec(supplied).mul(price).toString(),
|
|
141
|
+
drawn,
|
|
142
|
+
drawnUsd: new Dec(drawn).mul(price).toString(),
|
|
143
|
+
premium,
|
|
144
|
+
premiumUsd: new Dec(premium).mul(price).toString(),
|
|
145
|
+
borrowed,
|
|
146
|
+
borrowedUsd: new Dec(borrowed).mul(price).toString(),
|
|
147
|
+
isSupplied: !new Dec(supplied).eq(0),
|
|
148
|
+
isBorrowed: usedReserveAsset.isBorrowing,
|
|
149
|
+
collateral: usedReserveAsset.isUsingAsCollateral,
|
|
150
|
+
collateralFactor: new Dec(usedReserveAsset.collateralFactor).div(10000).toNumber(),
|
|
151
|
+
};
|
|
152
|
+
return acc;
|
|
153
|
+
}, {});
|
|
154
|
+
|
|
155
|
+
return {
|
|
156
|
+
usedAssets,
|
|
157
|
+
healthFactor,
|
|
158
|
+
...aaveV4GetAggregatedPositionData({
|
|
159
|
+
usedAssets,
|
|
160
|
+
assetsData: spokeData.assetsData,
|
|
161
|
+
network,
|
|
162
|
+
useUserCollateralFactor: true,
|
|
163
|
+
}),
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export async function getAaveV4AccountData(provider: EthereumProvider, network: NetworkNumber, marketData: AaveV4SpokeData, address: EthAddress, blockNumber: 'latest' | number = 'latest'): Promise<any> {
|
|
168
|
+
return _getAaveV4AccountData(getViemProvider(provider, network), network, marketData, address, blockNumber);
|
|
169
|
+
}
|
package/src/config/contracts.ts
CHANGED
|
@@ -1318,4 +1318,12 @@ export const SkySavings = {
|
|
|
1318
1318
|
} as const;
|
|
1319
1319
|
export const YearnV3Vault = {
|
|
1320
1320
|
"abi": [{"stateMutability":"view","type":"function","name":"balanceOf","inputs":[{"name":"addr","type":"address"}],"outputs":[{"name":"","type":"uint256"}]},{"stateMutability":"view","type":"function","name":"totalAssets","inputs":[],"outputs":[{"name":"","type":"uint256"}]},{"stateMutability":"view","type":"function","name":"totalDebt","inputs":[],"outputs":[{"name":"","type":"uint256"}]},{"stateMutability":"view","type":"function","name":"totalSupply","inputs":[],"outputs":[{"name":"","type":"uint256"}]}]
|
|
1321
|
+
} as const;
|
|
1322
|
+
export const AaveV4View = {
|
|
1323
|
+
"abi": [{"inputs":[{"internalType":"address","name":"_spoke","type":"address"},{"internalType":"address","name":"_user","type":"address"}],"name":"getHealthFactor","outputs":[{"internalType":"uint256","name":"healthFactor","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_hub","type":"address"}],"name":"getHubAllAssetsData","outputs":[{"components":[{"internalType":"uint16","name":"assetId","type":"uint16"},{"internalType":"uint8","name":"decimals","type":"uint8"},{"internalType":"address","name":"underlying","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"totalSupplied","type":"uint256"},{"internalType":"uint256","name":"totalDrawn","type":"uint256"},{"internalType":"uint256","name":"totalPremium","type":"uint256"},{"internalType":"uint256","name":"totalDebt","type":"uint256"},{"internalType":"uint256","name":"swept","type":"uint256"},{"internalType":"uint16","name":"liquidityFee","type":"uint16"},{"internalType":"uint120","name":"drawnIndex","type":"uint120"},{"internalType":"uint96","name":"drawnRate","type":"uint96"},{"internalType":"uint40","name":"lastUpdateTimestamp","type":"uint40"},{"internalType":"address","name":"irStrategy","type":"address"},{"internalType":"address","name":"reinvestmentController","type":"address"},{"internalType":"address","name":"feeReceiver","type":"address"},{"internalType":"uint256","name":"deficitRay","type":"uint256"}],"internalType":"structAaveV4View.HubAssetData[]","name":"hubAssetData","type":"tuple[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_hub","type":"address"},{"internalType":"uint256","name":"_assetId","type":"uint256"}],"name":"getHubAssetData","outputs":[{"components":[{"internalType":"uint16","name":"assetId","type":"uint16"},{"internalType":"uint8","name":"decimals","type":"uint8"},{"internalType":"address","name":"underlying","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"totalSupplied","type":"uint256"},{"internalType":"uint256","name":"totalDrawn","type":"uint256"},{"internalType":"uint256","name":"totalPremium","type":"uint256"},{"internalType":"uint256","name":"totalDebt","type":"uint256"},{"internalType":"uint256","name":"swept","type":"uint256"},{"internalType":"uint16","name":"liquidityFee","type":"uint16"},{"internalType":"uint120","name":"drawnIndex","type":"uint120"},{"internalType":"uint96","name":"drawnRate","type":"uint96"},{"internalType":"uint40","name":"lastUpdateTimestamp","type":"uint40"},{"internalType":"address","name":"irStrategy","type":"address"},{"internalType":"address","name":"reinvestmentController","type":"address"},{"internalType":"address","name":"feeReceiver","type":"address"},{"internalType":"uint256","name":"deficitRay","type":"uint256"}],"internalType":"structAaveV4View.HubAssetData","name":"hubAssetData","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_spoke","type":"address"},{"internalType":"address","name":"_user","type":"address"}],"name":"getLoanData","outputs":[{"components":[{"internalType":"address","name":"user","type":"address"},{"internalType":"uint256","name":"riskPremium","type":"uint256"},{"internalType":"uint256","name":"avgCollateralFactor","type":"uint256"},{"internalType":"uint256","name":"healthFactor","type":"uint256"},{"internalType":"uint256","name":"totalCollateralInUsd","type":"uint256"},{"internalType":"uint256","name":"totalDebtInUsd","type":"uint256"},{"internalType":"uint256","name":"activeCollateralCount","type":"uint256"},{"internalType":"uint256","name":"borrowedCount","type":"uint256"},{"components":[{"internalType":"uint256","name":"reserveId","type":"uint256"},{"internalType":"uint16","name":"assetId","type":"uint16"},{"internalType":"address","name":"underlying","type":"address"},{"internalType":"uint256","name":"supplied","type":"uint256"},{"internalType":"uint256","name":"drawn","type":"uint256"},{"internalType":"uint256","name":"premium","type":"uint256"},{"internalType":"uint256","name":"totalDebt","type":"uint256"},{"internalType":"uint16","name":"collateralFactor","type":"uint16"},{"internalType":"uint32","name":"maxLiquidationBonus","type":"uint32"},{"internalType":"uint16","name":"liquidationFee","type":"uint16"},{"internalType":"bool","name":"isUsingAsCollateral","type":"bool"},{"internalType":"bool","name":"isBorrowing","type":"bool"}],"internalType":"structAaveV4View.UserReserveData[]","name":"reserves","type":"tuple[]"}],"internalType":"structAaveV4View.LoanData","name":"loanData","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_user","type":"address"},{"internalType":"address[]","name":"_spokes","type":"address[]"}],"name":"getLoanDataForMultipleSpokes","outputs":[{"components":[{"internalType":"address","name":"user","type":"address"},{"internalType":"uint256","name":"riskPremium","type":"uint256"},{"internalType":"uint256","name":"avgCollateralFactor","type":"uint256"},{"internalType":"uint256","name":"healthFactor","type":"uint256"},{"internalType":"uint256","name":"totalCollateralInUsd","type":"uint256"},{"internalType":"uint256","name":"totalDebtInUsd","type":"uint256"},{"internalType":"uint256","name":"activeCollateralCount","type":"uint256"},{"internalType":"uint256","name":"borrowedCount","type":"uint256"},{"components":[{"internalType":"uint256","name":"reserveId","type":"uint256"},{"internalType":"uint16","name":"assetId","type":"uint16"},{"internalType":"address","name":"underlying","type":"address"},{"internalType":"uint256","name":"supplied","type":"uint256"},{"internalType":"uint256","name":"drawn","type":"uint256"},{"internalType":"uint256","name":"premium","type":"uint256"},{"internalType":"uint256","name":"totalDebt","type":"uint256"},{"internalType":"uint16","name":"collateralFactor","type":"uint16"},{"internalType":"uint32","name":"maxLiquidationBonus","type":"uint32"},{"internalType":"uint16","name":"liquidationFee","type":"uint16"},{"internalType":"bool","name":"isUsingAsCollateral","type":"bool"},{"internalType":"bool","name":"isBorrowing","type":"bool"}],"internalType":"structAaveV4View.UserReserveData[]","name":"reserves","type":"tuple[]"}],"internalType":"structAaveV4View.LoanData[]","name":"loans","type":"tuple[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_spoke","type":"address"},{"internalType":"address[]","name":"_users","type":"address[]"}],"name":"getLoanDataForMultipleUsers","outputs":[{"components":[{"internalType":"address","name":"user","type":"address"},{"internalType":"uint256","name":"riskPremium","type":"uint256"},{"internalType":"uint256","name":"avgCollateralFactor","type":"uint256"},{"internalType":"uint256","name":"healthFactor","type":"uint256"},{"internalType":"uint256","name":"totalCollateralInUsd","type":"uint256"},{"internalType":"uint256","name":"totalDebtInUsd","type":"uint256"},{"internalType":"uint256","name":"activeCollateralCount","type":"uint256"},{"internalType":"uint256","name":"borrowedCount","type":"uint256"},{"components":[{"internalType":"uint256","name":"reserveId","type":"uint256"},{"internalType":"uint16","name":"assetId","type":"uint16"},{"internalType":"address","name":"underlying","type":"address"},{"internalType":"uint256","name":"supplied","type":"uint256"},{"internalType":"uint256","name":"drawn","type":"uint256"},{"internalType":"uint256","name":"premium","type":"uint256"},{"internalType":"uint256","name":"totalDebt","type":"uint256"},{"internalType":"uint16","name":"collateralFactor","type":"uint16"},{"internalType":"uint32","name":"maxLiquidationBonus","type":"uint32"},{"internalType":"uint16","name":"liquidationFee","type":"uint16"},{"internalType":"bool","name":"isUsingAsCollateral","type":"bool"},{"internalType":"bool","name":"isBorrowing","type":"bool"}],"internalType":"structAaveV4View.UserReserveData[]","name":"reserves","type":"tuple[]"}],"internalType":"structAaveV4View.LoanData[]","name":"loans","type":"tuple[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_spoke","type":"address"},{"internalType":"address","name":"_user","type":"address"}],"name":"getLoanDataFull","outputs":[{"components":[{"internalType":"address","name":"user","type":"address"},{"internalType":"uint256","name":"riskPremium","type":"uint256"},{"internalType":"uint256","name":"avgCollateralFactor","type":"uint256"},{"internalType":"uint256","name":"healthFactor","type":"uint256"},{"internalType":"uint256","name":"totalCollateralInUsd","type":"uint256"},{"internalType":"uint256","name":"totalDebtInUsd","type":"uint256"},{"internalType":"uint256","name":"activeCollateralCount","type":"uint256"},{"internalType":"uint256","name":"borrowedCount","type":"uint256"},{"components":[{"internalType":"uint256","name":"reserveId","type":"uint256"},{"internalType":"address","name":"underlying","type":"address"},{"internalType":"uint256","name":"price","type":"uint256"},{"internalType":"uint8","name":"decimals","type":"uint8"},{"internalType":"bool","name":"isUsingAsCollateral","type":"bool"},{"internalType":"bool","name":"isBorrowing","type":"bool"},{"internalType":"bool","name":"reservePaused","type":"bool"},{"internalType":"bool","name":"reserveFrozen","type":"bool"},{"internalType":"bool","name":"borrowable","type":"bool"},{"internalType":"bool","name":"spokeActive","type":"bool"},{"internalType":"bool","name":"spokePaused","type":"bool"},{"internalType":"uint256","name":"userSupplied","type":"uint256"},{"internalType":"uint256","name":"userDrawn","type":"uint256"},{"internalType":"uint256","name":"userPremium","type":"uint256"},{"internalType":"uint256","name":"userTotalDebt","type":"uint256"},{"internalType":"uint24","name":"collateralRisk","type":"uint24"},{"internalType":"uint16","name":"userCollateralFactor","type":"uint16"},{"internalType":"uint32","name":"userMaxLiquidationBonus","type":"uint32"},{"internalType":"uint16","name":"userLiquidationFee","type":"uint16"},{"internalType":"uint16","name":"latestCollateralFactor","type":"uint16"},{"internalType":"uint32","name":"latestMaxLiquidationBonus","type":"uint32"},{"internalType":"uint16","name":"latestLiquidationFee","type":"uint16"},{"internalType":"address","name":"hub","type":"address"},{"internalType":"uint16","name":"hubAssetId","type":"uint16"},{"internalType":"uint256","name":"hubLiquidity","type":"uint256"},{"internalType":"uint96","name":"drawnRate","type":"uint96"},{"internalType":"uint120","name":"drawnIndex","type":"uint120"},{"internalType":"uint256","name":"spokeTotalSupplied","type":"uint256"},{"internalType":"uint256","name":"spokeTotalDrawn","type":"uint256"},{"internalType":"uint256","name":"spokeTotalPremium","type":"uint256"},{"internalType":"uint256","name":"spokeTotalDebt","type":"uint256"},{"internalType":"uint256","name":"spokeSupplyCap","type":"uint256"},{"internalType":"uint256","name":"spokeBorrowCap","type":"uint256"},{"internalType":"uint256","name":"spokeDeficitRay","type":"uint256"}],"internalType":"structAaveV4View.UserReserveDataFull[]","name":"reserves","type":"tuple[]"}],"internalType":"structAaveV4View.LoanDataWithFullReserves","name":"loanData","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_spoke","type":"address"},{"internalType":"uint256","name":"_reserveId","type":"uint256"}],"name":"getReserveData","outputs":[{"components":[{"internalType":"address","name":"underlying","type":"address"},{"internalType":"uint16","name":"collateralFactor","type":"uint16"},{"internalType":"uint256","name":"price","type":"uint256"}],"internalType":"structAaveV4View.ReserveData","name":"reserveData","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_spoke","type":"address"},{"internalType":"uint256","name":"_reserveId","type":"uint256"}],"name":"getReserveDataFull","outputs":[{"components":[{"internalType":"address","name":"underlying","type":"address"},{"internalType":"address","name":"hub","type":"address"},{"internalType":"uint16","name":"assetId","type":"uint16"},{"internalType":"uint8","name":"decimals","type":"uint8"},{"internalType":"bool","name":"paused","type":"bool"},{"internalType":"bool","name":"frozen","type":"bool"},{"internalType":"bool","name":"borrowable","type":"bool"},{"internalType":"uint24","name":"collateralRisk","type":"uint24"},{"internalType":"uint16","name":"collateralFactor","type":"uint16"},{"internalType":"uint32","name":"maxLiquidationBonus","type":"uint32"},{"internalType":"uint16","name":"liquidationFee","type":"uint16"},{"internalType":"uint256","name":"price","type":"uint256"},{"internalType":"uint256","name":"totalSupplied","type":"uint256"},{"internalType":"uint256","name":"totalDrawn","type":"uint256"},{"internalType":"uint256","name":"totalPremium","type":"uint256"},{"internalType":"uint256","name":"totalDebt","type":"uint256"},{"internalType":"uint256","name":"supplyCap","type":"uint256"},{"internalType":"uint256","name":"borrowCap","type":"uint256"},{"internalType":"uint256","name":"deficitRay","type":"uint256"},{"internalType":"bool","name":"spokeActive","type":"bool"},{"internalType":"bool","name":"spokePaused","type":"bool"}],"internalType":"structAaveV4View.ReserveDataFull","name":"reserveData","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_spoke","type":"address"},{"internalType":"uint256","name":"_reserveId","type":"uint256"}],"name":"getReservePrice","outputs":[{"internalType":"uint256","name":"price","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_spoke","type":"address"},{"internalType":"uint256[]","name":"_reserveIds","type":"uint256[]"}],"name":"getReservePrices","outputs":[{"internalType":"uint256[]","name":"prices","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_spoke","type":"address"},{"internalType":"uint256[]","name":"_reserveIds","type":"uint256[]"}],"name":"getReservesData","outputs":[{"components":[{"internalType":"address","name":"underlying","type":"address"},{"internalType":"uint16","name":"collateralFactor","type":"uint16"},{"internalType":"uint256","name":"price","type":"uint256"}],"internalType":"structAaveV4View.ReserveData[]","name":"reserveData","type":"tuple[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_spoke","type":"address"},{"internalType":"uint256[]","name":"_reserveIds","type":"uint256[]"}],"name":"getReservesDataFull","outputs":[{"components":[{"internalType":"address","name":"underlying","type":"address"},{"internalType":"address","name":"hub","type":"address"},{"internalType":"uint16","name":"assetId","type":"uint16"},{"internalType":"uint8","name":"decimals","type":"uint8"},{"internalType":"bool","name":"paused","type":"bool"},{"internalType":"bool","name":"frozen","type":"bool"},{"internalType":"bool","name":"borrowable","type":"bool"},{"internalType":"uint24","name":"collateralRisk","type":"uint24"},{"internalType":"uint16","name":"collateralFactor","type":"uint16"},{"internalType":"uint32","name":"maxLiquidationBonus","type":"uint32"},{"internalType":"uint16","name":"liquidationFee","type":"uint16"},{"internalType":"uint256","name":"price","type":"uint256"},{"internalType":"uint256","name":"totalSupplied","type":"uint256"},{"internalType":"uint256","name":"totalDrawn","type":"uint256"},{"internalType":"uint256","name":"totalPremium","type":"uint256"},{"internalType":"uint256","name":"totalDebt","type":"uint256"},{"internalType":"uint256","name":"supplyCap","type":"uint256"},{"internalType":"uint256","name":"borrowCap","type":"uint256"},{"internalType":"uint256","name":"deficitRay","type":"uint256"},{"internalType":"bool","name":"spokeActive","type":"bool"},{"internalType":"bool","name":"spokePaused","type":"bool"}],"internalType":"structAaveV4View.ReserveDataFull[]","name":"reserveData","type":"tuple[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_spoke","type":"address"}],"name":"getSpokeData","outputs":[{"components":[{"internalType":"uint128","name":"targetHealthFactor","type":"uint128"},{"internalType":"uint64","name":"healthFactorForMaxBonus","type":"uint64"},{"internalType":"uint16","name":"liquidationBonusFactor","type":"uint16"},{"internalType":"address","name":"oracle","type":"address"},{"internalType":"uint256","name":"oracleDecimals","type":"uint256"},{"internalType":"uint256","name":"reserveCount","type":"uint256"}],"internalType":"structAaveV4View.SpokeData","name":"spokeData","type":"tuple"},{"components":[{"internalType":"address","name":"underlying","type":"address"},{"internalType":"uint16","name":"collateralFactor","type":"uint16"},{"internalType":"uint256","name":"price","type":"uint256"}],"internalType":"structAaveV4View.ReserveData[]","name":"reserves","type":"tuple[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_spoke","type":"address"}],"name":"getSpokeDataFull","outputs":[{"components":[{"internalType":"uint128","name":"targetHealthFactor","type":"uint128"},{"internalType":"uint64","name":"healthFactorForMaxBonus","type":"uint64"},{"internalType":"uint16","name":"liquidationBonusFactor","type":"uint16"},{"internalType":"address","name":"oracle","type":"address"},{"internalType":"uint256","name":"oracleDecimals","type":"uint256"},{"internalType":"uint256","name":"reserveCount","type":"uint256"}],"internalType":"structAaveV4View.SpokeData","name":"spokeData","type":"tuple"},{"components":[{"internalType":"address","name":"underlying","type":"address"},{"internalType":"address","name":"hub","type":"address"},{"internalType":"uint16","name":"assetId","type":"uint16"},{"internalType":"uint8","name":"decimals","type":"uint8"},{"internalType":"bool","name":"paused","type":"bool"},{"internalType":"bool","name":"frozen","type":"bool"},{"internalType":"bool","name":"borrowable","type":"bool"},{"internalType":"uint24","name":"collateralRisk","type":"uint24"},{"internalType":"uint16","name":"collateralFactor","type":"uint16"},{"internalType":"uint32","name":"maxLiquidationBonus","type":"uint32"},{"internalType":"uint16","name":"liquidationFee","type":"uint16"},{"internalType":"uint256","name":"price","type":"uint256"},{"internalType":"uint256","name":"totalSupplied","type":"uint256"},{"internalType":"uint256","name":"totalDrawn","type":"uint256"},{"internalType":"uint256","name":"totalPremium","type":"uint256"},{"internalType":"uint256","name":"totalDebt","type":"uint256"},{"internalType":"uint256","name":"supplyCap","type":"uint256"},{"internalType":"uint256","name":"borrowCap","type":"uint256"},{"internalType":"uint256","name":"deficitRay","type":"uint256"},{"internalType":"bool","name":"spokeActive","type":"bool"},{"internalType":"bool","name":"spokePaused","type":"bool"}],"internalType":"structAaveV4View.ReserveDataFull[]","name":"reserves","type":"tuple[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_hub","type":"address"},{"internalType":"uint256","name":"_assetId","type":"uint256"}],"name":"getSpokesForAsset","outputs":[{"internalType":"address[]","name":"spokes","type":"address[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_spoke","type":"address"},{"internalType":"address","name":"_user","type":"address"},{"internalType":"uint256[]","name":"_reserveIds","type":"uint256[]"}],"name":"getUserReserveData","outputs":[{"components":[{"internalType":"uint256","name":"reserveId","type":"uint256"},{"internalType":"uint16","name":"assetId","type":"uint16"},{"internalType":"address","name":"underlying","type":"address"},{"internalType":"uint256","name":"supplied","type":"uint256"},{"internalType":"uint256","name":"drawn","type":"uint256"},{"internalType":"uint256","name":"premium","type":"uint256"},{"internalType":"uint256","name":"totalDebt","type":"uint256"},{"internalType":"uint16","name":"collateralFactor","type":"uint16"},{"internalType":"uint32","name":"maxLiquidationBonus","type":"uint32"},{"internalType":"uint16","name":"liquidationFee","type":"uint16"},{"internalType":"bool","name":"isUsingAsCollateral","type":"bool"},{"internalType":"bool","name":"isBorrowing","type":"bool"}],"internalType":"structAaveV4View.UserReserveData[]","name":"_userReserves","type":"tuple[]"}],"stateMutability":"view","type":"function"}],
|
|
1324
|
+
"networks": {
|
|
1325
|
+
"1": {
|
|
1326
|
+
"address": "0xF0D440dE7f82A1e598992A648E5Bfa5d69b8E3a9",
|
|
1327
|
+
}
|
|
1328
|
+
}
|
|
1321
1329
|
} as const;
|
package/src/contracts.ts
CHANGED
|
@@ -169,4 +169,6 @@ export const YearnViewContractViem = createViemContractFromConfigFunc('YearnView
|
|
|
169
169
|
|
|
170
170
|
export const MakerDsrContractViem = createViemContractFromConfigFunc('MakerDsr');
|
|
171
171
|
|
|
172
|
-
export const SkySavingsContractView = createViemContractFromConfigFunc('SkySavings');
|
|
172
|
+
export const SkySavingsContractView = createViemContractFromConfigFunc('SkySavings');
|
|
173
|
+
|
|
174
|
+
export const AaveV4ViewContractViem = createViemContractFromConfigFunc('AaveV4View');
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import Dec from 'decimal.js';
|
|
2
|
+
import { calcLeverageLiqPrice, getAssetsTotal, STABLE_ASSETS } from '../../moneymarket';
|
|
3
|
+
import {
|
|
4
|
+
AaveV4AggregatedPositionData, AaveV4AssetsData, AaveV4ReserveAssetData, AaveV4UsedReserveAsset, AaveV4UsedReserveAssets,
|
|
5
|
+
} from '../../types';
|
|
6
|
+
import { NetworkNumber } from '../../types/common';
|
|
7
|
+
|
|
8
|
+
export const aaveV4GetCollateralFactor = (assetData: AaveV4ReserveAssetData, usedAssetData: AaveV4UsedReserveAsset, useUserCollateralFactor: boolean = false): number => (useUserCollateralFactor ? usedAssetData.collateralFactor : assetData.collateralFactor);
|
|
9
|
+
|
|
10
|
+
export const isLeveragedPosAaveV4 = (usedAssets: AaveV4UsedReserveAssets, dustLimit = 5) => {
|
|
11
|
+
let borrowUnstable = 0;
|
|
12
|
+
let supplyStable = 0;
|
|
13
|
+
let borrowStable = 0;
|
|
14
|
+
let supplyUnstable = 0;
|
|
15
|
+
let longAsset = '';
|
|
16
|
+
let shortAsset = '';
|
|
17
|
+
Object.values(usedAssets).forEach(({
|
|
18
|
+
symbol, suppliedUsd, borrowedUsd, collateral, reserveId,
|
|
19
|
+
}) => {
|
|
20
|
+
const spokeAsset = `${symbol}-${reserveId}`;
|
|
21
|
+
const isSupplied = (+suppliedUsd) > dustLimit; // ignore dust like <limit leftover supply
|
|
22
|
+
const isBorrowed = (+borrowedUsd) > dustLimit; // ignore dust like <limit leftover supply
|
|
23
|
+
if (isSupplied && STABLE_ASSETS.includes(symbol) && collateral) supplyStable += 1;
|
|
24
|
+
if (isBorrowed && STABLE_ASSETS.includes(symbol)) borrowStable += 1;
|
|
25
|
+
if (isBorrowed && !STABLE_ASSETS.includes(symbol)) {
|
|
26
|
+
borrowUnstable += 1;
|
|
27
|
+
shortAsset = spokeAsset;
|
|
28
|
+
}
|
|
29
|
+
if (isSupplied && !STABLE_ASSETS.includes(symbol) && collateral) {
|
|
30
|
+
supplyUnstable += 1;
|
|
31
|
+
longAsset = spokeAsset;
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
const isLong = borrowStable > 0 && borrowUnstable === 0 && supplyUnstable === 1 && supplyStable === 0;
|
|
35
|
+
const isShort = supplyStable > 0 && supplyUnstable === 0 && borrowUnstable === 1 && borrowStable === 0;
|
|
36
|
+
// lsd -> liquid staking derivative
|
|
37
|
+
const isLsdLeveraged = supplyUnstable === 1 && borrowUnstable === 1 && shortAsset === 'ETH' && ['stETH', 'wstETH', 'cbETH', 'rETH', 'ezETH', 'weETH'].includes(longAsset);
|
|
38
|
+
if (isLong) {
|
|
39
|
+
return {
|
|
40
|
+
leveragedType: 'long',
|
|
41
|
+
leveragedAsset: longAsset,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
if (isShort) {
|
|
45
|
+
return {
|
|
46
|
+
leveragedType: 'short',
|
|
47
|
+
leveragedAsset: shortAsset,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
if (isLsdLeveraged) {
|
|
51
|
+
return {
|
|
52
|
+
leveragedType: 'lsd-leverage',
|
|
53
|
+
leveragedAsset: longAsset,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
leveragedType: '',
|
|
58
|
+
leveragedAsset: '',
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export const aaveV4GetAggregatedPositionData = ({
|
|
63
|
+
usedAssets,
|
|
64
|
+
assetsData,
|
|
65
|
+
network,
|
|
66
|
+
useUserCollateralFactor = false,
|
|
67
|
+
}: {
|
|
68
|
+
usedAssets: AaveV4UsedReserveAssets,
|
|
69
|
+
assetsData: AaveV4AssetsData,
|
|
70
|
+
network: NetworkNumber,
|
|
71
|
+
useUserCollateralFactor?: boolean,
|
|
72
|
+
}): AaveV4AggregatedPositionData => {
|
|
73
|
+
const payload = {} as AaveV4AggregatedPositionData;
|
|
74
|
+
payload.suppliedUsd = getAssetsTotal(usedAssets, ({ isSupplied }: { isSupplied: boolean }) => isSupplied, ({ suppliedUsd }: { suppliedUsd: string }) => suppliedUsd);
|
|
75
|
+
payload.suppliedCollateralUsd = getAssetsTotal(usedAssets, ({ isSupplied, collateral }: { isSupplied: boolean, collateral: string }) => isSupplied && collateral, ({ suppliedUsd }: { suppliedUsd: string }) => suppliedUsd);
|
|
76
|
+
payload.borrowLimitUsd = getAssetsTotal(
|
|
77
|
+
usedAssets,
|
|
78
|
+
({ isSupplied, collateral }: { isSupplied: boolean, collateral: string }) => isSupplied && collateral,
|
|
79
|
+
({ symbol, suppliedUsd, reserveId }: { symbol: string, suppliedUsd: string, reserveId: number }) => new Dec(suppliedUsd).mul(aaveV4GetCollateralFactor(assetsData[`${symbol}-${reserveId}`], usedAssets[`${symbol}-${reserveId}`], useUserCollateralFactor)),
|
|
80
|
+
);
|
|
81
|
+
payload.liquidationLimitUsd = payload.borrowLimitUsd;
|
|
82
|
+
payload.borrowedUsd = getAssetsTotal(usedAssets, ({ isBorrowed }: { isBorrowed: boolean }) => isBorrowed, ({ borrowedUsd }: { borrowedUsd: string }) => borrowedUsd);
|
|
83
|
+
payload.drawnUsd = getAssetsTotal(usedAssets, ({ isBorrowed }: { isBorrowed: boolean }) => isBorrowed, ({ drawnUsd }: { drawnUsd: string }) => drawnUsd);
|
|
84
|
+
payload.premiumUsd = getAssetsTotal(usedAssets, ({ isBorrowed }: { isBorrowed: boolean }) => isBorrowed, ({ premiumUsd }: { premiumUsd: string }) => premiumUsd);
|
|
85
|
+
const leftToBorrowUsd = new Dec(payload.borrowLimitUsd).sub(payload.borrowedUsd);
|
|
86
|
+
payload.leftToBorrowUsd = leftToBorrowUsd.lte('0') ? '0' : leftToBorrowUsd.toString();
|
|
87
|
+
payload.ratio = +payload.suppliedUsd ? new Dec(payload.borrowLimitUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
|
|
88
|
+
payload.collRatio = +payload.suppliedUsd ? new Dec(payload.suppliedCollateralUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
|
|
89
|
+
payload.liqRatio = new Dec(payload.borrowLimitUsd).div(payload.liquidationLimitUsd).toString();
|
|
90
|
+
payload.liqPercent = new Dec(payload.borrowLimitUsd).div(payload.liquidationLimitUsd).mul(100).toString();
|
|
91
|
+
const { leveragedType, leveragedAsset } = isLeveragedPosAaveV4(usedAssets);
|
|
92
|
+
payload.leveragedType = leveragedType;
|
|
93
|
+
payload.leveragedAsset = leveragedAsset;
|
|
94
|
+
payload.liquidationPrice = '';
|
|
95
|
+
if (leveragedType !== '') {
|
|
96
|
+
const leveragedAssetData = Object.values(assetsData).find((asset) => asset.symbol === leveragedAsset);
|
|
97
|
+
let assetPrice = leveragedAssetData?.price || '0';
|
|
98
|
+
if (leveragedType === 'lsd-leverage') {
|
|
99
|
+
// Treat ETH like a stablecoin in a long stETH position
|
|
100
|
+
const ethPrice = Object.values(assetsData).find((asset) => asset.symbol === 'ETH')?.price || '0';
|
|
101
|
+
payload.leveragedLsdAssetRatio = new Dec(leveragedAssetData?.price || '0').div(ethPrice).toDP(18).toString();
|
|
102
|
+
assetPrice = new Dec(assetPrice).div(ethPrice).toString();
|
|
103
|
+
}
|
|
104
|
+
payload.liquidationPrice = calcLeverageLiqPrice(leveragedType, assetPrice, payload.borrowedUsd, payload.liquidationLimitUsd);
|
|
105
|
+
}
|
|
106
|
+
payload.minCollRatio = new Dec(payload.suppliedCollateralUsd).div(payload.borrowLimitUsd).mul(100).toString();
|
|
107
|
+
payload.collLiquidationRatio = new Dec(payload.suppliedCollateralUsd).div(payload.liquidationLimitUsd).mul(100).toString();
|
|
108
|
+
// payload.healthRatio = new Dec(payload.liquidationLimitUsd).div(payload.borrowedUsd).toDP(4).toString();
|
|
109
|
+
payload.minHealthRatio = new Dec(payload.liquidationLimitUsd).div(payload.borrowLimitUsd).toDP(4).toString();
|
|
110
|
+
|
|
111
|
+
// TODO: Re-implement netApy calculation
|
|
112
|
+
// const { netApy, incentiveUsd, totalInterestUsd } = calculateNetApy({
|
|
113
|
+
// usedAssets,
|
|
114
|
+
// assetsData,
|
|
115
|
+
// optionalData: { healthRatio: payload.healthRatio },
|
|
116
|
+
// });
|
|
117
|
+
payload.netApy = '0';
|
|
118
|
+
payload.incentiveUsd = '0';
|
|
119
|
+
payload.totalInterestUsd = '0';
|
|
120
|
+
return payload;
|
|
121
|
+
};
|
package/src/helpers/index.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import './setup';
|
|
2
2
|
|
|
3
3
|
import * as fluid from './fluid';
|
|
4
|
+
import * as aaveV4 from './aaveV4';
|
|
4
5
|
import * as aaveV3 from './aaveV3';
|
|
5
6
|
import * as aaveV2 from './aaveV2';
|
|
6
7
|
import * as compoundV3 from './compoundV3';
|
|
@@ -27,6 +28,7 @@ export * from './types';
|
|
|
27
28
|
export {
|
|
28
29
|
aaveV2,
|
|
29
30
|
aaveV3,
|
|
31
|
+
aaveV4,
|
|
30
32
|
compoundV2,
|
|
31
33
|
compoundV3,
|
|
32
34
|
spark,
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { AaveV4SpokeInfo, AaveV4SpokesType } from '../../types';
|
|
2
|
+
import { NetworkNumber } from '../../types/common';
|
|
3
|
+
|
|
4
|
+
export const AAVE_V4_CORE_SPOKE = (networkId: NetworkNumber): AaveV4SpokeInfo => ({
|
|
5
|
+
chainIds: [NetworkNumber.Eth],
|
|
6
|
+
label: 'Core Spoke',
|
|
7
|
+
value: AaveV4SpokesType.AaveV4CoreSpoke,
|
|
8
|
+
url: 'core',
|
|
9
|
+
address: '0xBa97c5E52cd5BC3D7950Ae70779F8FfE92d40CdC',
|
|
10
|
+
hubs: [
|
|
11
|
+
'0xaD905aD5EA5B98cD50AE40Cfe368344686a21366',
|
|
12
|
+
],
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
export const AaveV4Spokes = (networkId: NetworkNumber) => ({
|
|
16
|
+
[AaveV4SpokesType.AaveV4CoreSpoke]: AAVE_V4_CORE_SPOKE(networkId),
|
|
17
|
+
}) as const;
|
|
18
|
+
|
|
19
|
+
export const getAaveV4SpokeTypeInfo = (type: AaveV4SpokesType, network?: NetworkNumber) => ({ ...AaveV4Spokes(network ?? NetworkNumber.Eth) }[type]);
|
package/src/markets/index.ts
CHANGED
|
@@ -511,6 +511,22 @@ export const MORPHO_BLUE_SUSDS_USDT_965 = (networkId: NetworkNumber = NetworkNum
|
|
|
511
511
|
protocolName: 'morpho-blue',
|
|
512
512
|
});
|
|
513
513
|
|
|
514
|
+
export const MORPHO_BLUE_MORPHO_USDC_625 = (networkId: NetworkNumber = NetworkNumber.Eth): MorphoBlueMarketData => ({
|
|
515
|
+
chainIds: [1],
|
|
516
|
+
label: 'Morpho',
|
|
517
|
+
shortLabel: 'MORPHO/USDC',
|
|
518
|
+
value: MorphoBlueVersions.MorphoBlueMORPHOUSDC_625,
|
|
519
|
+
url: 'morphousdc-6d95bf5f',
|
|
520
|
+
loanToken: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
|
|
521
|
+
collateralToken: '0x58D97B57BB95320F9a05dC918Aef65434969c2B2',
|
|
522
|
+
oracle: '0xEEcD66e6C723c1506532a5f646cC8a502c026A2E',
|
|
523
|
+
oracleType: MorphoBlueOracleType.MARKET_RATE,
|
|
524
|
+
irm: '0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC',
|
|
525
|
+
lltv: 0.625,
|
|
526
|
+
marketId: '0x6d95bf5fad1b0427205ee2b595f80b52e22394173de0832efa79fde88abb8525',
|
|
527
|
+
protocolName: 'morpho-blue',
|
|
528
|
+
});
|
|
529
|
+
|
|
514
530
|
// ###### BASE ########
|
|
515
531
|
|
|
516
532
|
export const MORPHO_BLUE_CBETH_USDC_860_BASE = (networkId: NetworkNumber = NetworkNumber.Eth): MorphoBlueMarketData => ({
|
|
@@ -913,6 +929,7 @@ export const MorphoBlueMarkets = (networkId: NetworkNumber) => ({
|
|
|
913
929
|
[MorphoBlueVersions.MorphoBlueLBTCUSDC_860]: MORPHO_BLUE_LBTC_USDC_860(networkId),
|
|
914
930
|
[MorphoBlueVersions.MorphoBlueLBTCCbBTC_945]: MORPHO_BLUE_LBTC_CBBTC_945(networkId),
|
|
915
931
|
[MorphoBlueVersions.MorphoBluesUSDSUSDT_965]: MORPHO_BLUE_SUSDS_USDT_965(networkId),
|
|
932
|
+
[MorphoBlueVersions.MorphoBlueMORPHOUSDC_625]: MORPHO_BLUE_MORPHO_USDC_625(networkId),
|
|
916
933
|
|
|
917
934
|
// wstETH/WETH
|
|
918
935
|
[MorphoBlueVersions.MorphoBlueWstEthEth_945]: MORPHO_BLUE_WSTETH_ETH_945(networkId),
|
package/src/portfolio/index.ts
CHANGED
|
@@ -2,6 +2,7 @@ import Dec from 'decimal.js';
|
|
|
2
2
|
import { EthAddress, EthereumProvider, NetworkNumber } from '../types/common';
|
|
3
3
|
import {
|
|
4
4
|
AaveMarkets,
|
|
5
|
+
AaveV4Spokes,
|
|
5
6
|
CompoundMarkets,
|
|
6
7
|
CrvUsdMarkets,
|
|
7
8
|
EulerV2Markets,
|
|
@@ -14,6 +15,7 @@ import { _getMorphoBlueAccountData, _getMorphoBlueMarketData, getMorphoEarn } fr
|
|
|
14
15
|
import {
|
|
15
16
|
AaveV2MarketData,
|
|
16
17
|
AaveV3MarketData,
|
|
18
|
+
AaveV4SpokeData,
|
|
17
19
|
AaveVersions,
|
|
18
20
|
CdpInfo,
|
|
19
21
|
CompoundV2MarketsData,
|
|
@@ -49,6 +51,7 @@ import { fetchSparkAirdropRewards, fetchSparkRewards } from '../claiming/spark';
|
|
|
49
51
|
import { fetchMorphoBlueRewards } from '../claiming/morphoBlue';
|
|
50
52
|
import { getKingRewards } from '../claiming/king';
|
|
51
53
|
import { fetchEthenaAirdropRewards } from '../claiming/ethena';
|
|
54
|
+
import { _getAaveV4AccountData, _getAaveV4SpokeData } from '../aaveV4';
|
|
52
55
|
|
|
53
56
|
export async function getPortfolioData(provider: EthereumProvider, network: NetworkNumber, defaultProvider: EthereumProvider, addresses: EthAddress[], isSim = false): Promise<{
|
|
54
57
|
positions: PortfolioPositionsData;
|
|
@@ -71,6 +74,7 @@ export async function getPortfolioData(provider: EthereumProvider, network: Netw
|
|
|
71
74
|
const llamaLendMarkets = [NetworkNumber.Eth, NetworkNumber.Arb].includes(network) ? Object.values(LlamaLendMarkets(network)).filter((market) => market.chainIds.includes(network)) : [];
|
|
72
75
|
const liquityV2Markets = [NetworkNumber.Eth].includes(network) ? Object.values(LiquityV2Markets(network)) : [];
|
|
73
76
|
const liquityV2MarketsStaking = [NetworkNumber.Eth].includes(network) ? Object.values(LiquityV2Markets(network)).filter(market => !market.isLegacy) : [];
|
|
77
|
+
const aaveV4Spokes = Object.values(AaveV4Spokes(network)).filter((market) => market.chainIds.includes(network));
|
|
74
78
|
|
|
75
79
|
const args: [NetworkNumber, any?] = [network, { batch: { multicall: { batchSize: isSim ? 500_000 : 2_500_000 } } }];
|
|
76
80
|
const client = getViemProvider(provider, ...args);
|
|
@@ -87,6 +91,7 @@ export async function getPortfolioData(provider: EthereumProvider, network: Netw
|
|
|
87
91
|
const crvUsdMarketsData: Record<string, CrvUSDGlobalMarketData> = {};
|
|
88
92
|
const llamaLendMarketsData: Record<string, LlamaLendGlobalMarketData> = {};
|
|
89
93
|
const liquityV2MarketsData: Record<string, LiquityV2MarketData> = {};
|
|
94
|
+
const aaveV4SpokesData: Record<string, AaveV4SpokeData> = {};
|
|
90
95
|
|
|
91
96
|
const markets = {
|
|
92
97
|
morphoMarketsData,
|
|
@@ -99,6 +104,7 @@ export async function getPortfolioData(provider: EthereumProvider, network: Netw
|
|
|
99
104
|
crvUsdMarketsData,
|
|
100
105
|
llamaLendMarketsData,
|
|
101
106
|
liquityV2MarketsData,
|
|
107
|
+
aaveV4SpokesData,
|
|
102
108
|
};
|
|
103
109
|
|
|
104
110
|
const positions: PortfolioPositionsData = {};
|
|
@@ -109,6 +115,7 @@ export async function getPortfolioData(provider: EthereumProvider, network: Netw
|
|
|
109
115
|
for (const address of allAddresses) {
|
|
110
116
|
positions[address.toLowerCase() as EthAddress] = {
|
|
111
117
|
aaveV3: {},
|
|
118
|
+
aaveV4: {},
|
|
112
119
|
morphoBlue: {},
|
|
113
120
|
compoundV3: {},
|
|
114
121
|
spark: {},
|
|
@@ -177,6 +184,10 @@ export async function getPortfolioData(provider: EthereumProvider, network: Netw
|
|
|
177
184
|
const marketData = await _getAaveV3MarketData(client, network, market);
|
|
178
185
|
aaveV3MarketsData[market.value] = marketData;
|
|
179
186
|
}),
|
|
187
|
+
...aaveV4Spokes.map(async (spoke) => {
|
|
188
|
+
const spokeData = await _getAaveV4SpokeData(client, network, spoke);
|
|
189
|
+
aaveV4SpokesData[spoke.value] = spokeData;
|
|
190
|
+
}),
|
|
180
191
|
...aaveV2Markets.map(async (market) => {
|
|
181
192
|
const marketData = await _getAaveV2MarketsData(client, network, market);
|
|
182
193
|
aaveV2MarketsData[market.value] = marketData;
|
|
@@ -429,6 +440,15 @@ export async function getPortfolioData(provider: EthereumProvider, network: Netw
|
|
|
429
440
|
positions[address.toLowerCase() as EthAddress].aaveV3[market.value] = { error: `Error fetching AaveV3 account data for address ${address} on market ${market.value}`, data: null };
|
|
430
441
|
}
|
|
431
442
|
})).flat(),
|
|
443
|
+
...aaveV4Spokes.map((spoke) => allAddresses.map(async (address) => {
|
|
444
|
+
try {
|
|
445
|
+
const accData = await _getAaveV4AccountData(client, network, aaveV4SpokesData[spoke.value], address);
|
|
446
|
+
if (new Dec(accData.suppliedUsd).gt(0)) positions[address.toLowerCase() as EthAddress].aaveV4[spoke.value] = { error: '', data: accData };
|
|
447
|
+
} catch (error) {
|
|
448
|
+
console.error(`Error fetching AaveV4 account data for address ${address} on spoke ${spoke.value}:`, error);
|
|
449
|
+
positions[address.toLowerCase() as EthAddress].aaveV4[spoke.value] = { error: `Error fetching AaveV4 account data for address ${address} on spoke ${spoke.value}`, data: null };
|
|
450
|
+
}
|
|
451
|
+
})).flat(),
|
|
432
452
|
...morphoMarkets.map((market) => addresses.map(async (address) => {
|
|
433
453
|
try {
|
|
434
454
|
const [accDataPromise, earnDataPromise] = await Promise.allSettled([
|