@morpho-org/morpho-sdk 3.0.0 → 3.1.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.
Files changed (60) hide show
  1. package/README.md +69 -3
  2. package/lib/cjs/actions/marketV1/index.d.ts +2 -0
  3. package/lib/cjs/actions/marketV1/index.js +2 -0
  4. package/lib/cjs/actions/marketV1/supply.d.ts +69 -0
  5. package/lib/cjs/actions/marketV1/supply.js +128 -0
  6. package/lib/cjs/actions/marketV1/supplyCollateral.d.ts +1 -1
  7. package/lib/cjs/actions/marketV1/supplyCollateral.js +2 -2
  8. package/lib/cjs/actions/marketV1/supplyCollateralBorrow.d.ts +1 -1
  9. package/lib/cjs/actions/marketV1/supplyCollateralBorrow.js +2 -2
  10. package/lib/cjs/actions/marketV1/withdraw.d.ts +83 -0
  11. package/lib/cjs/actions/marketV1/withdraw.js +110 -0
  12. package/lib/cjs/bundler/actions.d.ts +86 -4
  13. package/lib/cjs/bundler/actions.js +143 -8
  14. package/lib/cjs/bundler/types.d.ts +23 -4
  15. package/lib/cjs/entities/marketV1/marketV1.d.ts +121 -18
  16. package/lib/cjs/entities/marketV1/marketV1.js +144 -12
  17. package/lib/cjs/entities/reallocationData.js +3 -0
  18. package/lib/cjs/helpers/computeReallocations.d.ts +17 -6
  19. package/lib/cjs/helpers/computeReallocations.js +44 -11
  20. package/lib/cjs/helpers/index.d.ts +2 -2
  21. package/lib/cjs/helpers/index.js +6 -2
  22. package/lib/cjs/helpers/slippage.d.ts +57 -0
  23. package/lib/cjs/helpers/slippage.js +82 -0
  24. package/lib/cjs/helpers/validate.d.ts +38 -7
  25. package/lib/cjs/helpers/validate.js +54 -11
  26. package/lib/cjs/types/action.d.ts +27 -6
  27. package/lib/cjs/types/error.d.ts +68 -6
  28. package/lib/cjs/types/error.js +85 -12
  29. package/lib/cjs/utils.d.ts +1 -1
  30. package/lib/cjs/utils.js +2 -2
  31. package/lib/esm/actions/marketV1/index.d.ts +2 -0
  32. package/lib/esm/actions/marketV1/index.js +2 -0
  33. package/lib/esm/actions/marketV1/supply.d.ts +69 -0
  34. package/lib/esm/actions/marketV1/supply.js +124 -0
  35. package/lib/esm/actions/marketV1/supplyCollateral.d.ts +1 -1
  36. package/lib/esm/actions/marketV1/supplyCollateral.js +3 -3
  37. package/lib/esm/actions/marketV1/supplyCollateralBorrow.d.ts +1 -1
  38. package/lib/esm/actions/marketV1/supplyCollateralBorrow.js +3 -3
  39. package/lib/esm/actions/marketV1/withdraw.d.ts +83 -0
  40. package/lib/esm/actions/marketV1/withdraw.js +106 -0
  41. package/lib/esm/bundler/actions.d.ts +86 -4
  42. package/lib/esm/bundler/actions.js +143 -8
  43. package/lib/esm/bundler/types.d.ts +23 -4
  44. package/lib/esm/entities/marketV1/marketV1.d.ts +121 -18
  45. package/lib/esm/entities/marketV1/marketV1.js +147 -15
  46. package/lib/esm/entities/reallocationData.js +3 -0
  47. package/lib/esm/helpers/computeReallocations.d.ts +17 -6
  48. package/lib/esm/helpers/computeReallocations.js +45 -12
  49. package/lib/esm/helpers/index.d.ts +2 -2
  50. package/lib/esm/helpers/index.js +2 -2
  51. package/lib/esm/helpers/slippage.d.ts +57 -0
  52. package/lib/esm/helpers/slippage.js +80 -0
  53. package/lib/esm/helpers/validate.d.ts +38 -7
  54. package/lib/esm/helpers/validate.js +51 -10
  55. package/lib/esm/types/action.d.ts +27 -6
  56. package/lib/esm/types/error.d.ts +68 -6
  57. package/lib/esm/types/error.js +73 -9
  58. package/lib/esm/utils.d.ts +1 -1
  59. package/lib/esm/utils.js +1 -1
  60. package/package.json +14 -10
