@morpho-org/morpho-sdk 3.1.1 → 4.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 (103) hide show
  1. package/README.md +63 -13
  2. package/lib/cjs/actions/{marketV1 → blue}/borrow.d.ts +8 -8
  3. package/lib/cjs/actions/{marketV1 → blue}/borrow.js +8 -8
  4. package/lib/{esm/actions/marketV1 → cjs/actions/blue}/buildReallocationActions.d.ts +1 -1
  5. package/lib/cjs/actions/{marketV1 → blue}/buildReallocationActions.js +1 -1
  6. package/lib/cjs/actions/{marketV1 → blue}/index.d.ts +1 -0
  7. package/lib/cjs/actions/{marketV1 → blue}/index.js +1 -0
  8. package/lib/cjs/actions/blue/refinance.d.ts +117 -0
  9. package/lib/cjs/actions/blue/refinance.js +218 -0
  10. package/lib/{esm/actions/marketV1 → cjs/actions/blue}/repay.d.ts +8 -8
  11. package/lib/cjs/actions/{marketV1 → blue}/repay.js +8 -8
  12. package/lib/{esm/actions/marketV1 → cjs/actions/blue}/repayWithdrawCollateral.d.ts +8 -8
  13. package/lib/cjs/actions/{marketV1 → blue}/repayWithdrawCollateral.js +8 -8
  14. package/lib/{esm/actions/marketV1 → cjs/actions/blue}/supply.d.ts +8 -8
  15. package/lib/cjs/actions/{marketV1 → blue}/supply.js +8 -8
  16. package/lib/cjs/actions/{marketV1 → blue}/supplyCollateral.d.ts +8 -8
  17. package/lib/cjs/actions/{marketV1 → blue}/supplyCollateral.js +8 -8
  18. package/lib/{esm/actions/marketV1 → cjs/actions/blue}/supplyCollateralBorrow.d.ts +8 -8
  19. package/lib/cjs/actions/{marketV1 → blue}/supplyCollateralBorrow.js +8 -8
  20. package/lib/{esm/actions/marketV1 → cjs/actions/blue}/withdraw.d.ts +11 -11
  21. package/lib/cjs/actions/{marketV1 → blue}/withdraw.js +10 -10
  22. package/lib/{esm/actions/marketV1 → cjs/actions/blue}/withdrawCollateral.d.ts +8 -8
  23. package/lib/cjs/actions/{marketV1 → blue}/withdrawCollateral.js +8 -8
  24. package/lib/cjs/actions/index.d.ts +2 -2
  25. package/lib/cjs/actions/index.js +1 -1
  26. package/lib/cjs/actions/requirements/getMorphoAuthorizationRequirement.d.ts +1 -1
  27. package/lib/cjs/actions/requirements/getMorphoAuthorizationRequirement.js +1 -1
  28. package/lib/cjs/client/index.d.ts +0 -1
  29. package/lib/cjs/client/index.js +0 -1
  30. package/lib/cjs/client/morphoViemExtension.d.ts +17 -11
  31. package/lib/cjs/client/morphoViemExtension.js +55 -10
  32. package/lib/cjs/entities/{marketV1/marketV1.d.ts → blue/blue.d.ts} +76 -21
  33. package/lib/cjs/entities/{marketV1/marketV1.js → blue/blue.js} +169 -11
  34. package/lib/cjs/entities/blue/index.d.ts +1 -0
  35. package/lib/cjs/entities/{marketV1 → blue}/index.js +1 -1
  36. package/lib/cjs/entities/index.d.ts +1 -1
  37. package/lib/cjs/entities/index.js +3 -3
  38. package/lib/cjs/entities/reallocationData.d.ts +1 -1
  39. package/lib/cjs/entities/reallocationData.js +1 -1
  40. package/lib/cjs/entities/vaultV2/vaultV2.js +1 -1
  41. package/lib/cjs/helpers/computeReallocations.d.ts +1 -1
  42. package/lib/cjs/helpers/computeReallocations.js +1 -1
  43. package/lib/cjs/helpers/validate.d.ts +2 -2
  44. package/lib/cjs/helpers/validate.js +2 -2
  45. package/lib/cjs/types/action.d.ts +21 -9
  46. package/lib/cjs/types/client.d.ts +5 -4
  47. package/lib/cjs/types/error.d.ts +57 -0
  48. package/lib/cjs/types/error.js +82 -1
  49. package/lib/esm/actions/{marketV1 → blue}/borrow.d.ts +8 -8
  50. package/lib/esm/actions/{marketV1 → blue}/borrow.js +6 -6
  51. package/lib/{cjs/actions/marketV1 → esm/actions/blue}/buildReallocationActions.d.ts +1 -1
  52. package/lib/esm/actions/{marketV1 → blue}/buildReallocationActions.js +1 -1
  53. package/lib/esm/actions/{marketV1 → blue}/index.d.ts +1 -0
  54. package/lib/esm/actions/{marketV1 → blue}/index.js +1 -0
  55. package/lib/esm/actions/blue/refinance.d.ts +117 -0
  56. package/lib/esm/actions/blue/refinance.js +214 -0
  57. package/lib/{cjs/actions/marketV1 → esm/actions/blue}/repay.d.ts +8 -8
  58. package/lib/esm/actions/{marketV1 → blue}/repay.js +6 -6
  59. package/lib/{cjs/actions/marketV1 → esm/actions/blue}/repayWithdrawCollateral.d.ts +8 -8
  60. package/lib/esm/actions/{marketV1 → blue}/repayWithdrawCollateral.js +6 -6
  61. package/lib/{cjs/actions/marketV1 → esm/actions/blue}/supply.d.ts +8 -8
  62. package/lib/esm/actions/{marketV1 → blue}/supply.js +6 -6
  63. package/lib/esm/actions/{marketV1 → blue}/supplyCollateral.d.ts +8 -8
  64. package/lib/esm/actions/{marketV1 → blue}/supplyCollateral.js +6 -6
  65. package/lib/{cjs/actions/marketV1 → esm/actions/blue}/supplyCollateralBorrow.d.ts +8 -8
  66. package/lib/esm/actions/{marketV1 → blue}/supplyCollateralBorrow.js +6 -6
  67. package/lib/{cjs/actions/marketV1 → esm/actions/blue}/withdraw.d.ts +11 -11
  68. package/lib/esm/actions/{marketV1 → blue}/withdraw.js +8 -8
  69. package/lib/{cjs/actions/marketV1 → esm/actions/blue}/withdrawCollateral.d.ts +8 -8
  70. package/lib/esm/actions/{marketV1 → blue}/withdrawCollateral.js +6 -6
  71. package/lib/esm/actions/index.d.ts +2 -2
  72. package/lib/esm/actions/index.js +1 -1
  73. package/lib/esm/actions/requirements/getMorphoAuthorizationRequirement.d.ts +1 -1
  74. package/lib/esm/actions/requirements/getMorphoAuthorizationRequirement.js +1 -1
  75. package/lib/esm/client/index.d.ts +0 -1
  76. package/lib/esm/client/index.js +0 -1
  77. package/lib/esm/client/morphoViemExtension.d.ts +17 -11
  78. package/lib/esm/client/morphoViemExtension.js +55 -10
  79. package/lib/esm/entities/{marketV1/marketV1.d.ts → blue/blue.d.ts} +76 -21
  80. package/lib/esm/entities/{marketV1/marketV1.js → blue/blue.js} +170 -12
  81. package/lib/esm/entities/blue/index.d.ts +1 -0
  82. package/lib/esm/entities/blue/index.js +1 -0
  83. package/lib/esm/entities/index.d.ts +1 -1
  84. package/lib/esm/entities/index.js +1 -1
  85. package/lib/esm/entities/reallocationData.d.ts +1 -1
  86. package/lib/esm/entities/reallocationData.js +1 -1
  87. package/lib/esm/entities/vaultV2/vaultV2.js +1 -1
  88. package/lib/esm/helpers/computeReallocations.d.ts +1 -1
  89. package/lib/esm/helpers/computeReallocations.js +1 -1
  90. package/lib/esm/helpers/validate.d.ts +2 -2
  91. package/lib/esm/helpers/validate.js +2 -2
  92. package/lib/esm/types/action.d.ts +21 -9
  93. package/lib/esm/types/client.d.ts +5 -4
  94. package/lib/esm/types/error.d.ts +57 -0
  95. package/lib/esm/types/error.js +72 -0
  96. package/package.json +6 -6
  97. package/lib/cjs/client/morphoClient.d.ts +0 -152
  98. package/lib/cjs/client/morphoClient.js +0 -162
  99. package/lib/cjs/entities/marketV1/index.d.ts +0 -1
  100. package/lib/esm/client/morphoClient.d.ts +0 -152
  101. package/lib/esm/client/morphoClient.js +0 -158
  102. package/lib/esm/entities/marketV1/index.d.ts +0 -1
  103. package/lib/esm/entities/marketV1/index.js +0 -1
