@morpho-org/morpho-sdk 2.0.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (202) hide show
  1. package/lib/cjs/abis.d.ts +2816 -0
  2. package/lib/cjs/abis.js +1951 -0
  3. package/lib/cjs/actions/index.d.ts +3 -0
  4. package/lib/cjs/actions/marketV1/borrow.js +7 -10
  5. package/lib/cjs/actions/marketV1/buildReallocationActions.d.ts +2 -3
  6. package/lib/cjs/actions/marketV1/buildReallocationActions.js +1 -2
  7. package/lib/cjs/actions/marketV1/repay.d.ts +2 -0
  8. package/lib/cjs/actions/marketV1/repay.js +7 -8
  9. package/lib/cjs/actions/marketV1/repayWithdrawCollateral.d.ts +2 -0
  10. package/lib/cjs/actions/marketV1/repayWithdrawCollateral.js +9 -10
  11. package/lib/cjs/actions/marketV1/supplyCollateral.d.ts +2 -0
  12. package/lib/cjs/actions/marketV1/supplyCollateral.js +11 -12
  13. package/lib/cjs/actions/marketV1/supplyCollateralBorrow.d.ts +2 -0
  14. package/lib/cjs/actions/marketV1/supplyCollateralBorrow.js +13 -14
  15. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +2 -2
  16. package/lib/cjs/actions/requirements/getRequirementsAction.d.ts +28 -4
  17. package/lib/cjs/actions/requirements/getRequirementsAction.js +28 -4
  18. package/lib/cjs/actions/requirements/getRequirementsApproval.js +2 -2
  19. package/lib/cjs/actions/requirements/index.d.ts +1 -0
  20. package/lib/cjs/actions/requirements/index.js +1 -0
  21. package/lib/cjs/actions/vaultV1/deposit.d.ts +2 -0
  22. package/lib/cjs/actions/vaultV1/deposit.js +13 -14
  23. package/lib/cjs/actions/vaultV1/migrateToV2.d.ts +2 -0
  24. package/lib/cjs/actions/vaultV1/migrateToV2.js +11 -9
  25. package/lib/cjs/actions/vaultV2/deposit.d.ts +2 -0
  26. package/lib/cjs/actions/vaultV2/deposit.js +13 -14
  27. package/lib/cjs/addresses.d.ts +2 -0
  28. package/lib/cjs/addresses.js +18 -0
  29. package/lib/cjs/augment/AccrualPosition.d.ts +9 -0
  30. package/lib/cjs/augment/AccrualPosition.js +8 -0
  31. package/lib/cjs/augment/AccrualVault.d.ts +8 -0
  32. package/lib/cjs/augment/AccrualVault.js +7 -0
  33. package/lib/cjs/augment/Holding.d.ts +8 -0
  34. package/lib/cjs/augment/Holding.js +7 -0
  35. package/lib/cjs/augment/Market.d.ts +8 -0
  36. package/lib/cjs/augment/Market.js +7 -0
  37. package/lib/cjs/augment/MarketParams.d.ts +8 -0
  38. package/lib/cjs/augment/MarketParams.js +7 -0
  39. package/lib/cjs/augment/Position.d.ts +8 -0
  40. package/lib/cjs/augment/Position.js +7 -0
  41. package/lib/cjs/augment/Token.d.ts +8 -0
  42. package/lib/cjs/augment/Token.js +7 -0
  43. package/lib/cjs/augment/User.d.ts +8 -0
  44. package/lib/cjs/augment/User.js +7 -0
  45. package/lib/cjs/augment/Vault.d.ts +8 -0
  46. package/lib/cjs/augment/Vault.js +7 -0
  47. package/lib/cjs/augment/VaultConfig.d.ts +8 -0
  48. package/lib/cjs/augment/VaultConfig.js +7 -0
  49. package/lib/cjs/augment/VaultMarketAllocation.d.ts +8 -0
  50. package/lib/cjs/augment/VaultMarketAllocation.js +7 -0
  51. package/lib/cjs/augment/VaultMarketConfig.d.ts +8 -0
  52. package/lib/cjs/augment/VaultMarketConfig.js +7 -0
  53. package/lib/cjs/augment/VaultMarketPublicAllocatorConfig.d.ts +8 -0
  54. package/lib/cjs/augment/VaultMarketPublicAllocatorConfig.js +8 -0
  55. package/lib/cjs/augment/VaultUser.d.ts +8 -0
  56. package/lib/cjs/augment/VaultUser.js +7 -0
  57. package/lib/cjs/augment.d.ts +14 -0
  58. package/lib/cjs/augment.js +16 -0
  59. package/lib/cjs/bundler/actions.d.ts +555 -0
  60. package/lib/cjs/bundler/actions.js +953 -0
  61. package/lib/cjs/bundler/index.d.ts +2 -0
  62. package/lib/cjs/bundler/index.js +18 -0
  63. package/lib/cjs/bundler/types.d.ts +181 -0
  64. package/lib/cjs/bundler/types.js +2 -0
  65. package/lib/cjs/constants.d.ts +3 -0
  66. package/lib/cjs/constants.js +32 -0
  67. package/lib/cjs/entities/index.d.ts +6 -3
  68. package/lib/cjs/entities/index.js +41 -17
  69. package/lib/cjs/entities/marketV1/marketV1.d.ts +45 -11
  70. package/lib/cjs/entities/marketV1/marketV1.js +45 -23
  71. package/lib/cjs/entities/reallocationData.d.ts +193 -0
  72. package/lib/cjs/entities/reallocationData.js +406 -0
  73. package/lib/cjs/entities/vaultV1/vaultV1.js +6 -2
  74. package/lib/cjs/entities/vaultV2/vaultV2.js +6 -1
  75. package/lib/cjs/errors.d.ts +2 -0
  76. package/lib/cjs/errors.js +18 -0
  77. package/lib/cjs/fetch.d.ts +1 -0
  78. package/lib/cjs/fetch.js +30 -0
  79. package/lib/cjs/format.d.ts +2 -0
  80. package/lib/cjs/format.js +22 -0
  81. package/lib/cjs/helpers/computeReallocations.d.ts +48 -8
  82. package/lib/cjs/helpers/computeReallocations.js +120 -32
  83. package/lib/cjs/helpers/constant.d.ts +17 -0
  84. package/lib/cjs/helpers/constant.js +30 -1
  85. package/lib/cjs/helpers/index.d.ts +1 -0
  86. package/lib/cjs/helpers/index.js +9 -1
  87. package/lib/cjs/helpers/metadata.d.ts +10 -0
  88. package/lib/cjs/helpers/metadata.js +20 -1
  89. package/lib/cjs/helpers/validate.d.ts +2 -0
  90. package/lib/cjs/helpers/validate.js +14 -2
  91. package/lib/cjs/index.d.ts +0 -1
  92. package/lib/cjs/index.js +0 -1
  93. package/lib/cjs/types/client.d.ts +1 -1
  94. package/lib/cjs/types/error.d.ts +91 -1
  95. package/lib/cjs/types/error.js +126 -1
  96. package/lib/cjs/types/sharedLiquidity.d.ts +43 -3
  97. package/lib/cjs/types.d.ts +3 -0
  98. package/lib/cjs/types.js +17 -0
  99. package/lib/cjs/utils.d.ts +9 -0
  100. package/lib/cjs/utils.js +65 -0
  101. package/lib/esm/abis.d.ts +2816 -0
  102. package/lib/esm/abis.js +1922 -0
  103. package/lib/esm/actions/index.d.ts +3 -0
  104. package/lib/esm/actions/marketV1/borrow.js +2 -5
  105. package/lib/esm/actions/marketV1/buildReallocationActions.d.ts +2 -3
  106. package/lib/esm/actions/marketV1/buildReallocationActions.js +1 -2
  107. package/lib/esm/actions/marketV1/repay.d.ts +2 -0
  108. package/lib/esm/actions/marketV1/repay.js +4 -5
  109. package/lib/esm/actions/marketV1/repayWithdrawCollateral.d.ts +2 -0
  110. package/lib/esm/actions/marketV1/repayWithdrawCollateral.js +4 -5
  111. package/lib/esm/actions/marketV1/supplyCollateral.d.ts +2 -0
  112. package/lib/esm/actions/marketV1/supplyCollateral.js +4 -5
  113. package/lib/esm/actions/marketV1/supplyCollateralBorrow.d.ts +2 -0
  114. package/lib/esm/actions/marketV1/supplyCollateralBorrow.js +4 -5
  115. package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +1 -1
  116. package/lib/esm/actions/requirements/getRequirementsAction.d.ts +28 -4
  117. package/lib/esm/actions/requirements/getRequirementsAction.js +29 -5
  118. package/lib/esm/actions/requirements/getRequirementsApproval.js +1 -1
  119. package/lib/esm/actions/requirements/index.d.ts +1 -0
  120. package/lib/esm/actions/requirements/index.js +1 -0
  121. package/lib/esm/actions/vaultV1/deposit.d.ts +2 -0
  122. package/lib/esm/actions/vaultV1/deposit.js +3 -4
  123. package/lib/esm/actions/vaultV1/migrateToV2.d.ts +2 -0
  124. package/lib/esm/actions/vaultV1/migrateToV2.js +3 -1
  125. package/lib/esm/actions/vaultV2/deposit.d.ts +2 -0
  126. package/lib/esm/actions/vaultV2/deposit.js +3 -4
  127. package/lib/esm/addresses.d.ts +2 -0
  128. package/lib/esm/addresses.js +1 -0
  129. package/lib/esm/augment/AccrualPosition.d.ts +9 -0
  130. package/lib/esm/augment/AccrualPosition.js +5 -0
  131. package/lib/esm/augment/AccrualVault.d.ts +8 -0
  132. package/lib/esm/augment/AccrualVault.js +4 -0
  133. package/lib/esm/augment/Holding.d.ts +8 -0
  134. package/lib/esm/augment/Holding.js +4 -0
  135. package/lib/esm/augment/Market.d.ts +8 -0
  136. package/lib/esm/augment/Market.js +4 -0
  137. package/lib/esm/augment/MarketParams.d.ts +8 -0
  138. package/lib/esm/augment/MarketParams.js +4 -0
  139. package/lib/esm/augment/Position.d.ts +8 -0
  140. package/lib/esm/augment/Position.js +4 -0
  141. package/lib/esm/augment/Token.d.ts +8 -0
  142. package/lib/esm/augment/Token.js +4 -0
  143. package/lib/esm/augment/User.d.ts +8 -0
  144. package/lib/esm/augment/User.js +4 -0
  145. package/lib/esm/augment/Vault.d.ts +8 -0
  146. package/lib/esm/augment/Vault.js +4 -0
  147. package/lib/esm/augment/VaultConfig.d.ts +8 -0
  148. package/lib/esm/augment/VaultConfig.js +4 -0
  149. package/lib/esm/augment/VaultMarketAllocation.d.ts +8 -0
  150. package/lib/esm/augment/VaultMarketAllocation.js +4 -0
  151. package/lib/esm/augment/VaultMarketConfig.d.ts +8 -0
  152. package/lib/esm/augment/VaultMarketConfig.js +4 -0
  153. package/lib/esm/augment/VaultMarketPublicAllocatorConfig.d.ts +8 -0
  154. package/lib/esm/augment/VaultMarketPublicAllocatorConfig.js +5 -0
  155. package/lib/esm/augment/VaultUser.d.ts +8 -0
  156. package/lib/esm/augment/VaultUser.js +4 -0
  157. package/lib/esm/augment.d.ts +14 -0
  158. package/lib/esm/augment.js +14 -0
  159. package/lib/esm/bundler/actions.d.ts +555 -0
  160. package/lib/esm/bundler/actions.js +950 -0
  161. package/lib/esm/bundler/index.d.ts +2 -0
  162. package/lib/esm/bundler/index.js +2 -0
  163. package/lib/esm/bundler/types.d.ts +181 -0
  164. package/lib/esm/bundler/types.js +1 -0
  165. package/lib/esm/constants.d.ts +3 -0
  166. package/lib/esm/constants.js +3 -0
  167. package/lib/esm/entities/index.d.ts +6 -3
  168. package/lib/esm/entities/index.js +5 -3
  169. package/lib/esm/entities/marketV1/marketV1.d.ts +45 -11
  170. package/lib/esm/entities/marketV1/marketV1.js +46 -24
  171. package/lib/esm/entities/reallocationData.d.ts +193 -0
  172. package/lib/esm/entities/reallocationData.js +402 -0
  173. package/lib/esm/entities/vaultV1/vaultV1.js +6 -2
  174. package/lib/esm/entities/vaultV2/vaultV2.js +6 -1
  175. package/lib/esm/errors.d.ts +2 -0
  176. package/lib/esm/errors.js +1 -0
  177. package/lib/esm/fetch.d.ts +1 -0
  178. package/lib/esm/fetch.js +1 -0
  179. package/lib/esm/format.d.ts +2 -0
  180. package/lib/esm/format.js +1 -0
  181. package/lib/esm/helpers/computeReallocations.d.ts +48 -8
  182. package/lib/esm/helpers/computeReallocations.js +119 -31
  183. package/lib/esm/helpers/constant.d.ts +17 -0
  184. package/lib/esm/helpers/constant.js +30 -1
  185. package/lib/esm/helpers/index.d.ts +1 -0
  186. package/lib/esm/helpers/index.js +1 -0
  187. package/lib/esm/helpers/metadata.d.ts +10 -0
  188. package/lib/esm/helpers/metadata.js +20 -1
  189. package/lib/esm/helpers/validate.d.ts +2 -0
  190. package/lib/esm/helpers/validate.js +12 -1
  191. package/lib/esm/index.d.ts +0 -1
  192. package/lib/esm/index.js +0 -1
  193. package/lib/esm/types/client.d.ts +1 -1
  194. package/lib/esm/types/error.d.ts +91 -1
  195. package/lib/esm/types/error.js +118 -0
  196. package/lib/esm/types/sharedLiquidity.d.ts +43 -3
  197. package/lib/esm/types.d.ts +3 -0
  198. package/lib/esm/types.js +1 -0
  199. package/lib/esm/utils.d.ts +9 -0
  200. package/lib/esm/utils.js +7 -0
  201. package/package.json +121 -14
  202. package/src/index.ts +0 -1
