@defisaver/positions-sdk 0.0.166-dev → 0.0.166
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/config/contracts.d.ts +0 -72
- package/cjs/config/contracts.js +0 -6
- package/cjs/contracts.d.ts +0 -1
- package/cjs/contracts.js +1 -2
- package/cjs/helpers/index.d.ts +0 -1
- package/cjs/helpers/index.js +1 -2
- package/cjs/index.d.ts +1 -2
- package/cjs/index.js +1 -3
- package/cjs/markets/compound/marketsAssets.js +3 -3
- package/cjs/markets/index.d.ts +0 -1
- package/cjs/markets/index.js +1 -3
- package/cjs/markets/morphoBlue/index.d.ts +4 -0
- package/cjs/markets/morphoBlue/index.js +36 -1
- package/cjs/markets/spark/marketAssets.js +1 -1
- package/cjs/services/utils.d.ts +0 -2
- package/cjs/services/utils.js +1 -4
- package/cjs/staking/staking.d.ts +1 -0
- package/cjs/staking/staking.js +31 -2
- package/cjs/types/contracts/generated/index.d.ts +0 -1
- package/cjs/types/index.d.ts +0 -1
- package/cjs/types/index.js +0 -1
- package/cjs/types/morphoBlue.d.ts +3 -1
- package/cjs/types/morphoBlue.js +2 -0
- package/esm/config/contracts.d.ts +0 -72
- package/esm/config/contracts.js +0 -6
- package/esm/contracts.d.ts +0 -1
- package/esm/contracts.js +0 -1
- package/esm/helpers/index.d.ts +0 -1
- package/esm/helpers/index.js +0 -1
- package/esm/index.d.ts +1 -2
- package/esm/index.js +1 -2
- package/esm/markets/compound/marketsAssets.js +3 -3
- package/esm/markets/index.d.ts +0 -1
- package/esm/markets/index.js +0 -1
- package/esm/markets/morphoBlue/index.d.ts +4 -0
- package/esm/markets/morphoBlue/index.js +33 -0
- package/esm/markets/spark/marketAssets.js +1 -1
- package/esm/services/utils.d.ts +0 -2
- package/esm/services/utils.js +0 -2
- package/esm/staking/staking.d.ts +1 -0
- package/esm/staking/staking.js +29 -1
- package/esm/types/contracts/generated/index.d.ts +0 -1
- package/esm/types/index.d.ts +0 -1
- package/esm/types/index.js +0 -1
- package/esm/types/morphoBlue.d.ts +3 -1
- package/esm/types/morphoBlue.js +2 -0
- package/package.json +10 -5
- package/src/config/contracts.js +0 -6
- package/src/contracts.ts +0 -2
- package/src/helpers/index.ts +1 -2
- package/src/index.ts +0 -2
- package/src/markets/compound/marketsAssets.ts +3 -3
- package/src/markets/index.ts +1 -3
- package/src/markets/morphoBlue/index.ts +37 -0
- package/src/markets/spark/marketAssets.ts +1 -1
- package/src/services/utils.ts +0 -3
- package/src/staking/staking.ts +31 -2
- package/src/types/contracts/generated/index.ts +0 -1
- package/src/types/index.ts +1 -2
- package/src/types/morphoBlue.ts +2 -0
- package/cjs/eulerV2/index.d.ts +0 -40
- package/cjs/eulerV2/index.js +0 -207
- package/cjs/helpers/eulerHelpers/index.d.ts +0 -27
- package/cjs/helpers/eulerHelpers/index.js +0 -232
- package/cjs/markets/euler/index.d.ts +0 -10
- package/cjs/markets/euler/index.js +0 -41
- package/cjs/types/contracts/generated/EulerV2View.d.ts +0 -333
- package/cjs/types/contracts/generated/EulerV2View.js +0 -5
- package/cjs/types/euler.d.ts +0 -148
- package/cjs/types/euler.js +0 -15
- package/esm/eulerV2/index.d.ts +0 -40
- package/esm/eulerV2/index.js +0 -199
- package/esm/helpers/eulerHelpers/index.d.ts +0 -27
- package/esm/helpers/eulerHelpers/index.js +0 -219
- package/esm/markets/euler/index.d.ts +0 -10
- package/esm/markets/euler/index.js +0 -34
- package/esm/types/contracts/generated/EulerV2View.d.ts +0 -333
- package/esm/types/contracts/generated/EulerV2View.js +0 -4
- package/esm/types/euler.d.ts +0 -148
- package/esm/types/euler.js +0 -12
- package/src/eulerV2/index.ts +0 -286
- package/src/helpers/eulerHelpers/index.ts +0 -231
- package/src/markets/euler/index.ts +0 -38
- package/src/types/contracts/generated/EulerV2View.ts +0 -434
- package/src/types/euler.ts +0 -171
package/src/eulerV2/index.ts
DELETED
|
@@ -1,286 +0,0 @@
|
|
|
1
|
-
import Web3 from 'web3';
|
|
2
|
-
import Dec from 'decimal.js';
|
|
3
|
-
import { assetAmountInEth, getAssetInfoByAddress } from '@defisaver/tokens';
|
|
4
|
-
import { NetworkNumber } from '../types/common';
|
|
5
|
-
import { getStakingApy, STAKING_ASSETS } from '../staking';
|
|
6
|
-
import { compareAddresses, getEthAmountForDecimals, isMaxuint } from '../services/utils';
|
|
7
|
-
import {
|
|
8
|
-
EulerV2AssetData,
|
|
9
|
-
EulerV2AssetsData,
|
|
10
|
-
EulerV2FullMarketData,
|
|
11
|
-
EulerV2Market,
|
|
12
|
-
EulerV2MarketInfoData,
|
|
13
|
-
EulerV2PositionData,
|
|
14
|
-
EulerV2UsedAssets,
|
|
15
|
-
EulerV2VaultType,
|
|
16
|
-
} from '../types';
|
|
17
|
-
import {
|
|
18
|
-
getEulerV2AggregatedData,
|
|
19
|
-
getEulerV2BorrowRate,
|
|
20
|
-
getEulerV2SupplyRate,
|
|
21
|
-
getUtilizationRate,
|
|
22
|
-
} from '../helpers/eulerHelpers';
|
|
23
|
-
import { ZERO_ADDRESS } from '../constants';
|
|
24
|
-
import { EulerV2ViewContract } from '../contracts';
|
|
25
|
-
|
|
26
|
-
export const EMPTY_USED_ASSET = {
|
|
27
|
-
isSupplied: false,
|
|
28
|
-
isBorrowed: false,
|
|
29
|
-
supplied: '0',
|
|
30
|
-
suppliedUsd: '0',
|
|
31
|
-
borrowed: '0',
|
|
32
|
-
borrowedUsd: '0',
|
|
33
|
-
symbol: '',
|
|
34
|
-
collateral: false,
|
|
35
|
-
vaultAddress: '',
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
const UnitOfAccountUSD = '0x0000000000000000000000000000000000000348';
|
|
39
|
-
|
|
40
|
-
export const getEulerV2MarketsData = async (web3: Web3, network: NetworkNumber, selectedMarket: EulerV2Market, defaultWeb3: Web3): Promise<EulerV2FullMarketData> => {
|
|
41
|
-
const contract = EulerV2ViewContract(web3, network);
|
|
42
|
-
|
|
43
|
-
const data = await contract.methods.getVaultInfoFull(selectedMarket.marketAddress).call();
|
|
44
|
-
const isInUSD = compareAddresses(UnitOfAccountUSD, data.unitOfAccount);
|
|
45
|
-
|
|
46
|
-
const usdPrice = getEthAmountForDecimals(data.unitOfAccountInUsd, 8);
|
|
47
|
-
|
|
48
|
-
// parse collateral tokens
|
|
49
|
-
// imma use address as key for assetsData because there can be more collateral vaults with the same name
|
|
50
|
-
const colls: EulerV2AssetData[] = data.collaterals.map((collateral) => {
|
|
51
|
-
const decimals = collateral.decimals;
|
|
52
|
-
const assetInfo = getAssetInfoByAddress(collateral.assetAddr);
|
|
53
|
-
const borrowRate = getEulerV2BorrowRate(collateral.interestRate);
|
|
54
|
-
const utilizationRate = getUtilizationRate(collateral.totalBorrows, new Dec(collateral.totalBorrows).plus(collateral.cash).toString());
|
|
55
|
-
|
|
56
|
-
const supplyRate = getEulerV2SupplyRate(borrowRate, utilizationRate, collateral.interestFee);
|
|
57
|
-
return ({
|
|
58
|
-
vaultAddress: collateral.vaultAddr,
|
|
59
|
-
assetAddress: collateral.assetAddr,
|
|
60
|
-
symbol: assetInfo.symbol,
|
|
61
|
-
vaultSymbol: collateral.vaultSymbol,
|
|
62
|
-
decimals,
|
|
63
|
-
liquidationRatio: new Dec(collateral.liquidationLTV).div(10_000).toString(),
|
|
64
|
-
collateralFactor: new Dec(collateral.borrowLTV).div(10_000).toString(),
|
|
65
|
-
totalBorrow: getEthAmountForDecimals(collateral.totalBorrows, decimals), // parse
|
|
66
|
-
cash: getEthAmountForDecimals(collateral.cash, decimals),
|
|
67
|
-
supplyCap: isMaxuint(collateral.supplyCap) ? collateral.supplyCap : getEthAmountForDecimals(collateral.supplyCap, decimals),
|
|
68
|
-
borrowCap: '0',
|
|
69
|
-
price: isInUSD ? assetAmountInEth(collateral.assetPriceInUnit) : new Dec(assetAmountInEth(collateral.assetPriceInUnit)).mul(usdPrice).toString(), // 1e18 -> price in unitOfAccount (so it could be USD or any other token)
|
|
70
|
-
canBeBorrowed: false,
|
|
71
|
-
canBeSupplied: true,
|
|
72
|
-
borrowRate,
|
|
73
|
-
supplyRate,
|
|
74
|
-
utilization: new Dec(utilizationRate).mul(100).toString(),
|
|
75
|
-
});
|
|
76
|
-
});
|
|
77
|
-
for (const coll of colls) {
|
|
78
|
-
if (STAKING_ASSETS.includes(coll.symbol)) {
|
|
79
|
-
coll.incentiveSupplyApy = await getStakingApy(coll.symbol, defaultWeb3);
|
|
80
|
-
coll.incentiveSupplyToken = coll.symbol;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
const isEscrow = data.collaterals.length === 0;
|
|
84
|
-
const isGoverned = !compareAddresses(data.governorAdmin, ZERO_ADDRESS);
|
|
85
|
-
|
|
86
|
-
const vaultType = isEscrow ? EulerV2VaultType.Escrow : (
|
|
87
|
-
isGoverned ? EulerV2VaultType.Governed : EulerV2VaultType.Ungoverned
|
|
88
|
-
);
|
|
89
|
-
const decimals = data.decimals;
|
|
90
|
-
|
|
91
|
-
// (1 + SPY/10**27) ** secondsPerYear - 1
|
|
92
|
-
|
|
93
|
-
const interestRate = data.interestRate;
|
|
94
|
-
|
|
95
|
-
const borrowRate = getEulerV2BorrowRate(interestRate);
|
|
96
|
-
|
|
97
|
-
const utilizationRate = getUtilizationRate(data.totalBorrows, data.totalAssets);
|
|
98
|
-
const supplyRate = getEulerV2SupplyRate(borrowRate, utilizationRate, data.interestFee);
|
|
99
|
-
|
|
100
|
-
const marketAsset = {
|
|
101
|
-
assetAddress: data.assetAddr,
|
|
102
|
-
vaultAddress: data.vaultAddr,
|
|
103
|
-
symbol: selectedMarket.asset,
|
|
104
|
-
vaultSymbol: selectedMarket.shortLabel,
|
|
105
|
-
decimals,
|
|
106
|
-
totalBorrow: getEthAmountForDecimals(data.totalBorrows, decimals), // parse
|
|
107
|
-
cash: getEthAmountForDecimals(data.cash, decimals),
|
|
108
|
-
supplyCap: isMaxuint(data.supplyCap) ? data.supplyCap : getEthAmountForDecimals(data.supplyCap, decimals),
|
|
109
|
-
borrowCap: isMaxuint(data.supplyCap) ? data.borrowCap : getEthAmountForDecimals(data.borrowCap, decimals),
|
|
110
|
-
price: isInUSD ? assetAmountInEth(data.assetPriceInUnit) : new Dec(assetAmountInEth(data.assetPriceInUnit)).mul(usdPrice).toString(), // 1e18 -> price in unitOfAccount (so it could be USD or any other token)
|
|
111
|
-
sortIndex: 0,
|
|
112
|
-
canBeBorrowed: true,
|
|
113
|
-
canBeSupplied: false,
|
|
114
|
-
borrowRate,
|
|
115
|
-
supplyRate,
|
|
116
|
-
collateralFactor: '0',
|
|
117
|
-
liquidationRatio: '0',
|
|
118
|
-
utilization: new Dec(utilizationRate).mul(100).toString(),
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
const assetsData: EulerV2AssetsData = {
|
|
122
|
-
[data.vaultAddr.toLowerCase()]: marketAsset,
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
colls
|
|
126
|
-
.sort((coll1, coll2) => {
|
|
127
|
-
const aMarket = new Dec(coll1.price).times(coll1.totalBorrow).toString();
|
|
128
|
-
const bMarket = new Dec(coll2.price).times(coll2.totalBorrow).toString();
|
|
129
|
-
|
|
130
|
-
return new Dec(bMarket).minus(aMarket).toNumber();
|
|
131
|
-
})
|
|
132
|
-
.forEach((market, i) => {
|
|
133
|
-
assetsData[market.vaultAddress.toLowerCase()] = { ...market, sortIndex: i + 1 };
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
const marketData: EulerV2MarketInfoData = {
|
|
137
|
-
name: data.name,
|
|
138
|
-
symbol: data.symbol,
|
|
139
|
-
decimals: data.decimals,
|
|
140
|
-
irm: data.irm,
|
|
141
|
-
creator: data.creator,
|
|
142
|
-
governorAdmin: data.governorAdmin,
|
|
143
|
-
unitOfAccount: data.unitOfAccount,
|
|
144
|
-
unitOfAccountUsdPrice: usdPrice,
|
|
145
|
-
isInUSD,
|
|
146
|
-
oracle: data.oracle,
|
|
147
|
-
collaterals: data.collaterals.map((collateral) => collateral.vaultAddr),
|
|
148
|
-
isEscrow,
|
|
149
|
-
isGoverned,
|
|
150
|
-
vaultType,
|
|
151
|
-
vaultAddress: data.vaultAddr,
|
|
152
|
-
};
|
|
153
|
-
|
|
154
|
-
return {
|
|
155
|
-
marketData,
|
|
156
|
-
assetsData,
|
|
157
|
-
};
|
|
158
|
-
};
|
|
159
|
-
|
|
160
|
-
// export const getEulerV2AccountBalances = async (
|
|
161
|
-
// web3: Web3,
|
|
162
|
-
// network: NetworkNumber,
|
|
163
|
-
// selectedMarket: EulerV2MarketData
|
|
164
|
-
// ): Promise<> => {
|
|
165
|
-
//
|
|
166
|
-
// }
|
|
167
|
-
|
|
168
|
-
export const EMPTY_EULER_V2_DATA = {
|
|
169
|
-
usedAssets: {},
|
|
170
|
-
suppliedUsd: '0',
|
|
171
|
-
borrowedUsd: '0',
|
|
172
|
-
borrowLimitUsd: '0',
|
|
173
|
-
leftToBorrowUsd: '0',
|
|
174
|
-
ratio: '0',
|
|
175
|
-
minRatio: '0',
|
|
176
|
-
netApy: '0',
|
|
177
|
-
incentiveUsd: '0',
|
|
178
|
-
totalInterestUsd: '0',
|
|
179
|
-
isSubscribedToAutomation: false,
|
|
180
|
-
automationResubscribeRequired: false,
|
|
181
|
-
borrowVault: '',
|
|
182
|
-
borrowAmountInUnit: '0',
|
|
183
|
-
inLockDownMode: false,
|
|
184
|
-
inPermitDisabledMode: false,
|
|
185
|
-
lastUpdated: Date.now(),
|
|
186
|
-
hasBorrowInDifferentVault: false,
|
|
187
|
-
};
|
|
188
|
-
|
|
189
|
-
export const getEulerV2AccountData = async (
|
|
190
|
-
web3: Web3,
|
|
191
|
-
network: NetworkNumber,
|
|
192
|
-
address: string,
|
|
193
|
-
extractedState: ({
|
|
194
|
-
selectedMarket: EulerV2Market,
|
|
195
|
-
assetsData: EulerV2AssetsData,
|
|
196
|
-
marketData: EulerV2MarketInfoData,
|
|
197
|
-
}),
|
|
198
|
-
): Promise<EulerV2PositionData> => {
|
|
199
|
-
if (!address) throw new Error('No address provided');
|
|
200
|
-
|
|
201
|
-
const {
|
|
202
|
-
selectedMarket, assetsData, marketData,
|
|
203
|
-
} = extractedState;
|
|
204
|
-
|
|
205
|
-
let payload = {
|
|
206
|
-
...EMPTY_EULER_V2_DATA,
|
|
207
|
-
lastUpdated: Date.now(),
|
|
208
|
-
};
|
|
209
|
-
|
|
210
|
-
const isInUSD = marketData.isInUSD;
|
|
211
|
-
|
|
212
|
-
// TODO: maybe not the best practice to get decimals from tokens package
|
|
213
|
-
const parsingDecimals = isInUSD ? 18 : getAssetInfoByAddress(marketData.unitOfAccount).decimals;
|
|
214
|
-
const contract = EulerV2ViewContract(web3, network);
|
|
215
|
-
|
|
216
|
-
const loanData = await contract.methods.getUserData(address).call();
|
|
217
|
-
const usedAssets: EulerV2UsedAssets = {};
|
|
218
|
-
|
|
219
|
-
// there is no user position check for a specific market, only global check
|
|
220
|
-
// but we need to make sure it works for the UI and show position only for the selected market
|
|
221
|
-
if (!compareAddresses(loanData.borrowVault, selectedMarket.marketAddress)) {
|
|
222
|
-
payload = {
|
|
223
|
-
...payload,
|
|
224
|
-
borrowVault: ZERO_ADDRESS,
|
|
225
|
-
borrowAmountInUnit: '0',
|
|
226
|
-
inLockDownMode: false,
|
|
227
|
-
inPermitDisabledMode: false,
|
|
228
|
-
hasBorrowInDifferentVault: !compareAddresses(loanData.borrowVault, ZERO_ADDRESS),
|
|
229
|
-
};
|
|
230
|
-
} else {
|
|
231
|
-
payload = {
|
|
232
|
-
...payload,
|
|
233
|
-
borrowVault: loanData.borrowVault,
|
|
234
|
-
borrowAmountInUnit: loanData.borrowAmountInUnit,
|
|
235
|
-
inLockDownMode: loanData.inLockDownMode,
|
|
236
|
-
inPermitDisabledMode: loanData.inPermitDisabledMode,
|
|
237
|
-
hasBorrowInDifferentVault: false,
|
|
238
|
-
};
|
|
239
|
-
|
|
240
|
-
const borrowedInUnit = getEthAmountForDecimals(loanData.borrowAmountInUnit, parsingDecimals);
|
|
241
|
-
const borrowedInAsset = getEthAmountForDecimals(loanData.borrowAmountInAsset, marketData.decimals);
|
|
242
|
-
const borrowVault = loanData.borrowVault;
|
|
243
|
-
|
|
244
|
-
if (borrowVault && !compareAddresses(ZERO_ADDRESS, borrowVault) && borrowedInUnit) {
|
|
245
|
-
const borrowInfo = assetsData[borrowVault.toLowerCase()];
|
|
246
|
-
usedAssets[borrowVault.toLowerCase()] = {
|
|
247
|
-
...EMPTY_USED_ASSET,
|
|
248
|
-
isBorrowed: true,
|
|
249
|
-
borrowed: borrowedInAsset,
|
|
250
|
-
borrowedUsd: isInUSD ? borrowedInUnit : new Dec(borrowedInUnit).mul(marketData.unitOfAccountUsdPrice).toString(),
|
|
251
|
-
vaultAddress: loanData.borrowVault,
|
|
252
|
-
symbol: borrowInfo.symbol,
|
|
253
|
-
};
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
loanData.collaterals.forEach((collateral, i) => {
|
|
258
|
-
const key = collateral.collateralVault.toLowerCase();
|
|
259
|
-
const collInfo = assetsData[key];
|
|
260
|
-
|
|
261
|
-
if (!collInfo || !marketData.collaterals.map(a => a.toLowerCase()).includes(key)) return; // this is a token supplied but not being used as a collateral for the market
|
|
262
|
-
|
|
263
|
-
const suppliedInUnit = getEthAmountForDecimals(collateral.collateralAmountInUnit, parsingDecimals);
|
|
264
|
-
const suppliedInAsset = getEthAmountForDecimals(collateral.collateralAmountInAsset, collInfo.decimals);
|
|
265
|
-
const collateralAmountInUSD = getEthAmountForDecimals(collateral.collateralAmountInUSD, 18);
|
|
266
|
-
usedAssets[key] = {
|
|
267
|
-
...EMPTY_USED_ASSET,
|
|
268
|
-
collateral: true,
|
|
269
|
-
isSupplied: true,
|
|
270
|
-
supplied: suppliedInAsset,
|
|
271
|
-
suppliedUsd: collateralAmountInUSD,
|
|
272
|
-
vaultAddress: collateral.collateralVault,
|
|
273
|
-
symbol: collInfo.symbol,
|
|
274
|
-
};
|
|
275
|
-
});
|
|
276
|
-
|
|
277
|
-
payload = {
|
|
278
|
-
...payload,
|
|
279
|
-
usedAssets,
|
|
280
|
-
...getEulerV2AggregatedData({
|
|
281
|
-
usedAssets, assetsData, network,
|
|
282
|
-
}),
|
|
283
|
-
};
|
|
284
|
-
|
|
285
|
-
return payload;
|
|
286
|
-
};
|
|
@@ -1,231 +0,0 @@
|
|
|
1
|
-
import Dec from 'decimal.js';
|
|
2
|
-
import Web3 from 'web3';
|
|
3
|
-
import { assetAmountInWei } from '@defisaver/tokens';
|
|
4
|
-
import {
|
|
5
|
-
EthAddress, NetworkNumber,
|
|
6
|
-
} from '../../types/common';
|
|
7
|
-
import {
|
|
8
|
-
calcLeverageLiqPrice, getAssetsTotal, STABLE_ASSETS,
|
|
9
|
-
} from '../../moneymarket';
|
|
10
|
-
import { calculateInterestEarned } from '../../staking';
|
|
11
|
-
import {
|
|
12
|
-
EulerV2AggregatedPositionData,
|
|
13
|
-
EulerV2AssetsData,
|
|
14
|
-
EulerV2Market,
|
|
15
|
-
EulerV2UsedAssets,
|
|
16
|
-
} from '../../types';
|
|
17
|
-
import { EulerV2ViewContract } from '../../contracts';
|
|
18
|
-
import { borrowOperations } from '../../constants';
|
|
19
|
-
import { multicall } from '../../multicall';
|
|
20
|
-
|
|
21
|
-
export const isLeveragedPos = (usedAssets: EulerV2UsedAssets, dustLimit = 5) => {
|
|
22
|
-
let borrowUnstable = 0;
|
|
23
|
-
let supplyStable = 0;
|
|
24
|
-
let borrowStable = 0;
|
|
25
|
-
let supplyUnstable = 0;
|
|
26
|
-
let longAsset = '';
|
|
27
|
-
let shortAsset = '';
|
|
28
|
-
let leverageAssetVault = '';
|
|
29
|
-
Object.values(usedAssets).forEach(({
|
|
30
|
-
symbol, suppliedUsd, borrowedUsd, collateral, vaultAddress,
|
|
31
|
-
}) => {
|
|
32
|
-
const isSupplied = (+suppliedUsd) > dustLimit; // ignore dust like <limit leftover supply
|
|
33
|
-
const isBorrowed = (+borrowedUsd) > dustLimit; // ignore dust like <limit leftover supply
|
|
34
|
-
if (isSupplied && STABLE_ASSETS.includes(symbol) && collateral) supplyStable += 1;
|
|
35
|
-
if (isBorrowed && STABLE_ASSETS.includes(symbol)) borrowStable += 1;
|
|
36
|
-
if (isBorrowed && !STABLE_ASSETS.includes(symbol)) {
|
|
37
|
-
borrowUnstable += 1;
|
|
38
|
-
shortAsset = symbol;
|
|
39
|
-
leverageAssetVault = vaultAddress;
|
|
40
|
-
}
|
|
41
|
-
if (isSupplied && !STABLE_ASSETS.includes(symbol) && collateral) {
|
|
42
|
-
supplyUnstable += 1;
|
|
43
|
-
longAsset = symbol;
|
|
44
|
-
leverageAssetVault = vaultAddress;
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
const isLong = borrowStable > 0 && borrowUnstable === 0 && supplyUnstable === 1 && supplyStable === 0;
|
|
48
|
-
const isShort = supplyStable > 0 && supplyUnstable === 0 && borrowUnstable === 1 && borrowStable === 0;
|
|
49
|
-
// lsd -> liquid staking derivative
|
|
50
|
-
const isLsdLeveraged = supplyUnstable === 1 && borrowUnstable === 1 && shortAsset === 'ETH' && ['stETH', 'wstETH', 'cbETH', 'rETH'].includes(longAsset);
|
|
51
|
-
if (isLong) {
|
|
52
|
-
return {
|
|
53
|
-
leveragedType: 'long',
|
|
54
|
-
leveragedAsset: longAsset,
|
|
55
|
-
leveragedVault: leverageAssetVault,
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
if (isShort) {
|
|
59
|
-
return {
|
|
60
|
-
leveragedType: 'short',
|
|
61
|
-
leveragedAsset: shortAsset,
|
|
62
|
-
leveragedVault: leverageAssetVault,
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
if (isLsdLeveraged) {
|
|
66
|
-
return {
|
|
67
|
-
leveragedType: 'lsd-leverage',
|
|
68
|
-
leveragedAsset: longAsset,
|
|
69
|
-
leveragedVault: leverageAssetVault,
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
return {
|
|
73
|
-
leveragedType: '',
|
|
74
|
-
leveragedAsset: '',
|
|
75
|
-
leveragedVault: '',
|
|
76
|
-
};
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
export const calculateNetApy = (usedAssets: EulerV2UsedAssets, assetsData: EulerV2AssetsData) => {
|
|
80
|
-
const sumValues = Object.values(usedAssets).reduce((_acc, usedAsset) => {
|
|
81
|
-
const acc = { ..._acc };
|
|
82
|
-
const assetData = assetsData[usedAsset.vaultAddress.toLowerCase()];
|
|
83
|
-
|
|
84
|
-
if (usedAsset.isSupplied) {
|
|
85
|
-
const amount = usedAsset.suppliedUsd;
|
|
86
|
-
acc.suppliedUsd = new Dec(acc.suppliedUsd).add(amount).toString();
|
|
87
|
-
const rate = assetData.supplyRate;
|
|
88
|
-
const supplyInterest = calculateInterestEarned(amount, rate as string, 'year', true);
|
|
89
|
-
acc.supplyInterest = new Dec(acc.supplyInterest).add(supplyInterest.toString()).toString();
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
if (usedAsset.isBorrowed) {
|
|
93
|
-
const amount = usedAsset.borrowedUsd;
|
|
94
|
-
acc.borrowedUsd = new Dec(acc.borrowedUsd).add(amount).toString();
|
|
95
|
-
const rate = assetData.borrowRate;
|
|
96
|
-
const borrowInterest = calculateInterestEarned(amount, rate as string, 'year', true);
|
|
97
|
-
acc.borrowInterest = new Dec(acc.borrowInterest).sub(borrowInterest.toString()).toString();
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
return acc;
|
|
101
|
-
}, {
|
|
102
|
-
borrowInterest: '0', supplyInterest: '0', incentiveUsd: '0', borrowedUsd: '0', suppliedUsd: '0',
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
const {
|
|
106
|
-
borrowedUsd, suppliedUsd, borrowInterest, supplyInterest, incentiveUsd,
|
|
107
|
-
} = sumValues;
|
|
108
|
-
|
|
109
|
-
const totalInterestUsd = new Dec(borrowInterest).add(supplyInterest).add(incentiveUsd).toString();
|
|
110
|
-
const balance = new Dec(suppliedUsd).sub(borrowedUsd);
|
|
111
|
-
const netApy = new Dec(totalInterestUsd).div(balance).times(100).toString();
|
|
112
|
-
|
|
113
|
-
return { netApy, totalInterestUsd, incentiveUsd };
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
export const getEulerV2AggregatedData = ({
|
|
117
|
-
usedAssets, assetsData, network, ...rest
|
|
118
|
-
}: { usedAssets: EulerV2UsedAssets, assetsData: EulerV2AssetsData, network: NetworkNumber }) => {
|
|
119
|
-
const payload = {} as EulerV2AggregatedPositionData;
|
|
120
|
-
payload.suppliedUsd = getAssetsTotal(usedAssets, ({ isSupplied }: { isSupplied: boolean }) => isSupplied, ({ suppliedUsd }: { suppliedUsd: string }) => suppliedUsd);
|
|
121
|
-
payload.suppliedCollateralUsd = getAssetsTotal(usedAssets, ({ isSupplied, collateral }: { isSupplied: boolean, collateral: boolean }) => isSupplied && collateral, ({ suppliedUsd }: { suppliedUsd: string }) => suppliedUsd);
|
|
122
|
-
payload.borrowedUsd = getAssetsTotal(usedAssets, ({ isBorrowed }: { isBorrowed: boolean }) => isBorrowed, ({ borrowedUsd }: { borrowedUsd: string }) => borrowedUsd);
|
|
123
|
-
payload.borrowLimitUsd = getAssetsTotal(usedAssets, ({ isSupplied, collateral }: { isSupplied: boolean, collateral: boolean }) => isSupplied && collateral, ({ vaultAddress, suppliedUsd }: { vaultAddress: string, suppliedUsd: string }) => new Dec(suppliedUsd).mul(assetsData[vaultAddress.toLowerCase()].collateralFactor));
|
|
124
|
-
payload.liquidationLimitUsd = getAssetsTotal(usedAssets, ({ isSupplied, collateral }: { isSupplied: boolean, collateral: boolean }) => isSupplied && collateral, ({ vaultAddress, suppliedUsd }: { vaultAddress: string, suppliedUsd: string }) => new Dec(suppliedUsd).mul(assetsData[vaultAddress.toLowerCase()].liquidationRatio));
|
|
125
|
-
const leftToBorrowUsd = new Dec(payload.borrowLimitUsd).sub(payload.borrowedUsd);
|
|
126
|
-
payload.leftToBorrowUsd = leftToBorrowUsd.lte('0') ? '0' : leftToBorrowUsd.toString();
|
|
127
|
-
payload.ratio = +payload.suppliedUsd ? new Dec(payload.borrowLimitUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
|
|
128
|
-
payload.collRatio = +payload.suppliedUsd ? new Dec(payload.suppliedCollateralUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
|
|
129
|
-
const { netApy, incentiveUsd, totalInterestUsd } = calculateNetApy(usedAssets, assetsData);
|
|
130
|
-
payload.netApy = netApy;
|
|
131
|
-
payload.incentiveUsd = incentiveUsd;
|
|
132
|
-
payload.totalInterestUsd = totalInterestUsd;
|
|
133
|
-
payload.minRatio = '100';
|
|
134
|
-
payload.liqRatio = new Dec(payload.borrowLimitUsd).div(payload.liquidationLimitUsd).toString();
|
|
135
|
-
payload.liqPercent = new Dec(payload.borrowLimitUsd).div(payload.liquidationLimitUsd).mul(100).toString();
|
|
136
|
-
const { leveragedType, leveragedAsset, leveragedVault } = isLeveragedPos(usedAssets);
|
|
137
|
-
payload.leveragedType = leveragedType;
|
|
138
|
-
if (leveragedType !== '') {
|
|
139
|
-
payload.leveragedAsset = leveragedAsset;
|
|
140
|
-
let assetPrice = assetsData[leveragedVault.toLowerCase()].price;
|
|
141
|
-
if (leveragedType === 'lsd-leverage') {
|
|
142
|
-
const ethAsset = Object.values(assetsData).find((asset) => ['WETH', 'ETH'].includes(asset.symbol));
|
|
143
|
-
if (ethAsset) {
|
|
144
|
-
payload.leveragedLsdAssetRatio = new Dec(assetsData[leveragedVault.toLowerCase()].price).div(ethAsset.price).toString();
|
|
145
|
-
assetPrice = new Dec(assetPrice).div(ethAsset.price).toString();
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
payload.liquidationPrice = calcLeverageLiqPrice(leveragedType, assetPrice, payload.borrowedUsd, payload.liquidationLimitUsd);
|
|
149
|
-
}
|
|
150
|
-
return payload;
|
|
151
|
-
};
|
|
152
|
-
|
|
153
|
-
export const getEulerV2BorrowRate = (interestRate: string) => {
|
|
154
|
-
const _interestRate = new Dec(interestRate).div(1e27).toString();
|
|
155
|
-
const secondsPerYear = 31556953;
|
|
156
|
-
const a = new Dec(1).plus(_interestRate).pow(secondsPerYear - 1).toString();
|
|
157
|
-
return new Dec(new Dec(a).minus(1)).mul(100).toString();
|
|
158
|
-
};
|
|
159
|
-
|
|
160
|
-
export const getUtilizationRate = (totalBorrows: string, totalAssets: string) => new Dec(totalBorrows).div(totalAssets).toString();
|
|
161
|
-
|
|
162
|
-
export const getEulerV2SupplyRate = (borrowRate: string, utilizationRate: string, _interestFee: string) => {
|
|
163
|
-
const interestFee = new Dec(_interestFee).div(10000);
|
|
164
|
-
const fee = new Dec(1).minus(interestFee);
|
|
165
|
-
return new Dec(borrowRate).mul(utilizationRate).mul(fee).toString();
|
|
166
|
-
};
|
|
167
|
-
|
|
168
|
-
const getLiquidityChanges = (action: string, amount: string, isBorrowOperation: boolean) => {
|
|
169
|
-
let liquidityAdded;
|
|
170
|
-
let liquidityRemoved;
|
|
171
|
-
if (isBorrowOperation) {
|
|
172
|
-
liquidityAdded = action === 'payback' ? amount : '0';
|
|
173
|
-
liquidityRemoved = action === 'borrow' ? amount : '0';
|
|
174
|
-
} else {
|
|
175
|
-
liquidityAdded = action === 'collateral' ? amount : '0';
|
|
176
|
-
liquidityRemoved = action === 'withdraw' ? amount : '0';
|
|
177
|
-
}
|
|
178
|
-
return { liquidityAdded, liquidityRemoved };
|
|
179
|
-
};
|
|
180
|
-
|
|
181
|
-
export const getApyAfterValuesEstimationEulerV2 = async (actions: { action: string, amount: string, asset: string, vaultAddress: EthAddress }[], web3: Web3, network: NetworkNumber) => {
|
|
182
|
-
const eulerV2ViewContract = EulerV2ViewContract(web3, network);
|
|
183
|
-
const multicallData: any[] = [];
|
|
184
|
-
const apyAfterValuesEstimationParams: any[] = [];
|
|
185
|
-
actions.forEach(({
|
|
186
|
-
action, amount, asset, vaultAddress,
|
|
187
|
-
}) => {
|
|
188
|
-
const amountInWei = assetAmountInWei(amount, asset);
|
|
189
|
-
const isBorrowOperation = borrowOperations.includes(action);
|
|
190
|
-
const { liquidityAdded, liquidityRemoved } = getLiquidityChanges(action, amountInWei, isBorrowOperation);
|
|
191
|
-
apyAfterValuesEstimationParams.push([
|
|
192
|
-
vaultAddress,
|
|
193
|
-
borrowOperations.includes(action),
|
|
194
|
-
liquidityAdded,
|
|
195
|
-
liquidityRemoved,
|
|
196
|
-
]);
|
|
197
|
-
multicallData.push({
|
|
198
|
-
target: eulerV2ViewContract.options.address,
|
|
199
|
-
abiItem: eulerV2ViewContract.options.jsonInterface.find(({ name }) => name === 'getVaultInfoFull'),
|
|
200
|
-
params: [vaultAddress],
|
|
201
|
-
});
|
|
202
|
-
});
|
|
203
|
-
multicallData.push({
|
|
204
|
-
target: eulerV2ViewContract.options.address,
|
|
205
|
-
abiItem: eulerV2ViewContract.options.jsonInterface.find(({ name }) => name === 'getApyAfterValuesEstimation'),
|
|
206
|
-
params: [apyAfterValuesEstimationParams],
|
|
207
|
-
});
|
|
208
|
-
const multicallRes = await multicall(multicallData, web3, network);
|
|
209
|
-
const numOfActions = actions.length;
|
|
210
|
-
const data: any = {};
|
|
211
|
-
for (let i = 0; i < numOfActions; i += 1) {
|
|
212
|
-
const _interestRate = multicallRes[numOfActions].estimatedBorrowRates[i];
|
|
213
|
-
const vaultInfo = multicallRes[i][0];
|
|
214
|
-
const decimals = vaultInfo.decimals;
|
|
215
|
-
const borrowRate = getEulerV2BorrowRate(_interestRate);
|
|
216
|
-
|
|
217
|
-
const amount = new Dec(actions[i].amount).mul(10 ** decimals).toString();
|
|
218
|
-
const action = actions[i].action;
|
|
219
|
-
const isBorrowOperation = borrowOperations.includes(action);
|
|
220
|
-
const { liquidityAdded, liquidityRemoved } = getLiquidityChanges(action, amount, isBorrowOperation);
|
|
221
|
-
|
|
222
|
-
const totalBorrows = new Dec(vaultInfo.totalBorrows).add(isBorrowOperation ? liquidityRemoved : '0').sub(isBorrowOperation ? liquidityAdded : '0').toString();
|
|
223
|
-
const totalAssets = new Dec(vaultInfo.totalAssets).add(isBorrowOperation ? '0' : liquidityAdded).sub(isBorrowOperation ? '0' : liquidityRemoved).toString();
|
|
224
|
-
const utilizationRate = getUtilizationRate(totalBorrows, totalAssets);
|
|
225
|
-
data[vaultInfo.vaultAddr.toLowerCase()] = {
|
|
226
|
-
borrowRate,
|
|
227
|
-
supplyRate: getEulerV2SupplyRate(borrowRate, utilizationRate, vaultInfo.interestFee),
|
|
228
|
-
};
|
|
229
|
-
}
|
|
230
|
-
return data;
|
|
231
|
-
};
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { NetworkNumber } from '../../types/common';
|
|
2
|
-
import { EulerV2Market, EulerV2Versions } from '../../types';
|
|
3
|
-
|
|
4
|
-
export const eUSDC2 = (networkId: NetworkNumber): EulerV2Market => ({
|
|
5
|
-
chainIds: [NetworkNumber.Eth],
|
|
6
|
-
label: 'Euler Prime USDC',
|
|
7
|
-
shortLabel: 'eUSDC-2',
|
|
8
|
-
value: EulerV2Versions.eUSDC2,
|
|
9
|
-
asset: 'USDC',
|
|
10
|
-
secondLabel: 'Market',
|
|
11
|
-
marketAddress: '0x797DD80692c3b2dAdabCe8e30C07fDE5307D48a9',
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
export const eUSDC8 = (networkId: NetworkNumber): EulerV2Market => ({
|
|
15
|
-
chainIds: [NetworkNumber.Eth],
|
|
16
|
-
label: 'Stablecoin Maxi USDC',
|
|
17
|
-
shortLabel: 'eUSDC-8',
|
|
18
|
-
value: EulerV2Versions.eUSDC8,
|
|
19
|
-
asset: 'USDC',
|
|
20
|
-
secondLabel: 'Market',
|
|
21
|
-
marketAddress: '0xce45EF0414dE3516cAF1BCf937bF7F2Cf67873De',
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
export const eWETH2 = (networkId: NetworkNumber): EulerV2Market => ({
|
|
25
|
-
chainIds: [NetworkNumber.Eth],
|
|
26
|
-
label: 'Euler Prime WETH',
|
|
27
|
-
shortLabel: 'eWETH-2',
|
|
28
|
-
value: EulerV2Versions.eWETH2,
|
|
29
|
-
asset: 'WETH',
|
|
30
|
-
secondLabel: 'Market',
|
|
31
|
-
marketAddress: '0xD8b27CF359b7D15710a5BE299AF6e7Bf904984C2',
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
export const EulerV2Markets = (networkId: NetworkNumber) => ({
|
|
35
|
-
[EulerV2Versions.eUSDC2]: eUSDC2(networkId),
|
|
36
|
-
[EulerV2Versions.eUSDC8]: eUSDC8(networkId),
|
|
37
|
-
[EulerV2Versions.eWETH2]: eWETH2(networkId),
|
|
38
|
-
}) as const;
|