@morpho-org/morpho-sdk 5.9.0 → 6.0.0-next.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 (243) hide show
  1. package/MIGRATION-v5-to-v6.md +404 -0
  2. package/README.md +127 -69
  3. package/lib/cjs/abis.d.ts +210 -0
  4. package/lib/cjs/abis.js +142 -2
  5. package/lib/cjs/actions/blue/borrow.d.ts +62 -61
  6. package/lib/cjs/actions/blue/borrow.js +48 -80
  7. package/lib/cjs/actions/blue/common.d.ts +86 -0
  8. package/lib/cjs/actions/blue/common.js +205 -0
  9. package/lib/cjs/actions/blue/refinance.d.ts +72 -112
  10. package/lib/cjs/actions/blue/refinance.js +97 -199
  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 -90
  20. package/lib/cjs/actions/blue/supplyCollateralBorrow.js +133 -120
  21. package/lib/cjs/actions/blue/withdraw.d.ts +68 -79
  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/bundles/common.d.ts +200 -0
  26. package/lib/cjs/actions/bundles/common.js +469 -0
  27. package/lib/cjs/actions/bundles/index.d.ts +3 -0
  28. package/lib/cjs/actions/bundles/index.js +23 -0
  29. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.d.ts +76 -0
  30. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.js +125 -0
  31. package/lib/cjs/actions/index.d.ts +2 -1
  32. package/lib/cjs/actions/index.js +1 -0
  33. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.d.ts +2 -2
  34. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +2 -2
  35. package/lib/cjs/actions/midnight/takeBorrow.d.ts +2 -2
  36. package/lib/cjs/actions/midnight/takeBorrow.js +2 -2
  37. package/lib/cjs/actions/midnight/takeLend.d.ts +2 -2
  38. package/lib/cjs/actions/midnight/takeLend.js +2 -2
  39. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +29 -17
  40. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +60 -19
  41. package/lib/cjs/actions/requirements/blue/getUnusedPermit2Nonce.d.ts +48 -0
  42. package/lib/cjs/actions/requirements/blue/getUnusedPermit2Nonce.js +72 -0
  43. package/lib/cjs/actions/requirements/blue/index.d.ts +1 -0
  44. package/lib/cjs/actions/requirements/blue/index.js +1 -0
  45. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +10 -7
  46. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +30 -5
  47. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +2 -1
  48. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +8 -2
  49. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +16 -7
  50. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +39 -6
  51. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +61 -0
  52. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +123 -0
  53. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +4 -4
  54. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +6 -5
  55. package/lib/cjs/actions/requirements/encode/index.d.ts +1 -0
  56. package/lib/cjs/actions/requirements/encode/index.js +1 -0
  57. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +1 -1
  58. package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +6 -4
  59. package/lib/cjs/actions/requirements/getRequirementsApproval.js +6 -4
  60. package/lib/cjs/actions/signatures/getTokenRequirementActions.d.ts +1 -0
  61. package/lib/cjs/actions/signatures/getTokenRequirementActions.js +4 -0
  62. package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +22 -30
  63. package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.js +18 -66
  64. package/lib/cjs/actions/vaultV1/deposit.d.ts +73 -57
  65. package/lib/cjs/actions/vaultV1/deposit.js +110 -109
  66. package/lib/cjs/actions/vaultV1/migrateToV2.d.ts +43 -66
  67. package/lib/cjs/actions/vaultV1/migrateToV2.js +91 -104
  68. package/lib/cjs/actions/vaultV1/redeem.d.ts +30 -23
  69. package/lib/cjs/actions/vaultV1/redeem.js +56 -33
  70. package/lib/cjs/actions/vaultV1/withdraw.d.ts +58 -24
  71. package/lib/cjs/actions/vaultV1/withdraw.js +86 -34
  72. package/lib/cjs/actions/vaultV2/deposit.d.ts +74 -57
  73. package/lib/cjs/actions/vaultV2/deposit.js +111 -109
  74. package/lib/cjs/actions/vaultV2/redeem.d.ts +30 -24
  75. package/lib/cjs/actions/vaultV2/redeem.js +56 -34
  76. package/lib/cjs/actions/vaultV2/withdraw.d.ts +58 -25
  77. package/lib/cjs/actions/vaultV2/withdraw.js +86 -35
  78. package/lib/cjs/bundler/actions.d.ts +4 -4
  79. package/lib/cjs/bundler/actions.js +4 -4
  80. package/lib/cjs/client/morphoViemExtension.d.ts +2 -1
  81. package/lib/cjs/client/morphoViemExtension.js +2 -1
  82. package/lib/cjs/entities/blue/blue.d.ts +806 -374
  83. package/lib/cjs/entities/blue/blue.js +449 -696
  84. package/lib/cjs/entities/index.d.ts +2 -0
  85. package/lib/cjs/entities/index.js +15 -0
  86. package/lib/cjs/entities/midnight/midnight.d.ts +2 -2
  87. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.d.ts +87 -0
  88. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +203 -0
  89. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +28 -0
  90. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.js +74 -0
  91. package/lib/cjs/entities/requirements/index.d.ts +1 -0
  92. package/lib/cjs/entities/requirements/index.js +17 -0
  93. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +348 -98
  94. package/lib/cjs/entities/vaultV1/vaultV1.js +322 -120
  95. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +221 -68
  96. package/lib/cjs/entities/vaultV2/vaultV2.js +219 -77
  97. package/lib/cjs/helpers/computeVaultV1Reallocations.d.ts +3 -9
  98. package/lib/cjs/helpers/computeVaultV1Reallocations.js +3 -9
  99. package/lib/cjs/helpers/index.d.ts +2 -1
  100. package/lib/cjs/helpers/index.js +5 -1
  101. package/lib/cjs/helpers/referralFee.d.ts +21 -0
  102. package/lib/cjs/helpers/referralFee.js +35 -0
  103. package/lib/cjs/helpers/slippage.d.ts +90 -1
  104. package/lib/cjs/helpers/slippage.js +123 -0
  105. package/lib/cjs/helpers/validate.d.ts +16 -23
  106. package/lib/cjs/helpers/validate.js +41 -48
  107. package/lib/cjs/helpers/validateRequirementSpender.d.ts +2 -1
  108. package/lib/cjs/helpers/validateRequirementSpender.js +3 -0
  109. package/lib/cjs/index.d.ts +1 -0
  110. package/lib/cjs/index.js +1 -0
  111. package/lib/cjs/types/action.d.ts +303 -210
  112. package/lib/cjs/types/action.js +29 -2
  113. package/lib/cjs/types/client.d.ts +9 -0
  114. package/lib/cjs/types/error.d.ts +193 -18
  115. package/lib/cjs/types/error.js +267 -24
  116. package/lib/cjs/types/sharedLiquidity.d.ts +0 -7
  117. package/lib/esm/abis.d.ts +210 -0
  118. package/lib/esm/abis.js +140 -0
  119. package/lib/esm/actions/blue/borrow.d.ts +62 -61
  120. package/lib/esm/actions/blue/borrow.js +48 -80
  121. package/lib/esm/actions/blue/common.d.ts +86 -0
  122. package/lib/esm/actions/blue/common.js +193 -0
  123. package/lib/esm/actions/blue/refinance.d.ts +72 -112
  124. package/lib/esm/actions/blue/refinance.js +98 -200
  125. package/lib/esm/actions/blue/repay.d.ts +72 -68
  126. package/lib/esm/actions/blue/repay.js +56 -118
  127. package/lib/esm/actions/blue/repayWithdrawCollateral.d.ts +85 -90
  128. package/lib/esm/actions/blue/repayWithdrawCollateral.js +149 -138
  129. package/lib/esm/actions/blue/supply.d.ts +62 -45
  130. package/lib/esm/actions/blue/supply.js +94 -70
  131. package/lib/esm/actions/blue/supplyCollateral.d.ts +63 -40
  132. package/lib/esm/actions/blue/supplyCollateral.js +52 -69
  133. package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +82 -90
  134. package/lib/esm/actions/blue/supplyCollateralBorrow.js +134 -121
  135. package/lib/esm/actions/blue/withdraw.d.ts +68 -79
  136. package/lib/esm/actions/blue/withdraw.js +100 -96
  137. package/lib/esm/actions/blue/withdrawCollateral.d.ts +56 -28
  138. package/lib/esm/actions/blue/withdrawCollateral.js +45 -50
  139. package/lib/esm/actions/bundles/common.d.ts +200 -0
  140. package/lib/esm/actions/bundles/common.js +457 -0
  141. package/lib/esm/actions/bundles/index.d.ts +3 -0
  142. package/lib/esm/actions/bundles/index.js +2 -0
  143. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.d.ts +76 -0
  144. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.js +121 -0
  145. package/lib/esm/actions/index.d.ts +2 -1
  146. package/lib/esm/actions/index.js +1 -0
  147. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.d.ts +2 -2
  148. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +2 -2
  149. package/lib/esm/actions/midnight/takeBorrow.d.ts +2 -2
  150. package/lib/esm/actions/midnight/takeBorrow.js +2 -2
  151. package/lib/esm/actions/midnight/takeLend.d.ts +2 -2
  152. package/lib/esm/actions/midnight/takeLend.js +2 -2
  153. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +29 -17
  154. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +63 -22
  155. package/lib/esm/actions/requirements/blue/getUnusedPermit2Nonce.d.ts +48 -0
  156. package/lib/esm/actions/requirements/blue/getUnusedPermit2Nonce.js +68 -0
  157. package/lib/esm/actions/requirements/blue/index.d.ts +1 -0
  158. package/lib/esm/actions/requirements/blue/index.js +1 -0
  159. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +10 -7
  160. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +31 -6
  161. package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +2 -1
  162. package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +9 -3
  163. package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +16 -7
  164. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +40 -7
  165. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +61 -0
  166. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +119 -0
  167. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +4 -4
  168. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +6 -5
  169. package/lib/esm/actions/requirements/encode/index.d.ts +1 -0
  170. package/lib/esm/actions/requirements/encode/index.js +1 -0
  171. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +1 -1
  172. package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +6 -4
  173. package/lib/esm/actions/requirements/getRequirementsApproval.js +6 -4
  174. package/lib/esm/actions/signatures/getTokenRequirementActions.d.ts +1 -0
  175. package/lib/esm/actions/signatures/getTokenRequirementActions.js +5 -1
  176. package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +22 -30
  177. package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.js +19 -67
  178. package/lib/esm/actions/vaultV1/deposit.d.ts +73 -57
  179. package/lib/esm/actions/vaultV1/deposit.js +112 -111
  180. package/lib/esm/actions/vaultV1/migrateToV2.d.ts +43 -66
  181. package/lib/esm/actions/vaultV1/migrateToV2.js +93 -106
  182. package/lib/esm/actions/vaultV1/redeem.d.ts +30 -23
  183. package/lib/esm/actions/vaultV1/redeem.js +56 -33
  184. package/lib/esm/actions/vaultV1/withdraw.d.ts +58 -24
  185. package/lib/esm/actions/vaultV1/withdraw.js +86 -34
  186. package/lib/esm/actions/vaultV2/deposit.d.ts +74 -57
  187. package/lib/esm/actions/vaultV2/deposit.js +113 -111
  188. package/lib/esm/actions/vaultV2/redeem.d.ts +30 -24
  189. package/lib/esm/actions/vaultV2/redeem.js +56 -34
  190. package/lib/esm/actions/vaultV2/withdraw.d.ts +58 -25
  191. package/lib/esm/actions/vaultV2/withdraw.js +86 -35
  192. package/lib/esm/bundler/actions.d.ts +4 -4
  193. package/lib/esm/bundler/actions.js +4 -4
  194. package/lib/esm/client/morphoViemExtension.d.ts +2 -1
  195. package/lib/esm/client/morphoViemExtension.js +2 -1
  196. package/lib/esm/entities/blue/blue.d.ts +806 -374
  197. package/lib/esm/entities/blue/blue.js +455 -702
  198. package/lib/esm/entities/index.d.ts +2 -0
  199. package/lib/esm/entities/index.js +1 -0
  200. package/lib/esm/entities/midnight/midnight.d.ts +2 -2
  201. package/lib/esm/entities/requirements/getBundlesTokenRequirements.d.ts +87 -0
  202. package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +199 -0
  203. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +28 -0
  204. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.js +70 -0
  205. package/lib/esm/entities/requirements/index.d.ts +1 -0
  206. package/lib/esm/entities/requirements/index.js +1 -0
  207. package/lib/esm/entities/vaultV1/vaultV1.d.ts +348 -98
  208. package/lib/esm/entities/vaultV1/vaultV1.js +326 -124
  209. package/lib/esm/entities/vaultV2/vaultV2.d.ts +221 -68
  210. package/lib/esm/entities/vaultV2/vaultV2.js +222 -80
  211. package/lib/esm/helpers/computeVaultV1Reallocations.d.ts +3 -9
  212. package/lib/esm/helpers/computeVaultV1Reallocations.js +3 -9
  213. package/lib/esm/helpers/index.d.ts +2 -1
  214. package/lib/esm/helpers/index.js +2 -1
  215. package/lib/esm/helpers/referralFee.d.ts +21 -0
  216. package/lib/esm/helpers/referralFee.js +31 -0
  217. package/lib/esm/helpers/slippage.d.ts +90 -1
  218. package/lib/esm/helpers/slippage.js +122 -2
  219. package/lib/esm/helpers/validate.d.ts +16 -23
  220. package/lib/esm/helpers/validate.js +40 -48
  221. package/lib/esm/helpers/validateRequirementSpender.d.ts +2 -1
  222. package/lib/esm/helpers/validateRequirementSpender.js +3 -0
  223. package/lib/esm/index.d.ts +1 -0
  224. package/lib/esm/index.js +1 -0
  225. package/lib/esm/types/action.d.ts +303 -210
  226. package/lib/esm/types/action.js +28 -2
  227. package/lib/esm/types/client.d.ts +9 -0
  228. package/lib/esm/types/error.d.ts +193 -18
  229. package/lib/esm/types/error.js +252 -21
  230. package/lib/esm/types/sharedLiquidity.d.ts +0 -7
  231. package/package.json +6 -5
  232. package/lib/cjs/actions/blue/buildAssetFundingActions.d.ts +0 -47
  233. package/lib/cjs/actions/blue/buildAssetFundingActions.js +0 -63
  234. package/lib/cjs/actions/blue/buildReallocationActions.d.ts +0 -69
  235. package/lib/cjs/actions/blue/buildReallocationActions.js +0 -141
  236. package/lib/cjs/actions/blue/resolveRepayFunding.d.ts +0 -66
  237. package/lib/cjs/actions/blue/resolveRepayFunding.js +0 -85
  238. package/lib/esm/actions/blue/buildAssetFundingActions.d.ts +0 -47
  239. package/lib/esm/actions/blue/buildAssetFundingActions.js +0 -59
  240. package/lib/esm/actions/blue/buildReallocationActions.d.ts +0 -69
  241. package/lib/esm/actions/blue/buildReallocationActions.js +0 -135
  242. package/lib/esm/actions/blue/resolveRepayFunding.d.ts +0 -66
  243. package/lib/esm/actions/blue/resolveRepayFunding.js +0 -81
