@defisaver/positions-sdk 1.0.3-dev → 1.0.3
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/curveUsd/index.js +1 -8
- package/cjs/helpers/morphoBlueHelpers/index.js +66 -66
- package/cjs/markets/compound/marketsAssets.js +1 -1
- package/cjs/markets/spark/marketAssets.js +1 -1
- package/cjs/types/curveUsd.d.ts +0 -1
- package/esm/curveUsd/index.js +3 -10
- package/esm/helpers/morphoBlueHelpers/index.js +66 -66
- package/esm/markets/compound/marketsAssets.js +1 -1
- package/esm/markets/spark/marketAssets.js +1 -1
- package/esm/types/curveUsd.d.ts +0 -1
- package/package.json +52 -52
- 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 +1147 -1147
- package/src/constants/index.ts +6 -6
- package/src/contracts.ts +134 -134
- package/src/curveUsd/index.ts +230 -239
- package/src/eulerV2/index.ts +303 -303
- package/src/exchange/index.ts +17 -17
- package/src/fluid/index.ts +354 -354
- 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/fluidHelpers/index.ts +53 -53
- package/src/helpers/index.ts +11 -11
- package/src/helpers/liquityV2Helpers/index.ts +80 -80
- 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 +52 -52
- package/src/liquity/index.ts +116 -116
- package/src/liquityV2/index.ts +295 -295
- 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/fluid/index.ts +2012 -2012
- package/src/markets/index.ts +27 -27
- package/src/markets/liquityV2/index.ts +54 -54
- 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 +630 -630
- package/src/morphoBlue/index.ts +202 -202
- package/src/multicall/index.ts +33 -33
- package/src/services/priceService.ts +91 -91
- package/src/services/utils.ts +59 -59
- package/src/setup.ts +8 -8
- package/src/spark/index.ts +460 -460
- 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 -119
- package/src/types/euler.ts +171 -171
- package/src/types/fluid.ts +266 -266
- package/src/types/index.ts +11 -11
- package/src/types/liquity.ts +30 -30
- package/src/types/liquityV2.ts +119 -119
- 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
|
@@ -1,151 +1,151 @@
|
|
|
1
|
-
import Dec from 'decimal.js';
|
|
2
|
-
import Web3 from 'web3';
|
|
3
|
-
import { assetAmountInWei, getAssetInfo, getAssetInfoByAddress } from '@defisaver/tokens';
|
|
4
|
-
import {
|
|
5
|
-
aprToApy, calcLeverageLiqPrice, getAssetsTotal, isLeveragedPos,
|
|
6
|
-
} from '../../moneymarket';
|
|
7
|
-
import {
|
|
8
|
-
SparkAggregatedPositionData,
|
|
9
|
-
SparkAssetsData, SparkHelperCommon, SparkMarketData, SparkUsedAssets,
|
|
10
|
-
} from '../../types';
|
|
11
|
-
import { calculateNetApy } from '../../staking';
|
|
12
|
-
import { ethToWeth, wethToEth } from '../../services/utils';
|
|
13
|
-
import { SparkViewContract } from '../../contracts';
|
|
14
|
-
import { NetworkNumber } from '../../types/common';
|
|
15
|
-
import { borrowOperations } from '../../constants';
|
|
16
|
-
|
|
17
|
-
export const sparkIsInIsolationMode = ({ usedAssets, assetsData }: { usedAssets: SparkUsedAssets, assetsData: SparkAssetsData }) => Object.values(usedAssets).some(({ symbol, collateral }) => collateral && assetsData[symbol].isIsolated);
|
|
18
|
-
|
|
19
|
-
export const sparkGetCollSuppliedAssets = ({ usedAssets }: { usedAssets: SparkUsedAssets }) => Object.values(usedAssets).filter(({ isSupplied, collateral }) => isSupplied && collateral);
|
|
20
|
-
|
|
21
|
-
export const sparkGetSuppliableAssets = ({
|
|
22
|
-
usedAssets, eModeCategory, eModeCategories, assetsData, selectedMarket, network, ...rest
|
|
23
|
-
}: SparkHelperCommon) => {
|
|
24
|
-
const data = {
|
|
25
|
-
usedAssets, eModeCategory, eModeCategories, assetsData, selectedMarket, network, ...rest,
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
const collAccountAssets = sparkGetCollSuppliedAssets(data);
|
|
29
|
-
const marketAssets = Object.values(assetsData);
|
|
30
|
-
|
|
31
|
-
if (sparkIsInIsolationMode(data)) {
|
|
32
|
-
const collAsset = collAccountAssets[0].symbol;
|
|
33
|
-
return marketAssets.filter(d => d.canBeSupplied).map(({ symbol }) => ({ symbol, canBeCollateral: symbol === collAsset }));
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return marketAssets.filter(d => d.canBeSupplied).map(({ symbol, isIsolated }) => ({ symbol, canBeCollateral: !isIsolated }));
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
export const sparkGetSuppliableAsCollAssets = ({
|
|
40
|
-
usedAssets, eModeCategory, eModeCategories, assetsData, selectedMarket, network, ...rest
|
|
41
|
-
}: SparkHelperCommon) => sparkGetSuppliableAssets({
|
|
42
|
-
usedAssets, eModeCategory, eModeCategories, assetsData, selectedMarket, network, ...rest,
|
|
43
|
-
}).filter(({ canBeCollateral }) => canBeCollateral);
|
|
44
|
-
|
|
45
|
-
export const sparkGetEmodeMutableProps = ({
|
|
46
|
-
eModeCategory,
|
|
47
|
-
assetsData,
|
|
48
|
-
}: SparkHelperCommon,
|
|
49
|
-
_asset: string) => {
|
|
50
|
-
const asset = wethToEth(_asset);
|
|
51
|
-
|
|
52
|
-
const assetData = assetsData[asset];
|
|
53
|
-
if (
|
|
54
|
-
eModeCategory === 0
|
|
55
|
-
|| assetData.eModeCategory !== eModeCategory
|
|
56
|
-
|| new Dec(assetData?.eModeCategoryData?.collateralFactor || 0).eq(0)
|
|
57
|
-
) {
|
|
58
|
-
const { liquidationRatio, collateralFactor } = assetData;
|
|
59
|
-
return ({ liquidationRatio, collateralFactor });
|
|
60
|
-
}
|
|
61
|
-
const { liquidationRatio, collateralFactor } = assetData.eModeCategoryData;
|
|
62
|
-
return ({ liquidationRatio, collateralFactor });
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
export const sparkGetAggregatedPositionData = ({
|
|
66
|
-
usedAssets,
|
|
67
|
-
eModeCategory,
|
|
68
|
-
eModeCategories,
|
|
69
|
-
assetsData,
|
|
70
|
-
selectedMarket,
|
|
71
|
-
network,
|
|
72
|
-
...rest
|
|
73
|
-
}: SparkHelperCommon): SparkAggregatedPositionData => {
|
|
74
|
-
const data = {
|
|
75
|
-
usedAssets, eModeCategory, eModeCategories, assetsData, selectedMarket, network, ...rest,
|
|
76
|
-
};
|
|
77
|
-
const payload = {} as SparkAggregatedPositionData;
|
|
78
|
-
payload.suppliedUsd = getAssetsTotal(usedAssets, ({ isSupplied }: { isSupplied: boolean }) => isSupplied, ({ suppliedUsd }: { suppliedUsd: string }) => suppliedUsd);
|
|
79
|
-
payload.suppliedCollateralUsd = getAssetsTotal(usedAssets, ({ isSupplied, collateral }: { isSupplied: boolean, collateral: boolean }) => isSupplied && collateral, ({ suppliedUsd }: { suppliedUsd: string }) => suppliedUsd);
|
|
80
|
-
payload.borrowedUsd = getAssetsTotal(usedAssets, ({ isBorrowed }: { isBorrowed: boolean }) => isBorrowed, ({ borrowedUsd }: { borrowedUsd: string }) => borrowedUsd);
|
|
81
|
-
payload.borrowLimitUsd = getAssetsTotal(
|
|
82
|
-
usedAssets,
|
|
83
|
-
({ isSupplied, collateral }: { isSupplied: boolean, collateral: boolean }) => isSupplied && collateral,
|
|
84
|
-
({ symbol, suppliedUsd }: { symbol: string, suppliedUsd: string }) => new Dec(suppliedUsd).mul(sparkGetEmodeMutableProps(data, symbol).collateralFactor),
|
|
85
|
-
);
|
|
86
|
-
payload.liquidationLimitUsd = getAssetsTotal(
|
|
87
|
-
usedAssets,
|
|
88
|
-
({ isSupplied, collateral }: { isSupplied: boolean, collateral: boolean }) => isSupplied && collateral,
|
|
89
|
-
({ symbol, suppliedUsd }: { symbol: string, suppliedUsd: string }) => new Dec(suppliedUsd).mul(sparkGetEmodeMutableProps(data, symbol).liquidationRatio),
|
|
90
|
-
);
|
|
91
|
-
const leftToBorrowUsd = new Dec(payload.borrowLimitUsd).sub(payload.borrowedUsd);
|
|
92
|
-
payload.leftToBorrowUsd = leftToBorrowUsd.lte('0') ? '0' : leftToBorrowUsd.toString();
|
|
93
|
-
payload.ratio = +payload.suppliedUsd ? new Dec(payload.borrowLimitUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
|
|
94
|
-
payload.collRatio = +payload.suppliedUsd ? new Dec(payload.suppliedCollateralUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
|
|
95
|
-
const { netApy, incentiveUsd, totalInterestUsd } = calculateNetApy({ usedAssets, assetsData });
|
|
96
|
-
payload.netApy = netApy;
|
|
97
|
-
payload.incentiveUsd = incentiveUsd;
|
|
98
|
-
payload.totalInterestUsd = totalInterestUsd;
|
|
99
|
-
payload.liqRatio = new Dec(payload.borrowLimitUsd).div(payload.liquidationLimitUsd).toString();
|
|
100
|
-
payload.liqPercent = new Dec(payload.borrowLimitUsd).div(payload.liquidationLimitUsd).mul(100).toString();
|
|
101
|
-
const { leveragedType, leveragedAsset } = isLeveragedPos(usedAssets);
|
|
102
|
-
payload.leveragedType = leveragedType;
|
|
103
|
-
if (leveragedType !== '') {
|
|
104
|
-
payload.leveragedAsset = leveragedAsset;
|
|
105
|
-
let assetPrice = data.assetsData[leveragedAsset].price; // TODO sparkPrice or price??
|
|
106
|
-
if (leveragedType === 'lsd-leverage') {
|
|
107
|
-
// Treat ETH like a stablecoin in a long stETH position
|
|
108
|
-
payload.leveragedLsdAssetRatio = new Dec(assetsData[leveragedAsset].price).div(assetsData.ETH.price).toDP(18).toString();
|
|
109
|
-
assetPrice = new Dec(assetPrice).div(assetsData.ETH.price).toString();
|
|
110
|
-
}
|
|
111
|
-
payload.liquidationPrice = calcLeverageLiqPrice(leveragedType, assetPrice, payload.borrowedUsd, payload.liquidationLimitUsd);
|
|
112
|
-
}
|
|
113
|
-
return payload;
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
export const getApyAfterValuesEstimation = async (selectedMarket: SparkMarketData, actions: [{ action: string, amount: string, asset: string }], web3: Web3) => {
|
|
117
|
-
const sparkViewContract = SparkViewContract(web3, NetworkNumber.Eth);
|
|
118
|
-
const params = actions.map(({ action, asset, amount }: { action: string, amount: string, asset: string }) => {
|
|
119
|
-
const isDebtAsset = borrowOperations.includes(action);
|
|
120
|
-
const amountInWei = assetAmountInWei(amount, asset);
|
|
121
|
-
const assetInfo = getAssetInfo(ethToWeth(asset));
|
|
122
|
-
let liquidityAdded;
|
|
123
|
-
let liquidityTaken;
|
|
124
|
-
if (isDebtAsset) {
|
|
125
|
-
liquidityAdded = action === 'payback' ? amountInWei : '0';
|
|
126
|
-
liquidityTaken = action === 'borrow' ? amountInWei : '0';
|
|
127
|
-
} else {
|
|
128
|
-
liquidityAdded = action === 'collateral' ? amountInWei : '0';
|
|
129
|
-
liquidityTaken = action === 'withdraw' ? amountInWei : '0';
|
|
130
|
-
}
|
|
131
|
-
return {
|
|
132
|
-
reserveAddress: assetInfo.address,
|
|
133
|
-
liquidityAdded,
|
|
134
|
-
liquidityTaken,
|
|
135
|
-
isDebtAsset,
|
|
136
|
-
};
|
|
137
|
-
});
|
|
138
|
-
const data = await sparkViewContract.methods.getApyAfterValuesEstimation(
|
|
139
|
-
selectedMarket.providerAddress,
|
|
140
|
-
params,
|
|
141
|
-
).call();
|
|
142
|
-
const rates: { [key: string]: { supplyRate: string, borrowRate: string } } = {};
|
|
143
|
-
data.forEach((d) => {
|
|
144
|
-
const asset = wethToEth(getAssetInfoByAddress(d.reserveAddress).symbol);
|
|
145
|
-
rates[asset] = {
|
|
146
|
-
supplyRate: aprToApy(new Dec(d.supplyRate.toString()).div(1e25).toString()),
|
|
147
|
-
borrowRate: aprToApy(new Dec(d.variableBorrowRate.toString()).div(1e25).toString()),
|
|
148
|
-
};
|
|
149
|
-
});
|
|
150
|
-
return rates;
|
|
1
|
+
import Dec from 'decimal.js';
|
|
2
|
+
import Web3 from 'web3';
|
|
3
|
+
import { assetAmountInWei, getAssetInfo, getAssetInfoByAddress } from '@defisaver/tokens';
|
|
4
|
+
import {
|
|
5
|
+
aprToApy, calcLeverageLiqPrice, getAssetsTotal, isLeveragedPos,
|
|
6
|
+
} from '../../moneymarket';
|
|
7
|
+
import {
|
|
8
|
+
SparkAggregatedPositionData,
|
|
9
|
+
SparkAssetsData, SparkHelperCommon, SparkMarketData, SparkUsedAssets,
|
|
10
|
+
} from '../../types';
|
|
11
|
+
import { calculateNetApy } from '../../staking';
|
|
12
|
+
import { ethToWeth, wethToEth } from '../../services/utils';
|
|
13
|
+
import { SparkViewContract } from '../../contracts';
|
|
14
|
+
import { NetworkNumber } from '../../types/common';
|
|
15
|
+
import { borrowOperations } from '../../constants';
|
|
16
|
+
|
|
17
|
+
export const sparkIsInIsolationMode = ({ usedAssets, assetsData }: { usedAssets: SparkUsedAssets, assetsData: SparkAssetsData }) => Object.values(usedAssets).some(({ symbol, collateral }) => collateral && assetsData[symbol].isIsolated);
|
|
18
|
+
|
|
19
|
+
export const sparkGetCollSuppliedAssets = ({ usedAssets }: { usedAssets: SparkUsedAssets }) => Object.values(usedAssets).filter(({ isSupplied, collateral }) => isSupplied && collateral);
|
|
20
|
+
|
|
21
|
+
export const sparkGetSuppliableAssets = ({
|
|
22
|
+
usedAssets, eModeCategory, eModeCategories, assetsData, selectedMarket, network, ...rest
|
|
23
|
+
}: SparkHelperCommon) => {
|
|
24
|
+
const data = {
|
|
25
|
+
usedAssets, eModeCategory, eModeCategories, assetsData, selectedMarket, network, ...rest,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const collAccountAssets = sparkGetCollSuppliedAssets(data);
|
|
29
|
+
const marketAssets = Object.values(assetsData);
|
|
30
|
+
|
|
31
|
+
if (sparkIsInIsolationMode(data)) {
|
|
32
|
+
const collAsset = collAccountAssets[0].symbol;
|
|
33
|
+
return marketAssets.filter(d => d.canBeSupplied).map(({ symbol }) => ({ symbol, canBeCollateral: symbol === collAsset }));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return marketAssets.filter(d => d.canBeSupplied).map(({ symbol, isIsolated }) => ({ symbol, canBeCollateral: !isIsolated }));
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export const sparkGetSuppliableAsCollAssets = ({
|
|
40
|
+
usedAssets, eModeCategory, eModeCategories, assetsData, selectedMarket, network, ...rest
|
|
41
|
+
}: SparkHelperCommon) => sparkGetSuppliableAssets({
|
|
42
|
+
usedAssets, eModeCategory, eModeCategories, assetsData, selectedMarket, network, ...rest,
|
|
43
|
+
}).filter(({ canBeCollateral }) => canBeCollateral);
|
|
44
|
+
|
|
45
|
+
export const sparkGetEmodeMutableProps = ({
|
|
46
|
+
eModeCategory,
|
|
47
|
+
assetsData,
|
|
48
|
+
}: SparkHelperCommon,
|
|
49
|
+
_asset: string) => {
|
|
50
|
+
const asset = wethToEth(_asset);
|
|
51
|
+
|
|
52
|
+
const assetData = assetsData[asset];
|
|
53
|
+
if (
|
|
54
|
+
eModeCategory === 0
|
|
55
|
+
|| assetData.eModeCategory !== eModeCategory
|
|
56
|
+
|| new Dec(assetData?.eModeCategoryData?.collateralFactor || 0).eq(0)
|
|
57
|
+
) {
|
|
58
|
+
const { liquidationRatio, collateralFactor } = assetData;
|
|
59
|
+
return ({ liquidationRatio, collateralFactor });
|
|
60
|
+
}
|
|
61
|
+
const { liquidationRatio, collateralFactor } = assetData.eModeCategoryData;
|
|
62
|
+
return ({ liquidationRatio, collateralFactor });
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export const sparkGetAggregatedPositionData = ({
|
|
66
|
+
usedAssets,
|
|
67
|
+
eModeCategory,
|
|
68
|
+
eModeCategories,
|
|
69
|
+
assetsData,
|
|
70
|
+
selectedMarket,
|
|
71
|
+
network,
|
|
72
|
+
...rest
|
|
73
|
+
}: SparkHelperCommon): SparkAggregatedPositionData => {
|
|
74
|
+
const data = {
|
|
75
|
+
usedAssets, eModeCategory, eModeCategories, assetsData, selectedMarket, network, ...rest,
|
|
76
|
+
};
|
|
77
|
+
const payload = {} as SparkAggregatedPositionData;
|
|
78
|
+
payload.suppliedUsd = getAssetsTotal(usedAssets, ({ isSupplied }: { isSupplied: boolean }) => isSupplied, ({ suppliedUsd }: { suppliedUsd: string }) => suppliedUsd);
|
|
79
|
+
payload.suppliedCollateralUsd = getAssetsTotal(usedAssets, ({ isSupplied, collateral }: { isSupplied: boolean, collateral: boolean }) => isSupplied && collateral, ({ suppliedUsd }: { suppliedUsd: string }) => suppliedUsd);
|
|
80
|
+
payload.borrowedUsd = getAssetsTotal(usedAssets, ({ isBorrowed }: { isBorrowed: boolean }) => isBorrowed, ({ borrowedUsd }: { borrowedUsd: string }) => borrowedUsd);
|
|
81
|
+
payload.borrowLimitUsd = getAssetsTotal(
|
|
82
|
+
usedAssets,
|
|
83
|
+
({ isSupplied, collateral }: { isSupplied: boolean, collateral: boolean }) => isSupplied && collateral,
|
|
84
|
+
({ symbol, suppliedUsd }: { symbol: string, suppliedUsd: string }) => new Dec(suppliedUsd).mul(sparkGetEmodeMutableProps(data, symbol).collateralFactor),
|
|
85
|
+
);
|
|
86
|
+
payload.liquidationLimitUsd = getAssetsTotal(
|
|
87
|
+
usedAssets,
|
|
88
|
+
({ isSupplied, collateral }: { isSupplied: boolean, collateral: boolean }) => isSupplied && collateral,
|
|
89
|
+
({ symbol, suppliedUsd }: { symbol: string, suppliedUsd: string }) => new Dec(suppliedUsd).mul(sparkGetEmodeMutableProps(data, symbol).liquidationRatio),
|
|
90
|
+
);
|
|
91
|
+
const leftToBorrowUsd = new Dec(payload.borrowLimitUsd).sub(payload.borrowedUsd);
|
|
92
|
+
payload.leftToBorrowUsd = leftToBorrowUsd.lte('0') ? '0' : leftToBorrowUsd.toString();
|
|
93
|
+
payload.ratio = +payload.suppliedUsd ? new Dec(payload.borrowLimitUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
|
|
94
|
+
payload.collRatio = +payload.suppliedUsd ? new Dec(payload.suppliedCollateralUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
|
|
95
|
+
const { netApy, incentiveUsd, totalInterestUsd } = calculateNetApy({ usedAssets, assetsData });
|
|
96
|
+
payload.netApy = netApy;
|
|
97
|
+
payload.incentiveUsd = incentiveUsd;
|
|
98
|
+
payload.totalInterestUsd = totalInterestUsd;
|
|
99
|
+
payload.liqRatio = new Dec(payload.borrowLimitUsd).div(payload.liquidationLimitUsd).toString();
|
|
100
|
+
payload.liqPercent = new Dec(payload.borrowLimitUsd).div(payload.liquidationLimitUsd).mul(100).toString();
|
|
101
|
+
const { leveragedType, leveragedAsset } = isLeveragedPos(usedAssets);
|
|
102
|
+
payload.leveragedType = leveragedType;
|
|
103
|
+
if (leveragedType !== '') {
|
|
104
|
+
payload.leveragedAsset = leveragedAsset;
|
|
105
|
+
let assetPrice = data.assetsData[leveragedAsset].price; // TODO sparkPrice or price??
|
|
106
|
+
if (leveragedType === 'lsd-leverage') {
|
|
107
|
+
// Treat ETH like a stablecoin in a long stETH position
|
|
108
|
+
payload.leveragedLsdAssetRatio = new Dec(assetsData[leveragedAsset].price).div(assetsData.ETH.price).toDP(18).toString();
|
|
109
|
+
assetPrice = new Dec(assetPrice).div(assetsData.ETH.price).toString();
|
|
110
|
+
}
|
|
111
|
+
payload.liquidationPrice = calcLeverageLiqPrice(leveragedType, assetPrice, payload.borrowedUsd, payload.liquidationLimitUsd);
|
|
112
|
+
}
|
|
113
|
+
return payload;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
export const getApyAfterValuesEstimation = async (selectedMarket: SparkMarketData, actions: [{ action: string, amount: string, asset: string }], web3: Web3) => {
|
|
117
|
+
const sparkViewContract = SparkViewContract(web3, NetworkNumber.Eth);
|
|
118
|
+
const params = actions.map(({ action, asset, amount }: { action: string, amount: string, asset: string }) => {
|
|
119
|
+
const isDebtAsset = borrowOperations.includes(action);
|
|
120
|
+
const amountInWei = assetAmountInWei(amount, asset);
|
|
121
|
+
const assetInfo = getAssetInfo(ethToWeth(asset));
|
|
122
|
+
let liquidityAdded;
|
|
123
|
+
let liquidityTaken;
|
|
124
|
+
if (isDebtAsset) {
|
|
125
|
+
liquidityAdded = action === 'payback' ? amountInWei : '0';
|
|
126
|
+
liquidityTaken = action === 'borrow' ? amountInWei : '0';
|
|
127
|
+
} else {
|
|
128
|
+
liquidityAdded = action === 'collateral' ? amountInWei : '0';
|
|
129
|
+
liquidityTaken = action === 'withdraw' ? amountInWei : '0';
|
|
130
|
+
}
|
|
131
|
+
return {
|
|
132
|
+
reserveAddress: assetInfo.address,
|
|
133
|
+
liquidityAdded,
|
|
134
|
+
liquidityTaken,
|
|
135
|
+
isDebtAsset,
|
|
136
|
+
};
|
|
137
|
+
});
|
|
138
|
+
const data = await sparkViewContract.methods.getApyAfterValuesEstimation(
|
|
139
|
+
selectedMarket.providerAddress,
|
|
140
|
+
params,
|
|
141
|
+
).call();
|
|
142
|
+
const rates: { [key: string]: { supplyRate: string, borrowRate: string } } = {};
|
|
143
|
+
data.forEach((d) => {
|
|
144
|
+
const asset = wethToEth(getAssetInfoByAddress(d.reserveAddress).symbol);
|
|
145
|
+
rates[asset] = {
|
|
146
|
+
supplyRate: aprToApy(new Dec(d.supplyRate.toString()).div(1e25).toString()),
|
|
147
|
+
borrowRate: aprToApy(new Dec(d.variableBorrowRate.toString()).div(1e25).toString()),
|
|
148
|
+
};
|
|
149
|
+
});
|
|
150
|
+
return rates;
|
|
151
151
|
};
|
package/src/index.ts
CHANGED
|
@@ -1,52 +1,52 @@
|
|
|
1
|
-
import './setup';
|
|
2
|
-
|
|
3
|
-
import * as fluid from './fluid';
|
|
4
|
-
import * as aaveV3 from './aaveV3';
|
|
5
|
-
import * as morphoAaveV3 from './morphoAaveV3';
|
|
6
|
-
import * as aaveV2 from './aaveV2';
|
|
7
|
-
import * as morphoAaveV2 from './morphoAaveV2';
|
|
8
|
-
import * as compoundV3 from './compoundV3';
|
|
9
|
-
import * as compoundV2 from './compoundV2';
|
|
10
|
-
import * as spark from './spark';
|
|
11
|
-
import * as curveUsd from './curveUsd';
|
|
12
|
-
import * as liquity from './liquity';
|
|
13
|
-
import * as liquityV2 from './liquityV2';
|
|
14
|
-
import * as maker from './maker';
|
|
15
|
-
import * as staking from './staking';
|
|
16
|
-
import * as multicall from './multicall';
|
|
17
|
-
import * as moneymarket from './moneymarket';
|
|
18
|
-
import * as assets from './assets';
|
|
19
|
-
import * as markets from './markets';
|
|
20
|
-
import * as helpers from './helpers';
|
|
21
|
-
import * as chickenBonds from './chickenBonds';
|
|
22
|
-
import * as exchange from './exchange';
|
|
23
|
-
import * as morphoBlue from './morphoBlue';
|
|
24
|
-
import * as llamaLend from './llamaLend';
|
|
25
|
-
import * as eulerV2 from './eulerV2';
|
|
26
|
-
|
|
27
|
-
export * from './types';
|
|
28
|
-
|
|
29
|
-
export {
|
|
30
|
-
aaveV2,
|
|
31
|
-
aaveV3,
|
|
32
|
-
morphoAaveV2,
|
|
33
|
-
morphoAaveV3,
|
|
34
|
-
compoundV2,
|
|
35
|
-
compoundV3,
|
|
36
|
-
spark,
|
|
37
|
-
curveUsd,
|
|
38
|
-
liquity,
|
|
39
|
-
liquityV2,
|
|
40
|
-
maker,
|
|
41
|
-
chickenBonds,
|
|
42
|
-
exchange,
|
|
43
|
-
staking,
|
|
44
|
-
multicall,
|
|
45
|
-
moneymarket,
|
|
46
|
-
markets,
|
|
47
|
-
helpers,
|
|
48
|
-
morphoBlue,
|
|
49
|
-
llamaLend,
|
|
50
|
-
eulerV2,
|
|
51
|
-
fluid,
|
|
52
|
-
};
|
|
1
|
+
import './setup';
|
|
2
|
+
|
|
3
|
+
import * as fluid from './fluid';
|
|
4
|
+
import * as aaveV3 from './aaveV3';
|
|
5
|
+
import * as morphoAaveV3 from './morphoAaveV3';
|
|
6
|
+
import * as aaveV2 from './aaveV2';
|
|
7
|
+
import * as morphoAaveV2 from './morphoAaveV2';
|
|
8
|
+
import * as compoundV3 from './compoundV3';
|
|
9
|
+
import * as compoundV2 from './compoundV2';
|
|
10
|
+
import * as spark from './spark';
|
|
11
|
+
import * as curveUsd from './curveUsd';
|
|
12
|
+
import * as liquity from './liquity';
|
|
13
|
+
import * as liquityV2 from './liquityV2';
|
|
14
|
+
import * as maker from './maker';
|
|
15
|
+
import * as staking from './staking';
|
|
16
|
+
import * as multicall from './multicall';
|
|
17
|
+
import * as moneymarket from './moneymarket';
|
|
18
|
+
import * as assets from './assets';
|
|
19
|
+
import * as markets from './markets';
|
|
20
|
+
import * as helpers from './helpers';
|
|
21
|
+
import * as chickenBonds from './chickenBonds';
|
|
22
|
+
import * as exchange from './exchange';
|
|
23
|
+
import * as morphoBlue from './morphoBlue';
|
|
24
|
+
import * as llamaLend from './llamaLend';
|
|
25
|
+
import * as eulerV2 from './eulerV2';
|
|
26
|
+
|
|
27
|
+
export * from './types';
|
|
28
|
+
|
|
29
|
+
export {
|
|
30
|
+
aaveV2,
|
|
31
|
+
aaveV3,
|
|
32
|
+
morphoAaveV2,
|
|
33
|
+
morphoAaveV3,
|
|
34
|
+
compoundV2,
|
|
35
|
+
compoundV3,
|
|
36
|
+
spark,
|
|
37
|
+
curveUsd,
|
|
38
|
+
liquity,
|
|
39
|
+
liquityV2,
|
|
40
|
+
maker,
|
|
41
|
+
chickenBonds,
|
|
42
|
+
exchange,
|
|
43
|
+
staking,
|
|
44
|
+
multicall,
|
|
45
|
+
moneymarket,
|
|
46
|
+
markets,
|
|
47
|
+
helpers,
|
|
48
|
+
morphoBlue,
|
|
49
|
+
llamaLend,
|
|
50
|
+
eulerV2,
|
|
51
|
+
fluid,
|
|
52
|
+
};
|
package/src/liquity/index.ts
CHANGED
|
@@ -1,117 +1,117 @@
|
|
|
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 {
|
|
8
|
-
LiquityActivePoolContract, LiquityCollSurplusPoolContract, LiquityPriceFeedContract, LiquityTroveManagerContract, LiquityViewContract,
|
|
9
|
-
} from '../contracts';
|
|
10
|
-
import { multicall } from '../multicall';
|
|
11
|
-
import { LIQUITY_TROVE_STATUS_ENUM, LiquityTroveInfo } from '../types';
|
|
12
|
-
import { ZERO_ADDRESS } from '../constants';
|
|
13
|
-
|
|
14
|
-
export const LIQUITY_NORMAL_MODE_RATIO = 110; // MCR
|
|
15
|
-
export const LIQUITY_RECOVERY_MODE_RATIO = 150; // CCR
|
|
16
|
-
|
|
17
|
-
export const getLiquityAccountBalances = async (web3: Web3, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress): Promise<PositionBalances> => {
|
|
18
|
-
let balances: PositionBalances = {
|
|
19
|
-
collateral: {},
|
|
20
|
-
debt: {},
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
if (!address) {
|
|
24
|
-
return balances;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const viewContract = LiquityViewContract(web3, network, block);
|
|
28
|
-
const troveInfo = await viewContract.methods.getTroveInfo(address).call({}, block);
|
|
29
|
-
|
|
30
|
-
balances = {
|
|
31
|
-
collateral: {
|
|
32
|
-
[addressMapping ? getAssetInfo('ETH', network).address.toLowerCase() : 'ETH']: troveInfo[1],
|
|
33
|
-
},
|
|
34
|
-
debt: {
|
|
35
|
-
[addressMapping ? getAssetInfo('LUSD', network).address.toLowerCase() : 'LUSD']: troveInfo[2],
|
|
36
|
-
},
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
return balances;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
const _getDebtInFront = async (viewContract: any, address: string, accumulatedSum = '0', iterations = 2000) => viewContract.methods.getDebtInFront(address, accumulatedSum, iterations).call();
|
|
43
|
-
|
|
44
|
-
export const getDebtInFront = async (viewContract: any, address: string, accumulatedSum = '0', iterations = 2000): Promise<string> => {
|
|
45
|
-
const { debt, next } = await _getDebtInFront(viewContract, address, accumulatedSum, iterations);
|
|
46
|
-
if (next === ZERO_ADDRESS) return assetAmountInEth(debt, 'LUSD');
|
|
47
|
-
return getDebtInFront(viewContract, next, debt, iterations);
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
export const getLiquityTroveInfo = async (web3: Web3, network: NetworkNumber, address: string): Promise<LiquityTroveInfo> => {
|
|
51
|
-
const viewContract = LiquityViewContract(web3, network);
|
|
52
|
-
const collSurplusPoolContract = LiquityCollSurplusPoolContract(web3, network);
|
|
53
|
-
const troveManagerContract = LiquityTroveManagerContract(web3, network);
|
|
54
|
-
const priceFeedContract = LiquityPriceFeedContract(web3, network);
|
|
55
|
-
const activePoolContract = LiquityActivePoolContract(web3, network);
|
|
56
|
-
|
|
57
|
-
const multicallData = [
|
|
58
|
-
{
|
|
59
|
-
target: viewContract.options.address,
|
|
60
|
-
abiItem: viewContract.options.jsonInterface.find(({ name }) => name === 'getTroveInfo'),
|
|
61
|
-
params: [address],
|
|
62
|
-
},
|
|
63
|
-
{
|
|
64
|
-
target: collSurplusPoolContract.options.address,
|
|
65
|
-
abiItem: collSurplusPoolContract.options.jsonInterface.find(({ name }) => name === 'getCollateral'),
|
|
66
|
-
params: [address],
|
|
67
|
-
},
|
|
68
|
-
{
|
|
69
|
-
target: troveManagerContract.options.address,
|
|
70
|
-
abiItem: troveManagerContract.options.jsonInterface.find(({ name }) => name === 'getBorrowingRateWithDecay'),
|
|
71
|
-
params: [],
|
|
72
|
-
},
|
|
73
|
-
{
|
|
74
|
-
target: priceFeedContract.options.address,
|
|
75
|
-
abiItem: priceFeedContract.options.jsonInterface.find(({ name }) => name === 'fetchPrice'),
|
|
76
|
-
params: [],
|
|
77
|
-
},
|
|
78
|
-
{
|
|
79
|
-
target: activePoolContract.options.address,
|
|
80
|
-
abiItem: activePoolContract.options.jsonInterface.find(({ name }) => name === 'getETH'),
|
|
81
|
-
params: [],
|
|
82
|
-
},
|
|
83
|
-
{
|
|
84
|
-
target: activePoolContract.options.address,
|
|
85
|
-
abiItem: activePoolContract.options.jsonInterface.find(({ name }) => name === 'getLUSDDebt'),
|
|
86
|
-
params: [],
|
|
87
|
-
},
|
|
88
|
-
];
|
|
89
|
-
|
|
90
|
-
const [multiRes, debtInFront] = await Promise.all([
|
|
91
|
-
multicall(multicallData, web3, network),
|
|
92
|
-
getDebtInFront(viewContract, address),
|
|
93
|
-
]);
|
|
94
|
-
|
|
95
|
-
const recoveryMode = multiRes[0][6];
|
|
96
|
-
const totalETH = multiRes[4][0];
|
|
97
|
-
const totalLUSD = multiRes[5][0];
|
|
98
|
-
|
|
99
|
-
const payload = {
|
|
100
|
-
troveStatus: LIQUITY_TROVE_STATUS_ENUM[+multiRes[0][0].toString()],
|
|
101
|
-
collateral: assetAmountInEth(multiRes[0][1]),
|
|
102
|
-
debtInAsset: assetAmountInEth(multiRes[0][2]),
|
|
103
|
-
TCRatio: assetAmountInEth(multiRes[0][4]),
|
|
104
|
-
recoveryMode,
|
|
105
|
-
claimableCollateral: assetAmountInEth(multiRes[1][0]),
|
|
106
|
-
borrowingRateWithDecay: assetAmountInEth(multiRes[2][0]),
|
|
107
|
-
assetPrice: assetAmountInEth(multiRes[3][0]),
|
|
108
|
-
totalETH,
|
|
109
|
-
totalLUSD,
|
|
110
|
-
debtInFront,
|
|
111
|
-
minCollateralRatio: recoveryMode ? LIQUITY_RECOVERY_MODE_RATIO : LIQUITY_NORMAL_MODE_RATIO,
|
|
112
|
-
priceForRecovery: new Dec(recoveryMode ? LIQUITY_RECOVERY_MODE_RATIO : LIQUITY_NORMAL_MODE_RATIO).mul(totalLUSD).div(totalETH).div(100)
|
|
113
|
-
.toString(),
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
return payload;
|
|
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 {
|
|
8
|
+
LiquityActivePoolContract, LiquityCollSurplusPoolContract, LiquityPriceFeedContract, LiquityTroveManagerContract, LiquityViewContract,
|
|
9
|
+
} from '../contracts';
|
|
10
|
+
import { multicall } from '../multicall';
|
|
11
|
+
import { LIQUITY_TROVE_STATUS_ENUM, LiquityTroveInfo } from '../types';
|
|
12
|
+
import { ZERO_ADDRESS } from '../constants';
|
|
13
|
+
|
|
14
|
+
export const LIQUITY_NORMAL_MODE_RATIO = 110; // MCR
|
|
15
|
+
export const LIQUITY_RECOVERY_MODE_RATIO = 150; // CCR
|
|
16
|
+
|
|
17
|
+
export const getLiquityAccountBalances = async (web3: Web3, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress): Promise<PositionBalances> => {
|
|
18
|
+
let balances: PositionBalances = {
|
|
19
|
+
collateral: {},
|
|
20
|
+
debt: {},
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
if (!address) {
|
|
24
|
+
return balances;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const viewContract = LiquityViewContract(web3, network, block);
|
|
28
|
+
const troveInfo = await viewContract.methods.getTroveInfo(address).call({}, block);
|
|
29
|
+
|
|
30
|
+
balances = {
|
|
31
|
+
collateral: {
|
|
32
|
+
[addressMapping ? getAssetInfo('ETH', network).address.toLowerCase() : 'ETH']: troveInfo[1],
|
|
33
|
+
},
|
|
34
|
+
debt: {
|
|
35
|
+
[addressMapping ? getAssetInfo('LUSD', network).address.toLowerCase() : 'LUSD']: troveInfo[2],
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
return balances;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const _getDebtInFront = async (viewContract: any, address: string, accumulatedSum = '0', iterations = 2000) => viewContract.methods.getDebtInFront(address, accumulatedSum, iterations).call();
|
|
43
|
+
|
|
44
|
+
export const getDebtInFront = async (viewContract: any, address: string, accumulatedSum = '0', iterations = 2000): Promise<string> => {
|
|
45
|
+
const { debt, next } = await _getDebtInFront(viewContract, address, accumulatedSum, iterations);
|
|
46
|
+
if (next === ZERO_ADDRESS) return assetAmountInEth(debt, 'LUSD');
|
|
47
|
+
return getDebtInFront(viewContract, next, debt, iterations);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export const getLiquityTroveInfo = async (web3: Web3, network: NetworkNumber, address: string): Promise<LiquityTroveInfo> => {
|
|
51
|
+
const viewContract = LiquityViewContract(web3, network);
|
|
52
|
+
const collSurplusPoolContract = LiquityCollSurplusPoolContract(web3, network);
|
|
53
|
+
const troveManagerContract = LiquityTroveManagerContract(web3, network);
|
|
54
|
+
const priceFeedContract = LiquityPriceFeedContract(web3, network);
|
|
55
|
+
const activePoolContract = LiquityActivePoolContract(web3, network);
|
|
56
|
+
|
|
57
|
+
const multicallData = [
|
|
58
|
+
{
|
|
59
|
+
target: viewContract.options.address,
|
|
60
|
+
abiItem: viewContract.options.jsonInterface.find(({ name }) => name === 'getTroveInfo'),
|
|
61
|
+
params: [address],
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
target: collSurplusPoolContract.options.address,
|
|
65
|
+
abiItem: collSurplusPoolContract.options.jsonInterface.find(({ name }) => name === 'getCollateral'),
|
|
66
|
+
params: [address],
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
target: troveManagerContract.options.address,
|
|
70
|
+
abiItem: troveManagerContract.options.jsonInterface.find(({ name }) => name === 'getBorrowingRateWithDecay'),
|
|
71
|
+
params: [],
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
target: priceFeedContract.options.address,
|
|
75
|
+
abiItem: priceFeedContract.options.jsonInterface.find(({ name }) => name === 'fetchPrice'),
|
|
76
|
+
params: [],
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
target: activePoolContract.options.address,
|
|
80
|
+
abiItem: activePoolContract.options.jsonInterface.find(({ name }) => name === 'getETH'),
|
|
81
|
+
params: [],
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
target: activePoolContract.options.address,
|
|
85
|
+
abiItem: activePoolContract.options.jsonInterface.find(({ name }) => name === 'getLUSDDebt'),
|
|
86
|
+
params: [],
|
|
87
|
+
},
|
|
88
|
+
];
|
|
89
|
+
|
|
90
|
+
const [multiRes, debtInFront] = await Promise.all([
|
|
91
|
+
multicall(multicallData, web3, network),
|
|
92
|
+
getDebtInFront(viewContract, address),
|
|
93
|
+
]);
|
|
94
|
+
|
|
95
|
+
const recoveryMode = multiRes[0][6];
|
|
96
|
+
const totalETH = multiRes[4][0];
|
|
97
|
+
const totalLUSD = multiRes[5][0];
|
|
98
|
+
|
|
99
|
+
const payload = {
|
|
100
|
+
troveStatus: LIQUITY_TROVE_STATUS_ENUM[+multiRes[0][0].toString()],
|
|
101
|
+
collateral: assetAmountInEth(multiRes[0][1]),
|
|
102
|
+
debtInAsset: assetAmountInEth(multiRes[0][2]),
|
|
103
|
+
TCRatio: assetAmountInEth(multiRes[0][4]),
|
|
104
|
+
recoveryMode,
|
|
105
|
+
claimableCollateral: assetAmountInEth(multiRes[1][0]),
|
|
106
|
+
borrowingRateWithDecay: assetAmountInEth(multiRes[2][0]),
|
|
107
|
+
assetPrice: assetAmountInEth(multiRes[3][0]),
|
|
108
|
+
totalETH,
|
|
109
|
+
totalLUSD,
|
|
110
|
+
debtInFront,
|
|
111
|
+
minCollateralRatio: recoveryMode ? LIQUITY_RECOVERY_MODE_RATIO : LIQUITY_NORMAL_MODE_RATIO,
|
|
112
|
+
priceForRecovery: new Dec(recoveryMode ? LIQUITY_RECOVERY_MODE_RATIO : LIQUITY_NORMAL_MODE_RATIO).mul(totalLUSD).div(totalETH).div(100)
|
|
113
|
+
.toString(),
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
return payload;
|
|
117
117
|
};
|