@morpho-org/morpho-sdk 3.0.0 → 3.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/README.md +69 -3
  2. package/lib/cjs/actions/marketV1/index.d.ts +2 -0
  3. package/lib/cjs/actions/marketV1/index.js +2 -0
  4. package/lib/cjs/actions/marketV1/supply.d.ts +69 -0
  5. package/lib/cjs/actions/marketV1/supply.js +128 -0
  6. package/lib/cjs/actions/marketV1/supplyCollateral.d.ts +1 -1
  7. package/lib/cjs/actions/marketV1/supplyCollateral.js +2 -2
  8. package/lib/cjs/actions/marketV1/supplyCollateralBorrow.d.ts +1 -1
  9. package/lib/cjs/actions/marketV1/supplyCollateralBorrow.js +2 -2
  10. package/lib/cjs/actions/marketV1/withdraw.d.ts +83 -0
  11. package/lib/cjs/actions/marketV1/withdraw.js +110 -0
  12. package/lib/cjs/bundler/actions.d.ts +86 -4
  13. package/lib/cjs/bundler/actions.js +143 -8
  14. package/lib/cjs/bundler/types.d.ts +23 -4
  15. package/lib/cjs/entities/marketV1/marketV1.d.ts +121 -18
  16. package/lib/cjs/entities/marketV1/marketV1.js +144 -12
  17. package/lib/cjs/entities/reallocationData.js +3 -0
  18. package/lib/cjs/helpers/computeReallocations.d.ts +17 -6
  19. package/lib/cjs/helpers/computeReallocations.js +44 -11
  20. package/lib/cjs/helpers/index.d.ts +2 -2
  21. package/lib/cjs/helpers/index.js +6 -2
  22. package/lib/cjs/helpers/slippage.d.ts +57 -0
  23. package/lib/cjs/helpers/slippage.js +82 -0
  24. package/lib/cjs/helpers/validate.d.ts +38 -7
  25. package/lib/cjs/helpers/validate.js +54 -11
  26. package/lib/cjs/types/action.d.ts +27 -6
  27. package/lib/cjs/types/error.d.ts +68 -6
  28. package/lib/cjs/types/error.js +85 -12
  29. package/lib/cjs/utils.d.ts +1 -1
  30. package/lib/cjs/utils.js +2 -2
  31. package/lib/esm/actions/marketV1/index.d.ts +2 -0
  32. package/lib/esm/actions/marketV1/index.js +2 -0
  33. package/lib/esm/actions/marketV1/supply.d.ts +69 -0
  34. package/lib/esm/actions/marketV1/supply.js +124 -0
  35. package/lib/esm/actions/marketV1/supplyCollateral.d.ts +1 -1
  36. package/lib/esm/actions/marketV1/supplyCollateral.js +3 -3
  37. package/lib/esm/actions/marketV1/supplyCollateralBorrow.d.ts +1 -1
  38. package/lib/esm/actions/marketV1/supplyCollateralBorrow.js +3 -3
  39. package/lib/esm/actions/marketV1/withdraw.d.ts +83 -0
  40. package/lib/esm/actions/marketV1/withdraw.js +106 -0
  41. package/lib/esm/bundler/actions.d.ts +86 -4
  42. package/lib/esm/bundler/actions.js +143 -8
  43. package/lib/esm/bundler/types.d.ts +23 -4
  44. package/lib/esm/entities/marketV1/marketV1.d.ts +121 -18
  45. package/lib/esm/entities/marketV1/marketV1.js +147 -15
  46. package/lib/esm/entities/reallocationData.js +3 -0
  47. package/lib/esm/helpers/computeReallocations.d.ts +17 -6
  48. package/lib/esm/helpers/computeReallocations.js +45 -12
  49. package/lib/esm/helpers/index.d.ts +2 -2
  50. package/lib/esm/helpers/index.js +2 -2
  51. package/lib/esm/helpers/slippage.d.ts +57 -0
  52. package/lib/esm/helpers/slippage.js +80 -0
  53. package/lib/esm/helpers/validate.d.ts +38 -7
  54. package/lib/esm/helpers/validate.js +51 -10
  55. package/lib/esm/types/action.d.ts +27 -6
  56. package/lib/esm/types/error.d.ts +68 -6
  57. package/lib/esm/types/error.js +73 -9
  58. package/lib/esm/utils.d.ts +1 -1
  59. package/lib/esm/utils.js +1 -1
  60. package/package.json +14 -10
