@defisaver/positions-sdk 0.0.183-dev-allocator → 0.0.183-dev-allocator-3
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/cjs/helpers/morphoBlueHelpers/index.d.ts +2 -2
- package/cjs/helpers/morphoBlueHelpers/index.js +20 -4
- package/cjs/types/morphoBlue.d.ts +7 -0
- package/esm/helpers/morphoBlueHelpers/index.d.ts +2 -2
- package/esm/helpers/morphoBlueHelpers/index.js +20 -4
- package/esm/types/morphoBlue.d.ts +7 -0
- package/package.json +1 -1
- package/src/helpers/morphoBlueHelpers/index.ts +26 -5
- package/src/types/morphoBlue.ts +8 -0
|
@@ -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,
|
|
16
|
+
export declare const getReallocation: (marketId: string, amountToBorrow: string, network?: NetworkNumber) => Promise<{
|
|
17
17
|
vaults: string[];
|
|
18
|
-
withdrawals:
|
|
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,
|
|
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
|
-
|
|
195
|
-
|
|
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();
|
|
@@ -221,6 +236,7 @@ const getReallocation = (marketId, liquidityToAllocate, network = common_1.Netwo
|
|
|
221
236
|
item.allocationMarket.lltv,
|
|
222
237
|
],
|
|
223
238
|
amountToTake.toString(),
|
|
239
|
+
item.allocationMarket.uniqueKey,
|
|
224
240
|
];
|
|
225
241
|
if (!withdrawalsPerVault[vaultAddress]) {
|
|
226
242
|
withdrawalsPerVault[vaultAddress] = [];
|
|
@@ -229,7 +245,7 @@ const getReallocation = (marketId, liquidityToAllocate, network = common_1.Netwo
|
|
|
229
245
|
}
|
|
230
246
|
}
|
|
231
247
|
const vaults = Object.keys(withdrawalsPerVault);
|
|
232
|
-
const withdrawals = vaults.map((vaultAddress) => withdrawalsPerVault[vaultAddress]);
|
|
248
|
+
const withdrawals = vaults.map((vaultAddress) => withdrawalsPerVault[vaultAddress].sort((a, b) => a[2].localeCompare(b[2])).map(w => [w[0], w[1]]));
|
|
233
249
|
return {
|
|
234
250
|
vaults,
|
|
235
251
|
withdrawals,
|
|
@@ -152,13 +152,20 @@ export interface MorphoBlueAllocationMarket {
|
|
|
152
152
|
};
|
|
153
153
|
irmAddress: string;
|
|
154
154
|
lltv: string;
|
|
155
|
+
uniqueKey: string;
|
|
155
156
|
}
|
|
156
157
|
export interface MorphoBluePublicAllocatorItem {
|
|
157
158
|
vault: MorphoBlueVault;
|
|
158
159
|
assets: string;
|
|
159
160
|
allocationMarket: MorphoBlueAllocationMarket;
|
|
160
161
|
}
|
|
162
|
+
export interface MorphoBlueAllocatorMarketState {
|
|
163
|
+
borrowAssets: string;
|
|
164
|
+
supplyAssets: string;
|
|
165
|
+
}
|
|
161
166
|
export interface MorphoBlueRealloactionMarketData {
|
|
162
167
|
reallocatableLiquidityAssets: string;
|
|
168
|
+
targetBorrowUtilization: string;
|
|
163
169
|
publicAllocatorSharedLiquidity: MorphoBluePublicAllocatorItem[];
|
|
170
|
+
state: MorphoBlueAllocatorMarketState;
|
|
164
171
|
}
|
|
@@ -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,
|
|
16
|
+
export declare const getReallocation: (marketId: string, amountToBorrow: string, network?: NetworkNumber) => Promise<{
|
|
17
17
|
vaults: string[];
|
|
18
|
-
withdrawals:
|
|
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,
|
|
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
|
-
|
|
184
|
-
|
|
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();
|
|
@@ -210,6 +225,7 @@ export const getReallocation = (marketId, liquidityToAllocate, network = Network
|
|
|
210
225
|
item.allocationMarket.lltv,
|
|
211
226
|
],
|
|
212
227
|
amountToTake.toString(),
|
|
228
|
+
item.allocationMarket.uniqueKey,
|
|
213
229
|
];
|
|
214
230
|
if (!withdrawalsPerVault[vaultAddress]) {
|
|
215
231
|
withdrawalsPerVault[vaultAddress] = [];
|
|
@@ -218,7 +234,7 @@ export const getReallocation = (marketId, liquidityToAllocate, network = Network
|
|
|
218
234
|
}
|
|
219
235
|
}
|
|
220
236
|
const vaults = Object.keys(withdrawalsPerVault);
|
|
221
|
-
const withdrawals = vaults.map((vaultAddress) => withdrawalsPerVault[vaultAddress]);
|
|
237
|
+
const withdrawals = vaults.map((vaultAddress) => withdrawalsPerVault[vaultAddress].sort((a, b) => a[2].localeCompare(b[2])).map(w => [w[0], w[1]]));
|
|
222
238
|
return {
|
|
223
239
|
vaults,
|
|
224
240
|
withdrawals,
|
|
@@ -152,13 +152,20 @@ export interface MorphoBlueAllocationMarket {
|
|
|
152
152
|
};
|
|
153
153
|
irmAddress: string;
|
|
154
154
|
lltv: string;
|
|
155
|
+
uniqueKey: string;
|
|
155
156
|
}
|
|
156
157
|
export interface MorphoBluePublicAllocatorItem {
|
|
157
158
|
vault: MorphoBlueVault;
|
|
158
159
|
assets: string;
|
|
159
160
|
allocationMarket: MorphoBlueAllocationMarket;
|
|
160
161
|
}
|
|
162
|
+
export interface MorphoBlueAllocatorMarketState {
|
|
163
|
+
borrowAssets: string;
|
|
164
|
+
supplyAssets: string;
|
|
165
|
+
}
|
|
161
166
|
export interface MorphoBlueRealloactionMarketData {
|
|
162
167
|
reallocatableLiquidityAssets: string;
|
|
168
|
+
targetBorrowUtilization: string;
|
|
163
169
|
publicAllocatorSharedLiquidity: MorphoBluePublicAllocatorItem[];
|
|
170
|
+
state: MorphoBlueAllocatorMarketState;
|
|
164
171
|
}
|
package/package.json
CHANGED
|
@@ -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,
|
|
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
|
-
|
|
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) => {
|
|
@@ -215,7 +233,7 @@ export const getReallocation = async (marketId: string, liquidityToAllocate: str
|
|
|
215
233
|
([, a]: [string, string], [, b]: [string, string]) => new Dec(b || '0').sub(a || '0').toNumber(),
|
|
216
234
|
);
|
|
217
235
|
|
|
218
|
-
const withdrawalsPerVault: Record<string, [string[], string][]> = {};
|
|
236
|
+
const withdrawalsPerVault: Record<string, [string[], string, string][]> = {};
|
|
219
237
|
let totalReallocated = '0';
|
|
220
238
|
for (const [vaultAddress] of sortedVaults) {
|
|
221
239
|
if (new Dec(totalReallocated).gte(liquidityToAllocate)) break;
|
|
@@ -229,7 +247,7 @@ export const getReallocation = async (marketId: string, liquidityToAllocate: str
|
|
|
229
247
|
const leftToAllocate = new Dec(liquidityToAllocate).sub(totalReallocated).toString();
|
|
230
248
|
const amountToTake = new Dec(itemAmount).lt(leftToAllocate) ? itemAmount : leftToAllocate;
|
|
231
249
|
totalReallocated = new Dec(totalReallocated).add(amountToTake).toString();
|
|
232
|
-
const withdrawal: [string[], string] = [
|
|
250
|
+
const withdrawal: [string[], string, string] = [
|
|
233
251
|
[
|
|
234
252
|
item.allocationMarket.loanAsset.address,
|
|
235
253
|
item.allocationMarket.collateralAsset?.address,
|
|
@@ -238,6 +256,7 @@ export const getReallocation = async (marketId: string, liquidityToAllocate: str
|
|
|
238
256
|
item.allocationMarket.lltv,
|
|
239
257
|
],
|
|
240
258
|
amountToTake.toString(),
|
|
259
|
+
item.allocationMarket.uniqueKey,
|
|
241
260
|
];
|
|
242
261
|
if (!withdrawalsPerVault[vaultAddress]) {
|
|
243
262
|
withdrawalsPerVault[vaultAddress] = [];
|
|
@@ -248,7 +267,9 @@ export const getReallocation = async (marketId: string, liquidityToAllocate: str
|
|
|
248
267
|
|
|
249
268
|
const vaults = Object.keys(withdrawalsPerVault);
|
|
250
269
|
const withdrawals = vaults.map(
|
|
251
|
-
(vaultAddress) => withdrawalsPerVault[vaultAddress]
|
|
270
|
+
(vaultAddress) => withdrawalsPerVault[vaultAddress].sort(
|
|
271
|
+
(a, b) => a[2].localeCompare(b[2]),
|
|
272
|
+
).map(w => [w[0], w[1]]),
|
|
252
273
|
);
|
|
253
274
|
return {
|
|
254
275
|
vaults,
|
package/src/types/morphoBlue.ts
CHANGED
|
@@ -163,6 +163,7 @@ export interface MorphoBlueAllocationMarket {
|
|
|
163
163
|
oracle: { address: string },
|
|
164
164
|
irmAddress: string,
|
|
165
165
|
lltv: string,
|
|
166
|
+
uniqueKey: string,
|
|
166
167
|
}
|
|
167
168
|
|
|
168
169
|
export interface MorphoBluePublicAllocatorItem {
|
|
@@ -171,7 +172,14 @@ export interface MorphoBluePublicAllocatorItem {
|
|
|
171
172
|
allocationMarket: MorphoBlueAllocationMarket,
|
|
172
173
|
}
|
|
173
174
|
|
|
175
|
+
export interface MorphoBlueAllocatorMarketState {
|
|
176
|
+
borrowAssets: string,
|
|
177
|
+
supplyAssets: string,
|
|
178
|
+
}
|
|
179
|
+
|
|
174
180
|
export interface MorphoBlueRealloactionMarketData {
|
|
175
181
|
reallocatableLiquidityAssets: string,
|
|
182
|
+
targetBorrowUtilization: string,
|
|
176
183
|
publicAllocatorSharedLiquidity: MorphoBluePublicAllocatorItem[],
|
|
184
|
+
state: MorphoBlueAllocatorMarketState,
|
|
177
185
|
}
|