@morpho-org/morpho-sdk 6.5.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 +3 -1
- package/lib/cjs/entities/blue/blue.d.ts +10 -6
- package/lib/cjs/entities/blue/blue.js +5 -3
- 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/entities/vaultV2BlueReallocationData.d.ts +11 -3
- package/lib/cjs/entities/vaultV2BlueReallocationData.js +119 -43
- package/lib/cjs/helpers/callback.d.ts +54 -0
- package/lib/cjs/helpers/callback.js +73 -0
- package/lib/cjs/helpers/constant.d.ts +10 -0
- package/lib/cjs/helpers/constant.js +11 -1
- package/lib/cjs/helpers/index.d.ts +3 -2
- package/lib/cjs/helpers/index.js +7 -1
- package/lib/cjs/helpers/signAndVerifyTypedData.d.ts +3 -3
- package/lib/cjs/helpers/signAndVerifyTypedData.js +3 -3
- package/lib/cjs/helpers/utilization.d.ts +13 -0
- package/lib/cjs/helpers/utilization.js +26 -1
- 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/cjs/types/sharedLiquidity.d.ts +17 -0
- 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/blue/blue.d.ts +10 -6
- package/lib/esm/entities/blue/blue.js +5 -3
- 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/entities/vaultV2BlueReallocationData.d.ts +11 -3
- package/lib/esm/entities/vaultV2BlueReallocationData.js +120 -44
- package/lib/esm/helpers/callback.d.ts +54 -0
- package/lib/esm/helpers/callback.js +68 -0
- package/lib/esm/helpers/constant.d.ts +10 -0
- package/lib/esm/helpers/constant.js +10 -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/utilization.d.ts +13 -0
- package/lib/esm/helpers/utilization.js +25 -1
- 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/lib/esm/types/sharedLiquidity.d.ts +17 -0
- package/package.json +9 -8
- 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,68 @@
|
|
|
1
1
|
import { type MarketInput } from "@morpho-org/midnight-sdk";
|
|
2
2
|
import { type Address } from "viem";
|
|
3
|
-
import { type Metadata, type MidnightTakeBorrowAction, type Transaction } from "../../types/index.js";
|
|
4
|
-
import type { MidnightTakeableOffer } from "./types.js";
|
|
3
|
+
import { type Metadata, type MidnightSellTarget, type MidnightTakeBorrowAction, type Transaction } from "../../types/index.js";
|
|
4
|
+
import type { MidnightReferralFeeParams, MidnightTakeableOffer } from "./types.js";
|
|
5
5
|
/** Parameters for encoding a Midnight borrow take from already selected offers. */
|
|
6
|
-
export interface MidnightTakeBorrowParams {
|
|
6
|
+
export interface MidnightTakeBorrowParams extends MidnightReferralFeeParams {
|
|
7
7
|
readonly chainId: number;
|
|
8
8
|
readonly market: MarketInput;
|
|
9
|
-
|
|
10
|
-
readonly
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
readonly deadline: bigint;
|
|
9
|
+
/** Loan assets received with a unit cap, or units sold with a loan-asset floor. */
|
|
10
|
+
readonly target: MidnightSellTarget;
|
|
11
|
+
/** Recipient of the borrowed loan assets. */
|
|
12
|
+
readonly receiver: Address;
|
|
14
13
|
readonly takeableOffers: readonly MidnightTakeableOffer[];
|
|
14
|
+
/** Bounded bundle execution deadline timestamp. */
|
|
15
|
+
readonly deadline: bigint;
|
|
15
16
|
readonly metadata?: Metadata;
|
|
16
17
|
}
|
|
17
18
|
/**
|
|
18
|
-
* Encodes a
|
|
19
|
+
* Encodes a `MidnightBundlesV2` sell that borrows from lend-side offers for `msg.sender`.
|
|
19
20
|
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
* already handled prerequisites.
|
|
21
|
+
* `MidnightBundlesV2` first withdraws as much of the sender's existing credit as the target and
|
|
22
|
+
* market liquidity allow, and fills only the rest from offers: a sender with credit nets it before
|
|
23
|
+
* taking debt.
|
|
24
24
|
*
|
|
25
|
-
*
|
|
25
|
+
* Prefer `client.morpho.midnight(chainId).takeBorrow(...)` in app flows so the
|
|
26
|
+
* `MidnightBundlesV2` authorization is resolved before building the bundle.
|
|
27
|
+
*
|
|
28
|
+
* @param params.chainId - Chain id used to resolve `MidnightBundlesV2`.
|
|
26
29
|
* @param params.market - Midnight market traded by every takeable offer.
|
|
27
|
-
* @param params.
|
|
28
|
-
* @param params.
|
|
29
|
-
* @param params.taker - Borrower address executing the bundle.
|
|
30
|
-
* @param params.deadline - Bundle execution deadline timestamp; pass `maxUint256` explicitly for no expiry.
|
|
30
|
+
* @param params.target - `{ type: "assets", assets, maxUnits }` or `{ type: "units", units, minSellerAssets }`.
|
|
31
|
+
* @param params.receiver - Recipient of the borrowed loan assets.
|
|
31
32
|
* @param params.takeableOffers - ABI-ready lend-side offers returned by the Midnight API.
|
|
33
|
+
* @param params.deadline - Bounded bundle execution deadline timestamp.
|
|
34
|
+
* @param params.referralFeePct - Optional WAD-scaled referral fee taken from the received assets.
|
|
35
|
+
* @param params.referralFeeRecipient - Referral fee recipient; required with a positive fee.
|
|
32
36
|
* @param params.metadata - Optional analytics metadata appended to calldata.
|
|
33
|
-
* @returns A deep-frozen `Transaction<MidnightTakeBorrowAction>` targeting `
|
|
34
|
-
* @throws {
|
|
35
|
-
* @throws {
|
|
37
|
+
* @returns A deep-frozen `Transaction<MidnightTakeBorrowAction>` targeting `MidnightBundlesV2`.
|
|
38
|
+
* @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
|
|
39
|
+
* @throws {UnknownAddressError} when the chain has no `midnightBundlesV2` deployment.
|
|
40
|
+
* @throws {ChainIdMismatchError} when the market targets another chain.
|
|
41
|
+
* @throws {MidnightMarketAddressMismatchError} when the market targets another Midnight deployment.
|
|
36
42
|
* @throws {EmptyMidnightTakeableOffersError} when no offers are provided.
|
|
37
43
|
* @throws {MidnightOfferSideMismatchError} when any offer is not lend-side.
|
|
38
44
|
* @throws {MidnightTakeableOfferMarketMismatchError} when any offer belongs to another market.
|
|
39
|
-
* @throws {
|
|
40
|
-
* @throws {
|
|
45
|
+
* @throws {NonPositiveInputError} when the target amount, `maxUnits`, `minSellerAssets` or `deadline` is not positive.
|
|
46
|
+
* @throws {NegativeInputError} when `referralFeePct` is negative.
|
|
47
|
+
* @throws {InputExceedsMaxError} when `target.assets`, `target.maxUnits`, `target.units`,
|
|
48
|
+
* `target.minSellerAssets` or `deadline` exceeds `uint256`.
|
|
49
|
+
* @throws {UnboundedMidnightTakeInputError} when `target.units`, `target.maxUnits` or
|
|
50
|
+
* `deadline` is `maxUint256`.
|
|
51
|
+
* @throws {MidnightZeroReceiverError} when `receiver` is the zero address.
|
|
52
|
+
* @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
|
|
53
|
+
* @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
|
|
41
54
|
* @example
|
|
42
55
|
* ```ts
|
|
43
|
-
* import {
|
|
56
|
+
* import { Time } from "@morpho-org/morpho-ts";
|
|
44
57
|
* import { midnightTakeBorrow } from "@morpho-org/morpho-sdk";
|
|
45
58
|
*
|
|
46
59
|
* const tx = midnightTakeBorrow({
|
|
47
60
|
* chainId: 8453,
|
|
48
61
|
* market: marketData.params,
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
* taker: borrower,
|
|
62
|
+
* target: { type: "assets", assets: 1_000_000n, maxUnits: 1_100_000n },
|
|
63
|
+
* receiver: borrower,
|
|
52
64
|
* takeableOffers: quote.data.takeableOffers,
|
|
53
|
-
* deadline:
|
|
65
|
+
* deadline: Time.timestamp() + 600n,
|
|
54
66
|
* });
|
|
55
67
|
* ```
|
|
56
68
|
*/
|
|
@@ -5,87 +5,129 @@ const midnight_sdk_1 = require("@morpho-org/midnight-sdk");
|
|
|
5
5
|
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
6
6
|
const viem_1 = require("viem");
|
|
7
7
|
const index_js_1 = require("../../helpers/index.js");
|
|
8
|
+
const validate_js_1 = require("../../helpers/validate.js");
|
|
8
9
|
const validateMidnightMarket_js_1 = require("../../helpers/validateMidnightMarket.js");
|
|
9
10
|
const validateTakeableOffers_js_1 = require("../../helpers/validateTakeableOffers.js");
|
|
10
11
|
const index_js_2 = require("../../types/index.js");
|
|
11
12
|
/**
|
|
12
|
-
* Encodes a
|
|
13
|
+
* Encodes a `MidnightBundlesV2` sell that borrows from lend-side offers for `msg.sender`.
|
|
13
14
|
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
* already handled prerequisites.
|
|
15
|
+
* `MidnightBundlesV2` first withdraws as much of the sender's existing credit as the target and
|
|
16
|
+
* market liquidity allow, and fills only the rest from offers: a sender with credit nets it before
|
|
17
|
+
* taking debt.
|
|
18
18
|
*
|
|
19
|
-
*
|
|
19
|
+
* Prefer `client.morpho.midnight(chainId).takeBorrow(...)` in app flows so the
|
|
20
|
+
* `MidnightBundlesV2` authorization is resolved before building the bundle.
|
|
21
|
+
*
|
|
22
|
+
* @param params.chainId - Chain id used to resolve `MidnightBundlesV2`.
|
|
20
23
|
* @param params.market - Midnight market traded by every takeable offer.
|
|
21
|
-
* @param params.
|
|
22
|
-
* @param params.
|
|
23
|
-
* @param params.taker - Borrower address executing the bundle.
|
|
24
|
-
* @param params.deadline - Bundle execution deadline timestamp; pass `maxUint256` explicitly for no expiry.
|
|
24
|
+
* @param params.target - `{ type: "assets", assets, maxUnits }` or `{ type: "units", units, minSellerAssets }`.
|
|
25
|
+
* @param params.receiver - Recipient of the borrowed loan assets.
|
|
25
26
|
* @param params.takeableOffers - ABI-ready lend-side offers returned by the Midnight API.
|
|
27
|
+
* @param params.deadline - Bounded bundle execution deadline timestamp.
|
|
28
|
+
* @param params.referralFeePct - Optional WAD-scaled referral fee taken from the received assets.
|
|
29
|
+
* @param params.referralFeeRecipient - Referral fee recipient; required with a positive fee.
|
|
26
30
|
* @param params.metadata - Optional analytics metadata appended to calldata.
|
|
27
|
-
* @returns A deep-frozen `Transaction<MidnightTakeBorrowAction>` targeting `
|
|
28
|
-
* @throws {
|
|
29
|
-
* @throws {
|
|
31
|
+
* @returns A deep-frozen `Transaction<MidnightTakeBorrowAction>` 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.
|
|
30
36
|
* @throws {EmptyMidnightTakeableOffersError} when no offers are provided.
|
|
31
37
|
* @throws {MidnightOfferSideMismatchError} when any offer is not lend-side.
|
|
32
38
|
* @throws {MidnightTakeableOfferMarketMismatchError} when any offer belongs to another market.
|
|
33
|
-
* @throws {
|
|
34
|
-
* @throws {
|
|
39
|
+
* @throws {NonPositiveInputError} when the target amount, `maxUnits`, `minSellerAssets` or `deadline` is not positive.
|
|
40
|
+
* @throws {NegativeInputError} when `referralFeePct` is negative.
|
|
41
|
+
* @throws {InputExceedsMaxError} when `target.assets`, `target.maxUnits`, `target.units`,
|
|
42
|
+
* `target.minSellerAssets` or `deadline` exceeds `uint256`.
|
|
43
|
+
* @throws {UnboundedMidnightTakeInputError} when `target.units`, `target.maxUnits` or
|
|
44
|
+
* `deadline` is `maxUint256`.
|
|
45
|
+
* @throws {MidnightZeroReceiverError} when `receiver` is the zero address.
|
|
46
|
+
* @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
|
|
47
|
+
* @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
|
|
35
48
|
* @example
|
|
36
49
|
* ```ts
|
|
37
|
-
* import {
|
|
50
|
+
* import { Time } from "@morpho-org/morpho-ts";
|
|
38
51
|
* import { midnightTakeBorrow } from "@morpho-org/morpho-sdk";
|
|
39
52
|
*
|
|
40
53
|
* const tx = midnightTakeBorrow({
|
|
41
54
|
* chainId: 8453,
|
|
42
55
|
* market: marketData.params,
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
* taker: borrower,
|
|
56
|
+
* target: { type: "assets", assets: 1_000_000n, maxUnits: 1_100_000n },
|
|
57
|
+
* receiver: borrower,
|
|
46
58
|
* takeableOffers: quote.data.takeableOffers,
|
|
47
|
-
* deadline:
|
|
59
|
+
* deadline: Time.timestamp() + 600n,
|
|
48
60
|
* });
|
|
49
61
|
* ```
|
|
50
62
|
*/
|
|
51
63
|
const midnightTakeBorrow = (params) => {
|
|
52
|
-
|
|
53
|
-
throw new index_js_2.NonPositiveInputError("loanAssets", params.loanAssets);
|
|
54
|
-
}
|
|
55
|
-
if (params.maxUnits <= 0n) {
|
|
56
|
-
throw new index_js_2.NonPositiveInputError("maxUnits", params.maxUnits);
|
|
57
|
-
}
|
|
58
|
-
if (params.deadline < 0n) {
|
|
59
|
-
throw new index_js_2.NegativeInputError("deadline", params.deadline);
|
|
60
|
-
}
|
|
61
|
-
// Reject markets from another chain deployment before encoding the bundle.
|
|
64
|
+
// Reject markets from another chain deployment before checking offers against them.
|
|
62
65
|
(0, validateMidnightMarket_js_1.validateMidnightMarket)({ market: params.market, chainId: params.chainId });
|
|
63
66
|
const marketId = (0, validateTakeableOffers_js_1.validateTakeableOffers)({
|
|
64
67
|
market: params.market,
|
|
65
68
|
takeableOffers: params.takeableOffers,
|
|
66
69
|
expectedBuy: true,
|
|
67
70
|
});
|
|
68
|
-
|
|
71
|
+
if ((0, viem_1.isAddressEqual)(params.receiver, viem_1.zeroAddress)) {
|
|
72
|
+
throw new index_js_2.MidnightZeroReceiverError("receiver");
|
|
73
|
+
}
|
|
74
|
+
const { target } = params;
|
|
75
|
+
if (target.type === "assets") {
|
|
76
|
+
if (target.assets <= 0n) {
|
|
77
|
+
throw new index_js_2.NonPositiveInputError("target.assets", target.assets);
|
|
78
|
+
}
|
|
79
|
+
if (target.maxUnits === viem_1.maxUint256) {
|
|
80
|
+
throw new index_js_2.UnboundedMidnightTakeInputError("target.maxUnits");
|
|
81
|
+
}
|
|
82
|
+
if (target.maxUnits <= 0n) {
|
|
83
|
+
throw new index_js_2.NonPositiveInputError("target.maxUnits", target.maxUnits);
|
|
84
|
+
}
|
|
85
|
+
(0, validate_js_1.validateUint256Field)("target.assets", target.assets);
|
|
86
|
+
(0, validate_js_1.validateUint256Field)("target.maxUnits", target.maxUnits);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
if (target.units === viem_1.maxUint256) {
|
|
90
|
+
throw new index_js_2.UnboundedMidnightTakeInputError("target.units");
|
|
91
|
+
}
|
|
92
|
+
if (target.units <= 0n) {
|
|
93
|
+
throw new index_js_2.NonPositiveInputError("target.units", target.units);
|
|
94
|
+
}
|
|
95
|
+
if (target.minSellerAssets <= 0n) {
|
|
96
|
+
throw new index_js_2.NonPositiveInputError("target.minSellerAssets", target.minSellerAssets);
|
|
97
|
+
}
|
|
98
|
+
(0, validate_js_1.validateUint256Field)("target.units", target.units);
|
|
99
|
+
(0, validate_js_1.validateUint256Field)("target.minSellerAssets", target.minSellerAssets);
|
|
100
|
+
}
|
|
101
|
+
if (params.deadline === viem_1.maxUint256) {
|
|
102
|
+
throw new index_js_2.UnboundedMidnightTakeInputError("deadline");
|
|
103
|
+
}
|
|
104
|
+
(0, validate_js_1.validateDeadline)(params.deadline);
|
|
105
|
+
const { referralFeePct, referralFeeRecipient } = (0, validate_js_1.validateReferralFee)(params);
|
|
106
|
+
const market = midnight_sdk_1.MarketUtils.toStruct(params.market);
|
|
107
|
+
const args = [
|
|
108
|
+
false, // reduceOnly
|
|
109
|
+
params.receiver, // receiver
|
|
110
|
+
[], // collateralSupplies
|
|
111
|
+
params.takeableOffers, // offerFills
|
|
112
|
+
referralFeePct, // referralFeePct
|
|
113
|
+
referralFeeRecipient, // referralFeeRecipient
|
|
114
|
+
params.deadline, // deadline
|
|
115
|
+
viem_1.zeroAddress,
|
|
116
|
+
];
|
|
69
117
|
let tx = {
|
|
70
|
-
to:
|
|
118
|
+
to: (0, morpho_ts_1.getChainAddress)(params.chainId, "midnightBundlesV2"),
|
|
71
119
|
value: 0n,
|
|
72
|
-
data:
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
[],
|
|
82
|
-
|
|
83
|
-
0n,
|
|
84
|
-
viem_1.zeroAddress,
|
|
85
|
-
viem_1.maxUint256,
|
|
86
|
-
params.deadline,
|
|
87
|
-
],
|
|
88
|
-
}),
|
|
120
|
+
data: target.type === "assets"
|
|
121
|
+
? (0, viem_1.encodeFunctionData)({
|
|
122
|
+
abi: midnight_sdk_1.midnightBundlesV2Abi,
|
|
123
|
+
functionName: "midnightBundlesV2SupplyCollateralAndSellWithAssetsTarget",
|
|
124
|
+
args: [market, target.assets, target.maxUnits, ...args],
|
|
125
|
+
})
|
|
126
|
+
: (0, viem_1.encodeFunctionData)({
|
|
127
|
+
abi: midnight_sdk_1.midnightBundlesV2Abi,
|
|
128
|
+
functionName: "midnightBundlesV2SupplyCollateralAndSellWithUnitsTarget",
|
|
129
|
+
args: [market, target.units, target.minSellerAssets, ...args],
|
|
130
|
+
}),
|
|
89
131
|
};
|
|
90
132
|
if (params.metadata) {
|
|
91
133
|
tx = (0, index_js_1.addTransactionMetadata)(tx, params.metadata);
|
|
@@ -96,13 +138,12 @@ const midnightTakeBorrow = (params) => {
|
|
|
96
138
|
type: "midnightTakeBorrow",
|
|
97
139
|
args: {
|
|
98
140
|
market: marketId,
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
taker: params.taker,
|
|
102
|
-
receiver: params.taker,
|
|
103
|
-
collateralSupplies: 0,
|
|
141
|
+
target: { ...params.target },
|
|
142
|
+
receiver: params.receiver,
|
|
104
143
|
takeableOffers: params.takeableOffers.length,
|
|
105
144
|
deadline: params.deadline,
|
|
145
|
+
referralFeePct,
|
|
146
|
+
referralFeeRecipient,
|
|
106
147
|
},
|
|
107
148
|
},
|
|
108
149
|
});
|
|
@@ -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
|
*/
|
|
@@ -5,89 +5,145 @@ const midnight_sdk_1 = require("@morpho-org/midnight-sdk");
|
|
|
5
5
|
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
6
6
|
const viem_1 = require("viem");
|
|
7
7
|
const index_js_1 = require("../../helpers/index.js");
|
|
8
|
+
const validate_js_1 = require("../../helpers/validate.js");
|
|
8
9
|
const validateMidnightMarket_js_1 = require("../../helpers/validateMidnightMarket.js");
|
|
9
10
|
const validateTakeableOffers_js_1 = require("../../helpers/validateTakeableOffers.js");
|
|
10
11
|
const index_js_2 = require("../../types/index.js");
|
|
11
|
-
const types_js_1 = require("./types.js");
|
|
12
12
|
/**
|
|
13
|
-
* Encodes a
|
|
13
|
+
* Encodes a `MidnightBundlesV2` buy that lends into borrow-side offers for `msg.sender`.
|
|
14
14
|
*
|
|
15
|
-
* Prefer `client.morpho.midnight(chainId).takeLend(...)` in app flows so
|
|
16
|
-
*
|
|
17
|
-
* low-level builder only after the Midnight API has returned takeable offers
|
|
18
|
-
* and the caller has already handled prerequisites.
|
|
15
|
+
* Prefer `client.morpho.midnight(chainId).takeLend(...)` in app flows so the loan-token approval
|
|
16
|
+
* and the `MidnightBundlesV2` authorization are resolved before building the bundle.
|
|
19
17
|
*
|
|
20
|
-
* @param params.chainId - Chain id used to resolve `
|
|
18
|
+
* @param params.chainId - Chain id used to resolve `MidnightBundlesV2`.
|
|
21
19
|
* @param params.market - Midnight market traded by every takeable offer.
|
|
22
|
-
* @param params.
|
|
23
|
-
* @param params.
|
|
24
|
-
*
|
|
20
|
+
* @param params.target - `{ type: "assets", assets, minUnits }` or `{ type: "units", units, maxBuyerAssets }`.
|
|
21
|
+
* @param params.nativeAmount - Optional native ETH sent as `value`; the bundle wraps it into the
|
|
22
|
+
* chain's wNative and transfers it to `msg.sender` before pulling. It may cover part or all of the
|
|
23
|
+
* maximum wNative amount this call can pull, but not more. Any wrapped native not pulled stays in
|
|
24
|
+
* the sender's wallet as wNative. The wNative approval to `MidnightBundlesV2` is still required
|
|
25
|
+
* for the full pulled amount.
|
|
25
26
|
* @param params.takeableOffers - ABI-ready borrow-side offers returned by the Midnight API.
|
|
26
|
-
* @param params.
|
|
27
|
+
* @param params.maxContinuousFee - Largest market continuous fee accepted, in the unit of the market's
|
|
28
|
+
* onchain `continuousFee` (`Market.continuousFee`): a WAD-scaled per-second rate, so `317097919n`
|
|
29
|
+
* is about 1% per year. The take reverts if the market fee is higher. Pass `maxUint256` for no cap.
|
|
30
|
+
* @param params.deadline - Bounded bundle execution deadline timestamp.
|
|
31
|
+
* @param params.referralFeePct - Optional WAD-scaled referral fee paid out of the pulled assets.
|
|
32
|
+
* @param params.referralFeeRecipient - Referral fee recipient; required with a positive fee.
|
|
27
33
|
* @param params.metadata - Optional analytics metadata appended to calldata.
|
|
28
|
-
* @returns A deep-frozen `Transaction<MidnightTakeLendAction>` targeting `
|
|
29
|
-
* @throws {
|
|
30
|
-
* @throws {
|
|
34
|
+
* @returns A deep-frozen `Transaction<MidnightTakeLendAction>` targeting `MidnightBundlesV2`.
|
|
35
|
+
* @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
|
|
36
|
+
* @throws {UnknownAddressError} when the chain has no `midnightBundlesV2` deployment.
|
|
37
|
+
* @throws {ChainIdMismatchError} when the market targets another chain.
|
|
38
|
+
* @throws {MidnightMarketAddressMismatchError} when the market targets another Midnight deployment.
|
|
31
39
|
* @throws {EmptyMidnightTakeableOffersError} when no offers are provided.
|
|
32
40
|
* @throws {MidnightOfferSideMismatchError} when any offer is not borrow-side.
|
|
33
41
|
* @throws {MidnightTakeableOfferMarketMismatchError} when any offer belongs to another market.
|
|
34
|
-
* @throws {
|
|
35
|
-
* @throws {
|
|
42
|
+
* @throws {NonPositiveInputError} when the target amount, `minUnits`, `maxBuyerAssets` or `deadline` is not positive.
|
|
43
|
+
* @throws {NegativeInputError} when `maxContinuousFee`, `referralFeePct` or `nativeAmount` is negative.
|
|
44
|
+
* @throws {ChainWNativeMissingError} when wNative is not configured for the chain.
|
|
45
|
+
* @throws {NativeAmountOnNonWNativeAssetError} when `market.loanToken` is not wNative.
|
|
46
|
+
* @throws {InputExceedsMaxError} when `target.assets`, `target.units`, `minUnits`, `maxBuyerAssets`,
|
|
47
|
+
* `maxContinuousFee` or `deadline` exceeds `uint256`, or `nativeAmount` exceeds the maximum
|
|
48
|
+
* wNative amount this call can pull.
|
|
49
|
+
* @throws {UnboundedMidnightTakeInputError} when `target.units`, `target.maxBuyerAssets` or
|
|
50
|
+
* `deadline` is `maxUint256`.
|
|
51
|
+
* @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
|
|
52
|
+
* @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
|
|
36
53
|
* @example
|
|
37
54
|
* ```ts
|
|
55
|
+
* import { Time } from "@morpho-org/morpho-ts";
|
|
38
56
|
* import { maxUint256 } from "viem";
|
|
39
57
|
* import { midnightTakeLend } from "@morpho-org/morpho-sdk";
|
|
40
58
|
*
|
|
41
59
|
* const tx = midnightTakeLend({
|
|
42
60
|
* chainId: 8453,
|
|
43
61
|
* market: marketData.params,
|
|
44
|
-
* assets: 1_000_000n,
|
|
45
|
-
* minUnits: 900_000n,
|
|
46
|
-
* taker: lender,
|
|
62
|
+
* target: { type: "assets", assets: 1_000_000n, minUnits: 900_000n },
|
|
47
63
|
* takeableOffers: quote.data.takeableOffers,
|
|
48
|
-
*
|
|
64
|
+
* maxContinuousFee: maxUint256,
|
|
65
|
+
* deadline: Time.timestamp() + 600n,
|
|
49
66
|
* });
|
|
50
67
|
* ```
|
|
51
68
|
*/
|
|
52
69
|
const midnightTakeLend = (params) => {
|
|
53
|
-
|
|
54
|
-
throw new index_js_2.NonPositiveInputError("assets", params.assets);
|
|
55
|
-
}
|
|
56
|
-
if (params.minUnits < 0n) {
|
|
57
|
-
throw new index_js_2.NegativeInputError("minUnits", params.minUnits);
|
|
58
|
-
}
|
|
59
|
-
if (params.deadline < 0n) {
|
|
60
|
-
throw new index_js_2.NegativeInputError("deadline", params.deadline);
|
|
61
|
-
}
|
|
62
|
-
// Reject markets from another chain deployment before encoding the bundle.
|
|
70
|
+
// Reject markets from another chain deployment before checking offers against them.
|
|
63
71
|
(0, validateMidnightMarket_js_1.validateMidnightMarket)({ market: params.market, chainId: params.chainId });
|
|
64
72
|
const marketId = (0, validateTakeableOffers_js_1.validateTakeableOffers)({
|
|
65
73
|
market: params.market,
|
|
66
74
|
takeableOffers: params.takeableOffers,
|
|
67
75
|
expectedBuy: false,
|
|
68
76
|
});
|
|
69
|
-
const
|
|
77
|
+
const { target } = params;
|
|
78
|
+
if (target.type === "assets") {
|
|
79
|
+
if (target.assets <= 0n) {
|
|
80
|
+
throw new index_js_2.NonPositiveInputError("target.assets", target.assets);
|
|
81
|
+
}
|
|
82
|
+
if (target.minUnits <= 0n) {
|
|
83
|
+
throw new index_js_2.NonPositiveInputError("target.minUnits", target.minUnits);
|
|
84
|
+
}
|
|
85
|
+
(0, validate_js_1.validateUint256Field)("target.assets", target.assets);
|
|
86
|
+
(0, validate_js_1.validateUint256Field)("target.minUnits", target.minUnits);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
if (target.units === viem_1.maxUint256) {
|
|
90
|
+
throw new index_js_2.UnboundedMidnightTakeInputError("target.units");
|
|
91
|
+
}
|
|
92
|
+
if (target.maxBuyerAssets === viem_1.maxUint256) {
|
|
93
|
+
throw new index_js_2.UnboundedMidnightTakeInputError("target.maxBuyerAssets");
|
|
94
|
+
}
|
|
95
|
+
if (target.units <= 0n) {
|
|
96
|
+
throw new index_js_2.NonPositiveInputError("target.units", target.units);
|
|
97
|
+
}
|
|
98
|
+
if (target.maxBuyerAssets <= 0n) {
|
|
99
|
+
throw new index_js_2.NonPositiveInputError("target.maxBuyerAssets", target.maxBuyerAssets);
|
|
100
|
+
}
|
|
101
|
+
(0, validate_js_1.validateUint256Field)("target.units", target.units);
|
|
102
|
+
(0, validate_js_1.validateUint256Field)("target.maxBuyerAssets", target.maxBuyerAssets);
|
|
103
|
+
}
|
|
104
|
+
(0, validate_js_1.validateUint256Field)("maxContinuousFee", params.maxContinuousFee);
|
|
105
|
+
if (params.deadline === viem_1.maxUint256) {
|
|
106
|
+
throw new index_js_2.UnboundedMidnightTakeInputError("deadline");
|
|
107
|
+
}
|
|
108
|
+
(0, validate_js_1.validateDeadline)(params.deadline);
|
|
109
|
+
const { referralFeePct, referralFeeRecipient } = (0, validate_js_1.validateReferralFee)(params);
|
|
110
|
+
const market = midnight_sdk_1.MarketUtils.toStruct(params.market);
|
|
111
|
+
const nativeFunding = (0, validate_js_1.validateMidnightNativeAmount)({
|
|
112
|
+
chainId: params.chainId,
|
|
113
|
+
nativeAmount: params.nativeAmount,
|
|
114
|
+
pulls: [
|
|
115
|
+
{
|
|
116
|
+
token: market.loanToken,
|
|
117
|
+
assets: target.type === "assets" ? target.assets : target.maxBuyerAssets,
|
|
118
|
+
},
|
|
119
|
+
],
|
|
120
|
+
});
|
|
121
|
+
const args = [
|
|
122
|
+
false, // reduceOnly
|
|
123
|
+
false, // repayEnabled
|
|
124
|
+
params.takeableOffers, // offerFills
|
|
125
|
+
[], // collateralWithdrawals
|
|
126
|
+
viem_1.zeroAddress, // collateralReceiver
|
|
127
|
+
referralFeePct, // referralFeePct
|
|
128
|
+
referralFeeRecipient, // referralFeeRecipient
|
|
129
|
+
params.maxContinuousFee, // maxContinuousFee
|
|
130
|
+
params.deadline, // deadline
|
|
131
|
+
nativeFunding.wrappedNative, // wrappedNative
|
|
132
|
+
];
|
|
70
133
|
let tx = {
|
|
71
|
-
to:
|
|
72
|
-
value:
|
|
73
|
-
data:
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
viem_1.zeroAddress,
|
|
85
|
-
0n,
|
|
86
|
-
viem_1.zeroAddress,
|
|
87
|
-
viem_1.maxUint256,
|
|
88
|
-
params.deadline,
|
|
89
|
-
],
|
|
90
|
-
}),
|
|
134
|
+
to: (0, morpho_ts_1.getChainAddress)(params.chainId, "midnightBundlesV2"),
|
|
135
|
+
value: nativeFunding.value,
|
|
136
|
+
data: target.type === "assets"
|
|
137
|
+
? (0, viem_1.encodeFunctionData)({
|
|
138
|
+
abi: midnight_sdk_1.midnightBundlesV2Abi,
|
|
139
|
+
functionName: "midnightBundlesV2BuyWithAssetsTargetAndWithdrawCollateral",
|
|
140
|
+
args: [market, target.assets, target.minUnits, ...args],
|
|
141
|
+
})
|
|
142
|
+
: (0, viem_1.encodeFunctionData)({
|
|
143
|
+
abi: midnight_sdk_1.midnightBundlesV2Abi,
|
|
144
|
+
functionName: "midnightBundlesV2BuyWithUnitsTargetAndWithdrawCollateral",
|
|
145
|
+
args: [market, target.units, target.maxBuyerAssets, ...args],
|
|
146
|
+
}),
|
|
91
147
|
};
|
|
92
148
|
if (params.metadata) {
|
|
93
149
|
tx = (0, index_js_1.addTransactionMetadata)(tx, params.metadata);
|
|
@@ -98,11 +154,12 @@ const midnightTakeLend = (params) => {
|
|
|
98
154
|
type: "midnightTakeLend",
|
|
99
155
|
args: {
|
|
100
156
|
market: marketId,
|
|
101
|
-
|
|
102
|
-
minUnits: params.minUnits,
|
|
103
|
-
taker: params.taker,
|
|
157
|
+
target: { ...params.target },
|
|
104
158
|
takeableOffers: params.takeableOffers.length,
|
|
159
|
+
maxContinuousFee: params.maxContinuousFee,
|
|
105
160
|
deadline: params.deadline,
|
|
161
|
+
referralFeePct,
|
|
162
|
+
referralFeeRecipient,
|
|
106
163
|
},
|
|
107
164
|
},
|
|
108
165
|
});
|