@morpho-org/bundler-sdk-viem 2.2.1-next.0 → 3.0.0-next.0

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.
@@ -1,177 +1,207 @@
1
- import { aaveV2MigrationBundlerAbi, aaveV3MigrationBundlerAbi, aaveV3OptimizerMigrationBundlerAbi, compoundV2MigrationBundlerAbi, compoundV3MigrationBundlerAbi, erc20WrapperBundlerAbi, erc4626BundlerAbi, ethereumPermitBundlerAbi, morphoBundlerAbi, permit2BundlerAbi, permitBundlerAbi, stEthBundlerAbi, transferBundlerAbi, urdBundlerAbi, wNativeBundlerAbi, } from "./abis.js";
2
- import { encodeAbiParameters, encodeFunctionData, parseSignature, } from "viem";
1
+ import { aaveV2MigrationAdapterAbi, aaveV3MigrationAdapterAbi, aaveV3OptimizerMigrationAdapterAbi, bundler3Abi, compoundV2MigrationAdapterAbi, compoundV3MigrationAdapterAbi, coreAdapterAbi, erc20WrapperAdapterAbi, ethereumGeneralAdapter1Abi, generalAdapter1Abi, universalRewardsDistributorAbi, } from "./abis.js";
2
+ import { getChainAddresses, } from "@morpho-org/blue-sdk";
3
+ import { blueAbi, erc2612Abi, permit2Abi, publicAllocatorAbi, } from "@morpho-org/blue-sdk-viem";
4
+ import { encodeAbiParameters, encodeFunctionData, keccak256, maxUint256, parseSignature, toFunctionSelector, zeroHash, } from "viem";
3
5
  import { BundlerErrors } from "./errors.js";
6
+ const reenterSelectorHash = keccak256(toFunctionSelector(bundler3Abi.find((item) => item.type === "function" && item.name === "reenter")));
4
7
  /**
5
8
  * Namespace to easily encode calls to the Bundler contract, using ethers.
6
9
  */
7
10
  export var BundlerAction;
