@defisaver/positions-sdk 0.0.183-dev-allocator-6 → 0.0.183-dev-allocator-8

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.
@@ -16,12 +16,40 @@ export declare const getApyAfterValuesEstimation: (selectedMarket: MorphoBlueMar
16
16
  borrowRate: string;
17
17
  supplyRate: string;
18
18
  }>;
19
+ /**
20
+ * Get reallocatable liquidity to a given market and target borrow utilization
21
+ * @param marketId - Unique key of the market liquidity is reallocated to
22
+ * @param network - The network number
23
+ * @returns The reallocatable liquidity and target borrow utilization
24
+ */
19
25
  export declare const getReallocatableLiquidity: (marketId: string, network?: NetworkNumber) => Promise<{
20
26
  reallocatableLiquidity: string;
21
27
  targetBorrowUtilization: string;
22
28
  }>;
29
+ /**
30
+ * Get liquidity to allocate for a given amount to borrow.
31
+ * First, the function will try to calculate the amount of liquidity to allocate to be able to
32
+ * hit the target utilization. If it is not possible to allocate enough liquidity to hit the
33
+ * target utilization, the function will allocate the amount of liquidity needed to be able to
34
+ * borrow the selected amount.
35
+ * @param amountToBorrow - The amount to borrow
36
+ * @param totalBorrow - The total amount borrowed from market
37
+ * @param totalSupply - The total amount supplied to market
38
+ * @param targetBorrowUtilization - The target borrow utilization of market
39
+ * @param reallocatableLiquidityAssets - The amount of liquidity that can be reallocated from other markets
40
+ * @returns The amount of liquidity to allocate
41
+ */
23
42
  export declare const getLiquidityToAllocate: (amountToBorrow: string, totalBorrow: string, totalSupply: string, targetBorrowUtilization: string, reallocatableLiquidityAssets: string) => string;