@@ -0,0 +1,2 @@
1
+ export * from "./actions.js";
2
+ export * from "./types.js";
@@ -0,0 +1,2 @@
1
+ export * from "./actions.js";
2
+ export * from "./types.js";
@@ -0,0 +1,181 @@
1
+ import type { InputMarketParams } from "@morpho-org/blue-sdk";
2
+ import type { Address, Hex } from "viem";
3
+ /**
4
+ * Morpho authorization payload signed by a user for Bundler3-mediated Morpho
5
+ * Blue execution.
6
+ */
7
+ export interface Authorization {
8
+ /** Address granting or revoking authorization. */
9
+ readonly authorizer: Address;
10
+ /** Address receiving authorization. */
11
+ readonly authorized: Address;
12
+ /** Whether authorization is granted (`true`) or revoked (`false`). */
13
+ readonly isAuthorized: boolean;
14
+ /** Morpho authorization nonce consumed by the signature. */
15
+ readonly nonce: bigint;
16
+ /** Signature deadline timestamp in seconds. */
17
+ readonly deadline: bigint;
18
+ }
19
+ /**
20
+ * Public allocator withdrawal input used by a `reallocateTo` Bundler3 action.
21
+ */
22
+ export interface InputReallocation {
23
+ /** Market to withdraw liquidity from. */
24
+ readonly marketParams: InputMarketParams;
25
+ /** Amount of loan assets to withdraw from the market. */
26
+ readonly amount: bigint;
27
+ }
28
+ /**
29
+ * Permit2 single-permit token allowance details.
30
+ */
31
+ export interface Permit2PermitSingleDetails {
32
+ /** ERC20 token approved through Permit2. */
33
+ readonly token: Address;
34
+ /** Allowance amount approved through Permit2. */
35
+ readonly amount: bigint;
36
+ /** Permit2 allowance expiration timestamp in seconds. */
37
+ readonly expiration: number;
38
+ /** Permit2 nonce consumed by the signature. */
39
+ readonly nonce: number;
40
+ }
41
+ /**
42
+ * Permit2 single-permit payload signed by the token owner.
43
+ */
44
+ export interface Permit2PermitSingle {
45
+ /** Token, amount, expiration, and nonce being approved. */
46
+ readonly details: Permit2PermitSingleDetails;
47
+ /** Deadline timestamp for consuming the Permit2 signature. */
48
+ readonly sigDeadline: bigint;
49
+ }
50
+ /**
51
+ * Argument tuples for Bundler3 actions supported by `morpho-sdk`.
52
+ */
53
+ export interface ActionArgs {
54
+ /** Native-token transfer from `owner` to `recipient` for `amount`; `skipRevert` controls Bundler3 revert handling. */
55
+ readonly nativeTransfer: [
56
+ owner: Address,
57
+ recipient: Address,
58
+ amount: bigint,
59
+ skipRevert?: boolean
60
+ ];
61
+ /** ERC20 transfer from `adapter` to `recipient` for `amount` of `asset`; `skipRevert` controls Bundler3 revert handling. */
62
+ readonly erc20Transfer: [
63
+ asset: Address,
64
+ recipient: Address,
65
+ amount: bigint,
66
+ adapter: Address,
67
+ skipRevert?: boolean
68
+ ];
69
+ /** GeneralAdapter1 ERC20 `transferFrom` of `asset` and `amount` to `recipient`; `skipRevert` controls Bundler3 revert handling. */
70
+ readonly erc20TransferFrom: [
71
+ asset: Address,
72
+ amount: bigint,
73
+ recipient: Address,
74
+ skipRevert?: boolean
75
+ ];
76
+ /** ERC-2612 permit from `owner` for `asset`, `amount`, `deadline`, and `signature`; `skipRevert` controls Bundler3 revert handling. */
77
+ readonly permit: [
78
+ owner: Address,
79
+ asset: Address,
80
+ amount: bigint,
81
+ deadline: bigint,
82
+ signature: Hex | null,
83
+ skipRevert?: boolean
84
+ ];
85
+ /** Permit2 approval from `owner` for `permitSingle` and `signature`; `skipRevert` controls Bundler3 revert handling. */
86
+ readonly approve2: [
87
+ owner: Address,
88
+ permitSingle: Permit2PermitSingle,
89
+ signature: Hex | null,
90
+ skipRevert?: boolean
91
+ ];
92
+ /** GeneralAdapter1 Permit2 transfer of `asset` and `amount` to `recipient`; `skipRevert` controls Bundler3 revert handling. */
93
+ readonly transferFrom2: [
94
+ asset: Address,
95
+ amount: bigint,
96
+ recipient: Address,
97
+ skipRevert?: boolean
98
+ ];
99
+ /** ERC4626 deposit into `erc4626` for `assets`, `maxSharePrice`, and `receiver`; `skipRevert` controls Bundler3 revert handling. */
100
+ readonly erc4626Deposit: [
101
+ erc4626: Address,
102
+ assets: bigint,
103
+ maxSharePrice: bigint,
104
+ receiver: Address,
105
+ skipRevert?: boolean
106
+ ];
107
+ /** ERC4626 redeem from `erc4626` for `shares`, `minSharePrice`, `receiver`, and `owner`; `skipRevert` controls Bundler3 revert handling. */
108
+ readonly erc4626Redeem: [
109
+ erc4626: Address,
110
+ shares: bigint,
111
+ minSharePrice: bigint,
112
+ receiver: Address,
113
+ owner: Address,
114
+ skipRevert?: boolean
115
+ ];
116
+ /** Morpho Blue supply-collateral call for `market`, `assets`, `onBehalf`, and callback actions; `skipRevert` controls Bundler3 revert handling. */
117
+ readonly morphoSupplyCollateral: [
118
+ market: InputMarketParams,
119
+ assets: bigint,
120
+ onBehalf: Address,
121
+ onMorphoSupplyCollateral: Action[],
122
+ skipRevert?: boolean
123
+ ];
124
+ /** Morpho Blue borrow call for `market`, `assets` or `shares`, `slippageAmount`, and `receiver`; `skipRevert` controls Bundler3 revert handling. */
125
+ readonly morphoBorrow: [
126
+ market: InputMarketParams,
127
+ assets: bigint,
128
+ shares: bigint,
129
+ slippageAmount: bigint,
130
+ receiver: Address,
131
+ skipRevert?: boolean
132
+ ];
133
+ /** Morpho Blue repay call for `market`, `assets` or `shares`, `slippageAmount`, `onBehalf`, and callback actions; `skipRevert` controls Bundler3 revert handling. */
134
+ readonly morphoRepay: [
135
+ market: InputMarketParams,
136
+ assets: bigint,
137
+ shares: bigint,
138
+ slippageAmount: bigint,
139
+ onBehalf: Address,
140
+ onMorphoRepay: Action[],
141
+ skipRevert?: boolean
142
+ ];
143
+ /** Morpho Blue withdraw-collateral call for `market`, `assets`, and `receiver`; `skipRevert` controls Bundler3 revert handling. */
144
+ readonly morphoWithdrawCollateral: [
145
+ market: InputMarketParams,
146
+ assets: bigint,
147
+ receiver: Address,
148
+ skipRevert?: boolean
149
+ ];
150
+ /** PublicAllocator reallocation to `supplyMarket` from `vault` withdrawals while paying `fee`; `skipRevert` controls Bundler3 revert handling. */
151
+ readonly reallocateTo: [
152
+ vault: Address,
153
+ fee: bigint,
154
+ withdrawals: InputReallocation[],
155
+ supplyMarket: InputMarketParams,
156
+ skipRevert?: boolean
157
+ ];
158
+ /** GeneralAdapter1 native wrap of `amount` to `recipient`; `skipRevert` controls Bundler3 revert handling. */
159
+ readonly wrapNative: [
160
+ amount: bigint,
161
+ recipient: Address,
162
+ skipRevert?: boolean
163
+ ];
164
+ }
165
+ /**
166
+ * Supported Bundler3 action discriminator.
167
+ */
168
+ export type ActionType = keyof ActionArgs;
169
+ /**
170
+ * Supported Bundler3 action object map keyed by action discriminator.
171
+ */
172
+ export type Actions = {
173
+ readonly [T in ActionType]: {
174
+ readonly type: T;
175
+ readonly args: ActionArgs[T];
176
+ };
177
+ };
178
+ /**
179
+ * Discriminated union of Bundler3 actions supported by `morpho-sdk`.
180
+ */
181
+ export type Action = Actions[ActionType];
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ export { ChainId, ChainUtils, DEFAULT_SLIPPAGE_TOLERANCE, EIP_712_FIELDS, isMarketId, LIQUIDATION_CURSOR, MAX_LIQUIDATION_INCENTIVE_FACTOR, ORACLE_PRICE_SCALE, SECONDS_PER_YEAR, TransactionType, } from "@morpho-org/blue-sdk";
2
+ export { BLUE_API_BASE_URL, BLUE_API_GRAPHQL_URL, CDN_BASE_URL, DOCS_BASE_URL, MORPHO_DOMAIN, OPTIMIZERS_API_BASE_URL, OPTIMIZERS_BASE_URL, REWARDS_BASE_URL, ZERO_ADDRESS, } from "@morpho-org/morpho-ts";
3
+ export { APPROVE_ONLY_ONCE_TOKENS, DEFAULT_LLTV_BUFFER, DEFAULT_SUPPLY_TARGET_UTILIZATION, DEFAULT_WITHDRAWAL_TARGET_UTILIZATION, MAX_ABSOLUTE_SHARE_PRICE, MAX_SLIPPAGE_TOLERANCE, MAX_TOKEN_APPROVALS, } from "./helpers/constant.js";
@@ -0,0 +1,3 @@
1
+ export { ChainId, ChainUtils, DEFAULT_SLIPPAGE_TOLERANCE, EIP_712_FIELDS, isMarketId, LIQUIDATION_CURSOR, MAX_LIQUIDATION_INCENTIVE_FACTOR, ORACLE_PRICE_SCALE, SECONDS_PER_YEAR, TransactionType, } from "@morpho-org/blue-sdk";
2
+ export { BLUE_API_BASE_URL, BLUE_API_GRAPHQL_URL, CDN_BASE_URL, DOCS_BASE_URL, MORPHO_DOMAIN, OPTIMIZERS_API_BASE_URL, OPTIMIZERS_BASE_URL, REWARDS_BASE_URL, ZERO_ADDRESS, } from "@morpho-org/morpho-ts";
3
+ export { APPROVE_ONLY_ONCE_TOKENS, DEFAULT_LLTV_BUFFER, DEFAULT_SUPPLY_TARGET_UTILIZATION, DEFAULT_WITHDRAWAL_TARGET_UTILIZATION, MAX_ABSOLUTE_SHARE_PRICE, MAX_SLIPPAGE_TOLERANCE, MAX_TOKEN_APPROVALS, } from "./helpers/constant.js";
@@ -1,3 +1,6 @@
1
- export * from "./marketV1/index.js";
2
- export * from "./vaultV1/index.js";
3
- export * from "./vaultV2/index.js";
1
+ export type { IAccrualPosition, IAccrualVault, IAccrualVaultV2, IAccrualVaultV2Adapter, IAccrualVaultV2MorphoMarketV1Adapter, IAccrualVaultV2MorphoMarketV1AdapterV2, IAccrualVaultV2MorphoVaultV1Adapter, IAssetBalances, IEip5267Domain, IHolding, IMarket, IMarketParams, IPosition, IPreLiquidationParams, IPreLiquidationPosition, IToken, IVault, IVaultConfig, IVaultMarketAllocation, IVaultMarketConfig, IVaultMarketPublicAllocatorConfig, IVaultToken, IVaultUser, IVaultV2, IVaultV2Adapter, IVaultV2Allocation, IVaultV2MorphoMarketV1Adapter, IVaultV2MorphoMarketV1AdapterV2, IVaultV2MorphoVaultV1Adapter, PeripheralBalance, PeripheralBalanceType, VaultPublicAllocatorConfig, } from "@morpho-org/blue-sdk";
2
+ export { AccrualPosition, AccrualVault, AccrualVaultV2, AccrualVaultV2MorphoMarketV1Adapter, AccrualVaultV2MorphoMarketV1AdapterV2, AccrualVaultV2MorphoVaultV1Adapter, AssetBalances, ConstantWrappedToken, Eip5267Domain, ExchangeRateWrappedToken, Holding, Market, MarketParams, Position, PreLiquidationParams, PreLiquidationPosition, Token, User, Vault, VaultConfig, VaultMarketAllocation, VaultMarketConfig, VaultMarketPublicAllocatorConfig, VaultToken, VaultUser, VaultV2, VaultV2Adapter, VaultV2MorphoMarketV1Adapter, VaultV2MorphoMarketV1AdapterV2, VaultV2MorphoVaultV1Adapter, WrappedToken, } from "@morpho-org/blue-sdk";
3
+ export { MorphoMarketV1 } from "./marketV1/index.js";
4
+ export { type InputReallocationData, ReallocationData, } from "./reallocationData.js";
5
+ export { MorphoVaultV1 } from "./vaultV1/index.js";
6
+ export { MorphoVaultV2 } from "./vaultV2/index.js";
@@ -1,3 +1,5 @@
1
- export * from "./marketV1/index.js";
2
- export * from "./vaultV1/index.js";
3
- export * from "./vaultV2/index.js";
1
+ export { AccrualPosition, AccrualVault, AccrualVaultV2, AccrualVaultV2MorphoMarketV1Adapter, AccrualVaultV2MorphoMarketV1AdapterV2, AccrualVaultV2MorphoVaultV1Adapter, AssetBalances, ConstantWrappedToken, Eip5267Domain, ExchangeRateWrappedToken, Holding, Market, MarketParams, Position, PreLiquidationParams, PreLiquidationPosition, Token, User, Vault, VaultConfig, VaultMarketAllocation, VaultMarketConfig, VaultMarketPublicAllocatorConfig, VaultToken, VaultUser, VaultV2, VaultV2Adapter, VaultV2MorphoMarketV1Adapter, VaultV2MorphoMarketV1AdapterV2, VaultV2MorphoVaultV1Adapter, WrappedToken, } from "@morpho-org/blue-sdk";
2
+ export { MorphoMarketV1 } from "./marketV1/index.js";
3
+ export { ReallocationData, } from "./reallocationData.js";
4
+ export { MorphoVaultV1 } from "./vaultV1/index.js";
5
+ export { MorphoVaultV2 } from "./vaultV2/index.js";
@@ -1,8 +1,8 @@
1
1
  import { type AccrualPosition, type Market, type MarketParams } from "@morpho-org/blue-sdk";
