@morpho-org/morpho-sdk 4.2.0 → 5.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 (191) hide show
  1. package/README.md +16 -16
  2. package/lib/cjs/actions/blue/borrow.d.ts +12 -2
  3. package/lib/cjs/actions/blue/borrow.js +7 -1
  4. package/lib/cjs/actions/blue/buildAssetFundingActions.d.ts +47 -0
  5. package/lib/cjs/actions/blue/buildAssetFundingActions.js +63 -0
  6. package/lib/cjs/actions/blue/refinance.d.ts +13 -3
  7. package/lib/cjs/actions/blue/refinance.js +8 -2
  8. package/lib/cjs/actions/blue/repay.d.ts +44 -43
  9. package/lib/cjs/actions/blue/repay.js +70 -59
  10. package/lib/cjs/actions/blue/repayWithdrawCollateral.d.ts +50 -41
  11. package/lib/cjs/actions/blue/repayWithdrawCollateral.js +75 -54
  12. package/lib/cjs/actions/blue/resolveRepayFunding.d.ts +69 -0
  13. package/lib/cjs/actions/blue/resolveRepayFunding.js +82 -0
  14. package/lib/cjs/actions/blue/supply.d.ts +8 -8
  15. package/lib/cjs/actions/blue/supply.js +11 -35
  16. package/lib/cjs/actions/blue/supplyCollateral.d.ts +9 -9
  17. package/lib/cjs/actions/blue/supplyCollateral.js +12 -36
  18. package/lib/cjs/actions/blue/supplyCollateralBorrow.d.ts +19 -7
  19. package/lib/cjs/actions/blue/supplyCollateralBorrow.js +17 -32
  20. package/lib/cjs/actions/blue/withdraw.d.ts +10 -2
  21. package/lib/cjs/actions/blue/withdraw.js +7 -1
  22. package/lib/cjs/actions/index.d.ts +1 -0
  23. package/lib/cjs/actions/index.js +1 -0
  24. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +49 -0
  25. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +111 -0
  26. package/lib/cjs/actions/requirements/blue/index.d.ts +1 -0
  27. package/lib/cjs/actions/requirements/blue/index.js +17 -0
  28. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +55 -0
  29. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +89 -0
  30. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +6 -5
  31. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +17 -5
  32. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +9 -7
  33. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +18 -4
  34. package/lib/cjs/actions/requirements/encode/{encodeErc20Permit2.d.ts → encodeErc20Permit2Approve.d.ts} +11 -13
  35. package/lib/cjs/actions/requirements/encode/{encodeErc20Permit2.js → encodeErc20Permit2Approve.js} +10 -14
  36. package/lib/cjs/actions/requirements/encode/index.d.ts +2 -1
  37. package/lib/cjs/actions/requirements/encode/index.js +2 -1
  38. package/lib/cjs/actions/requirements/{getRequirements.d.ts → generalAdapter/getGeneralAdapterRequirements.d.ts} +34 -20
  39. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirements.js +151 -0
  40. package/lib/{esm/actions/requirements/getRequirementsPermit.d.ts → cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts} +6 -14
  41. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.js +50 -0
  42. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.d.ts +57 -0
  43. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.js +74 -0
  44. package/lib/cjs/actions/requirements/generalAdapter/index.d.ts +3 -0
  45. package/lib/cjs/actions/requirements/generalAdapter/index.js +19 -0
  46. package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +3 -2
  47. package/lib/cjs/actions/requirements/getRequirementsApproval.js +3 -2
  48. package/lib/cjs/actions/requirements/index.d.ts +2 -5
  49. package/lib/cjs/actions/requirements/index.js +2 -5
  50. package/lib/cjs/actions/signatures/getBlueAuthorizationAction.d.ts +29 -0
  51. package/lib/cjs/actions/signatures/getBlueAuthorizationAction.js +48 -0
  52. package/lib/{esm/actions/requirements/getRequirementsAction.d.ts → cjs/actions/signatures/getTokenRequirementActions.d.ts} +17 -17
  53. package/lib/cjs/actions/{requirements/getRequirementsAction.js → signatures/getTokenRequirementActions.js} +26 -13
  54. package/lib/cjs/actions/signatures/index.d.ts +2 -0
  55. package/lib/cjs/actions/signatures/index.js +18 -0
  56. package/lib/cjs/actions/vaultV1/deposit.d.ts +6 -8
  57. package/lib/cjs/actions/vaultV1/deposit.js +11 -21
  58. package/lib/cjs/actions/vaultV1/migrateToV2.d.ts +6 -6
  59. package/lib/cjs/actions/vaultV1/migrateToV2.js +12 -20
  60. package/lib/cjs/actions/vaultV2/deposit.d.ts +6 -8
  61. package/lib/cjs/actions/vaultV2/deposit.js +11 -21
  62. package/lib/cjs/addresses.d.ts +2 -2
  63. package/lib/cjs/addresses.js +2 -1
  64. package/lib/cjs/bundler/actions.d.ts +44 -4
  65. package/lib/cjs/bundler/actions.js +75 -2
  66. package/lib/cjs/bundler/types.d.ts +6 -0
  67. package/lib/cjs/constants.d.ts +2 -2
  68. package/lib/cjs/constants.js +3 -3
  69. package/lib/cjs/entities/blue/blue.d.ts +58 -33
  70. package/lib/cjs/entities/blue/blue.js +309 -189
  71. package/lib/cjs/entities/reallocationData.d.ts +0 -44
  72. package/lib/cjs/entities/reallocationData.js +3 -50
  73. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +9 -9
  74. package/lib/cjs/entities/vaultV1/vaultV1.js +44 -34
  75. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +6 -6
  76. package/lib/cjs/entities/vaultV2/vaultV2.js +21 -16
  77. package/lib/cjs/errors.d.ts +3 -2
  78. package/lib/cjs/errors.js +5 -2
  79. package/lib/cjs/helpers/index.d.ts +1 -1
  80. package/lib/cjs/helpers/index.js +1 -2
  81. package/lib/cjs/helpers/validate.d.ts +2 -20
  82. package/lib/cjs/helpers/validate.js +2 -39
  83. package/lib/cjs/types/action.d.ts +164 -12
  84. package/lib/cjs/types/action.js +79 -5
  85. package/lib/cjs/types/error.d.ts +97 -2
  86. package/lib/cjs/types/error.js +113 -7
  87. package/lib/cjs/types.d.ts +2 -1
  88. package/lib/cjs/utils.d.ts +2 -2
  89. package/lib/cjs/utils.js +2 -4
  90. package/lib/esm/actions/blue/borrow.d.ts +12 -2
  91. package/lib/esm/actions/blue/borrow.js +7 -1
  92. package/lib/esm/actions/blue/buildAssetFundingActions.d.ts +47 -0
  93. package/lib/esm/actions/blue/buildAssetFundingActions.js +59 -0
  94. package/lib/esm/actions/blue/refinance.d.ts +13 -3
  95. package/lib/esm/actions/blue/refinance.js +8 -2
  96. package/lib/esm/actions/blue/repay.d.ts +44 -43
  97. package/lib/esm/actions/blue/repay.js +71 -60
  98. package/lib/esm/actions/blue/repayWithdrawCollateral.d.ts +50 -41
  99. package/lib/esm/actions/blue/repayWithdrawCollateral.js +76 -55
  100. package/lib/esm/actions/blue/resolveRepayFunding.d.ts +69 -0
  101. package/lib/esm/actions/blue/resolveRepayFunding.js +78 -0
  102. package/lib/esm/actions/blue/supply.d.ts +8 -8
  103. package/lib/esm/actions/blue/supply.js +12 -36
  104. package/lib/esm/actions/blue/supplyCollateral.d.ts +9 -9
  105. package/lib/esm/actions/blue/supplyCollateral.js +13 -37
  106. package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +19 -7
  107. package/lib/esm/actions/blue/supplyCollateralBorrow.js +18 -33
  108. package/lib/esm/actions/blue/withdraw.d.ts +10 -2
  109. package/lib/esm/actions/blue/withdraw.js +7 -1
  110. package/lib/esm/actions/index.d.ts +1 -0
  111. package/lib/esm/actions/index.js +1 -0
  112. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +49 -0
  113. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +107 -0
  114. package/lib/esm/actions/requirements/blue/index.d.ts +1 -0
  115. package/lib/esm/actions/requirements/blue/index.js +1 -0
  116. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +55 -0
  117. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +85 -0
  118. package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +6 -5
  119. package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +19 -7
  120. package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +9 -7
  121. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +20 -6
  122. package/lib/esm/actions/requirements/encode/{encodeErc20Permit2.d.ts → encodeErc20Permit2Approve.d.ts} +11 -13
  123. package/lib/esm/actions/requirements/encode/{encodeErc20Permit2.js → encodeErc20Permit2Approve.js} +8 -12
  124. package/lib/esm/actions/requirements/encode/index.d.ts +2 -1
  125. package/lib/esm/actions/requirements/encode/index.js +2 -1
  126. package/lib/esm/actions/requirements/{getRequirements.d.ts → generalAdapter/getGeneralAdapterRequirements.d.ts} +34 -20
  127. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirements.js +147 -0
  128. package/lib/{cjs/actions/requirements/getRequirementsPermit.d.ts → esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts} +6 -14
  129. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.js +46 -0
  130. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.d.ts +57 -0
  131. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.js +70 -0
  132. package/lib/esm/actions/requirements/generalAdapter/index.d.ts +3 -0
  133. package/lib/esm/actions/requirements/generalAdapter/index.js +3 -0
  134. package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +3 -2
  135. package/lib/esm/actions/requirements/getRequirementsApproval.js +3 -2
  136. package/lib/esm/actions/requirements/index.d.ts +2 -5
  137. package/lib/esm/actions/requirements/index.js +2 -5
  138. package/lib/esm/actions/signatures/getBlueAuthorizationAction.d.ts +29 -0
  139. package/lib/esm/actions/signatures/getBlueAuthorizationAction.js +44 -0
  140. package/lib/{cjs/actions/requirements/getRequirementsAction.d.ts → esm/actions/signatures/getTokenRequirementActions.d.ts} +17 -17
  141. package/lib/esm/actions/{requirements/getRequirementsAction.js → signatures/getTokenRequirementActions.js} +24 -11
  142. package/lib/esm/actions/signatures/index.d.ts +2 -0
  143. package/lib/esm/actions/signatures/index.js +2 -0
  144. package/lib/esm/actions/vaultV1/deposit.d.ts +6 -8
  145. package/lib/esm/actions/vaultV1/deposit.js +11 -21
  146. package/lib/esm/actions/vaultV1/migrateToV2.d.ts +6 -6
  147. package/lib/esm/actions/vaultV1/migrateToV2.js +12 -20
  148. package/lib/esm/actions/vaultV2/deposit.d.ts +6 -8
  149. package/lib/esm/actions/vaultV2/deposit.js +11 -21
  150. package/lib/esm/addresses.d.ts +2 -2
  151. package/lib/esm/addresses.js +1 -1
  152. package/lib/esm/bundler/actions.d.ts +44 -4
  153. package/lib/esm/bundler/actions.js +77 -4
  154. package/lib/esm/bundler/types.d.ts +6 -0
  155. package/lib/esm/constants.d.ts +2 -2
  156. package/lib/esm/constants.js +2 -2
  157. package/lib/esm/entities/blue/blue.d.ts +58 -33
  158. package/lib/esm/entities/blue/blue.js +311 -191
  159. package/lib/esm/entities/reallocationData.d.ts +0 -44
  160. package/lib/esm/entities/reallocationData.js +2 -49
  161. package/lib/esm/entities/vaultV1/vaultV1.d.ts +9 -9
  162. package/lib/esm/entities/vaultV1/vaultV1.js +46 -36
  163. package/lib/esm/entities/vaultV2/vaultV2.d.ts +6 -6
  164. package/lib/esm/entities/vaultV2/vaultV2.js +23 -18
  165. package/lib/esm/errors.d.ts +3 -2
  166. package/lib/esm/errors.js +2 -1
  167. package/lib/esm/helpers/index.d.ts +1 -1
  168. package/lib/esm/helpers/index.js +1 -1
  169. package/lib/esm/helpers/validate.d.ts +2 -20
  170. package/lib/esm/helpers/validate.js +2 -38
  171. package/lib/esm/types/action.d.ts +164 -12
  172. package/lib/esm/types/action.js +75 -4
  173. package/lib/esm/types/error.d.ts +97 -2
  174. package/lib/esm/types/error.js +107 -5
  175. package/lib/esm/types.d.ts +2 -1
  176. package/lib/esm/utils.d.ts +2 -2
  177. package/lib/esm/utils.js +2 -2
  178. package/package.json +22 -21
  179. package/lib/cjs/actions/requirements/getMorphoAuthorizationRequirement.d.ts +0 -37
  180. package/lib/cjs/actions/requirements/getMorphoAuthorizationRequirement.js +0 -71
  181. package/lib/cjs/actions/requirements/getRequirements.js +0 -112
  182. package/lib/cjs/actions/requirements/getRequirementsPermit.js +0 -57
  183. package/lib/cjs/actions/requirements/getRequirementsPermit2.d.ts +0 -63
  184. package/lib/cjs/actions/requirements/getRequirementsPermit2.js +0 -85
  185. package/lib/esm/actions/requirements/getMorphoAuthorizationRequirement.d.ts +0 -37
  186. package/lib/esm/actions/requirements/getMorphoAuthorizationRequirement.js +0 -67
  187. package/lib/esm/actions/requirements/getRequirements.js +0 -108
  188. package/lib/esm/actions/requirements/getRequirementsPermit.js +0 -53
  189. package/lib/esm/actions/requirements/getRequirementsPermit2.d.ts +0 -63
  190. package/lib/esm/actions/requirements/getRequirementsPermit2.js +0 -81
  191. package/src/index.ts +0 -4
