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