@morpho-org/morpho-sdk 10.0.1 → 10.1.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/README.md +31 -10
- package/lib/cjs/abis.d.ts +1 -1
- package/lib/cjs/abis.js +5 -1
- package/lib/cjs/actions/blue/refinance.d.ts +1 -1
- package/lib/cjs/actions/blue/refinance.js +3 -8
- package/lib/cjs/actions/blue/refinanceValidation.d.ts +19 -0
- package/lib/cjs/actions/blue/refinanceValidation.js +46 -0
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +1 -1
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +3 -7
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +1 -1
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +4 -20
- package/lib/cjs/actions/requirements/getRequirementsApproval.js +7 -21
- package/lib/cjs/actions/requirements/validateClientChainId.d.ts +1 -26
- package/lib/cjs/actions/requirements/validateClientChainId.js +2 -31
- package/lib/cjs/actions/vaultV2/forceRedeem.d.ts +2 -1
- package/lib/cjs/actions/vaultV2/forceRedeem.js +20 -2
- package/lib/cjs/blue/abis.d.ts +1 -1
- package/lib/cjs/blue/abis.js +5 -1
- package/lib/cjs/blue/entities.d.ts +4 -4
- package/lib/cjs/blue/entities.js +3 -1
- package/lib/cjs/blue/errors.d.ts +2 -2
- package/lib/cjs/blue/errors.js +5 -1
- package/lib/cjs/blue/fetch.d.ts +2 -2
- package/lib/cjs/blue/fetch.js +3 -1
- package/lib/cjs/blue/utils.d.ts +1 -1
- package/lib/cjs/blue/utils.js +2 -1
- package/lib/cjs/entities/blue/blue.js +24 -55
- package/lib/cjs/entities/index.d.ts +4 -4
- package/lib/cjs/entities/index.js +3 -1
- package/lib/cjs/entities/requirements/getErc2612Permit.d.ts +2 -5
- package/lib/cjs/entities/requirements/getErc2612Permit.js +9 -58
- package/lib/cjs/entities/vaultV2/vaultV2.d.ts +34 -8
- package/lib/cjs/entities/vaultV2/vaultV2.js +39 -1
- package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +3 -2
- package/lib/cjs/entities/vaultV2BlueReallocationData.js +6 -2
- package/lib/cjs/errors.d.ts +4 -4
- package/lib/cjs/errors.js +7 -5
- package/lib/cjs/fetch.d.ts +2 -2
- package/lib/cjs/fetch.js +3 -1
- package/lib/cjs/helpers/deadShares.js +3 -0
- package/lib/cjs/helpers/quoteBlueRepay.d.ts +16 -0
- package/lib/cjs/helpers/quoteBlueRepay.js +46 -0
- package/lib/cjs/helpers/tokenApproval.d.ts +14 -0
- package/lib/cjs/helpers/tokenApproval.js +20 -0
- package/lib/cjs/helpers/validate.d.ts +1 -1
- package/lib/cjs/helpers/validate.js +2 -2
- package/lib/cjs/index.d.ts +2 -2
- package/lib/cjs/index.js +13 -1
- package/lib/cjs/midnight/errors.d.ts +2 -1
- package/lib/cjs/midnight/errors.js +5 -4
- package/lib/cjs/reads/readErc2612Permit.d.ts +22 -0
- package/lib/cjs/reads/readErc2612Permit.js +77 -0
- package/lib/cjs/reads/validateClientChainId.d.ts +26 -0
- package/lib/cjs/reads/validateClientChainId.js +34 -0
- package/lib/cjs/transactionPlan/blue/authorization.d.ts +23 -0
- package/lib/cjs/transactionPlan/blue/authorization.js +128 -0
- package/lib/cjs/transactionPlan/blue/borrow.d.ts +80 -0
- package/lib/cjs/transactionPlan/blue/borrow.js +248 -0
- package/lib/cjs/transactionPlan/blue/prepare.d.ts +102 -0
- package/lib/cjs/transactionPlan/blue/prepare.js +191 -0
- package/lib/cjs/transactionPlan/blue/refinance.d.ts +93 -0
- package/lib/cjs/transactionPlan/blue/refinance.js +281 -0
- package/lib/cjs/transactionPlan/blue/refinanceValidation.d.ts +11 -0
- package/lib/cjs/transactionPlan/blue/refinanceValidation.js +20 -0
- package/lib/cjs/transactionPlan/blue/repay.d.ts +80 -0
- package/lib/cjs/transactionPlan/blue/repay.js +229 -0
- package/lib/cjs/transactionPlan/blue/repayWithdrawCollateral.d.ts +98 -0
- package/lib/cjs/transactionPlan/blue/repayWithdrawCollateral.js +379 -0
- package/lib/cjs/transactionPlan/blue/supply.d.ts +181 -0
- package/lib/cjs/transactionPlan/blue/supply.js +238 -0
- package/lib/cjs/transactionPlan/blue/supplyCollateral.d.ts +97 -0
- package/lib/cjs/transactionPlan/blue/supplyCollateral.js +231 -0
- package/lib/cjs/transactionPlan/blue/supplyCollateralBorrow.d.ts +102 -0
- package/lib/cjs/transactionPlan/blue/supplyCollateralBorrow.js +395 -0
- package/lib/cjs/transactionPlan/blue/withdraw.d.ts +136 -0
- package/lib/cjs/transactionPlan/blue/withdraw.js +340 -0
- package/lib/cjs/transactionPlan/blue/withdrawCollateral.d.ts +123 -0
- package/lib/cjs/transactionPlan/blue/withdrawCollateral.js +246 -0
- package/lib/cjs/transactionPlan/deposit.js +8 -19
- package/lib/cjs/transactionPlan/error.d.ts +50 -0
- package/lib/cjs/transactionPlan/error.js +60 -1
- package/lib/cjs/transactionPlan/getTokenPermit.d.ts +17 -8
- package/lib/cjs/transactionPlan/getTokenPermit.js +35 -7
- package/lib/cjs/transactionPlan/index.d.ts +21 -1
- package/lib/cjs/transactionPlan/index.js +27 -5
- package/lib/cjs/transactionPlan/intent.d.ts +2 -2
- package/lib/cjs/transactionPlan/intent.js +8 -4
- package/lib/cjs/transactionPlan/permit.d.ts +16 -2
- package/lib/cjs/transactionPlan/permit.js +77 -45
- package/lib/cjs/transactionPlan/plan.d.ts +67 -12
- package/lib/cjs/transactionPlan/plan.js +284 -50
- package/lib/cjs/transactionPlan/requirements.d.ts +14 -5
- package/lib/cjs/transactionPlan/requirements.js +30 -12
- package/lib/cjs/transactionPlan/serde.d.ts +7 -4
- package/lib/cjs/transactionPlan/serde.js +80 -61
- package/lib/cjs/transactionPlan/types.d.ts +141 -13
- package/lib/cjs/transactionPlan/validateActions.js +11 -1
- package/lib/cjs/transactionPlan/validation.d.ts +8 -0
- package/lib/cjs/transactionPlan/validation.js +90 -0
- package/lib/cjs/transactionPlan/vaultv1/deposit.d.ts +1 -0
- package/lib/cjs/transactionPlan/vaultv1/deposit.js +1 -0
- package/lib/cjs/transactionPlan/vaultv1/inKindRedeem.d.ts +1 -0
- package/lib/cjs/transactionPlan/vaultv1/inKindRedeem.js +1 -0
- package/lib/cjs/transactionPlan/vaultv1/migrateToV2.d.ts +1 -0
- package/lib/cjs/transactionPlan/vaultv1/migrateToV2.js +1 -0
- package/lib/cjs/transactionPlan/vaultv1/redeem.d.ts +1 -0
- package/lib/cjs/transactionPlan/vaultv1/redeem.js +1 -0
- package/lib/cjs/transactionPlan/vaultv1/withdraw.d.ts +1 -0
- package/lib/cjs/transactionPlan/vaultv1/withdraw.js +1 -0
- package/lib/cjs/transactionPlan/vaultv2/deposit.d.ts +1 -0
- package/lib/cjs/transactionPlan/vaultv2/deposit.js +1 -0
- package/lib/cjs/transactionPlan/vaultv2/forceRedeem.d.ts +1 -0
- package/lib/cjs/transactionPlan/vaultv2/forceRedeem.js +1 -0
- package/lib/cjs/transactionPlan/vaultv2/forceWithdraw.d.ts +1 -0
- package/lib/cjs/transactionPlan/vaultv2/forceWithdraw.js +1 -0
- package/lib/cjs/transactionPlan/vaultv2/inKindRedeem.d.ts +1 -0
- package/lib/cjs/transactionPlan/vaultv2/inKindRedeem.js +1 -0
- package/lib/cjs/transactionPlan/vaultv2/redeem.d.ts +1 -0
- package/lib/cjs/transactionPlan/vaultv2/redeem.js +1 -0
- package/lib/cjs/transactionPlan/vaultv2/withdraw.d.ts +1 -0
- package/lib/cjs/transactionPlan/vaultv2/withdraw.js +1 -0
- package/lib/cjs/utils.d.ts +2 -2
- package/lib/cjs/utils.js +4 -2
- package/lib/cjs/version.d.ts +1 -1
- package/lib/cjs/version.js +1 -1
- package/lib/esm/abis.d.ts +1 -1
- package/lib/esm/abis.js +1 -1
- package/lib/esm/actions/blue/refinance.d.ts +1 -1
- package/lib/esm/actions/blue/refinance.js +4 -9
- package/lib/esm/actions/blue/refinanceValidation.d.ts +19 -0
- package/lib/esm/actions/blue/refinanceValidation.js +41 -0
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +1 -1
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +4 -8
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +1 -1
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +5 -21
- package/lib/esm/actions/requirements/getRequirementsApproval.js +7 -21
- package/lib/esm/actions/requirements/validateClientChainId.d.ts +1 -26
- package/lib/esm/actions/requirements/validateClientChainId.js +1 -30
- package/lib/esm/actions/vaultV2/forceRedeem.d.ts +2 -1
- package/lib/esm/actions/vaultV2/forceRedeem.js +20 -2
- package/lib/esm/blue/abis.d.ts +1 -1
- package/lib/esm/blue/abis.js +1 -1
- package/lib/esm/blue/entities.d.ts +4 -4
- package/lib/esm/blue/entities.js +2 -2
- package/lib/esm/blue/errors.d.ts +2 -2
- package/lib/esm/blue/errors.js +2 -2
- package/lib/esm/blue/fetch.d.ts +2 -2
- package/lib/esm/blue/fetch.js +2 -2
- package/lib/esm/blue/utils.d.ts +1 -1
- package/lib/esm/blue/utils.js +1 -1
- package/lib/esm/entities/blue/blue.js +26 -57
- package/lib/esm/entities/index.d.ts +4 -4
- package/lib/esm/entities/index.js +2 -2
- package/lib/esm/entities/requirements/getErc2612Permit.d.ts +2 -5
- package/lib/esm/entities/requirements/getErc2612Permit.js +9 -58
- package/lib/esm/entities/vaultV2/vaultV2.d.ts +34 -8
- package/lib/esm/entities/vaultV2/vaultV2.js +40 -2
- package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +3 -2
- package/lib/esm/entities/vaultV2BlueReallocationData.js +6 -2
- package/lib/esm/errors.d.ts +4 -4
- package/lib/esm/errors.js +4 -4
- package/lib/esm/fetch.d.ts +2 -2
- package/lib/esm/fetch.js +2 -2
- package/lib/esm/helpers/deadShares.js +4 -1
- package/lib/esm/helpers/quoteBlueRepay.d.ts +16 -0
- package/lib/esm/helpers/quoteBlueRepay.js +42 -0
- package/lib/esm/helpers/tokenApproval.d.ts +14 -0
- package/lib/esm/helpers/tokenApproval.js +16 -0
- package/lib/esm/helpers/validate.d.ts +1 -1
- package/lib/esm/helpers/validate.js +3 -3
- package/lib/esm/index.d.ts +2 -2
- package/lib/esm/index.js +1 -1
- package/lib/esm/midnight/errors.d.ts +2 -1
- package/lib/esm/midnight/errors.js +2 -1
- package/lib/esm/reads/readErc2612Permit.d.ts +22 -0
- package/lib/esm/reads/readErc2612Permit.js +73 -0
- package/lib/esm/reads/validateClientChainId.d.ts +26 -0
- package/lib/esm/reads/validateClientChainId.js +30 -0
- package/lib/esm/transactionPlan/blue/authorization.d.ts +23 -0
- package/lib/esm/transactionPlan/blue/authorization.js +122 -0
- package/lib/esm/transactionPlan/blue/borrow.d.ts +80 -0
- package/lib/esm/transactionPlan/blue/borrow.js +243 -0
- package/lib/esm/transactionPlan/blue/prepare.d.ts +102 -0
- package/lib/esm/transactionPlan/blue/prepare.js +187 -0
- package/lib/esm/transactionPlan/blue/refinance.d.ts +93 -0
- package/lib/esm/transactionPlan/blue/refinance.js +276 -0
- package/lib/esm/transactionPlan/blue/refinanceValidation.d.ts +11 -0
- package/lib/esm/transactionPlan/blue/refinanceValidation.js +17 -0
- package/lib/esm/transactionPlan/blue/repay.d.ts +80 -0
- package/lib/esm/transactionPlan/blue/repay.js +223 -0
- package/lib/esm/transactionPlan/blue/repayWithdrawCollateral.d.ts +98 -0
- package/lib/esm/transactionPlan/blue/repayWithdrawCollateral.js +373 -0
- package/lib/esm/transactionPlan/blue/supply.d.ts +181 -0
- package/lib/esm/transactionPlan/blue/supply.js +232 -0
- package/lib/esm/transactionPlan/blue/supplyCollateral.d.ts +97 -0
- package/lib/esm/transactionPlan/blue/supplyCollateral.js +225 -0
- package/lib/esm/transactionPlan/blue/supplyCollateralBorrow.d.ts +102 -0
- package/lib/esm/transactionPlan/blue/supplyCollateralBorrow.js +389 -0
- package/lib/esm/transactionPlan/blue/withdraw.d.ts +136 -0
- package/lib/esm/transactionPlan/blue/withdraw.js +334 -0
- package/lib/esm/transactionPlan/blue/withdrawCollateral.d.ts +123 -0
- package/lib/esm/transactionPlan/blue/withdrawCollateral.js +240 -0
- package/lib/esm/transactionPlan/deposit.js +9 -20
- package/lib/esm/transactionPlan/error.d.ts +50 -0
- package/lib/esm/transactionPlan/error.js +57 -0
- package/lib/esm/transactionPlan/getTokenPermit.d.ts +17 -8
- package/lib/esm/transactionPlan/getTokenPermit.js +35 -7
- package/lib/esm/transactionPlan/index.d.ts +21 -1
- package/lib/esm/transactionPlan/index.js +11 -1
- package/lib/esm/transactionPlan/intent.d.ts +2 -2
- package/lib/esm/transactionPlan/intent.js +8 -4
- package/lib/esm/transactionPlan/permit.d.ts +16 -2
- package/lib/esm/transactionPlan/permit.js +76 -44
- package/lib/esm/transactionPlan/plan.d.ts +67 -12
- package/lib/esm/transactionPlan/plan.js +279 -46
- package/lib/esm/transactionPlan/requirements.d.ts +14 -5
- package/lib/esm/transactionPlan/requirements.js +30 -12
- package/lib/esm/transactionPlan/serde.d.ts +7 -4
- package/lib/esm/transactionPlan/serde.js +73 -54
- package/lib/esm/transactionPlan/types.d.ts +141 -13
- package/lib/esm/transactionPlan/validateActions.js +11 -1
- package/lib/esm/transactionPlan/validation.d.ts +8 -0
- package/lib/esm/transactionPlan/validation.js +88 -1
- package/lib/esm/transactionPlan/vaultv1/deposit.d.ts +1 -0
- package/lib/esm/transactionPlan/vaultv1/deposit.js +1 -0
- package/lib/esm/transactionPlan/vaultv1/inKindRedeem.d.ts +1 -0
- package/lib/esm/transactionPlan/vaultv1/inKindRedeem.js +1 -0
- package/lib/esm/transactionPlan/vaultv1/migrateToV2.d.ts +1 -0
- package/lib/esm/transactionPlan/vaultv1/migrateToV2.js +1 -0
- package/lib/esm/transactionPlan/vaultv1/redeem.d.ts +1 -0
- package/lib/esm/transactionPlan/vaultv1/redeem.js +1 -0
- package/lib/esm/transactionPlan/vaultv1/withdraw.d.ts +1 -0
- package/lib/esm/transactionPlan/vaultv1/withdraw.js +1 -0
- package/lib/esm/transactionPlan/vaultv2/deposit.d.ts +1 -0
- package/lib/esm/transactionPlan/vaultv2/deposit.js +1 -0
- package/lib/esm/transactionPlan/vaultv2/forceRedeem.d.ts +1 -0
- package/lib/esm/transactionPlan/vaultv2/forceRedeem.js +1 -0
- package/lib/esm/transactionPlan/vaultv2/forceWithdraw.d.ts +1 -0
- package/lib/esm/transactionPlan/vaultv2/forceWithdraw.js +1 -0
- package/lib/esm/transactionPlan/vaultv2/inKindRedeem.d.ts +1 -0
- package/lib/esm/transactionPlan/vaultv2/inKindRedeem.js +1 -0
- package/lib/esm/transactionPlan/vaultv2/redeem.d.ts +1 -0
- package/lib/esm/transactionPlan/vaultv2/redeem.js +1 -0
- package/lib/esm/transactionPlan/vaultv2/withdraw.d.ts +1 -0
- package/lib/esm/transactionPlan/vaultv2/withdraw.js +1 -0
- package/lib/esm/utils.d.ts +2 -2
- package/lib/esm/utils.js +2 -2
- package/lib/esm/version.d.ts +1 -1
- package/lib/esm/version.js +1 -1
- package/package.json +7 -7
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
import { MathLib } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { deepFreeze, Time } from "@morpho-org/morpho-ts";
|
|
3
|
+
import { maxUint256 } from "viem";
|
|
4
|
+
import { validateBlueBundlesV1NativeFunding } from "../../actions/blue/common.js";
|
|
5
|
+
import { blueSupplyCollateralBorrow } from "../../actions/blue/supplyCollateralBorrow.js";
|
|
6
|
+
import { validateClientChainId } from "../../actions/requirements/validateClientChainId.js";
|
|
7
|
+
import { DEFAULT_LLTV_BUFFER, validatePositionHealth, } from "../../helpers/index.js";
|
|
8
|
+
import { MissingAccrualPositionError, } from "../../types/index.js";
|
|
9
|
+
import { IncompleteTransactionPlanError, InvalidTransactionPlanError, TransactionPlanSignatureMismatchError, } from "../error.js";
|
|
10
|
+
import { getVaultPermit, validateTokenPermitRequest } from "../permit.js";
|
|
11
|
+
import { getPlanObject, validatePlanField, validatePlanFields, } from "../validation.js";
|
|
12
|
+
import { validateMorphoAuthorizationRequest } from "./authorization.js";
|
|
13
|
+
import { prepareBlue, } from "./prepare.js";
|
|
14
|
+
/**
|
|
15
|
+
* Prepares collateral funding and Morpho authorization for one BlueBundlesV1 call. Snapshots
|
|
16
|
+
* are validated before reads; the position is quoted through max(now, lastUpdate) + two hours,
|
|
17
|
+
* matching the stable entity handle. The SDK never signs. Native funding is exclusive and
|
|
18
|
+
* only supports the chain's wrapped native collateral token. Reallocation penalties reduce proceeds.
|
|
19
|
+
* @param client - Viem client used for prerequisite and registered-contract reads.
|
|
20
|
+
* @param params - Intent, amounts and caller-fetched market/position snapshots.
|
|
21
|
+
* @returns A data-only plan with approvals, authorization and the combined operation.
|
|
22
|
+
* @throws {ZeroAccountError} when the account is zero.
|
|
23
|
+
* @throws {NonIntegerInputError} when expiry or referral basis points are not safe integers.
|
|
24
|
+
* @throws {ExpiredDeadlineError} when expiry is not later than the reference timestamp.
|
|
25
|
+
* @throws {InvalidCalldataOriginError} when attribution bytes are malformed or empty.
|
|
26
|
+
* @throws {BlueSnapshotChainMismatchError} when a snapshot belongs to another chain.
|
|
27
|
+
* @throws {MarketIdMismatchError} when a snapshot or market tuple belongs to another market.
|
|
28
|
+
* @throws {AccrualPositionUserMismatchError} when the position belongs to another account.
|
|
29
|
+
* @throws {MissingAccrualPositionError} when a borrow has no position snapshot.
|
|
30
|
+
* @throws {MissingMarketPriceError} when the position snapshot has no usable oracle price.
|
|
31
|
+
* @throws {BorrowExceedsSafeLtvError} when the quoted debt exceeds the buffered LLTV bound.
|
|
32
|
+
* @throws {NegativeInputError} when an amount or referral fee is negative.
|
|
33
|
+
* @throws {NonPositiveInputError} when both legs are zero, or a reallocation amount is not positive.
|
|
34
|
+
* @throws {InputExceedsMaxError} when an amount exceeds its ABI bound or aggregate penalties exceed borrowing.
|
|
35
|
+
* @throws {ReallocationsRequireBorrowError} when reallocations accompany no borrow.
|
|
36
|
+
* @throws {InvalidReallocationAddressError} when a vault or adapter address is malformed.
|
|
37
|
+
* @throws {InvalidVaultV2BlueReallocationShapeError} when a reallocation has an invalid shape.
|
|
38
|
+
* @throws {InvalidReallocationSourceTypeError} when a source is incomplete or unknown.
|
|
39
|
+
* @throws {InconsistentReallocationPenaltyError} when one vault has conflicting penalties.
|
|
40
|
+
* @throws {ReallocationWithdrawalOnTargetMarketError} when the source equals the target market.
|
|
41
|
+
* @throws {ReallocationLoanTokenMismatchError} when a source has another loan token.
|
|
42
|
+
* @throws {ReferralFeePctExceededError} when the referral fee reaches 100%.
|
|
43
|
+
* @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no nonzero recipient.
|
|
44
|
+
* @throws {NativeAmountOnNonWNativeAssetError} when native funding targets another token.
|
|
45
|
+
* @throws {ChainWNativeMissingError} when native funding has no registered wrapped native token.
|
|
46
|
+
* @throws {ChainIdMismatchError} when the client targets another chain.
|
|
47
|
+
* @throws {MissingContractCodeError} when a registered execution target is absent.
|
|
48
|
+
* @throws {UnsupportedChainIdError} when the chain is not registered.
|
|
49
|
+
* @throws {UnknownAddressError} when the chain has no registered execution target.
|
|
50
|
+
* @see blueSupplyCollateralBorrow for the underlying encoder constraints.
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* import { prepareBlueSupplyCollateralBorrow } from "@morpho-org/morpho-sdk";
|
|
54
|
+
* import { MarketParams } from "@morpho-org/morpho-sdk/blue/entities";
|
|
55
|
+
* import { fetchAccrualPosition, fetchMarket } from "@morpho-org/morpho-sdk/blue/fetch";
|
|
56
|
+
* import { createPublicClient, http, parseEther } from "viem";
|
|
57
|
+
* import { mainnet } from "viem/chains";
|
|
58
|
+
* const client = createPublicClient({ chain: mainnet, transport: http() });
|
|
59
|
+
* const marketParams = new MarketParams({
|
|
60
|
+
* loanToken: "0xdC035D45d973E3EC169d2276DDab16f1e407384F",
|
|
61
|
+
* collateralToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
|
|
62
|
+
* oracle: "0x76b2242ea5BE1FCBBF4206EA09601EA5aB22Af4d",
|
|
63
|
+
* irm: "0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC",
|
|
64
|
+
* lltv: 860000000000000000n,
|
|
65
|
+
* });
|
|
66
|
+
* const account = "0x0000000000000000000000000000000000000001";
|
|
67
|
+
* const block = await client.getBlock();
|
|
68
|
+
* const fetchParams = { blockNumber: block.number };
|
|
69
|
+
* const plan = await prepareBlueSupplyCollateralBorrow(client, {
|
|
70
|
+
* chainId: mainnet.id, account, marketId: marketParams.id,
|
|
71
|
+
* marketData: { chainId: mainnet.id, market: await fetchMarket(marketParams.id, client, fetchParams) },
|
|
72
|
+
* positionData: { chainId: mainnet.id, position: await fetchAccrualPosition(account, marketParams.id, client, fetchParams) },
|
|
73
|
+
* collateralAssets: parseEther("1"), borrowAssets: parseEther("100"),
|
|
74
|
+
* now: block.timestamp, supportSignature: true,
|
|
75
|
+
* });
|
|
76
|
+
* // plan: TransactionPlan; sign its pending requests externally before building.
|
|
77
|
+
* ```
|
|
78
|
+
* @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
|
|
79
|
+
*/
|
|
80
|
+
export async function prepareBlueSupplyCollateralBorrow(client, params) {
|
|
81
|
+
const preparedParams = captureSupplyBorrowIntent(params);
|
|
82
|
+
const { chainId, account, marketId, marketData, positionData, now } = preparedParams;
|
|
83
|
+
const { collateralAssets, borrowAssets } = params;
|
|
84
|
+
const id = "blueSupplyCollateralBorrow";
|
|
85
|
+
let label = "Supply collateral and borrow on Blue";
|
|
86
|
+
if (borrowAssets === 0n) {
|
|
87
|
+
label = "Supply collateral on Blue";
|
|
88
|
+
}
|
|
89
|
+
else if (collateralAssets === 0n) {
|
|
90
|
+
label = "Borrow on Blue";
|
|
91
|
+
}
|
|
92
|
+
const marketParams = marketData.market.params;
|
|
93
|
+
const reallocations = [...(params.reallocations ?? [])];
|
|
94
|
+
const maxLtv = borrowAssets > 0n
|
|
95
|
+
? MathLib.zeroFloorSub(marketParams.lltv, DEFAULT_LLTV_BUFFER)
|
|
96
|
+
: maxUint256;
|
|
97
|
+
const nativeAmount = params.funding?.type === "native" ? collateralAssets : undefined;
|
|
98
|
+
const prepared = await prepareBlue({ client, params: preparedParams }, (intent) => {
|
|
99
|
+
// Run all encoder constraints before reading prerequisites.
|
|
100
|
+
blueSupplyCollateralBorrow({
|
|
101
|
+
market: { chainId, marketParams },
|
|
102
|
+
args: {
|
|
103
|
+
userAddress: account,
|
|
104
|
+
collateralAssets,
|
|
105
|
+
borrowAssets,
|
|
106
|
+
maxLtv,
|
|
107
|
+
nativeAmount,
|
|
108
|
+
reallocations,
|
|
109
|
+
...intent,
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
if (borrowAssets > 0n) {
|
|
113
|
+
const position = positionData?.position;
|
|
114
|
+
if (position === undefined) {
|
|
115
|
+
throw new MissingAccrualPositionError(marketId);
|
|
116
|
+
}
|
|
117
|
+
validatePositionHealth({
|
|
118
|
+
positionData: position.accrueInterest(MathLib.max(now, position.market.lastUpdate) + Time.s.from.h(2n)),
|
|
119
|
+
additionalCollateral: collateralAssets,
|
|
120
|
+
borrowAmount: borrowAssets,
|
|
121
|
+
marketId,
|
|
122
|
+
lltv: marketParams.lltv,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
const native = validateBlueBundlesV1NativeFunding({
|
|
126
|
+
chainId,
|
|
127
|
+
token: marketParams.collateralToken,
|
|
128
|
+
fundedAmount: collateralAssets,
|
|
129
|
+
nativeAmount,
|
|
130
|
+
});
|
|
131
|
+
return {
|
|
132
|
+
id,
|
|
133
|
+
needsAuthorization: borrowAssets > 0n,
|
|
134
|
+
...(collateralAssets > 0n && native === 0n
|
|
135
|
+
? {
|
|
136
|
+
tokenPull: {
|
|
137
|
+
token: marketParams.collateralToken,
|
|
138
|
+
amount: collateralAssets,
|
|
139
|
+
},
|
|
140
|
+
}
|
|
141
|
+
: {}),
|
|
142
|
+
};
|
|
143
|
+
});
|
|
144
|
+
// Native-only supply has no prerequisites that would otherwise validate the client's chain.
|
|
145
|
+
await validateClientChainId(client, chainId);
|
|
146
|
+
const authorization = prepared.signatureRequests.find((request) => request.id === prepared.morphoAuthorizationRequestId);
|
|
147
|
+
const permit = prepared.signatureRequests.find((request) => request.kind === "permit");
|
|
148
|
+
return deepFreeze(structuredClone({
|
|
149
|
+
version: 2,
|
|
150
|
+
chainId,
|
|
151
|
+
account,
|
|
152
|
+
expiresAt: prepared.intent.expiresAt,
|
|
153
|
+
...(prepared.intent.calldataOrigin && {
|
|
154
|
+
calldataOrigin: prepared.intent.calldataOrigin,
|
|
155
|
+
}),
|
|
156
|
+
steps: [
|
|
157
|
+
...prepared.steps,
|
|
158
|
+
{
|
|
159
|
+
id,
|
|
160
|
+
label,
|
|
161
|
+
action: {
|
|
162
|
+
type: "blueSupplyCollateralBorrow",
|
|
163
|
+
inputs: {
|
|
164
|
+
chainId,
|
|
165
|
+
account,
|
|
166
|
+
marketParams: { ...marketParams, id: marketParams.id },
|
|
167
|
+
collateralAssets,
|
|
168
|
+
borrowAssets,
|
|
169
|
+
maxLtv,
|
|
170
|
+
nativeAssets: nativeAmount,
|
|
171
|
+
reallocations: reallocations.map((entry) => ({
|
|
172
|
+
...entry,
|
|
173
|
+
from: entry.from.type === "market"
|
|
174
|
+
? {
|
|
175
|
+
...entry.from,
|
|
176
|
+
marketParams: {
|
|
177
|
+
...entry.from.marketParams,
|
|
178
|
+
id: entry.from.marketParams.id,
|
|
179
|
+
},
|
|
180
|
+
}
|
|
181
|
+
: entry.from,
|
|
182
|
+
})),
|
|
183
|
+
deadline: prepared.intent.deadline,
|
|
184
|
+
referralFeePct: prepared.intent.referralFeePct,
|
|
185
|
+
referralFeeRecipient: prepared.intent.referralFeeRecipient,
|
|
186
|
+
...(authorization?.kind === "morphoAuthorization" && {
|
|
187
|
+
authorizationNonce: authorization.args.nonce,
|
|
188
|
+
}),
|
|
189
|
+
},
|
|
190
|
+
signatureRequestIds: {
|
|
191
|
+
...(permit && { tokenPermit: permit.id }),
|
|
192
|
+
...(authorization && { morphoAuthorization: authorization.id }),
|
|
193
|
+
},
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
],
|
|
197
|
+
signatureRequests: prepared.signatureRequests,
|
|
198
|
+
}));
|
|
199
|
+
}
|
|
200
|
+
function captureSupplyBorrowIntent(params) {
|
|
201
|
+
const { chainId, account, marketId, marketData, positionData, expiresAt, referralFee, supportSignature, calldataOrigin, deployless, now = Time.timestamp(), } = params;
|
|
202
|
+
return {
|
|
203
|
+
chainId,
|
|
204
|
+
account,
|
|
205
|
+
marketId,
|
|
206
|
+
marketData,
|
|
207
|
+
positionData,
|
|
208
|
+
expiresAt,
|
|
209
|
+
referralFee,
|
|
210
|
+
supportSignature,
|
|
211
|
+
calldataOrigin,
|
|
212
|
+
deployless,
|
|
213
|
+
now,
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
/** @internal Binds either signature role to the combined operation's stored inputs. */
|
|
217
|
+
export function validateSupplyCollateralBorrowRequest(request, action) {
|
|
218
|
+
const { inputs, signatureRequestIds } = action;
|
|
219
|
+
if (request.kind === "permit" &&
|
|
220
|
+
signatureRequestIds.tokenPermit === request.id) {
|
|
221
|
+
return validateTokenPermitRequest(request, {
|
|
222
|
+
chainId: inputs.chainId,
|
|
223
|
+
account: inputs.account,
|
|
224
|
+
deadline: inputs.deadline,
|
|
225
|
+
bundler: "bundles.blueBundlesV1",
|
|
226
|
+
asset: inputs.marketParams.collateralToken,
|
|
227
|
+
amount: inputs.collateralAssets,
|
|
228
|
+
exact: true,
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
if (request.kind === "morphoAuthorization" &&
|
|
232
|
+
signatureRequestIds.morphoAuthorization === request.id &&
|
|
233
|
+
inputs.authorizationNonce !== undefined) {
|
|
234
|
+
return validateMorphoAuthorizationRequest(request, {
|
|
235
|
+
...inputs,
|
|
236
|
+
nonce: inputs.authorizationNonce,
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
throw new TransactionPlanSignatureMismatchError(request.id, "reference");
|
|
240
|
+
}
|
|
241
|
+
/** @internal Encodes the operation using only plain inputs and attached signatures. */
|
|
242
|
+
export function buildBlueSupplyCollateralBorrow(action, requests) {
|
|
243
|
+
const { chainId, account, marketParams, nativeAssets, authorizationNonce: _nonce, ...args } = action.inputs;
|
|
244
|
+
const requirementSignature = getVaultPermit(requests, {
|
|
245
|
+
id: action.signatureRequestIds.tokenPermit,
|
|
246
|
+
asset: marketParams.collateralToken,
|
|
247
|
+
owner: account,
|
|
248
|
+
});
|
|
249
|
+
const request = requests.find((entry) => entry.id === action.signatureRequestIds.morphoAuthorization);
|
|
250
|
+
if (action.signatureRequestIds.morphoAuthorization !== undefined &&
|
|
251
|
+
request?.signature === undefined) {
|
|
252
|
+
throw new IncompleteTransactionPlanError(action.signatureRequestIds.morphoAuthorization);
|
|
253
|
+
}
|
|
254
|
+
if (request !== undefined && request.kind !== "morphoAuthorization") {
|
|
255
|
+
throw new TransactionPlanSignatureMismatchError(request.id, "reference");
|
|
256
|
+
}
|
|
257
|
+
const authorizationSignature = request?.kind === "morphoAuthorization" && request.signature !== undefined
|
|
258
|
+
? {
|
|
259
|
+
action: {
|
|
260
|
+
type: "authorization",
|
|
261
|
+
args: {
|
|
262
|
+
authorized: request.args.authorized,
|
|
263
|
+
isAuthorized: true,
|
|
264
|
+
deadline: request.args.deadline,
|
|
265
|
+
},
|
|
266
|
+
},
|
|
267
|
+
args: {
|
|
268
|
+
...request.args,
|
|
269
|
+
isAuthorized: true,
|
|
270
|
+
signature: request.signature,
|
|
271
|
+
},
|
|
272
|
+
}
|
|
273
|
+
: undefined;
|
|
274
|
+
return blueSupplyCollateralBorrow({
|
|
275
|
+
market: { chainId, marketParams },
|
|
276
|
+
args: {
|
|
277
|
+
...args,
|
|
278
|
+
nativeAmount: nativeAssets,
|
|
279
|
+
userAddress: account,
|
|
280
|
+
requirementSignature,
|
|
281
|
+
authorizationSignature,
|
|
282
|
+
},
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
function validateMarket(value, path) {
|
|
286
|
+
const market = getPlanObject(value, path);
|
|
287
|
+
validatePlanFields(market, {
|
|
288
|
+
path,
|
|
289
|
+
fields: {
|
|
290
|
+
id: "hex",
|
|
291
|
+
loanToken: "address",
|
|
292
|
+
collateralToken: "address",
|
|
293
|
+
oracle: "address",
|
|
294
|
+
irm: "address",
|
|
295
|
+
lltv: "uint256 bigint",
|
|
296
|
+
},
|
|
297
|
+
});
|
|
298
|
+
if (typeof market.id !== "string" || market.id.length !== 66) {
|
|
299
|
+
throw new InvalidTransactionPlanError({
|
|
300
|
+
path: `${path}.id`,
|
|
301
|
+
reason: "expected a 32-byte market ID",
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
/** @internal Validates the complete stored input and reallocation shape. */
|
|
306
|
+
export function validateSupplyCollateralBorrowPlanAction(action, path) {
|
|
307
|
+
const inputs = getPlanObject(action.inputs, `${path}.inputs`);
|
|
308
|
+
validatePlanFields(inputs, {
|
|
309
|
+
path: `${path}.inputs`,
|
|
310
|
+
fields: {
|
|
311
|
+
collateralAssets: "uint256 bigint",
|
|
312
|
+
borrowAssets: "uint256 bigint",
|
|
313
|
+
maxLtv: "uint256 bigint",
|
|
314
|
+
deadline: "uint256 bigint",
|
|
315
|
+
reallocations: "array",
|
|
316
|
+
},
|
|
317
|
+
});
|
|
318
|
+
for (const key of ["nativeAssets", "referralFeePct", "authorizationNonce"]) {
|
|
319
|
+
if (inputs[key] !== undefined) {
|
|
320
|
+
validatePlanField(inputs[key], {
|
|
321
|
+
path: `${path}.inputs.${key}`,
|
|
322
|
+
type: "uint256 bigint",
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
if (inputs.referralFeeRecipient !== undefined) {
|
|
327
|
+
validatePlanField(inputs.referralFeeRecipient, {
|
|
328
|
+
path: `${path}.inputs.referralFeeRecipient`,
|
|
329
|
+
type: "address",
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
validateMarket(inputs.marketParams, `${path}.inputs.marketParams`);
|
|
333
|
+
const ids = getPlanObject(action.signatureRequestIds, `${path}.signatureRequestIds`);
|
|
334
|
+
for (const key of Object.keys(ids)) {
|
|
335
|
+
if (key !== "tokenPermit" && key !== "morphoAuthorization") {
|
|
336
|
+
throw new InvalidTransactionPlanError({
|
|
337
|
+
path: `${path}.signatureRequestIds.${key}`,
|
|
338
|
+
reason: "unexpected signature role",
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
validatePlanField(ids[key], {
|
|
342
|
+
path: `${path}.signatureRequestIds.${key}`,
|
|
343
|
+
type: "string",
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
if (ids.tokenPermit !== undefined &&
|
|
347
|
+
ids.tokenPermit === ids.morphoAuthorization) {
|
|
348
|
+
throw new InvalidTransactionPlanError({
|
|
349
|
+
path: `${path}.signatureRequestIds.morphoAuthorization`,
|
|
350
|
+
reason: "signature roles must reference distinct requests",
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
if (ids.morphoAuthorization !== undefined) {
|
|
354
|
+
validatePlanField(inputs.authorizationNonce, {
|
|
355
|
+
path: `${path}.inputs.authorizationNonce`,
|
|
356
|
+
type: "uint256 bigint",
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
for (const [index, value] of inputs.reallocations.entries()) {
|
|
360
|
+
const entryPath = `${path}.inputs.reallocations[${index}]`;
|
|
361
|
+
const entry = getPlanObject(value, entryPath);
|
|
362
|
+
validatePlanFields(entry, {
|
|
363
|
+
path: entryPath,
|
|
364
|
+
fields: {
|
|
365
|
+
vault: "address",
|
|
366
|
+
assets: "uint256 bigint",
|
|
367
|
+
penalty: "uint256 bigint",
|
|
368
|
+
},
|
|
369
|
+
});
|
|
370
|
+
validatePlanFields(getPlanObject(entry.to, `${entryPath}.to`), {
|
|
371
|
+
path: `${entryPath}.to`,
|
|
372
|
+
fields: { adapter: "address" },
|
|
373
|
+
});
|
|
374
|
+
const from = getPlanObject(entry.from, `${entryPath}.from`);
|
|
375
|
+
if (from.type === "market") {
|
|
376
|
+
validatePlanFields(from, {
|
|
377
|
+
path: `${entryPath}.from`,
|
|
378
|
+
fields: { adapter: "address" },
|
|
379
|
+
});
|
|
380
|
+
validateMarket(from.marketParams, `${entryPath}.from.marketParams`);
|
|
381
|
+
}
|
|
382
|
+
else if (from.type !== "idle") {
|
|
383
|
+
throw new InvalidTransactionPlanError({
|
|
384
|
+
path: `${entryPath}.from.type`,
|
|
385
|
+
reason: "expected market or idle",
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { type MarketId } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { type Client } from "viem";
|
|
3
|
+
import type { AssetsOrSharesArgs, VaultV2BlueReallocation } from "../../types/index.js";
|
|
4
|
+
import type { IntentParams } from "../intent.js";
|
|
5
|
+
import type { Action, SignatureRequest, TransactionPlan } from "../types.js";
|
|
6
|
+
import { type BlueMarketSnapshot, type BluePositionSnapshot } from "./prepare.js";
|
|
7
|
+
/**
|
|
8
|
+
* Caller intent for withdrawing supplied Blue loan assets. Penalties and referral fees reduce proceeds.
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { markets } from "@morpho-org/morpho-test";
|
|
12
|
+
* import type { BlueWithdrawPlanParams } from "@morpho-org/morpho-sdk";
|
|
13
|
+
* import { fetchBlueAccrualPosition } from "@morpho-org/morpho-sdk/fetch";
|
|
14
|
+
* import { createPublicClient, type Address, http } from "viem";
|
|
15
|
+
* import { mainnet } from "viem/chains";
|
|
16
|
+
*
|
|
17
|
+
* const client = createPublicClient({ chain: mainnet, transport: http() });
|
|
18
|
+
* declare const account: Address; // Connected wallet holding a Blue supply position.
|
|
19
|
+
* const position = await fetchBlueAccrualPosition(account, markets[mainnet.id].usdc_wbtc.id, client);
|
|
20
|
+
* const market = position.market;
|
|
21
|
+
* const params: BlueWithdrawPlanParams = {
|
|
22
|
+
* chainId: mainnet.id, account, marketId: market.id,
|
|
23
|
+
* marketData: { chainId: mainnet.id, market }, assets: 1_000_000n,
|
|
24
|
+
* positionData: { chainId: mainnet.id, position },
|
|
25
|
+
* };
|
|
26
|
+
* // params carries intent and a caller-fetched snapshot, not signatures or transactions.
|
|
27
|
+
* ```
|
|
28
|
+
* @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
|
|
29
|
+
*/
|
|
30
|
+
export type BlueWithdrawPlanParams = IntentParams & AssetsOrSharesArgs & {
|
|
31
|
+
readonly marketId: MarketId;
|
|
32
|
+
readonly marketData: BlueMarketSnapshot;
|
|
33
|
+
/** Caller-fetched position used to validate the available supply balance. */
|
|
34
|
+
readonly positionData: BluePositionSnapshot;
|
|
35
|
+
readonly reallocations?: readonly VaultV2BlueReallocation[];
|
|
36
|
+
readonly now?: bigint;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Prepares a BlueBundlesV1 withdrawal with only Morpho authorization prerequisites, never token approvals.
|
|
40
|
+
* The caller supplies market and position snapshots; the SDK reads authorization but never signs.
|
|
41
|
+
* Share-based withdrawals have no minimum-proceeds guard.
|
|
42
|
+
* @param client - Viem client on the requested chain.
|
|
43
|
+
* @param params.chainId - Chain containing the market and registered BlueBundlesV1 contract.
|
|
44
|
+
* @param params.account - Non-zero account whose supply position is withdrawn and which sends every plan transaction.
|
|
45
|
+
* @param params.marketId - Market ID matching the caller-fetched snapshot and its market tuple.
|
|
46
|
+
* @param params.marketData.chainId - Chain on which the market snapshot was fetched; must equal `chainId`.
|
|
47
|
+
* @param params.marketData.market - Caller-fetched `Market` snapshot, used to fix the market tuple and quote share-mode penalty coverage; never stored in the plan.
|
|
48
|
+
* @param params.positionData.chainId - Position snapshot's chain; must match `chainId`.
|
|
49
|
+
* @param params.positionData.position - Caller-fetched `AccrualPosition` belonging to `account` and `marketId`; validates the withdrawal against supplied assets or shares.
|
|
50
|
+
* @param params.assets - Exact positive gross loan assets withdrawn in base units; exclusive with `shares`.
|
|
51
|
+
* @param params.shares - Exact positive supply shares burned; exclusive with `assets`, with no full-close sentinel or minimum-proceeds guarantee.
|
|
52
|
+
* @param params.reallocations - Optional ordered Vault V2 BluePublicAllocator inputs; defaults to an empty list. Penalties reduce withdrawal proceeds.
|
|
53
|
+
* @param params.reallocations[].vault - Vault whose liquidity the allocator moves.
|
|
54
|
+
* @param params.reallocations[].from.type - Source discriminator: `market` or `idle`.
|
|
55
|
+
* @param params.reallocations[].from.adapter - Source adapter, required only for a `market` source.
|
|
56
|
+
* @param params.reallocations[].from.marketParams.loanToken - Source market loan token, required only for a `market` source; must match the target loan token.
|
|
57
|
+
* @param params.reallocations[].from.marketParams.collateralToken - Source market collateral token, required only for a `market` source.
|
|
58
|
+
* @param params.reallocations[].from.marketParams.oracle - Source market oracle, required only for a `market` source.
|
|
59
|
+
* @param params.reallocations[].from.marketParams.irm - Source market interest-rate model, required only for a `market` source.
|
|
60
|
+
* @param params.reallocations[].from.marketParams.lltv - Source market WAD-scaled liquidation LTV, required only for a `market` source.
|
|
61
|
+
* @param params.reallocations[].to.adapter - Target adapter supplying the enclosing withdrawal market.
|
|
62
|
+
* @param params.reallocations[].assets - Positive reallocated assets in loan-token base units, bounded by uint128.
|
|
63
|
+
* @param params.reallocations[].penalty - Vault-configured WAD-scaled penalty, bounded by uint64 and applied with upward rounding per allocation.
|
|
64
|
+
* @param params.referralFee.bps - Optional integer referral fee in basis points of gross proceeds after allocator penalties; defaults to zero and must be below 10,000.
|
|
65
|
+
* @param params.referralFee.recipient - Optional fee recipient; required and non-zero when `referralFee.bps` is positive.
|
|
66
|
+
* @param params.supportSignature - Optional Morpho authorization signature preference; defaults to `false`, selecting a direct authorization transaction when needed. Neither mode requests a token allowance or Permit2 signature.
|
|
67
|
+
* @param params.now - Optional reference timestamp in seconds, normally the latest block timestamp; defaults to the device clock.
|
|
68
|
+
* @param params.expiresAt - Optional Unix expiry in safe-integer seconds, later than `now`; defaults to `now + 2h`.
|
|
69
|
+
* @param params.calldataOrigin - Optional non-empty, even-length hex bytes appended to every built transaction; omitted by default.
|
|
70
|
+
* @param params.deployless - Optional shared intent field; unused because withdrawal reads no token metadata.
|
|
71
|
+
* @returns A deep-frozen, data-only `{ version, chainId, account, expiresAt, steps, signatureRequests, calldataOrigin? }` plan with a withdrawal step. Direct Morpho authorization is a preceding step; signed authorization is consumed inline by the withdrawal. Attach signatures before building.
|
|
72
|
+
* @throws {ChainIdMismatchError} when the client declares or its transport serves another chain.
|
|
73
|
+
* @throws {UnsupportedChainIdError} when `chainId` is absent from the registry.
|
|
74
|
+
* @throws {UnknownAddressError} when Blue or BlueBundlesV1 is not registered on `chainId`.
|
|
75
|
+
* @throws {BlueSnapshotChainMismatchError} when a market or supplied position snapshot's chain differs from `chainId`.
|
|
76
|
+
* @throws {MarketIdMismatchError} when a market or supplied position snapshot's ID differs from `marketId` or does not hash from its market tuple.
|
|
77
|
+
* @throws {AccrualPositionUserMismatchError} when a supplied position belongs to another account.
|
|
78
|
+
* @throws {ZeroAccountError} when `account` is the zero address.
|
|
79
|
+
* @throws {NonIntegerInputError} when `expiresAt` or `referralFee.bps` is not a safe integer.
|
|
80
|
+
* @throws {NonPositiveInputError} when expiry, the selected withdrawal amount, or a reallocation amount is not positive.
|
|
81
|
+
* @throws {ExpiredDeadlineError} when `expiresAt` is at or before `now`.
|
|
82
|
+
* @throws {NegativeInputError} when the referral fee, a withdrawal amount, or a reallocation penalty is negative.
|
|
83
|
+
* @throws {InputExceedsMaxError} when referral basis points exceed 10,000, an encoded amount or penalty exceeds its ABI bound, or aggregate allocator penalties exceed withdrawn assets (the snapshot's downward-rounded asset quote in shares mode).
|
|
84
|
+
* @throws {InvalidCalldataOriginError} when `calldataOrigin` is empty, malformed or has an odd number of hex digits.
|
|
85
|
+
* @throws {InvalidVaultV2BlueReallocationShapeError} when a reallocation entry is not an object or has a legacy Vault V1 shape.
|
|
86
|
+
* @throws {InvalidReallocationAddressError} when a reallocation vault or adapter address is malformed.
|
|
87
|
+
* @throws {InvalidReallocationSourceTypeError} when a reallocation source discriminator or source market tuple is malformed.
|
|
88
|
+
* @throws {InconsistentReallocationPenaltyError} when allocations from one vault specify different penalties.
|
|
89
|
+
* @throws {ReallocationWithdrawalOnTargetMarketError} when a reallocation source is the withdrawal market.
|
|
90
|
+
* @throws {MutuallyExclusiveWithdrawAmountsError} when both withdrawal modes are nonzero.
|
|
91
|
+
* @throws {ReferralFeePctExceededError} when `referralFee.bps` equals 10,000.
|
|
92
|
+
* @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no non-zero recipient.
|
|
93
|
+
* @throws {ReallocationLoanTokenMismatchError} when a source market uses another loan token.
|
|
94
|
+
* @throws {MissingAccrualPositionError} when the required position snapshot is missing.
|
|
95
|
+
* @throws {WithdrawExceedsSupplyError} when `assets` exceeds the position's supplied assets.
|
|
96
|
+
* @throws {WithdrawSharesExceedSupplyError} when `shares` exceeds the position's supply shares.
|
|
97
|
+
* @throws {MissingContractCodeError} when registered BlueBundlesV1 or Blue has no deployed code.
|
|
98
|
+
* @throws {viem.BaseError} when a chain, bytecode, authorization or nonce RPC read fails, or ABI encoding rejects malformed input.
|
|
99
|
+
* @throws {UnexpectedRequirementSignatureError} when an authorization requirement returns a nonce that is not a bigint.
|
|
100
|
+
* @throws {TransactionPlanSignatureMismatchError} when the authorization requirement does not match the prepared account, chain, nonce or deadline.
|
|
101
|
+
* @example
|
|
102
|
+
* ```ts
|
|
103
|
+
* import { markets } from "@morpho-org/morpho-test";
|
|
104
|
+
* import { prepareBlueWithdraw, type TransactionPlan } from "@morpho-org/morpho-sdk";
|
|
105
|
+
* import { fetchBlueAccrualPosition } from "@morpho-org/morpho-sdk/fetch";
|
|
106
|
+
* import { createPublicClient, type Address, http } from "viem";
|
|
107
|
+
* import { mainnet } from "viem/chains";
|
|
108
|
+
*
|
|
109
|
+
* const client = createPublicClient({ chain: mainnet, transport: http() });
|
|
110
|
+
* declare const account: Address; // Connected wallet holding a Blue supply position.
|
|
111
|
+
* const position = await fetchBlueAccrualPosition(account, markets[mainnet.id].usdc_wbtc.id, client);
|
|
112
|
+
* const market = position.market;
|
|
113
|
+
* const plan: TransactionPlan = await prepareBlueWithdraw(client, {
|
|
114
|
+
* chainId: mainnet.id, account, marketId: market.id,
|
|
115
|
+
* marketData: { chainId: mainnet.id, market }, assets: 1_000_000n,
|
|
116
|
+
* positionData: { chainId: mainnet.id, position },
|
|
117
|
+
* now: (await client.getBlock()).timestamp,
|
|
118
|
+
* });
|
|
119
|
+
* // plan: { version: 1, chainId, account, expiresAt, steps, signatureRequests }
|
|
120
|
+
* // Sign any pending Morpho authorization request, then build and submit each step in order.
|
|
121
|
+
* ```
|
|
122
|
+
* @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
|
|
123
|
+
*/
|
|
124
|
+
export declare function prepareBlueWithdraw(client: Client, params: BlueWithdrawPlanParams): Promise<TransactionPlan>;
|
|
125
|
+
/** @internal Binds an inline authorization to the withdrawal that consumes it. */
|
|
126
|
+
export declare function validateBlueWithdrawAuthorizationRequest(request: Extract<SignatureRequest, {
|
|
127
|
+
kind: "morphoAuthorization";
|
|
128
|
+
}>, action: Extract<Action, {
|
|
129
|
+
type: "blueWithdraw";
|
|
130
|
+
}>): void;
|
|
131
|
+
/** @internal Encodes the self-contained withdrawal recipe and its inline authorization, if requested. */
|
|
132
|
+
export declare function buildBlueWithdraw(action: Extract<Action, {
|
|
133
|
+
type: "blueWithdraw";
|
|
134
|
+
}>, requests?: readonly SignatureRequest[]): Readonly<import("../../types/action.js").Transaction<import("../../types/action.js").BlueWithdrawAction>>;
|
|
135
|
+
/** @internal Validates stored withdrawal scalars and allocator input shapes. */
|
|
136
|
+
export declare function validateBlueWithdrawPlanAction(action: Record<string, unknown>, path: string): void;
|