@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
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
|
|
@@ -12,8 +12,3 @@ 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,15 +12,13 @@ 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.
|
|
15
|
+
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");
|
|
21
20
|
const constants_1 = require("../../constants");
|
|
22
21
|
const contracts_1 = require("../../contracts");
|
|
23
|
-
const utils_1 = require("../../services/utils");
|
|
24
22
|
const getMorphoBlueAggregatedPositionData = ({ usedAssets, assetsData, marketInfo }) => {
|
|
25
23
|
var _a, _b, _c, _d, _e, _f;
|
|
26
24
|
const payload = {};
|
|
@@ -112,127 +110,3 @@ const getApyAfterValuesEstimation = (selectedMarket, action, amount, asset, web3
|
|
|
112
110
|
return { borrowRate, supplyRate };
|
|
113
111
|
});
|
|
114
112
|
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.js
CHANGED
|
@@ -88,7 +88,7 @@ const getDsrApy = (web3, blockNumber = 'latest') => __awaiter(void 0, void 0, vo
|
|
|
88
88
|
});
|
|
89
89
|
exports.getDsrApy = getDsrApy;
|
|
90
90
|
const getSsrApy = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
91
|
-
const res = yield fetch('https://
|
|
91
|
+
const res = yield fetch('https://fe.defisaver.com/api/sky/data');
|
|
92
92
|
const data = yield res.json();
|
|
93
93
|
return new decimal_js_1.default(data.data.skyData[0].sky_savings_rate_apy).mul(100).toString();
|
|
94
94
|
});
|
|
@@ -111,7 +111,7 @@ const getSuperOETHApy = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
111
111
|
return new decimal_js_1.default(data.data.oTokenApies[0].apy).mul(100).toString();
|
|
112
112
|
});
|
|
113
113
|
const getApyFromDfsApi = (asset) => __awaiter(void 0, void 0, void 0, function* () {
|
|
114
|
-
const res = yield fetch(`https://
|
|
114
|
+
const res = yield fetch(`https://fe.defisaver.com/api/staking/apy?asset=${asset}`);
|
|
115
115
|
const data = yield res.json();
|
|
116
116
|
// if our server returns apr, transform it into apy
|
|
117
117
|
if (['weETH'].includes(asset)) {
|
|
@@ -137,28 +137,3 @@ 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
|
-
}
|
|
@@ -12,8 +12,3 @@ 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,10 +11,8 @@ 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';
|
|
15
14
|
import { borrowOperations, SECONDS_PER_YEAR, WAD } from '../../constants';
|
|
16
15
|
import { MorphoBlueViewContract } from '../../contracts';
|
|
17
|
-
import { compareAddresses } from '../../services/utils';
|
|
18
16
|
export const getMorphoBlueAggregatedPositionData = ({ usedAssets, assetsData, marketInfo }) => {
|
|
19
17
|
var _a, _b, _c, _d, _e, _f;
|
|
20
18
|
const payload = {};
|
|
@@ -102,125 +100,3 @@ export const getApyAfterValuesEstimation = (selectedMarket, action, amount, asse
|
|
|
102
100
|
const supplyRate = getSupplyRate(data.market.totalSupplyAssets, data.market.totalBorrowAssets, data.borrowRate, data.market.fee);
|
|
103
101
|
return { borrowRate, supplyRate };
|
|
104
102
|
});
|
|
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
|
-
});
|
package/esm/staking/staking.js
CHANGED
|
@@ -78,7 +78,7 @@ export const getDsrApy = (web3, blockNumber = 'latest') => __awaiter(void 0, voi
|
|
|
78
78
|
.toString();
|
|
79
79
|
});
|
|
80
80
|
export const getSsrApy = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
81
|
-
const res = yield fetch('https://
|
|
81
|
+
const res = yield fetch('https://fe.defisaver.com/api/sky/data');
|
|
82
82
|
const data = yield res.json();
|
|
83
83
|
return new Dec(data.data.skyData[0].sky_savings_rate_apy).mul(100).toString();
|
|
84
84
|
});
|
|
@@ -100,7 +100,7 @@ const getSuperOETHApy = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
100
100
|
return new Dec(data.data.oTokenApies[0].apy).mul(100).toString();
|
|
101
101
|
});
|
|
102
102
|
const getApyFromDfsApi = (asset) => __awaiter(void 0, void 0, void 0, function* () {
|
|
103
|
-
const res = yield fetch(`https://
|
|
103
|
+
const res = yield fetch(`https://fe.defisaver.com/api/staking/apy?asset=${asset}`);
|
|
104
104
|
const data = yield res.json();
|
|
105
105
|
// if our server returns apr, transform it into apy
|
|
106
106
|
if (['weETH'].includes(asset)) {
|
|
@@ -137,28 +137,3 @@ 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/package.json
CHANGED
|
@@ -1,49 +1,49 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@defisaver/positions-sdk",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "",
|
|
5
|
-
"main": "./cjs/index.js",
|
|
6
|
-
"module": "./esm/index.js",
|
|
7
|
-
"types": "./esm/index.d.ts",
|
|
8
|
-
"scripts": {
|
|
9
|
-
"build:esm": "rm -rf esm && tsc -p tsconfig.esm.json",
|
|
10
|
-
"build:cjs": "rm -rf cjs && tsc -p tsconfig.cjs.json",
|
|
11
|
-
"build": "npm run lint && npm run generate-contracts && npm run build:cjs && npm run build:esm",
|
|
12
|
-
"dev": "npm run generate-contracts && tsc -p tsconfig.json --watch",
|
|
13
|
-
"lint": "eslint src/ --fix",
|
|
14
|
-
"lint-check": "eslint src/",
|
|
15
|
-
"generate-contracts": "node scripts/generateContracts.js",
|
|
16
|
-
"test": "mocha tests/*",
|
|
17
|
-
"test-single": "mocha ./tests/$npm_config_name.ts",
|
|
18
|
-
"test:debugger": "mocha --inspect-brk tests/*",
|
|
19
|
-
"build-test": "npm run build && npm run test",
|
|
20
|
-
"version-bump": "git commit -am \"Version bump to $(npm version patch | cut -c 2-)\""
|
|
21
|
-
},
|
|
22
|
-
"keywords": [],
|
|
23
|
-
"author": "",
|
|
24
|
-
"license": "ISC",
|
|
25
|
-
"dependencies": {
|
|
26
|
-
"@defisaver/tokens": "^1.5.50",
|
|
27
|
-
"@ethersproject/bignumber": "^5.7.0",
|
|
28
|
-
"@morpho-org/morpho-aave-v3-sdk": "^1.5.3",
|
|
29
|
-
"decimal.js": "^10.4.3"
|
|
30
|
-
},
|
|
31
|
-
"devDependencies": {
|
|
32
|
-
"@defisaver/eslint-config": "^1.0.1",
|
|
33
|
-
"@types/chai": "^5.0.0",
|
|
34
|
-
"@types/mocha": "^10.0.9",
|
|
35
|
-
"@types/web3": "^1.0.20",
|
|
36
|
-
"chai": "^4.3.8",
|
|
37
|
-
"dotenv": "^16.3.1",
|
|
38
|
-
"eslint": "^8.49.0",
|
|
39
|
-
"eslint-plugin-import": "^2.31.0",
|
|
40
|
-
"mocha": "^10.2.0",
|
|
41
|
-
"ts-node": "^10.9.2",
|
|
42
|
-
"typechain": "^8.3.1",
|
|
43
|
-
"typechain-target-web3-v1-3mihai3": "^6.0.2",
|
|
44
|
-
"typescript": "^5.2.2"
|
|
45
|
-
},
|
|
46
|
-
"peerDependencies": {
|
|
47
|
-
"web3": "^1.10.2"
|
|
48
|
-
}
|
|
49
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@defisaver/positions-sdk",
|
|
3
|
+
"version": "0.0.184",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "./cjs/index.js",
|
|
6
|
+
"module": "./esm/index.js",
|
|
7
|
+
"types": "./esm/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build:esm": "rm -rf esm && tsc -p tsconfig.esm.json",
|
|
10
|
+
"build:cjs": "rm -rf cjs && tsc -p tsconfig.cjs.json",
|
|
11
|
+
"build": "npm run lint && npm run generate-contracts && npm run build:cjs && npm run build:esm",
|
|
12
|
+
"dev": "npm run generate-contracts && tsc -p tsconfig.json --watch",
|
|
13
|
+
"lint": "eslint src/ --fix",
|
|
14
|
+
"lint-check": "eslint src/",
|
|
15
|
+
"generate-contracts": "node scripts/generateContracts.js",
|
|
16
|
+
"test": "mocha tests/*",
|
|
17
|
+
"test-single": "mocha ./tests/$npm_config_name.ts",
|
|
18
|
+
"test:debugger": "mocha --inspect-brk tests/*",
|
|
19
|
+
"build-test": "npm run build && npm run test",
|
|
20
|
+
"version-bump": "git commit -am \"Version bump to $(npm version patch | cut -c 2-)\""
|
|
21
|
+
},
|
|
22
|
+
"keywords": [],
|
|
23
|
+
"author": "",
|
|
24
|
+
"license": "ISC",
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@defisaver/tokens": "^1.5.50",
|
|
27
|
+
"@ethersproject/bignumber": "^5.7.0",
|
|
28
|
+
"@morpho-org/morpho-aave-v3-sdk": "^1.5.3",
|
|
29
|
+
"decimal.js": "^10.4.3"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@defisaver/eslint-config": "^1.0.1",
|
|
33
|
+
"@types/chai": "^5.0.0",
|
|
34
|
+
"@types/mocha": "^10.0.9",
|
|
35
|
+
"@types/web3": "^1.0.20",
|
|
36
|
+
"chai": "^4.3.8",
|
|
37
|
+
"dotenv": "^16.3.1",
|
|
38
|
+
"eslint": "^8.49.0",
|
|
39
|
+
"eslint-plugin-import": "^2.31.0",
|
|
40
|
+
"mocha": "^10.2.0",
|
|
41
|
+
"ts-node": "^10.9.2",
|
|
42
|
+
"typechain": "^8.3.1",
|
|
43
|
+
"typechain-target-web3-v1-3mihai3": "^6.0.2",
|
|
44
|
+
"typescript": "^5.2.2"
|
|
45
|
+
},
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"web3": "^1.10.2"
|
|
48
|
+
}
|
|
49
|
+
}
|