@morpho-org/morpho-sdk 5.9.0 → 6.0.0-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (141) hide show
  1. package/MIGRATION-v5-to-v6.md +201 -0
  2. package/README.md +58 -40
  3. package/lib/cjs/actions/blue/borrow.d.ts +62 -61
  4. package/lib/cjs/actions/blue/borrow.js +48 -80
  5. package/lib/cjs/actions/blue/common.d.ts +99 -0
  6. package/lib/cjs/actions/blue/common.js +318 -0
  7. package/lib/cjs/actions/blue/refinance.d.ts +72 -112
  8. package/lib/cjs/actions/blue/refinance.js +97 -199
  9. package/lib/cjs/actions/blue/repay.d.ts +72 -68
  10. package/lib/cjs/actions/blue/repay.js +56 -118
  11. package/lib/cjs/actions/blue/repayWithdrawCollateral.d.ts +85 -90
  12. package/lib/cjs/actions/blue/repayWithdrawCollateral.js +148 -137
  13. package/lib/cjs/actions/blue/supply.d.ts +62 -45
  14. package/lib/cjs/actions/blue/supply.js +94 -70
  15. package/lib/cjs/actions/blue/supplyCollateral.d.ts +63 -40
  16. package/lib/cjs/actions/blue/supplyCollateral.js +52 -69
  17. package/lib/cjs/actions/blue/supplyCollateralBorrow.d.ts +82 -90
  18. package/lib/cjs/actions/blue/supplyCollateralBorrow.js +133 -120
  19. package/lib/cjs/actions/blue/withdraw.d.ts +68 -79
  20. package/lib/cjs/actions/blue/withdraw.js +101 -97
  21. package/lib/cjs/actions/blue/withdrawCollateral.d.ts +56 -28
  22. package/lib/cjs/actions/blue/withdrawCollateral.js +45 -50
  23. package/lib/cjs/actions/index.d.ts +1 -1
  24. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +29 -17
  25. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +60 -19
  26. package/lib/cjs/actions/requirements/blue/getBlueBundlesV1TokenRequirements.d.ts +80 -0
  27. package/lib/cjs/actions/requirements/blue/getBlueBundlesV1TokenRequirements.js +207 -0
  28. package/lib/cjs/actions/requirements/blue/getUnusedPermit2Nonce.d.ts +48 -0
  29. package/lib/cjs/actions/requirements/blue/getUnusedPermit2Nonce.js +72 -0
  30. package/lib/cjs/actions/requirements/blue/index.d.ts +2 -0
  31. package/lib/cjs/actions/requirements/blue/index.js +2 -0
  32. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +10 -7
  33. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +30 -5
  34. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +2 -1
  35. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +7 -2
  36. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +16 -7
  37. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +33 -6
  38. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2TransferFrom.d.ts +61 -0
  39. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2TransferFrom.js +123 -0
  40. package/lib/cjs/actions/requirements/encode/index.d.ts +1 -0
  41. package/lib/cjs/actions/requirements/encode/index.js +1 -0
  42. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +1 -1
  43. package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +5 -4
  44. package/lib/cjs/actions/requirements/getRequirementsApproval.js +5 -4
  45. package/lib/cjs/actions/signatures/getTokenRequirementActions.d.ts +1 -0
  46. package/lib/cjs/actions/signatures/getTokenRequirementActions.js +4 -0
  47. package/lib/cjs/bundler/actions.d.ts +4 -4
  48. package/lib/cjs/bundler/actions.js +4 -4
  49. package/lib/cjs/client/morphoViemExtension.d.ts +2 -1
  50. package/lib/cjs/client/morphoViemExtension.js +2 -1
  51. package/lib/cjs/entities/blue/blue.d.ts +806 -374
  52. package/lib/cjs/entities/blue/blue.js +447 -696
  53. package/lib/cjs/entities/index.d.ts +1 -0
  54. package/lib/cjs/helpers/computeVaultV1Reallocations.d.ts +3 -9
  55. package/lib/cjs/helpers/computeVaultV1Reallocations.js +3 -9
  56. package/lib/cjs/helpers/validate.d.ts +14 -23
  57. package/lib/cjs/helpers/validate.js +30 -48
  58. package/lib/cjs/helpers/validateRequirementSpender.d.ts +2 -1
  59. package/lib/cjs/helpers/validateRequirementSpender.js +2 -0
  60. package/lib/cjs/types/action.d.ts +146 -162
  61. package/lib/cjs/types/action.js +29 -2
  62. package/lib/cjs/types/client.d.ts +9 -0
  63. package/lib/cjs/types/error.d.ts +141 -14
  64. package/lib/cjs/types/error.js +198 -21
  65. package/lib/cjs/types/sharedLiquidity.d.ts +0 -7
  66. package/lib/esm/actions/blue/borrow.d.ts +62 -61
  67. package/lib/esm/actions/blue/borrow.js +48 -80
  68. package/lib/esm/actions/blue/common.d.ts +99 -0
  69. package/lib/esm/actions/blue/common.js +306 -0
  70. package/lib/esm/actions/blue/refinance.d.ts +72 -112
  71. package/lib/esm/actions/blue/refinance.js +98 -200
  72. package/lib/esm/actions/blue/repay.d.ts +72 -68
  73. package/lib/esm/actions/blue/repay.js +56 -118
  74. package/lib/esm/actions/blue/repayWithdrawCollateral.d.ts +85 -90
  75. package/lib/esm/actions/blue/repayWithdrawCollateral.js +149 -138
  76. package/lib/esm/actions/blue/supply.d.ts +62 -45
  77. package/lib/esm/actions/blue/supply.js +94 -70
  78. package/lib/esm/actions/blue/supplyCollateral.d.ts +63 -40
  79. package/lib/esm/actions/blue/supplyCollateral.js +52 -69
  80. package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +82 -90
  81. package/lib/esm/actions/blue/supplyCollateralBorrow.js +134 -121
  82. package/lib/esm/actions/blue/withdraw.d.ts +68 -79
  83. package/lib/esm/actions/blue/withdraw.js +100 -96
  84. package/lib/esm/actions/blue/withdrawCollateral.d.ts +56 -28
  85. package/lib/esm/actions/blue/withdrawCollateral.js +45 -50
  86. package/lib/esm/actions/index.d.ts +1 -1
  87. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +29 -17
  88. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +63 -22
  89. package/lib/esm/actions/requirements/blue/getBlueBundlesV1TokenRequirements.d.ts +80 -0
  90. package/lib/esm/actions/requirements/blue/getBlueBundlesV1TokenRequirements.js +203 -0
  91. package/lib/esm/actions/requirements/blue/getUnusedPermit2Nonce.d.ts +48 -0
  92. package/lib/esm/actions/requirements/blue/getUnusedPermit2Nonce.js +68 -0
  93. package/lib/esm/actions/requirements/blue/index.d.ts +2 -0
  94. package/lib/esm/actions/requirements/blue/index.js +2 -0
  95. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +10 -7
  96. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +31 -6
  97. package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +2 -1
  98. package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +8 -3
  99. package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +16 -7
  100. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +34 -7
  101. package/lib/esm/actions/requirements/encode/encodeErc20Permit2TransferFrom.d.ts +61 -0
  102. package/lib/esm/actions/requirements/encode/encodeErc20Permit2TransferFrom.js +119 -0
  103. package/lib/esm/actions/requirements/encode/index.d.ts +1 -0
  104. package/lib/esm/actions/requirements/encode/index.js +1 -0
  105. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +1 -1
  106. package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +5 -4
  107. package/lib/esm/actions/requirements/getRequirementsApproval.js +5 -4
  108. package/lib/esm/actions/signatures/getTokenRequirementActions.d.ts +1 -0
  109. package/lib/esm/actions/signatures/getTokenRequirementActions.js +5 -1
  110. package/lib/esm/bundler/actions.d.ts +4 -4
  111. package/lib/esm/bundler/actions.js +4 -4
  112. package/lib/esm/client/morphoViemExtension.d.ts +2 -1
  113. package/lib/esm/client/morphoViemExtension.js +2 -1
  114. package/lib/esm/entities/blue/blue.d.ts +806 -374
  115. package/lib/esm/entities/blue/blue.js +453 -702
  116. package/lib/esm/entities/index.d.ts +1 -0
  117. package/lib/esm/helpers/computeVaultV1Reallocations.d.ts +3 -9
  118. package/lib/esm/helpers/computeVaultV1Reallocations.js +3 -9
  119. package/lib/esm/helpers/validate.d.ts +14 -23
  120. package/lib/esm/helpers/validate.js +30 -48
  121. package/lib/esm/helpers/validateRequirementSpender.d.ts +2 -1
  122. package/lib/esm/helpers/validateRequirementSpender.js +2 -0
  123. package/lib/esm/types/action.d.ts +146 -162
  124. package/lib/esm/types/action.js +28 -2
  125. package/lib/esm/types/client.d.ts +9 -0
  126. package/lib/esm/types/error.d.ts +141 -14
  127. package/lib/esm/types/error.js +184 -17
  128. package/lib/esm/types/sharedLiquidity.d.ts +0 -7
  129. package/package.json +4 -3
  130. package/lib/cjs/actions/blue/buildAssetFundingActions.d.ts +0 -47
  131. package/lib/cjs/actions/blue/buildAssetFundingActions.js +0 -63
  132. package/lib/cjs/actions/blue/buildReallocationActions.d.ts +0 -69
  133. package/lib/cjs/actions/blue/buildReallocationActions.js +0 -141
  134. package/lib/cjs/actions/blue/resolveRepayFunding.d.ts +0 -66
  135. package/lib/cjs/actions/blue/resolveRepayFunding.js +0 -85
  136. package/lib/esm/actions/blue/buildAssetFundingActions.d.ts +0 -47
  137. package/lib/esm/actions/blue/buildAssetFundingActions.js +0 -59
  138. package/lib/esm/actions/blue/buildReallocationActions.d.ts +0 -69
  139. package/lib/esm/actions/blue/buildReallocationActions.js +0 -135
  140. package/lib/esm/actions/blue/resolveRepayFunding.d.ts +0 -66
  141. package/lib/esm/actions/blue/resolveRepayFunding.js +0 -81
