@morpho-org/bundler-sdk-viem 3.2.7 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/errors.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { SimulationResult } from "@morpho-org/simulation-sdk";
1
+ import type { OperationType, SimulationResult } from "@morpho-org/simulation-sdk";
2
2
  import type { Address } from "viem";
3
3
  import type { ActionType, InputBundlerOperation } from "./types/index.js";
4
4
  export declare namespace BundlerErrors {
@@ -21,4 +21,10 @@ export declare namespace BundlerErrors {
21
21
  class UnexpectedSignature extends Error {
22
22
  constructor(spender: Address);
23
23
  }
24
+ class MissingSkimHandler extends Error {
25
+ constructor(type: OperationType);
26
+ }
27
+ class UnskimedToken extends Error {
28
+ constructor(token: Address);
29
+ }
24
30
  }
package/lib/errors.js CHANGED
@@ -42,4 +42,16 @@ var BundlerErrors;
42
42
  }
43
43
  }
44
44
  BundlerErrors.UnexpectedSignature = UnexpectedSignature;
45
+ class MissingSkimHandler extends Error {
46
+ constructor(type) {
47
+ super(`missing skim handler for operation "${type}"`);
48
+ }
49
+ }
50
+ BundlerErrors.MissingSkimHandler = MissingSkimHandler;
51
+ class UnskimedToken extends Error {
52
+ constructor(token) {
53
+ super(`missing final skim for token "${token}"`);
54
+ }
55
+ }
56
+ BundlerErrors.UnskimedToken = UnskimedToken;
45
57
  })(BundlerErrors || (exports.BundlerErrors = BundlerErrors = {}));