@@ -0,0 +1,124 @@
1
+ import { getChainAddresses } from "@morpho-org/blue-sdk";
2
+ import { deepFreeze } from "@morpho-org/morpho-ts";
3
+ import { BundlerAction } from "../../bundler/index.js";
4
+ import { addTransactionMetadata, validateNativeAsset, } from "../../helpers/index.js";
5
+ import { NegativeNativeAmountError, NegativeSupplyAmountError, NegativeSupplyMaxSharePriceError, ZeroSupplyAmountError, } from "../../types/index.js";
6
+ import { getRequirementsAction } from "../requirements/getRequirementsAction.js";
7
+ /**
8
+ * Prepares a loan-asset supply transaction for a Morpho Blue market.
9
+ *
10
+ * Routed through bundler3 via `GeneralAdapter1`. When `nativeAmount > 0`, native ETH is wrapped
11
+ * via `GeneralAdapter1.wrapNative()` before the supply; the loan token must be the chain's
12
+ * wNative for that path. Uses `maxSharePrice` to protect against share-price inflation between
13
+ * transaction construction and execution.
14
+ *
15
+ * Zero loss: all supplied assets reach Morpho. No dust left in bundler or adapter.
16
+ *
17
+ * @param params.market.chainId - The chain the market lives on.
18
+ * @param params.market.marketParams - Market params (loanToken, collateralToken, oracle, irm, lltv).
19
+ * @param params.args.amount - Amount of ERC-20 loan asset to supply. At least one of `amount` or
20
+ * `nativeAmount` must be positive. Defaults to `0n`.
21
+ * @param params.args.onBehalf - Address whose Morpho supply position is credited.
22
+ * @param params.args.maxSharePrice - Maximum acceptable supply share price (in ray). Slippage
23
+ * protection.
24
+ * @param params.args.nativeAmount - Optional amount of native token to wrap into wNative for the
25
+ * supply. Requires the loan token to be the chain's wNative.
26
+ * @param params.args.requirementSignature - Optional pre-signed permit/permit2 approval. When
27
+ * absent, the bundle uses a plain `erc20TransferFrom` and assumes the user has already
28
+ * approved `GeneralAdapter1`.
29
+ * @param params.metadata - Optional analytics metadata attached to the bundle.
30
+ * @returns A deep-frozen `Transaction<MarketV1SupplyAction>` with `to`, `value`, `data`, and the
31
+ * typed `action` discriminator the simulation layer consumes.
32
+ * @throws {NegativeSupplyAmountError} when `amount < 0n`.
33
+ * @throws {NegativeNativeAmountError} when `nativeAmount < 0n`.
34
+ * @throws {ZeroSupplyAmountError} when both `amount` and `nativeAmount` resolve to zero.
35
+ * @throws {NegativeSupplyMaxSharePriceError} when `maxSharePrice < 0n`.
36
+ * @throws {ChainWNativeMissingError} when `nativeAmount > 0n` but the chain has no configured wNative.
37
+ * @throws {NativeAmountOnNonWNativeAssetError} when `nativeAmount > 0n` but the loan token is not
38
+ * the chain's wNative.
39
+ * @throws {DepositAssetMismatchError} from `getRequirementsAction` when `requirementSignature`
40
+ * is provided and the signed asset differs from `marketParams.loanToken`.
41
+ * @throws {DepositAmountMismatchError} from `getRequirementsAction` when `requirementSignature`
42
+ * is provided and the signed amount differs from `args.amount`.
43
+ * @example
44
+ * ```ts
45
+ * import { marketV1Supply } from "@morpho-org/morpho-sdk";
46
+ *
47
+ * const tx = marketV1Supply({
48
+ * market: { chainId: 1, marketParams },
49
+ * args: {
50
+ * amount: 1_000_000_000n,
51
+ * onBehalf: supplier,
52
+ * maxSharePrice: 1_010_000_000_000_000_000_000_000_000n, // RAY-scaled, 1.01x
53
+ * },
54
+ * });
55
+ * // tx satisfies Readonly<Transaction<MarketV1SupplyAction>>
56
+ * ```
57
+ */
58
+ export const marketV1Supply = ({ market: { chainId, marketParams }, args: { amount = 0n, onBehalf, maxSharePrice, requirementSignature, nativeAmount, }, metadata, }) => {
59
+ if (amount < 0n) {
60
+ throw new NegativeSupplyAmountError(marketParams.id);
61
+ }
62
+ if (nativeAmount !== undefined && nativeAmount < 0n) {
63
+ throw new NegativeNativeAmountError(nativeAmount);
64
+ }
65
+ if (maxSharePrice < 0n) {
66
+ throw new NegativeSupplyMaxSharePriceError(marketParams.id);
67
+ }
68
+ const totalAssets = amount + (nativeAmount ?? 0n);
69
+ if (totalAssets === 0n) {
70
+ throw new ZeroSupplyAmountError(marketParams.id);
71
+ }
72
+ const { bundler3: { generalAdapter1, bundler3 }, } = getChainAddresses(chainId);
73
+ const actions = [];
74
+ if (nativeAmount !== undefined && nativeAmount > 0n) {
75
+ validateNativeAsset(chainId, marketParams.loanToken);
76
+ actions.push({
77
+ type: "nativeTransfer",
78
+ args: [bundler3, generalAdapter1, nativeAmount, false],
79
+ }, {
80
+ type: "wrapNative",
81
+ args: [nativeAmount, generalAdapter1, false],
82
+ });
83
+ }
84
+ if (amount > 0n) {
85
+ if (requirementSignature) {
86
+ actions.push(...getRequirementsAction({
87
+ asset: marketParams.loanToken,
88
+ amount,
89
+ recipient: generalAdapter1,
90
+ requirementSignature,
91
+ }));
92
+ }
93
+ else {
94
+ actions.push({
95
+ type: "erc20TransferFrom",
96
+ args: [marketParams.loanToken, amount, generalAdapter1, false],
97
+ });
98
+ }
99
+ }
100
+ actions.push({
101
+ type: "morphoSupply",
102
+ args: [marketParams, totalAssets, 0n, maxSharePrice, onBehalf, [], false],
103
+ });
104
+ let tx = {
105
+ ...BundlerAction.encodeBundle(chainId, actions),
106
+ value: nativeAmount ?? 0n,
107
+ };
108
+ if (metadata) {
109
+ tx = addTransactionMetadata(tx, metadata);
110
+ }
111
+ return deepFreeze({
112
+ ...tx,
113
+ action: {
114
+ type: "marketV1Supply",
115
+ args: {
116
+ market: marketParams.id,
117
+ amount: totalAssets,
118
+ onBehalf,
119
+ maxSharePrice,
120
+ nativeAmount,
121
+ },
122
+ },
123
+ });
124
+ };
@@ -39,7 +39,7 @@ export interface MarketV1SupplyCollateralParams {
39
39
  * @throws {NegativeNativeAmountError} when `nativeAmount < 0n`.
40
40
  * @throws {ZeroCollateralAmountError} when both `amount` and `nativeAmount` resolve to zero.
41
41
  * @throws {ChainWNativeMissingError} when `nativeAmount > 0n` but the chain has no configured wNative.
42
- * @throws {NativeAmountOnNonWNativeCollateralError} when `nativeAmount > 0n` but the collateral
42
+ * @throws {NativeAmountOnNonWNativeAssetError} when `nativeAmount > 0n` but the collateral
43
43
  * token is not the chain's wNative.
44
44
  * @throws {DepositAssetMismatchError} from `getRequirementsAction` when `requirementSignature`
45
45
  * is provided and the signed asset differs from `marketParams.collateralToken`.
@@ -1,7 +1,7 @@
1
1
  import { getChainAddresses } from "@morpho-org/blue-sdk";
2
2
  import { deepFreeze } from "@morpho-org/morpho-ts";
3
3
  import { BundlerAction } from "../../bundler/index.js";
4
- import { addTransactionMetadata, validateNativeCollateral, } from "../../helpers/index.js";
4
+ import { addTransactionMetadata, validateNativeAsset, } from "../../helpers/index.js";
5
5
  import { NegativeNativeAmountError, NonPositiveAssetAmountError, ZeroCollateralAmountError, } from "../../types/index.js";
6
6
  import { getRequirementsAction } from "../requirements/getRequirementsAction.js";
7
7
  /**
@@ -30,7 +30,7 @@ import { getRequirementsAction } from "../requirements/getRequirementsAction.js"
30
30
  * @throws {NegativeNativeAmountError} when `nativeAmount < 0n`.
31
31
  * @throws {ZeroCollateralAmountError} when both `amount` and `nativeAmount` resolve to zero.
32
32
  * @throws {ChainWNativeMissingError} when `nativeAmount > 0n` but the chain has no configured wNative.
33
- * @throws {NativeAmountOnNonWNativeCollateralError} when `nativeAmount > 0n` but the collateral
33
+ * @throws {NativeAmountOnNonWNativeAssetError} when `nativeAmount > 0n` but the collateral
34
34
  * token is not the chain's wNative.
35
35
  * @throws {DepositAssetMismatchError} from `getRequirementsAction` when `requirementSignature`
36
36
  * is provided and the signed asset differs from `marketParams.collateralToken`.
@@ -63,7 +63,7 @@ export const marketV1SupplyCollateral = ({ market: { chainId, marketParams }, ar
63
63
  const { bundler3: { generalAdapter1, bundler3 }, } = getChainAddresses(chainId);
64
64
  const actions = [];
65
65
  if (nativeAmount !== undefined && nativeAmount > 0n) {
66
- validateNativeCollateral(chainId, marketParams.collateralToken);
66
+ validateNativeAsset(chainId, marketParams.collateralToken);
67
67
  actions.push({
68
68
  type: "nativeTransfer",
69
69
  args: [bundler3, generalAdapter1, nativeAmount, false],
@@ -55,7 +55,7 @@ export interface MarketV1SupplyCollateralBorrowParams {
55
55
  * @throws {NonPositiveMinBorrowSharePriceError} when `minSharePrice < 0n`.
56
56
  * @throws {ZeroCollateralAmountError} when both `amount` and `nativeAmount` resolve to zero.
57
57
  * @throws {ChainWNativeMissingError} when `nativeAmount > 0n` but the chain has no configured wNative.
58
- * @throws {NativeAmountOnNonWNativeCollateralError} when `nativeAmount > 0n` but the collateral
58
+ * @throws {NativeAmountOnNonWNativeAssetError} when `nativeAmount > 0n` but the collateral
59
59
  * token is not the chain's wNative.
60
60
  * @throws {DepositAssetMismatchError} from `getRequirementsAction` when `requirementSignature`
61
61
  * is provided and the signed asset differs from `marketParams.collateralToken`.
@@ -1,7 +1,7 @@
1
1
  import { getChainAddresses } from "@morpho-org/blue-sdk";
2
2
  import { deepFreeze } from "@morpho-org/morpho-ts";
3
3
  import { BundlerAction } from "../../bundler/index.js";
4
- import { addTransactionMetadata, validateNativeCollateral, } from "../../helpers/index.js";
4
+ import { addTransactionMetadata, validateNativeAsset, } from "../../helpers/index.js";
5
5
  import { NegativeNativeAmountError, NonPositiveAssetAmountError, NonPositiveBorrowAmountError, NonPositiveMinBorrowSharePriceError, ZeroCollateralAmountError, } from "../../types/index.js";
6
6
  import { getRequirementsAction } from "../requirements/getRequirementsAction.js";
7
7
  import { buildReallocationActions } from "./buildReallocationActions.js";
@@ -41,7 +41,7 @@ import { buildReallocationActions } from "./buildReallocationActions.js";
41
41
  * @throws {NonPositiveMinBorrowSharePriceError} when `minSharePrice < 0n`.
42
42
  * @throws {ZeroCollateralAmountError} when both `amount` and `nativeAmount` resolve to zero.
43
43
  * @throws {ChainWNativeMissingError} when `nativeAmount > 0n` but the chain has no configured wNative.
44
- * @throws {NativeAmountOnNonWNativeCollateralError} when `nativeAmount > 0n` but the collateral
44
+ * @throws {NativeAmountOnNonWNativeAssetError} when `nativeAmount > 0n` but the collateral
45
45
  * token is not the chain's wNative.
46
46
  * @throws {DepositAssetMismatchError} from `getRequirementsAction` when `requirementSignature`
47
47
  * is provided and the signed asset differs from `marketParams.collateralToken`.
@@ -96,7 +96,7 @@ export const marketV1SupplyCollateralBorrow = ({ market: { chainId, marketParams
96
96
  const { bundler3: { generalAdapter1, bundler3 }, } = getChainAddresses(chainId);
97
97
  const actions = [];
98
98
  if (nativeAmount !== undefined && nativeAmount > 0n) {
99
- validateNativeCollateral(chainId, marketParams.collateralToken);
99
+ validateNativeAsset(chainId, marketParams.collateralToken);
100
100
  actions.push({
101
101
  type: "nativeTransfer",
102
102
  args: [bundler3, generalAdapter1, nativeAmount, false],
@@ -0,0 +1,83 @@
1
+ import type { MarketParams } from "@morpho-org/blue-sdk";
2
+ import type { Address } from "viem";
3
+ import { type MarketV1WithdrawAction, type Metadata, type Transaction, type VaultReallocation } from "../../types/index.js";
4
+ /** Parameters for {@link marketV1Withdraw}. */
5
+ export interface MarketV1WithdrawParams {
6
+ market: {
7
+ readonly chainId: number;
8
+ readonly marketParams: MarketParams;
9
+ };
10
+ args: {
11
+ /** Withdraw assets amount (`0n` when withdrawing by shares). */
12
+ assets: bigint;
13
+ /** Withdraw shares amount (`0n` when withdrawing by assets). */
14
+ shares: bigint;
15
+ /** Address that receives the withdrawn assets. */
16
+ receiver: Address;
17
+ /** Minimum withdraw share price (in ray). Slippage protection. */
18
+ minSharePrice: bigint;
19
+ /**
20
+ * Vault reallocations to execute before withdrawing. Compute via
21
+ * `MorphoMarketV1.getReallocations({ operation: "withdraw", amount })` or directly via
22
+ * `computeReallocations({ operation: "withdraw", amount, ... })`.
23
+ */
24
+ reallocations?: readonly VaultReallocation[];
25
+ };
26
+ metadata?: Metadata;
27
+ }
28
+ /**
29
+ * Prepares a loan-asset withdraw transaction for a Morpho Blue market.
30
+ *
31
+ * Routed through bundler3 via `morphoWithdraw`. Supports two modes (exactly one):
32
+ *
33
+ * - **By assets** (`assets > 0, shares = 0`): withdraws an exact asset amount.
34
+ * - **By shares** (`assets = 0, shares > 0`): burns an exact share count (typical for a full
35
+ * supplier position close; immune to interest accrual between tx construction and execution).
36
+ *
37
+ * When `reallocations` are provided, `reallocateTo` actions are prepended to the bundle, moving
38
+ * liquidity from other markets into this one via the PublicAllocator before withdrawing.
39
+ * Reallocation fees accumulate in `tx.value`. The on-chain `morphoWithdraw` sends the assets
40
+ * computed on-chain directly to `receiver`; no skim is required.
41
+ *
42
+ * The withdraw is performed on behalf of the transaction initiator (signer) — there is no
43
+ * separate `onBehalf` field; mirror `marketV1Borrow`. The entity layer keeps `receiver` aligned
44
+ * with the user when none is provided. Requires the user to have authorized `GeneralAdapter1`
45
+ * on Morpho.
46
+ *
47
+ * @param params.market.chainId - The chain the market lives on.
48
+ * @param params.market.marketParams - Market params (loanToken, collateralToken, oracle, irm, lltv).
49
+ * @param params.args.assets - Withdraw amount in loan-token assets. Set to `0n` in shares mode.
50
+ * @param params.args.shares - Withdraw amount in supply shares. Set to `0n` in assets mode.
51
+ * @param params.args.receiver - Address that receives the withdrawn assets.
52
+ * @param params.args.minSharePrice - Minimum acceptable withdraw share price (in ray). Slippage
53
+ * protection.
54
+ * @param params.args.reallocations - Optional vault reallocations to execute before withdrawing,
55
+ * computed by the entity layer.
56
+ * @param params.metadata - Optional analytics metadata attached to the bundle.
57
+ * @returns A deep-frozen `Transaction<MarketV1WithdrawAction>` with `to`, `value`, `data`, and
58
+ * the typed `action` discriminator the simulation layer consumes.
59
+ * @throws {NonPositiveWithdrawAmountError} when both `assets` and `shares` are zero, or either is negative.
60
+ * @throws {MutuallyExclusiveWithdrawAmountsError} when both `assets` and `shares` are non-zero.
61
+ * @throws {NegativeWithdrawMinSharePriceError} when `minSharePrice < 0n` (zero is allowed despite
62
+ * the class name — pattern preserved for symmetry with `marketV1Borrow`).
63
+ * @throws Reallocation errors from `buildReallocationActions` when `reallocations` is malformed
64
+ * (see its JSDoc: `NegativeReallocationFeeError`, `EmptyReallocationWithdrawalsError`,
65
+ * `NonPositiveReallocationAmountError`, `ReallocationWithdrawalOnTargetMarketError`,
66
+ * `UnsortedReallocationWithdrawalsError`).
67
+ * @example
68
+ * ```ts
69
+ * import { marketV1Withdraw } from "@morpho-org/morpho-sdk";
70
+ *
71
+ * const tx = marketV1Withdraw({
72
+ * market: { chainId: 1, marketParams },
73
+ * args: {
74
+ * assets: 1_000_000_000n,
75
+ * shares: 0n,
76
+ * receiver: supplier,
77
+ * minSharePrice: 0n, // disables slippage protection — production code should compute via `computeMinWithdrawSharePrice` from market state + slippage tolerance
78
+ * },
79
+ * });
80
+ * // tx satisfies Readonly<Transaction<MarketV1WithdrawAction>>
81
+ * ```
82
+ */
83
+ export declare const marketV1Withdraw: ({ market: { chainId, marketParams }, args: { assets, shares, receiver, minSharePrice, reallocations }, metadata, }: MarketV1WithdrawParams) => Readonly<Transaction<MarketV1WithdrawAction>>;
@@ -0,0 +1,106 @@
1
+ import { deepFreeze } from "@morpho-org/morpho-ts";
2
+ import { BundlerAction } from "../../bundler/index.js";
3
+ import { addTransactionMetadata } from "../../helpers/index.js";
4
+ import { MutuallyExclusiveWithdrawAmountsError, NegativeWithdrawMinSharePriceError, NonPositiveWithdrawAmountError, } from "../../types/index.js";
5
+ import { buildReallocationActions } from "./buildReallocationActions.js";
6
+ /**
7
+ * Prepares a loan-asset withdraw transaction for a Morpho Blue market.
8
+ *
9
+ * Routed through bundler3 via `morphoWithdraw`. Supports two modes (exactly one):
10
+ *
11
+ * - **By assets** (`assets > 0, shares = 0`): withdraws an exact asset amount.
12
+ * - **By shares** (`assets = 0, shares > 0`): burns an exact share count (typical for a full
13
+ * supplier position close; immune to interest accrual between tx construction and execution).
14
+ *
15
+ * When `reallocations` are provided, `reallocateTo` actions are prepended to the bundle, moving
16
+ * liquidity from other markets into this one via the PublicAllocator before withdrawing.
17
+ * Reallocation fees accumulate in `tx.value`. The on-chain `morphoWithdraw` sends the assets
18
+ * computed on-chain directly to `receiver`; no skim is required.
19
+ *
20
+ * The withdraw is performed on behalf of the transaction initiator (signer) — there is no
21
+ * separate `onBehalf` field; mirror `marketV1Borrow`. The entity layer keeps `receiver` aligned
22
+ * with the user when none is provided. Requires the user to have authorized `GeneralAdapter1`
23
+ * on Morpho.
24
+ *
25
+ * @param params.market.chainId - The chain the market lives on.
26
+ * @param params.market.marketParams - Market params (loanToken, collateralToken, oracle, irm, lltv).
27
+ * @param params.args.assets - Withdraw amount in loan-token assets. Set to `0n` in shares mode.
28
+ * @param params.args.shares - Withdraw amount in supply shares. Set to `0n` in assets mode.
29
+ * @param params.args.receiver - Address that receives the withdrawn assets.
30
+ * @param params.args.minSharePrice - Minimum acceptable withdraw share price (in ray). Slippage
31
+ * protection.
32
+ * @param params.args.reallocations - Optional vault reallocations to execute before withdrawing,
33
+ * computed by the entity layer.
34
+ * @param params.metadata - Optional analytics metadata attached to the bundle.
35
+ * @returns A deep-frozen `Transaction<MarketV1WithdrawAction>` with `to`, `value`, `data`, and
36
+ * the typed `action` discriminator the simulation layer consumes.
37
+ * @throws {NonPositiveWithdrawAmountError} when both `assets` and `shares` are zero, or either is negative.
38
+ * @throws {MutuallyExclusiveWithdrawAmountsError} when both `assets` and `shares` are non-zero.
39
+ * @throws {NegativeWithdrawMinSharePriceError} when `minSharePrice < 0n` (zero is allowed despite
40
+ * the class name — pattern preserved for symmetry with `marketV1Borrow`).
41
+ * @throws Reallocation errors from `buildReallocationActions` when `reallocations` is malformed
42
+ * (see its JSDoc: `NegativeReallocationFeeError`, `EmptyReallocationWithdrawalsError`,
43
+ * `NonPositiveReallocationAmountError`, `ReallocationWithdrawalOnTargetMarketError`,
44
+ * `UnsortedReallocationWithdrawalsError`).
45
+ * @example
46
+ * ```ts
47
+ * import { marketV1Withdraw } from "@morpho-org/morpho-sdk";
48
+ *
49
+ * const tx = marketV1Withdraw({
50
+ * market: { chainId: 1, marketParams },
51
+ * args: {
52
+ * assets: 1_000_000_000n,
53
+ * shares: 0n,
54
+ * receiver: supplier,
55
+ * minSharePrice: 0n, // disables slippage protection — production code should compute via `computeMinWithdrawSharePrice` from market state + slippage tolerance
56
+ * },
57
+ * });
58
+ * // tx satisfies Readonly<Transaction<MarketV1WithdrawAction>>
59
+ * ```
60
+ */
61
+ export const marketV1Withdraw = ({ market: { chainId, marketParams }, args: { assets, shares, receiver, minSharePrice, reallocations }, metadata, }) => {
62
+ // Mutual exclusion is detected on "both values present" (either non-zero),
63
+ // before sign checks — otherwise `{ assets: -1n, shares: 5n }` would be
64
+ // misreported as a positivity error rather than the actual mode conflict.
65
+ if (assets !== 0n && shares !== 0n) {
66
+ throw new MutuallyExclusiveWithdrawAmountsError(marketParams.id);
67
+ }
68
+ if (assets < 0n || shares < 0n || (assets === 0n && shares === 0n)) {
69
+ throw new NonPositiveWithdrawAmountError(marketParams.id);
70
+ }
71
+ if (minSharePrice < 0n) {
72
+ throw new NegativeWithdrawMinSharePriceError(marketParams.id);
73
+ }
74
+ const actions = [];
75
+ let reallocationFee = 0n;
76
+ if (reallocations && reallocations.length > 0) {
77
+ const result = buildReallocationActions(reallocations, marketParams);
78
+ actions.push(...result.actions);
79
+ reallocationFee = result.fee;
80
+ }
81
+ actions.push({
82
+ type: "morphoWithdraw",
83
+ args: [marketParams, assets, shares, minSharePrice, receiver, false],
84
+ });
85
+ let tx = {
86
+ ...BundlerAction.encodeBundle(chainId, actions),
87
+ value: reallocationFee,
88
+ };
89
+ if (metadata) {
90
+ tx = addTransactionMetadata(tx, metadata);
91
+ }
92
+ return deepFreeze({
93
+ ...tx,
94
+ action: {
95
+ type: "marketV1Withdraw",
96
+ args: {
97
+ market: marketParams.id,
98
+ assets,
99
+ shares,
100
+ receiver,
101
+ minSharePrice,
102
+ reallocationFee,
103
+ },
104
+ },
105
+ });
106
+ };
@@ -372,6 +372,48 @@ export declare namespace BundlerAction {
372
372
  * ```
373
373
  */
374
374
  function morphoSupplyCollateral(chainId: number, market: InputMarketParams, assets: bigint, onBehalf: Address, callbackCalls: BundlerCall[], skipRevert?: boolean): BundlerCall[];
375
+ /**
376
+ * Encodes a GeneralAdapter1 Morpho Blue supply call.
377
+ *
378
+ * @param chainId - Chain where the action will execute.
379
+ * @param market - Morpho Blue market parameters.
380
+ * @param assets - Supply asset amount.
381
+ * @param shares - Supply share amount.
382
+ * @param slippageAmount - Slippage guard amount (max share price in RAY for share-price slippage).
383
+ * @param onBehalf - Account receiving the supply position.
384
+ * @param callbackCalls - Calls executed in Morpho's callback.
385
+ * @param skipRevert - Whether Bundler3 should tolerate a revert.
386
+ * @returns Encoded Bundler3 calls.
387
+ *
388
+ * @example
389
+ * ```ts
390
+ * import { BundlerAction } from "@morpho-org/morpho-sdk/bundler";
391
+ *
392
+ * const loanToken = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
393
+ * const collateralToken = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
394
+ * const oracle = "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419";
395
+ * const irm = "0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC";
396
+ * const onBehalf = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
397
+ * const marketParams = {
398
+ * loanToken,
399
+ * collateralToken,
400
+ * oracle,
401
+ * irm,
402
+ * lltv: 860_000000000000000000n,
403
+ * };
404
+ *
405
+ * const calls = BundlerAction.morphoSupply(
406
+ * 1,
407
+ * marketParams,
408
+ * 100n,
409
+ * 0n,
410
+ * 1_010000000000000000000000000n,
411
+ * onBehalf,
412
+ * [],
413
+ * );
414
+ * ```
415
+ */
416
+ function morphoSupply(chainId: number, market: InputMarketParams, assets: bigint, shares: bigint, slippageAmount: bigint, onBehalf: Address, callbackCalls: BundlerCall[], skipRevert?: boolean): BundlerCall[];
375
417
  /**
376
418
  * Encodes a GeneralAdapter1 Morpho Blue borrow call.
377
419
  *
@@ -379,7 +421,7 @@ export declare namespace BundlerAction {
379
421
  * @param market - Morpho Blue market parameters.
380
422
  * @param assets - Borrow asset amount.
381
423
  * @param shares - Borrow share amount.
382
- * @param slippageAmount - Slippage guard amount.
424
+ * @param minSharePrice - Minimum amount of borrowed assets per borrow share minted, scaled by 1e27.
383
425
  * @param receiver - Recipient of borrowed assets.
384
426
  * @param skipRevert - Whether Bundler3 should tolerate a revert.
385
427
  * @returns Encoded Bundler3 calls.
@@ -411,7 +453,7 @@ export declare namespace BundlerAction {
411
453
  * );
412
454
  * ```
413
455
  */
414
- function morphoBorrow(chainId: number, market: InputMarketParams, assets: bigint, shares: bigint, slippageAmount: bigint, receiver: Address, skipRevert?: boolean): BundlerCall[];
456
+ function morphoBorrow(chainId: number, market: InputMarketParams, assets: bigint, shares: bigint, minSharePrice: bigint, receiver: Address, skipRevert?: boolean): BundlerCall[];
415
457
  /**
416
458
  * Encodes a GeneralAdapter1 Morpho Blue repay call.
417
459
  *
@@ -419,7 +461,7 @@ export declare namespace BundlerAction {
419
461
  * @param market - Morpho Blue market parameters.
420
462
  * @param assets - Repay asset amount.
421
463
  * @param shares - Repay share amount.
422
- * @param slippageAmount - Slippage guard amount.
464
+ * @param maxSharePrice - Maximum amount of repaid assets per borrow share redeemed, scaled by 1e27.
423
465
  * @param onBehalf - Account whose borrow is repaid.
424
466
  * @param callbackCalls - Calls executed in Morpho's callback.
425
467
  * @param skipRevert - Whether Bundler3 should tolerate a revert.
@@ -453,7 +495,47 @@ export declare namespace BundlerAction {
453
495
  * );
454
496
  * ```
455
497
  */
456
- function morphoRepay(chainId: number, market: InputMarketParams, assets: bigint, shares: bigint, slippageAmount: bigint, onBehalf: Address, callbackCalls: BundlerCall[], skipRevert?: boolean): BundlerCall[];
498
+ function morphoRepay(chainId: number, market: InputMarketParams, assets: bigint, shares: bigint, maxSharePrice: bigint, onBehalf: Address, callbackCalls: BundlerCall[], skipRevert?: boolean): BundlerCall[];
499
+ /**
500
+ * Encodes a GeneralAdapter1 Morpho Blue withdraw call.
501
+ *
502
+ * @param chainId - Chain where the action will execute.
503
+ * @param market - Morpho Blue market parameters.
504
+ * @param assets - Withdraw asset amount.
505
+ * @param shares - Withdraw share amount.
506
+ * @param slippageAmount - Slippage guard amount (min share price in RAY for share-price slippage).
507
+ * @param receiver - Recipient of withdrawn assets.
508
+ * @param skipRevert - Whether Bundler3 should tolerate a revert.
509
+ * @returns Encoded Bundler3 calls.
510
+ *
511
+ * @example
512
+ * ```ts
513
+ * import { BundlerAction } from "@morpho-org/morpho-sdk/bundler";
514
+ *
515
+ * const loanToken = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
516
+ * const collateralToken = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
517
+ * const oracle = "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419";
518
+ * const irm = "0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC";
519
+ * const receiver = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
520
+ * const marketParams = {
521
+ * loanToken,
522
+ * collateralToken,
523
+ * oracle,
524
+ * irm,
525
+ * lltv: 860_000000000000000000n,
526
+ * };
527
+ *
528
+ * const calls = BundlerAction.morphoWithdraw(
529
+ * 1,
530
+ * marketParams,
531
+ * 100n,
532
+ * 0n,
533
+ * 990000000000000000000000000n,
534
+ * receiver,
535
+ * );
536
+ * ```
537
+ */
538
+ function morphoWithdraw(chainId: number, market: InputMarketParams, assets: bigint, shares: bigint, slippageAmount: bigint, receiver: Address, skipRevert?: boolean): BundlerCall[];
457
539
  /**
458
540
  * Encodes a GeneralAdapter1 Morpho Blue withdraw-collateral call.
459
541
  *