@morpho-org/morpho-sdk 5.2.0 → 5.3.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/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 +99 -80
- 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 +101 -82
- 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 +11 -2
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { midnightBundlesAbi } 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 { validateMidnightMarket } from "../../helpers/validateMidnightMarket.js";
|
|
6
|
+
import { validateTakeableOffers } from "../../helpers/validateTakeableOffers.js";
|
|
7
|
+
import { NegativeInputError, NonPositiveInputError, } from "../../types/index.js";
|
|
8
|
+
/**
|
|
9
|
+
* Encodes a Midnight bundle that borrows loan assets by taking lend-side offers.
|
|
10
|
+
*
|
|
11
|
+
* Prefer `client.morpho.midnight(chainId).takeBorrow(...)` in app flows so
|
|
12
|
+
* authorization requirements are resolved first. Use this low-level builder
|
|
13
|
+
* only after the Midnight API has returned takeable offers and the caller has
|
|
14
|
+
* already handled prerequisites.
|
|
15
|
+
*
|
|
16
|
+
* @param params.chainId - Chain id used to resolve `MidnightBundles`.
|
|
17
|
+
* @param params.market - Midnight market traded by every takeable offer.
|
|
18
|
+
* @param params.loanAssets - Loan assets the borrower receives.
|
|
19
|
+
* @param params.maxUnits - Maximum debt units accepted from the bundle quote.
|
|
20
|
+
* @param params.taker - Borrower address executing the bundle.
|
|
21
|
+
* @param params.deadline - Bundle execution deadline timestamp; pass `maxUint256` explicitly for no expiry.
|
|
22
|
+
* @param params.takeableOffers - ABI-ready lend-side offers returned by the Midnight API.
|
|
23
|
+
* @param params.metadata - Optional analytics metadata appended to calldata.
|
|
24
|
+
* @returns A deep-frozen `Transaction<MidnightTakeBorrowAction>` targeting `MidnightBundles`.
|
|
25
|
+
* @throws {NonPositiveInputError} when `loanAssets` or `maxUnits` is non-positive.
|
|
26
|
+
* @throws {NegativeInputError} when `deadline` is negative.
|
|
27
|
+
* @throws {EmptyMidnightTakeableOffersError} when no offers are provided.
|
|
28
|
+
* @throws {MidnightOfferSideMismatchError} when any offer is not lend-side.
|
|
29
|
+
* @throws {MidnightTakeableOfferMarketMismatchError} when any offer belongs to another market.
|
|
30
|
+
* @throws {ChainIdMismatchError} when the market targets another chain.
|
|
31
|
+
* @throws {MidnightMarketAddressMismatchError} when the market targets another Midnight deployment.
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* import { maxUint256 } from "viem";
|
|
35
|
+
* import { midnightTakeBorrow } from "@morpho-org/morpho-sdk";
|
|
36
|
+
*
|
|
37
|
+
* const tx = midnightTakeBorrow({
|
|
38
|
+
* chainId: 8453,
|
|
39
|
+
* market: marketData.params,
|
|
40
|
+
* loanAssets: BigInt(quote.data.availableAssets),
|
|
41
|
+
* maxUnits: BigInt(quote.data.availableUnits),
|
|
42
|
+
* taker: borrower,
|
|
43
|
+
* takeableOffers: quote.data.takeableOffers,
|
|
44
|
+
* deadline: maxUint256,
|
|
45
|
+
* });
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export const midnightTakeBorrow = (params) => {
|
|
49
|
+
if (params.loanAssets <= 0n) {
|
|
50
|
+
throw new NonPositiveInputError("loanAssets", params.loanAssets);
|
|
51
|
+
}
|
|
52
|
+
if (params.maxUnits <= 0n) {
|
|
53
|
+
throw new NonPositiveInputError("maxUnits", params.maxUnits);
|
|
54
|
+
}
|
|
55
|
+
if (params.deadline < 0n) {
|
|
56
|
+
throw new NegativeInputError("deadline", params.deadline);
|
|
57
|
+
}
|
|
58
|
+
// Reject markets from another chain deployment before encoding the bundle.
|
|
59
|
+
validateMidnightMarket({ market: params.market, chainId: params.chainId });
|
|
60
|
+
const marketId = validateTakeableOffers({
|
|
61
|
+
market: params.market,
|
|
62
|
+
takeableOffers: params.takeableOffers,
|
|
63
|
+
expectedBuy: true,
|
|
64
|
+
});
|
|
65
|
+
const midnightBundles = getChainAddress(params.chainId, "midnightBundles");
|
|
66
|
+
let tx = {
|
|
67
|
+
to: midnightBundles,
|
|
68
|
+
value: 0n,
|
|
69
|
+
data: encodeFunctionData({
|
|
70
|
+
abi: midnightBundlesAbi,
|
|
71
|
+
functionName: "midnightBundlesV1SupplyCollateralAndSellWithAssetsTarget",
|
|
72
|
+
args: [
|
|
73
|
+
params.loanAssets,
|
|
74
|
+
params.maxUnits,
|
|
75
|
+
params.taker,
|
|
76
|
+
false,
|
|
77
|
+
params.taker,
|
|
78
|
+
[],
|
|
79
|
+
params.takeableOffers,
|
|
80
|
+
0n,
|
|
81
|
+
zeroAddress,
|
|
82
|
+
maxUint256,
|
|
83
|
+
params.deadline,
|
|
84
|
+
],
|
|
85
|
+
}),
|
|
86
|
+
};
|
|
87
|
+
if (params.metadata) {
|
|
88
|
+
tx = addTransactionMetadata(tx, params.metadata);
|
|
89
|
+
}
|
|
90
|
+
return deepFreeze({
|
|
91
|
+
...tx,
|
|
92
|
+
action: {
|
|
93
|
+
type: "midnightTakeBorrow",
|
|
94
|
+
args: {
|
|
95
|
+
market: marketId,
|
|
96
|
+
loanAssets: params.loanAssets,
|
|
97
|
+
maxUnits: params.maxUnits,
|
|
98
|
+
taker: params.taker,
|
|
99
|
+
receiver: params.taker,
|
|
100
|
+
collateralSupplies: 0,
|
|
101
|
+
takeableOffers: params.takeableOffers.length,
|
|
102
|
+
deadline: params.deadline,
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
});
|
|
106
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { type MarketInput } from "@morpho-org/midnight-sdk";
|
|
2
|
+
import { type Address } from "viem";
|
|
3
|
+
import { type Metadata, type MidnightTakeLendAction, type Transaction } from "../../types/index.js";
|
|
4
|
+
import { type MidnightTakeableOffer } from "./types.js";
|
|
5
|
+
/** Parameters for encoding a Midnight lend take from already selected offers. */
|
|
6
|
+
export interface MidnightTakeLendParams {
|
|
7
|
+
readonly chainId: number;
|
|
8
|
+
readonly market: MarketInput;
|
|
9
|
+
readonly assets: bigint;
|
|
10
|
+
readonly minUnits: bigint;
|
|
11
|
+
readonly taker: Address;
|
|
12
|
+
readonly takeableOffers: readonly MidnightTakeableOffer[];
|
|
13
|
+
/** Bundle execution deadline timestamp. Pass `maxUint256` explicitly for no expiry. */
|
|
14
|
+
readonly deadline: bigint;
|
|
15
|
+
readonly metadata?: Metadata;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Encodes a Midnight bundle that lends loan assets by taking borrow-side offers.
|
|
19
|
+
*
|
|
20
|
+
* Prefer `client.morpho.midnight(chainId).takeLend(...)` in app flows so
|
|
21
|
+
* approval and authorization requirements are resolved first. Use this
|
|
22
|
+
* low-level builder only after the Midnight API has returned takeable offers
|
|
23
|
+
* and the caller has already handled prerequisites.
|
|
24
|
+
*
|
|
25
|
+
* @param params.chainId - Chain id used to resolve `MidnightBundles`.
|
|
26
|
+
* @param params.market - Midnight market traded by every takeable offer.
|
|
27
|
+
* @param params.assets - Loan assets the lender spends.
|
|
28
|
+
* @param params.minUnits - Minimum units accepted from the bundle quote.
|
|
29
|
+
* @param params.taker - Lender address executing the bundle.
|
|
30
|
+
* @param params.takeableOffers - ABI-ready borrow-side offers returned by the Midnight API.
|
|
31
|
+
* @param params.deadline - Bundle execution deadline timestamp; pass `maxUint256` explicitly for no expiry.
|
|
32
|
+
* @param params.metadata - Optional analytics metadata appended to calldata.
|
|
33
|
+
* @returns A deep-frozen `Transaction<MidnightTakeLendAction>` targeting `MidnightBundles`.
|
|
34
|
+
* @throws {NonPositiveInputError} when `assets <= 0n`.
|
|
35
|
+
* @throws {NegativeInputError} when `minUnits` or `deadline` is negative.
|
|
36
|
+
* @throws {EmptyMidnightTakeableOffersError} when no offers are provided.
|
|
37
|
+
* @throws {MidnightOfferSideMismatchError} when any offer is not borrow-side.
|
|
38
|
+
* @throws {MidnightTakeableOfferMarketMismatchError} when any offer belongs to another market.
|
|
39
|
+
* @throws {ChainIdMismatchError} when the market targets another chain.
|
|
40
|
+
* @throws {MidnightMarketAddressMismatchError} when the market targets another Midnight deployment.
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* import { maxUint256 } from "viem";
|
|
44
|
+
* import { midnightTakeLend } from "@morpho-org/morpho-sdk";
|
|
45
|
+
*
|
|
46
|
+
* const tx = midnightTakeLend({
|
|
47
|
+
* chainId: 8453,
|
|
48
|
+
* market: marketData.params,
|
|
49
|
+
* assets: BigInt(quote.data.availableAssets),
|
|
50
|
+
* minUnits: BigInt(quote.data.availableUnits),
|
|
51
|
+
* taker: lender,
|
|
52
|
+
* takeableOffers: quote.data.takeableOffers,
|
|
53
|
+
* deadline: maxUint256,
|
|
54
|
+
* });
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export declare const midnightTakeLend: (params: MidnightTakeLendParams) => Readonly<Transaction<MidnightTakeLendAction>>;
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { midnightBundlesAbi } 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 { validateMidnightMarket } from "../../helpers/validateMidnightMarket.js";
|
|
6
|
+
import { validateTakeableOffers } from "../../helpers/validateTakeableOffers.js";
|
|
7
|
+
import { NegativeInputError, NonPositiveInputError, } from "../../types/index.js";
|
|
8
|
+
import { PermitKind } from "./types.js";
|
|
9
|
+
/**
|
|
10
|
+
* Encodes a Midnight bundle that lends loan assets by taking borrow-side offers.
|
|
11
|
+
*
|
|
12
|
+
* Prefer `client.morpho.midnight(chainId).takeLend(...)` in app flows so
|
|
13
|
+
* approval and authorization requirements are resolved first. Use this
|
|
14
|
+
* low-level builder only after the Midnight API has returned takeable offers
|
|
15
|
+
* and the caller has already handled prerequisites.
|
|
16
|
+
*
|
|
17
|
+
* @param params.chainId - Chain id used to resolve `MidnightBundles`.
|
|
18
|
+
* @param params.market - Midnight market traded by every takeable offer.
|
|
19
|
+
* @param params.assets - Loan assets the lender spends.
|
|
20
|
+
* @param params.minUnits - Minimum units accepted from the bundle quote.
|
|
21
|
+
* @param params.taker - Lender address executing the bundle.
|
|
22
|
+
* @param params.takeableOffers - ABI-ready borrow-side offers returned by the Midnight API.
|
|
23
|
+
* @param params.deadline - Bundle execution deadline timestamp; pass `maxUint256` explicitly for no expiry.
|
|
24
|
+
* @param params.metadata - Optional analytics metadata appended to calldata.
|
|
25
|
+
* @returns A deep-frozen `Transaction<MidnightTakeLendAction>` targeting `MidnightBundles`.
|
|
26
|
+
* @throws {NonPositiveInputError} when `assets <= 0n`.
|
|
27
|
+
* @throws {NegativeInputError} when `minUnits` or `deadline` is negative.
|
|
28
|
+
* @throws {EmptyMidnightTakeableOffersError} when no offers are provided.
|
|
29
|
+
* @throws {MidnightOfferSideMismatchError} when any offer is not borrow-side.
|
|
30
|
+
* @throws {MidnightTakeableOfferMarketMismatchError} when any offer belongs to another market.
|
|
31
|
+
* @throws {ChainIdMismatchError} when the market targets another chain.
|
|
32
|
+
* @throws {MidnightMarketAddressMismatchError} when the market targets another Midnight deployment.
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* import { maxUint256 } from "viem";
|
|
36
|
+
* import { midnightTakeLend } from "@morpho-org/morpho-sdk";
|
|
37
|
+
*
|
|
38
|
+
* const tx = midnightTakeLend({
|
|
39
|
+
* chainId: 8453,
|
|
40
|
+
* market: marketData.params,
|
|
41
|
+
* assets: BigInt(quote.data.availableAssets),
|
|
42
|
+
* minUnits: BigInt(quote.data.availableUnits),
|
|
43
|
+
* taker: lender,
|
|
44
|
+
* takeableOffers: quote.data.takeableOffers,
|
|
45
|
+
* deadline: maxUint256,
|
|
46
|
+
* });
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export const midnightTakeLend = (params) => {
|
|
50
|
+
if (params.assets <= 0n) {
|
|
51
|
+
throw new NonPositiveInputError("assets", params.assets);
|
|
52
|
+
}
|
|
53
|
+
if (params.minUnits < 0n) {
|
|
54
|
+
throw new NegativeInputError("minUnits", params.minUnits);
|
|
55
|
+
}
|
|
56
|
+
if (params.deadline < 0n) {
|
|
57
|
+
throw new NegativeInputError("deadline", params.deadline);
|
|
58
|
+
}
|
|
59
|
+
// Reject markets from another chain deployment before encoding the bundle.
|
|
60
|
+
validateMidnightMarket({ market: params.market, chainId: params.chainId });
|
|
61
|
+
const marketId = validateTakeableOffers({
|
|
62
|
+
market: params.market,
|
|
63
|
+
takeableOffers: params.takeableOffers,
|
|
64
|
+
expectedBuy: false,
|
|
65
|
+
});
|
|
66
|
+
const midnightBundles = getChainAddress(params.chainId, "midnightBundles");
|
|
67
|
+
let tx = {
|
|
68
|
+
to: midnightBundles,
|
|
69
|
+
value: 0n,
|
|
70
|
+
data: encodeFunctionData({
|
|
71
|
+
abi: midnightBundlesAbi,
|
|
72
|
+
functionName: "midnightBundlesV1BuyWithAssetsTargetAndWithdrawCollateral",
|
|
73
|
+
args: [
|
|
74
|
+
params.assets,
|
|
75
|
+
params.minUnits,
|
|
76
|
+
params.taker,
|
|
77
|
+
false,
|
|
78
|
+
{ kind: PermitKind.None, data: "0x" },
|
|
79
|
+
params.takeableOffers,
|
|
80
|
+
[],
|
|
81
|
+
zeroAddress,
|
|
82
|
+
0n,
|
|
83
|
+
zeroAddress,
|
|
84
|
+
maxUint256,
|
|
85
|
+
params.deadline,
|
|
86
|
+
],
|
|
87
|
+
}),
|
|
88
|
+
};
|
|
89
|
+
if (params.metadata) {
|
|
90
|
+
tx = addTransactionMetadata(tx, params.metadata);
|
|
91
|
+
}
|
|
92
|
+
return deepFreeze({
|
|
93
|
+
...tx,
|
|
94
|
+
action: {
|
|
95
|
+
type: "midnightTakeLend",
|
|
96
|
+
args: {
|
|
97
|
+
market: marketId,
|
|
98
|
+
assets: params.assets,
|
|
99
|
+
minUnits: params.minUnits,
|
|
100
|
+
taker: params.taker,
|
|
101
|
+
takeableOffers: params.takeableOffers.length,
|
|
102
|
+
deadline: params.deadline,
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
});
|
|
106
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
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
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* ABI-ready Midnight takeable offer returned by quote/takeable-offer APIs.
|
|
30
|
+
*
|
|
31
|
+
* Pass these objects unchanged into `takeLend`, `takeBorrow`, or
|
|
32
|
+
* `supplyCollateralTakeBorrow`; the action builders validate side and market
|
|
33
|
+
* consistency before encoding the bundle.
|
|
34
|
+
*/
|
|
35
|
+
export interface MidnightTakeableOffer {
|
|
36
|
+
readonly units: bigint;
|
|
37
|
+
readonly offer: OfferStruct;
|
|
38
|
+
readonly ratifierData: Hex;
|
|
39
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
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,5 +1,5 @@
|
|
|
1
1
|
import type { Address } from "@morpho-org/blue-sdk";
|
|
2
|
-
import {
|
|
2
|
+
import type { Client } from "viem";
|
|
3
3
|
import { type AuthorizationRequirementSignature, type Requirement } from "../../../types/index.js";
|
|
4
4
|
/** Parameters for {@link encodeBlueSignatureAuthorization}. */
|
|
5
5
|
interface EncodeBlueSignatureAuthorizationParams {
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { getAuthorizationTypedData } from "@morpho-org/blue-sdk-viem";
|
|
2
2
|
import { deepFreeze, Time } from "@morpho-org/morpho-ts";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { validateUserAddress } from "../../../helpers/validate.js";
|
|
6
|
-
import { ChainIdMismatchError, InvalidSignatureError, } from "../../../types/index.js";
|
|
3
|
+
import { signAndVerifyTypedData } from "../../../helpers/signAndVerifyTypedData.js";
|
|
4
|
+
import { ChainIdMismatchError, } from "../../../types/index.js";
|
|
7
5
|
/**
|
|
8
6
|
* Builds a Morpho authorization `Requirement` that, once signed, lets `authorized` operate on
|
|
9
7
|
* Morpho on the signer's behalf through `setAuthorizationWithSig` — the offchain-signature
|
|
@@ -54,21 +52,12 @@ export const encodeBlueSignatureAuthorization = async (viemClient, params) => {
|
|
|
54
52
|
return {
|
|
55
53
|
action,
|
|
56
54
|
async sign(client, userAddress) {
|
|
57
|
-
const account = client.account;
|
|
58
|
-
validateUserAddress(account?.address, userAddress);
|
|
59
55
|
const typedData = getAuthorizationTypedData({ authorizer: userAddress, authorized, isAuthorized, nonce, deadline }, chainId);
|
|
60
|
-
const signature = await
|
|
61
|
-
|
|
62
|
-
|
|
56
|
+
const signature = await signAndVerifyTypedData({
|
|
57
|
+
client,
|
|
58
|
+
userAddress,
|
|
59
|
+
typedData,
|
|
63
60
|
});
|
|
64
|
-
const isValid = await verifyTypedData({
|
|
65
|
-
...typedData,
|
|
66
|
-
address: userAddress, // Verify against the authorizer.
|
|
67
|
-
signature,
|
|
68
|
-
});
|
|
69
|
-
if (!isValid) {
|
|
70
|
-
throw new InvalidSignatureError();
|
|
71
|
-
}
|
|
72
61
|
return deepFreeze({
|
|
73
62
|
args: {
|
|
74
63
|
owner: userAddress,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type Address } from "@morpho-org/blue-sdk";
|
|
2
|
-
import {
|
|
2
|
+
import type { ERC20ApprovalAction, Transaction } from "../../../types/index.js";
|
|
3
3
|
/** Parameters for {@link encodeErc20Approval}. */
|
|
4
4
|
interface EncodeErc20ApprovalParams {
|
|
5
5
|
token: Address;
|
|
@@ -8,18 +8,19 @@ interface EncodeErc20ApprovalParams {
|
|
|
8
8
|
chainId: number;
|
|
9
9
|
}
|
|
10
10
|
/**
|
|
11
|
-
* Encodes a deep-frozen ERC-20 approval transaction for
|
|
11
|
+
* Encodes a deep-frozen ERC-20 approval transaction for a supported SDK spender.
|
|
12
12
|
*
|
|
13
13
|
* Caps `amount` at the per-chain, per-token maximum from `MAX_TOKEN_APPROVALS` (defaults to
|
|
14
14
|
* `maxUint256`). Used by {@link getRequirementsApproval} and {@link getGeneralAdapterRequirementsPermit2}.
|
|
15
15
|
*
|
|
16
16
|
* @param params - Encoding parameters.
|
|
17
17
|
* @param params.token - ERC-20 token address to approve.
|
|
18
|
-
* @param params.spender - Address granted the allowance. Must be GeneralAdapter1
|
|
18
|
+
* @param params.spender - Address granted the allowance. Must be GeneralAdapter1, Permit2,
|
|
19
|
+
* Midnight, or MidnightBundles for the chain.
|
|
19
20
|
* @param params.amount - Allowance amount before per-token cap.
|
|
20
21
|
* @param params.chainId - The chain the transaction targets (used to resolve supported spenders and the per-token cap).
|
|
21
22
|
* @returns A deep-frozen `Transaction<ERC20ApprovalAction>` with the capped approval amount.
|
|
22
|
-
* @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not
|
|
23
|
+
* @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not a supported SDK spender for `chainId`.
|
|
23
24
|
* @example
|
|
24
25
|
* ```ts
|
|
25
26
|
* import { encodeErc20Approval } from "@morpho-org/morpho-sdk";
|
|
@@ -1,21 +1,22 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { MathLib } from "@morpho-org/blue-sdk";
|
|
2
2
|
import { deepFreeze } from "@morpho-org/morpho-ts";
|
|
3
|
-
import { encodeFunctionData, erc20Abi,
|
|
3
|
+
import { encodeFunctionData, erc20Abi, maxUint256 } from "viem";
|
|
4
4
|
import { MAX_TOKEN_APPROVALS } from "../../../helpers/constant.js";
|
|
5
|
-
import {
|
|
5
|
+
import { validateRequirementSpender } from "../../../helpers/validateRequirementSpender.js";
|
|
6
6
|
/**
|
|
7
|
-
* Encodes a deep-frozen ERC-20 approval transaction for
|
|
7
|
+
* Encodes a deep-frozen ERC-20 approval transaction for a supported SDK spender.
|
|
8
8
|
*
|
|
9
9
|
* Caps `amount` at the per-chain, per-token maximum from `MAX_TOKEN_APPROVALS` (defaults to
|
|
10
10
|
* `maxUint256`). Used by {@link getRequirementsApproval} and {@link getGeneralAdapterRequirementsPermit2}.
|
|
11
11
|
*
|
|
12
12
|
* @param params - Encoding parameters.
|
|
13
13
|
* @param params.token - ERC-20 token address to approve.
|
|
14
|
-
* @param params.spender - Address granted the allowance. Must be GeneralAdapter1
|
|
14
|
+
* @param params.spender - Address granted the allowance. Must be GeneralAdapter1, Permit2,
|
|
15
|
+
* Midnight, or MidnightBundles for the chain.
|
|
15
16
|
* @param params.amount - Allowance amount before per-token cap.
|
|
16
17
|
* @param params.chainId - The chain the transaction targets (used to resolve supported spenders and the per-token cap).
|
|
17
18
|
* @returns A deep-frozen `Transaction<ERC20ApprovalAction>` with the capped approval amount.
|
|
18
|
-
* @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not
|
|
19
|
+
* @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not a supported SDK spender for `chainId`.
|
|
19
20
|
* @example
|
|
20
21
|
* ```ts
|
|
21
22
|
* import { encodeErc20Approval } from "@morpho-org/morpho-sdk";
|
|
@@ -31,16 +32,11 @@ import { UnsupportedErc20ApprovalSpenderError, } from "../../../types/index.js";
|
|
|
31
32
|
*/
|
|
32
33
|
export const encodeErc20Approval = (params) => {
|
|
33
34
|
const { token, spender, amount, chainId } = params;
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
chainId,
|
|
40
|
-
generalAdapter1,
|
|
41
|
-
permit2,
|
|
42
|
-
});
|
|
43
|
-
}
|
|
35
|
+
validateRequirementSpender({
|
|
36
|
+
chainId,
|
|
37
|
+
spender,
|
|
38
|
+
allowed: ["generalAdapter1", "permit2", "midnight", "midnightBundles"],
|
|
39
|
+
});
|
|
44
40
|
const amountValue = MathLib.min(amount, MAX_TOKEN_APPROVALS[chainId]?.[token] ?? maxUint256);
|
|
45
41
|
return deepFreeze({
|
|
46
42
|
to: token,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import type { Address } from "@morpho-org/blue-sdk";
|
|
2
|
+
import type { Client } from "viem";
|
|
3
3
|
import { type PermitRequirementSignature, type Requirement } from "../../../types/index.js";
|
|
4
4
|
/** Parameters for {@link encodeErc20Permit}. */
|
|
5
5
|
interface EncodeErc20PermitParams {
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import { getChainAddresses } from "@morpho-org/blue-sdk";
|
|
2
1
|
import { fetchToken, getPermitTypedData } from "@morpho-org/blue-sdk-viem";
|
|
3
2
|
import { deepFreeze, Time } from "@morpho-org/morpho-ts";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import { ChainIdMismatchError, InvalidSignatureError, UnsupportedErc20ApprovalSpenderError, } from "../../../types/index.js";
|
|
3
|
+
import { signAndVerifyTypedData } from "../../../helpers/signAndVerifyTypedData.js";
|
|
4
|
+
import { validateRequirementSpender } from "../../../helpers/validateRequirementSpender.js";
|
|
5
|
+
import { ChainIdMismatchError, } from "../../../types/index.js";
|
|
8
6
|
/**
|
|
9
7
|
* Builds an EIP-2612 permit `Requirement` that, once signed, lets a supported SDK spender pull
|
|
10
8
|
* `amount` of `token`.
|
|
@@ -50,17 +48,11 @@ export const encodeErc20Permit = async (viemClient, params) => {
|
|
|
50
48
|
if (viemClient.chain?.id !== chainId) {
|
|
51
49
|
throw new ChainIdMismatchError(viemClient.chain?.id, chainId);
|
|
52
50
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
chainId,
|
|
59
|
-
generalAdapter1,
|
|
60
|
-
midnightBundles,
|
|
61
|
-
supportedSpenders: [generalAdapter1, midnightBundles],
|
|
62
|
-
});
|
|
63
|
-
}
|
|
51
|
+
validateRequirementSpender({
|
|
52
|
+
chainId,
|
|
53
|
+
spender,
|
|
54
|
+
allowed: ["generalAdapter1", "midnightBundles"],
|
|
55
|
+
});
|
|
64
56
|
const now = Time.timestamp();
|
|
65
57
|
const deadline = now + Time.s.from.h(2n);
|
|
66
58
|
const tokenData = await fetchToken(token, viemClient, {
|
|
@@ -77,8 +69,6 @@ export const encodeErc20Permit = async (viemClient, params) => {
|
|
|
77
69
|
return {
|
|
78
70
|
action,
|
|
79
71
|
async sign(client, userAddress) {
|
|
80
|
-
const account = client.account;
|
|
81
|
-
validateUserAddress(account?.address, userAddress);
|
|
82
72
|
const typedData = getPermitTypedData({
|
|
83
73
|
erc20: tokenData,
|
|
84
74
|
owner: userAddress,
|
|
@@ -87,18 +77,11 @@ export const encodeErc20Permit = async (viemClient, params) => {
|
|
|
87
77
|
nonce,
|
|
88
78
|
deadline,
|
|
89
79
|
}, chainId);
|
|
90
|
-
const signature = await
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
const isValid = await verifyTypedData({
|
|
95
|
-
...typedData,
|
|
96
|
-
address: userAddress, // Verify against the permit's owner.
|
|
97
|
-
signature,
|
|
80
|
+
const signature = await signAndVerifyTypedData({
|
|
81
|
+
client,
|
|
82
|
+
userAddress,
|
|
83
|
+
typedData,
|
|
98
84
|
});
|
|
99
|
-
if (!isValid) {
|
|
100
|
-
throw new InvalidSignatureError();
|
|
101
|
-
}
|
|
102
85
|
return deepFreeze({
|
|
103
86
|
args: {
|
|
104
87
|
owner: userAddress,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type Address } from "@morpho-org/blue-sdk";
|
|
2
|
-
import {
|
|
2
|
+
import type { PermitRequirementSignature, Requirement } from "../../../types/index.js";
|
|
3
3
|
/** Parameters for {@link encodeErc20Permit2Approve}. */
|
|
4
4
|
interface EncodeErc20Permit2ApproveParams {
|
|
5
5
|
token: Address;
|
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
import { getChainAddresses, MathLib } from "@morpho-org/blue-sdk";
|
|
2
2
|
import { getPermit2PermitTypedData } from "@morpho-org/blue-sdk-viem";
|
|
3
3
|
import { deepFreeze, Time } from "@morpho-org/morpho-ts";
|
|
4
|
-
import {
|
|
5
|
-
import { signTypedData } from "viem/actions";
|
|
6
|
-
import { validateUserAddress } from "../../../helpers/validate.js";
|
|
7
|
-
import { InvalidSignatureError, } from "../../../types/index.js";
|
|
4
|
+
import { signAndVerifyTypedData } from "../../../helpers/signAndVerifyTypedData.js";
|
|
8
5
|
/**
|
|
9
6
|
* Builds a Permit2 `Requirement` that, once signed, lets GeneralAdapter1 pull `amount` of `token`
|
|
10
7
|
* via the Permit2 contract.
|
|
@@ -52,8 +49,6 @@ export const encodeErc20Permit2Approve = (params) => {
|
|
|
52
49
|
return {
|
|
53
50
|
action,
|
|
54
51
|
async sign(client, userAddress) {
|
|
55
|
-
const account = client.account;
|
|
56
|
-
validateUserAddress(account?.address, userAddress);
|
|
57
52
|
const typedData = getPermit2PermitTypedData({
|
|
58
53
|
spender: generalAdapter1,
|
|
59
54
|
allowance: amount,
|
|
@@ -62,18 +57,11 @@ export const encodeErc20Permit2Approve = (params) => {
|
|
|
62
57
|
deadline,
|
|
63
58
|
expiration: Number(expiration),
|
|
64
59
|
}, chainId);
|
|
65
|
-
const signature = await
|
|
66
|
-
|
|
67
|
-
|
|
60
|
+
const signature = await signAndVerifyTypedData({
|
|
61
|
+
client,
|
|
62
|
+
userAddress,
|
|
63
|
+
typedData,
|
|
68
64
|
});
|
|
69
|
-
const isValid = await verifyTypedData({
|
|
70
|
-
...typedData,
|
|
71
|
-
address: userAddress,
|
|
72
|
-
signature,
|
|
73
|
-
});
|
|
74
|
-
if (!isValid) {
|
|
75
|
-
throw new InvalidSignatureError();
|
|
76
|
-
}
|
|
77
65
|
return deepFreeze({
|
|
78
66
|
args: {
|
|
79
67
|
owner: userAddress,
|
package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts
CHANGED
|
@@ -34,4 +34,4 @@ export declare const getGeneralAdapterRequirementsPermit: (viemClient: Client, p
|
|
|
34
34
|
};
|
|
35
35
|
nonce: bigint;
|
|
36
36
|
supportDeployless?: boolean;
|
|
37
|
-
}) => Promise<import("../../../index.js").Requirement<import("../../../index.js").PermitRequirementSignature>[]>;
|
|
37
|
+
}) => Promise<import("../../../index.js").Requirement<import("../../../index.js").PermitRequirementSignature, undefined>[]>;
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import type { Address } from "@morpho-org/blue-sdk";
|
|
2
2
|
import { type ERC20ApprovalAction, type Transaction } from "../../types/index.js";
|
|
3
3
|
/**
|
|
4
|
-
* Computes classic ERC-20 approval transactions for
|
|
4
|
+
* Computes classic ERC-20 approval transactions for a supported SDK spender, given the existing
|
|
5
|
+
* allowance.
|
|
6
|
+
*
|
|
7
|
+
* The spender is validated by {@link encodeErc20Approval}. Supported spenders are the chain's
|
|
8
|
+
* GeneralAdapter1, Permit2, Midnight, and MidnightBundles addresses when configured.
|
|
5
9
|
*
|
|
6
10
|
* Returns an empty array when the allowance already covers `spendAmount`. When the token is in
|
|
7
11
|
* `APPROVE_ONLY_ONCE_TOKENS` (e.g. USDT) and the existing allowance is non-zero, prepends a
|
|
@@ -9,16 +13,18 @@ import { type ERC20ApprovalAction, type Transaction } from "../../types/index.js
|
|
|
9
13
|
* before re-approving.
|
|
10
14
|
*
|
|
11
15
|
* @param params.address - ERC-20 token address.
|
|
12
|
-
* @param params.chainId - The chain the
|
|
16
|
+
* @param params.chainId - The chain the transaction targets, used to resolve supported spenders
|
|
17
|
+
* and token approval caps.
|
|
13
18
|
* @param params.args.spendAmount - The amount the bundle will actually pull.
|
|
14
19
|
* @param params.args.approvalAmount - The amount to approve (often equal to `spendAmount`, but
|
|
15
20
|
* may be `MAX_UINT_160` for Permit2 prerequisites).
|
|
16
|
-
* @param params.args.spender - Address that will be granted the approval. Must be GeneralAdapter1
|
|
21
|
+
* @param params.args.spender - Address that will be granted the approval. Must be GeneralAdapter1,
|
|
22
|
+
* Permit2, Midnight, or MidnightBundles for `chainId`.
|
|
17
23
|
* @param params.allowances - The user's current allowance of `address` for `spender`.
|
|
18
24
|
* @returns Up to two deep-frozen `Transaction<ERC20ApprovalAction>` entries: an optional reset
|
|
19
25
|
* followed by the new approval. Empty when no approval is needed.
|
|
20
26
|
* @throws {ApprovalAmountLessThanSpendAmountError} when `approvalAmount < spendAmount`.
|
|
21
|
-
* @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not
|
|
27
|
+
* @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not a supported SDK spender for `chainId`.
|
|
22
28
|
* @example
|
|
23
29
|
* ```ts
|
|
24
30
|
* import { getRequirementsApproval } from "@morpho-org/morpho-sdk";
|