@defisaver/positions-sdk 0.0.183-dev-allocator → 0.0.184
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.d.ts +0 -5
- package/cjs/helpers/morphoBlueHelpers/index.js +1 -127
- package/cjs/staking/staking.js +2 -2
- package/cjs/types/morphoBlue.d.ts +0 -25
- package/esm/helpers/morphoBlueHelpers/index.d.ts +0 -5
- package/esm/helpers/morphoBlueHelpers/index.js +0 -124
- package/esm/staking/staking.js +2 -2
- package/esm/types/morphoBlue.d.ts +0 -25
- package/package.json +49 -49
- package/src/aaveV2/index.ts +227 -227
- package/src/aaveV3/index.ts +624 -624
- 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 +282 -282
- package/src/config/contracts.js +1043 -1043
- package/src/constants/index.ts +6 -6
- package/src/contracts.ts +130 -130
- 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 +8 -8
- package/src/helpers/llamaLendHelpers/index.ts +53 -53
- package/src/helpers/makerHelpers/index.ts +94 -94
- package/src/helpers/morphoBlueHelpers/index.ts +115 -256
- package/src/helpers/sparkHelpers/index.ts +150 -150
- package/src/index.ts +48 -48
- package/src/liquity/index.ts +116 -116
- 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 +46 -46
- package/src/markets/compound/index.ts +173 -173
- package/src/markets/compound/marketsAssets.ts +64 -64
- package/src/markets/curveUsd/index.ts +69 -69
- package/src/markets/euler/index.ts +26 -26
- package/src/markets/index.ts +23 -23
- package/src/markets/llamaLend/contractAddresses.ts +141 -141
- package/src/markets/llamaLend/index.ts +235 -235
- package/src/markets/morphoBlue/index.ts +809 -809
- 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 +171 -171
- package/src/multicall/index.ts +22 -22
- package/src/services/dsrService.ts +15 -15
- package/src/services/priceService.ts +21 -21
- package/src/services/utils.ts +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 +270 -270
- package/src/types/chickenBonds.ts +45 -45
- package/src/types/common.ts +84 -84
- package/src/types/compound.ts +129 -129
- package/src/types/curveUsd.ts +118 -118
- package/src/types/euler.ts +171 -171
- package/src/types/index.ts +9 -9
- package/src/types/liquity.ts +30 -30
- package/src/types/llamaLend.ts +155 -155
- package/src/types/maker.ts +50 -50
- package/src/types/morphoBlue.ts +154 -177
- package/src/types/spark.ts +131 -131
|
@@ -1,95 +1,95 @@
|
|
|
1
|
-
import Web3 from 'web3';
|
|
2
|
-
import Dec from 'decimal.js';
|
|
3
|
-
import { Blockish, NetworkNumber } from '../../types/common';
|
|
4
|
-
import {
|
|
5
|
-
McdDogContract, McdJugContract, McdSpotterContract, McdVatContract,
|
|
6
|
-
} from '../../contracts';
|
|
7
|
-
import { multicall } from '../../multicall';
|
|
8
|
-
import { SECONDS_PER_YEAR } from '../../constants';
|
|
9
|
-
import { bytesToString } from '../../services/utils';
|
|
10
|
-
import { IlkInfo } from '../../types';
|
|
11
|
-
|
|
12
|
-
export const getUnclaimedCollateral = async (web3: Web3, network: NetworkNumber, urn: string, ilk: string) => {
|
|
13
|
-
const vatContract = McdVatContract(web3, network);
|
|
14
|
-
const coll = await vatContract.methods.gem(ilk, urn).call();
|
|
15
|
-
return coll;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export const getCollateralInfo = async (ilk: string, web3: Web3, network: NetworkNumber, block: Blockish = 'latest'): Promise<IlkInfo> => {
|
|
19
|
-
const spotterContract = McdSpotterContract(web3, network);
|
|
20
|
-
const vatContract = McdVatContract(web3, network);
|
|
21
|
-
const dogContract = McdDogContract(web3, network);
|
|
22
|
-
const jugContract = McdJugContract(web3, network);
|
|
23
|
-
|
|
24
|
-
const multicallData = [
|
|
25
|
-
{
|
|
26
|
-
target: spotterContract.options.address,
|
|
27
|
-
abiItem: spotterContract.options.jsonInterface.find(({ name }) => name === 'par'),
|
|
28
|
-
params: [],
|
|
29
|
-
},
|
|
30
|
-
{
|
|
31
|
-
target: spotterContract.options.address,
|
|
32
|
-
abiItem: spotterContract.options.jsonInterface.find(({ name }) => name === 'ilks'),
|
|
33
|
-
params: [ilk],
|
|
34
|
-
},
|
|
35
|
-
{
|
|
36
|
-
target: vatContract.options.address,
|
|
37
|
-
abiItem: vatContract.options.jsonInterface.find(({ name }) => name === 'ilks'),
|
|
38
|
-
params: [ilk],
|
|
39
|
-
},
|
|
40
|
-
{
|
|
41
|
-
target: jugContract.options.address,
|
|
42
|
-
abiItem: jugContract.options.jsonInterface.find(({ name }) => name === 'ilks'),
|
|
43
|
-
params: [ilk],
|
|
44
|
-
},
|
|
45
|
-
{
|
|
46
|
-
target: jugContract.options.address,
|
|
47
|
-
abiItem: jugContract.options.jsonInterface.find(({ name }) => name === 'drip'),
|
|
48
|
-
params: [ilk],
|
|
49
|
-
},
|
|
50
|
-
{
|
|
51
|
-
target: dogContract.options.address,
|
|
52
|
-
abiItem: dogContract.options.jsonInterface.find(({ name }) => name === 'chop'),
|
|
53
|
-
params: [ilk],
|
|
54
|
-
},
|
|
55
|
-
];
|
|
56
|
-
|
|
57
|
-
const multiRes = await multicall(multicallData, web3, network, block);
|
|
58
|
-
|
|
59
|
-
const par = new Dec(multiRes[0][0].toString()).div(1e27).toString();
|
|
60
|
-
const mat = new Dec(multiRes[1][1].toString()).div(1e27).toString();
|
|
61
|
-
const art = new Dec(multiRes[2][0].toString()).toString();
|
|
62
|
-
const rate = new Dec(multiRes[2][1].toString()).toString();
|
|
63
|
-
const spot = new Dec(multiRes[2][2].toString()).div(1e27).toString();
|
|
64
|
-
const line = new Dec(multiRes[2][3].toString()).div(1e45).toString();
|
|
65
|
-
const dust = new Dec(multiRes[2][1].toString()).div(1e45).toString();
|
|
66
|
-
const duty = new Dec(multiRes[3][0].toString()).toString();
|
|
67
|
-
const futureRate = new Dec(multiRes[4][0].toString()).toString();
|
|
68
|
-
const chop = new Dec(multiRes[5][0].toString()).div(1e18).toString();
|
|
69
|
-
|
|
70
|
-
const stabilityFee = new Dec(duty.toString())
|
|
71
|
-
.div(1e27)
|
|
72
|
-
.pow(SECONDS_PER_YEAR)
|
|
73
|
-
.minus(1)
|
|
74
|
-
.mul(100)
|
|
75
|
-
.toNumber();
|
|
76
|
-
const liquidationFee = new Dec(chop).mul(100).sub(100).toString();
|
|
77
|
-
const globalDebtCurrent = new Dec(art).div(1e18).mul(new Dec(futureRate).div(1e27)).toString();
|
|
78
|
-
const globalDebtCeiling = line;
|
|
79
|
-
const creatableDebt = new Dec(globalDebtCeiling).sub(globalDebtCurrent).toString();
|
|
80
|
-
|
|
81
|
-
return {
|
|
82
|
-
ilkLabel: bytesToString(ilk),
|
|
83
|
-
currentRate: rate,
|
|
84
|
-
futureRate,
|
|
85
|
-
minDebt: dust,
|
|
86
|
-
globalDebtCurrent,
|
|
87
|
-
globalDebtCeiling,
|
|
88
|
-
assetPrice: new Dec(spot).times(par).times(mat).toString(),
|
|
89
|
-
liqRatio: mat,
|
|
90
|
-
liqPercent: +mat * 100,
|
|
91
|
-
stabilityFee,
|
|
92
|
-
liquidationFee: new Dec(liquidationFee).lt(0) ? '0' : liquidationFee,
|
|
93
|
-
creatableDebt,
|
|
94
|
-
};
|
|
1
|
+
import Web3 from 'web3';
|
|
2
|
+
import Dec from 'decimal.js';
|
|
3
|
+
import { Blockish, NetworkNumber } from '../../types/common';
|
|
4
|
+
import {
|
|
5
|
+
McdDogContract, McdJugContract, McdSpotterContract, McdVatContract,
|
|
6
|
+
} from '../../contracts';
|
|
7
|
+
import { multicall } from '../../multicall';
|
|
8
|
+
import { SECONDS_PER_YEAR } from '../../constants';
|
|
9
|
+
import { bytesToString } from '../../services/utils';
|
|
10
|
+
import { IlkInfo } from '../../types';
|
|
11
|
+
|
|
12
|
+
export const getUnclaimedCollateral = async (web3: Web3, network: NetworkNumber, urn: string, ilk: string) => {
|
|
13
|
+
const vatContract = McdVatContract(web3, network);
|
|
14
|
+
const coll = await vatContract.methods.gem(ilk, urn).call();
|
|
15
|
+
return coll;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const getCollateralInfo = async (ilk: string, web3: Web3, network: NetworkNumber, block: Blockish = 'latest'): Promise<IlkInfo> => {
|
|
19
|
+
const spotterContract = McdSpotterContract(web3, network);
|
|
20
|
+
const vatContract = McdVatContract(web3, network);
|
|
21
|
+
const dogContract = McdDogContract(web3, network);
|
|
22
|
+
const jugContract = McdJugContract(web3, network);
|
|
23
|
+
|
|
24
|
+
const multicallData = [
|
|
25
|
+
{
|
|
26
|
+
target: spotterContract.options.address,
|
|
27
|
+
abiItem: spotterContract.options.jsonInterface.find(({ name }) => name === 'par'),
|
|
28
|
+
params: [],
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
target: spotterContract.options.address,
|
|
32
|
+
abiItem: spotterContract.options.jsonInterface.find(({ name }) => name === 'ilks'),
|
|
33
|
+
params: [ilk],
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
target: vatContract.options.address,
|
|
37
|
+
abiItem: vatContract.options.jsonInterface.find(({ name }) => name === 'ilks'),
|
|
38
|
+
params: [ilk],
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
target: jugContract.options.address,
|
|
42
|
+
abiItem: jugContract.options.jsonInterface.find(({ name }) => name === 'ilks'),
|
|
43
|
+
params: [ilk],
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
target: jugContract.options.address,
|
|
47
|
+
abiItem: jugContract.options.jsonInterface.find(({ name }) => name === 'drip'),
|
|
48
|
+
params: [ilk],
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
target: dogContract.options.address,
|
|
52
|
+
abiItem: dogContract.options.jsonInterface.find(({ name }) => name === 'chop'),
|
|
53
|
+
params: [ilk],
|
|
54
|
+
},
|
|
55
|
+
];
|
|
56
|
+
|
|
57
|
+
const multiRes = await multicall(multicallData, web3, network, block);
|
|
58
|
+
|
|
59
|
+
const par = new Dec(multiRes[0][0].toString()).div(1e27).toString();
|
|
60
|
+
const mat = new Dec(multiRes[1][1].toString()).div(1e27).toString();
|
|
61
|
+
const art = new Dec(multiRes[2][0].toString()).toString();
|
|
62
|
+
const rate = new Dec(multiRes[2][1].toString()).toString();
|
|
63
|
+
const spot = new Dec(multiRes[2][2].toString()).div(1e27).toString();
|
|
64
|
+
const line = new Dec(multiRes[2][3].toString()).div(1e45).toString();
|
|
65
|
+
const dust = new Dec(multiRes[2][1].toString()).div(1e45).toString();
|
|
66
|
+
const duty = new Dec(multiRes[3][0].toString()).toString();
|
|
67
|
+
const futureRate = new Dec(multiRes[4][0].toString()).toString();
|
|
68
|
+
const chop = new Dec(multiRes[5][0].toString()).div(1e18).toString();
|
|
69
|
+
|
|
70
|
+
const stabilityFee = new Dec(duty.toString())
|
|
71
|
+
.div(1e27)
|
|
72
|
+
.pow(SECONDS_PER_YEAR)
|
|
73
|
+
.minus(1)
|
|
74
|
+
.mul(100)
|
|
75
|
+
.toNumber();
|
|
76
|
+
const liquidationFee = new Dec(chop).mul(100).sub(100).toString();
|
|
77
|
+
const globalDebtCurrent = new Dec(art).div(1e18).mul(new Dec(futureRate).div(1e27)).toString();
|
|
78
|
+
const globalDebtCeiling = line;
|
|
79
|
+
const creatableDebt = new Dec(globalDebtCeiling).sub(globalDebtCurrent).toString();
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
ilkLabel: bytesToString(ilk),
|
|
83
|
+
currentRate: rate,
|
|
84
|
+
futureRate,
|
|
85
|
+
minDebt: dust,
|
|
86
|
+
globalDebtCurrent,
|
|
87
|
+
globalDebtCeiling,
|
|
88
|
+
assetPrice: new Dec(spot).times(par).times(mat).toString(),
|
|
89
|
+
liqRatio: mat,
|
|
90
|
+
liqPercent: +mat * 100,
|
|
91
|
+
stabilityFee,
|
|
92
|
+
liquidationFee: new Dec(liquidationFee).lt(0) ? '0' : liquidationFee,
|
|
93
|
+
creatableDebt,
|
|
94
|
+
};
|
|
95
95
|
};
|
|
@@ -1,257 +1,116 @@
|
|
|
1
|
-
import Dec from 'decimal.js';
|
|
2
|
-
import { assetAmountInWei
|
|
3
|
-
import Web3 from 'web3';
|
|
4
|
-
import { calcLeverageLiqPrice, getAssetsTotal, isLeveragedPos } from '../../moneymarket';
|
|
5
|
-
import { calculateNetApy } from '../../staking';
|
|
6
|
-
import { MMAssetsData, MMUsedAssets, NetworkNumber } from '../../types/common';
|
|
7
|
-
import {
|
|
8
|
-
MorphoBlueAggregatedPositionData, MorphoBlueAssetsData, MorphoBlueMarketData, MorphoBlueMarketInfo,
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
} from '../../
|
|
12
|
-
import {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
);
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
payload.
|
|
46
|
-
payload.
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const
|
|
97
|
-
const
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
liquidityAdded = action === '
|
|
105
|
-
liquidityRemoved = action === '
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
const borrowRate = getBorrowRate(data.borrowRate, data.market.totalBorrowShares);
|
|
117
|
-
const supplyRate = getSupplyRate(data.market.totalSupplyAssets, data.market.totalBorrowAssets, data.borrowRate, data.market.fee);
|
|
118
|
-
return { borrowRate, supplyRate };
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
const API_URL = 'https://blue-api.morpho.org/graphql';
|
|
122
|
-
const MARKET_QUERY = `
|
|
123
|
-
query MarketByUniqueKey($uniqueKey: String!, $chainId: Int!) {
|
|
124
|
-
marketByUniqueKey(uniqueKey: $uniqueKey, chainId: $chainId) {
|
|
125
|
-
reallocatableLiquidityAssets
|
|
126
|
-
loanAsset {
|
|
127
|
-
address
|
|
128
|
-
decimals
|
|
129
|
-
priceUsd
|
|
130
|
-
}
|
|
131
|
-
state {
|
|
132
|
-
liquidityAssets
|
|
133
|
-
}
|
|
134
|
-
publicAllocatorSharedLiquidity {
|
|
135
|
-
assets
|
|
136
|
-
vault {
|
|
137
|
-
address
|
|
138
|
-
name
|
|
139
|
-
}
|
|
140
|
-
allocationMarket {
|
|
141
|
-
uniqueKey
|
|
142
|
-
loanAsset {
|
|
143
|
-
address
|
|
144
|
-
}
|
|
145
|
-
collateralAsset {
|
|
146
|
-
address
|
|
147
|
-
}
|
|
148
|
-
irmAddress
|
|
149
|
-
oracle {
|
|
150
|
-
address
|
|
151
|
-
}
|
|
152
|
-
lltv
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
loanAsset {
|
|
156
|
-
address
|
|
157
|
-
}
|
|
158
|
-
collateralAsset {
|
|
159
|
-
address
|
|
160
|
-
}
|
|
161
|
-
oracle {
|
|
162
|
-
address
|
|
163
|
-
}
|
|
164
|
-
irmAddress
|
|
165
|
-
lltv
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
`;
|
|
169
|
-
|
|
170
|
-
export const getReallocatableLiquidity = async (marketId: string, network: NetworkNumber = NetworkNumber.Eth): Promise<string> => {
|
|
171
|
-
const response = await fetch(API_URL, {
|
|
172
|
-
method: 'POST',
|
|
173
|
-
headers: { 'Content-Type': 'application/json' },
|
|
174
|
-
body: JSON.stringify({
|
|
175
|
-
query: MARKET_QUERY,
|
|
176
|
-
variables: { uniqueKey: marketId, chainId: network },
|
|
177
|
-
}),
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
const data: { data: { marketByUniqueKey: MorphoBlueRealloactionMarketData } } = await response.json();
|
|
181
|
-
const marketData: MorphoBlueRealloactionMarketData = data?.data?.marketByUniqueKey;
|
|
182
|
-
|
|
183
|
-
if (!marketData) throw new Error('Market data not found');
|
|
184
|
-
|
|
185
|
-
return marketData.reallocatableLiquidityAssets;
|
|
186
|
-
};
|
|
187
|
-
|
|
188
|
-
export const getReallocation = async (marketId: string, liquidityToAllocate: string, network: NetworkNumber = NetworkNumber.Eth) => {
|
|
189
|
-
const response = await fetch(API_URL, {
|
|
190
|
-
method: 'POST',
|
|
191
|
-
headers: { 'Content-Type': 'application/json' },
|
|
192
|
-
body: JSON.stringify({
|
|
193
|
-
query: MARKET_QUERY,
|
|
194
|
-
variables: { uniqueKey: marketId, chainId: network },
|
|
195
|
-
}),
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
const data: { data: { marketByUniqueKey: MorphoBlueRealloactionMarketData } } = await response.json();
|
|
199
|
-
const marketData: MorphoBlueRealloactionMarketData = data?.data?.marketByUniqueKey;
|
|
200
|
-
|
|
201
|
-
if (!marketData) throw new Error('Market data not found');
|
|
202
|
-
|
|
203
|
-
if (new Dec(marketData.reallocatableLiquidityAssets).lt(liquidityToAllocate)) throw new Error('Not enough liquidity available to allocate');
|
|
204
|
-
|
|
205
|
-
const vaultTotalAssets = marketData.publicAllocatorSharedLiquidity.reduce(
|
|
206
|
-
(acc: Record<string, string>, item: MorphoBluePublicAllocatorItem) => {
|
|
207
|
-
const vaultAddress = item.vault.address;
|
|
208
|
-
acc[vaultAddress] = new Dec(acc[vaultAddress] || '0').add(item.assets).toString();
|
|
209
|
-
return acc;
|
|
210
|
-
},
|
|
211
|
-
{},
|
|
212
|
-
);
|
|
213
|
-
|
|
214
|
-
const sortedVaults = Object.entries(vaultTotalAssets).sort(
|
|
215
|
-
([, a]: [string, string], [, b]: [string, string]) => new Dec(b || '0').sub(a || '0').toNumber(),
|
|
216
|
-
);
|
|
217
|
-
|
|
218
|
-
const withdrawalsPerVault: Record<string, [string[], string][]> = {};
|
|
219
|
-
let totalReallocated = '0';
|
|
220
|
-
for (const [vaultAddress] of sortedVaults) {
|
|
221
|
-
if (new Dec(totalReallocated).gte(liquidityToAllocate)) break;
|
|
222
|
-
|
|
223
|
-
const vaultAllocations = marketData.publicAllocatorSharedLiquidity.filter(
|
|
224
|
-
(item: MorphoBluePublicAllocatorItem) => compareAddresses(item.vault.address, vaultAddress),
|
|
225
|
-
);
|
|
226
|
-
for (const item of vaultAllocations) {
|
|
227
|
-
if (new Dec(totalReallocated).gte(liquidityToAllocate)) break;
|
|
228
|
-
const itemAmount = item.assets;
|
|
229
|
-
const leftToAllocate = new Dec(liquidityToAllocate).sub(totalReallocated).toString();
|
|
230
|
-
const amountToTake = new Dec(itemAmount).lt(leftToAllocate) ? itemAmount : leftToAllocate;
|
|
231
|
-
totalReallocated = new Dec(totalReallocated).add(amountToTake).toString();
|
|
232
|
-
const withdrawal: [string[], string] = [
|
|
233
|
-
[
|
|
234
|
-
item.allocationMarket.loanAsset.address,
|
|
235
|
-
item.allocationMarket.collateralAsset?.address,
|
|
236
|
-
item.allocationMarket.oracle?.address,
|
|
237
|
-
item.allocationMarket.irmAddress,
|
|
238
|
-
item.allocationMarket.lltv,
|
|
239
|
-
],
|
|
240
|
-
amountToTake.toString(),
|
|
241
|
-
];
|
|
242
|
-
if (!withdrawalsPerVault[vaultAddress]) {
|
|
243
|
-
withdrawalsPerVault[vaultAddress] = [];
|
|
244
|
-
}
|
|
245
|
-
withdrawalsPerVault[vaultAddress].push(withdrawal);
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
const vaults = Object.keys(withdrawalsPerVault);
|
|
250
|
-
const withdrawals = vaults.map(
|
|
251
|
-
(vaultAddress) => withdrawalsPerVault[vaultAddress],
|
|
252
|
-
);
|
|
253
|
-
return {
|
|
254
|
-
vaults,
|
|
255
|
-
withdrawals,
|
|
256
|
-
};
|
|
1
|
+
import Dec from 'decimal.js';
|
|
2
|
+
import { assetAmountInWei } from '@defisaver/tokens';
|
|
3
|
+
import Web3 from 'web3';
|
|
4
|
+
import { calcLeverageLiqPrice, getAssetsTotal, isLeveragedPos } from '../../moneymarket';
|
|
5
|
+
import { calculateNetApy } from '../../staking';
|
|
6
|
+
import { MMAssetsData, MMUsedAssets, NetworkNumber } from '../../types/common';
|
|
7
|
+
import {
|
|
8
|
+
MorphoBlueAggregatedPositionData, MorphoBlueAssetsData, MorphoBlueMarketData, MorphoBlueMarketInfo,
|
|
9
|
+
} from '../../types';
|
|
10
|
+
import { borrowOperations, SECONDS_PER_YEAR, WAD } from '../../constants';
|
|
11
|
+
import { MorphoBlueViewContract } from '../../contracts';
|
|
12
|
+
import { MarketParamsStruct } from '../../types/contracts/generated/MorphoBlueView';
|
|
13
|
+
|
|
14
|
+
export const getMorphoBlueAggregatedPositionData = ({ usedAssets, assetsData, marketInfo }: { usedAssets: MMUsedAssets, assetsData: MorphoBlueAssetsData, marketInfo: MorphoBlueMarketInfo }): MorphoBlueAggregatedPositionData => {
|
|
15
|
+
const payload = {} as MorphoBlueAggregatedPositionData;
|
|
16
|
+
payload.suppliedUsd = getAssetsTotal(usedAssets, ({ isSupplied }: { isSupplied: boolean }) => isSupplied, ({ suppliedUsd }: { suppliedUsd: string }) => suppliedUsd);
|
|
17
|
+
payload.suppliedCollateralUsd = getAssetsTotal(usedAssets, ({ isSupplied, collateral }: { isSupplied: boolean, collateral: string }) => isSupplied && collateral, ({ suppliedUsd }: { suppliedUsd: string }) => suppliedUsd);
|
|
18
|
+
payload.borrowedUsd = getAssetsTotal(usedAssets, ({ isBorrowed }: { isBorrowed: boolean }) => isBorrowed, ({ borrowedUsd }: { borrowedUsd: string }) => borrowedUsd);
|
|
19
|
+
|
|
20
|
+
const {
|
|
21
|
+
lltv, oracle, collateralToken, loanToken,
|
|
22
|
+
} = marketInfo;
|
|
23
|
+
|
|
24
|
+
payload.borrowLimitUsd = getAssetsTotal(
|
|
25
|
+
usedAssets,
|
|
26
|
+
({ isSupplied, collateral }: { isSupplied: boolean, collateral: string }) => isSupplied && collateral,
|
|
27
|
+
({ symbol, suppliedUsd }: { symbol: string, suppliedUsd: string }) => {
|
|
28
|
+
const suppliedUsdAmount = suppliedUsd;
|
|
29
|
+
|
|
30
|
+
return new Dec(suppliedUsdAmount).mul(lltv);
|
|
31
|
+
},
|
|
32
|
+
);
|
|
33
|
+
payload.liquidationLimitUsd = payload.borrowLimitUsd;
|
|
34
|
+
const leftToBorrowUsd = new Dec(payload.borrowLimitUsd).sub(payload.borrowedUsd);
|
|
35
|
+
payload.leftToBorrowUsd = leftToBorrowUsd.lte('0') ? '0' : leftToBorrowUsd.toString();
|
|
36
|
+
|
|
37
|
+
payload.leftToBorrow = new Dec(usedAssets[collateralToken]?.supplied || 0).mul(oracle).mul(lltv).sub(usedAssets[loanToken]?.borrowed || 0)
|
|
38
|
+
.toString();
|
|
39
|
+
|
|
40
|
+
const { netApy, incentiveUsd, totalInterestUsd } = calculateNetApy({ usedAssets, assetsData: assetsData as unknown as MMAssetsData });
|
|
41
|
+
payload.netApy = netApy;
|
|
42
|
+
payload.incentiveUsd = incentiveUsd;
|
|
43
|
+
payload.totalInterestUsd = totalInterestUsd;
|
|
44
|
+
|
|
45
|
+
payload.ltv = new Dec(payload.borrowedUsd).div(payload.suppliedCollateralUsd).toString();
|
|
46
|
+
payload.ltv = new Dec(usedAssets[loanToken]?.borrowed || 0).div(oracle).div(usedAssets[collateralToken]?.supplied || 1).toString(); // default to 1 because can't div 0
|
|
47
|
+
payload.ratio = new Dec(usedAssets[collateralToken]?.supplied || 0).mul(oracle).div(usedAssets[loanToken]?.borrowed || 1).mul(100)
|
|
48
|
+
.toString();
|
|
49
|
+
|
|
50
|
+
const { leveragedType, leveragedAsset } = isLeveragedPos(usedAssets);
|
|
51
|
+
payload.leveragedType = leveragedType;
|
|
52
|
+
if (leveragedType !== '') {
|
|
53
|
+
payload.leveragedAsset = leveragedAsset;
|
|
54
|
+
let assetPrice = assetsData[leveragedAsset].price;
|
|
55
|
+
if (leveragedType === 'lsd-leverage') {
|
|
56
|
+
// Treat ETH like a stablecoin in a long stETH position
|
|
57
|
+
payload.leveragedLsdAssetRatio = new Dec(assetsData[leveragedAsset].price).div(assetsData.ETH.price).toDP(18).toString();
|
|
58
|
+
assetPrice = new Dec(assetPrice).div(assetsData.ETH.price).toString();
|
|
59
|
+
}
|
|
60
|
+
payload.liquidationPrice = calcLeverageLiqPrice(leveragedType, assetPrice, payload.borrowedUsd, payload.liquidationLimitUsd);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return payload;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const compound = (ratePerSeconds: string) => {
|
|
67
|
+
const compounding = new Dec(ratePerSeconds).mul(SECONDS_PER_YEAR).toString();
|
|
68
|
+
const apyNumber = Math.expm1(new Dec(compounding).div(WAD).toNumber());
|
|
69
|
+
return new Dec(apyNumber).mul(WAD).floor().toString();
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export const getSupplyRate = (totalSupplyAssets: string, totalBorrowAssets: string, borrowRate: string, fee: string) => {
|
|
73
|
+
if (totalBorrowAssets === '0' || totalSupplyAssets === '0') {
|
|
74
|
+
return '0';
|
|
75
|
+
}
|
|
76
|
+
const utillization = new Dec(totalBorrowAssets).mul(WAD).div(totalSupplyAssets).ceil()
|
|
77
|
+
.toString();
|
|
78
|
+
const supplyRate = new Dec(utillization).mul(borrowRate).div(WAD).ceil()
|
|
79
|
+
.toString();
|
|
80
|
+
const ratePerSecond = new Dec(supplyRate).mul(new Dec(WAD).minus(fee)).div(WAD).ceil()
|
|
81
|
+
.toString();
|
|
82
|
+
return new Dec(compound(ratePerSecond)).div(1e18).mul(100).toString();
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
export const getBorrowRate = (borrowRate: string, totalBorrowShares: string) => {
|
|
86
|
+
if (totalBorrowShares === '0') {
|
|
87
|
+
return '0';
|
|
88
|
+
}
|
|
89
|
+
return new Dec(compound(borrowRate)).div(1e18).mul(100).toString();
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
export const getApyAfterValuesEstimation = async (selectedMarket: MorphoBlueMarketData, action: string, amount: string, asset: string, web3: Web3, network: NetworkNumber) => {
|
|
93
|
+
const morphoBlueViewContract = MorphoBlueViewContract(web3, network);
|
|
94
|
+
const lltvInWei = assetAmountInWei(selectedMarket.lltv, 'ETH');
|
|
95
|
+
const marketData: MarketParamsStruct = [selectedMarket.loanToken, selectedMarket.collateralToken, selectedMarket.oracle, selectedMarket.irm, lltvInWei];
|
|
96
|
+
const isBorrowOperation = borrowOperations.includes(action);
|
|
97
|
+
const amountInWei = assetAmountInWei(amount, asset);
|
|
98
|
+
let liquidityAdded;
|
|
99
|
+
let liquidityRemoved;
|
|
100
|
+
if (isBorrowOperation) {
|
|
101
|
+
liquidityAdded = action === 'payback' ? amountInWei : '0';
|
|
102
|
+
liquidityRemoved = action === 'borrow' ? amountInWei : '0';
|
|
103
|
+
} else {
|
|
104
|
+
liquidityAdded = action === 'collateral' ? amountInWei : '0';
|
|
105
|
+
liquidityRemoved = action === 'withdraw' ? amountInWei : '0';
|
|
106
|
+
}
|
|
107
|
+
const data = await morphoBlueViewContract.methods.getApyAfterValuesEstimation([
|
|
108
|
+
marketData,
|
|
109
|
+
isBorrowOperation,
|
|
110
|
+
liquidityAdded,
|
|
111
|
+
liquidityRemoved,
|
|
112
|
+
]).call();
|
|
113
|
+
const borrowRate = getBorrowRate(data.borrowRate, data.market.totalBorrowShares);
|
|
114
|
+
const supplyRate = getSupplyRate(data.market.totalSupplyAssets, data.market.totalBorrowAssets, data.borrowRate, data.market.fee);
|
|
115
|
+
return { borrowRate, supplyRate };
|
|
257
116
|
};
|