@morpho-org/morpho-sdk 5.8.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 (167) hide show
  1. package/MIGRATION-v5-to-v6.md +201 -0
  2. package/README.md +62 -40
  3. package/lib/cjs/abis.d.ts +543 -0
  4. package/lib/cjs/abis.js +738 -0
  5. package/lib/cjs/actions/blue/borrow.d.ts +62 -58
  6. package/lib/cjs/actions/blue/borrow.js +48 -80
  7. package/lib/cjs/actions/blue/common.d.ts +99 -0
  8. package/lib/cjs/actions/blue/common.js +318 -0
  9. package/lib/cjs/actions/blue/refinance.d.ts +72 -107
  10. package/lib/cjs/actions/blue/refinance.js +97 -197
  11. package/lib/cjs/actions/blue/repay.d.ts +72 -68
  12. package/lib/cjs/actions/blue/repay.js +56 -118
  13. package/lib/cjs/actions/blue/repayWithdrawCollateral.d.ts +85 -90
  14. package/lib/cjs/actions/blue/repayWithdrawCollateral.js +148 -137
  15. package/lib/cjs/actions/blue/supply.d.ts +62 -45
  16. package/lib/cjs/actions/blue/supply.js +94 -70
  17. package/lib/cjs/actions/blue/supplyCollateral.d.ts +63 -40
  18. package/lib/cjs/actions/blue/supplyCollateral.js +52 -69
  19. package/lib/cjs/actions/blue/supplyCollateralBorrow.d.ts +82 -86
  20. package/lib/cjs/actions/blue/supplyCollateralBorrow.js +133 -119
  21. package/lib/cjs/actions/blue/withdraw.d.ts +68 -78
  22. package/lib/cjs/actions/blue/withdraw.js +101 -97
  23. package/lib/cjs/actions/blue/withdrawCollateral.d.ts +56 -28
  24. package/lib/cjs/actions/blue/withdrawCollateral.js +45 -50
  25. package/lib/cjs/actions/index.d.ts +1 -1
  26. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +29 -17
  27. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +60 -19
  28. package/lib/cjs/actions/requirements/blue/getBlueBundlesV1TokenRequirements.d.ts +80 -0
  29. package/lib/cjs/actions/requirements/blue/getBlueBundlesV1TokenRequirements.js +207 -0
  30. package/lib/cjs/actions/requirements/blue/getUnusedPermit2Nonce.d.ts +48 -0
  31. package/lib/cjs/actions/requirements/blue/getUnusedPermit2Nonce.js +72 -0
  32. package/lib/cjs/actions/requirements/blue/index.d.ts +2 -0
  33. package/lib/cjs/actions/requirements/blue/index.js +2 -0
  34. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +10 -7
  35. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +30 -5
  36. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +2 -1
  37. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +7 -2
  38. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +16 -7
  39. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +33 -6
  40. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2TransferFrom.d.ts +61 -0
  41. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2TransferFrom.js +123 -0
  42. package/lib/cjs/actions/requirements/encode/index.d.ts +1 -0
  43. package/lib/cjs/actions/requirements/encode/index.js +1 -0
  44. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +1 -1
  45. package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +5 -4
  46. package/lib/cjs/actions/requirements/getRequirementsApproval.js +5 -4
  47. package/lib/cjs/actions/signatures/getTokenRequirementActions.d.ts +1 -0
  48. package/lib/cjs/actions/signatures/getTokenRequirementActions.js +4 -0
  49. package/lib/cjs/blue/types.d.ts +3 -1
  50. package/lib/cjs/blue/utils.d.ts +3 -1
  51. package/lib/cjs/blue/utils.js +1 -0
  52. package/lib/cjs/bundler/actions.d.ts +6 -4
  53. package/lib/cjs/bundler/actions.js +6 -4
  54. package/lib/cjs/bundler/types.d.ts +10 -1
  55. package/lib/cjs/client/morphoViemExtension.d.ts +2 -1
  56. package/lib/cjs/client/morphoViemExtension.js +2 -1
  57. package/lib/cjs/entities/blue/blue.d.ts +817 -360
  58. package/lib/cjs/entities/blue/blue.js +455 -698
  59. package/lib/cjs/entities/index.d.ts +1 -0
  60. package/lib/cjs/entities/vaultV1/vaultV1.js +4 -12
  61. package/lib/cjs/entities/vaultV1ReallocationData.d.ts +18 -3
  62. package/lib/cjs/entities/vaultV1ReallocationData.js +11 -1
  63. package/lib/cjs/entities/vaultV2/vaultV2.js +6 -18
  64. package/lib/cjs/helpers/computeVaultV1Reallocations.d.ts +7 -10
  65. package/lib/cjs/helpers/computeVaultV1Reallocations.js +7 -10
  66. package/lib/cjs/helpers/validate.d.ts +16 -23
  67. package/lib/cjs/helpers/validate.js +32 -48
  68. package/lib/cjs/helpers/validateRequirementSpender.d.ts +2 -1
  69. package/lib/cjs/helpers/validateRequirementSpender.js +2 -0
  70. package/lib/cjs/types/action.d.ts +146 -162
  71. package/lib/cjs/types/action.js +29 -2
  72. package/lib/cjs/types/client.d.ts +9 -0
  73. package/lib/cjs/types/error.d.ts +173 -18
  74. package/lib/cjs/types/error.js +230 -25
  75. package/lib/cjs/types/sharedLiquidity.d.ts +20 -4
  76. package/lib/cjs/types.d.ts +3 -1
  77. package/lib/cjs/utils.d.ts +3 -1
  78. package/lib/cjs/utils.js +1 -0
  79. package/lib/esm/abis.d.ts +543 -0
  80. package/lib/esm/abis.js +737 -0
  81. package/lib/esm/actions/blue/borrow.d.ts +62 -58
  82. package/lib/esm/actions/blue/borrow.js +48 -80
  83. package/lib/esm/actions/blue/common.d.ts +99 -0
  84. package/lib/esm/actions/blue/common.js +306 -0
  85. package/lib/esm/actions/blue/refinance.d.ts +72 -107
  86. package/lib/esm/actions/blue/refinance.js +98 -198
  87. package/lib/esm/actions/blue/repay.d.ts +72 -68
  88. package/lib/esm/actions/blue/repay.js +56 -118
  89. package/lib/esm/actions/blue/repayWithdrawCollateral.d.ts +85 -90
  90. package/lib/esm/actions/blue/repayWithdrawCollateral.js +149 -138
  91. package/lib/esm/actions/blue/supply.d.ts +62 -45
  92. package/lib/esm/actions/blue/supply.js +94 -70
  93. package/lib/esm/actions/blue/supplyCollateral.d.ts +63 -40
  94. package/lib/esm/actions/blue/supplyCollateral.js +52 -69
  95. package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +82 -86
  96. package/lib/esm/actions/blue/supplyCollateralBorrow.js +134 -120
  97. package/lib/esm/actions/blue/withdraw.d.ts +68 -78
  98. package/lib/esm/actions/blue/withdraw.js +100 -96
  99. package/lib/esm/actions/blue/withdrawCollateral.d.ts +56 -28
  100. package/lib/esm/actions/blue/withdrawCollateral.js +45 -50
  101. package/lib/esm/actions/index.d.ts +1 -1
  102. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +29 -17
  103. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +63 -22
  104. package/lib/esm/actions/requirements/blue/getBlueBundlesV1TokenRequirements.d.ts +80 -0
  105. package/lib/esm/actions/requirements/blue/getBlueBundlesV1TokenRequirements.js +203 -0
  106. package/lib/esm/actions/requirements/blue/getUnusedPermit2Nonce.d.ts +48 -0
  107. package/lib/esm/actions/requirements/blue/getUnusedPermit2Nonce.js +68 -0
  108. package/lib/esm/actions/requirements/blue/index.d.ts +2 -0
  109. package/lib/esm/actions/requirements/blue/index.js +2 -0
  110. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +10 -7
  111. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +31 -6
  112. package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +2 -1
  113. package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +8 -3
  114. package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +16 -7
  115. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +34 -7
  116. package/lib/esm/actions/requirements/encode/encodeErc20Permit2TransferFrom.d.ts +61 -0
  117. package/lib/esm/actions/requirements/encode/encodeErc20Permit2TransferFrom.js +119 -0
  118. package/lib/esm/actions/requirements/encode/index.d.ts +1 -0
  119. package/lib/esm/actions/requirements/encode/index.js +1 -0
  120. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +1 -1
  121. package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +5 -4
  122. package/lib/esm/actions/requirements/getRequirementsApproval.js +5 -4
  123. package/lib/esm/actions/signatures/getTokenRequirementActions.d.ts +1 -0
  124. package/lib/esm/actions/signatures/getTokenRequirementActions.js +5 -1
  125. package/lib/esm/blue/types.d.ts +3 -1
  126. package/lib/esm/blue/utils.d.ts +3 -1
  127. package/lib/esm/blue/utils.js +3 -1
  128. package/lib/esm/bundler/actions.d.ts +6 -4
  129. package/lib/esm/bundler/actions.js +6 -4
  130. package/lib/esm/bundler/types.d.ts +10 -1
  131. package/lib/esm/client/morphoViemExtension.d.ts +2 -1
  132. package/lib/esm/client/morphoViemExtension.js +2 -1
  133. package/lib/esm/entities/blue/blue.d.ts +817 -360
  134. package/lib/esm/entities/blue/blue.js +461 -704
  135. package/lib/esm/entities/index.d.ts +1 -0
  136. package/lib/esm/entities/vaultV1/vaultV1.js +4 -12
  137. package/lib/esm/entities/vaultV1ReallocationData.d.ts +18 -3
  138. package/lib/esm/entities/vaultV1ReallocationData.js +13 -2
  139. package/lib/esm/entities/vaultV2/vaultV2.js +7 -19
  140. package/lib/esm/helpers/computeVaultV1Reallocations.d.ts +7 -10
  141. package/lib/esm/helpers/computeVaultV1Reallocations.js +7 -10
  142. package/lib/esm/helpers/validate.d.ts +16 -23
  143. package/lib/esm/helpers/validate.js +32 -48
  144. package/lib/esm/helpers/validateRequirementSpender.d.ts +2 -1
  145. package/lib/esm/helpers/validateRequirementSpender.js +2 -0
  146. package/lib/esm/types/action.d.ts +146 -162
  147. package/lib/esm/types/action.js +28 -2
  148. package/lib/esm/types/client.d.ts +9 -0
  149. package/lib/esm/types/error.d.ts +173 -18
  150. package/lib/esm/types/error.js +216 -21
  151. package/lib/esm/types/sharedLiquidity.d.ts +20 -4
  152. package/lib/esm/types.d.ts +3 -1
  153. package/lib/esm/utils.d.ts +3 -1
  154. package/lib/esm/utils.js +3 -1
  155. package/package.json +7 -6
  156. package/lib/cjs/actions/blue/buildAssetFundingActions.d.ts +0 -47
  157. package/lib/cjs/actions/blue/buildAssetFundingActions.js +0 -63
  158. package/lib/cjs/actions/blue/buildReallocationActions.d.ts +0 -69
  159. package/lib/cjs/actions/blue/buildReallocationActions.js +0 -141
  160. package/lib/cjs/actions/blue/resolveRepayFunding.d.ts +0 -66
  161. package/lib/cjs/actions/blue/resolveRepayFunding.js +0 -85
  162. package/lib/esm/actions/blue/buildAssetFundingActions.d.ts +0 -47
  163. package/lib/esm/actions/blue/buildAssetFundingActions.js +0 -59
  164. package/lib/esm/actions/blue/buildReallocationActions.d.ts +0 -69
  165. package/lib/esm/actions/blue/buildReallocationActions.js +0 -135
  166. package/lib/esm/actions/blue/resolveRepayFunding.d.ts +0 -66
  167. package/lib/esm/actions/blue/resolveRepayFunding.js +0 -81
