@morpho-org/morpho-sdk 5.2.0 → 5.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +46 -2
- package/lib/cjs/abis.d.ts +1 -0
- package/lib/cjs/abis.js +6 -1
- package/lib/cjs/actions/blue/borrow.d.ts +3 -7
- package/lib/cjs/actions/blue/borrow.js +5 -9
- package/lib/cjs/actions/blue/buildReallocationActions.d.ts +3 -4
- package/lib/cjs/actions/blue/buildReallocationActions.js +3 -4
- package/lib/cjs/actions/blue/refinance.d.ts +4 -8
- package/lib/cjs/actions/blue/refinance.js +10 -14
- package/lib/cjs/actions/blue/repay.d.ts +2 -5
- package/lib/cjs/actions/blue/repay.js +2 -5
- package/lib/cjs/actions/blue/repayWithdrawCollateral.d.ts +3 -6
- package/lib/cjs/actions/blue/repayWithdrawCollateral.js +4 -7
- package/lib/cjs/actions/blue/resolveRepayFunding.d.ts +2 -5
- package/lib/cjs/actions/blue/resolveRepayFunding.js +15 -12
- package/lib/cjs/actions/blue/supply.d.ts +2 -4
- package/lib/cjs/actions/blue/supply.js +6 -8
- package/lib/cjs/actions/blue/supplyCollateral.d.ts +2 -3
- package/lib/cjs/actions/blue/supplyCollateral.js +5 -6
- package/lib/cjs/actions/blue/supplyCollateralBorrow.d.ts +4 -9
- package/lib/cjs/actions/blue/supplyCollateralBorrow.js +9 -14
- package/lib/cjs/actions/blue/withdraw.d.ts +9 -7
- package/lib/cjs/actions/blue/withdraw.js +18 -10
- package/lib/cjs/actions/blue/withdrawCollateral.d.ts +1 -1
- package/lib/cjs/actions/blue/withdrawCollateral.js +2 -2
- package/lib/cjs/actions/index.d.ts +2 -0
- package/lib/cjs/actions/index.js +1 -0
- package/lib/cjs/actions/midnight/authorization.d.ts +37 -0
- package/lib/cjs/actions/midnight/authorization.js +69 -0
- package/lib/cjs/actions/midnight/cancelOffer.d.ts +38 -0
- package/lib/cjs/actions/midnight/cancelOffer.js +73 -0
- package/lib/cjs/actions/midnight/index.d.ts +11 -0
- package/lib/cjs/actions/midnight/index.js +27 -0
- package/lib/cjs/actions/midnight/mempoolSubmitOffers.d.ts +52 -0
- package/lib/cjs/actions/midnight/mempoolSubmitOffers.js +72 -0
- package/lib/cjs/actions/midnight/redeem.d.ts +43 -0
- package/lib/cjs/actions/midnight/redeem.js +79 -0
- package/lib/cjs/actions/midnight/repayWithdrawCollateral.d.ts +53 -0
- package/lib/cjs/actions/midnight/repayWithdrawCollateral.js +121 -0
- package/lib/cjs/actions/midnight/setterRatifierRatifyRoot.d.ts +40 -0
- package/lib/cjs/actions/midnight/setterRatifierRatifyRoot.js +59 -0
- package/lib/cjs/actions/midnight/supplyCollateral.d.ts +45 -0
- package/lib/cjs/actions/midnight/supplyCollateral.js +83 -0
- package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.d.ts +51 -0
- package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +128 -0
- package/lib/cjs/actions/midnight/takeBorrow.d.ts +57 -0
- package/lib/cjs/actions/midnight/takeBorrow.js +110 -0
- package/lib/cjs/actions/midnight/takeLend.d.ts +57 -0
- package/lib/cjs/actions/midnight/takeLend.js +110 -0
- package/lib/cjs/actions/midnight/types.d.ts +39 -0
- package/lib/cjs/actions/midnight/types.js +15 -0
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +1 -1
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +5 -16
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +5 -4
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +10 -14
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +2 -2
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +11 -28
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2Approve.d.ts +1 -1
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2Approve.js +5 -17
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +1 -1
- package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +10 -4
- package/lib/cjs/actions/requirements/getRequirementsApproval.js +10 -4
- package/lib/cjs/actions/requirements/index.d.ts +1 -0
- package/lib/cjs/actions/requirements/index.js +1 -0
- package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.d.ts +49 -0
- package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.js +76 -0
- package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.d.ts +44 -0
- package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +78 -0
- package/lib/cjs/actions/requirements/midnight/getSetterRatifierRatifyRootRequirement.d.ts +42 -0
- package/lib/cjs/actions/requirements/midnight/getSetterRatifierRatifyRootRequirement.js +70 -0
- package/lib/cjs/actions/requirements/midnight/index.d.ts +3 -0
- package/lib/cjs/actions/requirements/midnight/index.js +19 -0
- package/lib/cjs/actions/vaultV1/deposit.d.ts +3 -4
- package/lib/cjs/actions/vaultV1/deposit.js +7 -8
- package/lib/cjs/actions/vaultV1/migrateToV2.d.ts +2 -3
- package/lib/cjs/actions/vaultV1/migrateToV2.js +5 -6
- package/lib/cjs/actions/vaultV1/redeem.d.ts +1 -1
- package/lib/cjs/actions/vaultV1/redeem.js +2 -2
- package/lib/cjs/actions/vaultV1/withdraw.d.ts +1 -1
- package/lib/cjs/actions/vaultV1/withdraw.js +2 -2
- package/lib/cjs/actions/vaultV2/deposit.d.ts +3 -4
- package/lib/cjs/actions/vaultV2/deposit.js +7 -8
- package/lib/cjs/actions/vaultV2/forceRedeem.d.ts +2 -3
- package/lib/cjs/actions/vaultV2/forceRedeem.js +3 -4
- package/lib/cjs/actions/vaultV2/forceWithdraw.d.ts +1 -1
- package/lib/cjs/actions/vaultV2/forceWithdraw.js +2 -2
- package/lib/cjs/actions/vaultV2/redeem.d.ts +1 -1
- package/lib/cjs/actions/vaultV2/redeem.js +2 -2
- package/lib/cjs/actions/vaultV2/withdraw.d.ts +1 -1
- package/lib/cjs/actions/vaultV2/withdraw.js +2 -2
- package/lib/cjs/client/morphoViemExtension.d.ts +1 -1
- package/lib/cjs/client/morphoViemExtension.js +5 -2
- package/lib/cjs/constants.d.ts +1 -0
- package/lib/cjs/constants.js +15 -1
- package/lib/cjs/entities/blue/blue.d.ts +5 -5
- package/lib/cjs/entities/blue/blue.js +93 -68
- package/lib/cjs/entities/index.d.ts +1 -0
- package/lib/cjs/entities/index.js +15 -0
- package/lib/cjs/entities/midnight/index.d.ts +2 -0
- package/lib/cjs/entities/midnight/index.js +18 -0
- package/lib/cjs/entities/midnight/midnight.d.ts +402 -0
- package/lib/cjs/entities/midnight/midnight.js +1036 -0
- package/lib/cjs/entities/midnight/types.d.ts +164 -0
- package/lib/cjs/entities/midnight/types.js +2 -0
- package/lib/cjs/entities/vaultV1/vaultV1.js +10 -17
- package/lib/cjs/entities/vaultV2/vaultV2.js +19 -21
- package/lib/cjs/errors.d.ts +1 -0
- package/lib/cjs/errors.js +17 -1
- package/lib/cjs/helpers/encodeDeallocation.d.ts +1 -0
- package/lib/cjs/helpers/encodeDeallocation.js +2 -1
- package/lib/cjs/helpers/index.d.ts +4 -1
- package/lib/cjs/helpers/index.js +8 -1
- package/lib/cjs/helpers/signAndVerifyTypedData.d.ts +34 -0
- package/lib/cjs/helpers/signAndVerifyTypedData.js +58 -0
- package/lib/cjs/helpers/validate.d.ts +40 -1
- package/lib/cjs/helpers/validate.js +47 -5
- package/lib/cjs/helpers/validateMidnightAuthorizationTarget.d.ts +15 -0
- package/lib/cjs/helpers/validateMidnightAuthorizationTarget.js +32 -0
- package/lib/cjs/helpers/validateMidnightMarket.d.ts +10 -0
- package/lib/cjs/helpers/validateMidnightMarket.js +24 -0
- package/lib/cjs/helpers/validateOfferSides.d.ts +21 -0
- package/lib/cjs/helpers/validateOfferSides.js +36 -0
- package/lib/cjs/helpers/validateRequirementSpender.d.ts +29 -0
- package/lib/cjs/helpers/validateRequirementSpender.js +49 -0
- package/lib/cjs/helpers/validateTakeableOffers.d.ts +18 -0
- package/lib/cjs/helpers/validateTakeableOffers.js +35 -0
- package/lib/cjs/midnight-api.d.ts +1 -0
- package/lib/cjs/midnight-api.js +17 -0
- package/lib/cjs/types/action.d.ts +189 -23
- package/lib/cjs/types/action.js +27 -7
- package/lib/cjs/types/client.d.ts +3 -2
- package/lib/cjs/types/error.d.ts +288 -119
- package/lib/cjs/types/error.js +259 -180
- package/lib/cjs/utils.d.ts +1 -0
- package/lib/cjs/utils.js +12 -2
- package/lib/esm/abis.d.ts +1 -0
- package/lib/esm/abis.js +1 -0
- package/lib/esm/actions/blue/borrow.d.ts +3 -7
- package/lib/esm/actions/blue/borrow.js +6 -10
- package/lib/esm/actions/blue/buildReallocationActions.d.ts +3 -4
- package/lib/esm/actions/blue/buildReallocationActions.js +3 -4
- package/lib/esm/actions/blue/refinance.d.ts +4 -8
- package/lib/esm/actions/blue/refinance.js +11 -15
- package/lib/esm/actions/blue/repay.d.ts +2 -5
- package/lib/esm/actions/blue/repay.js +2 -5
- package/lib/esm/actions/blue/repayWithdrawCollateral.d.ts +3 -6
- package/lib/esm/actions/blue/repayWithdrawCollateral.js +5 -8
- package/lib/esm/actions/blue/resolveRepayFunding.d.ts +2 -5
- package/lib/esm/actions/blue/resolveRepayFunding.js +16 -13
- package/lib/esm/actions/blue/supply.d.ts +2 -4
- package/lib/esm/actions/blue/supply.js +7 -9
- package/lib/esm/actions/blue/supplyCollateral.d.ts +2 -3
- package/lib/esm/actions/blue/supplyCollateral.js +6 -7
- package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +4 -9
- package/lib/esm/actions/blue/supplyCollateralBorrow.js +10 -15
- package/lib/esm/actions/blue/withdraw.d.ts +9 -7
- package/lib/esm/actions/blue/withdraw.js +19 -11
- package/lib/esm/actions/blue/withdrawCollateral.d.ts +1 -1
- package/lib/esm/actions/blue/withdrawCollateral.js +3 -3
- package/lib/esm/actions/index.d.ts +2 -0
- package/lib/esm/actions/index.js +1 -0
- package/lib/esm/actions/midnight/authorization.d.ts +37 -0
- package/lib/esm/actions/midnight/authorization.js +65 -0
- package/lib/esm/actions/midnight/cancelOffer.d.ts +38 -0
- package/lib/esm/actions/midnight/cancelOffer.js +69 -0
- package/lib/esm/actions/midnight/index.d.ts +11 -0
- package/lib/esm/actions/midnight/index.js +11 -0
- package/lib/esm/actions/midnight/mempoolSubmitOffers.d.ts +52 -0
- package/lib/esm/actions/midnight/mempoolSubmitOffers.js +68 -0
- package/lib/esm/actions/midnight/redeem.d.ts +43 -0
- package/lib/esm/actions/midnight/redeem.js +75 -0
- package/lib/esm/actions/midnight/repayWithdrawCollateral.d.ts +53 -0
- package/lib/esm/actions/midnight/repayWithdrawCollateral.js +117 -0
- package/lib/esm/actions/midnight/setterRatifierRatifyRoot.d.ts +40 -0
- package/lib/esm/actions/midnight/setterRatifierRatifyRoot.js +55 -0
- package/lib/esm/actions/midnight/supplyCollateral.d.ts +45 -0
- package/lib/esm/actions/midnight/supplyCollateral.js +79 -0
- package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.d.ts +51 -0
- package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +124 -0
- package/lib/esm/actions/midnight/takeBorrow.d.ts +57 -0
- package/lib/esm/actions/midnight/takeBorrow.js +106 -0
- package/lib/esm/actions/midnight/takeLend.d.ts +57 -0
- package/lib/esm/actions/midnight/takeLend.js +106 -0
- package/lib/esm/actions/midnight/types.d.ts +39 -0
- package/lib/esm/actions/midnight/types.js +12 -0
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +1 -1
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +6 -17
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +5 -4
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +12 -16
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +2 -2
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +12 -29
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2Approve.d.ts +1 -1
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2Approve.js +5 -17
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +1 -1
- package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +10 -4
- package/lib/esm/actions/requirements/getRequirementsApproval.js +10 -4
- package/lib/esm/actions/requirements/index.d.ts +1 -0
- package/lib/esm/actions/requirements/index.js +1 -0
- package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.d.ts +49 -0
- package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.js +72 -0
- package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.d.ts +44 -0
- package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +74 -0
- package/lib/esm/actions/requirements/midnight/getSetterRatifierRatifyRootRequirement.d.ts +42 -0
- package/lib/esm/actions/requirements/midnight/getSetterRatifierRatifyRootRequirement.js +66 -0
- package/lib/esm/actions/requirements/midnight/index.d.ts +3 -0
- package/lib/esm/actions/requirements/midnight/index.js +3 -0
- package/lib/esm/actions/vaultV1/deposit.d.ts +3 -4
- package/lib/esm/actions/vaultV1/deposit.js +8 -9
- package/lib/esm/actions/vaultV1/migrateToV2.d.ts +2 -3
- package/lib/esm/actions/vaultV1/migrateToV2.js +6 -7
- package/lib/esm/actions/vaultV1/redeem.d.ts +1 -1
- package/lib/esm/actions/vaultV1/redeem.js +3 -3
- package/lib/esm/actions/vaultV1/withdraw.d.ts +1 -1
- package/lib/esm/actions/vaultV1/withdraw.js +3 -3
- package/lib/esm/actions/vaultV2/deposit.d.ts +3 -4
- package/lib/esm/actions/vaultV2/deposit.js +8 -9
- package/lib/esm/actions/vaultV2/forceRedeem.d.ts +2 -3
- package/lib/esm/actions/vaultV2/forceRedeem.js +4 -5
- package/lib/esm/actions/vaultV2/forceWithdraw.d.ts +1 -1
- package/lib/esm/actions/vaultV2/forceWithdraw.js +3 -3
- package/lib/esm/actions/vaultV2/redeem.d.ts +1 -1
- package/lib/esm/actions/vaultV2/redeem.js +3 -3
- package/lib/esm/actions/vaultV2/withdraw.d.ts +1 -1
- package/lib/esm/actions/vaultV2/withdraw.js +3 -3
- package/lib/esm/client/morphoViemExtension.d.ts +1 -1
- package/lib/esm/client/morphoViemExtension.js +6 -3
- package/lib/esm/constants.d.ts +1 -0
- package/lib/esm/constants.js +1 -0
- package/lib/esm/entities/blue/blue.d.ts +5 -5
- package/lib/esm/entities/blue/blue.js +95 -70
- package/lib/esm/entities/index.d.ts +1 -0
- package/lib/esm/entities/index.js +1 -0
- package/lib/esm/entities/midnight/index.d.ts +2 -0
- package/lib/esm/entities/midnight/index.js +2 -0
- package/lib/esm/entities/midnight/midnight.d.ts +402 -0
- package/lib/esm/entities/midnight/midnight.js +1032 -0
- package/lib/esm/entities/midnight/types.d.ts +164 -0
- package/lib/esm/entities/midnight/types.js +1 -0
- package/lib/esm/entities/vaultV1/vaultV1.js +13 -20
- package/lib/esm/entities/vaultV2/vaultV2.js +9 -11
- package/lib/esm/errors.d.ts +1 -0
- package/lib/esm/errors.js +1 -0
- package/lib/esm/helpers/encodeDeallocation.d.ts +1 -0
- package/lib/esm/helpers/encodeDeallocation.js +3 -2
- package/lib/esm/helpers/index.d.ts +4 -1
- package/lib/esm/helpers/index.js +4 -1
- package/lib/esm/helpers/signAndVerifyTypedData.d.ts +34 -0
- package/lib/esm/helpers/signAndVerifyTypedData.js +54 -0
- package/lib/esm/helpers/validate.d.ts +40 -1
- package/lib/esm/helpers/validate.js +46 -5
- package/lib/esm/helpers/validateMidnightAuthorizationTarget.d.ts +15 -0
- package/lib/esm/helpers/validateMidnightAuthorizationTarget.js +28 -0
- package/lib/esm/helpers/validateMidnightMarket.d.ts +10 -0
- package/lib/esm/helpers/validateMidnightMarket.js +20 -0
- package/lib/esm/helpers/validateOfferSides.d.ts +21 -0
- package/lib/esm/helpers/validateOfferSides.js +32 -0
- package/lib/esm/helpers/validateRequirementSpender.d.ts +29 -0
- package/lib/esm/helpers/validateRequirementSpender.js +45 -0
- package/lib/esm/helpers/validateTakeableOffers.d.ts +18 -0
- package/lib/esm/helpers/validateTakeableOffers.js +31 -0
- package/lib/esm/midnight-api.d.ts +1 -0
- package/lib/esm/midnight-api.js +1 -0
- package/lib/esm/types/action.d.ts +189 -23
- package/lib/esm/types/action.js +26 -7
- package/lib/esm/types/client.d.ts +3 -2
- package/lib/esm/types/error.d.ts +288 -119
- package/lib/esm/types/error.js +232 -154
- package/lib/esm/utils.d.ts +1 -0
- package/lib/esm/utils.js +1 -0
- package/package.json +12 -3
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import type { AccrualPosition, Market, MarketInput, MidnightFetchParams, Tree, TreeInput, TreeMempoolValidateParams } from "@morpho-org/midnight-sdk";
|
|
2
|
+
import type { Address, Hex } from "viem";
|
|
3
|
+
import type { MidnightTakeableOffer } from "../../actions/midnight/types.js";
|
|
4
|
+
import type { ActionOutput, BaseAction, MempoolSubmitOffersAction, MidnightOfferRootSignature } from "../../types/action.js";
|
|
5
|
+
/** Optional Midnight API validation controls for make-offer flows. */
|
|
6
|
+
export type OfferValidationParams = Omit<TreeMempoolValidateParams, "chainId">;
|
|
7
|
+
/** Parameters for building and validating Midnight offer data. */
|
|
8
|
+
export interface GetOffersDataParams {
|
|
9
|
+
readonly accountAddress: Address;
|
|
10
|
+
readonly offers: TreeInput;
|
|
11
|
+
readonly validation?: OfferValidationParams;
|
|
12
|
+
}
|
|
13
|
+
interface OffersDataBase {
|
|
14
|
+
readonly accountAddress: Address;
|
|
15
|
+
readonly groups: readonly Hex[];
|
|
16
|
+
readonly tree: Tree;
|
|
17
|
+
readonly ratifier: Address;
|
|
18
|
+
}
|
|
19
|
+
/** Prepared Midnight maker-offer data discriminated by its ratifier execution path. */
|
|
20
|
+
export type OffersData = (OffersDataBase & {
|
|
21
|
+
readonly ratifierType: "ecrecover";
|
|
22
|
+
readonly setterPayload?: never;
|
|
23
|
+
}) | (OffersDataBase & {
|
|
24
|
+
readonly ratifierType: "setter";
|
|
25
|
+
readonly setterPayload: Hex;
|
|
26
|
+
});
|
|
27
|
+
/** Parameters shared by Midnight maker-offer flows. */
|
|
28
|
+
export interface MakeOffersParams {
|
|
29
|
+
readonly accountAddress: Address;
|
|
30
|
+
readonly offers: TreeInput;
|
|
31
|
+
readonly validation?: OfferValidationParams;
|
|
32
|
+
}
|
|
33
|
+
/** Parameters for the Midnight make-lend maker flow. */
|
|
34
|
+
export interface MakeLendParams extends MakeOffersParams {
|
|
35
|
+
readonly loanToken: Address;
|
|
36
|
+
/** New group loan reserve. For grouped OCA offers, pass the group reserve once instead of summing every leg. */
|
|
37
|
+
readonly loanAssets: bigint;
|
|
38
|
+
/** Existing loan assets reserved across the maker's other open groups, including consumed amounts when available. */
|
|
39
|
+
readonly reservedLoanAssets?: bigint;
|
|
40
|
+
}
|
|
41
|
+
/** Parameters for the Midnight supply-collateral-and-make-borrow maker flow. */
|
|
42
|
+
export interface SupplyCollateralMakeBorrowParams extends MakeOffersParams {
|
|
43
|
+
readonly market: MarketInput;
|
|
44
|
+
/** Collateral supplied before offer submission and the new group collateral reserve counted once for grouped offers. */
|
|
45
|
+
readonly collateralAssets: bigint;
|
|
46
|
+
/** Existing collateral assets reserved across the maker's other open groups, including consumed amounts when available. */
|
|
47
|
+
readonly reservedCollateralAssets?: bigint;
|
|
48
|
+
readonly collateralIndex?: bigint;
|
|
49
|
+
}
|
|
50
|
+
/** Signatures accepted by Midnight action-output transaction builders. */
|
|
51
|
+
export type MidnightActionSignatures = MidnightOfferRootSignature | readonly MidnightOfferRootSignature[];
|
|
52
|
+
/**
|
|
53
|
+
* Lazy Midnight entity result with async requirements and sync transaction building.
|
|
54
|
+
*
|
|
55
|
+
* Call `getRequirements()` first to collect approvals, authorizations, or
|
|
56
|
+
* signatures. Once those are handled, pass any requirement signatures to
|
|
57
|
+
* `buildTx`; the transaction builder itself performs no fetching or signing.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* const output = midnight.takeLend(params);
|
|
62
|
+
* const requirements = await output.getRequirements();
|
|
63
|
+
* for (const requirement of requirements) {
|
|
64
|
+
* if (!("sign" in requirement)) {
|
|
65
|
+
* await walletClient.sendTransaction({
|
|
66
|
+
* to: requirement.to,
|
|
67
|
+
* data: requirement.data,
|
|
68
|
+
* value: requirement.value,
|
|
69
|
+
* });
|
|
70
|
+
* }
|
|
71
|
+
* }
|
|
72
|
+
* const tx = output.buildTx();
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export type MidnightActionOutput<TAction extends BaseAction, TSignatures = undefined> = ActionOutput<TAction, TSignatures, undefined>;
|
|
76
|
+
/**
|
|
77
|
+
* Output returned by maker-offer flows after offer-tree preparation.
|
|
78
|
+
*
|
|
79
|
+
* Use `groups` and `root` for review UI, call `getRequirements()` to collect
|
|
80
|
+
* ratifier approval or signature requirements, then call `buildTx(signatures)`
|
|
81
|
+
* to publish the encoded payload to the Midnight mempool.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```ts
|
|
85
|
+
* const output = await midnight.makeLend(params);
|
|
86
|
+
* console.log(output.root, output.groups);
|
|
87
|
+
* const requirements = await output.getRequirements();
|
|
88
|
+
* const signatures = [];
|
|
89
|
+
* for (const requirement of requirements) {
|
|
90
|
+
* if ("sign" in requirement) {
|
|
91
|
+
* signatures.push(await requirement.sign(walletClient, maker));
|
|
92
|
+
* } else {
|
|
93
|
+
* await walletClient.sendTransaction({
|
|
94
|
+
* to: requirement.to,
|
|
95
|
+
* data: requirement.data,
|
|
96
|
+
* value: requirement.value,
|
|
97
|
+
* });
|
|
98
|
+
* }
|
|
99
|
+
* }
|
|
100
|
+
* const tx = output.buildTx(signatures);
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
export interface MakeOffersOutput extends MidnightActionOutput<MempoolSubmitOffersAction, MidnightActionSignatures> {
|
|
104
|
+
readonly groups: readonly Hex[];
|
|
105
|
+
readonly root: Hex;
|
|
106
|
+
readonly ratifierType: "ecrecover" | "setter";
|
|
107
|
+
}
|
|
108
|
+
/** Parameters shared by Midnight market action flows. */
|
|
109
|
+
export interface MarketActionParams {
|
|
110
|
+
readonly accountAddress: Address;
|
|
111
|
+
readonly marketData: Market;
|
|
112
|
+
}
|
|
113
|
+
/** Parameters for the Midnight take-lend taker flow. */
|
|
114
|
+
export interface TakeLendParams extends MarketActionParams {
|
|
115
|
+
readonly assets: bigint;
|
|
116
|
+
readonly minUnits: bigint;
|
|
117
|
+
readonly takeableOffers: readonly MidnightTakeableOffer[];
|
|
118
|
+
/** Bundle execution deadline timestamp. Pass `maxUint256` explicitly for no expiry. */
|
|
119
|
+
readonly deadline: bigint;
|
|
120
|
+
}
|
|
121
|
+
/** Parameters for the Midnight take-borrow taker flow. */
|
|
122
|
+
export interface TakeBorrowParams extends MarketActionParams {
|
|
123
|
+
readonly loanAssets: bigint;
|
|
124
|
+
readonly maxUnits: bigint;
|
|
125
|
+
readonly takeableOffers: readonly MidnightTakeableOffer[];
|
|
126
|
+
/** Bundle execution deadline timestamp. Pass `maxUint256` explicitly for no expiry. */
|
|
127
|
+
readonly deadline: bigint;
|
|
128
|
+
}
|
|
129
|
+
/** Parameters for the Midnight supply-collateral-and-take-borrow taker flow. */
|
|
130
|
+
export interface SupplyCollateralTakeBorrowParams extends TakeBorrowParams {
|
|
131
|
+
readonly collateralAssets: bigint;
|
|
132
|
+
readonly collateralIndex?: bigint;
|
|
133
|
+
}
|
|
134
|
+
/** Parameters for the Midnight supply-collateral flow. */
|
|
135
|
+
export interface SupplyCollateralParams extends MarketActionParams {
|
|
136
|
+
readonly collateralAssets: bigint;
|
|
137
|
+
/** Existing collateral assets reserved across the maker's open groups, including consumed amounts when available. */
|
|
138
|
+
readonly reservedCollateralAssets?: bigint;
|
|
139
|
+
readonly collateralIndex?: bigint;
|
|
140
|
+
}
|
|
141
|
+
/** Parameters for the Midnight redeem flow. */
|
|
142
|
+
export interface RedeemParams {
|
|
143
|
+
readonly accountAddress: Address;
|
|
144
|
+
/** Owner-bound snapshot returned by `getPositionData`; manually constructed ownerless snapshots are rejected. */
|
|
145
|
+
readonly positionData: AccrualPosition;
|
|
146
|
+
readonly receiver?: Address;
|
|
147
|
+
readonly units?: bigint;
|
|
148
|
+
}
|
|
149
|
+
/** Parameters for the Midnight repay-and-withdraw-collateral flow. */
|
|
150
|
+
export interface RepayWithdrawCollateralParams extends MarketActionParams {
|
|
151
|
+
readonly repayAssets: bigint;
|
|
152
|
+
readonly withdrawCollateralAssets: bigint;
|
|
153
|
+
readonly collateralIndex?: bigint;
|
|
154
|
+
/** Bundle execution deadline timestamp. Pass `maxUint256` explicitly for no expiry. */
|
|
155
|
+
readonly deadline: bigint;
|
|
156
|
+
}
|
|
157
|
+
/** Parameters for fetching a Midnight user position with market data. */
|
|
158
|
+
export interface GetPositionDataParams {
|
|
159
|
+
readonly marketId: Hex;
|
|
160
|
+
readonly accountAddress: Address;
|
|
161
|
+
/** Optional fetch controls. Pass an externally fetched block number to coordinate this snapshot with other reads. */
|
|
162
|
+
readonly parameters?: MidnightFetchParams;
|
|
163
|
+
}
|
|
164
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -3,9 +3,9 @@ import { fetchAccrualVault } from "@morpho-org/blue-sdk-viem";
|
|
|
3
3
|
import { Time } from "@morpho-org/morpho-ts";
|
|
4
4
|
import { isAddressEqual } from "viem";
|
|
5
5
|
import { getGeneralAdapterRequirements, vaultV1Deposit, vaultV1MigrateToV2, vaultV1Redeem, vaultV1Withdraw, } from "../../actions/index.js";
|
|
6
|
-
import { MAX_ABSOLUTE_SHARE_PRICE
|
|
7
|
-
import { validateChainId } from "../../helpers/index.js";
|
|
8
|
-
import { ChainIdMismatchError, ChainWNativeMissingError,
|
|
6
|
+
import { MAX_ABSOLUTE_SHARE_PRICE } from "../../helpers/constant.js";
|
|
7
|
+
import { validateChainId, validateSlippageTolerance, } from "../../helpers/index.js";
|
|
8
|
+
import { ChainIdMismatchError, ChainWNativeMissingError, NativeAmountOnNonWNativeVaultError, NegativeInputError, NonPositiveInputError, selectRequirementSignatures, VaultAddressMismatchError, VaultAssetMismatchError, } from "../../types/index.js";
|
|
9
9
|
export class MorphoVaultV1 {
|
|
10
10
|
client;
|
|
11
11
|
vault;
|
|
@@ -35,10 +35,10 @@ export class MorphoVaultV1 {
|
|
|
35
35
|
throw new VaultAddressMismatchError(this.vault, vaultData.address);
|
|
36
36
|
}
|
|
37
37
|
if (amount < 0n) {
|
|
38
|
-
throw new
|
|
38
|
+
throw new NegativeInputError("amount", amount);
|
|
39
39
|
}
|
|
40
40
|
if (nativeAmount && nativeAmount < 0n) {
|
|
41
|
-
throw new
|
|
41
|
+
throw new NegativeInputError("nativeAmount", nativeAmount);
|
|
42
42
|
}
|
|
43
43
|
let wNative;
|
|
44
44
|
if (nativeAmount) {
|
|
@@ -47,21 +47,19 @@ export class MorphoVaultV1 {
|
|
|
47
47
|
throw new ChainWNativeMissingError(this.chainId);
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
|
-
|
|
51
|
-
throw new NegativeSlippageToleranceError(slippageTolerance);
|
|
52
|
-
}
|
|
53
|
-
if (slippageTolerance > MAX_SLIPPAGE_TOLERANCE) {
|
|
54
|
-
throw new ExcessiveSlippageToleranceError(slippageTolerance);
|
|
55
|
-
}
|
|
50
|
+
validateSlippageTolerance(slippageTolerance);
|
|
56
51
|
if (nativeAmount && wNative) {
|
|
57
52
|
if (!isAddressEqual(vaultData.asset, wNative)) {
|
|
58
53
|
throw new NativeAmountOnNonWNativeVaultError(vaultData.asset, wNative);
|
|
59
54
|
}
|
|
60
55
|
}
|
|
61
56
|
const totalAssets = amount + (nativeAmount ?? 0n);
|
|
57
|
+
if (totalAssets === 0n) {
|
|
58
|
+
throw new NonPositiveInputError("totalAssets", totalAssets);
|
|
59
|
+
}
|
|
62
60
|
const shares = vaultData.toShares(totalAssets);
|
|
63
61
|
if (shares <= 0n) {
|
|
64
|
-
throw new
|
|
62
|
+
throw new NonPositiveInputError("shares", shares);
|
|
65
63
|
}
|
|
66
64
|
const maxSharePrice = MathLib.min(MathLib.mulDivUp(totalAssets, MathLib.wToRay(MathLib.WAD + slippageTolerance), shares), MAX_ABSOLUTE_SHARE_PRICE);
|
|
67
65
|
return {
|
|
@@ -139,14 +137,9 @@ export class MorphoVaultV1 {
|
|
|
139
137
|
throw new VaultAssetMismatchError(sourceVault.asset, targetVault.asset);
|
|
140
138
|
}
|
|
141
139
|
if (shares <= 0n) {
|
|
142
|
-
throw new
|
|
143
|
-
}
|
|
144
|
-
if (slippageTolerance < 0n) {
|
|
145
|
-
throw new NegativeSlippageToleranceError(slippageTolerance);
|
|
146
|
-
}
|
|
147
|
-
if (slippageTolerance > MAX_SLIPPAGE_TOLERANCE) {
|
|
148
|
-
throw new ExcessiveSlippageToleranceError(slippageTolerance);
|
|
140
|
+
throw new NonPositiveInputError("shares", shares);
|
|
149
141
|
}
|
|
142
|
+
validateSlippageTolerance(slippageTolerance);
|
|
150
143
|
// Compute minSharePriceVaultV1 for V1 redeem (slippage downward)
|
|
151
144
|
const v1RefAssets = sourceVault.toAssets(shares);
|
|
152
145
|
const minSharePriceVaultV1 = MathLib.mulDivDown(v1RefAssets, MathLib.wToRay(MathLib.WAD - slippageTolerance), shares);
|
|
@@ -156,7 +149,7 @@ export class MorphoVaultV1 {
|
|
|
156
149
|
const { vault: accruedTargetVault } = targetVault.accrueInterest(targetAccrualTimestamp);
|
|
157
150
|
const v2RefShares = accruedTargetVault.toShares(v1RefAssets);
|
|
158
151
|
if (v2RefShares <= 0n) {
|
|
159
|
-
throw new
|
|
152
|
+
throw new NonPositiveInputError("targetVaultShares", v2RefShares);
|
|
160
153
|
}
|
|
161
154
|
const maxSharePriceVaultV2 = MathLib.min(MathLib.mulDivUp(v1RefAssets, MathLib.wToRay(MathLib.WAD + slippageTolerance), v2RefShares), MAX_ABSOLUTE_SHARE_PRICE);
|
|
162
155
|
return {
|
|
@@ -3,8 +3,8 @@ import { fetchAccrualVaultV2 } from "@morpho-org/blue-sdk-viem";
|
|
|
3
3
|
import { Time } from "@morpho-org/morpho-ts";
|
|
4
4
|
import { isAddressEqual } from "viem";
|
|
5
5
|
import { getGeneralAdapterRequirements, vaultV2Deposit, vaultV2ForceRedeem, vaultV2ForceWithdraw, vaultV2Redeem, vaultV2Withdraw, } from "../../actions/index.js";
|
|
6
|
-
import {
|
|
7
|
-
import { ChainIdMismatchError, ChainWNativeMissingError,
|
|
6
|
+
import { validateSlippageTolerance } from "../../helpers/index.js";
|
|
7
|
+
import { ChainIdMismatchError, ChainWNativeMissingError, NativeAmountOnNonWNativeVaultError, NegativeInputError, NonPositiveInputError, selectRequirementSignatures, VaultAddressMismatchError, } from "../../types/index.js";
|
|
8
8
|
export class MorphoVaultV2 {
|
|
9
9
|
client;
|
|
10
10
|
vault;
|
|
@@ -34,10 +34,10 @@ export class MorphoVaultV2 {
|
|
|
34
34
|
throw new VaultAddressMismatchError(this.vault, vaultData.address);
|
|
35
35
|
}
|
|
36
36
|
if (amount < 0n) {
|
|
37
|
-
throw new
|
|
37
|
+
throw new NegativeInputError("amount", amount);
|
|
38
38
|
}
|
|
39
39
|
if (nativeAmount && nativeAmount < 0n) {
|
|
40
|
-
throw new
|
|
40
|
+
throw new NegativeInputError("nativeAmount", nativeAmount);
|
|
41
41
|
}
|
|
42
42
|
let wNative;
|
|
43
43
|
if (nativeAmount) {
|
|
@@ -46,25 +46,23 @@ export class MorphoVaultV2 {
|
|
|
46
46
|
throw new ChainWNativeMissingError(this.chainId);
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
|
-
|
|
50
|
-
throw new NegativeSlippageToleranceError(slippageTolerance);
|
|
51
|
-
}
|
|
52
|
-
if (slippageTolerance > MAX_SLIPPAGE_TOLERANCE) {
|
|
53
|
-
throw new ExcessiveSlippageToleranceError(slippageTolerance);
|
|
54
|
-
}
|
|
49
|
+
validateSlippageTolerance(slippageTolerance);
|
|
55
50
|
if (nativeAmount && wNative) {
|
|
56
51
|
if (!isAddressEqual(vaultData.asset, wNative)) {
|
|
57
52
|
throw new NativeAmountOnNonWNativeVaultError(vaultData.asset, wNative);
|
|
58
53
|
}
|
|
59
54
|
}
|
|
60
55
|
const totalAssets = amount + (nativeAmount ?? 0n);
|
|
56
|
+
if (totalAssets === 0n) {
|
|
57
|
+
throw new NonPositiveInputError("totalAssets", totalAssets);
|
|
58
|
+
}
|
|
61
59
|
// Accrue interest forward to bound the on-chain share price at execution.
|
|
62
60
|
// Mirrors blue repay's 2h forward-accrual buffer.
|
|
63
61
|
const accrualTimestamp = MathLib.max(Time.timestamp(), vaultData.lastUpdate) + Time.s.from.h(2n);
|
|
64
62
|
const { vault: accruedVault } = vaultData.accrueInterest(accrualTimestamp);
|
|
65
63
|
const shares = accruedVault.toShares(totalAssets);
|
|
66
64
|
if (shares <= 0n) {
|
|
67
|
-
throw new
|
|
65
|
+
throw new NonPositiveInputError("shares", shares);
|
|
68
66
|
}
|
|
69
67
|
const maxSharePrice = MathLib.min(MathLib.mulDivUp(totalAssets, MathLib.wToRay(MathLib.WAD + slippageTolerance), shares), MathLib.RAY * 100n);
|
|
70
68
|
return {
|
package/lib/esm/errors.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { BlueErrors, IncompleteChainRegistryError, InvalidMarketParamsError, UnknownAddressError, UnknownDataError, UnknownFactory, UnknownMarketParamsError, UnknownOfFactory, UnknownTokenError, UnknownTokenPriceError, UnknownVaultConfigError, UnsupportedChainIdError, UnsupportedPreLiquidationParamsError, UnsupportedVaultV2AdapterError, VaultV2Errors, } from "@morpho-org/blue-sdk";
|
|
2
|
+
export { InvalidMidnightApiResponseError, InvalidOfferGroupError, InvalidOfferParameterError, InvalidPositionAccrualStateError, InvalidPositionAccrualTimestampError, InvalidPositionLossFactorError, InvalidTickSpacingError, InvalidTreeError, InvalidTreeHeightError, MidnightApiError, MidnightMempoolValidationError, PriceGreaterThanOneError, SettlementFeeExceedsPriceError, TickOutOfRangeError, UnknownCollateralIndexError, } from "@morpho-org/midnight-sdk";
|
|
2
3
|
export type { ErrorClass } from "@morpho-org/morpho-ts";
|
|
3
4
|
export { _try } from "@morpho-org/morpho-ts";
|
package/lib/esm/errors.js
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
export { BlueErrors, IncompleteChainRegistryError, InvalidMarketParamsError, UnknownAddressError, UnknownDataError, UnknownFactory, UnknownMarketParamsError, UnknownOfFactory, UnknownTokenError, UnknownTokenPriceError, UnknownVaultConfigError, UnsupportedChainIdError, UnsupportedPreLiquidationParamsError, UnsupportedVaultV2AdapterError, VaultV2Errors, } from "@morpho-org/blue-sdk";
|
|
2
|
+
export { InvalidMidnightApiResponseError, InvalidOfferGroupError, InvalidOfferParameterError, InvalidPositionAccrualStateError, InvalidPositionAccrualTimestampError, InvalidPositionLossFactorError, InvalidTickSpacingError, InvalidTreeError, InvalidTreeHeightError, MidnightApiError, MidnightMempoolValidationError, PriceGreaterThanOneError, SettlementFeeExceedsPriceError, TickOutOfRangeError, UnknownCollateralIndexError, } from "@morpho-org/midnight-sdk";
|
|
2
3
|
export { _try } from "@morpho-org/morpho-ts";
|
|
@@ -6,5 +6,6 @@ import { type Deallocation } from "../types/index.js";
|
|
|
6
6
|
* @param deallocation - A deallocation entry.
|
|
7
7
|
* @param onBehalf - The address from which the penalty is taken (share owner).
|
|
8
8
|
* @returns The ABI-encoded calldata for `VaultV2.forceDeallocate`.
|
|
9
|
+
* @throws {NonPositiveInputError} when `deallocation.amount <= 0n`.
|
|
9
10
|
*/
|
|
10
11
|
export declare function encodeForceDeallocateCall(deallocation: Deallocation, onBehalf: Address): Hex;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { marketParamsAbi } from "@morpho-org/blue-sdk";
|
|
2
2
|
import { vaultV2Abi } from "@morpho-org/blue-sdk-viem";
|
|
3
3
|
import { encodeAbiParameters, encodeFunctionData, } from "viem";
|
|
4
|
-
import {
|
|
4
|
+
import { NonPositiveInputError } from "../types/index.js";
|
|
5
5
|
/**
|
|
6
6
|
* Encodes the adapter-specific `data` bytes for a `forceDeallocate` call.
|
|
7
7
|
*
|
|
@@ -20,10 +20,11 @@ function encodeDeallocateData(deallocation) {
|
|
|
20
20
|
* @param deallocation - A deallocation entry.
|
|
21
21
|
* @param onBehalf - The address from which the penalty is taken (share owner).
|
|
22
22
|
* @returns The ABI-encoded calldata for `VaultV2.forceDeallocate`.
|
|
23
|
+
* @throws {NonPositiveInputError} when `deallocation.amount <= 0n`.
|
|
23
24
|
*/
|
|
24
25
|
export function encodeForceDeallocateCall(deallocation, onBehalf) {
|
|
25
26
|
if (deallocation.amount <= 0n) {
|
|
26
|
-
throw new
|
|
27
|
+
throw new NonPositiveInputError(`deallocation[${deallocation.adapter}].amount`, deallocation.amount);
|
|
27
28
|
}
|
|
28
29
|
const data = encodeDeallocateData(deallocation);
|
|
29
30
|
return encodeFunctionData({
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
export { computeReallocations } from "./computeReallocations.js";
|
|
2
2
|
export { APPROVE_ONLY_ONCE_TOKENS, DEFAULT_LLTV_BUFFER, DEFAULT_SUPPLY_TARGET_UTILIZATION, DEFAULT_WITHDRAWAL_TARGET_UTILIZATION, MAX_ABSOLUTE_SHARE_PRICE, MAX_SLIPPAGE_TOLERANCE, MAX_TOKEN_APPROVALS, } from "./constant.js";
|
|
3
3
|
export { addTransactionMetadata } from "./metadata.js";
|
|
4
|
+
export { signAndVerifyTypedData } from "./signAndVerifyTypedData.js";
|
|
4
5
|
export { computeMaxRepaySharePrice, computeMaxSupplySharePrice, computeMinBorrowSharePrice, computeMinWithdrawSharePrice, } from "./slippage.js";
|
|
5
|
-
export { validateAccrualPosition, validateChainId, validateNativeAsset, validatePositionHealth, validatePositionHealthAfterWithdraw, validateReallocations, validateRepayAmount, validateRepayShares, validateSlippageTolerance, validateUserAddress, validateWithdrawAmount, validateWithdrawShares, } from "./validate.js";
|
|
6
|
+
export { validateAccrualPosition, validateChainId, validateMidnightMarketChainId, validateNativeAsset, validatePositionHealth, validatePositionHealthAfterWithdraw, validateReallocations, validateRepayAmount, validateRepayShares, validateSlippageTolerance, validateUserAddress, validateWithdrawAmount, validateWithdrawShares, } from "./validate.js";
|
|
7
|
+
export { validateOfferSides } from "./validateOfferSides.js";
|
|
8
|
+
export { type RequirementSpenderKey, validateRequirementSpender, } from "./validateRequirementSpender.js";
|
package/lib/esm/helpers/index.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
export { computeReallocations } from "./computeReallocations.js";
|
|
2
2
|
export { APPROVE_ONLY_ONCE_TOKENS, DEFAULT_LLTV_BUFFER, DEFAULT_SUPPLY_TARGET_UTILIZATION, DEFAULT_WITHDRAWAL_TARGET_UTILIZATION, MAX_ABSOLUTE_SHARE_PRICE, MAX_SLIPPAGE_TOLERANCE, MAX_TOKEN_APPROVALS, } from "./constant.js";
|
|
3
3
|
export { addTransactionMetadata } from "./metadata.js";
|
|
4
|
+
export { signAndVerifyTypedData } from "./signAndVerifyTypedData.js";
|
|
4
5
|
export { computeMaxRepaySharePrice, computeMaxSupplySharePrice, computeMinBorrowSharePrice, computeMinWithdrawSharePrice, } from "./slippage.js";
|
|
5
|
-
export { validateAccrualPosition, validateChainId, validateNativeAsset, validatePositionHealth, validatePositionHealthAfterWithdraw, validateReallocations, validateRepayAmount, validateRepayShares, validateSlippageTolerance, validateUserAddress, validateWithdrawAmount, validateWithdrawShares, } from "./validate.js";
|
|
6
|
+
export { validateAccrualPosition, validateChainId, validateMidnightMarketChainId, validateNativeAsset, validatePositionHealth, validatePositionHealthAfterWithdraw, validateReallocations, validateRepayAmount, validateRepayShares, validateSlippageTolerance, validateUserAddress, validateWithdrawAmount, validateWithdrawShares, } from "./validate.js";
|
|
7
|
+
export { validateOfferSides } from "./validateOfferSides.js";
|
|
8
|
+
export { validateRequirementSpender, } from "./validateRequirementSpender.js";
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Address, Hex, TypedDataDefinition, WalletClient } from "viem";
|
|
2
|
+
/**
|
|
3
|
+
* Signs EIP-712 typed data with a wallet client, verifies that the produced signature recovers
|
|
4
|
+
* `userAddress`, and returns the signature.
|
|
5
|
+
*
|
|
6
|
+
* Use inside requirement `sign(...)` callbacks before converting a signature
|
|
7
|
+
* into protocol calldata or mempool payload bytes. The verification step keeps
|
|
8
|
+
* wallet/account mismatches from reaching transaction builders.
|
|
9
|
+
*
|
|
10
|
+
* @param params - Signing and verification parameters.
|
|
11
|
+
* @param params.client - Wallet client used to sign the typed data.
|
|
12
|
+
* @param params.userAddress - Address expected to own the produced signature.
|
|
13
|
+
* @param params.typedData - EIP-712 typed data to sign and verify.
|
|
14
|
+
* @returns The verified EIP-712 signature.
|
|
15
|
+
* @throws {MissingClientPropertyError} when the wallet client has no account address.
|
|
16
|
+
* @throws {AddressMismatchError} when the wallet client account differs from `userAddress`.
|
|
17
|
+
* @throws {ChainIdMismatchError} when the wallet client targets a different chain than the typed-data domain.
|
|
18
|
+
* @throws {InvalidSignatureError} when the signature does not recover to `userAddress`.
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* import { signAndVerifyTypedData } from "@morpho-org/morpho-sdk";
|
|
22
|
+
*
|
|
23
|
+
* const signature = await signAndVerifyTypedData({
|
|
24
|
+
* client: walletClient,
|
|
25
|
+
* userAddress: maker,
|
|
26
|
+
* typedData: offerRootTypedData,
|
|
27
|
+
* });
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare const signAndVerifyTypedData: (params: {
|
|
31
|
+
readonly client: WalletClient;
|
|
32
|
+
readonly userAddress: Address;
|
|
33
|
+
readonly typedData: TypedDataDefinition<Record<string, unknown>, string>;
|
|
34
|
+
}) => Promise<Hex>;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { verifyTypedData } from "viem";
|
|
2
|
+
import { signTypedData } from "viem/actions";
|
|
3
|
+
import { InvalidSignatureError } from "../types/index.js";
|
|
4
|
+
import { validateChainId, validateUserAddress } from "./validate.js";
|
|
5
|
+
/**
|
|
6
|
+
* Signs EIP-712 typed data with a wallet client, verifies that the produced signature recovers
|
|
7
|
+
* `userAddress`, and returns the signature.
|
|
8
|
+
*
|
|
9
|
+
* Use inside requirement `sign(...)` callbacks before converting a signature
|
|
10
|
+
* into protocol calldata or mempool payload bytes. The verification step keeps
|
|
11
|
+
* wallet/account mismatches from reaching transaction builders.
|
|
12
|
+
*
|
|
13
|
+
* @param params - Signing and verification parameters.
|
|
14
|
+
* @param params.client - Wallet client used to sign the typed data.
|
|
15
|
+
* @param params.userAddress - Address expected to own the produced signature.
|
|
16
|
+
* @param params.typedData - EIP-712 typed data to sign and verify.
|
|
17
|
+
* @returns The verified EIP-712 signature.
|
|
18
|
+
* @throws {MissingClientPropertyError} when the wallet client has no account address.
|
|
19
|
+
* @throws {AddressMismatchError} when the wallet client account differs from `userAddress`.
|
|
20
|
+
* @throws {ChainIdMismatchError} when the wallet client targets a different chain than the typed-data domain.
|
|
21
|
+
* @throws {InvalidSignatureError} when the signature does not recover to `userAddress`.
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { signAndVerifyTypedData } from "@morpho-org/morpho-sdk";
|
|
25
|
+
*
|
|
26
|
+
* const signature = await signAndVerifyTypedData({
|
|
27
|
+
* client: walletClient,
|
|
28
|
+
* userAddress: maker,
|
|
29
|
+
* typedData: offerRootTypedData,
|
|
30
|
+
* });
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export const signAndVerifyTypedData = async (params) => {
|
|
34
|
+
const { client, userAddress, typedData } = params;
|
|
35
|
+
const account = client.account;
|
|
36
|
+
validateUserAddress(account?.address, userAddress);
|
|
37
|
+
const typedDataChainId = typedData.domain?.chainId;
|
|
38
|
+
if (typedDataChainId != null) {
|
|
39
|
+
validateChainId(client.chain?.id, Number(typedDataChainId));
|
|
40
|
+
}
|
|
41
|
+
const signature = await signTypedData(client, {
|
|
42
|
+
...typedData,
|
|
43
|
+
account,
|
|
44
|
+
});
|
|
45
|
+
const isValid = await verifyTypedData({
|
|
46
|
+
...typedData,
|
|
47
|
+
address: userAddress,
|
|
48
|
+
signature,
|
|
49
|
+
});
|
|
50
|
+
if (!isValid) {
|
|
51
|
+
throw new InvalidSignatureError();
|
|
52
|
+
}
|
|
53
|
+
return signature;
|
|
54
|
+
};
|
|
@@ -1,8 +1,24 @@
|
|
|
1
1
|
import { type AccrualPosition, type MarketId } from "@morpho-org/blue-sdk";
|
|
2
|
+
import type { MarketInput as MidnightMarketInput } from "@morpho-org/midnight-sdk";
|
|
2
3
|
import { type Address } from "viem";
|
|
3
4
|
import { type VaultReallocation } from "../types/index.js";
|
|
4
5
|
/** @internal */
|
|
5
6
|
export declare const compareMarketIds: (idA: MarketId, idB: MarketId) => 1 | 0 | -1;
|
|
7
|
+
/**
|
|
8
|
+
* Validates that a raw or hydrated Midnight market belongs to the expected chain.
|
|
9
|
+
*
|
|
10
|
+
* @param market - Midnight market params or hydrated market state.
|
|
11
|
+
* @param chainId - Expected EIP-155 chain id.
|
|
12
|
+
* @returns Nothing when the market belongs to `chainId`.
|
|
13
|
+
* @throws {ChainIdMismatchError} when the market belongs to another chain.
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { validateMidnightMarketChainId } from "@morpho-org/morpho-sdk";
|
|
17
|
+
*
|
|
18
|
+
* validateMidnightMarketChainId(marketParams, 8453);
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare const validateMidnightMarketChainId: (market: MidnightMarketInput, chainId: number) => void;
|
|
6
22
|
/**
|
|
7
23
|
* Asserts that the client has a connected account AND that it matches
|
|
8
24
|
* the provided user address.
|
|
@@ -134,15 +150,38 @@ export declare const validateRepayShares: (params: {
|
|
|
134
150
|
*
|
|
135
151
|
* @param reallocations - The reallocations to validate.
|
|
136
152
|
* @param targetMarketId - The ID of the operation's target market. No withdrawal may reference this market.
|
|
153
|
+
* @returns Nothing when every reallocation is valid.
|
|
154
|
+
* @throws {NegativeInputError} when a reallocation fee is negative.
|
|
155
|
+
* @throws {EmptyReallocationWithdrawalsError} when a reallocation has no withdrawals.
|
|
156
|
+
* @throws {NonPositiveInputError} when a withdrawal amount is non-positive.
|
|
157
|
+
* @throws {ReallocationWithdrawalOnTargetMarketError} when a withdrawal references the target market.
|
|
158
|
+
* @throws {UnsortedReallocationWithdrawalsError} when withdrawals are not strictly market-id sorted.
|
|
159
|
+
* @example
|
|
160
|
+
* ```ts
|
|
161
|
+
* import type { MarketId } from "@morpho-org/blue-sdk";
|
|
162
|
+
* import { validateReallocations } from "@morpho-org/morpho-sdk";
|
|
163
|
+
* import { zeroHash } from "viem";
|
|
164
|
+
*
|
|
165
|
+
* const result: void = validateReallocations([], zeroHash as MarketId);
|
|
166
|
+
* ```
|
|
137
167
|
*/
|
|
138
168
|
export declare const validateReallocations: (reallocations: readonly VaultReallocation[], targetMarketId: MarketId) => void;
|
|
139
169
|
/**
|
|
140
170
|
* Validates that a slippage tolerance is within an acceptable range.
|
|
141
171
|
*
|
|
142
|
-
* Throws {@link
|
|
172
|
+
* Throws {@link NegativeInputError} if negative.
|
|
143
173
|
* Throws {@link ExcessiveSlippageToleranceError} if greater than {@link MAX_SLIPPAGE_TOLERANCE}.
|
|
144
174
|
*
|
|
145
175
|
* @param slippageTolerance - The slippage tolerance in WAD.
|
|
176
|
+
* @returns Nothing when the slippage tolerance is valid.
|
|
177
|
+
* @throws {NegativeInputError} when `slippageTolerance < 0n`.
|
|
178
|
+
* @throws {ExcessiveSlippageToleranceError} when the tolerance exceeds the SDK maximum.
|
|
179
|
+
* @example
|
|
180
|
+
* ```ts
|
|
181
|
+
* import { validateSlippageTolerance } from "@morpho-org/morpho-sdk";
|
|
182
|
+
*
|
|
183
|
+
* const result: void = validateSlippageTolerance(5_000000000000000n);
|
|
184
|
+
* ```
|
|
146
185
|
*/
|
|
147
186
|
export declare const validateSlippageTolerance: (slippageTolerance: bigint) => void;
|
|
148
187
|
/**
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { getChainAddresses, MathLib, ORACLE_PRICE_SCALE, } from "@morpho-org/blue-sdk";
|
|
2
2
|
import { isDefined } from "@morpho-org/morpho-ts";
|
|
3
3
|
import { isAddressEqual } from "viem";
|
|
4
|
-
import { AccrualPositionUserMismatchError, AddressMismatchError, BorrowExceedsSafeLtvError, ChainIdMismatchError, ChainWNativeMissingError, EmptyReallocationWithdrawalsError, ExcessiveSlippageToleranceError, MarketIdMismatchError, MissingClientPropertyError, MissingMarketPriceError, NativeAmountOnNonWNativeAssetError,
|
|
4
|
+
import { AccrualPositionUserMismatchError, AddressMismatchError, BorrowExceedsSafeLtvError, ChainIdMismatchError, ChainWNativeMissingError, EmptyReallocationWithdrawalsError, ExcessiveSlippageToleranceError, MarketIdMismatchError, MissingClientPropertyError, MissingMarketPriceError, NativeAmountOnNonWNativeAssetError, NegativeInputError, NonPositiveInputError, ReallocationWithdrawalOnTargetMarketError, RepayExceedsDebtError, RepaySharesExceedDebtError, UnsortedReallocationWithdrawalsError, WithdrawExceedsCollateralError, WithdrawExceedsSupplyError, WithdrawMakesPositionUnhealthyError, WithdrawSharesExceedSupplyError, } from "../types/index.js";
|
|
5
5
|
import { DEFAULT_LLTV_BUFFER, MAX_SLIPPAGE_TOLERANCE } from "./constant.js";
|
|
6
6
|
/** @internal */
|
|
7
7
|
export const compareMarketIds = (idA, idB) => {
|
|
@@ -13,6 +13,24 @@ export const compareMarketIds = (idA, idB) => {
|
|
|
13
13
|
return -1;
|
|
14
14
|
return 0;
|
|
15
15
|
};
|
|
16
|
+
/**
|
|
17
|
+
* Validates that a raw or hydrated Midnight market belongs to the expected chain.
|
|
18
|
+
*
|
|
19
|
+
* @param market - Midnight market params or hydrated market state.
|
|
20
|
+
* @param chainId - Expected EIP-155 chain id.
|
|
21
|
+
* @returns Nothing when the market belongs to `chainId`.
|
|
22
|
+
* @throws {ChainIdMismatchError} when the market belongs to another chain.
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* import { validateMidnightMarketChainId } from "@morpho-org/morpho-sdk";
|
|
26
|
+
*
|
|
27
|
+
* validateMidnightMarketChainId(marketParams, 8453);
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export const validateMidnightMarketChainId = (market, chainId) => {
|
|
31
|
+
const marketParams = "params" in market ? market.params : market;
|
|
32
|
+
validateChainId(Number(marketParams.chainId), chainId);
|
|
33
|
+
};
|
|
16
34
|
/**
|
|
17
35
|
* Asserts that the client has a connected account AND that it matches
|
|
18
36
|
* the provided user address.
|
|
@@ -212,11 +230,25 @@ export const validateRepayShares = (params) => {
|
|
|
212
230
|
*
|
|
213
231
|
* @param reallocations - The reallocations to validate.
|
|
214
232
|
* @param targetMarketId - The ID of the operation's target market. No withdrawal may reference this market.
|
|
233
|
+
* @returns Nothing when every reallocation is valid.
|
|
234
|
+
* @throws {NegativeInputError} when a reallocation fee is negative.
|
|
235
|
+
* @throws {EmptyReallocationWithdrawalsError} when a reallocation has no withdrawals.
|
|
236
|
+
* @throws {NonPositiveInputError} when a withdrawal amount is non-positive.
|
|
237
|
+
* @throws {ReallocationWithdrawalOnTargetMarketError} when a withdrawal references the target market.
|
|
238
|
+
* @throws {UnsortedReallocationWithdrawalsError} when withdrawals are not strictly market-id sorted.
|
|
239
|
+
* @example
|
|
240
|
+
* ```ts
|
|
241
|
+
* import type { MarketId } from "@morpho-org/blue-sdk";
|
|
242
|
+
* import { validateReallocations } from "@morpho-org/morpho-sdk";
|
|
243
|
+
* import { zeroHash } from "viem";
|
|
244
|
+
*
|
|
245
|
+
* const result: void = validateReallocations([], zeroHash as MarketId);
|
|
246
|
+
* ```
|
|
215
247
|
*/
|
|
216
248
|
export const validateReallocations = (reallocations, targetMarketId) => {
|
|
217
249
|
for (const r of reallocations) {
|
|
218
250
|
if (r.fee < 0n) {
|
|
219
|
-
throw new
|
|
251
|
+
throw new NegativeInputError("reallocation.fee", r.fee);
|
|
220
252
|
}
|
|
221
253
|
if (r.withdrawals.length === 0) {
|
|
222
254
|
throw new EmptyReallocationWithdrawalsError(r.vault);
|
|
@@ -224,7 +256,7 @@ export const validateReallocations = (reallocations, targetMarketId) => {
|
|
|
224
256
|
let prevId;
|
|
225
257
|
for (const w of r.withdrawals) {
|
|
226
258
|
if (w.amount <= 0n) {
|
|
227
|
-
throw new
|
|
259
|
+
throw new NonPositiveInputError(`reallocation.withdrawals[${w.marketParams.id}].amount`, w.amount);
|
|
228
260
|
}
|
|
229
261
|
if (w.marketParams.id === targetMarketId) {
|
|
230
262
|
throw new ReallocationWithdrawalOnTargetMarketError(r.vault, w.marketParams.id);
|
|
@@ -240,14 +272,23 @@ export const validateReallocations = (reallocations, targetMarketId) => {
|
|
|
240
272
|
/**
|
|
241
273
|
* Validates that a slippage tolerance is within an acceptable range.
|
|
242
274
|
*
|
|
243
|
-
* Throws {@link
|
|
275
|
+
* Throws {@link NegativeInputError} if negative.
|
|
244
276
|
* Throws {@link ExcessiveSlippageToleranceError} if greater than {@link MAX_SLIPPAGE_TOLERANCE}.
|
|
245
277
|
*
|
|
246
278
|
* @param slippageTolerance - The slippage tolerance in WAD.
|
|
279
|
+
* @returns Nothing when the slippage tolerance is valid.
|
|
280
|
+
* @throws {NegativeInputError} when `slippageTolerance < 0n`.
|
|
281
|
+
* @throws {ExcessiveSlippageToleranceError} when the tolerance exceeds the SDK maximum.
|
|
282
|
+
* @example
|
|
283
|
+
* ```ts
|
|
284
|
+
* import { validateSlippageTolerance } from "@morpho-org/morpho-sdk";
|
|
285
|
+
*
|
|
286
|
+
* const result: void = validateSlippageTolerance(5_000000000000000n);
|
|
287
|
+
* ```
|
|
247
288
|
*/
|
|
248
289
|
export const validateSlippageTolerance = (slippageTolerance) => {
|
|
249
290
|
if (slippageTolerance < 0n) {
|
|
250
|
-
throw new
|
|
291
|
+
throw new NegativeInputError("slippageTolerance", slippageTolerance);
|
|
251
292
|
}
|
|
252
293
|
if (slippageTolerance > MAX_SLIPPAGE_TOLERANCE) {
|
|
253
294
|
throw new ExcessiveSlippageToleranceError(slippageTolerance);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type Address } from "viem";
|
|
2
|
+
/**
|
|
3
|
+
* Validates that a Midnight authorization grant targets a supported protocol operator.
|
|
4
|
+
*
|
|
5
|
+
* @param params - Authorization target validation parameters.
|
|
6
|
+
* @param params.chainId - Chain id used to resolve supported Midnight operators.
|
|
7
|
+
* @param params.authorized - Operator address receiving authorization.
|
|
8
|
+
* @returns Nothing after the operator matches MidnightBundles or a supported ratifier.
|
|
9
|
+
* @throws {UnsupportedMidnightAuthorizationTargetError} when `authorized` is not a supported operator.
|
|
10
|
+
* @internal
|
|
11
|
+
*/
|
|
12
|
+
export declare const validateMidnightAuthorizationTarget: (params: {
|
|
13
|
+
readonly chainId: number;
|
|
14
|
+
readonly authorized: Address;
|
|
15
|
+
}) => void;
|