@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,101 +1,90 @@
|
|
|
1
1
|
import type { MarketParams } from "@morpho-org/blue-sdk";
|
|
2
|
-
import type
|
|
3
|
-
import { type AuthorizationRequirementSignature, type
|
|
2
|
+
import { type Address } from "viem";
|
|
3
|
+
import { type AuthorizationRequirementSignature, type BlueWithdrawAction, type Metadata, type Transaction, type VaultV2BlueReallocation } from "../../types/index.js";
|
|
4
4
|
/** Parameters for {@link blueWithdraw}. */
|
|
5
5
|
export interface BlueWithdrawParams {
|
|
6
|
-
market
|
|
6
|
+
/** Chain and scoped Morpho Blue market. */
|
|
7
|
+
readonly market: {
|
|
7
8
|
readonly chainId: number;
|
|
8
9
|
readonly marketParams: MarketParams;
|
|
9
10
|
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
|
|
28
|
-
* prepended to the bundle so GeneralAdapter1 is authorized in-bundle instead of via a
|
|
29
|
-
* standalone `setAuthorization` transaction.
|
|
30
|
-
*/
|
|
31
|
-
authorizationSignature?: AuthorizationRequirementSignature;
|
|
11
|
+
/** Direct BlueBundlesV1 withdrawal arguments. */
|
|
12
|
+
readonly args: {
|
|
13
|
+
/** User whose supply position is withdrawn. */
|
|
14
|
+
readonly userAddress: Address;
|
|
15
|
+
/** Exact loan assets withdrawn, exclusive with `withdrawShares`. */
|
|
16
|
+
readonly withdrawAssets: bigint;
|
|
17
|
+
/** Exact supply shares burned, exclusive with `withdrawAssets`. */
|
|
18
|
+
readonly withdrawShares: bigint;
|
|
19
|
+
/** Optional validated Vault V2 BluePublicAllocator reallocations. */
|
|
20
|
+
readonly reallocations?: Iterable<VaultV2BlueReallocation>;
|
|
21
|
+
/** Final call deadline in Unix seconds. */
|
|
22
|
+
readonly deadline: bigint;
|
|
23
|
+
/** Optional WAD-scaled referral fee, strictly below 100%. */
|
|
24
|
+
readonly referralFeePct?: bigint;
|
|
25
|
+
/** Recipient required when `referralFeePct` is positive. */
|
|
26
|
+
readonly referralFeeRecipient?: Address;
|
|
27
|
+
/** Optional Morpho authorization signature for BlueBundlesV1. */
|
|
28
|
+
readonly authorizationSignature?: AuthorizationRequirementSignature;
|
|
32
29
|
};
|
|
33
|
-
metadata
|
|
30
|
+
/** Optional transaction metadata suffix. */
|
|
31
|
+
readonly metadata?: Metadata;
|
|
34
32
|
}
|
|
35
33
|
/**
|
|
36
|
-
*
|
|
34
|
+
* Encodes a direct BlueBundlesV1 loan-asset withdrawal transaction.
|
|
37
35
|
*
|
|
38
|
-
*
|
|
36
|
+
* Vault V2 allocator penalties and referral fees reduce the assets received. Shares mode has no
|
|
37
|
+
* saturated full-close sentinel or onchain minimum-assets guarantee, and this route has no
|
|
38
|
+
* Bundler3 share-price bound or `slippageTolerance` input.
|
|
39
39
|
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
* @
|
|
56
|
-
* @
|
|
57
|
-
*
|
|
58
|
-
* @
|
|
59
|
-
* @
|
|
60
|
-
* @
|
|
61
|
-
*
|
|
62
|
-
* @
|
|
63
|
-
*
|
|
64
|
-
* @
|
|
65
|
-
*
|
|
66
|
-
* @
|
|
67
|
-
* @
|
|
68
|
-
*
|
|
69
|
-
* @throws {
|
|
70
|
-
* penalty is negative.
|
|
71
|
-
* @throws {NonPositiveInputError} when both `assets` and `shares` are zero or any reallocation
|
|
72
|
-
* withdrawal amount is non-positive.
|
|
73
|
-
* @throws {InputExceedsMaxError} when a V2 reallocation asset amount exceeds `uint128` or its penalty exceeds WAD.
|
|
74
|
-
* @throws {InconsistentReallocationPenaltyError} when V2 entries for one vault use different penalties.
|
|
75
|
-
* @throws {InvalidReallocationAddressError} when a V2 vault or adapter address is malformed.
|
|
76
|
-
* @throws {InvalidReallocationSourceTypeError} when a V2 source is absent, incomplete, or has an unknown discriminator.
|
|
77
|
-
* @throws {InvalidReallocationShapeError} when an entry matches both or neither V1/V2 shape.
|
|
78
|
-
* @throws {MixedReallocationVersionsError} when one plan contains both V1 and V2 entries.
|
|
79
|
-
* @throws {MutuallyExclusiveWithdrawAmountsError} when both `assets` and `shares` are non-zero.
|
|
80
|
-
* @throws {EmptyReallocationWithdrawalsError} when any reallocation has no withdrawals.
|
|
81
|
-
* @throws {ReallocationWithdrawalOnTargetMarketError} when a reallocation withdrawal references
|
|
82
|
-
* the target market.
|
|
83
|
-
* @throws {UnsortedReallocationWithdrawalsError} when reallocation withdrawals are not strictly
|
|
84
|
-
* sorted by market id.
|
|
40
|
+
* @param params - Withdrawal encoding parameters.
|
|
41
|
+
* @param params.market.chainId - Chain containing BlueBundlesV1.
|
|
42
|
+
* @param params.market.marketParams - Scoped Morpho Blue market parameters.
|
|
43
|
+
* @param params.args.userAddress - User whose supply position is withdrawn.
|
|
44
|
+
* @param params.args.withdrawAssets - Exact assets withdrawn, exclusive with shares.
|
|
45
|
+
* @param params.args.withdrawShares - Exact shares burned, exclusive with assets.
|
|
46
|
+
* @param params.args.reallocations - Vault V2 reallocations executed before withdrawal.
|
|
47
|
+
* @param params.args.deadline - Final call deadline in Unix seconds.
|
|
48
|
+
* @param params.args.referralFeePct - Optional WAD-scaled fee below 100%.
|
|
49
|
+
* @param params.args.referralFeeRecipient - Recipient required for a positive fee.
|
|
50
|
+
* @param params.args.authorizationSignature - Optional Blue authorization signature.
|
|
51
|
+
* @param params.metadata - Optional transaction metadata.
|
|
52
|
+
* @returns A deep-frozen `Readonly<Transaction<BlueWithdrawAction>>` whose `to` address is
|
|
53
|
+
* BlueBundlesV1 and whose `action` records the normalized withdrawal inputs.
|
|
54
|
+
* @throws {NegativeInputError} when an amount, fee, or reallocation penalty is negative.
|
|
55
|
+
* @throws {MutuallyExclusiveWithdrawAmountsError} when assets and shares are both nonzero.
|
|
56
|
+
* @throws {NonPositiveInputError} when neither mode, no valid deadline, or a non-positive
|
|
57
|
+
* reallocation amount is provided.
|
|
58
|
+
* @throws {InputExceedsMaxError} when a withdraw amount, fee, reallocation amount, or penalty exceeds its ABI bound.
|
|
59
|
+
* @throws {MissingReferralFeeRecipientError} when a positive fee has no recipient.
|
|
60
|
+
* @throws {InvalidReallocationAddressError} when a vault or adapter address is malformed.
|
|
61
|
+
* @throws {InvalidReallocationShapeError} when a reallocation entry is not a valid Vault V2 reallocation.
|
|
62
|
+
* @throws {InvalidReallocationSourceTypeError} when a reallocation source is malformed.
|
|
63
|
+
* @throws {InconsistentReallocationPenaltyError} when one vault uses different penalties.
|
|
64
|
+
* @throws {ReallocationWithdrawalOnTargetMarketError} when a source is the target market.
|
|
65
|
+
* @throws {ReallocationLoanTokenMismatchError} when a source market uses another loan token.
|
|
66
|
+
* @throws {DepositOwnerMismatchError} when the signed authorization owner differs from `userAddress`.
|
|
67
|
+
* @throws {BlueBundlesV1RequirementSignatureMismatchError} when authorization cannot be bound safely.
|
|
68
|
+
* @throws {UnsupportedChainIdError} when the chain is absent from the registry.
|
|
69
|
+
* @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
|
|
85
70
|
* @example
|
|
86
71
|
* ```ts
|
|
72
|
+
* import { markets } from "@morpho-org/morpho-test";
|
|
87
73
|
* import { blueWithdraw } from "@morpho-org/morpho-sdk";
|
|
74
|
+
* import { zeroAddress } from "viem";
|
|
75
|
+
* import { mainnet } from "viem/chains";
|
|
88
76
|
*
|
|
77
|
+
* const marketParams = markets[mainnet.id].usdc_wbtc;
|
|
89
78
|
* const tx = blueWithdraw({
|
|
90
|
-
* market: { chainId:
|
|
79
|
+
* market: { chainId: mainnet.id, marketParams },
|
|
91
80
|
* args: {
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
81
|
+
* userAddress: zeroAddress,
|
|
82
|
+
* withdrawAssets: 1_000_000n,
|
|
83
|
+
* withdrawShares: 0n,
|
|
84
|
+
* deadline: 1_900_000_000n,
|
|
96
85
|
* },
|
|
97
86
|
* });
|
|
98
87
|
* // tx satisfies Readonly<Transaction<BlueWithdrawAction>>
|
|
99
88
|
* ```
|
|
100
89
|
*/
|
|
101
|
-
export declare const blueWithdraw: (
|
|
90
|
+
export declare const blueWithdraw: (params: BlueWithdrawParams) => Readonly<Transaction<BlueWithdrawAction>>;
|
|
@@ -1,124 +1,128 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import { buildBlueReallocationActions } from "./buildReallocationActions.js";
|
|
1
|
+
import { encodeFunctionData } from "viem";
|
|
2
|
+
import { blueBundlesV1Abi } from "../../abis.js";
|
|
3
|
+
import { validateUint256Field } from "../../helpers/validate.js";
|
|
4
|
+
import { InputExceedsMaxError, MutuallyExclusiveWithdrawAmountsError, NonPositiveInputError, } from "../../types/index.js";
|
|
5
|
+
import { finalizeBlueBundlesV1Transaction, getBlueBundlesV1PenaltyAssets, getBlueBundlesV1PublicAllocations, getBlueBundlesV1SignedAuthorization, normalizeBlueBundlesV1CommonParams, } from "./common.js";
|
|
7
6
|
/**
|
|
8
|
-
*
|
|
7
|
+
* Encodes a direct BlueBundlesV1 loan-asset withdrawal transaction.
|
|
9
8
|
*
|
|
10
|
-
*
|
|
9
|
+
* Vault V2 allocator penalties and referral fees reduce the assets received. Shares mode has no
|
|
10
|
+
* saturated full-close sentinel or onchain minimum-assets guarantee, and this route has no
|
|
11
|
+
* Bundler3 share-price bound or `slippageTolerance` input.
|
|
11
12
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
* @
|
|
28
|
-
* @
|
|
29
|
-
*
|
|
30
|
-
* @
|
|
31
|
-
* @
|
|
32
|
-
* @
|
|
33
|
-
*
|
|
34
|
-
* @
|
|
35
|
-
*
|
|
36
|
-
* @
|
|
37
|
-
*
|
|
38
|
-
* @
|
|
39
|
-
* @
|
|
40
|
-
*
|
|
41
|
-
* @throws {
|
|
42
|
-
* penalty is negative.
|
|
43
|
-
* @throws {NonPositiveInputError} when both `assets` and `shares` are zero or any reallocation
|
|
44
|
-
* withdrawal amount is non-positive.
|
|
45
|
-
* @throws {InputExceedsMaxError} when a V2 reallocation asset amount exceeds `uint128` or its penalty exceeds WAD.
|
|
46
|
-
* @throws {InconsistentReallocationPenaltyError} when V2 entries for one vault use different penalties.
|
|
47
|
-
* @throws {InvalidReallocationAddressError} when a V2 vault or adapter address is malformed.
|
|
48
|
-
* @throws {InvalidReallocationSourceTypeError} when a V2 source is absent, incomplete, or has an unknown discriminator.
|
|
49
|
-
* @throws {InvalidReallocationShapeError} when an entry matches both or neither V1/V2 shape.
|
|
50
|
-
* @throws {MixedReallocationVersionsError} when one plan contains both V1 and V2 entries.
|
|
51
|
-
* @throws {MutuallyExclusiveWithdrawAmountsError} when both `assets` and `shares` are non-zero.
|
|
52
|
-
* @throws {EmptyReallocationWithdrawalsError} when any reallocation has no withdrawals.
|
|
53
|
-
* @throws {ReallocationWithdrawalOnTargetMarketError} when a reallocation withdrawal references
|
|
54
|
-
* the target market.
|
|
55
|
-
* @throws {UnsortedReallocationWithdrawalsError} when reallocation withdrawals are not strictly
|
|
56
|
-
* sorted by market id.
|
|
13
|
+
* @param params - Withdrawal encoding parameters.
|
|
14
|
+
* @param params.market.chainId - Chain containing BlueBundlesV1.
|
|
15
|
+
* @param params.market.marketParams - Scoped Morpho Blue market parameters.
|
|
16
|
+
* @param params.args.userAddress - User whose supply position is withdrawn.
|
|
17
|
+
* @param params.args.withdrawAssets - Exact assets withdrawn, exclusive with shares.
|
|
18
|
+
* @param params.args.withdrawShares - Exact shares burned, exclusive with assets.
|
|
19
|
+
* @param params.args.reallocations - Vault V2 reallocations executed before withdrawal.
|
|
20
|
+
* @param params.args.deadline - Final call deadline in Unix seconds.
|
|
21
|
+
* @param params.args.referralFeePct - Optional WAD-scaled fee below 100%.
|
|
22
|
+
* @param params.args.referralFeeRecipient - Recipient required for a positive fee.
|
|
23
|
+
* @param params.args.authorizationSignature - Optional Blue authorization signature.
|
|
24
|
+
* @param params.metadata - Optional transaction metadata.
|
|
25
|
+
* @returns A deep-frozen `Readonly<Transaction<BlueWithdrawAction>>` whose `to` address is
|
|
26
|
+
* BlueBundlesV1 and whose `action` records the normalized withdrawal inputs.
|
|
27
|
+
* @throws {NegativeInputError} when an amount, fee, or reallocation penalty is negative.
|
|
28
|
+
* @throws {MutuallyExclusiveWithdrawAmountsError} when assets and shares are both nonzero.
|
|
29
|
+
* @throws {NonPositiveInputError} when neither mode, no valid deadline, or a non-positive
|
|
30
|
+
* reallocation amount is provided.
|
|
31
|
+
* @throws {InputExceedsMaxError} when a withdraw amount, fee, reallocation amount, or penalty exceeds its ABI bound.
|
|
32
|
+
* @throws {MissingReferralFeeRecipientError} when a positive fee has no recipient.
|
|
33
|
+
* @throws {InvalidReallocationAddressError} when a vault or adapter address is malformed.
|
|
34
|
+
* @throws {InvalidReallocationShapeError} when a reallocation entry is not a valid Vault V2 reallocation.
|
|
35
|
+
* @throws {InvalidReallocationSourceTypeError} when a reallocation source is malformed.
|
|
36
|
+
* @throws {InconsistentReallocationPenaltyError} when one vault uses different penalties.
|
|
37
|
+
* @throws {ReallocationWithdrawalOnTargetMarketError} when a source is the target market.
|
|
38
|
+
* @throws {ReallocationLoanTokenMismatchError} when a source market uses another loan token.
|
|
39
|
+
* @throws {DepositOwnerMismatchError} when the signed authorization owner differs from `userAddress`.
|
|
40
|
+
* @throws {BlueBundlesV1RequirementSignatureMismatchError} when authorization cannot be bound safely.
|
|
41
|
+
* @throws {UnsupportedChainIdError} when the chain is absent from the registry.
|
|
42
|
+
* @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
|
|
57
43
|
* @example
|
|
58
44
|
* ```ts
|
|
45
|
+
* import { markets } from "@morpho-org/morpho-test";
|
|
59
46
|
* import { blueWithdraw } from "@morpho-org/morpho-sdk";
|
|
47
|
+
* import { zeroAddress } from "viem";
|
|
48
|
+
* import { mainnet } from "viem/chains";
|
|
60
49
|
*
|
|
50
|
+
* const marketParams = markets[mainnet.id].usdc_wbtc;
|
|
61
51
|
* const tx = blueWithdraw({
|
|
62
|
-
* market: { chainId:
|
|
52
|
+
* market: { chainId: mainnet.id, marketParams },
|
|
63
53
|
* args: {
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
54
|
+
* userAddress: zeroAddress,
|
|
55
|
+
* withdrawAssets: 1_000_000n,
|
|
56
|
+
* withdrawShares: 0n,
|
|
57
|
+
* deadline: 1_900_000_000n,
|
|
68
58
|
* },
|
|
69
59
|
* });
|
|
70
60
|
* // tx satisfies Readonly<Transaction<BlueWithdrawAction>>
|
|
71
61
|
* ```
|
|
72
62
|
*/
|
|
73
|
-
export const blueWithdraw = (
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
63
|
+
export const blueWithdraw = (params) => {
|
|
64
|
+
const { chainId, marketParams } = params.market;
|
|
65
|
+
const { userAddress, withdrawAssets, withdrawShares, authorizationSignature, } = params.args;
|
|
66
|
+
validateUint256Field("withdrawAssets", withdrawAssets);
|
|
67
|
+
validateUint256Field("withdrawShares", withdrawShares);
|
|
68
|
+
if (withdrawAssets > 0n && withdrawShares > 0n) {
|
|
78
69
|
throw new MutuallyExclusiveWithdrawAmountsError(marketParams.id);
|
|
79
70
|
}
|
|
80
|
-
if (
|
|
81
|
-
throw new
|
|
82
|
-
}
|
|
83
|
-
if (shares < 0n) {
|
|
84
|
-
throw new NegativeInputError("shares", shares);
|
|
85
|
-
}
|
|
86
|
-
if (assets === 0n && shares === 0n) {
|
|
87
|
-
throw new NonPositiveInputError("assets or shares", 0n);
|
|
71
|
+
if (withdrawAssets === 0n && withdrawShares === 0n) {
|
|
72
|
+
throw new NonPositiveInputError("withdrawAssets or withdrawShares", 0n);
|
|
88
73
|
}
|
|
89
|
-
|
|
90
|
-
throw new NegativeInputError("minSharePrice", minSharePrice);
|
|
91
|
-
}
|
|
92
|
-
const actions = [];
|
|
93
|
-
if (authorizationSignature) {
|
|
94
|
-
actions.push(getBlueAuthorizationAction(chainId, authorizationSignature));
|
|
95
|
-
}
|
|
96
|
-
const { actions: reallocationActions, fee: reallocationFee, penaltyAssets: reallocationPenaltyAssets, } = buildBlueReallocationActions({
|
|
74
|
+
const common = {
|
|
97
75
|
chainId,
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
76
|
+
userAddress,
|
|
77
|
+
deadline: params.args.deadline,
|
|
78
|
+
referralFeePct: params.args.referralFeePct,
|
|
79
|
+
referralFeeRecipient: params.args.referralFeeRecipient,
|
|
80
|
+
metadata: params.metadata,
|
|
81
|
+
};
|
|
82
|
+
const { referralFeePct, referralFeeRecipient } = normalizeBlueBundlesV1CommonParams(common);
|
|
83
|
+
const signedAuthorization = getBlueBundlesV1SignedAuthorization({
|
|
84
|
+
chainId,
|
|
85
|
+
userAddress,
|
|
86
|
+
authorizationSignature,
|
|
105
87
|
});
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
88
|
+
const publicAllocations = getBlueBundlesV1PublicAllocations([...(params.args.reallocations ?? [])], marketParams);
|
|
89
|
+
const reallocationPenaltyAssets = getBlueBundlesV1PenaltyAssets(publicAllocations);
|
|
90
|
+
if (withdrawAssets > 0n && reallocationPenaltyAssets > withdrawAssets) {
|
|
91
|
+
throw new InputExceedsMaxError({
|
|
92
|
+
field: "reallocationPenaltyAssets",
|
|
93
|
+
value: reallocationPenaltyAssets,
|
|
94
|
+
max: withdrawAssets,
|
|
95
|
+
});
|
|
109
96
|
}
|
|
110
|
-
return
|
|
111
|
-
|
|
97
|
+
return finalizeBlueBundlesV1Transaction({
|
|
98
|
+
common,
|
|
99
|
+
value: 0n,
|
|
100
|
+
data: encodeFunctionData({
|
|
101
|
+
abi: blueBundlesV1Abi,
|
|
102
|
+
functionName: "blueBundlesV1Withdraw",
|
|
103
|
+
args: [
|
|
104
|
+
marketParams,
|
|
105
|
+
withdrawAssets,
|
|
106
|
+
withdrawShares,
|
|
107
|
+
signedAuthorization,
|
|
108
|
+
publicAllocations,
|
|
109
|
+
referralFeePct,
|
|
110
|
+
referralFeeRecipient,
|
|
111
|
+
params.args.deadline,
|
|
112
|
+
],
|
|
113
|
+
}),
|
|
112
114
|
action: {
|
|
113
115
|
type: "blueWithdraw",
|
|
114
116
|
args: {
|
|
115
117
|
market: marketParams.id,
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
reallocationFee,
|
|
118
|
+
withdrawAssets,
|
|
119
|
+
withdrawShares,
|
|
120
|
+
onBehalf: userAddress,
|
|
121
|
+
reallocations: publicAllocations.length,
|
|
121
122
|
reallocationPenaltyAssets,
|
|
123
|
+
referralFeePct,
|
|
124
|
+
referralFeeRecipient,
|
|
125
|
+
deadline: params.args.deadline,
|
|
122
126
|
},
|
|
123
127
|
},
|
|
124
128
|
});
|
|
@@ -1,49 +1,77 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import type { MarketParams } from "@morpho-org/blue-sdk";
|
|
2
|
+
import type { Address } from "viem";
|
|
3
|
+
import type { AuthorizationRequirementSignature, BlueWithdrawCollateralAction, Metadata, Transaction } from "../../types/index.js";
|
|
4
4
|
/** Parameters for {@link blueWithdrawCollateral}. */
|
|
5
5
|
export interface BlueWithdrawCollateralParams {
|
|
6
|
-
market
|
|
6
|
+
/** Chain and scoped Morpho Blue market. */
|
|
7
|
+
readonly market: {
|
|
7
8
|
readonly chainId: number;
|
|
8
9
|
readonly marketParams: MarketParams;
|
|
9
10
|
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
/** Direct BlueBundlesV1 collateral-withdrawal arguments. */
|
|
12
|
+
readonly args: {
|
|
13
|
+
/** User whose collateral position is reduced. */
|
|
14
|
+
readonly userAddress: Address;
|
|
15
|
+
/** Collateral assets withdrawn. */
|
|
16
|
+
readonly collateralAssets: bigint;
|
|
17
|
+
/** Maximum post-operation LTV enforced by BlueBundlesV1. */
|
|
18
|
+
readonly maxLtv: bigint;
|
|
19
|
+
/** Final call deadline in Unix seconds. */
|
|
20
|
+
readonly deadline: bigint;
|
|
21
|
+
/** Optional WAD-scaled referral fee, strictly below 100%. */
|
|
22
|
+
readonly referralFeePct?: bigint;
|
|
23
|
+
/** Recipient required when `referralFeePct` is positive. */
|
|
24
|
+
readonly referralFeeRecipient?: Address;
|
|
25
|
+
/** Optional Morpho authorization signature for BlueBundlesV1. */
|
|
26
|
+
readonly authorizationSignature?: AuthorizationRequirementSignature;
|
|
14
27
|
};
|
|
15
|
-
metadata
|
|
28
|
+
/** Optional transaction metadata suffix. */
|
|
29
|
+
readonly metadata?: Metadata;
|
|
16
30
|
}
|
|
17
31
|
/**
|
|
18
|
-
*
|
|
32
|
+
* Encodes a pure collateral withdrawal through BlueBundlesV1.
|
|
19
33
|
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
34
|
+
* Delegates to {@link blueRepayWithdrawCollateral} with a zero repay leg. BlueBundlesV1 enforces
|
|
35
|
+
* the supplied post-operation LTV, and no standalone Morpho or Bundler3 call is encoded.
|
|
22
36
|
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
* @param params.market.
|
|
26
|
-
* @param params.
|
|
27
|
-
* @param params.args.
|
|
28
|
-
* @param params.args.
|
|
29
|
-
* @param params.args.
|
|
30
|
-
* @param params.
|
|
31
|
-
* @
|
|
32
|
-
*
|
|
33
|
-
* @
|
|
37
|
+
* @param params - Collateral-withdrawal encoding parameters.
|
|
38
|
+
* @param params.market.chainId - Chain containing BlueBundlesV1.
|
|
39
|
+
* @param params.market.marketParams - Scoped Morpho Blue market parameters.
|
|
40
|
+
* @param params.args.userAddress - User whose collateral position is reduced.
|
|
41
|
+
* @param params.args.collateralAssets - Collateral assets withdrawn.
|
|
42
|
+
* @param params.args.maxLtv - Maximum post-operation LTV.
|
|
43
|
+
* @param params.args.deadline - Final call deadline in Unix seconds.
|
|
44
|
+
* @param params.args.referralFeePct - Optional WAD-scaled referral fee.
|
|
45
|
+
* @param params.args.referralFeeRecipient - Recipient required for a positive fee.
|
|
46
|
+
* @param params.args.authorizationSignature - Optional Blue authorization signature.
|
|
47
|
+
* @param params.metadata - Optional transaction metadata.
|
|
48
|
+
* @returns A deep-frozen `Readonly<Transaction<BlueWithdrawCollateralAction>>` whose calldata
|
|
49
|
+
* invokes the combined BlueBundlesV1 entrypoint with a zero repay leg.
|
|
50
|
+
* @throws {NegativeInputError} when collateral, LTV, or fee is negative.
|
|
51
|
+
* @throws {NonPositiveInputError} when collateral or deadline is not positive.
|
|
52
|
+
* @throws {InputExceedsMaxError} when the referral fee is at least WAD.
|
|
53
|
+
* @throws {MissingReferralFeeRecipientError} when a positive fee has no recipient.
|
|
54
|
+
* @throws {DepositOwnerMismatchError} when the signed authorization owner differs from `userAddress`.
|
|
55
|
+
* @throws {BlueBundlesV1RequirementSignatureMismatchError} when authorization cannot be bound safely.
|
|
56
|
+
* @throws {UnsupportedChainIdError} when the chain is absent from the registry.
|
|
57
|
+
* @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
|
|
34
58
|
* @example
|
|
35
59
|
* ```ts
|
|
60
|
+
* import { markets } from "@morpho-org/morpho-test";
|
|
36
61
|
* import { blueWithdrawCollateral } from "@morpho-org/morpho-sdk";
|
|
62
|
+
* import { zeroAddress } from "viem";
|
|
63
|
+
* import { mainnet } from "viem/chains";
|
|
37
64
|
*
|
|
38
65
|
* const tx = blueWithdrawCollateral({
|
|
39
|
-
* market: { chainId:
|
|
66
|
+
* market: { chainId: mainnet.id, marketParams: markets[mainnet.id].usdc_wbtc },
|
|
40
67
|
* args: {
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
68
|
+
* userAddress: zeroAddress,
|
|
69
|
+
* collateralAssets: 1_000_000_000_000_000_000n,
|
|
70
|
+
* maxLtv: 850000000000000000n,
|
|
71
|
+
* deadline: 1_900_000_000n,
|
|
44
72
|
* },
|
|
45
73
|
* });
|
|
46
74
|
* // tx satisfies Readonly<Transaction<BlueWithdrawCollateralAction>>
|
|
47
75
|
* ```
|
|
48
76
|
*/
|
|
49
|
-
export declare const blueWithdrawCollateral: (
|
|
77
|
+
export declare const blueWithdrawCollateral: (params: BlueWithdrawCollateralParams) => Readonly<Transaction<BlueWithdrawCollateralAction>>;
|