@@ -1,113 +1,105 @@
1
1
  import type { MarketParams } from "@morpho-org/blue-sdk";
2
2
  import { type Address } from "viem";
3
- import { type AuthorizationRequirementSignature, type BlueReallocationPlan, type BlueSupplyCollateralBorrowAction, type DepositAmountArgs, type Metadata, type PermitRequirementSignature, type Transaction } from "../../types/index.js";
3
+ import { type AuthorizationRequirementSignature, type BlueSupplyCollateralBorrowAction, type BundlesTokenRequirementSignature, type Metadata, type Transaction, type VaultV2BlueReallocation } from "../../types/index.js";
4
4
  /** Parameters for {@link blueSupplyCollateralBorrow}. */
5
5
  export interface BlueSupplyCollateralBorrowParams {
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
- /** Amount of loan asset to borrow after the collateral is supplied. */
12
- borrowAmount: bigint;
13
- /** Address whose Morpho collateral and borrow positions are credited. */
14
- onBehalf: Address;
15
- /** Address that receives the borrowed assets. */
16
- receiver: Address;
17
- /** Minimum borrow share price (in ray). Protects against share price manipulation. */
18
- minSharePrice: bigint;
19
- /** Optional pre-signed permit/permit2 approval for the collateral transfer. */
20
- requirementSignature?: PermitRequirementSignature;
21
- /**
22
- * Homogeneous Vault V1 or Vault V2 reallocations to execute before borrowing.
23
- * Vault V1 inputs are deprecated for high-level Blue writes; use Vault V2 for new integrations.
24
- */
25
- reallocations?: BlueReallocationPlan;
26
- /**
27
- * Optional signed Morpho authorization. When provided, a `setAuthorizationWithSig` call is
28
- * prepended to the bundle so GeneralAdapter1 is authorized in-bundle instead of via a
29
- * standalone `setAuthorization` transaction.
30
- */
31
- authorizationSignature?: AuthorizationRequirementSignature;
11
+ /** Direct BlueBundlesV1 combined-operation arguments. */
12
+ readonly args: {
13
+ /** User whose collateral and debt position is changed. */
14
+ readonly userAddress: Address;
15
+ /** Gross collateral supplied; zero selects a pure borrow. */
16
+ readonly collateralAssets: bigint;
17
+ /** Loan assets borrowed; zero selects a pure collateral supply. */
18
+ readonly borrowAssets: bigint;
19
+ /** Maximum post-operation LTV enforced by BlueBundlesV1. */
20
+ readonly maxLtv: bigint;
21
+ /** Full native collateral funding; must equal `collateralAssets`. */
22
+ readonly nativeAmount?: bigint;
23
+ /** Optional validated Vault V2 BluePublicAllocator reallocations. */
24
+ readonly reallocations?: Iterable<VaultV2BlueReallocation>;
25
+ /** Final call deadline in Unix seconds. */
26
+ readonly deadline: bigint;
27
+ /** Optional WAD-scaled referral fee, strictly below 100%. */
28
+ readonly referralFeePct?: bigint;
29
+ /** Recipient required when `referralFeePct` is positive. */
30
+ readonly referralFeeRecipient?: Address;
31
+ /** Optional collateral ERC-2612 or Permit2 SignatureTransfer result. */
32
+ readonly requirementSignature?: BundlesTokenRequirementSignature;
33
+ /** Optional Morpho authorization signature for BlueBundlesV1. */
34
+ readonly authorizationSignature?: AuthorizationRequirementSignature;
32
35
  };
33
- metadata?: Metadata;
36
+ /** Optional transaction metadata suffix. */
37
+ readonly metadata?: Metadata;
34
38
  }