@@ -140,7 +140,7 @@ export declare class ReallocationData implements InputReallocationData {
140
140
  * }).extend(morphoViemExtension());
141
141
  *
142
142
  * const marketParams = markets[mainnet.id].usdc_wbtc;
143
- * const market = client.morpho.marketV1(marketParams, mainnet.id);
143
+ * const market = client.morpho.blue(marketParams, mainnet.id);
144
144
  * const block = await client.getBlock();
145
145
  * const reallocationData = await market.getReallocationData({
146
146
  * vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
@@ -229,7 +229,7 @@ export class ReallocationData {
229
229
  * }).extend(morphoViemExtension());
230
230
  *
231
231
  * const marketParams = markets[mainnet.id].usdc_wbtc;
232
- * const market = client.morpho.marketV1(marketParams, mainnet.id);
232
+ * const market = client.morpho.blue(marketParams, mainnet.id);
233
233
  * const block = await client.getBlock();
234
234
  * const reallocationData = await market.getReallocationData({
235
235
  * vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
@@ -59,7 +59,7 @@ export class MorphoVaultV2 {
59
59
  }
60
60
  const totalAssets = amount + (nativeAmount ?? 0n);
61
61
  // Accrue interest forward to bound the on-chain share price at execution.
62
- // Mirrors marketV1 repay's 2h forward-accrual buffer.
62
+ // Mirrors blue repay's 2h forward-accrual buffer.
63
63
  const accrualTimestamp = MathLib.max(Time.timestamp(), vaultData.lastUpdate) + Time.s.from.h(2n);
64
64
  const { vault: accruedVault } = vaultData.accrueInterest(accrualTimestamp);
65
65
  const shares = accruedVault.toShares(totalAssets);
@@ -42,7 +42,7 @@ import { type ReallocationComputeOptions, type VaultReallocation } from "../type
42
42
  *
43
43
  * const userAddress = "0x000000000000000000000000000000000000dEaD";
44
44
  * const marketParams = markets[mainnet.id].usdc_wbtc;
45
- * const market = client.morpho.marketV1(marketParams, mainnet.id);
45
+ * const market = client.morpho.blue(marketParams, mainnet.id);
46
46
  * const block = await client.getBlock();
47
47
  * const reallocationData = await market.getReallocationData({
48
48
  * vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
@@ -95,7 +95,7 @@ const capVaultWithdrawals = (withdrawals, assets) => {
95
95
  *
96
96
  * const userAddress = "0x000000000000000000000000000000000000dEaD";
97
97
  * const marketParams = markets[mainnet.id].usdc_wbtc;
98
- * const market = client.morpho.marketV1(marketParams, mainnet.id);
98
+ * const market = client.morpho.blue(marketParams, mainnet.id);
99
99
  * const block = await client.getBlock();
100
100
  * const reallocationData = await market.getReallocationData({
101
101
  * vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
@@ -122,8 +122,8 @@ export declare const validateRepayShares: (params: {
122
122
  marketId: MarketId;
123
123
  }) => void;
124
124
  /**
125
- * Validates the common repay input parameters shared by `marketV1Repay`
126
- * and `marketV1RepayWithdrawCollateral`.
125
+ * Validates the common repay input parameters shared by `blueRepay`
126
+ * and `blueRepayWithdrawCollateral`.
127
127
  *
128
128
  * @param params - Validation parameters.
129
129
  * @param params.assets - Repay assets amount (0n when repaying by shares).
@@ -200,8 +200,8 @@ export const validateRepayShares = (params) => {
200
200
  }
201
201
  };
202
202
  /**
203
- * Validates the common repay input parameters shared by `marketV1Repay`
204
- * and `marketV1RepayWithdrawCollateral`.
203
+ * Validates the common repay input parameters shared by `blueRepay`
204
+ * and `blueRepayWithdrawCollateral`.
205
205
  *
206
206
  * @param params - Validation parameters.
207
207
  * @param params.assets - Repay assets amount (0n when repaying by shares).
@@ -81,7 +81,7 @@ export interface VaultV1MigrateToV2Action extends BaseAction<"vaultV1MigrateToV2
81
81
  recipient: Address;
82
82
  }> {
83
83
  }
84
- export interface MarketV1SupplyAction extends BaseAction<"marketV1Supply", {
84
+ export interface BlueSupplyAction extends BaseAction<"blueSupply", {
85
85
  market: Hex;
86
86
  amount: bigint;
87
87
  onBehalf: Address;
@@ -89,7 +89,7 @@ export interface MarketV1SupplyAction extends BaseAction<"marketV1Supply", {
89
89
  nativeAmount?: bigint;
90
90
  }> {
91
91
  }
92
- export interface MarketV1WithdrawAction extends BaseAction<"marketV1Withdraw", {
92
+ export interface BlueWithdrawAction extends BaseAction<"blueWithdraw", {
93
93
  market: Hex;
94
94
  assets: bigint;
95
95
  shares: bigint;
@@ -98,14 +98,14 @@ export interface MarketV1WithdrawAction extends BaseAction<"marketV1Withdraw", {
98
98
  reallocationFee: bigint;
99
99
  }> {
100
100
  }
101
- export interface MarketV1SupplyCollateralAction extends BaseAction<"marketV1SupplyCollateral", {
101
+ export interface BlueSupplyCollateralAction extends BaseAction<"blueSupplyCollateral", {
102
102
  market: Hex;
103
103
  amount: bigint;
104
104
  onBehalf: Address;
105
105
  nativeAmount?: bigint;
106
106
  }> {
107
107
  }
108
- export interface MarketV1BorrowAction extends BaseAction<"marketV1Borrow", {
108
+ export interface BlueBorrowAction extends BaseAction<"blueBorrow", {
109
109
  market: Hex;
110
110
  amount: bigint;
111
111
  receiver: Address;
@@ -113,7 +113,7 @@ export interface MarketV1BorrowAction extends BaseAction<"marketV1Borrow", {
113
113
  reallocationFee: bigint;
114
114
  }> {
115
115
  }
116
- export interface MarketV1SupplyCollateralBorrowAction extends BaseAction<"marketV1SupplyCollateralBorrow", {
116
+ export interface BlueSupplyCollateralBorrowAction extends BaseAction<"blueSupplyCollateralBorrow", {
117
117
  market: Hex;
118
118
  collateralAmount: bigint;
119
119
  borrowAmount: bigint;
@@ -124,7 +124,7 @@ export interface MarketV1SupplyCollateralBorrowAction extends BaseAction<"market
124
124
  reallocationFee: bigint;
125
125
  }> {
126
126
  }
127
- export interface MarketV1RepayAction extends BaseAction<"marketV1Repay", {
127
+ export interface BlueRepayAction extends BaseAction<"blueRepay", {
128
128
  market: Hex;
129
129
  assets: bigint;
130
130
  shares: bigint;
@@ -134,14 +134,14 @@ export interface MarketV1RepayAction extends BaseAction<"marketV1Repay", {
134
134
  maxSharePrice: bigint;
135
135
  }> {
136
136
  }
137
- export interface MarketV1WithdrawCollateralAction extends BaseAction<"marketV1WithdrawCollateral", {
137
+ export interface BlueWithdrawCollateralAction extends BaseAction<"blueWithdrawCollateral", {
138
138
  market: Hex;
139
139
  amount: bigint;
140
140
  onBehalf: Address;
141
141
  receiver: Address;
142
142
  }> {
143
143
  }
144
- export interface MarketV1RepayWithdrawCollateralAction extends BaseAction<"marketV1RepayWithdrawCollateral", {
144
+ export interface BlueRepayWithdrawCollateralAction extends BaseAction<"blueRepayWithdrawCollateral", {
145
145
  market: Hex;
146
146
  repayAssets: bigint;
147
147
  repayShares: bigint;
@@ -152,6 +152,18 @@ export interface MarketV1RepayWithdrawCollateralAction extends BaseAction<"marke
152
152
  receiver: Address;
153
153
  }> {
154
154
  }
155
+ export interface BlueRefinanceAction extends BaseAction<"blueRefinance", {
156
+ readonly sourceMarket: Hex;
157
+ readonly targetMarket: Hex;
158
+ readonly collateralAmount: bigint;
159
+ readonly borrowAssets: bigint;
160
+ readonly borrowShares: bigint;
161
+ readonly minBorrowSharePrice: bigint;
162
+ readonly maxRepaySharePrice: bigint;
163
+ readonly user: Address;
164
+ readonly reallocationFee: bigint;
165
+ }> {
166
+ }
155
167
  /**
156
168
  * Enforces that exactly one of `assets` / `shares` is provided.
157
169
  *
@@ -173,7 +185,7 @@ export interface MorphoAuthorizationAction extends BaseAction<"morphoAuthorizati
173
185
  isAuthorized: boolean;
174
186
  }> {
175
187
  }
176
- export type TransactionAction = ERC20ApprovalAction | VaultV2DepositAction | VaultV2WithdrawAction | VaultV2RedeemAction | VaultV2ForceWithdrawAction | VaultV2ForceRedeemAction | VaultV1DepositAction | VaultV1WithdrawAction | VaultV1RedeemAction | VaultV1MigrateToV2Action | MarketV1SupplyAction | MarketV1WithdrawAction | MarketV1SupplyCollateralAction | MarketV1BorrowAction | MarketV1SupplyCollateralBorrowAction | MarketV1RepayAction | MarketV1WithdrawCollateralAction | MarketV1RepayWithdrawCollateralAction | MorphoAuthorizationAction;
188
+ export type TransactionAction = ERC20ApprovalAction | VaultV2DepositAction | VaultV2WithdrawAction | VaultV2RedeemAction | VaultV2ForceWithdrawAction | VaultV2ForceRedeemAction | VaultV1DepositAction | VaultV1WithdrawAction | VaultV1RedeemAction | VaultV1MigrateToV2Action | BlueSupplyAction | BlueWithdrawAction | BlueSupplyCollateralAction | BlueBorrowAction | BlueSupplyCollateralBorrowAction | BlueRepayAction | BlueWithdrawCollateralAction | BlueRepayWithdrawCollateralAction | BlueRefinanceAction | MorphoAuthorizationAction;
177
189
  export interface Transaction<TAction extends BaseAction = TransactionAction> {
178
190
  readonly to: Address;
179
191
  readonly value: bigint;
@@ -1,10 +1,11 @@
1
1
  import type { MarketParams } from "@morpho-org/blue-sdk";
2
2
  import type { Address, Client } from "viem";
3
- import type { MarketV1Actions, VaultV1Actions, VaultV2Actions } from "../actions/index.js";
3
+ import type { BlueActions, VaultV1Actions, VaultV2Actions } from "../actions/index.js";
4
4
  import type { Metadata } from "./index.js";
5
5
  /**
6
- * Structural contract every concrete `MorphoClient` implementation satisfies. Carries the viem
7
- * client, the resolved options bag, and the three entity-factory methods the SDK exposes.
6
+ * Structural contract the `morpho` namespace satisfies — the object exposed under `client.morpho`
7
+ * once a viem client is extended with {@link morphoViemExtension}. Carries the viem client, the
8
+ * resolved options bag, and the three entity-factory methods the SDK exposes.
8
9
  */
9
10
  export interface MorphoClientType {
10
11
  readonly viemClient: Client;
@@ -15,5 +16,5 @@ export interface MorphoClientType {
15
16
  };
16
17
  vaultV1: (vault: Address, chainId: number) => VaultV1Actions;
17
18
  vaultV2: (vault: Address, chainId: number) => VaultV2Actions;
18
- marketV1: (marketParams: MarketParams, chainId: number) => MarketV1Actions;
19
+ blue: (marketParams: MarketParams, chainId: number) => BlueActions;
19
20
  }
@@ -375,3 +375,60 @@ export declare class VaultAssetMismatchError extends Error {
375
375
  export declare class NegativeMinSharePriceError extends Error {
376
376
  constructor(vault: Address);
377
377
  }
378
+ /** Thrown when a refinance's `borrowShares` argument is negative. */
379
+ export declare class NegativeBorrowSharesError extends Error {
380
+ constructor(market: string);
381
+ }
382
+ /** Thrown when a refinance specifies both `borrowAssets` and `borrowShares` as non-zero (modes are mutually exclusive). */
383
+ export declare class BorrowAmountAndSharesExclusiveError extends Error {
384
+ constructor(market: string);
385
+ }
386
+ /** Thrown when a refinance's `maxRepaySharePrice` slippage bound is negative. */
387
+ export declare class NegativeMaxRepaySharePriceError extends Error {
388
+ constructor(market: string);
389
+ }
390
+ /** Thrown when a refinance has identical source and target market ids (a refinance to the same market is a costly no-op). */
391
+ export declare class RefinanceSameMarketError extends Error {
392
+ constructor(market: string);
393
+ }
394
+ /** Thrown when a refinance's source and target markets do not share the same loan or collateral token. */
395
+ export declare class RefinanceTokenMismatchError extends Error {
396
+ constructor(sourceMarket: string, targetMarket: string);
397
+ }
398
+ /** Thrown when a refinance's `collateralAmount` exceeds the source position's available collateral. */
399
+ export declare class RefinanceExceedsCollateralError extends Error {
400
+ readonly market: string;
401
+ readonly requested: bigint;
402
+ readonly available: bigint;
403
+ constructor(params: {
404
+ market: string;
405
+ requested: bigint;
406
+ available: bigint;
407
+ });
408
+ }
409
+ /** Thrown when a refinance's `borrowShares` exceeds the source position's outstanding borrow shares. */
410
+ export declare class RefinanceExceedsBorrowSharesError extends Error {
411
+ readonly market: string;
412
+ readonly requested: bigint;
413
+ readonly available: bigint;
414
+ constructor(params: {
415
+ market: string;
416
+ requested: bigint;
417
+ available: bigint;
418
+ });
419
+ }
420
+ /** Thrown when a refinance's `borrowAssets` exceeds the source position's outstanding debt assets. */
421
+ export declare class RefinanceExceedsBorrowAssetsError extends Error {
422
+ readonly market: string;
423
+ readonly requested: bigint;
424
+ readonly available: bigint;
425
+ constructor(params: {
426
+ market: string;
427
+ requested: bigint;
428
+ available: bigint;
429
+ });
430
+ }
431
+ /** Thrown when a refinance in shares mode (`borrowShares > 0n`) omits the `borrowAssets` overshoot for the target borrow leg. */
432
+ export declare class RefinanceSharesMissingBorrowAssetsError extends Error {
433
+ constructor(market: string);
434
+ }
@@ -470,3 +470,75 @@ export class NegativeMinSharePriceError extends Error {
470
470
  super(`Min share price must be non-negative for vault: ${vault}`);
471
471
  }
472
472
  }
473
+ /** Thrown when a refinance's `borrowShares` argument is negative. */
474
+ export class NegativeBorrowSharesError extends Error {
475
+ constructor(market) {
476
+ super(`Borrow shares must be non-negative for market: ${market}`);
477
+ }
478
+ }
479
+ /** Thrown when a refinance specifies both `borrowAssets` and `borrowShares` as non-zero (modes are mutually exclusive). */
480
+ export class BorrowAmountAndSharesExclusiveError extends Error {
481
+ constructor(market) {
482
+ super(`Exactly one of borrowAssets or borrowShares must be non-zero for market: ${market}. Both were provided.`);
483
+ }
484
+ }
485
+ /** Thrown when a refinance's `maxRepaySharePrice` slippage bound is negative. */
486
+ export class NegativeMaxRepaySharePriceError extends Error {
487
+ constructor(market) {
488
+ super(`Max repay share price must be non-negative for market: ${market}`);
489
+ }
490
+ }
491
+ /** Thrown when a refinance has identical source and target market ids (a refinance to the same market is a costly no-op). */
492
+ export class RefinanceSameMarketError extends Error {
493
+ constructor(market) {
494
+ super(`Refinance source and target market ${market} are identical. Refinance requires a different target market.`);
495
+ }
496
+ }
497
+ /** Thrown when a refinance's source and target markets do not share the same loan or collateral token. */
498
+ export class RefinanceTokenMismatchError extends Error {
499
+ constructor(sourceMarket, targetMarket) {
500
+ super(`Refinance source market ${sourceMarket} and target market ${targetMarket} must share the same loanToken and collateralToken.`);
501
+ }
502
+ }
503
+ /** Thrown when a refinance's `collateralAmount` exceeds the source position's available collateral. */
504
+ export class RefinanceExceedsCollateralError extends Error {
505
+ market;
506
+ requested;
507
+ available;
508
+ constructor(params) {
509
+ super(`Refinance collateral amount ${params.requested} exceeds available collateral ${params.available} for market: ${params.market}`);
510
+ this.market = params.market;
511
+ this.requested = params.requested;
512
+ this.available = params.available;
513
+ }
514
+ }
515
+ /** Thrown when a refinance's `borrowShares` exceeds the source position's outstanding borrow shares. */
516
+ export class RefinanceExceedsBorrowSharesError extends Error {
517
+ market;
518
+ requested;
519
+ available;
520
+ constructor(params) {
521
+ super(`Refinance borrow shares ${params.requested} exceed outstanding borrow shares ${params.available} for market: ${params.market}`);
522
+ this.market = params.market;
523
+ this.requested = params.requested;
524
+ this.available = params.available;
525
+ }
526
+ }
527
+ /** Thrown when a refinance's `borrowAssets` exceeds the source position's outstanding debt assets. */
528
+ export class RefinanceExceedsBorrowAssetsError extends Error {
529
+ market;
530
+ requested;
531
+ available;
532
+ constructor(params) {
533
+ super(`Refinance borrow assets ${params.requested} exceed outstanding debt assets ${params.available} for market: ${params.market}`);
534
+ this.market = params.market;
535
+ this.requested = params.requested;
536
+ this.available = params.available;
537
+ }
538
+ }
539
+ /** Thrown when a refinance in shares mode (`borrowShares > 0n`) omits the `borrowAssets` overshoot for the target borrow leg. */
540
+ export class RefinanceSharesMissingBorrowAssetsError extends Error {
541
+ constructor(market) {
542
+ super(`Refinance shares mode requires a positive borrowAssets overshoot for the target borrow leg (market: ${market}).`);
543
+ }
544
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@morpho-org/morpho-sdk",
3
3
  "description": "Abstraction layer for Morpho's complexity.",
4
- "version": "3.1.1",
4
+ "version": "4.0.0",
5
5
  "author": "Morpho Association <contact@morpho.org>",
6
6
  "contributors": [
7
7
  "Foulks-Plb <https://x.com/FoulkPlb>"
@@ -137,8 +137,8 @@
137
137
  ],
138
138
  "dependencies": {
139
139
  "zod": "^4.4.3",
140
- "@morpho-org/blue-sdk": "^6.1.0",
141
- "@morpho-org/blue-sdk-viem": "^5.1.0",
140
+ "@morpho-org/blue-sdk": "^6.2.0",
141
+ "@morpho-org/blue-sdk-viem": "^5.1.1",
142
142
  "@morpho-org/morpho-ts": "^2.6.0"
143
143
  },
144
144
  "peerDependencies": {
@@ -148,10 +148,10 @@
148
148
  "dotenv": "^17.4.2",
149
149
  "fast-check": "^4.8.0",
150
150
  "typescript": "^6.0.3",
151
- "viem": "^2.50.4",
152
- "vitest": "^4.1.7",
153
- "@morpho-org/bundler-sdk-viem": "^5.0.3",
151
+ "viem": "2.52.2",
152
+ "vitest": "^4.1.8",
154
153
  "@morpho-org/simulation-sdk": "^4.0.3",
154
+ "@morpho-org/bundler-sdk-viem": "^5.0.3",
155
155
  "@morpho-org/test": "^2.8.1"
156
156
  },
157
157
  "scripts": {
@@ -1,152 +0,0 @@
1
- import { type MarketParams } from "@morpho-org/blue-sdk";
2
- import type { Address, Client } from "viem";
3
- import { MorphoMarketV1, MorphoVaultV1, MorphoVaultV2 } from "../entities/index.js";
4
- import { type Metadata, type MorphoClientType } from "../types/index.js";
5
- /**
6
- * Stateless entry point of the SDK. Wraps a viem `Client` plus a frozen options bag and exposes
7
- * factory methods for the protocol entities.
8
- *
9
- * Holds no state beyond configuration: no cache, no `init()`, no warm-up. Each factory call
10
- * (`vaultV1`, `vaultV2`, `marketV1`) returns a fresh entity bound to this client.
11
- *
12
- * @deprecated Use {@link morphoViemExtension} instead. Extending your viem client keeps a single
13
- * transport / chain / account on the integrator side and exposes the same factories under
14
- * `client.morpho`, so reads and writes share one client. `MorphoClient` will be removed in the
15
- * next major release.
16
- *
17
- * @example
18
- * ```ts
19
- * // Before — wrapping a viem client in MorphoClient:
20
- * import { createPublicClient, http } from "viem";
21
- * import { mainnet } from "viem/chains";
22
- * import { MorphoClient } from "@morpho-org/morpho-sdk";
23
- *
24
- * const publicClient = createPublicClient({ chain: mainnet, transport: http() });
25
- * const morpho = new MorphoClient(publicClient);
26
- *
27
- * const vault = morpho.vaultV1(vaultAddress, 1);
28
- * const vaultData = await vault.getData();
29
- * ```
30
- *
31
- * @example
32
- * ```ts
33
- * // After — extend the public client directly:
34
- * import { createPublicClient, http } from "viem";
35
- * import { mainnet } from "viem/chains";
36
- * import { morphoViemExtension } from "@morpho-org/morpho-sdk";
37
- *
38
- * const client = createPublicClient({ chain: mainnet, transport: http() }).extend(
39
- * morphoViemExtension(),
40
- * );
41
- *
42
- * // Native viem reads and Morpho factories share the same client:
43
- * const block = await client.getBlockNumber();
44
- * const vault = client.morpho.vaultV1(vaultAddress, 1);
45
- * const vaultData = await vault.getData();
46
- * ```
47
- */
48
- export declare class MorphoClient implements MorphoClientType {
49
- readonly viemClient: Client;
50
- readonly _options?: {
51
- readonly supportSignature?: boolean;
52
- readonly supportDeployless?: boolean;
53
- readonly metadata?: Metadata;
54
- } | undefined;
55
- /** SDK-wide options resolved from the constructor's `_options` argument. */
56
- readonly options: {
57
- readonly supportSignature: boolean;
58
- readonly supportDeployless?: boolean;
59
- readonly metadata?: Metadata;
60
- };
61
- /**
62
- * @param viemClient - Connected viem `Client`. Public reads use it as-is; signature flows use
63
- * it as a `WalletClient` and require `account` to be set.
64
- * @param _options - SDK-wide options.
65
- * @param _options.supportSignature - Whether the integrator can collect EIP-712 signatures for
66
- * permit / permit2. Defaults to `false` (classic approvals only).
67
- * @param _options.supportDeployless - Whether entity fetchers may use deployless multicall.
68
- * @param _options.metadata - Optional analytics metadata applied to every transaction this
69
- * client builds.
70
- * @deprecated Prefer {@link morphoViemExtension} so the SDK rides on top of a viem client the
71
- * integrator already owns (public or wallet). Will be removed in the next major release.
72
- * @example
73
- * ```ts
74
- * import { createPublicClient, http } from "viem";
75
- * import { mainnet } from "viem/chains";
76
- * import { morphoViemExtension } from "@morpho-org/morpho-sdk";
77
- *
78
- * const client = createPublicClient({ chain: mainnet, transport: http() }).extend(
79
- * morphoViemExtension({ supportSignature: true }),
80
- * );
81
- *
82
- * const vault = client.morpho.vaultV1(vaultAddress, 1);
83
- * ```
84
- */
85
- constructor(viemClient: Client, _options?: {
86
- readonly supportSignature?: boolean;
87
- readonly supportDeployless?: boolean;
88
- readonly metadata?: Metadata;
89
- } | undefined);
90
- /**
91
- * Returns a `MorphoVaultV1` (MetaMorpho) entity bound to this client.
92
- *
93
- * @param vault - VaultV1 address.
94
- * @param chainId - Chain the vault lives on.
95
- * @returns A fresh `MorphoVaultV1` entity.
96
- * @example
97
- * ```ts
98
- * const vault = client.vaultV1(vaultAddress, 1);
99
- * const vaultData = await vault.getData();
100
- * const { buildTx } = vault.deposit({
101
- * amount: 1_000_000n,
102
- * userAddress: depositor,
103
- * vaultData,
104
- * });
105
- * const tx = buildTx();
106
- * ```
107
- */
108
- vaultV1(vault: Address, chainId: number): MorphoVaultV1;
109
- /**
110
- * Returns a `MorphoVaultV2` entity bound to this client.
111
- *
112
- * @param vault - VaultV2 address.
113
- * @param chainId - Chain the vault lives on.
114
- * @returns A fresh `MorphoVaultV2` entity.
115
- * @example
116
- * ```ts
117
- * const vault = client.vaultV2(vaultAddress, 1);
118
- * const vaultData = await vault.getData();
119
- * const { buildTx } = vault.deposit({
120
- * amount: 1_000_000n,
121
- * userAddress: depositor,
122
- * vaultData,
123
- * });
124
- * const tx = buildTx();
125
- * ```
126
- */
127
- vaultV2(vault: Address, chainId: number): MorphoVaultV2;
128
- /**
129
- * Returns a `MorphoMarketV1` entity bound to this client. Validates that the supplied
130
- * `marketParams.id` matches the hash of the other params, so a hand-rolled or agent-written
131
- * `MarketParams` cannot point at the wrong market silently.
132
- *
133
- * @param marketParams - Market params (`loanToken`, `collateralToken`, `oracle`, `irm`, `lltv`,
134
- * `id`).
135
- * @param chainId - Chain the market lives on.
136
- * @returns A fresh `MorphoMarketV1` entity.
137
- * @throws {MarketIdMismatchError} when `marketParams.id` does not equal the hash derived from
138
- * the other fields.
139
- * @example
140
- * ```ts
141
- * const market = client.marketV1(marketParams, 1);
142
- * const positionData = await market.getPositionData(borrower);
143
- * const { buildTx } = market.borrow({
144
- * userAddress: borrower,
145
- * amount: 1_000_000n,
146
- * positionData,
147
- * });
148
- * const tx = buildTx();
149
- * ```
150
- */
151
- marketV1(marketParams: MarketParams, chainId: number): MorphoMarketV1;
152
- }