@defisaver/positions-sdk 2.1.69 → 2.1.70-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 +174 -0
- package/cjs/config/contracts.d.ts +1535 -0
- package/cjs/config/contracts.js +9 -0
- package/cjs/contracts.d.ts +32401 -0
- package/cjs/contracts.js +2 -1
- package/cjs/helpers/aaveV4Helpers/index.d.ts +13 -0
- package/cjs/helpers/aaveV4Helpers/index.js +117 -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 +28 -0
- package/cjs/markets/aaveV4/index.js +140 -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/types/aaveV4.d.ts +145 -0
- package/cjs/types/aaveV4.js +19 -0
- package/cjs/types/common.d.ts +1 -1
- package/cjs/types/common.js +1 -1
- 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 +165 -0
- package/esm/config/contracts.d.ts +1535 -0
- package/esm/config/contracts.js +8 -0
- package/esm/contracts.d.ts +32401 -0
- package/esm/contracts.js +1 -0
- package/esm/helpers/aaveV4Helpers/index.d.ts +13 -0
- package/esm/helpers/aaveV4Helpers/index.js +108 -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 +28 -0
- package/esm/markets/aaveV4/index.js +122 -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/types/aaveV4.d.ts +145 -0
- package/esm/types/aaveV4.js +16 -0
- package/esm/types/common.d.ts +1 -1
- package/esm/types/common.js +1 -1
- 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 +1 -1
- package/src/aaveV4/index.ts +176 -0
- package/src/config/contracts.ts +9 -1
- package/src/contracts.ts +3 -1
- package/src/helpers/aaveV4Helpers/index.ts +128 -0
- package/src/helpers/index.ts +1 -0
- package/src/index.ts +2 -0
- package/src/markets/aaveV4/index.ts +149 -0
- package/src/markets/index.ts +6 -1
- package/src/portfolio/index.ts +20 -0
- package/src/types/aaveV4.ts +161 -0
- package/src/types/common.ts +1 -1
- package/src/types/index.ts +2 -1
- package/src/types/portfolio.ts +4 -0
|
@@ -0,0 +1,176 @@
|
|
|
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
|
+
import { getAaveV4HubByAddress } from '../markets/aaveV4';
|
|
20
|
+
|
|
21
|
+
const fetchHubData = async (viewContract: ReturnType<typeof AaveV4ViewContractViem>, hubAddress: EthAddress): Promise<AaveV4HubOnChainData> => {
|
|
22
|
+
const hubData = await viewContract.read.getHubAllAssetsData([hubAddress]);
|
|
23
|
+
return {
|
|
24
|
+
assets: hubData.reduce((acc: Record<number, AaveV4HubAssetOnChainData>, assetOnChainData) => {
|
|
25
|
+
acc[assetOnChainData.assetId] = {
|
|
26
|
+
assetId: assetOnChainData.assetId,
|
|
27
|
+
drawnRate: assetOnChainData.drawnRate,
|
|
28
|
+
};
|
|
29
|
+
return acc;
|
|
30
|
+
}, {}),
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const formatReserveAsset = async (reserveAsset: AaveV4ReserveAssetOnChain, hubAsset: AaveV4HubAssetOnChainData, reserveId: number, oracleDecimals: number, network: NetworkNumber): Promise<AaveV4ReserveAssetData> => {
|
|
35
|
+
const assetInfo = getAssetInfoByAddress(reserveAsset.underlying, network);
|
|
36
|
+
const symbol = wethToEth(assetInfo.symbol);
|
|
37
|
+
const hubInfo = getAaveV4HubByAddress(network, reserveAsset.hub);
|
|
38
|
+
if (!hubInfo) {
|
|
39
|
+
throw new Error(`Hub not found with address: ${reserveAsset.hub}`);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const isStakingAsset = STAKING_ASSETS.includes(symbol);
|
|
43
|
+
const supplyIncentives: IncentiveData[] = [];
|
|
44
|
+
const borrowIncentives: IncentiveData[] = [];
|
|
45
|
+
|
|
46
|
+
if (isStakingAsset) {
|
|
47
|
+
const yieldApy = await getStakingApy(symbol, network as NetworkNumber);
|
|
48
|
+
supplyIncentives.push({
|
|
49
|
+
apy: yieldApy,
|
|
50
|
+
token: symbol,
|
|
51
|
+
incentiveKind: IncentiveKind.Staking,
|
|
52
|
+
description: `Native ${symbol} yield.`,
|
|
53
|
+
});
|
|
54
|
+
if (reserveAsset.borrowable) {
|
|
55
|
+
// when borrowing assets whose value increases over time
|
|
56
|
+
borrowIncentives.push({
|
|
57
|
+
apy: new Dec(yieldApy).mul(-1).toString(),
|
|
58
|
+
token: symbol,
|
|
59
|
+
incentiveKind: IncentiveKind.Reward,
|
|
60
|
+
description: `Due to the native yield of ${symbol}, the value of the debt would increase over time.`,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return ({
|
|
66
|
+
symbol,
|
|
67
|
+
underlying: reserveAsset.underlying,
|
|
68
|
+
hub: hubInfo.address,
|
|
69
|
+
hubName: hubInfo?.label,
|
|
70
|
+
assetId: reserveAsset.assetId,
|
|
71
|
+
reserveId,
|
|
72
|
+
paused: reserveAsset.paused,
|
|
73
|
+
frozen: reserveAsset.frozen,
|
|
74
|
+
borrowable: reserveAsset.borrowable,
|
|
75
|
+
collateralRisk: new Dec(reserveAsset.collateralRisk).div(10000).toNumber(),
|
|
76
|
+
collateralFactor: new Dec(reserveAsset.collateralFactor).div(10000).toNumber(),
|
|
77
|
+
liquidationFee: new Dec(reserveAsset.liquidationFee).div(10000).toNumber(),
|
|
78
|
+
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),
|
|
85
|
+
spokeActive: reserveAsset.spokeActive,
|
|
86
|
+
spokeHalted: reserveAsset.spokeHalted,
|
|
87
|
+
drawnRate: new Dec(hubAsset.drawnRate).div(new Dec(10).pow(27)).toString(),
|
|
88
|
+
supplyRate: '0', // To be implemented
|
|
89
|
+
supplyIncentives,
|
|
90
|
+
borrowIncentives,
|
|
91
|
+
canBeBorrowed: reserveAsset.spokeActive && !reserveAsset.spokeHalted && !reserveAsset.paused && !reserveAsset.frozen,
|
|
92
|
+
canBeSupplied: reserveAsset.spokeActive && !reserveAsset.spokeHalted && !reserveAsset.paused && !reserveAsset.frozen,
|
|
93
|
+
canBeWithdrawn: reserveAsset.spokeActive && !reserveAsset.spokeHalted && !reserveAsset.paused,
|
|
94
|
+
canBePayBacked: reserveAsset.spokeActive && !reserveAsset.spokeHalted && !reserveAsset.paused,
|
|
95
|
+
utilization: new Dec(reserveAsset.totalDrawn.toString()).times(100).div(new Dec(reserveAsset.totalSupplied.toString())).toString(),
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
export async function _getAaveV4SpokeData(provider: Client, network: NetworkNumber, market: AaveV4SpokeInfo, blockNumber: 'latest' | number = 'latest'): Promise<AaveV4SpokeData> {
|
|
100
|
+
const viewContract = AaveV4ViewContractViem(provider, network, blockNumber);
|
|
101
|
+
|
|
102
|
+
const hubsData: Record<EthAddress, AaveV4HubOnChainData> = {};
|
|
103
|
+
const [spokeData] = await Promise.all([
|
|
104
|
+
viewContract.read.getSpokeDataFull([market.address]),
|
|
105
|
+
...market.hubs.map(async (hubAddress) => {
|
|
106
|
+
hubsData[hubAddress] = await fetchHubData(viewContract, hubAddress);
|
|
107
|
+
}),
|
|
108
|
+
]);
|
|
109
|
+
|
|
110
|
+
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)));
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
assetsData: reserveAssetsArray.reduce((acc: Record<string, AaveV4ReserveAssetData>, reserveAsset: AaveV4ReserveAssetData) => {
|
|
114
|
+
acc[`${reserveAsset.symbol}-${reserveAsset.reserveId}`] = reserveAsset;
|
|
115
|
+
return acc;
|
|
116
|
+
}, {}),
|
|
117
|
+
oracle: spokeData[0].oracle,
|
|
118
|
+
oracleDecimals: +spokeData[0].oracleDecimals.toString(),
|
|
119
|
+
address: market.address,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export async function getAaveV4SpokeData(provider: EthereumProvider, network: NetworkNumber, spoke: AaveV4SpokeInfo, blockNumber: 'latest' | number = 'latest'): Promise<AaveV4SpokeData> {
|
|
124
|
+
return _getAaveV4SpokeData(getViemProvider(provider, network), network, spoke, blockNumber);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export async function _getAaveV4AccountData(provider: Client, network: NetworkNumber, spokeData: AaveV4SpokeData, address: EthAddress, blockNumber: 'latest' | number = 'latest'): Promise<AaveV4AccountData> {
|
|
128
|
+
const viewContract = AaveV4ViewContractViem(provider, network, blockNumber);
|
|
129
|
+
|
|
130
|
+
const loanData = await viewContract.read.getLoanData([spokeData.address, address]);
|
|
131
|
+
|
|
132
|
+
const healthFactor = new Dec(loanData.healthFactor).div(1e18).toString();
|
|
133
|
+
const usedAssets = loanData.reserves.reduce((acc: AaveV4UsedReserveAssets, usedReserveAsset) => {
|
|
134
|
+
const identifier = `${wethToEth(getAssetInfoByAddress(usedReserveAsset.underlying, network).symbol)}-${+usedReserveAsset.reserveId.toString()}`;
|
|
135
|
+
const reserveData = spokeData.assetsData[identifier];
|
|
136
|
+
const price = reserveData.price;
|
|
137
|
+
const supplied = assetAmountInEth(usedReserveAsset.supplied.toString(), reserveData.symbol);
|
|
138
|
+
const drawn = assetAmountInEth(usedReserveAsset.drawn.toString(), reserveData.symbol);
|
|
139
|
+
const premium = assetAmountInEth(usedReserveAsset.premium.toString(), reserveData.symbol);
|
|
140
|
+
const borrowed = assetAmountInEth(usedReserveAsset.totalDebt.toString(), reserveData.symbol);
|
|
141
|
+
acc[identifier] = {
|
|
142
|
+
symbol: reserveData.symbol,
|
|
143
|
+
hubName: reserveData.hubName,
|
|
144
|
+
assetId: reserveData.assetId,
|
|
145
|
+
reserveId: +usedReserveAsset.reserveId.toString(),
|
|
146
|
+
supplied,
|
|
147
|
+
suppliedUsd: new Dec(supplied).mul(price).toString(),
|
|
148
|
+
drawn,
|
|
149
|
+
drawnUsd: new Dec(drawn).mul(price).toString(),
|
|
150
|
+
premium,
|
|
151
|
+
premiumUsd: new Dec(premium).mul(price).toString(),
|
|
152
|
+
borrowed,
|
|
153
|
+
borrowedUsd: new Dec(borrowed).mul(price).toString(),
|
|
154
|
+
isSupplied: !new Dec(supplied).eq(0),
|
|
155
|
+
isBorrowed: usedReserveAsset.isBorrowing,
|
|
156
|
+
collateral: usedReserveAsset.isUsingAsCollateral,
|
|
157
|
+
collateralFactor: new Dec(usedReserveAsset.collateralFactor).div(10000).toNumber(),
|
|
158
|
+
};
|
|
159
|
+
return acc;
|
|
160
|
+
}, {});
|
|
161
|
+
|
|
162
|
+
return {
|
|
163
|
+
usedAssets,
|
|
164
|
+
healthFactor,
|
|
165
|
+
...aaveV4GetAggregatedPositionData({
|
|
166
|
+
usedAssets,
|
|
167
|
+
assetsData: spokeData.assetsData,
|
|
168
|
+
network,
|
|
169
|
+
useUserCollateralFactor: true,
|
|
170
|
+
}),
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export async function getAaveV4AccountData(provider: EthereumProvider, network: NetworkNumber, marketData: AaveV4SpokeData, address: EthAddress, blockNumber: 'latest' | number = 'latest'): Promise<any> {
|
|
175
|
+
return _getAaveV4AccountData(getViemProvider(provider, network), network, marketData, address, blockNumber);
|
|
176
|
+
}
|
package/src/config/contracts.ts
CHANGED
|
@@ -620,7 +620,7 @@ export const AaveLoanInfoV2 = {
|
|
|
620
620
|
"address": "0xEDf1087544a01596b70Da746F861B878F245B08f"
|
|
621
621
|
},
|
|
622
622
|
}
|
|
623
|
-
},
|
|
623
|
+
},
|
|
624
624
|
}
|
|
625
625
|
} as const;
|
|
626
626
|
export const LendingPoolAddressesProvider = {
|
|
@@ -1322,4 +1322,12 @@ export const SkySavings = {
|
|
|
1322
1322
|
} as const;
|
|
1323
1323
|
export const YearnV3Vault = {
|
|
1324
1324
|
"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"}]}]
|
|
1325
|
+
} as const;
|
|
1326
|
+
export const AaveV4View = {
|
|
1327
|
+
"abi": [{"inputs":[{"internalType":"address","name":"_eoa","type":"address"},{"internalType":"address","name":"_proxy","type":"address"},{"internalType":"address","name":"_spoke","type":"address"}],"name":"getEOAApprovalsAndBalances","outputs":[{"components":[{"internalType":"address","name":"eoa","type":"address"},{"internalType":"address","name":"proxy","type":"address"},{"internalType":"address","name":"spoke","type":"address"},{"internalType":"bool","name":"giverPositionManagerEnabled","type":"bool"},{"internalType":"bool","name":"takerPositionManagerEnabled","type":"bool"},{"internalType":"bool","name":"configPositionManagerEnabled","type":"bool"},{"internalType":"bool","name":"canSetUsingAsCollateral","type":"bool"},{"internalType":"bool","name":"canUpdateUserRiskPremium","type":"bool"},{"internalType":"bool","name":"canUpdateUserDynamicConfig","type":"bool"},{"components":[{"internalType":"uint256","name":"reserveId","type":"uint256"},{"internalType":"address","name":"underlying","type":"address"},{"internalType":"uint256","name":"delegateeBorrowApproval","type":"uint256"},{"internalType":"uint256","name":"delegateeWithdrawApproval","type":"uint256"},{"internalType":"uint256","name":"eoaReserveBalance","type":"uint256"}],"internalType":"struct AaveV4View.EOAReserveApprovalData[]","name":"reserveApprovals","type":"tuple[]"}],"internalType":"struct AaveV4View.EOAApprovalData","name":"data","type":"tuple"}],"stateMutability":"view","type":"function"},{"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":"struct AaveV4View.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":"struct AaveV4View.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":"totalDebtInUsdRay","type":"uint256"},{"internalType":"uint256","name":"activeCollateralCount","type":"uint256"},{"internalType":"uint256","name":"borrowCount","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":"struct AaveV4View.UserReserveData[]","name":"reserves","type":"tuple[]"}],"internalType":"struct AaveV4View.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":"totalDebtInUsdRay","type":"uint256"},{"internalType":"uint256","name":"activeCollateralCount","type":"uint256"},{"internalType":"uint256","name":"borrowCount","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":"struct AaveV4View.UserReserveData[]","name":"reserves","type":"tuple[]"}],"internalType":"struct AaveV4View.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":"totalDebtInUsdRay","type":"uint256"},{"internalType":"uint256","name":"activeCollateralCount","type":"uint256"},{"internalType":"uint256","name":"borrowCount","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":"struct AaveV4View.UserReserveData[]","name":"reserves","type":"tuple[]"}],"internalType":"struct AaveV4View.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":"totalDebtInUsdRay","type":"uint256"},{"internalType":"uint256","name":"activeCollateralCount","type":"uint256"},{"internalType":"uint256","name":"borrowCount","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":"spokeHalted","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":"struct AaveV4View.UserReserveDataFull[]","name":"reserves","type":"tuple[]"}],"internalType":"struct AaveV4View.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":"struct AaveV4View.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":"spokeHalted","type":"bool"}],"internalType":"struct AaveV4View.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":"struct AaveV4View.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":"spokeHalted","type":"bool"}],"internalType":"struct AaveV4View.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":"struct AaveV4View.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":"struct AaveV4View.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":"struct AaveV4View.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":"spokeHalted","type":"bool"}],"internalType":"struct AaveV4View.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"}],"name":"getTokenizationSpokeData","outputs":[{"components":[{"internalType":"address","name":"underlyingAsset","type":"address"},{"internalType":"uint256","name":"assetId","type":"uint256"},{"internalType":"uint8","name":"decimals","type":"uint8"},{"internalType":"address","name":"spoke","type":"address"},{"internalType":"bool","name":"spokeActive","type":"bool"},{"internalType":"bool","name":"spokeHalted","type":"bool"},{"internalType":"uint256","name":"spokeDepositCap","type":"uint256"},{"internalType":"uint256","name":"spokeTotalAssets","type":"uint256"},{"internalType":"uint256","name":"spokeTotalShares","type":"uint256"},{"internalType":"address","name":"hub","type":"address"},{"internalType":"uint256","name":"hubLiquidity","type":"uint256"},{"internalType":"uint96","name":"hubDrawnRate","type":"uint96"},{"internalType":"uint256","name":"convertToShares","type":"uint256"},{"internalType":"uint256","name":"convertToAssets","type":"uint256"},{"internalType":"address","name":"user","type":"address"},{"internalType":"uint256","name":"userSuppliedAssets","type":"uint256"},{"internalType":"uint256","name":"userSuppliedShares","type":"uint256"}],"internalType":"struct AaveV4View.TokenizationSpokeData","name":"spokeData","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address[]","name":"_spokes","type":"address[]"},{"internalType":"address","name":"_user","type":"address"}],"name":"getTokenizationSpokesData","outputs":[{"components":[{"internalType":"address","name":"underlyingAsset","type":"address"},{"internalType":"uint256","name":"assetId","type":"uint256"},{"internalType":"uint8","name":"decimals","type":"uint8"},{"internalType":"address","name":"spoke","type":"address"},{"internalType":"bool","name":"spokeActive","type":"bool"},{"internalType":"bool","name":"spokeHalted","type":"bool"},{"internalType":"uint256","name":"spokeDepositCap","type":"uint256"},{"internalType":"uint256","name":"spokeTotalAssets","type":"uint256"},{"internalType":"uint256","name":"spokeTotalShares","type":"uint256"},{"internalType":"address","name":"hub","type":"address"},{"internalType":"uint256","name":"hubLiquidity","type":"uint256"},{"internalType":"uint96","name":"hubDrawnRate","type":"uint96"},{"internalType":"uint256","name":"convertToShares","type":"uint256"},{"internalType":"uint256","name":"convertToAssets","type":"uint256"},{"internalType":"address","name":"user","type":"address"},{"internalType":"uint256","name":"userSuppliedAssets","type":"uint256"},{"internalType":"uint256","name":"userSuppliedShares","type":"uint256"}],"internalType":"struct AaveV4View.TokenizationSpokeData[]","name":"spokeData","type":"tuple[]"}],"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":"struct AaveV4View.UserReserveData[]","name":"_userReserves","type":"tuple[]"}],"stateMutability":"view","type":"function"}],
|
|
1328
|
+
"networks": {
|
|
1329
|
+
"1": {
|
|
1330
|
+
"address": "0xF0D440dE7f82A1e598992A648E5Bfa5d69b8E3a9",
|
|
1331
|
+
}
|
|
1332
|
+
}
|
|
1325
1333
|
} 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,128 @@
|
|
|
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 { LeverageType, 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
|
+
const isVolatilePair = supplyUnstable === 1 && borrowUnstable === 1 && supplyStable === 0 && borrowStable === 0;
|
|
37
|
+
if (isLong) {
|
|
38
|
+
return {
|
|
39
|
+
leveragedType: LeverageType.Long,
|
|
40
|
+
leveragedAsset: longAsset,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
if (isShort) {
|
|
44
|
+
return {
|
|
45
|
+
leveragedType: LeverageType.Short,
|
|
46
|
+
leveragedAsset: shortAsset,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
if (isVolatilePair) {
|
|
50
|
+
return {
|
|
51
|
+
leveragedType: LeverageType.VolatilePair,
|
|
52
|
+
leveragedAsset: longAsset,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
return {
|
|
56
|
+
leveragedType: LeverageType.None,
|
|
57
|
+
leveragedAsset: '',
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export const aaveV4GetAggregatedPositionData = ({
|
|
62
|
+
usedAssets,
|
|
63
|
+
assetsData,
|
|
64
|
+
network,
|
|
65
|
+
useUserCollateralFactor = false,
|
|
66
|
+
}: {
|
|
67
|
+
usedAssets: AaveV4UsedReserveAssets,
|
|
68
|
+
assetsData: AaveV4AssetsData,
|
|
69
|
+
network: NetworkNumber,
|
|
70
|
+
useUserCollateralFactor?: boolean,
|
|
71
|
+
}): AaveV4AggregatedPositionData => {
|
|
72
|
+
const payload = {} as AaveV4AggregatedPositionData;
|
|
73
|
+
payload.suppliedUsd = getAssetsTotal(usedAssets, ({ isSupplied }: { isSupplied: boolean }) => isSupplied, ({ suppliedUsd }: { suppliedUsd: string }) => suppliedUsd);
|
|
74
|
+
payload.suppliedCollateralUsd = getAssetsTotal(usedAssets, ({ isSupplied, collateral }: { isSupplied: boolean, collateral: string }) => isSupplied && collateral, ({ suppliedUsd }: { suppliedUsd: string }) => suppliedUsd);
|
|
75
|
+
payload.borrowLimitUsd = getAssetsTotal(
|
|
76
|
+
usedAssets,
|
|
77
|
+
({ isSupplied, collateral }: { isSupplied: boolean, collateral: string }) => isSupplied && collateral,
|
|
78
|
+
({ symbol, suppliedUsd, reserveId }: { symbol: string, suppliedUsd: string, reserveId: number }) => new Dec(suppliedUsd).mul(aaveV4GetCollateralFactor(assetsData[`${symbol}-${reserveId}`], usedAssets[`${symbol}-${reserveId}`], useUserCollateralFactor)),
|
|
79
|
+
);
|
|
80
|
+
payload.liquidationLimitUsd = payload.borrowLimitUsd;
|
|
81
|
+
payload.borrowedUsd = getAssetsTotal(usedAssets, ({ isBorrowed }: { isBorrowed: boolean }) => isBorrowed, ({ borrowedUsd }: { borrowedUsd: string }) => borrowedUsd);
|
|
82
|
+
payload.drawnUsd = getAssetsTotal(usedAssets, ({ isBorrowed }: { isBorrowed: boolean }) => isBorrowed, ({ drawnUsd }: { drawnUsd: string }) => drawnUsd);
|
|
83
|
+
payload.premiumUsd = getAssetsTotal(usedAssets, ({ isBorrowed }: { isBorrowed: boolean }) => isBorrowed, ({ premiumUsd }: { premiumUsd: string }) => premiumUsd);
|
|
84
|
+
const leftToBorrowUsd = new Dec(payload.borrowLimitUsd).sub(payload.borrowedUsd);
|
|
85
|
+
payload.leftToBorrowUsd = leftToBorrowUsd.lte('0') ? '0' : leftToBorrowUsd.toString();
|
|
86
|
+
payload.ratio = +payload.suppliedUsd ? new Dec(payload.borrowLimitUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
|
|
87
|
+
payload.collRatio = +payload.suppliedUsd ? new Dec(payload.suppliedCollateralUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
|
|
88
|
+
payload.liqRatio = new Dec(payload.borrowLimitUsd).div(payload.liquidationLimitUsd).toString();
|
|
89
|
+
payload.liqPercent = new Dec(payload.borrowLimitUsd).div(payload.liquidationLimitUsd).mul(100).toString();
|
|
90
|
+
const { leveragedType, leveragedAsset } = isLeveragedPosAaveV4(usedAssets);
|
|
91
|
+
payload.leveragedType = leveragedType;
|
|
92
|
+
payload.leveragedAsset = leveragedAsset;
|
|
93
|
+
payload.liquidationPrice = '';
|
|
94
|
+
if (leveragedType !== '') {
|
|
95
|
+
const leveragedAssetData = assetsData[leveragedAsset];
|
|
96
|
+
let assetPrice = leveragedAssetData?.price || '0';
|
|
97
|
+
if (leveragedType === LeverageType.VolatilePair) {
|
|
98
|
+
const borrowedAsset = (Object.values(usedAssets) as AaveV4UsedReserveAsset[]).find(({ borrowedUsd }: { borrowedUsd: string }) => +borrowedUsd > 0);
|
|
99
|
+
const borrowedAssetPrice = assetsData[`${borrowedAsset!.symbol}-${borrowedAsset!.reserveId}`].price;
|
|
100
|
+
const leveragedAssetPrice = assetsData[leveragedAsset].price;
|
|
101
|
+
const isReverse = new Dec(leveragedAssetPrice).lt(borrowedAssetPrice);
|
|
102
|
+
if (isReverse) {
|
|
103
|
+
payload.leveragedType = LeverageType.VolatilePairReverse;
|
|
104
|
+
payload.currentVolatilePairRatio = new Dec(borrowedAssetPrice).div(leveragedAssetPrice).toDP(18).toString();
|
|
105
|
+
assetPrice = new Dec(borrowedAssetPrice).div(assetPrice).toString();
|
|
106
|
+
} else {
|
|
107
|
+
assetPrice = new Dec(assetPrice).div(borrowedAssetPrice).toString();
|
|
108
|
+
payload.currentVolatilePairRatio = new Dec(leveragedAssetPrice).div(borrowedAssetPrice).toDP(18).toString();
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
payload.liquidationPrice = calcLeverageLiqPrice(payload.leveragedType, assetPrice, payload.borrowedUsd, payload.liquidationLimitUsd);
|
|
112
|
+
}
|
|
113
|
+
payload.minCollRatio = new Dec(payload.suppliedCollateralUsd).div(payload.borrowLimitUsd).mul(100).toString();
|
|
114
|
+
payload.collLiquidationRatio = new Dec(payload.suppliedCollateralUsd).div(payload.liquidationLimitUsd).mul(100).toString();
|
|
115
|
+
// payload.healthRatio = new Dec(payload.liquidationLimitUsd).div(payload.borrowedUsd).toDP(4).toString();
|
|
116
|
+
payload.minHealthRatio = new Dec(payload.liquidationLimitUsd).div(payload.borrowLimitUsd).toDP(4).toString();
|
|
117
|
+
|
|
118
|
+
// TODO: Re-implement netApy calculation
|
|
119
|
+
// const { netApy, incentiveUsd, totalInterestUsd } = calculateNetApy({
|
|
120
|
+
// usedAssets,
|
|
121
|
+
// assetsData,
|
|
122
|
+
// optionalData: { healthRatio: payload.healthRatio },
|
|
123
|
+
// });
|
|
124
|
+
payload.netApy = '0';
|
|
125
|
+
payload.incentiveUsd = '0';
|
|
126
|
+
payload.totalInterestUsd = '0';
|
|
127
|
+
return payload;
|
|
128
|
+
};
|
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,149 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AaveV4HubInfo,
|
|
3
|
+
AaveV4HubsType,
|
|
4
|
+
AaveV4SpokeInfo,
|
|
5
|
+
AaveV4SpokesType,
|
|
6
|
+
NetworkNumber,
|
|
7
|
+
} from '../../types';
|
|
8
|
+
|
|
9
|
+
// HUBS
|
|
10
|
+
|
|
11
|
+
export const AAVE_V4_CORE_HUB = (networkId: NetworkNumber): AaveV4HubInfo => ({
|
|
12
|
+
chainIds: [NetworkNumber.Eth],
|
|
13
|
+
label: 'Core Hub',
|
|
14
|
+
value: AaveV4HubsType.AaveV4CoreHub,
|
|
15
|
+
address: '0x8b5aABDbf90744FA259fa7A708c00fF5Cf43FD75',
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
export const AAVE_V4_PLUS_HUB = (networkId: NetworkNumber): AaveV4HubInfo => ({
|
|
19
|
+
chainIds: [NetworkNumber.Eth],
|
|
20
|
+
label: 'Plus Hub',
|
|
21
|
+
value: AaveV4HubsType.AaveV4PlusHub,
|
|
22
|
+
address: '0xDf488F18631Ff7DcF39Ab305C9BE5AeC06F673d0',
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
export const AAVE_V4_PRIME_HUB = (networkId: NetworkNumber): AaveV4HubInfo => ({
|
|
26
|
+
chainIds: [NetworkNumber.Eth],
|
|
27
|
+
label: 'Prime Hub',
|
|
28
|
+
value: AaveV4HubsType.AaveV4PrimeHub,
|
|
29
|
+
address: '0xaa605F00a695fE90f4818CcB11C0daF22e23Aa69',
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
export const AaveV4Hubs = (networkId: NetworkNumber) => ({
|
|
33
|
+
[AaveV4HubsType.AaveV4CoreHub]: AAVE_V4_CORE_HUB(networkId),
|
|
34
|
+
[AaveV4HubsType.AaveV4PlusHub]: AAVE_V4_PLUS_HUB(networkId),
|
|
35
|
+
[AaveV4HubsType.AaveV4PrimeHub]: AAVE_V4_PRIME_HUB(networkId),
|
|
36
|
+
}) as const;
|
|
37
|
+
|
|
38
|
+
export const getAaveV4HubTypeInfo = (type: AaveV4HubsType, network?: NetworkNumber) => ({ ...AaveV4Hubs(network ?? NetworkNumber.Eth) }[type]);
|
|
39
|
+
|
|
40
|
+
export const getAaveV4HubByAddress = (networkId: NetworkNumber, address: string): AaveV4HubInfo | undefined => Object.values(AaveV4Hubs(networkId)).find(
|
|
41
|
+
hub => hub.address.toLowerCase() === address.toLowerCase(),
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
// SPOKES
|
|
45
|
+
|
|
46
|
+
export const AAVE_V4_BLUECHIP_SPOKE = (networkId: NetworkNumber): AaveV4SpokeInfo => ({
|
|
47
|
+
chainIds: [NetworkNumber.Eth],
|
|
48
|
+
label: 'Bluechip Spoke',
|
|
49
|
+
value: AaveV4SpokesType.AaveV4BluechipSpoke,
|
|
50
|
+
url: 'bluechip',
|
|
51
|
+
address: '0xe8344a3A199Cb995241BEcD6129a93Cc128f24ca',
|
|
52
|
+
hubs: [
|
|
53
|
+
AAVE_V4_CORE_HUB(NetworkNumber.Eth).address,
|
|
54
|
+
AAVE_V4_PLUS_HUB(NetworkNumber.Eth).address,
|
|
55
|
+
AAVE_V4_PRIME_HUB(NetworkNumber.Eth).address,
|
|
56
|
+
],
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
export const AAVE_V4_ETHENA_SPOKE = (networkId: NetworkNumber): AaveV4SpokeInfo => ({
|
|
60
|
+
chainIds: [NetworkNumber.Eth],
|
|
61
|
+
label: 'Ethena Spoke',
|
|
62
|
+
value: AaveV4SpokesType.AaveV4EthenaSpoke,
|
|
63
|
+
url: 'ethena',
|
|
64
|
+
address: '0x57893f96d25f7125211d797bC39972b629572b2c',
|
|
65
|
+
hubs: [
|
|
66
|
+
AAVE_V4_CORE_HUB(NetworkNumber.Eth).address,
|
|
67
|
+
AAVE_V4_PLUS_HUB(NetworkNumber.Eth).address,
|
|
68
|
+
AAVE_V4_PRIME_HUB(NetworkNumber.Eth).address,
|
|
69
|
+
],
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
export const AAVE_V4_ETHERFI_SPOKE = (networkId: NetworkNumber): AaveV4SpokeInfo => ({
|
|
73
|
+
chainIds: [NetworkNumber.Eth],
|
|
74
|
+
label: 'Etherfi Spoke',
|
|
75
|
+
value: AaveV4SpokesType.AaveV4EtherfiSpoke,
|
|
76
|
+
url: 'etherfi',
|
|
77
|
+
address: '0xFC2685999D5a1d15AAEf0E73426673829A483cA6',
|
|
78
|
+
hubs: [
|
|
79
|
+
AAVE_V4_CORE_HUB(NetworkNumber.Eth).address,
|
|
80
|
+
AAVE_V4_PLUS_HUB(NetworkNumber.Eth).address,
|
|
81
|
+
AAVE_V4_PRIME_HUB(NetworkNumber.Eth).address,
|
|
82
|
+
],
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
export const AAVE_V4_GOLD_SPOKE = (networkId: NetworkNumber): AaveV4SpokeInfo => ({
|
|
86
|
+
chainIds: [NetworkNumber.Eth],
|
|
87
|
+
label: 'Gold Spoke',
|
|
88
|
+
value: AaveV4SpokesType.AaveV4GoldSpoke,
|
|
89
|
+
url: 'gold',
|
|
90
|
+
address: '0x4b8A81E96dcbD21Aaf6bd32E03Bd0c64b8a2E6Fa',
|
|
91
|
+
hubs: [
|
|
92
|
+
AAVE_V4_CORE_HUB(NetworkNumber.Eth).address,
|
|
93
|
+
AAVE_V4_PLUS_HUB(NetworkNumber.Eth).address,
|
|
94
|
+
AAVE_V4_PRIME_HUB(NetworkNumber.Eth).address,
|
|
95
|
+
],
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
export const AAVE_V4_KELP_SPOKE = (networkId: NetworkNumber): AaveV4SpokeInfo => ({
|
|
99
|
+
chainIds: [NetworkNumber.Eth],
|
|
100
|
+
label: 'Kelp Spoke',
|
|
101
|
+
value: AaveV4SpokesType.AaveV4KelpSpoke,
|
|
102
|
+
url: 'kelp',
|
|
103
|
+
address: '0x4a2A38377cC85BFd8548a4C9AC71DBb245dc9A9D',
|
|
104
|
+
hubs: [
|
|
105
|
+
AAVE_V4_CORE_HUB(NetworkNumber.Eth).address,
|
|
106
|
+
AAVE_V4_PLUS_HUB(NetworkNumber.Eth).address,
|
|
107
|
+
AAVE_V4_PRIME_HUB(NetworkNumber.Eth).address,
|
|
108
|
+
],
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
export const AAVE_V4_LIDO_SPOKE = (networkId: NetworkNumber): AaveV4SpokeInfo => ({
|
|
112
|
+
chainIds: [NetworkNumber.Eth],
|
|
113
|
+
label: 'Lido Spoke',
|
|
114
|
+
value: AaveV4SpokesType.AaveV4LidoSpoke,
|
|
115
|
+
url: 'lido',
|
|
116
|
+
address: '0x4f7202367f72ADae87393c3e49b3Fbc353a9110D',
|
|
117
|
+
hubs: [
|
|
118
|
+
AAVE_V4_CORE_HUB(NetworkNumber.Eth).address,
|
|
119
|
+
AAVE_V4_PLUS_HUB(NetworkNumber.Eth).address,
|
|
120
|
+
AAVE_V4_PRIME_HUB(NetworkNumber.Eth).address,
|
|
121
|
+
],
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
export const AAVE_V4_MAIN_SPOKE = (networkId: NetworkNumber): AaveV4SpokeInfo => ({
|
|
125
|
+
chainIds: [NetworkNumber.Eth],
|
|
126
|
+
label: 'Main Spoke',
|
|
127
|
+
value: AaveV4SpokesType.AaveV4MainSpoke,
|
|
128
|
+
url: 'main',
|
|
129
|
+
address: '0x5738d9cB82d6a1617973C257D05A387bF5568F47',
|
|
130
|
+
hubs: [
|
|
131
|
+
AAVE_V4_CORE_HUB(NetworkNumber.Eth).address,
|
|
132
|
+
AAVE_V4_PLUS_HUB(NetworkNumber.Eth).address,
|
|
133
|
+
AAVE_V4_PRIME_HUB(NetworkNumber.Eth).address,
|
|
134
|
+
],
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
export const AaveV4Spokes = (networkId: NetworkNumber) => ({
|
|
138
|
+
[AaveV4SpokesType.AaveV4BluechipSpoke]: AAVE_V4_BLUECHIP_SPOKE(networkId),
|
|
139
|
+
[AaveV4SpokesType.AaveV4EthenaSpoke]: AAVE_V4_ETHENA_SPOKE(networkId),
|
|
140
|
+
[AaveV4SpokesType.AaveV4EtherfiSpoke]: AAVE_V4_ETHERFI_SPOKE(networkId),
|
|
141
|
+
[AaveV4SpokesType.AaveV4GoldSpoke]: AAVE_V4_GOLD_SPOKE(networkId),
|
|
142
|
+
[AaveV4SpokesType.AaveV4KelpSpoke]: AAVE_V4_KELP_SPOKE(networkId),
|
|
143
|
+
[AaveV4SpokesType.AaveV4LidoSpoke]: AAVE_V4_LIDO_SPOKE(networkId),
|
|
144
|
+
[AaveV4SpokesType.AaveV4MainSpoke]: AAVE_V4_MAIN_SPOKE(networkId),
|
|
145
|
+
}) as const;
|
|
146
|
+
|
|
147
|
+
export const getAaveV4SpokeTypeInfo = (type: AaveV4SpokesType, network?: NetworkNumber) => ({ ...AaveV4Spokes(network ?? NetworkNumber.Eth) }[type]);
|
|
148
|
+
|
|
149
|
+
|
package/src/markets/index.ts
CHANGED
|
@@ -21,5 +21,10 @@ export { LlamaLendMarkets } from './llamaLend';
|
|
|
21
21
|
export { LiquityV2Markets, findLiquityV2MarketByAddress } from './liquityV2';
|
|
22
22
|
export { EulerV2Markets } from './euler';
|
|
23
23
|
export {
|
|
24
|
-
FluidMarkets,
|
|
24
|
+
FluidMarkets,
|
|
25
|
+
getFluidVersionsDataForNetwork,
|
|
26
|
+
getFluidMarketInfoById,
|
|
27
|
+
getFTokenAddress,
|
|
28
|
+
getFluidMarketInfoByAddress,
|
|
25
29
|
} from './fluid';
|
|
30
|
+
export { AaveV4Spokes } from './aaveV4';
|