@morpho-org/bundler-sdk-viem 3.0.0 → 3.0.1
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/lib/ActionBundle.js +9 -4
- package/lib/BundlerAction.js +230 -227
- package/lib/abis.js +80 -77
- package/lib/actions.js +75 -70
- package/lib/bundle.js +10 -6
- package/lib/errors.js +5 -2
- package/lib/index.js +24 -8
- package/lib/operations.js +81 -68
- package/lib/types/actions.js +2 -1
- package/lib/types/index.js +18 -2
- package/lib/types/operations.js +16 -10
- package/package.json +6 -7
package/lib/operations.js
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.simulateBundlerOperations = exports.simulateBundlerOperation = exports.handleBundlerOperations = exports.handleBundlerOperation = exports.getSimulatedBundlerOperation = exports.simulateRequiredTokenAmounts = exports.populateBundle = exports.finalizeBundle = exports.populateSubBundle = exports.populateInputTransfer = exports.DEFAULT_SUPPLY_TARGET_UTILIZATION = void 0;
|
|
4
|
+
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
5
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
6
|
+
const simulation_sdk_1 = require("@morpho-org/simulation-sdk");
|
|
7
|
+
const viem_1 = require("viem");
|
|
8
|
+
const errors_js_1 = require("./errors.js");
|
|
6
9
|
/**
|
|
7
10
|
* The default target utilization above which the shared liquidity algorithm is triggered (scaled by WAD).
|
|
8
11
|
*/
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const { bundler3: { generalAdapter1 }, permit2, } = getChainAddresses(data.chainId);
|
|
12
|
+
exports.DEFAULT_SUPPLY_TARGET_UTILIZATION = 905000000000000000n;
|
|
13
|
+
const populateInputTransfer = ({ address, args: { amount, from } }, data, { hasSimplePermit = false } = {}) => {
|
|
14
|
+
const { bundler3: { generalAdapter1 }, permit2, } = (0, blue_sdk_1.getChainAddresses)(data.chainId);
|
|
12
15
|
// If native token, it is expected to be sent along as call value.
|
|
13
|
-
if (address === NATIVE_ADDRESS)
|
|
16
|
+
if (address === blue_sdk_1.NATIVE_ADDRESS)
|
|
14
17
|
return [
|
|
15
18
|
{
|
|
16
19
|
type: "Erc20_Transfer",
|
|
@@ -45,8 +48,8 @@ export const populateInputTransfer = ({ address, args: { amount, from } }, data,
|
|
|
45
48
|
hasSimplePermit);
|
|
46
49
|
const useSimpleTransfer = permit2 == null ||
|
|
47
50
|
// Token is permissioned and Permit2 may not be authorized so Permit2 cannot be used.
|
|
48
|
-
!!permissionedWrapperTokens[data.chainId]?.has(address) ||
|
|
49
|
-
!!permissionedBackedTokens[data.chainId]?.has(address);
|
|
51
|
+
!!blue_sdk_1.permissionedWrapperTokens[data.chainId]?.has(address) ||
|
|
52
|
+
!!blue_sdk_1.permissionedBackedTokens[data.chainId]?.has(address);
|
|
50
53
|
if (useSimplePermit)
|
|
51
54
|
operations.push({
|
|
52
55
|
type: "Erc20_Permit",
|
|
@@ -87,7 +90,7 @@ export const populateInputTransfer = ({ address, args: { amount, from } }, data,
|
|
|
87
90
|
sender: from,
|
|
88
91
|
address,
|
|
89
92
|
args: {
|
|
90
|
-
amount: MathLib.MAX_UINT_160, // Always approve infinite.
|
|
93
|
+
amount: blue_sdk_1.MathLib.MAX_UINT_160, // Always approve infinite.
|
|
91
94
|
spender: permit2,
|
|
92
95
|
},
|
|
93
96
|
});
|
|
@@ -99,7 +102,7 @@ export const populateInputTransfer = ({ address, args: { amount, from } }, data,
|
|
|
99
102
|
address,
|
|
100
103
|
args: {
|
|
101
104
|
amount,
|
|
102
|
-
expiration: MathLib.MAX_UINT_48, // Always approve indefinitely.
|
|
105
|
+
expiration: blue_sdk_1.MathLib.MAX_UINT_48, // Always approve indefinitely.
|
|
103
106
|
nonce: permit2BundlerAllowance.nonce,
|
|
104
107
|
},
|
|
105
108
|
});
|
|
@@ -116,6 +119,7 @@ export const populateInputTransfer = ({ address, args: { amount, from } }, data,
|
|
|
116
119
|
}
|
|
117
120
|
return operations;
|
|
118
121
|
};
|
|
122
|
+
exports.populateInputTransfer = populateInputTransfer;
|
|
119
123
|
/**
|
|
120
124
|
* Simulates the input operation on the given simulation data with args tweaked so the bundler operates on behalf of the sender.
|
|
121
125
|
* Then, populates a bundle of operations made of:
|
|
@@ -129,18 +133,18 @@ export const populateInputTransfer = ({ address, args: { amount, from } }, data,
|
|
|
129
133
|
* @param wrapSlippage The slippage simulated during wraps. Should never be 0.
|
|
130
134
|
* @return The bundle of operations to optimize and skim before being encoded.
|
|
131
135
|
*/
|
|
132
|
-
|
|
136
|
+
const populateSubBundle = (inputOperation, data, options = {}) => {
|
|
133
137
|
const { sender } = inputOperation;
|
|
134
|
-
const { morpho, bundler3: { bundler3, generalAdapter1 }, } = getChainAddresses(data.chainId);
|
|
138
|
+
const { morpho, bundler3: { bundler3, generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(data.chainId);
|
|
135
139
|
const { withSimplePermit = new Set(), publicAllocatorOptions, getRequirementOperations, } = options;
|
|
136
140
|
const operations = [];
|
|
137
141
|
const wrappedToken = inputOperation.type === "Erc20_Wrap"
|
|
138
142
|
? data.getWrappedToken(inputOperation.address)
|
|
139
143
|
: undefined;
|
|
140
144
|
const isErc20Wrapper = !!wrappedToken &&
|
|
141
|
-
!!erc20WrapperTokens[data.chainId]?.has(wrappedToken.address);
|
|
145
|
+
!!blue_sdk_1.erc20WrapperTokens[data.chainId]?.has(wrappedToken.address);
|
|
142
146
|
// Transform input operation to act on behalf of the sender, via the bundler.
|
|
143
|
-
const mainOperation = produceImmutable(inputOperation, (draft) => {
|
|
147
|
+
const mainOperation = (0, simulation_sdk_1.produceImmutable)(inputOperation, (draft) => {
|
|
144
148
|
draft.sender = generalAdapter1;
|
|
145
149
|
// Redirect MetaMorpho operation owner.
|
|
146
150
|
switch (draft.type) {
|
|
@@ -203,8 +207,8 @@ export const populateSubBundle = (inputOperation, data, options = {}) => {
|
|
|
203
207
|
const reallocations = {};
|
|
204
208
|
const supplyTargetUtilization = publicAllocatorOptions.supplyTargetUtilization?.[market.params.id] ??
|
|
205
209
|
publicAllocatorOptions.defaultSupplyTargetUtilization ??
|
|
206
|
-
DEFAULT_SUPPLY_TARGET_UTILIZATION;
|
|
207
|
-
if (MarketUtils.getUtilization({
|
|
210
|
+
exports.DEFAULT_SUPPLY_TARGET_UTILIZATION;
|
|
211
|
+
if (blue_sdk_1.MarketUtils.getUtilization({
|
|
208
212
|
totalSupplyAssets: newTotalSupplyAssets,
|
|
209
213
|
totalBorrowAssets: newTotalBorrowAssets,
|
|
210
214
|
}) > supplyTargetUtilization) {
|
|
@@ -213,8 +217,8 @@ export const populateSubBundle = (inputOperation, data, options = {}) => {
|
|
|
213
217
|
// We first try to find public reallocations that respect every markets targets.
|
|
214
218
|
// If this is not enough, the first market to be pushed above target is the supply market. Then we fully withdraw from every market.
|
|
215
219
|
let requiredAssets = supplyTargetUtilization === 0n
|
|
216
|
-
? MathLib.MAX_UINT_160
|
|
217
|
-
: MathLib.wDivDown(newTotalBorrowAssets, supplyTargetUtilization) -
|
|
220
|
+
? blue_sdk_1.MathLib.MAX_UINT_160
|
|
221
|
+
: blue_sdk_1.MathLib.wDivDown(newTotalBorrowAssets, supplyTargetUtilization) -
|
|
218
222
|
newTotalSupplyAssets;
|
|
219
223
|
const { withdrawals, data: friendlyReallocationData } = data.getMarketPublicReallocations(market.id, publicAllocatorOptions);
|
|
220
224
|
const friendlyReallocationMarket = friendlyReallocationData.getMarket(market.id);
|
|
@@ -224,14 +228,14 @@ export const populateSubBundle = (inputOperation, data, options = {}) => {
|
|
|
224
228
|
requiredAssets = newTotalBorrowAssets - newTotalSupplyAssets;
|
|
225
229
|
withdrawals.push(...friendlyReallocationData.getMarketPublicReallocations(market.id, {
|
|
226
230
|
...publicAllocatorOptions,
|
|
227
|
-
defaultMaxWithdrawalUtilization: MathLib.WAD,
|
|
231
|
+
defaultMaxWithdrawalUtilization: blue_sdk_1.MathLib.WAD,
|
|
228
232
|
maxWithdrawalUtilization: {},
|
|
229
233
|
}).withdrawals);
|
|
230
234
|
}
|
|
231
235
|
for (const { vault, ...withdrawal } of withdrawals) {
|
|
232
236
|
const vaultReallocations = (reallocations[vault] ??= []);
|
|
233
237
|
const vaultMarketReallocation = vaultReallocations.find((item) => item.id === withdrawal.id);
|
|
234
|
-
const reallocatedAssets = MathLib.min(withdrawal.assets, requiredAssets);
|
|
238
|
+
const reallocatedAssets = blue_sdk_1.MathLib.min(withdrawal.assets, requiredAssets);
|
|
235
239
|
if (vaultMarketReallocation != null)
|
|
236
240
|
vaultMarketReallocation.assets += reallocatedAssets;
|
|
237
241
|
else
|
|
@@ -246,13 +250,13 @@ export const populateSubBundle = (inputOperation, data, options = {}) => {
|
|
|
246
250
|
// TODO: we know there are no unwrap native in the middle
|
|
247
251
|
// of the bundle so we are certain we need to add an input transfer.
|
|
248
252
|
// This could be handled by `simulateRequiredTokenAmounts` below.
|
|
249
|
-
const fees = keys(reallocations).reduce((total, vault) => total + data.getVault(vault).publicAllocatorConfig.fee, 0n);
|
|
253
|
+
const fees = (0, morpho_ts_1.keys)(reallocations).reduce((total, vault) => total + data.getVault(vault).publicAllocatorConfig.fee, 0n);
|
|
250
254
|
// Native input transfer of all fees.
|
|
251
255
|
if (fees > 0n)
|
|
252
256
|
operations.push({
|
|
253
257
|
type: "Erc20_Transfer",
|
|
254
258
|
sender,
|
|
255
|
-
address: NATIVE_ADDRESS,
|
|
259
|
+
address: blue_sdk_1.NATIVE_ADDRESS,
|
|
256
260
|
args: {
|
|
257
261
|
amount: fees,
|
|
258
262
|
from: sender,
|
|
@@ -272,7 +276,7 @@ export const populateSubBundle = (inputOperation, data, options = {}) => {
|
|
|
272
276
|
},
|
|
273
277
|
})));
|
|
274
278
|
}
|
|
275
|
-
const callback = getValue(mainOperation.args, "callback");
|
|
279
|
+
const callback = (0, morpho_ts_1.getValue)(mainOperation.args, "callback");
|
|
276
280
|
const simulatedOperation = {
|
|
277
281
|
...mainOperation,
|
|
278
282
|
args: {
|
|
@@ -280,9 +284,9 @@ export const populateSubBundle = (inputOperation, data, options = {}) => {
|
|
|
280
284
|
...(callback && {
|
|
281
285
|
callback: (data) => {
|
|
282
286
|
const operations = callback.flatMap((inputOperation) => {
|
|
283
|
-
const subBundleOperations = populateSubBundle(inputOperation, data, options);
|
|
287
|
+
const subBundleOperations = (0, exports.populateSubBundle)(inputOperation, data, options);
|
|
284
288
|
// Handle to mutate data (not simulate).
|
|
285
|
-
handleBundlerOperations(subBundleOperations, data);
|
|
289
|
+
(0, exports.handleBundlerOperations)(subBundleOperations, data);
|
|
286
290
|
return subBundleOperations;
|
|
287
291
|
});
|
|
288
292
|
mainOperation.args.callback =
|
|
@@ -293,27 +297,27 @@ export const populateSubBundle = (inputOperation, data, options = {}) => {
|
|
|
293
297
|
},
|
|
294
298
|
};
|
|
295
299
|
// Operations with callbacks are populated recursively as a side-effect of the simulation, within the callback itself.
|
|
296
|
-
let requiredTokenAmounts = simulateRequiredTokenAmounts(operations.concat([simulatedOperation]), data);
|
|
300
|
+
let requiredTokenAmounts = (0, exports.simulateRequiredTokenAmounts)(operations.concat([simulatedOperation]), data);
|
|
297
301
|
const allOperations = operations.concat([
|
|
298
302
|
mainOperation,
|
|
299
303
|
]);
|
|
300
304
|
// Skip approvals/transfers if operation only uses available balances (via maxUint256).
|
|
301
305
|
if (("amount" in mainOperation.args &&
|
|
302
|
-
mainOperation.args.amount === maxUint256) ||
|
|
306
|
+
mainOperation.args.amount === viem_1.maxUint256) ||
|
|
303
307
|
("assets" in mainOperation.args &&
|
|
304
|
-
mainOperation.args.assets === maxUint256) ||
|
|
305
|
-
("shares" in mainOperation.args && mainOperation.args.shares === maxUint256)) {
|
|
308
|
+
mainOperation.args.assets === viem_1.maxUint256) ||
|
|
309
|
+
("shares" in mainOperation.args && mainOperation.args.shares === viem_1.maxUint256)) {
|
|
306
310
|
if (mainOperation.type === "MetaMorpho_Withdraw")
|
|
307
311
|
mainOperation.args.owner = generalAdapter1;
|
|
308
312
|
return allOperations;
|
|
309
313
|
}
|
|
310
314
|
const requirementOperations = getRequirementOperations?.(requiredTokenAmounts) ?? [];
|
|
311
|
-
requiredTokenAmounts = simulateRequiredTokenAmounts(requirementOperations
|
|
315
|
+
requiredTokenAmounts = (0, exports.simulateRequiredTokenAmounts)(requirementOperations
|
|
312
316
|
.concat(allOperations)
|
|
313
|
-
.map((operation) => getSimulatedBundlerOperation(operation)), data);
|
|
317
|
+
.map((operation) => (0, exports.getSimulatedBundlerOperation)(operation)), data);
|
|
314
318
|
// Append required input transfers.
|
|
315
319
|
requiredTokenAmounts.forEach(({ token, required }) => {
|
|
316
|
-
requirementOperations.push(...populateInputTransfer({
|
|
320
|
+
requirementOperations.push(...(0, exports.populateInputTransfer)({
|
|
317
321
|
type: "Erc20_Transfer",
|
|
318
322
|
sender: generalAdapter1,
|
|
319
323
|
address: token,
|
|
@@ -326,6 +330,7 @@ export const populateSubBundle = (inputOperation, data, options = {}) => {
|
|
|
326
330
|
});
|
|
327
331
|
return requirementOperations.concat(allOperations);
|
|
328
332
|
};
|
|
333
|
+
exports.populateSubBundle = populateSubBundle;
|
|
329
334
|
/**
|
|
330
335
|
* Merges unnecessary duplicate `Erc20_Approve`, `Erc20_Transfer` and `Erc20_Wrap`.
|
|
331
336
|
* Also redirects `Blue_Borrow|Withdraw|WithdrawCollateral` & `MetaMorpho_Withdraw` operations from the bundler to the receiver,
|
|
@@ -338,13 +343,13 @@ export const populateSubBundle = (inputOperation, data, options = {}) => {
|
|
|
338
343
|
* @param unwrapSlippage The slippage simulated during unwraps. Should never be 0.
|
|
339
344
|
* @return The optimized bundle.
|
|
340
345
|
*/
|
|
341
|
-
|
|
346
|
+
const finalizeBundle = (operations, startData, receiver, unwrapTokens = new Set(), unwrapSlippage = blue_sdk_1.DEFAULT_SLIPPAGE_TOLERANCE) => {
|
|
342
347
|
const nbOperations = operations.length;
|
|
343
348
|
if (nbOperations === 0)
|
|
344
349
|
return operations;
|
|
345
|
-
const { bundler3: { bundler3, generalAdapter1 }, } = getChainAddresses(startData.chainId);
|
|
346
|
-
if (isAddressEqual(receiver, bundler3) ||
|
|
347
|
-
isAddressEqual(receiver, generalAdapter1))
|
|
350
|
+
const { bundler3: { bundler3, generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(startData.chainId);
|
|
351
|
+
if ((0, viem_1.isAddressEqual)(receiver, bundler3) ||
|
|
352
|
+
(0, viem_1.isAddressEqual)(receiver, generalAdapter1))
|
|
348
353
|
throw Error(`receiver is bundler`);
|
|
349
354
|
const approvals = [];
|
|
350
355
|
const permits = [];
|
|
@@ -398,7 +403,7 @@ export const finalizeBundle = (operations, startData, receiver, unwrapTokens = n
|
|
|
398
403
|
const { address, sender, args: { amount, from, to }, } = operation;
|
|
399
404
|
if (from !== generalAdapter1 &&
|
|
400
405
|
to === generalAdapter1 &&
|
|
401
|
-
!erc20WrapperTokens[startData.chainId]?.has(address)) {
|
|
406
|
+
!blue_sdk_1.erc20WrapperTokens[startData.chainId]?.has(address)) {
|
|
402
407
|
const duplicateTransfer = inputTransfers.find((transfer) => transfer.address === address &&
|
|
403
408
|
transfer.sender === sender &&
|
|
404
409
|
transfer.args.from === from);
|
|
@@ -441,7 +446,7 @@ export const finalizeBundle = (operations, startData, receiver, unwrapTokens = n
|
|
|
441
446
|
inputTransfer2s,
|
|
442
447
|
others,
|
|
443
448
|
].flat(1);
|
|
444
|
-
let steps = simulateBundlerOperations(operations, startData);
|
|
449
|
+
let steps = (0, exports.simulateBundlerOperations)(operations, startData);
|
|
445
450
|
// Redirect MetaMorpho deposits.
|
|
446
451
|
operations.forEach((operation, index) => {
|
|
447
452
|
if (operation.type !== "MetaMorpho_Deposit" ||
|
|
@@ -524,15 +529,15 @@ export const finalizeBundle = (operations, startData, receiver, unwrapTokens = n
|
|
|
524
529
|
return false;
|
|
525
530
|
});
|
|
526
531
|
// Simulate without slippage to skim the bundler of all possible surplus of shares & assets.
|
|
527
|
-
steps = simulateBundlerOperations(operations, startData, { slippage: 0n });
|
|
532
|
+
steps = (0, exports.simulateBundlerOperations)(operations, startData, { slippage: 0n });
|
|
528
533
|
// Unwrap requested remaining wrapped tokens.
|
|
529
534
|
const unwraps = [];
|
|
530
|
-
const endBundlerTokenData = getLast(steps).holdings[generalAdapter1] ?? {};
|
|
535
|
+
const endBundlerTokenData = (0, morpho_ts_1.getLast)(steps).holdings[generalAdapter1] ?? {};
|
|
531
536
|
unwrapTokens.forEach((wrappedToken) => {
|
|
532
537
|
const remaining = endBundlerTokenData[wrappedToken]?.balance ?? 0n;
|
|
533
538
|
if (remaining <= 5n)
|
|
534
539
|
return;
|
|
535
|
-
const unwrappedToken = getUnwrappedToken(wrappedToken, startData.chainId);
|
|
540
|
+
const unwrappedToken = (0, blue_sdk_1.getUnwrappedToken)(wrappedToken, startData.chainId);
|
|
536
541
|
if (unwrappedToken == null)
|
|
537
542
|
return;
|
|
538
543
|
unwraps.push({
|
|
@@ -540,22 +545,22 @@ export const finalizeBundle = (operations, startData, receiver, unwrapTokens = n
|
|
|
540
545
|
address: wrappedToken,
|
|
541
546
|
sender: generalAdapter1,
|
|
542
547
|
args: {
|
|
543
|
-
amount: maxUint256,
|
|
548
|
+
amount: viem_1.maxUint256,
|
|
544
549
|
receiver,
|
|
545
550
|
slippage: unwrapSlippage,
|
|
546
551
|
},
|
|
547
552
|
});
|
|
548
553
|
});
|
|
549
554
|
if (unwraps.length > 0)
|
|
550
|
-
steps = simulateBundlerOperations(operations.concat(unwraps), startData, {
|
|
555
|
+
steps = (0, exports.simulateBundlerOperations)(operations.concat(unwraps), startData, {
|
|
551
556
|
slippage: 0n,
|
|
552
557
|
});
|
|
553
558
|
// Skim any token expected to be left on the bundler.
|
|
554
559
|
const skims = [];
|
|
555
560
|
{
|
|
556
561
|
const startBundlerTokenData = steps[0].holdings[generalAdapter1] ?? {};
|
|
557
|
-
const endBundlerTokenData = getLast(steps).holdings[generalAdapter1] ?? {};
|
|
558
|
-
skims.push(...entries(endBundlerTokenData)
|
|
562
|
+
const endBundlerTokenData = (0, morpho_ts_1.getLast)(steps).holdings[generalAdapter1] ?? {};
|
|
563
|
+
skims.push(...(0, morpho_ts_1.entries)(endBundlerTokenData)
|
|
559
564
|
.filter(([token, holding]) => holding != null &&
|
|
560
565
|
holding.balance - (startBundlerTokenData[token]?.balance ?? 0n) >
|
|
561
566
|
5n)
|
|
@@ -564,7 +569,7 @@ export const finalizeBundle = (operations, startData, receiver, unwrapTokens = n
|
|
|
564
569
|
address,
|
|
565
570
|
sender: generalAdapter1,
|
|
566
571
|
args: {
|
|
567
|
-
amount: maxUint256,
|
|
572
|
+
amount: viem_1.maxUint256,
|
|
568
573
|
from: generalAdapter1,
|
|
569
574
|
to: receiver,
|
|
570
575
|
},
|
|
@@ -572,35 +577,37 @@ export const finalizeBundle = (operations, startData, receiver, unwrapTokens = n
|
|
|
572
577
|
}
|
|
573
578
|
return operations.concat(unwraps, skims);
|
|
574
579
|
};
|
|
575
|
-
|
|
580
|
+
exports.finalizeBundle = finalizeBundle;
|
|
581
|
+
const populateBundle = (inputOperations, data, options) => {
|
|
576
582
|
const steps = [data];
|
|
577
583
|
let end = data;
|
|
578
584
|
const operations = inputOperations.flatMap((inputOperation, index) => {
|
|
579
585
|
try {
|
|
580
|
-
const subBundleOperations = populateSubBundle(inputOperation, end, options);
|
|
581
|
-
steps.push((end = getLast(simulateBundlerOperations(subBundleOperations, end))));
|
|
586
|
+
const subBundleOperations = (0, exports.populateSubBundle)(inputOperation, end, options);
|
|
587
|
+
steps.push((end = (0, morpho_ts_1.getLast)((0, exports.simulateBundlerOperations)(subBundleOperations, end))));
|
|
582
588
|
return subBundleOperations;
|
|
583
589
|
}
|
|
584
590
|
catch (error) {
|
|
585
591
|
if (!(error instanceof Error))
|
|
586
592
|
throw error;
|
|
587
|
-
throw new BundlerErrors.Bundle(error, index, inputOperation, steps);
|
|
593
|
+
throw new errors_js_1.BundlerErrors.Bundle(error, index, inputOperation, steps);
|
|
588
594
|
}
|
|
589
595
|
});
|
|
590
596
|
return { operations, steps };
|
|
591
597
|
};
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
const
|
|
598
|
+
exports.populateBundle = populateBundle;
|
|
599
|
+
const simulateRequiredTokenAmounts = (operations, data) => {
|
|
600
|
+
const { bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(data.chainId);
|
|
601
|
+
const virtualBundlerData = (0, simulation_sdk_1.produceImmutable)(data, (draft) => {
|
|
595
602
|
Object.values(draft.holdings[generalAdapter1] ?? {}).forEach((bundlerTokenData) => {
|
|
596
603
|
if (bundlerTokenData == null)
|
|
597
604
|
return;
|
|
598
605
|
// Virtual balance to calculate the amount required.
|
|
599
|
-
bundlerTokenData.balance += MathLib.MAX_UINT_160;
|
|
606
|
+
bundlerTokenData.balance += blue_sdk_1.MathLib.MAX_UINT_160;
|
|
600
607
|
});
|
|
601
608
|
});
|
|
602
|
-
const steps = simulateOperations(operations, virtualBundlerData);
|
|
603
|
-
const bundlerTokenDiffs = keys(virtualBundlerData.holdings[generalAdapter1]).map((token) => ({
|
|
609
|
+
const steps = (0, simulation_sdk_1.simulateOperations)(operations, virtualBundlerData);
|
|
610
|
+
const bundlerTokenDiffs = (0, morpho_ts_1.keys)(virtualBundlerData.holdings[generalAdapter1]).map((token) => ({
|
|
604
611
|
token,
|
|
605
612
|
required: steps
|
|
606
613
|
.map((step) =>
|
|
@@ -609,22 +616,23 @@ export const simulateRequiredTokenAmounts = (operations, data) => {
|
|
|
609
616
|
// | | |=> MAX_UINT_160 - (3 * MAX_UINT_160 + z) < 0
|
|
610
617
|
// | |=> MAX_UINT_160 - (2 * MAX_UINT_160 - y) < 0
|
|
611
618
|
// |=> MAX_UINT_160 - (MAX_UINT_160 - y - x) > 0
|
|
612
|
-
MathLib.MAX_UINT_160 -
|
|
619
|
+
blue_sdk_1.MathLib.MAX_UINT_160 -
|
|
613
620
|
(step.holdings[generalAdapter1]?.[token]?.balance ?? 0n))
|
|
614
|
-
.sort(bigIntComparator((required) => required,
|
|
621
|
+
.sort((0, morpho_ts_1.bigIntComparator)((required) => required,
|
|
615
622
|
// Take the highest required amount among all operations.
|
|
616
623
|
"desc"))[0],
|
|
617
624
|
}));
|
|
618
625
|
return bundlerTokenDiffs.filter(({ required }) => required > 0n);
|
|
619
626
|
};
|
|
620
|
-
|
|
621
|
-
|
|
627
|
+
exports.simulateRequiredTokenAmounts = simulateRequiredTokenAmounts;
|
|
628
|
+
const getSimulatedBundlerOperation = (operation, { slippage } = {}) => {
|
|
629
|
+
const callback = (0, morpho_ts_1.getValue)(operation.args, "callback");
|
|
622
630
|
const simulatedOperation = {
|
|
623
631
|
...operation,
|
|
624
632
|
args: {
|
|
625
633
|
...operation.args,
|
|
626
634
|
...(callback && {
|
|
627
|
-
callback: () => callback.map((operation) => getSimulatedBundlerOperation(operation, { slippage })),
|
|
635
|
+
callback: () => callback.map((operation) => (0, exports.getSimulatedBundlerOperation)(operation, { slippage })),
|
|
628
636
|
}),
|
|
629
637
|
},
|
|
630
638
|
};
|
|
@@ -644,7 +652,12 @@ export const getSimulatedBundlerOperation = (operation, { slippage } = {}) => {
|
|
|
644
652
|
}
|
|
645
653
|
return simulatedOperation;
|
|
646
654
|
};
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
655
|
+
exports.getSimulatedBundlerOperation = getSimulatedBundlerOperation;
|
|
656
|
+
const handleBundlerOperation = (options) => (operation, startData, index) => (0, simulation_sdk_1.handleOperation)((0, exports.getSimulatedBundlerOperation)(operation, options), startData, index);
|
|
657
|
+
exports.handleBundlerOperation = handleBundlerOperation;
|
|
658
|
+
const handleBundlerOperations = (operations, startData, options) => (0, simulation_sdk_1.handleOperations)(operations, startData, (0, exports.handleBundlerOperation)(options));
|
|
659
|
+
exports.handleBundlerOperations = handleBundlerOperations;
|
|
660
|
+
const simulateBundlerOperation = (options) => (operation, startData, index) => (0, simulation_sdk_1.simulateOperation)((0, exports.getSimulatedBundlerOperation)(operation, options), startData, index);
|
|
661
|
+
exports.simulateBundlerOperation = simulateBundlerOperation;
|
|
662
|
+
const simulateBundlerOperations = (operations, startData, options) => (0, simulation_sdk_1.handleOperations)(operations, startData, (0, exports.simulateBundlerOperation)(options));
|
|
663
|
+
exports.simulateBundlerOperations = simulateBundlerOperations;
|
package/lib/types/actions.js
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
package/lib/types/index.js
CHANGED
|
@@ -1,2 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./actions.js"), exports);
|
|
18
|
+
__exportStar(require("./operations.js"), exports);
|
package/lib/types/operations.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isErc20InputBundlerOperation = exports.isMetaMorphoInputBundlerOperation = exports.isBlueInputBundlerOperation = exports.ERC20_INPUT_OPERATIONS = exports.METAMORPHO_INPUT_OPERATIONS = exports.BLUE_INPUT_OPERATIONS = exports.BUNDLER_OPERATIONS = void 0;
|
|
4
|
+
exports.BUNDLER_OPERATIONS = [
|
|
2
5
|
"Blue_SetAuthorization",
|
|
3
6
|
"Blue_Borrow",
|
|
4
7
|
"Blue_Repay",
|
|
@@ -17,7 +20,7 @@ export const BUNDLER_OPERATIONS = [
|
|
|
17
20
|
"Erc20_Wrap",
|
|
18
21
|
"Erc20_Unwrap",
|
|
19
22
|
];
|
|
20
|
-
|
|
23
|
+
exports.BLUE_INPUT_OPERATIONS = [
|
|
21
24
|
"Blue_Borrow",
|
|
22
25
|
"Blue_Repay",
|
|
23
26
|
"Blue_Supply",
|
|
@@ -26,11 +29,11 @@ export const BLUE_INPUT_OPERATIONS = [
|
|
|
26
29
|
"Blue_WithdrawCollateral",
|
|
27
30
|
"Blue_SetAuthorization",
|
|
28
31
|
];
|
|
29
|
-
|
|
32
|
+
exports.METAMORPHO_INPUT_OPERATIONS = [
|
|
30
33
|
"MetaMorpho_Deposit",
|
|
31
34
|
"MetaMorpho_Withdraw",
|
|
32
35
|
];
|
|
33
|
-
|
|
36
|
+
exports.ERC20_INPUT_OPERATIONS = [
|
|
34
37
|
"Erc20_Wrap",
|
|
35
38
|
"Erc20_Unwrap",
|
|
36
39
|
];
|
|
@@ -41,12 +44,15 @@ export const ERC20_INPUT_OPERATIONS = [
|
|
|
41
44
|
// operation.type
|
|
42
45
|
// );
|
|
43
46
|
// };
|
|
44
|
-
|
|
45
|
-
return BLUE_INPUT_OPERATIONS.includes(operation.type);
|
|
47
|
+
const isBlueInputBundlerOperation = (operation) => {
|
|
48
|
+
return exports.BLUE_INPUT_OPERATIONS.includes(operation.type);
|
|
46
49
|
};
|
|
47
|
-
|
|
48
|
-
|
|
50
|
+
exports.isBlueInputBundlerOperation = isBlueInputBundlerOperation;
|
|
51
|
+
const isMetaMorphoInputBundlerOperation = (operation) => {
|
|
52
|
+
return exports.METAMORPHO_INPUT_OPERATIONS.includes(operation.type);
|
|
49
53
|
};
|
|
50
|
-
|
|
51
|
-
|
|
54
|
+
exports.isMetaMorphoInputBundlerOperation = isMetaMorphoInputBundlerOperation;
|
|
55
|
+
const isErc20InputBundlerOperation = (operation) => {
|
|
56
|
+
return exports.ERC20_INPUT_OPERATIONS.includes(operation.type);
|
|
52
57
|
};
|
|
58
|
+
exports.isErc20InputBundlerOperation = isErc20InputBundlerOperation;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@morpho-org/bundler-sdk-viem",
|
|
3
3
|
"description": "Viem-based extension of `@morpho-org/simulation-sdk` that exports utilities to transform simple interactions on Morpho (such as `Blue_Borrow`) and Morpho Vaults (such as `MetaMorpho_Deposit`) into the required bundles (with ERC20 approvals, transfers, etc) to submit to the bundler onchain.",
|
|
4
|
-
"version": "3.0.
|
|
4
|
+
"version": "3.0.1",
|
|
5
5
|
"author": "Morpho Association <contact@morpho.org>",
|
|
6
6
|
"contributors": [
|
|
7
7
|
"Rubilmax <rmilon@gmail.com>"
|
|
@@ -13,15 +13,14 @@
|
|
|
13
13
|
"email": "contact@morpho.org"
|
|
14
14
|
},
|
|
15
15
|
"license": "MIT",
|
|
16
|
-
"type": "module",
|
|
17
16
|
"main": "lib/index.js",
|
|
18
17
|
"files": [
|
|
19
18
|
"lib"
|
|
20
19
|
],
|
|
21
20
|
"peerDependencies": {
|
|
22
21
|
"viem": "^2.0.0",
|
|
23
|
-
"@morpho-org/blue-sdk": "^3.0.0",
|
|
24
22
|
"@morpho-org/blue-sdk-viem": "^3.0.0",
|
|
23
|
+
"@morpho-org/blue-sdk": "^3.0.0",
|
|
25
24
|
"@morpho-org/simulation-sdk": "^3.0.0",
|
|
26
25
|
"@morpho-org/morpho-ts": "^2.3.0"
|
|
27
26
|
},
|
|
@@ -35,14 +34,14 @@
|
|
|
35
34
|
"typescript": "^5.7.2",
|
|
36
35
|
"viem": "^2.23.0",
|
|
37
36
|
"vitest": "^3.0.5",
|
|
37
|
+
"@morpho-org/blue-sdk": "^3.0.0",
|
|
38
38
|
"@morpho-org/blue-sdk-viem": "^3.0.0",
|
|
39
|
-
"@morpho-org/morpho-ts": "^2.3.0",
|
|
40
39
|
"@morpho-org/morpho-test": "^2.3.0",
|
|
41
|
-
"@morpho-org/blue-sdk": "^3.0.0",
|
|
42
40
|
"@morpho-org/simulation-sdk": "^3.0.0",
|
|
43
|
-
"@morpho-org/
|
|
41
|
+
"@morpho-org/morpho-ts": "^2.3.0",
|
|
42
|
+
"@morpho-org/test": "^2.1.3",
|
|
44
43
|
"@morpho-org/simulation-sdk-wagmi": "^3.0.0",
|
|
45
|
-
"@morpho-org/test": "^2.
|
|
44
|
+
"@morpho-org/test-wagmi": "^2.0.4"
|
|
46
45
|
},
|
|
47
46
|
"scripts": {
|
|
48
47
|
"prepublish": "$npm_execpath build",
|