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

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