@defisaver/positions-sdk 0.0.183-dev-allocator → 0.0.183-dev-allocator-2

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.
@@ -13,7 +13,7 @@ export declare const getApyAfterValuesEstimation: (selectedMarket: MorphoBlueMar
13
13
  supplyRate: string;
14
14
  }>;
15
15
  export declare const getReallocatableLiquidity: (marketId: string, network?: NetworkNumber) => Promise<string>;
16
- export declare const getReallocation: (marketId: string, liquidityToAllocate: string, network?: NetworkNumber) => Promise<{
16
+ export declare const getReallocation: (marketId: string, amountToBorrow: string, network?: NetworkNumber) => Promise<{
17
17
  vaults: string[];
18
18
  withdrawals: [string[], string][][];
19
19
  }>;
@@ -117,6 +117,7 @@ const MARKET_QUERY = `
117
117
  query MarketByUniqueKey($uniqueKey: String!, $chainId: Int!) {
118
118
  marketByUniqueKey(uniqueKey: $uniqueKey, chainId: $chainId) {
119
119
  reallocatableLiquidityAssets
120
+ targetBorrowUtilization
120
121
  loanAsset {
121
122
  address
122
123
  decimals
@@ -124,6 +125,8 @@ const MARKET_QUERY = `
124
125
  }
125
126
  state {
126
127
  liquidityAssets
128
+ borrowAssets
129
+ supplyAssets
127
130
  }
128
131
  publicAllocatorSharedLiquidity {
129
132
  assets
@@ -177,7 +180,7 @@ const getReallocatableLiquidity = (marketId, network = common_1.NetworkNumber.Et
177
180
  return marketData.reallocatableLiquidityAssets;
178
181
  });
179
182
  exports.getReallocatableLiquidity = getReallocatableLiquidity;