@@ -372,6 +372,48 @@ export declare namespace BundlerAction {
372
372
  * ```
373
373
  */
374
374
  function morphoSupplyCollateral(chainId: number, market: InputMarketParams, assets: bigint, onBehalf: Address, callbackCalls: BundlerCall[], skipRevert?: boolean): BundlerCall[];
375
+ /**
376
+ * Encodes a GeneralAdapter1 Morpho Blue supply call.
377
+ *
378
+ * @param chainId - Chain where the action will execute.
379
+ * @param market - Morpho Blue market parameters.
380
+ * @param assets - Supply asset amount.
381
+ * @param shares - Supply share amount.
382
+ * @param slippageAmount - Slippage guard amount (max share price in RAY for share-price slippage).
383
+ * @param onBehalf - Account receiving the supply position.
384
+ * @param callbackCalls - Calls executed in Morpho's callback.
385
+ * @param skipRevert - Whether Bundler3 should tolerate a revert.
386
+ * @returns Encoded Bundler3 calls.
387
+ *
388
+ * @example
389
+ * ```ts
390
+ * import { BundlerAction } from "@morpho-org/morpho-sdk/bundler";
391
+ *
392
+ * const loanToken = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
393
+ * const collateralToken = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
394
+ * const oracle = "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419";
395
+ * const irm = "0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC";
396
+ * const onBehalf = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
397
+ * const marketParams = {
398
+ * loanToken,
399
+ * collateralToken,
400
+ * oracle,
401
+ * irm,
402
+ * lltv: 860_000000000000000000n,
403
+ * };
404
+ *
405
+ * const calls = BundlerAction.morphoSupply(
406
+ * 1,
407
+ * marketParams,
408
+ * 100n,
409
+ * 0n,
410
+ * 1_010000000000000000000000000n,
411
+ * onBehalf,
412
+ * [],
413
+ * );
414
+ * ```
415
+ */
416
+ function morphoSupply(chainId: number, market: InputMarketParams, assets: bigint, shares: bigint, slippageAmount: bigint, onBehalf: Address, callbackCalls: BundlerCall[], skipRevert?: boolean): BundlerCall[];
375
417
  /**
376
418
  * Encodes a GeneralAdapter1 Morpho Blue borrow call.
377
419
  *
@@ -379,7 +421,7 @@ export declare namespace BundlerAction {
379
421
  * @param market - Morpho Blue market parameters.
380
422
  * @param assets - Borrow asset amount.
381
423
  * @param shares - Borrow share amount.
382
- * @param slippageAmount - Slippage guard amount.
424
+ * @param minSharePrice - Minimum amount of borrowed assets per borrow share minted, scaled by 1e27.
383
425
  * @param receiver - Recipient of borrowed assets.
384
426
  * @param skipRevert - Whether Bundler3 should tolerate a revert.
385
427
  * @returns Encoded Bundler3 calls.
@@ -411,7 +453,7 @@ export declare namespace BundlerAction {
411
453
  * );
412
454
  * ```
413
455
  */
414
- function morphoBorrow(chainId: number, market: InputMarketParams, assets: bigint, shares: bigint, slippageAmount: bigint, receiver: Address, skipRevert?: boolean): BundlerCall[];
456
+ function morphoBorrow(chainId: number, market: InputMarketParams, assets: bigint, shares: bigint, minSharePrice: bigint, receiver: Address, skipRevert?: boolean): BundlerCall[];
415
457
  /**
416
458
  * Encodes a GeneralAdapter1 Morpho Blue repay call.
417
459
  *
@@ -419,7 +461,7 @@ export declare namespace BundlerAction {
419
461
  * @param market - Morpho Blue market parameters.
420
462
  * @param assets - Repay asset amount.
421
463
  * @param shares - Repay share amount.
422
- * @param slippageAmount - Slippage guard amount.
464
+ * @param maxSharePrice - Maximum amount of repaid assets per borrow share redeemed, scaled by 1e27.
423
465
  * @param onBehalf - Account whose borrow is repaid.
424
466
  * @param callbackCalls - Calls executed in Morpho's callback.
425
467
  * @param skipRevert - Whether Bundler3 should tolerate a revert.
@@ -453,7 +495,47 @@ export declare namespace BundlerAction {
453
495
  * );
454
496
  * ```
455
497
  */
456
- function morphoRepay(chainId: number, market: InputMarketParams, assets: bigint, shares: bigint, slippageAmount: bigint, onBehalf: Address, callbackCalls: BundlerCall[], skipRevert?: boolean): BundlerCall[];
498
+ function morphoRepay(chainId: number, market: InputMarketParams, assets: bigint, shares: bigint, maxSharePrice: bigint, onBehalf: Address, callbackCalls: BundlerCall[], skipRevert?: boolean): BundlerCall[];
499
+ /**
500
+ * Encodes a GeneralAdapter1 Morpho Blue withdraw call.
501
+ *
502
+ * @param chainId - Chain where the action will execute.
503
+ * @param market - Morpho Blue market parameters.
504
+ * @param assets - Withdraw asset amount.
505
+ * @param shares - Withdraw share amount.
506
+ * @param slippageAmount - Slippage guard amount (min share price in RAY for share-price slippage).
507
+ * @param receiver - Recipient of withdrawn assets.
508
+ * @param skipRevert - Whether Bundler3 should tolerate a revert.
509
+ * @returns Encoded Bundler3 calls.
510
+ *
511
+ * @example
512
+ * ```ts
513
+ * import { BundlerAction } from "@morpho-org/morpho-sdk/bundler";
514
+ *
515
+ * const loanToken = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
516
+ * const collateralToken = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
517
+ * const oracle = "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419";
518
+ * const irm = "0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC";
519
+ * const receiver = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
520
+ * const marketParams = {
521
+ * loanToken,
522
+ * collateralToken,
523
+ * oracle,
524
+ * irm,
525
+ * lltv: 860_000000000000000000n,
526
+ * };
527
+ *
528
+ * const calls = BundlerAction.morphoWithdraw(
529
+ * 1,
530
+ * marketParams,
531
+ * 100n,
532
+ * 0n,
533
+ * 990000000000000000000000000n,
534
+ * receiver,
535
+ * );
536
+ * ```
537
+ */
538
+ function morphoWithdraw(chainId: number, market: InputMarketParams, assets: bigint, shares: bigint, slippageAmount: bigint, receiver: Address, skipRevert?: boolean): BundlerCall[];
457
539
  /**
458
540
  * Encodes a GeneralAdapter1 Morpho Blue withdraw-collateral call.
459
541
  *
@@ -165,12 +165,19 @@ var BundlerAction;
165
165
  const [market, amount, onBehalf, onMorphoSupplyCollateral, skipRevert] = args;
166
166
  return BundlerAction.morphoSupplyCollateral(chainId, market, amount, onBehalf, onMorphoSupplyCollateral.flatMap(BundlerAction.encode.bind(null, chainId)), skipRevert);
167
167
  }
168
+ case "morphoSupply": {
169
+ const [market, assets, shares, slippageAmount, onBehalf, onMorphoSupply, skipRevert,] = args;
170
+ return BundlerAction.morphoSupply(chainId, market, assets, shares, slippageAmount, onBehalf, onMorphoSupply.flatMap(BundlerAction.encode.bind(null, chainId)), skipRevert);
171
+ }
168
172
  case "morphoBorrow": {
169
173
  return BundlerAction.morphoBorrow(chainId, ...args);
170
174
  }
175
+ case "morphoWithdraw": {
176
+ return BundlerAction.morphoWithdraw(chainId, ...args);
177
+ }
171
178
  case "morphoRepay": {
172
- const [market, assets, shares, slippageAmount, onBehalf, onMorphoRepay, skipRevert,] = args;
173
- return BundlerAction.morphoRepay(chainId, market, assets, shares, slippageAmount, onBehalf, onMorphoRepay.flatMap(BundlerAction.encode.bind(null, chainId)), skipRevert);
179
+ const [market, assets, shares, maxSharePrice, onBehalf, onMorphoRepay, skipRevert,] = args;
180
+ return BundlerAction.morphoRepay(chainId, market, assets, shares, maxSharePrice, onBehalf, onMorphoRepay.flatMap(BundlerAction.encode.bind(null, chainId)), skipRevert);
174
181
  }
175
182
  case "morphoWithdrawCollateral": {
176
183
  return BundlerAction.morphoWithdrawCollateral(chainId, ...args);
@@ -212,6 +219,17 @@ var BundlerAction;
212
219
  nextValueState = encodedCallback.valueState;
213
220
  }
214
221
  }
222
+ if (action.type === "morphoSupply") {
223
+ const [, , , , , onMorphoSupply] = action.args;
224
+ for (const callbackAction of onMorphoSupply) {
225
+ const encodedCallback = encodeBundleAction({
226
+ chainId,
227
+ action: callbackAction,
228
+ valueState: nextValueState,
229
+ });
230
+ nextValueState = encodedCallback.valueState;
231
+ }
232
+ }
215
233
  if (action.type === "morphoRepay") {
216
234
  const [, , , , , onMorphoRepay] = action.args;
217
235
  for (const callbackAction of onMorphoRepay) {
@@ -681,6 +699,66 @@ var BundlerAction;
681
699
  ];
682
700
  }
683
701
  BundlerAction.morphoSupplyCollateral = morphoSupplyCollateral;
702
+ /**
703
+ * Encodes a GeneralAdapter1 Morpho Blue supply call.
704
+ *
705
+ * @param chainId - Chain where the action will execute.
706
+ * @param market - Morpho Blue market parameters.
707
+ * @param assets - Supply asset amount.
708
+ * @param shares - Supply share amount.
709
+ * @param slippageAmount - Slippage guard amount (max share price in RAY for share-price slippage).
710
+ * @param onBehalf - Account receiving the supply position.
711
+ * @param callbackCalls - Calls executed in Morpho's callback.
712
+ * @param skipRevert - Whether Bundler3 should tolerate a revert.
713
+ * @returns Encoded Bundler3 calls.
714
+ *
715
+ * @example
716
+ * ```ts
717
+ * import { BundlerAction } from "@morpho-org/morpho-sdk/bundler";
718
+ *
719
+ * const loanToken = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
720
+ * const collateralToken = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
721
+ * const oracle = "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419";
722
+ * const irm = "0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC";
723
+ * const onBehalf = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
724
+ * const marketParams = {
725
+ * loanToken,
726
+ * collateralToken,
727
+ * oracle,
728
+ * irm,
729
+ * lltv: 860_000000000000000000n,
730
+ * };
731
+ *
732
+ * const calls = BundlerAction.morphoSupply(
733
+ * 1,
734
+ * marketParams,
735
+ * 100n,
736
+ * 0n,
737
+ * 1_010000000000000000000000000n,
738
+ * onBehalf,
739
+ * [],
740
+ * );
741
+ * ```
742
+ */
743
+ // biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
744
+ function morphoSupply(chainId, market, assets, shares, slippageAmount, onBehalf, callbackCalls, skipRevert = false) {
745
+ const { bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
746
+ const { callbackHash, reenterData } = encodeCallbackCalls(callbackCalls);
747
+ return [
748
+ {
749
+ to: generalAdapter1,
750
+ data: (0, viem_1.encodeFunctionData)({
751
+ abi: abis_js_1.generalAdapter1Abi,
752
+ functionName: "morphoSupply",
753
+ args: [market, assets, shares, slippageAmount, onBehalf, reenterData],
754
+ }),
755
+ value: 0n,
756
+ skipRevert,
757
+ callbackHash,
758
+ },
759
+ ];
760
+ }
761
+ BundlerAction.morphoSupply = morphoSupply;
684
762
  /**
685
763
  * Encodes a GeneralAdapter1 Morpho Blue borrow call.
686
764
  *
@@ -688,7 +766,7 @@ var BundlerAction;
688
766
  * @param market - Morpho Blue market parameters.
689
767
  * @param assets - Borrow asset amount.
690
768
  * @param shares - Borrow share amount.
691
- * @param slippageAmount - Slippage guard amount.
769
+ * @param minSharePrice - Minimum amount of borrowed assets per borrow share minted, scaled by 1e27.
692
770
  * @param receiver - Recipient of borrowed assets.
693
771
  * @param skipRevert - Whether Bundler3 should tolerate a revert.
694
772
  * @returns Encoded Bundler3 calls.
@@ -721,7 +799,7 @@ var BundlerAction;
721
799
  * ```
722
800
  */
723
801
  // biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
724
- function morphoBorrow(chainId, market, assets, shares, slippageAmount, receiver, skipRevert = false) {
802
+ function morphoBorrow(chainId, market, assets, shares, minSharePrice, receiver, skipRevert = false) {
725
803
  const { bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
726
804
  return [
727
805
  {
@@ -729,7 +807,7 @@ var BundlerAction;
729
807
  data: (0, viem_1.encodeFunctionData)({
730
808
  abi: abis_js_1.generalAdapter1Abi,
731
809
  functionName: "morphoBorrow",
732
- args: [market, assets, shares, slippageAmount, receiver],
810
+ args: [market, assets, shares, minSharePrice, receiver],
733
811
  }),
734
812
  value: 0n,
735
813
  skipRevert,
@@ -745,7 +823,7 @@ var BundlerAction;
745
823
  * @param market - Morpho Blue market parameters.
746
824
  * @param assets - Repay asset amount.
747
825
  * @param shares - Repay share amount.
748
- * @param slippageAmount - Slippage guard amount.
826
+ * @param maxSharePrice - Maximum amount of repaid assets per borrow share redeemed, scaled by 1e27.
749
827
  * @param onBehalf - Account whose borrow is repaid.
750
828
  * @param callbackCalls - Calls executed in Morpho's callback.
751
829
  * @param skipRevert - Whether Bundler3 should tolerate a revert.
@@ -780,7 +858,7 @@ var BundlerAction;
780
858
  * ```
781
859
  */
782
860
  // biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
783
- function morphoRepay(chainId, market, assets, shares, slippageAmount, onBehalf, callbackCalls, skipRevert = false) {
861
+ function morphoRepay(chainId, market, assets, shares, maxSharePrice, onBehalf, callbackCalls, skipRevert = false) {
784
862
  const { bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
785
863
  const { callbackHash, reenterData } = encodeCallbackCalls(callbackCalls);
786
864
  return [
@@ -789,7 +867,7 @@ var BundlerAction;
789
867
  data: (0, viem_1.encodeFunctionData)({
790
868
  abi: abis_js_1.generalAdapter1Abi,
791
869
  functionName: "morphoRepay",
792
- args: [market, assets, shares, slippageAmount, onBehalf, reenterData],
870
+ args: [market, assets, shares, maxSharePrice, onBehalf, reenterData],
793
871
  }),
794
872
  value: 0n,
795
873
  skipRevert,
@@ -798,6 +876,63 @@ var BundlerAction;
798
876
  ];
799
877
  }
800
878
  BundlerAction.morphoRepay = morphoRepay;
879
+ /**
880
+ * Encodes a GeneralAdapter1 Morpho Blue withdraw call.
881
+ *
882
+ * @param chainId - Chain where the action will execute.
883
+ * @param market - Morpho Blue market parameters.
884
+ * @param assets - Withdraw asset amount.
885
+ * @param shares - Withdraw share amount.
886
+ * @param slippageAmount - Slippage guard amount (min share price in RAY for share-price slippage).
887
+ * @param receiver - Recipient of withdrawn assets.
888
+ * @param skipRevert - Whether Bundler3 should tolerate a revert.
889
+ * @returns Encoded Bundler3 calls.
890
+ *
891
+ * @example
892
+ * ```ts
893
+ * import { BundlerAction } from "@morpho-org/morpho-sdk/bundler";
894
+ *
895
+ * const loanToken = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
896
+ * const collateralToken = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
897
+ * const oracle = "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419";
898
+ * const irm = "0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC";
899
+ * const receiver = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
900
+ * const marketParams = {
901
+ * loanToken,
902
+ * collateralToken,
903
+ * oracle,
904
+ * irm,
905
+ * lltv: 860_000000000000000000n,
906
+ * };
907
+ *
908
+ * const calls = BundlerAction.morphoWithdraw(
909
+ * 1,
910
+ * marketParams,
911
+ * 100n,
912
+ * 0n,
913
+ * 990000000000000000000000000n,
914
+ * receiver,
915
+ * );
916
+ * ```
917
+ */
918
+ // biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
919
+ function morphoWithdraw(chainId, market, assets, shares, slippageAmount, receiver, skipRevert = false) {
920
+ const { bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
921
+ return [
922
+ {
923
+ to: generalAdapter1,
924
+ data: (0, viem_1.encodeFunctionData)({
925
+ abi: abis_js_1.generalAdapter1Abi,
926
+ functionName: "morphoWithdraw",
927
+ args: [market, assets, shares, slippageAmount, receiver],
928
+ }),
929
+ value: 0n,
930
+ skipRevert,
931
+ callbackHash: viem_1.zeroHash,
932
+ },
933
+ ];
934
+ }
935
+ BundlerAction.morphoWithdraw = morphoWithdraw;
801
936
  /**
802
937
  * Encodes a GeneralAdapter1 Morpho Blue withdraw-collateral call.
803
938
  *
@@ -121,25 +121,44 @@ export interface ActionArgs {
121
121
  onMorphoSupplyCollateral: Action[],
122
122
  skipRevert?: boolean
123
123
  ];
124
- /** Morpho Blue borrow call for `market`, `assets` or `shares`, `slippageAmount`, and `receiver`; `skipRevert` controls Bundler3 revert handling. */
125
- readonly morphoBorrow: [
124
+ /** Morpho Blue supply call for `market`, `assets` or `shares`, `slippageAmount`, `onBehalf`, and callback actions; `skipRevert` controls Bundler3 revert handling. */
125
+ readonly morphoSupply: [
126
126
  market: InputMarketParams,
127
127
  assets: bigint,
128
128
  shares: bigint,
129
129
  slippageAmount: bigint,
130
+ onBehalf: Address,
131
+ onMorphoSupply: Action[],
132
+ skipRevert?: boolean
133
+ ];
134
+ /** Morpho Blue borrow call for `market`, `assets` or `shares`, `minSharePrice`, and `receiver`; `skipRevert` controls Bundler3 revert handling. */
135
+ readonly morphoBorrow: [
136
+ market: InputMarketParams,
137
+ assets: bigint,
138
+ shares: bigint,
139
+ minSharePrice: bigint,
130
140
  receiver: Address,
131
141
  skipRevert?: boolean
132
142
  ];
133
- /** Morpho Blue repay call for `market`, `assets` or `shares`, `slippageAmount`, `onBehalf`, and callback actions; `skipRevert` controls Bundler3 revert handling. */
143
+ /** Morpho Blue repay call for `market`, `assets` or `shares`, `maxSharePrice`, `onBehalf`, and callback actions; `skipRevert` controls Bundler3 revert handling. */
134
144
  readonly morphoRepay: [
135
145
  market: InputMarketParams,
136
146
  assets: bigint,
137
147
  shares: bigint,
138
- slippageAmount: bigint,
148
+ maxSharePrice: bigint,
139
149
  onBehalf: Address,
140
150
  onMorphoRepay: Action[],
141
151
  skipRevert?: boolean
142
152
  ];
153
+ /** Morpho Blue withdraw call for `market`, `assets` or `shares`, `slippageAmount`, and `receiver`; `skipRevert` controls Bundler3 revert handling. */
154
+ readonly morphoWithdraw: [
155
+ market: InputMarketParams,
156
+ assets: bigint,
157
+ shares: bigint,
158
+ slippageAmount: bigint,
159
+ receiver: Address,
160
+ skipRevert?: boolean
161
+ ];
143
162
  /** Morpho Blue withdraw-collateral call for `market`, `assets`, and `receiver`; `skipRevert` controls Bundler3 revert handling. */
144
163
  readonly morphoWithdrawCollateral: [
145
164
  market: InputMarketParams,
@@ -1,7 +1,7 @@
1
1
  import { type AccrualPosition, type Market, type MarketParams } from "@morpho-org/blue-sdk";
2
2
  import type { Address } from "viem";
3
3
  import type { FetchParameters } from "../../types/data.js";
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";
4
+ import { type AssetsOrSharesArgs, type DepositAmountArgs, type ERC20ApprovalAction, type MarketV1BorrowAction, type MarketV1RepayAction, type MarketV1RepayWithdrawCollateralAction, type MarketV1SupplyAction, type MarketV1SupplyCollateralAction, type MarketV1SupplyCollateralBorrowAction, type MarketV1WithdrawAction, type MarketV1WithdrawCollateralAction, type MorphoAuthorizationAction, type MorphoClientType, type ReallocationComputeOptions, type RepayAmountArgs, type Requirement, type RequirementSignature, type Transaction, type VaultReallocation } from "../../types/index.js";
5
5
  import { ReallocationData } from "../reallocationData.js";
6
6
  export interface MarketV1Actions {
7
7
  /**
@@ -37,6 +37,61 @@ export interface MarketV1Actions {
37
37
  useSimplePermit?: boolean;
38
38
  }) => Promise<(Readonly<Transaction<ERC20ApprovalAction>> | Requirement)[]>;
39
39
  };
40
+ /**
41
+ * Prepares a loan-asset supply transaction.
42
+ *
43
+ * Routed through bundler via GeneralAdapter1. Computes `maxSharePrice` from market supply
44
+ * state and `slippageTolerance` to protect against share-price inflation.
45
+ * `getRequirements` returns ERC20 approval or permit for `GeneralAdapter1` on the loan token.
46
+ * When `nativeAmount` is provided, native token is wrapped; the loan token must be wNative.
47
+ *
48
+ * No Morpho authorization required (supplier is crediting, not withdrawing).
49
+ *
50
+ * @param params - Supply parameters.
51
+ * @returns Object with `buildTx` and `getRequirements`.
52
+ */
53
+ supply: (params: {
54
+ userAddress: Address;
55
+ marketData: Market;
56
+ slippageTolerance?: bigint;
57
+ } & DepositAmountArgs) => {
58
+ buildTx: (requirementSignature?: RequirementSignature) => Readonly<Transaction<MarketV1SupplyAction>>;
59
+ getRequirements: (params?: {
60
+ useSimplePermit?: boolean;
61
+ }) => Promise<(Readonly<Transaction<ERC20ApprovalAction>> | Requirement)[]>;
62
+ };
63
+ /**
64
+ * Prepares a loan-asset withdraw transaction.
65
+ *
66
+ * Routed through bundler3 via `morphoWithdraw`. Supports two modes via {@link AssetsOrSharesArgs}:
67
+ * - **By assets** (`{ assets }`): withdraws an exact asset amount.
68
+ * - **By shares** (`{ shares }`): burns an exact share count (full close, immune to interest accrual).
69
+ *
70
+ * Computes `minSharePrice` from market supply state and `slippageTolerance`.
71
+ *
72
+ * When `reallocations` is provided, `reallocateTo` actions are prepended to the bundle,
73
+ * moving liquidity from other markets via the PublicAllocator before withdrawing — used to
74
+ * unblock withdraws that exceed on-market liquidity.
75
+ *
76
+ * `getRequirements` returns `morpho.setAuthorization(generalAdapter1, true)` if GA1 is not
77
+ * yet authorized on Morpho (returns `[]` when already authorized), since the bundler calls
78
+ * `withdraw(...,onBehalf=user,...)`.
79
+ *
80
+ * **Stale `positionData` may cause unexpected supply share calculations.**
81
+ *
82
+ * @param params - Withdraw parameters including pre-fetched `positionData`.
83
+ * @returns Object with `buildTx` and `getRequirements`.
84
+ */
85
+ withdraw: (params: {
86
+ userAddress: Address;
87
+ receiver?: Address;
88
+ positionData: AccrualPosition;
89
+ slippageTolerance?: bigint;
90
+ reallocations?: readonly VaultReallocation[];
91
+ } & AssetsOrSharesArgs) => {
92
+ buildTx: () => Readonly<Transaction<MarketV1WithdrawAction>>;
93
+ getRequirements: () => Promise<Readonly<Transaction<MorphoAuthorizationAction>>[]>;
94
+ };
40
95
  /**
41
96
  * Prepares a borrow transaction.
42
97
  *
@@ -201,29 +256,43 @@ export interface MarketV1Actions {
201
256
  };
202
257
  }) => Promise<ReallocationData>;
203
258
  /**
204
- * Computes vault reallocations for a borrow on this market.
259
+ * Computes vault reallocations for a borrow or withdraw on this market.
205
260
  *
206
- * Uses the shared liquidity algorithm to determine which vaults should
207
- * reallocate liquidity to this market via the PublicAllocator, based on
208
- * post-borrow utilization targets.
261
+ * Uses the shared-liquidity algorithm to determine which vaults should reallocate liquidity to
262
+ * this market via the PublicAllocator, based on the post-operation utilization target.
263
+ *
264
+ * Pass `{ borrowAmount }` for a borrow (legacy alias, equivalent to `{ operation: "borrow",
265
+ * amount }`) or `{ operation: "withdraw", amount }` for a loan-asset withdraw.
209
266
  *
210
267
  * @param params.reallocationData - The current on-chain state (from {@link getReallocationData}).
211
- * @param params.borrowAmount - The intended borrow amount.
268
+ * @param params.operation - The operation driving the reallocation (`"borrow"` or `"withdraw"`).
269
+ * Defaults to `"borrow"` when `borrowAmount` is provided.
270
+ * @param params.amount - The borrow or withdraw amount used to compute the post-state utilization.
271
+ * @param params.borrowAmount - {@deprecated} Equivalent to `{ operation: "borrow", amount }`. Use the
272
+ * `operation` + `amount` form on new code.
212
273
  * @param params.options - Optional reallocation computation options
213
274
  * (timestamp, utilization targets, reallocatable vaults filter, etc.).
214
275
  * Pass the fetched block timestamp to compute reallocations at the same block.
215
- * @returns Array of vault reallocations ready to pass to `borrow()` or
216
- * `supplyCollateralBorrow()`. Empty array if no reallocation is needed.
276
+ * @returns Array of vault reallocations ready to pass to `borrow()`, `supplyCollateralBorrow()`,
277
+ * or `withdraw()`. Empty array if no reallocation is needed.
217
278
  * @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.
279
+ * @throws {InsufficientSharedLiquidityError} when shared liquidity cannot cover the operation's absolute shortfall on the target market — preventing fee-bearing reallocations from being attached to a call that would still revert onchain.
219
280
  * @throws {MissingPublicAllocatorConfigError} when a selected vault is missing its public allocator config.
220
281
  * @throws {UnknownReallocationMarketError} when the target market is absent from the reallocation data.
221
282
  */
222
283
  getReallocations: (params: {
223
284
  reallocationData: ReallocationData;
224
- borrowAmount: bigint;
225
285
  options?: ReallocationComputeOptions;
226
- }) => readonly VaultReallocation[];
286
+ } & ({
287
+ operation: "borrow" | "withdraw";
288
+ amount: bigint;
289
+ borrowAmount?: never;
290
+ } | {
291
+ /** @deprecated Pass `{ operation: "borrow", amount }` instead. */
292
+ borrowAmount: bigint;
293
+ operation?: never;
294
+ amount?: never;
295
+ })) => readonly VaultReallocation[];
227
296
  }
228
297
  export declare class MorphoMarketV1 implements MarketV1Actions {
229
298
  private readonly client;
@@ -232,6 +301,26 @@ export declare class MorphoMarketV1 implements MarketV1Actions {
232
301
  constructor(client: MorphoClientType, marketParams: MarketParams, chainId: number);
233
302
  getMarketData(parameters?: FetchParameters): Promise<Market>;
234
303
  getPositionData(userAddress: Address, parameters?: FetchParameters): Promise<AccrualPosition>;
304
+ supply({ amount, userAddress, nativeAmount, marketData, slippageTolerance, }: {
305
+ userAddress: Address;
306
+ marketData: Market;
307
+ slippageTolerance?: bigint;
308
+ } & DepositAmountArgs): {
309
+ getRequirements: (params?: {
310
+ useSimplePermit?: boolean;
311
+ }) => Promise<(Requirement | Readonly<Transaction<ERC20ApprovalAction>>)[]>;
312
+ buildTx: (requirementSignature?: RequirementSignature) => Readonly<Transaction<MarketV1SupplyAction>>;
313
+ };
314
+ withdraw(params: {
315
+ userAddress: Address;
316
+ receiver?: Address;
317
+ positionData: AccrualPosition;
318
+ slippageTolerance?: bigint;
319
+ reallocations?: readonly VaultReallocation[];
320
+ } & AssetsOrSharesArgs): {
321
+ getRequirements: () => Promise<Readonly<Transaction<MorphoAuthorizationAction>>[]>;
322
+ buildTx: () => Readonly<Transaction<MarketV1WithdrawAction>>;
323
+ };
235
324
  supplyCollateral({ amount, userAddress, nativeAmount, }: {
236
325
  userAddress: Address;
237
326
  } & DepositAmountArgs): {
@@ -307,21 +396,35 @@ export declare class MorphoMarketV1 implements MarketV1Actions {
307
396
  };
308
397
  }): Promise<ReallocationData>;
309
398
  /**
310
- * Computes public allocator reallocations for a borrow on this market.
399
+ * Computes public allocator reallocations for a borrow or withdraw on this market.
400
+ *
401
+ * Pass `{ borrowAmount }` for a borrow (legacy alias, equivalent to `{ operation: "borrow", amount }`)
402
+ * or `{ operation, amount }` for a borrow or loan-asset withdraw.
311
403
  *
312
404
  * @param params - Reallocation computation parameters.
313
405
  * @param params.reallocationData - State returned by {@link getReallocationData}.
314
- * @param params.borrowAmount - Borrow amount to test against post-borrow utilization.
406
+ * @param params.operation - The operation driving the reallocation (`"borrow"` or `"withdraw"`).
407
+ * @param params.amount - The borrow or withdraw amount used to compute the post-state utilization.
408
+ * @param params.borrowAmount - {@deprecated Pass `{ operation: "borrow", amount }` instead.}
315
409
  * @param params.options - Optional allocator and utilization options.
316
- * @returns Vault reallocations ready to pass to `borrow` or `supplyCollateralBorrow`.
410
+ * @returns Vault reallocations ready to pass to `borrow`, `supplyCollateralBorrow`, or `withdraw`.
317
411
  * @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.
412
+ * @throws {InsufficientSharedLiquidityError} when shared liquidity cannot cover the operation's absolute shortfall on the target market.
413
+ * @throws {ReallocationWithdrawExceedsMarketSupplyError} when `operation === "withdraw"` and `amount` exceeds the target market's `totalSupplyAssets`.
319
414
  * @throws {MissingPublicAllocatorConfigError} when a selected vault is missing its public allocator config.
320
415
  * @throws {UnknownReallocationMarketError} when the target market is absent from the reallocation data.
321
416
  */
322
- getReallocations({ reallocationData, borrowAmount, options, }: {
417
+ getReallocations(params: {
323
418
  reallocationData: ReallocationData;
324
- borrowAmount: bigint;
325
419
  options?: ReallocationComputeOptions;
326
- }): readonly VaultReallocation[];
420
+ } & ({
421
+ operation: "borrow" | "withdraw";
422
+ amount: bigint;
423
+ borrowAmount?: never;
424
+ } | {
425
+ /** @deprecated Pass `{ operation: "borrow", amount }` instead. */
426
+ borrowAmount: bigint;
427
+ operation?: never;
428
+ amount?: never;
429
+ })): readonly VaultReallocation[];
327
430
  }