@@ -1,97 +1,121 @@
1
- import { deepFreeze } from "@morpho-org/morpho-ts";
2
- import { BundlerAction } from "../../bundler/index.js";
3
- import { addTransactionMetadata } from "../../helpers/index.js";
4
- import { NegativeInputError, NonPositiveInputError, } from "../../types/index.js";
5
- import { buildAssetFundingActions } from "./buildAssetFundingActions.js";
1
+ import { encodeFunctionData, maxUint256 } from "viem";
2
+ import { blueBundlesV1Abi } from "../../abis.js";
3
+ import { InputExceedsMaxError, NonPositiveInputError, } from "../../types/index.js";
4
+ import { finalizeBlueBundlesV1Transaction, getBlueBundlesV1TokenPermit, normalizeBlueBundlesV1CommonParams, validateBlueBundlesV1NativeFunding, } from "./common.js";
6
5
  /**
7
- * Prepares a loan-asset supply transaction for a Morpho Blue market.
6
+ * Encodes a direct BlueBundlesV1 loan-asset supply transaction.
8
7
  *
9
- * Routed through bundler3 via `GeneralAdapter1`. When `nativeAmount > 0`, native ETH is wrapped
10
- * via `GeneralAdapter1.wrapNative()` before the supply; the loan token must be the chain's
11
- * wNative for that path. Uses `maxSharePrice` to protect against share-price inflation between
12
- * transaction construction and execution.
8
+ * `assets` is the gross funded amount; BlueBundlesV1 deducts the referral fee before supplying.
9
+ * This route has no Bundler3 share-price bound or `slippageTolerance` input.
13
10
  *
14
- * Zero loss: all supplied assets reach Morpho. No dust left in bundler or adapter.
15
- *
16
- * @param params.market.chainId - The chain the market lives on.
17
- * @param params.market.marketParams - Market params (loanToken, collateralToken, oracle, irm, lltv).
18
- * @param params.args.amount - Amount of ERC-20 loan asset to supply. At least one of `amount` or
19
- * `nativeAmount` must be positive. Defaults to `0n`.
20
- * @param params.args.onBehalf - Address whose Morpho supply position is credited.
21
- * @param params.args.maxSharePrice - Maximum acceptable supply share price (in ray). Slippage
22
- * protection.
23
- * @param params.args.nativeAmount - Optional amount of native token to wrap into wNative for the
24
- * supply. Requires the loan token to be the chain's wNative.
25
- * @param params.args.requirementSignature - Optional pre-signed permit/permit2 approval.
26
- * @param params.metadata - Optional analytics metadata attached to the bundle.
27
- * @returns A deep-frozen `Transaction<BlueSupplyAction>` with `to`, `value`, `data`, and the
28
- * typed `action` discriminator the simulation layer consumes.
29
- * @throws {NegativeInputError} when `amount`, `nativeAmount`, or `maxSharePrice` is negative.
30
- * @throws {NonPositiveInputError} when both `amount` and `nativeAmount` resolve to zero.
31
- * @throws {ChainWNativeMissingError} when `nativeAmount > 0n` but the chain has no configured wNative.
32
- * @throws {NativeAmountOnNonWNativeAssetError} when `nativeAmount > 0n` but the loan token is not
33
- * the chain's wNative.
34
- * @throws {DepositAssetMismatchError} from `getTokenRequirementActions` when `requirementSignature`
35
- * is provided and the signed asset differs from `marketParams.loanToken`.
36
- * @throws {DepositAmountMismatchError} from `getTokenRequirementActions` when `requirementSignature`
37
- * is provided and the signed amount differs from `args.amount`.
11
+ * @param params - Supply encoding parameters.
12
+ * @param params.market.chainId - Chain containing the BlueBundlesV1 deployment.
13
+ * @param params.market.marketParams - Scoped Morpho Blue market parameters.
14
+ * @param params.args.userAddress - User funding and receiving the position.
15
+ * @param params.args.assets - Gross loan-token funding amount.
16
+ * @param params.args.nativeAmount - Exclusive native funding; must equal `assets`.
17
+ * @param params.args.deadline - Final call deadline in Unix seconds.
18
+ * @param params.args.referralFeePct - Optional WAD-scaled fee below 100%.
19
+ * @param params.args.referralFeeRecipient - Recipient required for a positive fee.
20
+ * @param params.args.requirementSignature - Optional ERC-2612 or Permit2 SignatureTransfer result.
21
+ * @param params.metadata - Optional transaction metadata.
22
+ * @returns A deep-frozen `Readonly<Transaction<BlueSupplyAction>>` whose `to` address is
23
+ * BlueBundlesV1 and whose `action` records the normalized supply inputs.
24
+ * @throws {NonPositiveInputError} when `assets` or `deadline` is not positive.
25
+ * @throws {NegativeInputError} when `nativeAmount` or `referralFeePct` is negative.
26
+ * @throws {InputExceedsMaxError} when `assets` exceeds `uint256` or `referralFeePct` is at least WAD.
27
+ * @throws {MissingReferralFeeRecipientError} when a positive fee has no recipient.
28
+ * @throws {NativeFundingAmountMismatchError} when native funding does not equal `assets`.
29
+ * @throws {ChainWNativeMissingError} when native funding is requested on a chain without wNative.
30
+ * @throws {NativeAmountOnNonWNativeAssetError} when native funding targets another token.
31
+ * @throws {UnexpectedRequirementSignatureError} when native funding also supplies a token signature.
32
+ * @throws {DepositOwnerMismatchError} when the signed owner differs from `userAddress`.
33
+ * @throws {DepositAssetMismatchError} when the signed asset differs from the loan token.
34
+ * @throws {DepositAmountMismatchError} when the signed amount differs from `assets`.
35
+ * @throws {DepositSpenderMismatchError} when the signed spender is not BlueBundlesV1.
36
+ * @throws {BlueBundlesV1RequirementSignatureMismatchError} when the signature kind or encoding is invalid.
37
+ * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
38
+ * @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
38
39
  * @example
39
40
  * ```ts
41
+ * import { markets } from "@morpho-org/morpho-test";
40
42
  * import { blueSupply } from "@morpho-org/morpho-sdk";
43
+ * import { zeroAddress } from "viem";
44
+ * import { mainnet } from "viem/chains";
41
45
  *
46
+ * const marketParams = markets[mainnet.id].usdc_wbtc;
42
47
  * const tx = blueSupply({
43
- * market: { chainId: 1, marketParams },
48
+ * market: { chainId: mainnet.id, marketParams },
44
49
  * args: {
45
- * amount: 1_000_000_000n,
46
- * onBehalf: supplier,
47
- * maxSharePrice: 1_010_000_000_000_000_000_000_000_000n, // RAY-scaled, 1.01x
50
+ * userAddress: zeroAddress,
51
+ * assets: 1_000_000n,
52
+ * deadline: 1_900_000_000n,
48
53
  * },
49
54
  * });
50
55
  * // tx satisfies Readonly<Transaction<BlueSupplyAction>>
51
56
  * ```
52
57
  */
