@morpho-org/morpho-sdk 5.9.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.
- package/MIGRATION-v5-to-v6.md +201 -0
- package/README.md +58 -40
- package/lib/cjs/actions/blue/borrow.d.ts +62 -61
- package/lib/cjs/actions/blue/borrow.js +48 -80
- package/lib/cjs/actions/blue/common.d.ts +99 -0
- package/lib/cjs/actions/blue/common.js +318 -0
- package/lib/cjs/actions/blue/refinance.d.ts +72 -112
- package/lib/cjs/actions/blue/refinance.js +97 -199
- package/lib/cjs/actions/blue/repay.d.ts +72 -68
- package/lib/cjs/actions/blue/repay.js +56 -118
- package/lib/cjs/actions/blue/repayWithdrawCollateral.d.ts +85 -90
- package/lib/cjs/actions/blue/repayWithdrawCollateral.js +148 -137
- package/lib/cjs/actions/blue/supply.d.ts +62 -45
- package/lib/cjs/actions/blue/supply.js +94 -70
- package/lib/cjs/actions/blue/supplyCollateral.d.ts +63 -40
- package/lib/cjs/actions/blue/supplyCollateral.js +52 -69
- package/lib/cjs/actions/blue/supplyCollateralBorrow.d.ts +82 -90
- package/lib/cjs/actions/blue/supplyCollateralBorrow.js +133 -120
- package/lib/cjs/actions/blue/withdraw.d.ts +68 -79
- package/lib/cjs/actions/blue/withdraw.js +101 -97
- package/lib/cjs/actions/blue/withdrawCollateral.d.ts +56 -28
- package/lib/cjs/actions/blue/withdrawCollateral.js +45 -50
- package/lib/cjs/actions/index.d.ts +1 -1
- package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +29 -17
- package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +60 -19
- package/lib/cjs/actions/requirements/blue/getBlueBundlesV1TokenRequirements.d.ts +80 -0
- package/lib/cjs/actions/requirements/blue/getBlueBundlesV1TokenRequirements.js +207 -0
- package/lib/cjs/actions/requirements/blue/getUnusedPermit2Nonce.d.ts +48 -0
- package/lib/cjs/actions/requirements/blue/getUnusedPermit2Nonce.js +72 -0
- package/lib/cjs/actions/requirements/blue/index.d.ts +2 -0
- package/lib/cjs/actions/requirements/blue/index.js +2 -0
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +10 -7
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +30 -5
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +2 -1
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +7 -2
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +16 -7
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +33 -6
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2TransferFrom.d.ts +61 -0
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2TransferFrom.js +123 -0
- package/lib/cjs/actions/requirements/encode/index.d.ts +1 -0
- package/lib/cjs/actions/requirements/encode/index.js +1 -0
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +1 -1
- package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +5 -4
- package/lib/cjs/actions/requirements/getRequirementsApproval.js +5 -4
- package/lib/cjs/actions/signatures/getTokenRequirementActions.d.ts +1 -0
- package/lib/cjs/actions/signatures/getTokenRequirementActions.js +4 -0
- package/lib/cjs/bundler/actions.d.ts +4 -4
- package/lib/cjs/bundler/actions.js +4 -4
- package/lib/cjs/client/morphoViemExtension.d.ts +2 -1
- package/lib/cjs/client/morphoViemExtension.js +2 -1
- package/lib/cjs/entities/blue/blue.d.ts +806 -374
- package/lib/cjs/entities/blue/blue.js +447 -696
- package/lib/cjs/entities/index.d.ts +1 -0
- package/lib/cjs/helpers/computeVaultV1Reallocations.d.ts +3 -9
- package/lib/cjs/helpers/computeVaultV1Reallocations.js +3 -9
- package/lib/cjs/helpers/validate.d.ts +14 -23
- package/lib/cjs/helpers/validate.js +30 -48
- package/lib/cjs/helpers/validateRequirementSpender.d.ts +2 -1
- package/lib/cjs/helpers/validateRequirementSpender.js +2 -0
- package/lib/cjs/types/action.d.ts +146 -162
- package/lib/cjs/types/action.js +29 -2
- package/lib/cjs/types/client.d.ts +9 -0
- package/lib/cjs/types/error.d.ts +141 -14
- package/lib/cjs/types/error.js +198 -21
- package/lib/cjs/types/sharedLiquidity.d.ts +0 -7
- package/lib/esm/actions/blue/borrow.d.ts +62 -61
- package/lib/esm/actions/blue/borrow.js +48 -80
- package/lib/esm/actions/blue/common.d.ts +99 -0
- package/lib/esm/actions/blue/common.js +306 -0
- package/lib/esm/actions/blue/refinance.d.ts +72 -112
- package/lib/esm/actions/blue/refinance.js +98 -200
- package/lib/esm/actions/blue/repay.d.ts +72 -68
- package/lib/esm/actions/blue/repay.js +56 -118
- package/lib/esm/actions/blue/repayWithdrawCollateral.d.ts +85 -90
- package/lib/esm/actions/blue/repayWithdrawCollateral.js +149 -138
- package/lib/esm/actions/blue/supply.d.ts +62 -45
- package/lib/esm/actions/blue/supply.js +94 -70
- package/lib/esm/actions/blue/supplyCollateral.d.ts +63 -40
- package/lib/esm/actions/blue/supplyCollateral.js +52 -69
- package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +82 -90
- package/lib/esm/actions/blue/supplyCollateralBorrow.js +134 -121
- package/lib/esm/actions/blue/withdraw.d.ts +68 -79
- package/lib/esm/actions/blue/withdraw.js +100 -96
- package/lib/esm/actions/blue/withdrawCollateral.d.ts +56 -28
- package/lib/esm/actions/blue/withdrawCollateral.js +45 -50
- package/lib/esm/actions/index.d.ts +1 -1
- package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +29 -17
- package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +63 -22
- package/lib/esm/actions/requirements/blue/getBlueBundlesV1TokenRequirements.d.ts +80 -0
- package/lib/esm/actions/requirements/blue/getBlueBundlesV1TokenRequirements.js +203 -0
- package/lib/esm/actions/requirements/blue/getUnusedPermit2Nonce.d.ts +48 -0
- package/lib/esm/actions/requirements/blue/getUnusedPermit2Nonce.js +68 -0
- package/lib/esm/actions/requirements/blue/index.d.ts +2 -0
- package/lib/esm/actions/requirements/blue/index.js +2 -0
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +10 -7
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +31 -6
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +2 -1
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +8 -3
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +16 -7
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +34 -7
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2TransferFrom.d.ts +61 -0
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2TransferFrom.js +119 -0
- package/lib/esm/actions/requirements/encode/index.d.ts +1 -0
- package/lib/esm/actions/requirements/encode/index.js +1 -0
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +1 -1
- package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +5 -4
- package/lib/esm/actions/requirements/getRequirementsApproval.js +5 -4
- package/lib/esm/actions/signatures/getTokenRequirementActions.d.ts +1 -0
- package/lib/esm/actions/signatures/getTokenRequirementActions.js +5 -1
- package/lib/esm/bundler/actions.d.ts +4 -4
- package/lib/esm/bundler/actions.js +4 -4
- package/lib/esm/client/morphoViemExtension.d.ts +2 -1
- package/lib/esm/client/morphoViemExtension.js +2 -1
- package/lib/esm/entities/blue/blue.d.ts +806 -374
- package/lib/esm/entities/blue/blue.js +453 -702
- package/lib/esm/entities/index.d.ts +1 -0
- package/lib/esm/helpers/computeVaultV1Reallocations.d.ts +3 -9
- package/lib/esm/helpers/computeVaultV1Reallocations.js +3 -9
- package/lib/esm/helpers/validate.d.ts +14 -23
- package/lib/esm/helpers/validate.js +30 -48
- package/lib/esm/helpers/validateRequirementSpender.d.ts +2 -1
- package/lib/esm/helpers/validateRequirementSpender.js +2 -0
- package/lib/esm/types/action.d.ts +146 -162
- package/lib/esm/types/action.js +28 -2
- package/lib/esm/types/client.d.ts +9 -0
- package/lib/esm/types/error.d.ts +141 -14
- package/lib/esm/types/error.js +184 -17
- package/lib/esm/types/sharedLiquidity.d.ts +0 -7
- package/package.json +4 -3
- package/lib/cjs/actions/blue/buildAssetFundingActions.d.ts +0 -47
- package/lib/cjs/actions/blue/buildAssetFundingActions.js +0 -63
- package/lib/cjs/actions/blue/buildReallocationActions.d.ts +0 -69
- package/lib/cjs/actions/blue/buildReallocationActions.js +0 -141
- package/lib/cjs/actions/blue/resolveRepayFunding.d.ts +0 -66
- package/lib/cjs/actions/blue/resolveRepayFunding.js +0 -85
- package/lib/esm/actions/blue/buildAssetFundingActions.d.ts +0 -47
- package/lib/esm/actions/blue/buildAssetFundingActions.js +0 -59
- package/lib/esm/actions/blue/buildReallocationActions.d.ts +0 -69
- package/lib/esm/actions/blue/buildReallocationActions.js +0 -135
- package/lib/esm/actions/blue/resolveRepayFunding.d.ts +0 -66
- package/lib/esm/actions/blue/resolveRepayFunding.js +0 -81
|
@@ -1,135 +1,95 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { MarketParams } from "@morpho-org/blue-sdk";
|
|
2
2
|
import { type Address } from "viem";
|
|
3
|
-
import { type AuthorizationRequirementSignature, type
|
|
3
|
+
import { type AuthorizationRequirementSignature, type BlueRefinanceAction, type Metadata, type Transaction, type VaultV2BlueReallocation } from "../../types/index.js";
|
|
4
4
|
/** Parameters for {@link blueRefinance}. */
|
|
5
5
|
export interface BlueRefinanceParams {
|
|
6
|
-
source
|
|
6
|
+
/** Chain plus source and destination Morpho Blue markets. */
|
|
7
|
+
readonly market: {
|
|
7
8
|
readonly chainId: number;
|
|
8
|
-
readonly
|
|
9
|
+
readonly sourceMarketParams: MarketParams;
|
|
10
|
+
readonly destinationMarketParams: MarketParams;
|
|
9
11
|
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
};
|
|
13
|
-
args: {
|
|
14
|
-
/** Address whose position is refinanced from the source to the target market. */
|
|
15
|
-
user: Address;
|
|
16
|
-
/** Amount of collateral moved from the source market to the target market. */
|
|
17
|
-
collateralAmount: bigint;
|
|
18
|
-
/**
|
|
19
|
-
* Loan assets to borrow on the target. Assets mode: the exact borrow (exclusive with
|
|
20
|
-
* `borrowShares`). Shares mode: the positive overshoot covering accrual + slippage; omitting it
|
|
21
|
-
* throws {@link RefinanceSharesMissingBorrowAssetsError}.
|
|
22
|
-
*/
|
|
23
|
-
borrowAssets?: bigint;
|
|
24
|
-
/** Source borrow shares to repay (immune to mid-tx accrual); exclusive with `borrowAssets`. */
|
|
25
|
-
borrowShares?: bigint;
|
|
26
|
-
/** Minimum borrow share price on the target market (in ray). */
|
|
27
|
-
minBorrowSharePrice: bigint;
|
|
28
|
-
/** Maximum repay share price on the source market (in ray); must be > 0 when a repay leg exists. */
|
|
29
|
-
maxRepaySharePrice: bigint;
|
|
12
|
+
/** Direct BlueBundlesV1 full-position migration arguments. */
|
|
13
|
+
readonly args: {
|
|
30
14
|
/**
|
|
31
|
-
*
|
|
32
|
-
*
|
|
15
|
+
* Intended user used to bind an optional Morpho authorization and action metadata. This value is
|
|
16
|
+
* not encoded; the caller must send from the same account because BlueBundlesV1 always migrates
|
|
17
|
+
* `msg.sender`.
|
|
33
18
|
*/
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
19
|
+
readonly userAddress: Address;
|
|
20
|
+
/** Maximum destination LTV enforced by BlueBundlesV1. */
|
|
21
|
+
readonly maxLtv: bigint;
|
|
22
|
+
/** Optional Vault V2 reallocations into the destination market. */
|
|
23
|
+
readonly reallocations?: Iterable<VaultV2BlueReallocation>;
|
|
24
|
+
/** Final call deadline in Unix seconds. */
|
|
25
|
+
readonly deadline: bigint;
|
|
26
|
+
/** Optional WAD-scaled referral fee, strictly below 100%. */
|
|
27
|
+
readonly referralFeePct?: bigint;
|
|
28
|
+
/** Recipient required when `referralFeePct` is positive. */
|
|
29
|
+
readonly referralFeeRecipient?: Address;
|
|
30
|
+
/** Optional Morpho authorization signature for BlueBundlesV1. */
|
|
31
|
+
readonly authorizationSignature?: AuthorizationRequirementSignature;
|
|
41
32
|
};
|
|
42
|
-
metadata
|
|
33
|
+
/** Optional transaction metadata suffix. */
|
|
34
|
+
readonly metadata?: Metadata;
|
|
43
35
|
}
|
|
44
36
|
/**
|
|
45
|
-
*
|
|
46
|
-
* chain that shares the same loan and collateral tokens.
|
|
47
|
-
*
|
|
48
|
-
* Strategy: flash-collateral via the target's `onMorphoSupplyCollateral` callback. The collateral
|
|
49
|
-
* is credited before the deferred `safeTransferFrom`, so inside the callback GA1 borrows on the
|
|
50
|
-
* target, repays the source, then withdraws the source collateral to settle the transfer.
|
|
51
|
-
*
|
|
52
|
-
* Bundle shape (callback contents depend on borrow mode):
|
|
53
|
-
*
|
|
54
|
-
* ```text
|
|
55
|
-
* // optional targetReallocations run first:
|
|
56
|
-
* reallocateTo(...) | reallocate(...) | allocateFromIdle(...),
|
|
57
|
-
*
|
|
58
|
-
* morphoSupplyCollateral(target, collateralAmount, user, [
|
|
59
|
-
* // omitted in collat-only mode
|
|
60
|
-
* morphoBorrow(target, borrowAssets, 0, minBorrowSharePrice, GA1),
|
|
61
|
-
* morphoRepay(source, assets|0, 0|shares, maxRepaySharePrice, user, []),
|
|
62
|
-
* // shares mode only: sweep overshoot before the withdraw so same-token markets aren't drained
|
|
63
|
-
* morphoRepay(target, maxUint256, 0, maxUint256, user, [], skipRevert=true),
|
|
64
|
-
* // shares mode only: fallback if the repay is skipped, skim residual loan tokens to the user
|
|
65
|
-
* erc20Transfer(loanToken, user, maxUint256, GA1, skipRevert=false),
|
|
66
|
-
* morphoWithdrawCollateral(source, collateralAmount, GA1),
|
|
67
|
-
* ])
|
|
68
|
-
* ```
|
|
69
|
-
*
|
|
70
|
-
* Borrow modes:
|
|
71
|
-
*
|
|
72
|
-
* - **Assets mode** (`borrowAssets > 0n`): exact-asset borrow and repay, no GA1 dust.
|
|
73
|
-
* - **Shares mode** (`borrowShares > 0n`, `borrowAssets` is the overshoot): the trailing
|
|
74
|
-
* `morphoRepay(target, maxUint256, …, skipRevert=true)` sweeps the residual into the target debt,
|
|
75
|
-
* then an `erc20Transfer` skims any residual to the user if that repay is skipped.
|
|
76
|
-
* - **Collat-only** (both zero/omitted): only collateral is migrated; borrow/repay legs omitted.
|
|
37
|
+
* Encodes a direct BlueBundlesV1 full borrow-position migration transaction.
|
|
77
38
|
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
39
|
+
* BlueBundlesV1 reads and moves the user's complete live source debt and collateral. Source and
|
|
40
|
+
* destination must use identical tokens, and referral fees plus allocator penalties increase the
|
|
41
|
+
* destination debt. Partial and collateral-only migration are not supported.
|
|
80
42
|
*
|
|
81
|
-
* @param params
|
|
82
|
-
* @param params.
|
|
83
|
-
* @param params.
|
|
84
|
-
* @param params.
|
|
85
|
-
* @param params.args.
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
* @param params.args.
|
|
89
|
-
* @param params.args.
|
|
90
|
-
* @param params.args.
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
* @param params.
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
* @
|
|
98
|
-
* @
|
|
99
|
-
*
|
|
100
|
-
* @throws {NonPositiveInputError} when
|
|
101
|
-
*
|
|
102
|
-
* @throws {
|
|
103
|
-
* @throws {
|
|
104
|
-
* @throws {
|
|
105
|
-
* @throws {InvalidReallocationSourceTypeError} when a
|
|
106
|
-
* @throws {
|
|
107
|
-
* @throws {
|
|
108
|
-
* @throws {
|
|
109
|
-
*
|
|
110
|
-
* @throws {
|
|
111
|
-
* @throws {
|
|
112
|
-
* @throws {
|
|
113
|
-
* @throws {EmptyReallocationWithdrawalsError} when any `reallocation.withdrawals` is empty.
|
|
114
|
-
* @throws {ReallocationWithdrawalOnTargetMarketError} when a reallocation withdrawal references the target market.
|
|
115
|
-
* @throws {UnsortedReallocationWithdrawalsError} when reallocation withdrawals are not strictly sorted by market id.
|
|
43
|
+
* @param params - Migration encoding parameters.
|
|
44
|
+
* @param params.market.chainId - Chain containing BlueBundlesV1.
|
|
45
|
+
* @param params.market.sourceMarketParams - Source market scoped by the Blue entity.
|
|
46
|
+
* @param params.market.destinationMarketParams - Distinct compatible destination market.
|
|
47
|
+
* @param params.args.userAddress - Intended user used to bind an optional Morpho authorization and
|
|
48
|
+
* action metadata. This value is not encoded; the caller must send from the same account because
|
|
49
|
+
* BlueBundlesV1 always migrates `msg.sender`.
|
|
50
|
+
* @param params.args.maxLtv - Maximum destination LTV.
|
|
51
|
+
* @param params.args.reallocations - Vault V2 reallocations into the destination.
|
|
52
|
+
* @param params.args.deadline - Final call deadline in Unix seconds.
|
|
53
|
+
* @param params.args.referralFeePct - Optional WAD-scaled fee below 100%.
|
|
54
|
+
* @param params.args.referralFeeRecipient - Recipient required for a positive fee.
|
|
55
|
+
* @param params.args.authorizationSignature - Optional Blue authorization signature.
|
|
56
|
+
* @param params.metadata - Optional transaction metadata.
|
|
57
|
+
* @returns A deep-frozen `Readonly<Transaction<BlueRefinanceAction>>` whose
|
|
58
|
+
* `to` address is BlueBundlesV1 and whose `action` records the normalized migration inputs.
|
|
59
|
+
* @throws {RefinanceSameMarketError} when source and destination IDs match.
|
|
60
|
+
* @throws {RefinanceTokenMismatchError} when their loan or collateral tokens differ.
|
|
61
|
+
* @throws {NegativeInputError} when `maxLtv`, a referral fee, or a reallocation penalty is negative.
|
|
62
|
+
* @throws {NonPositiveInputError} when the deadline or a reallocation amount is not positive.
|
|
63
|
+
* @throws {InputExceedsMaxError} when a fee, reallocation amount, or penalty exceeds its ABI bound.
|
|
64
|
+
* @throws {MissingReferralFeeRecipientError} when a positive fee has no recipient.
|
|
65
|
+
* @throws {InvalidReallocationAddressError} when a vault or adapter address is malformed.
|
|
66
|
+
* @throws {InvalidReallocationShapeError} when a reallocation entry is not a valid Vault V2 reallocation.
|
|
67
|
+
* @throws {InvalidReallocationSourceTypeError} when a reallocation source is malformed.
|
|
68
|
+
* @throws {InconsistentReallocationPenaltyError} when one vault uses different penalties.
|
|
69
|
+
* @throws {ReallocationWithdrawalOnTargetMarketError} when a source is the destination market.
|
|
70
|
+
* @throws {ReallocationLoanTokenMismatchError} when a source market uses another loan token.
|
|
71
|
+
* @throws {DepositOwnerMismatchError} when the signed authorization owner differs from `userAddress`.
|
|
72
|
+
* @throws {BlueBundlesV1RequirementSignatureMismatchError} when authorization cannot be bound safely.
|
|
73
|
+
* @throws {UnsupportedChainIdError} when the chain is absent from the registry.
|
|
74
|
+
* @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
|
|
116
75
|
* @example
|
|
117
76
|
* ```ts
|
|
77
|
+
* import { markets } from "@morpho-org/morpho-test";
|
|
118
78
|
* import { blueRefinance } from "@morpho-org/morpho-sdk";
|
|
79
|
+
* import { zeroAddress } from "viem";
|
|
80
|
+
* import { mainnet } from "viem/chains";
|
|
119
81
|
*
|
|
82
|
+
* const sourceMarketParams = markets[mainnet.id].eth_wstEth_2;
|
|
83
|
+
* const destinationMarketParams = markets[mainnet.id].eth_wstEth;
|
|
120
84
|
* const tx = blueRefinance({
|
|
121
|
-
*
|
|
122
|
-
* target: { marketParams: targetParams },
|
|
85
|
+
* market: { chainId: mainnet.id, sourceMarketParams, destinationMarketParams },
|
|
123
86
|
* args: {
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
* borrowAssets: 501_000_000n, // overshoot computed by entity layer
|
|
128
|
-
* minBorrowSharePrice: 0n,
|
|
129
|
-
* maxRepaySharePrice: 1_500_000_000_000_000_000_000_000_000n,
|
|
87
|
+
* userAddress: zeroAddress,
|
|
88
|
+
* maxLtv: 850000000000000000n,
|
|
89
|
+
* deadline: 1_900_000_000n,
|
|
130
90
|
* },
|
|
131
91
|
* });
|
|
132
92
|
* // tx satisfies Readonly<Transaction<BlueRefinanceAction>>
|
|
133
93
|
* ```
|
|
134
94
|
*/
|
|
135
|
-
export declare const blueRefinance: (
|
|
95
|
+
export declare const blueRefinance: (params: BlueRefinanceParams) => Readonly<Transaction<BlueRefinanceAction>>;
|
|
@@ -1,228 +1,126 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.blueRefinance = void 0;
|
|
4
|
-
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
5
|
-
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
6
4
|
const viem_1 = require("viem");
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const buildReallocationActions_js_1 = require("./buildReallocationActions.js");
|
|
5
|
+
const abis_js_1 = require("../../abis.js");
|
|
6
|
+
const validate_js_1 = require("../../helpers/validate.js");
|
|
7
|
+
const index_js_1 = require("../../types/index.js");
|
|
8
|
+
const common_js_1 = require("./common.js");
|
|
12
9
|
/**
|
|
13
|
-
*
|
|
14
|
-
* chain that shares the same loan and collateral tokens.
|
|
10
|
+
* Encodes a direct BlueBundlesV1 full borrow-position migration transaction.
|
|
15
11
|
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
12
|
+
* BlueBundlesV1 reads and moves the user's complete live source debt and collateral. Source and
|
|
13
|
+
* destination must use identical tokens, and referral fees plus allocator penalties increase the
|
|
14
|
+
* destination debt. Partial and collateral-only migration are not supported.
|
|
19
15
|
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
* @
|
|
50
|
-
* @
|
|
51
|
-
* @
|
|
52
|
-
* @param params.args.user - Position owner on both markets.
|
|
53
|
-
* @param params.args.collateralAmount - Amount of collateral to migrate.
|
|
54
|
-
* @param params.args.borrowAssets - Loan assets to borrow on the target; exclusive with `borrowShares`. Defaults to `0n`.
|
|
55
|
-
* @param params.args.borrowShares - Borrow shares to repay on the source; exclusive with `borrowAssets`. Defaults to `0n`.
|
|
56
|
-
* @param params.args.minBorrowSharePrice - Minimum borrow share price (ray) on the target.
|
|
57
|
-
* @param params.args.maxRepaySharePrice - Maximum repay share price (ray) on the source.
|
|
58
|
-
* @param params.args.targetReallocations - Homogeneous Vault V1 or Vault V2 reallocations into the
|
|
59
|
-
* target, run before the supply leg. Vault V1 inputs are deprecated for high-level Blue writes;
|
|
60
|
-
* use Vault V2 for new integrations. V1 fees add to `tx.value`; V2 penalties are paid in the
|
|
61
|
-
* target loan token.
|
|
62
|
-
* @param params.args.authorizationSignature - Optional signed Morpho authorization; when present,
|
|
63
|
-
* a `setAuthorizationWithSig` call is prepended to the bundle.
|
|
64
|
-
* @param params.metadata - Optional analytics metadata appended to `tx.data`.
|
|
65
|
-
* @returns A deep-frozen `Transaction<BlueRefinanceAction>`.
|
|
66
|
-
* @remarks `borrowAssets` and `borrowShares` describe different markets (target borrow vs. source
|
|
67
|
-
* repay); in shares mode the entity passes both. Caller-facing mutual exclusivity is enforced at the entity layer.
|
|
68
|
-
* @throws {NonPositiveInputError} when `collateralAmount <= 0n`, a repay leg has a non-positive
|
|
69
|
-
* `maxRepaySharePrice`, or any reallocation withdrawal amount is non-positive.
|
|
70
|
-
* @throws {InputExceedsMaxError} when a V2 reallocation asset amount exceeds `uint128` or its penalty exceeds WAD.
|
|
71
|
-
* @throws {InconsistentReallocationPenaltyError} when V2 entries for one vault use different penalties.
|
|
72
|
-
* @throws {InvalidReallocationAddressError} when a V2 vault or adapter address is malformed.
|
|
73
|
-
* @throws {InvalidReallocationSourceTypeError} when a V2 source is absent, incomplete, or has an unknown discriminator.
|
|
74
|
-
* @throws {InvalidReallocationShapeError} when an entry matches both or neither V1/V2 shape.
|
|
75
|
-
* @throws {MixedReallocationVersionsError} when one plan contains both V1 and V2 entries.
|
|
76
|
-
* @throws {NegativeInputError} when `borrowAssets`, `borrowShares`, `minBorrowSharePrice`,
|
|
77
|
-
* `maxRepaySharePrice`, a V1 fee, or a V2 penalty is negative.
|
|
78
|
-
* @throws {RefinanceSameMarketError} when source and target market ids are equal.
|
|
79
|
-
* @throws {RefinanceTokenMismatchError} when source and target do not share both tokens.
|
|
80
|
-
* @throws {RefinanceSharesMissingBorrowAssetsError} when `borrowShares > 0n` but `borrowAssets` is omitted or non-positive.
|
|
81
|
-
* @throws {EmptyReallocationWithdrawalsError} when any `reallocation.withdrawals` is empty.
|
|
82
|
-
* @throws {ReallocationWithdrawalOnTargetMarketError} when a reallocation withdrawal references the target market.
|
|
83
|
-
* @throws {UnsortedReallocationWithdrawalsError} when reallocation withdrawals are not strictly sorted by market id.
|
|
16
|
+
* @param params - Migration encoding parameters.
|
|
17
|
+
* @param params.market.chainId - Chain containing BlueBundlesV1.
|
|
18
|
+
* @param params.market.sourceMarketParams - Source market scoped by the Blue entity.
|
|
19
|
+
* @param params.market.destinationMarketParams - Distinct compatible destination market.
|
|
20
|
+
* @param params.args.userAddress - Intended user used to bind an optional Morpho authorization and
|
|
21
|
+
* action metadata. This value is not encoded; the caller must send from the same account because
|
|
22
|
+
* BlueBundlesV1 always migrates `msg.sender`.
|
|
23
|
+
* @param params.args.maxLtv - Maximum destination LTV.
|
|
24
|
+
* @param params.args.reallocations - Vault V2 reallocations into the destination.
|
|
25
|
+
* @param params.args.deadline - Final call deadline in Unix seconds.
|
|
26
|
+
* @param params.args.referralFeePct - Optional WAD-scaled fee below 100%.
|
|
27
|
+
* @param params.args.referralFeeRecipient - Recipient required for a positive fee.
|
|
28
|
+
* @param params.args.authorizationSignature - Optional Blue authorization signature.
|
|
29
|
+
* @param params.metadata - Optional transaction metadata.
|
|
30
|
+
* @returns A deep-frozen `Readonly<Transaction<BlueRefinanceAction>>` whose
|
|
31
|
+
* `to` address is BlueBundlesV1 and whose `action` records the normalized migration inputs.
|
|
32
|
+
* @throws {RefinanceSameMarketError} when source and destination IDs match.
|
|
33
|
+
* @throws {RefinanceTokenMismatchError} when their loan or collateral tokens differ.
|
|
34
|
+
* @throws {NegativeInputError} when `maxLtv`, a referral fee, or a reallocation penalty is negative.
|
|
35
|
+
* @throws {NonPositiveInputError} when the deadline or a reallocation amount is not positive.
|
|
36
|
+
* @throws {InputExceedsMaxError} when a fee, reallocation amount, or penalty exceeds its ABI bound.
|
|
37
|
+
* @throws {MissingReferralFeeRecipientError} when a positive fee has no recipient.
|
|
38
|
+
* @throws {InvalidReallocationAddressError} when a vault or adapter address is malformed.
|
|
39
|
+
* @throws {InvalidReallocationShapeError} when a reallocation entry is not a valid Vault V2 reallocation.
|
|
40
|
+
* @throws {InvalidReallocationSourceTypeError} when a reallocation source is malformed.
|
|
41
|
+
* @throws {InconsistentReallocationPenaltyError} when one vault uses different penalties.
|
|
42
|
+
* @throws {ReallocationWithdrawalOnTargetMarketError} when a source is the destination market.
|
|
43
|
+
* @throws {ReallocationLoanTokenMismatchError} when a source market uses another loan token.
|
|
44
|
+
* @throws {DepositOwnerMismatchError} when the signed authorization owner differs from `userAddress`.
|
|
45
|
+
* @throws {BlueBundlesV1RequirementSignatureMismatchError} when authorization cannot be bound safely.
|
|
46
|
+
* @throws {UnsupportedChainIdError} when the chain is absent from the registry.
|
|
47
|
+
* @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
|
|
84
48
|
* @example
|
|
85
49
|
* ```ts
|
|
50
|
+
* import { markets } from "@morpho-org/morpho-test";
|
|
86
51
|
* import { blueRefinance } from "@morpho-org/morpho-sdk";
|
|
52
|
+
* import { zeroAddress } from "viem";
|
|
53
|
+
* import { mainnet } from "viem/chains";
|
|
87
54
|
*
|
|
55
|
+
* const sourceMarketParams = markets[mainnet.id].eth_wstEth_2;
|
|
56
|
+
* const destinationMarketParams = markets[mainnet.id].eth_wstEth;
|
|
88
57
|
* const tx = blueRefinance({
|
|
89
|
-
*
|
|
90
|
-
* target: { marketParams: targetParams },
|
|
58
|
+
* market: { chainId: mainnet.id, sourceMarketParams, destinationMarketParams },
|
|
91
59
|
* args: {
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
* borrowAssets: 501_000_000n, // overshoot computed by entity layer
|
|
96
|
-
* minBorrowSharePrice: 0n,
|
|
97
|
-
* maxRepaySharePrice: 1_500_000_000_000_000_000_000_000_000n,
|
|
60
|
+
* userAddress: zeroAddress,
|
|
61
|
+
* maxLtv: 850000000000000000n,
|
|
62
|
+
* deadline: 1_900_000_000n,
|
|
98
63
|
* },
|
|
99
64
|
* });
|
|
100
65
|
* // tx satisfies Readonly<Transaction<BlueRefinanceAction>>
|
|
101
66
|
* ```
|
|
102
67
|
*/
|
|
103
|
-
const blueRefinance = (
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
throw new index_js_3.NegativeInputError("borrowAssets", borrowAssets);
|
|
109
|
-
}
|
|
110
|
-
if (borrowShares < 0n) {
|
|
111
|
-
throw new index_js_3.NegativeInputError("borrowShares", borrowShares);
|
|
112
|
-
}
|
|
113
|
-
if (minBorrowSharePrice < 0n) {
|
|
114
|
-
throw new index_js_3.NegativeInputError("minBorrowSharePrice", minBorrowSharePrice);
|
|
115
|
-
}
|
|
116
|
-
if (maxRepaySharePrice < 0n) {
|
|
117
|
-
throw new index_js_3.NegativeInputError("maxRepaySharePrice", maxRepaySharePrice);
|
|
118
|
-
}
|
|
119
|
-
if (sourceParams.id === targetParams.id) {
|
|
120
|
-
throw new index_js_3.RefinanceSameMarketError(sourceParams.id);
|
|
121
|
-
}
|
|
122
|
-
if (!(0, viem_1.isAddressEqual)(sourceParams.collateralToken, targetParams.collateralToken) ||
|
|
123
|
-
!(0, viem_1.isAddressEqual)(sourceParams.loanToken, targetParams.loanToken)) {
|
|
124
|
-
throw new index_js_3.RefinanceTokenMismatchError(sourceParams.id, targetParams.id);
|
|
68
|
+
const blueRefinance = (params) => {
|
|
69
|
+
const { chainId, sourceMarketParams, destinationMarketParams } = params.market;
|
|
70
|
+
const { userAddress, maxLtv, authorizationSignature } = params.args;
|
|
71
|
+
if (sourceMarketParams.id === destinationMarketParams.id) {
|
|
72
|
+
throw new index_js_1.RefinanceSameMarketError(sourceMarketParams.id);
|
|
125
73
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
// Shares mode borrows in assets; Morpho rejects a zero borrow, so require a positive overshoot.
|
|
130
|
-
if (sharesMode && borrowAssets <= 0n) {
|
|
131
|
-
throw new index_js_3.RefinanceSharesMissingBorrowAssetsError(sourceParams.id);
|
|
74
|
+
if (!(0, viem_1.isAddressEqual)(sourceMarketParams.loanToken, destinationMarketParams.loanToken) ||
|
|
75
|
+
!(0, viem_1.isAddressEqual)(sourceMarketParams.collateralToken, destinationMarketParams.collateralToken)) {
|
|
76
|
+
throw new index_js_1.RefinanceTokenMismatchError(sourceMarketParams.id, destinationMarketParams.id);
|
|
132
77
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
throw new index_js_3.NonPositiveInputError("maxRepaySharePrice", maxRepaySharePrice);
|
|
136
|
-
}
|
|
137
|
-
const callback = [];
|
|
138
|
-
if (shouldMigrateBorrow) {
|
|
139
|
-
callback.push({
|
|
140
|
-
type: "morphoBorrow",
|
|
141
|
-
args: [
|
|
142
|
-
targetParams,
|
|
143
|
-
borrowAssets,
|
|
144
|
-
0n,
|
|
145
|
-
minBorrowSharePrice,
|
|
146
|
-
generalAdapter1,
|
|
147
|
-
false,
|
|
148
|
-
],
|
|
149
|
-
});
|
|
150
|
-
callback.push(sharesMode
|
|
151
|
-
? {
|
|
152
|
-
type: "morphoRepay",
|
|
153
|
-
args: [
|
|
154
|
-
sourceParams,
|
|
155
|
-
0n,
|
|
156
|
-
borrowShares,
|
|
157
|
-
maxRepaySharePrice,
|
|
158
|
-
user,
|
|
159
|
-
[],
|
|
160
|
-
false,
|
|
161
|
-
],
|
|
162
|
-
}
|
|
163
|
-
: {
|
|
164
|
-
type: "morphoRepay",
|
|
165
|
-
args: [
|
|
166
|
-
sourceParams,
|
|
167
|
-
borrowAssets,
|
|
168
|
-
0n,
|
|
169
|
-
maxRepaySharePrice,
|
|
170
|
-
user,
|
|
171
|
-
[],
|
|
172
|
-
false,
|
|
173
|
-
],
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
// Sweep the borrow overshoot back into target debt so GA1 ends drained. Must run before the
|
|
177
|
-
// withdraw: in same-token markets maxUint256 would otherwise drain the just-withdrawn collateral.
|
|
178
|
-
if (sharesMode) {
|
|
179
|
-
callback.push({
|
|
180
|
-
type: "morphoRepay",
|
|
181
|
-
args: [targetParams, viem_1.maxUint256, 0n, viem_1.maxUint256, user, [], true],
|
|
182
|
-
});
|
|
183
|
-
// Fallback: if the repay above is skipped, skim any residual loan tokens to the user.
|
|
184
|
-
callback.push({
|
|
185
|
-
type: "erc20Transfer",
|
|
186
|
-
args: [targetParams.loanToken, user, viem_1.maxUint256, generalAdapter1, false],
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
callback.push({
|
|
190
|
-
type: "morphoWithdrawCollateral",
|
|
191
|
-
args: [sourceParams, collateralAmount, generalAdapter1, false],
|
|
192
|
-
});
|
|
193
|
-
const actions = [];
|
|
194
|
-
if (authorizationSignature) {
|
|
195
|
-
actions.push((0, getBlueAuthorizationAction_js_1.getBlueAuthorizationAction)(chainId, authorizationSignature));
|
|
196
|
-
}
|
|
197
|
-
const { actions: reallocationActions, fee: reallocationFee, penaltyAssets: reallocationPenaltyAssets, } = (0, buildReallocationActions_js_1.buildBlueReallocationActions)({
|
|
78
|
+
(0, validate_js_1.validateUint256Field)("maxLtv", maxLtv);
|
|
79
|
+
const common = {
|
|
198
80
|
chainId,
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
81
|
+
userAddress,
|
|
82
|
+
deadline: params.args.deadline,
|
|
83
|
+
referralFeePct: params.args.referralFeePct,
|
|
84
|
+
referralFeeRecipient: params.args.referralFeeRecipient,
|
|
85
|
+
metadata: params.metadata,
|
|
86
|
+
};
|
|
87
|
+
const { referralFeePct, referralFeeRecipient } = (0, common_js_1.normalizeBlueBundlesV1CommonParams)(common);
|
|
88
|
+
const signedAuthorization = (0, common_js_1.getBlueBundlesV1SignedAuthorization)({
|
|
89
|
+
chainId,
|
|
90
|
+
userAddress,
|
|
91
|
+
authorizationSignature,
|
|
206
92
|
});
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
93
|
+
const publicAllocations = (0, common_js_1.getBlueBundlesV1PublicAllocations)([...(params.args.reallocations ?? [])], destinationMarketParams);
|
|
94
|
+
const reallocationPenaltyAssets = (0, common_js_1.getBlueBundlesV1PenaltyAssets)(publicAllocations);
|
|
95
|
+
return (0, common_js_1.finalizeBlueBundlesV1Transaction)({
|
|
96
|
+
common,
|
|
97
|
+
value: 0n,
|
|
98
|
+
data: (0, viem_1.encodeFunctionData)({
|
|
99
|
+
abi: abis_js_1.blueBundlesV1Abi,
|
|
100
|
+
functionName: "blueBundlesV1MigrateBorrowPosition",
|
|
101
|
+
args: [
|
|
102
|
+
sourceMarketParams,
|
|
103
|
+
destinationMarketParams,
|
|
104
|
+
maxLtv,
|
|
105
|
+
signedAuthorization,
|
|
106
|
+
publicAllocations,
|
|
107
|
+
referralFeePct,
|
|
108
|
+
referralFeeRecipient,
|
|
109
|
+
params.args.deadline,
|
|
110
|
+
],
|
|
111
|
+
}),
|
|
213
112
|
action: {
|
|
214
113
|
type: "blueRefinance",
|
|
215
114
|
args: {
|
|
216
|
-
sourceMarket:
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
minBorrowSharePrice,
|
|
222
|
-
maxRepaySharePrice,
|
|
223
|
-
user,
|
|
224
|
-
reallocationFee,
|
|
115
|
+
sourceMarket: sourceMarketParams.id,
|
|
116
|
+
destinationMarket: destinationMarketParams.id,
|
|
117
|
+
maxLtv,
|
|
118
|
+
onBehalf: userAddress,
|
|
119
|
+
reallocations: publicAllocations.length,
|
|
225
120
|
reallocationPenaltyAssets,
|
|
121
|
+
referralFeePct,
|
|
122
|
+
referralFeeRecipient,
|
|
123
|
+
deadline: params.args.deadline,
|
|
226
124
|
},
|
|
227
125
|
},
|
|
228
126
|
});
|