180
- const getReallocation = (marketId, liquidityToAllocate, network = common_1.NetworkNumber.Eth) => __awaiter(void 0, void 0, void 0, function* () {
183
+ const getReallocation = (marketId, amountToBorrow, network = common_1.NetworkNumber.Eth) => __awaiter(void 0, void 0, void 0, function* () {
181
184
  var _b, _c, _d;
182
185
  const response = yield fetch(API_URL, {
183
186
  method: 'POST',
@@ -191,8 +194,20 @@ const getReallocation = (marketId, liquidityToAllocate, network = common_1.Netwo
191
194
  const marketData = (_b = data === null || data === void 0 ? void 0 : data.data) === null || _b === void 0 ? void 0 : _b.marketByUniqueKey;
192
195
  if (!marketData)
193
196
  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');
197
+ const newTotalBorrowAssets = new decimal_js_1.default(marketData.state.borrowAssets).add(amountToBorrow).toString();
198
+ const leftToBorrow = new decimal_js_1.default(marketData.state.supplyAssets).sub(marketData.state.borrowAssets).toString();
199
+ const newUtil = new decimal_js_1.default(newTotalBorrowAssets).div(marketData.state.supplyAssets).toString();
200
+ const newUtilScaled = new decimal_js_1.default(newUtil).mul(1e18).toString();
201
+ if (new decimal_js_1.default(newUtilScaled).lt(marketData.targetBorrowUtilization))
202
+ return { vaults: [], withdrawals: [] };
203
+ let liquidityToAllocate = new decimal_js_1.default(newTotalBorrowAssets).div(marketData.targetBorrowUtilization).mul(1e18).sub(marketData.state.supplyAssets)
204
+ .toFixed(0)
205
+ .toString();
206
+ if (new decimal_js_1.default(marketData.reallocatableLiquidityAssets).lt(liquidityToAllocate)) {
207
+ liquidityToAllocate = new decimal_js_1.default(amountToBorrow).sub(leftToBorrow).toString();
208
+ if (new decimal_js_1.default(marketData.reallocatableLiquidityAssets).lt(liquidityToAllocate))
209
+ throw new Error('Not enough liquidity available to allocate');
210
+ }
196
211
  const vaultTotalAssets = marketData.publicAllocatorSharedLiquidity.reduce((acc, item) => {
197
212
  const vaultAddress = item.vault.address;
198
213
  acc[vaultAddress] = new decimal_js_1.default(acc[vaultAddress] || '0').add(item.assets).toString();
@@ -158,7 +158,13 @@ export interface MorphoBluePublicAllocatorItem {
158
158
  assets: string;
159
159
  allocationMarket: MorphoBlueAllocationMarket;
160
160
  }
161
+ export interface MorphoBlueAllocatorMarketState {
162
+ borrowAssets: string;
163
+ supplyAssets: string;
164
+ }
161
165
  export interface MorphoBlueRealloactionMarketData {
162
166
  reallocatableLiquidityAssets: string;
167
+ targetBorrowUtilization: string;
163
168
  publicAllocatorSharedLiquidity: MorphoBluePublicAllocatorItem[];
169
+ state: MorphoBlueAllocatorMarketState;
164
170
  }
@@ -13,7 +13,7 @@ export declare const getApyAfterValuesEstimation: (selectedMarket: MorphoBlueMar
13
13
  supplyRate: string;
14
14
  }>;
15
15
  export declare const getReallocatableLiquidity: (marketId: string, network?: NetworkNumber) => Promise<string>;
16
- export declare const getReallocation: (marketId: string, liquidityToAllocate: string, network?: NetworkNumber) => Promise<{
16
+ export declare const getReallocation: (marketId: string, amountToBorrow: string, network?: NetworkNumber) => Promise<{
17
17
  vaults: string[];
18
18
  withdrawals: [string[], string][][];
19
19
  }>;
@@ -107,6 +107,7 @@ const MARKET_QUERY = `
107
107
  query MarketByUniqueKey($uniqueKey: String!, $chainId: Int!) {
108
108
  marketByUniqueKey(uniqueKey: $uniqueKey, chainId: $chainId) {
109
109
  reallocatableLiquidityAssets
110
+ targetBorrowUtilization
110
111
  loanAsset {
111
112
  address
112
113
  decimals
@@ -114,6 +115,8 @@ const MARKET_QUERY = `
114
115
  }
115
116
  state {
116
117
  liquidityAssets
118
+ borrowAssets
119
+ supplyAssets
117
120
  }
118
121
  publicAllocatorSharedLiquidity {
119
122
  assets
@@ -166,7 +169,7 @@ export const getReallocatableLiquidity = (marketId, network = NetworkNumber.Eth)
166
169
  throw new Error('Market data not found');
167
170
  return marketData.reallocatableLiquidityAssets;
168
171
  });
169
- export const getReallocation = (marketId, liquidityToAllocate, network = NetworkNumber.Eth) => __awaiter(void 0, void 0, void 0, function* () {
172
+ export const getReallocation = (marketId, amountToBorrow, network = NetworkNumber.Eth) => __awaiter(void 0, void 0, void 0, function* () {
170
173
  var _b, _c, _d;
171
174
  const response = yield fetch(API_URL, {
172
175
  method: 'POST',
@@ -180,8 +183,20 @@ export const getReallocation = (marketId, liquidityToAllocate, network = Network
180
183
  const marketData = (_b = data === null || data === void 0 ? void 0 : data.data) === null || _b === void 0 ? void 0 : _b.marketByUniqueKey;
181
184
  if (!marketData)
182
185
  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');
186
+ const newTotalBorrowAssets = new Dec(marketData.state.borrowAssets).add(amountToBorrow).toString();
187
+ const leftToBorrow = new Dec(marketData.state.supplyAssets).sub(marketData.state.borrowAssets).toString();
188
+ const newUtil = new Dec(newTotalBorrowAssets).div(marketData.state.supplyAssets).toString();
189
+ const newUtilScaled = new Dec(newUtil).mul(1e18).toString();
190
+ if (new Dec(newUtilScaled).lt(marketData.targetBorrowUtilization))
191
+ return { vaults: [], withdrawals: [] };
192
+ let liquidityToAllocate = new Dec(newTotalBorrowAssets).div(marketData.targetBorrowUtilization).mul(1e18).sub(marketData.state.supplyAssets)
193
+ .toFixed(0)
194
+ .toString();
195
+ if (new Dec(marketData.reallocatableLiquidityAssets).lt(liquidityToAllocate)) {
196
+ liquidityToAllocate = new Dec(amountToBorrow).sub(leftToBorrow).toString();
197
+ if (new Dec(marketData.reallocatableLiquidityAssets).lt(liquidityToAllocate))
198
+ throw new Error('Not enough liquidity available to allocate');
199
+ }
185
200
  const vaultTotalAssets = marketData.publicAllocatorSharedLiquidity.reduce((acc, item) => {
186
201
  const vaultAddress = item.vault.address;
187
202
  acc[vaultAddress] = new Dec(acc[vaultAddress] || '0').add(item.assets).toString();
@@ -158,7 +158,13 @@ export interface MorphoBluePublicAllocatorItem {
158
158
  assets: string;
159
159
  allocationMarket: MorphoBlueAllocationMarket;
160
160
  }
161
+ export interface MorphoBlueAllocatorMarketState {
162
+ borrowAssets: string;
163
+ supplyAssets: string;
164
+ }
161
165
  export interface MorphoBlueRealloactionMarketData {
162
166
  reallocatableLiquidityAssets: string;
167
+ targetBorrowUtilization: string;
163
168
  publicAllocatorSharedLiquidity: MorphoBluePublicAllocatorItem[];
169
+ state: MorphoBlueAllocatorMarketState;
164
170
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defisaver/positions-sdk",
3
- "version": "0.0.183-dev-allocator",
3
+ "version": "0.0.183-dev-allocator-2",
4
4
  "description": "",
5
5
  "main": "./cjs/index.js",
6
6
  "module": "./esm/index.js",
@@ -123,6 +123,7 @@ const MARKET_QUERY = `
123
123
  query MarketByUniqueKey($uniqueKey: String!, $chainId: Int!) {
124
124
  marketByUniqueKey(uniqueKey: $uniqueKey, chainId: $chainId) {
125
125
  reallocatableLiquidityAssets
126
+ targetBorrowUtilization
126
127
  loanAsset {
127
128
  address
128
129
  decimals
@@ -130,6 +131,8 @@ const MARKET_QUERY = `
130
131
  }
131
132
  state {
132
133
  liquidityAssets
134
+ borrowAssets
135
+ supplyAssets
133
136
  }
134
137
  publicAllocatorSharedLiquidity {
135
138
  assets
@@ -185,7 +188,7 @@ export const getReallocatableLiquidity = async (marketId: string, network: Netwo
185
188
  return marketData.reallocatableLiquidityAssets;
186
189
  };
187
190
 
188
- export const getReallocation = async (marketId: string, liquidityToAllocate: string, network: NetworkNumber = NetworkNumber.Eth) => {
191
+ export const getReallocation = async (marketId: string, amountToBorrow: string, network: NetworkNumber = NetworkNumber.Eth) => {
189
192
  const response = await fetch(API_URL, {
190
193
  method: 'POST',
191
194
  headers: { 'Content-Type': 'application/json' },
@@ -200,7 +203,22 @@ export const getReallocation = async (marketId: string, liquidityToAllocate: str
200
203
 
201
204
  if (!marketData) throw new Error('Market data not found');
202
205
 
203
- if (new Dec(marketData.reallocatableLiquidityAssets).lt(liquidityToAllocate)) throw new Error('Not enough liquidity available to allocate');
206
+ const newTotalBorrowAssets = new Dec(marketData.state.borrowAssets).add(amountToBorrow).toString();
207
+ const leftToBorrow = new Dec(marketData.state.supplyAssets).sub(marketData.state.borrowAssets).toString();
208
+
209
+ const newUtil = new Dec(newTotalBorrowAssets).div(marketData.state.supplyAssets).toString();
210
+ const newUtilScaled = new Dec(newUtil).mul(1e18).toString();
211
+
212
+ if (new Dec(newUtilScaled).lt(marketData.targetBorrowUtilization)) return { vaults: [], withdrawals: [] };
213
+
214
+ let liquidityToAllocate = new Dec(newTotalBorrowAssets).div(marketData.targetBorrowUtilization).mul(1e18).sub(marketData.state.supplyAssets)
215
+ .toFixed(0)
216
+ .toString();
217
+
218
+ if (new Dec(marketData.reallocatableLiquidityAssets).lt(liquidityToAllocate)) {
219
+ liquidityToAllocate = new Dec(amountToBorrow).sub(leftToBorrow).toString();
220
+ if (new Dec(marketData.reallocatableLiquidityAssets).lt(liquidityToAllocate)) throw new Error('Not enough liquidity available to allocate');
221
+ }
204
222
 
205
223
  const vaultTotalAssets = marketData.publicAllocatorSharedLiquidity.reduce(
206
224
  (acc: Record<string, string>, item: MorphoBluePublicAllocatorItem) => {
@@ -171,7 +171,14 @@ export interface MorphoBluePublicAllocatorItem {
171
171
  allocationMarket: MorphoBlueAllocationMarket,
172
172
  }
173
173
 
174
+ export interface MorphoBlueAllocatorMarketState {
175
+ borrowAssets: string,
176
+ supplyAssets: string,
177
+ }
178
+
174
179
  export interface MorphoBlueRealloactionMarketData {
175
180
  reallocatableLiquidityAssets: string,
181
+ targetBorrowUtilization: string,
176
182
  publicAllocatorSharedLiquidity: MorphoBluePublicAllocatorItem[],
183
+ state: MorphoBlueAllocatorMarketState,
177
184
  }