53
- export const blueSupply = ({ market: { chainId, marketParams }, args: { amount = 0n, onBehalf, maxSharePrice, requirementSignature, nativeAmount, }, metadata, }) => {
54
- if (amount < 0n) {
55
- throw new NegativeInputError("amount", amount);
56
- }
57
- if (nativeAmount !== undefined && nativeAmount < 0n) {
58
- throw new NegativeInputError("nativeAmount", nativeAmount);
59
- }
60
- if (maxSharePrice < 0n) {
61
- throw new NegativeInputError("maxSharePrice", maxSharePrice);
58
+ export const blueSupply = (params) => {
59
+ const { chainId, marketParams } = params.market;
60
+ const { assets, userAddress, requirementSignature } = params.args;
61
+ if (assets <= 0n) {
62
+ throw new NonPositiveInputError("assets", assets);
62
63
  }
63
- const totalAssets = amount + (nativeAmount ?? 0n);
64
- if (totalAssets === 0n) {
65
- throw new NonPositiveInputError("totalAssets", totalAssets);
64
+ if (assets > maxUint256) {
65
+ throw new InputExceedsMaxError({
66
+ field: "assets",
67
+ value: assets,
68
+ max: maxUint256,
69
+ });
66
70
  }
67
- const actions = buildAssetFundingActions({
71
+ const common = {
72
+ chainId,
73
+ userAddress,
74
+ deadline: params.args.deadline,
75
+ referralFeePct: params.args.referralFeePct,
76
+ referralFeeRecipient: params.args.referralFeeRecipient,
77
+ metadata: params.metadata,
78
+ };
79
+ const { referralFeePct, referralFeeRecipient } = normalizeBlueBundlesV1CommonParams(common);
80
+ const value = validateBlueBundlesV1NativeFunding({
68
81
  chainId,
69
- asset: marketParams.loanToken,
70
- erc20Amount: amount,
71
- nativeAmount: nativeAmount ?? 0n,
82
+ token: marketParams.loanToken,
83
+ fundedAmount: assets,
84
+ nativeAmount: params.args.nativeAmount,
72
85
  requirementSignature,
73
86
  });
74
- actions.push({
75
- type: "morphoSupply",
76
- args: [marketParams, totalAssets, 0n, maxSharePrice, onBehalf, [], false],
87
+ const tokenPermit = getBlueBundlesV1TokenPermit({
88
+ chainId,
89
+ userAddress,
90
+ token: marketParams.loanToken,
91
+ amount: assets,
92
+ requirementSignature,
77
93
  });
78
- let tx = {
79
- ...BundlerAction.encodeBundle(chainId, actions),
80
- value: nativeAmount ?? 0n,
81
- };
82
- if (metadata) {
83
- tx = addTransactionMetadata(tx, metadata);
84
- }
85
- return deepFreeze({
86
- ...tx,
94
+ return finalizeBlueBundlesV1Transaction({
95
+ common,
96
+ value,
97
+ data: encodeFunctionData({
98
+ abi: blueBundlesV1Abi,
99
+ functionName: "blueBundlesV1Supply",
100
+ args: [
101
+ marketParams,
102
+ assets,
103
+ tokenPermit,
104
+ referralFeePct,
105
+ referralFeeRecipient,
106
+ params.args.deadline,
107
+ ],
108
+ }),
87
109
  action: {
88
110
  type: "blueSupply",
89
111
  args: {
90
112
  market: marketParams.id,
91
- amount: totalAssets,
92
- onBehalf,
93
- maxSharePrice,
94
- nativeAmount,
113
+ assets,
114
+ onBehalf: userAddress,
115
+ nativeAmount: value > 0n ? value : undefined,
116
+ referralFeePct,
117
+ referralFeeRecipient,
118
+ deadline: params.args.deadline,
95
119
  },
96
120
  },
97
121
  });
@@ -1,60 +1,83 @@
1
1
  import type { MarketParams } from "@morpho-org/blue-sdk";
2
- import type { Address } from "viem";
3
- import { type BlueSupplyCollateralAction, type DepositAmountArgs, type Metadata, type PermitRequirementSignature, type Transaction } from "../../types/index.js";
2
+ import { type Address } from "viem";
3
+ import type { BlueBundlesV1TokenRequirementSignature, BlueSupplyCollateralAction, Metadata, Transaction } from "../../types/index.js";
4
4
  /** Parameters for {@link blueSupplyCollateral}. */
5
5
  export interface BlueSupplyCollateralParams {
6
- market: {
6
+ /** Chain and scoped Morpho Blue market. */
7
+ readonly market: {
7
8
  readonly chainId: number;
8
9
  readonly marketParams: MarketParams;
9
10
  };
10
- args: DepositAmountArgs & {
11
- /** Address whose Morpho collateral position is credited. */
12
- onBehalf: Address;
13
- /** Optional pre-signed permit/permit2 approval for the collateral transfer. */
14
- requirementSignature?: PermitRequirementSignature;
11
+ /** Direct BlueBundlesV1 collateral-supply arguments. */
12
+ readonly args: {
13
+ /** User whose collateral position is credited. */
14
+ readonly userAddress: Address;
15
+ /** Gross collateral assets supplied. */
16
+ readonly collateralAssets: bigint;
17
+ /** Full native collateral funding; must equal `collateralAssets`. */
18
+ readonly nativeAmount?: bigint;
19
+ /** Final call deadline in Unix seconds. */
20
+ readonly deadline: bigint;
21
+ /** Optional WAD-scaled referral fee, strictly below 100%. */
22
+ readonly referralFeePct?: bigint;
23
+ /** Recipient required when `referralFeePct` is positive. */
24
+ readonly referralFeeRecipient?: Address;
25
+ /** Optional collateral ERC-2612 or Permit2 SignatureTransfer result. */
26
+ readonly requirementSignature?: BlueBundlesV1TokenRequirementSignature;
15
27
  };
16
- metadata?: Metadata;
28
+ /** Optional transaction metadata suffix. */
29
+ readonly metadata?: Metadata;
17
30
  }
18
31
  /**
19
- * Prepares a supply-collateral transaction for a Morpho Blue market.
32
+ * Encodes a pure collateral supply through BlueBundlesV1.
20
33
  *
21
- * Routed through bundler3 via `GeneralAdapter1`. When `nativeAmount > 0`, native ETH is wrapped
22
- * via `GeneralAdapter1.wrapNative()` before the collateral supply; the collateral token must be
23
- * the chain's wNative for that path.
34
+ * Delegates to {@link blueSupplyCollateralBorrow} with a zero borrow leg and an unrestricted LTV
35
+ * cap. Native funding is exclusive with ERC-20 funding and no Bundler3 action is encoded.
24
36
  *
25
- * Zero loss: all collateral reaches Morpho. No dust left in bundler or adapter.
26
- *
27
- * @param params.market.chainId - The chain the market lives on.
28
- * @param params.market.marketParams - Market params (loanToken, collateralToken, oracle, irm, lltv).
29
- * @param params.args.amount - Amount of ERC-20 collateral to supply. At least one of `amount` or
30
- * `nativeAmount` must be positive. Defaults to `0n`.
31
- * @param params.args.onBehalf - Address whose Morpho position is credited with the collateral.
32
- * @param params.args.requirementSignature - Optional pre-signed permit/permit2 approval.
33
- * @param params.args.nativeAmount - Optional amount of native token to wrap into wNative for the
34
- * supply. Requires the collateral token to be the chain's wNative.
35
- * @param params.metadata - Optional analytics metadata attached to the bundle.
36
- * @returns A deep-frozen `Transaction<BlueSupplyCollateralAction>` with `to`, `value`, `data`,
37
- * and the typed `action` discriminator the simulation layer consumes.
38
- * @throws {NegativeInputError} when `amount < 0n` or `nativeAmount < 0n`.
39
- * @throws {NonPositiveInputError} when both `amount` and `nativeAmount` resolve to zero.
40
- * @throws {ChainWNativeMissingError} when `nativeAmount > 0n` but the chain has no configured wNative.
41
- * @throws {NativeAmountOnNonWNativeAssetError} when `nativeAmount > 0n` but the collateral
42
- * token is not the chain's wNative.
43
- * @throws {DepositAssetMismatchError} from `getTokenRequirementActions` when `requirementSignature`
44
- * is provided and the signed asset differs from `marketParams.collateralToken`.
45
- * @throws {DepositAmountMismatchError} from `getTokenRequirementActions` when `requirementSignature`
46
- * is provided and the signed amount differs from `args.amount`.
47
- * @throws {Permit2ExpirationMissingError} from `getTokenRequirementActions` when a Permit2 requirement
48
- * signature is missing its expiration.
37
+ * @param params - Collateral-supply encoding parameters.
38
+ * @param params.market.chainId - Chain containing BlueBundlesV1.
39
+ * @param params.market.marketParams - Scoped Morpho Blue market parameters.
40
+ * @param params.args.userAddress - User whose collateral position is credited.
41
+ * @param params.args.collateralAssets - Gross collateral assets supplied.
42
+ * @param params.args.nativeAmount - Exclusive native collateral funding.
43
+ * @param params.args.deadline - Final call deadline in Unix seconds.
44
+ * @param params.args.referralFeePct - Optional WAD-scaled referral fee.
45
+ * @param params.args.referralFeeRecipient - Recipient required for a positive fee.
46
+ * @param params.args.requirementSignature - Optional collateral-token signature.
47
+ * @param params.metadata - Optional transaction metadata.
48
+ * @returns A deep-frozen `Readonly<Transaction<BlueSupplyCollateralAction>>` whose calldata
49
+ * invokes the combined BlueBundlesV1 entrypoint with a zero borrow leg.
50
+ * @throws {NegativeInputError} when an amount or fee is negative.
51
+ * @throws {NonPositiveInputError} when collateral or deadline is not positive.
52
+ * @throws {NativeFundingAmountMismatchError} when native funding is partial or mixed.
53
+ * @throws {ChainWNativeMissingError} when native funding is requested without a registered wNative.
54
+ * @throws {NativeAmountOnNonWNativeAssetError} when native funding targets another collateral token.
55
+ * @throws {UnexpectedRequirementSignatureError} when native funding also supplies a token signature.
56
+ * @throws {DepositOwnerMismatchError} when the signed owner differs from `userAddress`.
57
+ * @throws {DepositAssetMismatchError} when the signed asset differs from the collateral token.
58
+ * @throws {DepositAmountMismatchError} when the signed amount differs from `collateralAssets`.
59
+ * @throws {DepositSpenderMismatchError} when the signed spender is not BlueBundlesV1.
60
+ * @throws {BlueBundlesV1RequirementSignatureMismatchError} when a signature cannot be encoded safely.
61
+ * @throws {InputExceedsMaxError} when the referral fee is at least WAD.
62
+ * @throws {MissingReferralFeeRecipientError} when a positive fee has no recipient.
63
+ * @throws {UnsupportedChainIdError} when the chain is absent from the registry.
64
+ * @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
49
65
  * @example
50
66
  * ```ts
67
+ * import { markets } from "@morpho-org/morpho-test";
51
68
  * import { blueSupplyCollateral } from "@morpho-org/morpho-sdk";
69
+ * import { zeroAddress } from "viem";
70
+ * import { mainnet } from "viem/chains";
52
71
  *
53
72
  * const tx = blueSupplyCollateral({
54
- * market: { chainId: 1, marketParams },
55
- * args: { amount: 1_000_000_000_000_000_000n, onBehalf },
73
+ * market: { chainId: mainnet.id, marketParams: markets[mainnet.id].usdc_wbtc },
74
+ * args: {
75
+ * userAddress: zeroAddress,
76
+ * collateralAssets: 1_000_000_000_000_000_000n,
77
+ * deadline: 1_900_000_000n,
78
+ * },
56
79
  * });
57
80
  * // tx satisfies Readonly<Transaction<BlueSupplyCollateralAction>>
58
81
  * ```
59
82
  */
60
- export declare const blueSupplyCollateral: ({ market: { chainId, marketParams }, args: { amount, onBehalf, requirementSignature, nativeAmount }, metadata, }: BlueSupplyCollateralParams) => Readonly<Transaction<BlueSupplyCollateralAction>>;
83
+ export declare const blueSupplyCollateral: (params: BlueSupplyCollateralParams) => Readonly<Transaction<BlueSupplyCollateralAction>>;
@@ -1,86 +1,69 @@
1
1
  import { deepFreeze } from "@morpho-org/morpho-ts";
2
- import { BundlerAction } from "../../bundler/index.js";
3
- import { addTransactionMetadata } from "../../helpers/index.js";
4
- import { NegativeInputError, NonPositiveInputError, } from "../../types/index.js";
5
- import { buildAssetFundingActions } from "./buildAssetFundingActions.js";
2
+ import { maxUint256 } from "viem";
3
+ import { blueSupplyCollateralBorrow } from "./supplyCollateralBorrow.js";
6
4
  /**
7
- * Prepares a supply-collateral transaction for a Morpho Blue market.
5
+ * Encodes a pure collateral supply through BlueBundlesV1.
8
6
  *
9
- * Routed through bundler3 via `GeneralAdapter1`. When `nativeAmount > 0`, native ETH is wrapped
10
- * via `GeneralAdapter1.wrapNative()` before the collateral supply; the collateral token must be
11
- * the chain's wNative for that path.
7
+ * Delegates to {@link blueSupplyCollateralBorrow} with a zero borrow leg and an unrestricted LTV
8
+ * cap. Native funding is exclusive with ERC-20 funding and no Bundler3 action is encoded.
12
9
  *
13
- * Zero loss: all collateral reaches Morpho. No dust left in bundler or adapter.
14
- *
15
- * @param params.market.chainId - The chain the market lives on.
16
- * @param params.market.marketParams - Market params (loanToken, collateralToken, oracle, irm, lltv).
17
- * @param params.args.amount - Amount of ERC-20 collateral to supply. At least one of `amount` or
18
- * `nativeAmount` must be positive. Defaults to `0n`.
19
- * @param params.args.onBehalf - Address whose Morpho position is credited with the collateral.
20
- * @param params.args.requirementSignature - Optional pre-signed permit/permit2 approval.
21
- * @param params.args.nativeAmount - Optional amount of native token to wrap into wNative for the
22
- * supply. Requires the collateral token to be the chain's wNative.
23
- * @param params.metadata - Optional analytics metadata attached to the bundle.
24
- * @returns A deep-frozen `Transaction<BlueSupplyCollateralAction>` with `to`, `value`, `data`,
25
- * and the typed `action` discriminator the simulation layer consumes.
26
- * @throws {NegativeInputError} when `amount < 0n` or `nativeAmount < 0n`.
27
- * @throws {NonPositiveInputError} when both `amount` and `nativeAmount` resolve to zero.
28
- * @throws {ChainWNativeMissingError} when `nativeAmount > 0n` but the chain has no configured wNative.
29
- * @throws {NativeAmountOnNonWNativeAssetError} when `nativeAmount > 0n` but the collateral
30
- * token is not the chain's wNative.
31
- * @throws {DepositAssetMismatchError} from `getTokenRequirementActions` when `requirementSignature`
32
- * is provided and the signed asset differs from `marketParams.collateralToken`.
33
- * @throws {DepositAmountMismatchError} from `getTokenRequirementActions` when `requirementSignature`
34
- * is provided and the signed amount differs from `args.amount`.
35
- * @throws {Permit2ExpirationMissingError} from `getTokenRequirementActions` when a Permit2 requirement
36
- * signature is missing its expiration.
10
+ * @param params - Collateral-supply encoding parameters.
11
+ * @param params.market.chainId - Chain containing BlueBundlesV1.
12
+ * @param params.market.marketParams - Scoped Morpho Blue market parameters.
13
+ * @param params.args.userAddress - User whose collateral position is credited.
14
+ * @param params.args.collateralAssets - Gross collateral assets supplied.
15
+ * @param params.args.nativeAmount - Exclusive native collateral funding.
16
+ * @param params.args.deadline - Final call deadline in Unix seconds.
17
+ * @param params.args.referralFeePct - Optional WAD-scaled referral fee.
18
+ * @param params.args.referralFeeRecipient - Recipient required for a positive fee.
19
+ * @param params.args.requirementSignature - Optional collateral-token signature.
20
+ * @param params.metadata - Optional transaction metadata.
21
+ * @returns A deep-frozen `Readonly<Transaction<BlueSupplyCollateralAction>>` whose calldata
22
+ * invokes the combined BlueBundlesV1 entrypoint with a zero borrow leg.
23
+ * @throws {NegativeInputError} when an amount or fee is negative.
24
+ * @throws {NonPositiveInputError} when collateral or deadline is not positive.
25
+ * @throws {NativeFundingAmountMismatchError} when native funding is partial or mixed.
26
+ * @throws {ChainWNativeMissingError} when native funding is requested without a registered wNative.
27
+ * @throws {NativeAmountOnNonWNativeAssetError} when native funding targets another collateral token.
28
+ * @throws {UnexpectedRequirementSignatureError} when native funding also supplies a token signature.
29
+ * @throws {DepositOwnerMismatchError} when the signed owner differs from `userAddress`.
30
+ * @throws {DepositAssetMismatchError} when the signed asset differs from the collateral token.
31
+ * @throws {DepositAmountMismatchError} when the signed amount differs from `collateralAssets`.
32
+ * @throws {DepositSpenderMismatchError} when the signed spender is not BlueBundlesV1.
33
+ * @throws {BlueBundlesV1RequirementSignatureMismatchError} when a signature cannot be encoded safely.
34
+ * @throws {InputExceedsMaxError} when the referral fee is at least WAD.
35
+ * @throws {MissingReferralFeeRecipientError} when a positive fee has no recipient.
36
+ * @throws {UnsupportedChainIdError} when the chain is absent from the registry.
37
+ * @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
37
38
  * @example
38
39
  * ```ts
40
+ * import { markets } from "@morpho-org/morpho-test";
39
41
  * import { blueSupplyCollateral } from "@morpho-org/morpho-sdk";
42
+ * import { zeroAddress } from "viem";
43
+ * import { mainnet } from "viem/chains";
40
44
  *
41
45
  * const tx = blueSupplyCollateral({
42
- * market: { chainId: 1, marketParams },
43
- * args: { amount: 1_000_000_000_000_000_000n, onBehalf },
46
+ * market: { chainId: mainnet.id, marketParams: markets[mainnet.id].usdc_wbtc },
47
+ * args: {
48
+ * userAddress: zeroAddress,
49
+ * collateralAssets: 1_000_000_000_000_000_000n,
50
+ * deadline: 1_900_000_000n,
51
+ * },
44
52
  * });
45
53
  * // tx satisfies Readonly<Transaction<BlueSupplyCollateralAction>>
46
54
  * ```
47
55
  */
48
- export const blueSupplyCollateral = ({ market: { chainId, marketParams }, args: { amount = 0n, onBehalf, requirementSignature, nativeAmount }, metadata, }) => {
49
- if (amount < 0n) {
50
- throw new NegativeInputError("amount", amount);
51
- }
52
- if (nativeAmount !== undefined && nativeAmount < 0n) {
53
- throw new NegativeInputError("nativeAmount", nativeAmount);
54
- }
55
- const totalCollateral = amount + (nativeAmount ?? 0n);
56
- if (totalCollateral === 0n) {
57
- throw new NonPositiveInputError("totalCollateral", totalCollateral);
58
- }
59
- const actions = buildAssetFundingActions({
60
- chainId,
61
- asset: marketParams.collateralToken,
62
- erc20Amount: amount,
63
- nativeAmount: nativeAmount ?? 0n,
64
- requirementSignature,
65
- });
66
- actions.push({
67
- type: "morphoSupplyCollateral",
68
- args: [marketParams, totalCollateral, onBehalf, [], false],
56
+ export const blueSupplyCollateral = (params) => {
57
+ const transaction = blueSupplyCollateralBorrow({
58
+ ...params,
59
+ args: {
60
+ ...params.args,
61
+ borrowAssets: 0n,
62
+ maxLtv: maxUint256,
63
+ },
69
64
  });
70
- let tx = BundlerAction.encodeBundle(chainId, actions);
71
- if (metadata) {
72
- tx = addTransactionMetadata(tx, metadata);
73
- }
74
65
  return deepFreeze({
75
- ...tx,
76
- action: {
77
- type: "blueSupplyCollateral",
78
- args: {
79
- market: marketParams.id,
80
- amount: totalCollateral,
81
- onBehalf,
82
- nativeAmount,
83
- },
84
- },
66
+ ...transaction,
67
+ action: { ...transaction.action, type: "blueSupplyCollateral" },
85
68
  });
86
69
  };