24
- export declare const getReallocation: (marketId: string, amountToBorrow: string, network?: NetworkNumber) => Promise<{
43
+ /**
44
+ * Get the vaults and withdrawals needed to reallocate liquidity for a given amount to borrow.
45
+ * Amount to be reallocated is calculated in `getLiquidityToAllocate`
46
+ * @param market - The market data
47
+ * @param assetsData - The assets data
48
+ * @param amountToBorrow - Amount being borrowed (not the amount being reallocated)
49
+ * @param network - The network number
50
+ * @returns The vaults and withdrawals needed to reallocate liquidity
51
+ */
52
+ export declare const getReallocation: (market: MorphoBlueMarketData, assetsData: MorphoBlueAssetsData, amountToBorrow: string, network?: NetworkNumber) => Promise<{
25
53
  vaults: string[];
26
54
  withdrawals: (string | string[])[][][];
27
55
  }>;
@@ -165,6 +165,12 @@ const MARKET_QUERY = `
165
165
  }
166
166
  }
167
167
  `;
168
+ /**
169
+ * Get reallocatable liquidity to a given market and target borrow utilization
170
+ * @param marketId - Unique key of the market liquidity is reallocated to
171
+ * @param network - The network number
172
+ * @returns The reallocatable liquidity and target borrow utilization
173
+ */
168
174
  const getReallocatableLiquidity = (marketId, network = common_1.NetworkNumber.Eth) => __awaiter(void 0, void 0, void 0, function* () {
169
175
  var _a;
170
176
  const response = yield fetch(API_URL, {
@@ -182,6 +188,19 @@ const getReallocatableLiquidity = (marketId, network = common_1.NetworkNumber.Et
182
188
  return { reallocatableLiquidity: marketData.reallocatableLiquidityAssets, targetBorrowUtilization: marketData.targetBorrowUtilization };
183
189
  });
184
190
  exports.getReallocatableLiquidity = getReallocatableLiquidity;
191
+ /**
192
+ * Get liquidity to allocate for a given amount to borrow.
193
+ * First, the function will try to calculate the amount of liquidity to allocate to be able to
194
+ * hit the target utilization. If it is not possible to allocate enough liquidity to hit the
195
+ * target utilization, the function will allocate the amount of liquidity needed to be able to
196
+ * borrow the selected amount.
197
+ * @param amountToBorrow - The amount to borrow
198
+ * @param totalBorrow - The total amount borrowed from market
199
+ * @param totalSupply - The total amount supplied to market
200
+ * @param targetBorrowUtilization - The target borrow utilization of market
201
+ * @param reallocatableLiquidityAssets - The amount of liquidity that can be reallocated from other markets
202
+ * @returns The amount of liquidity to allocate
203
+ */
185
204
  const getLiquidityToAllocate = (amountToBorrow, totalBorrow, totalSupply, targetBorrowUtilization, reallocatableLiquidityAssets) => {
186
205
  const newTotalBorrowAssets = new decimal_js_1.default(totalBorrow).add(amountToBorrow).toString();
187
206
  const leftToBorrow = new decimal_js_1.default(totalSupply).sub(totalBorrow).toString();
@@ -196,8 +215,18 @@ const getLiquidityToAllocate = (amountToBorrow, totalBorrow, totalSupply, target
196
215
  return liquidityToAllocate;
197
216
  };
198
217
  exports.getLiquidityToAllocate = getLiquidityToAllocate;
199
- const getReallocation = (marketId, amountToBorrow, network = common_1.NetworkNumber.Eth) => __awaiter(void 0, void 0, void 0, function* () {
218
+ /**
219
+ * Get the vaults and withdrawals needed to reallocate liquidity for a given amount to borrow.
220
+ * Amount to be reallocated is calculated in `getLiquidityToAllocate`
221
+ * @param market - The market data
222
+ * @param assetsData - The assets data
223
+ * @param amountToBorrow - Amount being borrowed (not the amount being reallocated)
224
+ * @param network - The network number
225
+ * @returns The vaults and withdrawals needed to reallocate liquidity
226
+ */
227
+ const getReallocation = (market, assetsData, amountToBorrow, network = common_1.NetworkNumber.Eth) => __awaiter(void 0, void 0, void 0, function* () {
200
228
  var _b, _c, _d;
229
+ const { marketId, loanToken } = market;
201
230
  const response = yield fetch(API_URL, {
202
231
  method: 'POST',
203
232
  headers: { 'Content-Type': 'application/json' },
@@ -210,12 +239,16 @@ const getReallocation = (marketId, amountToBorrow, network = common_1.NetworkNum
210
239
  const marketData = (_b = data === null || data === void 0 ? void 0 : data.data) === null || _b === void 0 ? void 0 : _b.marketByUniqueKey;
211
240
  if (!marketData)
212
241
  throw new Error('Market data not found');
213
- const newTotalBorrowAssets = new decimal_js_1.default(marketData.state.borrowAssets).add(amountToBorrow).toString();
214
- const newUtil = new decimal_js_1.default(newTotalBorrowAssets).div(marketData.state.supplyAssets).toString();
242
+ const loanAssetInfo = (0, tokens_1.getAssetInfoByAddress)(loanToken, network);
243
+ const { totalBorrow, totalSupply } = assetsData[loanAssetInfo.symbol] || { totalBorrow: '0', totalSupply: '0' };
244
+ const totalBorrowWei = (0, tokens_1.assetAmountInWei)(totalBorrow, loanToken);
245
+ const totalSupplyWei = (0, tokens_1.assetAmountInWei)(totalSupply, loanToken);
246
+ const newTotalBorrowAssets = new decimal_js_1.default(totalBorrowWei).add(amountToBorrow).toString();
247
+ const newUtil = new decimal_js_1.default(newTotalBorrowAssets).div(totalSupplyWei).toString();
215
248
  const newUtilScaled = new decimal_js_1.default(newUtil).mul(1e18).toString();
216
249
  if (new decimal_js_1.default(newUtilScaled).lt(marketData.targetBorrowUtilization))
217
250
  return { vaults: [], withdrawals: [] };
218
- const liquidityToAllocate = (0, exports.getLiquidityToAllocate)(amountToBorrow, marketData.state.borrowAssets, marketData.state.supplyAssets, marketData.targetBorrowUtilization, marketData.reallocatableLiquidityAssets);
251
+ const liquidityToAllocate = (0, exports.getLiquidityToAllocate)(amountToBorrow, totalBorrowWei, totalSupplyWei, marketData.targetBorrowUtilization, marketData.reallocatableLiquidityAssets);
219
252
  const vaultTotalAssets = marketData.publicAllocatorSharedLiquidity.reduce((acc, item) => {
220
253
  const vaultAddress = item.vault.address;
221
254
  acc[vaultAddress] = new decimal_js_1.default(acc[vaultAddress] || '0').add(item.assets).toString();
@@ -16,12 +16,40 @@ export declare const getApyAfterValuesEstimation: (selectedMarket: MorphoBlueMar
16
16
  borrowRate: string;
17
17
  supplyRate: string;
18
18
  }>;
19
+ /**
20
+ * Get reallocatable liquidity to a given market and target borrow utilization
21
+ * @param marketId - Unique key of the market liquidity is reallocated to
22
+ * @param network - The network number
23
+ * @returns The reallocatable liquidity and target borrow utilization
24
+ */
19
25
  export declare const getReallocatableLiquidity: (marketId: string, network?: NetworkNumber) => Promise<{
20
26
  reallocatableLiquidity: string;
21
27
  targetBorrowUtilization: string;
22
28
  }>;
29
+ /**
30
+ * Get liquidity to allocate for a given amount to borrow.
31
+ * First, the function will try to calculate the amount of liquidity to allocate to be able to
32
+ * hit the target utilization. If it is not possible to allocate enough liquidity to hit the
33
+ * target utilization, the function will allocate the amount of liquidity needed to be able to
34
+ * borrow the selected amount.
35
+ * @param amountToBorrow - The amount to borrow
36
+ * @param totalBorrow - The total amount borrowed from market
37
+ * @param totalSupply - The total amount supplied to market
38
+ * @param targetBorrowUtilization - The target borrow utilization of market
39
+ * @param reallocatableLiquidityAssets - The amount of liquidity that can be reallocated from other markets
40
+ * @returns The amount of liquidity to allocate
41
+ */
23
42
  export declare const getLiquidityToAllocate: (amountToBorrow: string, totalBorrow: string, totalSupply: string, targetBorrowUtilization: string, reallocatableLiquidityAssets: string) => string;
24
- export declare const getReallocation: (marketId: string, amountToBorrow: string, network?: NetworkNumber) => Promise<{
43
+ /**
44
+ * Get the vaults and withdrawals needed to reallocate liquidity for a given amount to borrow.
45
+ * Amount to be reallocated is calculated in `getLiquidityToAllocate`
46
+ * @param market - The market data
47
+ * @param assetsData - The assets data
48
+ * @param amountToBorrow - Amount being borrowed (not the amount being reallocated)
49
+ * @param network - The network number
50
+ * @returns The vaults and withdrawals needed to reallocate liquidity
51
+ */
52
+ export declare const getReallocation: (market: MorphoBlueMarketData, assetsData: MorphoBlueAssetsData, amountToBorrow: string, network?: NetworkNumber) => Promise<{
25
53
  vaults: string[];
26
54
  withdrawals: (string | string[])[][][];
27
55
  }>;
@@ -8,7 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import Dec from 'decimal.js';
11
- import { assetAmountInWei } from '@defisaver/tokens';
11
+ import { assetAmountInWei, getAssetInfoByAddress } from '@defisaver/tokens';
12
12
  import { calcLeverageLiqPrice, getAssetsTotal, isLeveragedPos } from '../../moneymarket';
13
13
  import { calculateNetApy } from '../../staking';
14
14
  import { NetworkNumber } from '../../types/common';
@@ -155,6 +155,12 @@ const MARKET_QUERY = `
155
155
  }
156
156
  }
157
157
  `;
158
+ /**
159
+ * Get reallocatable liquidity to a given market and target borrow utilization
160
+ * @param marketId - Unique key of the market liquidity is reallocated to
161
+ * @param network - The network number
162
+ * @returns The reallocatable liquidity and target borrow utilization
163
+ */
158
164
  export const getReallocatableLiquidity = (marketId, network = NetworkNumber.Eth) => __awaiter(void 0, void 0, void 0, function* () {
159
165
  var _a;
160
166
  const response = yield fetch(API_URL, {
@@ -171,6 +177,19 @@ export const getReallocatableLiquidity = (marketId, network = NetworkNumber.Eth)
171
177
  throw new Error('Market data not found');
172
178
  return { reallocatableLiquidity: marketData.reallocatableLiquidityAssets, targetBorrowUtilization: marketData.targetBorrowUtilization };
173
179
  });
180
+ /**
181
+ * Get liquidity to allocate for a given amount to borrow.
182
+ * First, the function will try to calculate the amount of liquidity to allocate to be able to
183
+ * hit the target utilization. If it is not possible to allocate enough liquidity to hit the
184
+ * target utilization, the function will allocate the amount of liquidity needed to be able to
185
+ * borrow the selected amount.
186
+ * @param amountToBorrow - The amount to borrow
187
+ * @param totalBorrow - The total amount borrowed from market
188
+ * @param totalSupply - The total amount supplied to market
189
+ * @param targetBorrowUtilization - The target borrow utilization of market
190
+ * @param reallocatableLiquidityAssets - The amount of liquidity that can be reallocated from other markets
191
+ * @returns The amount of liquidity to allocate
192
+ */
174
193
  export const getLiquidityToAllocate = (amountToBorrow, totalBorrow, totalSupply, targetBorrowUtilization, reallocatableLiquidityAssets) => {
175
194
  const newTotalBorrowAssets = new Dec(totalBorrow).add(amountToBorrow).toString();
176
195
  const leftToBorrow = new Dec(totalSupply).sub(totalBorrow).toString();
@@ -184,8 +203,18 @@ export const getLiquidityToAllocate = (amountToBorrow, totalBorrow, totalSupply,
184
203
  }
185
204
  return liquidityToAllocate;
186
205
  };
187
- export const getReallocation = (marketId, amountToBorrow, network = NetworkNumber.Eth) => __awaiter(void 0, void 0, void 0, function* () {
206
+ /**
207
+ * Get the vaults and withdrawals needed to reallocate liquidity for a given amount to borrow.
208
+ * Amount to be reallocated is calculated in `getLiquidityToAllocate`
209
+ * @param market - The market data
210
+ * @param assetsData - The assets data
211
+ * @param amountToBorrow - Amount being borrowed (not the amount being reallocated)
212
+ * @param network - The network number
213
+ * @returns The vaults and withdrawals needed to reallocate liquidity
214
+ */
215
+ export const getReallocation = (market, assetsData, amountToBorrow, network = NetworkNumber.Eth) => __awaiter(void 0, void 0, void 0, function* () {
188
216
  var _b, _c, _d;
217
+ const { marketId, loanToken } = market;
189
218
  const response = yield fetch(API_URL, {
190
219
  method: 'POST',
191
220
  headers: { 'Content-Type': 'application/json' },
@@ -198,12 +227,16 @@ export const getReallocation = (marketId, amountToBorrow, network = NetworkNumbe
198
227
  const marketData = (_b = data === null || data === void 0 ? void 0 : data.data) === null || _b === void 0 ? void 0 : _b.marketByUniqueKey;
199
228
  if (!marketData)
200
229
  throw new Error('Market data not found');
201
- const newTotalBorrowAssets = new Dec(marketData.state.borrowAssets).add(amountToBorrow).toString();
202
- const newUtil = new Dec(newTotalBorrowAssets).div(marketData.state.supplyAssets).toString();
230
+ const loanAssetInfo = getAssetInfoByAddress(loanToken, network);
231
+ const { totalBorrow, totalSupply } = assetsData[loanAssetInfo.symbol] || { totalBorrow: '0', totalSupply: '0' };
232
+ const totalBorrowWei = assetAmountInWei(totalBorrow, loanToken);
233
+ const totalSupplyWei = assetAmountInWei(totalSupply, loanToken);
234
+ const newTotalBorrowAssets = new Dec(totalBorrowWei).add(amountToBorrow).toString();
235
+ const newUtil = new Dec(newTotalBorrowAssets).div(totalSupplyWei).toString();
203
236
  const newUtilScaled = new Dec(newUtil).mul(1e18).toString();
204
237
  if (new Dec(newUtilScaled).lt(marketData.targetBorrowUtilization))
205
238
  return { vaults: [], withdrawals: [] };
206
- const liquidityToAllocate = getLiquidityToAllocate(amountToBorrow, marketData.state.borrowAssets, marketData.state.supplyAssets, marketData.targetBorrowUtilization, marketData.reallocatableLiquidityAssets);
239
+ const liquidityToAllocate = getLiquidityToAllocate(amountToBorrow, totalBorrowWei, totalSupplyWei, marketData.targetBorrowUtilization, marketData.reallocatableLiquidityAssets);
207
240
  const vaultTotalAssets = marketData.publicAllocatorSharedLiquidity.reduce((acc, item) => {
208
241
  const vaultAddress = item.vault.address;
209
242
  acc[vaultAddress] = new Dec(acc[vaultAddress] || '0').add(item.assets).toString();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defisaver/positions-sdk",
3
- "version": "0.0.183-dev-allocator-6",
3
+ "version": "0.0.183-dev-allocator-8",
4
4
  "description": "",
5
5
  "main": "./cjs/index.js",
6
6
  "module": "./esm/index.js",
@@ -176,6 +176,12 @@ const MARKET_QUERY = `
176
176
  }
177
177
  `;
178
178
 
179
+ /**
180
+ * Get reallocatable liquidity to a given market and target borrow utilization
181
+ * @param marketId - Unique key of the market liquidity is reallocated to
182
+ * @param network - The network number
183
+ * @returns The reallocatable liquidity and target borrow utilization
184
+ */
179
185
  export const getReallocatableLiquidity = async (marketId: string, network: NetworkNumber = NetworkNumber.Eth): Promise<{ reallocatableLiquidity: string, targetBorrowUtilization: string }> => {
180
186
  const response = await fetch(API_URL, {
181
187
  method: 'POST',
@@ -194,6 +200,19 @@ export const getReallocatableLiquidity = async (marketId: string, network: Netwo
194
200
  return { reallocatableLiquidity: marketData.reallocatableLiquidityAssets, targetBorrowUtilization: marketData.targetBorrowUtilization };
195
201
  };
196
202
 
203
+ /**
204
+ * Get liquidity to allocate for a given amount to borrow.
205
+ * First, the function will try to calculate the amount of liquidity to allocate to be able to
206
+ * hit the target utilization. If it is not possible to allocate enough liquidity to hit the
207
+ * target utilization, the function will allocate the amount of liquidity needed to be able to
208
+ * borrow the selected amount.
209
+ * @param amountToBorrow - The amount to borrow
210
+ * @param totalBorrow - The total amount borrowed from market
211
+ * @param totalSupply - The total amount supplied to market
212
+ * @param targetBorrowUtilization - The target borrow utilization of market
213
+ * @param reallocatableLiquidityAssets - The amount of liquidity that can be reallocated from other markets
214
+ * @returns The amount of liquidity to allocate
215
+ */
197
216
  export const getLiquidityToAllocate = (amountToBorrow: string, totalBorrow: string, totalSupply: string, targetBorrowUtilization: string, reallocatableLiquidityAssets: string) => {
198
217
  const newTotalBorrowAssets = new Dec(totalBorrow).add(amountToBorrow).toString();
199
218
  const leftToBorrow = new Dec(totalSupply).sub(totalBorrow).toString();
@@ -209,7 +228,17 @@ export const getLiquidityToAllocate = (amountToBorrow: string, totalBorrow: stri
209
228
  return liquidityToAllocate;
210
229
  };
211
230
 
212
- export const getReallocation = async (marketId: string, amountToBorrow: string, network: NetworkNumber = NetworkNumber.Eth) => {
231
+ /**
232
+ * Get the vaults and withdrawals needed to reallocate liquidity for a given amount to borrow.
233
+ * Amount to be reallocated is calculated in `getLiquidityToAllocate`
234
+ * @param market - The market data
235
+ * @param assetsData - The assets data
236
+ * @param amountToBorrow - Amount being borrowed (not the amount being reallocated)
237
+ * @param network - The network number
238
+ * @returns The vaults and withdrawals needed to reallocate liquidity
239
+ */
240
+ export const getReallocation = async (market: MorphoBlueMarketData, assetsData: MorphoBlueAssetsData, amountToBorrow: string, network: NetworkNumber = NetworkNumber.Eth) => {
241
+ const { marketId, loanToken } = market;
213
242
  const response = await fetch(API_URL, {
214
243
  method: 'POST',
215
244
  headers: { 'Content-Type': 'application/json' },
@@ -224,14 +253,19 @@ export const getReallocation = async (marketId: string, amountToBorrow: string,
224
253
 
225
254
  if (!marketData) throw new Error('Market data not found');
226
255
 
227
- const newTotalBorrowAssets = new Dec(marketData.state.borrowAssets).add(amountToBorrow).toString();
256
+ const loanAssetInfo = getAssetInfoByAddress(loanToken, network);
257
+ const { totalBorrow, totalSupply } = assetsData[loanAssetInfo.symbol] || { totalBorrow: '0', totalSupply: '0' };
258
+ const totalBorrowWei = assetAmountInWei(totalBorrow!, loanToken);
259
+ const totalSupplyWei = assetAmountInWei(totalSupply!, loanToken);
228
260
 
229
- const newUtil = new Dec(newTotalBorrowAssets).div(marketData.state.supplyAssets).toString();
261
+ const newTotalBorrowAssets = new Dec(totalBorrowWei).add(amountToBorrow).toString();
262
+
263
+ const newUtil = new Dec(newTotalBorrowAssets).div(totalSupplyWei).toString();
230
264
  const newUtilScaled = new Dec(newUtil).mul(1e18).toString();
231
265
 
232
266
  if (new Dec(newUtilScaled).lt(marketData.targetBorrowUtilization)) return { vaults: [], withdrawals: [] };
233
267
 
234
- const liquidityToAllocate = getLiquidityToAllocate(amountToBorrow, marketData.state.borrowAssets, marketData.state.supplyAssets, marketData.targetBorrowUtilization, marketData.reallocatableLiquidityAssets);
268
+ const liquidityToAllocate = getLiquidityToAllocate(amountToBorrow, totalBorrowWei, totalSupplyWei, marketData.targetBorrowUtilization, marketData.reallocatableLiquidityAssets);
235
269
 
236
270
  const vaultTotalAssets = marketData.publicAllocatorSharedLiquidity.reduce(
237
271
  (acc: Record<string, string>, item: MorphoBluePublicAllocatorItem) => {
@@ -288,4 +322,4 @@ export const getReallocation = async (marketId: string, amountToBorrow: string,
288
322
  vaults,
289
323
  withdrawals,
290
324
  };
291
- };
325
+ };