@morpho-org/morpho-sdk 6.6.0 → 7.1.0-next.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/MIGRATION-v6-to-v7.md +297 -0
- package/README.md +26 -12
- package/lib/cjs/abis.d.ts +2 -2
- package/lib/cjs/abis.js +7 -2
- package/lib/cjs/actions/bundles/common.d.ts +9 -5
- package/lib/cjs/actions/bundles/common.js +22 -13
- package/lib/cjs/actions/midnight/authorization.d.ts +3 -2
- package/lib/cjs/actions/midnight/authorization.js +3 -2
- package/lib/cjs/actions/midnight/cancelAndMake.d.ts +153 -0
- package/lib/cjs/actions/midnight/cancelAndMake.js +292 -0
- package/lib/cjs/actions/midnight/index.d.ts +3 -3
- package/lib/cjs/actions/midnight/index.js +3 -3
- package/lib/cjs/actions/midnight/repayWithdrawCollateral.d.ts +40 -23
- package/lib/cjs/actions/midnight/repayWithdrawCollateral.js +105 -69
- package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.d.ts +52 -27
- package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +128 -71
- package/lib/cjs/actions/midnight/takeBorrow.d.ts +40 -28
- package/lib/cjs/actions/midnight/takeBorrow.js +95 -54
- package/lib/cjs/actions/midnight/takeLend.d.ts +51 -27
- package/lib/cjs/actions/midnight/takeLend.js +111 -54
- package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.d.ts +97 -0
- package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.js +201 -0
- package/lib/cjs/actions/midnight/takeWithdraw.d.ts +69 -0
- package/lib/cjs/actions/midnight/takeWithdraw.js +153 -0
- package/lib/cjs/actions/midnight/types.d.ts +7 -26
- package/lib/cjs/actions/midnight/types.js +0 -13
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +2 -1
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +3 -2
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +4 -4
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +5 -5
- package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +2 -2
- package/lib/cjs/actions/requirements/getRequirementsApproval.js +2 -2
- package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.d.ts +24 -8
- package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.js +24 -13
- package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.d.ts +11 -7
- package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +11 -7
- package/lib/cjs/actions/requirements/midnight/index.d.ts +0 -1
- package/lib/cjs/actions/requirements/midnight/index.js +0 -1
- 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/constants.d.ts +1 -1
- package/lib/cjs/constants.js +2 -1
- package/lib/cjs/entities/index.d.ts +6 -6
- package/lib/cjs/entities/index.js +3 -1
- package/lib/cjs/entities/midnight/midnight.d.ts +585 -108
- package/lib/cjs/entities/midnight/midnight.js +984 -509
- package/lib/cjs/entities/midnight/types.d.ts +142 -60
- package/lib/cjs/entities/vaultV1/vaultV1.d.ts +3 -2
- package/lib/cjs/entities/vaultV1/vaultV1.js +32 -34
- package/lib/cjs/entities/vaultV2/vaultV2.d.ts +37 -12
- package/lib/cjs/entities/vaultV2/vaultV2.js +68 -41
- package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +5 -3
- package/lib/cjs/entities/vaultV2BlueReallocationData.js +9 -3
- package/lib/cjs/errors.d.ts +4 -4
- package/lib/cjs/errors.js +10 -5
- package/lib/cjs/fetch.d.ts +2 -2
- package/lib/cjs/fetch.js +3 -1
- package/lib/cjs/helpers/callback.d.ts +54 -0
- package/lib/cjs/helpers/callback.js +73 -0
- package/lib/cjs/helpers/constant.d.ts +2 -0
- package/lib/cjs/helpers/constant.js +3 -1
- package/lib/cjs/helpers/index.d.ts +3 -2
- package/lib/cjs/helpers/index.js +6 -1
- package/lib/cjs/helpers/signAndVerifyTypedData.d.ts +3 -3
- package/lib/cjs/helpers/signAndVerifyTypedData.js +3 -3
- package/lib/cjs/helpers/validate.d.ts +43 -2
- package/lib/cjs/helpers/validate.js +60 -3
- package/lib/cjs/helpers/validateMidnightAuthorizationTarget.d.ts +1 -1
- package/lib/cjs/helpers/validateMidnightAuthorizationTarget.js +3 -7
- package/lib/cjs/helpers/validateRequirementSpender.d.ts +3 -3
- package/lib/cjs/helpers/validateRequirementSpender.js +4 -5
- package/lib/cjs/midnight/abis.d.ts +1 -1
- package/lib/cjs/midnight/abis.js +3 -2
- package/lib/cjs/midnight/errors.d.ts +2 -1
- package/lib/cjs/midnight/errors.js +5 -4
- package/lib/cjs/midnight/types.d.ts +1 -1
- package/lib/cjs/types/action.d.ts +219 -90
- package/lib/cjs/types/action.js +103 -20
- package/lib/cjs/types/error.d.ts +126 -40
- package/lib/cjs/types/error.js +154 -49
- package/lib/cjs/utils.d.ts +2 -2
- package/lib/cjs/utils.js +4 -2
- package/lib/esm/abis.d.ts +2 -2
- package/lib/esm/abis.js +2 -2
- package/lib/esm/actions/bundles/common.d.ts +9 -5
- package/lib/esm/actions/bundles/common.js +22 -13
- package/lib/esm/actions/midnight/authorization.d.ts +3 -2
- package/lib/esm/actions/midnight/authorization.js +3 -2
- package/lib/esm/actions/midnight/cancelAndMake.d.ts +153 -0
- package/lib/esm/actions/midnight/cancelAndMake.js +288 -0
- package/lib/esm/actions/midnight/index.d.ts +3 -3
- package/lib/esm/actions/midnight/index.js +3 -3
- package/lib/esm/actions/midnight/repayWithdrawCollateral.d.ts +40 -23
- package/lib/esm/actions/midnight/repayWithdrawCollateral.js +108 -72
- package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.d.ts +52 -27
- package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +131 -74
- package/lib/esm/actions/midnight/takeBorrow.d.ts +40 -28
- package/lib/esm/actions/midnight/takeBorrow.js +98 -57
- package/lib/esm/actions/midnight/takeLend.d.ts +51 -27
- package/lib/esm/actions/midnight/takeLend.js +114 -57
- package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.d.ts +97 -0
- package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.js +197 -0
- package/lib/esm/actions/midnight/takeWithdraw.d.ts +69 -0
- package/lib/esm/actions/midnight/takeWithdraw.js +149 -0
- package/lib/esm/actions/midnight/types.d.ts +7 -26
- package/lib/esm/actions/midnight/types.js +1 -12
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +2 -1
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +3 -2
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +4 -4
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +5 -5
- package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +2 -2
- package/lib/esm/actions/requirements/getRequirementsApproval.js +2 -2
- package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.d.ts +24 -8
- package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.js +25 -14
- package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.d.ts +11 -7
- package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +11 -7
- package/lib/esm/actions/requirements/midnight/index.d.ts +0 -1
- package/lib/esm/actions/requirements/midnight/index.js +0 -1
- 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/constants.d.ts +1 -1
- package/lib/esm/constants.js +1 -1
- package/lib/esm/entities/index.d.ts +6 -6
- package/lib/esm/entities/index.js +2 -2
- package/lib/esm/entities/midnight/midnight.d.ts +585 -108
- package/lib/esm/entities/midnight/midnight.js +990 -515
- package/lib/esm/entities/midnight/types.d.ts +142 -60
- package/lib/esm/entities/vaultV1/vaultV1.d.ts +3 -2
- package/lib/esm/entities/vaultV1/vaultV1.js +33 -35
- package/lib/esm/entities/vaultV2/vaultV2.d.ts +37 -12
- package/lib/esm/entities/vaultV2/vaultV2.js +69 -42
- package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +5 -3
- package/lib/esm/entities/vaultV2BlueReallocationData.js +9 -3
- 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/callback.d.ts +54 -0
- package/lib/esm/helpers/callback.js +68 -0
- package/lib/esm/helpers/constant.d.ts +2 -0
- package/lib/esm/helpers/constant.js +2 -0
- package/lib/esm/helpers/index.d.ts +3 -2
- package/lib/esm/helpers/index.js +3 -2
- package/lib/esm/helpers/signAndVerifyTypedData.d.ts +3 -3
- package/lib/esm/helpers/signAndVerifyTypedData.js +3 -3
- package/lib/esm/helpers/validate.d.ts +43 -2
- package/lib/esm/helpers/validate.js +58 -2
- package/lib/esm/helpers/validateMidnightAuthorizationTarget.d.ts +1 -1
- package/lib/esm/helpers/validateMidnightAuthorizationTarget.js +3 -7
- package/lib/esm/helpers/validateRequirementSpender.d.ts +3 -3
- package/lib/esm/helpers/validateRequirementSpender.js +4 -5
- package/lib/esm/midnight/abis.d.ts +1 -1
- package/lib/esm/midnight/abis.js +1 -1
- package/lib/esm/midnight/errors.d.ts +2 -1
- package/lib/esm/midnight/errors.js +2 -1
- package/lib/esm/midnight/types.d.ts +1 -1
- package/lib/esm/types/action.d.ts +219 -90
- package/lib/esm/types/action.js +103 -19
- package/lib/esm/types/error.d.ts +126 -40
- package/lib/esm/types/error.js +135 -38
- package/lib/esm/utils.d.ts +2 -2
- package/lib/esm/utils.js +2 -2
- package/package.json +10 -10
- package/lib/cjs/actions/midnight/cancelOffer.d.ts +0 -38
- package/lib/cjs/actions/midnight/cancelOffer.js +0 -73
- package/lib/cjs/actions/midnight/mempoolSubmitOffers.d.ts +0 -52
- package/lib/cjs/actions/midnight/mempoolSubmitOffers.js +0 -72
- package/lib/cjs/actions/midnight/setterRatifierRatifyRoot.d.ts +0 -40
- package/lib/cjs/actions/midnight/setterRatifierRatifyRoot.js +0 -59
- package/lib/cjs/actions/requirements/midnight/getSetterRatifierRatifyRootRequirement.d.ts +0 -42
- package/lib/cjs/actions/requirements/midnight/getSetterRatifierRatifyRootRequirement.js +0 -70
- package/lib/esm/actions/midnight/cancelOffer.d.ts +0 -38
- package/lib/esm/actions/midnight/cancelOffer.js +0 -69
- package/lib/esm/actions/midnight/mempoolSubmitOffers.d.ts +0 -52
- package/lib/esm/actions/midnight/mempoolSubmitOffers.js +0 -68
- package/lib/esm/actions/midnight/setterRatifierRatifyRoot.d.ts +0 -40
- package/lib/esm/actions/midnight/setterRatifierRatifyRoot.js +0 -55
- package/lib/esm/actions/requirements/midnight/getSetterRatifierRatifyRootRequirement.d.ts +0 -42
- package/lib/esm/actions/requirements/midnight/getSetterRatifierRatifyRootRequirement.js +0 -66
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { type MarketInput } from "@morpho-org/midnight-sdk";
|
|
2
|
+
import { type Address, type Hex } from "viem";
|
|
3
|
+
import { type Metadata, type MidnightBlueSupply, type MidnightCancelAndMakeAction, type MidnightCollateralTransfer, type MidnightGroupCancellation, type Transaction } from "../../types/index.js";
|
|
4
|
+
/**
|
|
5
|
+
* Delegated root-activation signature passed to the ratifier's `setIsRootRatifiedWithSig`.
|
|
6
|
+
* Omit it to activate the root directly with `setIsRootRatified`.
|
|
7
|
+
*/
|
|
8
|
+
export interface MidnightRootActivationSignature {
|
|
9
|
+
/** Tree height committed by the signature. */
|
|
10
|
+
readonly height: bigint;
|
|
11
|
+
/** Ratifier signature nonce. */
|
|
12
|
+
readonly nonce: bigint;
|
|
13
|
+
/** Signature deadline timestamp, independent of the bundle deadline. */
|
|
14
|
+
readonly deadline: bigint;
|
|
15
|
+
readonly v: number;
|
|
16
|
+
readonly r: Hex;
|
|
17
|
+
readonly s: Hex;
|
|
18
|
+
}
|
|
19
|
+
/** Collateral supplied for the maker before root activation; every offer must target `market`. */
|
|
20
|
+
export interface MidnightMakeBorrowCollateral {
|
|
21
|
+
readonly market: MarketInput;
|
|
22
|
+
readonly supplies: readonly MidnightCollateralTransfer[];
|
|
23
|
+
}
|
|
24
|
+
/** Offer root activated and published by a Midnight Bundles V2 maker bundle. */
|
|
25
|
+
export interface MidnightOfferPublication {
|
|
26
|
+
/**
|
|
27
|
+
* The chain's registered `priceRatifierV1` or `rateRatifierV1`, which activates `root`.
|
|
28
|
+
* MidnightBundlesV2 authorizes it over the maker's whole Midnight account.
|
|
29
|
+
*/
|
|
30
|
+
readonly ratifier: Address;
|
|
31
|
+
/** Offer tree root to activate. */
|
|
32
|
+
readonly root: Hex;
|
|
33
|
+
/** Distinct offer groups contained in `root`. */
|
|
34
|
+
readonly groups: readonly Hex[];
|
|
35
|
+
/** Encoded offer payload for `root`, published to the Midnight log. */
|
|
36
|
+
readonly payload: Hex;
|
|
37
|
+
/** Optional delegated root-activation signature. */
|
|
38
|
+
readonly rootSignature?: MidnightRootActivationSignature;
|
|
39
|
+
/** Optional loan assets parked on Morpho Blue for the maker; share-price slippage is not checked. */
|
|
40
|
+
readonly blueSupply?: MidnightBlueSupply;
|
|
41
|
+
/** Optional collateral supplied to `market` for the maker before activation. */
|
|
42
|
+
readonly collateral?: MidnightMakeBorrowCollateral;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Parameters for encoding a Midnight Bundles V2 maker bundle: an offer publication or repost, or,
|
|
46
|
+
* without a publication, a cancellation of offer groups only.
|
|
47
|
+
*/
|
|
48
|
+
export interface MidnightCancelAndMakeParams {
|
|
49
|
+
/** Chain id used to resolve `MidnightBundlesV2`. */
|
|
50
|
+
readonly chainId: number;
|
|
51
|
+
/** Offer groups to cancel, each with its largest accepted consumption. Empty for a new publication. */
|
|
52
|
+
readonly cancellations: readonly MidnightGroupCancellation[];
|
|
53
|
+
/** Bundle execution deadline timestamp. Pass `maxUint256` explicitly for no expiry. */
|
|
54
|
+
readonly deadline: bigint;
|
|
55
|
+
/**
|
|
56
|
+
* Optional native ETH sent as `value`; the bundle wraps it into the chain's wNative and transfers
|
|
57
|
+
* it to `msg.sender` before pulling. It may cover part or all of the maximum wNative amount this
|
|
58
|
+
* call can pull, but not more. Any wrapped native not pulled stays in the sender's wallet as
|
|
59
|
+
* wNative. The wNative approval to `MidnightBundlesV2` is still required for the full pulled amount.
|
|
60
|
+
*/
|
|
61
|
+
readonly nativeAmount?: bigint;
|
|
62
|
+
/** Optional analytics metadata appended to calldata. */
|
|
63
|
+
readonly metadata?: Metadata;
|
|
64
|
+
/** Offers to publish after the cancellations. Omit to cancel offer groups only. */
|
|
65
|
+
readonly publication?: MidnightOfferPublication;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Encodes `MidnightBundlesV2.midnightBundlesV2CancelAndMake` for `msg.sender`: cancel previous
|
|
69
|
+
* groups under consumption guards, optionally park loan assets on Morpho Blue for the maker's
|
|
70
|
+
* `BlueBuyCallback` or supply collateral, activate `root`, and publish `payload`.
|
|
71
|
+
* Without `publication`, it only cancels groups (`cancelOffers` uses this).
|
|
72
|
+
* Execution reverts as a whole if any group's consumption exceeds its `maxConsumed` ceiling.
|
|
73
|
+
*
|
|
74
|
+
* The contract does not check that `payload` matches `root`; callers must derive both from the
|
|
75
|
+
* same tree. Prefer `client.morpho.midnight(chainId).makeLend(...)`,
|
|
76
|
+
* `makeBorrow(...)`, `supplyCollateralMakeBorrow(...)`, or `supplyBlueMakeLend(...)`, which derive
|
|
77
|
+
* the tree data and resolve the requirements for each flow; `supplyBlueMakeLend` also runs the
|
|
78
|
+
* Blue parking checks (callback, callbackData, empty-market and zero-share guards) that this builder
|
|
79
|
+
* skips.
|
|
80
|
+
*
|
|
81
|
+
* @param params.chainId - Chain id used to resolve `MidnightBundlesV2`.
|
|
82
|
+
* @param params.cancellations - Offer groups to cancel and their consumption ceilings.
|
|
83
|
+
* @param params.deadline - Bundle execution deadline timestamp; pass `maxUint256` explicitly for no expiry.
|
|
84
|
+
* @param params.nativeAmount - Optional native ETH sent as `value`; the bundle wraps it into the
|
|
85
|
+
* chain's wNative and transfers it to `msg.sender` before pulling. It may cover part or all of the
|
|
86
|
+
* maximum wNative amount this call can pull, but not more. Any wrapped native not pulled stays in
|
|
87
|
+
* the sender's wallet as wNative. The wNative approval to `MidnightBundlesV2` is still required
|
|
88
|
+
* for the full pulled amount.
|
|
89
|
+
* @param params.metadata - Optional analytics metadata appended to calldata.
|
|
90
|
+
* @param params.publication - Offers to publish after the cancellations; omit for cancellation only.
|
|
91
|
+
* @param params.publication.ratifier - The chain's registered `priceRatifierV1` or `rateRatifierV1`,
|
|
92
|
+
* which activates the offer root.
|
|
93
|
+
* @param params.publication.root - Offer tree root to activate.
|
|
94
|
+
* @param params.publication.groups - Distinct offer groups contained in the root.
|
|
95
|
+
* @param params.publication.payload - Encoded offer payload published to the Midnight log.
|
|
96
|
+
* @param params.publication.rootSignature - Optional delegated root-activation signature; omitted, all fields are zero.
|
|
97
|
+
* @param params.publication.rootSignature.height - Tree height committed by the signature.
|
|
98
|
+
* @param params.publication.rootSignature.nonce - Ratifier signature nonce.
|
|
99
|
+
* @param params.publication.rootSignature.deadline - Signature deadline timestamp, independent of the bundle `deadline`.
|
|
100
|
+
* @param params.publication.rootSignature.v - Signature recovery id.
|
|
101
|
+
* @param params.publication.rootSignature.r - Signature `r` component.
|
|
102
|
+
* @param params.publication.rootSignature.s - Signature `s` component.
|
|
103
|
+
* @param params.publication.blueSupply - Optional loan assets to supply on Blue for the maker's `BlueBuyCallback`.
|
|
104
|
+
* @param params.publication.blueSupply.market - Blue market receiving the loan assets.
|
|
105
|
+
* @param params.publication.blueSupply.assets - Loan assets pulled from `msg.sender` and supplied on Blue; must be positive.
|
|
106
|
+
* @param params.publication.blueSupply.callbackSalt - Salt selecting the maker's `BlueBuyCallback`.
|
|
107
|
+
* @param params.publication.collateral - Optional collateral to supply on `collateral.market` for `msg.sender`.
|
|
108
|
+
* @param params.publication.collateral.market - Midnight market receiving the collateral; every offer must target it.
|
|
109
|
+
* @param params.publication.collateral.supplies - Collateral index and assets per supply; must not be empty.
|
|
110
|
+
* @returns Deep-frozen transaction targeting `MidnightBundlesV2`.
|
|
111
|
+
* @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
|
|
112
|
+
* @throws {UnknownAddressError} when the chain has no `midnightBundlesV2` deployment.
|
|
113
|
+
* @throws {UnsupportedMidnightAuthorizationTargetError} when the ratifier is not the chain's registered `priceRatifierV1` or `rateRatifierV1`.
|
|
114
|
+
* @throws {EmptyMidnightGroupCancellationsError} when nothing is published and no groups are cancelled.
|
|
115
|
+
* @throws {EmptyMidnightCollateralSuppliesError} when a collateral market has no collateral supplies.
|
|
116
|
+
* @throws {InvalidTreeError} when `root` is zero, or `payload` or `groups` is empty.
|
|
117
|
+
* @throws {MidnightReplacementGroupCancelledError} when a published group is also cancelled.
|
|
118
|
+
* @throws {DuplicateMidnightGroupCancellationError} when a cancelled group appears more than once.
|
|
119
|
+
* @throws {NonPositiveInputError} when `deadline` is not positive.
|
|
120
|
+
* @throws {NegativeInputError} when a `maxConsumed` ceiling or `nativeAmount` is negative.
|
|
121
|
+
* @throws {InputExceedsMaxError} when a `maxConsumed` ceiling exceeds `uint128`, or `deadline` or a collateral supply amount exceeds `uint256`, or `nativeAmount` exceeds the maximum wNative pull, including zero when no wNative is pulled.
|
|
122
|
+
* @throws {ChainWNativeMissingError} when wNative is not configured for the chain.
|
|
123
|
+
* @throws {NativeAmountOnNonWNativeAssetError} when configured token pulls contain no wNative token.
|
|
124
|
+
* @throws {ChainIdMismatchError} when the collateral market targets another chain.
|
|
125
|
+
* @throws {MidnightMarketAddressMismatchError} when the collateral market targets another Midnight deployment.
|
|
126
|
+
* @throws {UnknownCollateralIndexError} when a collateral index is not configured on the market.
|
|
127
|
+
* @throws {NonPositiveInputError} when a collateral supply amount is non-positive.
|
|
128
|
+
* @throws {NonPositiveInputError} when `blueSupply.assets` is non-positive.
|
|
129
|
+
* @example
|
|
130
|
+
* ```ts
|
|
131
|
+
* import { getChainAddress } from "@morpho-org/morpho-ts";
|
|
132
|
+
* import { maxUint256, type Hex } from "viem";
|
|
133
|
+
* import { midnightCancelAndMake } from "@morpho-org/morpho-sdk";
|
|
134
|
+
*
|
|
135
|
+
* const rateRatifierV1 = getChainAddress(8453, "rateRatifierV1");
|
|
136
|
+
* declare const root: Hex;
|
|
137
|
+
* declare const group: Hex;
|
|
138
|
+
* declare const payload: Hex;
|
|
139
|
+
* declare const previousGroup: Hex;
|
|
140
|
+
* const tx = midnightCancelAndMake({
|
|
141
|
+
* chainId: 8453,
|
|
142
|
+
* cancellations: [{ group: previousGroup, maxConsumed: 0n }],
|
|
143
|
+
* deadline: maxUint256,
|
|
144
|
+
* publication: {
|
|
145
|
+
* ratifier: rateRatifierV1,
|
|
146
|
+
* root,
|
|
147
|
+
* groups: [group],
|
|
148
|
+
* payload,
|
|
149
|
+
* },
|
|
150
|
+
* });
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
export declare const midnightCancelAndMake: (params: MidnightCancelAndMakeParams) => Readonly<Transaction<MidnightCancelAndMakeAction>>;
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.midnightCancelAndMake = void 0;
|
|
4
|
+
const midnight_sdk_1 = require("@morpho-org/midnight-sdk");
|
|
5
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
6
|
+
const viem_1 = require("viem");
|
|
7
|
+
const index_js_1 = require("../../helpers/index.js");
|
|
8
|
+
const validate_js_1 = require("../../helpers/validate.js");
|
|
9
|
+
const validateMidnightMarket_js_1 = require("../../helpers/validateMidnightMarket.js");
|
|
10
|
+
const index_js_2 = require("../../types/index.js");
|
|
11
|
+
/** Blue market argument for `MidnightBundlesV2` calls that park no loan assets. */
|
|
12
|
+
const emptyBlueMarket = {
|
|
13
|
+
loanToken: viem_1.zeroAddress,
|
|
14
|
+
collateralToken: viem_1.zeroAddress,
|
|
15
|
+
oracle: viem_1.zeroAddress,
|
|
16
|
+
irm: viem_1.zeroAddress,
|
|
17
|
+
lltv: 0n,
|
|
18
|
+
};
|
|
19
|
+
/** Midnight market argument for `MidnightBundlesV2` calls that supply no collateral. */
|
|
20
|
+
const emptyMidnightMarket = {
|
|
21
|
+
chainId: 0n,
|
|
22
|
+
midnight: viem_1.zeroAddress,
|
|
23
|
+
loanToken: viem_1.zeroAddress,
|
|
24
|
+
collateralParams: [],
|
|
25
|
+
maturity: 0n,
|
|
26
|
+
rcfThreshold: 0n,
|
|
27
|
+
enterGate: viem_1.zeroAddress,
|
|
28
|
+
liquidatorGate: viem_1.zeroAddress,
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Encodes `MidnightBundlesV2.midnightBundlesV2CancelAndMake` for `msg.sender`: cancel previous
|
|
32
|
+
* groups under consumption guards, optionally park loan assets on Morpho Blue for the maker's
|
|
33
|
+
* `BlueBuyCallback` or supply collateral, activate `root`, and publish `payload`.
|
|
34
|
+
* Without `publication`, it only cancels groups (`cancelOffers` uses this).
|
|
35
|
+
* Execution reverts as a whole if any group's consumption exceeds its `maxConsumed` ceiling.
|
|
36
|
+
*
|
|
37
|
+
* The contract does not check that `payload` matches `root`; callers must derive both from the
|
|
38
|
+
* same tree. Prefer `client.morpho.midnight(chainId).makeLend(...)`,
|
|
39
|
+
* `makeBorrow(...)`, `supplyCollateralMakeBorrow(...)`, or `supplyBlueMakeLend(...)`, which derive
|
|
40
|
+
* the tree data and resolve the requirements for each flow; `supplyBlueMakeLend` also runs the
|
|
41
|
+
* Blue parking checks (callback, callbackData, empty-market and zero-share guards) that this builder
|
|
42
|
+
* skips.
|
|
43
|
+
*
|
|
44
|
+
* @param params.chainId - Chain id used to resolve `MidnightBundlesV2`.
|
|
45
|
+
* @param params.cancellations - Offer groups to cancel and their consumption ceilings.
|
|
46
|
+
* @param params.deadline - Bundle execution deadline timestamp; pass `maxUint256` explicitly for no expiry.
|
|
47
|
+
* @param params.nativeAmount - Optional native ETH sent as `value`; the bundle wraps it into the
|
|
48
|
+
* chain's wNative and transfers it to `msg.sender` before pulling. It may cover part or all of the
|
|
49
|
+
* maximum wNative amount this call can pull, but not more. Any wrapped native not pulled stays in
|
|
50
|
+
* the sender's wallet as wNative. The wNative approval to `MidnightBundlesV2` is still required
|
|
51
|
+
* for the full pulled amount.
|
|
52
|
+
* @param params.metadata - Optional analytics metadata appended to calldata.
|
|
53
|
+
* @param params.publication - Offers to publish after the cancellations; omit for cancellation only.
|
|
54
|
+
* @param params.publication.ratifier - The chain's registered `priceRatifierV1` or `rateRatifierV1`,
|
|
55
|
+
* which activates the offer root.
|
|
56
|
+
* @param params.publication.root - Offer tree root to activate.
|
|
57
|
+
* @param params.publication.groups - Distinct offer groups contained in the root.
|
|
58
|
+
* @param params.publication.payload - Encoded offer payload published to the Midnight log.
|
|
59
|
+
* @param params.publication.rootSignature - Optional delegated root-activation signature; omitted, all fields are zero.
|
|
60
|
+
* @param params.publication.rootSignature.height - Tree height committed by the signature.
|
|
61
|
+
* @param params.publication.rootSignature.nonce - Ratifier signature nonce.
|
|
62
|
+
* @param params.publication.rootSignature.deadline - Signature deadline timestamp, independent of the bundle `deadline`.
|
|
63
|
+
* @param params.publication.rootSignature.v - Signature recovery id.
|
|
64
|
+
* @param params.publication.rootSignature.r - Signature `r` component.
|
|
65
|
+
* @param params.publication.rootSignature.s - Signature `s` component.
|
|
66
|
+
* @param params.publication.blueSupply - Optional loan assets to supply on Blue for the maker's `BlueBuyCallback`.
|
|
67
|
+
* @param params.publication.blueSupply.market - Blue market receiving the loan assets.
|
|
68
|
+
* @param params.publication.blueSupply.assets - Loan assets pulled from `msg.sender` and supplied on Blue; must be positive.
|
|
69
|
+
* @param params.publication.blueSupply.callbackSalt - Salt selecting the maker's `BlueBuyCallback`.
|
|
70
|
+
* @param params.publication.collateral - Optional collateral to supply on `collateral.market` for `msg.sender`.
|
|
71
|
+
* @param params.publication.collateral.market - Midnight market receiving the collateral; every offer must target it.
|
|
72
|
+
* @param params.publication.collateral.supplies - Collateral index and assets per supply; must not be empty.
|
|
73
|
+
* @returns Deep-frozen transaction targeting `MidnightBundlesV2`.
|
|
74
|
+
* @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
|
|
75
|
+
* @throws {UnknownAddressError} when the chain has no `midnightBundlesV2` deployment.
|
|
76
|
+
* @throws {UnsupportedMidnightAuthorizationTargetError} when the ratifier is not the chain's registered `priceRatifierV1` or `rateRatifierV1`.
|
|
77
|
+
* @throws {EmptyMidnightGroupCancellationsError} when nothing is published and no groups are cancelled.
|
|
78
|
+
* @throws {EmptyMidnightCollateralSuppliesError} when a collateral market has no collateral supplies.
|
|
79
|
+
* @throws {InvalidTreeError} when `root` is zero, or `payload` or `groups` is empty.
|
|
80
|
+
* @throws {MidnightReplacementGroupCancelledError} when a published group is also cancelled.
|
|
81
|
+
* @throws {DuplicateMidnightGroupCancellationError} when a cancelled group appears more than once.
|
|
82
|
+
* @throws {NonPositiveInputError} when `deadline` is not positive.
|
|
83
|
+
* @throws {NegativeInputError} when a `maxConsumed` ceiling or `nativeAmount` is negative.
|
|
84
|
+
* @throws {InputExceedsMaxError} when a `maxConsumed` ceiling exceeds `uint128`, or `deadline` or a collateral supply amount exceeds `uint256`, or `nativeAmount` exceeds the maximum wNative pull, including zero when no wNative is pulled.
|
|
85
|
+
* @throws {ChainWNativeMissingError} when wNative is not configured for the chain.
|
|
86
|
+
* @throws {NativeAmountOnNonWNativeAssetError} when configured token pulls contain no wNative token.
|
|
87
|
+
* @throws {ChainIdMismatchError} when the collateral market targets another chain.
|
|
88
|
+
* @throws {MidnightMarketAddressMismatchError} when the collateral market targets another Midnight deployment.
|
|
89
|
+
* @throws {UnknownCollateralIndexError} when a collateral index is not configured on the market.
|
|
90
|
+
* @throws {NonPositiveInputError} when a collateral supply amount is non-positive.
|
|
91
|
+
* @throws {NonPositiveInputError} when `blueSupply.assets` is non-positive.
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts
|
|
94
|
+
* import { getChainAddress } from "@morpho-org/morpho-ts";
|
|
95
|
+
* import { maxUint256, type Hex } from "viem";
|
|
96
|
+
* import { midnightCancelAndMake } from "@morpho-org/morpho-sdk";
|
|
97
|
+
*
|
|
98
|
+
* const rateRatifierV1 = getChainAddress(8453, "rateRatifierV1");
|
|
99
|
+
* declare const root: Hex;
|
|
100
|
+
* declare const group: Hex;
|
|
101
|
+
* declare const payload: Hex;
|
|
102
|
+
* declare const previousGroup: Hex;
|
|
103
|
+
* const tx = midnightCancelAndMake({
|
|
104
|
+
* chainId: 8453,
|
|
105
|
+
* cancellations: [{ group: previousGroup, maxConsumed: 0n }],
|
|
106
|
+
* deadline: maxUint256,
|
|
107
|
+
* publication: {
|
|
108
|
+
* ratifier: rateRatifierV1,
|
|
109
|
+
* root,
|
|
110
|
+
* groups: [group],
|
|
111
|
+
* payload,
|
|
112
|
+
* },
|
|
113
|
+
* });
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
const midnightCancelAndMake = (params) => {
|
|
117
|
+
(0, validate_js_1.validateDeadline)(params.deadline);
|
|
118
|
+
const cancelledGroups = new Set();
|
|
119
|
+
for (const [index, { group, maxConsumed },] of params.cancellations.entries()) {
|
|
120
|
+
const field = `cancellations[${index}].maxConsumed`;
|
|
121
|
+
if (maxConsumed < 0n)
|
|
122
|
+
throw new index_js_2.NegativeInputError(field, maxConsumed);
|
|
123
|
+
if (maxConsumed > viem_1.maxUint128) {
|
|
124
|
+
throw new index_js_2.InputExceedsMaxError({
|
|
125
|
+
field,
|
|
126
|
+
value: maxConsumed,
|
|
127
|
+
max: viem_1.maxUint128,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
const key = group.toLowerCase();
|
|
131
|
+
if (cancelledGroups.has(key)) {
|
|
132
|
+
throw new index_js_2.DuplicateMidnightGroupCancellationError({ index, group });
|
|
133
|
+
}
|
|
134
|
+
cancelledGroups.add(key);
|
|
135
|
+
}
|
|
136
|
+
const publication = params.publication;
|
|
137
|
+
if (publication == null) {
|
|
138
|
+
if (params.cancellations.length === 0) {
|
|
139
|
+
throw new index_js_2.EmptyMidnightGroupCancellationsError();
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
if (publication.root === viem_1.zeroHash) {
|
|
144
|
+
throw new midnight_sdk_1.InvalidTreeError("Offer root cannot be zero.");
|
|
145
|
+
}
|
|
146
|
+
if (publication.payload === "0x") {
|
|
147
|
+
throw new midnight_sdk_1.InvalidTreeError("Offer payload cannot be empty.");
|
|
148
|
+
}
|
|
149
|
+
if (publication.groups.length === 0) {
|
|
150
|
+
throw new midnight_sdk_1.InvalidTreeError("Offer groups cannot be empty.");
|
|
151
|
+
}
|
|
152
|
+
const { priceRatifierV1, rateRatifierV1 } = (0, morpho_ts_1.getChainAddresses)(params.chainId);
|
|
153
|
+
const supportedTargets = [priceRatifierV1, rateRatifierV1].filter((target) => target != null);
|
|
154
|
+
if (!supportedTargets.some((target) => (0, viem_1.isAddressEqual)(publication.ratifier, target))) {
|
|
155
|
+
throw new index_js_2.UnsupportedMidnightAuthorizationTargetError({
|
|
156
|
+
authorized: publication.ratifier,
|
|
157
|
+
chainId: params.chainId,
|
|
158
|
+
supportedTargets,
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
for (const group of publication.groups) {
|
|
162
|
+
if (cancelledGroups.has(group.toLowerCase())) {
|
|
163
|
+
throw new index_js_2.MidnightReplacementGroupCancelledError({ group });
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (publication.blueSupply != null && publication.blueSupply.assets <= 0n) {
|
|
167
|
+
throw new index_js_2.NonPositiveInputError("blueSupply.assets", publication.blueSupply.assets);
|
|
168
|
+
}
|
|
169
|
+
if (publication.collateral != null) {
|
|
170
|
+
if (publication.collateral.supplies.length === 0) {
|
|
171
|
+
throw new index_js_2.EmptyMidnightCollateralSuppliesError();
|
|
172
|
+
}
|
|
173
|
+
(0, validateMidnightMarket_js_1.validateMidnightMarket)({
|
|
174
|
+
market: publication.collateral.market,
|
|
175
|
+
chainId: params.chainId,
|
|
176
|
+
});
|
|
177
|
+
for (const [index, { collateralIndex, assets },] of publication.collateral.supplies.entries()) {
|
|
178
|
+
if (assets <= 0n) {
|
|
179
|
+
throw new index_js_2.NonPositiveInputError(`collateral.supplies[${index}].assets`, assets);
|
|
180
|
+
}
|
|
181
|
+
if (assets > viem_1.maxUint256) {
|
|
182
|
+
throw new index_js_2.InputExceedsMaxError({
|
|
183
|
+
field: `collateral.supplies[${index}].assets`,
|
|
184
|
+
value: assets,
|
|
185
|
+
max: viem_1.maxUint256,
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
// Throws UnknownCollateralIndexError when the index is not configured on the market.
|
|
189
|
+
midnight_sdk_1.MarketUtils.getCollateralByIndex(publication.collateral.market, collateralIndex);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
const collateral = publication?.collateral;
|
|
194
|
+
const collateralPulls = collateral == null
|
|
195
|
+
? []
|
|
196
|
+
: collateral.supplies.map(({ collateralIndex, assets }) => ({
|
|
197
|
+
token: midnight_sdk_1.MarketUtils.getCollateralByIndex(collateral.market, collateralIndex).token,
|
|
198
|
+
assets,
|
|
199
|
+
}));
|
|
200
|
+
const nativeFunding = (0, validate_js_1.validateMidnightNativeAmount)({
|
|
201
|
+
chainId: params.chainId,
|
|
202
|
+
nativeAmount: params.nativeAmount,
|
|
203
|
+
pulls: [
|
|
204
|
+
...(publication?.blueSupply != null && publication.blueSupply.assets > 0n
|
|
205
|
+
? [
|
|
206
|
+
{
|
|
207
|
+
token: publication.blueSupply.market.loanToken,
|
|
208
|
+
assets: publication.blueSupply.assets,
|
|
209
|
+
},
|
|
210
|
+
]
|
|
211
|
+
: []),
|
|
212
|
+
...collateralPulls,
|
|
213
|
+
],
|
|
214
|
+
});
|
|
215
|
+
const cancellations = params.cancellations.map(({ group, maxConsumed }) => ({
|
|
216
|
+
group,
|
|
217
|
+
maxConsumed,
|
|
218
|
+
}));
|
|
219
|
+
const supplies = (publication?.collateral?.supplies ?? []).map(({ collateralIndex, assets }) => ({ collateralIndex, assets }));
|
|
220
|
+
const blueSupply = publication?.blueSupply == null
|
|
221
|
+
? undefined
|
|
222
|
+
: {
|
|
223
|
+
market: {
|
|
224
|
+
loanToken: publication.blueSupply.market.loanToken,
|
|
225
|
+
collateralToken: publication.blueSupply.market.collateralToken,
|
|
226
|
+
oracle: publication.blueSupply.market.oracle,
|
|
227
|
+
irm: publication.blueSupply.market.irm,
|
|
228
|
+
lltv: publication.blueSupply.market.lltv,
|
|
229
|
+
},
|
|
230
|
+
assets: publication.blueSupply.assets,
|
|
231
|
+
callbackSalt: publication.blueSupply.callbackSalt,
|
|
232
|
+
};
|
|
233
|
+
const signature = publication?.rootSignature ?? {
|
|
234
|
+
height: 0n,
|
|
235
|
+
nonce: 0n,
|
|
236
|
+
deadline: 0n,
|
|
237
|
+
v: 0,
|
|
238
|
+
r: viem_1.zeroHash,
|
|
239
|
+
s: viem_1.zeroHash,
|
|
240
|
+
};
|
|
241
|
+
let tx = {
|
|
242
|
+
to: (0, morpho_ts_1.getChainAddress)(params.chainId, "midnightBundlesV2"),
|
|
243
|
+
value: nativeFunding.value,
|
|
244
|
+
data: (0, viem_1.encodeFunctionData)({
|
|
245
|
+
abi: midnight_sdk_1.midnightBundlesV2Abi,
|
|
246
|
+
functionName: "midnightBundlesV2CancelAndMake",
|
|
247
|
+
args: [
|
|
248
|
+
blueSupply?.market ?? emptyBlueMarket, // blueMarket
|
|
249
|
+
blueSupply?.assets ?? 0n, // assetsToPark
|
|
250
|
+
blueSupply?.callbackSalt ?? viem_1.zeroHash, // callbackSalt
|
|
251
|
+
publication?.collateral == null
|
|
252
|
+
? emptyMidnightMarket
|
|
253
|
+
: midnight_sdk_1.MarketUtils.toStruct(publication.collateral.market), // market
|
|
254
|
+
supplies, // collateralSupplies
|
|
255
|
+
publication?.ratifier ?? viem_1.zeroAddress, // ratifier
|
|
256
|
+
publication?.root ?? viem_1.zeroHash, // newRoot
|
|
257
|
+
signature.height, // signatureHeight
|
|
258
|
+
signature.nonce, // signatureNonce
|
|
259
|
+
signature.deadline, // signatureDeadline
|
|
260
|
+
signature.v, // v
|
|
261
|
+
signature.r, // r
|
|
262
|
+
signature.s, // s
|
|
263
|
+
cancellations, // groupsToCancel
|
|
264
|
+
publication?.payload ?? "0x", // payload
|
|
265
|
+
params.deadline, // deadline
|
|
266
|
+
nativeFunding.wrappedNative, // wrappedNative
|
|
267
|
+
],
|
|
268
|
+
}),
|
|
269
|
+
};
|
|
270
|
+
if (params.metadata) {
|
|
271
|
+
tx = (0, index_js_1.addTransactionMetadata)(tx, params.metadata);
|
|
272
|
+
}
|
|
273
|
+
return (0, morpho_ts_1.deepFreeze)({
|
|
274
|
+
...tx,
|
|
275
|
+
action: {
|
|
276
|
+
type: "midnightCancelAndMake",
|
|
277
|
+
args: {
|
|
278
|
+
market: publication?.collateral == null
|
|
279
|
+
? viem_1.zeroHash
|
|
280
|
+
: midnight_sdk_1.MarketUtils.toId(publication.collateral.market),
|
|
281
|
+
ratifier: publication?.ratifier ?? viem_1.zeroAddress,
|
|
282
|
+
root: publication?.root ?? viem_1.zeroHash,
|
|
283
|
+
groups: [...(publication?.groups ?? [])],
|
|
284
|
+
cancellations,
|
|
285
|
+
collateralSupplies: supplies,
|
|
286
|
+
...(blueSupply && { blueSupply }),
|
|
287
|
+
deadline: params.deadline,
|
|
288
|
+
},
|
|
289
|
+
},
|
|
290
|
+
});
|
|
291
|
+
};
|
|
292
|
+
exports.midnightCancelAndMake = midnightCancelAndMake;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
export * from "./authorization.js";
|
|
2
|
-
export * from "./
|
|
3
|
-
export * from "./mempoolSubmitOffers.js";
|
|
2
|
+
export * from "./cancelAndMake.js";
|
|
4
3
|
export * from "./redeem.js";
|
|
5
4
|
export * from "./repayWithdrawCollateral.js";
|
|
6
|
-
export * from "./setterRatifierRatifyRoot.js";
|
|
7
5
|
export * from "./supplyCollateral.js";
|
|
8
6
|
export * from "./supplyCollateralTakeBorrow.js";
|
|
9
7
|
export * from "./takeBorrow.js";
|
|
10
8
|
export * from "./takeLend.js";
|
|
9
|
+
export * from "./takeRepayWithdrawCollateral.js";
|
|
10
|
+
export * from "./takeWithdraw.js";
|
|
11
11
|
export * from "./types.js";
|
|
@@ -15,13 +15,13 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./authorization.js"), exports);
|
|
18
|
-
__exportStar(require("./
|
|
19
|
-
__exportStar(require("./mempoolSubmitOffers.js"), exports);
|
|
18
|
+
__exportStar(require("./cancelAndMake.js"), exports);
|
|
20
19
|
__exportStar(require("./redeem.js"), exports);
|
|
21
20
|
__exportStar(require("./repayWithdrawCollateral.js"), exports);
|
|
22
|
-
__exportStar(require("./setterRatifierRatifyRoot.js"), exports);
|
|
23
21
|
__exportStar(require("./supplyCollateral.js"), exports);
|
|
24
22
|
__exportStar(require("./supplyCollateralTakeBorrow.js"), exports);
|
|
25
23
|
__exportStar(require("./takeBorrow.js"), exports);
|
|
26
24
|
__exportStar(require("./takeLend.js"), exports);
|
|
25
|
+
__exportStar(require("./takeRepayWithdrawCollateral.js"), exports);
|
|
26
|
+
__exportStar(require("./takeWithdraw.js"), exports);
|
|
27
27
|
__exportStar(require("./types.js"), exports);
|
|
@@ -1,51 +1,68 @@
|
|
|
1
1
|
import { type MarketInput } from "@morpho-org/midnight-sdk";
|
|
2
2
|
import { type Address } from "viem";
|
|
3
|
-
import { type Metadata, type MidnightRepayWithdrawCollateralAction, type Transaction } from "../../types/index.js";
|
|
4
|
-
/** Parameters for encoding a Midnight
|
|
3
|
+
import { type Metadata, type MidnightCollateralTransfer, type MidnightRepay, type MidnightRepayWithdrawCollateralAction, type Transaction } from "../../types/index.js";
|
|
4
|
+
/** Parameters for encoding a direct Midnight repayment and/or collateral withdrawal. */
|
|
5
5
|
export interface MidnightRepayWithdrawCollateralParams {
|
|
6
6
|
readonly chainId: number;
|
|
7
7
|
readonly market: MarketInput;
|
|
8
|
-
readonly
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
readonly repay: MidnightRepay;
|
|
9
|
+
/**
|
|
10
|
+
* Optional native ETH sent as `value`; the bundle wraps it into the chain's wNative and transfers
|
|
11
|
+
* it to `msg.sender` before pulling. It may cover part or all of the maximum wNative amount this
|
|
12
|
+
* call can pull, but not more. Any wrapped native not pulled stays in the sender's wallet as
|
|
13
|
+
* wNative. The wNative approval to `MidnightBundlesV2` is still required for the full pulled amount.
|
|
14
|
+
*/
|
|
15
|
+
readonly nativeAmount?: bigint;
|
|
16
|
+
/** `assets: maxUint256` withdraws the sender's whole balance of that collateral at execution. */
|
|
17
|
+
readonly collateralWithdrawals: readonly MidnightCollateralTransfer[];
|
|
18
|
+
readonly collateralReceiver: Address;
|
|
12
19
|
/** Bundle execution deadline timestamp. Pass `maxUint256` explicitly for no expiry. */
|
|
13
20
|
readonly deadline: bigint;
|
|
14
21
|
readonly metadata?: Metadata;
|
|
15
22
|
}
|
|
16
23
|
/**
|
|
17
|
-
* Encodes a
|
|
24
|
+
* Encodes a reduce-only `MidnightBundlesV2` buy with no offers, so the sender's debt is repaid
|
|
25
|
+
* directly, then collateral is withdrawn to `collateralReceiver`. An `assets` repay uses the
|
|
26
|
+
* assets-target entrypoint with `minUnits = assets` (a direct repay counts one unit per asset); a
|
|
27
|
+
* `full` repay uses the units-target entrypoint with `targetUnits = maxUint256`.
|
|
18
28
|
*
|
|
19
|
-
* Prefer `client.morpho.midnight(chainId).repayWithdrawCollateral(...)` in app
|
|
20
|
-
*
|
|
21
|
-
* resolved first. Use this low-level builder only when those requirements and
|
|
22
|
-
* collateral-index checks are already handled by the caller.
|
|
29
|
+
* Prefer `client.morpho.midnight(chainId).repayWithdrawCollateral(...)` in app flows so the
|
|
30
|
+
* loan-token approval and Midnight authorization requirements are resolved first.
|
|
23
31
|
*
|
|
24
|
-
* @param params.chainId - Chain id used to resolve `
|
|
32
|
+
* @param params.chainId - Chain id used to resolve `MidnightBundlesV2`.
|
|
25
33
|
* @param params.market - Midnight market whose position is updated.
|
|
26
|
-
* @param params.
|
|
27
|
-
* @param params.
|
|
28
|
-
*
|
|
29
|
-
*
|
|
34
|
+
* @param params.repay - `{ type: "assets", assets }` (`0n` to only withdraw) or `{ type: "full", maxBuyerAssets }`.
|
|
35
|
+
* @param params.nativeAmount - Optional native ETH sent as `value`; the bundle wraps it into the
|
|
36
|
+
* chain's wNative and transfers it to `msg.sender` before pulling. It may cover part or all of the
|
|
37
|
+
* maximum wNative amount this call can pull, but not more. Any wrapped native not pulled stays in
|
|
38
|
+
* the sender's wallet as wNative. The wNative approval to `MidnightBundlesV2` is still required
|
|
39
|
+
* for the full pulled amount.
|
|
40
|
+
* @param params.collateralWithdrawals - Collateral withdrawals; `assets: maxUint256` withdraws the full balance.
|
|
41
|
+
* @param params.collateralReceiver - Recipient of the withdrawn collateral.
|
|
30
42
|
* @param params.deadline - Bundle execution deadline timestamp; pass `maxUint256` explicitly for no expiry.
|
|
31
43
|
* @param params.metadata - Optional analytics metadata appended to calldata.
|
|
32
|
-
* @returns A deep-frozen `Transaction<MidnightRepayWithdrawCollateralAction>` targeting `
|
|
33
|
-
* @throws {NegativeInputError} when
|
|
34
|
-
* @throws {NonPositiveInputError} when
|
|
44
|
+
* @returns A deep-frozen `Transaction<MidnightRepayWithdrawCollateralAction>` targeting `MidnightBundlesV2`.
|
|
45
|
+
* @throws {NegativeInputError} when `repay.assets` or `nativeAmount` is negative.
|
|
46
|
+
* @throws {NonPositiveInputError} when nothing is repaid or withdrawn, a withdrawal amount is zero, `repay.maxBuyerAssets` is zero, or `deadline` is not positive.
|
|
47
|
+
* @throws {InputExceedsMaxError} when `repay.assets` or `repay.maxBuyerAssets` is at least `maxUint256`, a withdrawal amount or `deadline` exceeds uint256, or `nativeAmount` exceeds the maximum wNative pull, including zero for a withdraw-only call.
|
|
48
|
+
* @throws {ChainWNativeMissingError} when wNative is not configured for the chain.
|
|
49
|
+
* @throws {NativeAmountOnNonWNativeAssetError} when `market.loanToken` is not wNative.
|
|
50
|
+
* @throws {UnknownAddressError} when the chain has no `midnightBundlesV2` deployment.
|
|
35
51
|
* @throws {ChainIdMismatchError} when the market targets another chain.
|
|
36
52
|
* @throws {MidnightMarketAddressMismatchError} when the market targets another Midnight deployment.
|
|
37
|
-
* @throws {UnknownCollateralIndexError} when a
|
|
53
|
+
* @throws {UnknownCollateralIndexError} when a withdrawal targets an unconfigured collateral index.
|
|
38
54
|
* @example
|
|
39
55
|
* ```ts
|
|
40
56
|
* import { maxUint256 } from "viem";
|
|
41
57
|
* import { midnightRepayWithdrawCollateral } from "@morpho-org/morpho-sdk";
|
|
42
58
|
*
|
|
59
|
+
* // Close the position: repay all debt, then withdraw all collateral at index 0.
|
|
43
60
|
* const tx = midnightRepayWithdrawCollateral({
|
|
44
61
|
* chainId: 8453,
|
|
45
62
|
* market: marketData.params,
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
63
|
+
* repay: { type: "full", maxBuyerAssets: 1_010_000n },
|
|
64
|
+
* collateralWithdrawals: [{ collateralIndex: 0n, assets: maxUint256 }],
|
|
65
|
+
* collateralReceiver: user,
|
|
49
66
|
* deadline: maxUint256,
|
|
50
67
|
* });
|
|
51
68
|
* ```
|