@morpho-org/consumer-sdk 0.3.0 → 0.5.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 (82) hide show
  1. package/README.md +301 -41
  2. package/lib/actions/index.d.ts +2 -0
  3. package/lib/actions/index.js +2 -0
  4. package/lib/actions/marketV1/borrow.d.ts +34 -0
  5. package/lib/actions/marketV1/borrow.js +62 -0
  6. package/lib/actions/marketV1/buildReallocationActions.d.ts +17 -0
  7. package/lib/actions/marketV1/buildReallocationActions.js +36 -0
  8. package/lib/actions/marketV1/index.d.ts +6 -0
  9. package/lib/actions/marketV1/index.js +22 -0
  10. package/lib/actions/marketV1/repay.d.ts +44 -0
  11. package/lib/actions/marketV1/repay.js +93 -0
  12. package/lib/actions/marketV1/repayWithdrawCollateral.d.ts +51 -0
  13. package/lib/actions/marketV1/repayWithdrawCollateral.js +108 -0
  14. package/lib/actions/marketV1/supplyCollateral.d.ts +28 -0
  15. package/lib/actions/marketV1/supplyCollateral.js +85 -0
  16. package/lib/actions/marketV1/supplyCollateralBorrow.d.ts +37 -0
  17. package/lib/actions/marketV1/supplyCollateralBorrow.js +109 -0
  18. package/lib/actions/marketV1/withdrawCollateral.d.ts +28 -0
  19. package/lib/actions/marketV1/withdrawCollateral.js +51 -0
  20. package/lib/actions/requirements/encode/encodeErc20Permit.js +4 -1
  21. package/lib/actions/requirements/encode/encodeErc20Permit2.js +4 -1
  22. package/lib/actions/requirements/getMorphoAuthorizationRequirement.d.ts +21 -0
  23. package/lib/actions/requirements/getMorphoAuthorizationRequirement.js +55 -0
  24. package/lib/actions/requirements/getRequirements.js +3 -0
  25. package/lib/actions/requirements/getRequirementsAction.d.ts +2 -2
  26. package/lib/actions/requirements/getRequirementsAction.js +7 -7
  27. package/lib/actions/requirements/index.d.ts +1 -0
  28. package/lib/actions/requirements/index.js +1 -0
  29. package/lib/actions/vaultV1/deposit.d.ts +41 -0
  30. package/lib/actions/vaultV1/deposit.js +116 -0
  31. package/lib/actions/vaultV1/index.d.ts +3 -0
  32. package/lib/actions/vaultV1/index.js +19 -0
  33. package/lib/actions/vaultV1/redeem.d.ts +29 -0
  34. package/lib/actions/vaultV1/redeem.js +48 -0
  35. package/lib/actions/vaultV1/withdraw.d.ts +29 -0
  36. package/lib/actions/vaultV1/withdraw.js +48 -0
  37. package/lib/actions/vaultV2/deposit.d.ts +20 -25
  38. package/lib/actions/vaultV2/deposit.js +78 -40
  39. package/lib/actions/vaultV2/forceRedeem.js +2 -2
  40. package/lib/actions/vaultV2/forceWithdraw.d.ts +2 -2
  41. package/lib/actions/vaultV2/forceWithdraw.js +12 -8
  42. package/lib/actions/vaultV2/redeem.js +2 -2
  43. package/lib/actions/vaultV2/withdraw.d.ts +3 -3
  44. package/lib/actions/vaultV2/withdraw.js +6 -6
  45. package/lib/client/morphoClient.d.ts +4 -1
  46. package/lib/client/morphoClient.js +6 -0
  47. package/lib/client/morphoViemExtension.d.ts +12 -9
  48. package/lib/client/morphoViemExtension.js +12 -9
  49. package/lib/entities/index.d.ts +2 -0
  50. package/lib/entities/index.js +2 -0
  51. package/lib/entities/marketV1/index.d.ts +1 -0
  52. package/lib/entities/marketV1/index.js +17 -0
  53. package/lib/entities/marketV1/marketV1.d.ts +290 -0
  54. package/lib/entities/marketV1/marketV1.js +528 -0
  55. package/lib/entities/vaultV1/index.d.ts +1 -0
  56. package/lib/entities/vaultV1/index.js +17 -0
  57. package/lib/entities/vaultV1/vaultV1.d.ts +95 -0
  58. package/lib/entities/vaultV1/vaultV1.js +129 -0
  59. package/lib/entities/vaultV2/vaultV2.d.ts +24 -21
  60. package/lib/entities/vaultV2/vaultV2.js +43 -16
  61. package/lib/helpers/computeReallocations.d.ts +23 -0
  62. package/lib/helpers/computeReallocations.js +98 -0
  63. package/lib/helpers/constant.d.ts +5 -0
  64. package/lib/helpers/constant.js +6 -1
  65. package/lib/helpers/encodeDeallocation.js +3 -3
  66. package/lib/helpers/index.d.ts +3 -0
  67. package/lib/helpers/index.js +18 -1
  68. package/lib/helpers/slippage.d.ts +46 -0
  69. package/lib/helpers/slippage.js +73 -0
  70. package/lib/helpers/validate.d.ts +150 -0
  71. package/lib/helpers/validate.js +279 -0
  72. package/lib/types/action.d.ts +113 -6
  73. package/lib/types/action.js +12 -1
  74. package/lib/types/client.d.ts +4 -1
  75. package/lib/types/deallocation.d.ts +1 -1
  76. package/lib/types/error.d.ts +127 -4
  77. package/lib/types/error.js +210 -12
  78. package/lib/types/index.d.ts +1 -0
  79. package/lib/types/index.js +1 -0
  80. package/lib/types/sharedLiquidity.d.ts +41 -0
  81. package/lib/types/sharedLiquidity.js +2 -0
  82. package/package.json +1 -1
