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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -340,7 +340,7 @@ export declare namespace BundlerAction {
340
340
  * @param manager The address of the manager to approve. Defaults to the chain's bundler3 AaveV3OptimizerMigrationAdapter.
341
341
  * @param skipRevert Whether to allow the signature to revert without making the whole multicall revert.
342
342
  */
343
- function aaveV3OptimizerApproveManagerWithSig(chainId: ChainId, owner: Address, isApproved: boolean, nonce: bigint, deadline: bigint, signature: Hex, manager?: Address, skipRevert?: boolean): BundlerCall[];
343
+ function aaveV3OptimizerApproveManagerWithSig(chainId: ChainId, aaveV3Optimizer: Address, owner: Address, isApproved: boolean, nonce: bigint, deadline: bigint, signature: Hex, manager?: Address, skipRevert?: boolean): BundlerCall[];
344
344
  /**
345
345
  * Encodes a call to the Adapter to repay a debt on CompoundV2.
346
346
  * @param chainId The chain id for which to encode the call.
@@ -348,7 +348,7 @@ export declare namespace BundlerAction {
348
348
  * @param amount The amount of debt to repay.
349
349
  * @param recipient The recipient of ERC20 tokens. Defaults to the chain's bundler3 general adapter.
350
350
  */
351
- function compoundV2Repay(chainId: ChainId, cToken: Address, amount: bigint, recipient?: Address): BundlerCall[];
351
+ function compoundV2Repay(chainId: ChainId, cToken: Address, amount: bigint, isEth: boolean, onBehalf: Address): BundlerCall[];
352
352
  /**
353
353
  * Encodes a call to the Adapter to withdraw collateral from CompoundV2.
354
354
  * @param chainId The chain id for which to encode the call.
@@ -356,7 +356,7 @@ export declare namespace BundlerAction {
356
356
  * @param amount The amount to withdraw.
357
357
  * @param recipient The recipient of ERC20 tokens. Defaults to the chain's bundler3 general adapter.
358
358
  */
359
- function compoundV2Redeem(chainId: ChainId, cToken: Address, amount: bigint, recipient?: Address): BundlerCall[];
359
+ function compoundV2Redeem(chainId: ChainId, cToken: Address, amount: bigint, isEth: boolean, recipient?: Address): BundlerCall[];
360
360
  /**
361
361
  * Encodes a call to the Adapter to repay a debt on CompoundV3.
362
362
  * @param chainId The chain id for which to encode the call.
@@ -178,10 +178,10 @@ export var BundlerAction;
178
178
  return BundlerAction.aaveV3OptimizerWithdrawCollateral(chainId, ...args);
179
179
  }
180
180
  case "aaveV3OptimizerApproveManagerWithSig": {
181
- const [owner, isApproved, nonce, deadline, signature, manager, skipRevert,] = args;
181
+ const [aaveV3Optimizer, owner, isApproved, nonce, deadline, signature, manager, skipRevert,] = args;
182
182
  if (signature == null)
183
183
  throw new BundlerErrors.MissingSignature();
184
- return BundlerAction.aaveV3OptimizerApproveManagerWithSig(chainId, owner, isApproved, nonce, deadline, signature, manager, skipRevert);
184
+ return BundlerAction.aaveV3OptimizerApproveManagerWithSig(chainId, aaveV3Optimizer, owner, isApproved, nonce, deadline, signature, manager, skipRevert);
185
185
  }
186
186
  /* CompoundV2 */
187
187
  case "compoundV2Repay": {
@@ -867,6 +867,8 @@ export var BundlerAction;
867
867
  */
868
868
  function publicAllocatorReallocateTo(chainId, vault, fee, withdrawals, supplyMarketParams) {
869
869
  const { publicAllocator } = getChainAddresses(chainId);
870
+ if (publicAllocator == null)
871
+ throw new BundlerErrors.UnexpectedAction("reallocateTo", chainId);
870
872
  return [
871
873
  {
872
874
  to: publicAllocator,
@@ -1239,9 +1241,9 @@ export var BundlerAction;
1239
1241
  * @param manager The address of the manager to approve. Defaults to the chain's bundler3 AaveV3OptimizerMigrationAdapter.
1240
1242
  * @param skipRevert Whether to allow the signature to revert without making the whole multicall revert.
1241
1243
  */
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)
1244
+ function aaveV3OptimizerApproveManagerWithSig(chainId, aaveV3Optimizer, owner, isApproved, nonce, deadline, signature, manager, skipRevert = true) {
1245
+ const { bundler3: { aaveV3OptimizerMigrationAdapter }, } = getChainAddresses(chainId);
1246
+ if (aaveV3OptimizerMigrationAdapter == null)
1245
1247
  throw new BundlerErrors.UnexpectedAction("aaveV3OptimizerApproveManagerWithSig", chainId);
1246
1248
  manager ??= aaveV3OptimizerMigrationAdapter;
1247
1249
  const { r, s, yParity } = parseSignature(signature);
@@ -1299,12 +1301,10 @@ export var BundlerAction;
1299
1301
  * @param amount The amount of debt to repay.
1300
1302
  * @param recipient The recipient of ERC20 tokens. Defaults to the chain's bundler3 general adapter.
1301
1303
  */
1302
- function compoundV2Repay(chainId, cToken, amount, recipient) {
1303
- const { cEth, bundler3: { compoundV2MigrationAdapter }, } = getChainAddresses(chainId);
1304
- if (cEth == null || compoundV2MigrationAdapter == null)
1304
+ function compoundV2Repay(chainId, cToken, amount, isEth, onBehalf) {
1305
+ const { bundler3: { compoundV2MigrationAdapter }, } = getChainAddresses(chainId);
1306
+ if (compoundV2MigrationAdapter == null)
1305
1307
  throw new BundlerErrors.UnexpectedAction("compoundV2Repay", chainId);
1306
- const isEth = cToken === cEth;
1307
- recipient ??= compoundV2MigrationAdapter;
1308
1308
  return [
1309
1309
  {
1310
1310
  to: compoundV2MigrationAdapter,
@@ -1312,12 +1312,12 @@ export var BundlerAction;
1312
1312
  ? encodeFunctionData({
1313
1313
  abi: compoundV2MigrationAdapterAbi,
1314
1314
  functionName: "compoundV2RepayEth",
1315
- args: [amount, recipient],
1315
+ args: [amount, onBehalf],
1316
1316
  })
1317
1317
  : encodeFunctionData({
1318
1318
  abi: compoundV2MigrationAdapterAbi,
1319
1319
  functionName: "compoundV2RepayErc20",
1320
- args: [cToken, amount, recipient],
1320
+ args: [cToken, amount, onBehalf],
1321
1321
  }),
1322
1322
  value: isEth ? amount : 0n,
1323
1323
  skipRevert: false,
@@ -1333,11 +1333,10 @@ export var BundlerAction;
1333
1333
  * @param amount The amount to withdraw.
1334
1334
  * @param recipient The recipient of ERC20 tokens. Defaults to the chain's bundler3 general adapter.
1335
1335
  */
1336
- function compoundV2Redeem(chainId, cToken, amount, recipient) {
1337
- const { cEth, bundler3: { compoundV2MigrationAdapter }, } = getChainAddresses(chainId);
1338
- if (cEth == null || compoundV2MigrationAdapter == null)
1336
+ function compoundV2Redeem(chainId, cToken, amount, isEth, recipient) {
1337
+ const { bundler3: { compoundV2MigrationAdapter }, } = getChainAddresses(chainId);
1338
+ if (compoundV2MigrationAdapter == null)
1339
1339
  throw new BundlerErrors.UnexpectedAction("compoundV2Repay", chainId);
1340
- const isEth = cToken === cEth;
1341
1340
  recipient ??= compoundV2MigrationAdapter;
1342
1341
  return [
1343
1342
  {
package/lib/actions.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { type Address } from "viem";
2
- import { ChainId } from "@morpho-org/blue-sdk";
3
2
  import { type MaybeDraft, type SimulationState } from "@morpho-org/simulation-sdk";
4
3
  import type { Action, ActionBundle, BundlerOperation, TransactionRequirement } from "./types/index.js";
5
- export declare const APPROVE_ONLY_ONCE_TOKENS: Partial<Record<ChainId, Address[]>>;
4
+ export declare const APPROVE_ONLY_ONCE_TOKENS: Partial<Record<number, Address[]>>;
5
+ export declare const MAX_TOKEN_APPROVALS: Partial<Record<number, Record<Address, bigint>>>;
6
6
  export declare const encodeOperation: (operation: BundlerOperation, dataBefore: MaybeDraft<SimulationState>, supportsSignature?: boolean, index?: number) => {
7
7
  dataAfter: MaybeDraft<SimulationState>;
8
8
  actions: Action[];
package/lib/actions.js CHANGED
@@ -11,7 +11,7 @@ export const APPROVE_ONLY_ONCE_TOKENS = {
11
11
  "0xD533a949740bb3306d119CC777fa900bA034cd52", // CRV
12
12
  ],
13
13
  };
14
- const MAX_TOKEN_APPROVALS = {
14
+ export const MAX_TOKEN_APPROVALS = {
15
15
  [ChainId.EthMainnet]: {
16
16
  "0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984": MathLib.maxUint(96), // UNI --> see https://github.com/Uniswap/governance/blob/eabd8c71ad01f61fb54ed6945162021ee419998e/contracts/Uni.sol#L154
17
17
  },
@@ -346,7 +346,7 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
346
346
  break;
347
347
  }
348
348
  default: {
349
- if (erc20WrapperTokens[chainId].has(address)) {
349
+ if (erc20WrapperTokens[chainId]?.has(address)) {
350
350
  const underlying = getUnwrappedToken(address, chainId);
351
351
  if (underlying == null)
352
352
  throw Error(`unknown wrapped token: ${address}`);
@@ -357,7 +357,7 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
357
357
  break;
358
358
  }
359
359
  // Convex token wrapping is executed onchain along with supplyCollateral, via depositFor.
360
- if (!convexWrapperTokens[chainId].has(address))
360
+ if (!convexWrapperTokens[chainId]?.has(address))
361
361
  throw Error(`unexpected token wrap: ${address}`);
362
362
  }
363
363
  }
@@ -381,7 +381,7 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
381
381
  break;
382
382
  }
383
383
  default: {
384
- if (!erc20WrapperTokens[chainId].has(address))
384
+ if (!erc20WrapperTokens[chainId]?.has(address))
385
385
  throw Error(`unexpected token unwrap: ${address}`);
386
386
  actions.push({
387
387
  type: "erc20WrapperWithdrawTo",
@@ -393,13 +393,12 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
393
393
  }
394
394
  case "Blue_Supply": {
395
395
  const { id, assets = 0n, shares = 0n, onBehalf, slippage = 0n, } = operation.args;
396
- const { params, totalSupplyAssets, totalSupplyShares } = dataBefore.getMarket(id);
397
- const maxSharePrice = (totalSupplyAssets * MathLib.wToRay(MathLib.WAD + slippage)) /
398
- totalSupplyShares;
396
+ const market = dataBefore.getMarket(id);
397
+ const maxSharePrice = market.toSupplyAssets(MathLib.wToRay(MathLib.WAD + slippage));
399
398
  actions.push({
400
399
  type: "morphoSupply",
401
400
  args: [
402
- params,
401
+ market.params,
403
402
  assets,
404
403
  shares,
405
404
  maxSharePrice,
@@ -411,35 +410,32 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
411
410
  }
412
411
  case "Blue_Withdraw": {
413
412
  const { id, assets = 0n, shares = 0n, receiver, slippage = 0n, } = operation.args;
414
- const { params, totalSupplyAssets, totalSupplyShares } = dataBefore.getMarket(id);
415
- const minSharePrice = (totalSupplyAssets * MathLib.wToRay(MathLib.WAD - slippage)) /
416
- totalSupplyShares;
413
+ const market = dataBefore.getMarket(id);
414
+ const minSharePrice = market.toSupplyAssets(MathLib.wToRay(MathLib.WAD - slippage));
417
415
  actions.push({
418
416
  type: "morphoWithdraw",
419
- args: [params, assets, shares, minSharePrice, receiver],
417
+ args: [market.params, assets, shares, minSharePrice, receiver],
420
418
  });
421
419
  break;
422
420
  }
423
421
  case "Blue_Borrow": {
424
422
  const { id, assets = 0n, shares = 0n, receiver, slippage = 0n, } = operation.args;
425
- const { params, totalBorrowAssets, totalBorrowShares } = dataBefore.getMarket(id);
426
- const minSharePrice = (totalBorrowAssets * MathLib.wToRay(MathLib.WAD - slippage)) /
427
- totalBorrowShares;
423
+ const market = dataBefore.getMarket(id);
424
+ const minSharePrice = market.toBorrowAssets(MathLib.wToRay(MathLib.WAD - slippage));
428
425
  actions.push({
429
426
  type: "morphoBorrow",
430
- args: [params, assets, shares, minSharePrice, receiver],
427
+ args: [market.params, assets, shares, minSharePrice, receiver],
431
428
  });
432
429
  break;
433
430
  }
434
431
  case "Blue_Repay": {
435
432
  const { id, assets = 0n, shares = 0n, onBehalf, slippage = 0n, } = operation.args;
436
- const { params, totalBorrowAssets, totalBorrowShares } = dataBefore.getMarket(id);
437
- const maxSharePrice = (totalBorrowAssets * MathLib.wToRay(MathLib.WAD + slippage)) /
438
- totalBorrowShares;
433
+ const market = dataBefore.getMarket(id);
434
+ const maxSharePrice = market.toBorrowAssets(MathLib.wToRay(MathLib.WAD + slippage));
439
435
  actions.push({
440
436
  type: "morphoRepay",
441
437
  args: [
442
- params,
438
+ market.params,
443
439
  assets,
444
440
  shares,
445
441
  maxSharePrice,
@@ -452,7 +448,7 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
452
448
  case "Blue_SupplyCollateral": {
453
449
  const { id, assets, onBehalf } = operation.args;
454
450
  const { params } = dataBefore.getMarket(id);
455
- if (convexWrapperTokens[chainId].has(params.collateralToken)) {
451
+ if (convexWrapperTokens[chainId]?.has(params.collateralToken)) {
456
452
  const underlying = getUnwrappedToken(address, chainId);
457
453
  if (underlying == null)
458
454
  throw Error(`unknown wrapped token: ${address}`);
@@ -480,8 +476,7 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
480
476
  case "MetaMorpho_Deposit": {
481
477
  const { assets = 0n, shares = 0n, owner, slippage = 0n } = operation.args;
482
478
  const vault = dataBefore.getVault(address);
483
- const maxSharePrice = (vault.totalAssets * MathLib.wToRay(MathLib.WAD + slippage)) /
484
- vault.totalSupply;
479
+ const maxSharePrice = vault.toAssets(MathLib.wToRay(MathLib.WAD + slippage));
485
480
  if (shares === 0n)
486
481
  actions.push({
487
482
  type: "erc4626Deposit",
@@ -497,8 +492,7 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
497
492
  case "MetaMorpho_Withdraw": {
498
493
  const { assets = 0n, shares = 0n, owner, receiver, slippage = 0n, } = operation.args;
499
494
  const vault = dataBefore.getVault(address);
500
- const minSharePrice = (vault.totalAssets * MathLib.wToRay(MathLib.WAD - slippage)) /
501
- vault.totalSupply;
495
+ const minSharePrice = vault.toAssets(MathLib.wToRay(MathLib.WAD - slippage));
502
496
  if (assets > 0n)
503
497
  actions.push({
504
498
  type: "erc4626Withdraw",
package/lib/index.d.ts CHANGED
@@ -2,3 +2,4 @@ export * from "./actions.js";
2
2
  export * from "./operations.js";
3
3
  export * from "./errors.js";
4
4
  export * from "./types/index.js";
5
+ export * from "./BundlerAction.js";
package/lib/index.js CHANGED
@@ -2,3 +2,4 @@ export * from "./actions.js";
2
2
  export * from "./operations.js";
3
3
  export * from "./errors.js";
4
4
  export * from "./types/index.js";
5
+ export * from "./BundlerAction.js";
package/lib/operations.js CHANGED
@@ -45,8 +45,8 @@ export const populateInputTransfer = ({ address, args: { amount, from } }, data,
45
45
  hasSimplePermit);
46
46
  const useSimpleTransfer = permit2 == null ||
47
47
  // Token is permissioned and Permit2 may not be authorized so Permit2 cannot be used.
48
- permissionedWrapperTokens[data.chainId].has(address) ||
49
- permissionedBackedTokens[data.chainId].has(address);
48
+ !!permissionedWrapperTokens[data.chainId]?.has(address) ||
49
+ !!permissionedBackedTokens[data.chainId]?.has(address);
50
50
  if (useSimplePermit)
51
51
  operations.push({
52
52
  type: "Erc20_Permit",
@@ -138,7 +138,7 @@ export const populateSubBundle = (inputOperation, data, options = {}) => {
138
138
  ? data.getWrappedToken(inputOperation.address)
139
139
  : undefined;
140
140
  const isErc20Wrapper = !!wrappedToken &&
141
- erc20WrapperTokens[data.chainId].has(wrappedToken.address);
141
+ !!erc20WrapperTokens[data.chainId]?.has(wrappedToken.address);
142
142
  // Transform input operation to act on behalf of the sender, via the bundler.
143
143
  const mainOperation = produceImmutable(inputOperation, (draft) => {
144
144
  draft.sender = generalAdapter1;
@@ -394,7 +394,7 @@ export const finalizeBundle = (operations, startData, receiver, unwrapTokens = n
394
394
  const { address, sender, args: { amount, from, to }, } = operation;
395
395
  if (from !== generalAdapter1 &&
396
396
  to === generalAdapter1 &&
397
- !erc20WrapperTokens[startData.chainId].has(address)) {
397
+ !erc20WrapperTokens[startData.chainId]?.has(address)) {
398
398
  const duplicateTransfer = inputTransfers.find((transfer) => transfer.address === address &&
399
399
  transfer.sender === sender &&
400
400
  transfer.args.from === from);
@@ -196,6 +196,7 @@ export interface ActionArgs {
196
196
  recipient?: Address
197
197
  ];
198
198
  aaveV3OptimizerApproveManagerWithSig: [
199
+ aaveV3Optimizer: Address,
199
200
  owner: Address,
200
201
  isApproved: boolean,
201
202
  nonce: bigint,
@@ -204,8 +205,18 @@ export interface ActionArgs {
204
205
  manager?: Address,
205
206
  skipRevert?: boolean
206
207
  ];
207
- compoundV2Repay: [cToken: Address, amount: bigint, onBehalf: Address];
208
- compoundV2Redeem: [cToken: Address, amount: bigint, recipient?: Address];
208
+ compoundV2Repay: [
209
+ cToken: Address,
210
+ amount: bigint,
211
+ isEth: boolean,
212
+ onBehalf: Address
213
+ ];
214
+ compoundV2Redeem: [
215
+ cToken: Address,
216
+ amount: bigint,
217
+ isEth: boolean,
218
+ recipient?: Address
219
+ ];
209
220
  compoundV3Repay: [instance: Address, amount: bigint, onBehalf: Address];
210
221
  compoundV3WithdrawFrom: [
211
222
  instance: Address,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@morpho-org/bundler-sdk-viem",
3
3
  "description": "Viem-based extension of `@morpho-org/simulation-sdk` that exports utilities to transform simple interactions on Morpho (such as `Blue_Borrow`) and Morpho Vaults (such as `MetaMorpho_Deposit`) into the required bundles (with ERC20 approvals, transfers, etc) to submit to the bundler onchain.",
4
- "version": "3.0.0-next.1",
4
+ "version": "3.0.0-next.3",
5
5
  "author": "Morpho Association <contact@morpho.org>",
6
6
  "contributors": [
7
7
  "Rubilmax <rmilon@gmail.com>"
@@ -20,10 +20,10 @@
20
20
  ],
21
21
  "peerDependencies": {
22
22
  "viem": "^2.0.0",
23
- "@morpho-org/blue-sdk-viem": "^3.0.0-next.1",
24
- "@morpho-org/simulation-sdk": "^3.0.0-next.1",
25
- "@morpho-org/blue-sdk": "^3.0.0-next.3",
26
- "@morpho-org/morpho-ts": "^2.1.0"
23
+ "@morpho-org/blue-sdk": "^3.0.0-next.5",
24
+ "@morpho-org/blue-sdk-viem": "^3.0.0-next.2",
25
+ "@morpho-org/simulation-sdk": "^3.0.0-next.2",
26
+ "@morpho-org/morpho-ts": "^2.2.0-next.0"
27
27
  },
28
28
  "devDependencies": {
29
29
  "@tanstack/query-core": "^5.62.16",
@@ -35,14 +35,14 @@
35
35
  "typescript": "^5.7.2",
36
36
  "viem": "^2.23.0",
37
37
  "vitest": "^3.0.5",
38
- "@morpho-org/blue-sdk": "^3.0.0-next.3",
39
- "@morpho-org/blue-sdk-viem": "^3.0.0-next.1",
40
- "@morpho-org/morpho-test": "^2.2.1",
41
- "@morpho-org/morpho-ts": "^2.1.0",
42
- "@morpho-org/simulation-sdk": "^3.0.0-next.1",
43
- "@morpho-org/simulation-sdk-wagmi": "^2.0.5",
38
+ "@morpho-org/blue-sdk": "^3.0.0-next.5",
39
+ "@morpho-org/blue-sdk-viem": "^3.0.0-next.2",
40
+ "@morpho-org/morpho-test": "^2.3.0-next.0",
41
+ "@morpho-org/simulation-sdk": "^3.0.0-next.2",
42
+ "@morpho-org/morpho-ts": "^2.2.0-next.0",
44
43
  "@morpho-org/test": "^2.0.6",
45
- "@morpho-org/test-wagmi": "^2.0.4"
44
+ "@morpho-org/test-wagmi": "^2.0.4",
45
+ "@morpho-org/simulation-sdk-wagmi": "^3.0.0-next.1"
46
46
  },
47
47
  "scripts": {
48
48
  "prepublish": "$npm_execpath build",