@morpho-org/morpho-sdk 5.8.0 → 6.0.0-next.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 (167) hide show
  1. package/MIGRATION-v5-to-v6.md +201 -0
  2. package/README.md +62 -40
  3. package/lib/cjs/abis.d.ts +543 -0
  4. package/lib/cjs/abis.js +738 -0
  5. package/lib/cjs/actions/blue/borrow.d.ts +62 -58
  6. package/lib/cjs/actions/blue/borrow.js +48 -80
  7. package/lib/cjs/actions/blue/common.d.ts +99 -0
  8. package/lib/cjs/actions/blue/common.js +318 -0
  9. package/lib/cjs/actions/blue/refinance.d.ts +72 -107
  10. package/lib/cjs/actions/blue/refinance.js +97 -197
  11. package/lib/cjs/actions/blue/repay.d.ts +72 -68
  12. package/lib/cjs/actions/blue/repay.js +56 -118
  13. package/lib/cjs/actions/blue/repayWithdrawCollateral.d.ts +85 -90
  14. package/lib/cjs/actions/blue/repayWithdrawCollateral.js +148 -137
  15. package/lib/cjs/actions/blue/supply.d.ts +62 -45
  16. package/lib/cjs/actions/blue/supply.js +94 -70
  17. package/lib/cjs/actions/blue/supplyCollateral.d.ts +63 -40
  18. package/lib/cjs/actions/blue/supplyCollateral.js +52 -69
  19. package/lib/cjs/actions/blue/supplyCollateralBorrow.d.ts +82 -86
  20. package/lib/cjs/actions/blue/supplyCollateralBorrow.js +133 -119
  21. package/lib/cjs/actions/blue/withdraw.d.ts +68 -78
  22. package/lib/cjs/actions/blue/withdraw.js +101 -97
  23. package/lib/cjs/actions/blue/withdrawCollateral.d.ts +56 -28
  24. package/lib/cjs/actions/blue/withdrawCollateral.js +45 -50
  25. package/lib/cjs/actions/index.d.ts +1 -1
  26. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +29 -17
  27. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +60 -19
  28. package/lib/cjs/actions/requirements/blue/getBlueBundlesV1TokenRequirements.d.ts +80 -0
  29. package/lib/cjs/actions/requirements/blue/getBlueBundlesV1TokenRequirements.js +207 -0
  30. package/lib/cjs/actions/requirements/blue/getUnusedPermit2Nonce.d.ts +48 -0
  31. package/lib/cjs/actions/requirements/blue/getUnusedPermit2Nonce.js +72 -0
  32. package/lib/cjs/actions/requirements/blue/index.d.ts +2 -0
  33. package/lib/cjs/actions/requirements/blue/index.js +2 -0
  34. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +10 -7
  35. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +30 -5
  36. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +2 -1
  37. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +7 -2
  38. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +16 -7
  39. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +33 -6
  40. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2TransferFrom.d.ts +61 -0
  41. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2TransferFrom.js +123 -0
  42. package/lib/cjs/actions/requirements/encode/index.d.ts +1 -0
  43. package/lib/cjs/actions/requirements/encode/index.js +1 -0
  44. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +1 -1
  45. package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +5 -4
  46. package/lib/cjs/actions/requirements/getRequirementsApproval.js +5 -4
  47. package/lib/cjs/actions/signatures/getTokenRequirementActions.d.ts +1 -0
  48. package/lib/cjs/actions/signatures/getTokenRequirementActions.js +4 -0
  49. package/lib/cjs/blue/types.d.ts +3 -1
  50. package/lib/cjs/blue/utils.d.ts +3 -1
  51. package/lib/cjs/blue/utils.js +1 -0
  52. package/lib/cjs/bundler/actions.d.ts +6 -4
  53. package/lib/cjs/bundler/actions.js +6 -4
  54. package/lib/cjs/bundler/types.d.ts +10 -1
  55. package/lib/cjs/client/morphoViemExtension.d.ts +2 -1
  56. package/lib/cjs/client/morphoViemExtension.js +2 -1
  57. package/lib/cjs/entities/blue/blue.d.ts +817 -360
  58. package/lib/cjs/entities/blue/blue.js +455 -698
  59. package/lib/cjs/entities/index.d.ts +1 -0
  60. package/lib/cjs/entities/vaultV1/vaultV1.js +4 -12
  61. package/lib/cjs/entities/vaultV1ReallocationData.d.ts +18 -3
  62. package/lib/cjs/entities/vaultV1ReallocationData.js +11 -1
  63. package/lib/cjs/entities/vaultV2/vaultV2.js +6 -18
  64. package/lib/cjs/helpers/computeVaultV1Reallocations.d.ts +7 -10
  65. package/lib/cjs/helpers/computeVaultV1Reallocations.js +7 -10
  66. package/lib/cjs/helpers/validate.d.ts +16 -23
  67. package/lib/cjs/helpers/validate.js +32 -48
  68. package/lib/cjs/helpers/validateRequirementSpender.d.ts +2 -1
  69. package/lib/cjs/helpers/validateRequirementSpender.js +2 -0
  70. package/lib/cjs/types/action.d.ts +146 -162
  71. package/lib/cjs/types/action.js +29 -2
  72. package/lib/cjs/types/client.d.ts +9 -0
  73. package/lib/cjs/types/error.d.ts +173 -18
  74. package/lib/cjs/types/error.js +230 -25
  75. package/lib/cjs/types/sharedLiquidity.d.ts +20 -4
  76. package/lib/cjs/types.d.ts +3 -1
  77. package/lib/cjs/utils.d.ts +3 -1
  78. package/lib/cjs/utils.js +1 -0
  79. package/lib/esm/abis.d.ts +543 -0
  80. package/lib/esm/abis.js +737 -0
  81. package/lib/esm/actions/blue/borrow.d.ts +62 -58
  82. package/lib/esm/actions/blue/borrow.js +48 -80
  83. package/lib/esm/actions/blue/common.d.ts +99 -0
  84. package/lib/esm/actions/blue/common.js +306 -0
  85. package/lib/esm/actions/blue/refinance.d.ts +72 -107
  86. package/lib/esm/actions/blue/refinance.js +98 -198
  87. package/lib/esm/actions/blue/repay.d.ts +72 -68
  88. package/lib/esm/actions/blue/repay.js +56 -118
  89. package/lib/esm/actions/blue/repayWithdrawCollateral.d.ts +85 -90
  90. package/lib/esm/actions/blue/repayWithdrawCollateral.js +149 -138
  91. package/lib/esm/actions/blue/supply.d.ts +62 -45
  92. package/lib/esm/actions/blue/supply.js +94 -70
  93. package/lib/esm/actions/blue/supplyCollateral.d.ts +63 -40
  94. package/lib/esm/actions/blue/supplyCollateral.js +52 -69
  95. package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +82 -86
  96. package/lib/esm/actions/blue/supplyCollateralBorrow.js +134 -120
  97. package/lib/esm/actions/blue/withdraw.d.ts +68 -78
  98. package/lib/esm/actions/blue/withdraw.js +100 -96
  99. package/lib/esm/actions/blue/withdrawCollateral.d.ts +56 -28
  100. package/lib/esm/actions/blue/withdrawCollateral.js +45 -50
  101. package/lib/esm/actions/index.d.ts +1 -1
  102. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +29 -17
  103. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +63 -22
  104. package/lib/esm/actions/requirements/blue/getBlueBundlesV1TokenRequirements.d.ts +80 -0
  105. package/lib/esm/actions/requirements/blue/getBlueBundlesV1TokenRequirements.js +203 -0
  106. package/lib/esm/actions/requirements/blue/getUnusedPermit2Nonce.d.ts +48 -0
  107. package/lib/esm/actions/requirements/blue/getUnusedPermit2Nonce.js +68 -0
  108. package/lib/esm/actions/requirements/blue/index.d.ts +2 -0
  109. package/lib/esm/actions/requirements/blue/index.js +2 -0
  110. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +10 -7
  111. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +31 -6
  112. package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +2 -1
  113. package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +8 -3
  114. package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +16 -7
  115. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +34 -7
  116. package/lib/esm/actions/requirements/encode/encodeErc20Permit2TransferFrom.d.ts +61 -0
  117. package/lib/esm/actions/requirements/encode/encodeErc20Permit2TransferFrom.js +119 -0
  118. package/lib/esm/actions/requirements/encode/index.d.ts +1 -0
  119. package/lib/esm/actions/requirements/encode/index.js +1 -0
  120. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +1 -1
  121. package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +5 -4
  122. package/lib/esm/actions/requirements/getRequirementsApproval.js +5 -4
  123. package/lib/esm/actions/signatures/getTokenRequirementActions.d.ts +1 -0
  124. package/lib/esm/actions/signatures/getTokenRequirementActions.js +5 -1
  125. package/lib/esm/blue/types.d.ts +3 -1
  126. package/lib/esm/blue/utils.d.ts +3 -1
  127. package/lib/esm/blue/utils.js +3 -1
  128. package/lib/esm/bundler/actions.d.ts +6 -4
  129. package/lib/esm/bundler/actions.js +6 -4
  130. package/lib/esm/bundler/types.d.ts +10 -1
  131. package/lib/esm/client/morphoViemExtension.d.ts +2 -1
  132. package/lib/esm/client/morphoViemExtension.js +2 -1
  133. package/lib/esm/entities/blue/blue.d.ts +817 -360
  134. package/lib/esm/entities/blue/blue.js +461 -704
  135. package/lib/esm/entities/index.d.ts +1 -0
  136. package/lib/esm/entities/vaultV1/vaultV1.js +4 -12
  137. package/lib/esm/entities/vaultV1ReallocationData.d.ts +18 -3
  138. package/lib/esm/entities/vaultV1ReallocationData.js +13 -2
  139. package/lib/esm/entities/vaultV2/vaultV2.js +7 -19
  140. package/lib/esm/helpers/computeVaultV1Reallocations.d.ts +7 -10
  141. package/lib/esm/helpers/computeVaultV1Reallocations.js +7 -10
  142. package/lib/esm/helpers/validate.d.ts +16 -23
  143. package/lib/esm/helpers/validate.js +32 -48
  144. package/lib/esm/helpers/validateRequirementSpender.d.ts +2 -1
  145. package/lib/esm/helpers/validateRequirementSpender.js +2 -0
  146. package/lib/esm/types/action.d.ts +146 -162
  147. package/lib/esm/types/action.js +28 -2
  148. package/lib/esm/types/client.d.ts +9 -0
  149. package/lib/esm/types/error.d.ts +173 -18
  150. package/lib/esm/types/error.js +216 -21
  151. package/lib/esm/types/sharedLiquidity.d.ts +20 -4
  152. package/lib/esm/types.d.ts +3 -1
  153. package/lib/esm/utils.d.ts +3 -1
  154. package/lib/esm/utils.js +3 -1
  155. package/package.json +7 -6
  156. package/lib/cjs/actions/blue/buildAssetFundingActions.d.ts +0 -47
  157. package/lib/cjs/actions/blue/buildAssetFundingActions.js +0 -63
  158. package/lib/cjs/actions/blue/buildReallocationActions.d.ts +0 -69
  159. package/lib/cjs/actions/blue/buildReallocationActions.js +0 -141
  160. package/lib/cjs/actions/blue/resolveRepayFunding.d.ts +0 -66
  161. package/lib/cjs/actions/blue/resolveRepayFunding.js +0 -85
  162. package/lib/esm/actions/blue/buildAssetFundingActions.d.ts +0 -47
  163. package/lib/esm/actions/blue/buildAssetFundingActions.js +0 -59
  164. package/lib/esm/actions/blue/buildReallocationActions.d.ts +0 -69
  165. package/lib/esm/actions/blue/buildReallocationActions.js +0 -135
  166. package/lib/esm/actions/blue/resolveRepayFunding.d.ts +0 -66
  167. package/lib/esm/actions/blue/resolveRepayFunding.js +0 -81