@@ -0,0 +1,290 @@
1
+ import { type AccrualPosition, type Market, type MarketParams } from "@morpho-org/blue-sdk";
2
+ import { type MinimalBlock, SimulationState } from "@morpho-org/simulation-sdk";
3
+ import type { Address } from "viem";
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";
5
+ import type { FetchParameters } from "../../types/data";
6
+ export interface MarketV1Actions {
7
+ /**
8
+ * Fetches the latest market data with accrued interest.
9
+ *
10
+ * @param parameters - Optional fetch parameters (block number, state overrides).
11
+ * @returns Market state including total supply/borrow assets and shares.
12
+ */
13
+ getMarketData: (parameters?: FetchParameters) => Promise<Market>;
14
+ /**
15
+ * Fetches the user's position in this market with accrued interest.
16
+ *
17
+ * @param userAddress - The user whose position to fetch.
18
+ * @param parameters - Optional fetch parameters (block number, state overrides).
19
+ * @returns Accrual position with health metrics (maxBorrowAssets, ltv, isHealthy).
20
+ */
21
+ getPositionData: (userAddress: Address, parameters?: FetchParameters) => Promise<AccrualPosition>;
22
+ /**
23
+ * Prepares a supply-collateral transaction.
24
+ *
25
+ * Routed through bundler via GeneralAdapter1.
26
+ * `getRequirements` returns ERC20 approval or permit for GeneralAdapter1.
27
+ * When `nativeAmount` is provided, native token is wrapped; collateral must be wNative.
28
+ *
29
+ * @param params - Supply collateral parameters.
30
+ * @returns Object with `buildTx` and `getRequirements`.
31
+ */
32
+ supplyCollateral: (params: {
33
+ userAddress: Address;
34
+ } & DepositAmountArgs) => {
35
+ buildTx: (requirementSignature?: RequirementSignature) => Readonly<Transaction<MarketV1SupplyCollateralAction>>;
36
+ getRequirements: (params?: {
37
+ useSimplePermit?: boolean;
38
+ }) => Promise<(Readonly<Transaction<ERC20ApprovalAction>> | Requirement)[]>;
39
+ };
40
+ /**
41
+ * Prepares a borrow transaction.
42
+ *
43
+ * Routed through bundler3 via `morphoBorrow`.
44
+ * Validates position health with LLTV buffer (0.5%) using the pre-fetched `positionData`.
45
+ * Computes `minSharePrice` from market borrow state and `slippageTolerance`.
46
+ *
47
+ * When `reallocations` is provided, `reallocateTo` actions are prepended to the bundle,
48
+ * moving liquidity from other markets via the PublicAllocator before borrowing.
49
+ *
50
+ * `getRequirements` returns `morpho.setAuthorization(generalAdapter1, true)` if not yet authorized,
51
+ * since borrowing through bundler3 requires GeneralAdapter1 authorization on Morpho.
52
+ *
53
+ * **Stale `positionData` may cause unexpected health.**
54
+ *
55
+ * @param params - Borrow parameters including pre-fetched `positionData` for health validation.
56
+ * @returns Object with `buildTx` and `getRequirements`.
57
+ */
58
+ borrow: (params: {
59
+ userAddress: Address;
60
+ amount: bigint;
61
+ positionData: AccrualPosition;
62
+ slippageTolerance?: bigint;
63
+ reallocations?: readonly VaultReallocation[];
64
+ }) => {
65
+ buildTx: () => Readonly<Transaction<MarketV1BorrowAction>>;
66
+ getRequirements: () => Promise<Readonly<Transaction<MorphoAuthorizationAction>>[]>;
67
+ };
68
+ /**
69
+ * Prepares a repay transaction.
70
+ *
71
+ * Routed through bundler3 via GeneralAdapter1.
72
+ * Supports two modes via {@link RepayAmountArgs}:
73
+ * - **By assets** (`{ amount }`): repays an exact asset amount (partial repay).
74
+ * - **By shares** (`{ shares }`): repays exact shares (full repay, immune to interest accrual).
75
+ *
76
+ * Computes `maxSharePrice` from market borrow state and `slippageTolerance`.
77
+ *
78
+ * `getRequirements` returns ERC20 approval for loan token to GeneralAdapter1.
79
+ * Does NOT require Morpho authorization (anyone can repay on behalf of anyone).
80
+ *
81
+ * **Shares mode:** `slippageTolerance` also caps `transferAmount`.
82
+ *
83
+ * @param params - Repay parameters including pre-fetched `positionData`.
84
+ * @returns Object with `buildTx` and `getRequirements`.
85
+ */
86
+ repay: (params: {
87
+ userAddress: Address;
88
+ positionData: AccrualPosition;
89
+ slippageTolerance?: bigint;
90
+ } & RepayAmountArgs) => {
91
+ buildTx: (requirementSignature?: RequirementSignature) => Readonly<Transaction<MarketV1RepayAction>>;
92
+ getRequirements: (params?: {
93
+ useSimplePermit?: boolean;
94
+ }) => Promise<(Readonly<Transaction<ERC20ApprovalAction>> | Requirement)[]>;
95
+ };
96
+ /**
97
+ * Prepares a withdraw-collateral transaction.
98
+ *
99
+ * Direct call to `morpho.withdrawCollateral()` — no bundler, no GeneralAdapter1.
100
+ * The caller (`msg.sender`) must be `onBehalf`.
101
+ * Validates position health after withdrawal using the LLTV buffer.
102
+ *
103
+ * No `getRequirements` — no ERC20 approval or GeneralAdapter1 authorization needed
104
+ * (collateral flows out of Morpho, not in).
105
+ *
106
+ * **No on-chain slippage guard — stale `positionData` risks liquidation.**
107
+ *
108
+ * @param params - Withdraw collateral parameters including pre-fetched `positionData` for health validation.
109
+ * @returns Object with `buildTx`.
110
+ */
111
+ withdrawCollateral: (params: {
112
+ userAddress: Address;
113
+ amount: bigint;
114
+ positionData: AccrualPosition;
115
+ }) => {
116
+ buildTx: () => Readonly<Transaction<MarketV1WithdrawCollateralAction>>;
117
+ };
118
+ /**
119
+ * Prepares an atomic repay-and-withdraw-collateral transaction.
120
+ *
121
+ * Routed through bundler3. Bundle order: repay FIRST, then withdraw.
122
+ * Validates combined position health: simulates the repay, then checks
123
+ * that the resulting position can sustain the collateral withdrawal.
124
+ *
125
+ * `getRequirements` returns in parallel:
126
+ * - ERC20 approval for loan token to GeneralAdapter1 (for the repay).
127
+ * - `morpho.setAuthorization(generalAdapter1, true)` if not yet authorized (for the withdraw).
128
+ *
129
+ * **Stale `positionData` risks underestimated debt and unsafe withdrawal.**
130
+ *
131
+ * @param params - Combined parameters including pre-fetched `positionData`.
132
+ * @returns Object with `buildTx` and `getRequirements`.
133
+ */
134
+ repayWithdrawCollateral: (params: {
135
+ userAddress: Address;
136
+ withdrawAmount: bigint;
137
+ positionData: AccrualPosition;
138
+ slippageTolerance?: bigint;
139
+ } & RepayAmountArgs) => {
140
+ buildTx: (requirementSignature?: RequirementSignature) => Readonly<Transaction<MarketV1RepayWithdrawCollateralAction>>;
141
+ getRequirements: (params?: {
142
+ useSimplePermit?: boolean;
143
+ }) => Promise<(Readonly<Transaction<ERC20ApprovalAction>> | Readonly<Transaction<MorphoAuthorizationAction>> | Requirement)[]>;
144
+ };
145
+ /**
146
+ * Prepares an atomic supply-collateral-and-borrow transaction.
147
+ *
148
+ * Routed through the bundler. Validates position health with LLTV buffer
149
+ * to prevent instant liquidation on new positions near the LLTV threshold.
150
+ *
151
+ * When `reallocations` is provided, `reallocateTo` actions are prepended before
152
+ * `morphoBorrow` in the bundle.
153
+ *
154
+ * `getRequirements` returns in parallel:
155
+ * - ERC20 approval or permit for collateral token (to GeneralAdapter1).
156
+ * - `morpho.setAuthorization(generalAdapter1, true)` if adapter is not yet authorized.
157
+ *
158
+ * **Stale `positionData` may cause unexpected health.**
159
+ *
160
+ * @param params - Combined parameters including pre-fetched `positionData` for health validation.
161
+ * @returns Object with `buildTx` and `getRequirements`.
162
+ */
163
+ supplyCollateralBorrow: (params: {
164
+ userAddress: Address;
165
+ positionData: AccrualPosition;
166
+ borrowAmount: bigint;
167
+ slippageTolerance?: bigint;
168
+ reallocations?: readonly VaultReallocation[];
169
+ } & DepositAmountArgs) => {
170
+ buildTx: (requirementSignature?: RequirementSignature) => Readonly<Transaction<MarketV1SupplyCollateralBorrowAction>>;
171
+ getRequirements: (params?: {
172
+ useSimplePermit?: boolean;
173
+ }) => Promise<(Readonly<Transaction<ERC20ApprovalAction>> | Readonly<Transaction<MorphoAuthorizationAction>> | Requirement)[]>;
174
+ };
175
+ /**
176
+ * Fetches all on-chain data needed to construct a {@link SimulationState}
177
+ * for computing vault reallocations via the public allocator.
178
+ *
179
+ * The returned simulation state can be passed to {@link getReallocations}
180
+ * to compute the `VaultReallocation[]` array for `borrow()` or
181
+ * `supplyCollateralBorrow()`.
182
+ *
183
+ * **Stale data reverts on-chain (fail-safe).**
184
+ *
185
+ * @param params.vaultAddresses - Addresses of MetaMorpho vaults that allocate to this market.
186
+ * @param params.market - The target market data (from {@link getPositionData} or {@link getMarketData}).
187
+ * @param params.block - The block to fetch data at (number and timestamp).
188
+ * @returns A SimulationState populated with all required data.
189
+ */
190
+ getReallocationData: (params: {
191
+ vaultAddresses: readonly Address[];
192
+ market: Market;
193
+ block: MinimalBlock;
194
+ }) => Promise<SimulationState>;
195
+ /**
196
+ * Computes vault reallocations for a borrow on this market.
197
+ *
198
+ * Uses the shared liquidity algorithm to determine which vaults should
199
+ * reallocate liquidity to this market via the PublicAllocator, based on
200
+ * post-borrow utilization targets.
201
+ *
202
+ * @param params.reallocationData - The current on-chain state (from {@link getReallocationData}).
203
+ * @param params.borrowAmount - The intended borrow amount.
204
+ * @param params.options - Optional reallocation computation options
205
+ * (utilization targets, reallocatable vaults filter, etc.).
206
+ * @returns Array of vault reallocations ready to pass to `borrow()` or
207
+ * `supplyCollateralBorrow()`. Empty array if no reallocation is needed.
208
+ */
209
+ getReallocations: (params: {
210
+ reallocationData: SimulationState;
211
+ borrowAmount: bigint;
212
+ options?: ReallocationComputeOptions;
213
+ }) => readonly VaultReallocation[];
214
+ }
215
+ export declare class MorphoMarketV1 implements MarketV1Actions {
216
+ private readonly client;
217
+ readonly marketParams: MarketParams;
218
+ private readonly chainId;
219
+ constructor(client: MorphoClientType, marketParams: MarketParams, chainId: number);
220
+ getMarketData(parameters?: FetchParameters): Promise<Market>;
221
+ getPositionData(userAddress: Address, parameters?: FetchParameters): Promise<AccrualPosition>;
222
+ supplyCollateral({ amount, userAddress, nativeAmount, }: {
223
+ userAddress: Address;
224
+ } & DepositAmountArgs): {
225
+ getRequirements: (params?: {
226
+ useSimplePermit?: boolean;
227
+ }) => Promise<(Requirement | Readonly<Transaction<ERC20ApprovalAction>>)[]>;
228
+ buildTx: (requirementSignature?: RequirementSignature) => Readonly<Transaction<MarketV1SupplyCollateralAction>>;
229
+ };
230
+ borrow({ amount, userAddress, positionData, slippageTolerance, reallocations, }: {
231
+ amount: bigint;
232
+ userAddress: Address;
233
+ positionData: AccrualPosition;
234
+ slippageTolerance?: bigint;
235
+ reallocations?: readonly VaultReallocation[];
236
+ }): {
237
+ getRequirements: () => Promise<Readonly<Transaction<MorphoAuthorizationAction>>[]>;
238
+ buildTx: () => Readonly<Transaction<MarketV1BorrowAction>>;
239
+ };
240
+ repay(params: {
241
+ userAddress: Address;
242
+ positionData: AccrualPosition;
243
+ slippageTolerance?: bigint;
244
+ } & RepayAmountArgs): {
245
+ getRequirements: (reqParams?: {
246
+ useSimplePermit?: boolean;
247
+ }) => Promise<(Requirement | Readonly<Transaction<ERC20ApprovalAction>>)[]>;
248
+ buildTx: (requirementSignature?: RequirementSignature) => Readonly<Transaction<MarketV1RepayAction>>;
249
+ };
250
+ withdrawCollateral({ userAddress, amount, positionData, }: {
251
+ userAddress: Address;
252
+ amount: bigint;
253
+ positionData: AccrualPosition;
254
+ }): {
255
+ buildTx: () => Readonly<Transaction<MarketV1WithdrawCollateralAction>>;
256
+ };
257
+ repayWithdrawCollateral(params: {
258
+ userAddress: Address;
259
+ withdrawAmount: bigint;
260
+ positionData: AccrualPosition;
261
+ slippageTolerance?: bigint;
262
+ } & RepayAmountArgs): {
263
+ getRequirements: (reqParams?: {
264
+ useSimplePermit?: boolean;
265
+ }) => Promise<(Requirement | Readonly<Transaction<ERC20ApprovalAction>> | Readonly<Transaction<MorphoAuthorizationAction>>)[]>;
266
+ buildTx: (requirementSignature?: RequirementSignature) => Readonly<Transaction<MarketV1RepayWithdrawCollateralAction>>;
267
+ };
268
+ supplyCollateralBorrow({ amount, userAddress, positionData, borrowAmount, nativeAmount, slippageTolerance, reallocations, }: {
269
+ userAddress: Address;
270
+ positionData: AccrualPosition;
271
+ borrowAmount: bigint;
272
+ slippageTolerance?: bigint;
273
+ reallocations?: readonly VaultReallocation[];
274
+ } & DepositAmountArgs): {
275
+ getRequirements: (params?: {
276
+ useSimplePermit?: boolean;
277
+ }) => Promise<(Requirement | Readonly<Transaction<ERC20ApprovalAction>> | Readonly<Transaction<MorphoAuthorizationAction>>)[]>;
278
+ buildTx: (requirementSignature?: RequirementSignature) => Readonly<Transaction<MarketV1SupplyCollateralBorrowAction>>;
279
+ };
280
+ getReallocationData({ vaultAddresses, market, block, }: {
281
+ vaultAddresses: readonly Address[];
282
+ market: Market;
283
+ block: MinimalBlock;
284
+ }): Promise<SimulationState>;
285
+ getReallocations({ reallocationData, borrowAmount, options, }: {
286
+ reallocationData: SimulationState;
287
+ borrowAmount: bigint;
288
+ options?: ReallocationComputeOptions;
289
+ }): readonly VaultReallocation[];
290
+ }