@morpho-org/morpho-sdk 6.0.0-next.2 → 6.0.0-next.3

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 (257) hide show
  1. package/MIGRATION-v5-to-v6.md +83 -29
  2. package/README.md +9 -7
  3. package/lib/cjs/abis.d.ts +3 -2844
  4. package/lib/cjs/abis.js +1 -1951
  5. package/lib/cjs/actions/blue/borrow.d.ts +3 -2
  6. package/lib/cjs/actions/blue/borrow.js +3 -2
  7. package/lib/cjs/actions/blue/common.js +1 -9
  8. package/lib/cjs/actions/blue/refinance.d.ts +2 -2
  9. package/lib/cjs/actions/blue/refinance.js +2 -2
  10. package/lib/cjs/actions/blue/repay.d.ts +1 -1
  11. package/lib/cjs/actions/blue/repay.js +1 -1
  12. package/lib/cjs/actions/blue/repayWithdrawCollateral.d.ts +2 -2
  13. package/lib/cjs/actions/blue/repayWithdrawCollateral.js +2 -2
  14. package/lib/cjs/actions/blue/supply.d.ts +2 -2
  15. package/lib/cjs/actions/blue/supply.js +2 -2
  16. package/lib/cjs/actions/blue/supplyCollateral.d.ts +2 -2
  17. package/lib/cjs/actions/blue/supplyCollateral.js +2 -2
  18. package/lib/cjs/actions/blue/supplyCollateralBorrow.d.ts +3 -2
  19. package/lib/cjs/actions/blue/supplyCollateralBorrow.js +3 -2
  20. package/lib/cjs/actions/blue/withdraw.d.ts +3 -3
  21. package/lib/cjs/actions/blue/withdraw.js +3 -3
  22. package/lib/cjs/actions/blue/withdrawCollateral.d.ts +2 -2
  23. package/lib/cjs/actions/blue/withdrawCollateral.js +2 -2
  24. package/lib/cjs/actions/bundles/common.d.ts +6 -2
  25. package/lib/cjs/actions/bundles/common.js +23 -5
  26. package/lib/cjs/actions/index.d.ts +0 -1
  27. package/lib/cjs/actions/index.js +0 -1
  28. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +6 -8
  29. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +11 -16
  30. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +5 -6
  31. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +4 -5
  32. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +4 -4
  33. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +4 -5
  34. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +5 -5
  35. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +6 -11
  36. package/lib/cjs/actions/requirements/encode/index.d.ts +0 -1
  37. package/lib/cjs/actions/requirements/encode/index.js +0 -1
  38. package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +5 -6
  39. package/lib/cjs/actions/requirements/getRequirementsApproval.js +5 -6
  40. package/lib/cjs/actions/requirements/index.d.ts +0 -1
  41. package/lib/cjs/actions/requirements/index.js +0 -1
  42. package/lib/cjs/actions/vaultV1/deposit.d.ts +1 -2
  43. package/lib/cjs/actions/vaultV1/deposit.js +1 -2
  44. package/lib/cjs/actions/vaultV1/inKindRedeem.d.ts +6 -5
  45. package/lib/cjs/actions/vaultV1/inKindRedeem.js +10 -7
  46. package/lib/cjs/actions/vaultV2/deposit.d.ts +1 -2
  47. package/lib/cjs/actions/vaultV2/deposit.js +1 -2
  48. package/lib/cjs/actions/vaultV2/forceWithdraw.d.ts +1 -1
  49. package/lib/cjs/actions/vaultV2/forceWithdraw.js +3 -3
  50. package/lib/cjs/actions/vaultV2/inKindRedeem.d.ts +6 -5
  51. package/lib/cjs/actions/vaultV2/inKindRedeem.js +10 -7
  52. package/lib/cjs/augment.d.ts +0 -1
  53. package/lib/cjs/augment.js +0 -1
  54. package/lib/cjs/blue/abis.d.ts +1 -5
  55. package/lib/cjs/blue/abis.js +1 -7
  56. package/lib/cjs/blue/entities.d.ts +2 -8
  57. package/lib/cjs/blue/entities.js +1 -3
  58. package/lib/cjs/blue/fetch.d.ts +1 -3
  59. package/lib/cjs/blue/fetch.js +1 -3
  60. package/lib/cjs/blue/types.d.ts +2 -4
  61. package/lib/cjs/blue/utils.d.ts +1 -3
  62. package/lib/cjs/blue/utils.js +1 -3
  63. package/lib/cjs/client/morphoViemExtension.d.ts +1 -1
  64. package/lib/cjs/client/morphoViemExtension.js +1 -1
  65. package/lib/cjs/constants.d.ts +3 -37
  66. package/lib/cjs/constants.js +1 -39
  67. package/lib/cjs/entities/blue/blue.d.ts +30 -265
  68. package/lib/cjs/entities/blue/blue.js +1 -205
  69. package/lib/cjs/entities/index.d.ts +3 -66
  70. package/lib/cjs/entities/index.js +1 -21
  71. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +9 -2
  72. package/lib/cjs/entities/vaultV1/vaultV1.js +12 -7
  73. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +22 -9
  74. package/lib/cjs/entities/vaultV2/vaultV2.js +28 -15
  75. package/lib/cjs/entities/vaultV2BlueReallocationData.js +4 -21
  76. package/lib/cjs/errors.d.ts +2 -46
  77. package/lib/cjs/errors.js +1 -46
  78. package/lib/cjs/fetch.d.ts +1 -17
  79. package/lib/cjs/fetch.js +1 -17
  80. package/lib/cjs/helpers/constant.d.ts +0 -2
  81. package/lib/cjs/helpers/constant.js +0 -2
  82. package/lib/cjs/helpers/index.d.ts +2 -3
  83. package/lib/cjs/helpers/index.js +1 -9
  84. package/lib/cjs/helpers/slippage.d.ts +1 -103
  85. package/lib/cjs/helpers/slippage.js +0 -150
  86. package/lib/cjs/helpers/utilization.d.ts +12 -10
  87. package/lib/cjs/helpers/utilization.js +27 -12
  88. package/lib/cjs/helpers/validate.d.ts +1 -24
  89. package/lib/cjs/helpers/validate.js +2 -49
  90. package/lib/cjs/helpers/validateRequirementSpender.d.ts +1 -1
  91. package/lib/cjs/helpers/validateRequirementSpender.js +1 -3
  92. package/lib/cjs/types/action.d.ts +17 -65
  93. package/lib/cjs/types/action.js +11 -4
  94. package/lib/cjs/types/error.d.ts +50 -381
  95. package/lib/cjs/types/error.js +64 -395
  96. package/lib/cjs/types/sharedLiquidity.d.ts +1 -117
  97. package/lib/cjs/types.d.ts +2 -18
  98. package/lib/cjs/utils.d.ts +6 -35
  99. package/lib/cjs/utils.js +2 -36
  100. package/lib/esm/abis.d.ts +3 -2844
  101. package/lib/esm/abis.js +3 -1950
  102. package/lib/esm/actions/blue/borrow.d.ts +3 -2
  103. package/lib/esm/actions/blue/borrow.js +3 -2
  104. package/lib/esm/actions/blue/common.js +1 -9
  105. package/lib/esm/actions/blue/refinance.d.ts +2 -2
  106. package/lib/esm/actions/blue/refinance.js +2 -2
  107. package/lib/esm/actions/blue/repay.d.ts +1 -1
  108. package/lib/esm/actions/blue/repay.js +1 -1
  109. package/lib/esm/actions/blue/repayWithdrawCollateral.d.ts +2 -2
  110. package/lib/esm/actions/blue/repayWithdrawCollateral.js +2 -2
  111. package/lib/esm/actions/blue/supply.d.ts +2 -2
  112. package/lib/esm/actions/blue/supply.js +2 -2
  113. package/lib/esm/actions/blue/supplyCollateral.d.ts +2 -2
  114. package/lib/esm/actions/blue/supplyCollateral.js +2 -2
  115. package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +3 -2
  116. package/lib/esm/actions/blue/supplyCollateralBorrow.js +3 -2
  117. package/lib/esm/actions/blue/withdraw.d.ts +3 -3
  118. package/lib/esm/actions/blue/withdraw.js +3 -3
  119. package/lib/esm/actions/blue/withdrawCollateral.d.ts +2 -2
  120. package/lib/esm/actions/blue/withdrawCollateral.js +2 -2
  121. package/lib/esm/actions/bundles/common.d.ts +6 -2
  122. package/lib/esm/actions/bundles/common.js +24 -6
  123. package/lib/esm/actions/index.d.ts +0 -1
  124. package/lib/esm/actions/index.js +0 -1
  125. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +6 -8
  126. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +12 -17
  127. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +5 -6
  128. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +4 -5
  129. package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +4 -4
  130. package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +4 -5
  131. package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +5 -5
  132. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +6 -11
  133. package/lib/esm/actions/requirements/encode/index.d.ts +0 -1
  134. package/lib/esm/actions/requirements/encode/index.js +0 -1
  135. package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +5 -6
  136. package/lib/esm/actions/requirements/getRequirementsApproval.js +5 -6
  137. package/lib/esm/actions/requirements/index.d.ts +0 -1
  138. package/lib/esm/actions/requirements/index.js +0 -1
  139. package/lib/esm/actions/vaultV1/deposit.d.ts +1 -2
  140. package/lib/esm/actions/vaultV1/deposit.js +1 -2
  141. package/lib/esm/actions/vaultV1/inKindRedeem.d.ts +6 -5
  142. package/lib/esm/actions/vaultV1/inKindRedeem.js +10 -7
  143. package/lib/esm/actions/vaultV2/deposit.d.ts +1 -2
  144. package/lib/esm/actions/vaultV2/deposit.js +1 -2
  145. package/lib/esm/actions/vaultV2/forceWithdraw.d.ts +1 -1
  146. package/lib/esm/actions/vaultV2/forceWithdraw.js +3 -3
  147. package/lib/esm/actions/vaultV2/inKindRedeem.d.ts +6 -5
  148. package/lib/esm/actions/vaultV2/inKindRedeem.js +10 -7
  149. package/lib/esm/augment.d.ts +0 -1
  150. package/lib/esm/augment.js +0 -1
  151. package/lib/esm/blue/abis.d.ts +1 -5
  152. package/lib/esm/blue/abis.js +1 -5
  153. package/lib/esm/blue/entities.d.ts +2 -8
  154. package/lib/esm/blue/entities.js +1 -3
  155. package/lib/esm/blue/fetch.d.ts +1 -3
  156. package/lib/esm/blue/fetch.js +1 -3
  157. package/lib/esm/blue/types.d.ts +2 -4
  158. package/lib/esm/blue/utils.d.ts +1 -3
  159. package/lib/esm/blue/utils.js +1 -3
  160. package/lib/esm/client/morphoViemExtension.d.ts +1 -1
  161. package/lib/esm/client/morphoViemExtension.js +1 -1
  162. package/lib/esm/constants.d.ts +3 -37
  163. package/lib/esm/constants.js +3 -37
  164. package/lib/esm/entities/blue/blue.d.ts +30 -265
  165. package/lib/esm/entities/blue/blue.js +4 -208
  166. package/lib/esm/entities/index.d.ts +3 -66
  167. package/lib/esm/entities/index.js +1 -18
  168. package/lib/esm/entities/vaultV1/vaultV1.d.ts +9 -2
  169. package/lib/esm/entities/vaultV1/vaultV1.js +15 -10
  170. package/lib/esm/entities/vaultV2/vaultV2.d.ts +22 -9
  171. package/lib/esm/entities/vaultV2/vaultV2.js +32 -19
  172. package/lib/esm/entities/vaultV2BlueReallocationData.js +2 -19
  173. package/lib/esm/errors.d.ts +2 -46
  174. package/lib/esm/errors.js +2 -46
  175. package/lib/esm/fetch.d.ts +1 -17
  176. package/lib/esm/fetch.js +1 -17
  177. package/lib/esm/helpers/constant.d.ts +0 -2
  178. package/lib/esm/helpers/constant.js +0 -2
  179. package/lib/esm/helpers/index.d.ts +2 -3
  180. package/lib/esm/helpers/index.js +2 -3
  181. package/lib/esm/helpers/slippage.d.ts +1 -103
  182. package/lib/esm/helpers/slippage.js +1 -147
  183. package/lib/esm/helpers/utilization.d.ts +12 -10
  184. package/lib/esm/helpers/utilization.js +26 -11
  185. package/lib/esm/helpers/validate.d.ts +1 -24
  186. package/lib/esm/helpers/validate.js +2 -48
  187. package/lib/esm/helpers/validateRequirementSpender.d.ts +1 -1
  188. package/lib/esm/helpers/validateRequirementSpender.js +1 -3
  189. package/lib/esm/types/action.d.ts +17 -65
  190. package/lib/esm/types/action.js +12 -5
  191. package/lib/esm/types/error.d.ts +50 -381
  192. package/lib/esm/types/error.js +58 -377
  193. package/lib/esm/types/sharedLiquidity.d.ts +1 -117
  194. package/lib/esm/types.d.ts +2 -18
  195. package/lib/esm/utils.d.ts +6 -35
  196. package/lib/esm/utils.js +5 -34
  197. package/package.json +5 -13
  198. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2Approve.d.ts +0 -44
  199. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2Approve.js +0 -88
  200. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirements.d.ts +0 -88
  201. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirements.js +0 -152
  202. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +0 -40
  203. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.js +0 -53
  204. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.d.ts +0 -57
  205. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.js +0 -74
  206. package/lib/cjs/actions/requirements/generalAdapter/index.d.ts +0 -3
  207. package/lib/cjs/actions/requirements/generalAdapter/index.js +0 -19
  208. package/lib/cjs/actions/signatures/getBlueAuthorizationAction.d.ts +0 -29
  209. package/lib/cjs/actions/signatures/getBlueAuthorizationAction.js +0 -48
  210. package/lib/cjs/actions/signatures/getTokenRequirementActions.d.ts +0 -58
  211. package/lib/cjs/actions/signatures/getTokenRequirementActions.js +0 -119
  212. package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +0 -50
  213. package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.js +0 -45
  214. package/lib/cjs/actions/signatures/index.d.ts +0 -3
  215. package/lib/cjs/actions/signatures/index.js +0 -19
  216. package/lib/cjs/augment/VaultMarketPublicAllocatorConfig.d.ts +0 -11
  217. package/lib/cjs/augment/VaultMarketPublicAllocatorConfig.js +0 -8
  218. package/lib/cjs/bundler/actions.d.ts +0 -797
  219. package/lib/cjs/bundler/actions.js +0 -1371
  220. package/lib/cjs/bundler/index.d.ts +0 -2
  221. package/lib/cjs/bundler/index.js +0 -18
  222. package/lib/cjs/bundler/types.d.ts +0 -235
  223. package/lib/cjs/bundler/types.js +0 -2
  224. package/lib/cjs/entities/vaultV1ReallocationData.d.ts +0 -332
  225. package/lib/cjs/entities/vaultV1ReallocationData.js +0 -557
  226. package/lib/cjs/helpers/computeVaultV1Reallocations.d.ts +0 -80
  227. package/lib/cjs/helpers/computeVaultV1Reallocations.js +0 -245
  228. package/lib/esm/actions/requirements/encode/encodeErc20Permit2Approve.d.ts +0 -44
  229. package/lib/esm/actions/requirements/encode/encodeErc20Permit2Approve.js +0 -84
  230. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirements.d.ts +0 -88
  231. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirements.js +0 -148
  232. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +0 -40
  233. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.js +0 -49
  234. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.d.ts +0 -57
  235. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.js +0 -70
  236. package/lib/esm/actions/requirements/generalAdapter/index.d.ts +0 -3
  237. package/lib/esm/actions/requirements/generalAdapter/index.js +0 -3
  238. package/lib/esm/actions/signatures/getBlueAuthorizationAction.d.ts +0 -29
  239. package/lib/esm/actions/signatures/getBlueAuthorizationAction.js +0 -44
  240. package/lib/esm/actions/signatures/getTokenRequirementActions.d.ts +0 -58
  241. package/lib/esm/actions/signatures/getTokenRequirementActions.js +0 -115
  242. package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +0 -50
  243. package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.js +0 -41
  244. package/lib/esm/actions/signatures/index.d.ts +0 -3
  245. package/lib/esm/actions/signatures/index.js +0 -3
  246. package/lib/esm/augment/VaultMarketPublicAllocatorConfig.d.ts +0 -11
  247. package/lib/esm/augment/VaultMarketPublicAllocatorConfig.js +0 -7
  248. package/lib/esm/bundler/actions.d.ts +0 -797
  249. package/lib/esm/bundler/actions.js +0 -1368
  250. package/lib/esm/bundler/index.d.ts +0 -2
  251. package/lib/esm/bundler/index.js +0 -2
  252. package/lib/esm/bundler/types.d.ts +0 -235
  253. package/lib/esm/bundler/types.js +0 -1
  254. package/lib/esm/entities/vaultV1ReallocationData.d.ts +0 -332
  255. package/lib/esm/entities/vaultV1ReallocationData.js +0 -559
  256. package/lib/esm/helpers/computeVaultV1Reallocations.d.ts +0 -80
  257. package/lib/esm/helpers/computeVaultV1Reallocations.js +0 -241
