@morpho-org/morpho-sdk 1.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 (120) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +496 -0
  3. package/lib/actions/index.d.ts +4 -0
  4. package/lib/actions/index.js +20 -0
  5. package/lib/actions/marketV1/borrow.d.ts +34 -0
  6. package/lib/actions/marketV1/borrow.js +62 -0
  7. package/lib/actions/marketV1/buildReallocationActions.d.ts +17 -0
  8. package/lib/actions/marketV1/buildReallocationActions.js +36 -0
  9. package/lib/actions/marketV1/index.d.ts +6 -0
  10. package/lib/actions/marketV1/index.js +22 -0
  11. package/lib/actions/marketV1/repay.d.ts +44 -0
  12. package/lib/actions/marketV1/repay.js +93 -0
  13. package/lib/actions/marketV1/repayWithdrawCollateral.d.ts +51 -0
  14. package/lib/actions/marketV1/repayWithdrawCollateral.js +108 -0
  15. package/lib/actions/marketV1/supplyCollateral.d.ts +28 -0
  16. package/lib/actions/marketV1/supplyCollateral.js +85 -0
  17. package/lib/actions/marketV1/supplyCollateralBorrow.d.ts +37 -0
  18. package/lib/actions/marketV1/supplyCollateralBorrow.js +109 -0
  19. package/lib/actions/marketV1/withdrawCollateral.d.ts +28 -0
  20. package/lib/actions/marketV1/withdrawCollateral.js +51 -0
  21. package/lib/actions/requirements/encode/encodeErc20Approval.d.ts +10 -0
  22. package/lib/actions/requirements/encode/encodeErc20Approval.js +25 -0
  23. package/lib/actions/requirements/encode/encodeErc20Permit.d.ts +13 -0
  24. package/lib/actions/requirements/encode/encodeErc20Permit.js +70 -0
  25. package/lib/actions/requirements/encode/encodeErc20Permit2.d.ts +11 -0
  26. package/lib/actions/requirements/encode/encodeErc20Permit2.js +70 -0
  27. package/lib/actions/requirements/encode/index.d.ts +3 -0
  28. package/lib/actions/requirements/encode/index.js +19 -0
  29. package/lib/actions/requirements/getMorphoAuthorizationRequirement.d.ts +21 -0
  30. package/lib/actions/requirements/getMorphoAuthorizationRequirement.js +55 -0
  31. package/lib/actions/requirements/getRequirements.d.ts +42 -0
  32. package/lib/actions/requirements/getRequirements.js +80 -0
  33. package/lib/actions/requirements/getRequirementsAction.d.ts +17 -0
  34. package/lib/actions/requirements/getRequirementsAction.js +64 -0
  35. package/lib/actions/requirements/getRequirementsApproval.d.ts +31 -0
  36. package/lib/actions/requirements/getRequirementsApproval.js +51 -0
  37. package/lib/actions/requirements/getRequirementsPermit.d.ts +28 -0
  38. package/lib/actions/requirements/getRequirementsPermit.js +40 -0
  39. package/lib/actions/requirements/getRequirementsPermit2.d.ts +37 -0
  40. package/lib/actions/requirements/getRequirementsPermit2.js +59 -0
  41. package/lib/actions/requirements/index.d.ts +6 -0
  42. package/lib/actions/requirements/index.js +22 -0
  43. package/lib/actions/vaultV1/deposit.d.ts +41 -0
  44. package/lib/actions/vaultV1/deposit.js +116 -0
  45. package/lib/actions/vaultV1/index.d.ts +4 -0
  46. package/lib/actions/vaultV1/index.js +20 -0
  47. package/lib/actions/vaultV1/migrateToV2.d.ts +50 -0
  48. package/lib/actions/vaultV1/migrateToV2.js +108 -0
  49. package/lib/actions/vaultV1/redeem.d.ts +29 -0
  50. package/lib/actions/vaultV1/redeem.js +48 -0
  51. package/lib/actions/vaultV1/withdraw.d.ts +29 -0
  52. package/lib/actions/vaultV1/withdraw.js +48 -0
  53. package/lib/actions/vaultV2/deposit.d.ts +41 -0
  54. package/lib/actions/vaultV2/deposit.js +116 -0
  55. package/lib/actions/vaultV2/forceRedeem.d.ts +48 -0
  56. package/lib/actions/vaultV2/forceRedeem.js +85 -0
  57. package/lib/actions/vaultV2/forceWithdraw.d.ts +40 -0
  58. package/lib/actions/vaultV2/forceWithdraw.js +85 -0
  59. package/lib/actions/vaultV2/index.d.ts +5 -0
  60. package/lib/actions/vaultV2/index.js +21 -0
  61. package/lib/actions/vaultV2/redeem.d.ts +32 -0
  62. package/lib/actions/vaultV2/redeem.js +51 -0
  63. package/lib/actions/vaultV2/withdraw.d.ts +32 -0
  64. package/lib/actions/vaultV2/withdraw.js +51 -0
  65. package/lib/client/index.d.ts +2 -0
  66. package/lib/client/index.js +18 -0
  67. package/lib/client/morphoClient.d.ts +25 -0
  68. package/lib/client/morphoClient.js +28 -0
  69. package/lib/client/morphoViemExtension.d.ts +39 -0
  70. package/lib/client/morphoViemExtension.js +40 -0
  71. package/lib/entities/index.d.ts +3 -0
  72. package/lib/entities/index.js +19 -0
  73. package/lib/entities/marketV1/index.d.ts +1 -0
  74. package/lib/entities/marketV1/index.js +17 -0
  75. package/lib/entities/marketV1/marketV1.d.ts +290 -0
  76. package/lib/entities/marketV1/marketV1.js +531 -0
  77. package/lib/entities/vaultV1/index.d.ts +1 -0
  78. package/lib/entities/vaultV1/index.js +17 -0
  79. package/lib/entities/vaultV1/vaultV1.d.ts +133 -0
  80. package/lib/entities/vaultV1/vaultV1.js +186 -0
  81. package/lib/entities/vaultV2/index.d.ts +1 -0
  82. package/lib/entities/vaultV2/index.js +17 -0
  83. package/lib/entities/vaultV2/vaultV2.d.ts +180 -0
  84. package/lib/entities/vaultV2/vaultV2.js +167 -0
  85. package/lib/helpers/computeReallocations.d.ts +23 -0
  86. package/lib/helpers/computeReallocations.js +98 -0
  87. package/lib/helpers/constant.d.ts +6 -0
  88. package/lib/helpers/constant.js +10 -0
  89. package/lib/helpers/encodeDeallocation.d.ts +10 -0
  90. package/lib/helpers/encodeDeallocation.js +37 -0
  91. package/lib/helpers/index.d.ts +4 -0
  92. package/lib/helpers/index.js +22 -0
  93. package/lib/helpers/metadata.d.ts +33 -0
  94. package/lib/helpers/metadata.js +49 -0
  95. package/lib/helpers/slippage.d.ts +46 -0
  96. package/lib/helpers/slippage.js +73 -0
  97. package/lib/helpers/validate.d.ts +156 -0
  98. package/lib/helpers/validate.js +287 -0
  99. package/lib/index.d.ts +5 -0
  100. package/lib/index.js +21 -0
  101. package/lib/types/action.d.ts +216 -0
  102. package/lib/types/action.js +26 -0
  103. package/lib/types/client.d.ts +15 -0
  104. package/lib/types/client.js +2 -0
  105. package/lib/types/data.d.ts +2 -0
  106. package/lib/types/data.js +2 -0
  107. package/lib/types/deallocation.d.ts +15 -0
  108. package/lib/types/deallocation.js +2 -0
  109. package/lib/types/entity.d.ts +5 -0
  110. package/lib/types/entity.js +2 -0
  111. package/lib/types/error.d.ts +166 -0
  112. package/lib/types/error.js +285 -0
  113. package/lib/types/index.d.ts +7 -0
  114. package/lib/types/index.js +23 -0
  115. package/lib/types/metadata.d.ts +4 -0
  116. package/lib/types/metadata.js +2 -0
  117. package/lib/types/sharedLiquidity.d.ts +41 -0
  118. package/lib/types/sharedLiquidity.js +2 -0
  119. package/package.json +81 -0
  120. package/src/index.ts +5 -0
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildReallocationActions = void 0;
4
+ const helpers_1 = require("../../helpers");
5
+ /**
6
+ * Builds reallocation bundler actions and computes the total fee.
7
+ *
8
+ * Validates the reallocations, then encodes each as a `reallocateTo` action.
9
+ * Caller must ensure `reallocations` is non-empty before calling.
10
+ *
11
+ * @param reallocations - The vault reallocations to encode.
12
+ * @param targetMarketParams - The target market params for the borrow.
13
+ * @returns The encoded actions and total reallocation fee.
14
+ */
15
+ const buildReallocationActions = (reallocations, targetMarketParams) => {
16
+ (0, helpers_1.validateReallocations)(reallocations, targetMarketParams.id);
17
+ const fee = reallocations.reduce((sum, r) => sum + r.fee, 0n);
18
+ const actions = [];
19
+ for (const r of reallocations) {
20
+ actions.push({
21
+ type: "reallocateTo",
22
+ args: [
23
+ r.vault,
24
+ r.fee,
25
+ r.withdrawals.map((w) => ({
26
+ marketParams: w.marketParams,
27
+ amount: w.amount,
28
+ })),
29
+ targetMarketParams,
30
+ false,
31
+ ],
32
+ });
33
+ }
34
+ return { actions, fee };
35
+ };
36
+ exports.buildReallocationActions = buildReallocationActions;
@@ -0,0 +1,6 @@
1
+ export * from "./borrow";
2
+ export * from "./repay";
3
+ export * from "./repayWithdrawCollateral";
4
+ export * from "./supplyCollateral";
5
+ export * from "./supplyCollateralBorrow";
6
+ export * from "./withdrawCollateral";
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./borrow"), exports);
18
+ __exportStar(require("./repay"), exports);
19
+ __exportStar(require("./repayWithdrawCollateral"), exports);
20
+ __exportStar(require("./supplyCollateral"), exports);
21
+ __exportStar(require("./supplyCollateralBorrow"), exports);
22
+ __exportStar(require("./withdrawCollateral"), exports);
@@ -0,0 +1,44 @@
1
+ import { type MarketParams } from "@morpho-org/blue-sdk";
2
+ import { type Address } from "viem";
3
+ import type { MarketV1RepayAction, Metadata, RequirementSignature, Transaction } from "../../types";
4
+ /** Parameters for {@link marketV1Repay}. */
5
+ export interface MarketV1RepayParams {
6
+ market: {
7
+ readonly chainId: number;
8
+ readonly marketParams: MarketParams;
9
+ };
10
+ args: {
11
+ /** Repay assets amount (0n when repaying by shares). */
12
+ assets: bigint;
13
+ /** Repay shares amount (0n when repaying by assets). */
14
+ shares: bigint;
15
+ /** ERC20 amount to transfer to GeneralAdapter1. Must be greater than or equal to the repay amount to take into account the slippage. */
16
+ transferAmount: bigint;
17
+ /** Address whose debt is being repaid. */
18
+ onBehalf: Address;
19
+ /** Receives residual loan tokens in shares mode. */
20
+ receiver: Address;
21
+ /** Maximum repay share price (in ray). Protects against share price manipulation. */
22
+ maxSharePrice: bigint;
23
+ requirementSignature?: RequirementSignature;
24
+ };
25
+ metadata?: Metadata;
26
+ }
27
+ /**
28
+ * Prepares a repay transaction for a Morpho Blue market.
29
+ *
30
+ * Routed through bundler3 via GeneralAdapter1. Supports two modes:
31
+ * - **By assets** (`assets > 0, shares = 0`): repays an exact asset amount.
32
+ * - **By shares** (`assets = 0, shares > 0`): repays exact shares (full repay).
33
+ *
34
+ * Exactly one of `assets`/`shares` must be non-zero. The `transferAmount` controls
35
+ * how many ERC20 tokens are pulled from the user (may differ from `assets` in
36
+ * shares mode where the entity computes an upper-bound estimate).
37
+ *
38
+ * Uses `maxSharePrice` to protect against share price manipulation between
39
+ * transaction construction and execution.
40
+ *
41
+ * @param params - Repay parameters.
42
+ * @returns Deep-frozen transaction.
43
+ */
44
+ export declare const marketV1Repay: ({ market: { chainId, marketParams }, args: { assets, shares, transferAmount, onBehalf, receiver, maxSharePrice, requirementSignature, }, metadata, }: MarketV1RepayParams) => Readonly<Transaction<MarketV1RepayAction>>;
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.marketV1Repay = void 0;
4
+ const blue_sdk_1 = require("@morpho-org/blue-sdk");
5
+ const bundler_sdk_viem_1 = require("@morpho-org/bundler-sdk-viem");
6
+ const morpho_ts_1 = require("@morpho-org/morpho-ts");
7
+ const viem_1 = require("viem");
8
+ const helpers_1 = require("../../helpers");
9
+ const getRequirementsAction_1 = require("../requirements/getRequirementsAction");
10
+ /**
11
+ * Prepares a repay transaction for a Morpho Blue market.
12
+ *
13
+ * Routed through bundler3 via GeneralAdapter1. Supports two modes:
14
+ * - **By assets** (`assets > 0, shares = 0`): repays an exact asset amount.
15
+ * - **By shares** (`assets = 0, shares > 0`): repays exact shares (full repay).
16
+ *
17
+ * Exactly one of `assets`/`shares` must be non-zero. The `transferAmount` controls
18
+ * how many ERC20 tokens are pulled from the user (may differ from `assets` in
19
+ * shares mode where the entity computes an upper-bound estimate).
20
+ *
21
+ * Uses `maxSharePrice` to protect against share price manipulation between
22
+ * transaction construction and execution.
23
+ *
24
+ * @param params - Repay parameters.
25
+ * @returns Deep-frozen transaction.
26
+ */
27
+ const marketV1Repay = ({ market: { chainId, marketParams }, args: { assets, shares, transferAmount, onBehalf, receiver, maxSharePrice, requirementSignature, }, metadata, }) => {
28
+ (0, helpers_1.validateRepayParams)({
29
+ assets,
30
+ shares,
31
+ transferAmount,
32
+ maxSharePrice,
33
+ marketId: marketParams.id,
34
+ });
35
+ const { bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
36
+ const actions = [];
37
+ if (requirementSignature) {
38
+ actions.push(...(0, getRequirementsAction_1.getRequirementsAction)({
39
+ chainId,
40
+ asset: marketParams.loanToken,
41
+ amount: transferAmount,
42
+ requirementSignature,
43
+ }));
44
+ }
45
+ else {
46
+ actions.push({
47
+ type: "erc20TransferFrom",
48
+ args: [marketParams.loanToken, transferAmount, generalAdapter1, false],
49
+ });
50
+ }
51
+ actions.push({
52
+ type: "morphoRepay",
53
+ args: [marketParams, assets, shares, maxSharePrice, onBehalf, [], false],
54
+ });
55
+ // Skim residual loan tokens back to the payer when repaying by shares.
56
+ // In shares mode, transferAmount is an upper-bound estimate; morphoRepay
57
+ // consumes only the exact amount needed, leaving a residual in the adapter.
58
+ if (shares > 0n) {
59
+ actions.push({
60
+ type: "erc20Transfer",
61
+ args: [
62
+ marketParams.loanToken,
63
+ receiver,
64
+ viem_1.maxUint256,
65
+ generalAdapter1,
66
+ false,
67
+ ],
68
+ });
69
+ }
70
+ let tx = {
71
+ ...bundler_sdk_viem_1.BundlerAction.encodeBundle(chainId, actions),
72
+ value: 0n,
73
+ };
74
+ if (metadata) {
75
+ tx = (0, helpers_1.addTransactionMetadata)(tx, metadata);
76
+ }
77
+ return (0, morpho_ts_1.deepFreeze)({
78
+ ...tx,
79
+ action: {
80
+ type: "marketV1Repay",
81
+ args: {
82
+ market: marketParams.id,
83
+ assets,
84
+ shares,
85
+ transferAmount,
86
+ onBehalf,
87
+ receiver,
88
+ maxSharePrice,
89
+ },
90
+ },
91
+ });
92
+ };
93
+ exports.marketV1Repay = marketV1Repay;
@@ -0,0 +1,51 @@
1
+ import { type MarketParams } from "@morpho-org/blue-sdk";
2
+ import { type Address } from "viem";
3
+ import { type MarketV1RepayWithdrawCollateralAction, type Metadata, type RequirementSignature, type Transaction } from "../../types";
4
+ /** Parameters for {@link marketV1RepayWithdrawCollateral}. */
5
+ export interface MarketV1RepayWithdrawCollateralParams {
6
+ market: {
7
+ readonly chainId: number;
8
+ readonly marketParams: MarketParams;
9
+ };
10
+ args: {
11
+ /** Repay assets amount (0n when repaying by shares). */
12
+ assets: bigint;
13
+ /** Repay shares amount (0n when repaying by assets). */
14
+ shares: bigint;
15
+ /** ERC20 amount to transfer to GeneralAdapter1 (computed by entity). */
16
+ transferAmount: bigint;
17
+ /** Amount of collateral to withdraw. */
18
+ withdrawAmount: bigint;
19
+ /** Address whose debt is being repaid. */
20
+ onBehalf: Address;
21
+ /** Receives withdrawn collateral and residual loan tokens in shares mode. */
22
+ receiver: Address;
23
+ /** Maximum repay share price (in ray). Protects against share price manipulation. */
24
+ maxSharePrice: bigint;
25
+ requirementSignature?: RequirementSignature;
26
+ };
27
+ metadata?: Metadata;
28
+ }
29
+ /**
30
+ * Prepares an atomic repay-and-withdraw-collateral transaction for a Morpho Blue market.
31
+ *
32
+ * Routed through bundler3. The bundle order is critical:
33
+ * 1. ERC20 transfer (loan token to GeneralAdapter1)
34
+ * 2. `morphoRepay` — reduces debt FIRST
35
+ * 3. `morphoWithdrawCollateral` — then withdraws collateral
36
+ *
37
+ * If the order were reversed, Morpho would revert because the position would be
38
+ * insolvent at the time of the withdraw.
39
+ *
40
+ * Supports two repay modes:
41
+ * - **By assets** (`assets > 0, shares = 0`): repays an exact asset amount.
42
+ * - **By shares** (`assets = 0, shares > 0`): repays exact shares (full repay).
43
+ *
44
+ * **Prerequisites:**
45
+ * - ERC20 approval for loan token to GeneralAdapter1 (for the repay).
46
+ * - GeneralAdapter1 must be authorized on Morpho (for the withdraw).
47
+ *
48
+ * @param params - Combined repay and withdraw collateral parameters.
49
+ * @returns Deep-frozen transaction.
50
+ */
51
+ export declare const marketV1RepayWithdrawCollateral: ({ market: { chainId, marketParams }, args: { assets, shares, transferAmount, withdrawAmount, onBehalf, receiver, maxSharePrice, requirementSignature, }, metadata, }: MarketV1RepayWithdrawCollateralParams) => Readonly<Transaction<MarketV1RepayWithdrawCollateralAction>>;
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.marketV1RepayWithdrawCollateral = void 0;
4
+ const blue_sdk_1 = require("@morpho-org/blue-sdk");
5
+ const bundler_sdk_viem_1 = require("@morpho-org/bundler-sdk-viem");
6
+ const morpho_ts_1 = require("@morpho-org/morpho-ts");
7
+ const viem_1 = require("viem");
8
+ const helpers_1 = require("../../helpers");
9
+ const types_1 = require("../../types");
10
+ const getRequirementsAction_1 = require("../requirements/getRequirementsAction");
11
+ /**
12
+ * Prepares an atomic repay-and-withdraw-collateral transaction for a Morpho Blue market.
13
+ *
14
+ * Routed through bundler3. The bundle order is critical:
15
+ * 1. ERC20 transfer (loan token to GeneralAdapter1)
16
+ * 2. `morphoRepay` — reduces debt FIRST
17
+ * 3. `morphoWithdrawCollateral` — then withdraws collateral
18
+ *
19
+ * If the order were reversed, Morpho would revert because the position would be
20
+ * insolvent at the time of the withdraw.
21
+ *
22
+ * Supports two repay modes:
23
+ * - **By assets** (`assets > 0, shares = 0`): repays an exact asset amount.
24
+ * - **By shares** (`assets = 0, shares > 0`): repays exact shares (full repay).
25
+ *
26
+ * **Prerequisites:**
27
+ * - ERC20 approval for loan token to GeneralAdapter1 (for the repay).
28
+ * - GeneralAdapter1 must be authorized on Morpho (for the withdraw).
29
+ *
30
+ * @param params - Combined repay and withdraw collateral parameters.
31
+ * @returns Deep-frozen transaction.
32
+ */
33
+ const marketV1RepayWithdrawCollateral = ({ market: { chainId, marketParams }, args: { assets, shares, transferAmount, withdrawAmount, onBehalf, receiver, maxSharePrice, requirementSignature, }, metadata, }) => {
34
+ (0, helpers_1.validateRepayParams)({
35
+ assets,
36
+ shares,
37
+ transferAmount,
38
+ maxSharePrice,
39
+ marketId: marketParams.id,
40
+ });
41
+ if (withdrawAmount <= 0n) {
42
+ throw new types_1.NonPositiveWithdrawCollateralAmountError(marketParams.id);
43
+ }
44
+ const { bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
45
+ const actions = [];
46
+ if (requirementSignature) {
47
+ actions.push(...(0, getRequirementsAction_1.getRequirementsAction)({
48
+ chainId,
49
+ asset: marketParams.loanToken,
50
+ amount: transferAmount,
51
+ requirementSignature,
52
+ }));
53
+ }
54
+ else {
55
+ actions.push({
56
+ type: "erc20TransferFrom",
57
+ args: [marketParams.loanToken, transferAmount, generalAdapter1, false],
58
+ });
59
+ }
60
+ // REPAY FIRST — reduces debt before withdrawing collateral
61
+ actions.push({
62
+ type: "morphoRepay",
63
+ args: [marketParams, assets, shares, maxSharePrice, onBehalf, [], false],
64
+ });
65
+ // Skim residual loan tokens back to the payer when repaying by shares.
66
+ // In shares mode, transferAmount is an upper-bound estimate; morphoRepay
67
+ // consumes only the exact amount needed, leaving a residual in the adapter.
68
+ if (shares > 0n) {
69
+ actions.push({
70
+ type: "erc20Transfer",
71
+ args: [
72
+ marketParams.loanToken,
73
+ receiver,
74
+ viem_1.maxUint256,
75
+ generalAdapter1,
76
+ false,
77
+ ],
78
+ });
79
+ }
80
+ actions.push({
81
+ type: "morphoWithdrawCollateral",
82
+ args: [marketParams, withdrawAmount, receiver, false],
83
+ });
84
+ let tx = {
85
+ ...bundler_sdk_viem_1.BundlerAction.encodeBundle(chainId, actions),
86
+ value: 0n,
87
+ };
88
+ if (metadata) {
89
+ tx = (0, helpers_1.addTransactionMetadata)(tx, metadata);
90
+ }
91
+ return (0, morpho_ts_1.deepFreeze)({
92
+ ...tx,
93
+ action: {
94
+ type: "marketV1RepayWithdrawCollateral",
95
+ args: {
96
+ market: marketParams.id,
97
+ repayAssets: assets,
98
+ repayShares: shares,
99
+ transferAmount,
100
+ withdrawAmount,
101
+ maxSharePrice,
102
+ onBehalf,
103
+ receiver,
104
+ },
105
+ },
106
+ });
107
+ };
108
+ exports.marketV1RepayWithdrawCollateral = marketV1RepayWithdrawCollateral;
@@ -0,0 +1,28 @@
1
+ import { type MarketParams } from "@morpho-org/blue-sdk";
2
+ import type { Address } from "viem";
3
+ import { type DepositAmountArgs, type MarketV1SupplyCollateralAction, type Metadata, type RequirementSignature, type Transaction } from "../../types";
4
+ /** Parameters for {@link marketV1SupplyCollateral}. */
5
+ export interface MarketV1SupplyCollateralParams {
6
+ market: {
7
+ readonly chainId: number;
8
+ readonly marketParams: MarketParams;
9
+ };
10
+ args: DepositAmountArgs & {
11
+ onBehalf: Address;
12
+ requirementSignature?: RequirementSignature;
13
+ };
14
+ metadata?: Metadata;
15
+ }
16
+ /**
17
+ * Prepares a supply-collateral transaction for a Morpho Blue market.
18
+ *
19
+ * Routed through bundler via GeneralAdapter1.
20
+ * When `nativeAmount` is provided, native token is wrapped via GeneralAdapter1.
21
+ * Collateral token must be the chain's wNative when `nativeAmount` is used.
22
+ *
23
+ * Zero loss: all collateral reaches Morpho. No dust left in bundler or adapter.
24
+ *
25
+ * @param params - Supply collateral parameters.
26
+ * @returns Deep-frozen transaction.
27
+ */
28
+ export declare const marketV1SupplyCollateral: ({ market: { chainId, marketParams }, args: { amount, onBehalf, requirementSignature, nativeAmount }, metadata, }: MarketV1SupplyCollateralParams) => Readonly<Transaction<MarketV1SupplyCollateralAction>>;
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.marketV1SupplyCollateral = void 0;
4
+ const blue_sdk_1 = require("@morpho-org/blue-sdk");
5
+ const bundler_sdk_viem_1 = require("@morpho-org/bundler-sdk-viem");
6
+ const morpho_ts_1 = require("@morpho-org/morpho-ts");
7
+ const helpers_1 = require("../../helpers");
8
+ const types_1 = require("../../types");
9
+ const getRequirementsAction_1 = require("../requirements/getRequirementsAction");
10
+ /**
11
+ * Prepares a supply-collateral transaction for a Morpho Blue market.
12
+ *
13
+ * Routed through bundler via GeneralAdapter1.
14
+ * When `nativeAmount` is provided, native token is wrapped via GeneralAdapter1.
15
+ * Collateral token must be the chain's wNative when `nativeAmount` is used.
16
+ *
17
+ * Zero loss: all collateral reaches Morpho. No dust left in bundler or adapter.
18
+ *
19
+ * @param params - Supply collateral parameters.
20
+ * @returns Deep-frozen transaction.
21
+ */
22
+ const marketV1SupplyCollateral = ({ market: { chainId, marketParams }, args: { amount = 0n, onBehalf, requirementSignature, nativeAmount }, metadata, }) => {
23
+ if (amount < 0n) {
24
+ throw new types_1.NonPositiveAssetAmountError(marketParams.collateralToken);
25
+ }
26
+ if (nativeAmount !== undefined && nativeAmount < 0n) {
27
+ throw new types_1.NegativeNativeAmountError(nativeAmount);
28
+ }
29
+ const totalCollateral = amount + (nativeAmount ?? 0n);
30
+ if (totalCollateral === 0n) {
31
+ throw new types_1.ZeroCollateralAmountError(marketParams.id);
32
+ }
33
+ const { bundler3: { generalAdapter1, bundler3 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
34
+ const actions = [];
35
+ if (nativeAmount !== undefined && nativeAmount > 0n) {
36
+ (0, helpers_1.validateNativeCollateral)(chainId, marketParams.collateralToken);
37
+ actions.push({
38
+ type: "nativeTransfer",
39
+ args: [bundler3, generalAdapter1, nativeAmount, false],
40
+ }, {
41
+ type: "wrapNative",
42
+ args: [nativeAmount, generalAdapter1, false],
43
+ });
44
+ }
45
+ if (amount > 0n) {
46
+ if (requirementSignature) {
47
+ actions.push(...(0, getRequirementsAction_1.getRequirementsAction)({
48
+ chainId,
49
+ asset: marketParams.collateralToken,
50
+ amount,
51
+ requirementSignature,
52
+ }));
53
+ }
54
+ else {
55
+ actions.push({
56
+ type: "erc20TransferFrom",
57
+ args: [marketParams.collateralToken, amount, generalAdapter1, false],
58
+ });
59
+ }
60
+ }
61
+ actions.push({
62
+ type: "morphoSupplyCollateral",
63
+ args: [marketParams, totalCollateral, onBehalf, [], false],
64
+ });
65
+ let tx = {
66
+ ...bundler_sdk_viem_1.BundlerAction.encodeBundle(chainId, actions),
67
+ value: nativeAmount ?? 0n,
68
+ };
69
+ if (metadata) {
70
+ tx = (0, helpers_1.addTransactionMetadata)(tx, metadata);
71
+ }
72
+ return (0, morpho_ts_1.deepFreeze)({
73
+ ...tx,
74
+ action: {
75
+ type: "marketV1SupplyCollateral",
76
+ args: {
77
+ market: marketParams.id,
78
+ amount: totalCollateral,
79
+ onBehalf,
80
+ nativeAmount,
81
+ },
82
+ },
83
+ });
84
+ };
85
+ exports.marketV1SupplyCollateral = marketV1SupplyCollateral;
@@ -0,0 +1,37 @@
1
+ import { type MarketParams } from "@morpho-org/blue-sdk";
2
+ import type { Address } from "viem";
3
+ import { type DepositAmountArgs, type MarketV1SupplyCollateralBorrowAction, type Metadata, type RequirementSignature, type Transaction, type VaultReallocation } from "../../types";
4
+ /** Parameters for {@link marketV1SupplyCollateralBorrow}. */
5
+ export interface MarketV1SupplyCollateralBorrowParams {
6
+ market: {
7
+ readonly chainId: number;
8
+ readonly marketParams: MarketParams;
9
+ };
10
+ args: DepositAmountArgs & {
11
+ borrowAmount: bigint;
12
+ onBehalf: Address;
13
+ receiver: Address;
14
+ /** Minimum borrow share price (in ray). Protects against share price manipulation. */
15
+ minSharePrice: bigint;
16
+ requirementSignature?: RequirementSignature;
17
+ /** Vault reallocations to execute before borrowing (computed by entity). */
18
+ reallocations?: readonly VaultReallocation[];
19
+ };
20
+ metadata?: Metadata;
21
+ }
22
+ /**
23
+ * Prepares an atomic supply-collateral-and-borrow transaction for a Morpho Blue market.
24
+ *
25
+ * Routed through the bundler: collateral transfer + `morphoSupplyCollateral` + `morphoBorrow`.
26
+ * When `nativeAmount` is provided, native ETH is wrapped via GeneralAdapter1.
27
+ *
28
+ * **Prerequisite:** GeneralAdapter1 must be authorized on Morpho to borrow on behalf of the user.
29
+ * Use `getRequirements()` on the entity to check and obtain the authorization transaction.
30
+ *
31
+ * Zero loss: all collateral reaches Morpho, all borrowed tokens reach the receiver.
32
+ * No dust left in bundler or adapter.
33
+ *
34
+ * @param params - Combined supply collateral and borrow parameters.
35
+ * @returns Deep-frozen transaction.
36
+ */
37
+ export declare const marketV1SupplyCollateralBorrow: ({ market: { chainId, marketParams }, args: { amount, borrowAmount, onBehalf, receiver, minSharePrice, requirementSignature, nativeAmount, reallocations, }, metadata, }: MarketV1SupplyCollateralBorrowParams) => Readonly<Transaction<MarketV1SupplyCollateralBorrowAction>>;
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.marketV1SupplyCollateralBorrow = void 0;
4
+ const blue_sdk_1 = require("@morpho-org/blue-sdk");
5
+ const bundler_sdk_viem_1 = require("@morpho-org/bundler-sdk-viem");
6
+ const morpho_ts_1 = require("@morpho-org/morpho-ts");
7
+ const helpers_1 = require("../../helpers");
8
+ const types_1 = require("../../types");
9
+ const getRequirementsAction_1 = require("../requirements/getRequirementsAction");
10
+ const buildReallocationActions_1 = require("./buildReallocationActions");
11
+ /**
12
+ * Prepares an atomic supply-collateral-and-borrow transaction for a Morpho Blue market.
13
+ *
14
+ * Routed through the bundler: collateral transfer + `morphoSupplyCollateral` + `morphoBorrow`.
15
+ * When `nativeAmount` is provided, native ETH is wrapped via GeneralAdapter1.
16
+ *
17
+ * **Prerequisite:** GeneralAdapter1 must be authorized on Morpho to borrow on behalf of the user.
18
+ * Use `getRequirements()` on the entity to check and obtain the authorization transaction.
19
+ *
20
+ * Zero loss: all collateral reaches Morpho, all borrowed tokens reach the receiver.
21
+ * No dust left in bundler or adapter.
22
+ *
23
+ * @param params - Combined supply collateral and borrow parameters.
24
+ * @returns Deep-frozen transaction.
25
+ */
26
+ const marketV1SupplyCollateralBorrow = ({ market: { chainId, marketParams }, args: { amount = 0n, borrowAmount, onBehalf, receiver, minSharePrice, requirementSignature, nativeAmount, reallocations, }, metadata, }) => {
27
+ if (amount < 0n) {
28
+ throw new types_1.NonPositiveAssetAmountError(marketParams.collateralToken);
29
+ }
30
+ if (nativeAmount !== undefined && nativeAmount < 0n) {
31
+ throw new types_1.NegativeNativeAmountError(nativeAmount);
32
+ }
33
+ if (borrowAmount <= 0n) {
34
+ throw new types_1.NonPositiveBorrowAmountError(marketParams.id);
35
+ }
36
+ if (minSharePrice < 0n) {
37
+ throw new types_1.NonPositiveMinBorrowSharePriceError(marketParams.id);
38
+ }
39
+ const totalCollateral = amount + (nativeAmount ?? 0n);
40
+ if (totalCollateral === 0n) {
41
+ throw new types_1.ZeroCollateralAmountError(marketParams.id);
42
+ }
43
+ const { bundler3: { generalAdapter1, bundler3 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
44
+ const actions = [];
45
+ if (nativeAmount !== undefined && nativeAmount > 0n) {
46
+ (0, helpers_1.validateNativeCollateral)(chainId, marketParams.collateralToken);
47
+ actions.push({
48
+ type: "nativeTransfer",
49
+ args: [bundler3, generalAdapter1, nativeAmount, false],
50
+ }, {
51
+ type: "wrapNative",
52
+ args: [nativeAmount, generalAdapter1, false],
53
+ });
54
+ }
55
+ if (amount > 0n) {
56
+ if (requirementSignature) {
57
+ actions.push(...(0, getRequirementsAction_1.getRequirementsAction)({
58
+ chainId,
59
+ asset: marketParams.collateralToken,
60
+ amount,
61
+ requirementSignature,
62
+ }));
63
+ }
64
+ else {
65
+ actions.push({
66
+ type: "erc20TransferFrom",
67
+ args: [marketParams.collateralToken, amount, generalAdapter1, false],
68
+ });
69
+ }
70
+ }
71
+ actions.push({
72
+ type: "morphoSupplyCollateral",
73
+ args: [marketParams, totalCollateral, onBehalf, [], false],
74
+ });
75
+ let reallocationFee = 0n;
76
+ if (reallocations && reallocations.length > 0) {
77
+ const result = (0, buildReallocationActions_1.buildReallocationActions)(reallocations, marketParams);
78
+ actions.push(...result.actions);
79
+ reallocationFee = result.fee;
80
+ }
81
+ actions.push({
82
+ type: "morphoBorrow",
83
+ args: [marketParams, borrowAmount, 0n, minSharePrice, receiver, false],
84
+ });
85
+ let tx = {
86
+ ...bundler_sdk_viem_1.BundlerAction.encodeBundle(chainId, actions),
87
+ value: (nativeAmount ?? 0n) + reallocationFee,
88
+ };
89
+ if (metadata) {
90
+ tx = (0, helpers_1.addTransactionMetadata)(tx, metadata);
91
+ }
92
+ return (0, morpho_ts_1.deepFreeze)({
93
+ ...tx,
94
+ action: {
95
+ type: "marketV1SupplyCollateralBorrow",
96
+ args: {
97
+ market: marketParams.id,
98
+ collateralAmount: totalCollateral,
99
+ borrowAmount,
100
+ minSharePrice,
101
+ onBehalf,
102
+ receiver,
103
+ nativeAmount,
104
+ reallocationFee,
105
+ },
106
+ },
107
+ });
108
+ };
109
+ exports.marketV1SupplyCollateralBorrow = marketV1SupplyCollateralBorrow;
@@ -0,0 +1,28 @@
1
+ import { type MarketParams } from "@morpho-org/blue-sdk";
2
+ import { type Address } from "viem";
3
+ import { type MarketV1WithdrawCollateralAction, type Metadata, type Transaction } from "../../types";
4
+ /** Parameters for {@link marketV1WithdrawCollateral}. */
5
+ export interface MarketV1WithdrawCollateralParams {
6
+ market: {
7
+ readonly chainId: number;
8
+ readonly marketParams: MarketParams;
9
+ };
10
+ args: {
11
+ amount: bigint;
12
+ onBehalf: Address;
13
+ receiver: Address;
14
+ };
15
+ metadata?: Metadata;
16
+ }
17
+ /**
18
+ * Prepares a withdraw-collateral transaction for a Morpho Blue market.
19
+ *
20
+ * Direct call to `morpho.withdrawCollateral`. No bundler needed — collateral
21
+ * flows out of Morpho, so there is no attack surface requiring the bundler.
22
+ *
23
+ * The caller (`msg.sender`) must be `onBehalf` or be authorized by them.
24
+ *
25
+ * @param params - Withdraw collateral parameters.
26
+ * @returns Deep-frozen transaction.
27
+ */
28
+ export declare const marketV1WithdrawCollateral: ({ market: { chainId, marketParams }, args: { amount, onBehalf, receiver }, metadata, }: MarketV1WithdrawCollateralParams) => Readonly<Transaction<MarketV1WithdrawCollateralAction>>;