package/lib/operations.js CHANGED
@@ -614,29 +614,57 @@ const finalizeBundle = (operations, startData, receiver, unwrapTokens = new Set(
614
614
  }
615
615
  // Skim any token expected to be left on the bundler.
616
616
  const skims = [];
617
- const startBundlerTokenData = steps[0].holdings[generalAdapter1] ?? {};
618
- const uniqueSkimTokens = new Set((0, morpho_ts_1.entries)(endBundlerTokenData)
619
- .filter(([token, holding]) => holding != null &&
620
- holding.balance - (startBundlerTokenData[token]?.balance ?? 0n) > 5n)
621
- .map(([address]) => address));
622
- const pushCustomSkim = (operation) => {
617
+ const uniqueSkimTokens = new Set();
618
+ const pushSkim = (operation) => {
623
619
  // Paraswap does not guarantee that the amount effectively bought (resp. sold) corresponds to
624
620
  // the requested amount to buy (resp. sell), so we force skim the possible surplus of bought (resp. sold) token.
625
621
  switch (operation.type) {
622
+ case "Blue_Borrow":
623
+ case "Blue_Repay":
624
+ case "Blue_Supply":
625
+ case "Blue_Withdraw":
626
+ case "Blue_Paraswap_BuyDebt":
627
+ uniqueSkimTokens.add(startData.getMarket(operation.args.id).params.loanToken);
628
+ break;
629
+ case "Blue_WithdrawCollateral":
630
+ case "Blue_SupplyCollateral":
631
+ uniqueSkimTokens.add(startData.getMarket(operation.args.id).params.collateralToken);
632
+ break;
633
+ case "Blue_FlashLoan":
634
+ uniqueSkimTokens.add(operation.args.token);
635
+ break;
626
636
  case "Paraswap_Buy":
627
637
  case "Paraswap_Sell":
638
+ case "Erc20_Transfer":
639
+ case "Erc20_Transfer2":
628
640
  uniqueSkimTokens.add(operation.address);
629
641
  break;
630
- case "Blue_Paraswap_BuyDebt":
631
- uniqueSkimTokens.add(startData.getMarket(operation.args.id).params.loanToken);
642
+ case "Blue_SetAuthorization":
643
+ case "Erc20_Approve":
644
+ case "Erc20_Permit":
645
+ case "Erc20_Permit2":
632
646
  break;
633
- default:
647
+ case "Erc20_Wrap":
648
+ case "Erc20_Unwrap":
649
+ uniqueSkimTokens.add(operation.address);
650
+ uniqueSkimTokens.add(startData.getWrappedToken(operation.address).underlying);
634
651
  break;
652
+ case "MetaMorpho_Deposit":
653
+ case "MetaMorpho_Withdraw":
654
+ uniqueSkimTokens.add(operation.address);
655
+ uniqueSkimTokens.add(startData.getVault(operation.address).asset);
656
+ break;
657
+ case "MetaMorpho_PublicReallocate":
658
+ uniqueSkimTokens.add(blue_sdk_1.NATIVE_ADDRESS);
659
+ break;
660
+ default:
661
+ //@ts-ignore This is dead code but acts as a guard in case a new operation is added
662
+ throw new errors_js_1.BundlerErrors.MissingSkimHandler(operation.type);
635
663
  }
636
664
  if ("callback" in operation.args)
637
- operation.args.callback?.forEach(pushCustomSkim);
665
+ operation.args.callback?.forEach(pushSkim);
638
666
  };
639
- operations.forEach(pushCustomSkim);
667
+ operations.concat(unwraps).forEach(pushSkim);
640
668
  skims.push(...Array.from(uniqueSkimTokens, (address) => ({
641
669
  type: "Erc20_Transfer",
642
670
  address,
@@ -647,7 +675,15 @@ const finalizeBundle = (operations, startData, receiver, unwrapTokens = new Set(
647
675
  to: receiver,
648
676
  },
649
677
  })));
650
- return operations.concat(unwraps, skims);
678
+ const finalizedOperations = operations.concat(unwraps, skims);
679
+ const finalizedSteps = (0, exports.simulateBundlerOperations)(finalizedOperations, startData);
680
+ for (const holding of (0, morpho_ts_1.values)((0, morpho_ts_1.getLast)(finalizedSteps).holdings[generalAdapter1])) {
681
+ if (!holding)
682
+ continue;
683
+ if (holding.balance > 0n)
684
+ throw new errors_js_1.BundlerErrors.UnskimedToken(holding.token);
685
+ }
686
+ return finalizedOperations;
651
687
  };
652
688
  exports.finalizeBundle = finalizeBundle;
653
689
  const populateBundle = (inputOperations, data, options) => {
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.2.7",
4
+ "version": "3.3.0",
5
5
  "author": "Morpho Association <contact@morpho.org>",
6
6
  "contributors": [
7
7
  "Rubilmax <rmilon@gmail.com>"
@@ -19,10 +19,10 @@
19
19
  ],
20
20
  "peerDependencies": {
21
21
  "viem": "^2.0.0",
22
- "@morpho-org/blue-sdk": "^4.9.0",
23
- "@morpho-org/blue-sdk-viem": "^3.1.1",
22
+ "@morpho-org/blue-sdk-viem": "^3.1.2",
24
23
  "@morpho-org/morpho-ts": "^2.4.1",
25
- "@morpho-org/simulation-sdk": "^3.1.3"
24
+ "@morpho-org/simulation-sdk": "^3.1.3",
25
+ "@morpho-org/blue-sdk": "^4.9.1"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@tanstack/query-core": "^5.62.16",
@@ -34,14 +34,14 @@
34
34
  "typescript": "^5.7.2",
35
35
  "viem": "^2.23.0",
36
36
  "vitest": "^3.0.5",
37
- "@morpho-org/blue-sdk-viem": "^3.1.1",
38
- "@morpho-org/blue-sdk": "^4.9.0",
37
+ "@morpho-org/blue-sdk": "^4.9.1",
39
38
  "@morpho-org/morpho-ts": "^2.4.1",
40
- "@morpho-org/morpho-test": "^2.4.0",
39
+ "@morpho-org/blue-sdk-viem": "^3.1.2",
41
40
  "@morpho-org/simulation-sdk": "^3.1.3",
41
+ "@morpho-org/morpho-test": "^2.4.0",
42
42
  "@morpho-org/simulation-sdk-wagmi": "^3.0.3",
43
- "@morpho-org/test": "^2.1.5",
44
- "@morpho-org/test-wagmi": "^2.0.4"
43
+ "@morpho-org/test-wagmi": "^2.0.4",
44
+ "@morpho-org/test": "^2.1.5"
45
45
  },
46
46
  "scripts": {
47
47
  "prepublish": "$npm_execpath build",