35
39
  /**
36
- * Prepares an atomic supply-collateral-and-borrow transaction for a Morpho Blue market.
40
+ * Encodes a direct BlueBundlesV1 collateral supply and/or borrow transaction.
37
41
  *
38
- * Routed through bundler3: collateral funding → `morphoSupplyCollateral` → optional Public
39
- * Allocator calls → `morphoBorrow`. Each plan contains either V1 or V2 entries,
40
- * never both. When `nativeAmount > 0`, native ETH is wrapped via
41
- * `GeneralAdapter1.wrapNative()` before the supply leg. V1 fees add to
42
- * `tx.value`; V2 penalties are paid in the target loan token and donated to the vaults. When the
43
- * collateral and loan tokens match, one combined pull funds both collateral and penalties through
44
- * `GeneralAdapter1`.
42
+ * Reallocations are Vault V2-only and require a borrow leg. Penalties and referral fees reduce
43
+ * borrow proceeds. This route has no Bundler3 share-price bound or `slippageTolerance` input.
45
44
  *
46
- * Prerequisite: `GeneralAdapter1` must be authorized on Morpho to borrow on behalf of the user.
47
- * Use `getRequirements()` on the entity to check and obtain the authorization transaction.
48
- *
49
- * Zero loss: all collateral reaches Morpho, all borrowed tokens reach the receiver. No dust left
50
- * in bundler or adapter.
51
- *
52
- * @param params.market.chainId - The chain the market lives on.
53
- * @param params.market.marketParams - Market params (loanToken, collateralToken, oracle, irm, lltv).
54
- * @param params.args.amount - Amount of ERC-20 collateral to supply. At least one of `amount` or
55
- * `nativeAmount` must be positive. Defaults to `0n`.
56
- * @param params.args.borrowAmount - Loan asset amount to borrow.
57
- * @param params.args.onBehalf - Address whose Morpho position is credited with the collateral.
58
- * @param params.args.receiver - Address that receives the borrowed assets.
59
- * @param params.args.minSharePrice - Minimum borrow share price (in ray). Slippage protection.
60
- * @param params.args.requirementSignature - Optional pre-signed permit/permit2 approval for the
61
- * collateral funding. When collateral and loan tokens match, its amount includes V2 penalties.
62
- * @param params.args.nativeAmount - Optional amount of native token to wrap into wNative for the
63
- * collateral supply. Requires the collateral token to be the chain's wNative.
64
- * @param params.args.reallocations - Optional homogeneous Vault V1 or Vault V2 reallocations to
65
- * execute between the supply and borrow legs. Vault V1 inputs are deprecated; use Vault V2 for
66
- * new integrations.
67
- * @param params.args.authorizationSignature - Optional signed Morpho authorization; when present,
68
- * a `setAuthorizationWithSig` call is prepended to the bundle.
69
- * @param params.metadata - Optional analytics metadata attached to the bundle.
70
- * @returns A deep-frozen `Transaction<BlueSupplyCollateralBorrowAction>` with `to`, `value`,
71
- * `data`, and the typed `action` discriminator the simulation layer consumes.
72
- * @throws {NegativeInputError} when `amount`, `nativeAmount`, `minSharePrice`, a V1 fee, or a V2
73
- * penalty is negative.
74
- * @throws {NonPositiveInputError} when `borrowAmount <= 0n`, both collateral amounts resolve to
75
- * zero, or any reallocation withdrawal amount is non-positive.
76
- * @throws {InputExceedsMaxError} when a V2 reallocation asset amount exceeds `uint128` or its penalty exceeds WAD.
77
- * @throws {InconsistentReallocationPenaltyError} when V2 entries for one vault use different penalties.
78
- * @throws {InvalidReallocationAddressError} when a V2 vault or adapter address is malformed.
79
- * @throws {InvalidReallocationSourceTypeError} when a V2 source is absent, incomplete, or has an unknown discriminator.
80
- * @throws {InvalidReallocationShapeError} when an entry matches both or neither V1/V2 shape.
81
- * @throws {MixedReallocationVersionsError} when one plan contains both V1 and V2 entries.
82
- * @throws {ChainWNativeMissingError} when `nativeAmount > 0n` but the chain has no configured wNative.
83
- * @throws {NativeAmountOnNonWNativeAssetError} when `nativeAmount > 0n` but the collateral
84
- * token is not the chain's wNative.
85
- * @throws {DepositAssetMismatchError} from `getTokenRequirementActions` when `requirementSignature`
86
- * is provided and the signed asset differs from `marketParams.collateralToken`.
87
- * @throws {DepositAmountMismatchError} from `getTokenRequirementActions` when `requirementSignature`
88
- * is provided and the signed amount differs from the total ERC-20 funding amount.
89
- * @throws {Permit2ExpirationMissingError} from `getTokenRequirementActions` when a Permit2 requirement
90
- * signature is missing its expiration.
91
- * @throws {EmptyReallocationWithdrawalsError} when any `reallocation.withdrawals` is empty.
92
- * @throws {ReallocationWithdrawalOnTargetMarketError} when any reallocation withdrawal references
93
- * the target market.
94
- * @throws {UnsortedReallocationWithdrawalsError} when reallocation withdrawals are not strictly
95
- * sorted by market id.
45
+ * @param params - Combined-operation encoding parameters.
46
+ * @param params.market.chainId - Chain containing BlueBundlesV1.
47
+ * @param params.market.marketParams - Scoped Morpho Blue market parameters.
48
+ * @param params.args.userAddress - User whose position is changed.
49
+ * @param params.args.collateralAssets - Gross collateral supplied, or zero.
50
+ * @param params.args.borrowAssets - Loan assets borrowed, or zero.
51
+ * @param params.args.maxLtv - Maximum post-operation LTV.
52
+ * @param params.args.nativeAmount - Exclusive native collateral funding.
53
+ * @param params.args.reallocations - Vault V2 reallocations executed before borrowing.
54
+ * @param params.args.deadline - Final call deadline in Unix seconds.
55
+ * @param params.args.referralFeePct - Optional WAD-scaled fee below 100%.
56
+ * @param params.args.referralFeeRecipient - Recipient required for a positive fee.
57
+ * @param params.args.requirementSignature - Optional collateral token signature.
58
+ * @param params.args.authorizationSignature - Optional Blue authorization signature.
59
+ * @param params.metadata - Optional transaction metadata.
60
+ * @returns A deep-frozen `Readonly<Transaction<BlueSupplyCollateralBorrowAction>>`
61
+ * whose `to` address is BlueBundlesV1 and whose `action` records the normalized inputs.
62
+ * @throws {NegativeInputError} when an amount, `maxLtv`, native value, or fee is negative.
63
+ * @throws {NonPositiveInputError} when both operation legs are zero, the deadline is not positive,
64
+ * or a reallocation amount is not positive.
65
+ * @throws {ReallocationsRequireBorrowError} when reallocations accompany no borrow.
66
+ * @throws {InputExceedsMaxError} when a fee, reallocation amount, or penalty exceeds its ABI bound.
67
+ * @throws {MissingReferralFeeRecipientError} when a positive fee has no recipient.
68
+ * @throws {InvalidReallocationAddressError} when a vault or adapter address is malformed.
69
+ * @throws {InvalidReallocationSourceTypeError} when a reallocation source is malformed.
70
+ * @throws {InconsistentReallocationPenaltyError} when one vault uses different penalties.
71
+ * @throws {ReallocationWithdrawalOnTargetMarketError} when a source is the target market.
72
+ * @throws {ReallocationLoanTokenMismatchError} when a source market uses another loan token.
73
+ * @throws {NativeFundingAmountMismatchError} when native funding is partial or mixed.
74
+ * @throws {ChainWNativeMissingError} when native funding is requested on a chain without wNative.
75
+ * @throws {NativeAmountOnNonWNativeAssetError} when native funding targets another token.
76
+ * @throws {UnexpectedRequirementSignatureError} when a signature is supplied for an inactive leg.
77
+ * @throws {DepositOwnerMismatchError} when a signed owner differs from `userAddress`.
78
+ * @throws {DepositAssetMismatchError} when the signed asset differs from the collateral token.
79
+ * @throws {DepositAmountMismatchError} when the signed amount differs from `collateralAssets`.
80
+ * @throws {DepositSpenderMismatchError} when the signed spender is not BlueBundlesV1.
81
+ * @throws {BundlesRequirementSignatureMismatchError} when a signature cannot be bound safely.
82
+ * @throws {UnsupportedChainIdError} when the chain is absent from the registry.
83
+ * @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
96
84
  * @example
97
85
  * ```ts
86
+ * import { markets } from "@morpho-org/morpho-test";
98
87
  * import { blueSupplyCollateralBorrow } from "@morpho-org/morpho-sdk";
88
+ * import { maxUint256, zeroAddress } from "viem";
89
+ * import { mainnet } from "viem/chains";
99
90
  *
91
+ * const marketParams = markets[mainnet.id].usdc_wbtc;
100
92
  * const tx = blueSupplyCollateralBorrow({
101
- * market: { chainId: 1, marketParams },
93
+ * market: { chainId: mainnet.id, marketParams },
102
94
  * args: {
103
- * amount: 1_000_000_000_000_000_000n,
104
- * borrowAmount: 500_000_000n,
105
- * onBehalf: borrower,
106
- * receiver: borrower,
107
- * minSharePrice: 0n, // disables slippage protection — production code should compute via `computeMinBorrowSharePrice` from market state + slippage tolerance
95
+ * userAddress: zeroAddress,
96
+ * collateralAssets: 1_000_000_000_000_000_000n,
97
+ * borrowAssets: 0n,
98
+ * maxLtv: maxUint256,
99
+ * deadline: 1_900_000_000n,
108
100
  * },
109
101
  * });
110
102
  * // tx satisfies Readonly<Transaction<BlueSupplyCollateralBorrowAction>>
111
103
  * ```
112
104
  */