@@ -1,47 +0,0 @@
1
- import type { Address } from "viem";
2
- import type { Action } from "../../bundler/index.js";
3
- import type { PermitRequirementSignature } from "../../types/index.js";
4
- /** Parameters for {@link buildAssetFundingActions}. */
5
- export interface BuildAssetFundingActionsParams {
6
- /** Chain the operation runs on — resolves adapter addresses and validates wNative. */
7
- readonly chainId: number;
8
- /** Asset wrapped and/or pulled into `GeneralAdapter1` (loan token, collateral token, …). */
9
- readonly asset: Address;
10
- /** ERC-20 amount of `asset` to pull from the payer (`0n` on a fully-native funding). */
11
- readonly erc20Amount: bigint;
12
- /** Native ETH to wrap into wNative before the pull (`0n` when not wrapping). */
13
- readonly nativeAmount: bigint;
14
- /** Optional pre-signed permit/permit2 approval for the ERC-20 pull. */
15
- readonly requirementSignature?: PermitRequirementSignature;
16
- }
17
- /**
18
- * Builds the shared asset-funding actions — native wrapping followed by the ERC-20 pull — that move
19
- * `asset` into `GeneralAdapter1` before a bundled Morpho operation (`blueSupply`,
20
- * `blueSupplyCollateral`, `blueSupplyCollateralBorrow`, `blueRepay`, `blueRepayWithdrawCollateral`).
21
- *
22
- * Encode-only and synchronous: returns a fresh `Action[]` (never mutates its inputs, mirroring
23
- * `buildVaultV2BlueReallocationActions`) and reads no on-chain state. When `nativeAmount > 0n`, validates
24
- * `asset` is the chain's wNative and emits `nativeTransfer → wrapNative`. When `erc20Amount > 0n`,
25
- * pulls the ERC-20 via a signed permit/permit2 (`requirementSignature`) or a plain
26
- * `erc20TransferFrom` — `getTokenRequirementActions` emits the latter itself when no signature is
27
- * supplied. A fully-native funding emits no ERC-20 action; funding nothing returns `[]`.
28
- *
29
- * Single source of truth for the native-wrap + ERC-20-pull composition every bundled Blue funding
30
- * path depends on — a fix here applies to all of them instead of risking a silent divergence
31
- * between copies.
32
- *
33
- * @param params - See {@link BuildAssetFundingActionsParams}.
34
- * @returns The `nativeTransfer`/`wrapNative`/pull actions, in bundle order (empty when nothing is
35
- * funded).
36
- * @throws {ChainWNativeMissingError} when `nativeAmount > 0n` but the chain has no configured wNative.
37
- * @throws {NativeAmountOnNonWNativeAssetError} when `nativeAmount > 0n` but `asset` is not the
38
- * chain's wNative.
39
- * @throws {DepositAssetMismatchError} from `getTokenRequirementActions` when `requirementSignature` is
40
- * provided and the signed asset differs from `asset`.
41
- * @throws {DepositAmountMismatchError} from `getTokenRequirementActions` when `requirementSignature` is
42
- * provided and the signed amount differs from `erc20Amount`.
43
- * @throws {Permit2ExpirationMissingError} from `getTokenRequirementActions` when a Permit2 requirement
44
- * signature is missing its expiration.
45
- * @internal
46
- */
47
- export declare const buildAssetFundingActions: ({ chainId, asset, erc20Amount, nativeAmount, requirementSignature, }: BuildAssetFundingActionsParams) => Action[];
@@ -1,59 +0,0 @@
1
- import { getChainAddresses } from "@morpho-org/blue-sdk";
2
- import { validateNativeAsset } from "../../helpers/index.js";
3
- import { getTokenRequirementActions } from "../signatures/getTokenRequirementActions.js";
4
- /**
5
- * Builds the shared asset-funding actions — native wrapping followed by the ERC-20 pull — that move
6
- * `asset` into `GeneralAdapter1` before a bundled Morpho operation (`blueSupply`,
7
- * `blueSupplyCollateral`, `blueSupplyCollateralBorrow`, `blueRepay`, `blueRepayWithdrawCollateral`).
8
- *
9
- * Encode-only and synchronous: returns a fresh `Action[]` (never mutates its inputs, mirroring
10
- * `buildVaultV2BlueReallocationActions`) and reads no on-chain state. When `nativeAmount > 0n`, validates
11
- * `asset` is the chain's wNative and emits `nativeTransfer → wrapNative`. When `erc20Amount > 0n`,
12
- * pulls the ERC-20 via a signed permit/permit2 (`requirementSignature`) or a plain
13
- * `erc20TransferFrom` — `getTokenRequirementActions` emits the latter itself when no signature is
14
- * supplied. A fully-native funding emits no ERC-20 action; funding nothing returns `[]`.
15
- *
16
- * Single source of truth for the native-wrap + ERC-20-pull composition every bundled Blue funding
17
- * path depends on — a fix here applies to all of them instead of risking a silent divergence
18
- * between copies.
19
- *
20
- * @param params - See {@link BuildAssetFundingActionsParams}.
21
- * @returns The `nativeTransfer`/`wrapNative`/pull actions, in bundle order (empty when nothing is
22
- * funded).
23
- * @throws {ChainWNativeMissingError} when `nativeAmount > 0n` but the chain has no configured wNative.
24
- * @throws {NativeAmountOnNonWNativeAssetError} when `nativeAmount > 0n` but `asset` is not the
25
- * chain's wNative.
26
- * @throws {DepositAssetMismatchError} from `getTokenRequirementActions` when `requirementSignature` is
27
- * provided and the signed asset differs from `asset`.
28
- * @throws {DepositAmountMismatchError} from `getTokenRequirementActions` when `requirementSignature` is
29
- * provided and the signed amount differs from `erc20Amount`.
30
- * @throws {Permit2ExpirationMissingError} from `getTokenRequirementActions` when a Permit2 requirement
31
- * signature is missing its expiration.
32
- * @internal
33
- */
34
- export const buildAssetFundingActions = ({ chainId, asset, erc20Amount, nativeAmount, requirementSignature, }) => {
35
- const { bundler3: { generalAdapter1, bundler3 }, } = getChainAddresses(chainId);
36
- const actions = [];
37
- // Wrap native into wNative before pulling the ERC-20 remainder.
38
- if (nativeAmount > 0n) {
39
- validateNativeAsset(chainId, asset);
40
- actions.push({
41
- type: "nativeTransfer",
42
- args: [bundler3, generalAdapter1, nativeAmount, false],
43
- }, {
44
- type: "wrapNative",
45
- args: [nativeAmount, generalAdapter1, false],
46
- });
47
- }
48
- // Pull the ERC-20 portion (0 on a fully native funding). getTokenRequirementActions emits a plain
49
- // erc20TransferFrom itself when no permit/permit2 signature is supplied.
50
- if (erc20Amount > 0n) {
51
- actions.push(...getTokenRequirementActions({
52
- asset,
53
- amount: erc20Amount,
54
- recipient: generalAdapter1,
55
- requirementSignature,
56
- }));
57
- }
58
- return actions;
59
- };
@@ -1,69 +0,0 @@
1
- import { type MarketParams } from "@morpho-org/blue-sdk";
2
- import type { Action } from "../../bundler/index.js";
3
- import type { BlueReallocationPlan, VaultV1Reallocation, VaultV2BlueReallocation } from "../../types/index.js";
4
- /**
5
- * Builds PublicAllocator V1 reallocation actions for a Morpho Blue target market.
6
- *
7
- * Preserves the supplied reallocation order and aggregates each allocator call's native fee.
8
- *
9
- * @param params.reallocations - Validated Vault V1 reallocations to execute.
10
- * @param params.targetMarketParams - Morpho Blue market receiving the reallocated liquidity.
11
- * @returns The reallocation actions and aggregate native fee, with zero V2 penalty assets.
12
- * @internal
13
- */
14
- export declare const buildVaultV1ReallocationActions: ({ reallocations, targetMarketParams, }: {
15
- readonly reallocations: readonly VaultV1Reallocation[];
16
- readonly targetMarketParams: MarketParams;
17
- }) => {
18
- actions: Action[];
19
- fee: bigint;
20
- penaltyAssets: bigint;
21
- };
22
- /**
23
- * Builds BluePublicAllocator reallocation actions for a Morpho Blue target market.
24
- *
25
- * Prepends one aggregate loan-token funding action when penalties are non-zero, then builds one
26
- * allocator action per validated Vault V2 reallocation.
27
- *
28
- * @param params.chainId - Chain whose registered Bundler3 and allocator addresses are used.
29
- * @param params.reallocations - Validated Vault V2 Blue reallocations to execute.
30
- * @param params.targetMarketParams - Morpho Blue market receiving the reallocated liquidity.
31
- * @param params.penaltyFundingSource - Optional source of penalty assets. Defaults to the
32
- * transaction initiator.
33
- * @returns The funding and reallocation actions and aggregate penalty assets, with zero native fee.
34
- * @internal
35
- */
36
- export declare const buildVaultV2BlueReallocationActions: ({ chainId, reallocations, targetMarketParams, penaltyFundingSource, }: {
37
- readonly chainId: number;
38
- readonly reallocations: readonly VaultV2BlueReallocation[];
39
- readonly targetMarketParams: MarketParams;
40
- readonly penaltyFundingSource?: "initiator" | "generalAdapter1";
41
- }) => {
42
- actions: Action[];
43
- fee: bigint;
44
- penaltyAssets: bigint;
45
- };
46
- /**
47
- * Validates a homogeneous Blue reallocation plan and builds its Bundler actions.
48
- *
49
- * Dispatches Vault V1 and Vault V2 plans to their version-specific builders. V1 plans aggregate
50
- * native fees, while V2 plans aggregate loan-token penalties using the selected funding source.
51
- *
52
- * @param params.chainId - Chain whose registered Bundler3 and allocator addresses are used.
53
- * @param params.reallocations - Optional homogeneous Vault V1 or Vault V2 reallocation plan.
54
- * @param params.targetMarketParams - Morpho Blue market receiving the reallocated liquidity.
55
- * @param params.penaltyFundingSource - Optional source of V2 penalty assets. Defaults to the
56
- * transaction initiator.
57
- * @returns The reallocation actions, aggregate V1 native fee, and aggregate V2 penalty assets.
58
- * @internal
59
- */
60
- export declare const buildBlueReallocationActions: ({ chainId, reallocations, targetMarketParams, penaltyFundingSource, }: {
61
- readonly chainId: number;
62
- readonly reallocations: BlueReallocationPlan | undefined;
63
- readonly targetMarketParams: MarketParams;
64
- readonly penaltyFundingSource?: "initiator" | "generalAdapter1";
65
- }) => {
66
- actions: Action[];
67
- fee: bigint;
68
- penaltyAssets: bigint;
69
- };
@@ -1,135 +0,0 @@
1
- import { getChainAddresses, VaultV2BluePublicAllocatorConfigUtils, } from "@morpho-org/blue-sdk";
2
- import { validateAndNormalizeReallocations } from "../../helpers/validate.js";
3
- /**
4
- * Builds PublicAllocator V1 reallocation actions for a Morpho Blue target market.
5
- *
6
- * Preserves the supplied reallocation order and aggregates each allocator call's native fee.
7
- *
8
- * @param params.reallocations - Validated Vault V1 reallocations to execute.
9
- * @param params.targetMarketParams - Morpho Blue market receiving the reallocated liquidity.
10
- * @returns The reallocation actions and aggregate native fee, with zero V2 penalty assets.
11
- * @internal
12
- */
13
- export const buildVaultV1ReallocationActions = ({ reallocations, targetMarketParams, }) => {
14
- let fee = 0n;
15
- const actions = [];
16
- for (const reallocation of reallocations) {
17
- actions.push({
18
- type: "reallocateTo",
19
- args: [
20
- reallocation.vault,
21
- reallocation.fee,
22
- reallocation.withdrawals.map((withdrawal) => ({
23
- marketParams: withdrawal.marketParams,
24
- amount: withdrawal.amount,
25
- })),
26
- targetMarketParams,
27
- false,
28
- ],
29
- });
30
- fee += reallocation.fee;
31
- }
32
- return { actions, fee, penaltyAssets: 0n };
33
- };
34
- /**
35
- * Builds BluePublicAllocator reallocation actions for a Morpho Blue target market.
36
- *
37
- * Prepends one aggregate loan-token funding action when penalties are non-zero, then builds one
38
- * allocator action per validated Vault V2 reallocation.
39
- *
40
- * @param params.chainId - Chain whose registered Bundler3 and allocator addresses are used.
41
- * @param params.reallocations - Validated Vault V2 Blue reallocations to execute.
42
- * @param params.targetMarketParams - Morpho Blue market receiving the reallocated liquidity.
43
- * @param params.penaltyFundingSource - Optional source of penalty assets. Defaults to the
44
- * transaction initiator.
45
- * @returns The funding and reallocation actions and aggregate penalty assets, with zero native fee.
46
- * @internal
47
- */
48
- export const buildVaultV2BlueReallocationActions = ({ chainId, reallocations, targetMarketParams, penaltyFundingSource = "initiator", }) => {
49
- const actions = [];
50
- const penaltyAssets = reallocations.reduce((total, reallocation) => total +
51
- VaultV2BluePublicAllocatorConfigUtils.getPenaltyAssets(reallocation, reallocation.assets), 0n);
52
- if (penaltyAssets > 0n) {
53
- const { bundler3: { bundler3, generalAdapter1 }, } = getChainAddresses(chainId);
54
- actions.push(penaltyFundingSource === "generalAdapter1"
55
- ? {
56
- type: "erc20Transfer",
57
- args: [
58
- targetMarketParams.loanToken,
59
- bundler3,
60
- penaltyAssets,
61
- generalAdapter1,
62
- false,
63
- ],
64
- }
65
- : {
66
- type: "erc20TransferFrom",
67
- args: [
68
- targetMarketParams.loanToken,
69
- penaltyAssets,
70
- bundler3,
71
- false,
72
- ],
73
- });
74
- }
75
- for (const reallocation of reallocations) {
76
- actions.push(reallocation.from.type === "market"
77
- ? {
78
- type: "vaultV2BluePublicAllocatorReallocate",
79
- args: [
80
- reallocation.vault,
81
- reallocation.from.adapter,
82
- reallocation.from.marketParams,
83
- reallocation.to.adapter,
84
- targetMarketParams,
85
- reallocation.assets,
86
- reallocation.penalty,
87
- false,
88
- ],
89
- }
90
- : {
91
- type: "vaultV2BluePublicAllocatorAllocateFromIdle",
92
- args: [
93
- reallocation.vault,
94
- reallocation.to.adapter,
95
- targetMarketParams,
96
- reallocation.assets,
97
- reallocation.penalty,
98
- false,
99
- ],
100
- });
101
- }
102
- return { actions, fee: 0n, penaltyAssets };
103
- };
104
- /**
105
- * Validates a homogeneous Blue reallocation plan and builds its Bundler actions.
106
- *
107
- * Dispatches Vault V1 and Vault V2 plans to their version-specific builders. V1 plans aggregate
108
- * native fees, while V2 plans aggregate loan-token penalties using the selected funding source.
109
- *
110
- * @param params.chainId - Chain whose registered Bundler3 and allocator addresses are used.
111
- * @param params.reallocations - Optional homogeneous Vault V1 or Vault V2 reallocation plan.
112
- * @param params.targetMarketParams - Morpho Blue market receiving the reallocated liquidity.
113
- * @param params.penaltyFundingSource - Optional source of V2 penalty assets. Defaults to the
114
- * transaction initiator.
115
- * @returns The reallocation actions, aggregate V1 native fee, and aggregate V2 penalty assets.
116
- * @internal
117
- */
118
- export const buildBlueReallocationActions = ({ chainId, reallocations, targetMarketParams, penaltyFundingSource, }) => {
119
- const reallocationPlan = validateAndNormalizeReallocations({
120
- reallocations,
121
- targetMarketId: targetMarketParams.id,
122
- chainId,
123
- });
124
- return reallocationPlan.type === "vaultV1"
125
- ? buildVaultV1ReallocationActions({
126
- reallocations: reallocationPlan.reallocations,
127
- targetMarketParams,
128
- })
129
- : buildVaultV2BlueReallocationActions({
130
- chainId,
131
- reallocations: reallocationPlan.reallocations,
132
- targetMarketParams,
133
- penaltyFundingSource,
134
- });
135
- };
@@ -1,66 +0,0 @@
1
- import type { MarketId } from "@morpho-org/blue-sdk";
2
- /**
3
- * Pre-resolved repay amounts (plus `maxSharePrice`) validated by {@link resolveRepayFunding}.
4
- *
5
- * These are the fields the entity layer (`MorphoBlue.repay` / `repayWithdrawCollateral`) resolves
6
- * from live market/position state before either repay builder assembles the bundle.
7
- *
8
- * @internal
9
- */
10
- export interface RepayFundingInput {
11
- /** (assets mode) ERC-20 loan tokens pulled from the payer. */
12
- readonly amount: bigint;
13
- /** (shares mode) Borrow shares to repay. Discriminates the mode when `> 0n`. */
14
- readonly shares: bigint;
15
- /** Native ETH wrapped into wNative to help fund the repay. */
16
- readonly nativeAmount: bigint;
17
- /** Loan tokens routed into `GeneralAdapter1` (see `RepayActionAmountArgs`). */
18
- readonly transferAmount: bigint;
19
- /** Maximum acceptable repay share price (in ray). */
20
- readonly maxSharePrice: bigint;
21
- }
22
- /**
23
- * Derived repay funding values consumed by the repay builders.
24
- *
25
- * @internal
26
- */
27
- export interface RepayFunding {
28
- /** `true` when repaying an exact share count (full close); `false` in assets mode. */
29
- readonly isSharesMode: boolean;
30
- /** Assets passed to `morphoRepay` (`transferAmount` in assets mode, `0n` in shares mode). */
31
- readonly repayAssets: bigint;
32
- /** Shares passed to `morphoRepay` (`shares` in shares mode, `0n` in assets mode). */
33
- readonly repayShares: bigint;
34
- /** ERC-20 loan tokens pulled into `GeneralAdapter1` (net of any wrapped native). */
35
- readonly erc20Amount: bigint;
36
- }
37
- /**
38
- * Resolves and validates the repay funding envelope shared by `blueRepay` and
39
- * `blueRepayWithdrawCollateral`.
40
- *
41
- * Encode-only and synchronous — reads no on-chain state and does no amount arithmetic beyond
42
- * deriving the bundle values from the pre-resolved args (the entity layer resolves the amounts
43
- * upstream). Runs the shared mode/amount guards, discriminates assets vs shares mode on
44
- * `shares > 0n`, then validates the funding so the assembled bundle neither strands over-pulled
45
- * tokens nor under-funds the repay:
46
- *
47
- * - **assets mode** (`shares` `0n`): repays `transferAmount` (`= amount + nativeAmount`, additive
48
- * like `blueSupply`); ERC-20 pulled = `amount`.
49
- * - **shares mode** (`shares > 0n`): repays exact `shares`; ERC-20 pulled = `transferAmount` (already
50
- * net of native).
51
- *
52
- * Single source of truth for the funding math both repay builders depend on — a correctness fix here
53
- * applies to both paths instead of risking a silent divergence between two copies.
54
- *
55
- * @param input - The pre-resolved repay amounts plus the repay `maxSharePrice`.
56
- * @param marketId - Market id used to tag the thrown errors.
57
- * @returns `{ isSharesMode, repayAssets, repayShares, erc20Amount }` — the mode discriminator and the
58
- * amounts the repay builders assemble the bundle from.
59
- * @throws {NonPositiveInputError} when `maxSharePrice <= 0n` or total repay funding is zero.
60
- * @throws {NegativeInputError} when `amount`, `shares`, `nativeAmount`, or `transferAmount` is negative.
61
- * @throws {MutuallyExclusiveRepayAmountsError} when both `amount` and `shares` are `> 0n`.
62
- * @throws {TransferAmountNotEqualToAssetsError} when in assets mode and
63
- * `transferAmount !== amount + nativeAmount`.
64
- * @internal
65
- */
66
- export declare const resolveRepayFunding: ({ amount, shares, nativeAmount, transferAmount, maxSharePrice, }: RepayFundingInput, marketId: MarketId) => RepayFunding;
@@ -1,81 +0,0 @@
1
- import { MutuallyExclusiveRepayAmountsError, NegativeInputError, NonPositiveInputError, TransferAmountNotEqualToAssetsError, } from "../../types/index.js";
2
- /**
3
- * Resolves and validates the repay funding envelope shared by `blueRepay` and
4
- * `blueRepayWithdrawCollateral`.
5
- *
6
- * Encode-only and synchronous — reads no on-chain state and does no amount arithmetic beyond
7
- * deriving the bundle values from the pre-resolved args (the entity layer resolves the amounts
8
- * upstream). Runs the shared mode/amount guards, discriminates assets vs shares mode on
9
- * `shares > 0n`, then validates the funding so the assembled bundle neither strands over-pulled
10
- * tokens nor under-funds the repay:
11
- *
12
- * - **assets mode** (`shares` `0n`): repays `transferAmount` (`= amount + nativeAmount`, additive
13
- * like `blueSupply`); ERC-20 pulled = `amount`.
14
- * - **shares mode** (`shares > 0n`): repays exact `shares`; ERC-20 pulled = `transferAmount` (already
15
- * net of native).
16
- *
17
- * Single source of truth for the funding math both repay builders depend on — a correctness fix here
18
- * applies to both paths instead of risking a silent divergence between two copies.
19
- *
20
- * @param input - The pre-resolved repay amounts plus the repay `maxSharePrice`.
21
- * @param marketId - Market id used to tag the thrown errors.
22
- * @returns `{ isSharesMode, repayAssets, repayShares, erc20Amount }` — the mode discriminator and the
23
- * amounts the repay builders assemble the bundle from.
24
- * @throws {NonPositiveInputError} when `maxSharePrice <= 0n` or total repay funding is zero.
25
- * @throws {NegativeInputError} when `amount`, `shares`, `nativeAmount`, or `transferAmount` is negative.
26
- * @throws {MutuallyExclusiveRepayAmountsError} when both `amount` and `shares` are `> 0n`.
27
- * @throws {TransferAmountNotEqualToAssetsError} when in assets mode and
28
- * `transferAmount !== amount + nativeAmount`.
29
- * @internal
30
- */
31
- export const resolveRepayFunding = ({ amount, shares, nativeAmount, transferAmount, maxSharePrice, }, marketId) => {
32
- if (maxSharePrice <= 0n) {
33
- throw new NonPositiveInputError("maxSharePrice", maxSharePrice);
34
- }
35
- if (nativeAmount < 0n) {
36
- throw new NegativeInputError("nativeAmount", nativeAmount);
37
- }
38
- if (amount < 0n) {
39
- throw new NegativeInputError("amount", amount);
40
- }
41
- if (shares < 0n) {
42
- throw new NegativeInputError("shares", shares);
43
- }
44
- if (amount > 0n && shares > 0n) {
45
- throw new MutuallyExclusiveRepayAmountsError(marketId);
46
- }
47
- // Shares mode repays an exact share count and pulls `transferAmount` ERC-20
48
- // (already net of native), skimming the residual. Assets mode repays
49
- // `transferAmount` (= amount + native, additive) and pulls `amount`.
50
- const isSharesMode = shares > 0n;
51
- const repayAssets = isSharesMode ? 0n : transferAmount;
52
- const repayShares = shares;
53
- const erc20Amount = isSharesMode ? transferAmount : amount;
54
- // Validate the pre-resolved funding. Assets mode is additive like `blueSupply`:
55
- // the ERC-20 pulled (`amount`) plus the wrapped `nativeAmount` must equal the
56
- // assets repaid, and the total must be positive — so the adapter neither strands
57
- // over-pulled tokens nor under-funds the repay. Shares mode pulls `transferAmount`
58
- // ERC-20 (0 on a fully-native repay) and wraps `nativeAmount`; the bundle must
59
- // fund the repay with a non-negative transfer and a positive total.
60
- if (isSharesMode) {
61
- if (transferAmount < 0n) {
62
- throw new NegativeInputError("transferAmount", transferAmount);
63
- }
64
- if (transferAmount + nativeAmount <= 0n) {
65
- throw new NonPositiveInputError("transferAmount + nativeAmount", transferAmount + nativeAmount);
66
- }
67
- }
68
- else {
69
- if (transferAmount !== amount + nativeAmount) {
70
- throw new TransferAmountNotEqualToAssetsError({
71
- transferAmount,
72
- assets: amount + nativeAmount,
73
- market: marketId,
74
- });
75
- }
76
- if (repayAssets <= 0n) {
77
- throw new NonPositiveInputError("amount + nativeAmount", repayAssets);
78
- }
79
- }
80
- return { isSharesMode, repayAssets, repayShares, erc20Amount };
81
- };