@@ -1,81 +1,85 @@
1
1
  import type { MarketParams } from "@morpho-org/blue-sdk";
2
2
  import type { Address } from "viem";
3
- import { type AuthorizationRequirementSignature, type BlueBorrowAction, type BlueReallocationPlan, type Metadata, type Transaction } from "../../types/index.js";
3
+ import type { AuthorizationRequirementSignature, BlueBorrowAction, Metadata, Transaction, VaultV2BlueReallocation } from "../../types/index.js";
4
4
  /** Parameters for {@link blueBorrow}. */
5
5
  export interface BlueBorrowParams {
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: {
11
- /** Amount of loan asset to borrow. */
12
- amount: bigint;
13
- /** Address that receives the borrowed assets. */
14
- receiver: Address;
15
- /** Minimum borrow share price (in ray). Protects against share price manipulation. */
16
- minSharePrice: bigint;
17
- /** Homogeneous Vault V1 or Vault V2 reallocations to execute before borrowing. */
18
- reallocations?: BlueReallocationPlan;
19
- /**
20
- * Optional signed Morpho authorization. When provided, a `setAuthorizationWithSig` call is
21
- * prepended to the bundle so GeneralAdapter1 is authorized in-bundle instead of via a
22
- * standalone `setAuthorization` transaction.
23
- */
24
- authorizationSignature?: AuthorizationRequirementSignature;
11
+ /** Direct BlueBundlesV1 borrow arguments. */
12
+ readonly args: {
13
+ /** User whose debt position is increased. */
14
+ readonly userAddress: Address;
15
+ /** Gross loan assets borrowed before penalties and referral fees. */
16
+ readonly borrowAssets: bigint;
17
+ /** Maximum post-operation LTV enforced by BlueBundlesV1. */
18
+ readonly maxLtv: bigint;
19
+ /** Optional validated Vault V2 BluePublicAllocator reallocations. */
20
+ readonly reallocations?: Iterable<VaultV2BlueReallocation>;
21
+ /** Final call deadline in Unix seconds. */
22
+ readonly deadline: bigint;
23
+ /** Optional WAD-scaled referral fee, strictly below 100%. */
24
+ readonly referralFeePct?: bigint;
25
+ /** Recipient required when `referralFeePct` is positive. */
26
+ readonly referralFeeRecipient?: Address;
27
+ /** Optional Morpho authorization signature for BlueBundlesV1. */
28
+ readonly authorizationSignature?: AuthorizationRequirementSignature;
25
29
  };
26
- metadata?: Metadata;
30
+ /** Optional transaction metadata suffix. */
31
+ readonly metadata?: Metadata;
27
32
  }
28
33
  /**
29
- * Prepares a borrow transaction for a Morpho Blue market.
34
+ * Encodes a pure borrow through BlueBundlesV1.
30
35
  *
31
- * Routed through bundler3 via `morphoBorrow`. The bundler uses the transaction initiator as
32
- * `onBehalf`. Uses `minSharePrice` to protect against share price manipulation between
33
- * transaction construction and execution.
36
+ * Delegates to {@link blueSupplyCollateralBorrow} with a zero collateral leg. Vault V2 allocator
37
+ * penalties and referral fees reduce the assets received, and no Bundler3 action is encoded.
34
38
  *
35
- * A `reallocations` plan contains either PublicAllocator V1 entries or Vault V2
36
- * BluePublicAllocator entries, never both. The calls run before the borrow.
37
- * V1 fees accumulate in `tx.value`; V2 penalties are paid in the target loan
38
- * token and donated directly to each vault.
39
- *
40
- * @param params.market.chainId - The chain the market lives on.
41
- * @param params.market.marketParams - Market params (loanToken, collateralToken, oracle, irm, lltv).
42
- * @param params.args.amount - Loan asset amount to borrow, in the loan token's smallest unit.
43
- * @param params.args.receiver - Address that receives the borrowed assets.
44
- * @param params.args.minSharePrice - Minimum borrow share price (in ray). Slippage protection.
45
- * @param params.args.reallocations - Optional homogeneous Vault V1 or Vault V2 reallocations to
46
- * execute before borrowing.
47
- * @param params.args.authorizationSignature - Optional signed Morpho authorization; when present,
48
- * a `setAuthorizationWithSig` call is prepended to the bundle.
49
- * @param params.metadata - Optional analytics metadata attached to the bundle.
50
- * @returns A deep-frozen `Transaction<BlueBorrowAction>` with `to`, `value`, `data`, and the
51
- * typed `action` discriminator the simulation layer consumes.
52
- * @throws {NonPositiveInputError} when `amount <= 0n` or any reallocation withdrawal amount
53
- * is non-positive.
54
- * @throws {InputExceedsMaxError} when a V2 reallocation asset amount exceeds `uint128` or its penalty exceeds WAD.
55
- * @throws {InconsistentReallocationPenaltyError} when V2 entries for one vault use different penalties.
56
- * @throws {InvalidReallocationAddressError} when a V2 vault or adapter address is malformed.
57
- * @throws {InvalidReallocationSourceTypeError} when a V2 source is absent, incomplete, or has an unknown discriminator.
58
- * @throws {InvalidReallocationShapeError} when an entry matches both or neither V1/V2 shape.
59
- * @throws {MixedReallocationVersionsError} when one plan contains both V1 and V2 entries.
60
- * @throws {NegativeInputError} when `minSharePrice < 0n`, a V1 fee, or a V2 penalty is negative.
61
- * @throws {EmptyReallocationWithdrawalsError} when any `reallocation.withdrawals` is empty.
62
- * @throws {ReallocationWithdrawalOnTargetMarketError} when any reallocation withdrawal references
63
- * the target market.
64
- * @throws {UnsortedReallocationWithdrawalsError} when reallocation withdrawals are not strictly
65
- * sorted by market id.
39
+ * @param params - Borrow encoding parameters.
40
+ * @param params.market.chainId - Chain containing BlueBundlesV1.
41
+ * @param params.market.marketParams - Scoped Morpho Blue market parameters.
42
+ * @param params.args.userAddress - User whose debt position is increased.
43
+ * @param params.args.borrowAssets - Gross loan assets borrowed.
44
+ * @param params.args.maxLtv - Maximum post-operation LTV.
45
+ * @param params.args.reallocations - Vault V2 reallocations executed before borrowing.
46
+ * @param params.args.deadline - Final call deadline in Unix seconds.
47
+ * @param params.args.referralFeePct - Optional WAD-scaled referral fee.
48
+ * @param params.args.referralFeeRecipient - Recipient required for a positive fee.
49
+ * @param params.args.authorizationSignature - Optional Blue authorization signature.
50
+ * @param params.metadata - Optional transaction metadata.
51
+ * @returns A deep-frozen `Readonly<Transaction<BlueBorrowAction>>` whose calldata invokes the
52
+ * combined BlueBundlesV1 entrypoint with a zero collateral leg.
53
+ * @throws {NegativeInputError} when an amount, LTV, fee, or reallocation penalty is negative.
54
+ * @throws {NonPositiveInputError} when the borrow, deadline, or reallocation amount is not positive.
55
+ * @throws {InputExceedsMaxError} when a fee, reallocation amount, or penalty exceeds its bound.
56
+ * @throws {MissingReferralFeeRecipientError} when a positive fee has no recipient.
57
+ * @throws {InvalidReallocationAddressError} when a vault or adapter address is malformed.
58
+ * @throws {InvalidReallocationSourceTypeError} when a reallocation source is malformed.
59
+ * @throws {InconsistentReallocationPenaltyError} when one vault uses different penalties.
60
+ * @throws {ReallocationWithdrawalOnTargetMarketError} when a source is the target market.
61
+ * @throws {ReallocationLoanTokenMismatchError} when a source market uses another loan token.
62
+ * @throws {DepositOwnerMismatchError} when the signed authorization owner differs from `userAddress`.
63
+ * @throws {BlueBundlesV1RequirementSignatureMismatchError} when authorization cannot be bound safely.
64
+ * @throws {UnsupportedChainIdError} when the chain is absent from the registry.
65
+ * @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
66
66
  * @example
67
67
  * ```ts
68
+ * import { markets } from "@morpho-org/morpho-test";
68
69
  * import { blueBorrow } from "@morpho-org/morpho-sdk";
70
+ * import { zeroAddress } from "viem";
71
+ * import { mainnet } from "viem/chains";
69
72
  *
70
73
  * const tx = blueBorrow({
71
- * market: { chainId: 1, marketParams },
74
+ * market: { chainId: mainnet.id, marketParams: markets[mainnet.id].usdc_wbtc },
72
75
  * args: {
73
- * amount: 1_000_000n,
74
- * receiver: borrower,
75
- * minSharePrice: 0n, // disables slippage protection — production code should compute via `computeMinBorrowSharePrice` from market state + slippage tolerance
76
+ * userAddress: zeroAddress,
77
+ * borrowAssets: 1_000_000n,
78
+ * maxLtv: 850000000000000000n,
79
+ * deadline: 1_900_000_000n,
76
80
  * },
77
81
  * });
78
82
  * // tx satisfies Readonly<Transaction<BlueBorrowAction>>
79
83
  * ```
80
84
  */
81
- export declare const blueBorrow: ({ market: { chainId, marketParams }, args: { amount, receiver, minSharePrice, reallocations, authorizationSignature, }, metadata, }: BlueBorrowParams) => Readonly<Transaction<BlueBorrowAction>>;
85
+ export declare const blueBorrow: (params: BlueBorrowParams) => Readonly<Transaction<BlueBorrowAction>>;
@@ -2,102 +2,70 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.blueBorrow = void 0;
4
4
  const morpho_ts_1 = require("@morpho-org/morpho-ts");
5
- const index_js_1 = require("../../bundler/index.js");
6
- const index_js_2 = require("../../helpers/index.js");
7
- const index_js_3 = require("../../types/index.js");
8
- const getBlueAuthorizationAction_js_1 = require("../signatures/getBlueAuthorizationAction.js");
9
- const buildReallocationActions_js_1 = require("./buildReallocationActions.js");
5
+ const supplyCollateralBorrow_js_1 = require("./supplyCollateralBorrow.js");
10
6
  /**
11
- * Prepares a borrow transaction for a Morpho Blue market.
7
+ * Encodes a pure borrow through BlueBundlesV1.
12
8
  *
13
- * Routed through bundler3 via `morphoBorrow`. The bundler uses the transaction initiator as
14
- * `onBehalf`. Uses `minSharePrice` to protect against share price manipulation between
15
- * transaction construction and execution.
9
+ * Delegates to {@link blueSupplyCollateralBorrow} with a zero collateral leg. Vault V2 allocator
10
+ * penalties and referral fees reduce the assets received, and no Bundler3 action is encoded.
16
11
  *
17
- * A `reallocations` plan contains either PublicAllocator V1 entries or Vault V2
18
- * BluePublicAllocator entries, never both. The calls run before the borrow.
19
- * V1 fees accumulate in `tx.value`; V2 penalties are paid in the target loan
20
- * token and donated directly to each vault.
21
- *
22
- * @param params.market.chainId - The chain the market lives on.
23
- * @param params.market.marketParams - Market params (loanToken, collateralToken, oracle, irm, lltv).
24
- * @param params.args.amount - Loan asset amount to borrow, in the loan token's smallest unit.
25
- * @param params.args.receiver - Address that receives the borrowed assets.
26
- * @param params.args.minSharePrice - Minimum borrow share price (in ray). Slippage protection.
27
- * @param params.args.reallocations - Optional homogeneous Vault V1 or Vault V2 reallocations to
28
- * execute before borrowing.
29
- * @param params.args.authorizationSignature - Optional signed Morpho authorization; when present,
30
- * a `setAuthorizationWithSig` call is prepended to the bundle.
31
- * @param params.metadata - Optional analytics metadata attached to the bundle.
32
- * @returns A deep-frozen `Transaction<BlueBorrowAction>` with `to`, `value`, `data`, and the
33
- * typed `action` discriminator the simulation layer consumes.
34
- * @throws {NonPositiveInputError} when `amount <= 0n` or any reallocation withdrawal amount
35
- * is non-positive.
36
- * @throws {InputExceedsMaxError} when a V2 reallocation asset amount exceeds `uint128` or its penalty exceeds WAD.
37
- * @throws {InconsistentReallocationPenaltyError} when V2 entries for one vault use different penalties.
38
- * @throws {InvalidReallocationAddressError} when a V2 vault or adapter address is malformed.
39
- * @throws {InvalidReallocationSourceTypeError} when a V2 source is absent, incomplete, or has an unknown discriminator.
40
- * @throws {InvalidReallocationShapeError} when an entry matches both or neither V1/V2 shape.
41
- * @throws {MixedReallocationVersionsError} when one plan contains both V1 and V2 entries.
42
- * @throws {NegativeInputError} when `minSharePrice < 0n`, a V1 fee, or a V2 penalty is negative.
43
- * @throws {EmptyReallocationWithdrawalsError} when any `reallocation.withdrawals` is empty.
44
- * @throws {ReallocationWithdrawalOnTargetMarketError} when any reallocation withdrawal references
45
- * the target market.
46
- * @throws {UnsortedReallocationWithdrawalsError} when reallocation withdrawals are not strictly
47
- * sorted by market id.
12
+ * @param params - Borrow encoding parameters.
13
+ * @param params.market.chainId - Chain containing BlueBundlesV1.
14
+ * @param params.market.marketParams - Scoped Morpho Blue market parameters.
15
+ * @param params.args.userAddress - User whose debt position is increased.
16
+ * @param params.args.borrowAssets - Gross loan assets borrowed.
17
+ * @param params.args.maxLtv - Maximum post-operation LTV.
18
+ * @param params.args.reallocations - Vault V2 reallocations executed before borrowing.
19
+ * @param params.args.deadline - Final call deadline in Unix seconds.
20
+ * @param params.args.referralFeePct - Optional WAD-scaled referral fee.
21
+ * @param params.args.referralFeeRecipient - Recipient required for a positive fee.
22
+ * @param params.args.authorizationSignature - Optional Blue authorization signature.
23
+ * @param params.metadata - Optional transaction metadata.
24
+ * @returns A deep-frozen `Readonly<Transaction<BlueBorrowAction>>` whose calldata invokes the
25
+ * combined BlueBundlesV1 entrypoint with a zero collateral leg.
26
+ * @throws {NegativeInputError} when an amount, LTV, fee, or reallocation penalty is negative.
27
+ * @throws {NonPositiveInputError} when the borrow, deadline, or reallocation amount is not positive.
28
+ * @throws {InputExceedsMaxError} when a fee, reallocation amount, or penalty exceeds its bound.
29
+ * @throws {MissingReferralFeeRecipientError} when a positive fee has no recipient.
30
+ * @throws {InvalidReallocationAddressError} when a vault or adapter address is malformed.
31
+ * @throws {InvalidReallocationSourceTypeError} when a reallocation source is malformed.
32
+ * @throws {InconsistentReallocationPenaltyError} when one vault uses different penalties.
33
+ * @throws {ReallocationWithdrawalOnTargetMarketError} when a source is the target market.
34
+ * @throws {ReallocationLoanTokenMismatchError} when a source market uses another loan token.
35
+ * @throws {DepositOwnerMismatchError} when the signed authorization owner differs from `userAddress`.
36
+ * @throws {BlueBundlesV1RequirementSignatureMismatchError} when authorization cannot be bound safely.
37
+ * @throws {UnsupportedChainIdError} when the chain is absent from the registry.
38
+ * @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
48
39
  * @example
49
40
  * ```ts
41
+ * import { markets } from "@morpho-org/morpho-test";
50
42
  * import { blueBorrow } from "@morpho-org/morpho-sdk";
43
+ * import { zeroAddress } from "viem";
44
+ * import { mainnet } from "viem/chains";
51
45
  *
52
46
  * const tx = blueBorrow({
53
- * market: { chainId: 1, marketParams },
47
+ * market: { chainId: mainnet.id, marketParams: markets[mainnet.id].usdc_wbtc },
54
48
  * args: {
55
- * amount: 1_000_000n,
56
- * receiver: borrower,
57
- * minSharePrice: 0n, // disables slippage protection — production code should compute via `computeMinBorrowSharePrice` from market state + slippage tolerance
49
+ * userAddress: zeroAddress,
50
+ * borrowAssets: 1_000_000n,
51
+ * maxLtv: 850000000000000000n,
52
+ * deadline: 1_900_000_000n,
58
53
  * },
59
54
  * });
60
55
  * // tx satisfies Readonly<Transaction<BlueBorrowAction>>
61
56
  * ```
62
57
  */
63
- const blueBorrow = ({ market: { chainId, marketParams }, args: { amount, receiver, minSharePrice, reallocations, authorizationSignature, }, metadata, }) => {
64
- if (amount <= 0n) {
65
- throw new index_js_3.NonPositiveInputError("amount", amount);
66
- }
67
- if (minSharePrice < 0n) {
68
- throw new index_js_3.NegativeInputError("minSharePrice", minSharePrice);
69
- }
70
- const actions = [];
71
- if (authorizationSignature) {
72
- actions.push((0, getBlueAuthorizationAction_js_1.getBlueAuthorizationAction)(chainId, authorizationSignature));
73
- }
74
- const { actions: reallocationActions, fee: reallocationFee, penaltyAssets: reallocationPenaltyAssets, } = (0, buildReallocationActions_js_1.buildBlueReallocationActions)({
75
- chainId,
76
- reallocations,
77
- targetMarketParams: marketParams,
78
- });
79
- actions.push(...reallocationActions);
80
- actions.push({
81
- type: "morphoBorrow",
82
- args: [marketParams, amount, 0n, minSharePrice, receiver, false],
58
+ const blueBorrow = (params) => {
59
+ const transaction = (0, supplyCollateralBorrow_js_1.blueSupplyCollateralBorrow)({
60
+ ...params,
61
+ args: {
62
+ ...params.args,
63
+ collateralAssets: 0n,
64
+ },
83
65
  });
84
- let tx = index_js_1.BundlerAction.encodeBundle(chainId, actions);
85
- if (metadata) {
86
- tx = (0, index_js_2.addTransactionMetadata)(tx, metadata);
87
- }
88
66
  return (0, morpho_ts_1.deepFreeze)({
89
- ...tx,
90
- action: {
91
- type: "blueBorrow",
92
- args: {
93
- market: marketParams.id,
94
- amount,
95
- receiver,
96
- minSharePrice,
97
- reallocationFee,
98
- reallocationPenaltyAssets,
99
- },
100
- },
67
+ ...transaction,
68
+ action: { ...transaction.action, type: "blueBorrow" },
101
69
  });
102
70
  };
103
71
  exports.blueBorrow = blueBorrow;
@@ -0,0 +1,99 @@
1
+ import { MarketParams } from "@morpho-org/blue-sdk";
2
+ import { type Address, type Hex } from "viem";
3
+ import { type AuthorizationRequirementSignature, type BaseAction, type BlueBundlesV1TokenRequirementSignature, type Metadata, type RequirementSignature, type TokenRequirementSignature, type Transaction, type VaultV2BlueReallocation } from "../../types/index.js";
4
+ /** @internal */
5
+ export interface BlueBundlesV1CommonParams {
6
+ chainId: number;
7
+ userAddress: Address;
8
+ deadline: bigint;
9
+ referralFeePct?: bigint;
10
+ referralFeeRecipient?: Address;
11
+ metadata?: Metadata;
12
+ }
13
+ /** @internal */
14
+ export interface NormalizedBlueBundlesV1CommonParams {
15
+ referralFeePct: bigint;
16
+ referralFeeRecipient: Address;
17
+ }
18
+ /**
19
+ * BlueBundlesV1 token-permit discriminator carried in the encoded permit struct: `none` (0) skips
20
+ * the pull, `erc2612` (1) is an ERC-2612 permit, `permit2TransferFrom` (2) is a Permit2
21
+ * SignatureTransfer. Named so a transposed literal can't silently route a permit to the wrong branch.
22
+ * @internal
23
+ */
24
+ declare const BLUE_BUNDLES_V1_TOKEN_PERMIT_KIND: {
25
+ readonly none: 0;
26
+ readonly erc2612: 1;
27
+ readonly permit2TransferFrom: 2;
28
+ };
29
+ interface BlueBundlesV1TokenPermit {
30
+ kind: (typeof BLUE_BUNDLES_V1_TOKEN_PERMIT_KIND)[keyof typeof BLUE_BUNDLES_V1_TOKEN_PERMIT_KIND];
31
+ data: Hex;
32
+ }
33
+ interface BlueBundlesV1SignedAuthorization {
34
+ signature: {
35
+ v: number;
36
+ r: Hex;
37
+ s: Hex;
38
+ };
39
+ nonce: bigint;
40
+ deadline: bigint;
41
+ }
42
+ /** @internal */
43
+ export declare const normalizeBlueBundlesV1CommonParams: (params: BlueBundlesV1CommonParams) => NormalizedBlueBundlesV1CommonParams;
44
+ /** @internal */
45
+ export declare const validateBlueBundlesV1NativeFunding: (params: {
46
+ chainId: number;
47
+ token: Address;
48
+ fundedAmount: bigint;
49
+ nativeAmount?: bigint;
50
+ requirementSignature?: BlueBundlesV1TokenRequirementSignature;
51
+ }) => bigint;
52
+ /** @internal */
53
+ export declare const getBlueBundlesV1TokenPermit: (params: {
54
+ chainId: number;
55
+ userAddress: Address;
56
+ token: Address;
57
+ amount: bigint;
58
+ requirementSignature?: TokenRequirementSignature;
59
+ }) => BlueBundlesV1TokenPermit;
60
+ /** @internal */
61
+ export declare const getBlueBundlesV1SignedAuthorization: (params: {
62
+ chainId: number;
63
+ userAddress: Address;
64
+ authorizationSignature?: AuthorizationRequirementSignature;
65
+ }) => BlueBundlesV1SignedAuthorization;
66
+ /** @internal */
67
+ export declare const getBlueBundlesV1PublicAllocations: (reallocations: readonly VaultV2BlueReallocation[], targetMarketParams: MarketParams) => {
68
+ vault: `0x${string}`;
69
+ adapter: `0x${string}`;
70
+ marketParams: MarketParams;
71
+ fromIdle: boolean;
72
+ sourceAdapter: `0x${string}`;
73
+ sourceMarketParams: MarketParams;
74
+ assets: bigint;
75
+ penalty: bigint;
76
+ }[];
77
+ /** @internal */
78
+ export declare const getBlueBundlesV1PenaltyAssets: (reallocations: readonly {
79
+ assets: bigint;
80
+ penalty: bigint;
81
+ }[]) => bigint;
82
+ /** @internal */
83
+ export declare const getBlueBundlesV1ReferralFeeAssets: (assets: bigint, referralFeePct: bigint) => bigint;
84
+ /** @internal */
85
+ export declare const selectBlueBundlesV1RequirementSignatures: (signatures: readonly RequirementSignature[] | undefined, accepts: {
86
+ token?: boolean;
87
+ authorization?: boolean;
88
+ }) => {
89
+ token?: BlueBundlesV1TokenRequirementSignature;
90
+ authorization?: AuthorizationRequirementSignature;
91
+ };
92
+ /** @internal */
93
+ export declare const finalizeBlueBundlesV1Transaction: <TAction extends BaseAction>(params: {
94
+ common: BlueBundlesV1CommonParams;
95
+ value: bigint;
96
+ data: Hex;
97
+ action: TAction;
98
+ }) => Readonly<Transaction<TAction>>;
99
+ export {};