113
- export declare const blueSupplyCollateralBorrow: ({ market: { chainId, marketParams }, args: { amount, borrowAmount, onBehalf, receiver, minSharePrice, requirementSignature, nativeAmount, reallocations, authorizationSignature, }, metadata, }: BlueSupplyCollateralBorrowParams) => Readonly<Transaction<BlueSupplyCollateralBorrowAction>>;
105
+ export declare const blueSupplyCollateralBorrow: (params: BlueSupplyCollateralBorrowParams) => Readonly<Transaction<BlueSupplyCollateralBorrowAction>>;
@@ -1,154 +1,167 @@
1
- import { deepFreeze } from "@morpho-org/morpho-ts";
2
- import { isAddressEqual } from "viem";
3
- import { BundlerAction } from "../../bundler/index.js";
4
- import { addTransactionMetadata } from "../../helpers/index.js";
5
- import { NegativeInputError, NonPositiveInputError, } from "../../types/index.js";
6
- import { getBlueAuthorizationAction } from "../signatures/getBlueAuthorizationAction.js";
7
- import { buildAssetFundingActions } from "./buildAssetFundingActions.js";
8
- import { buildBlueReallocationActions } from "./buildReallocationActions.js";
1
+ import { encodeFunctionData } from "viem";
2
+ import { blueBundlesV1Abi } from "../../abis.js";
3
+ import { validateUint256Field } from "../../helpers/validate.js";
4
+ import { InputExceedsMaxError, NonPositiveInputError, ReallocationsRequireBorrowError, UnexpectedRequirementSignatureError, } from "../../types/index.js";
5
+ import { finalizeBlueBundlesV1Transaction, getBlueBundlesV1PenaltyAssets, getBlueBundlesV1PublicAllocations, getBlueBundlesV1SignedAuthorization, getBlueBundlesV1TokenPermit, normalizeBlueBundlesV1CommonParams, validateBlueBundlesV1NativeFunding, } from "./common.js";
9
6
  /**
10
- * Prepares an atomic supply-collateral-and-borrow transaction for a Morpho Blue market.
7
+ * Encodes a direct BlueBundlesV1 collateral supply and/or borrow transaction.
11
8
  *
12
- * Routed through bundler3: collateral funding → `morphoSupplyCollateral` → optional Public
13
- * Allocator calls → `morphoBorrow`. Each plan contains either V1 or V2 entries,
14
- * never both. When `nativeAmount > 0`, native ETH is wrapped via
15
- * `GeneralAdapter1.wrapNative()` before the supply leg. V1 fees add to
16
- * `tx.value`; V2 penalties are paid in the target loan token and donated to the vaults. When the
17
- * collateral and loan tokens match, one combined pull funds both collateral and penalties through
18
- * `GeneralAdapter1`.
9
+ * Reallocations are Vault V2-only and require a borrow leg. Penalties and referral fees reduce
10
+ * borrow proceeds. This route has no Bundler3 share-price bound or `slippageTolerance` input.
19
11
  *
20
- * Prerequisite: `GeneralAdapter1` must be authorized on Morpho to borrow on behalf of the user.
21
- * Use `getRequirements()` on the entity to check and obtain the authorization transaction.
22
- *
23
- * Zero loss: all collateral reaches Morpho, all borrowed tokens reach the receiver. No dust left
24
- * in bundler or adapter.
25
- *
26
- * @param params.market.chainId - The chain the market lives on.
27
- * @param params.market.marketParams - Market params (loanToken, collateralToken, oracle, irm, lltv).
28
- * @param params.args.amount - Amount of ERC-20 collateral to supply. At least one of `amount` or
29
- * `nativeAmount` must be positive. Defaults to `0n`.
30
- * @param params.args.borrowAmount - Loan asset amount to borrow.
31
- * @param params.args.onBehalf - Address whose Morpho position is credited with the collateral.
32
- * @param params.args.receiver - Address that receives the borrowed assets.
33
- * @param params.args.minSharePrice - Minimum borrow share price (in ray). Slippage protection.
34
- * @param params.args.requirementSignature - Optional pre-signed permit/permit2 approval for the
35
- * collateral funding. When collateral and loan tokens match, its amount includes V2 penalties.
36
- * @param params.args.nativeAmount - Optional amount of native token to wrap into wNative for the
37
- * collateral supply. Requires the collateral token to be the chain's wNative.
38
- * @param params.args.reallocations - Optional homogeneous Vault V1 or Vault V2 reallocations to
39
- * execute between the supply and borrow legs. Vault V1 inputs are deprecated; use Vault V2 for
40
- * new integrations.
41
- * @param params.args.authorizationSignature - Optional signed Morpho authorization; when present,
42
- * a `setAuthorizationWithSig` call is prepended to the bundle.
43
- * @param params.metadata - Optional analytics metadata attached to the bundle.
44
- * @returns A deep-frozen `Transaction<BlueSupplyCollateralBorrowAction>` with `to`, `value`,
45
- * `data`, and the typed `action` discriminator the simulation layer consumes.
46
- * @throws {NegativeInputError} when `amount`, `nativeAmount`, `minSharePrice`, a V1 fee, or a V2
47
- * penalty is negative.
48
- * @throws {NonPositiveInputError} when `borrowAmount <= 0n`, both collateral amounts resolve to
49
- * zero, or any reallocation withdrawal amount is non-positive.
50
- * @throws {InputExceedsMaxError} when a V2 reallocation asset amount exceeds `uint128` or its penalty exceeds WAD.
51
- * @throws {InconsistentReallocationPenaltyError} when V2 entries for one vault use different penalties.
52
- * @throws {InvalidReallocationAddressError} when a V2 vault or adapter address is malformed.
53
- * @throws {InvalidReallocationSourceTypeError} when a V2 source is absent, incomplete, or has an unknown discriminator.
54
- * @throws {InvalidReallocationShapeError} when an entry matches both or neither V1/V2 shape.
55
- * @throws {MixedReallocationVersionsError} when one plan contains both V1 and V2 entries.
56
- * @throws {ChainWNativeMissingError} when `nativeAmount > 0n` but the chain has no configured wNative.
57
- * @throws {NativeAmountOnNonWNativeAssetError} when `nativeAmount > 0n` but the collateral
58
- * token is not the chain's wNative.
59
- * @throws {DepositAssetMismatchError} from `getTokenRequirementActions` when `requirementSignature`
60
- * is provided and the signed asset differs from `marketParams.collateralToken`.
61
- * @throws {DepositAmountMismatchError} from `getTokenRequirementActions` when `requirementSignature`
62
- * is provided and the signed amount differs from the total ERC-20 funding amount.
63
- * @throws {Permit2ExpirationMissingError} from `getTokenRequirementActions` when a Permit2 requirement
64
- * signature is missing its expiration.
65
- * @throws {EmptyReallocationWithdrawalsError} when any `reallocation.withdrawals` is empty.
66
- * @throws {ReallocationWithdrawalOnTargetMarketError} when any reallocation withdrawal references
67
- * the target market.
68
- * @throws {UnsortedReallocationWithdrawalsError} when reallocation withdrawals are not strictly
69
- * sorted by market id.
12
+ * @param params - Combined-operation 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 position is changed.
16
+ * @param params.args.collateralAssets - Gross collateral supplied, or zero.
17
+ * @param params.args.borrowAssets - Loan assets borrowed, or zero.
18
+ * @param params.args.maxLtv - Maximum post-operation LTV.
19
+ * @param params.args.nativeAmount - Exclusive native collateral funding.
20
+ * @param params.args.reallocations - Vault V2 reallocations executed before borrowing.
21
+ * @param params.args.deadline - Final call deadline in Unix seconds.
22
+ * @param params.args.referralFeePct - Optional WAD-scaled fee below 100%.
23
+ * @param params.args.referralFeeRecipient - Recipient required for a positive fee.
24
+ * @param params.args.requirementSignature - Optional collateral token signature.
25
+ * @param params.args.authorizationSignature - Optional Blue authorization signature.
26
+ * @param params.metadata - Optional transaction metadata.
27
+ * @returns A deep-frozen `Readonly<Transaction<BlueSupplyCollateralBorrowAction>>`
28
+ * whose `to` address is BlueBundlesV1 and whose `action` records the normalized inputs.
29
+ * @throws {NegativeInputError} when an amount, `maxLtv`, native value, or fee is negative.
30
+ * @throws {NonPositiveInputError} when both operation legs are zero, the deadline is not positive,
31
+ * or a reallocation amount is not positive.
32
+ * @throws {ReallocationsRequireBorrowError} when reallocations accompany no borrow.
33
+ * @throws {InputExceedsMaxError} when a fee, reallocation amount, or penalty exceeds its ABI bound.
34
+ * @throws {MissingReferralFeeRecipientError} when a positive fee has no recipient.
35
+ * @throws {InvalidReallocationAddressError} when a vault or adapter address is malformed.
36
+ * @throws {InvalidReallocationSourceTypeError} when a reallocation source is malformed.
37
+ * @throws {InconsistentReallocationPenaltyError} when one vault uses different penalties.
38
+ * @throws {ReallocationWithdrawalOnTargetMarketError} when a source is the target market.
39
+ * @throws {ReallocationLoanTokenMismatchError} when a source market uses another loan token.
40
+ * @throws {NativeFundingAmountMismatchError} when native funding is partial or mixed.
41
+ * @throws {ChainWNativeMissingError} when native funding is requested on a chain without wNative.
42
+ * @throws {NativeAmountOnNonWNativeAssetError} when native funding targets another token.
43
+ * @throws {UnexpectedRequirementSignatureError} when a signature is supplied for an inactive leg.
44
+ * @throws {DepositOwnerMismatchError} when a signed owner differs from `userAddress`.
45
+ * @throws {DepositAssetMismatchError} when the signed asset differs from the collateral token.
46
+ * @throws {DepositAmountMismatchError} when the signed amount differs from `collateralAssets`.
47
+ * @throws {DepositSpenderMismatchError} when the signed spender is not BlueBundlesV1.
48
+ * @throws {BundlesRequirementSignatureMismatchError} when a signature cannot be bound safely.
49
+ * @throws {UnsupportedChainIdError} when the chain is absent from the registry.
50
+ * @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
70
51
  * @example
71
52
  * ```ts
53
+ * import { markets } from "@morpho-org/morpho-test";
72
54
  * import { blueSupplyCollateralBorrow } from "@morpho-org/morpho-sdk";
55
+ * import { maxUint256, zeroAddress } from "viem";
56
+ * import { mainnet } from "viem/chains";
73
57
  *
58
+ * const marketParams = markets[mainnet.id].usdc_wbtc;
74
59
  * const tx = blueSupplyCollateralBorrow({
75
- * market: { chainId: 1, marketParams },
60
+ * market: { chainId: mainnet.id, marketParams },
76
61
  * args: {
77
- * amount: 1_000_000_000_000_000_000n,
78
- * borrowAmount: 500_000_000n,
79
- * onBehalf: borrower,
80
- * receiver: borrower,
81
- * minSharePrice: 0n, // disables slippage protection — production code should compute via `computeMinBorrowSharePrice` from market state + slippage tolerance
62
+ * userAddress: zeroAddress,
63
+ * collateralAssets: 1_000_000_000_000_000_000n,
64
+ * borrowAssets: 0n,
65
+ * maxLtv: maxUint256,
66
+ * deadline: 1_900_000_000n,
82
67
  * },
83
68
  * });
84
69
  * // tx satisfies Readonly<Transaction<BlueSupplyCollateralBorrowAction>>
85
70
  * ```
86
71
  */