2
- import { type MinimalBlock, SimulationState } from "@morpho-org/simulation-sdk";
3
2
  import type { Address } from "viem";
4
3
  import type { FetchParameters } from "../../types/data.js";
5
4
  import { type DepositAmountArgs, type ERC20ApprovalAction, type MarketV1BorrowAction, type MarketV1RepayAction, type MarketV1RepayWithdrawCollateralAction, type MarketV1SupplyCollateralAction, type MarketV1SupplyCollateralBorrowAction, type MarketV1WithdrawCollateralAction, type MorphoAuthorizationAction, type MorphoClientType, type ReallocationComputeOptions, type RepayAmountArgs, type Requirement, type RequirementSignature, type Transaction, type VaultReallocation } from "../../types/index.js";
5
+ import { ReallocationData } from "../reallocationData.js";
6
6
  export interface MarketV1Actions {
7
7
  /**
8
8
  * Fetches the latest market data with accrued interest.
@@ -173,7 +173,7 @@ export interface MarketV1Actions {
173
173
  }) => Promise<(Readonly<Transaction<ERC20ApprovalAction>> | Readonly<Transaction<MorphoAuthorizationAction>> | Requirement)[]>;
174
174
  };
175
175
  /**
176
- * Fetches all on-chain data needed to construct a {@link SimulationState}
176
+ * Fetches all on-chain data needed to construct a {@link ReallocationData}
177
177
  * for computing vault reallocations via the public allocator.
178
178
  *
179
179
  * The target market is refetched internally at `block.number` so the
@@ -182,7 +182,7 @@ export interface MarketV1Actions {
182
182
  * inject unnecessary `reallocateTo` actions (and their PublicAllocator
183
183
  * fees) into the resulting bundle.
184
184
  *
185
- * The returned simulation state can be passed to {@link getReallocations}
185
+ * The returned reallocation data can be passed to {@link getReallocations}
186
186
  * to compute the `VaultReallocation[]` array for `borrow()` or
187
187
  * `supplyCollateralBorrow()`.
188
188
  *
@@ -190,12 +190,16 @@ export interface MarketV1Actions {
190
190
  *
191
191
  * @param params.vaultAddresses - Addresses of MetaMorpho vaults that allocate to this market.
192
192
  * @param params.block - The block to fetch data at (number and timestamp).
193
- * @returns A SimulationState populated with all required data.
193
+ * @returns A ReallocationData instance populated with all required data.
194
+ * @throws {ChainIdMismatchError} when the client chain does not match this market.
194
195
  */
195
196
  getReallocationData: (params: {
196
197
  vaultAddresses: readonly Address[];
197
- block: MinimalBlock;
198
- }) => Promise<SimulationState>;
198
+ block: {
199
+ readonly number: bigint;
200
+ readonly timestamp: bigint;
201
+ };
202
+ }) => Promise<ReallocationData>;
199
203
  /**
200
204
  * Computes vault reallocations for a borrow on this market.
201
205
  *
@@ -206,12 +210,17 @@ export interface MarketV1Actions {
206
210
  * @param params.reallocationData - The current on-chain state (from {@link getReallocationData}).
207
211
  * @param params.borrowAmount - The intended borrow amount.
208
212
  * @param params.options - Optional reallocation computation options
209
- * (utilization targets, reallocatable vaults filter, etc.).
213
+ * (timestamp, utilization targets, reallocatable vaults filter, etc.).
214
+ * Pass the fetched block timestamp to compute reallocations at the same block.
210
215
  * @returns Array of vault reallocations ready to pass to `borrow()` or
211
216
  * `supplyCollateralBorrow()`. Empty array if no reallocation is needed.
217
+ * @throws {ChainIdMismatchError} when `reallocationData` belongs to a different chain than this market.
218
+ * @throws {InsufficientSharedLiquidityError} when shared liquidity cannot cover the borrow shortfall on the target market.
219
+ * @throws {MissingPublicAllocatorConfigError} when a selected vault is missing its public allocator config.
220
+ * @throws {UnknownReallocationMarketError} when the target market is absent from the reallocation data.
212
221
  */
