@morpho-org/bundler-sdk-viem 3.0.0-next.1 → 3.0.0-next.11
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.d.ts +35 -0
- package/lib/ActionBundle.js +28 -0
- package/lib/BundlerAction.d.ts +78 -44
- package/lib/BundlerAction.js +146 -119
- package/lib/actions.d.ts +6 -8
- package/lib/actions.js +129 -84
- package/lib/bundle.d.ts +12 -0
- package/lib/bundle.js +11 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.js +4 -0
- package/lib/operations.js +6 -5
- package/lib/types/actions.d.ts +102 -46
- package/lib/types/operations.d.ts +1 -1
- package/lib/types/operations.js +1 -0
- package/package.json +12 -12
package/lib/actions.d.ts
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
import { type Address } from "viem";
|
|
2
|
-
import { ChainId } from "@morpho-org/blue-sdk";
|
|
3
2
|
import { type MaybeDraft, type SimulationState } from "@morpho-org/simulation-sdk";
|
|
4
|
-
import
|
|
5
|
-
|
|
3
|
+
import { ActionBundle, ActionBundleRequirements } from "./ActionBundle.js";
|
|
4
|
+
import type { Action, BundlerOperation, TransactionRequirement } from "./types/index.js";
|
|
5
|
+
export declare const APPROVE_ONLY_ONCE_TOKENS: Partial<Record<number, Address[]>>;
|
|
6
|
+
export declare const MAX_TOKEN_APPROVALS: Partial<Record<number, Record<Address, bigint>>>;
|
|
6
7
|
export declare const encodeOperation: (operation: BundlerOperation, dataBefore: MaybeDraft<SimulationState>, supportsSignature?: boolean, index?: number) => {
|
|
7
8
|
dataAfter: MaybeDraft<SimulationState>;
|
|
8
9
|
actions: Action[];
|
|
9
|
-
requirements:
|
|
10
|
-
signatures: import("./types/actions.js").SignatureRequirement[];
|
|
11
|
-
txs: TransactionRequirement[];
|
|
12
|
-
};
|
|
10
|
+
requirements: ActionBundleRequirements<TransactionRequirement, import("./types/actions.js").SignatureRequirement>;
|
|
13
11
|
};
|
|
14
|
-
export declare function encodeBundle(operations: BundlerOperation[], startData: MaybeDraft<SimulationState>, supportsSignature?: boolean): ActionBundle
|
|
12
|
+
export declare function encodeBundle(operations: BundlerOperation[], startData: MaybeDraft<SimulationState>, supportsSignature?: boolean): ActionBundle<TransactionRequirement, import("./types/actions.js").SignatureRequirement>;
|
package/lib/actions.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import { encodeFunctionData, erc20Abi, maxUint256, verifyTypedData, zeroAddress, } from "viem";
|
|
2
|
-
import { ChainId, MathLib, NATIVE_ADDRESS, convexWrapperTokens, erc20WrapperTokens, getChainAddresses, getUnwrappedToken, } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { ChainId, DEFAULT_SLIPPAGE_TOLERANCE, MathLib, NATIVE_ADDRESS, convexWrapperTokens, erc20WrapperTokens, getChainAddresses, getUnwrappedToken, } from "@morpho-org/blue-sdk";
|
|
3
3
|
import { Time, getValue } from "@morpho-org/morpho-ts";
|
|
4
|
-
import { simulateOperation, } from "@morpho-org/simulation-sdk";
|
|
4
|
+
import { getCurrent, simulateOperation, } from "@morpho-org/simulation-sdk";
|
|
5
5
|
import { blueAbi, getAuthorizationTypedData, getDaiPermitTypedData, getPermit2PermitTypedData, getPermitTypedData, } from "@morpho-org/blue-sdk-viem";
|
|
6
6
|
import { signTypedData } from "viem/actions";
|
|
7
|
-
import
|
|
7
|
+
import { ActionBundle, ActionBundleRequirements } from "./ActionBundle.js";
|
|
8
8
|
export const APPROVE_ONLY_ONCE_TOKENS = {
|
|
9
9
|
[ChainId.EthMainnet]: [
|
|
10
10
|
"0xdAC17F958D2ee523a2206206994597C13D831ec7", // USDT
|
|
11
11
|
"0xD533a949740bb3306d119CC777fa900bA034cd52", // CRV
|
|
12
12
|
],
|
|
13
13
|
};
|
|
14
|
-
const MAX_TOKEN_APPROVALS = {
|
|
14
|
+
export const MAX_TOKEN_APPROVALS = {
|
|
15
15
|
[ChainId.EthMainnet]: {
|
|
16
16
|
"0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984": MathLib.maxUint(96), // UNI --> see https://github.com/Uniswap/governance/blob/eabd8c71ad01f61fb54ed6945162021ee419998e/contracts/Uni.sol#L154
|
|
17
17
|
},
|
|
@@ -67,10 +67,7 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
67
67
|
const deadline = Time.timestamp() + Time.s.from.h(24n);
|
|
68
68
|
const { morpho, bundler3: { bundler3, generalAdapter1 }, permit2, wNative, dai, wstEth, stEth, } = getChainAddresses(chainId);
|
|
69
69
|
const actions = [];
|
|
70
|
-
const requirements =
|
|
71
|
-
signatures: [],
|
|
72
|
-
txs: [],
|
|
73
|
-
};
|
|
70
|
+
const requirements = new ActionBundleRequirements();
|
|
74
71
|
let callbackBundle;
|
|
75
72
|
const callback = getValue(operation.args, "callback");
|
|
76
73
|
const simulatedOperation = {
|
|
@@ -79,7 +76,7 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
79
76
|
...operation.args,
|
|
80
77
|
...(callback && {
|
|
81
78
|
callback: (dataBefore) => {
|
|
82
|
-
callbackBundle = encodeBundle(callback, dataBefore, supportsSignature);
|
|
79
|
+
callbackBundle = encodeBundle(callback, getCurrent(dataBefore), supportsSignature);
|
|
83
80
|
return callback;
|
|
84
81
|
},
|
|
85
82
|
}),
|
|
@@ -94,19 +91,19 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
94
91
|
const { sender, address } = operation;
|
|
95
92
|
switch (operation.type) {
|
|
96
93
|
case "Blue_SetAuthorization": {
|
|
97
|
-
const { owner } = operation.args;
|
|
94
|
+
const { owner, isAuthorized, authorized } = operation.args;
|
|
98
95
|
if (supportsSignature) {
|
|
99
96
|
const ownerData = dataBefore.getUser(owner);
|
|
100
97
|
const authorization = {
|
|
101
98
|
authorizer: owner,
|
|
102
|
-
authorized
|
|
103
|
-
isAuthorized
|
|
99
|
+
authorized,
|
|
100
|
+
isAuthorized,
|
|
104
101
|
deadline,
|
|
105
102
|
nonce: ownerData.morphoNonce,
|
|
106
103
|
};
|
|
107
104
|
const action = {
|
|
108
105
|
type: "morphoSetAuthorizationWithSig",
|
|
109
|
-
args: [authorization, null],
|
|
106
|
+
args: [authorization, null, operation.skipRevert],
|
|
110
107
|
};
|
|
111
108
|
actions.push(action);
|
|
112
109
|
requirements.signatures.push({
|
|
@@ -133,13 +130,13 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
133
130
|
// Signatures are not supported, fallback to standard approval.
|
|
134
131
|
requirements.txs.push({
|
|
135
132
|
type: "morphoSetAuthorization",
|
|
136
|
-
args: [
|
|
133
|
+
args: [authorized, isAuthorized],
|
|
137
134
|
tx: {
|
|
138
135
|
to: morpho,
|
|
139
136
|
data: encodeFunctionData({
|
|
140
137
|
abi: blueAbi,
|
|
141
138
|
functionName: "setAuthorization",
|
|
142
|
-
args: [
|
|
139
|
+
args: [authorized, isAuthorized],
|
|
143
140
|
}),
|
|
144
141
|
},
|
|
145
142
|
});
|
|
@@ -165,11 +162,27 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
165
162
|
const action = address === dai
|
|
166
163
|
? {
|
|
167
164
|
type: "permitDai",
|
|
168
|
-
args: [
|
|
165
|
+
args: [
|
|
166
|
+
sender,
|
|
167
|
+
nonce,
|
|
168
|
+
deadline,
|
|
169
|
+
true,
|
|
170
|
+
null,
|
|
171
|
+
spender,
|
|
172
|
+
operation.skipRevert,
|
|
173
|
+
],
|
|
169
174
|
}
|
|
170
175
|
: {
|
|
171
176
|
type: "permit",
|
|
172
|
-
args: [
|
|
177
|
+
args: [
|
|
178
|
+
sender,
|
|
179
|
+
address,
|
|
180
|
+
amount,
|
|
181
|
+
deadline,
|
|
182
|
+
null,
|
|
183
|
+
spender,
|
|
184
|
+
operation.skipRevert,
|
|
185
|
+
],
|
|
173
186
|
};
|
|
174
187
|
actions.push(action);
|
|
175
188
|
const tokenData = dataBefore.getToken(address);
|
|
@@ -246,6 +259,7 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
246
259
|
sigDeadline: deadline,
|
|
247
260
|
},
|
|
248
261
|
null,
|
|
262
|
+
operation.skipRevert,
|
|
249
263
|
],
|
|
250
264
|
};
|
|
251
265
|
actions.push(action);
|
|
@@ -287,7 +301,7 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
287
301
|
if (address === NATIVE_ADDRESS) {
|
|
288
302
|
actions.push({
|
|
289
303
|
type: "nativeTransfer",
|
|
290
|
-
args: [from, to, amount],
|
|
304
|
+
args: [from, to, amount, operation.skipRevert],
|
|
291
305
|
});
|
|
292
306
|
break;
|
|
293
307
|
}
|
|
@@ -295,13 +309,13 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
295
309
|
if (from === generalAdapter1) {
|
|
296
310
|
actions.push({
|
|
297
311
|
type: "erc20Transfer",
|
|
298
|
-
args: [address, to, amount],
|
|
312
|
+
args: [address, to, amount, undefined, operation.skipRevert],
|
|
299
313
|
});
|
|
300
314
|
break;
|
|
301
315
|
}
|
|
302
316
|
actions.push({
|
|
303
317
|
type: "erc20TransferFrom",
|
|
304
|
-
args: [address, amount, to],
|
|
318
|
+
args: [address, amount, to, operation.skipRevert],
|
|
305
319
|
});
|
|
306
320
|
break;
|
|
307
321
|
}
|
|
@@ -310,14 +324,14 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
310
324
|
if (supportsSignature) {
|
|
311
325
|
actions.push({
|
|
312
326
|
type: "transferFrom2",
|
|
313
|
-
args: [address, from, amount, to],
|
|
327
|
+
args: [address, from, amount, to, operation.skipRevert],
|
|
314
328
|
});
|
|
315
329
|
break;
|
|
316
330
|
}
|
|
317
331
|
// Signatures are not supported, fallback to standard transfer.
|
|
318
332
|
actions.push({
|
|
319
333
|
type: "erc20TransferFrom",
|
|
320
|
-
args: [address, amount, to],
|
|
334
|
+
args: [address, amount, to, operation.skipRevert],
|
|
321
335
|
});
|
|
322
336
|
break;
|
|
323
337
|
}
|
|
@@ -327,37 +341,43 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
327
341
|
case wNative: {
|
|
328
342
|
actions.push({
|
|
329
343
|
type: "wrapNative",
|
|
330
|
-
args: [amount],
|
|
344
|
+
args: [amount, undefined, operation.skipRevert],
|
|
331
345
|
});
|
|
332
346
|
break;
|
|
333
347
|
}
|
|
334
348
|
case wstEth: {
|
|
335
349
|
actions.push({
|
|
336
350
|
type: "wrapStEth",
|
|
337
|
-
args: [amount],
|
|
351
|
+
args: [amount, undefined, operation.skipRevert],
|
|
338
352
|
});
|
|
339
353
|
break;
|
|
340
354
|
}
|
|
341
355
|
case stEth: {
|
|
342
356
|
actions.push({
|
|
343
357
|
type: "stakeEth",
|
|
344
|
-
args: [
|
|
358
|
+
args: [
|
|
359
|
+
amount,
|
|
360
|
+
MathLib.MAX_UINT_256,
|
|
361
|
+
zeroAddress,
|
|
362
|
+
undefined,
|
|
363
|
+
operation.skipRevert,
|
|
364
|
+
],
|
|
345
365
|
});
|
|
346
366
|
break;
|
|
347
367
|
}
|
|
348
368
|
default: {
|
|
349
|
-
if (erc20WrapperTokens[chainId]
|
|
369
|
+
if (erc20WrapperTokens[chainId]?.has(address)) {
|
|
350
370
|
const underlying = getUnwrappedToken(address, chainId);
|
|
351
371
|
if (underlying == null)
|
|
352
372
|
throw Error(`unknown wrapped token: ${address}`);
|
|
353
373
|
actions.push({
|
|
354
374
|
type: "erc20WrapperDepositFor",
|
|
355
|
-
args: [address, underlying, amount],
|
|
375
|
+
args: [address, underlying, amount, operation.skipRevert],
|
|
356
376
|
});
|
|
357
377
|
break;
|
|
358
378
|
}
|
|
359
379
|
// Convex token wrapping is executed onchain along with supplyCollateral, via depositFor.
|
|
360
|
-
if (!convexWrapperTokens[chainId]
|
|
380
|
+
if (!convexWrapperTokens[chainId]?.has(address))
|
|
361
381
|
throw Error(`unexpected token wrap: ${address}`);
|
|
362
382
|
}
|
|
363
383
|
}
|
|
@@ -369,82 +389,94 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
369
389
|
case wNative: {
|
|
370
390
|
actions.push({
|
|
371
391
|
type: "unwrapNative",
|
|
372
|
-
args: [amount],
|
|
392
|
+
args: [amount, undefined, operation.skipRevert],
|
|
373
393
|
});
|
|
374
394
|
break;
|
|
375
395
|
}
|
|
376
396
|
case wstEth: {
|
|
377
397
|
actions.push({
|
|
378
398
|
type: "unwrapStEth",
|
|
379
|
-
args: [amount],
|
|
399
|
+
args: [amount, undefined, operation.skipRevert],
|
|
380
400
|
});
|
|
381
401
|
break;
|
|
382
402
|
}
|
|
383
403
|
default: {
|
|
384
|
-
if (!erc20WrapperTokens[chainId]
|
|
404
|
+
if (!erc20WrapperTokens[chainId]?.has(address))
|
|
385
405
|
throw Error(`unexpected token unwrap: ${address}`);
|
|
386
406
|
actions.push({
|
|
387
407
|
type: "erc20WrapperWithdrawTo",
|
|
388
|
-
args: [address, receiver, amount],
|
|
408
|
+
args: [address, receiver, amount, operation.skipRevert],
|
|
389
409
|
});
|
|
390
410
|
}
|
|
391
411
|
}
|
|
392
412
|
break;
|
|
393
413
|
}
|
|
394
414
|
case "Blue_Supply": {
|
|
395
|
-
const { id, assets = 0n, shares = 0n, onBehalf, slippage =
|
|
396
|
-
const
|
|
397
|
-
const maxSharePrice = (
|
|
398
|
-
totalSupplyShares;
|
|
415
|
+
const { id, assets = 0n, shares = 0n, onBehalf, slippage = DEFAULT_SLIPPAGE_TOLERANCE, } = operation.args;
|
|
416
|
+
const market = dataBefore.getMarket(id);
|
|
417
|
+
const maxSharePrice = market.toSupplyAssets(MathLib.wToRay(MathLib.WAD + slippage));
|
|
399
418
|
actions.push({
|
|
400
419
|
type: "morphoSupply",
|
|
401
420
|
args: [
|
|
402
|
-
params,
|
|
421
|
+
market.params,
|
|
403
422
|
assets,
|
|
404
423
|
shares,
|
|
405
424
|
maxSharePrice,
|
|
406
425
|
onBehalf,
|
|
407
426
|
callbackBundle?.actions ?? [],
|
|
427
|
+
operation.skipRevert,
|
|
408
428
|
],
|
|
409
429
|
});
|
|
410
430
|
break;
|
|
411
431
|
}
|
|
412
432
|
case "Blue_Withdraw": {
|
|
413
|
-
const { id, assets = 0n, shares = 0n, receiver, slippage =
|
|
414
|
-
const
|
|
415
|
-
const minSharePrice = (
|
|
416
|
-
totalSupplyShares;
|
|
433
|
+
const { id, assets = 0n, shares = 0n, receiver, slippage = DEFAULT_SLIPPAGE_TOLERANCE, } = operation.args;
|
|
434
|
+
const market = dataBefore.getMarket(id);
|
|
435
|
+
const minSharePrice = market.toSupplyAssets(MathLib.wToRay(MathLib.WAD - slippage));
|
|
417
436
|
actions.push({
|
|
418
437
|
type: "morphoWithdraw",
|
|
419
|
-
args: [
|
|
438
|
+
args: [
|
|
439
|
+
market.params,
|
|
440
|
+
assets,
|
|
441
|
+
shares,
|
|
442
|
+
minSharePrice,
|
|
443
|
+
receiver,
|
|
444
|
+
operation.skipRevert,
|
|
445
|
+
],
|
|
420
446
|
});
|
|
421
447
|
break;
|
|
422
448
|
}
|
|
423
449
|
case "Blue_Borrow": {
|
|
424
|
-
const { id, assets = 0n, shares = 0n, receiver, slippage =
|
|
425
|
-
const
|
|
426
|
-
const minSharePrice = (
|
|
427
|
-
totalBorrowShares;
|
|
450
|
+
const { id, assets = 0n, shares = 0n, receiver, slippage = DEFAULT_SLIPPAGE_TOLERANCE, } = operation.args;
|
|
451
|
+
const market = dataBefore.getMarket(id);
|
|
452
|
+
const minSharePrice = market.toBorrowAssets(MathLib.wToRay(MathLib.WAD - slippage));
|
|
428
453
|
actions.push({
|
|
429
454
|
type: "morphoBorrow",
|
|
430
|
-
args: [
|
|
455
|
+
args: [
|
|
456
|
+
market.params,
|
|
457
|
+
assets,
|
|
458
|
+
shares,
|
|
459
|
+
minSharePrice,
|
|
460
|
+
receiver,
|
|
461
|
+
operation.skipRevert,
|
|
462
|
+
],
|
|
431
463
|
});
|
|
432
464
|
break;
|
|
433
465
|
}
|
|
434
466
|
case "Blue_Repay": {
|
|
435
|
-
const { id, assets = 0n, shares = 0n, onBehalf, slippage =
|
|
436
|
-
const
|
|
437
|
-
const maxSharePrice = (
|
|
438
|
-
totalBorrowShares;
|
|
467
|
+
const { id, assets = 0n, shares = 0n, onBehalf, slippage = DEFAULT_SLIPPAGE_TOLERANCE, } = operation.args;
|
|
468
|
+
const market = dataBefore.getMarket(id);
|
|
469
|
+
const maxSharePrice = market.toBorrowAssets(MathLib.wToRay(MathLib.WAD + slippage));
|
|
439
470
|
actions.push({
|
|
440
471
|
type: "morphoRepay",
|
|
441
472
|
args: [
|
|
442
|
-
params,
|
|
473
|
+
market.params,
|
|
443
474
|
assets,
|
|
444
475
|
shares,
|
|
445
476
|
maxSharePrice,
|
|
446
477
|
onBehalf,
|
|
447
478
|
callbackBundle?.actions ?? [],
|
|
479
|
+
operation.skipRevert,
|
|
448
480
|
],
|
|
449
481
|
});
|
|
450
482
|
break;
|
|
@@ -452,19 +484,30 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
452
484
|
case "Blue_SupplyCollateral": {
|
|
453
485
|
const { id, assets, onBehalf } = operation.args;
|
|
454
486
|
const { params } = dataBefore.getMarket(id);
|
|
455
|
-
if (convexWrapperTokens[chainId]
|
|
487
|
+
if (convexWrapperTokens[chainId]?.has(params.collateralToken)) {
|
|
456
488
|
const underlying = getUnwrappedToken(address, chainId);
|
|
457
489
|
if (underlying == null)
|
|
458
490
|
throw Error(`unknown wrapped token: ${address}`);
|
|
459
491
|
actions.push({
|
|
460
492
|
type: "erc20WrapperDepositFor",
|
|
461
|
-
args: [
|
|
493
|
+
args: [
|
|
494
|
+
params.collateralToken,
|
|
495
|
+
underlying,
|
|
496
|
+
assets,
|
|
497
|
+
operation.skipRevert,
|
|
498
|
+
],
|
|
462
499
|
});
|
|
463
500
|
break;
|
|
464
501
|
}
|
|
465
502
|
actions.push({
|
|
466
503
|
type: "morphoSupplyCollateral",
|
|
467
|
-
args: [
|
|
504
|
+
args: [
|
|
505
|
+
params,
|
|
506
|
+
assets,
|
|
507
|
+
onBehalf,
|
|
508
|
+
callbackBundle?.actions ?? [],
|
|
509
|
+
operation.skipRevert,
|
|
510
|
+
],
|
|
468
511
|
});
|
|
469
512
|
break;
|
|
470
513
|
}
|
|
@@ -473,41 +516,53 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
473
516
|
const { params } = dataBefore.getMarket(id);
|
|
474
517
|
actions.push({
|
|
475
518
|
type: "morphoWithdrawCollateral",
|
|
476
|
-
args: [params, assets, receiver],
|
|
519
|
+
args: [params, assets, receiver, operation.skipRevert],
|
|
477
520
|
});
|
|
478
521
|
break;
|
|
479
522
|
}
|
|
480
523
|
case "MetaMorpho_Deposit": {
|
|
481
|
-
const { assets = 0n, shares = 0n, owner, slippage =
|
|
524
|
+
const { assets = 0n, shares = 0n, owner, slippage = DEFAULT_SLIPPAGE_TOLERANCE, } = operation.args;
|
|
482
525
|
const vault = dataBefore.getVault(address);
|
|
483
|
-
const maxSharePrice =
|
|
484
|
-
vault.totalSupply;
|
|
526
|
+
const maxSharePrice = vault.toAssets(MathLib.wToRay(MathLib.WAD + slippage));
|
|
485
527
|
if (shares === 0n)
|
|
486
528
|
actions.push({
|
|
487
529
|
type: "erc4626Deposit",
|
|
488
|
-
args: [address, assets, maxSharePrice, owner],
|
|
530
|
+
args: [address, assets, maxSharePrice, owner, operation.skipRevert],
|
|
489
531
|
});
|
|
490
532
|
else
|
|
491
533
|
actions.push({
|
|
492
534
|
type: "erc4626Mint",
|
|
493
|
-
args: [address, shares, maxSharePrice, owner],
|
|
535
|
+
args: [address, shares, maxSharePrice, owner, operation.skipRevert],
|
|
494
536
|
});
|
|
495
537
|
break;
|
|
496
538
|
}
|
|
497
539
|
case "MetaMorpho_Withdraw": {
|
|
498
|
-
const { assets = 0n, shares = 0n, owner, receiver, slippage =
|
|
540
|
+
const { assets = 0n, shares = 0n, owner, receiver, slippage = DEFAULT_SLIPPAGE_TOLERANCE, } = operation.args;
|
|
499
541
|
const vault = dataBefore.getVault(address);
|
|
500
|
-
const minSharePrice =
|
|
501
|
-
vault.totalSupply;
|
|
542
|
+
const minSharePrice = vault.toAssets(MathLib.wToRay(MathLib.WAD - slippage));
|
|
502
543
|
if (assets > 0n)
|
|
503
544
|
actions.push({
|
|
504
545
|
type: "erc4626Withdraw",
|
|
505
|
-
args: [
|
|
546
|
+
args: [
|
|
547
|
+
address,
|
|
548
|
+
assets,
|
|
549
|
+
minSharePrice,
|
|
550
|
+
receiver,
|
|
551
|
+
owner,
|
|
552
|
+
operation.skipRevert,
|
|
553
|
+
],
|
|
506
554
|
});
|
|
507
555
|
else
|
|
508
556
|
actions.push({
|
|
509
557
|
type: "erc4626Redeem",
|
|
510
|
-
args: [
|
|
558
|
+
args: [
|
|
559
|
+
address,
|
|
560
|
+
shares,
|
|
561
|
+
minSharePrice,
|
|
562
|
+
receiver,
|
|
563
|
+
owner,
|
|
564
|
+
operation.skipRevert,
|
|
565
|
+
],
|
|
511
566
|
});
|
|
512
567
|
break;
|
|
513
568
|
}
|
|
@@ -525,6 +580,7 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
525
580
|
amount: assets,
|
|
526
581
|
})),
|
|
527
582
|
dataBefore.getMarket(supplyMarketId).params,
|
|
583
|
+
operation.skipRevert,
|
|
528
584
|
],
|
|
529
585
|
});
|
|
530
586
|
break;
|
|
@@ -537,24 +593,13 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
537
593
|
};
|
|
538
594
|
};
|
|
539
595
|
export function encodeBundle(operations, startData, supportsSignature = true) {
|
|
540
|
-
const
|
|
541
|
-
const actions = [];
|
|
542
|
-
const requirements = {
|
|
543
|
-
signatures: [],
|
|
544
|
-
txs: [],
|
|
545
|
-
};
|
|
546
|
-
const steps = [startData];
|
|
596
|
+
const bundle = new ActionBundle([startData]);
|
|
547
597
|
for (let index = 0; index < operations.length; ++index) {
|
|
548
|
-
const
|
|
549
|
-
steps.push(
|
|
550
|
-
actions.push(...
|
|
551
|
-
requirements.signatures.push(...
|
|
552
|
-
requirements.txs.push(...
|
|
598
|
+
const { dataAfter, actions, requirements } = encodeOperation(operations[index], bundle.steps[index], supportsSignature, index);
|
|
599
|
+
bundle.steps.push(dataAfter);
|
|
600
|
+
bundle.actions.push(...actions);
|
|
601
|
+
bundle.requirements.signatures.push(...requirements.signatures);
|
|
602
|
+
bundle.requirements.txs.push(...requirements.txs);
|
|
553
603
|
}
|
|
554
|
-
return
|
|
555
|
-
steps,
|
|
556
|
-
actions,
|
|
557
|
-
requirements,
|
|
558
|
-
tx: () => BundlerAction.encodeBundle(chainId, actions),
|
|
559
|
-
};
|
|
604
|
+
return bundle;
|
|
560
605
|
}
|
package/lib/bundle.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { SimulationState } from "@morpho-org/simulation-sdk";
|
|
2
|
+
import type { Address } from "viem";
|
|
3
|
+
import { type BundlingOptions } from "./operations.js";
|
|
4
|
+
import type { InputBundlerOperation } from "./types/index.js";
|
|
5
|
+
export declare const setupBundle: (inputOperations: InputBundlerOperation[], startData: SimulationState, receiver: Address, { supportsSignature, unwrapTokens, unwrapSlippage, ...options }?: BundlingOptions & {
|
|
6
|
+
supportsSignature?: boolean;
|
|
7
|
+
unwrapTokens?: Set<Address>;
|
|
8
|
+
unwrapSlippage?: bigint;
|
|
9
|
+
}) => {
|
|
10
|
+
operations: import("./types/operations.js").BundlerOperation[];
|
|
11
|
+
bundle: import("./ActionBundle.js").ActionBundle<import("./types/actions.js").TransactionRequirement, import("./types/actions.js").SignatureRequirement>;
|
|
12
|
+
};
|
package/lib/bundle.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { encodeBundle } from "./actions.js";
|
|
2
|
+
import { finalizeBundle, populateBundle, } from "./operations.js";
|
|
3
|
+
export const setupBundle = (inputOperations, startData, receiver, { supportsSignature, unwrapTokens, unwrapSlippage, ...options } = {}) => {
|
|
4
|
+
let { operations } = populateBundle(inputOperations, startData, options);
|
|
5
|
+
operations = finalizeBundle(operations, startData, receiver, unwrapTokens, unwrapSlippage);
|
|
6
|
+
const bundle = encodeBundle(operations, startData, supportsSignature);
|
|
7
|
+
return {
|
|
8
|
+
operations,
|
|
9
|
+
bundle,
|
|
10
|
+
};
|
|
11
|
+
};
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
package/lib/operations.js
CHANGED
|
@@ -45,8 +45,8 @@ export const populateInputTransfer = ({ address, args: { amount, from } }, data,
|
|
|
45
45
|
hasSimplePermit);
|
|
46
46
|
const useSimpleTransfer = permit2 == null ||
|
|
47
47
|
// Token is permissioned and Permit2 may not be authorized so Permit2 cannot be used.
|
|
48
|
-
permissionedWrapperTokens[data.chainId]
|
|
49
|
-
permissionedBackedTokens[data.chainId]
|
|
48
|
+
!!permissionedWrapperTokens[data.chainId]?.has(address) ||
|
|
49
|
+
!!permissionedBackedTokens[data.chainId]?.has(address);
|
|
50
50
|
if (useSimplePermit)
|
|
51
51
|
operations.push({
|
|
52
52
|
type: "Erc20_Permit",
|
|
@@ -138,7 +138,7 @@ export const populateSubBundle = (inputOperation, data, options = {}) => {
|
|
|
138
138
|
? data.getWrappedToken(inputOperation.address)
|
|
139
139
|
: undefined;
|
|
140
140
|
const isErc20Wrapper = !!wrappedToken &&
|
|
141
|
-
erc20WrapperTokens[data.chainId]
|
|
141
|
+
!!erc20WrapperTokens[data.chainId]?.has(wrappedToken.address);
|
|
142
142
|
// Transform input operation to act on behalf of the sender, via the bundler.
|
|
143
143
|
const mainOperation = produceImmutable(inputOperation, (draft) => {
|
|
144
144
|
draft.sender = generalAdapter1;
|
|
@@ -179,7 +179,8 @@ export const populateSubBundle = (inputOperation, data, options = {}) => {
|
|
|
179
179
|
address: morpho,
|
|
180
180
|
args: {
|
|
181
181
|
owner: sender,
|
|
182
|
-
|
|
182
|
+
isAuthorized: true,
|
|
183
|
+
authorized: generalAdapter1,
|
|
183
184
|
},
|
|
184
185
|
});
|
|
185
186
|
// Reallocate liquidity if necessary.
|
|
@@ -394,7 +395,7 @@ export const finalizeBundle = (operations, startData, receiver, unwrapTokens = n
|
|
|
394
395
|
const { address, sender, args: { amount, from, to }, } = operation;
|
|
395
396
|
if (from !== generalAdapter1 &&
|
|
396
397
|
to === generalAdapter1 &&
|
|
397
|
-
!erc20WrapperTokens[startData.chainId]
|
|
398
|
+
!erc20WrapperTokens[startData.chainId]?.has(address)) {
|
|
398
399
|
const duplicateTransfer = inputTransfers.find((transfer) => transfer.address === address &&
|
|
399
400
|
transfer.sender === sender &&
|
|
400
401
|
transfer.args.from === from);
|