@@ -1,1368 +0,0 @@
1
- import { getChainAddresses, VaultV2BluePublicAllocatorConfigUtils, } from "@morpho-org/blue-sdk";
2
- import { blueAbi, erc2612Abi, permit2Abi, vaultV1PublicAllocatorAbi, vaultV2BluePublicAllocatorAbi, } from "@morpho-org/blue-sdk-viem";
3
- import { encodeAbiParameters, encodeFunctionData, erc20Abi, isAddressEqual, keccak256, parseSignature, serializeSignature, zeroHash, } from "viem";
4
- import { bundler3Abi, coreAdapterAbi, generalAdapter1Abi } from "../abis.js";
5
- import { BundlerErrors } from "../types/error.js";
6
- const reenterAbiInputs = bundler3Abi.find((item) => item.type === "function" && item.name === "reenter").inputs;
7
- const encodeCallbackCalls = (callbackCalls) => {
8
- const reenter = callbackCalls.length > 0;
9
- const reenterData = reenter
10
- ? encodeAbiParameters(reenterAbiInputs, [callbackCalls])
11
- : "0x";
12
- return {
13
- callbackHash: reenter ? keccak256(reenterData) : zeroHash,
14
- reenterData,
15
- };
16
- };
17
- const addBundlerPrefund = (state, amount) => ({
18
- value: state.value + amount,
19
- availableBundlerValue: state.availableBundlerValue + amount,
20
- });
21
- /**
22
- * Normalizes a raw ECDSA signature to its hex form. Integrators get a `Hex` from
23
- * `signTypedData`/`signMessage` and a viem `Signature` object from the low-level
24
- * `sign`; both are accepted so no manual conversion is required at the call site.
25
- */
26
- const toSignatureHex = (signature) => typeof signature === "string" ? signature : serializeSignature(signature);
27
- const consumeCallValue = (state, call) => {
28
- if (call.value > state.availableBundlerValue) {
29
- return {
30
- value: state.value + call.value - state.availableBundlerValue,
31
- availableBundlerValue: 0n,
32
- };
33
- }
34
- return {
35
- value: state.value,
36
- availableBundlerValue: state.availableBundlerValue - call.value,
37
- };
38
- };
39
- /**
40
- * Encodes the Bundler3 action subset used by `morpho-sdk` transaction builders.
41
- *
42
- * @remarks
43
- * The namespace covers only the Bundler3 actions required by `morpho-sdk`.
44
- * It does not expose operation population or broad simulation helpers.
45
- */
46
- export var BundlerAction;
47
- (function (BundlerAction) {
48
- /**
49
- * Encodes a list of Bundler3 actions into a single Bundler3 multicall
50
- * transaction request.
51
- *
52
- * @remarks
53
- * This is a low-level encoding helper, not a safe bundle constructor. It
54
- * does not validate that native-token pre-funding is fully consumed or that
55
- * pre-funding actions are ordered before the value-carrying calls they fund.
56
- *
57
- * When custom actions include `nativeTransfer(externalOwner, bundler3,
58
- * amount)`, no inner Bundler3 call is emitted for that transfer. Consumers
59
- * must ensure those pre-funding actions precede their downstream consumers
60
- * and leave no residual pre-funded native token; otherwise `tx.value` can be
61
- * over-counted or native token can remain stranded on Bundler3. The
62
- * high-level `morpho-sdk` action builders construct actions in that order,
63
- * but this invariant is not checked at runtime by `encodeBundle`.
64
- *
65
- * @param chainId - Chain where the bundle will execute.
66
- * @param actions - Ordered Bundler3 actions to encode.
67
- * @returns Transaction target, calldata, and native value required by the
68
- * bundle.
69
- * @throws {BundlerErrors.MissingSignature} when a signature action is unsigned.
70
- * @throws {BundlerErrors.UnexpectedAction} when an action is unavailable on the chain.
71
- *
72
- * @example
73
- * ```ts
74
- * import { getChainAddresses } from "@morpho-org/morpho-sdk/addresses";
75
- * import { BundlerAction } from "@morpho-org/morpho-sdk/bundler";
76
- *
77
- * const { generalAdapter1 } = getChainAddresses(1).bundler3;
78
- * const sender = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
79
- *
80
- * const tx = BundlerAction.encodeBundle(1, [
81
- * {
82
- * type: "nativeTransfer",
83
- * args: [sender, generalAdapter1, 1_000000000000000000n],
84
- * },
85
- * ]);
86
- * ```
87
- */
88
- function encodeBundle(chainId, actions) {
89
- const { bundler3: { bundler3 }, } = getChainAddresses(chainId);
90
- let valueState = {
91
- value: 0n,
92
- availableBundlerValue: 0n,
93
- };
94
- const encodedActions = [];
95
- for (const action of actions) {
96
- const encodedAction = encodeBundleAction({ chainId, action, valueState });
97
- encodedActions.push(...encodedAction.calls);
98
- valueState = encodedAction.valueState;
99
- }
100
- return {
101
- to: bundler3,
102
- value: valueState.value,
103
- data: encodeFunctionData({
104
- abi: bundler3Abi,
105
- functionName: "multicall",
106
- args: [encodedActions],
107
- }),
108
- };
109
- }
110
- BundlerAction.encodeBundle = encodeBundle;
111
- /**
112
- * Encodes a single supported Bundler3 action into one or more low-level
113
- * Bundler3 calls.
114
- *
115
- * @param chainId - Chain where the action will execute.
116
- * @param action - Bundler3 action to encode.
117
- * @returns Encoded Bundler3 calls.
118
- * @throws {BundlerErrors.MissingSignature} when a signature action is unsigned.
119
- * @throws {BundlerErrors.UnexpectedAction} when the action is unavailable on the chain.
120
- *
121
- * @example
122
- * ```ts
123
- * import { BundlerAction } from "@morpho-org/morpho-sdk/bundler";
124
- *
125
- * const recipient = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
126
- *
127
- * const calls = BundlerAction.encode(1, {
128
- * type: "wrapNative",
129
- * args: [1_000000000000000000n, recipient],
130
- * });
131
- * ```
132
- */
133
- function encode(chainId, action) {
134
- const { type, args } = action;
135
- switch (type) {
136
- case "nativeTransfer": {
137
- return BundlerAction.nativeTransfer(chainId, ...args);
138
- }
139
- case "erc20Transfer": {
140
- return BundlerAction.erc20Transfer(...args);
141
- }
142
- case "erc20TransferFrom": {
143
- return BundlerAction.erc20TransferFrom(chainId, ...args);
144
- }
145
- case "permit": {
146
- const [sender, asset, amount, deadline, signature, skipRevert] = args;
147
- if (signature == null)
148
- throw new BundlerErrors.MissingSignature();
149
- return BundlerAction.permit(chainId, sender, asset, amount, deadline, signature, skipRevert);
150
- }
151
- case "approve2": {
152
- const [sender, permitSingle, signature, skipRevert] = args;
153
- if (signature == null)
154
- throw new BundlerErrors.MissingSignature();
155
- return BundlerAction.approve2(chainId, sender, permitSingle, signature, skipRevert);
156
- }
157
- case "transferFrom2": {
158
- return BundlerAction.transferFrom2(chainId, ...args);
159
- }
160
- case "erc4626Deposit": {
161
- return BundlerAction.erc4626Deposit(chainId, ...args);
162
- }
163
- case "erc4626Redeem": {
164
- return BundlerAction.erc4626Redeem(chainId, ...args);
165
- }
166
- case "morphoSetAuthorizationWithSig": {
167
- const [authorization, signature, skipRevert] = args;
168
- if (signature == null)
169
- throw new BundlerErrors.MissingSignature();
170
- return BundlerAction.morphoSetAuthorizationWithSig(chainId, authorization, signature, skipRevert);
171
- }
172
- case "morphoSupplyCollateral": {
173
- const [market, amount, onBehalf, onMorphoSupplyCollateral, skipRevert] = args;
174
- return BundlerAction.morphoSupplyCollateral(chainId, market, amount, onBehalf, onMorphoSupplyCollateral.flatMap(BundlerAction.encode.bind(null, chainId)), skipRevert);
175
- }
176
- case "morphoSupply": {
177
- const [market, assets, shares, slippageAmount, onBehalf, onMorphoSupply, skipRevert,] = args;
178
- return BundlerAction.morphoSupply(chainId, market, assets, shares, slippageAmount, onBehalf, onMorphoSupply.flatMap(BundlerAction.encode.bind(null, chainId)), skipRevert);
179
- }
180
- case "morphoBorrow": {
181
- return BundlerAction.morphoBorrow(chainId, ...args);
182
- }
183
- case "morphoWithdraw": {
184
- return BundlerAction.morphoWithdraw(chainId, ...args);
185
- }
186
- case "morphoRepay": {
187
- const [market, assets, shares, maxSharePrice, onBehalf, onMorphoRepay, skipRevert,] = args;
188
- return BundlerAction.morphoRepay(chainId, market, assets, shares, maxSharePrice, onBehalf, onMorphoRepay.flatMap(BundlerAction.encode.bind(null, chainId)), skipRevert);
189
- }
190
- case "morphoWithdrawCollateral": {
191
- return BundlerAction.morphoWithdrawCollateral(chainId, ...args);
192
- }
193
- case "reallocateTo": {
194
- return BundlerAction.publicAllocatorReallocateTo(chainId, ...args);
195
- }
196
- case "vaultV2BluePublicAllocatorReallocate": {
197
- return BundlerAction.vaultV2BluePublicAllocatorReallocate(chainId, ...args);
198
- }
199
- case "vaultV2BluePublicAllocatorAllocateFromIdle": {
200
- return BundlerAction.vaultV2BluePublicAllocatorAllocateFromIdle(chainId, ...args);
201
- }
202
- case "wrapNative": {
203
- return BundlerAction.wrapNative(chainId, ...args);
204
- }
205
- }
206
- }
207
- BundlerAction.encode = encode;
208
- function encodeBundleAction({ chainId, action, valueState, }) {
209
- const { bundler3: { bundler3, generalAdapter1 }, } = getChainAddresses(chainId);
210
- let nextValueState = valueState;
211
- if (action.type === "nativeTransfer") {
212
- const [owner, recipient, amount] = action.args;
213
- // A transfer into Bundler3 emits no inner call; it pre-funds later
214
- // value-carrying calls in the same multicall or callback reentry.
215
- if (!isAddressEqual(owner, bundler3) &&
216
- !isAddressEqual(owner, generalAdapter1) &&
217
- isAddressEqual(recipient, bundler3)) {
218
- nextValueState = addBundlerPrefund(nextValueState, amount);
219
- }
220
- }
221
- const calls = BundlerAction.encode(chainId, action);
222
- for (const call of calls) {
223
- nextValueState = consumeCallValue(nextValueState, call);
224
- }
225
- if (action.type === "morphoSupplyCollateral") {
226
- const [, , , onMorphoSupplyCollateral] = action.args;
227
- for (const callbackAction of onMorphoSupplyCollateral) {
228
- const encodedCallback = encodeBundleAction({
229
- chainId,
230
- action: callbackAction,
231
- valueState: nextValueState,
232
- });
233
- nextValueState = encodedCallback.valueState;
234
- }
235
- }
236
- if (action.type === "morphoSupply") {
237
- const [, , , , , onMorphoSupply] = action.args;
238
- for (const callbackAction of onMorphoSupply) {
239
- const encodedCallback = encodeBundleAction({
240
- chainId,
241
- action: callbackAction,
242
- valueState: nextValueState,
243
- });
244
- nextValueState = encodedCallback.valueState;
245
- }
246
- }
247
- if (action.type === "morphoRepay") {
248
- const [, , , , , onMorphoRepay] = action.args;
249
- for (const callbackAction of onMorphoRepay) {
250
- const encodedCallback = encodeBundleAction({
251
- chainId,
252
- action: callbackAction,
253
- valueState: nextValueState,
254
- });
255
- nextValueState = encodedCallback.valueState;
256
- }
257
- }
258
- return { calls, valueState: nextValueState };
259
- }
260
- /**
261
- * Encodes a native-token transfer for Bundler3 execution.
262
- *
263
- * @remarks
264
- * Transfers to Bundler3 are treated as bundle pre-funding and emit no inner
265
- * call. Transfers whose `owner` is GeneralAdapter1 are encoded as
266
- * `GeneralAdapter1.nativeTransfer(recipient, amount)` and always use
267
- * `skipRevert: false`; the caller-supplied `skipRevert` argument applies
268
- * only to direct native transfers from other owners.
269
- *
270
- * @param chainId - Chain where the action will execute.
271
- * @param owner - Current native-token owner in the bundle.
272
- * @param recipient - Native-token recipient.
273
- * @param amount - Native-token amount in wei.
274
- * @param skipRevert - Whether Bundler3 should tolerate a revert.
275
- * @returns Encoded Bundler3 calls.
276
- *
277
- * @example
278
- * ```ts
279
- * import { BundlerAction } from "@morpho-org/morpho-sdk/bundler";
280
- *
281
- * const sender = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
282
- * const recipient = "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359";
283
- *
284
- * const calls = BundlerAction.nativeTransfer(
285
- * 1,
286
- * sender,
287
- * recipient,
288
- * 1_000000000000000000n,
289
- * );
290
- * ```
291
- */
292
- // biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
293
- function nativeTransfer(chainId, owner, recipient, amount, skipRevert = false) {
294
- const { bundler3: { bundler3, generalAdapter1 }, } = getChainAddresses(chainId);
295
- if (isAddressEqual(recipient, bundler3))
296
- return [];
297
- if (isAddressEqual(owner, generalAdapter1)) {
298
- return [
299
- {
300
- to: generalAdapter1,
301
- data: encodeFunctionData({
302
- abi: coreAdapterAbi,
303
- functionName: "nativeTransfer",
304
- args: [recipient, amount],
305
- }),
306
- value: 0n,
307
- skipRevert: false,
308
- callbackHash: zeroHash,
309
- },
310
- ];
311
- }
312
- return [
313
- {
314
- to: recipient,
315
- data: "0x",
316
- value: amount,
317
- skipRevert,
318
- callbackHash: zeroHash,
319
- },
320
- ];
321
- }
322
- BundlerAction.nativeTransfer = nativeTransfer;
323
- /**
324
- * Encodes an ERC20 transfer from the given adapter.
325
- *
326
- * @param asset - ERC20 token to transfer.
327
- * @param recipient - Recipient of the transferred tokens.
328
- * @param amount - Token amount to transfer.
329
- * @param adapter - Adapter that currently holds the tokens.
330
- * @param skipRevert - Whether Bundler3 should tolerate a revert.
331
- * @returns Encoded Bundler3 calls.
332
- *
333
- * @example
334
- * ```ts
335
- * import { getChainAddresses } from "@morpho-org/morpho-sdk/addresses";
336
- * import { BundlerAction } from "@morpho-org/morpho-sdk/bundler";
337
- *
338
- * const adapter = getChainAddresses(1).bundler3.generalAdapter1;
339
- * const asset = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
340
- * const recipient = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
341
- *
342
- * const calls = BundlerAction.erc20Transfer(
343
- * asset,
344
- * recipient,
345
- * 100n,
346
- * adapter,
347
- * );
348
- * ```
349
- */
350
- // biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
351
- function erc20Transfer(asset, recipient, amount, adapter, skipRevert = false) {
352
- return [
353
- {
354
- to: adapter,
355
- data: encodeFunctionData({
356
- abi: coreAdapterAbi,
357
- functionName: "erc20Transfer",
358
- args: [asset, recipient, amount],
359
- }),
360
- value: 0n,
361
- skipRevert,
362
- callbackHash: zeroHash,
363
- },
364
- ];
365
- }
366
- BundlerAction.erc20Transfer = erc20Transfer;
367
- /**
368
- * Encodes a GeneralAdapter1 ERC20 `transferFrom`.
369
- *
370
- * @param chainId - Chain where the action will execute.
371
- * @param asset - ERC20 token to transfer.
372
- * @param amount - Token amount to transfer.
373
- * @param recipient - Recipient of the transferred tokens.
374
- * @param skipRevert - Whether Bundler3 should tolerate a revert.
375
- * @returns Encoded Bundler3 calls.
376
- *
377
- * @example
378
- * ```ts
379
- * import { BundlerAction } from "@morpho-org/morpho-sdk/bundler";
380
- *
381
- * const asset = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
382
- * const recipient = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
383
- *
384
- * const calls = BundlerAction.erc20TransferFrom(
385
- * 1,
386
- * asset,
387
- * 100n,
388
- * recipient,
389
- * );
390
- * ```
391
- */
392
- // biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
393
- function erc20TransferFrom(chainId, asset, amount, recipient, skipRevert = false) {
394
- const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
395
- return [
396
- {
397
- to: generalAdapter1,
398
- data: encodeFunctionData({
399
- abi: generalAdapter1Abi,
400
- functionName: "erc20TransferFrom",
401
- args: [asset, recipient, amount],
402
- }),
403
- value: 0n,
404
- skipRevert,
405
- callbackHash: zeroHash,
406
- },
407
- ];
408
- }
409
- BundlerAction.erc20TransferFrom = erc20TransferFrom;
410
- /**
411
- * Encodes an ERC20 permit for GeneralAdapter1 spending.
412
- *
413
- * @param chainId - Chain where the action will execute.
414
- * @param owner - Token owner signing the permit.
415
- * @param asset - ERC20 token being permitted.
416
- * @param amount - Allowance amount.
417
- * @param deadline - Permit deadline timestamp.
418
- * @param signature - Owner signature.
419
- * @param skipRevert - Whether Bundler3 should tolerate a revert.
420
- * @returns Encoded Bundler3 calls.
421
- *
422
- * @example
423
- * ```ts
424
- * import { BundlerAction } from "@morpho-org/morpho-sdk/bundler";
425
- *
426
- * const owner = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
427
- * const asset = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
428
- * const signature =
429
- * "0x111111111111111111111111111111111111111111111111111111111111111122222222222222222222222222222222222222222222222222222222222222221b";
430
- *
431
- * const calls = BundlerAction.permit(
432
- * 1,
433
- * owner,
434
- * asset,
435
- * 100n,
436
- * 1_900_000_000n,
437
- * signature,
438
- * );
439
- * ```
440
- */
441
- // biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
442
- function permit(chainId, owner, asset, amount, deadline, signature, skipRevert = true) {
443
- const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
444
- const { r, s, yParity } = parseSignature(toSignatureHex(signature));
445
- return [
446
- {
447
- to: asset,
448
- data: encodeFunctionData({
449
- abi: erc2612Abi,
450
- functionName: "permit",
451
- args: [owner, generalAdapter1, amount, deadline, yParity + 27, r, s],
452
- }),
453
- value: 0n,
454
- skipRevert,
455
- callbackHash: zeroHash,
456
- },
457
- ];
458
- }
459
- BundlerAction.permit = permit;
460
- /**
461
- * Encodes a Permit2 approval for GeneralAdapter1 spending.
462
- *
463
- * @param chainId - Chain where the action will execute.
464
- * @param owner - Token owner signing the Permit2 payload.
465
- * @param permitSingle - Permit2 allowance payload.
466
- * @param signature - Owner signature.
467
- * @param skipRevert - Whether Bundler3 should tolerate a revert.
468
- * @returns Encoded Bundler3 calls.
469
- * @throws {BundlerErrors.UnexpectedAction} when Permit2 is unavailable on the chain.
470
- *
471
- * @example
472
- * ```ts
473
- * import { BundlerAction } from "@morpho-org/morpho-sdk/bundler";
474
- *
475
- * const owner = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
476
- * const token = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
477
- * const signature =
478
- * "0x111111111111111111111111111111111111111111111111111111111111111122222222222222222222222222222222222222222222222222222222222222221b";
479
- *
480
- * const calls = BundlerAction.approve2(
481
- * 1,
482
- * owner,
483
- * {
484
- * details: {
485
- * token,
486
- * amount: 100n,
487
- * expiration: 1_900_000_000,
488
- * nonce: 0,
489
- * },
490
- * sigDeadline: 1_900_000_000n,
491
- * },
492
- * signature,
493
- * );
494
- * ```
495
- */
496
- // biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
497
- function approve2(chainId, owner, permitSingle, signature, skipRevert = true) {
498
- const { permit2, bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
499
- if (permit2 == null) {
500
- throw new BundlerErrors.UnexpectedAction("approve2", chainId);
501
- }
502
- return [
503
- {
504
- to: permit2,
505
- data: encodeFunctionData({
506
- abi: permit2Abi,
507
- functionName: "permit",
508
- args: [
509
- owner,
510
- {
511
- ...permitSingle,
512
- spender: generalAdapter1,
513
- },
514
- toSignatureHex(signature),
515
- ],
516
- }),
517
- value: 0n,
518
- skipRevert,
519
- callbackHash: zeroHash,
520
- },
521
- ];
522
- }
523
- BundlerAction.approve2 = approve2;
524
- /**
525
- * Encodes a GeneralAdapter1 Permit2 transfer.
526
- *
527
- * @param chainId - Chain where the action will execute.
528
- * @param asset - ERC20 token to transfer through Permit2.
529
- * @param amount - Token amount to transfer.
530
- * @param recipient - Recipient of the transferred tokens.
531
- * @param skipRevert - Whether Bundler3 should tolerate a revert.
532
- * @returns Encoded Bundler3 calls.
533
- *
534
- * @example
535
- * ```ts
536
- * import { BundlerAction } from "@morpho-org/morpho-sdk/bundler";
537
- *
538
- * const asset = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
539
- * const recipient = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
540
- *
541
- * const calls = BundlerAction.transferFrom2(
542
- * 1,
543
- * asset,
544
- * 100n,
545
- * recipient,
546
- * );
547
- * ```
548
- */
549
- // biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
550
- function transferFrom2(chainId, asset, amount, recipient, skipRevert = false) {
551
- const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
552
- return [
553
- {
554
- to: generalAdapter1,
555
- data: encodeFunctionData({
556
- abi: generalAdapter1Abi,
557
- functionName: "permit2TransferFrom",
558
- args: [asset, recipient, amount],
559
- }),
560
- value: 0n,
561
- skipRevert,
562
- callbackHash: zeroHash,
563
- },
564
- ];
565
- }
566
- BundlerAction.transferFrom2 = transferFrom2;
567
- /**
568
- * Encodes a GeneralAdapter1 ERC4626 deposit.
569
- *
570
- * @param chainId - Chain where the action will execute.
571
- * @param erc4626 - ERC4626 vault address.
572
- * @param assets - Asset amount to deposit.
573
- * @param maxSharePrice - Maximum accepted share price in RAY.
574
- * @param receiver - Recipient of minted vault shares.
575
- * @param skipRevert - Whether Bundler3 should tolerate a revert.
576
- * @returns Encoded Bundler3 calls.
577
- *
578
- * @example
579
- * ```ts
580
- * import { BundlerAction } from "@morpho-org/morpho-sdk/bundler";
581
- *
582
- * const vault = "0x186514400e52270cef3D80e1c6F8d10A75d47344";
583
- * const recipient = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
584
- *
585
- * const calls = BundlerAction.erc4626Deposit(
586
- * 1,
587
- * vault,
588
- * 100n,
589
- * 1_000000000000000000000000000n,
590
- * recipient,
591
- * );
592
- * ```
593
- */
594
- // biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
595
- function erc4626Deposit(chainId, erc4626, assets, maxSharePrice, receiver, skipRevert = false) {
596
- const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
597
- return [
598
- {
599
- to: generalAdapter1,
600
- data: encodeFunctionData({
601
- abi: generalAdapter1Abi,
602
- functionName: "erc4626Deposit",
603
- args: [erc4626, assets, maxSharePrice, receiver],
604
- }),
605
- value: 0n,
606
- skipRevert,
607
- callbackHash: zeroHash,
608
- },
609
- ];
610
- }
611
- BundlerAction.erc4626Deposit = erc4626Deposit;
612
- /**
613
- * Encodes a GeneralAdapter1 ERC4626 redeem.
614
- *
615
- * @param chainId - Chain where the action will execute.
616
- * @param erc4626 - ERC4626 vault address.
617
- * @param shares - Share amount to redeem.
618
- * @param minSharePrice - Minimum accepted share price in RAY.
619
- * @param receiver - Recipient of redeemed assets.
620
- * @param owner - Owner of the shares being redeemed.
621
- * @param skipRevert - Whether Bundler3 should tolerate a revert.
622
- * @returns Encoded Bundler3 calls.
623
- *
624
- * @example
625
- * ```ts
626
- * import { BundlerAction } from "@morpho-org/morpho-sdk/bundler";
627
- *
628
- * const vault = "0x186514400e52270cef3D80e1c6F8d10A75d47344";
629
- * const recipient = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
630
- * const owner = recipient;
631
- *
632
- * const calls = BundlerAction.erc4626Redeem(
633
- * 1,
634
- * vault,
635
- * 100n,
636
- * 900000000000000000000000000n,
637
- * recipient,
638
- * owner,
639
- * );
640
- * ```
641
- */
642
- // biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
643
- function erc4626Redeem(chainId, erc4626, shares, minSharePrice, receiver, owner, skipRevert = false) {
644
- const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
645
- return [
646
- {
647
- to: generalAdapter1,
648
- data: encodeFunctionData({
649
- abi: generalAdapter1Abi,
650
- functionName: "erc4626Redeem",
651
- args: [erc4626, shares, minSharePrice, receiver, owner],
652
- }),
653
- value: 0n,
654
- skipRevert,
655
- callbackHash: zeroHash,
656
- },
657
- ];
658
- }
659
- BundlerAction.erc4626Redeem = erc4626Redeem;
660
- /**
661
- * Encodes a Morpho Blue `setAuthorizationWithSig` call that submits a signed authorization.
662
- *
663
- * Lets a bundle grant `authorization.authorized` (GeneralAdapter1) operator rights on Morpho
664
- * on behalf of `authorization.authorizer` without a separate `setAuthorization` transaction.
665
- *
666
- * @param chainId - Chain where the action will execute.
667
- * @param authorization - The Morpho authorization payload covered by the signature.
668
- * @param signature - The owner's EIP-712 signature over the authorization typed data, as a hex
669
- * string or a viem `Signature` object.
670
- * @param skipRevert - Whether Bundler3 should tolerate a revert. Defaults to `true`, matching
671
- * the convention for already-authorized accounts (the call reverts harmlessly).
672
- * @returns Encoded Bundler3 calls.
673
- * @throws {BundlerErrors.UnexpectedSignature} when `authorization.authorized` is not the chain's
674
- * `GeneralAdapter1` — the only operator this bundled path may grant rights to.
675
- *
676
- * @example
677
- * ```ts
678
- * import { getChainAddresses } from "@morpho-org/morpho-sdk/addresses";
679
- * import { BundlerAction } from "@morpho-org/morpho-sdk/bundler";
680
- *
681
- * const { generalAdapter1 } = getChainAddresses(1).bundler3;
682
- * const owner = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
683
- * const signature =
684
- * "0x111111111111111111111111111111111111111111111111111111111111111122222222222222222222222222222222222222222222222222222222222222221b";
685
- *
686
- * const calls = BundlerAction.morphoSetAuthorizationWithSig(
687
- * 1,
688
- * {
689
- * authorizer: owner,
690
- * authorized: generalAdapter1,
691
- * isAuthorized: true,
692
- * nonce: 0n,
693
- * deadline: 1_900_000_000n,
694
- * },
695
- * signature,
696
- * );
697
- * ```
698
- */
699
- // biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
700
- function morphoSetAuthorizationWithSig(chainId, authorization, signature, skipRevert = true) {
701
- const { morpho, bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
702
- const { r, s, yParity } = parseSignature(toSignatureHex(signature));
703
- if (!isAddressEqual(authorization.authorized, generalAdapter1)) {
704
- throw new BundlerErrors.UnexpectedSignature(authorization.authorized);
705
- }
706
- return [
707
- {
708
- to: morpho,
709
- data: encodeFunctionData({
710
- abi: blueAbi,
711
- functionName: "setAuthorizationWithSig",
712
- args: [authorization, { v: yParity + 27, r, s }],
713
- }),
714
- value: 0n,
715
- skipRevert,
716
- callbackHash: zeroHash,
717
- },
718
- ];
719
- }
720
- BundlerAction.morphoSetAuthorizationWithSig = morphoSetAuthorizationWithSig;
721
- /**
722
- * Encodes a GeneralAdapter1 Morpho Blue supply-collateral call.
723
- *
724
- * @param chainId - Chain where the action will execute.
725
- * @param market - Morpho Blue market parameters.
726
- * @param assets - Collateral asset amount to supply.
727
- * @param onBehalf - Account receiving the collateral position.
728
- * @param callbackCalls - Calls executed in Morpho's callback.
729
- * @param skipRevert - Whether Bundler3 should tolerate a revert.
730
- * @returns Encoded Bundler3 calls.
731
- *
732
- * @example
733
- * ```ts
734
- * import { BundlerAction } from "@morpho-org/morpho-sdk/bundler";
735
- *
736
- * const loanToken = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
737
- * const collateralToken = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
738
- * const oracle = "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419";
739
- * const irm = "0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC";
740
- * const onBehalf = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
741
- * const marketParams = {
742
- * loanToken,
743
- * collateralToken,
744
- * oracle,
745
- * irm,
746
- * lltv: 860_000000000000000000n,
747
- * };
748
- *
749
- * const calls = BundlerAction.morphoSupplyCollateral(
750
- * 1,
751
- * marketParams,
752
- * 100n,
753
- * onBehalf,
754
- * [],
755
- * );
756
- * ```
757
- */
758
- // biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
759
- function morphoSupplyCollateral(chainId, market, assets, onBehalf, callbackCalls, skipRevert = false) {
760
- const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
761
- const { callbackHash, reenterData } = encodeCallbackCalls(callbackCalls);
762
- return [
763
- {
764
- to: generalAdapter1,
765
- data: encodeFunctionData({
766
- abi: generalAdapter1Abi,
767
- functionName: "morphoSupplyCollateral",
768
- args: [market, assets, onBehalf, reenterData],
769
- }),
770
- value: 0n,
771
- skipRevert,
772
- callbackHash,
773
- },
774
- ];
775
- }
776
- BundlerAction.morphoSupplyCollateral = morphoSupplyCollateral;
777
- /**
778
- * Encodes a GeneralAdapter1 Morpho Blue supply call.
779
- *
780
- * @param chainId - Chain where the action will execute.
781
- * @param market - Morpho Blue market parameters.
782
- * @param assets - Supply asset amount.
783
- * @param shares - Supply share amount.
784
- * @param slippageAmount - Slippage guard amount (max share price in RAY for share-price slippage).
785
- * @param onBehalf - Account receiving the supply position.
786
- * @param callbackCalls - Calls executed in Morpho's callback.
787
- * @param skipRevert - Whether Bundler3 should tolerate a revert.
788
- * @returns Encoded Bundler3 calls.
789
- *
790
- * @example
791
- * ```ts
792
- * import { BundlerAction } from "@morpho-org/morpho-sdk/bundler";
793
- *
794
- * const loanToken = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
795
- * const collateralToken = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
796
- * const oracle = "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419";
797
- * const irm = "0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC";
798
- * const onBehalf = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
799
- * const marketParams = {
800
- * loanToken,
801
- * collateralToken,
802
- * oracle,
803
- * irm,
804
- * lltv: 860_000000000000000000n,
805
- * };
806
- *
807
- * const calls = BundlerAction.morphoSupply(
808
- * 1,
809
- * marketParams,
810
- * 100n,
811
- * 0n,
812
- * 1_010000000000000000000000000n,
813
- * onBehalf,
814
- * [],
815
- * );
816
- * ```
817
- */
818
- // biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
819
- function morphoSupply(chainId, market, assets, shares, slippageAmount, onBehalf, callbackCalls, skipRevert = false) {
820
- const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
821
- const { callbackHash, reenterData } = encodeCallbackCalls(callbackCalls);
822
- return [
823
- {
824
- to: generalAdapter1,
825
- data: encodeFunctionData({
826
- abi: generalAdapter1Abi,
827
- functionName: "morphoSupply",
828
- args: [market, assets, shares, slippageAmount, onBehalf, reenterData],
829
- }),
830
- value: 0n,
831
- skipRevert,
832
- callbackHash,
833
- },
834
- ];
835
- }
836
- BundlerAction.morphoSupply = morphoSupply;
837
- /**
838
- * Encodes a GeneralAdapter1 Morpho Blue borrow call.
839
- *
840
- * @param chainId - Chain where the action will execute.
841
- * @param market - Morpho Blue market parameters.
842
- * @param assets - Borrow asset amount.
843
- * @param shares - Borrow share amount.
844
- * @param minSharePrice - Minimum amount of borrowed assets per borrow share minted, scaled by 1e27.
845
- * @param receiver - Recipient of borrowed assets.
846
- * @param skipRevert - Whether Bundler3 should tolerate a revert.
847
- * @returns Encoded Bundler3 calls.
848
- *
849
- * @example
850
- * ```ts
851
- * import { BundlerAction } from "@morpho-org/morpho-sdk/bundler";
852
- *
853
- * const loanToken = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
854
- * const collateralToken = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
855
- * const oracle = "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419";
856
- * const irm = "0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC";
857
- * const receiver = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
858
- * const marketParams = {
859
- * loanToken,
860
- * collateralToken,
861
- * oracle,
862
- * irm,
863
- * lltv: 860_000000000000000000n,
864
- * };
865
- *
866
- * const calls = BundlerAction.morphoBorrow(
867
- * 1,
868
- * marketParams,
869
- * 100n,
870
- * 0n,
871
- * 1_000000000000000000000000000n,
872
- * receiver,
873
- * );
874
- * ```
875
- */
876
- // biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
877
- function morphoBorrow(chainId, market, assets, shares, minSharePrice, receiver, skipRevert = false) {
878
- const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
879
- return [
880
- {
881
- to: generalAdapter1,
882
- data: encodeFunctionData({
883
- abi: generalAdapter1Abi,
884
- functionName: "morphoBorrow",
885
- args: [market, assets, shares, minSharePrice, receiver],
886
- }),
887
- value: 0n,
888
- skipRevert,
889
- callbackHash: zeroHash,
890
- },
891
- ];
892
- }
893
- BundlerAction.morphoBorrow = morphoBorrow;
894
- /**
895
- * Encodes a GeneralAdapter1 Morpho Blue repay call.
896
- *
897
- * @param chainId - Chain where the action will execute.
898
- * @param market - Morpho Blue market parameters.
899
- * @param assets - Repay asset amount.
900
- * @param shares - Repay share amount.
901
- * @param maxSharePrice - Maximum amount of repaid assets per borrow share redeemed, scaled by 1e27.
902
- * @param onBehalf - Account whose borrow is repaid.
903
- * @param callbackCalls - Calls executed in Morpho's callback.
904
- * @param skipRevert - Whether Bundler3 should tolerate a revert.
905
- * @returns Encoded Bundler3 calls.
906
- *
907
- * @example
908
- * ```ts
909
- * import { BundlerAction } from "@morpho-org/morpho-sdk/bundler";
910
- *
911
- * const loanToken = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
912
- * const collateralToken = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
913
- * const oracle = "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419";
914
- * const irm = "0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC";
915
- * const onBehalf = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
916
- * const marketParams = {
917
- * loanToken,
918
- * collateralToken,
919
- * oracle,
920
- * irm,
921
- * lltv: 860_000000000000000000n,
922
- * };
923
- *
924
- * const calls = BundlerAction.morphoRepay(
925
- * 1,
926
- * marketParams,
927
- * 100n,
928
- * 0n,
929
- * 1_000000000000000000000000000n,
930
- * onBehalf,
931
- * [],
932
- * );
933
- * ```
934
- */
935
- // biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
936
- function morphoRepay(chainId, market, assets, shares, maxSharePrice, onBehalf, callbackCalls, skipRevert = false) {
937
- const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
938
- const { callbackHash, reenterData } = encodeCallbackCalls(callbackCalls);
939
- return [
940
- {
941
- to: generalAdapter1,
942
- data: encodeFunctionData({
943
- abi: generalAdapter1Abi,
944
- functionName: "morphoRepay",
945
- args: [market, assets, shares, maxSharePrice, onBehalf, reenterData],
946
- }),
947
- value: 0n,
948
- skipRevert,
949
- callbackHash,
950
- },
951
- ];
952
- }
953
- BundlerAction.morphoRepay = morphoRepay;
954
- /**
955
- * Encodes a GeneralAdapter1 Morpho Blue withdraw call.
956
- *
957
- * @param chainId - Chain where the action will execute.
958
- * @param market - Morpho Blue market parameters.
959
- * @param assets - Withdraw asset amount.
960
- * @param shares - Withdraw share amount.
961
- * @param slippageAmount - Slippage guard amount (min share price in RAY for share-price slippage).
962
- * @param receiver - Recipient of withdrawn assets.
963
- * @param skipRevert - Whether Bundler3 should tolerate a revert.
964
- * @returns Encoded Bundler3 calls.
965
- *
966
- * @example
967
- * ```ts
968
- * import { BundlerAction } from "@morpho-org/morpho-sdk/bundler";
969
- *
970
- * const loanToken = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
971
- * const collateralToken = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
972
- * const oracle = "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419";
973
- * const irm = "0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC";
974
- * const receiver = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
975
- * const marketParams = {
976
- * loanToken,
977
- * collateralToken,
978
- * oracle,
979
- * irm,
980
- * lltv: 860_000000000000000000n,
981
- * };
982
- *
983
- * const calls = BundlerAction.morphoWithdraw(
984
- * 1,
985
- * marketParams,
986
- * 100n,
987
- * 0n,
988
- * 990000000000000000000000000n,
989
- * receiver,
990
- * );
991
- * ```
992
- */
993
- // biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
994
- function morphoWithdraw(chainId, market, assets, shares, slippageAmount, receiver, skipRevert = false) {
995
- const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
996
- return [
997
- {
998
- to: generalAdapter1,
999
- data: encodeFunctionData({
1000
- abi: generalAdapter1Abi,
1001
- functionName: "morphoWithdraw",
1002
- args: [market, assets, shares, slippageAmount, receiver],
1003
- }),
1004
- value: 0n,
1005
- skipRevert,
1006
- callbackHash: zeroHash,
1007
- },
1008
- ];
1009
- }
1010
- BundlerAction.morphoWithdraw = morphoWithdraw;
1011
- /**
1012
- * Encodes a GeneralAdapter1 Morpho Blue withdraw-collateral call.
1013
- *
1014
- * @param chainId - Chain where the action will execute.
1015
- * @param market - Morpho Blue market parameters.
1016
- * @param assets - Collateral asset amount to withdraw.
1017
- * @param receiver - Recipient of withdrawn collateral.
1018
- * @param skipRevert - Whether Bundler3 should tolerate a revert.
1019
- * @returns Encoded Bundler3 calls.
1020
- *
1021
- * @example
1022
- * ```ts
1023
- * import { BundlerAction } from "@morpho-org/morpho-sdk/bundler";
1024
- *
1025
- * const loanToken = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
1026
- * const collateralToken = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
1027
- * const oracle = "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419";
1028
- * const irm = "0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC";
1029
- * const receiver = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
1030
- * const marketParams = {
1031
- * loanToken,
1032
- * collateralToken,
1033
- * oracle,
1034
- * irm,
1035
- * lltv: 860_000000000000000000n,
1036
- * };
1037
- *
1038
- * const calls = BundlerAction.morphoWithdrawCollateral(
1039
- * 1,
1040
- * marketParams,
1041
- * 100n,
1042
- * receiver,
1043
- * );
1044
- * ```
1045
- */
1046
- // biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
1047
- function morphoWithdrawCollateral(chainId, market, assets, receiver, skipRevert = false) {
1048
- const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
1049
- return [
1050
- {
1051
- to: generalAdapter1,
1052
- data: encodeFunctionData({
1053
- abi: generalAdapter1Abi,
1054
- functionName: "morphoWithdrawCollateral",
1055
- args: [market, assets, receiver],
1056
- }),
1057
- value: 0n,
1058
- skipRevert,
1059
- callbackHash: zeroHash,
1060
- },
1061
- ];
1062
- }
1063
- BundlerAction.morphoWithdrawCollateral = morphoWithdrawCollateral;
1064
- /**
1065
- * Encodes a PublicAllocator reallocation call.
1066
- *
1067
- * @param chainId - Chain where the action will execute.
1068
- * @param vault - Vault to reallocate.
1069
- * @param fee - Public allocator fee.
1070
- * @param withdrawals - Market withdrawals performed before supply.
1071
- * @param supplyMarketParams - Target supply market parameters.
1072
- * @param skipRevert - Whether Bundler3 should tolerate a revert.
1073
- * @returns Encoded Bundler3 calls.
1074
- * @throws {BundlerErrors.UnexpectedAction} when the PublicAllocator is unavailable on the chain.
1075
- * @deprecated Vault V1 PublicAllocator composition will be removed in the next major. Use
1076
- * `vaultV2BluePublicAllocatorReallocate` or `vaultV2BluePublicAllocatorAllocateFromIdle`.
1077
- *
1078
- * @example
1079
- * ```ts
1080
- * import { BundlerAction } from "@morpho-org/morpho-sdk/bundler";
1081
- *
1082
- * const loanToken = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
1083
- * const collateralToken = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
1084
- * const oracle = "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419";
1085
- * const irm = "0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC";
1086
- * const vault = "0x186514400e52270cef3D80e1c6F8d10A75d47344";
1087
- * const marketParams = {
1088
- * loanToken,
1089
- * collateralToken,
1090
- * oracle,
1091
- * irm,
1092
- * lltv: 860_000000000000000000n,
1093
- * };
1094
- *
1095
- * const calls = BundlerAction.publicAllocatorReallocateTo(
1096
- * 1,
1097
- * vault,
1098
- * 0n,
1099
- * [{ marketParams, amount: 100n }],
1100
- * marketParams,
1101
- * );
1102
- * ```
1103
- */
1104
- // biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
1105
- function publicAllocatorReallocateTo(chainId, vault, fee, withdrawals, supplyMarketParams, skipRevert = false) {
1106
- const { vaultV1PublicAllocator } = getChainAddresses(chainId);
1107
- if (vaultV1PublicAllocator == null) {
1108
- throw new BundlerErrors.UnexpectedAction("reallocateTo", chainId);
1109
- }
1110
- return [
1111
- {
1112
- to: vaultV1PublicAllocator,
1113
- data: encodeFunctionData({
1114
- abi: vaultV1PublicAllocatorAbi,
1115
- functionName: "reallocateTo",
1116
- args: [vault, withdrawals, supplyMarketParams],
1117
- }),
1118
- value: fee,
1119
- skipRevert,
1120
- callbackHash: zeroHash,
1121
- },
1122
- ];
1123
- }
1124
- BundlerAction.publicAllocatorReallocateTo = publicAllocatorReallocateTo;
1125
- /**
1126
- * Encodes a Vault V2 Blue Public Allocator market-to-market reallocation.
1127
- *
1128
- * @remarks Bundler3 must already hold the computed penalty assets. Explicit low-level
1129
- * composition must add the corresponding GeneralAdapter1 transfer.
1130
- *
1131
- * @param chainId - Chain whose canonical Blue Public Allocator is called.
1132
- * @param vault - Vault whose liquidity is reallocated.
1133
- * @param deallocateAdapter - Vault V2 adapter supplying the source market.
1134
- * @param deallocateMarket - Source Morpho Blue market parameters.
1135
- * @param allocateAdapter - Vault V2 adapter supplying the target market.
1136
- * @param allocateMarket - Target Morpho Blue market parameters.
1137
- * @param assets - Assets to reallocate, bounded by `uint128` by the high-level action.
1138
- * @param penalty - Vault-configured proportional penalty, scaled by WAD.
1139
- * @param skipRevert - Whether Bundler3 should tolerate a revert.
1140
- * @returns A zero reset and exact approval when needed, then the allocator call.
1141
- * @throws {BundlerErrors.UnexpectedAction} when the chain has no Blue Public Allocator deployment.
1142
- * @throws {BundlerErrors.SkippableAllocatorPenalty} when `skipRevert` is true and a token approval is required.
1143
- * @example
1144
- * ```ts
1145
- * import { ChainId } from "@morpho-org/morpho-sdk/constants";
1146
- * import type { BlueInputMarketParams } from "@morpho-org/morpho-sdk/types";
1147
- * import {
1148
- * BundlerAction,
1149
- * type BundlerCall,
1150
- * } from "@morpho-org/morpho-sdk/bundler";
1151
- * import type { Address } from "viem";
1152
- *
1153
- * const keyrockUsdcVault =
1154
- * "0x04422053aDDbc9bB2759b248B574e3FCA76Bc145" satisfies Address;
1155
- * const sourceAdapterFixture =
1156
- * "0x70997970C51812dc3A010C7d01b50e0d17dc79C8" satisfies Address;
1157
- * const targetAdapterFixture =
1158
- * "0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC" satisfies Address;
1159
- * const usdc = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48" satisfies Address;
1160
- * const weth = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" satisfies Address;
1161
- * const wbtc = "0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599" satisfies Address;
1162
- * const ethUsdOracle = "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419" satisfies Address;
1163
- * const adaptiveCurveIrm = "0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC" satisfies Address;
1164
- * const sourceMarket = {
1165
- * loanToken: usdc,
1166
- * collateralToken: weth,
1167
- * oracle: ethUsdOracle,
1168
- * irm: adaptiveCurveIrm,
1169
- * lltv: 860_000_000_000_000_000n,
1170
- * } satisfies BlueInputMarketParams;
1171
- * const targetMarket = {
1172
- * ...sourceMarket,
1173
- * collateralToken: wbtc,
1174
- * } satisfies BlueInputMarketParams;
1175
- *
1176
- * const calls: BundlerCall[] = BundlerAction.vaultV2BluePublicAllocatorReallocate(
1177
- * ChainId.EthMainnet,
1178
- * keyrockUsdcVault,
1179
- * sourceAdapterFixture,
1180
- * sourceMarket,
1181
- * targetAdapterFixture,
1182
- * targetMarket,
1183
- * 1_000_000n,
1184
- * 1_000_000_000_000_000n,
1185
- * );
1186
- * // Bundler3 resets and approves 1_000 USDC units, then calls `reallocate` with zero native value.
1187
- * ```
1188
- */
1189
- // biome-ignore lint/complexity/useMaxParams: mirrors the protocol call
1190
- function vaultV2BluePublicAllocatorReallocate(chainId, vault, deallocateAdapter, deallocateMarket, allocateAdapter, allocateMarket, assets, penalty, skipRevert = false) {
1191
- const { vaultV2BluePublicAllocator: allocator } = getChainAddresses(chainId);
1192
- if (allocator == null) {
1193
- throw new BundlerErrors.UnexpectedAction("vaultV2BluePublicAllocatorReallocate", chainId);
1194
- }
1195
- const calls = [];
1196
- const penaltyAssets = VaultV2BluePublicAllocatorConfigUtils.getPenaltyAssets({ penalty }, assets);
1197
- if (skipRevert && penaltyAssets > 0n) {
1198
- throw new BundlerErrors.SkippableAllocatorPenalty(penaltyAssets);
1199
- }
1200
- if (penaltyAssets > 0n) {
1201
- for (const amount of [0n, penaltyAssets])
1202
- calls.push({
1203
- to: allocateMarket.loanToken,
1204
- data: encodeFunctionData({
1205
- abi: erc20Abi,
1206
- functionName: "approve",
1207
- args: [allocator, amount],
1208
- }),
1209
- value: 0n,
1210
- skipRevert,
1211
- callbackHash: zeroHash,
1212
- });
1213
- }
1214
- calls.push({
1215
- to: allocator,
1216
- data: encodeFunctionData({
1217
- abi: vaultV2BluePublicAllocatorAbi,
1218
- functionName: "reallocate",
1219
- args: [
1220
- vault,
1221
- deallocateAdapter,
1222
- deallocateMarket,
1223
- allocateAdapter,
1224
- allocateMarket,
1225
- assets,
1226
- penalty,
1227
- ],
1228
- }),
1229
- value: 0n,
1230
- skipRevert,
1231
- callbackHash: zeroHash,
1232
- });
1233
- return calls;
1234
- }
1235
- BundlerAction.vaultV2BluePublicAllocatorReallocate = vaultV2BluePublicAllocatorReallocate;
1236
- /**
1237
- * Encodes a Vault V2 Blue Public Allocator allocation from vault idle liquidity.
1238
- *
1239
- * @remarks Bundler3 must already hold the computed penalty assets. Explicit low-level
1240
- * composition must add the corresponding GeneralAdapter1 transfer.
1241
- *
1242
- * @param chainId - Chain whose canonical Blue Public Allocator is called.
1243
- * @param vault - Vault whose idle liquidity is allocated.
1244
- * @param adapter - Vault V2 adapter supplying the target market.
1245
- * @param market - Target Morpho Blue market parameters.
1246
- * @param assets - Assets to allocate, bounded by `uint128` by the high-level action.
1247
- * @param penalty - Vault-configured proportional penalty, scaled by WAD.
1248
- * @param skipRevert - Whether Bundler3 should tolerate a revert.
1249
- * @returns A zero reset and exact approval when needed, then the allocator call.
1250
- * @throws {BundlerErrors.UnexpectedAction} when the chain has no Blue Public Allocator deployment.
1251
- * @throws {BundlerErrors.SkippableAllocatorPenalty} when `skipRevert` is true and a token approval is required.
1252
- * @example
1253
- * ```ts
1254
- * import { ChainId } from "@morpho-org/morpho-sdk/constants";
1255
- * import type { BlueInputMarketParams } from "@morpho-org/morpho-sdk/types";
1256
- * import {
1257
- * BundlerAction,
1258
- * type BundlerCall,
1259
- * } from "@morpho-org/morpho-sdk/bundler";
1260
- * import type { Address } from "viem";
1261
- *
1262
- * const keyrockUsdcVault =
1263
- * "0x04422053aDDbc9bB2759b248B574e3FCA76Bc145" satisfies Address;
1264
- * const targetAdapterFixture =
1265
- * "0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC" satisfies Address;
1266
- * const usdc = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48" satisfies Address;
1267
- * const weth = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" satisfies Address;
1268
- * const ethUsdOracle = "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419" satisfies Address;
1269
- * const adaptiveCurveIrm = "0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC" satisfies Address;
1270
- * const targetMarket = {
1271
- * loanToken: usdc,
1272
- * collateralToken: weth,
1273
- * oracle: ethUsdOracle,
1274
- * irm: adaptiveCurveIrm,
1275
- * lltv: 860_000_000_000_000_000n,
1276
- * } satisfies BlueInputMarketParams;
1277
- *
1278
- * const calls: BundlerCall[] = BundlerAction.vaultV2BluePublicAllocatorAllocateFromIdle(
1279
- * ChainId.EthMainnet,
1280
- * keyrockUsdcVault,
1281
- * targetAdapterFixture,
1282
- * targetMarket,
1283
- * 1_000_000n,
1284
- * 1_000_000_000_000_000n,
1285
- * );
1286
- * // Bundler3 resets and approves 1_000 USDC units, then calls `allocateFromIdle` with zero native value.
1287
- * ```
1288
- */
1289
- // biome-ignore lint/complexity/useMaxParams: mirrors the protocol call
1290
- function vaultV2BluePublicAllocatorAllocateFromIdle(chainId, vault, adapter, market, assets, penalty, skipRevert = false) {
1291
- const { vaultV2BluePublicAllocator: allocator } = getChainAddresses(chainId);
1292
- if (allocator == null) {
1293
- throw new BundlerErrors.UnexpectedAction("vaultV2BluePublicAllocatorAllocateFromIdle", chainId);
1294
- }
1295
- const calls = [];
1296
- const penaltyAssets = VaultV2BluePublicAllocatorConfigUtils.getPenaltyAssets({ penalty }, assets);
1297
- if (skipRevert && penaltyAssets > 0n) {
1298
- throw new BundlerErrors.SkippableAllocatorPenalty(penaltyAssets);
1299
- }
1300
- if (penaltyAssets > 0n) {
1301
- for (const amount of [0n, penaltyAssets])
1302
- calls.push({
1303
- to: market.loanToken,
1304
- data: encodeFunctionData({
1305
- abi: erc20Abi,
1306
- functionName: "approve",
1307
- args: [allocator, amount],
1308
- }),
1309
- value: 0n,
1310
- skipRevert,
1311
- callbackHash: zeroHash,
1312
- });
1313
- }
1314
- calls.push({
1315
- to: allocator,
1316
- data: encodeFunctionData({
1317
- abi: vaultV2BluePublicAllocatorAbi,
1318
- functionName: "allocateFromIdle",
1319
- args: [vault, adapter, market, assets, penalty],
1320
- }),
1321
- value: 0n,
1322
- skipRevert,
1323
- callbackHash: zeroHash,
1324
- });
1325
- return calls;
1326
- }
1327
- BundlerAction.vaultV2BluePublicAllocatorAllocateFromIdle = vaultV2BluePublicAllocatorAllocateFromIdle;
1328
- /**
1329
- * Encodes a GeneralAdapter1 native-token wrap call.
1330
- *
1331
- * @param chainId - Chain where the action will execute.
1332
- * @param amount - Native-token amount to wrap.
1333
- * @param recipient - Recipient of wrapped native tokens.
1334
- * @param skipRevert - Whether Bundler3 should tolerate a revert.
1335
- * @returns Encoded Bundler3 calls.
1336
- *
1337
- * @example
1338
- * ```ts
1339
- * import { BundlerAction } from "@morpho-org/morpho-sdk/bundler";
1340
- *
1341
- * const recipient = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
1342
- *
1343
- * const calls = BundlerAction.wrapNative(
1344
- * 1,
1345
- * 1_000000000000000000n,
1346
- * recipient,
1347
- * );
1348
- * ```
1349
- */
1350
- // biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
1351
- function wrapNative(chainId, amount, recipient, skipRevert = false) {
1352
- const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
1353
- return [
1354
- {
1355
- to: generalAdapter1,
1356
- data: encodeFunctionData({
1357
- abi: generalAdapter1Abi,
1358
- functionName: "wrapNative",
1359
- args: [amount, recipient],
1360
- }),
1361
- value: 0n,
1362
- skipRevert,
1363
- callbackHash: zeroHash,
1364
- },
1365
- ];
1366
- }
1367
- BundlerAction.wrapNative = wrapNative;
1368
- })(BundlerAction || (BundlerAction = {}));