@defisaver/positions-sdk 0.0.182 → 0.0.183-dev-allocator
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/aaveV3/index.js +3 -3
- package/cjs/config/contracts.d.ts +3 -0
- package/cjs/config/contracts.js +3 -0
- package/cjs/helpers/aaveHelpers/index.js +0 -1
- package/cjs/helpers/morphoBlueHelpers/index.d.ts +5 -0
- package/cjs/helpers/morphoBlueHelpers/index.js +127 -1
- package/cjs/staking/staking.d.ts +2 -3
- package/cjs/staking/staking.js +2 -2
- package/cjs/types/morphoBlue.d.ts +25 -0
- package/esm/aaveV3/index.js +4 -4
- package/esm/config/contracts.d.ts +3 -0
- package/esm/config/contracts.js +3 -0
- package/esm/helpers/aaveHelpers/index.js +0 -1
- package/esm/helpers/morphoBlueHelpers/index.d.ts +5 -0
- package/esm/helpers/morphoBlueHelpers/index.js +124 -0
- package/esm/staking/staking.d.ts +2 -3
- package/esm/staking/staking.js +2 -2
- package/esm/types/morphoBlue.d.ts +25 -0
- package/package.json +49 -49
- package/src/aaveV2/index.ts +227 -227
- package/src/aaveV3/index.ts +624 -628
- 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 -1040
- 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 -199
- 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 +256 -115
- 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 -222
- 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 +177 -154
- package/src/types/spark.ts +131 -131
- package/.vscode/launch.json +0 -17
- package/.vscode/settings.json +0 -37
package/.mocharc.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
{
|
|
2
|
-
"require": "ts-node/register",
|
|
3
|
-
"extension": ["ts"]
|
|
4
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"require": "ts-node/register",
|
|
3
|
+
"extension": ["ts"]
|
|
4
|
+
}
|
package/.nvmrc
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
v20.17.0
|
|
1
|
+
v20.17.0
|
package/README.md
CHANGED
|
@@ -1,69 +1,69 @@
|
|
|
1
|
-
# DeFi Saver Positions SDK
|
|
2
|
-
|
|
3
|
-
Supported protocols:
|
|
4
|
-
- [Maker](https://github.com/defisaver/defisaver-positions-sdk/tree/main/src/maker)
|
|
5
|
-
- [Spark](https://github.com/defisaver/defisaver-positions-sdk/tree/main/src/spark)
|
|
6
|
-
- [CrvUSD](https://github.com/defisaver/defisaver-positions-sdk/tree/main/src/curveUsd)
|
|
7
|
-
- [Aave V2](https://github.com/defisaver/defisaver-positions-sdk/tree/main/src/aaveV2)
|
|
8
|
-
- [Aave V3](https://github.com/defisaver/defisaver-positions-sdk/tree/main/src/aaveV3)
|
|
9
|
-
- [Morpho Aave V2](https://github.com/defisaver/defisaver-positions-sdk/tree/main/src/morphoAaveV2)
|
|
10
|
-
- [Morpho Aave V3](https://github.com/defisaver/defisaver-positions-sdk/tree/main/src/morphoAaveV3)
|
|
11
|
-
- [Compound V2](https://github.com/defisaver/defisaver-positions-sdk/tree/main/src/compoundV2)
|
|
12
|
-
- [Compound V3](https://github.com/defisaver/defisaver-positions-sdk/tree/main/src/compoundV3)
|
|
13
|
-
- [Liquity](https://github.com/defisaver/defisaver-positions-sdk/tree/main/src/liquity)
|
|
14
|
-
- [Chicken Bonds](https://github.com/defisaver/defisaver-positions-sdk/tree/main/src/chickenBonds)
|
|
15
|
-
|
|
16
|
-
## Setup
|
|
17
|
-
Supported Node version is v10.
|
|
18
|
-
|
|
19
|
-
- run `npm install` (first time)
|
|
20
|
-
- run `npm run build`
|
|
21
|
-
|
|
22
|
-
`build` command will generate contracts and build ejs and esm folders
|
|
23
|
-
|
|
24
|
-
## How to use
|
|
25
|
-
[All available imports](https://github.com/defisaver/defisaver-positions-sdk/blob/main/src/index.ts)
|
|
26
|
-
|
|
27
|
-
This is a Compound V3 example, and every other protocol is similar
|
|
28
|
-
```js
|
|
29
|
-
import Web3 from 'web3';
|
|
30
|
-
import { compoundV3 } from '@defisaver/positions-sdk';
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
// every protocol has market data and user data getters
|
|
34
|
-
const {
|
|
35
|
-
getCompoundV3MarketsData,
|
|
36
|
-
getCompoundV3AccountData,
|
|
37
|
-
} = compoundV3;
|
|
38
|
-
|
|
39
|
-
const provider = 'Your RPC provider';
|
|
40
|
-
const web3 = new Web3(provider);
|
|
41
|
-
|
|
42
|
-
const user = '0x123...';
|
|
43
|
-
|
|
44
|
-
const { assetsData } = await getCompoundV3MarketsData(
|
|
45
|
-
web3, // rpc for the network you are using (note: can be tenderly or any other testnet rpc)
|
|
46
|
-
1, // network
|
|
47
|
-
selectedMarket, // market object like in /src/markets/compound/index.ts
|
|
48
|
-
web3, // this must be mainnet rpc - used for getting prices onchain and calculating apys
|
|
49
|
-
);
|
|
50
|
-
|
|
51
|
-
const userData = await getCompoundV3AccountData(
|
|
52
|
-
web3,
|
|
53
|
-
1, // network
|
|
54
|
-
userAddress, // EOA or DSProxy
|
|
55
|
-
'', // proxy address of the user, or just empty string if checking for EOA
|
|
56
|
-
{
|
|
57
|
-
selectedMarket, // market object as in /src/markets/compound/index.ts
|
|
58
|
-
assetsData,
|
|
59
|
-
}
|
|
60
|
-
);
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
More examples found [here](https://github.com/defisaver/defisaver-positions-sdk/tree/main/tests)
|
|
64
|
-
|
|
65
|
-
## Testing
|
|
66
|
-
|
|
67
|
-
`npm run test` - Run all tests
|
|
68
|
-
|
|
69
|
-
`npm run test-single --name=your_test_name` - Run single test for specified name e.g. for MyTest.js test name is MyTest
|
|
1
|
+
# DeFi Saver Positions SDK
|
|
2
|
+
|
|
3
|
+
Supported protocols:
|
|
4
|
+
- [Maker](https://github.com/defisaver/defisaver-positions-sdk/tree/main/src/maker)
|
|
5
|
+
- [Spark](https://github.com/defisaver/defisaver-positions-sdk/tree/main/src/spark)
|
|
6
|
+
- [CrvUSD](https://github.com/defisaver/defisaver-positions-sdk/tree/main/src/curveUsd)
|
|
7
|
+
- [Aave V2](https://github.com/defisaver/defisaver-positions-sdk/tree/main/src/aaveV2)
|
|
8
|
+
- [Aave V3](https://github.com/defisaver/defisaver-positions-sdk/tree/main/src/aaveV3)
|
|
9
|
+
- [Morpho Aave V2](https://github.com/defisaver/defisaver-positions-sdk/tree/main/src/morphoAaveV2)
|
|
10
|
+
- [Morpho Aave V3](https://github.com/defisaver/defisaver-positions-sdk/tree/main/src/morphoAaveV3)
|
|
11
|
+
- [Compound V2](https://github.com/defisaver/defisaver-positions-sdk/tree/main/src/compoundV2)
|
|
12
|
+
- [Compound V3](https://github.com/defisaver/defisaver-positions-sdk/tree/main/src/compoundV3)
|
|
13
|
+
- [Liquity](https://github.com/defisaver/defisaver-positions-sdk/tree/main/src/liquity)
|
|
14
|
+
- [Chicken Bonds](https://github.com/defisaver/defisaver-positions-sdk/tree/main/src/chickenBonds)
|
|
15
|
+
|
|
16
|
+
## Setup
|
|
17
|
+
Supported Node version is v10.
|
|
18
|
+
|
|
19
|
+
- run `npm install` (first time)
|
|
20
|
+
- run `npm run build`
|
|
21
|
+
|
|
22
|
+
`build` command will generate contracts and build ejs and esm folders
|
|
23
|
+
|
|
24
|
+
## How to use
|
|
25
|
+
[All available imports](https://github.com/defisaver/defisaver-positions-sdk/blob/main/src/index.ts)
|
|
26
|
+
|
|
27
|
+
This is a Compound V3 example, and every other protocol is similar
|
|
28
|
+
```js
|
|
29
|
+
import Web3 from 'web3';
|
|
30
|
+
import { compoundV3 } from '@defisaver/positions-sdk';
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
// every protocol has market data and user data getters
|
|
34
|
+
const {
|
|
35
|
+
getCompoundV3MarketsData,
|
|
36
|
+
getCompoundV3AccountData,
|
|
37
|
+
} = compoundV3;
|
|
38
|
+
|
|
39
|
+
const provider = 'Your RPC provider';
|
|
40
|
+
const web3 = new Web3(provider);
|
|
41
|
+
|
|
42
|
+
const user = '0x123...';
|
|
43
|
+
|
|
44
|
+
const { assetsData } = await getCompoundV3MarketsData(
|
|
45
|
+
web3, // rpc for the network you are using (note: can be tenderly or any other testnet rpc)
|
|
46
|
+
1, // network
|
|
47
|
+
selectedMarket, // market object like in /src/markets/compound/index.ts
|
|
48
|
+
web3, // this must be mainnet rpc - used for getting prices onchain and calculating apys
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
const userData = await getCompoundV3AccountData(
|
|
52
|
+
web3,
|
|
53
|
+
1, // network
|
|
54
|
+
userAddress, // EOA or DSProxy
|
|
55
|
+
'', // proxy address of the user, or just empty string if checking for EOA
|
|
56
|
+
{
|
|
57
|
+
selectedMarket, // market object as in /src/markets/compound/index.ts
|
|
58
|
+
assetsData,
|
|
59
|
+
}
|
|
60
|
+
);
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
More examples found [here](https://github.com/defisaver/defisaver-positions-sdk/tree/main/tests)
|
|
64
|
+
|
|
65
|
+
## Testing
|
|
66
|
+
|
|
67
|
+
`npm run test` - Run all tests
|
|
68
|
+
|
|
69
|
+
`npm run test-single --name=your_test_name` - Run single test for specified name e.g. for MyTest.js test name is MyTest
|
package/cjs/aaveV3/index.js
CHANGED
|
@@ -104,7 +104,7 @@ function getAaveV3MarketData(web3, network, market, defaultWeb3) {
|
|
|
104
104
|
params: [],
|
|
105
105
|
},
|
|
106
106
|
{
|
|
107
|
-
target: (0, tokens_1.getAssetInfo)('GHO').address,
|
|
107
|
+
target: (0, tokens_1.getAssetInfo)('GHO', network).address,
|
|
108
108
|
abiItem: (0, utils_1.getAbiItem)(GhoTokenAbi, 'getFacilitatorsList'),
|
|
109
109
|
params: [],
|
|
110
110
|
},
|
|
@@ -148,7 +148,7 @@ function getAaveV3MarketData(web3, network, market, defaultWeb3) {
|
|
|
148
148
|
const assetsData = yield Promise.all(loanInfo
|
|
149
149
|
.map((tokenMarket, i) => __awaiter(this, void 0, void 0, function* () {
|
|
150
150
|
const symbol = market.assets[i];
|
|
151
|
-
const nativeAsset = symbol === 'GHO';
|
|
151
|
+
const nativeAsset = symbol === 'GHO' && network === common_1.NetworkNumber.Eth;
|
|
152
152
|
// eslint-disable-next-line guard-for-in
|
|
153
153
|
for (const eModeIndex in eModeCategoriesData) {
|
|
154
154
|
if ((0, utils_1.isEnabledOnBitmap)(Number(eModeCategoriesData[eModeIndex].collateralBitmap), Number(tokenMarket.assetId)))
|
|
@@ -423,7 +423,7 @@ const getAaveV3AccountData = (web3, network, address, extractedState) => __await
|
|
|
423
423
|
}
|
|
424
424
|
if (!usedAssets[asset])
|
|
425
425
|
usedAssets[asset] = {};
|
|
426
|
-
const nativeAsset = asset === 'GHO';
|
|
426
|
+
const nativeAsset = asset === 'GHO' && network === common_1.NetworkNumber.Eth;
|
|
427
427
|
let discountRateOnBorrow = '0';
|
|
428
428
|
const borrowed = new decimal_js_1.default(borrowedStable).add(borrowedVariable).toString();
|
|
429
429
|
if (nativeAsset && new decimal_js_1.default(borrowed).gt(0) && new decimal_js_1.default(stkAaveBalance).gt(0)) {
|
package/cjs/config/contracts.js
CHANGED
|
@@ -12,3 +12,8 @@ export declare const getApyAfterValuesEstimation: (selectedMarket: MorphoBlueMar
|
|
|
12
12
|
borrowRate: string;
|
|
13
13
|
supplyRate: string;
|
|
14
14
|
}>;
|
|
15
|
+
export declare const getReallocatableLiquidity: (marketId: string, network?: NetworkNumber) => Promise<string>;
|
|
16
|
+
export declare const getReallocation: (marketId: string, liquidityToAllocate: string, network?: NetworkNumber) => Promise<{
|
|
17
|
+
vaults: string[];
|
|
18
|
+
withdrawals: [string[], string][][];
|
|
19
|
+
}>;
|
|
@@ -12,13 +12,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
12
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.getApyAfterValuesEstimation = exports.getBorrowRate = exports.getSupplyRate = exports.getMorphoBlueAggregatedPositionData = void 0;
|
|
15
|
+
exports.getReallocation = exports.getReallocatableLiquidity = exports.getApyAfterValuesEstimation = exports.getBorrowRate = exports.getSupplyRate = exports.getMorphoBlueAggregatedPositionData = void 0;
|
|
16
16
|
const decimal_js_1 = __importDefault(require("decimal.js"));
|
|
17
17
|
const tokens_1 = require("@defisaver/tokens");
|
|
18
18
|
const moneymarket_1 = require("../../moneymarket");
|
|
19
19
|
const staking_1 = require("../../staking");
|
|
20
|
+
const common_1 = require("../../types/common");
|
|
20
21
|
const constants_1 = require("../../constants");
|
|
21
22
|
const contracts_1 = require("../../contracts");
|
|
23
|
+
const utils_1 = require("../../services/utils");
|
|
22
24
|
const getMorphoBlueAggregatedPositionData = ({ usedAssets, assetsData, marketInfo }) => {
|
|
23
25
|
var _a, _b, _c, _d, _e, _f;
|
|
24
26
|
const payload = {};
|
|
@@ -110,3 +112,127 @@ const getApyAfterValuesEstimation = (selectedMarket, action, amount, asset, web3
|
|
|
110
112
|
return { borrowRate, supplyRate };
|
|
111
113
|
});
|
|
112
114
|
exports.getApyAfterValuesEstimation = getApyAfterValuesEstimation;
|
|
115
|
+
const API_URL = 'https://blue-api.morpho.org/graphql';
|
|
116
|
+
const MARKET_QUERY = `
|
|
117
|
+
query MarketByUniqueKey($uniqueKey: String!, $chainId: Int!) {
|
|
118
|
+
marketByUniqueKey(uniqueKey: $uniqueKey, chainId: $chainId) {
|
|
119
|
+
reallocatableLiquidityAssets
|
|
120
|
+
loanAsset {
|
|
121
|
+
address
|
|
122
|
+
decimals
|
|
123
|
+
priceUsd
|
|
124
|
+
}
|
|
125
|
+
state {
|
|
126
|
+
liquidityAssets
|
|
127
|
+
}
|
|
128
|
+
publicAllocatorSharedLiquidity {
|
|
129
|
+
assets
|
|
130
|
+
vault {
|
|
131
|
+
address
|
|
132
|
+
name
|
|
133
|
+
}
|
|
134
|
+
allocationMarket {
|
|
135
|
+
uniqueKey
|
|
136
|
+
loanAsset {
|
|
137
|
+
address
|
|
138
|
+
}
|
|
139
|
+
collateralAsset {
|
|
140
|
+
address
|
|
141
|
+
}
|
|
142
|
+
irmAddress
|
|
143
|
+
oracle {
|
|
144
|
+
address
|
|
145
|
+
}
|
|
146
|
+
lltv
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
loanAsset {
|
|
150
|
+
address
|
|
151
|
+
}
|
|
152
|
+
collateralAsset {
|
|
153
|
+
address
|
|
154
|
+
}
|
|
155
|
+
oracle {
|
|
156
|
+
address
|
|
157
|
+
}
|
|
158
|
+
irmAddress
|
|
159
|
+
lltv
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
`;
|
|
163
|
+
const getReallocatableLiquidity = (marketId, network = common_1.NetworkNumber.Eth) => __awaiter(void 0, void 0, void 0, function* () {
|
|
164
|
+
var _a;
|
|
165
|
+
const response = yield fetch(API_URL, {
|
|
166
|
+
method: 'POST',
|
|
167
|
+
headers: { 'Content-Type': 'application/json' },
|
|
168
|
+
body: JSON.stringify({
|
|
169
|
+
query: MARKET_QUERY,
|
|
170
|
+
variables: { uniqueKey: marketId, chainId: network },
|
|
171
|
+
}),
|
|
172
|
+
});
|
|
173
|
+
const data = yield response.json();
|
|
174
|
+
const marketData = (_a = data === null || data === void 0 ? void 0 : data.data) === null || _a === void 0 ? void 0 : _a.marketByUniqueKey;
|
|
175
|
+
if (!marketData)
|
|
176
|
+
throw new Error('Market data not found');
|
|
177
|
+
return marketData.reallocatableLiquidityAssets;
|
|
178
|
+
});
|
|
179
|
+
exports.getReallocatableLiquidity = getReallocatableLiquidity;
|
|
180
|
+
const getReallocation = (marketId, liquidityToAllocate, network = common_1.NetworkNumber.Eth) => __awaiter(void 0, void 0, void 0, function* () {
|
|
181
|
+
var _b, _c, _d;
|
|
182
|
+
const response = yield fetch(API_URL, {
|
|
183
|
+
method: 'POST',
|
|
184
|
+
headers: { 'Content-Type': 'application/json' },
|
|
185
|
+
body: JSON.stringify({
|
|
186
|
+
query: MARKET_QUERY,
|
|
187
|
+
variables: { uniqueKey: marketId, chainId: network },
|
|
188
|
+
}),
|
|
189
|
+
});
|
|
190
|
+
const data = yield response.json();
|
|
191
|
+
const marketData = (_b = data === null || data === void 0 ? void 0 : data.data) === null || _b === void 0 ? void 0 : _b.marketByUniqueKey;
|
|
192
|
+
if (!marketData)
|
|
193
|
+
throw new Error('Market data not found');
|
|
194
|
+
if (new decimal_js_1.default(marketData.reallocatableLiquidityAssets).lt(liquidityToAllocate))
|
|
195
|
+
throw new Error('Not enough liquidity available to allocate');
|
|
196
|
+
const vaultTotalAssets = marketData.publicAllocatorSharedLiquidity.reduce((acc, item) => {
|
|
197
|
+
const vaultAddress = item.vault.address;
|
|
198
|
+
acc[vaultAddress] = new decimal_js_1.default(acc[vaultAddress] || '0').add(item.assets).toString();
|
|
199
|
+
return acc;
|
|
200
|
+
}, {});
|
|
201
|
+
const sortedVaults = Object.entries(vaultTotalAssets).sort(([, a], [, b]) => new decimal_js_1.default(b || '0').sub(a || '0').toNumber());
|
|
202
|
+
const withdrawalsPerVault = {};
|
|
203
|
+
let totalReallocated = '0';
|
|
204
|
+
for (const [vaultAddress] of sortedVaults) {
|
|
205
|
+
if (new decimal_js_1.default(totalReallocated).gte(liquidityToAllocate))
|
|
206
|
+
break;
|
|
207
|
+
const vaultAllocations = marketData.publicAllocatorSharedLiquidity.filter((item) => (0, utils_1.compareAddresses)(item.vault.address, vaultAddress));
|
|
208
|
+
for (const item of vaultAllocations) {
|
|
209
|
+
if (new decimal_js_1.default(totalReallocated).gte(liquidityToAllocate))
|
|
210
|
+
break;
|
|
211
|
+
const itemAmount = item.assets;
|
|
212
|
+
const leftToAllocate = new decimal_js_1.default(liquidityToAllocate).sub(totalReallocated).toString();
|
|
213
|
+
const amountToTake = new decimal_js_1.default(itemAmount).lt(leftToAllocate) ? itemAmount : leftToAllocate;
|
|
214
|
+
totalReallocated = new decimal_js_1.default(totalReallocated).add(amountToTake).toString();
|
|
215
|
+
const withdrawal = [
|
|
216
|
+
[
|
|
217
|
+
item.allocationMarket.loanAsset.address,
|
|
218
|
+
(_c = item.allocationMarket.collateralAsset) === null || _c === void 0 ? void 0 : _c.address,
|
|
219
|
+
(_d = item.allocationMarket.oracle) === null || _d === void 0 ? void 0 : _d.address,
|
|
220
|
+
item.allocationMarket.irmAddress,
|
|
221
|
+
item.allocationMarket.lltv,
|
|
222
|
+
],
|
|
223
|
+
amountToTake.toString(),
|
|
224
|
+
];
|
|
225
|
+
if (!withdrawalsPerVault[vaultAddress]) {
|
|
226
|
+
withdrawalsPerVault[vaultAddress] = [];
|
|
227
|
+
}
|
|
228
|
+
withdrawalsPerVault[vaultAddress].push(withdrawal);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
const vaults = Object.keys(withdrawalsPerVault);
|
|
232
|
+
const withdrawals = vaults.map((vaultAddress) => withdrawalsPerVault[vaultAddress]);
|
|
233
|
+
return {
|
|
234
|
+
vaults,
|
|
235
|
+
withdrawals,
|
|
236
|
+
};
|
|
237
|
+
});
|
|
238
|
+
exports.getReallocation = getReallocation;
|
package/cjs/staking/staking.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import Web3 from 'web3';
|
|
2
|
-
import { MMAssetsData, MMUsedAssets
|
|
2
|
+
import { MMAssetsData, MMUsedAssets } from '../types/common';
|
|
3
3
|
export declare const getStETHApy: (web3: Web3, fromBlock?: number, blockNumber?: 'latest' | number) => Promise<string>;
|
|
4
4
|
export declare const getCbETHApy: (web3: Web3, blockNumber?: 'latest' | number) => Promise<string>;
|
|
5
5
|
export declare const getREthApy: (web3: Web3, blockNumber?: 'latest' | number) => Promise<string>;
|
|
@@ -8,11 +8,10 @@ export declare const getSsrApy: () => Promise<string>;
|
|
|
8
8
|
export declare const STAKING_ASSETS: string[];
|
|
9
9
|
export declare const getStakingApy: (asset: string, web3: Web3, blockNumber?: 'latest' | number, fromBlock?: number | undefined) => "0" | Promise<any> | undefined;
|
|
10
10
|
export declare const calculateInterestEarned: (principal: string, interest: string, type: string, apy?: boolean) => number;
|
|
11
|
-
export declare const calculateNetApy: ({ usedAssets, assetsData, isMorpho
|
|
11
|
+
export declare const calculateNetApy: ({ usedAssets, assetsData, isMorpho }: {
|
|
12
12
|
usedAssets: MMUsedAssets;
|
|
13
13
|
assetsData: MMAssetsData;
|
|
14
14
|
isMorpho?: boolean | undefined;
|
|
15
|
-
network?: NetworkNumber | undefined;
|
|
16
15
|
}) => {
|
|
17
16
|
netApy: string;
|
|
18
17
|
totalInterestUsd: string;
|
package/cjs/staking/staking.js
CHANGED
|
@@ -170,7 +170,7 @@ const calculateInterestEarned = (principal, interest, type, apy = false) => {
|
|
|
170
170
|
return (+principal * (Math.pow(((1 + (+interest / 100) / constants_1.BLOCKS_IN_A_YEAR)), (constants_1.BLOCKS_IN_A_YEAR * interval)))) - +principal; // eslint-disable-line
|
|
171
171
|
};
|
|
172
172
|
exports.calculateInterestEarned = calculateInterestEarned;
|
|
173
|
-
const calculateNetApy = ({ usedAssets, assetsData, isMorpho = false
|
|
173
|
+
const calculateNetApy = ({ usedAssets, assetsData, isMorpho = false }) => {
|
|
174
174
|
const sumValues = Object.values(usedAssets).reduce((_acc, usedAsset) => {
|
|
175
175
|
const acc = Object.assign({}, _acc);
|
|
176
176
|
const assetData = assetsData[usedAsset.symbol];
|
|
@@ -193,7 +193,7 @@ const calculateNetApy = ({ usedAssets, assetsData, isMorpho = false, network = 1
|
|
|
193
193
|
acc.borrowedUsd = new decimal_js_1.default(acc.borrowedUsd).add(amount).toString();
|
|
194
194
|
const rate = isMorpho
|
|
195
195
|
? usedAsset.borrowRate === '0' ? assetData.borrowRateP2P : usedAsset.borrowRate
|
|
196
|
-
: (usedAsset.symbol === 'GHO' &&
|
|
196
|
+
: (usedAsset.symbol === 'GHO' && assetsData.nativeAsset)
|
|
197
197
|
? usedAsset.discountedBorrowRate
|
|
198
198
|
: ((usedAsset === null || usedAsset === void 0 ? void 0 : usedAsset.interestMode) === '1' ? usedAsset.stableBorrowRate : assetData.borrowRate);
|
|
199
199
|
const borrowInterest = (0, exports.calculateInterestEarned)(amount, rate, 'year', true);
|
|
@@ -137,3 +137,28 @@ export interface MorphoBluePositionData {
|
|
|
137
137
|
supplyShares: string;
|
|
138
138
|
borrowShares: string;
|
|
139
139
|
}
|
|
140
|
+
export interface MorphoBlueVault {
|
|
141
|
+
address: string;
|
|
142
|
+
}
|
|
143
|
+
export interface MorphoBlueAllocationMarket {
|
|
144
|
+
loanAsset: {
|
|
145
|
+
address: string;
|
|
146
|
+
};
|
|
147
|
+
collateralAsset: {
|
|
148
|
+
address: string;
|
|
149
|
+
};
|
|
150
|
+
oracle: {
|
|
151
|
+
address: string;
|
|
152
|
+
};
|
|
153
|
+
irmAddress: string;
|
|
154
|
+
lltv: string;
|
|
155
|
+
}
|
|
156
|
+
export interface MorphoBluePublicAllocatorItem {
|
|
157
|
+
vault: MorphoBlueVault;
|
|
158
|
+
assets: string;
|
|
159
|
+
allocationMarket: MorphoBlueAllocationMarket;
|
|
160
|
+
}
|
|
161
|
+
export interface MorphoBlueRealloactionMarketData {
|
|
162
|
+
reallocatableLiquidityAssets: string;
|
|
163
|
+
publicAllocatorSharedLiquidity: MorphoBluePublicAllocatorItem[];
|
|
164
|
+
}
|
package/esm/aaveV3/index.js
CHANGED
|
@@ -16,7 +16,7 @@ import { getStakingApy, STAKING_ASSETS } from '../staking';
|
|
|
16
16
|
import { multicall } from '../multicall';
|
|
17
17
|
import { getAssetsBalances } from '../assets';
|
|
18
18
|
import { aprToApy, calculateBorrowingAssetLimit } from '../moneymarket';
|
|
19
|
-
import { aaveAnyGetAggregatedPositionData, aaveV3IsInIsolationMode, aaveV3IsInSiloedMode
|
|
19
|
+
import { aaveAnyGetAggregatedPositionData, aaveV3IsInIsolationMode, aaveV3IsInSiloedMode } from '../helpers/aaveHelpers';
|
|
20
20
|
import { AAVE_V3 } from '../markets/aave';
|
|
21
21
|
export const test = (web3, network) => {
|
|
22
22
|
const contract = AaveV3ViewContract(web3, 1);
|
|
@@ -95,7 +95,7 @@ export function getAaveV3MarketData(web3, network, market, defaultWeb3) {
|
|
|
95
95
|
params: [],
|
|
96
96
|
},
|
|
97
97
|
{
|
|
98
|
-
target: getAssetInfo('GHO').address,
|
|
98
|
+
target: getAssetInfo('GHO', network).address,
|
|
99
99
|
abiItem: getAbiItem(GhoTokenAbi, 'getFacilitatorsList'),
|
|
100
100
|
params: [],
|
|
101
101
|
},
|
|
@@ -139,7 +139,7 @@ export function getAaveV3MarketData(web3, network, market, defaultWeb3) {
|
|
|
139
139
|
const assetsData = yield Promise.all(loanInfo
|
|
140
140
|
.map((tokenMarket, i) => __awaiter(this, void 0, void 0, function* () {
|
|
141
141
|
const symbol = market.assets[i];
|
|
142
|
-
const nativeAsset = symbol === 'GHO';
|
|
142
|
+
const nativeAsset = symbol === 'GHO' && network === NetworkNumber.Eth;
|
|
143
143
|
// eslint-disable-next-line guard-for-in
|
|
144
144
|
for (const eModeIndex in eModeCategoriesData) {
|
|
145
145
|
if (isEnabledOnBitmap(Number(eModeCategoriesData[eModeIndex].collateralBitmap), Number(tokenMarket.assetId)))
|
|
@@ -412,7 +412,7 @@ export const getAaveV3AccountData = (web3, network, address, extractedState) =>
|
|
|
412
412
|
}
|
|
413
413
|
if (!usedAssets[asset])
|
|
414
414
|
usedAssets[asset] = {};
|
|
415
|
-
const nativeAsset = asset === 'GHO';
|
|
415
|
+
const nativeAsset = asset === 'GHO' && network === NetworkNumber.Eth;
|
|
416
416
|
let discountRateOnBorrow = '0';
|
|
417
417
|
const borrowed = new Dec(borrowedStable).add(borrowedVariable).toString();
|
|
418
418
|
if (nativeAsset && new Dec(borrowed).gt(0) && new Dec(stkAaveBalance).gt(0)) {
|
package/esm/config/contracts.js
CHANGED
|
@@ -12,3 +12,8 @@ export declare const getApyAfterValuesEstimation: (selectedMarket: MorphoBlueMar
|
|
|
12
12
|
borrowRate: string;
|
|
13
13
|
supplyRate: string;
|
|
14
14
|
}>;
|
|
15
|
+
export declare const getReallocatableLiquidity: (marketId: string, network?: NetworkNumber) => Promise<string>;
|
|
16
|
+
export declare const getReallocation: (marketId: string, liquidityToAllocate: string, network?: NetworkNumber) => Promise<{
|
|
17
|
+
vaults: string[];
|
|
18
|
+
withdrawals: [string[], string][][];
|
|
19
|
+
}>;
|
|
@@ -11,8 +11,10 @@ import Dec from 'decimal.js';
|
|
|
11
11
|
import { assetAmountInWei } from '@defisaver/tokens';
|
|
12
12
|
import { calcLeverageLiqPrice, getAssetsTotal, isLeveragedPos } from '../../moneymarket';
|
|
13
13
|
import { calculateNetApy } from '../../staking';
|
|
14
|
+
import { NetworkNumber } from '../../types/common';
|
|
14
15
|
import { borrowOperations, SECONDS_PER_YEAR, WAD } from '../../constants';
|
|
15
16
|
import { MorphoBlueViewContract } from '../../contracts';
|
|
17
|
+
import { compareAddresses } from '../../services/utils';
|
|
16
18
|
export const getMorphoBlueAggregatedPositionData = ({ usedAssets, assetsData, marketInfo }) => {
|
|
17
19
|
var _a, _b, _c, _d, _e, _f;
|
|
18
20
|
const payload = {};
|
|
@@ -100,3 +102,125 @@ export const getApyAfterValuesEstimation = (selectedMarket, action, amount, asse
|
|
|
100
102
|
const supplyRate = getSupplyRate(data.market.totalSupplyAssets, data.market.totalBorrowAssets, data.borrowRate, data.market.fee);
|
|
101
103
|
return { borrowRate, supplyRate };
|
|
102
104
|
});
|
|
105
|
+
const API_URL = 'https://blue-api.morpho.org/graphql';
|
|
106
|
+
const MARKET_QUERY = `
|
|
107
|
+
query MarketByUniqueKey($uniqueKey: String!, $chainId: Int!) {
|
|
108
|
+
marketByUniqueKey(uniqueKey: $uniqueKey, chainId: $chainId) {
|
|
109
|
+
reallocatableLiquidityAssets
|
|
110
|
+
loanAsset {
|
|
111
|
+
address
|
|
112
|
+
decimals
|
|
113
|
+
priceUsd
|
|
114
|
+
}
|
|
115
|
+
state {
|
|
116
|
+
liquidityAssets
|
|
117
|
+
}
|
|
118
|
+
publicAllocatorSharedLiquidity {
|
|
119
|
+
assets
|
|
120
|
+
vault {
|
|
121
|
+
address
|
|
122
|
+
name
|
|
123
|
+
}
|
|
124
|
+
allocationMarket {
|
|
125
|
+
uniqueKey
|
|
126
|
+
loanAsset {
|
|
127
|
+
address
|
|
128
|
+
}
|
|
129
|
+
collateralAsset {
|
|
130
|
+
address
|
|
131
|
+
}
|
|
132
|
+
irmAddress
|
|
133
|
+
oracle {
|
|
134
|
+
address
|
|
135
|
+
}
|
|
136
|
+
lltv
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
loanAsset {
|
|
140
|
+
address
|
|
141
|
+
}
|
|
142
|
+
collateralAsset {
|
|
143
|
+
address
|
|
144
|
+
}
|
|
145
|
+
oracle {
|
|
146
|
+
address
|
|
147
|
+
}
|
|
148
|
+
irmAddress
|
|
149
|
+
lltv
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
`;
|
|
153
|
+
export const getReallocatableLiquidity = (marketId, network = NetworkNumber.Eth) => __awaiter(void 0, void 0, void 0, function* () {
|
|
154
|
+
var _a;
|
|
155
|
+
const response = yield fetch(API_URL, {
|
|
156
|
+
method: 'POST',
|
|
157
|
+
headers: { 'Content-Type': 'application/json' },
|
|
158
|
+
body: JSON.stringify({
|
|
159
|
+
query: MARKET_QUERY,
|
|
160
|
+
variables: { uniqueKey: marketId, chainId: network },
|
|
161
|
+
}),
|
|
162
|
+
});
|
|
163
|
+
const data = yield response.json();
|
|
164
|
+
const marketData = (_a = data === null || data === void 0 ? void 0 : data.data) === null || _a === void 0 ? void 0 : _a.marketByUniqueKey;
|
|
165
|
+
if (!marketData)
|
|
166
|
+
throw new Error('Market data not found');
|
|
167
|
+
return marketData.reallocatableLiquidityAssets;
|
|
168
|
+
});
|
|
169
|
+
export const getReallocation = (marketId, liquidityToAllocate, network = NetworkNumber.Eth) => __awaiter(void 0, void 0, void 0, function* () {
|
|
170
|
+
var _b, _c, _d;
|
|
171
|
+
const response = yield 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
|
+
const data = yield response.json();
|
|
180
|
+
const marketData = (_b = data === null || data === void 0 ? void 0 : data.data) === null || _b === void 0 ? void 0 : _b.marketByUniqueKey;
|
|
181
|
+
if (!marketData)
|
|
182
|
+
throw new Error('Market data not found');
|
|
183
|
+
if (new Dec(marketData.reallocatableLiquidityAssets).lt(liquidityToAllocate))
|
|
184
|
+
throw new Error('Not enough liquidity available to allocate');
|
|
185
|
+
const vaultTotalAssets = marketData.publicAllocatorSharedLiquidity.reduce((acc, item) => {
|
|
186
|
+
const vaultAddress = item.vault.address;
|
|
187
|
+
acc[vaultAddress] = new Dec(acc[vaultAddress] || '0').add(item.assets).toString();
|
|
188
|
+
return acc;
|
|
189
|
+
}, {});
|
|
190
|
+
const sortedVaults = Object.entries(vaultTotalAssets).sort(([, a], [, b]) => new Dec(b || '0').sub(a || '0').toNumber());
|
|
191
|
+
const withdrawalsPerVault = {};
|
|
192
|
+
let totalReallocated = '0';
|
|
193
|
+
for (const [vaultAddress] of sortedVaults) {
|
|
194
|
+
if (new Dec(totalReallocated).gte(liquidityToAllocate))
|
|
195
|
+
break;
|
|
196
|
+
const vaultAllocations = marketData.publicAllocatorSharedLiquidity.filter((item) => compareAddresses(item.vault.address, vaultAddress));
|
|
197
|
+
for (const item of vaultAllocations) {
|
|
198
|
+
if (new Dec(totalReallocated).gte(liquidityToAllocate))
|
|
199
|
+
break;
|
|
200
|
+
const itemAmount = item.assets;
|
|
201
|
+
const leftToAllocate = new Dec(liquidityToAllocate).sub(totalReallocated).toString();
|
|
202
|
+
const amountToTake = new Dec(itemAmount).lt(leftToAllocate) ? itemAmount : leftToAllocate;
|
|
203
|
+
totalReallocated = new Dec(totalReallocated).add(amountToTake).toString();
|
|
204
|
+
const withdrawal = [
|
|
205
|
+
[
|
|
206
|
+
item.allocationMarket.loanAsset.address,
|
|
207
|
+
(_c = item.allocationMarket.collateralAsset) === null || _c === void 0 ? void 0 : _c.address,
|
|
208
|
+
(_d = item.allocationMarket.oracle) === null || _d === void 0 ? void 0 : _d.address,
|
|
209
|
+
item.allocationMarket.irmAddress,
|
|
210
|
+
item.allocationMarket.lltv,
|
|
211
|
+
],
|
|
212
|
+
amountToTake.toString(),
|
|
213
|
+
];
|
|
214
|
+
if (!withdrawalsPerVault[vaultAddress]) {
|
|
215
|
+
withdrawalsPerVault[vaultAddress] = [];
|
|
216
|
+
}
|
|
217
|
+
withdrawalsPerVault[vaultAddress].push(withdrawal);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
const vaults = Object.keys(withdrawalsPerVault);
|
|
221
|
+
const withdrawals = vaults.map((vaultAddress) => withdrawalsPerVault[vaultAddress]);
|
|
222
|
+
return {
|
|
223
|
+
vaults,
|
|
224
|
+
withdrawals,
|
|
225
|
+
};
|
|
226
|
+
});
|