@defisaver/positions-sdk 0.0.183-dev-allocator-7 → 0.0.183-dev-allocator-9

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