@@ -60,7 +60,7 @@ class MorphoBlue {
60
60
  slippageTolerance,
61
61
  });
62
62
  return {
63
- getRequirements: (params) => (0, index_js_1.getRequirements)(this.client.viemClient, {
63
+ getRequirements: (params) => (0, index_js_1.getGeneralAdapterRequirements)(this.client.viemClient, {
64
64
  address: this.marketParams.loanToken,
65
65
  chainId: this.chainId,
66
66
  supportSignature: this.client.options.supportSignature,
@@ -68,17 +68,22 @@ class MorphoBlue {
68
68
  useSimplePermit: params?.useSimplePermit,
69
69
  args: { amount, from: userAddress },
70
70
  }),
71
- buildTx: (requirementSignature) => (0, index_js_1.blueSupply)({
72
- market: { chainId: this.chainId, marketParams: this.marketParams },
73
- args: {
74
- amount,
75
- nativeAmount,
76
- onBehalf: userAddress,
77
- maxSharePrice,
78
- requirementSignature,
79
- },
80
- metadata: this.client.options.metadata,
81
- }),
71
+ buildTx: (signatures) => {
72
+ const { permit } = (0, index_js_3.selectRequirementSignatures)(signatures, {
73
+ permit: true,
74
+ });
75
+ return (0, index_js_1.blueSupply)({
76
+ market: { chainId: this.chainId, marketParams: this.marketParams },
77
+ args: {
78
+ amount,
79
+ nativeAmount,
80
+ onBehalf: userAddress,
81
+ maxSharePrice,
82
+ requirementSignature: permit,
83
+ },
84
+ metadata: this.client.options.metadata,
85
+ });
86
+ },
82
87
  };
83
88
  }
84
89
  withdraw(params) {
@@ -129,24 +134,31 @@ class MorphoBlue {
129
134
  });
130
135
  return {
131
136
  getRequirements: async () => {
132
- const authTx = await (0, index_js_1.getMorphoAuthorizationRequirement)({
137
+ const authTx = await (0, index_js_1.getBlueAuthorizationRequirement)({
133
138
  viemClient: this.client.viemClient,
134
139
  chainId: this.chainId,
135
140
  userAddress,
141
+ supportSignature: this.client.options.supportSignature,
136
142
  });
137
143
  return authTx ? [authTx] : [];
138
144
  },
139
- buildTx: () => (0, index_js_1.blueWithdraw)({
140
- market: { chainId: this.chainId, marketParams: this.marketParams },
141
- args: {
142
- assets,
143
- shares,
144
- receiver,
145
- minSharePrice,
146
- reallocations,
147
- },
148
- metadata: this.client.options.metadata,
149
- }),
145
+ buildTx: (signatures) => {
146
+ const { authorization } = (0, index_js_3.selectRequirementSignatures)(signatures, {
147
+ authorization: true,
148
+ });
149
+ return (0, index_js_1.blueWithdraw)({
150
+ market: { chainId: this.chainId, marketParams: this.marketParams },
151
+ args: {
152
+ assets,
153
+ shares,
154
+ receiver,
155
+ minSharePrice,
156
+ reallocations,
157
+ authorizationSignature: authorization,
158
+ },
159
+ metadata: this.client.options.metadata,
160
+ });
161
+ },
150
162
  };
151
163
  }
152
164
  supplyCollateral({ amount = 0n, userAddress, nativeAmount, }) {
@@ -165,7 +177,7 @@ class MorphoBlue {
165
177
  (0, index_js_2.validateNativeAsset)(this.chainId, this.marketParams.collateralToken);
166
178
  }
167
179
  return {
168
- getRequirements: (params) => (0, index_js_1.getRequirements)(this.client.viemClient, {
180
+ getRequirements: (params) => (0, index_js_1.getGeneralAdapterRequirements)(this.client.viemClient, {
169
181
  address: this.marketParams.collateralToken,
170
182
  chainId: this.chainId,
171
183
  supportSignature: this.client.options.supportSignature,
@@ -173,19 +185,24 @@ class MorphoBlue {
173
185
  useSimplePermit: params?.useSimplePermit,
174
186
  args: { amount, from: userAddress },
175
187
  }),
176
- buildTx: (requirementSignature) => (0, index_js_1.blueSupplyCollateral)({
177
- market: {
178
- chainId: this.chainId,
179
- marketParams: this.marketParams,
180
- },
181
- args: {
182
- amount,
183
- nativeAmount,
184
- onBehalf: userAddress,
185
- requirementSignature,
186
- },
187
- metadata: this.client.options.metadata,
188
- }),
188
+ buildTx: (signatures) => {
189
+ const { permit } = (0, index_js_3.selectRequirementSignatures)(signatures, {
190
+ permit: true,
191
+ });
192
+ return (0, index_js_1.blueSupplyCollateral)({
193
+ market: {
194
+ chainId: this.chainId,
195
+ marketParams: this.marketParams,
196
+ },
197
+ args: {
198
+ amount,
199
+ nativeAmount,
200
+ onBehalf: userAddress,
201
+ requirementSignature: permit,
202
+ },
203
+ metadata: this.client.options.metadata,
204
+ });
205
+ },
189
206
  };
190
207
  }
191
208
  borrow({ amount, userAddress, positionData, slippageTolerance = blue_sdk_1.DEFAULT_SLIPPAGE_TOLERANCE, reallocations, }) {
@@ -216,44 +233,44 @@ class MorphoBlue {
216
233
  });
217
234
  return {
218
235
  getRequirements: async () => {
219
- const authTx = await (0, index_js_1.getMorphoAuthorizationRequirement)({
236
+ const authTx = await (0, index_js_1.getBlueAuthorizationRequirement)({
220
237
  viemClient: this.client.viemClient,
221
238
  chainId: this.chainId,
222
239
  userAddress,
240
+ supportSignature: this.client.options.supportSignature,
223
241
  });
224
242
  return authTx ? [authTx] : [];
225
243
  },
226
- buildTx: () => (0, index_js_1.blueBorrow)({
227
- market: {
228
- chainId: this.chainId,
229
- marketParams: this.marketParams,
230
- },
231
- args: {
232
- amount,
233
- receiver: userAddress,
234
- minSharePrice,
235
- reallocations,
236
- },
237
- metadata: this.client.options.metadata,
238
- }),
244
+ buildTx: (signatures) => {
245
+ const { authorization } = (0, index_js_3.selectRequirementSignatures)(signatures, {
246
+ authorization: true,
247
+ });
248
+ return (0, index_js_1.blueBorrow)({
249
+ market: {
250
+ chainId: this.chainId,
251
+ marketParams: this.marketParams,
252
+ },
253
+ args: {
254
+ amount,
255
+ receiver: userAddress,
256
+ minSharePrice,
257
+ reallocations,
258
+ authorizationSignature: authorization,
259
+ },
260
+ metadata: this.client.options.metadata,
261
+ });
262
+ },
239
263
  };
240
264
  }
241
265
  repay(params) {
242
266
  (0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
243
267
  const { userAddress, positionData, slippageTolerance = blue_sdk_1.DEFAULT_SLIPPAGE_TOLERANCE, } = params;
244
- if ("assets" in params && "shares" in params) {
268
+ if ("amount" in params && "shares" in params) {
245
269
  throw new index_js_3.MutuallyExclusiveRepayAmountsError(this.marketParams.id);
246
270
  }
247
- const isSharesMode = "shares" in params;
248
- if (isSharesMode) {
249
- if (params.shares <= 0n) {
250
- throw new index_js_3.NonPositiveRepayAmountError(this.marketParams.id);
251
- }
252
- }
253
- else {
254
- if (params.assets <= 0n) {
255
- throw new index_js_3.NonPositiveRepayAmountError(this.marketParams.id);
256
- }
271
+ const nativeAmount = params.nativeAmount ?? 0n;
272
+ if (nativeAmount < 0n) {
273
+ throw new index_js_3.NegativeNativeAmountError(nativeAmount);
257
274
  }
258
275
  (0, index_js_2.validateSlippageTolerance)(slippageTolerance);
259
276
  if (!positionData) {
@@ -264,67 +281,116 @@ class MorphoBlue {
264
281
  expectedMarketId: this.marketParams.id,
265
282
  expectedUser: userAddress,
266
283
  });
267
- let assets;
268
- let shares;
269
- let transferAmount;
284
+ if (nativeAmount > 0n) {
285
+ (0, index_js_2.validateNativeAsset)(this.chainId, this.marketParams.loanToken);
286
+ }
287
+ let repayAssets;
288
+ let repayShares;
289
+ let erc20Amount;
270
290
  let marketForRepay;
271
- if (isSharesMode) {
291
+ if ("shares" in params) {
292
+ const shares = params.shares;
293
+ if (shares <= 0n) {
294
+ throw new index_js_3.NonPositiveRepayAmountError(this.marketParams.id);
295
+ }
272
296
  (0, index_js_2.validateRepayShares)({
273
297
  positionData,
274
- repayShares: params.shares,
298
+ repayShares: shares,
275
299
  marketId: this.marketParams.id,
276
300
  });
277
- assets = 0n;
278
- shares = params.shares;
301
+ repayAssets = 0n;
302
+ repayShares = shares;
279
303
  // 2h forward accrual upper-bounds the on-chain repay price; bundle
280
304
  // skims residual back to the receiver.
281
305
  const accrualTimestamp = blue_sdk_1.MathLib.max(morpho_ts_1.Time.timestamp(), positionData.market.lastUpdate) +
282
306
  morpho_ts_1.Time.s.from.h(2n);
283
307
  marketForRepay = positionData.market.accrueInterest(accrualTimestamp);
284
- transferAmount = marketForRepay.toBorrowAssets(shares, "Up");
308
+ const borrowAssets = marketForRepay.toBorrowAssets(shares, "Up");
309
+ // Native funds part of the transfer; the ERC-20 pulled is the remainder.
310
+ if (nativeAmount > borrowAssets) {
311
+ throw new index_js_3.NativeAmountExceedsTransferAmountError({
312
+ nativeAmount,
313
+ transferAmount: borrowAssets,
314
+ market: this.marketParams.id,
315
+ });
316
+ }
317
+ erc20Amount = borrowAssets - nativeAmount;
285
318
  }
286
319
  else {
320
+ // Assets mode is additive, like supply: repaid = amount + nativeAmount.
321
+ const amount = params.amount ?? 0n;
322
+ // Reject a negative ERC-20 amount before it can be masked by nativeAmount
323
+ // in the sum below (a negative amount would otherwise yield a negative
324
+ // erc20Amount and a negative approval in getRequirements).
325
+ if (amount < 0n) {
326
+ throw new index_js_3.NonPositiveRepayAmountError(this.marketParams.id);
327
+ }
328
+ repayAssets = amount + nativeAmount;
329
+ if (repayAssets <= 0n) {
330
+ throw new index_js_3.NonPositiveRepayAmountError(this.marketParams.id);
331
+ }
287
332
  (0, index_js_2.validateRepayAmount)({
288
333
  positionData,
289
- repayAssets: params.assets,
334
+ repayAssets,
290
335
  marketId: this.marketParams.id,
291
336
  });
292
- assets = params.assets;
293
- shares = 0n;
294
- transferAmount = params.assets;
337
+ repayShares = 0n;
338
+ erc20Amount = amount;
295
339
  marketForRepay = positionData.market;
296
340
  }
297
341
  const maxSharePrice = (0, index_js_2.computeMaxRepaySharePrice)({
298
- repayAssets: assets,
299
- repayShares: shares,
342
+ repayAssets,
343
+ repayShares,
300
344
  market: marketForRepay,
301
345
  slippageTolerance,
302
346
  });
303
347
  return {
304
- getRequirements: (reqParams) => (0, index_js_1.getRequirements)(this.client.viemClient, {
305
- address: this.marketParams.loanToken,
306
- chainId: this.chainId,
307
- supportSignature: this.client.options.supportSignature,
308
- supportDeployless: this.client.options.supportDeployless,
309
- useSimplePermit: reqParams?.useSimplePermit,
310
- args: { amount: transferAmount, from: userAddress },
311
- }),
312
- buildTx: (requirementSignature) => (0, index_js_1.blueRepay)({
313
- market: {
348
+ getRequirements: (reqParams) => {
349
+ // Fully native repay pulls no ERC-20, so it needs no approval/permit.
350
+ if (erc20Amount === 0n)
351
+ return Promise.resolve([]);
352
+ return (0, index_js_1.getGeneralAdapterRequirements)(this.client.viemClient, {
353
+ address: this.marketParams.loanToken,
314
354
  chainId: this.chainId,
315
- marketParams: this.marketParams,
316
- },
317
- args: {
318
- assets,
319
- shares,
320
- transferAmount,
321
- onBehalf: userAddress,
322
- receiver: userAddress,
323
- maxSharePrice,
324
- requirementSignature,
325
- },
326
- metadata: this.client.options.metadata,
327
- }),
355
+ supportSignature: this.client.options.supportSignature,
356
+ supportDeployless: this.client.options.supportDeployless,
357
+ useSimplePermit: reqParams?.useSimplePermit,
358
+ args: { amount: erc20Amount, from: userAddress },
359
+ });
360
+ },
361
+ buildTx: (signatures) => {
362
+ const { permit } = (0, index_js_3.selectRequirementSignatures)(signatures, {
363
+ permit: true,
364
+ });
365
+ return (0, index_js_1.blueRepay)({
366
+ market: {
367
+ chainId: this.chainId,
368
+ marketParams: this.marketParams,
369
+ },
370
+ // Shares mode: repay `shares`, ERC-20 to pull = `erc20Amount`.
371
+ // Assets mode: repay `repayAssets` (= amount + native), pull `erc20Amount`.
372
+ args: repayShares > 0n
373
+ ? {
374
+ shares: repayShares,
375
+ transferAmount: erc20Amount,
376
+ nativeAmount,
377
+ onBehalf: userAddress,
378
+ receiver: userAddress,
379
+ maxSharePrice,
380
+ requirementSignature: permit,
381
+ }
382
+ : {
383
+ amount: erc20Amount,
384
+ transferAmount: repayAssets,
385
+ nativeAmount,
386
+ onBehalf: userAddress,
387
+ receiver: userAddress,
388
+ maxSharePrice,
389
+ requirementSignature: permit,
390
+ },
391
+ metadata: this.client.options.metadata,
392
+ });
393
+ },
328
394
  };
329
395
  }
330
396
  withdrawCollateral({ userAddress, amount, positionData, }) {
@@ -371,19 +437,12 @@ class MorphoBlue {
371
437
  repayWithdrawCollateral(params) {
372
438
  (0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
373
439
  const { userAddress, withdrawAmount, positionData, slippageTolerance = blue_sdk_1.DEFAULT_SLIPPAGE_TOLERANCE, } = params;
374
- if ("assets" in params && "shares" in params) {
440
+ if ("amount" in params && "shares" in params) {
375
441
  throw new index_js_3.MutuallyExclusiveRepayAmountsError(this.marketParams.id);
376
442
  }
377
- const isSharesMode = "shares" in params;
378
- if (isSharesMode) {
379
- if (params.shares <= 0n) {
380
- throw new index_js_3.NonPositiveRepayAmountError(this.marketParams.id);
381
- }
382
- }
383
- else {
384
- if (params.assets <= 0n) {
385
- throw new index_js_3.NonPositiveRepayAmountError(this.marketParams.id);
386
- }
443
+ const nativeAmount = params.nativeAmount ?? 0n;
444
+ if (nativeAmount < 0n) {
445
+ throw new index_js_3.NegativeNativeAmountError(nativeAmount);
387
446
  }
388
447
  if (withdrawAmount <= 0n) {
389
448
  throw new index_js_3.NonPositiveWithdrawCollateralAmountError(this.marketParams.id);
@@ -397,34 +456,61 @@ class MorphoBlue {
397
456
  expectedMarketId: this.marketParams.id,
398
457
  expectedUser: userAddress,
399
458
  });
400
- let assets;
401
- let shares;
402
- let transferAmount;
459
+ if (nativeAmount > 0n) {
460
+ (0, index_js_2.validateNativeAsset)(this.chainId, this.marketParams.loanToken);
461
+ }
462
+ let repayAssets;
463
+ let repayShares;
464
+ let erc20Amount;
403
465
  let marketForRepay;
404
466
  // 2h forward accrual upper-bounds the on-chain repay price (shares
405
467
  // mode) and the post-repay health check; bundle skims residual back.
406
468
  const accrualTimestamp = blue_sdk_1.MathLib.max(morpho_ts_1.Time.timestamp(), positionData.market.lastUpdate) +
407
469
  morpho_ts_1.Time.s.from.h(2n);
408
- if (isSharesMode) {
470
+ if ("shares" in params) {
471
+ const shares = params.shares;
472
+ if (shares <= 0n) {
473
+ throw new index_js_3.NonPositiveRepayAmountError(this.marketParams.id);
474
+ }
409
475
  (0, index_js_2.validateRepayShares)({
410
476
  positionData,
411
- repayShares: params.shares,
477
+ repayShares: shares,
412
478
  marketId: this.marketParams.id,
413
479
  });
414
- assets = 0n;
415
- shares = params.shares;
480
+ repayAssets = 0n;
481
+ repayShares = shares;
416
482
  marketForRepay = positionData.market.accrueInterest(accrualTimestamp);
417
- transferAmount = marketForRepay.toBorrowAssets(shares, "Up");
483
+ const borrowAssets = marketForRepay.toBorrowAssets(shares, "Up");
484
+ // Native funds part of the transfer; the ERC-20 pulled is the remainder.
485
+ if (nativeAmount > borrowAssets) {
486
+ throw new index_js_3.NativeAmountExceedsTransferAmountError({
487
+ nativeAmount,
488
+ transferAmount: borrowAssets,
489
+ market: this.marketParams.id,
490
+ });
491
+ }
492
+ erc20Amount = borrowAssets - nativeAmount;
418
493
  }
419
494
  else {
495
+ // Assets mode is additive, like supply: repaid = amount + nativeAmount.
496
+ const amount = params.amount ?? 0n;
497
+ // Reject a negative ERC-20 amount before it can be masked by nativeAmount
498
+ // in the sum below (a negative amount would otherwise yield a negative
499
+ // erc20Amount and a negative approval in getRequirements).
500
+ if (amount < 0n) {
501
+ throw new index_js_3.NonPositiveRepayAmountError(this.marketParams.id);
502
+ }
503
+ repayAssets = amount + nativeAmount;
504
+ if (repayAssets <= 0n) {
505
+ throw new index_js_3.NonPositiveRepayAmountError(this.marketParams.id);
506
+ }
420
507
  (0, index_js_2.validateRepayAmount)({
421
508
  positionData,
422
- repayAssets: params.assets,
509
+ repayAssets,
423
510
  marketId: this.marketParams.id,
424
511
  });
425
- assets = params.assets;
426
- shares = 0n;
427
- transferAmount = params.assets;
512
+ repayShares = 0n;
513
+ erc20Amount = amount;
428
514
  marketForRepay = positionData.market;
429
515
  }
430
516
  if (withdrawAmount > positionData.collateral) {
@@ -434,7 +520,7 @@ class MorphoBlue {
434
520
  market: positionData.marketId,
435
521
  });
436
522
  }
437
- const { position: positionAfterRepay } = positionData.repay(assets, shares, accrualTimestamp);
523
+ const { position: positionAfterRepay } = positionData.repay(repayAssets, repayShares, accrualTimestamp);
438
524
  (0, index_js_2.validatePositionHealthAfterWithdraw)({
439
525
  positionData: positionAfterRepay,
440
526
  withdrawAmount,
@@ -442,47 +528,69 @@ class MorphoBlue {
442
528
  marketId: this.marketParams.id,
443
529
  });
444
530
  const maxSharePrice = (0, index_js_2.computeMaxRepaySharePrice)({
445
- repayAssets: assets,
446
- repayShares: shares,
531
+ repayAssets,
532
+ repayShares,
447
533
  market: marketForRepay,
448
534
  slippageTolerance,
449
535
  });
450
536
  return {
451
537
  getRequirements: async (reqParams) => {
452
538
  const [erc20Requirements, authTx] = await Promise.all([
453
- (0, index_js_1.getRequirements)(this.client.viemClient, {
454
- address: this.marketParams.loanToken,
455
- chainId: this.chainId,
456
- supportSignature: this.client.options.supportSignature,
457
- supportDeployless: this.client.options.supportDeployless,
458
- useSimplePermit: reqParams?.useSimplePermit,
459
- args: { amount: transferAmount, from: userAddress },
460
- }),
461
- (0, index_js_1.getMorphoAuthorizationRequirement)({
539
+ // Fully native repay pulls no ERC-20, so it needs no approval/permit.
540
+ erc20Amount === 0n
541
+ ? Promise.resolve([])
542
+ : (0, index_js_1.getGeneralAdapterRequirements)(this.client.viemClient, {
543
+ address: this.marketParams.loanToken,
544
+ chainId: this.chainId,
545
+ supportSignature: this.client.options.supportSignature,
546
+ supportDeployless: this.client.options.supportDeployless,
547
+ useSimplePermit: reqParams?.useSimplePermit,
548
+ args: { amount: erc20Amount, from: userAddress },
549
+ }),
550
+ (0, index_js_1.getBlueAuthorizationRequirement)({
462
551
  viemClient: this.client.viemClient,
463
552
  chainId: this.chainId,
464
553
  userAddress,
554
+ supportSignature: this.client.options.supportSignature,
465
555
  }),
466
556
  ]);
467
557
  return [...erc20Requirements, ...(authTx ? [authTx] : [])];
468
558
  },
469
- buildTx: (requirementSignature) => (0, index_js_1.blueRepayWithdrawCollateral)({
470
- market: {
471
- chainId: this.chainId,
472
- marketParams: this.marketParams,
473
- },
474
- args: {
475
- assets,
476
- shares,
477
- transferAmount,
478
- withdrawAmount,
479
- onBehalf: userAddress,
480
- receiver: userAddress,
481
- maxSharePrice,
482
- requirementSignature,
483
- },
484
- metadata: this.client.options.metadata,
485
- }),
559
+ buildTx: (signatures) => {
560
+ const { permit, authorization } = (0, index_js_3.selectRequirementSignatures)(signatures, { permit: true, authorization: true });
561
+ return (0, index_js_1.blueRepayWithdrawCollateral)({
562
+ market: {
563
+ chainId: this.chainId,
564
+ marketParams: this.marketParams,
565
+ },
566
+ // Shares mode: repay `shares`, ERC-20 to pull = `erc20Amount`.
567
+ // Assets mode: repay `repayAssets` (= amount + native), pull `erc20Amount`.
568
+ args: repayShares > 0n
569
+ ? {
570
+ shares: repayShares,
571
+ transferAmount: erc20Amount,
572
+ nativeAmount,
573
+ withdrawAmount,
574
+ onBehalf: userAddress,
575
+ receiver: userAddress,
576
+ maxSharePrice,
577
+ requirementSignature: permit,
578
+ authorizationSignature: authorization,
579
+ }
580
+ : {
581
+ amount: erc20Amount,
582
+ transferAmount: repayAssets,
583
+ nativeAmount,
584
+ withdrawAmount,
585
+ onBehalf: userAddress,
586
+ receiver: userAddress,
587
+ maxSharePrice,
588
+ requirementSignature: permit,
589
+ authorizationSignature: authorization,
590
+ },
591
+ metadata: this.client.options.metadata,
592
+ });
593
+ },
486
594
  };
487
595
  }
488
596
  supplyCollateralBorrow({ amount = 0n, userAddress, positionData, borrowAmount, nativeAmount, slippageTolerance = blue_sdk_1.DEFAULT_SLIPPAGE_TOLERANCE, reallocations, }) {
@@ -527,7 +635,7 @@ class MorphoBlue {
527
635
  return {
528
636
  getRequirements: async (params) => {
529
637
  const [erc20Requirements, authTx] = await Promise.all([
530
- (0, index_js_1.getRequirements)(this.client.viemClient, {
638
+ (0, index_js_1.getGeneralAdapterRequirements)(this.client.viemClient, {
531
639
  address: this.marketParams.collateralToken,
532
640
  chainId: this.chainId,
533
641
  supportSignature: this.client.options.supportSignature,
@@ -535,31 +643,36 @@ class MorphoBlue {
535
643
  useSimplePermit: params?.useSimplePermit,
536
644
  args: { amount, from: userAddress },
537
645
  }),
538
- (0, index_js_1.getMorphoAuthorizationRequirement)({
646
+ (0, index_js_1.getBlueAuthorizationRequirement)({
539
647
  viemClient: this.client.viemClient,
540
648
  chainId: this.chainId,
541
649
  userAddress,
650
+ supportSignature: this.client.options.supportSignature,
542
651
  }),
543
652
  ]);
544
653
  return [...erc20Requirements, ...(authTx ? [authTx] : [])];
545
654
  },
546
- buildTx: (requirementSignature) => (0, index_js_1.blueSupplyCollateralBorrow)({
547
- market: {
548
- chainId: this.chainId,
549
- marketParams: this.marketParams,
550
- },
551
- args: {
552
- amount,
553
- nativeAmount,
554
- borrowAmount,
555
- onBehalf: userAddress,
556
- receiver: userAddress,
557
- minSharePrice,
558
- requirementSignature,
559
- reallocations,
560
- },
561
- metadata: this.client.options.metadata,
562
- }),
655
+ buildTx: (signatures) => {
656
+ const { permit, authorization } = (0, index_js_3.selectRequirementSignatures)(signatures, { permit: true, authorization: true });
657
+ return (0, index_js_1.blueSupplyCollateralBorrow)({
658
+ market: {
659
+ chainId: this.chainId,
660
+ marketParams: this.marketParams,
661
+ },
662
+ args: {
663
+ amount,
664
+ nativeAmount,
665
+ borrowAmount,
666
+ onBehalf: userAddress,
667
+ receiver: userAddress,
668
+ minSharePrice,
669
+ requirementSignature: permit,
670
+ authorizationSignature: authorization,
671
+ reallocations,
672
+ },
673
+ metadata: this.client.options.metadata,
674
+ });
675
+ },
563
676
  };
564
677
  }
565
678
  refinance({ userAddress, positionData, target, collateralAmount, borrowAssets, borrowShares, slippageTolerance = blue_sdk_1.DEFAULT_SLIPPAGE_TOLERANCE, targetReallocations, }) {
@@ -693,30 +806,37 @@ class MorphoBlue {
693
806
  : 0n;
694
807
  return {
695
808
  getRequirements: async () => {
696
- const authTx = await (0, index_js_1.getMorphoAuthorizationRequirement)({
809
+ const authTx = await (0, index_js_1.getBlueAuthorizationRequirement)({
697
810
  viemClient: this.client.viemClient,
698
811
  chainId: this.chainId,
699
812
  userAddress,
813
+ supportSignature: this.client.options.supportSignature,
700
814
  });
701
815
  return authTx ? [authTx] : [];
702
816
  },
703
- buildTx: () => (0, index_js_1.blueRefinance)({
704
- source: {
705
- chainId: this.chainId,
706
- marketParams: this.marketParams,
707
- },
708
- target: { marketParams: target.marketParams },
709
- args: {
710
- user: userAddress,
711
- collateralAmount,
712
- borrowAssets: borrowAssetsAdjusted,
713
- borrowShares: requestedShares,
714
- minBorrowSharePrice,
715
- maxRepaySharePrice,
716
- targetReallocations,
717
- },
718
- metadata: this.client.options.metadata,
719
- }),
817
+ buildTx: (signatures) => {
818
+ const { authorization } = (0, index_js_3.selectRequirementSignatures)(signatures, {
819
+ authorization: true,
820
+ });
821
+ return (0, index_js_1.blueRefinance)({
822
+ source: {
823
+ chainId: this.chainId,
824
+ marketParams: this.marketParams,
825
+ },
826
+ target: { marketParams: target.marketParams },
827
+ args: {
828
+ user: userAddress,
829
+ collateralAmount,
830
+ borrowAssets: borrowAssetsAdjusted,
831
+ borrowShares: requestedShares,
832
+ minBorrowSharePrice,
833
+ maxRepaySharePrice,
834
+ targetReallocations,
835
+ authorizationSignature: authorization,
836
+ },
837
+ metadata: this.client.options.metadata,
838
+ });
839
+ },
720
840
  };
721
841
  }
722
842
  /**