@morpho-org/morpho-sdk 6.6.0 → 7.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/MIGRATION-v6-to-v7.md +291 -0
- package/README.md +26 -12
- package/lib/cjs/abis.d.ts +1 -1
- package/lib/cjs/abis.js +3 -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/constants.d.ts +1 -1
- package/lib/cjs/constants.js +2 -1
- package/lib/cjs/entities/index.d.ts +2 -2
- package/lib/cjs/entities/midnight/midnight.d.ts +582 -108
- package/lib/cjs/entities/midnight/midnight.js +981 -509
- package/lib/cjs/entities/midnight/types.d.ts +142 -60
- package/lib/cjs/entities/vaultV1/vaultV1.d.ts +2 -1
- package/lib/cjs/entities/vaultV1/vaultV1.js +1 -0
- package/lib/cjs/entities/vaultV2/vaultV2.d.ts +2 -1
- package/lib/cjs/entities/vaultV2/vaultV2.js +1 -0
- 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/types.d.ts +1 -1
- package/lib/cjs/types/action.d.ts +117 -89
- package/lib/cjs/types/action.js +1 -19
- package/lib/cjs/types/error.d.ts +126 -40
- package/lib/cjs/types/error.js +154 -49
- package/lib/esm/abis.d.ts +1 -1
- package/lib/esm/abis.js +1 -1
- 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/constants.d.ts +1 -1
- package/lib/esm/constants.js +1 -1
- package/lib/esm/entities/index.d.ts +2 -2
- package/lib/esm/entities/midnight/midnight.d.ts +582 -108
- package/lib/esm/entities/midnight/midnight.js +987 -515
- package/lib/esm/entities/midnight/types.d.ts +142 -60
- package/lib/esm/entities/vaultV1/vaultV1.d.ts +2 -1
- package/lib/esm/entities/vaultV1/vaultV1.js +1 -0
- package/lib/esm/entities/vaultV2/vaultV2.d.ts +2 -1
- package/lib/esm/entities/vaultV2/vaultV2.js +1 -0
- 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/types.d.ts +1 -1
- package/lib/esm/types/action.d.ts +117 -89
- package/lib/esm/types/action.js +1 -18
- package/lib/esm/types/error.d.ts +126 -40
- package/lib/esm/types/error.js +135 -38
- package/package.json +8 -7
- 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
|
@@ -1,56 +1,80 @@
|
|
|
1
1
|
import { type MarketInput } from "@morpho-org/midnight-sdk";
|
|
2
|
-
import { type
|
|
3
|
-
import
|
|
4
|
-
import { type MidnightTakeableOffer } from "./types.js";
|
|
2
|
+
import { type Metadata, type MidnightBuyTarget, type MidnightTakeLendAction, type Transaction } from "../../types/index.js";
|
|
3
|
+
import type { MidnightReferralFeeParams, MidnightTakeableOffer } from "./types.js";
|
|
5
4
|
/** Parameters for encoding a Midnight lend take from already selected offers. */
|
|
6
|
-
export interface MidnightTakeLendParams {
|
|
5
|
+
export interface MidnightTakeLendParams extends MidnightReferralFeeParams {
|
|
7
6
|
readonly chainId: number;
|
|
8
7
|
readonly market: MarketInput;
|
|
9
|
-
|
|
10
|
-
readonly
|
|
11
|
-
|
|
8
|
+
/** Loan assets paid with a unit floor, or units bought with a loan-asset cap. */
|
|
9
|
+
readonly target: MidnightBuyTarget;
|
|
10
|
+
/**
|
|
11
|
+
* Optional native ETH sent as `value`; the bundle wraps it into the chain's wNative and transfers
|
|
12
|
+
* it to `msg.sender` before pulling. It may cover part or all of the maximum wNative amount this
|
|
13
|
+
* call can pull, but not more. Any wrapped native not pulled stays in the sender's wallet as
|
|
14
|
+
* wNative. The wNative approval to `MidnightBundlesV2` is still required for the full pulled amount.
|
|
15
|
+
*/
|
|
16
|
+
readonly nativeAmount?: bigint;
|
|
12
17
|
readonly takeableOffers: readonly MidnightTakeableOffer[];
|
|
13
|
-
/**
|
|
18
|
+
/** Largest market continuous fee accepted when taking offers. Pass `maxUint256` explicitly for no cap. */
|
|
19
|
+
readonly maxContinuousFee: bigint;
|
|
20
|
+
/** Bounded bundle execution deadline timestamp. */
|
|
14
21
|
readonly deadline: bigint;
|
|
15
22
|
readonly metadata?: Metadata;
|
|
16
23
|
}
|
|
17
24
|
/**
|
|
18
|
-
* Encodes a
|
|
25
|
+
* Encodes a `MidnightBundlesV2` buy that lends into borrow-side offers for `msg.sender`.
|
|
19
26
|
*
|
|
20
|
-
* Prefer `client.morpho.midnight(chainId).takeLend(...)` in app flows so
|
|
21
|
-
*
|
|
22
|
-
* low-level builder only after the Midnight API has returned takeable offers
|
|
23
|
-
* and the caller has already handled prerequisites.
|
|
27
|
+
* Prefer `client.morpho.midnight(chainId).takeLend(...)` in app flows so the loan-token approval
|
|
28
|
+
* and the `MidnightBundlesV2` authorization are resolved before building the bundle.
|
|
24
29
|
*
|
|
25
|
-
* @param params.chainId - Chain id used to resolve `
|
|
30
|
+
* @param params.chainId - Chain id used to resolve `MidnightBundlesV2`.
|
|
26
31
|
* @param params.market - Midnight market traded by every takeable offer.
|
|
27
|
-
* @param params.
|
|
28
|
-
* @param params.
|
|
29
|
-
*
|
|
32
|
+
* @param params.target - `{ type: "assets", assets, minUnits }` or `{ type: "units", units, maxBuyerAssets }`.
|
|
33
|
+
* @param params.nativeAmount - Optional native ETH sent as `value`; the bundle wraps it into the
|
|
34
|
+
* chain's wNative and transfers it to `msg.sender` before pulling. It may cover part or all of the
|
|
35
|
+
* maximum wNative amount this call can pull, but not more. Any wrapped native not pulled stays in
|
|
36
|
+
* the sender's wallet as wNative. The wNative approval to `MidnightBundlesV2` is still required
|
|
37
|
+
* for the full pulled amount.
|
|
30
38
|
* @param params.takeableOffers - ABI-ready borrow-side offers returned by the Midnight API.
|
|
31
|
-
* @param params.
|
|
39
|
+
* @param params.maxContinuousFee - Largest market continuous fee accepted, in the unit of the market's
|
|
40
|
+
* onchain `continuousFee` (`Market.continuousFee`): a WAD-scaled per-second rate, so `317097919n`
|
|
41
|
+
* is about 1% per year. The take reverts if the market fee is higher. Pass `maxUint256` for no cap.
|
|
42
|
+
* @param params.deadline - Bounded bundle execution deadline timestamp.
|
|
43
|
+
* @param params.referralFeePct - Optional WAD-scaled referral fee paid out of the pulled assets.
|
|
44
|
+
* @param params.referralFeeRecipient - Referral fee recipient; required with a positive fee.
|
|
32
45
|
* @param params.metadata - Optional analytics metadata appended to calldata.
|
|
33
|
-
* @returns A deep-frozen `Transaction<MidnightTakeLendAction>` targeting `
|
|
34
|
-
* @throws {
|
|
35
|
-
* @throws {
|
|
46
|
+
* @returns A deep-frozen `Transaction<MidnightTakeLendAction>` targeting `MidnightBundlesV2`.
|
|
47
|
+
* @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
|
|
48
|
+
* @throws {UnknownAddressError} when the chain has no `midnightBundlesV2` deployment.
|
|
49
|
+
* @throws {ChainIdMismatchError} when the market targets another chain.
|
|
50
|
+
* @throws {MidnightMarketAddressMismatchError} when the market targets another Midnight deployment.
|
|
36
51
|
* @throws {EmptyMidnightTakeableOffersError} when no offers are provided.
|
|
37
52
|
* @throws {MidnightOfferSideMismatchError} when any offer is not borrow-side.
|
|
38
53
|
* @throws {MidnightTakeableOfferMarketMismatchError} when any offer belongs to another market.
|
|
39
|
-
* @throws {
|
|
40
|
-
* @throws {
|
|
54
|
+
* @throws {NonPositiveInputError} when the target amount, `minUnits`, `maxBuyerAssets` or `deadline` is not positive.
|
|
55
|
+
* @throws {NegativeInputError} when `maxContinuousFee`, `referralFeePct` or `nativeAmount` is negative.
|
|
56
|
+
* @throws {ChainWNativeMissingError} when wNative is not configured for the chain.
|
|
57
|
+
* @throws {NativeAmountOnNonWNativeAssetError} when `market.loanToken` is not wNative.
|
|
58
|
+
* @throws {InputExceedsMaxError} when `target.assets`, `target.units`, `minUnits`, `maxBuyerAssets`,
|
|
59
|
+
* `maxContinuousFee` or `deadline` exceeds `uint256`, or `nativeAmount` exceeds the maximum
|
|
60
|
+
* wNative amount this call can pull.
|
|
61
|
+
* @throws {UnboundedMidnightTakeInputError} when `target.units`, `target.maxBuyerAssets` or
|
|
62
|
+
* `deadline` is `maxUint256`.
|
|
63
|
+
* @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
|
|
64
|
+
* @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
|
|
41
65
|
* @example
|
|
42
66
|
* ```ts
|
|
67
|
+
* import { Time } from "@morpho-org/morpho-ts";
|
|
43
68
|
* import { maxUint256 } from "viem";
|
|
44
69
|
* import { midnightTakeLend } from "@morpho-org/morpho-sdk";
|
|
45
70
|
*
|
|
46
71
|
* const tx = midnightTakeLend({
|
|
47
72
|
* chainId: 8453,
|
|
48
73
|
* market: marketData.params,
|
|
49
|
-
* assets: 1_000_000n,
|
|
50
|
-
* minUnits: 900_000n,
|
|
51
|
-
* taker: lender,
|
|
74
|
+
* target: { type: "assets", assets: 1_000_000n, minUnits: 900_000n },
|
|
52
75
|
* takeableOffers: quote.data.takeableOffers,
|
|
53
|
-
*
|
|
76
|
+
* maxContinuousFee: maxUint256,
|
|
77
|
+
* deadline: Time.timestamp() + 600n,
|
|
54
78
|
* });
|
|
55
79
|
* ```
|
|
56
80
|
*/
|
|
@@ -1,90 +1,146 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { MarketUtils, midnightBundlesV2Abi, } from "@morpho-org/midnight-sdk";
|
|
2
2
|
import { deepFreeze, getChainAddress } from "@morpho-org/morpho-ts";
|
|
3
|
-
import { encodeFunctionData, maxUint256, zeroAddress
|
|
3
|
+
import { encodeFunctionData, maxUint256, zeroAddress } from "viem";
|
|
4
4
|
import { addTransactionMetadata } from "../../helpers/index.js";
|
|
5
|
+
import { validateDeadline, validateMidnightNativeAmount, validateReferralFee, validateUint256Field, } from "../../helpers/validate.js";
|
|
5
6
|
import { validateMidnightMarket } from "../../helpers/validateMidnightMarket.js";
|
|
6
7
|
import { validateTakeableOffers } from "../../helpers/validateTakeableOffers.js";
|
|
7
|
-
import {
|
|
8
|
-
import { PermitKind } from "./types.js";
|
|
8
|
+
import { NonPositiveInputError, UnboundedMidnightTakeInputError, } from "../../types/index.js";
|
|
9
9
|
/**
|
|
10
|
-
* Encodes a
|
|
10
|
+
* Encodes a `MidnightBundlesV2` buy that lends into borrow-side offers for `msg.sender`.
|
|
11
11
|
*
|
|
12
|
-
* Prefer `client.morpho.midnight(chainId).takeLend(...)` in app flows so
|
|
13
|
-
*
|
|
14
|
-
* low-level builder only after the Midnight API has returned takeable offers
|
|
15
|
-
* and the caller has already handled prerequisites.
|
|
12
|
+
* Prefer `client.morpho.midnight(chainId).takeLend(...)` in app flows so the loan-token approval
|
|
13
|
+
* and the `MidnightBundlesV2` authorization are resolved before building the bundle.
|
|
16
14
|
*
|
|
17
|
-
* @param params.chainId - Chain id used to resolve `
|
|
15
|
+
* @param params.chainId - Chain id used to resolve `MidnightBundlesV2`.
|
|
18
16
|
* @param params.market - Midnight market traded by every takeable offer.
|
|
19
|
-
* @param params.
|
|
20
|
-
* @param params.
|
|
21
|
-
*
|
|
17
|
+
* @param params.target - `{ type: "assets", assets, minUnits }` or `{ type: "units", units, maxBuyerAssets }`.
|
|
18
|
+
* @param params.nativeAmount - Optional native ETH sent as `value`; the bundle wraps it into the
|
|
19
|
+
* chain's wNative and transfers it to `msg.sender` before pulling. It may cover part or all of the
|
|
20
|
+
* maximum wNative amount this call can pull, but not more. Any wrapped native not pulled stays in
|
|
21
|
+
* the sender's wallet as wNative. The wNative approval to `MidnightBundlesV2` is still required
|
|
22
|
+
* for the full pulled amount.
|
|
22
23
|
* @param params.takeableOffers - ABI-ready borrow-side offers returned by the Midnight API.
|
|
23
|
-
* @param params.
|
|
24
|
+
* @param params.maxContinuousFee - Largest market continuous fee accepted, in the unit of the market's
|
|
25
|
+
* onchain `continuousFee` (`Market.continuousFee`): a WAD-scaled per-second rate, so `317097919n`
|
|
26
|
+
* is about 1% per year. The take reverts if the market fee is higher. Pass `maxUint256` for no cap.
|
|
27
|
+
* @param params.deadline - Bounded bundle execution deadline timestamp.
|
|
28
|
+
* @param params.referralFeePct - Optional WAD-scaled referral fee paid out of the pulled assets.
|
|
29
|
+
* @param params.referralFeeRecipient - Referral fee recipient; required with a positive fee.
|
|
24
30
|
* @param params.metadata - Optional analytics metadata appended to calldata.
|
|
25
|
-
* @returns A deep-frozen `Transaction<MidnightTakeLendAction>` targeting `
|
|
26
|
-
* @throws {
|
|
27
|
-
* @throws {
|
|
31
|
+
* @returns A deep-frozen `Transaction<MidnightTakeLendAction>` targeting `MidnightBundlesV2`.
|
|
32
|
+
* @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
|
|
33
|
+
* @throws {UnknownAddressError} when the chain has no `midnightBundlesV2` deployment.
|
|
34
|
+
* @throws {ChainIdMismatchError} when the market targets another chain.
|
|
35
|
+
* @throws {MidnightMarketAddressMismatchError} when the market targets another Midnight deployment.
|
|
28
36
|
* @throws {EmptyMidnightTakeableOffersError} when no offers are provided.
|
|
29
37
|
* @throws {MidnightOfferSideMismatchError} when any offer is not borrow-side.
|
|
30
38
|
* @throws {MidnightTakeableOfferMarketMismatchError} when any offer belongs to another market.
|
|
31
|
-
* @throws {
|
|
32
|
-
* @throws {
|
|
39
|
+
* @throws {NonPositiveInputError} when the target amount, `minUnits`, `maxBuyerAssets` or `deadline` is not positive.
|
|
40
|
+
* @throws {NegativeInputError} when `maxContinuousFee`, `referralFeePct` or `nativeAmount` is negative.
|
|
41
|
+
* @throws {ChainWNativeMissingError} when wNative is not configured for the chain.
|
|
42
|
+
* @throws {NativeAmountOnNonWNativeAssetError} when `market.loanToken` is not wNative.
|
|
43
|
+
* @throws {InputExceedsMaxError} when `target.assets`, `target.units`, `minUnits`, `maxBuyerAssets`,
|
|
44
|
+
* `maxContinuousFee` or `deadline` exceeds `uint256`, or `nativeAmount` exceeds the maximum
|
|
45
|
+
* wNative amount this call can pull.
|
|
46
|
+
* @throws {UnboundedMidnightTakeInputError} when `target.units`, `target.maxBuyerAssets` or
|
|
47
|
+
* `deadline` is `maxUint256`.
|
|
48
|
+
* @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
|
|
49
|
+
* @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
|
|
33
50
|
* @example
|
|
34
51
|
* ```ts
|
|
52
|
+
* import { Time } from "@morpho-org/morpho-ts";
|
|
35
53
|
* import { maxUint256 } from "viem";
|
|
36
54
|
* import { midnightTakeLend } from "@morpho-org/morpho-sdk";
|
|
37
55
|
*
|
|
38
56
|
* const tx = midnightTakeLend({
|
|
39
57
|
* chainId: 8453,
|
|
40
58
|
* market: marketData.params,
|
|
41
|
-
* assets: 1_000_000n,
|
|
42
|
-
* minUnits: 900_000n,
|
|
43
|
-
* taker: lender,
|
|
59
|
+
* target: { type: "assets", assets: 1_000_000n, minUnits: 900_000n },
|
|
44
60
|
* takeableOffers: quote.data.takeableOffers,
|
|
45
|
-
*
|
|
61
|
+
* maxContinuousFee: maxUint256,
|
|
62
|
+
* deadline: Time.timestamp() + 600n,
|
|
46
63
|
* });
|
|
47
64
|
* ```
|
|
48
65
|
*/
|
|
49
66
|
export const midnightTakeLend = (params) => {
|
|
50
|
-
|
|
51
|
-
throw new NonPositiveInputError("assets", params.assets);
|
|
52
|
-
}
|
|
53
|
-
if (params.minUnits < 0n) {
|
|
54
|
-
throw new NegativeInputError("minUnits", params.minUnits);
|
|
55
|
-
}
|
|
56
|
-
if (params.deadline < 0n) {
|
|
57
|
-
throw new NegativeInputError("deadline", params.deadline);
|
|
58
|
-
}
|
|
59
|
-
// Reject markets from another chain deployment before encoding the bundle.
|
|
67
|
+
// Reject markets from another chain deployment before checking offers against them.
|
|
60
68
|
validateMidnightMarket({ market: params.market, chainId: params.chainId });
|
|
61
69
|
const marketId = validateTakeableOffers({
|
|
62
70
|
market: params.market,
|
|
63
71
|
takeableOffers: params.takeableOffers,
|
|
64
72
|
expectedBuy: false,
|
|
65
73
|
});
|
|
66
|
-
const
|
|
74
|
+
const { target } = params;
|
|
75
|
+
if (target.type === "assets") {
|
|
76
|
+
if (target.assets <= 0n) {
|
|
77
|
+
throw new NonPositiveInputError("target.assets", target.assets);
|
|
78
|
+
}
|
|
79
|
+
if (target.minUnits <= 0n) {
|
|
80
|
+
throw new NonPositiveInputError("target.minUnits", target.minUnits);
|
|
81
|
+
}
|
|
82
|
+
validateUint256Field("target.assets", target.assets);
|
|
83
|
+
validateUint256Field("target.minUnits", target.minUnits);
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
if (target.units === maxUint256) {
|
|
87
|
+
throw new UnboundedMidnightTakeInputError("target.units");
|
|
88
|
+
}
|
|
89
|
+
if (target.maxBuyerAssets === maxUint256) {
|
|
90
|
+
throw new UnboundedMidnightTakeInputError("target.maxBuyerAssets");
|
|
91
|
+
}
|
|
92
|
+
if (target.units <= 0n) {
|
|
93
|
+
throw new NonPositiveInputError("target.units", target.units);
|
|
94
|
+
}
|
|
95
|
+
if (target.maxBuyerAssets <= 0n) {
|
|
96
|
+
throw new NonPositiveInputError("target.maxBuyerAssets", target.maxBuyerAssets);
|
|
97
|
+
}
|
|
98
|
+
validateUint256Field("target.units", target.units);
|
|
99
|
+
validateUint256Field("target.maxBuyerAssets", target.maxBuyerAssets);
|
|
100
|
+
}
|
|
101
|
+
validateUint256Field("maxContinuousFee", params.maxContinuousFee);
|
|
102
|
+
if (params.deadline === maxUint256) {
|
|
103
|
+
throw new UnboundedMidnightTakeInputError("deadline");
|
|
104
|
+
}
|
|
105
|
+
validateDeadline(params.deadline);
|
|
106
|
+
const { referralFeePct, referralFeeRecipient } = validateReferralFee(params);
|
|
107
|
+
const market = MarketUtils.toStruct(params.market);
|
|
108
|
+
const nativeFunding = validateMidnightNativeAmount({
|
|
109
|
+
chainId: params.chainId,
|
|
110
|
+
nativeAmount: params.nativeAmount,
|
|
111
|
+
pulls: [
|
|
112
|
+
{
|
|
113
|
+
token: market.loanToken,
|
|
114
|
+
assets: target.type === "assets" ? target.assets : target.maxBuyerAssets,
|
|
115
|
+
},
|
|
116
|
+
],
|
|
117
|
+
});
|
|
118
|
+
const args = [
|
|
119
|
+
false, // reduceOnly
|
|
120
|
+
false, // repayEnabled
|
|
121
|
+
params.takeableOffers, // offerFills
|
|
122
|
+
[], // collateralWithdrawals
|
|
123
|
+
zeroAddress, // collateralReceiver
|
|
124
|
+
referralFeePct, // referralFeePct
|
|
125
|
+
referralFeeRecipient, // referralFeeRecipient
|
|
126
|
+
params.maxContinuousFee, // maxContinuousFee
|
|
127
|
+
params.deadline, // deadline
|
|
128
|
+
nativeFunding.wrappedNative, // wrappedNative
|
|
129
|
+
];
|
|
67
130
|
let tx = {
|
|
68
|
-
to:
|
|
69
|
-
value:
|
|
70
|
-
data:
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
zeroAddress,
|
|
82
|
-
0n,
|
|
83
|
-
zeroAddress,
|
|
84
|
-
maxUint256,
|
|
85
|
-
params.deadline,
|
|
86
|
-
],
|
|
87
|
-
}),
|
|
131
|
+
to: getChainAddress(params.chainId, "midnightBundlesV2"),
|
|
132
|
+
value: nativeFunding.value,
|
|
133
|
+
data: target.type === "assets"
|
|
134
|
+
? encodeFunctionData({
|
|
135
|
+
abi: midnightBundlesV2Abi,
|
|
136
|
+
functionName: "midnightBundlesV2BuyWithAssetsTargetAndWithdrawCollateral",
|
|
137
|
+
args: [market, target.assets, target.minUnits, ...args],
|
|
138
|
+
})
|
|
139
|
+
: encodeFunctionData({
|
|
140
|
+
abi: midnightBundlesV2Abi,
|
|
141
|
+
functionName: "midnightBundlesV2BuyWithUnitsTargetAndWithdrawCollateral",
|
|
142
|
+
args: [market, target.units, target.maxBuyerAssets, ...args],
|
|
143
|
+
}),
|
|
88
144
|
};
|
|
89
145
|
if (params.metadata) {
|
|
90
146
|
tx = addTransactionMetadata(tx, params.metadata);
|
|
@@ -95,11 +151,12 @@ export const midnightTakeLend = (params) => {
|
|
|
95
151
|
type: "midnightTakeLend",
|
|
96
152
|
args: {
|
|
97
153
|
market: marketId,
|
|
98
|
-
|
|
99
|
-
minUnits: params.minUnits,
|
|
100
|
-
taker: params.taker,
|
|
154
|
+
target: { ...params.target },
|
|
101
155
|
takeableOffers: params.takeableOffers.length,
|
|
156
|
+
maxContinuousFee: params.maxContinuousFee,
|
|
102
157
|
deadline: params.deadline,
|
|
158
|
+
referralFeePct,
|
|
159
|
+
referralFeeRecipient,
|
|
103
160
|
},
|
|
104
161
|
},
|
|
105
162
|
});
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { type MarketInput } from "@morpho-org/midnight-sdk";
|
|
2
|
+
import { type Address } from "viem";
|
|
3
|
+
import { type Metadata, type MidnightBuyTarget, type MidnightCollateralTransfer, type MidnightTakeRepayWithdrawCollateralAction, type Transaction } from "../../types/index.js";
|
|
4
|
+
import type { MidnightReferralFeeParams, MidnightTakeableOffer } from "./types.js";
|
|
5
|
+
/** Parameters for encoding a Midnight debt repayment through offers, then collateral withdrawals. */
|
|
6
|
+
export interface MidnightTakeRepayWithdrawCollateralParams extends MidnightReferralFeeParams {
|
|
7
|
+
readonly chainId: number;
|
|
8
|
+
readonly market: MarketInput;
|
|
9
|
+
/** Loan assets paid with a unit floor, or debt units repaid with a loan-asset cap. */
|
|
10
|
+
readonly target: MidnightBuyTarget;
|
|
11
|
+
/**
|
|
12
|
+
* Optional native ETH sent as `value`; the bundle wraps it into the chain's wNative and transfers
|
|
13
|
+
* it to `msg.sender` before pulling. It may cover part or all of the maximum wNative amount this
|
|
14
|
+
* call can pull, but not more. Any wrapped native not pulled stays in the sender's wallet as
|
|
15
|
+
* wNative. The wNative approval to `MidnightBundlesV2` is still required for the full pulled amount.
|
|
16
|
+
*/
|
|
17
|
+
readonly nativeAmount?: bigint;
|
|
18
|
+
readonly takeableOffers: readonly MidnightTakeableOffer[];
|
|
19
|
+
/** Repays the rest of the target directly to Midnight when offers do not fill it. */
|
|
20
|
+
readonly repayEnabled: boolean;
|
|
21
|
+
/** Collateral withdrawn after repaying; `maxUint256` assets withdraws the whole balance. */
|
|
22
|
+
readonly collateralWithdrawals: readonly MidnightCollateralTransfer[];
|
|
23
|
+
readonly collateralReceiver: Address;
|
|
24
|
+
/** Largest market continuous fee accepted when taking offers. Pass `maxUint256` explicitly for no cap. */
|
|
25
|
+
readonly maxContinuousFee: bigint;
|
|
26
|
+
/** Bounded bundle execution deadline timestamp. */
|
|
27
|
+
readonly deadline: bigint;
|
|
28
|
+
readonly metadata?: Metadata;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Encodes a `MidnightBundlesV2` reduce-only buy that repays `msg.sender`'s debt by taking
|
|
32
|
+
* borrow-side offers, optionally repays the remainder directly, then withdraws collateral.
|
|
33
|
+
*
|
|
34
|
+
* `reduceOnly` is always set, so the buy never opens a lender position. Prefer
|
|
35
|
+
* `client.morpho.midnight(chainId).takeRepayWithdrawCollateral(...)` in app flows so the
|
|
36
|
+
* loan-token approval and the `MidnightBundlesV2` authorization are resolved first.
|
|
37
|
+
*
|
|
38
|
+
* @param params.chainId - Chain id used to resolve `MidnightBundlesV2`.
|
|
39
|
+
* @param params.market - Midnight market traded by every takeable offer.
|
|
40
|
+
* @param params.target - `{ type: "assets", assets, minUnits }` or `{ type: "units", units, maxBuyerAssets }`; `units: maxUint256` targets the sender's whole debt, capped by `maxBuyerAssets` (must be finite; `units: maxUint256` already selects the whole debt).
|
|
41
|
+
* @param params.nativeAmount - Optional native ETH sent as `value`; the bundle wraps it into the
|
|
42
|
+
* chain's wNative and transfers it to `msg.sender` before pulling. It may cover part or all of the
|
|
43
|
+
* maximum wNative amount this call can pull, but not more. Any wrapped native not pulled stays in
|
|
44
|
+
* the sender's wallet as wNative. The wNative approval to `MidnightBundlesV2` is still required
|
|
45
|
+
* for the full pulled amount.
|
|
46
|
+
* @param params.takeableOffers - ABI-ready borrow-side offers returned by the Midnight API; must not be empty.
|
|
47
|
+
* @param params.repayEnabled - Whether the unfilled remainder is repaid directly to Midnight.
|
|
48
|
+
* @param params.collateralWithdrawals - Collateral index and assets per withdrawal; may be empty.
|
|
49
|
+
* @param params.collateralReceiver - Recipient of withdrawn collateral.
|
|
50
|
+
* @param params.maxContinuousFee - Largest market continuous fee accepted, in the unit of the market's
|
|
51
|
+
* onchain `continuousFee` (`Market.continuousFee`): a WAD-scaled per-second rate, so `317097919n`
|
|
52
|
+
* is about 1% per year. The take reverts if the market fee is higher. Pass `maxUint256` for no cap.
|
|
53
|
+
* @param params.deadline - Bounded bundle execution deadline timestamp.
|
|
54
|
+
* @param params.referralFeePct - Optional WAD-scaled referral fee paid out of the pulled assets.
|
|
55
|
+
* @param params.referralFeeRecipient - Referral fee recipient; required with a positive fee.
|
|
56
|
+
* @param params.metadata - Optional analytics metadata appended to calldata.
|
|
57
|
+
* @returns A deep-frozen `Transaction<MidnightTakeRepayWithdrawCollateralAction>` targeting `MidnightBundlesV2`.
|
|
58
|
+
* @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
|
|
59
|
+
* @throws {UnknownAddressError} when the chain has no `midnightBundlesV2` deployment.
|
|
60
|
+
* @throws {ChainIdMismatchError} when the market targets another chain.
|
|
61
|
+
* @throws {MidnightMarketAddressMismatchError} when the market targets another Midnight deployment.
|
|
62
|
+
* @throws {EmptyMidnightTakeableOffersError} when no offers are provided.
|
|
63
|
+
* @throws {MidnightOfferSideMismatchError} when any offer is not borrow-side.
|
|
64
|
+
* @throws {MidnightTakeableOfferMarketMismatchError} when any offer belongs to another market.
|
|
65
|
+
* @throws {UnknownCollateralIndexError} when a withdrawal targets a collateral index not configured on the market.
|
|
66
|
+
* @throws {NonPositiveInputError} when the target amount, `minUnits`, `maxBuyerAssets`, a
|
|
67
|
+
* withdrawal amount or `deadline` is not positive.
|
|
68
|
+
* @throws {NegativeInputError} when `maxContinuousFee`, `referralFeePct` or `nativeAmount` is negative.
|
|
69
|
+
* @throws {ChainWNativeMissingError} when wNative is not configured for the chain.
|
|
70
|
+
* @throws {NativeAmountOnNonWNativeAssetError} when `market.loanToken` is not wNative.
|
|
71
|
+
* @throws {InputExceedsMaxError} when `target.assets` or `target.maxBuyerAssets` is at
|
|
72
|
+
* least `maxUint256`, or a `collateralWithdrawals[].assets` amount, `target.units`,
|
|
73
|
+
* `minUnits`, `maxBuyerAssets`, `maxContinuousFee` or `deadline` exceeds `uint256`, or
|
|
74
|
+
* `nativeAmount` exceeds the maximum wNative amount this call can pull.
|
|
75
|
+
* @throws {UnboundedMidnightTakeInputError} when `deadline` is `maxUint256`.
|
|
76
|
+
* @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
|
|
77
|
+
* @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
|
|
78
|
+
* @example
|
|
79
|
+
* ```ts
|
|
80
|
+
* import { Time } from "@morpho-org/morpho-ts";
|
|
81
|
+
* import { maxUint256 } from "viem";
|
|
82
|
+
* import { midnightTakeRepayWithdrawCollateral } from "@morpho-org/morpho-sdk";
|
|
83
|
+
*
|
|
84
|
+
* const tx = midnightTakeRepayWithdrawCollateral({
|
|
85
|
+
* chainId: 8453,
|
|
86
|
+
* market: marketData.params,
|
|
87
|
+
* target: { type: "units", units: 1_000_000n, maxBuyerAssets: 990_000n },
|
|
88
|
+
* takeableOffers: quote.data.takeableOffers,
|
|
89
|
+
* repayEnabled: true,
|
|
90
|
+
* collateralWithdrawals: [{ collateralIndex: 0n, assets: maxUint256 }],
|
|
91
|
+
* collateralReceiver: borrower,
|
|
92
|
+
* maxContinuousFee: maxUint256,
|
|
93
|
+
* deadline: Time.timestamp() + 600n,
|
|
94
|
+
* });
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
export declare const midnightTakeRepayWithdrawCollateral: (params: MidnightTakeRepayWithdrawCollateralParams) => Readonly<Transaction<MidnightTakeRepayWithdrawCollateralAction>>;
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { MarketUtils, midnightBundlesV2Abi, } from "@morpho-org/midnight-sdk";
|
|
2
|
+
import { deepFreeze, getChainAddress } from "@morpho-org/morpho-ts";
|
|
3
|
+
import { encodeFunctionData, maxUint256 } from "viem";
|
|
4
|
+
import { addTransactionMetadata } from "../../helpers/index.js";
|
|
5
|
+
import { validateDeadline, validateMidnightNativeAmount, validateReferralFee, validateUint256Field, } from "../../helpers/validate.js";
|
|
6
|
+
import { validateMidnightMarket } from "../../helpers/validateMidnightMarket.js";
|
|
7
|
+
import { validateTakeableOffers } from "../../helpers/validateTakeableOffers.js";
|
|
8
|
+
import { InputExceedsMaxError, NonPositiveInputError, UnboundedMidnightTakeInputError, } from "../../types/index.js";
|
|
9
|
+
/**
|
|
10
|
+
* Encodes a `MidnightBundlesV2` reduce-only buy that repays `msg.sender`'s debt by taking
|
|
11
|
+
* borrow-side offers, optionally repays the remainder directly, then withdraws collateral.
|
|
12
|
+
*
|
|
13
|
+
* `reduceOnly` is always set, so the buy never opens a lender position. Prefer
|
|
14
|
+
* `client.morpho.midnight(chainId).takeRepayWithdrawCollateral(...)` in app flows so the
|
|
15
|
+
* loan-token approval and the `MidnightBundlesV2` authorization are resolved first.
|
|
16
|
+
*
|
|
17
|
+
* @param params.chainId - Chain id used to resolve `MidnightBundlesV2`.
|
|
18
|
+
* @param params.market - Midnight market traded by every takeable offer.
|
|
19
|
+
* @param params.target - `{ type: "assets", assets, minUnits }` or `{ type: "units", units, maxBuyerAssets }`; `units: maxUint256` targets the sender's whole debt, capped by `maxBuyerAssets` (must be finite; `units: maxUint256` already selects the whole debt).
|
|
20
|
+
* @param params.nativeAmount - Optional native ETH sent as `value`; the bundle wraps it into the
|
|
21
|
+
* chain's wNative and transfers it to `msg.sender` before pulling. It may cover part or all of the
|
|
22
|
+
* maximum wNative amount this call can pull, but not more. Any wrapped native not pulled stays in
|
|
23
|
+
* the sender's wallet as wNative. The wNative approval to `MidnightBundlesV2` is still required
|
|
24
|
+
* for the full pulled amount.
|
|
25
|
+
* @param params.takeableOffers - ABI-ready borrow-side offers returned by the Midnight API; must not be empty.
|
|
26
|
+
* @param params.repayEnabled - Whether the unfilled remainder is repaid directly to Midnight.
|
|
27
|
+
* @param params.collateralWithdrawals - Collateral index and assets per withdrawal; may be empty.
|
|
28
|
+
* @param params.collateralReceiver - Recipient of withdrawn collateral.
|
|
29
|
+
* @param params.maxContinuousFee - Largest market continuous fee accepted, in the unit of the market's
|
|
30
|
+
* onchain `continuousFee` (`Market.continuousFee`): a WAD-scaled per-second rate, so `317097919n`
|
|
31
|
+
* is about 1% per year. The take reverts if the market fee is higher. Pass `maxUint256` for no cap.
|
|
32
|
+
* @param params.deadline - Bounded bundle execution deadline timestamp.
|
|
33
|
+
* @param params.referralFeePct - Optional WAD-scaled referral fee paid out of the pulled assets.
|
|
34
|
+
* @param params.referralFeeRecipient - Referral fee recipient; required with a positive fee.
|
|
35
|
+
* @param params.metadata - Optional analytics metadata appended to calldata.
|
|
36
|
+
* @returns A deep-frozen `Transaction<MidnightTakeRepayWithdrawCollateralAction>` targeting `MidnightBundlesV2`.
|
|
37
|
+
* @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
|
|
38
|
+
* @throws {UnknownAddressError} when the chain has no `midnightBundlesV2` deployment.
|
|
39
|
+
* @throws {ChainIdMismatchError} when the market targets another chain.
|
|
40
|
+
* @throws {MidnightMarketAddressMismatchError} when the market targets another Midnight deployment.
|
|
41
|
+
* @throws {EmptyMidnightTakeableOffersError} when no offers are provided.
|
|
42
|
+
* @throws {MidnightOfferSideMismatchError} when any offer is not borrow-side.
|
|
43
|
+
* @throws {MidnightTakeableOfferMarketMismatchError} when any offer belongs to another market.
|
|
44
|
+
* @throws {UnknownCollateralIndexError} when a withdrawal targets a collateral index not configured on the market.
|
|
45
|
+
* @throws {NonPositiveInputError} when the target amount, `minUnits`, `maxBuyerAssets`, a
|
|
46
|
+
* withdrawal amount or `deadline` is not positive.
|
|
47
|
+
* @throws {NegativeInputError} when `maxContinuousFee`, `referralFeePct` or `nativeAmount` is negative.
|
|
48
|
+
* @throws {ChainWNativeMissingError} when wNative is not configured for the chain.
|
|
49
|
+
* @throws {NativeAmountOnNonWNativeAssetError} when `market.loanToken` is not wNative.
|
|
50
|
+
* @throws {InputExceedsMaxError} when `target.assets` or `target.maxBuyerAssets` is at
|
|
51
|
+
* least `maxUint256`, or a `collateralWithdrawals[].assets` amount, `target.units`,
|
|
52
|
+
* `minUnits`, `maxBuyerAssets`, `maxContinuousFee` or `deadline` exceeds `uint256`, or
|
|
53
|
+
* `nativeAmount` exceeds the maximum wNative amount this call can pull.
|
|
54
|
+
* @throws {UnboundedMidnightTakeInputError} when `deadline` is `maxUint256`.
|
|
55
|
+
* @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
|
|
56
|
+
* @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* import { Time } from "@morpho-org/morpho-ts";
|
|
60
|
+
* import { maxUint256 } from "viem";
|
|
61
|
+
* import { midnightTakeRepayWithdrawCollateral } from "@morpho-org/morpho-sdk";
|
|
62
|
+
*
|
|
63
|
+
* const tx = midnightTakeRepayWithdrawCollateral({
|
|
64
|
+
* chainId: 8453,
|
|
65
|
+
* market: marketData.params,
|
|
66
|
+
* target: { type: "units", units: 1_000_000n, maxBuyerAssets: 990_000n },
|
|
67
|
+
* takeableOffers: quote.data.takeableOffers,
|
|
68
|
+
* repayEnabled: true,
|
|
69
|
+
* collateralWithdrawals: [{ collateralIndex: 0n, assets: maxUint256 }],
|
|
70
|
+
* collateralReceiver: borrower,
|
|
71
|
+
* maxContinuousFee: maxUint256,
|
|
72
|
+
* deadline: Time.timestamp() + 600n,
|
|
73
|
+
* });
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export const midnightTakeRepayWithdrawCollateral = (params) => {
|
|
77
|
+
if (params.target.type === "assets" && params.target.assets <= 0n) {
|
|
78
|
+
throw new NonPositiveInputError("target.assets", params.target.assets);
|
|
79
|
+
}
|
|
80
|
+
// Reject markets from another chain deployment before checking offers against them.
|
|
81
|
+
validateMidnightMarket({ market: params.market, chainId: params.chainId });
|
|
82
|
+
for (const [index, { collateralIndex, assets },] of params.collateralWithdrawals.entries()) {
|
|
83
|
+
if (assets <= 0n) {
|
|
84
|
+
throw new NonPositiveInputError(`collateralWithdrawals[${index}].assets`, assets);
|
|
85
|
+
}
|
|
86
|
+
validateUint256Field(`collateralWithdrawals[${index}].assets`, assets);
|
|
87
|
+
// Throws UnknownCollateralIndexError when the index is not configured on the market.
|
|
88
|
+
MarketUtils.getCollateralByIndex(params.market, collateralIndex);
|
|
89
|
+
}
|
|
90
|
+
const marketId = validateTakeableOffers({
|
|
91
|
+
market: params.market,
|
|
92
|
+
takeableOffers: params.takeableOffers,
|
|
93
|
+
expectedBuy: false,
|
|
94
|
+
});
|
|
95
|
+
const { target } = params;
|
|
96
|
+
if (target.type === "assets") {
|
|
97
|
+
// `assets` bounds the loan assets pulled; it stays finite since there is no
|
|
98
|
+
// "whole debt" sentinel for an assets target.
|
|
99
|
+
if (target.assets >= maxUint256) {
|
|
100
|
+
throw new InputExceedsMaxError({
|
|
101
|
+
field: "target.assets",
|
|
102
|
+
value: target.assets,
|
|
103
|
+
max: maxUint256 - 1n,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
if (target.minUnits <= 0n) {
|
|
107
|
+
throw new NonPositiveInputError("target.minUnits", target.minUnits);
|
|
108
|
+
}
|
|
109
|
+
validateUint256Field("target.minUnits", target.minUnits);
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
if (target.units <= 0n) {
|
|
113
|
+
throw new NonPositiveInputError("target.units", target.units);
|
|
114
|
+
}
|
|
115
|
+
if (target.maxBuyerAssets <= 0n) {
|
|
116
|
+
throw new NonPositiveInputError("target.maxBuyerAssets", target.maxBuyerAssets);
|
|
117
|
+
}
|
|
118
|
+
// `maxBuyerAssets` bounds the loan assets pulled; it stays finite since
|
|
119
|
+
// `units: maxUint256` already selects the whole debt.
|
|
120
|
+
if (target.maxBuyerAssets >= maxUint256) {
|
|
121
|
+
throw new InputExceedsMaxError({
|
|
122
|
+
field: "target.maxBuyerAssets",
|
|
123
|
+
value: target.maxBuyerAssets,
|
|
124
|
+
max: maxUint256 - 1n,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
validateUint256Field("target.units", target.units);
|
|
128
|
+
}
|
|
129
|
+
validateUint256Field("maxContinuousFee", params.maxContinuousFee);
|
|
130
|
+
if (params.deadline === maxUint256) {
|
|
131
|
+
throw new UnboundedMidnightTakeInputError("deadline");
|
|
132
|
+
}
|
|
133
|
+
validateDeadline(params.deadline);
|
|
134
|
+
const { referralFeePct, referralFeeRecipient } = validateReferralFee(params);
|
|
135
|
+
const market = MarketUtils.toStruct(params.market);
|
|
136
|
+
const nativeFunding = validateMidnightNativeAmount({
|
|
137
|
+
chainId: params.chainId,
|
|
138
|
+
nativeAmount: params.nativeAmount,
|
|
139
|
+
pulls: [
|
|
140
|
+
{
|
|
141
|
+
token: market.loanToken,
|
|
142
|
+
assets: target.type === "assets" ? target.assets : target.maxBuyerAssets,
|
|
143
|
+
},
|
|
144
|
+
],
|
|
145
|
+
});
|
|
146
|
+
const args = [
|
|
147
|
+
true, // reduceOnly
|
|
148
|
+
params.repayEnabled, // repayEnabled
|
|
149
|
+
params.takeableOffers, // offerFills
|
|
150
|
+
params.collateralWithdrawals.map(({ collateralIndex, assets }) => ({
|
|
151
|
+
collateralIndex,
|
|
152
|
+
assets,
|
|
153
|
+
})), // collateralWithdrawals
|
|
154
|
+
params.collateralReceiver, // collateralReceiver
|
|
155
|
+
referralFeePct, // referralFeePct
|
|
156
|
+
referralFeeRecipient, // referralFeeRecipient
|
|
157
|
+
params.maxContinuousFee, // maxContinuousFee
|
|
158
|
+
params.deadline, // deadline
|
|
159
|
+
nativeFunding.wrappedNative, // wrappedNative
|
|
160
|
+
];
|
|
161
|
+
let tx = {
|
|
162
|
+
to: getChainAddress(params.chainId, "midnightBundlesV2"),
|
|
163
|
+
value: nativeFunding.value,
|
|
164
|
+
data: target.type === "assets"
|
|
165
|
+
? encodeFunctionData({
|
|
166
|
+
abi: midnightBundlesV2Abi,
|
|
167
|
+
functionName: "midnightBundlesV2BuyWithAssetsTargetAndWithdrawCollateral",
|
|
168
|
+
args: [market, target.assets, target.minUnits, ...args],
|
|
169
|
+
})
|
|
170
|
+
: encodeFunctionData({
|
|
171
|
+
abi: midnightBundlesV2Abi,
|
|
172
|
+
functionName: "midnightBundlesV2BuyWithUnitsTargetAndWithdrawCollateral",
|
|
173
|
+
args: [market, target.units, target.maxBuyerAssets, ...args],
|
|
174
|
+
}),
|
|
175
|
+
};
|
|
176
|
+
if (params.metadata) {
|
|
177
|
+
tx = addTransactionMetadata(tx, params.metadata);
|
|
178
|
+
}
|
|
179
|
+
return deepFreeze({
|
|
180
|
+
...tx,
|
|
181
|
+
action: {
|
|
182
|
+
type: "midnightTakeRepayWithdrawCollateral",
|
|
183
|
+
args: {
|
|
184
|
+
market: marketId,
|
|
185
|
+
target: { ...params.target },
|
|
186
|
+
repayEnabled: params.repayEnabled,
|
|
187
|
+
collateralWithdrawals: params.collateralWithdrawals.map(({ collateralIndex, assets }) => ({ collateralIndex, assets })),
|
|
188
|
+
collateralReceiver: params.collateralReceiver,
|
|
189
|
+
takeableOffers: params.takeableOffers.length,
|
|
190
|
+
maxContinuousFee: params.maxContinuousFee,
|
|
191
|
+
deadline: params.deadline,
|
|
192
|
+
referralFeePct,
|
|
193
|
+
referralFeeRecipient,
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
});
|
|
197
|
+
};
|