@defisaver/positions-sdk 0.0.28 → 0.0.30-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/README.md +63 -63
- package/cjs/config/contracts.d.ts +926 -42
- package/cjs/config/contracts.js +176 -46
- package/cjs/contracts.d.ts +1 -1
- package/cjs/contracts.js +21 -4
- package/cjs/curveUsd/index.js +1 -1
- package/cjs/helpers/curveUsdHelpers/index.d.ts +2 -1
- package/cjs/helpers/curveUsdHelpers/index.js +25 -17
- package/cjs/markets/aave/marketAssets.d.ts +9 -5
- package/cjs/markets/aave/marketAssets.js +16 -23
- package/cjs/markets/compound/marketsAssets.d.ts +11 -5
- package/cjs/markets/compound/marketsAssets.js +17 -19
- package/cjs/markets/spark/marketAssets.d.ts +3 -2
- package/cjs/markets/spark/marketAssets.js +5 -5
- package/cjs/moneymarket/moneymarketCommonService.js +1 -1
- package/cjs/morphoAaveV3/index.js +0 -2
- package/cjs/services/utils.d.ts +4 -0
- package/cjs/services/utils.js +13 -1
- package/cjs/types/contracts/generated/CrvUSDView.d.ts +30 -4
- package/cjs/types/contracts/generated/MorphoAaveV3ProxyEthMarket.d.ts +1 -0
- package/cjs/types/curveUsd.d.ts +6 -0
- package/esm/config/contracts.d.ts +926 -42
- package/esm/config/contracts.js +176 -46
- package/esm/contracts.d.ts +1 -1
- package/esm/contracts.js +21 -4
- package/esm/curveUsd/index.js +1 -1
- package/esm/helpers/curveUsdHelpers/index.d.ts +2 -1
- package/esm/helpers/curveUsdHelpers/index.js +26 -18
- package/esm/markets/aave/marketAssets.d.ts +9 -5
- package/esm/markets/aave/marketAssets.js +15 -22
- package/esm/markets/compound/marketsAssets.d.ts +11 -5
- package/esm/markets/compound/marketsAssets.js +16 -18
- package/esm/markets/spark/marketAssets.d.ts +3 -2
- package/esm/markets/spark/marketAssets.js +4 -4
- package/esm/moneymarket/moneymarketCommonService.js +1 -1
- package/esm/morphoAaveV3/index.js +0 -2
- package/esm/services/utils.d.ts +4 -0
- package/esm/services/utils.js +11 -0
- package/esm/types/contracts/generated/CrvUSDView.d.ts +30 -4
- package/esm/types/contracts/generated/MorphoAaveV3ProxyEthMarket.d.ts +1 -0
- package/esm/types/curveUsd.d.ts +6 -0
- package/package.json +40 -40
- package/src/aaveV2/index.ts +226 -226
- package/src/aaveV3/index.ts +561 -561
- package/src/assets/index.ts +60 -60
- package/src/chickenBonds/index.ts +123 -123
- package/src/compoundV2/index.ts +219 -219
- package/src/compoundV3/index.ts +275 -275
- package/src/config/contracts.js +803 -676
- package/src/constants/index.ts +3 -3
- package/src/contracts.ts +120 -126
- package/src/curveUsd/index.ts +228 -228
- package/src/exchange/index.ts +17 -17
- package/src/helpers/aaveHelpers/index.ts +134 -134
- package/src/helpers/chickenBondsHelpers/index.ts +23 -23
- package/src/helpers/compoundHelpers/index.ts +181 -181
- package/src/helpers/curveUsdHelpers/index.ts +40 -32
- package/src/helpers/index.ts +5 -5
- package/src/helpers/makerHelpers/index.ts +94 -94
- package/src/helpers/sparkHelpers/index.ts +106 -106
- package/src/index.ts +40 -40
- package/src/liquity/index.ts +116 -116
- package/src/maker/index.ts +101 -101
- package/src/markets/aave/index.ts +80 -80
- package/src/markets/aave/marketAssets.ts +24 -32
- package/src/markets/compound/index.ts +141 -141
- package/src/markets/compound/marketsAssets.ts +48 -46
- package/src/markets/curveUsd/index.ts +69 -69
- package/src/markets/index.ts +3 -3
- package/src/markets/spark/index.ts +29 -29
- package/src/markets/spark/marketAssets.ts +10 -9
- package/src/moneymarket/moneymarketCommonService.ts +75 -75
- package/src/morpho/markets.ts +39 -39
- package/src/morphoAaveV2/index.ts +255 -255
- package/src/morphoAaveV3/index.ts +619 -619
- package/src/multicall/index.ts +22 -22
- package/src/services/dsrService.ts +15 -15
- package/src/services/priceService.ts +21 -21
- package/src/services/utils.ts +48 -35
- package/src/spark/index.ts +422 -422
- package/src/staking/staking.ts +167 -167
- package/src/types/aave.ts +256 -256
- package/src/types/chickenBonds.ts +45 -45
- package/src/types/common.ts +83 -83
- package/src/types/compound.ts +128 -128
- package/src/types/contracts/generated/CrvUSDView.ts +43 -8
- package/src/types/contracts/generated/MorphoAaveV3ProxyEthMarket.ts +2 -0
- package/src/types/curveUsd.ts +118 -112
- package/src/types/index.ts +6 -6
- package/src/types/liquity.ts +30 -30
- package/src/types/maker.ts +50 -50
- package/src/types/spark.ts +106 -106
- package/yarn-error.log +64 -0
|
@@ -1,620 +1,620 @@
|
|
|
1
|
-
import {
|
|
2
|
-
assetAmountInEth, assetAmountInWei, getAssetInfo, getAssetInfoByAddress,
|
|
3
|
-
} from '@defisaver/tokens';
|
|
4
|
-
import { MorphoAaveMath } from '@morpho-org/morpho-aave-v3-sdk/lib/maths/AaveV3.maths';
|
|
5
|
-
import PoolInterestRates from '@morpho-org/morpho-aave-v3-sdk/lib/maths/PoolInterestRates';
|
|
6
|
-
import P2PInterestRates from '@morpho-org/morpho-aave-v3-sdk/lib/maths/P2PInterestRates';
|
|
7
|
-
import { BigNumber } from '@ethersproject/bignumber';
|
|
8
|
-
import Web3 from 'web3';
|
|
9
|
-
import Dec from 'decimal.js';
|
|
10
|
-
import {
|
|
11
|
-
Blockish, EthAddress, NetworkNumber, PositionBalances,
|
|
12
|
-
} from '../types/common';
|
|
13
|
-
import {
|
|
14
|
-
ethToWeth, ethToWethByAddress, getAbiItem, isLayer2Network, wethToEthByAddress,
|
|
15
|
-
} from '../services/utils';
|
|
16
|
-
import {
|
|
17
|
-
createContractWrapper,
|
|
18
|
-
getConfigContractAbi,
|
|
19
|
-
getConfigContractAddress,
|
|
20
|
-
} from '../contracts';
|
|
21
|
-
import { multicall } from '../multicall';
|
|
22
|
-
import { getCbETHApr, getREthApr, getStETHApr } from '../staking';
|
|
23
|
-
import {
|
|
24
|
-
MorphoAaveV3AssetData, MorphoAaveV3AssetsData, MorphoAaveV3MarketData, MorphoAaveV3MarketInfo, MorphoAaveV3PositionData,
|
|
25
|
-
} from '../types';
|
|
26
|
-
import { getDsrApy } from '../services/dsrService';
|
|
27
|
-
import { calculateBorrowingAssetLimit } from '../moneymarket';
|
|
28
|
-
import { EMPTY_AAVE_DATA } from '../aaveV3';
|
|
29
|
-
import { aaveAnyGetAggregatedPositionData } from '../helpers/aaveHelpers';
|
|
30
|
-
import { MORPHO_AAVE_V3_ETH } from '../markets/aave';
|
|
31
|
-
|
|
32
|
-
const morphoAaveMath = new MorphoAaveMath();
|
|
33
|
-
const poolInterestRates = new PoolInterestRates();
|
|
34
|
-
const p2pInterestRates = new P2PInterestRates();
|
|
35
|
-
|
|
36
|
-
const computeMorphoMarketData = (
|
|
37
|
-
loanInfo: any, morphoMarketData: any, aaveIndexes: any, // TODO: morpho v3 type
|
|
38
|
-
) => {
|
|
39
|
-
const { newPoolSupplyIndex, newPoolBorrowIndex } = poolInterestRates.computePoolIndexes({
|
|
40
|
-
lastPoolSupplyIndex: BigNumber.from(aaveIndexes.liquidityIndex),
|
|
41
|
-
lastPoolBorrowIndex: BigNumber.from(aaveIndexes.variableBorrowIndex),
|
|
42
|
-
lastUpdateTimestamp: BigNumber.from(aaveIndexes.lastUpdateTimestamp),
|
|
43
|
-
poolBorrowRatePerYear: BigNumber.from(loanInfo.borrowRateVariable),
|
|
44
|
-
poolSupplyRatePerYear: BigNumber.from(loanInfo.supplyRate),
|
|
45
|
-
currentTimestamp: BigNumber.from(new Dec(Date.now()).div(1000).toDP(0).toString()),
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
const proportionIdle = new Dec(morphoMarketData.idleSupply).eq(0)
|
|
49
|
-
? '0'
|
|
50
|
-
: Dec.min(
|
|
51
|
-
morphoAaveMath.INDEX_ONE.toString(),
|
|
52
|
-
morphoAaveMath.indexDiv(
|
|
53
|
-
morphoMarketData.idleSupply,
|
|
54
|
-
morphoAaveMath.indexMul(morphoMarketData.deltas.supply.scaledP2PTotal, morphoMarketData.indexes.supply.p2pIndex).toString(),
|
|
55
|
-
).toString(),
|
|
56
|
-
).toString();
|
|
57
|
-
|
|
58
|
-
const supplyProportionDelta = new Dec(morphoMarketData.idleSupply).eq(0)
|
|
59
|
-
? '0'
|
|
60
|
-
: Dec.min(
|
|
61
|
-
new Dec(morphoAaveMath.INDEX_ONE.toString()).sub(proportionIdle).toString(),
|
|
62
|
-
morphoAaveMath.indexDiv(
|
|
63
|
-
morphoAaveMath.indexMul(morphoMarketData.deltas.supply.scaledDelta, newPoolSupplyIndex),
|
|
64
|
-
morphoAaveMath.indexMul(morphoMarketData.deltas.supply.scaledP2PTotal, morphoMarketData.indexes.supply.p2pIndex),
|
|
65
|
-
).toString(),
|
|
66
|
-
).toString();
|
|
67
|
-
|
|
68
|
-
const borrowProportionDelta = new Dec(morphoMarketData.idleSupply).eq(0)
|
|
69
|
-
? '0'
|
|
70
|
-
: Dec.min(
|
|
71
|
-
morphoAaveMath.INDEX_ONE.toString(),
|
|
72
|
-
morphoAaveMath.indexDiv(
|
|
73
|
-
morphoAaveMath.indexMul(morphoMarketData.deltas.borrow.scaledDelta, newPoolBorrowIndex),
|
|
74
|
-
morphoAaveMath.indexMul(morphoMarketData.deltas.borrow.scaledP2PTotal, morphoMarketData.indexes.borrow.p2pIndex),
|
|
75
|
-
).toString(),
|
|
76
|
-
).toString();
|
|
77
|
-
|
|
78
|
-
const apys = morphoAaveMath.computeApysFromRates(
|
|
79
|
-
BigNumber.from(loanInfo.supplyRate),
|
|
80
|
-
BigNumber.from(loanInfo.borrowRateVariable),
|
|
81
|
-
BigNumber.from(morphoMarketData.p2pIndexCursor),
|
|
82
|
-
BigNumber.from(supplyProportionDelta),
|
|
83
|
-
BigNumber.from(borrowProportionDelta),
|
|
84
|
-
BigNumber.from(proportionIdle),
|
|
85
|
-
BigNumber.from(morphoMarketData.reserveFactor),
|
|
86
|
-
);
|
|
87
|
-
|
|
88
|
-
const { newP2PSupplyIndex, newP2PBorrowIndex } = p2pInterestRates.computeP2PIndexes({
|
|
89
|
-
p2pIndexCursor: BigNumber.from(morphoMarketData.p2pIndexCursor),
|
|
90
|
-
lastBorrowIndexes: {
|
|
91
|
-
p2pIndex: BigNumber.from(morphoMarketData.indexes.borrow.p2pIndex),
|
|
92
|
-
poolIndex: BigNumber.from(aaveIndexes.variableBorrowIndex),
|
|
93
|
-
},
|
|
94
|
-
lastSupplyIndexes: {
|
|
95
|
-
p2pIndex: BigNumber.from(morphoMarketData.indexes.supply.p2pIndex),
|
|
96
|
-
poolIndex: BigNumber.from(aaveIndexes.liquidityIndex),
|
|
97
|
-
},
|
|
98
|
-
poolSupplyIndex: BigNumber.from(newPoolSupplyIndex),
|
|
99
|
-
poolBorrowIndex: BigNumber.from(newPoolBorrowIndex),
|
|
100
|
-
deltas: {
|
|
101
|
-
borrow: {
|
|
102
|
-
scaledDelta: BigNumber.from(morphoMarketData.deltas.borrow.scaledDelta),
|
|
103
|
-
scaledP2PTotal: BigNumber.from(morphoMarketData.deltas.borrow.scaledP2PTotal),
|
|
104
|
-
},
|
|
105
|
-
supply: {
|
|
106
|
-
scaledDelta: BigNumber.from(morphoMarketData.deltas.supply.scaledDelta),
|
|
107
|
-
scaledP2PTotal: BigNumber.from(morphoMarketData.deltas.supply.scaledP2PTotal),
|
|
108
|
-
},
|
|
109
|
-
},
|
|
110
|
-
reserveFactor: BigNumber.from(morphoMarketData.reserveFactor),
|
|
111
|
-
proportionIdle: BigNumber.from(proportionIdle),
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
return {
|
|
115
|
-
...loanInfo,
|
|
116
|
-
...morphoMarketData,
|
|
117
|
-
...aaveIndexes,
|
|
118
|
-
p2pBorrowAPY: new Dec(apys.p2pBorrowAPY.toString()).div(100).toString(),
|
|
119
|
-
p2pSupplyAPY: new Dec(apys.p2pSupplyAPY.toString()).div(100).toString(),
|
|
120
|
-
morphoBorrowInP2P: assetAmountInEth(morphoAaveMath.indexMul(
|
|
121
|
-
morphoMarketData.deltas.borrow.scaledP2PTotal,
|
|
122
|
-
newP2PBorrowIndex,
|
|
123
|
-
).toString(), getAssetInfoByAddress(loanInfo.underlyingTokenAddress).symbol),
|
|
124
|
-
morphoBorrowOnPool: assetAmountInEth(morphoAaveMath.indexMul(
|
|
125
|
-
morphoMarketData.scaledMorphoBorrowOnPool,
|
|
126
|
-
newPoolBorrowIndex,
|
|
127
|
-
).toString(), getAssetInfoByAddress(loanInfo.underlyingTokenAddress).symbol),
|
|
128
|
-
morphoSupplyInP2P: assetAmountInEth(morphoAaveMath.indexMul(
|
|
129
|
-
morphoMarketData.deltas.supply.scaledP2PTotal,
|
|
130
|
-
newP2PSupplyIndex,
|
|
131
|
-
).toString(), getAssetInfoByAddress(loanInfo.underlyingTokenAddress).symbol),
|
|
132
|
-
morphoSupplyOnPool: assetAmountInEth(morphoAaveMath.indexMul(
|
|
133
|
-
morphoMarketData.isCollateral ? '0' : morphoMarketData.scaledMorphoSupplyOnPool,
|
|
134
|
-
newPoolSupplyIndex,
|
|
135
|
-
).toString(), getAssetInfoByAddress(loanInfo.underlyingTokenAddress).symbol),
|
|
136
|
-
};
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
export const getMorphoAaveV3MarketsData = async (web3: Web3, network: NetworkNumber, selectedMarket: MorphoAaveV3MarketInfo, mainnetWeb3: Web3): Promise<MorphoAaveV3MarketData> => {
|
|
140
|
-
// @ts-ignore
|
|
141
|
-
const lendingPoolContract = createContractWrapper(web3, network, selectedMarket.lendingPool, selectedMarket.lendingPoolAddress);
|
|
142
|
-
|
|
143
|
-
const _addresses = selectedMarket.assets.map((a: string) => getAssetInfo(ethToWeth(a)).address);
|
|
144
|
-
|
|
145
|
-
const splitStart = Math.floor(_addresses.length / 2);
|
|
146
|
-
const loanInfoCallsToSkip = 2; // skipping getFullTokensInfo calls at the start of multicallArray
|
|
147
|
-
|
|
148
|
-
const AaveV3ViewAddress = getConfigContractAddress('AaveV3View', network);
|
|
149
|
-
const AaveV3ViewAbi = getConfigContractAbi('AaveV3View');
|
|
150
|
-
|
|
151
|
-
const multicallArray = [
|
|
152
|
-
{
|
|
153
|
-
target: AaveV3ViewAddress,
|
|
154
|
-
abiItem: getAbiItem(AaveV3ViewAbi, 'getFullTokensInfo'),
|
|
155
|
-
params: [selectedMarket.providerAddress, _addresses.slice(0, splitStart)],
|
|
156
|
-
},
|
|
157
|
-
{
|
|
158
|
-
target: AaveV3ViewAddress,
|
|
159
|
-
abiItem: getAbiItem(AaveV3ViewAbi, 'getFullTokensInfo'),
|
|
160
|
-
params: [selectedMarket.providerAddress, _addresses.slice(splitStart, _addresses.length)],
|
|
161
|
-
},
|
|
162
|
-
...(_addresses.map((underlyingAddress: string) => (
|
|
163
|
-
[{
|
|
164
|
-
target: lendingPoolContract.options.address,
|
|
165
|
-
abiItem: getAbiItem(lendingPoolContract.options.jsonInterface, 'market'),
|
|
166
|
-
params: [underlyingAddress],
|
|
167
|
-
},
|
|
168
|
-
{
|
|
169
|
-
target: selectedMarket.protocolDataAddress, // TODO: aave refactor add to Aave view
|
|
170
|
-
// @ts-ignore
|
|
171
|
-
abiItem: getAbiItem(getConfigContractAbi(selectedMarket.protocolData), 'getReserveData'),
|
|
172
|
-
params: [underlyingAddress],
|
|
173
|
-
}]
|
|
174
|
-
))).flat(),
|
|
175
|
-
];
|
|
176
|
-
|
|
177
|
-
const multicallResponse = await multicall(multicallArray, web3, network);
|
|
178
|
-
const loanInfo = [...multicallResponse[0][0], ...multicallResponse[1][0]];
|
|
179
|
-
|
|
180
|
-
const IVariableDebtTokenAbi = getConfigContractAbi('IVariableDebtToken');
|
|
181
|
-
const IATokenAbi = getConfigContractAbi('IAToken');
|
|
182
|
-
|
|
183
|
-
const scaledBalanceMulticall = [
|
|
184
|
-
...loanInfo.map((_, i: number) => [
|
|
185
|
-
{
|
|
186
|
-
target: multicallResponse[(2 * i) + loanInfoCallsToSkip][0].variableDebtToken, // TODO: aave refactor add to Aave view
|
|
187
|
-
abiItem: getAbiItem(IVariableDebtTokenAbi, 'scaledBalanceOf'),
|
|
188
|
-
params: [lendingPoolContract.options.address],
|
|
189
|
-
},
|
|
190
|
-
{
|
|
191
|
-
target: loanInfo[i].aTokenAddress, // TODO: aave refactor add to Aave view
|
|
192
|
-
abiItem: getAbiItem(IATokenAbi, 'scaledBalanceOf'),
|
|
193
|
-
params: [lendingPoolContract.options.address],
|
|
194
|
-
},
|
|
195
|
-
]).flat(),
|
|
196
|
-
];
|
|
197
|
-
|
|
198
|
-
const [scaledBalanceResponse, morphoRewards] = await Promise.allSettled([
|
|
199
|
-
multicall(scaledBalanceMulticall, web3, network),
|
|
200
|
-
fetch('https://api.morpho.xyz/rewards/emissions'),
|
|
201
|
-
]);
|
|
202
|
-
|
|
203
|
-
if (scaledBalanceResponse.status !== 'fulfilled') {
|
|
204
|
-
throw new Error('Failed to fetch market data.');
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
const morphoRewardsData = morphoRewards.status === 'fulfilled' ? await morphoRewards.value.json() : null;
|
|
208
|
-
|
|
209
|
-
const assetsData: MorphoAaveV3AssetData[] = await Promise.all(loanInfo.map(async (info, i: number) => {
|
|
210
|
-
const morphoMarketData = {
|
|
211
|
-
...multicallResponse[(2 * i) + loanInfoCallsToSkip][0],
|
|
212
|
-
scaledMorphoBorrowOnPool: scaledBalanceResponse.value[2 * i][0],
|
|
213
|
-
scaledMorphoSupplyOnPool: scaledBalanceResponse.value[(2 * i) + 1][0],
|
|
214
|
-
};
|
|
215
|
-
const marketData = computeMorphoMarketData(
|
|
216
|
-
info,
|
|
217
|
-
morphoMarketData,
|
|
218
|
-
multicallResponse[(2 * i) + (loanInfoCallsToSkip + 1)],
|
|
219
|
-
);
|
|
220
|
-
|
|
221
|
-
const { symbol, address } = getAssetInfoByAddress(wethToEthByAddress(marketData.underlyingTokenAddress));
|
|
222
|
-
|
|
223
|
-
const data = {
|
|
224
|
-
symbol,
|
|
225
|
-
morphoMarketData,
|
|
226
|
-
hasDelta: new Dec(marketData.p2pSupplyAPY).minus(marketData.p2pBorrowAPY).gte(0.3),
|
|
227
|
-
eModeCategory: +marketData.emodeCategory,
|
|
228
|
-
aTokenAddress: marketData.aTokenAddress,
|
|
229
|
-
underlyingTokenAddress: address,
|
|
230
|
-
price: new Dec(marketData.price.toString()).div(1e8).toString(), // is actually price in USD
|
|
231
|
-
|
|
232
|
-
supplyRate: new Dec(marketData.supplyRate.toString()).div(1e25).toString(),
|
|
233
|
-
supplyRateP2P: marketData.p2pSupplyAPY,
|
|
234
|
-
borrowRate: new Dec(marketData.borrowRateVariable.toString()).div(1e25).toString(),
|
|
235
|
-
borrowRateP2P: marketData.p2pBorrowAPY,
|
|
236
|
-
totalSupply: assetAmountInEth(marketData.totalSupply.toString(), symbol),
|
|
237
|
-
totalBorrow: assetAmountInEth(marketData.totalBorrow.toString(), symbol),
|
|
238
|
-
|
|
239
|
-
totalSupplyP2P: marketData.morphoSupplyInP2P,
|
|
240
|
-
totalSupplyPool: marketData.morphoSupplyOnPool,
|
|
241
|
-
totalBorrowP2P: marketData.morphoBorrowInP2P,
|
|
242
|
-
totalBorrowPool: marketData.morphoBorrowOnPool,
|
|
243
|
-
|
|
244
|
-
supplyCap: marketData.supplyCap,
|
|
245
|
-
borrowCap: marketData.borrowCap,
|
|
246
|
-
usageAsCollateralEnabled: marketData.isCollateral,
|
|
247
|
-
collateralFactor: marketData.isCollateral ? new Dec(marketData.collateralFactor).div(10000).toString() : '0',
|
|
248
|
-
liquidationRatio: new Dec(marketData.liquidationRatio).div(10000).toString(),
|
|
249
|
-
isInactive: !marketData.isActive,
|
|
250
|
-
isFrozen: marketData.isFrozen,
|
|
251
|
-
isPaused: marketData.isPaused,
|
|
252
|
-
canBeBorrowed: !marketData.isFrozen
|
|
253
|
-
&& marketData.borrowingEnabled
|
|
254
|
-
&& !marketData.pauseStatuses.isBorrowPaused
|
|
255
|
-
&& !marketData.pauseStatuses.isDeprecated,
|
|
256
|
-
canBeSupplied: !marketData.isFrozen
|
|
257
|
-
&& marketData.isCollateral ? !marketData.pauseStatuses.isSupplyCollateralPaused : !marketData.pauseStatuses.isSupplyPaused
|
|
258
|
-
&& !marketData.pauseStatuses.isDeprecated,
|
|
259
|
-
canBeWithdrawn: marketData.isActive
|
|
260
|
-
&& !marketData.isPaused
|
|
261
|
-
&& marketData.isCollateral ? !marketData.pauseStatuses.isWithdrawCollateralPaused : !marketData.pauseStatuses.isWithdrawPaused,
|
|
262
|
-
canBePayBacked: marketData.isActive && !marketData.isPaused && !marketData.pauseStatuses.isRepayPaused,
|
|
263
|
-
reserveFactor: new Dec(marketData.reserveFactor).toString(),
|
|
264
|
-
pauseStatus: {
|
|
265
|
-
isSupplyPaused: marketData.pauseStatuses.isSupplyPaused,
|
|
266
|
-
isSupplyCollateralPaused: marketData.pauseStatuses.isSupplyCollateralPaused,
|
|
267
|
-
isWithdrawPaused: marketData.pauseStatuses.isWithdrawPaused,
|
|
268
|
-
isWithdrawCollateralPaused: marketData.pauseStatuses.isWithdrawCollateralPaused,
|
|
269
|
-
isRepayPaused: marketData.pauseStatuses.isRepayPaused,
|
|
270
|
-
isBorrowPaused: marketData.pauseStatuses.isBorrowPaused,
|
|
271
|
-
isDeprecated: marketData.pauseStatuses.isDeprecated,
|
|
272
|
-
isP2PDisabled: marketData.pauseStatuses.isP2PDisabled,
|
|
273
|
-
},
|
|
274
|
-
marketLiquidity: assetAmountInEth(new Dec(marketData.totalSupply.toString())
|
|
275
|
-
.sub(marketData.totalBorrow.toString())
|
|
276
|
-
.toString(), symbol),
|
|
277
|
-
utilization: new Dec(marketData.totalBorrow.toString())
|
|
278
|
-
.div(new Dec(marketData.totalSupply.toString()))
|
|
279
|
-
.times(100)
|
|
280
|
-
.toString(),
|
|
281
|
-
|
|
282
|
-
eModeCategoryData: {
|
|
283
|
-
label: marketData.label,
|
|
284
|
-
liquidationBonus: new Dec(marketData.liquidationBonus).div(10000).toString(),
|
|
285
|
-
liquidationRatio: new Dec(marketData.liquidationThreshold).div(10000).toString(),
|
|
286
|
-
collateralFactor: new Dec(marketData.ltv).div(10000).toString(),
|
|
287
|
-
priceSource: marketData.priceSource,
|
|
288
|
-
},
|
|
289
|
-
|
|
290
|
-
incentiveSupplyToken: 'MORPHO',
|
|
291
|
-
incentiveBorrowToken: 'MORPHO',
|
|
292
|
-
incentiveSupplyApy: morphoRewardsData?.markets?.[marketData.underlyingTokenAddress?.toLowerCase()]?.morphoRatePerSecondSupplySide || '0',
|
|
293
|
-
incentiveBorrowApy: morphoRewardsData.markets?.[marketData.underlyingTokenAddress?.toLowerCase()]?.morphoRatePerSecondBorrowSide || '0',
|
|
294
|
-
|
|
295
|
-
totalBorrowVar: '0', // Morpho doesn't have all these, keeping it for compatability
|
|
296
|
-
borrowRateStable: '0',
|
|
297
|
-
disabledStableBorrowing: false,
|
|
298
|
-
isIsolated: false,
|
|
299
|
-
debtCeilingForIsolationMode: '0',
|
|
300
|
-
isSiloed: false,
|
|
301
|
-
isolationModeTotalDebt: '0',
|
|
302
|
-
assetId: null,
|
|
303
|
-
isolationModeBorrowingEnabled: false,
|
|
304
|
-
isFlashLoanEnabled: false,
|
|
305
|
-
};
|
|
306
|
-
|
|
307
|
-
if (symbol === 'wstETH') {
|
|
308
|
-
data.incentiveSupplyApy = await getStETHApr(mainnetWeb3);
|
|
309
|
-
data.incentiveSupplyToken = symbol;
|
|
310
|
-
}
|
|
311
|
-
if (symbol === 'cbETH' && !isLayer2Network(network)) {
|
|
312
|
-
data.incentiveSupplyApy = await getCbETHApr(mainnetWeb3);
|
|
313
|
-
data.incentiveSupplyToken = symbol;
|
|
314
|
-
}
|
|
315
|
-
if (symbol === 'rETH') {
|
|
316
|
-
data.incentiveSupplyApy = await getREthApr(mainnetWeb3);
|
|
317
|
-
data.incentiveSupplyToken = symbol;
|
|
318
|
-
}
|
|
319
|
-
if (data.symbol === 'sDAI') {
|
|
320
|
-
data.incentiveSupplyApy = await getDsrApy(web3, network);
|
|
321
|
-
data.incentiveSupplyToken = 'sDAI';
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
return data;
|
|
325
|
-
}));
|
|
326
|
-
|
|
327
|
-
const payload: MorphoAaveV3AssetsData = {};
|
|
328
|
-
// Sort by market size
|
|
329
|
-
assetsData
|
|
330
|
-
.sort((a, b) => {
|
|
331
|
-
const aMarket = new Dec(a.price).times(a.totalSupply).toString();
|
|
332
|
-
const bMarket = new Dec(b.price).times(b.totalSupply).toString();
|
|
333
|
-
|
|
334
|
-
return new Dec(bMarket).minus(aMarket).toNumber();
|
|
335
|
-
})
|
|
336
|
-
.forEach((assetData: MorphoAaveV3AssetData, i: number) => {
|
|
337
|
-
payload[assetData.symbol] = { ...assetData, sortIndex: i };
|
|
338
|
-
});
|
|
339
|
-
|
|
340
|
-
return { assetsData: payload };
|
|
341
|
-
};
|
|
342
|
-
|
|
343
|
-
export const getMorphoAaveV3AccountBalances = async (web3: Web3, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress): Promise<PositionBalances> => {
|
|
344
|
-
let balances: PositionBalances = {
|
|
345
|
-
collateral: {},
|
|
346
|
-
debt: {},
|
|
347
|
-
};
|
|
348
|
-
|
|
349
|
-
if (!address) {
|
|
350
|
-
return balances;
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
const selectedMarket = MORPHO_AAVE_V3_ETH(network);
|
|
354
|
-
// @ts-ignore
|
|
355
|
-
const lendingPoolContract = createContractWrapper(web3, network, selectedMarket.lendingPool, selectedMarket.lendingPoolAddress);
|
|
356
|
-
// @ts-ignore
|
|
357
|
-
const protocolDataProviderContract = createContractWrapper(web3, network, selectedMarket.protocolData, selectedMarket.protocolDataAddress);
|
|
358
|
-
|
|
359
|
-
const reserveTokens = await protocolDataProviderContract.methods.getAllReservesTokens().call({}, block);
|
|
360
|
-
const symbols = reserveTokens.map(({ symbol }: { symbol: string }) => symbol);
|
|
361
|
-
const _addresses = reserveTokens.map(({ tokenAddress }: { tokenAddress: EthAddress }) => tokenAddress);
|
|
362
|
-
|
|
363
|
-
const multicallArray = [
|
|
364
|
-
...(_addresses.map((underlyingAddress: string) => ([
|
|
365
|
-
{
|
|
366
|
-
target: lendingPoolContract.options.address,
|
|
367
|
-
abiItem: getAbiItem(lendingPoolContract.options.jsonInterface, 'market'),
|
|
368
|
-
params: [underlyingAddress],
|
|
369
|
-
},
|
|
370
|
-
{
|
|
371
|
-
target: lendingPoolContract.options.address,
|
|
372
|
-
abiItem: getAbiItem(lendingPoolContract.options.jsonInterface, 'scaledP2PSupplyBalance'),
|
|
373
|
-
params: [underlyingAddress, address],
|
|
374
|
-
},
|
|
375
|
-
{
|
|
376
|
-
target: lendingPoolContract.options.address,
|
|
377
|
-
abiItem: getAbiItem(lendingPoolContract.options.jsonInterface, 'scaledPoolSupplyBalance'),
|
|
378
|
-
params: [underlyingAddress, address],
|
|
379
|
-
},
|
|
380
|
-
{
|
|
381
|
-
target: lendingPoolContract.options.address,
|
|
382
|
-
abiItem: getAbiItem(lendingPoolContract.options.jsonInterface, 'scaledCollateralBalance'),
|
|
383
|
-
params: [underlyingAddress, address],
|
|
384
|
-
},
|
|
385
|
-
{
|
|
386
|
-
target: lendingPoolContract.options.address,
|
|
387
|
-
abiItem: getAbiItem(lendingPoolContract.options.jsonInterface, 'scaledP2PBorrowBalance'),
|
|
388
|
-
params: [underlyingAddress, address],
|
|
389
|
-
},
|
|
390
|
-
{
|
|
391
|
-
target: lendingPoolContract.options.address,
|
|
392
|
-
abiItem: getAbiItem(lendingPoolContract.options.jsonInterface, 'scaledPoolBorrowBalance'),
|
|
393
|
-
params: [underlyingAddress, address],
|
|
394
|
-
},
|
|
395
|
-
]))).flat(),
|
|
396
|
-
];
|
|
397
|
-
|
|
398
|
-
const multicallResponse = await multicall(multicallArray, web3, network, block);
|
|
399
|
-
|
|
400
|
-
const numberOfMultiCalls = 6;
|
|
401
|
-
|
|
402
|
-
_addresses.forEach((underlyingAddr: string, i: number) => {
|
|
403
|
-
const currentMulticallIndex = numberOfMultiCalls * i;
|
|
404
|
-
const morphoMarketData = multicallResponse[currentMulticallIndex][0];
|
|
405
|
-
const assetAddr = wethToEthByAddress(underlyingAddr, network).toLowerCase();
|
|
406
|
-
const { symbol } = getAssetInfoByAddress(assetAddr, network);
|
|
407
|
-
|
|
408
|
-
const suppliedP2P = morphoAaveMath.indexMul(
|
|
409
|
-
multicallResponse[currentMulticallIndex + 1][0],
|
|
410
|
-
morphoMarketData.indexes.supply.p2pIndex,
|
|
411
|
-
).toString();
|
|
412
|
-
const suppliedPool = morphoAaveMath.indexMul(
|
|
413
|
-
multicallResponse[currentMulticallIndex + 2][0],
|
|
414
|
-
morphoMarketData.indexes.supply.poolIndex,
|
|
415
|
-
).toString();
|
|
416
|
-
const suppliedTotal = new Dec(suppliedP2P).add(suppliedPool).toString();
|
|
417
|
-
const suppliedCollateral = morphoAaveMath.indexMul(
|
|
418
|
-
multicallResponse[currentMulticallIndex + 3][0],
|
|
419
|
-
morphoMarketData.indexes.supply.poolIndex,
|
|
420
|
-
).toString();
|
|
421
|
-
const supplied = new Dec(suppliedTotal).add(suppliedCollateral).toString();
|
|
422
|
-
|
|
423
|
-
const borrowedP2P = morphoAaveMath.indexMul(
|
|
424
|
-
multicallResponse[currentMulticallIndex + 4][0],
|
|
425
|
-
morphoMarketData.indexes.borrow.p2pIndex,
|
|
426
|
-
).toString();
|
|
427
|
-
const borrowedPool = morphoAaveMath.indexMul(
|
|
428
|
-
multicallResponse[currentMulticallIndex + 5][0],
|
|
429
|
-
morphoMarketData.indexes.borrow.poolIndex,
|
|
430
|
-
).toString();
|
|
431
|
-
const borrowed = new Dec(borrowedP2P).add(borrowedPool).toString();
|
|
432
|
-
|
|
433
|
-
balances = {
|
|
434
|
-
collateral: {
|
|
435
|
-
...balances.collateral,
|
|
436
|
-
[addressMapping ? assetAddr : symbol]: supplied,
|
|
437
|
-
},
|
|
438
|
-
debt: {
|
|
439
|
-
...balances.debt,
|
|
440
|
-
[addressMapping ? assetAddr : symbol]: borrowed,
|
|
441
|
-
},
|
|
442
|
-
};
|
|
443
|
-
});
|
|
444
|
-
|
|
445
|
-
return balances;
|
|
446
|
-
};
|
|
447
|
-
|
|
448
|
-
export const getMorphoAaveV3AccountData = async (
|
|
449
|
-
web3: Web3,
|
|
450
|
-
network: NetworkNumber,
|
|
451
|
-
address: string,
|
|
452
|
-
assetsData: MorphoAaveV3AssetsData,
|
|
453
|
-
delegator: string,
|
|
454
|
-
selectedMarket: MorphoAaveV3MarketInfo,
|
|
455
|
-
): Promise<MorphoAaveV3PositionData> => {
|
|
456
|
-
if (!address) {
|
|
457
|
-
throw new Error('No address provided.');
|
|
458
|
-
}
|
|
459
|
-
const eModeCategory = 1; // TODO: morpho v3 pass as arg
|
|
460
|
-
|
|
461
|
-
let payload: MorphoAaveV3PositionData = {
|
|
462
|
-
...EMPTY_AAVE_DATA,
|
|
463
|
-
usedAssets: {}, // Typescript is bugging out due to JSDocs version of AavePositionData.UsedAssets
|
|
464
|
-
eModeCategory,
|
|
465
|
-
minRatio: '100',
|
|
466
|
-
lastUpdated: Date.now(),
|
|
467
|
-
};
|
|
468
|
-
|
|
469
|
-
// @ts-ignore
|
|
470
|
-
const lendingPoolContract = createContractWrapper(web3, network, selectedMarket.lendingPool, selectedMarket.lendingPoolAddress);
|
|
471
|
-
|
|
472
|
-
const isManagedBy = delegator && lendingPoolContract?.methods?.isManagedBy
|
|
473
|
-
? await lendingPoolContract.methods.isManagedBy(address, delegator).call()
|
|
474
|
-
: null;
|
|
475
|
-
payload.approvedManager = isManagedBy ? delegator : '';
|
|
476
|
-
|
|
477
|
-
const markets = Object.values(assetsData);
|
|
478
|
-
// @ts-ignore
|
|
479
|
-
const marketAddresses = markets.map(m => ethToWethByAddress(m.underlyingTokenAddress));
|
|
480
|
-
|
|
481
|
-
const multicallArray = [
|
|
482
|
-
...(marketAddresses.map((marketAddr) => [
|
|
483
|
-
{
|
|
484
|
-
target: lendingPoolContract.options.address,
|
|
485
|
-
abiItem: getAbiItem(lendingPoolContract.options.jsonInterface, 'scaledP2PSupplyBalance'),
|
|
486
|
-
params: [marketAddr, address],
|
|
487
|
-
},
|
|
488
|
-
{
|
|
489
|
-
target: lendingPoolContract.options.address,
|
|
490
|
-
abiItem: getAbiItem(lendingPoolContract.options.jsonInterface, 'scaledPoolSupplyBalance'),
|
|
491
|
-
params: [marketAddr, address],
|
|
492
|
-
},
|
|
493
|
-
{
|
|
494
|
-
target: lendingPoolContract.options.address,
|
|
495
|
-
abiItem: getAbiItem(lendingPoolContract.options.jsonInterface, 'scaledCollateralBalance'),
|
|
496
|
-
params: [marketAddr, address],
|
|
497
|
-
},
|
|
498
|
-
{
|
|
499
|
-
target: lendingPoolContract.options.address,
|
|
500
|
-
abiItem: getAbiItem(lendingPoolContract.options.jsonInterface, 'scaledP2PBorrowBalance'),
|
|
501
|
-
params: [marketAddr, address],
|
|
502
|
-
},
|
|
503
|
-
{
|
|
504
|
-
target: lendingPoolContract.options.address,
|
|
505
|
-
abiItem: getAbiItem(lendingPoolContract.options.jsonInterface, 'scaledPoolBorrowBalance'),
|
|
506
|
-
params: [marketAddr, address],
|
|
507
|
-
},
|
|
508
|
-
]).flat()),
|
|
509
|
-
];
|
|
510
|
-
|
|
511
|
-
const multicallResponse = await multicall(multicallArray, web3, network);
|
|
512
|
-
|
|
513
|
-
markets.forEach((market: any, i: number) => {
|
|
514
|
-
const { symbol } = getAssetInfoByAddress(wethToEthByAddress(market.underlyingTokenAddress));
|
|
515
|
-
const assetAavePrice = assetsData[symbol].price;
|
|
516
|
-
|
|
517
|
-
const suppliedP2P = assetAmountInEth(morphoAaveMath.indexMul(
|
|
518
|
-
multicallResponse[i * 5][0],
|
|
519
|
-
market.morphoMarketData.indexes.supply.p2pIndex,
|
|
520
|
-
), symbol);
|
|
521
|
-
const suppliedPool = assetAmountInEth(morphoAaveMath.indexMul(
|
|
522
|
-
multicallResponse[(i * 5) + 1][0],
|
|
523
|
-
market.morphoMarketData.indexes.supply.poolIndex,
|
|
524
|
-
), symbol);
|
|
525
|
-
const suppliedTotal = new Dec(suppliedP2P).add(suppliedPool).toString();
|
|
526
|
-
const suppliedCollateral = assetAmountInEth(morphoAaveMath.indexMul(
|
|
527
|
-
multicallResponse[(i * 5) + 2][0],
|
|
528
|
-
market.morphoMarketData.indexes.supply.poolIndex,
|
|
529
|
-
), symbol);
|
|
530
|
-
const supplied = new Dec(suppliedTotal).add(suppliedCollateral).toString();
|
|
531
|
-
const suppliedMatched = new Dec(suppliedTotal).eq(0)
|
|
532
|
-
? '0'
|
|
533
|
-
: morphoAaveMath.percentDiv(
|
|
534
|
-
assetAmountInWei(suppliedP2P, symbol),
|
|
535
|
-
assetAmountInWei(suppliedTotal, symbol),
|
|
536
|
-
).div(100).toString();
|
|
537
|
-
|
|
538
|
-
const borrowedP2P = assetAmountInEth(morphoAaveMath.indexMul(
|
|
539
|
-
multicallResponse[(i * 5) + 3][0],
|
|
540
|
-
market.morphoMarketData.indexes.borrow.p2pIndex,
|
|
541
|
-
), symbol);
|
|
542
|
-
const borrowedPool = assetAmountInEth(morphoAaveMath.indexMul(
|
|
543
|
-
multicallResponse[(i * 5) + 4][0],
|
|
544
|
-
market.morphoMarketData.indexes.borrow.poolIndex,
|
|
545
|
-
), symbol);
|
|
546
|
-
const borrowed = new Dec(borrowedP2P).add(borrowedPool).toString();
|
|
547
|
-
const borrowedMatched = new Dec(borrowed).eq(0)
|
|
548
|
-
? '0'
|
|
549
|
-
: morphoAaveMath.percentDiv(
|
|
550
|
-
assetAmountInWei(borrowedP2P, symbol),
|
|
551
|
-
assetAmountInWei(borrowed, symbol),
|
|
552
|
-
).div(100).toString();
|
|
553
|
-
|
|
554
|
-
const supplyRate = new Dec(new Dec(market.supplyRateP2P).mul(suppliedMatched))
|
|
555
|
-
.add(new Dec(market.supplyRate).mul(100 - +suppliedMatched)).div(100).toString();
|
|
556
|
-
const borrowRate = new Dec(new Dec(market.borrowRateP2P).mul(borrowedMatched))
|
|
557
|
-
.add(new Dec(market.borrowRate).mul(100 - +borrowedMatched)).div(100).toString();
|
|
558
|
-
|
|
559
|
-
if (new Dec(supplied).gt(0) || new Dec(borrowed).gt(0)) {
|
|
560
|
-
payload.usedAssets[symbol] = {
|
|
561
|
-
symbol,
|
|
562
|
-
eModeCategory: market.eModeCategory,
|
|
563
|
-
supplied,
|
|
564
|
-
suppliedP2P,
|
|
565
|
-
suppliedPool,
|
|
566
|
-
suppliedMatched,
|
|
567
|
-
borrowed,
|
|
568
|
-
borrowedP2P,
|
|
569
|
-
borrowedPool,
|
|
570
|
-
borrowedMatched,
|
|
571
|
-
supplyRate,
|
|
572
|
-
borrowRate,
|
|
573
|
-
suppliedUsd: new Dec(supplied).mul(assetAavePrice).toString(),
|
|
574
|
-
suppliedP2PUsd: new Dec(suppliedP2P).mul(assetAavePrice).toString(),
|
|
575
|
-
suppliedPoolUsd: new Dec(suppliedPool).mul(assetAavePrice).toString(),
|
|
576
|
-
borrowedUsd: new Dec(borrowed).mul(assetAavePrice).toString(),
|
|
577
|
-
borrowedP2PUsd: new Dec(borrowedP2P).mul(assetAavePrice).toString(),
|
|
578
|
-
borrowedPoolUsd: new Dec(borrowedPool).mul(assetAavePrice).toString(),
|
|
579
|
-
borrowedVariable: borrowed,
|
|
580
|
-
borrowedUsdVariable: new Dec(borrowed).mul(assetAavePrice).toString(),
|
|
581
|
-
collateral: new Dec(suppliedCollateral).gt(0),
|
|
582
|
-
isSupplied: new Dec(supplied).gt(0),
|
|
583
|
-
isBorrowed: new Dec(borrowed).gt(0),
|
|
584
|
-
// supplyRate: new Dec(market.experiencedSupplyAPY._hex).div(100).toString(),
|
|
585
|
-
// borrowRate: new Dec(market.experiencedBorrowAPY._hex).div(100).toString(),
|
|
586
|
-
limit: '0',
|
|
587
|
-
|
|
588
|
-
interestMode: '', // Morpho doesn't have all these, keeping it for compatability
|
|
589
|
-
stableBorrowRate: '0',
|
|
590
|
-
borrowedStable: '0',
|
|
591
|
-
borrowedUsdStable: '0',
|
|
592
|
-
stableLimit: '0',
|
|
593
|
-
variableLimit: '0',
|
|
594
|
-
};
|
|
595
|
-
}
|
|
596
|
-
});
|
|
597
|
-
|
|
598
|
-
payload = {
|
|
599
|
-
...payload,
|
|
600
|
-
...aaveAnyGetAggregatedPositionData({
|
|
601
|
-
usedAssets: payload.usedAssets, assetsData, eModeCategory, selectedMarket,
|
|
602
|
-
}),
|
|
603
|
-
};
|
|
604
|
-
|
|
605
|
-
// Calculate borrow limits per asset
|
|
606
|
-
Object.values(payload.usedAssets).forEach((item) => {
|
|
607
|
-
if (item.isBorrowed) {
|
|
608
|
-
// eslint-disable-next-line no-param-reassign
|
|
609
|
-
item.limit = calculateBorrowingAssetLimit(item.borrowedUsd, payload.borrowLimitUsd);
|
|
610
|
-
}
|
|
611
|
-
});
|
|
612
|
-
|
|
613
|
-
return payload;
|
|
614
|
-
};
|
|
615
|
-
|
|
616
|
-
export const getMorphoAaveV3FullPositionData = async (web3: Web3, network: NetworkNumber, address: string, delegator: string, market: MorphoAaveV3MarketInfo, mainnetWeb3: Web3): Promise<MorphoAaveV3PositionData> => {
|
|
617
|
-
const marketData = await getMorphoAaveV3MarketsData(web3, network, market, mainnetWeb3);
|
|
618
|
-
const positionData = await getMorphoAaveV3AccountData(web3, network, address, marketData.assetsData, delegator, market);
|
|
619
|
-
return positionData;
|
|
1
|
+
import {
|
|
2
|
+
assetAmountInEth, assetAmountInWei, getAssetInfo, getAssetInfoByAddress,
|
|
3
|
+
} from '@defisaver/tokens';
|
|
4
|
+
import { MorphoAaveMath } from '@morpho-org/morpho-aave-v3-sdk/lib/maths/AaveV3.maths';
|
|
5
|
+
import PoolInterestRates from '@morpho-org/morpho-aave-v3-sdk/lib/maths/PoolInterestRates';
|
|
6
|
+
import P2PInterestRates from '@morpho-org/morpho-aave-v3-sdk/lib/maths/P2PInterestRates';
|
|
7
|
+
import { BigNumber } from '@ethersproject/bignumber';
|
|
8
|
+
import Web3 from 'web3';
|
|
9
|
+
import Dec from 'decimal.js';
|
|
10
|
+
import {
|
|
11
|
+
Blockish, EthAddress, NetworkNumber, PositionBalances,
|
|
12
|
+
} from '../types/common';
|
|
13
|
+
import {
|
|
14
|
+
ethToWeth, ethToWethByAddress, getAbiItem, isLayer2Network, wethToEthByAddress,
|
|
15
|
+
} from '../services/utils';
|
|
16
|
+
import {
|
|
17
|
+
createContractWrapper,
|
|
18
|
+
getConfigContractAbi,
|
|
19
|
+
getConfigContractAddress,
|
|
20
|
+
} from '../contracts';
|
|
21
|
+
import { multicall } from '../multicall';
|
|
22
|
+
import { getCbETHApr, getREthApr, getStETHApr } from '../staking';
|
|
23
|
+
import {
|
|
24
|
+
MorphoAaveV3AssetData, MorphoAaveV3AssetsData, MorphoAaveV3MarketData, MorphoAaveV3MarketInfo, MorphoAaveV3PositionData,
|
|
25
|
+
} from '../types';
|
|
26
|
+
import { getDsrApy } from '../services/dsrService';
|
|
27
|
+
import { calculateBorrowingAssetLimit } from '../moneymarket';
|
|
28
|
+
import { EMPTY_AAVE_DATA } from '../aaveV3';
|
|
29
|
+
import { aaveAnyGetAggregatedPositionData } from '../helpers/aaveHelpers';
|
|
30
|
+
import { MORPHO_AAVE_V3_ETH } from '../markets/aave';
|
|
31
|
+
|
|
32
|
+
const morphoAaveMath = new MorphoAaveMath();
|
|
33
|
+
const poolInterestRates = new PoolInterestRates();
|
|
34
|
+
const p2pInterestRates = new P2PInterestRates();
|
|
35
|
+
|
|
36
|
+
const computeMorphoMarketData = (
|
|
37
|
+
loanInfo: any, morphoMarketData: any, aaveIndexes: any, // TODO: morpho v3 type
|
|
38
|
+
) => {
|
|
39
|
+
const { newPoolSupplyIndex, newPoolBorrowIndex } = poolInterestRates.computePoolIndexes({
|
|
40
|
+
lastPoolSupplyIndex: BigNumber.from(aaveIndexes.liquidityIndex),
|
|
41
|
+
lastPoolBorrowIndex: BigNumber.from(aaveIndexes.variableBorrowIndex),
|
|
42
|
+
lastUpdateTimestamp: BigNumber.from(aaveIndexes.lastUpdateTimestamp),
|
|
43
|
+
poolBorrowRatePerYear: BigNumber.from(loanInfo.borrowRateVariable),
|
|
44
|
+
poolSupplyRatePerYear: BigNumber.from(loanInfo.supplyRate),
|
|
45
|
+
currentTimestamp: BigNumber.from(new Dec(Date.now()).div(1000).toDP(0).toString()),
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const proportionIdle = new Dec(morphoMarketData.idleSupply).eq(0)
|
|
49
|
+
? '0'
|
|
50
|
+
: Dec.min(
|
|
51
|
+
morphoAaveMath.INDEX_ONE.toString(),
|
|
52
|
+
morphoAaveMath.indexDiv(
|
|
53
|
+
morphoMarketData.idleSupply,
|
|
54
|
+
morphoAaveMath.indexMul(morphoMarketData.deltas.supply.scaledP2PTotal, morphoMarketData.indexes.supply.p2pIndex).toString(),
|
|
55
|
+
).toString(),
|
|
56
|
+
).toString();
|
|
57
|
+
|
|
58
|
+
const supplyProportionDelta = new Dec(morphoMarketData.idleSupply).eq(0)
|
|
59
|
+
? '0'
|
|
60
|
+
: Dec.min(
|
|
61
|
+
new Dec(morphoAaveMath.INDEX_ONE.toString()).sub(proportionIdle).toString(),
|
|
62
|
+
morphoAaveMath.indexDiv(
|
|
63
|
+
morphoAaveMath.indexMul(morphoMarketData.deltas.supply.scaledDelta, newPoolSupplyIndex),
|
|
64
|
+
morphoAaveMath.indexMul(morphoMarketData.deltas.supply.scaledP2PTotal, morphoMarketData.indexes.supply.p2pIndex),
|
|
65
|
+
).toString(),
|
|
66
|
+
).toString();
|
|
67
|
+
|
|
68
|
+
const borrowProportionDelta = new Dec(morphoMarketData.idleSupply).eq(0)
|
|
69
|
+
? '0'
|
|
70
|
+
: Dec.min(
|
|
71
|
+
morphoAaveMath.INDEX_ONE.toString(),
|
|
72
|
+
morphoAaveMath.indexDiv(
|
|
73
|
+
morphoAaveMath.indexMul(morphoMarketData.deltas.borrow.scaledDelta, newPoolBorrowIndex),
|
|
74
|
+
morphoAaveMath.indexMul(morphoMarketData.deltas.borrow.scaledP2PTotal, morphoMarketData.indexes.borrow.p2pIndex),
|
|
75
|
+
).toString(),
|
|
76
|
+
).toString();
|
|
77
|
+
|
|
78
|
+
const apys = morphoAaveMath.computeApysFromRates(
|
|
79
|
+
BigNumber.from(loanInfo.supplyRate),
|
|
80
|
+
BigNumber.from(loanInfo.borrowRateVariable),
|
|
81
|
+
BigNumber.from(morphoMarketData.p2pIndexCursor),
|
|
82
|
+
BigNumber.from(supplyProportionDelta),
|
|
83
|
+
BigNumber.from(borrowProportionDelta),
|
|
84
|
+
BigNumber.from(proportionIdle),
|
|
85
|
+
BigNumber.from(morphoMarketData.reserveFactor),
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
const { newP2PSupplyIndex, newP2PBorrowIndex } = p2pInterestRates.computeP2PIndexes({
|
|
89
|
+
p2pIndexCursor: BigNumber.from(morphoMarketData.p2pIndexCursor),
|
|
90
|
+
lastBorrowIndexes: {
|
|
91
|
+
p2pIndex: BigNumber.from(morphoMarketData.indexes.borrow.p2pIndex),
|
|
92
|
+
poolIndex: BigNumber.from(aaveIndexes.variableBorrowIndex),
|
|
93
|
+
},
|
|
94
|
+
lastSupplyIndexes: {
|
|
95
|
+
p2pIndex: BigNumber.from(morphoMarketData.indexes.supply.p2pIndex),
|
|
96
|
+
poolIndex: BigNumber.from(aaveIndexes.liquidityIndex),
|
|
97
|
+
},
|
|
98
|
+
poolSupplyIndex: BigNumber.from(newPoolSupplyIndex),
|
|
99
|
+
poolBorrowIndex: BigNumber.from(newPoolBorrowIndex),
|
|
100
|
+
deltas: {
|
|
101
|
+
borrow: {
|
|
102
|
+
scaledDelta: BigNumber.from(morphoMarketData.deltas.borrow.scaledDelta),
|
|
103
|
+
scaledP2PTotal: BigNumber.from(morphoMarketData.deltas.borrow.scaledP2PTotal),
|
|
104
|
+
},
|
|
105
|
+
supply: {
|
|
106
|
+
scaledDelta: BigNumber.from(morphoMarketData.deltas.supply.scaledDelta),
|
|
107
|
+
scaledP2PTotal: BigNumber.from(morphoMarketData.deltas.supply.scaledP2PTotal),
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
reserveFactor: BigNumber.from(morphoMarketData.reserveFactor),
|
|
111
|
+
proportionIdle: BigNumber.from(proportionIdle),
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
return {
|
|
115
|
+
...loanInfo,
|
|
116
|
+
...morphoMarketData,
|
|
117
|
+
...aaveIndexes,
|
|
118
|
+
p2pBorrowAPY: new Dec(apys.p2pBorrowAPY.toString()).div(100).toString(),
|
|
119
|
+
p2pSupplyAPY: new Dec(apys.p2pSupplyAPY.toString()).div(100).toString(),
|
|
120
|
+
morphoBorrowInP2P: assetAmountInEth(morphoAaveMath.indexMul(
|
|
121
|
+
morphoMarketData.deltas.borrow.scaledP2PTotal,
|
|
122
|
+
newP2PBorrowIndex,
|
|
123
|
+
).toString(), getAssetInfoByAddress(loanInfo.underlyingTokenAddress).symbol),
|
|
124
|
+
morphoBorrowOnPool: assetAmountInEth(morphoAaveMath.indexMul(
|
|
125
|
+
morphoMarketData.scaledMorphoBorrowOnPool,
|
|
126
|
+
newPoolBorrowIndex,
|
|
127
|
+
).toString(), getAssetInfoByAddress(loanInfo.underlyingTokenAddress).symbol),
|
|
128
|
+
morphoSupplyInP2P: assetAmountInEth(morphoAaveMath.indexMul(
|
|
129
|
+
morphoMarketData.deltas.supply.scaledP2PTotal,
|
|
130
|
+
newP2PSupplyIndex,
|
|
131
|
+
).toString(), getAssetInfoByAddress(loanInfo.underlyingTokenAddress).symbol),
|
|
132
|
+
morphoSupplyOnPool: assetAmountInEth(morphoAaveMath.indexMul(
|
|
133
|
+
morphoMarketData.isCollateral ? '0' : morphoMarketData.scaledMorphoSupplyOnPool,
|
|
134
|
+
newPoolSupplyIndex,
|
|
135
|
+
).toString(), getAssetInfoByAddress(loanInfo.underlyingTokenAddress).symbol),
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
export const getMorphoAaveV3MarketsData = async (web3: Web3, network: NetworkNumber, selectedMarket: MorphoAaveV3MarketInfo, mainnetWeb3: Web3): Promise<MorphoAaveV3MarketData> => {
|
|
140
|
+
// @ts-ignore
|
|
141
|
+
const lendingPoolContract = createContractWrapper(web3, network, selectedMarket.lendingPool, selectedMarket.lendingPoolAddress);
|
|
142
|
+
|
|
143
|
+
const _addresses = selectedMarket.assets.map((a: string) => getAssetInfo(ethToWeth(a)).address);
|
|
144
|
+
|
|
145
|
+
const splitStart = Math.floor(_addresses.length / 2);
|
|
146
|
+
const loanInfoCallsToSkip = 2; // skipping getFullTokensInfo calls at the start of multicallArray
|
|
147
|
+
|
|
148
|
+
const AaveV3ViewAddress = getConfigContractAddress('AaveV3View', network);
|
|
149
|
+
const AaveV3ViewAbi = getConfigContractAbi('AaveV3View');
|
|
150
|
+
|
|
151
|
+
const multicallArray = [
|
|
152
|
+
{
|
|
153
|
+
target: AaveV3ViewAddress,
|
|
154
|
+
abiItem: getAbiItem(AaveV3ViewAbi, 'getFullTokensInfo'),
|
|
155
|
+
params: [selectedMarket.providerAddress, _addresses.slice(0, splitStart)],
|
|
156
|
+
},
|
|
157
|
+
{
|
|
158
|
+
target: AaveV3ViewAddress,
|
|
159
|
+
abiItem: getAbiItem(AaveV3ViewAbi, 'getFullTokensInfo'),
|
|
160
|
+
params: [selectedMarket.providerAddress, _addresses.slice(splitStart, _addresses.length)],
|
|
161
|
+
},
|
|
162
|
+
...(_addresses.map((underlyingAddress: string) => (
|
|
163
|
+
[{
|
|
164
|
+
target: lendingPoolContract.options.address,
|
|
165
|
+
abiItem: getAbiItem(lendingPoolContract.options.jsonInterface, 'market'),
|
|
166
|
+
params: [underlyingAddress],
|
|
167
|
+
},
|
|
168
|
+
{
|
|
169
|
+
target: selectedMarket.protocolDataAddress, // TODO: aave refactor add to Aave view
|
|
170
|
+
// @ts-ignore
|
|
171
|
+
abiItem: getAbiItem(getConfigContractAbi(selectedMarket.protocolData), 'getReserveData'),
|
|
172
|
+
params: [underlyingAddress],
|
|
173
|
+
}]
|
|
174
|
+
))).flat(),
|
|
175
|
+
];
|
|
176
|
+
|
|
177
|
+
const multicallResponse = await multicall(multicallArray, web3, network);
|
|
178
|
+
const loanInfo = [...multicallResponse[0][0], ...multicallResponse[1][0]];
|
|
179
|
+
|
|
180
|
+
const IVariableDebtTokenAbi = getConfigContractAbi('IVariableDebtToken');
|
|
181
|
+
const IATokenAbi = getConfigContractAbi('IAToken');
|
|
182
|
+
|
|
183
|
+
const scaledBalanceMulticall = [
|
|
184
|
+
...loanInfo.map((_, i: number) => [
|
|
185
|
+
{
|
|
186
|
+
target: multicallResponse[(2 * i) + loanInfoCallsToSkip][0].variableDebtToken, // TODO: aave refactor add to Aave view
|
|
187
|
+
abiItem: getAbiItem(IVariableDebtTokenAbi, 'scaledBalanceOf'),
|
|
188
|
+
params: [lendingPoolContract.options.address],
|
|
189
|
+
},
|
|
190
|
+
{
|
|
191
|
+
target: loanInfo[i].aTokenAddress, // TODO: aave refactor add to Aave view
|
|
192
|
+
abiItem: getAbiItem(IATokenAbi, 'scaledBalanceOf'),
|
|
193
|
+
params: [lendingPoolContract.options.address],
|
|
194
|
+
},
|
|
195
|
+
]).flat(),
|
|
196
|
+
];
|
|
197
|
+
|
|
198
|
+
const [scaledBalanceResponse, morphoRewards] = await Promise.allSettled([
|
|
199
|
+
multicall(scaledBalanceMulticall, web3, network),
|
|
200
|
+
fetch('https://api.morpho.xyz/rewards/emissions'),
|
|
201
|
+
]);
|
|
202
|
+
|
|
203
|
+
if (scaledBalanceResponse.status !== 'fulfilled') {
|
|
204
|
+
throw new Error('Failed to fetch market data.');
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const morphoRewardsData = morphoRewards.status === 'fulfilled' ? await morphoRewards.value.json() : null;
|
|
208
|
+
|
|
209
|
+
const assetsData: MorphoAaveV3AssetData[] = await Promise.all(loanInfo.map(async (info, i: number) => {
|
|
210
|
+
const morphoMarketData = {
|
|
211
|
+
...multicallResponse[(2 * i) + loanInfoCallsToSkip][0],
|
|
212
|
+
scaledMorphoBorrowOnPool: scaledBalanceResponse.value[2 * i][0],
|
|
213
|
+
scaledMorphoSupplyOnPool: scaledBalanceResponse.value[(2 * i) + 1][0],
|
|
214
|
+
};
|
|
215
|
+
const marketData = computeMorphoMarketData(
|
|
216
|
+
info,
|
|
217
|
+
morphoMarketData,
|
|
218
|
+
multicallResponse[(2 * i) + (loanInfoCallsToSkip + 1)],
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
const { symbol, address } = getAssetInfoByAddress(wethToEthByAddress(marketData.underlyingTokenAddress));
|
|
222
|
+
|
|
223
|
+
const data = {
|
|
224
|
+
symbol,
|
|
225
|
+
morphoMarketData,
|
|
226
|
+
hasDelta: new Dec(marketData.p2pSupplyAPY).minus(marketData.p2pBorrowAPY).gte(0.3),
|
|
227
|
+
eModeCategory: +marketData.emodeCategory,
|
|
228
|
+
aTokenAddress: marketData.aTokenAddress,
|
|
229
|
+
underlyingTokenAddress: address,
|
|
230
|
+
price: new Dec(marketData.price.toString()).div(1e8).toString(), // is actually price in USD
|
|
231
|
+
|
|
232
|
+
supplyRate: new Dec(marketData.supplyRate.toString()).div(1e25).toString(),
|
|
233
|
+
supplyRateP2P: marketData.p2pSupplyAPY,
|
|
234
|
+
borrowRate: new Dec(marketData.borrowRateVariable.toString()).div(1e25).toString(),
|
|
235
|
+
borrowRateP2P: marketData.p2pBorrowAPY,
|
|
236
|
+
totalSupply: assetAmountInEth(marketData.totalSupply.toString(), symbol),
|
|
237
|
+
totalBorrow: assetAmountInEth(marketData.totalBorrow.toString(), symbol),
|
|
238
|
+
|
|
239
|
+
totalSupplyP2P: marketData.morphoSupplyInP2P,
|
|
240
|
+
totalSupplyPool: marketData.morphoSupplyOnPool,
|
|
241
|
+
totalBorrowP2P: marketData.morphoBorrowInP2P,
|
|
242
|
+
totalBorrowPool: marketData.morphoBorrowOnPool,
|
|
243
|
+
|
|
244
|
+
supplyCap: marketData.supplyCap,
|
|
245
|
+
borrowCap: marketData.borrowCap,
|
|
246
|
+
usageAsCollateralEnabled: marketData.isCollateral,
|
|
247
|
+
collateralFactor: marketData.isCollateral ? new Dec(marketData.collateralFactor).div(10000).toString() : '0',
|
|
248
|
+
liquidationRatio: new Dec(marketData.liquidationRatio).div(10000).toString(),
|
|
249
|
+
isInactive: !marketData.isActive,
|
|
250
|
+
isFrozen: marketData.isFrozen,
|
|
251
|
+
isPaused: marketData.isPaused,
|
|
252
|
+
canBeBorrowed: !marketData.isFrozen
|
|
253
|
+
&& marketData.borrowingEnabled
|
|
254
|
+
&& !marketData.pauseStatuses.isBorrowPaused
|
|
255
|
+
&& !marketData.pauseStatuses.isDeprecated,
|
|
256
|
+
canBeSupplied: !marketData.isFrozen
|
|
257
|
+
&& marketData.isCollateral ? !marketData.pauseStatuses.isSupplyCollateralPaused : !marketData.pauseStatuses.isSupplyPaused
|
|
258
|
+
&& !marketData.pauseStatuses.isDeprecated,
|
|
259
|
+
canBeWithdrawn: marketData.isActive
|
|
260
|
+
&& !marketData.isPaused
|
|
261
|
+
&& marketData.isCollateral ? !marketData.pauseStatuses.isWithdrawCollateralPaused : !marketData.pauseStatuses.isWithdrawPaused,
|
|
262
|
+
canBePayBacked: marketData.isActive && !marketData.isPaused && !marketData.pauseStatuses.isRepayPaused,
|
|
263
|
+
reserveFactor: new Dec(marketData.reserveFactor).toString(),
|
|
264
|
+
pauseStatus: {
|
|
265
|
+
isSupplyPaused: marketData.pauseStatuses.isSupplyPaused,
|
|
266
|
+
isSupplyCollateralPaused: marketData.pauseStatuses.isSupplyCollateralPaused,
|
|
267
|
+
isWithdrawPaused: marketData.pauseStatuses.isWithdrawPaused,
|
|
268
|
+
isWithdrawCollateralPaused: marketData.pauseStatuses.isWithdrawCollateralPaused,
|
|
269
|
+
isRepayPaused: marketData.pauseStatuses.isRepayPaused,
|
|
270
|
+
isBorrowPaused: marketData.pauseStatuses.isBorrowPaused,
|
|
271
|
+
isDeprecated: marketData.pauseStatuses.isDeprecated,
|
|
272
|
+
isP2PDisabled: marketData.pauseStatuses.isP2PDisabled,
|
|
273
|
+
},
|
|
274
|
+
marketLiquidity: assetAmountInEth(new Dec(marketData.totalSupply.toString())
|
|
275
|
+
.sub(marketData.totalBorrow.toString())
|
|
276
|
+
.toString(), symbol),
|
|
277
|
+
utilization: new Dec(marketData.totalBorrow.toString())
|
|
278
|
+
.div(new Dec(marketData.totalSupply.toString()))
|
|
279
|
+
.times(100)
|
|
280
|
+
.toString(),
|
|
281
|
+
|
|
282
|
+
eModeCategoryData: {
|
|
283
|
+
label: marketData.label,
|
|
284
|
+
liquidationBonus: new Dec(marketData.liquidationBonus).div(10000).toString(),
|
|
285
|
+
liquidationRatio: new Dec(marketData.liquidationThreshold).div(10000).toString(),
|
|
286
|
+
collateralFactor: new Dec(marketData.ltv).div(10000).toString(),
|
|
287
|
+
priceSource: marketData.priceSource,
|
|
288
|
+
},
|
|
289
|
+
|
|
290
|
+
incentiveSupplyToken: 'MORPHO',
|
|
291
|
+
incentiveBorrowToken: 'MORPHO',
|
|
292
|
+
incentiveSupplyApy: morphoRewardsData?.markets?.[marketData.underlyingTokenAddress?.toLowerCase()]?.morphoRatePerSecondSupplySide || '0',
|
|
293
|
+
incentiveBorrowApy: morphoRewardsData.markets?.[marketData.underlyingTokenAddress?.toLowerCase()]?.morphoRatePerSecondBorrowSide || '0',
|
|
294
|
+
|
|
295
|
+
totalBorrowVar: '0', // Morpho doesn't have all these, keeping it for compatability
|
|
296
|
+
borrowRateStable: '0',
|
|
297
|
+
disabledStableBorrowing: false,
|
|
298
|
+
isIsolated: false,
|
|
299
|
+
debtCeilingForIsolationMode: '0',
|
|
300
|
+
isSiloed: false,
|
|
301
|
+
isolationModeTotalDebt: '0',
|
|
302
|
+
assetId: null,
|
|
303
|
+
isolationModeBorrowingEnabled: false,
|
|
304
|
+
isFlashLoanEnabled: false,
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
if (symbol === 'wstETH') {
|
|
308
|
+
data.incentiveSupplyApy = await getStETHApr(mainnetWeb3);
|
|
309
|
+
data.incentiveSupplyToken = symbol;
|
|
310
|
+
}
|
|
311
|
+
if (symbol === 'cbETH' && !isLayer2Network(network)) {
|
|
312
|
+
data.incentiveSupplyApy = await getCbETHApr(mainnetWeb3);
|
|
313
|
+
data.incentiveSupplyToken = symbol;
|
|
314
|
+
}
|
|
315
|
+
if (symbol === 'rETH') {
|
|
316
|
+
data.incentiveSupplyApy = await getREthApr(mainnetWeb3);
|
|
317
|
+
data.incentiveSupplyToken = symbol;
|
|
318
|
+
}
|
|
319
|
+
if (data.symbol === 'sDAI') {
|
|
320
|
+
data.incentiveSupplyApy = await getDsrApy(web3, network);
|
|
321
|
+
data.incentiveSupplyToken = 'sDAI';
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
return data;
|
|
325
|
+
}));
|
|
326
|
+
|
|
327
|
+
const payload: MorphoAaveV3AssetsData = {};
|
|
328
|
+
// Sort by market size
|
|
329
|
+
assetsData
|
|
330
|
+
.sort((a, b) => {
|
|
331
|
+
const aMarket = new Dec(a.price).times(a.totalSupply).toString();
|
|
332
|
+
const bMarket = new Dec(b.price).times(b.totalSupply).toString();
|
|
333
|
+
|
|
334
|
+
return new Dec(bMarket).minus(aMarket).toNumber();
|
|
335
|
+
})
|
|
336
|
+
.forEach((assetData: MorphoAaveV3AssetData, i: number) => {
|
|
337
|
+
payload[assetData.symbol] = { ...assetData, sortIndex: i };
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
return { assetsData: payload };
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
export const getMorphoAaveV3AccountBalances = async (web3: Web3, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress): Promise<PositionBalances> => {
|
|
344
|
+
let balances: PositionBalances = {
|
|
345
|
+
collateral: {},
|
|
346
|
+
debt: {},
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
if (!address) {
|
|
350
|
+
return balances;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
const selectedMarket = MORPHO_AAVE_V3_ETH(network);
|
|
354
|
+
// @ts-ignore
|
|
355
|
+
const lendingPoolContract = createContractWrapper(web3, network, selectedMarket.lendingPool, selectedMarket.lendingPoolAddress);
|
|
356
|
+
// @ts-ignore
|
|
357
|
+
const protocolDataProviderContract = createContractWrapper(web3, network, selectedMarket.protocolData, selectedMarket.protocolDataAddress);
|
|
358
|
+
|
|
359
|
+
const reserveTokens = await protocolDataProviderContract.methods.getAllReservesTokens().call({}, block);
|
|
360
|
+
const symbols = reserveTokens.map(({ symbol }: { symbol: string }) => symbol);
|
|
361
|
+
const _addresses = reserveTokens.map(({ tokenAddress }: { tokenAddress: EthAddress }) => tokenAddress);
|
|
362
|
+
|
|
363
|
+
const multicallArray = [
|
|
364
|
+
...(_addresses.map((underlyingAddress: string) => ([
|
|
365
|
+
{
|
|
366
|
+
target: lendingPoolContract.options.address,
|
|
367
|
+
abiItem: getAbiItem(lendingPoolContract.options.jsonInterface, 'market'),
|
|
368
|
+
params: [underlyingAddress],
|
|
369
|
+
},
|
|
370
|
+
{
|
|
371
|
+
target: lendingPoolContract.options.address,
|
|
372
|
+
abiItem: getAbiItem(lendingPoolContract.options.jsonInterface, 'scaledP2PSupplyBalance'),
|
|
373
|
+
params: [underlyingAddress, address],
|
|
374
|
+
},
|
|
375
|
+
{
|
|
376
|
+
target: lendingPoolContract.options.address,
|
|
377
|
+
abiItem: getAbiItem(lendingPoolContract.options.jsonInterface, 'scaledPoolSupplyBalance'),
|
|
378
|
+
params: [underlyingAddress, address],
|
|
379
|
+
},
|
|
380
|
+
{
|
|
381
|
+
target: lendingPoolContract.options.address,
|
|
382
|
+
abiItem: getAbiItem(lendingPoolContract.options.jsonInterface, 'scaledCollateralBalance'),
|
|
383
|
+
params: [underlyingAddress, address],
|
|
384
|
+
},
|
|
385
|
+
{
|
|
386
|
+
target: lendingPoolContract.options.address,
|
|
387
|
+
abiItem: getAbiItem(lendingPoolContract.options.jsonInterface, 'scaledP2PBorrowBalance'),
|
|
388
|
+
params: [underlyingAddress, address],
|
|
389
|
+
},
|
|
390
|
+
{
|
|
391
|
+
target: lendingPoolContract.options.address,
|
|
392
|
+
abiItem: getAbiItem(lendingPoolContract.options.jsonInterface, 'scaledPoolBorrowBalance'),
|
|
393
|
+
params: [underlyingAddress, address],
|
|
394
|
+
},
|
|
395
|
+
]))).flat(),
|
|
396
|
+
];
|
|
397
|
+
|
|
398
|
+
const multicallResponse = await multicall(multicallArray, web3, network, block);
|
|
399
|
+
|
|
400
|
+
const numberOfMultiCalls = 6;
|
|
401
|
+
|
|
402
|
+
_addresses.forEach((underlyingAddr: string, i: number) => {
|
|
403
|
+
const currentMulticallIndex = numberOfMultiCalls * i;
|
|
404
|
+
const morphoMarketData = multicallResponse[currentMulticallIndex][0];
|
|
405
|
+
const assetAddr = wethToEthByAddress(underlyingAddr, network).toLowerCase();
|
|
406
|
+
const { symbol } = getAssetInfoByAddress(assetAddr, network);
|
|
407
|
+
|
|
408
|
+
const suppliedP2P = morphoAaveMath.indexMul(
|
|
409
|
+
multicallResponse[currentMulticallIndex + 1][0],
|
|
410
|
+
morphoMarketData.indexes.supply.p2pIndex,
|
|
411
|
+
).toString();
|
|
412
|
+
const suppliedPool = morphoAaveMath.indexMul(
|
|
413
|
+
multicallResponse[currentMulticallIndex + 2][0],
|
|
414
|
+
morphoMarketData.indexes.supply.poolIndex,
|
|
415
|
+
).toString();
|
|
416
|
+
const suppliedTotal = new Dec(suppliedP2P).add(suppliedPool).toString();
|
|
417
|
+
const suppliedCollateral = morphoAaveMath.indexMul(
|
|
418
|
+
multicallResponse[currentMulticallIndex + 3][0],
|
|
419
|
+
morphoMarketData.indexes.supply.poolIndex,
|
|
420
|
+
).toString();
|
|
421
|
+
const supplied = new Dec(suppliedTotal).add(suppliedCollateral).toString();
|
|
422
|
+
|
|
423
|
+
const borrowedP2P = morphoAaveMath.indexMul(
|
|
424
|
+
multicallResponse[currentMulticallIndex + 4][0],
|
|
425
|
+
morphoMarketData.indexes.borrow.p2pIndex,
|
|
426
|
+
).toString();
|
|
427
|
+
const borrowedPool = morphoAaveMath.indexMul(
|
|
428
|
+
multicallResponse[currentMulticallIndex + 5][0],
|
|
429
|
+
morphoMarketData.indexes.borrow.poolIndex,
|
|
430
|
+
).toString();
|
|
431
|
+
const borrowed = new Dec(borrowedP2P).add(borrowedPool).toString();
|
|
432
|
+
|
|
433
|
+
balances = {
|
|
434
|
+
collateral: {
|
|
435
|
+
...balances.collateral,
|
|
436
|
+
[addressMapping ? assetAddr : symbol]: supplied,
|
|
437
|
+
},
|
|
438
|
+
debt: {
|
|
439
|
+
...balances.debt,
|
|
440
|
+
[addressMapping ? assetAddr : symbol]: borrowed,
|
|
441
|
+
},
|
|
442
|
+
};
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
return balances;
|
|
446
|
+
};
|
|
447
|
+
|
|
448
|
+
export const getMorphoAaveV3AccountData = async (
|
|
449
|
+
web3: Web3,
|
|
450
|
+
network: NetworkNumber,
|
|
451
|
+
address: string,
|
|
452
|
+
assetsData: MorphoAaveV3AssetsData,
|
|
453
|
+
delegator: string,
|
|
454
|
+
selectedMarket: MorphoAaveV3MarketInfo,
|
|
455
|
+
): Promise<MorphoAaveV3PositionData> => {
|
|
456
|
+
if (!address) {
|
|
457
|
+
throw new Error('No address provided.');
|
|
458
|
+
}
|
|
459
|
+
const eModeCategory = 1; // TODO: morpho v3 pass as arg
|
|
460
|
+
|
|
461
|
+
let payload: MorphoAaveV3PositionData = {
|
|
462
|
+
...EMPTY_AAVE_DATA,
|
|
463
|
+
usedAssets: {}, // Typescript is bugging out due to JSDocs version of AavePositionData.UsedAssets
|
|
464
|
+
eModeCategory,
|
|
465
|
+
minRatio: '100',
|
|
466
|
+
lastUpdated: Date.now(),
|
|
467
|
+
};
|
|
468
|
+
|
|
469
|
+
// @ts-ignore
|
|
470
|
+
const lendingPoolContract = createContractWrapper(web3, network, selectedMarket.lendingPool, selectedMarket.lendingPoolAddress);
|
|
471
|
+
|
|
472
|
+
const isManagedBy = delegator && lendingPoolContract?.methods?.isManagedBy
|
|
473
|
+
? await lendingPoolContract.methods.isManagedBy(address, delegator).call()
|
|
474
|
+
: null;
|
|
475
|
+
payload.approvedManager = isManagedBy ? delegator : '';
|
|
476
|
+
|
|
477
|
+
const markets = Object.values(assetsData);
|
|
478
|
+
// @ts-ignore
|
|
479
|
+
const marketAddresses = markets.map(m => ethToWethByAddress(m.underlyingTokenAddress));
|
|
480
|
+
|
|
481
|
+
const multicallArray = [
|
|
482
|
+
...(marketAddresses.map((marketAddr) => [
|
|
483
|
+
{
|
|
484
|
+
target: lendingPoolContract.options.address,
|
|
485
|
+
abiItem: getAbiItem(lendingPoolContract.options.jsonInterface, 'scaledP2PSupplyBalance'),
|
|
486
|
+
params: [marketAddr, address],
|
|
487
|
+
},
|
|
488
|
+
{
|
|
489
|
+
target: lendingPoolContract.options.address,
|
|
490
|
+
abiItem: getAbiItem(lendingPoolContract.options.jsonInterface, 'scaledPoolSupplyBalance'),
|
|
491
|
+
params: [marketAddr, address],
|
|
492
|
+
},
|
|
493
|
+
{
|
|
494
|
+
target: lendingPoolContract.options.address,
|
|
495
|
+
abiItem: getAbiItem(lendingPoolContract.options.jsonInterface, 'scaledCollateralBalance'),
|
|
496
|
+
params: [marketAddr, address],
|
|
497
|
+
},
|
|
498
|
+
{
|
|
499
|
+
target: lendingPoolContract.options.address,
|
|
500
|
+
abiItem: getAbiItem(lendingPoolContract.options.jsonInterface, 'scaledP2PBorrowBalance'),
|
|
501
|
+
params: [marketAddr, address],
|
|
502
|
+
},
|
|
503
|
+
{
|
|
504
|
+
target: lendingPoolContract.options.address,
|
|
505
|
+
abiItem: getAbiItem(lendingPoolContract.options.jsonInterface, 'scaledPoolBorrowBalance'),
|
|
506
|
+
params: [marketAddr, address],
|
|
507
|
+
},
|
|
508
|
+
]).flat()),
|
|
509
|
+
];
|
|
510
|
+
|
|
511
|
+
const multicallResponse = await multicall(multicallArray, web3, network);
|
|
512
|
+
|
|
513
|
+
markets.forEach((market: any, i: number) => {
|
|
514
|
+
const { symbol } = getAssetInfoByAddress(wethToEthByAddress(market.underlyingTokenAddress));
|
|
515
|
+
const assetAavePrice = assetsData[symbol].price;
|
|
516
|
+
|
|
517
|
+
const suppliedP2P = assetAmountInEth(morphoAaveMath.indexMul(
|
|
518
|
+
multicallResponse[i * 5][0],
|
|
519
|
+
market.morphoMarketData.indexes.supply.p2pIndex,
|
|
520
|
+
), symbol);
|
|
521
|
+
const suppliedPool = assetAmountInEth(morphoAaveMath.indexMul(
|
|
522
|
+
multicallResponse[(i * 5) + 1][0],
|
|
523
|
+
market.morphoMarketData.indexes.supply.poolIndex,
|
|
524
|
+
), symbol);
|
|
525
|
+
const suppliedTotal = new Dec(suppliedP2P).add(suppliedPool).toString();
|
|
526
|
+
const suppliedCollateral = assetAmountInEth(morphoAaveMath.indexMul(
|
|
527
|
+
multicallResponse[(i * 5) + 2][0],
|
|
528
|
+
market.morphoMarketData.indexes.supply.poolIndex,
|
|
529
|
+
), symbol);
|
|
530
|
+
const supplied = new Dec(suppliedTotal).add(suppliedCollateral).toString();
|
|
531
|
+
const suppliedMatched = new Dec(suppliedTotal).eq(0)
|
|
532
|
+
? '0'
|
|
533
|
+
: morphoAaveMath.percentDiv(
|
|
534
|
+
assetAmountInWei(suppliedP2P, symbol),
|
|
535
|
+
assetAmountInWei(suppliedTotal, symbol),
|
|
536
|
+
).div(100).toString();
|
|
537
|
+
|
|
538
|
+
const borrowedP2P = assetAmountInEth(morphoAaveMath.indexMul(
|
|
539
|
+
multicallResponse[(i * 5) + 3][0],
|
|
540
|
+
market.morphoMarketData.indexes.borrow.p2pIndex,
|
|
541
|
+
), symbol);
|
|
542
|
+
const borrowedPool = assetAmountInEth(morphoAaveMath.indexMul(
|
|
543
|
+
multicallResponse[(i * 5) + 4][0],
|
|
544
|
+
market.morphoMarketData.indexes.borrow.poolIndex,
|
|
545
|
+
), symbol);
|
|
546
|
+
const borrowed = new Dec(borrowedP2P).add(borrowedPool).toString();
|
|
547
|
+
const borrowedMatched = new Dec(borrowed).eq(0)
|
|
548
|
+
? '0'
|
|
549
|
+
: morphoAaveMath.percentDiv(
|
|
550
|
+
assetAmountInWei(borrowedP2P, symbol),
|
|
551
|
+
assetAmountInWei(borrowed, symbol),
|
|
552
|
+
).div(100).toString();
|
|
553
|
+
|
|
554
|
+
const supplyRate = new Dec(new Dec(market.supplyRateP2P).mul(suppliedMatched))
|
|
555
|
+
.add(new Dec(market.supplyRate).mul(100 - +suppliedMatched)).div(100).toString();
|
|
556
|
+
const borrowRate = new Dec(new Dec(market.borrowRateP2P).mul(borrowedMatched))
|
|
557
|
+
.add(new Dec(market.borrowRate).mul(100 - +borrowedMatched)).div(100).toString();
|
|
558
|
+
|
|
559
|
+
if (new Dec(supplied).gt(0) || new Dec(borrowed).gt(0)) {
|
|
560
|
+
payload.usedAssets[symbol] = {
|
|
561
|
+
symbol,
|
|
562
|
+
eModeCategory: market.eModeCategory,
|
|
563
|
+
supplied,
|
|
564
|
+
suppliedP2P,
|
|
565
|
+
suppliedPool,
|
|
566
|
+
suppliedMatched,
|
|
567
|
+
borrowed,
|
|
568
|
+
borrowedP2P,
|
|
569
|
+
borrowedPool,
|
|
570
|
+
borrowedMatched,
|
|
571
|
+
supplyRate,
|
|
572
|
+
borrowRate,
|
|
573
|
+
suppliedUsd: new Dec(supplied).mul(assetAavePrice).toString(),
|
|
574
|
+
suppliedP2PUsd: new Dec(suppliedP2P).mul(assetAavePrice).toString(),
|
|
575
|
+
suppliedPoolUsd: new Dec(suppliedPool).mul(assetAavePrice).toString(),
|
|
576
|
+
borrowedUsd: new Dec(borrowed).mul(assetAavePrice).toString(),
|
|
577
|
+
borrowedP2PUsd: new Dec(borrowedP2P).mul(assetAavePrice).toString(),
|
|
578
|
+
borrowedPoolUsd: new Dec(borrowedPool).mul(assetAavePrice).toString(),
|
|
579
|
+
borrowedVariable: borrowed,
|
|
580
|
+
borrowedUsdVariable: new Dec(borrowed).mul(assetAavePrice).toString(),
|
|
581
|
+
collateral: new Dec(suppliedCollateral).gt(0),
|
|
582
|
+
isSupplied: new Dec(supplied).gt(0),
|
|
583
|
+
isBorrowed: new Dec(borrowed).gt(0),
|
|
584
|
+
// supplyRate: new Dec(market.experiencedSupplyAPY._hex).div(100).toString(),
|
|
585
|
+
// borrowRate: new Dec(market.experiencedBorrowAPY._hex).div(100).toString(),
|
|
586
|
+
limit: '0',
|
|
587
|
+
|
|
588
|
+
interestMode: '', // Morpho doesn't have all these, keeping it for compatability
|
|
589
|
+
stableBorrowRate: '0',
|
|
590
|
+
borrowedStable: '0',
|
|
591
|
+
borrowedUsdStable: '0',
|
|
592
|
+
stableLimit: '0',
|
|
593
|
+
variableLimit: '0',
|
|
594
|
+
};
|
|
595
|
+
}
|
|
596
|
+
});
|
|
597
|
+
|
|
598
|
+
payload = {
|
|
599
|
+
...payload,
|
|
600
|
+
...aaveAnyGetAggregatedPositionData({
|
|
601
|
+
usedAssets: payload.usedAssets, assetsData, eModeCategory, selectedMarket,
|
|
602
|
+
}),
|
|
603
|
+
};
|
|
604
|
+
|
|
605
|
+
// Calculate borrow limits per asset
|
|
606
|
+
Object.values(payload.usedAssets).forEach((item) => {
|
|
607
|
+
if (item.isBorrowed) {
|
|
608
|
+
// eslint-disable-next-line no-param-reassign
|
|
609
|
+
item.limit = calculateBorrowingAssetLimit(item.borrowedUsd, payload.borrowLimitUsd);
|
|
610
|
+
}
|
|
611
|
+
});
|
|
612
|
+
|
|
613
|
+
return payload;
|
|
614
|
+
};
|
|
615
|
+
|
|
616
|
+
export const getMorphoAaveV3FullPositionData = async (web3: Web3, network: NetworkNumber, address: string, delegator: string, market: MorphoAaveV3MarketInfo, mainnetWeb3: Web3): Promise<MorphoAaveV3PositionData> => {
|
|
617
|
+
const marketData = await getMorphoAaveV3MarketsData(web3, network, market, mainnetWeb3);
|
|
618
|
+
const positionData = await getMorphoAaveV3AccountData(web3, network, address, marketData.assetsData, delegator, market);
|
|
619
|
+
return positionData;
|
|
620
620
|
};
|