213
222
  getReallocations: (params: {
214
- reallocationData: SimulationState;
223
+ reallocationData: ReallocationData;
215
224
  borrowAmount: bigint;
216
225
  options?: ReallocationComputeOptions;
217
226
  }) => readonly VaultReallocation[];
@@ -281,12 +290,37 @@ export declare class MorphoMarketV1 implements MarketV1Actions {
281
290
  }) => Promise<(Requirement | Readonly<Transaction<ERC20ApprovalAction>> | Readonly<Transaction<MorphoAuthorizationAction>>)[]>;
282
291
  buildTx: (requirementSignature?: RequirementSignature) => Readonly<Transaction<MarketV1SupplyCollateralBorrowAction>>;
283
292
  };
293
+ /**
294
+ * Fetches all on-chain inputs needed to compute public allocator reallocations.
295
+ *
296
+ * @param params - Reallocation data fetch parameters.
297
+ * @param params.vaultAddresses - Vaults to inspect for source-market liquidity.
298
+ * @param params.block - Block number and timestamp used for consistent RPC reads.
299
+ * @returns Reallocation data ready for {@link getReallocations}.
300
+ * @throws {ChainIdMismatchError} when the client chain does not match this market.
301
+ */
284
302
  getReallocationData({ vaultAddresses, block, }: {
285
303
  vaultAddresses: readonly Address[];
286
- block: MinimalBlock;
287
- }): Promise<SimulationState>;
304
+ block: {
305
+ readonly number: bigint;
306
+ readonly timestamp: bigint;
307
+ };
308
+ }): Promise<ReallocationData>;
309
+ /**
310
+ * Computes public allocator reallocations for a borrow on this market.
311
+ *
312
+ * @param params - Reallocation computation parameters.
313
+ * @param params.reallocationData - State returned by {@link getReallocationData}.
314
+ * @param params.borrowAmount - Borrow amount to test against post-borrow utilization.
315
+ * @param params.options - Optional allocator and utilization options.
316
+ * @returns Vault reallocations ready to pass to `borrow` or `supplyCollateralBorrow`.
317
+ * @throws {ChainIdMismatchError} when `reallocationData` belongs to a different chain than this market.
318
+ * @throws {InsufficientSharedLiquidityError} when shared liquidity cannot cover the borrow shortfall on the target market.
319
+ * @throws {MissingPublicAllocatorConfigError} when a selected vault is missing its public allocator config.
320
+ * @throws {UnknownReallocationMarketError} when the target market is absent from the reallocation data.
321
+ */
288
322
  getReallocations({ reallocationData, borrowAmount, options, }: {
289
- reallocationData: SimulationState;
323
+ reallocationData: ReallocationData;
290
324
  borrowAmount: bigint;
291
325
  options?: ReallocationComputeOptions;
292
326
  }): readonly VaultReallocation[];