8
11
  (function (BundlerAction) {
9
- function encode({ type, args }) {
12
+ function encodeBundle(chainId, actions) {
13
+ const { bundler3: { bundler3, generalAdapter1 }, } = getChainAddresses(chainId);
14
+ let value = 0n;
15
+ let generalAdapter1Value = 0n;
16
+ for (const { type, args } of actions) {
17
+ if (type !== "nativeTransfer")
18
+ continue;
19
+ const [owner, recipient, amount] = args;
20
+ if (owner !== bundler3 &&
21
+ owner !== generalAdapter1 &&
22
+ (recipient === bundler3 || recipient === generalAdapter1))
23
+ value += amount;
24
+ if (owner !== generalAdapter1 && recipient === generalAdapter1)
25
+ generalAdapter1Value += amount;
26
+ }
27
+ const encodedActions = actions.flatMap(BundlerAction.encode.bind(null, chainId));
28
+ if (generalAdapter1Value > 0n)
29
+ encodedActions.unshift({
30
+ to: generalAdapter1,
31
+ value: generalAdapter1Value,
32
+ data: "0x",
33
+ skipRevert: false,
34
+ callbackHash: zeroHash,
35
+ });
36
+ return {
37
+ to: bundler3,
38
+ value,
39
+ data: encodeFunctionData({
40
+ abi: bundler3Abi,
41
+ functionName: "multicall",
42
+ args: [encodedActions],
43
+ }),
44
+ };
45
+ }
46
+ BundlerAction.encodeBundle = encodeBundle;
47
+ function encode(chainId, { type, args }) {
10
48
  switch (type) {
11
49
  /* ERC20 */
12
50
  case "nativeTransfer": {
13
- return BundlerAction.nativeTransfer(...args);
51
+ return BundlerAction.nativeTransfer(chainId, ...args);
14
52
  }
15
53
  case "erc20Transfer": {
16
- return BundlerAction.erc20Transfer(...args);
54
+ return BundlerAction.erc20Transfer(chainId, ...args);
17
55
  }
18
56
  case "erc20TransferFrom": {
19
- return BundlerAction.erc20TransferFrom(...args);
57
+ return BundlerAction.erc20TransferFrom(chainId, ...args);
20
58
  }
21
59
  /* ERC20Wrapper */
22
60
  case "erc20WrapperDepositFor": {
23
- return BundlerAction.erc20WrapperDepositFor(...args);
61
+ return BundlerAction.erc20WrapperDepositFor(chainId, ...args);
24
62
  }
25
63
  case "erc20WrapperWithdrawTo": {
26
- return BundlerAction.erc20WrapperWithdrawTo(...args);
64
+ return BundlerAction.erc20WrapperWithdrawTo(chainId, ...args);
27
65
  }
28
66
  /* Permit */
29
67
  case "permit": {
30
- const [asset, amount, deadline, signature, skipRevert = true] = args;
68
+ const [sender, asset, amount, deadline, signature, spender, skipRevert,] = args;
31
69
  if (signature == null)
32
70
  throw new BundlerErrors.MissingSignature();
33
- return BundlerAction.permit(asset, amount, deadline, signature, skipRevert);
71
+ return BundlerAction.permit(chainId, sender, asset, amount, deadline, signature, spender, skipRevert);
34
72
  }
35
73
  case "permitDai": {
36
- const [nonce, expiry, allowed, signature, skipRevert = true] = args;
74
+ const [sender, nonce, expiry, allowed, signature, spender, skipRevert] = args;
37
75
  if (signature == null)
38
76
  throw new BundlerErrors.MissingSignature();
39
- return BundlerAction.permitDai(nonce, expiry, allowed, signature, skipRevert);
77
+ return BundlerAction.permitDai(chainId, sender, nonce, expiry, allowed, signature, spender, skipRevert);
40
78
  }
41
79
  /* Permit2 */
42
80
  case "approve2": {
43
- const [permitSingle, signature, skipRevert = true] = args;
81
+ const [sender, permitSingle, signature, skipRevert] = args;
44
82
  if (signature == null)
45
83
  throw new BundlerErrors.MissingSignature();
46
- return BundlerAction.approve2(permitSingle, signature, skipRevert);
84
+ return BundlerAction.approve2(chainId, sender, permitSingle, signature, skipRevert);
47
85
  }
48
86
  case "transferFrom2": {
49
- return BundlerAction.transferFrom2(...args);
87
+ return BundlerAction.transferFrom2(chainId, ...args);
50
88
  }
51
89
  /* ERC4626 */
52
90
  case "erc4626Mint": {
53
- return BundlerAction.erc4626Mint(...args);
91
+ return BundlerAction.erc4626Mint(chainId, ...args);
54
92
  }
55
93
  case "erc4626Deposit": {
56
- return BundlerAction.erc4626Deposit(...args);
94
+ return BundlerAction.erc4626Deposit(chainId, ...args);
57
95
  }
58
96
  case "erc4626Withdraw": {
59
- return BundlerAction.erc4626Withdraw(...args);
97
+ return BundlerAction.erc4626Withdraw(chainId, ...args);
60
98
  }
61
99
  case "erc4626Redeem": {
62
- return BundlerAction.erc4626Redeem(...args);
100
+ return BundlerAction.erc4626Redeem(chainId, ...args);
63
101
  }
64
102
  /* Morpho */
65
103
  case "morphoSetAuthorizationWithSig": {
66
- const [authorization, signature, skipRevert = true] = args;
104
+ const [authorization, signature, skipRevert] = args;
67
105
  if (signature == null)
68
106
  throw new BundlerErrors.MissingSignature();
69
- return BundlerAction.morphoSetAuthorizationWithSig(authorization, signature, skipRevert);
107
+ return BundlerAction.morphoSetAuthorizationWithSig(chainId, authorization, signature, skipRevert);
70
108
  }
71
109
  case "morphoSupply": {
72
110
  const [market, assets, shares, slippageAmount, onBehalf, onMorphoSupply,] = args;
73
- return BundlerAction.morphoSupply(market, assets, shares, slippageAmount, onBehalf, onMorphoSupply.map(BundlerAction.encode));
111
+ return BundlerAction.morphoSupply(chainId, market, assets, shares, slippageAmount, onBehalf, onMorphoSupply.flatMap(BundlerAction.encode.bind(null, chainId)));
74
112
  }
75
113
  case "morphoSupplyCollateral": {
76
114
  const [market, amount, onBehalf, onMorphoSupplyCollateral] = args;
77
- return BundlerAction.morphoSupplyCollateral(market, amount, onBehalf, onMorphoSupplyCollateral.map(BundlerAction.encode));
115
+ return BundlerAction.morphoSupplyCollateral(chainId, market, amount, onBehalf, onMorphoSupplyCollateral.flatMap(BundlerAction.encode.bind(null, chainId)));
78
116
  }
79
117
  case "morphoBorrow": {
80
- const [market, assets, shares, slippageAmount, receiver] = args;
81
- return BundlerAction.morphoBorrow(market, assets, shares, slippageAmount, receiver);
118
+ return BundlerAction.morphoBorrow(chainId, ...args);
82
119
  }
83
120
  case "morphoRepay": {
84
121
  const [market, assets, shares, slippageAmount, onBehalf, onMorphoRepay,] = args;
85
- return BundlerAction.morphoRepay(market, assets, shares, slippageAmount, onBehalf, onMorphoRepay.map(BundlerAction.encode));
122
+ return BundlerAction.morphoRepay(chainId, market, assets, shares, slippageAmount, onBehalf, onMorphoRepay.flatMap(BundlerAction.encode.bind(null, chainId)));
86
123
  }
87
124
  case "morphoWithdraw": {
88
- const [market, assets, shares, slippageAmount, receiver] = args;
89
- return BundlerAction.morphoWithdraw(market, assets, shares, slippageAmount, receiver);
125
+ return BundlerAction.morphoWithdraw(chainId, ...args);
90
126
  }
91
127
  case "morphoWithdrawCollateral": {
92
- const [market, amount, receiver] = args;
93
- return BundlerAction.morphoWithdrawCollateral(market, amount, receiver);
128
+ return BundlerAction.morphoWithdrawCollateral(chainId, ...args);
94
129
  }
95
- /* MetaMorpho */
130
+ /* PublicAllocator */
96
131
  case "reallocateTo": {
97
- const [publicAllocator, vault, value, withdrawals, supplyMarket] = args;
98
- return BundlerAction.metaMorphoReallocateTo(publicAllocator, vault, value, withdrawals, supplyMarket);
132
+ return BundlerAction.publicAllocatorReallocateTo(chainId, ...args);
99
133
  }
100
134
  /* Universal Rewards Distributor */
101
135
  case "urdClaim": {
102
- const [distributor, account, reward, amount, proof, skipRevert = true] = args;
103
- return BundlerAction.urdClaim(distributor, account, reward, amount, proof, skipRevert);
136
+ return BundlerAction.urdClaim(...args);
104
137
  }
105
138
  /* Wrapped Native */
106
139
  case "wrapNative": {
107
- return BundlerAction.wrapNative(...args);
140
+ return BundlerAction.wrapNative(chainId, ...args);
108
141
  }
109
142
  case "unwrapNative": {
110
- return BundlerAction.unwrapNative(...args);
143
+ return BundlerAction.unwrapNative(chainId, ...args);
111
144
  }
112
145
  /* stETH */
113
146
  case "stakeEth": {
114
- return BundlerAction.stakeEth(...args);
147
+ return BundlerAction.stakeEth(chainId, ...args);
115
148
  }
116
149
  /* Wrapped stETH */
117
150
  case "wrapStEth": {
118
- return BundlerAction.wrapStEth(...args);
151
+ return BundlerAction.wrapStEth(chainId, ...args);
119
152
  }
120
153
  case "unwrapStEth": {
121
- return BundlerAction.unwrapStEth(...args);
154
+ return BundlerAction.unwrapStEth(chainId, ...args);
122
155
  }
123
156
  /* AaveV2 */
124
157
  case "aaveV2Repay": {
125
- const [asset, amount, rateMode = 1n] = args;
126
- return BundlerAction.aaveV2Repay(asset, amount, rateMode);
158
+ return BundlerAction.aaveV2Repay(chainId, ...args);
127
159
  }
128
160
  case "aaveV2Withdraw": {
129
- return BundlerAction.aaveV2Withdraw(...args);
161
+ return BundlerAction.aaveV2Withdraw(chainId, ...args);
130
162
  }
131
163
  /* AaveV3 */
132
164
  case "aaveV3Repay": {
133
- const [asset, amount, rateMode = 1n] = args;
134
- return BundlerAction.aaveV3Repay(asset, amount, rateMode);
165
+ return BundlerAction.aaveV3Repay(chainId, ...args);
135
166
  }
136
167
  case "aaveV3Withdraw": {
137
- return BundlerAction.aaveV3Withdraw(...args);
168
+ return BundlerAction.aaveV3Withdraw(chainId, ...args);
138
169
  }
139
170
  /* AaveV3 Optimizer */
140
171
  case "aaveV3OptimizerRepay": {
141
- return BundlerAction.aaveV3OptimizerRepay(...args);
172
+ return BundlerAction.aaveV3OptimizerRepay(chainId, ...args);
142
173
  }
143
174
  case "aaveV3OptimizerWithdraw": {
144
- const [underlying, amount, maxIterations = 4n] = args;
145
- return BundlerAction.aaveV3OptimizerWithdraw(underlying, amount, maxIterations);
175
+ return BundlerAction.aaveV3OptimizerWithdraw(chainId, ...args);
146
176
  }
147
177
  case "aaveV3OptimizerWithdrawCollateral": {
148
- return BundlerAction.aaveV3OptimizerWithdrawCollateral(...args);
178
+ return BundlerAction.aaveV3OptimizerWithdrawCollateral(chainId, ...args);
149
179
  }
150
180
  case "aaveV3OptimizerApproveManagerWithSig": {
151
- const [isApproved, nonce, deadline, signature, skipRevert = true] = args;
181
+ const [owner, isApproved, nonce, deadline, signature, manager, skipRevert,] = args;
152
182
  if (signature == null)
153
183
  throw new BundlerErrors.MissingSignature();
154
- return BundlerAction.aaveV3OptimizerApproveManagerWithSig(isApproved, nonce, deadline, signature, skipRevert);
184
+ return BundlerAction.aaveV3OptimizerApproveManagerWithSig(chainId, owner, isApproved, nonce, deadline, signature, manager, skipRevert);
155
185
  }
156
186
  /* CompoundV2 */
157
187
  case "compoundV2Repay": {
158
- return BundlerAction.compoundV2Repay(...args);
188
+ return BundlerAction.compoundV2Repay(chainId, ...args);
159
189
  }
160
190
  case "compoundV2Redeem": {
161
- return BundlerAction.compoundV2Redeem(...args);
191
+ return BundlerAction.compoundV2Redeem(chainId, ...args);
162
192
  }
163
193
  /* CompoundV3 */
164
194
  case "compoundV3Repay": {
165
- return BundlerAction.compoundV3Repay(...args);
195
+ return BundlerAction.compoundV3Repay(chainId, ...args);
166
196
  }
167
197
  case "compoundV3WithdrawFrom": {
168
- return BundlerAction.compoundV3WithdrawFrom(...args);
198
+ return BundlerAction.compoundV3WithdrawFrom(chainId, ...args);
169
199
  }
170
200
  case "compoundV3AllowBySig": {
171
- const [instance, isAllowed, nonce, expiry, signature, skipRevert = true,] = args;
201
+ const [instance, owner, isAllowed, nonce, expiry, signature, manager, skipRevert,] = args;
172
202
  if (signature == null)
173
203
  throw new BundlerErrors.MissingSignature();
174
- return BundlerAction.compoundV3AllowBySig(instance, isAllowed, nonce, expiry, signature, skipRevert);
204
+ return BundlerAction.compoundV3AllowBySig(chainId, instance, owner, isAllowed, nonce, expiry, signature, manager, skipRevert);
175
205
  }
176
206
  }
177
207
  throw Error(`unhandled action encoding: ${type}`);
@@ -179,215 +209,454 @@ export var BundlerAction;
179
209
  BundlerAction.encode = encode;
180
210
  /* ERC20 */
181
211
  /**
182
- * Encodes a call to the Bundler to transfer native tokens (ETH on ethereum, MATIC on polygon, etc).
212
+ * Encodes a call to the Adapter to transfer native tokens (ETH on ethereum, MATIC on polygon, etc).
213
+ * @param chainId The chain id for which to encode the call.
214
+ * @param owner The owner of native tokens.
183
215
  * @param recipient The address to send native tokens to.
184
216
  * @param amount The amount of native tokens to send (in wei).
185
217
  */
186
- function nativeTransfer(recipient, amount) {
187
- return encodeFunctionData({
188
- abi: transferBundlerAbi,
189
- functionName: "nativeTransfer",
190
- args: [recipient, amount],
191
- });
218
+ function nativeTransfer(chainId, owner, recipient, amount) {
219
+ const { bundler3: { bundler3, generalAdapter1 }, } = getChainAddresses(chainId);
220
+ if (recipient === bundler3)
221
+ return [];
222
+ if (owner === generalAdapter1)
223
+ return [
224
+ {
225
+ to: generalAdapter1,
226
+ data: encodeFunctionData({
227
+ abi: coreAdapterAbi,
228
+ functionName: "nativeTransfer",
229
+ args: [recipient, amount],
230
+ }),
231
+ value: 0n,
232
+ skipRevert: false,
233
+ callbackHash: zeroHash,
234
+ },
235
+ ];
236
+ return [
237
+ {
238
+ to: recipient,
239
+ data: "0x",
240
+ value: amount,
241
+ skipRevert: false,
242
+ callbackHash: zeroHash,
243
+ },
244
+ ];
192
245
  }
193
246
  BundlerAction.nativeTransfer = nativeTransfer;
194
247
  /**
195
- * Encodes a call to the Bundler to transfer ERC20 tokens.
248
+ * Encodes a call to the Adapter to transfer ERC20 tokens.
249
+ * @param chainId The chain id for which to encode the call.
196
250
  * @param asset The address of the ERC20 token to transfer.
197
251
  * @param recipient The address to send tokens to.
198
252
  * @param amount The amount of tokens to send.
199
253
  */
200
- function erc20Transfer(asset, recipient, amount) {
201
- return encodeFunctionData({
202
- abi: transferBundlerAbi,
203
- functionName: "erc20Transfer",
204
- args: [asset, recipient, amount],
205
- });
254
+ function erc20Transfer(chainId, asset, recipient, amount) {
255
+ const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
256
+ return [
257
+ {
258
+ to: generalAdapter1,
259
+ data: encodeFunctionData({
260
+ abi: coreAdapterAbi,
261
+ functionName: "erc20Transfer",
262
+ args: [asset, recipient, amount],
263
+ }),
264
+ value: 0n,
265
+ skipRevert: false,
266
+ callbackHash: zeroHash,
267
+ },
268
+ ];
206
269
  }
207
270
  BundlerAction.erc20Transfer = erc20Transfer;
208
271
  /**
209
- * Encodes a call to the Bundler to transfer ERC20 tokens from the sender to the Bundler.
272
+ * Encodes a call to the Adapter to transfer ERC20 tokens from the sender to the Bundler.
273
+ * @param chainId The chain id for which to encode the call.
210
274
  * @param asset The address of the ERC20 token to transfer.
211
275
  * @param amount The amount of tokens to send.
276
+ * @param recipient The recipient of ERC20 tokens. Defaults to the chain's bundler3 general adapter.
212
277
  */
213
- function erc20TransferFrom(asset, amount) {
214
- return encodeFunctionData({
215
- abi: transferBundlerAbi,
216
- functionName: "erc20TransferFrom",
217
- args: [asset, amount],
218
- });
278
+ function erc20TransferFrom(chainId, asset, amount, recipient) {
279
+ const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
280
+ recipient ??= generalAdapter1;
281
+ return [
282
+ {
283
+ to: generalAdapter1,
284
+ data: encodeFunctionData({
285
+ abi: generalAdapter1Abi,
286
+ functionName: "erc20TransferFrom",
287
+ args: [asset, recipient, amount],
288
+ }),
289
+ value: 0n,
290
+ skipRevert: false,
291
+ callbackHash: zeroHash,
292
+ },
293
+ ];
219
294
  }
220
295
  BundlerAction.erc20TransferFrom = erc20TransferFrom;
221
296
  /* Permit */
222
297
  /**
223
- * Encodes a call to the Bundler to permit an ERC20 token.
298
+ * Encodes a call to the Adapter to permit an ERC20 token.
299
+ * @param chainId The chain id for which to encode the call.
300
+ * @param owner The address which owns the tokens.
224
301
  * @param asset The address of the ERC20 token to permit.
225
302
  * @param amount The amount of tokens to permit.
226
303
  * @param deadline The timestamp until which the signature is valid.
227
304
  * @param signature The Ethers signature to permit the tokens.
305
+ * @param spender The address allowed to spend the tokens.
228
306
  * @param skipRevert Whether to allow the permit to revert without making the whole multicall revert.
229
307
  */
230
- function permit(asset, amount, deadline, signature, skipRevert) {
308
+ function permit(chainId, owner, asset, amount, deadline, signature, spender, skipRevert = true) {
309
+ const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
231
310
  const { r, s, yParity } = parseSignature(signature);
232
- return encodeFunctionData({
233
- abi: permitBundlerAbi,
234
- functionName: "permit",
235
- args: [asset, amount, deadline, yParity + 27, r, s, skipRevert],
236
- });
311
+ spender ??= generalAdapter1;
312
+ return [
313
+ {
314
+ to: asset,
315
+ data: encodeFunctionData({
316
+ abi: erc2612Abi,
317
+ functionName: "permit",
318
+ args: [owner, spender, amount, deadline, yParity + 27, r, s],
319
+ }),
320
+ value: 0n,
321
+ skipRevert,
322
+ callbackHash: zeroHash,
323
+ },
324
+ ];
237
325
  }
238
326
  BundlerAction.permit = permit;
239
327
  /**
240
- * Encodes a call to the Bundler to permit DAI.
328
+ * Encodes a call to the Adapter to permit DAI.
329
+ * @param chainId The chain id for which to encode the call.
330
+ * @param owner The address which owns the tokens.
241
331
  * @param nonce The permit nonce used.
242
332
  * @param expiry The timestamp until which the signature is valid.
243
333
  * @param allowed The amount of DAI to permit.
244
334
  * @param signature The Ethers signature to permit the tokens.
335
+ * @param spender The address allowed to spend the tokens.
245
336
  * @param skipRevert Whether to allow the permit to revert without making the whole multicall revert.
246
337
  */
247
- function permitDai(nonce, expiry, allowed, signature, skipRevert) {
338
+ function permitDai(chainId, owner, nonce, expiry, allowed, signature, spender, skipRevert = true) {
339
+ const { dai, bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
340
+ if (dai == null)
341
+ throw new BundlerErrors.UnexpectedAction("permitDai", chainId);
342
+ spender ??= generalAdapter1;
248
343
  const { r, s, yParity } = parseSignature(signature);
249
- return encodeFunctionData({
250
- abi: ethereumPermitBundlerAbi,
251
- functionName: "permitDai",
252
- args: [nonce, expiry, allowed, yParity + 27, r, s, skipRevert],
253
- });
344
+ return [
345
+ {
346
+ to: dai,
347
+ data: encodeFunctionData({
348
+ abi: [
349
+ {
350
+ constant: false,
351
+ inputs: [
352
+ { name: "holder", type: "address" },
353
+ { name: "spender", type: "address" },
354
+ { name: "nonce", type: "uint256" },
355
+ { name: "expiry", type: "uint256" },
356
+ { name: "allowed", type: "bool" },
357
+ { name: "v", type: "uint8" },
358
+ { name: "r", type: "bytes32" },
359
+ { name: "s", type: "bytes32" },
360
+ ],
361
+ name: "permit",
362
+ outputs: [],
363
+ payable: false,
364
+ stateMutability: "nonpayable",
365
+ type: "function",
366
+ },
367
+ ],
368
+ functionName: "permit",
369
+ args: [owner, spender, nonce, expiry, allowed, yParity + 27, r, s],
370
+ }),
371
+ value: 0n,
372
+ skipRevert,
373
+ callbackHash: zeroHash,
374
+ },
375
+ ];
254
376
  }
255
377
  BundlerAction.permitDai = permitDai;
256
378
  /* Permit2 */
257
379
  /**
258
- * Encodes a call to the Bundler to permit ERC20 tokens via Permit2.
380
+ * Encodes a call to the Adapter to permit ERC20 tokens via Permit2.
381
+ * @param chainId The chain id for which to encode the call.
382
+ * @param owner The owner of ERC20 tokens.
259
383
  * @param permitSingle The permit details to submit to Permit2.
260
384
  * @param signature The Ethers signature to permit the tokens.
261
385
  * @param skipRevert Whether to allow the permit to revert without making the whole multicall revert.
262
386
  */
263
- function approve2(permitSingle, signature, skipRevert) {
264
- return encodeFunctionData({
265
- abi: permit2BundlerAbi,
266
- functionName: "approve2",
267
- args: [permitSingle, signature, skipRevert],
268
- });
387
+ function approve2(chainId, owner, permitSingle, signature, skipRevert = true) {
388
+ const { permit2 } = getChainAddresses(chainId);
389
+ return [
390
+ {
391
+ to: permit2,
392
+ data: encodeFunctionData({
393
+ abi: permit2Abi,
394
+ functionName: "permit",
395
+ args: [owner, permitSingle, signature],
396
+ }),
397
+ value: 0n,
398
+ skipRevert,
399
+ callbackHash: zeroHash,
400
+ },
401
+ ];
269
402
  }
270
403
  BundlerAction.approve2 = approve2;
271
404
  /**
272
- * Encodes a call to the Bundler to transfer ERC20 tokens via Permit2 from the sender to the Bundler.
405
+ * Encodes a call to the Adapter to transfer ERC20 tokens via Permit2 from the sender to the Bundler.
406
+ * @param chainId The chain id for which to encode the call.
273
407
  * @param asset The address of the ERC20 token to transfer.
408
+ * @param owner The owner of ERC20 tokens.
274
409
  * @param amount The amount of tokens to send.
410
+ * @param recipient The recipient of ERC20 tokens. Defaults to the chain's bundler3 general adapter.
275
411
  */
276
- function transferFrom2(asset, amount) {
277
- return encodeFunctionData({
278
- abi: permit2BundlerAbi,
279
- functionName: "transferFrom2",
280
- args: [asset, amount],
281
- });
412
+ function transferFrom2(chainId, asset, owner, amount, recipient) {
413
+ const { permit2, bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
414
+ recipient ??= generalAdapter1;
415
+ return [
416
+ {
417
+ to: permit2,
418
+ data: encodeFunctionData({
419
+ abi: permit2Abi,
420
+ functionName: "transferFrom",
421
+ // TODO: batch all permit2 transfers via transferFrom(AllowanceTransferDetails[] calldata)
422
+ args: [owner, recipient, amount, asset],
423
+ }),
424
+ value: 0n,
425
+ skipRevert: false,
426
+ callbackHash: zeroHash,
427
+ },
428
+ ];
282
429
  }
283
430
  BundlerAction.transferFrom2 = transferFrom2;
284
431
  /* ERC20 Wrapper */
285
432
  /**
286
- * Encodes a call to the Bundler to wrap ERC20 tokens via the provided ERC20Wrapper.
433
+ * Encodes a call to the Adapter to wrap ERC20 tokens via the provided ERC20Wrapper.
434
+ * @param chainId The chain id for which to encode the call.
287
435
  * @param wrapper The address of the ERC20 wrapper token.
436
+ * @param underlying The address of the underlying ERC20 token.
288
437
  * @param amount The amount of tokens to send.
289
438
  */
290
- function erc20WrapperDepositFor(wrapper, amount) {
291
- return encodeFunctionData({
292
- abi: erc20WrapperBundlerAbi,
293
- functionName: "erc20WrapperDepositFor",
294
- args: [wrapper, amount],
295
- });
439
+ function erc20WrapperDepositFor(chainId, wrapper, underlying, amount) {
440
+ const { bundler3: { generalAdapter1, erc20WrapperAdapter }, } = getChainAddresses(chainId);
441
+ if (erc20WrapperAdapter == null)
442
+ throw new BundlerErrors.UnexpectedAction("erc20WrapperDepositFor", chainId);
443
+ return [
444
+ {
445
+ to: generalAdapter1,
446
+ data: encodeFunctionData({
447
+ abi: generalAdapter1Abi,
448
+ functionName: "erc20Transfer",
449
+ args: [underlying, erc20WrapperAdapter, maxUint256],
450
+ }),
451
+ value: 0n,
452
+ skipRevert: false,
453
+ callbackHash: zeroHash,
454
+ },
455
+ {
456
+ to: erc20WrapperAdapter,
457
+ data: encodeFunctionData({
458
+ abi: erc20WrapperAdapterAbi,
459
+ functionName: "erc20WrapperDepositFor",
460
+ args: [wrapper, amount],
461
+ }),
462
+ value: 0n,
463
+ skipRevert: false,
464
+ callbackHash: zeroHash,
465
+ },
466
+ {
467
+ to: erc20WrapperAdapter,
468
+ data: encodeFunctionData({
469
+ abi: erc20WrapperAdapterAbi,
470
+ functionName: "erc20Transfer",
471
+ args: [underlying, generalAdapter1, maxUint256],
472
+ }),
473
+ value: 0n,
474
+ skipRevert: false,
475
+ callbackHash: zeroHash,
476
+ },
477
+ ];
296
478
  }
297
479
  BundlerAction.erc20WrapperDepositFor = erc20WrapperDepositFor;
298
480
  /**
299
- * Encodes a call to the Bundler to unwrap ERC20 tokens from the provided ERC20Wrapper.
481
+ * Encodes a call to the Adapter to unwrap ERC20 tokens from the provided ERC20Wrapper.
482
+ * @param chainId The chain id for which to encode the call.
300
483
  * @param wrapper The address of the ERC20 wrapper token.
301
484
  * @param account The address to send the underlying ERC20 tokens.
302
485
  * @param amount The amount of tokens to send.
303
486
  */
304
- function erc20WrapperWithdrawTo(wrapper, account, amount) {
305
- return encodeFunctionData({
306
- abi: erc20WrapperBundlerAbi,
307
- functionName: "erc20WrapperWithdrawTo",
308
- args: [wrapper, account, amount],
309
- });
487
+ function erc20WrapperWithdrawTo(chainId, wrapper, receiver, amount) {
488
+ const { bundler3: { generalAdapter1, erc20WrapperAdapter }, } = getChainAddresses(chainId);
489
+ if (erc20WrapperAdapter == null)
490
+ throw new BundlerErrors.UnexpectedAction("erc20WrapperWithdrawTo", chainId);
491
+ return [
492
+ {
493
+ to: generalAdapter1,
494
+ data: encodeFunctionData({
495
+ abi: generalAdapter1Abi,
496
+ functionName: "erc20Transfer",
497
+ args: [wrapper, erc20WrapperAdapter, maxUint256],
498
+ }),
499
+ value: 0n,
500
+ skipRevert: false,
501
+ callbackHash: zeroHash,
502
+ },
503
+ {
504
+ to: erc20WrapperAdapter,
505
+ data: encodeFunctionData({
506
+ abi: erc20WrapperAdapterAbi,
507
+ functionName: "erc20WrapperWithdrawTo",
508
+ args: [wrapper, receiver, amount],
509
+ }),
510
+ value: 0n,
511
+ skipRevert: false,
512
+ callbackHash: zeroHash,
513
+ },
514
+ {
515
+ to: erc20WrapperAdapter,
516
+ data: encodeFunctionData({
517
+ abi: erc20WrapperAdapterAbi,
518
+ functionName: "erc20Transfer",
519
+ args: [wrapper, generalAdapter1, maxUint256],
520
+ }),
521
+ value: 0n,
522
+ skipRevert: false,
523
+ callbackHash: zeroHash,
524
+ },
525
+ ];
310
526
  }
311
527
  BundlerAction.erc20WrapperWithdrawTo = erc20WrapperWithdrawTo;
312
528
  /* ERC4626 */
313
529
  /**
314
- * Encodes a call to the Bundler to mint shares of the provided ERC4626 vault.
530
+ * Encodes a call to the Adapter to mint shares of the provided ERC4626 vault.
531
+ * @param chainId The chain id for which to encode the call.
315
532
  * @param erc4626 The address of the ERC4626 vault.
316
533
  * @param shares The amount of shares to mint.
317
- * @param maxAssets The maximum amount of assets to deposit (protects the sender from unexpected slippage).
534
+ * @param maxSharePrice The maximum amount of assets to pay to get 1 share (scaled by RAY).
318
535
  * @param receiver The address to send the shares to.
319
536
  */
320
- function erc4626Mint(erc4626, shares, maxAssets, receiver) {
321
- return encodeFunctionData({
322
- abi: erc4626BundlerAbi,
323
- functionName: "erc4626Mint",
324
- args: [erc4626, shares, maxAssets, receiver],
325
- });
537
+ function erc4626Mint(chainId, erc4626, shares, maxSharePrice, receiver) {
538
+ const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
539
+ return [
540
+ {
541
+ to: generalAdapter1,
542
+ data: encodeFunctionData({
543
+ abi: generalAdapter1Abi,
544
+ functionName: "erc4626Mint",
545
+ args: [erc4626, shares, maxSharePrice, receiver],
546
+ }),
547
+ value: 0n,
548
+ skipRevert: false,
549
+ callbackHash: zeroHash,
550
+ },
551
+ ];
326
552
  }
327
553
  BundlerAction.erc4626Mint = erc4626Mint;
328
554
  /**
329
- * Encodes a call to the Bundler to deposit assets into the provided ERC4626 vault.
555
+ * Encodes a call to the Adapter to deposit assets into the provided ERC4626 vault.
556
+ * @param chainId The chain id for which to encode the call.
330
557
  * @param erc4626 The address of the ERC4626 vault.
331
558
  * @param assets The amount of assets to deposit.
332
- * @param minShares The minimum amount of shares to mint (protects the sender from unexpected slippage).
559
+ * @param maxSharePrice The maximum amount of assets to pay to get 1 share (scaled by RAY).
333
560
  * @param receiver The address to send the shares to.
334
561
  */
335
- function erc4626Deposit(erc4626, assets, minShares, receiver) {
336
- return encodeFunctionData({
337
- abi: erc4626BundlerAbi,
338
- functionName: "erc4626Deposit",
339
- args: [erc4626, assets, minShares, receiver],
340
- });
562
+ function erc4626Deposit(chainId, erc4626, assets, maxSharePrice, receiver) {
563
+ const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
564
+ return [
565
+ {
566
+ to: generalAdapter1,
567
+ data: encodeFunctionData({
568
+ abi: generalAdapter1Abi,
569
+ functionName: "erc4626Deposit",
570
+ args: [erc4626, assets, maxSharePrice, receiver],
571
+ }),
572
+ value: 0n,
573
+ skipRevert: false,
574
+ callbackHash: zeroHash,
575
+ },
576
+ ];
341
577
  }
342
578
  BundlerAction.erc4626Deposit = erc4626Deposit;
343
579
  /**
344
- * Encodes a call to the Bundler to withdraw assets from the provided ERC4626 vault.
580
+ * Encodes a call to the Adapter to withdraw assets from the provided ERC4626 vault.
581
+ * @param chainId The chain id for which to encode the call.
345
582
  * @param erc4626 The address of the ERC4626 vault.
346
583
  * @param assets The amount of assets to withdraw.
347
- * @param maxShares The maximum amount of shares to redeem (protects the sender from unexpected slippage).
584
+ * @param minSharePrice The minimum number of assets to receive per share (scaled by RAY).
348
585
  * @param receiver The address to send the assets to.
586
+ * @param owner The address on behalf of which the assets are withdrawn.
349
587
  */
350
- function erc4626Withdraw(erc4626, assets, maxShares, receiver, owner) {
351
- return encodeFunctionData({
352
- abi: erc4626BundlerAbi,
353
- functionName: "erc4626Withdraw",
354
- args: [erc4626, assets, maxShares, receiver, owner],
355
- });
588
+ function erc4626Withdraw(chainId, erc4626, assets, minSharePrice, receiver, owner) {
589
+ const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
590
+ return [
591
+ {
592
+ to: generalAdapter1,
593
+ data: encodeFunctionData({
594
+ abi: generalAdapter1Abi,
595
+ functionName: "erc4626Withdraw",
596
+ args: [erc4626, assets, minSharePrice, receiver, owner],
597
+ }),
598
+ value: 0n,
599
+ skipRevert: false,
600
+ callbackHash: zeroHash,
601
+ },
602
+ ];
356
603
  }
357
604
  BundlerAction.erc4626Withdraw = erc4626Withdraw;
358
605
  /**
359
- * Encodes a call to the Bundler to redeem shares from the provided ERC4626 vault.
606
+ * Encodes a call to the Adapter to redeem shares from the provided ERC4626 vault.
607
+ * @param chainId The chain id for which to encode the call.
360
608
  * @param erc4626 The address of the ERC4626 vault.
361
609
  * @param shares The amount of shares to redeem.
362
- * @param minAssets The minimum amount of assets to withdraw (protects the sender from unexpected slippage).
610
+ * @param minSharePrice The minimum number of assets to receive per share (scaled by RAY).
363
611
  * @param receiver The address to send the assets to.
612
+ * @param owner The address on behalf of which the assets are withdrawn.
364
613
  */
365
- function erc4626Redeem(erc4626, shares, minAssets, receiver, owner) {
366
- return encodeFunctionData({
367
- abi: erc4626BundlerAbi,
368
- functionName: "erc4626Redeem",
369
- args: [erc4626, shares, minAssets, receiver, owner],
370
- });
614
+ function erc4626Redeem(chainId, erc4626, shares, minSharePrice, receiver, owner) {
615
+ const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
616
+ return [
617
+ {
618
+ to: generalAdapter1,
619
+ data: encodeFunctionData({
620
+ abi: generalAdapter1Abi,
621
+ functionName: "erc4626Redeem",
622
+ args: [erc4626, shares, minSharePrice, receiver, owner],
623
+ }),
624
+ value: 0n,
625
+ skipRevert: false,
626
+ callbackHash: zeroHash,
627
+ },
628
+ ];
371
629
  }
372
630
  BundlerAction.erc4626Redeem = erc4626Redeem;
373
631
  /* Morpho */
374
632
  /**
375
- * Encodes a call to the Bundler to authorize an account on Morpho Blue.
633
+ * Encodes a call to the Adapter to authorize an account on Morpho Blue.
634
+ * @param chainId The chain id for which to encode the call.
376
635
  * @param authorization The authorization details to submit to Morpho Blue.
377
636
  * @param signature The Ethers signature to authorize the account.
378
637
  * @param skipRevert Whether to allow the authorization call to revert without making the whole multicall revert.
379
638
  */
380
- function morphoSetAuthorizationWithSig(authorization, signature, skipRevert) {
639
+ function morphoSetAuthorizationWithSig(chainId, authorization, signature, skipRevert = true) {
640
+ const { morpho } = getChainAddresses(chainId);
381
641
  const { r, s, yParity } = parseSignature(signature);
382
- return encodeFunctionData({
383
- abi: morphoBundlerAbi,
384
- functionName: "morphoSetAuthorizationWithSig",
385
- args: [authorization, { v: yParity + 27, r, s }, skipRevert],
386
- });
642
+ return [
643
+ {
644
+ to: morpho,
645
+ data: encodeFunctionData({
646
+ abi: blueAbi,
647
+ functionName: "setAuthorizationWithSig",
648
+ args: [authorization, { v: yParity + 27, r, s }],
649
+ }),
650
+ value: 0n,
651
+ skipRevert,
652
+ callbackHash: zeroHash,
653
+ },
654
+ ];
387
655
  }
388
656
  BundlerAction.morphoSetAuthorizationWithSig = morphoSetAuthorizationWithSig;
389
657
  /**
390
- * Encodes a call to the Bundler to supply to a Morpho Blue market.
658
+ * Encodes a call to the Adapter to supply to a Morpho Blue market.
659
+ * @param chainId The chain id for which to encode the call.
391
660
  * @param market The market params to supply to.
392
661
  * @param assets The amount of assets to supply.
393
662
  * @param shares The amount of supply shares to mint.
@@ -395,59 +664,85 @@ export var BundlerAction;
395
664
  * @param onBehalf The address to supply on behalf of.
396
665
  * @param callbackCalls The array of calls to execute inside Morpho Blue's `onMorphoSupply` callback.
397
666
  */
398
- function morphoSupply(market, assets, shares, slippageAmount, onBehalf, callbackCalls) {
399
- return encodeFunctionData({
400
- abi: morphoBundlerAbi,
401
- functionName: "morphoSupply",
402
- args: [
403
- market,
404
- assets,
405
- shares,
406
- slippageAmount,
407
- onBehalf,
408
- encodeAbiParameters([{ type: "bytes[]" }], [callbackCalls]),
409
- ],
410
- });
667
+ function morphoSupply(chainId, market, assets, shares, slippageAmount, onBehalf, callbackCalls) {
668
+ const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
669
+ const reenter = callbackCalls.length > 0;
670
+ const reenterData = reenter
671
+ ? encodeAbiParameters(bundler3Abi.find((item) => item.name === "reenter").inputs, [callbackCalls])
672
+ : "0x";
673
+ return [
674
+ {
675
+ to: generalAdapter1,
676
+ data: encodeFunctionData({
677
+ abi: generalAdapter1Abi,
678
+ functionName: "morphoSupply",
679
+ args: [market, assets, shares, slippageAmount, onBehalf, reenterData],
680
+ }),
681
+ value: 0n,
682
+ skipRevert: false,
683
+ callbackHash: reenter ? keccak256(reenterData) : zeroHash,
684
+ },
685
+ ];
411
686
  }
412
687
  BundlerAction.morphoSupply = morphoSupply;
413
688
  /**
414
- * Encodes a call to the Bundler to supply collateral to a Morpho Blue market.
689
+ * Encodes a call to the Adapter to supply collateral to a Morpho Blue market.
690
+ * @param chainId The chain id for which to encode the call.
415
691
  * @param market The market params to supply to.
416
692
  * @param assets The amount of assets to supply.
417
693
  * @param onBehalf The address to supply on behalf of.
418
694
  * @param callbackCalls The array of calls to execute inside Morpho Blue's `onMorphoSupplyCollateral` callback.
419
695
  */
420
- function morphoSupplyCollateral(market, assets, onBehalf, callbackCalls) {
421
- return encodeFunctionData({
422
- abi: morphoBundlerAbi,
423
- functionName: "morphoSupplyCollateral",
424
- args: [
425
- market,
426
- assets,
427
- onBehalf,
428
- encodeAbiParameters([{ type: "bytes[]" }], [callbackCalls]),
429
- ],
430
- });
696
+ function morphoSupplyCollateral(chainId, market, assets, onBehalf, callbackCalls) {
697
+ const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
698
+ const reenter = callbackCalls.length > 0;
699
+ const reenterData = reenter
700
+ ? encodeAbiParameters(bundler3Abi.find((item) => item.name === "reenter").inputs, [callbackCalls])
701
+ : "0x";
702
+ return [
703
+ {
704
+ to: generalAdapter1,
705
+ data: encodeFunctionData({
706
+ abi: generalAdapter1Abi,
707
+ functionName: "morphoSupplyCollateral",
708
+ args: [market, assets, onBehalf, reenterData],
709
+ }),
710
+ value: 0n,
711
+ skipRevert: false,
712
+ callbackHash: reenter ? keccak256(reenterData) : zeroHash,
713
+ },
714
+ ];
431
715
  }
432
716
  BundlerAction.morphoSupplyCollateral = morphoSupplyCollateral;
433
717
  /**
434
- * Encodes a call to the Bundler to borrow from a Morpho Blue market.
718
+ * Encodes a call to the Adapter to borrow from a Morpho Blue market.
719
+ * @param chainId The chain id for which to encode the call.
435
720
  * @param market The market params to borrow from.
436
721
  * @param assets The amount of assets to borrow.
437
722
  * @param shares The amount of borrow shares to mint.
438
723
  * @param slippageAmount The minimum (resp. maximum) amount of assets (resp. borrow shares) to borrow (resp. mint) (protects the sender from unexpected slippage).
439
724
  * @param receiver The address to send borrowed tokens to.
440
725
  */
441
- function morphoBorrow(market, assets, shares, slippageAmount, receiver) {
442
- return encodeFunctionData({
443
- abi: morphoBundlerAbi,
444
- functionName: "morphoBorrow",
445
- args: [market, assets, shares, slippageAmount, receiver],
446
- });
726
+ function morphoBorrow(chainId, market, assets, shares, slippageAmount, receiver) {
727
+ const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
728
+ return [
729
+ {
730
+ to: generalAdapter1,
731
+ data: encodeFunctionData({
732
+ abi: generalAdapter1Abi,
733
+ functionName: "morphoBorrow",
734
+ args: [market, assets, shares, slippageAmount, receiver],
735
+ }),
736
+ value: 0n,
737
+ skipRevert: false,
738
+ callbackHash: zeroHash,
739
+ },
740
+ ];
447
741
  }
448
742
  BundlerAction.morphoBorrow = morphoBorrow;
449
743
  /**
450
- * Encodes a call to the Bundler to repay to a Morpho Blue market.
744
+ * Encodes a call to the Adapter to repay to a Morpho Blue market.
745
+ * @param chainId The chain id for which to encode the call.
451
746
  * @param market The market params to repay to.
452
747
  * @param assets The amount of assets to repay.
453
748
  * @param shares The amount of borrow shares to redeem.
@@ -455,88 +750,138 @@ export var BundlerAction;
455
750
  * @param onBehalf The address to repay on behalf of.
456
751
  * @param callbackCalls The array of calls to execute inside Morpho Blue's `onMorphoSupply` callback.
457
752
  */
458
- function morphoRepay(market, assets, shares, slippageAmount, onBehalf, callbackCalls) {
459
- return encodeFunctionData({
460
- abi: morphoBundlerAbi,
461
- functionName: "morphoRepay",
462
- args: [
463
- market,
464
- assets,
465
- shares,
466
- slippageAmount,
467
- onBehalf,
468
- encodeAbiParameters([{ type: "bytes[]" }], [callbackCalls]),
469
- ],
470
- });
753
+ function morphoRepay(chainId, market, assets, shares, slippageAmount, onBehalf, callbackCalls) {
754
+ const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
755
+ const reenter = callbackCalls.length > 0;
756
+ const reenterData = reenter
757
+ ? encodeAbiParameters(bundler3Abi.find((item) => item.name === "reenter").inputs, [callbackCalls])
758
+ : "0x";
759
+ return [
760
+ {
761
+ to: generalAdapter1,
762
+ data: encodeFunctionData({
763
+ abi: generalAdapter1Abi,
764
+ functionName: "morphoRepay",
765
+ args: [market, assets, shares, slippageAmount, onBehalf, reenterData],
766
+ }),
767
+ value: 0n,
768
+ skipRevert: false,
769
+ callbackHash: reenter ? keccak256(reenterData) : zeroHash,
770
+ },
771
+ ];
471
772
  }
472
773
  BundlerAction.morphoRepay = morphoRepay;
473
774
  /**
474
- * Encodes a call to the Bundler to withdraw from a Morpho Blue market.
775
+ * Encodes a call to the Adapter to withdraw from a Morpho Blue market.
776
+ * @param chainId The chain id for which to encode the call.
475
777
  * @param market The market params to withdraw from.
476
778
  * @param assets The amount of assets to withdraw.
477
779
  * @param shares The amount of supply shares to redeem.
478
780
  * @param slippageAmount The minimum (resp. maximum) amount of assets (resp. supply shares) to withdraw (resp. redeem) (protects the sender from unexpected slippage).
479
781
  * @param receiver The address to send withdrawn tokens to.
480
782
  */
481
- function morphoWithdraw(market, assets, shares, slippageAmount, receiver) {
482
- return encodeFunctionData({
483
- abi: morphoBundlerAbi,
484
- functionName: "morphoWithdraw",
485
- args: [market, assets, shares, slippageAmount, receiver],
486
- });
783
+ function morphoWithdraw(chainId, market, assets, shares, slippageAmount, receiver) {
784
+ const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
785
+ return [
786
+ {
787
+ to: generalAdapter1,
788
+ data: encodeFunctionData({
789
+ abi: generalAdapter1Abi,
790
+ functionName: "morphoWithdraw",
791
+ args: [market, assets, shares, slippageAmount, receiver],
792
+ }),
793
+ value: 0n,
794
+ skipRevert: false,
795
+ callbackHash: zeroHash,
796
+ },
797
+ ];
487
798
  }
488
799
  BundlerAction.morphoWithdraw = morphoWithdraw;
489
800
  /**
490
- * Encodes a call to the Bundler to withdraw collateral from a Morpho Blue market.
801
+ * Encodes a call to the Adapter to withdraw collateral from a Morpho Blue market.
802
+ * @param chainId The chain id for which to encode the call.
491
803
  * @param market The market params to withdraw from.
492
804
  * @param assets The amount of assets to withdraw.
493
805
  * @param receiver The address to send withdrawn tokens to.
494
806
  */
495
- function morphoWithdrawCollateral(market, assets, receiver) {
496
- return encodeFunctionData({
497
- abi: morphoBundlerAbi,
498
- functionName: "morphoWithdrawCollateral",
499
- args: [market, assets, receiver],
500
- });
807
+ function morphoWithdrawCollateral(chainId, market, assets, receiver) {
808
+ const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
809
+ return [
810
+ {
811
+ to: generalAdapter1,
812
+ data: encodeFunctionData({
813
+ abi: generalAdapter1Abi,
814
+ functionName: "morphoWithdrawCollateral",
815
+ args: [market, assets, receiver],
816
+ }),
817
+ value: 0n,
818
+ skipRevert: false,
819
+ callbackHash: zeroHash,
820
+ },
821
+ ];
501
822
  }
502
823
  BundlerAction.morphoWithdrawCollateral = morphoWithdrawCollateral;
503
824
  /**
504
- * Encodes a call to the Bundler to flash loan from Morpho Blue.
825
+ * Encodes a call to the Adapter to flash loan from Morpho Blue.
826
+ * @param chainId The chain id for which to encode the call.
505
827
  * @param asset The address of the ERC20 token to flash loan.
506
828
  * @param amount The amount of tokens to flash loan.
507
829
  * @param callbackCalls The array of calls to execute inside Morpho Blue's `onMorphoFlashLoan` callback.
508
830
  */
509
- function morphoFlashLoan(asset, amount, callbackCalls) {
510
- return encodeFunctionData({
511
- abi: morphoBundlerAbi,
512
- functionName: "morphoFlashLoan",
513
- args: [
514
- asset,
515
- amount,
516
- encodeAbiParameters([{ type: "bytes[]" }], [callbackCalls]),
517
- ],
518
- });
831
+ function morphoFlashLoan(chainId, asset, amount, callbackCalls) {
832
+ const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
833
+ return [
834
+ {
835
+ to: generalAdapter1,
836
+ data: encodeFunctionData({
837
+ abi: generalAdapter1Abi,
838
+ functionName: "morphoFlashLoan",
839
+ args: [
840
+ asset,
841
+ amount,
842
+ encodeFunctionData({
843
+ abi: bundler3Abi,
844
+ functionName: "reenter",
845
+ args: [callbackCalls],
846
+ }),
847
+ ],
848
+ }),
849
+ value: 0n,
850
+ skipRevert: false,
851
+ callbackHash: keccak256(`${generalAdapter1}${reenterSelectorHash}`),
852
+ },
853
+ ];
519
854
  }
520
855
  BundlerAction.morphoFlashLoan = morphoFlashLoan;
521
856
  /**
522
- * Encodes a call to the Bundler to trigger a public reallocation on the PublicAllocator.
523
- * @param publicAllocator The address of the PublicAllocator to use.
857
+ * Encodes a call to the Adapter to trigger a public reallocation on the PublicAllocator.
858
+ * @param chainId The chain id for which to encode the call.
524
859
  * @param vault The vault to reallocate.
525
- * @param value The value of the call. Can be used to pay the vault reallocation fees.
860
+ * @param fee The vault public reallocation fee.
526
861
  * @param withdrawals The array of withdrawals to perform, before supplying everything to the supply market.
527
862
  * @param supplyMarketParams The market params to reallocate to.
528
863
  */
529
- function metaMorphoReallocateTo(publicAllocator, vault, value, withdrawals, supplyMarketParams) {
530
- return encodeFunctionData({
531
- abi: morphoBundlerAbi,
532
- functionName: "reallocateTo",
533
- args: [publicAllocator, vault, value, withdrawals, supplyMarketParams],
534
- });
864
+ function publicAllocatorReallocateTo(chainId, vault, fee, withdrawals, supplyMarketParams) {
865
+ const { publicAllocator } = getChainAddresses(chainId);
866
+ return [
867
+ {
868
+ to: publicAllocator,
869
+ data: encodeFunctionData({
870
+ abi: publicAllocatorAbi,
871
+ functionName: "reallocateTo",
872
+ args: [vault, withdrawals, supplyMarketParams],
873
+ }),
874
+ value: fee,
875
+ skipRevert: false,
876
+ callbackHash: zeroHash,
877
+ },
878
+ ];
535
879
  }
536
- BundlerAction.metaMorphoReallocateTo = metaMorphoReallocateTo;
880
+ BundlerAction.publicAllocatorReallocateTo = publicAllocatorReallocateTo;
537
881
  /* Universal Rewards Distributor */
538
882
  /**
539
- * Encodes a call to the Bundler to claim rewards from the Universal Rewards Distributor.
883
+ * Encodes a call to the Adapter to claim rewards from the Universal Rewards Distributor.
884
+ * @param chainId The chain id for which to encode the call.
540
885
  * @param distributor The address of the distributor to claim rewards from.
541
886
  * @param account The address to claim rewards for.
542
887
  * @param reward The address of the reward token to claim.
@@ -544,292 +889,576 @@ export var BundlerAction;
544
889
  * @param proof The Merkle proof to claim the rewards.
545
890
  * @param skipRevert Whether to allow the claim to revert without making the whole multicall revert.
546
891
  */
547
- function urdClaim(distributor, account, reward, amount, proof, skipRevert) {
548
- return encodeFunctionData({
549
- abi: urdBundlerAbi,
550
- functionName: "urdClaim",
551
- args: [distributor, account, reward, amount, proof, skipRevert],
552
- });
892
+ function urdClaim(distributor, account, reward, amount, proof, skipRevert = true) {
893
+ return [
894
+ {
895
+ to: distributor,
896
+ data: encodeFunctionData({
897
+ abi: universalRewardsDistributorAbi,
898
+ functionName: "claim",
899
+ args: [account, reward, amount, proof],
900
+ }),
901
+ value: 0n,
902
+ skipRevert,
903
+ callbackHash: zeroHash,
904
+ },
905
+ ];
553
906
  }
554
907
  BundlerAction.urdClaim = urdClaim;
555
908
  /* Wrapped Native */
556
909
  /**
557
- * Encodes a call to the Bundler to wrap native tokens (ETH to WETH on ethereum, MATIC to WMATIC on polygon, etc).
910
+ * Encodes a call to the Adapter to wrap native tokens (ETH to WETH on ethereum, MATIC to WMATIC on polygon, etc).
911
+ * @param chainId The chain id for which to encode the call.
558
912
  * @param amount The amount of native tokens to wrap (in wei).
913
+ * @param recipient The address to send tokens to. Defaults to the chain's bundler3 general adapter.
559
914
  */
560
- function wrapNative(amount) {
561
- return encodeFunctionData({
562
- abi: wNativeBundlerAbi,
563
- functionName: "wrapNative",
564
- args: [amount],
565
- });
915
+ function wrapNative(chainId, amount, recipient) {
916
+ const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
917
+ recipient ??= generalAdapter1;
918
+ return [
919
+ {
920
+ to: generalAdapter1,
921
+ data: encodeFunctionData({
922
+ abi: generalAdapter1Abi,
923
+ functionName: "wrapNative",
924
+ args: [amount, recipient],
925
+ }),
926
+ value: 0n,
927
+ skipRevert: false,
928
+ callbackHash: zeroHash,
929
+ },
930
+ ];
566
931
  }
567
932
  BundlerAction.wrapNative = wrapNative;
568
933
  /**
569
- * Encodes a call to the Bundler to unwrap native tokens (WETH to ETH on ethereum, WMATIC to MATIC on polygon, etc).
934
+ * Encodes a call to the Adapter to unwrap native tokens (WETH to ETH on ethereum, WMATIC to MATIC on polygon, etc).
935
+ * @param chainId The chain id for which to encode the call.
570
936
  * @param amount The amount of native tokens to unwrap (in wei).
937
+ * @param recipient The address to send tokens to. Defaults to the chain's bundler3 general adapter.
571
938
  */
572
- function unwrapNative(amount) {
573
- return encodeFunctionData({
574
- abi: wNativeBundlerAbi,
575
- functionName: "unwrapNative",
576
- args: [amount],
577
- });
939
+ function unwrapNative(chainId, amount, recipient) {
940
+ const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
941
+ recipient ??= generalAdapter1;
942
+ return [
943
+ {
944
+ to: generalAdapter1,
945
+ data: encodeFunctionData({
946
+ abi: generalAdapter1Abi,
947
+ functionName: "unwrapNative",
948
+ args: [amount, recipient],
949
+ }),
950
+ value: 0n,
951
+ skipRevert: false,
952
+ callbackHash: zeroHash,
953
+ },
954
+ ];
578
955
  }
579
956
  BundlerAction.unwrapNative = unwrapNative;
580
957
  /* stETH */
581
958
  /**
582
- * Encodes a call to the Bundler to stake native tokens using Lido (ETH to stETH on ethereum).
959
+ * Encodes a call to the Adapter to stake native tokens using Lido (ETH to stETH on ethereum).
960
+ * @param chainId The chain id for which to encode the call.
583
961
  * @param amount The amount of native tokens to stake (in wei).
584
- * @param minShares The minimum amount of shares to mint (protects the sender from unexpected slippage).
962
+ * @param maxSharePrice The maximum amount of wei to pay for minting 1 share (scaled by RAY).
585
963
  * @param referral The referral address to use.
964
+ * @param recipient The address to send stETH to. Defaults to the chain's bundler3 general adapter.
586
965
  */
587
- function stakeEth(amount, minShares, referral) {
588
- return encodeFunctionData({
589
- abi: stEthBundlerAbi,
590
- functionName: "stakeEth",
591
- args: [amount, minShares, referral],
592
- });
966
+ function stakeEth(chainId, amount, maxSharePrice, referral, recipient) {
967
+ const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
968
+ recipient ??= generalAdapter1;
969
+ return [
970
+ {
971
+ to: generalAdapter1,
972
+ data: encodeFunctionData({
973
+ abi: ethereumGeneralAdapter1Abi,
974
+ functionName: "stakeEth",
975
+ args: [amount, maxSharePrice, referral, recipient],
976
+ }),
977
+ value: 0n,
978
+ skipRevert: false,
979
+ callbackHash: zeroHash,
980
+ },
981
+ ];
593
982
  }
594
983
  BundlerAction.stakeEth = stakeEth;
595
984
  /* Wrapped stETH */
596
985
  /**
597
- * Encodes a call to the Bundler to wrap stETH (stETH to wstETH on ethereum).
986
+ * Encodes a call to the Adapter to wrap stETH (stETH to wstETH on ethereum).
987
+ * @param chainId The chain id for which to encode the call.
598
988
  * @param amount The amount of stETH to wrap (in wei).
989
+ * @param recipient The address to send wstETH to. Defaults to the chain's bundler3 general adapter.
599
990
  */
600
- function wrapStEth(amount) {
601
- return encodeFunctionData({
602
- abi: stEthBundlerAbi,
603
- functionName: "wrapStEth",
604
- args: [amount],
605
- });
991
+ function wrapStEth(chainId, amount, recipient) {
992
+ const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
993
+ recipient ??= generalAdapter1;
994
+ return [
995
+ {
996
+ to: generalAdapter1,
997
+ data: encodeFunctionData({
998
+ abi: ethereumGeneralAdapter1Abi,
999
+ functionName: "wrapStEth",
1000
+ args: [amount, recipient],
1001
+ }),
1002
+ value: 0n,
1003
+ skipRevert: false,
1004
+ callbackHash: zeroHash,
1005
+ },
1006
+ ];
606
1007
  }
607
1008
  BundlerAction.wrapStEth = wrapStEth;
608
1009
  /**
609
- * Encodes a call to the Bundler to unwrap wstETH (wstETH to stETH on ethereum).
1010
+ * Encodes a call to the Adapter to unwrap wstETH (wstETH to stETH on ethereum).
1011
+ * @param chainId The chain id for which to encode the call.
610
1012
  * @param amount The amount of wstETH to unwrap (in wei).
1013
+ * @param recipient The address to send stETH to. Defaults to the chain's bundler3 general adapter.
611
1014
  */
612
- function unwrapStEth(amount) {
613
- return encodeFunctionData({
614
- abi: stEthBundlerAbi,
615
- functionName: "unwrapStEth",
616
- args: [amount],
617
- });
1015
+ function unwrapStEth(chainId, amount, recipient) {
1016
+ const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
1017
+ recipient ??= generalAdapter1;
1018
+ return [
1019
+ {
1020
+ to: generalAdapter1,
1021
+ data: encodeFunctionData({
1022
+ abi: ethereumGeneralAdapter1Abi,
1023
+ functionName: "unwrapStEth",
1024
+ args: [amount, recipient],
1025
+ }),
1026
+ value: 0n,
1027
+ skipRevert: false,
1028
+ callbackHash: zeroHash,
1029
+ },
1030
+ ];
618
1031
  }
619
1032
  BundlerAction.unwrapStEth = unwrapStEth;
620
1033
  /* AaveV2 */
621
1034
  /**
622
- * ! Only available on AaveV2MigrationBundler instances (not the main Bundler contract!).
623
- * Encodes a call to the Bundler to repay a debt on AaveV2.
1035
+ * Encodes a call to the Adapter to repay a debt on AaveV2.
1036
+ * @param chainId The chain id for which to encode the call.
624
1037
  * @param asset The debt asset to repay.
625
1038
  * @param amount The amount of debt to repay.
1039
+ * @param onBehalf The address on behalf of which to repay.
626
1040
  * @param rateMode The interest rate mode used by the debt to repay.
627
1041
  */
628
- function aaveV2Repay(asset, amount, rateMode) {
629
- return encodeFunctionData({
630
- abi: aaveV2MigrationBundlerAbi,
631
- functionName: "aaveV2Repay",
632
- args: [asset, amount, rateMode],
633
- });
1042
+ function aaveV2Repay(chainId, asset, amount, onBehalf, rateMode = 1n) {
1043
+ const { bundler3: { aaveV2MigrationAdapter }, } = getChainAddresses(chainId);
1044
+ if (aaveV2MigrationAdapter == null)
1045
+ throw new BundlerErrors.UnexpectedAction("aaveV2Repay", chainId);
1046
+ return [
1047
+ {
1048
+ to: aaveV2MigrationAdapter,
1049
+ data: encodeFunctionData({
1050
+ abi: aaveV2MigrationAdapterAbi,
1051
+ functionName: "aaveV2Repay",
1052
+ args: [asset, amount, rateMode, onBehalf],
1053
+ }),
1054
+ value: 0n,
1055
+ skipRevert: false,
1056
+ callbackHash: zeroHash,
1057
+ },
1058
+ ];
634
1059
  }
635
1060
  BundlerAction.aaveV2Repay = aaveV2Repay;
636
1061
  /**
637
- * ! Only available on AaveV2MigrationBundler instances (not the main Bundler contract!).
638
- * Encodes a call to the Bundler to withdrawn from AaveV2.
1062
+ * Encodes a call to the Adapter to withdrawn from AaveV2.
1063
+ * @param chainId The chain id for which to encode the call.
639
1064
  * @param asset The asset to withdraw.
640
1065
  * @param amount The amount of asset to withdraw.
1066
+ * @param recipient The recipient of ERC20 tokens. Defaults to the chain's bundler3 general adapter.
641
1067
  */
642
- function aaveV2Withdraw(asset, amount) {
643
- return encodeFunctionData({
644
- abi: aaveV2MigrationBundlerAbi,
645
- functionName: "aaveV2Withdraw",
646
- args: [asset, amount],
647
- });
1068
+ function aaveV2Withdraw(chainId, asset, amount, recipient) {
1069
+ const { bundler3: { aaveV2MigrationAdapter }, } = getChainAddresses(chainId);
1070
+ if (aaveV2MigrationAdapter == null)
1071
+ throw new BundlerErrors.UnexpectedAction("aaveV2Withdraw", chainId);
1072
+ recipient ??= aaveV2MigrationAdapter;
1073
+ return [
1074
+ {
1075
+ to: aaveV2MigrationAdapter,
1076
+ data: encodeFunctionData({
1077
+ abi: aaveV2MigrationAdapterAbi,
1078
+ functionName: "aaveV2Withdraw",
1079
+ args: [asset, amount, recipient],
1080
+ }),
1081
+ value: 0n,
1082
+ skipRevert: false,
1083
+ callbackHash: zeroHash,
1084
+ },
1085
+ ];
648
1086
  }
649
1087
  BundlerAction.aaveV2Withdraw = aaveV2Withdraw;
650
1088
  /* AaveV3 */
651
1089
  /**
652
- * ! Only available on AaveV3MigrationBundler instances (not the main Bundler contract!).
653
- * Encodes a call to the Bundler to repay a debt on AaveV3.
1090
+ * Encodes a call to the Adapter to repay a debt on AaveV3.
1091
+ * @param chainId The chain id for which to encode the call.
654
1092
  * @param asset The debt asset to repay.
655
1093
  * @param amount The amount of debt to repay.
1094
+ * @param onBehalf The address on behalf of which to repay.
656
1095
  * @param rateMode The interest rate mode used by the debt to repay.
657
1096
  */
658
- function aaveV3Repay(asset, amount, rateMode) {
659
- return encodeFunctionData({
660
- abi: aaveV3MigrationBundlerAbi,
661
- functionName: "aaveV3Repay",
662
- args: [asset, amount, rateMode],
663
- });
1097
+ function aaveV3Repay(chainId, asset, amount, onBehalf, rateMode = 1n) {
1098
+ const { bundler3: { aaveV3CoreMigrationAdapter }, // TODO: choose between core & prime
1099
+ } = getChainAddresses(chainId);
1100
+ if (aaveV3CoreMigrationAdapter == null)
1101
+ throw new BundlerErrors.UnexpectedAction("aaveV3Repay", chainId);
1102
+ return [
1103
+ {
1104
+ to: aaveV3CoreMigrationAdapter,
1105
+ data: encodeFunctionData({
1106
+ abi: aaveV3MigrationAdapterAbi,
1107
+ functionName: "aaveV3Repay",
1108
+ args: [asset, amount, rateMode, onBehalf],
1109
+ }),
1110
+ value: 0n,
1111
+ skipRevert: false,
1112
+ callbackHash: zeroHash,
1113
+ },
1114
+ ];
664
1115
  }
665
1116
  BundlerAction.aaveV3Repay = aaveV3Repay;
666
1117
  /**
667
- * ! Only available on AaveV3MigrationBundler instances (not the main Bundler contract!).
668
- * Encodes a call to the Bundler to withdrawn from AaveV3.
1118
+ * Encodes a call to the Adapter to withdrawn from AaveV3.
1119
+ * @param chainId The chain id for which to encode the call.
669
1120
  * @param asset The asset to withdraw.
670
1121
  * @param amount The amount of asset to withdraw.
1122
+ * @param recipient The recipient of ERC20 tokens. Defaults to the chain's bundler3 general adapter.
671
1123
  */
672
- function aaveV3Withdraw(asset, amount) {
673
- return encodeFunctionData({
674
- abi: aaveV3MigrationBundlerAbi,
675
- functionName: "aaveV3Withdraw",
676
- args: [asset, amount],
677
- });
1124
+ function aaveV3Withdraw(chainId, asset, amount, recipient) {
1125
+ const { bundler3: { aaveV3CoreMigrationAdapter }, // TODO: choose between core & prime
1126
+ } = getChainAddresses(chainId);
1127
+ if (aaveV3CoreMigrationAdapter == null)
1128
+ throw new BundlerErrors.UnexpectedAction("aaveV3Withdraw", chainId);
1129
+ recipient ??= aaveV3CoreMigrationAdapter;
1130
+ return [
1131
+ {
1132
+ to: aaveV3CoreMigrationAdapter,
1133
+ data: encodeFunctionData({
1134
+ abi: aaveV3MigrationAdapterAbi,
1135
+ functionName: "aaveV3Withdraw",
1136
+ args: [asset, amount, recipient],
1137
+ }),
1138
+ value: 0n,
1139
+ skipRevert: false,
1140
+ callbackHash: zeroHash,
1141
+ },
1142
+ ];
678
1143
  }
679
1144
  BundlerAction.aaveV3Withdraw = aaveV3Withdraw;
680
1145
  /* AaveV3 Optimizer */
681
1146
  /**
682
- * ! Only available on AaveV3OptimizerMigrationBundler instances (not the main Bundler contract!).
683
- * Encodes a call to the Bundler to repay a debt on Morpho's AaveV3Optimizer.
1147
+ * Encodes a call to the Adapter to repay a debt on Morpho's AaveV3Optimizer.
1148
+ * @param chainId The chain id for which to encode the call.
684
1149
  * @param underlying The underlying debt asset to repay.
685
1150
  * @param amount The amount of debt to repay.
686
- * @param maxIterations The maximum amount of iterations to use for the repayment.
1151
+ * @param onBehalf The address on behalf of which to repay.
687
1152
  */
688
- function aaveV3OptimizerRepay(underlying, amount) {
689
- return encodeFunctionData({
690
- abi: aaveV3OptimizerMigrationBundlerAbi,
691
- functionName: "aaveV3OptimizerRepay",
692
- args: [underlying, amount],
693
- });
1153
+ function aaveV3OptimizerRepay(chainId, underlying, amount, onBehalf) {
1154
+ const { bundler3: { aaveV3OptimizerMigrationAdapter }, } = getChainAddresses(chainId);
1155
+ if (aaveV3OptimizerMigrationAdapter == null)
1156
+ throw new BundlerErrors.UnexpectedAction("aaveV3OptimizerRepay", chainId);
1157
+ return [
1158
+ {
1159
+ to: aaveV3OptimizerMigrationAdapter,
1160
+ data: encodeFunctionData({
1161
+ abi: aaveV3OptimizerMigrationAdapterAbi,
1162
+ functionName: "aaveV3OptimizerRepay",
1163
+ args: [underlying, amount, onBehalf],
1164
+ }),
1165
+ value: 0n,
1166
+ skipRevert: false,
1167
+ callbackHash: zeroHash,
1168
+ },
1169
+ ];
694
1170
  }
695
1171
  BundlerAction.aaveV3OptimizerRepay = aaveV3OptimizerRepay;
696
1172
  /**
697
- * ! Only available on AaveV3OptimizerMigrationBundler instances (not the main Bundler contract!).
698
- * Encodes a call to the Bundler to withdraw from Morpho's AaveV3Optimizer.
1173
+ * Encodes a call to the Adapter to withdraw from Morpho's AaveV3Optimizer.
1174
+ * @param chainId The chain id for which to encode the call.
699
1175
  * @param underlying The underlying asset to withdraw.
700
1176
  * @param amount The amount to withdraw.
701
1177
  * @param maxIterations The maximum amount of iterations to use for the withdrawal.
1178
+ * @param recipient The recipient of ERC20 tokens. Defaults to the chain's bundler3 general adapter.
702
1179
  */
703
- function aaveV3OptimizerWithdraw(underlying, amount, maxIterations) {
704
- return encodeFunctionData({
705
- abi: aaveV3OptimizerMigrationBundlerAbi,
706
- functionName: "aaveV3OptimizerWithdraw",
707
- args: [underlying, amount, maxIterations],
708
- });
1180
+ function aaveV3OptimizerWithdraw(chainId, underlying, amount, maxIterations, recipient) {
1181
+ const { bundler3: { aaveV3OptimizerMigrationAdapter }, } = getChainAddresses(chainId);
1182
+ if (aaveV3OptimizerMigrationAdapter == null)
1183
+ throw new BundlerErrors.UnexpectedAction("aaveV3OptimizerWithdraw", chainId);
1184
+ recipient ??= aaveV3OptimizerMigrationAdapter;
1185
+ return [
1186
+ {
1187
+ to: aaveV3OptimizerMigrationAdapter,
1188
+ data: encodeFunctionData({
1189
+ abi: aaveV3OptimizerMigrationAdapterAbi,
1190
+ functionName: "aaveV3OptimizerWithdraw",
1191
+ args: [underlying, amount, maxIterations, recipient],
1192
+ }),
1193
+ value: 0n,
1194
+ skipRevert: false,
1195
+ callbackHash: zeroHash,
1196
+ },
1197
+ ];
709
1198
  }
710
1199
  BundlerAction.aaveV3OptimizerWithdraw = aaveV3OptimizerWithdraw;
711
1200
  /**
712
- * ! Only available on AaveV3OptimizerMigrationBundler instances (not the main Bundler contract!).
713
- * Encodes a call to the Bundler to withdraw collateral from Morpho's AaveV3Optimizer.
1201
+ * Encodes a call to the Adapter to withdraw collateral from Morpho's AaveV3Optimizer.
1202
+ * @param chainId The chain id for which to encode the call.
714
1203
  * @param underlying The underlying asset to withdraw.
715
1204
  * @param amount The amount to withdraw.
1205
+ * @param recipient The recipient of ERC20 tokens. Defaults to the chain's bundler3 general adapter.
716
1206
  */
717
- function aaveV3OptimizerWithdrawCollateral(underlying, amount) {
718
- return encodeFunctionData({
719
- abi: aaveV3OptimizerMigrationBundlerAbi,
720
- functionName: "aaveV3OptimizerWithdrawCollateral",
721
- args: [underlying, amount],
722
- });
1207
+ function aaveV3OptimizerWithdrawCollateral(chainId, underlying, amount, recipient) {
1208
+ const { bundler3: { aaveV3OptimizerMigrationAdapter }, } = getChainAddresses(chainId);
1209
+ if (aaveV3OptimizerMigrationAdapter == null)
1210
+ throw new BundlerErrors.UnexpectedAction("aaveV3OptimizerWithdrawCollateral", chainId);
1211
+ recipient ??= aaveV3OptimizerMigrationAdapter;
1212
+ return [
1213
+ {
1214
+ to: aaveV3OptimizerMigrationAdapter,
1215
+ data: encodeFunctionData({
1216
+ abi: aaveV3OptimizerMigrationAdapterAbi,
1217
+ functionName: "aaveV3OptimizerWithdrawCollateral",
1218
+ args: [underlying, amount, recipient],
1219
+ }),
1220
+ value: 0n,
1221
+ skipRevert: false,
1222
+ callbackHash: zeroHash,
1223
+ },
1224
+ ];
723
1225
  }
724
1226
  BundlerAction.aaveV3OptimizerWithdrawCollateral = aaveV3OptimizerWithdrawCollateral;
725
1227
  /**
726
- * ! Only available on AaveV3OptimizerMigrationBundler instances (not the main Bundler contract!).
727
- * Encodes a call to the Bundler to approve the Bundler as the sender's manager on Morpho's AaveV3Optimizer.
1228
+ * Encodes a call to the Adapter to approve the Bundler as the sender's manager on Morpho's AaveV3Optimizer.
1229
+ * @param chainId The chain id for which to encode the call.
1230
+ * @param owner The owner of the AaveV3Optimizer position.
728
1231
  * @param isApproved Whether the manager is approved.
729
1232
  * @param nonce The nonce used to sign.
730
1233
  * @param deadline The timestamp until which the signature is valid.
731
1234
  * @param signature The Ethers signature to submit.
1235
+ * @param manager The address of the manager to approve. Defaults to the chain's bundler3 AaveV3OptimizerMigrationAdapter.
732
1236
  * @param skipRevert Whether to allow the signature to revert without making the whole multicall revert.
733
1237
  */
734
- function aaveV3OptimizerApproveManagerWithSig(isApproved, nonce, deadline, signature, skipRevert) {
1238
+ function aaveV3OptimizerApproveManagerWithSig(chainId, owner, isApproved, nonce, deadline, signature, manager, skipRevert = true) {
1239
+ const { aaveV3Optimizer, bundler3: { aaveV3OptimizerMigrationAdapter }, } = getChainAddresses(chainId);
1240
+ if (aaveV3Optimizer == null || aaveV3OptimizerMigrationAdapter == null)
1241
+ throw new BundlerErrors.UnexpectedAction("aaveV3OptimizerApproveManagerWithSig", chainId);
1242
+ manager ??= aaveV3OptimizerMigrationAdapter;
735
1243
  const { r, s, yParity } = parseSignature(signature);
736
- return encodeFunctionData({
737
- abi: aaveV3OptimizerMigrationBundlerAbi,
738
- functionName: "aaveV3OptimizerApproveManagerWithSig",
739
- args: [
740
- isApproved,
741
- nonce,
742
- deadline,
743
- { v: yParity + 27, r, s },
1244
+ return [
1245
+ {
1246
+ to: aaveV3Optimizer,
1247
+ data: encodeFunctionData({
1248
+ abi: [
1249
+ {
1250
+ inputs: [
1251
+ { name: "delegator", type: "address" },
1252
+ { name: "manager", type: "address" },
1253
+ { name: "isAllowed", type: "bool" },
1254
+ { name: "nonce", type: "uint256" },
1255
+ { name: "deadline", type: "uint256" },
1256
+ {
1257
+ components: [
1258
+ { name: "v", type: "uint8" },
1259
+ { name: "r", type: "bytes32" },
1260
+ { name: "s", type: "bytes32" },
1261
+ ],
1262
+ internalType: "struct Types.Signature",
1263
+ name: "signature",
1264
+ type: "tuple",
1265
+ },
1266
+ ],
1267
+ name: "approveManagerWithSig",
1268
+ outputs: [],
1269
+ stateMutability: "nonpayable",
1270
+ type: "function",
1271
+ },
1272
+ ],
1273
+ functionName: "approveManagerWithSig",
1274
+ args: [
1275
+ owner,
1276
+ manager,
1277
+ isApproved,
1278
+ nonce,
1279
+ deadline,
1280
+ { v: yParity + 27, r, s },
1281
+ ],
1282
+ }),
1283
+ value: 0n,
744
1284
  skipRevert,
745
- ],
746
- });
1285
+ callbackHash: zeroHash,
1286
+ },
1287
+ ];
747
1288
  }
748
1289
  BundlerAction.aaveV3OptimizerApproveManagerWithSig = aaveV3OptimizerApproveManagerWithSig;
749
1290
  /* CompoundV2 */
750
1291
  /**
751
- * ! Only available on CompoundV2MigrationBundler instances (not the main Bundler contract!).
752
- * Encodes a call to the Bundler to repay a debt on CompoundV2.
1292
+ * Encodes a call to the Adapter to repay a debt on CompoundV2.
1293
+ * @param chainId The chain id for which to encode the call.
753
1294
  * @param cToken The cToken on which to repay the debt.
754
1295
  * @param amount The amount of debt to repay.
1296
+ * @param recipient The recipient of ERC20 tokens. Defaults to the chain's bundler3 general adapter.
755
1297
  */
756
- function compoundV2Repay(cToken, amount) {
757
- return encodeFunctionData({
758
- abi: compoundV2MigrationBundlerAbi,
759
- functionName: "compoundV2Repay",
760
- args: [cToken, amount],
761
- });
1298
+ function compoundV2Repay(chainId, cToken, amount, recipient) {
1299
+ const { cEth, bundler3: { compoundV2MigrationAdapter }, } = getChainAddresses(chainId);
1300
+ if (cEth == null || compoundV2MigrationAdapter == null)
1301
+ throw new BundlerErrors.UnexpectedAction("compoundV2Repay", chainId);
1302
+ const isEth = cToken === cEth;
1303
+ recipient ??= compoundV2MigrationAdapter;
1304
+ return [
1305
+ {
1306
+ to: compoundV2MigrationAdapter,
1307
+ data: isEth
1308
+ ? encodeFunctionData({
1309
+ abi: compoundV2MigrationAdapterAbi,
1310
+ functionName: "compoundV2RepayEth",
1311
+ args: [amount, recipient],
1312
+ })
1313
+ : encodeFunctionData({
1314
+ abi: compoundV2MigrationAdapterAbi,
1315
+ functionName: "compoundV2RepayErc20",
1316
+ args: [cToken, amount, recipient],
1317
+ }),
1318
+ value: isEth ? amount : 0n,
1319
+ skipRevert: false,
1320
+ callbackHash: zeroHash,
1321
+ },
1322
+ ];
762
1323
  }
763
1324
  BundlerAction.compoundV2Repay = compoundV2Repay;
764
1325
  /**
765
- * ! Only available on CompoundV2MigrationBundler instances (not the main Bundler contract!).
766
- * Encodes a call to the Bundler to withdraw collateral from CompoundV2.
1326
+ * Encodes a call to the Adapter to withdraw collateral from CompoundV2.
1327
+ * @param chainId The chain id for which to encode the call.
767
1328
  * @param cToken The cToken on which to withdraw.
768
1329
  * @param amount The amount to withdraw.
1330
+ * @param recipient The recipient of ERC20 tokens. Defaults to the chain's bundler3 general adapter.
769
1331
  */
770
- function compoundV2Redeem(cToken, amount) {
771
- return encodeFunctionData({
772
- abi: compoundV2MigrationBundlerAbi,
773
- functionName: "compoundV2Redeem",
774
- args: [cToken, amount],
775
- });
1332
+ function compoundV2Redeem(chainId, cToken, amount, recipient) {
1333
+ const { cEth, bundler3: { compoundV2MigrationAdapter }, } = getChainAddresses(chainId);
1334
+ if (cEth == null || compoundV2MigrationAdapter == null)
1335
+ throw new BundlerErrors.UnexpectedAction("compoundV2Repay", chainId);
1336
+ const isEth = cToken === cEth;
1337
+ recipient ??= compoundV2MigrationAdapter;
1338
+ return [
1339
+ {
1340
+ to: compoundV2MigrationAdapter,
1341
+ data: isEth
1342
+ ? encodeFunctionData({
1343
+ abi: compoundV2MigrationAdapterAbi,
1344
+ functionName: "compoundV2RedeemEth",
1345
+ args: [amount, recipient],
1346
+ })
1347
+ : encodeFunctionData({
1348
+ abi: compoundV2MigrationAdapterAbi,
1349
+ functionName: "compoundV2RedeemErc20",
1350
+ args: [cToken, amount, recipient],
1351
+ }),
1352
+ value: 0n,
1353
+ skipRevert: false,
1354
+ callbackHash: zeroHash,
1355
+ },
1356
+ ];
776
1357
  }
777
1358
  BundlerAction.compoundV2Redeem = compoundV2Redeem;
778
1359
  /* CompoundV3 */
779
1360
  /**
780
- * ! Only available on CompoundV3MigrationBundler instances (not the main Bundler contract!).
781
- * Encodes a call to the Bundler to repay a debt on CompoundV3.
1361
+ * Encodes a call to the Adapter to repay a debt on CompoundV3.
1362
+ * @param chainId The chain id for which to encode the call.
782
1363
  * @param instance The CompoundV3 instance on which to repay the debt.
783
1364
  * @param amount The amount of debt to repay.
1365
+ * @param onBehalf The address on behalf of which to repay.
784
1366
  */
785
- function compoundV3Repay(instance, amount) {
786
- return encodeFunctionData({
787
- abi: compoundV3MigrationBundlerAbi,
788
- functionName: "compoundV3Repay",
789
- args: [instance, amount],
790
- });
1367
+ function compoundV3Repay(chainId, instance, amount, onBehalf) {
1368
+ const { bundler3: { compoundV3MigrationAdapter }, } = getChainAddresses(chainId);
1369
+ if (compoundV3MigrationAdapter == null)
1370
+ throw new BundlerErrors.UnexpectedAction("compoundV3Repay", chainId);
1371
+ return [
1372
+ {
1373
+ to: compoundV3MigrationAdapter,
1374
+ data: encodeFunctionData({
1375
+ abi: compoundV3MigrationAdapterAbi,
1376
+ functionName: "compoundV3Repay",
1377
+ args: [instance, amount, onBehalf],
1378
+ }),
1379
+ value: 0n,
1380
+ skipRevert: false,
1381
+ callbackHash: zeroHash,
1382
+ },
1383
+ ];
791
1384
  }
792
1385
  BundlerAction.compoundV3Repay = compoundV3Repay;
793
1386
  /**
794
- * ! Only available on CompoundV3MigrationBundler instances (not the main Bundler contract!).
795
- * Encodes a call to the Bundler to withdraw collateral from CompoundV3.
1387
+ * Encodes a call to the Adapter to withdraw collateral from CompoundV3.
1388
+ * @param chainId The chain id for which to encode the call.
796
1389
  * @param instance The CompoundV3 instance on which to withdraw.
1390
+ * @param asset The asset to withdraw.
797
1391
  * @param amount The amount to withdraw.
1392
+ * @param recipient The recipient of ERC20 tokens. Defaults to the chain's bundler3 general adapter.
798
1393
  */
799
- function compoundV3WithdrawFrom(instance, asset, amount) {
800
- return encodeFunctionData({
801
- abi: compoundV3MigrationBundlerAbi,
802
- functionName: "compoundV3WithdrawFrom",
803
- args: [instance, asset, amount],
804
- });
1394
+ function compoundV3WithdrawFrom(chainId, instance, asset, amount, recipient) {
1395
+ const { bundler3: { compoundV3MigrationAdapter }, } = getChainAddresses(chainId);
1396
+ if (compoundV3MigrationAdapter == null)
1397
+ throw new BundlerErrors.UnexpectedAction("compoundV3WithdrawFrom", chainId);
1398
+ recipient ??= compoundV3MigrationAdapter;
1399
+ return [
1400
+ {
1401
+ to: compoundV3MigrationAdapter,
1402
+ data: encodeFunctionData({
1403
+ abi: compoundV3MigrationAdapterAbi,
1404
+ functionName: "compoundV3WithdrawFrom",
1405
+ args: [instance, asset, amount, recipient],
1406
+ }),
1407
+ value: 0n,
1408
+ skipRevert: false,
1409
+ callbackHash: zeroHash,
1410
+ },
1411
+ ];
805
1412
  }
806
1413
  BundlerAction.compoundV3WithdrawFrom = compoundV3WithdrawFrom;
807
1414
  /**
808
- * ! Only available on CompoundV3MigrationBundler instances (not the main Bundler contract!).
809
- * Encodes a call to the Bundler to allow the Bundler to act on the sender's position on CompoundV3.
1415
+ * Encodes a call to the Adapter to allow the Bundler to act on the sender's position on CompoundV3.
1416
+ * @param chainId The chain id for which to encode the call.
810
1417
  * @param instance The CompoundV3 instance on which to submit the signature.
1418
+ * @param owner The owner of the CompoundV3 position.
811
1419
  * @param isAllowed Whether the manager is allowed.
812
1420
  * @param nonce The nonce used to sign.
813
1421
  * @param expiry The timestamp until which the signature is valid.
814
1422
  * @param signature The Ethers signature to submit.
1423
+ * @param manager The address of the manager to approve. Defaults to the chain's bundler3 CompoundV3MigrationAdapter.
815
1424
  * @param skipRevert Whether to allow the signature to revert without making the whole multicall revert.
816
1425
  */
817
- function compoundV3AllowBySig(instance, isAllowed, nonce, expiry, signature, skipRevert) {
1426
+ function compoundV3AllowBySig(chainId, instance, owner, isAllowed, nonce, expiry, signature, manager, skipRevert = true) {
1427
+ const { bundler3: { compoundV3MigrationAdapter }, } = getChainAddresses(chainId);
1428
+ if (compoundV3MigrationAdapter == null)
1429
+ throw new BundlerErrors.UnexpectedAction("compoundV3AllowBySig", chainId);
818
1430
  const { r, s, yParity } = parseSignature(signature);
819
- return encodeFunctionData({
820
- abi: compoundV3MigrationBundlerAbi,
821
- functionName: "compoundV3AllowBySig",
822
- args: [
823
- instance,
824
- isAllowed,
825
- nonce,
826
- expiry,
827
- yParity + 27,
828
- r,
829
- s,
1431
+ manager ??= compoundV3MigrationAdapter;
1432
+ return [
1433
+ {
1434
+ to: instance,
1435
+ data: encodeFunctionData({
1436
+ abi: [
1437
+ {
1438
+ inputs: [
1439
+ { name: "owner", type: "address" },
1440
+ { name: "manager", type: "address" },
1441
+ { name: "isAllowed_", type: "bool" },
1442
+ { name: "nonce", type: "uint256" },
1443
+ { name: "expiry", type: "uint256" },
1444
+ { name: "v", type: "uint8" },
1445
+ { name: "r", type: "bytes32" },
1446
+ { name: "s", type: "bytes32" },
1447
+ ],
1448
+ name: "allowBySig",
1449
+ outputs: [],
1450
+ stateMutability: "nonpayable",
1451
+ type: "function",
1452
+ },
1453
+ ],
1454
+ functionName: "allowBySig",
1455
+ args: [owner, manager, isAllowed, nonce, expiry, yParity + 27, r, s],
1456
+ }),
1457
+ value: 0n,
830
1458
  skipRevert,
831
- ],
832
- });
1459
+ callbackHash: zeroHash,
1460
+ },
1461
+ ];
833
1462
  }
834
1463
  BundlerAction.compoundV3AllowBySig = compoundV3AllowBySig;
835
1464
  })(BundlerAction || (BundlerAction = {}));