@defisaver/positions-sdk 0.0.200 → 0.0.201
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/.mocharc.json +4 -4
- package/.nvmrc +1 -1
- package/README.md +69 -69
- package/cjs/helpers/morphoBlueHelpers/index.js +66 -66
- package/cjs/markets/aave/marketAssets.js +1 -1
- package/esm/helpers/morphoBlueHelpers/index.js +66 -66
- package/esm/markets/aave/marketAssets.js +1 -1
- package/package.json +49 -49
- package/src/aaveV2/index.ts +227 -227
- package/src/aaveV3/index.ts +625 -625
- package/src/assets/index.ts +60 -60
- package/src/chickenBonds/index.ts +123 -123
- package/src/compoundV2/index.ts +220 -220
- package/src/compoundV3/index.ts +291 -291
- package/src/config/contracts.js +1109 -1109
- package/src/constants/index.ts +6 -6
- package/src/contracts.ts +133 -133
- package/src/curveUsd/index.ts +229 -229
- package/src/eulerV2/index.ts +303 -303
- package/src/exchange/index.ts +17 -17
- package/src/helpers/aaveHelpers/index.ts +198 -198
- package/src/helpers/chickenBondsHelpers/index.ts +23 -23
- package/src/helpers/compoundHelpers/index.ts +246 -246
- package/src/helpers/curveUsdHelpers/index.ts +40 -40
- package/src/helpers/eulerHelpers/index.ts +232 -232
- package/src/helpers/index.ts +10 -10
- package/src/helpers/liquityV2Helpers/index.ts +79 -79
- package/src/helpers/llamaLendHelpers/index.ts +53 -53
- package/src/helpers/makerHelpers/index.ts +94 -94
- package/src/helpers/morphoBlueHelpers/index.ts +365 -365
- package/src/helpers/sparkHelpers/index.ts +150 -150
- package/src/index.ts +50 -50
- package/src/liquity/index.ts +116 -116
- package/src/liquityV2/index.ts +227 -227
- package/src/llamaLend/index.ts +275 -275
- package/src/maker/index.ts +117 -117
- package/src/markets/aave/index.ts +152 -152
- package/src/markets/aave/marketAssets.ts +44 -44
- package/src/markets/compound/index.ts +213 -213
- package/src/markets/compound/marketsAssets.ts +82 -82
- package/src/markets/curveUsd/index.ts +69 -69
- package/src/markets/euler/index.ts +26 -26
- package/src/markets/index.ts +24 -24
- package/src/markets/liquityV2/index.ts +43 -43
- package/src/markets/llamaLend/contractAddresses.ts +141 -141
- package/src/markets/llamaLend/index.ts +235 -235
- package/src/markets/morphoBlue/index.ts +895 -895
- package/src/markets/spark/index.ts +29 -29
- package/src/markets/spark/marketAssets.ts +10 -10
- package/src/moneymarket/moneymarketCommonService.ts +80 -80
- package/src/morphoAaveV2/index.ts +256 -256
- package/src/morphoAaveV3/index.ts +631 -631
- package/src/morphoBlue/index.ts +204 -204
- package/src/multicall/index.ts +22 -22
- package/src/services/dsrService.ts +15 -15
- package/src/services/priceService.ts +62 -62
- package/src/services/utils.ts +56 -56
- package/src/setup.ts +8 -8
- package/src/spark/index.ts +461 -461
- package/src/staking/staking.ts +220 -220
- package/src/types/aave.ts +271 -271
- package/src/types/chickenBonds.ts +45 -45
- package/src/types/common.ts +84 -84
- package/src/types/compound.ts +131 -131
- package/src/types/curveUsd.ts +118 -118
- package/src/types/euler.ts +171 -171
- package/src/types/index.ts +10 -10
- package/src/types/liquity.ts +30 -30
- package/src/types/liquityV2.ts +118 -118
- package/src/types/llamaLend.ts +155 -155
- package/src/types/maker.ts +50 -50
- package/src/types/morphoBlue.ts +192 -192
- package/src/types/spark.ts +131 -131
package/src/aaveV2/index.ts
CHANGED
|
@@ -1,227 +1,227 @@
|
|
|
1
|
-
import Web3 from 'web3';
|
|
2
|
-
import Dec from 'decimal.js';
|
|
3
|
-
import { assetAmountInEth, getAssetInfo } from '@defisaver/tokens';
|
|
4
|
-
import {
|
|
5
|
-
Blockish, EthAddress, NetworkNumber, PositionBalances,
|
|
6
|
-
} from '../types/common';
|
|
7
|
-
import { calculateNetApy, getStETHApy } from '../staking';
|
|
8
|
-
import { ethToWeth, wethToEth, wethToEthByAddress } from '../services/utils';
|
|
9
|
-
import { AaveLoanInfoV2Contract, createContractWrapper } from '../contracts';
|
|
10
|
-
import { aprToApy, calculateBorrowingAssetLimit } from '../moneymarket';
|
|
11
|
-
import {
|
|
12
|
-
AaveMarketInfo, AaveV2AssetData, AaveV2AssetsData, AaveV2PositionData, AaveV2UsedAsset, AaveV2UsedAssets,
|
|
13
|
-
} from '../types';
|
|
14
|
-
import { EMPTY_AAVE_DATA } from '../aaveV3';
|
|
15
|
-
import { AAVE_V2 } from '../markets/aave';
|
|
16
|
-
import { aaveAnyGetAggregatedPositionData } from '../helpers/aaveHelpers';
|
|
17
|
-
import { getEthPrice } from '../services/priceService';
|
|
18
|
-
|
|
19
|
-
export const getAaveV2MarketsData = async (web3: Web3, network: NetworkNumber, selectedMarket: AaveMarketInfo, mainnetWeb3: Web3) => {
|
|
20
|
-
const ethPrice = await getEthPrice(web3);
|
|
21
|
-
const _addresses = selectedMarket.assets.map(a => getAssetInfo(ethToWeth(a)).address);
|
|
22
|
-
const loanInfoContract = AaveLoanInfoV2Contract(web3, network);
|
|
23
|
-
const marketAddress = selectedMarket.providerAddress;
|
|
24
|
-
const loanInfo = await loanInfoContract.methods.getFullTokensInfo(marketAddress, _addresses).call();
|
|
25
|
-
const markets = loanInfo
|
|
26
|
-
.map((market, i) => ({
|
|
27
|
-
symbol: selectedMarket.assets[i],
|
|
28
|
-
underlyingTokenAddress: market.underlyingTokenAddress,
|
|
29
|
-
supplyRate: aprToApy(new Dec(market.supplyRate.toString()).div(1e25).toString()),
|
|
30
|
-
borrowRate: aprToApy(new Dec(market.borrowRateVariable.toString()).div(1e25).toString()),
|
|
31
|
-
borrowRateStable: aprToApy(new Dec(market.borrowRateStable.toString()).div(1e25).toString()),
|
|
32
|
-
collateralFactor: new Dec(market.collateralFactor.toString()).div(10000).toString(),
|
|
33
|
-
liquidationRatio: new Dec(market.liquidationRatio.toString()).div(10000).toString(),
|
|
34
|
-
marketLiquidity: assetAmountInEth(new Dec(market.totalSupply.toString())
|
|
35
|
-
.sub(market.totalBorrow.toString())
|
|
36
|
-
.toString(), selectedMarket.assets[i]),
|
|
37
|
-
utilization: new Dec(market.totalBorrow.toString())
|
|
38
|
-
.div(new Dec(market.totalSupply.toString()))
|
|
39
|
-
.times(100)
|
|
40
|
-
.toString(),
|
|
41
|
-
usageAsCollateralEnabled: market.usageAsCollateralEnabled,
|
|
42
|
-
supplyCap: '0',
|
|
43
|
-
borrowCap: '0', // v2 doesnt have borrow cap but adding it for compatability with v3
|
|
44
|
-
totalSupply: assetAmountInEth(market.totalSupply.toString(), selectedMarket.assets[i]),
|
|
45
|
-
isInactive: !market.isActive,
|
|
46
|
-
isFrozen: market.isFrozen,
|
|
47
|
-
canBeBorrowed: market.isActive && market.borrowingEnabled && !market.isFrozen,
|
|
48
|
-
canBeSupplied: market.isActive && !market.isFrozen,
|
|
49
|
-
canBeWithdrawn: market.isActive,
|
|
50
|
-
canBePayBacked: market.isActive,
|
|
51
|
-
disabledStableBorrowing: !market.stableBorrowRateEnabled,
|
|
52
|
-
totalBorrow: assetAmountInEth(market.totalBorrow.toString(), selectedMarket.assets[i]),
|
|
53
|
-
totalBorrowVar: assetAmountInEth(market.totalBorrowVar.toString(), selectedMarket.assets[i]),
|
|
54
|
-
priceInEth: new Dec(market.price.toString()).div(1e18).toString(),
|
|
55
|
-
incentiveSupplyToken: 'AAVE',
|
|
56
|
-
incentiveBorrowToken: 'AAVE',
|
|
57
|
-
incentiveSupplyApy: '0',
|
|
58
|
-
price: new Dec(market.price.toString()).div(1e18).mul(ethPrice).toString(),
|
|
59
|
-
}));
|
|
60
|
-
|
|
61
|
-
const stEthMarket = markets.find(({ symbol }) => symbol === 'stETH');
|
|
62
|
-
if (stEthMarket) {
|
|
63
|
-
stEthMarket.incentiveSupplyApy = await getStETHApy(mainnetWeb3);
|
|
64
|
-
stEthMarket.incentiveSupplyToken = 'stETH';
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const payload: AaveV2AssetsData = {};
|
|
68
|
-
// Sort by market size
|
|
69
|
-
markets
|
|
70
|
-
.sort((a, b) => {
|
|
71
|
-
const aMarket = new Dec(a.price).times(a.totalSupply).toString();
|
|
72
|
-
const bMarket = new Dec(b.price).times(b.totalSupply).toString();
|
|
73
|
-
|
|
74
|
-
return new Dec(bMarket).minus(aMarket).toNumber();
|
|
75
|
-
})
|
|
76
|
-
.forEach((assetData: AaveV2AssetData, i) => {
|
|
77
|
-
payload[assetData.symbol] = { ...assetData, sortIndex: i };
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
return { assetsData: payload };
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
export const getAaveV2AccountBalances = async (web3: Web3, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress): Promise<PositionBalances> => {
|
|
84
|
-
let balances: PositionBalances = {
|
|
85
|
-
collateral: {},
|
|
86
|
-
debt: {},
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
if (!address) {
|
|
90
|
-
return balances;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const market = AAVE_V2;
|
|
94
|
-
|
|
95
|
-
const loanInfoContract = AaveLoanInfoV2Contract(web3, network, block);
|
|
96
|
-
|
|
97
|
-
const marketAddress = market.providerAddress;
|
|
98
|
-
// @ts-ignore
|
|
99
|
-
const protocolDataProviderContract = createContractWrapper(web3, network, market.protocolData, market.protocolDataAddress);
|
|
100
|
-
|
|
101
|
-
const reserveTokens = await protocolDataProviderContract.methods.getAllReservesTokens().call({}, block);
|
|
102
|
-
const symbols = reserveTokens.map(({ symbol }: { symbol: string }) => symbol);
|
|
103
|
-
const _addresses = reserveTokens.map(({ tokenAddress }: { tokenAddress: EthAddress }) => tokenAddress);
|
|
104
|
-
|
|
105
|
-
const loanInfo = await loanInfoContract.methods.getTokenBalances(marketAddress, address, _addresses).call({}, block);
|
|
106
|
-
|
|
107
|
-
loanInfo.forEach((_tokenInfo: any, i: number) => {
|
|
108
|
-
const asset = wethToEth(symbols[i]);
|
|
109
|
-
const assetAddr = wethToEthByAddress(_addresses[i], network).toLowerCase();
|
|
110
|
-
const tokenInfo = { ..._tokenInfo };
|
|
111
|
-
|
|
112
|
-
// known bug: stETH leaves 1 wei on every transfer
|
|
113
|
-
if (asset === 'stETH' && tokenInfo.balance.toString() === '1') {
|
|
114
|
-
tokenInfo.balance = '0';
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
balances = {
|
|
118
|
-
collateral: {
|
|
119
|
-
...balances.collateral,
|
|
120
|
-
[addressMapping ? assetAddr : asset]: tokenInfo.balance.toString(),
|
|
121
|
-
},
|
|
122
|
-
debt: {
|
|
123
|
-
...balances.debt,
|
|
124
|
-
[addressMapping ? assetAddr : asset]: new Dec(tokenInfo.borrowsStable.toString()).add(tokenInfo.borrowsVariable.toString()).toString(),
|
|
125
|
-
},
|
|
126
|
-
};
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
return balances;
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
export const getAaveV2AccountData = async (web3: Web3, network: NetworkNumber, address: string, assetsData: AaveV2AssetsData, market: AaveMarketInfo): Promise<AaveV2PositionData> => {
|
|
133
|
-
if (!address) throw new Error('Address is required');
|
|
134
|
-
|
|
135
|
-
let payload: AaveV2PositionData = {
|
|
136
|
-
...EMPTY_AAVE_DATA,
|
|
137
|
-
lastUpdated: Date.now(),
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
const loanInfoContract = AaveLoanInfoV2Contract(web3, network);
|
|
141
|
-
const marketAddress = market.providerAddress;
|
|
142
|
-
const _addresses = market.assets.map(a => getAssetInfo(ethToWeth(a)).address);
|
|
143
|
-
const loanInfo = await loanInfoContract.methods.getTokenBalances(marketAddress, address, _addresses).call();
|
|
144
|
-
const usedAssets = {} as AaveV2UsedAssets;
|
|
145
|
-
loanInfo.forEach((_tokenInfo, i) => {
|
|
146
|
-
const asset = market.assets[i];
|
|
147
|
-
const tokenInfo = { ..._tokenInfo };
|
|
148
|
-
|
|
149
|
-
// known bug: stETH leaves 1 wei on every transfer
|
|
150
|
-
if (asset === 'stETH' && tokenInfo.balance.toString() === '1') tokenInfo.balance = '0';
|
|
151
|
-
|
|
152
|
-
const isSupplied = tokenInfo.balance.toString() !== '0';
|
|
153
|
-
const isBorrowed = tokenInfo.borrowsStable.toString() !== '0' || tokenInfo.borrowsVariable.toString() !== '0';
|
|
154
|
-
if (!isSupplied && !isBorrowed) return;
|
|
155
|
-
|
|
156
|
-
const supplied = assetAmountInEth(tokenInfo.balance.toString(), asset);
|
|
157
|
-
const borrowedStable = assetAmountInEth(tokenInfo.borrowsStable.toString(), asset);
|
|
158
|
-
const borrowedVariable = assetAmountInEth(tokenInfo.borrowsVariable.toString(), asset);
|
|
159
|
-
const enabledAsCollateral = assetsData[asset].usageAsCollateralEnabled ? tokenInfo.enabledAsCollateral : false;
|
|
160
|
-
let interestMode;
|
|
161
|
-
if (borrowedVariable === '0' && borrowedStable !== '0') {
|
|
162
|
-
interestMode = '1';
|
|
163
|
-
} else if (borrowedVariable !== '0' && borrowedStable === '0') {
|
|
164
|
-
interestMode = '2';
|
|
165
|
-
} else {
|
|
166
|
-
interestMode = 'both';
|
|
167
|
-
}
|
|
168
|
-
if (!usedAssets[asset]) usedAssets[asset] = {} as AaveV2UsedAsset;
|
|
169
|
-
usedAssets[asset] = {
|
|
170
|
-
...usedAssets[asset],
|
|
171
|
-
symbol: asset,
|
|
172
|
-
supplied,
|
|
173
|
-
suppliedUsd: new Dec(supplied).mul(assetsData[asset].price).toString(),
|
|
174
|
-
isSupplied,
|
|
175
|
-
collateral: enabledAsCollateral,
|
|
176
|
-
stableBorrowRate: aprToApy(new Dec(tokenInfo.stableBorrowRate).div(1e25).toString()),
|
|
177
|
-
borrowedStable,
|
|
178
|
-
borrowedVariable,
|
|
179
|
-
borrowedUsdStable: new Dec(borrowedStable).mul(assetsData[asset].price).toString(),
|
|
180
|
-
borrowedUsdVariable: new Dec(borrowedVariable).mul(assetsData[asset].price).toString(),
|
|
181
|
-
borrowed: new Dec(borrowedStable).add(borrowedVariable).toString(),
|
|
182
|
-
borrowedUsd: new Dec(new Dec(borrowedVariable).add(borrowedStable)).mul(assetsData[asset].price).toString(),
|
|
183
|
-
isBorrowed,
|
|
184
|
-
interestMode,
|
|
185
|
-
};
|
|
186
|
-
});
|
|
187
|
-
payload = {
|
|
188
|
-
...payload,
|
|
189
|
-
usedAssets,
|
|
190
|
-
...aaveAnyGetAggregatedPositionData({
|
|
191
|
-
usedAssets, assetsData, eModeCategory: 0, selectedMarket: market,
|
|
192
|
-
}),
|
|
193
|
-
};
|
|
194
|
-
|
|
195
|
-
payload.ratio = payload.borrowedUsd && payload.borrowedUsd !== '0'
|
|
196
|
-
? new Dec(payload.borrowLimitUsd).div(payload.borrowedUsd).mul(100).toString()
|
|
197
|
-
: '0';
|
|
198
|
-
payload.minRatio = '100';
|
|
199
|
-
payload.collRatio = payload.borrowedUsd && payload.borrowedUsd !== '0'
|
|
200
|
-
? new Dec(payload.suppliedCollateralUsd).div(payload.borrowedUsd).mul(100).toString()
|
|
201
|
-
: '0';
|
|
202
|
-
|
|
203
|
-
// Calculate borrow limits per asset
|
|
204
|
-
Object.values(payload.usedAssets).forEach((item) => {
|
|
205
|
-
if (item.isBorrowed) {
|
|
206
|
-
// eslint-disable-next-line no-param-reassign
|
|
207
|
-
item.stableLimit = calculateBorrowingAssetLimit(item.borrowedUsdStable, payload.borrowLimitUsd);
|
|
208
|
-
// eslint-disable-next-line no-param-reassign
|
|
209
|
-
item.variableLimit = calculateBorrowingAssetLimit(item.borrowedUsdVariable, payload.borrowLimitUsd);
|
|
210
|
-
// eslint-disable-next-line no-param-reassign
|
|
211
|
-
item.limit = calculateBorrowingAssetLimit(item.borrowedUsd, payload.borrowLimitUsd);
|
|
212
|
-
}
|
|
213
|
-
});
|
|
214
|
-
|
|
215
|
-
const { netApy, incentiveUsd, totalInterestUsd } = calculateNetApy({ usedAssets, assetsData });
|
|
216
|
-
payload.netApy = netApy;
|
|
217
|
-
payload.incentiveUsd = incentiveUsd;
|
|
218
|
-
payload.totalInterestUsd = totalInterestUsd;
|
|
219
|
-
|
|
220
|
-
return payload;
|
|
221
|
-
};
|
|
222
|
-
|
|
223
|
-
export const getAaveV2FullPositionData = async (web3: Web3, network: NetworkNumber, address: string, market: AaveMarketInfo, mainnetWeb3: Web3): Promise<AaveV2PositionData> => {
|
|
224
|
-
const marketData = await getAaveV2MarketsData(web3, network, market, mainnetWeb3);
|
|
225
|
-
const positionData = await getAaveV2AccountData(web3, network, address, marketData.assetsData, market);
|
|
226
|
-
return positionData;
|
|
227
|
-
};
|
|
1
|
+
import Web3 from 'web3';
|
|
2
|
+
import Dec from 'decimal.js';
|
|
3
|
+
import { assetAmountInEth, getAssetInfo } from '@defisaver/tokens';
|
|
4
|
+
import {
|
|
5
|
+
Blockish, EthAddress, NetworkNumber, PositionBalances,
|
|
6
|
+
} from '../types/common';
|
|
7
|
+
import { calculateNetApy, getStETHApy } from '../staking';
|
|
8
|
+
import { ethToWeth, wethToEth, wethToEthByAddress } from '../services/utils';
|
|
9
|
+
import { AaveLoanInfoV2Contract, createContractWrapper } from '../contracts';
|
|
10
|
+
import { aprToApy, calculateBorrowingAssetLimit } from '../moneymarket';
|
|
11
|
+
import {
|
|
12
|
+
AaveMarketInfo, AaveV2AssetData, AaveV2AssetsData, AaveV2PositionData, AaveV2UsedAsset, AaveV2UsedAssets,
|
|
13
|
+
} from '../types';
|
|
14
|
+
import { EMPTY_AAVE_DATA } from '../aaveV3';
|
|
15
|
+
import { AAVE_V2 } from '../markets/aave';
|
|
16
|
+
import { aaveAnyGetAggregatedPositionData } from '../helpers/aaveHelpers';
|
|
17
|
+
import { getEthPrice } from '../services/priceService';
|
|
18
|
+
|
|
19
|
+
export const getAaveV2MarketsData = async (web3: Web3, network: NetworkNumber, selectedMarket: AaveMarketInfo, mainnetWeb3: Web3) => {
|
|
20
|
+
const ethPrice = await getEthPrice(web3);
|
|
21
|
+
const _addresses = selectedMarket.assets.map(a => getAssetInfo(ethToWeth(a)).address);
|
|
22
|
+
const loanInfoContract = AaveLoanInfoV2Contract(web3, network);
|
|
23
|
+
const marketAddress = selectedMarket.providerAddress;
|
|
24
|
+
const loanInfo = await loanInfoContract.methods.getFullTokensInfo(marketAddress, _addresses).call();
|
|
25
|
+
const markets = loanInfo
|
|
26
|
+
.map((market, i) => ({
|
|
27
|
+
symbol: selectedMarket.assets[i],
|
|
28
|
+
underlyingTokenAddress: market.underlyingTokenAddress,
|
|
29
|
+
supplyRate: aprToApy(new Dec(market.supplyRate.toString()).div(1e25).toString()),
|
|
30
|
+
borrowRate: aprToApy(new Dec(market.borrowRateVariable.toString()).div(1e25).toString()),
|
|
31
|
+
borrowRateStable: aprToApy(new Dec(market.borrowRateStable.toString()).div(1e25).toString()),
|
|
32
|
+
collateralFactor: new Dec(market.collateralFactor.toString()).div(10000).toString(),
|
|
33
|
+
liquidationRatio: new Dec(market.liquidationRatio.toString()).div(10000).toString(),
|
|
34
|
+
marketLiquidity: assetAmountInEth(new Dec(market.totalSupply.toString())
|
|
35
|
+
.sub(market.totalBorrow.toString())
|
|
36
|
+
.toString(), selectedMarket.assets[i]),
|
|
37
|
+
utilization: new Dec(market.totalBorrow.toString())
|
|
38
|
+
.div(new Dec(market.totalSupply.toString()))
|
|
39
|
+
.times(100)
|
|
40
|
+
.toString(),
|
|
41
|
+
usageAsCollateralEnabled: market.usageAsCollateralEnabled,
|
|
42
|
+
supplyCap: '0',
|
|
43
|
+
borrowCap: '0', // v2 doesnt have borrow cap but adding it for compatability with v3
|
|
44
|
+
totalSupply: assetAmountInEth(market.totalSupply.toString(), selectedMarket.assets[i]),
|
|
45
|
+
isInactive: !market.isActive,
|
|
46
|
+
isFrozen: market.isFrozen,
|
|
47
|
+
canBeBorrowed: market.isActive && market.borrowingEnabled && !market.isFrozen,
|
|
48
|
+
canBeSupplied: market.isActive && !market.isFrozen,
|
|
49
|
+
canBeWithdrawn: market.isActive,
|
|
50
|
+
canBePayBacked: market.isActive,
|
|
51
|
+
disabledStableBorrowing: !market.stableBorrowRateEnabled,
|
|
52
|
+
totalBorrow: assetAmountInEth(market.totalBorrow.toString(), selectedMarket.assets[i]),
|
|
53
|
+
totalBorrowVar: assetAmountInEth(market.totalBorrowVar.toString(), selectedMarket.assets[i]),
|
|
54
|
+
priceInEth: new Dec(market.price.toString()).div(1e18).toString(),
|
|
55
|
+
incentiveSupplyToken: 'AAVE',
|
|
56
|
+
incentiveBorrowToken: 'AAVE',
|
|
57
|
+
incentiveSupplyApy: '0',
|
|
58
|
+
price: new Dec(market.price.toString()).div(1e18).mul(ethPrice).toString(),
|
|
59
|
+
}));
|
|
60
|
+
|
|
61
|
+
const stEthMarket = markets.find(({ symbol }) => symbol === 'stETH');
|
|
62
|
+
if (stEthMarket) {
|
|
63
|
+
stEthMarket.incentiveSupplyApy = await getStETHApy(mainnetWeb3);
|
|
64
|
+
stEthMarket.incentiveSupplyToken = 'stETH';
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const payload: AaveV2AssetsData = {};
|
|
68
|
+
// Sort by market size
|
|
69
|
+
markets
|
|
70
|
+
.sort((a, b) => {
|
|
71
|
+
const aMarket = new Dec(a.price).times(a.totalSupply).toString();
|
|
72
|
+
const bMarket = new Dec(b.price).times(b.totalSupply).toString();
|
|
73
|
+
|
|
74
|
+
return new Dec(bMarket).minus(aMarket).toNumber();
|
|
75
|
+
})
|
|
76
|
+
.forEach((assetData: AaveV2AssetData, i) => {
|
|
77
|
+
payload[assetData.symbol] = { ...assetData, sortIndex: i };
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
return { assetsData: payload };
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export const getAaveV2AccountBalances = async (web3: Web3, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress): Promise<PositionBalances> => {
|
|
84
|
+
let balances: PositionBalances = {
|
|
85
|
+
collateral: {},
|
|
86
|
+
debt: {},
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
if (!address) {
|
|
90
|
+
return balances;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const market = AAVE_V2;
|
|
94
|
+
|
|
95
|
+
const loanInfoContract = AaveLoanInfoV2Contract(web3, network, block);
|
|
96
|
+
|
|
97
|
+
const marketAddress = market.providerAddress;
|
|
98
|
+
// @ts-ignore
|
|
99
|
+
const protocolDataProviderContract = createContractWrapper(web3, network, market.protocolData, market.protocolDataAddress);
|
|
100
|
+
|
|
101
|
+
const reserveTokens = await protocolDataProviderContract.methods.getAllReservesTokens().call({}, block);
|
|
102
|
+
const symbols = reserveTokens.map(({ symbol }: { symbol: string }) => symbol);
|
|
103
|
+
const _addresses = reserveTokens.map(({ tokenAddress }: { tokenAddress: EthAddress }) => tokenAddress);
|
|
104
|
+
|
|
105
|
+
const loanInfo = await loanInfoContract.methods.getTokenBalances(marketAddress, address, _addresses).call({}, block);
|
|
106
|
+
|
|
107
|
+
loanInfo.forEach((_tokenInfo: any, i: number) => {
|
|
108
|
+
const asset = wethToEth(symbols[i]);
|
|
109
|
+
const assetAddr = wethToEthByAddress(_addresses[i], network).toLowerCase();
|
|
110
|
+
const tokenInfo = { ..._tokenInfo };
|
|
111
|
+
|
|
112
|
+
// known bug: stETH leaves 1 wei on every transfer
|
|
113
|
+
if (asset === 'stETH' && tokenInfo.balance.toString() === '1') {
|
|
114
|
+
tokenInfo.balance = '0';
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
balances = {
|
|
118
|
+
collateral: {
|
|
119
|
+
...balances.collateral,
|
|
120
|
+
[addressMapping ? assetAddr : asset]: tokenInfo.balance.toString(),
|
|
121
|
+
},
|
|
122
|
+
debt: {
|
|
123
|
+
...balances.debt,
|
|
124
|
+
[addressMapping ? assetAddr : asset]: new Dec(tokenInfo.borrowsStable.toString()).add(tokenInfo.borrowsVariable.toString()).toString(),
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
return balances;
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
export const getAaveV2AccountData = async (web3: Web3, network: NetworkNumber, address: string, assetsData: AaveV2AssetsData, market: AaveMarketInfo): Promise<AaveV2PositionData> => {
|
|
133
|
+
if (!address) throw new Error('Address is required');
|
|
134
|
+
|
|
135
|
+
let payload: AaveV2PositionData = {
|
|
136
|
+
...EMPTY_AAVE_DATA,
|
|
137
|
+
lastUpdated: Date.now(),
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const loanInfoContract = AaveLoanInfoV2Contract(web3, network);
|
|
141
|
+
const marketAddress = market.providerAddress;
|
|
142
|
+
const _addresses = market.assets.map(a => getAssetInfo(ethToWeth(a)).address);
|
|
143
|
+
const loanInfo = await loanInfoContract.methods.getTokenBalances(marketAddress, address, _addresses).call();
|
|
144
|
+
const usedAssets = {} as AaveV2UsedAssets;
|
|
145
|
+
loanInfo.forEach((_tokenInfo, i) => {
|
|
146
|
+
const asset = market.assets[i];
|
|
147
|
+
const tokenInfo = { ..._tokenInfo };
|
|
148
|
+
|
|
149
|
+
// known bug: stETH leaves 1 wei on every transfer
|
|
150
|
+
if (asset === 'stETH' && tokenInfo.balance.toString() === '1') tokenInfo.balance = '0';
|
|
151
|
+
|
|
152
|
+
const isSupplied = tokenInfo.balance.toString() !== '0';
|
|
153
|
+
const isBorrowed = tokenInfo.borrowsStable.toString() !== '0' || tokenInfo.borrowsVariable.toString() !== '0';
|
|
154
|
+
if (!isSupplied && !isBorrowed) return;
|
|
155
|
+
|
|
156
|
+
const supplied = assetAmountInEth(tokenInfo.balance.toString(), asset);
|
|
157
|
+
const borrowedStable = assetAmountInEth(tokenInfo.borrowsStable.toString(), asset);
|
|
158
|
+
const borrowedVariable = assetAmountInEth(tokenInfo.borrowsVariable.toString(), asset);
|
|
159
|
+
const enabledAsCollateral = assetsData[asset].usageAsCollateralEnabled ? tokenInfo.enabledAsCollateral : false;
|
|
160
|
+
let interestMode;
|
|
161
|
+
if (borrowedVariable === '0' && borrowedStable !== '0') {
|
|
162
|
+
interestMode = '1';
|
|
163
|
+
} else if (borrowedVariable !== '0' && borrowedStable === '0') {
|
|
164
|
+
interestMode = '2';
|
|
165
|
+
} else {
|
|
166
|
+
interestMode = 'both';
|
|
167
|
+
}
|
|
168
|
+
if (!usedAssets[asset]) usedAssets[asset] = {} as AaveV2UsedAsset;
|
|
169
|
+
usedAssets[asset] = {
|
|
170
|
+
...usedAssets[asset],
|
|
171
|
+
symbol: asset,
|
|
172
|
+
supplied,
|
|
173
|
+
suppliedUsd: new Dec(supplied).mul(assetsData[asset].price).toString(),
|
|
174
|
+
isSupplied,
|
|
175
|
+
collateral: enabledAsCollateral,
|
|
176
|
+
stableBorrowRate: aprToApy(new Dec(tokenInfo.stableBorrowRate).div(1e25).toString()),
|
|
177
|
+
borrowedStable,
|
|
178
|
+
borrowedVariable,
|
|
179
|
+
borrowedUsdStable: new Dec(borrowedStable).mul(assetsData[asset].price).toString(),
|
|
180
|
+
borrowedUsdVariable: new Dec(borrowedVariable).mul(assetsData[asset].price).toString(),
|
|
181
|
+
borrowed: new Dec(borrowedStable).add(borrowedVariable).toString(),
|
|
182
|
+
borrowedUsd: new Dec(new Dec(borrowedVariable).add(borrowedStable)).mul(assetsData[asset].price).toString(),
|
|
183
|
+
isBorrowed,
|
|
184
|
+
interestMode,
|
|
185
|
+
};
|
|
186
|
+
});
|
|
187
|
+
payload = {
|
|
188
|
+
...payload,
|
|
189
|
+
usedAssets,
|
|
190
|
+
...aaveAnyGetAggregatedPositionData({
|
|
191
|
+
usedAssets, assetsData, eModeCategory: 0, selectedMarket: market,
|
|
192
|
+
}),
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
payload.ratio = payload.borrowedUsd && payload.borrowedUsd !== '0'
|
|
196
|
+
? new Dec(payload.borrowLimitUsd).div(payload.borrowedUsd).mul(100).toString()
|
|
197
|
+
: '0';
|
|
198
|
+
payload.minRatio = '100';
|
|
199
|
+
payload.collRatio = payload.borrowedUsd && payload.borrowedUsd !== '0'
|
|
200
|
+
? new Dec(payload.suppliedCollateralUsd).div(payload.borrowedUsd).mul(100).toString()
|
|
201
|
+
: '0';
|
|
202
|
+
|
|
203
|
+
// Calculate borrow limits per asset
|
|
204
|
+
Object.values(payload.usedAssets).forEach((item) => {
|
|
205
|
+
if (item.isBorrowed) {
|
|
206
|
+
// eslint-disable-next-line no-param-reassign
|
|
207
|
+
item.stableLimit = calculateBorrowingAssetLimit(item.borrowedUsdStable, payload.borrowLimitUsd);
|
|
208
|
+
// eslint-disable-next-line no-param-reassign
|
|
209
|
+
item.variableLimit = calculateBorrowingAssetLimit(item.borrowedUsdVariable, payload.borrowLimitUsd);
|
|
210
|
+
// eslint-disable-next-line no-param-reassign
|
|
211
|
+
item.limit = calculateBorrowingAssetLimit(item.borrowedUsd, payload.borrowLimitUsd);
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
const { netApy, incentiveUsd, totalInterestUsd } = calculateNetApy({ usedAssets, assetsData });
|
|
216
|
+
payload.netApy = netApy;
|
|
217
|
+
payload.incentiveUsd = incentiveUsd;
|
|
218
|
+
payload.totalInterestUsd = totalInterestUsd;
|
|
219
|
+
|
|
220
|
+
return payload;
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
export const getAaveV2FullPositionData = async (web3: Web3, network: NetworkNumber, address: string, market: AaveMarketInfo, mainnetWeb3: Web3): Promise<AaveV2PositionData> => {
|
|
224
|
+
const marketData = await getAaveV2MarketsData(web3, network, market, mainnetWeb3);
|
|
225
|
+
const positionData = await getAaveV2AccountData(web3, network, address, marketData.assetsData, market);
|
|
226
|
+
return positionData;
|
|
227
|
+
};
|