@@ -1,10 +1,10 @@
1
1
  import { DEFAULT_SLIPPAGE_TOLERANCE, MathLib, } from "@morpho-org/blue-sdk";
2
- import { fetchAccrualPosition, fetchHolding, fetchMarket, fetchPosition, fetchVault, fetchVaultMarketConfig, } from "@morpho-org/blue-sdk-viem";
2
+ import { fetchAccrualPosition, fetchMarket, fetchPosition, fetchVault, fetchVaultMarketConfig, } from "@morpho-org/blue-sdk-viem";
3
3
  import { Time } from "@morpho-org/morpho-ts";
4
- import { SimulationState } from "@morpho-org/simulation-sdk";
5
4
  import { getMorphoAuthorizationRequirement, getRequirements, marketV1Borrow, marketV1Repay, marketV1RepayWithdrawCollateral, marketV1SupplyCollateral, marketV1SupplyCollateralBorrow, marketV1WithdrawCollateral, } from "../../actions/index.js";
6
5
  import { computeMaxRepaySharePrice, computeMinBorrowSharePrice, computeReallocations, validateAccrualPosition, validateChainId, validateNativeCollateral, validatePositionHealth, validatePositionHealthAfterWithdraw, validateRepayAmount, validateRepayShares, validateSlippageTolerance, } from "../../helpers/index.js";
