@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,69 @@
|
|
|
1
|
+
import { type MarketInput } from "@morpho-org/midnight-sdk";
|
|
2
|
+
import { type Address } from "viem";
|
|
3
|
+
import { type Metadata, type MidnightSellTarget, type MidnightTakeWithdrawAction, type Transaction } from "../../types/index.js";
|
|
4
|
+
import type { MidnightReferralFeeParams, MidnightTakeableOffer } from "./types.js";
|
|
5
|
+
/** Parameters for encoding a Midnight credit withdrawal that redeems first, then sells to offers. */
|
|
6
|
+
export interface MidnightTakeWithdrawParams extends MidnightReferralFeeParams {
|
|
7
|
+
readonly chainId: number;
|
|
8
|
+
readonly market: MarketInput;
|
|
9
|
+
/** Loan assets received with a credit-unit cap, or credit units sold with a loan-asset floor. */
|
|
10
|
+
readonly target: MidnightSellTarget;
|
|
11
|
+
/** Recipient of the withdrawn loan assets. */
|
|
12
|
+
readonly receiver: Address;
|
|
13
|
+
readonly takeableOffers: readonly MidnightTakeableOffer[];
|
|
14
|
+
/** Bounded bundle execution deadline timestamp. */
|
|
15
|
+
readonly deadline: bigint;
|
|
16
|
+
readonly metadata?: Metadata;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Encodes a reduce-only `MidnightBundlesV2` sell that withdraws `msg.sender`'s credit.
|
|
20
|
+
*
|
|
21
|
+
* `MidnightBundlesV2` first redeems as much credit as the target and market liquidity allow, then
|
|
22
|
+
* sells the rest of the target to lend-side offers. `reduceOnly` is always set, so the sell fails
|
|
23
|
+
* instead of opening debt when the sender's credit does not cover the target.
|
|
24
|
+
*
|
|
25
|
+
* Prefer `client.morpho.midnight(chainId).takeWithdraw(...)` 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`.
|
|
29
|
+
* @param params.market - Midnight market traded by every takeable offer.
|
|
30
|
+
* @param params.target - `{ type: "assets", assets, maxUnits }` or `{ type: "units", units, minSellerAssets }`.
|
|
31
|
+
* @param params.receiver - Recipient of the withdrawn loan assets.
|
|
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.
|
|
36
|
+
* @param params.metadata - Optional analytics metadata appended to calldata.
|
|
37
|
+
* @returns A deep-frozen `Transaction<MidnightTakeWithdrawAction>` 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.
|
|
42
|
+
* @throws {EmptyMidnightTakeableOffersError} when no offers are provided.
|
|
43
|
+
* @throws {MidnightOfferSideMismatchError} when any offer is not lend-side.
|
|
44
|
+
* @throws {MidnightTakeableOfferMarketMismatchError} when any offer belongs to another market.
|
|
45
|
+
* @throws {NonPositiveInputError} when the target amount, `maxUnits`, `minSellerAssets` or
|
|
46
|
+
* `deadline` is not positive.
|
|
47
|
+
* @throws {NegativeInputError} when `referralFeePct` is negative.
|
|
48
|
+
* @throws {InputExceedsMaxError} when `target.assets`, `target.maxUnits`, `target.units`,
|
|
49
|
+
* `target.minSellerAssets` or `deadline` exceeds `uint256`.
|
|
50
|
+
* @throws {UnboundedMidnightTakeInputError} when `target.units`, `target.maxUnits` or `deadline`
|
|
51
|
+
* is `maxUint256`.
|
|
52
|
+
* @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
|
|
53
|
+
* @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* import { Time } from "@morpho-org/morpho-ts";
|
|
57
|
+
* import { midnightTakeWithdraw } from "@morpho-org/morpho-sdk";
|
|
58
|
+
*
|
|
59
|
+
* const tx = midnightTakeWithdraw({
|
|
60
|
+
* chainId: 8453,
|
|
61
|
+
* market: marketData.params,
|
|
62
|
+
* target: { type: "units", units: credit, minSellerAssets: 990_000n },
|
|
63
|
+
* receiver: lender,
|
|
64
|
+
* takeableOffers: quote.data.takeableOffers,
|
|
65
|
+
* deadline: Time.timestamp() + 600n,
|
|
66
|
+
* });
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
export declare const midnightTakeWithdraw: (params: MidnightTakeWithdrawParams) => Readonly<Transaction<MidnightTakeWithdrawAction>>;
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { MarketUtils, midnightBundlesV2Abi, } from "@morpho-org/midnight-sdk";
|
|
2
|
+
import { deepFreeze, getChainAddress } from "@morpho-org/morpho-ts";
|
|
3
|
+
import { encodeFunctionData, maxUint256, zeroAddress, } from "viem";
|
|
4
|
+
import { addTransactionMetadata } from "../../helpers/index.js";
|
|
5
|
+
import { validateDeadline, validateReferralFee, validateUint256Field, } from "../../helpers/validate.js";
|
|
6
|
+
import { validateMidnightMarket } from "../../helpers/validateMidnightMarket.js";
|
|
7
|
+
import { validateTakeableOffers } from "../../helpers/validateTakeableOffers.js";
|
|
8
|
+
import { NonPositiveInputError, UnboundedMidnightTakeInputError, } from "../../types/index.js";
|
|
9
|
+
const validateParams = (params) => {
|
|
10
|
+
// Reject markets from another chain deployment before checking offers against them.
|
|
11
|
+
validateMidnightMarket({ market: params.market, chainId: params.chainId });
|
|
12
|
+
const marketId = validateTakeableOffers({
|
|
13
|
+
market: params.market,
|
|
14
|
+
takeableOffers: params.takeableOffers,
|
|
15
|
+
expectedBuy: true,
|
|
16
|
+
});
|
|
17
|
+
const { target } = params;
|
|
18
|
+
if (target.type === "assets") {
|
|
19
|
+
if (target.assets <= 0n) {
|
|
20
|
+
throw new NonPositiveInputError("target.assets", target.assets);
|
|
21
|
+
}
|
|
22
|
+
if (target.maxUnits === maxUint256) {
|
|
23
|
+
throw new UnboundedMidnightTakeInputError("target.maxUnits");
|
|
24
|
+
}
|
|
25
|
+
if (target.maxUnits <= 0n) {
|
|
26
|
+
throw new NonPositiveInputError("target.maxUnits", target.maxUnits);
|
|
27
|
+
}
|
|
28
|
+
validateUint256Field("target.assets", target.assets);
|
|
29
|
+
validateUint256Field("target.maxUnits", target.maxUnits);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
if (target.units === maxUint256) {
|
|
33
|
+
throw new UnboundedMidnightTakeInputError("target.units");
|
|
34
|
+
}
|
|
35
|
+
if (target.units <= 0n) {
|
|
36
|
+
throw new NonPositiveInputError("target.units", target.units);
|
|
37
|
+
}
|
|
38
|
+
if (target.minSellerAssets <= 0n) {
|
|
39
|
+
throw new NonPositiveInputError("target.minSellerAssets", target.minSellerAssets);
|
|
40
|
+
}
|
|
41
|
+
validateUint256Field("target.units", target.units);
|
|
42
|
+
validateUint256Field("target.minSellerAssets", target.minSellerAssets);
|
|
43
|
+
}
|
|
44
|
+
if (params.deadline === maxUint256) {
|
|
45
|
+
throw new UnboundedMidnightTakeInputError("deadline");
|
|
46
|
+
}
|
|
47
|
+
validateDeadline(params.deadline);
|
|
48
|
+
const { referralFeePct, referralFeeRecipient } = validateReferralFee(params);
|
|
49
|
+
return { marketId, referralFeePct, referralFeeRecipient };
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Encodes a reduce-only `MidnightBundlesV2` sell that withdraws `msg.sender`'s credit.
|
|
53
|
+
*
|
|
54
|
+
* `MidnightBundlesV2` first redeems as much credit as the target and market liquidity allow, then
|
|
55
|
+
* sells the rest of the target to lend-side offers. `reduceOnly` is always set, so the sell fails
|
|
56
|
+
* instead of opening debt when the sender's credit does not cover the target.
|
|
57
|
+
*
|
|
58
|
+
* Prefer `client.morpho.midnight(chainId).takeWithdraw(...)` in app flows so the
|
|
59
|
+
* `MidnightBundlesV2` authorization is resolved before building the bundle.
|
|
60
|
+
*
|
|
61
|
+
* @param params.chainId - Chain id used to resolve `MidnightBundlesV2`.
|
|
62
|
+
* @param params.market - Midnight market traded by every takeable offer.
|
|
63
|
+
* @param params.target - `{ type: "assets", assets, maxUnits }` or `{ type: "units", units, minSellerAssets }`.
|
|
64
|
+
* @param params.receiver - Recipient of the withdrawn loan assets.
|
|
65
|
+
* @param params.takeableOffers - ABI-ready lend-side offers returned by the Midnight API.
|
|
66
|
+
* @param params.deadline - Bounded bundle execution deadline timestamp.
|
|
67
|
+
* @param params.referralFeePct - Optional WAD-scaled referral fee taken from the received assets.
|
|
68
|
+
* @param params.referralFeeRecipient - Referral fee recipient; required with a positive fee.
|
|
69
|
+
* @param params.metadata - Optional analytics metadata appended to calldata.
|
|
70
|
+
* @returns A deep-frozen `Transaction<MidnightTakeWithdrawAction>` targeting `MidnightBundlesV2`.
|
|
71
|
+
* @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
|
|
72
|
+
* @throws {UnknownAddressError} when the chain has no `midnightBundlesV2` deployment.
|
|
73
|
+
* @throws {ChainIdMismatchError} when the market targets another chain.
|
|
74
|
+
* @throws {MidnightMarketAddressMismatchError} when the market targets another Midnight deployment.
|
|
75
|
+
* @throws {EmptyMidnightTakeableOffersError} when no offers are provided.
|
|
76
|
+
* @throws {MidnightOfferSideMismatchError} when any offer is not lend-side.
|
|
77
|
+
* @throws {MidnightTakeableOfferMarketMismatchError} when any offer belongs to another market.
|
|
78
|
+
* @throws {NonPositiveInputError} when the target amount, `maxUnits`, `minSellerAssets` or
|
|
79
|
+
* `deadline` is not positive.
|
|
80
|
+
* @throws {NegativeInputError} when `referralFeePct` is negative.
|
|
81
|
+
* @throws {InputExceedsMaxError} when `target.assets`, `target.maxUnits`, `target.units`,
|
|
82
|
+
* `target.minSellerAssets` or `deadline` exceeds `uint256`.
|
|
83
|
+
* @throws {UnboundedMidnightTakeInputError} when `target.units`, `target.maxUnits` or `deadline`
|
|
84
|
+
* is `maxUint256`.
|
|
85
|
+
* @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
|
|
86
|
+
* @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* import { Time } from "@morpho-org/morpho-ts";
|
|
90
|
+
* import { midnightTakeWithdraw } from "@morpho-org/morpho-sdk";
|
|
91
|
+
*
|
|
92
|
+
* const tx = midnightTakeWithdraw({
|
|
93
|
+
* chainId: 8453,
|
|
94
|
+
* market: marketData.params,
|
|
95
|
+
* target: { type: "units", units: credit, minSellerAssets: 990_000n },
|
|
96
|
+
* receiver: lender,
|
|
97
|
+
* takeableOffers: quote.data.takeableOffers,
|
|
98
|
+
* deadline: Time.timestamp() + 600n,
|
|
99
|
+
* });
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
export const midnightTakeWithdraw = (params) => {
|
|
103
|
+
const { marketId, referralFeePct, referralFeeRecipient } = validateParams(params);
|
|
104
|
+
const { target } = params;
|
|
105
|
+
const market = MarketUtils.toStruct(params.market);
|
|
106
|
+
const args = [
|
|
107
|
+
true, // reduceOnly
|
|
108
|
+
params.receiver, // receiver
|
|
109
|
+
[], // collateralSupplies
|
|
110
|
+
params.takeableOffers, // offerFills
|
|
111
|
+
referralFeePct, // referralFeePct
|
|
112
|
+
referralFeeRecipient, // referralFeeRecipient
|
|
113
|
+
params.deadline, // deadline
|
|
114
|
+
zeroAddress, // wrappedNative
|
|
115
|
+
];
|
|
116
|
+
let tx = {
|
|
117
|
+
to: getChainAddress(params.chainId, "midnightBundlesV2"),
|
|
118
|
+
value: 0n,
|
|
119
|
+
data: target.type === "assets"
|
|
120
|
+
? encodeFunctionData({
|
|
121
|
+
abi: midnightBundlesV2Abi,
|
|
122
|
+
functionName: "midnightBundlesV2SupplyCollateralAndSellWithAssetsTarget",
|
|
123
|
+
args: [market, target.assets, target.maxUnits, ...args],
|
|
124
|
+
})
|
|
125
|
+
: encodeFunctionData({
|
|
126
|
+
abi: midnightBundlesV2Abi,
|
|
127
|
+
functionName: "midnightBundlesV2SupplyCollateralAndSellWithUnitsTarget",
|
|
128
|
+
args: [market, target.units, target.minSellerAssets, ...args],
|
|
129
|
+
}),
|
|
130
|
+
};
|
|
131
|
+
if (params.metadata) {
|
|
132
|
+
tx = addTransactionMetadata(tx, params.metadata);
|
|
133
|
+
}
|
|
134
|
+
return deepFreeze({
|
|
135
|
+
...tx,
|
|
136
|
+
action: {
|
|
137
|
+
type: "midnightTakeWithdraw",
|
|
138
|
+
args: {
|
|
139
|
+
market: marketId,
|
|
140
|
+
target: { ...params.target },
|
|
141
|
+
receiver: params.receiver,
|
|
142
|
+
takeableOffers: params.takeableOffers.length,
|
|
143
|
+
deadline: params.deadline,
|
|
144
|
+
referralFeePct,
|
|
145
|
+
referralFeeRecipient,
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
});
|
|
149
|
+
};
|
|
@@ -1,30 +1,5 @@
|
|
|
1
1
|
import type { OfferStruct } from "@morpho-org/midnight-sdk";
|
|
2
|
-
import type { Hex } from "viem";
|
|
3
|
-
/**
|
|
4
|
-
* MidnightBundles token permit selector.
|
|
5
|
-
*
|
|
6
|
-
* The first Midnight action implementation only emits `None`; `ERC2612` and
|
|
7
|
-
* `Permit2` are reserved for future token-signature bundle support.
|
|
8
|
-
*/
|
|
9
|
-
export declare enum PermitKind {
|
|
10
|
-
None = 0,
|
|
11
|
-
ERC2612 = 1,
|
|
12
|
-
Permit2 = 2
|
|
13
|
-
}
|
|
14
|
-
/** ABI-ready token permit metadata consumed by MidnightBundles. */
|
|
15
|
-
export type MidnightTokenPermit = {
|
|
16
|
-
readonly kind: PermitKind.None;
|
|
17
|
-
readonly data: "0x";
|
|
18
|
-
} | {
|
|
19
|
-
readonly kind: PermitKind.ERC2612 | PermitKind.Permit2;
|
|
20
|
-
readonly data: Hex;
|
|
21
|
-
};
|
|
22
|
-
/** Protocol-shaped collateral supply used by Midnight bundle calls. */
|
|
23
|
-
export interface MidnightCollateralSupply {
|
|
24
|
-
readonly collateralIndex: bigint;
|
|
25
|
-
readonly assets: bigint;
|
|
26
|
-
readonly permit: MidnightTokenPermit;
|
|
27
|
-
}
|
|
2
|
+
import type { Address, Hex } from "viem";
|
|
28
3
|
/**
|
|
29
4
|
* ABI-ready Midnight takeable offer returned by quote/takeable-offer APIs.
|
|
30
5
|
*
|
|
@@ -37,3 +12,9 @@ export interface MidnightTakeableOffer {
|
|
|
37
12
|
readonly offer: OfferStruct;
|
|
38
13
|
readonly ratifierData: Hex;
|
|
39
14
|
}
|
|
15
|
+
/** Optional referral fee shared by the Midnight taker builders. */
|
|
16
|
+
export interface MidnightReferralFeeParams {
|
|
17
|
+
/** WAD-scaled share of the filled loan assets paid to `referralFeeRecipient`. Defaults to `0n`. */
|
|
18
|
+
readonly referralFeePct?: bigint;
|
|
19
|
+
readonly referralFeeRecipient?: Address;
|
|
20
|
+
}
|
|
@@ -1,12 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* MidnightBundles token permit selector.
|
|
3
|
-
*
|
|
4
|
-
* The first Midnight action implementation only emits `None`; `ERC2612` and
|
|
5
|
-
* `Permit2` are reserved for future token-signature bundle support.
|
|
6
|
-
*/
|
|
7
|
-
export var PermitKind;
|
|
8
|
-
(function (PermitKind) {
|
|
9
|
-
PermitKind[PermitKind["None"] = 0] = "None";
|
|
10
|
-
PermitKind[PermitKind["ERC2612"] = 1] = "ERC2612";
|
|
11
|
-
PermitKind[PermitKind["Permit2"] = 2] = "Permit2";
|
|
12
|
-
})(PermitKind || (PermitKind = {}));
|
|
1
|
+
export {};
|
|
@@ -16,7 +16,8 @@ interface EncodeErc20ApprovalParams {
|
|
|
16
16
|
* @param params - Encoding parameters.
|
|
17
17
|
* @param params.token - ERC-20 token address to approve.
|
|
18
18
|
* @param params.spender - Address granted the allowance. Must be Permit2, Midnight,
|
|
19
|
-
*
|
|
19
|
+
* MidnightBundlesV2, VaultExitBundlesV1, VaultBundlesV1, or BlueBundlesV1 for
|
|
20
|
+
* the chain.
|
|
20
21
|
* @param params.amount - Allowance amount before per-token cap.
|
|
21
22
|
* @param params.chainId - The chain the transaction targets (used to resolve supported spenders and the per-token cap).
|
|
22
23
|
* @returns A deep-frozen `Transaction<ERC20ApprovalAction>` with the capped approval amount.
|
|
@@ -12,7 +12,8 @@ import { validateRequirementSpender } from "../../../helpers/validateRequirement
|
|
|
12
12
|
* @param params - Encoding parameters.
|
|
13
13
|
* @param params.token - ERC-20 token address to approve.
|
|
14
14
|
* @param params.spender - Address granted the allowance. Must be Permit2, Midnight,
|
|
15
|
-
*
|
|
15
|
+
* MidnightBundlesV2, VaultExitBundlesV1, VaultBundlesV1, or BlueBundlesV1 for
|
|
16
|
+
* the chain.
|
|
16
17
|
* @param params.amount - Allowance amount before per-token cap.
|
|
17
18
|
* @param params.chainId - The chain the transaction targets (used to resolve supported spenders and the per-token cap).
|
|
18
19
|
* @returns A deep-frozen `Transaction<ERC20ApprovalAction>` with the capped approval amount.
|
|
@@ -40,7 +41,7 @@ export const encodeErc20Approval = (params) => {
|
|
|
40
41
|
allowed: [
|
|
41
42
|
"permit2",
|
|
42
43
|
"midnight",
|
|
43
|
-
"
|
|
44
|
+
"midnightBundlesV2",
|
|
44
45
|
"vaultExitBundlesV1",
|
|
45
46
|
"vaultBundlesV1",
|
|
46
47
|
"blueBundlesV1",
|
|
@@ -27,8 +27,8 @@ interface EncodeErc20PermitParams {
|
|
|
27
27
|
* @param params - Permit encoding parameters.
|
|
28
28
|
* @param params.token - ERC-20 token address (must support EIP-2612).
|
|
29
29
|
* @param params.owner - Account that owns the tokens and signs the permit.
|
|
30
|
-
* @param params.spender - Permit spender. Must be
|
|
31
|
-
*
|
|
30
|
+
* @param params.spender - Permit spender. Must be VaultBundlesV1 or BlueBundlesV1 for the
|
|
31
|
+
* chain.
|
|
32
32
|
* @param params.amount - Permit allowance amount.
|
|
33
33
|
* @param params.chainId - Target chain id.
|
|
34
34
|
* @param params.nonce - The owner's current EIP-2612 nonce on `token`.
|
|
@@ -38,8 +38,8 @@ interface EncodeErc20PermitParams {
|
|
|
38
38
|
* `sign(client, userAddress)` produces the deep-frozen signature.
|
|
39
39
|
* @throws {ChainIdMismatchError} when `viemClient.chain?.id !== params.chainId`.
|
|
40
40
|
* @throws {UnsupportedChainIdError} when `chainId` is absent from the address registry.
|
|
41
|
-
* @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not
|
|
42
|
-
*
|
|
41
|
+
* @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not VaultBundlesV1 or
|
|
42
|
+
* BlueBundlesV1 for `chainId`.
|
|
43
43
|
* @throws {NonPositiveInputError} when an explicit `deadline` is not positive.
|
|
44
44
|
* @throws {InputExceedsMaxError} when an explicit `deadline` exceeds `uint256`.
|
|
45
45
|
* @throws {ExpiredDeadlineError} when an explicit `deadline` is positive but not in the future.
|
|
@@ -18,8 +18,8 @@ import { ChainIdMismatchError, ExpiredDeadlineError, } from "../../../types/inde
|
|
|
18
18
|
* @param params - Permit encoding parameters.
|
|
19
19
|
* @param params.token - ERC-20 token address (must support EIP-2612).
|
|
20
20
|
* @param params.owner - Account that owns the tokens and signs the permit.
|
|
21
|
-
* @param params.spender - Permit spender. Must be
|
|
22
|
-
*
|
|
21
|
+
* @param params.spender - Permit spender. Must be VaultBundlesV1 or BlueBundlesV1 for the
|
|
22
|
+
* chain.
|
|
23
23
|
* @param params.amount - Permit allowance amount.
|
|
24
24
|
* @param params.chainId - Target chain id.
|
|
25
25
|
* @param params.nonce - The owner's current EIP-2612 nonce on `token`.
|
|
@@ -29,8 +29,8 @@ import { ChainIdMismatchError, ExpiredDeadlineError, } from "../../../types/inde
|
|
|
29
29
|
* `sign(client, userAddress)` produces the deep-frozen signature.
|
|
30
30
|
* @throws {ChainIdMismatchError} when `viemClient.chain?.id !== params.chainId`.
|
|
31
31
|
* @throws {UnsupportedChainIdError} when `chainId` is absent from the address registry.
|
|
32
|
-
* @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not
|
|
33
|
-
*
|
|
32
|
+
* @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not VaultBundlesV1 or
|
|
33
|
+
* BlueBundlesV1 for `chainId`.
|
|
34
34
|
* @throws {NonPositiveInputError} when an explicit `deadline` is not positive.
|
|
35
35
|
* @throws {InputExceedsMaxError} when an explicit `deadline` exceeds `uint256`.
|
|
36
36
|
* @throws {ExpiredDeadlineError} when an explicit `deadline` is positive but not in the future.
|
|
@@ -67,7 +67,7 @@ export const encodeErc20Permit = async (viemClient, params) => {
|
|
|
67
67
|
validateRequirementSpender({
|
|
68
68
|
chainId,
|
|
69
69
|
spender,
|
|
70
|
-
allowed: ["
|
|
70
|
+
allowed: ["vaultBundlesV1", "blueBundlesV1"],
|
|
71
71
|
});
|
|
72
72
|
const now = Time.timestamp();
|
|
73
73
|
// Validate an explicit deadline before the token-metadata read: this exported encoder can be
|
|
@@ -5,7 +5,7 @@ import { type ERC20ApprovalAction, type Transaction } from "../../types/index.js
|
|
|
5
5
|
* allowance.
|
|
6
6
|
*
|
|
7
7
|
* The spender is validated by {@link encodeErc20Approval}. Supported spenders are the chain's
|
|
8
|
-
* Permit2, Midnight,
|
|
8
|
+
* Permit2, Midnight, MidnightBundlesV2, VaultExitBundlesV1, VaultBundlesV1, and BlueBundlesV1
|
|
9
9
|
* addresses when configured.
|
|
10
10
|
*
|
|
11
11
|
* Returns an empty array when the allowance already covers `spendAmount`. When the token is in
|
|
@@ -20,7 +20,7 @@ import { type ERC20ApprovalAction, type Transaction } from "../../types/index.js
|
|
|
20
20
|
* @param params.args.approvalAmount - The amount to approve (often equal to `spendAmount`, but
|
|
21
21
|
* may be `maxUint256` for reusable Permit2 or saturated-share-repay approvals).
|
|
22
22
|
* @param params.args.spender - Address that will be granted the approval. Must be Permit2,
|
|
23
|
-
* Midnight,
|
|
23
|
+
* Midnight, MidnightBundlesV2, VaultExitBundlesV1, VaultBundlesV1, or BlueBundlesV1 for `chainId`.
|
|
24
24
|
* @param params.allowances - The user's current allowance of `address` for `spender`.
|
|
25
25
|
* @returns Up to two deep-frozen `Transaction<ERC20ApprovalAction>` entries: an optional reset
|
|
26
26
|
* followed by the new approval. Empty when no approval is needed.
|
|
@@ -7,7 +7,7 @@ import { encodeErc20Approval } from "./encode/encodeErc20Approval.js";
|
|
|
7
7
|
* allowance.
|
|
8
8
|
*
|
|
9
9
|
* The spender is validated by {@link encodeErc20Approval}. Supported spenders are the chain's
|
|
10
|
-
* Permit2, Midnight,
|
|
10
|
+
* Permit2, Midnight, MidnightBundlesV2, VaultExitBundlesV1, VaultBundlesV1, and BlueBundlesV1
|
|
11
11
|
* addresses when configured.
|
|
12
12
|
*
|
|
13
13
|
* Returns an empty array when the allowance already covers `spendAmount`. When the token is in
|
|
@@ -22,7 +22,7 @@ import { encodeErc20Approval } from "./encode/encodeErc20Approval.js";
|
|
|
22
22
|
* @param params.args.approvalAmount - The amount to approve (often equal to `spendAmount`, but
|
|
23
23
|
* may be `maxUint256` for reusable Permit2 or saturated-share-repay approvals).
|
|
24
24
|
* @param params.args.spender - Address that will be granted the approval. Must be Permit2,
|
|
25
|
-
* Midnight,
|
|
25
|
+
* Midnight, MidnightBundlesV2, VaultExitBundlesV1, VaultBundlesV1, or BlueBundlesV1 for `chainId`.
|
|
26
26
|
* @param params.allowances - The user's current allowance of `address` for `spender`.
|
|
27
27
|
* @returns Up to two deep-frozen `Transaction<ERC20ApprovalAction>` entries: an optional reset
|
|
28
28
|
* followed by the new approval. Empty when no approval is needed.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Address, Client } from "viem";
|
|
2
|
-
import {
|
|
2
|
+
import type { ERC20ApprovalAction, Transaction } from "../../../types/index.js";
|
|
3
3
|
/** Parameters for {@link getMidnightApprovalRequirements}. */
|
|
4
4
|
export interface GetMidnightApprovalRequirementsParams {
|
|
5
5
|
readonly viemClient: Client;
|
|
@@ -8,37 +8,53 @@ export interface GetMidnightApprovalRequirementsParams {
|
|
|
8
8
|
readonly owner: Address;
|
|
9
9
|
readonly spender: Address;
|
|
10
10
|
readonly amount: bigint;
|
|
11
|
+
/**
|
|
12
|
+
* Allowance to set when an approval is needed. Defaults to `maxUint256`; must be
|
|
13
|
+
* at least `amount`.
|
|
14
|
+
*/
|
|
15
|
+
readonly approvalAmount?: bigint;
|
|
11
16
|
}
|
|
12
17
|
/**
|
|
13
18
|
* Resolves classic ERC20 approval requirements for a Midnight spender.
|
|
14
19
|
*
|
|
15
20
|
* Entity flows call this from `getRequirements()`. Direct low-level consumers
|
|
16
|
-
* should
|
|
17
|
-
* `
|
|
21
|
+
* should pass the spender used by the action: `Midnight` for direct calls, or
|
|
22
|
+
* `MidnightBundlesV2` for bundle flows, including `repayWithdrawCollateral`.
|
|
23
|
+
*
|
|
24
|
+
* The returned approval sets an unlimited allowance (`maxUint256`, capped per
|
|
25
|
+
* token by `MAX_TOKEN_APPROVALS`) whenever the current allowance is below
|
|
26
|
+
* `amount`, because all of an account's Midnight offers and bundle calls share
|
|
27
|
+
* one allowance per token. Pass `approvalAmount` to set a different allowance.
|
|
18
28
|
*
|
|
19
29
|
* @param params.viemClient - Viem client used to read ERC20 allowance.
|
|
20
30
|
* @param params.chainId - Chain id expected by the viem client.
|
|
21
31
|
* @param params.token - ERC20 token that may need approval.
|
|
22
32
|
* @param params.owner - Token owner.
|
|
23
33
|
* @param params.spender - Midnight spender that will pull tokens.
|
|
24
|
-
* @param params.amount -
|
|
34
|
+
* @param params.amount - Minimum allowance the spender needs.
|
|
35
|
+
* @param params.approvalAmount - Allowance to approve when one is needed; defaults to `maxUint256` and must be at least `amount`.
|
|
25
36
|
* @returns Approval transactions required for `spender` to pull `amount`.
|
|
26
37
|
* @throws {ChainIdMismatchError} when the viem client is connected to another chain.
|
|
27
|
-
* @throws {NegativeInputError} when `amount
|
|
28
|
-
* @throws {
|
|
38
|
+
* @throws {NegativeInputError} when `amount` or `approvalAmount` is negative.
|
|
39
|
+
* @throws {InputExceedsMaxError} when `amount` or `approvalAmount` exceeds `maxUint256`.
|
|
40
|
+
* @throws {ApprovalAmountLessThanSpendAmountError} when `approvalAmount < amount`.
|
|
41
|
+
* @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not the chain's Midnight or MidnightBundlesV2 deployment.
|
|
29
42
|
* @example
|
|
30
43
|
* ```ts
|
|
44
|
+
* import { getChainAddress } from "@morpho-org/morpho-sdk/addresses";
|
|
31
45
|
* import {
|
|
32
46
|
* getMidnightApprovalRequirements,
|
|
33
47
|
* midnightTakeLend,
|
|
34
48
|
* } from "@morpho-org/morpho-sdk";
|
|
35
49
|
*
|
|
50
|
+
* const chainId = 8453;
|
|
51
|
+
* const midnightBundlesV2 = getChainAddress(chainId, "midnightBundlesV2");
|
|
36
52
|
* const approvals = await getMidnightApprovalRequirements({
|
|
37
53
|
* viemClient: client,
|
|
38
|
-
* chainId
|
|
54
|
+
* chainId,
|
|
39
55
|
* token: loanToken,
|
|
40
56
|
* owner: user,
|
|
41
|
-
* spender:
|
|
57
|
+
* spender: midnightBundlesV2,
|
|
42
58
|
* amount: 1_000_000n,
|
|
43
59
|
* });
|
|
44
60
|
* if (approvals.length === 0) {
|
|
@@ -1,38 +1,49 @@
|
|
|
1
|
-
import { erc20Abi } from "viem";
|
|
1
|
+
import { erc20Abi, maxUint256 } from "viem";
|
|
2
2
|
import { readContract } from "viem/actions";
|
|
3
3
|
import { validateChainId, validateRequirementSpender, } from "../../../helpers/index.js";
|
|
4
|
-
import {
|
|
4
|
+
import { validateUint256Field } from "../../../helpers/validate.js";
|
|
5
5
|
import { getRequirementsApproval } from "../getRequirementsApproval.js";
|
|
6
6
|
/**
|
|
7
7
|
* Resolves classic ERC20 approval requirements for a Midnight spender.
|
|
8
8
|
*
|
|
9
9
|
* Entity flows call this from `getRequirements()`. Direct low-level consumers
|
|
10
|
-
* should
|
|
11
|
-
* `
|
|
10
|
+
* should pass the spender used by the action: `Midnight` for direct calls, or
|
|
11
|
+
* `MidnightBundlesV2` for bundle flows, including `repayWithdrawCollateral`.
|
|
12
|
+
*
|
|
13
|
+
* The returned approval sets an unlimited allowance (`maxUint256`, capped per
|
|
14
|
+
* token by `MAX_TOKEN_APPROVALS`) whenever the current allowance is below
|
|
15
|
+
* `amount`, because all of an account's Midnight offers and bundle calls share
|
|
16
|
+
* one allowance per token. Pass `approvalAmount` to set a different allowance.
|
|
12
17
|
*
|
|
13
18
|
* @param params.viemClient - Viem client used to read ERC20 allowance.
|
|
14
19
|
* @param params.chainId - Chain id expected by the viem client.
|
|
15
20
|
* @param params.token - ERC20 token that may need approval.
|
|
16
21
|
* @param params.owner - Token owner.
|
|
17
22
|
* @param params.spender - Midnight spender that will pull tokens.
|
|
18
|
-
* @param params.amount -
|
|
23
|
+
* @param params.amount - Minimum allowance the spender needs.
|
|
24
|
+
* @param params.approvalAmount - Allowance to approve when one is needed; defaults to `maxUint256` and must be at least `amount`.
|
|
19
25
|
* @returns Approval transactions required for `spender` to pull `amount`.
|
|
20
26
|
* @throws {ChainIdMismatchError} when the viem client is connected to another chain.
|
|
21
|
-
* @throws {NegativeInputError} when `amount
|
|
22
|
-
* @throws {
|
|
27
|
+
* @throws {NegativeInputError} when `amount` or `approvalAmount` is negative.
|
|
28
|
+
* @throws {InputExceedsMaxError} when `amount` or `approvalAmount` exceeds `maxUint256`.
|
|
29
|
+
* @throws {ApprovalAmountLessThanSpendAmountError} when `approvalAmount < amount`.
|
|
30
|
+
* @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not the chain's Midnight or MidnightBundlesV2 deployment.
|
|
23
31
|
* @example
|
|
24
32
|
* ```ts
|
|
33
|
+
* import { getChainAddress } from "@morpho-org/morpho-sdk/addresses";
|
|
25
34
|
* import {
|
|
26
35
|
* getMidnightApprovalRequirements,
|
|
27
36
|
* midnightTakeLend,
|
|
28
37
|
* } from "@morpho-org/morpho-sdk";
|
|
29
38
|
*
|
|
39
|
+
* const chainId = 8453;
|
|
40
|
+
* const midnightBundlesV2 = getChainAddress(chainId, "midnightBundlesV2");
|
|
30
41
|
* const approvals = await getMidnightApprovalRequirements({
|
|
31
42
|
* viemClient: client,
|
|
32
|
-
* chainId
|
|
43
|
+
* chainId,
|
|
33
44
|
* token: loanToken,
|
|
34
45
|
* owner: user,
|
|
35
|
-
* spender:
|
|
46
|
+
* spender: midnightBundlesV2,
|
|
36
47
|
* amount: 1_000_000n,
|
|
37
48
|
* });
|
|
38
49
|
* if (approvals.length === 0) {
|
|
@@ -42,14 +53,14 @@ import { getRequirementsApproval } from "../getRequirementsApproval.js";
|
|
|
42
53
|
*/
|
|
43
54
|
export const getMidnightApprovalRequirements = async (params) => {
|
|
44
55
|
validateChainId(params.viemClient.chain?.id, params.chainId);
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
56
|
+
validateUint256Field("amount", params.amount);
|
|
57
|
+
if (params.approvalAmount !== undefined)
|
|
58
|
+
validateUint256Field("approvalAmount", params.approvalAmount);
|
|
48
59
|
// Validate the target even when no approval transaction needs to be encoded.
|
|
49
60
|
validateRequirementSpender({
|
|
50
61
|
chainId: params.chainId,
|
|
51
62
|
spender: params.spender,
|
|
52
|
-
allowed: ["midnight", "
|
|
63
|
+
allowed: ["midnight", "midnightBundlesV2"],
|
|
53
64
|
});
|
|
54
65
|
if (params.amount === 0n)
|
|
55
66
|
return [];
|
|
@@ -65,7 +76,7 @@ export const getMidnightApprovalRequirements = async (params) => {
|
|
|
65
76
|
args: {
|
|
66
77
|
spender: params.spender,
|
|
67
78
|
spendAmount: params.amount,
|
|
68
|
-
approvalAmount: params.
|
|
79
|
+
approvalAmount: params.approvalAmount ?? maxUint256,
|
|
69
80
|
},
|
|
70
81
|
allowances: allowance,
|
|
71
82
|
});
|
|
@@ -8,29 +8,33 @@ export interface GetMidnightAuthorizationRequirementParams {
|
|
|
8
8
|
readonly authorized: Address;
|
|
9
9
|
}
|
|
10
10
|
/**
|
|
11
|
-
* Resolves the Midnight authorization transaction for a
|
|
11
|
+
* Resolves the Midnight authorization transaction for a Midnight bundle operator.
|
|
12
12
|
*
|
|
13
|
-
* Call before actions that rely on `Midnight.isAuthorized(owner, authorized)
|
|
14
|
-
*
|
|
13
|
+
* Call before actions that rely on `Midnight.isAuthorized(owner, authorized)`.
|
|
14
|
+
* MidnightBundlesV2 taker and cancel-and-make flows, including
|
|
15
|
+
* `repayWithdrawCollateral`, authorize `MidnightBundlesV2`. Entity flows call
|
|
15
16
|
* this from `getRequirements()` and omit the transaction when authorization is
|
|
16
17
|
* already set.
|
|
17
18
|
*
|
|
18
19
|
* @param params.viemClient - Viem client used to read `Midnight.isAuthorized`.
|
|
19
20
|
* @param params.chainId - Chain id expected by the viem client.
|
|
20
21
|
* @param params.owner - Account granting authorization.
|
|
21
|
-
* @param params.authorized -
|
|
22
|
+
* @param params.authorized - MidnightBundlesV2.
|
|
22
23
|
* @returns Authorization transaction, or `null` when already authorized.
|
|
23
24
|
* @throws {ChainIdMismatchError} when the viem client is connected to another chain.
|
|
24
|
-
* @throws {UnsupportedMidnightAuthorizationTargetError} when `authorized` is not
|
|
25
|
+
* @throws {UnsupportedMidnightAuthorizationTargetError} when `authorized` is not MidnightBundlesV2.
|
|
25
26
|
* @example
|
|
26
27
|
* ```ts
|
|
28
|
+
* import { getChainAddress } from "@morpho-org/morpho-sdk/addresses";
|
|
27
29
|
* import { getMidnightAuthorizationRequirement } from "@morpho-org/morpho-sdk";
|
|
28
30
|
*
|
|
31
|
+
* const chainId = 8453;
|
|
32
|
+
* const midnightBundlesV2 = getChainAddress(chainId, "midnightBundlesV2");
|
|
29
33
|
* const tx = await getMidnightAuthorizationRequirement({
|
|
30
34
|
* viemClient: client,
|
|
31
|
-
* chainId
|
|
35
|
+
* chainId,
|
|
32
36
|
* owner: user,
|
|
33
|
-
* authorized:
|
|
37
|
+
* authorized: midnightBundlesV2,
|
|
34
38
|
* });
|
|
35
39
|
* if (tx) {
|
|
36
40
|
* await walletClient.sendTransaction({
|
|
@@ -5,29 +5,33 @@ import { readContract } from "viem/actions";
|
|
|
5
5
|
import { validateChainId } from "../../../helpers/index.js";
|
|
6
6
|
import { validateMidnightAuthorizationTarget } from "../../../helpers/validateMidnightAuthorizationTarget.js";
|
|
7
7
|
/**
|
|
8
|
-
* Resolves the Midnight authorization transaction for a
|
|
8
|
+
* Resolves the Midnight authorization transaction for a Midnight bundle operator.
|
|
9
9
|
*
|
|
10
|
-
* Call before actions that rely on `Midnight.isAuthorized(owner, authorized)
|
|
11
|
-
*
|
|
10
|
+
* Call before actions that rely on `Midnight.isAuthorized(owner, authorized)`.
|
|
11
|
+
* MidnightBundlesV2 taker and cancel-and-make flows, including
|
|
12
|
+
* `repayWithdrawCollateral`, authorize `MidnightBundlesV2`. Entity flows call
|
|
12
13
|
* this from `getRequirements()` and omit the transaction when authorization is
|
|
13
14
|
* already set.
|
|
14
15
|
*
|
|
15
16
|
* @param params.viemClient - Viem client used to read `Midnight.isAuthorized`.
|
|
16
17
|
* @param params.chainId - Chain id expected by the viem client.
|
|
17
18
|
* @param params.owner - Account granting authorization.
|
|
18
|
-
* @param params.authorized -
|
|
19
|
+
* @param params.authorized - MidnightBundlesV2.
|
|
19
20
|
* @returns Authorization transaction, or `null` when already authorized.
|
|
20
21
|
* @throws {ChainIdMismatchError} when the viem client is connected to another chain.
|
|
21
|
-
* @throws {UnsupportedMidnightAuthorizationTargetError} when `authorized` is not
|
|
22
|
+
* @throws {UnsupportedMidnightAuthorizationTargetError} when `authorized` is not MidnightBundlesV2.
|
|
22
23
|
* @example
|
|
23
24
|
* ```ts
|
|
25
|
+
* import { getChainAddress } from "@morpho-org/morpho-sdk/addresses";
|
|
24
26
|
* import { getMidnightAuthorizationRequirement } from "@morpho-org/morpho-sdk";
|
|
25
27
|
*
|
|
28
|
+
* const chainId = 8453;
|
|
29
|
+
* const midnightBundlesV2 = getChainAddress(chainId, "midnightBundlesV2");
|
|
26
30
|
* const tx = await getMidnightAuthorizationRequirement({
|
|
27
31
|
* viemClient: client,
|
|
28
|
-
* chainId
|
|
32
|
+
* chainId,
|
|
29
33
|
* owner: user,
|
|
30
|
-
* authorized:
|
|
34
|
+
* authorized: midnightBundlesV2,
|
|
31
35
|
* });
|
|
32
36
|
* if (tx) {
|
|
33
37
|
* await walletClient.sendTransaction({
|