87
- export const blueSupplyCollateralBorrow = ({ market: { chainId, marketParams }, args: { amount = 0n, borrowAmount, onBehalf, receiver, minSharePrice, requirementSignature, nativeAmount, reallocations, authorizationSignature, }, metadata, }) => {
88
- if (amount < 0n) {
89
- throw new NegativeInputError("amount", amount);
72
+ export const blueSupplyCollateralBorrow = (params) => {
73
+ const { chainId, marketParams } = params.market;
74
+ const { userAddress, collateralAssets, borrowAssets, maxLtv, requirementSignature, authorizationSignature, } = params.args;
75
+ for (const [field, value] of [
76
+ ["collateralAssets", collateralAssets],
77
+ ["borrowAssets", borrowAssets],
78
+ ["maxLtv", maxLtv],
79
+ ]) {
80
+ validateUint256Field(field, value);
90
81
  }
91
- if (nativeAmount !== undefined && nativeAmount < 0n) {
92
- throw new NegativeInputError("nativeAmount", nativeAmount);
82
+ if (collateralAssets === 0n && borrowAssets === 0n) {
83
+ throw new NonPositiveInputError("collateralAssets or borrowAssets", 0n);
93
84
  }
94
- if (borrowAmount <= 0n) {
95
- throw new NonPositiveInputError("borrowAmount", borrowAmount);
85
+ const reallocations = [...(params.args.reallocations ?? [])];
86
+ if (borrowAssets === 0n && reallocations.length > 0) {
87
+ throw new ReallocationsRequireBorrowError();
96
88
  }
97
- if (minSharePrice < 0n) {
98
- throw new NegativeInputError("minSharePrice", minSharePrice);
89
+ if (collateralAssets === 0n && requirementSignature != null) {
90
+ throw new UnexpectedRequirementSignatureError(requirementSignature.action.type);
99
91
  }
100
- const totalCollateral = amount + (nativeAmount ?? 0n);
101
- if (totalCollateral === 0n) {
102
- throw new NonPositiveInputError("totalCollateral", totalCollateral);
92
+ if (borrowAssets === 0n && authorizationSignature != null) {
93
+ throw new UnexpectedRequirementSignatureError("authorization");
103
94
  }
104
- const usesSharedFundingToken = isAddressEqual(marketParams.collateralToken, marketParams.loanToken);
105
- const reallocationResult = buildBlueReallocationActions({
95
+ const common = {
96
+ chainId,
97
+ userAddress,
98
+ deadline: params.args.deadline,
99
+ referralFeePct: params.args.referralFeePct,
100
+ referralFeeRecipient: params.args.referralFeeRecipient,
101
+ metadata: params.metadata,
102
+ };
103
+ const { referralFeePct, referralFeeRecipient } = normalizeBlueBundlesV1CommonParams(common);
104
+ const value = validateBlueBundlesV1NativeFunding({
106
105
  chainId,
107
- reallocations,
108
- targetMarketParams: marketParams,
109
- penaltyFundingSource: usesSharedFundingToken
110
- ? "generalAdapter1"
111
- : "initiator",
106
+ token: marketParams.collateralToken,
107
+ fundedAmount: collateralAssets,
108
+ nativeAmount: params.args.nativeAmount,
109
+ requirementSignature,
112
110
  });
113
- const erc20FundingAmount = amount + (usesSharedFundingToken ? reallocationResult.penaltyAssets : 0n);
114
- const actions = [];
115
- if (authorizationSignature) {
116
- actions.push(getBlueAuthorizationAction(chainId, authorizationSignature));
117
- }
118
- actions.push(...buildAssetFundingActions({
111
+ const collateralPermit = getBlueBundlesV1TokenPermit({
119
112
  chainId,
120
- asset: marketParams.collateralToken,
121
- erc20Amount: erc20FundingAmount,
122
- nativeAmount: nativeAmount ?? 0n,
113
+ userAddress,
114
+ token: marketParams.collateralToken,
115
+ amount: collateralAssets,
123
116
  requirementSignature,
124
- }));
125
- actions.push({
126
- type: "morphoSupplyCollateral",
127
- args: [marketParams, totalCollateral, onBehalf, [], false],
128
117
  });
129
- actions.push(...reallocationResult.actions);
130
- actions.push({
131
- type: "morphoBorrow",
132
- args: [marketParams, borrowAmount, 0n, minSharePrice, receiver, false],
118
+ const signedAuthorization = getBlueBundlesV1SignedAuthorization({
119
+ chainId,
120
+ userAddress,
121
+ authorizationSignature,
133
122
  });
134
- let tx = BundlerAction.encodeBundle(chainId, actions);
135
- if (metadata) {
136
- tx = addTransactionMetadata(tx, metadata);
123
+ const publicAllocations = getBlueBundlesV1PublicAllocations(reallocations, marketParams);
124
+ const reallocationPenaltyAssets = getBlueBundlesV1PenaltyAssets(publicAllocations);
125
+ if (reallocationPenaltyAssets > borrowAssets) {
126
+ throw new InputExceedsMaxError({
127
+ field: "reallocationPenaltyAssets",
128
+ value: reallocationPenaltyAssets,
129
+ max: borrowAssets,
130
+ });
137
131
  }
138
- return deepFreeze({
139
- ...tx,
132
+ return finalizeBlueBundlesV1Transaction({
133
+ common,
134
+ value,
135
+ data: encodeFunctionData({
136
+ abi: blueBundlesV1Abi,
137
+ functionName: "blueBundlesV1SupplyCollateralAndBorrow",
138
+ args: [
139
+ marketParams,
140
+ collateralAssets,
141
+ borrowAssets,
142
+ maxLtv,
143
+ collateralPermit,
144
+ signedAuthorization,
145
+ publicAllocations,
146
+ referralFeePct,
147
+ referralFeeRecipient,
148
+ params.args.deadline,
149
+ ],
150
+ }),
140
151
  action: {
141
152
  type: "blueSupplyCollateralBorrow",
142
153
  args: {
143
154
  market: marketParams.id,
144
- collateralAmount: totalCollateral,
145
- borrowAmount,
146
- minSharePrice,
147
- onBehalf,
148
- receiver,
149
- nativeAmount,
150
- reallocationFee: reallocationResult.fee,
151
- reallocationPenaltyAssets: reallocationResult.penaltyAssets,
155
+ collateralAssets,
156
+ borrowAssets,
157
+ maxLtv,
158
+ onBehalf: userAddress,
159
+ nativeAmount: value > 0n ? value : undefined,
160
+ reallocations: publicAllocations.length,
161
+ reallocationPenaltyAssets,
162
+ referralFeePct,
163
+ referralFeeRecipient,
164
+ deadline: params.args.deadline,
152
165
  },
153
166
  },
154
167
  });