7
6
  import { MissingAccrualPositionError, MutuallyExclusiveRepayAmountsError, NegativeNativeAmountError, NonPositiveAssetAmountError, NonPositiveBorrowAmountError, NonPositiveRepayAmountError, NonPositiveWithdrawCollateralAmountError, WithdrawExceedsCollateralError, ZeroCollateralAmountError, } from "../../types/index.js";
7
+ import { ReallocationData } from "../reallocationData.js";
8
8
  export class MorphoMarketV1 {
9
9
  client;
10
10
  marketParams;
@@ -149,6 +149,7 @@ export class MorphoMarketV1 {
149
149
  let assets;
150
150
  let shares;
151
151
  let transferAmount;
152
+ let marketForRepay;
152
153
  if (isSharesMode) {
153
154
  validateRepayShares({
154
155
  positionData,
@@ -157,10 +158,12 @@ export class MorphoMarketV1 {
157
158
  });
158
159
  assets = 0n;
159
160
  shares = params.shares;
160
- // Add slippage buffer to cover interest accrued between tx construction and execution.
161
- // Without this, the on-chain repay amount may exceed the pre-transferred ERC20 amount.
162
- const baseTransferAmount = positionData.market.toBorrowAssets(shares, "Up");
163
- transferAmount = MathLib.wMulUp(baseTransferAmount, MathLib.WAD + slippageTolerance);
161
+ // 2h forward accrual upper-bounds the on-chain repay price; bundle
162
+ // skims residual back to the receiver.
163
+ const accrualTimestamp = MathLib.max(Time.timestamp(), positionData.market.lastUpdate) +
164
+ Time.s.from.h(2n);
165
+ marketForRepay = positionData.market.accrueInterest(accrualTimestamp);
166
+ transferAmount = marketForRepay.toBorrowAssets(shares, "Up");
164
167
  }
165
168
  else {
166
169
  validateRepayAmount({
@@ -171,11 +174,12 @@ export class MorphoMarketV1 {
171
174
  assets = params.assets;
172
175
  shares = 0n;
173
176
  transferAmount = params.assets;
177
+ marketForRepay = positionData.market;
174
178
  }
175
179
  const maxSharePrice = computeMaxRepaySharePrice({
176
180
  repayAssets: assets,
177
181
  repayShares: shares,
178
- market: positionData.market,
182
+ market: marketForRepay,
179
183
  slippageTolerance,
180
184
  });
181
185
  return {
@@ -278,6 +282,11 @@ export class MorphoMarketV1 {
278
282
  let assets;
279
283
  let shares;
280
284
  let transferAmount;
285
+ let marketForRepay;
286
+ // 2h forward accrual upper-bounds the on-chain repay price (shares
287
+ // mode) and the post-repay health check; bundle skims residual back.
288
+ const accrualTimestamp = MathLib.max(Time.timestamp(), positionData.market.lastUpdate) +
289
+ Time.s.from.h(2n);
281
290
  if (isSharesMode) {
282
291
  validateRepayShares({
283
292
  positionData,
@@ -286,8 +295,8 @@ export class MorphoMarketV1 {
286
295
  });
287
296
  assets = 0n;
288
297
  shares = params.shares;
289
- const baseTransferAmount = positionData.market.toBorrowAssets(shares, "Up");
290
- transferAmount = MathLib.wMulUp(baseTransferAmount, MathLib.WAD + slippageTolerance);
298
+ marketForRepay = positionData.market.accrueInterest(accrualTimestamp);
299
+ transferAmount = marketForRepay.toBorrowAssets(shares, "Up");
291
300
  }
292
301
  else {
293
302
  validateRepayAmount({
@@ -298,6 +307,7 @@ export class MorphoMarketV1 {
298
307
  assets = params.assets;
299
308
  shares = 0n;
300
309
  transferAmount = params.assets;
310
+ marketForRepay = positionData.market;
301
311
  }
302
312
  if (withdrawAmount > positionData.collateral) {
303
313
  throw new WithdrawExceedsCollateralError({
@@ -306,9 +316,6 @@ export class MorphoMarketV1 {
306
316
  market: positionData.marketId,
307
317
  });
308
318
  }
309
- // +10 min accrual buffer: residual debt grows between build and execute.
310
- const accrualTimestamp = MathLib.max(Time.timestamp(), positionData.market.lastUpdate) +
311
- Time.s.from.min(10n);
312
319
  const { position: positionAfterRepay } = positionData.repay(assets, shares, accrualTimestamp);
313
320
  validatePositionHealthAfterWithdraw({
314
321
  positionData: positionAfterRepay,
@@ -319,7 +326,7 @@ export class MorphoMarketV1 {
319
326
  const maxSharePrice = computeMaxRepaySharePrice({
320
327
  repayAssets: assets,
321
328
  repayShares: shares,
322
- market: positionData.market,
329
+ market: marketForRepay,
323
330
  slippageTolerance,
324
331
  });
325
332
  return {
@@ -437,6 +444,15 @@ export class MorphoMarketV1 {
437
444
  }),
438
445
  };
439
446
  }
447
+ /**
448
+ * Fetches all on-chain inputs needed to compute public allocator reallocations.
449
+ *
450
+ * @param params - Reallocation data fetch parameters.
451
+ * @param params.vaultAddresses - Vaults to inspect for source-market liquidity.
452
+ * @param params.block - Block number and timestamp used for consistent RPC reads.
453
+ * @returns Reallocation data ready for {@link getReallocations}.
454
+ * @throws {ChainIdMismatchError} when the client chain does not match this market.
455
+ */
440
456
  async getReallocationData({ vaultAddresses, block, }) {
441
457
  validateChainId(this.client.viemClient.chain?.id, this.chainId);
442
458
  const client = this.client.viemClient;
@@ -468,8 +484,7 @@ export class MorphoMarketV1 {
468
484
  }
469
485
  // Phase 2: Fetch all source markets, vault configs, and positions in parallel.
470
486
  const sourceMarketIds = [...allMarketIds].filter((mid) => mid !== targetMarketId);
471
- const loanToken = targetMarket.params.loanToken;
472
- const [markets, configs, positions, holdings] = await Promise.all([
487
+ const [markets, configs, positions] = await Promise.all([
473
488
  Promise.all(sourceMarketIds.map((mid) => fetchMarket(mid, client, fetchParams))),
474
489
  Promise.all(vaultMarketPairs.map(({ vault, marketId: mid }) => fetchVaultMarketConfig(vault, mid, client, fetchParams).then((config) => ({ vault, mid, config })))),
475
490
  Promise.all(vaultMarketPairs.map(({ vault, marketId: mid }) => fetchPosition(vault, mid, client, fetchParams).then((position) => ({
@@ -477,9 +492,8 @@ export class MorphoMarketV1 {
477
492
  mid,
478
493
  position,
479
494
  })))),
480
- Promise.all(vaultAddresses.map((addr) => fetchHolding(addr, loanToken, client, fetchParams))),
481
495
  ]);
482
- // Assemble records for SimulationState.
496
+ // Assemble records for ReallocationData.
483
497
  const marketsRecord = {
484
498
  [targetMarketId]: targetMarket,
485
499
  };
@@ -498,21 +512,29 @@ export class MorphoMarketV1 {
498
512
  for (const { vault, mid, position } of positions) {
499
513
  (positionsRecord[vault] ??= {})[mid] = position;
500
514
  }
501
- const holdingsRecord = {};
502
- for (const holding of holdings) {
503
- (holdingsRecord[holding.user] ??= {})[holding.token] = holding;
504
- }
505
- return new SimulationState({
515
+ return new ReallocationData({
506
516
  chainId: this.chainId,
507
- block,
508
517
  markets: marketsRecord,
509
518
  vaults: vaultsRecord,
510
519
  vaultMarketConfigs: vaultMarketConfigsRecord,
511
520
  positions: positionsRecord,
512
- holdings: holdingsRecord,
513
521
  });
514
522
  }
523
+ /**
524
+ * Computes public allocator reallocations for a borrow on this market.
525
+ *
526
+ * @param params - Reallocation computation parameters.
527
+ * @param params.reallocationData - State returned by {@link getReallocationData}.
528
+ * @param params.borrowAmount - Borrow amount to test against post-borrow utilization.
529
+ * @param params.options - Optional allocator and utilization options.
530
+ * @returns Vault reallocations ready to pass to `borrow` or `supplyCollateralBorrow`.
531
+ * @throws {ChainIdMismatchError} when `reallocationData` belongs to a different chain than this market.
532
+ * @throws {InsufficientSharedLiquidityError} when shared liquidity cannot cover the borrow shortfall on the target market.
533
+ * @throws {MissingPublicAllocatorConfigError} when a selected vault is missing its public allocator config.
534
+ * @throws {UnknownReallocationMarketError} when the target market is absent from the reallocation data.
535
+ */
515
536
  getReallocations({ reallocationData, borrowAmount, options, }) {
537
+ validateChainId(reallocationData.chainId, this.chainId);
516
538
  return computeReallocations({
517
539
  reallocationData,
518
540
  marketId: this.marketParams.id,