@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,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.midnightRedeem = void 0;
|
|
4
|
+
const midnight_sdk_1 = require("@morpho-org/midnight-sdk");
|
|
5
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
6
|
+
const viem_1 = require("viem");
|
|
7
|
+
const index_js_1 = require("../../helpers/index.js");
|
|
8
|
+
const validateMidnightMarket_js_1 = require("../../helpers/validateMidnightMarket.js");
|
|
9
|
+
const index_js_2 = require("../../types/index.js");
|
|
10
|
+
/**
|
|
11
|
+
* Encodes a direct Midnight credit redemption.
|
|
12
|
+
*
|
|
13
|
+
* Use this low-level builder after the caller has fetched and checked position
|
|
14
|
+
* credit and market withdrawable liquidity. App flows should usually call
|
|
15
|
+
* `client.morpho.midnight(chainId).redeem(...)`, which performs those checks
|
|
16
|
+
* from the supplied `positionData` snapshot before exposing `buildTx`.
|
|
17
|
+
*
|
|
18
|
+
* @param params.chainId - Chain id used to resolve `Midnight`.
|
|
19
|
+
* @param params.market - Midnight market whose credit is redeemed.
|
|
20
|
+
* @param params.units - Credit units to withdraw.
|
|
21
|
+
* @param params.onBehalf - Account whose credit balance is reduced.
|
|
22
|
+
* @param params.receiver - Optional receiver for withdrawn loan assets; defaults to `onBehalf`.
|
|
23
|
+
* @param params.metadata - Optional analytics metadata appended to calldata.
|
|
24
|
+
* @returns A deep-frozen `Transaction<MidnightRedeemAction>` targeting `Midnight`.
|
|
25
|
+
* @throws {NonPositiveInputError} when `units <= 0n`.
|
|
26
|
+
* @throws {ChainIdMismatchError} when the market targets another chain.
|
|
27
|
+
* @throws {MidnightMarketAddressMismatchError} when the market targets another Midnight deployment.
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* import { midnightRedeem } from "@morpho-org/morpho-sdk";
|
|
31
|
+
*
|
|
32
|
+
* const tx = midnightRedeem({
|
|
33
|
+
* chainId: 8453,
|
|
34
|
+
* market: marketData.params,
|
|
35
|
+
* units: positionData.faceValue,
|
|
36
|
+
* onBehalf: user,
|
|
37
|
+
* });
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
const midnightRedeem = (params) => {
|
|
41
|
+
if (params.units <= 0n) {
|
|
42
|
+
throw new index_js_2.NonPositiveInputError("units", params.units);
|
|
43
|
+
}
|
|
44
|
+
// Reject markets from another chain deployment before encoding the call.
|
|
45
|
+
(0, validateMidnightMarket_js_1.validateMidnightMarket)({ market: params.market, chainId: params.chainId });
|
|
46
|
+
const marketId = midnight_sdk_1.MarketUtils.toId(params.market);
|
|
47
|
+
const midnight = (0, morpho_ts_1.getChainAddress)(params.chainId, "midnight");
|
|
48
|
+
const receiver = params.receiver ?? params.onBehalf;
|
|
49
|
+
let tx = {
|
|
50
|
+
to: midnight,
|
|
51
|
+
value: 0n,
|
|
52
|
+
data: (0, viem_1.encodeFunctionData)({
|
|
53
|
+
abi: midnight_sdk_1.midnightAbi,
|
|
54
|
+
functionName: "withdraw",
|
|
55
|
+
args: [
|
|
56
|
+
midnight_sdk_1.MarketUtils.toStruct(params.market),
|
|
57
|
+
params.units,
|
|
58
|
+
params.onBehalf,
|
|
59
|
+
receiver,
|
|
60
|
+
],
|
|
61
|
+
}),
|
|
62
|
+
};
|
|
63
|
+
if (params.metadata) {
|
|
64
|
+
tx = (0, index_js_1.addTransactionMetadata)(tx, params.metadata);
|
|
65
|
+
}
|
|
66
|
+
return (0, morpho_ts_1.deepFreeze)({
|
|
67
|
+
...tx,
|
|
68
|
+
action: {
|
|
69
|
+
type: "midnightRedeem",
|
|
70
|
+
args: {
|
|
71
|
+
market: marketId,
|
|
72
|
+
units: params.units,
|
|
73
|
+
onBehalf: params.onBehalf,
|
|
74
|
+
receiver,
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
};
|
|
79
|
+
exports.midnightRedeem = midnightRedeem;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { type MarketInput } from "@morpho-org/midnight-sdk";
|
|
2
|
+
import { type Address } from "viem";
|
|
3
|
+
import { type Metadata, type MidnightRepayWithdrawCollateralAction, type Transaction } from "../../types/index.js";
|
|
4
|
+
/** Parameters for encoding a Midnight repay and/or collateral withdrawal bundle. */
|
|
5
|
+
export interface MidnightRepayWithdrawCollateralParams {
|
|
6
|
+
readonly chainId: number;
|
|
7
|
+
readonly market: MarketInput;
|
|
8
|
+
readonly repayAssets: bigint;
|
|
9
|
+
readonly withdrawCollateralAssets: bigint;
|
|
10
|
+
readonly onBehalf: Address;
|
|
11
|
+
readonly collateralIndex?: bigint;
|
|
12
|
+
/** Bundle execution deadline timestamp. Pass `maxUint256` explicitly for no expiry. */
|
|
13
|
+
readonly deadline: bigint;
|
|
14
|
+
readonly metadata?: Metadata;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Encodes a Midnight bundle that repays debt, withdraws collateral, or both.
|
|
18
|
+
*
|
|
19
|
+
* Prefer `client.morpho.midnight(chainId).repayWithdrawCollateral(...)` in app
|
|
20
|
+
* flows so loan-token approval and Midnight authorization requirements are
|
|
21
|
+
* resolved first. Use this low-level builder only when those requirements and
|
|
22
|
+
* collateral-index checks are already handled by the caller.
|
|
23
|
+
*
|
|
24
|
+
* @param params.chainId - Chain id used to resolve `MidnightBundles`.
|
|
25
|
+
* @param params.market - Midnight market whose position is updated.
|
|
26
|
+
* @param params.repayAssets - Loan assets repaid; may be zero when only withdrawing collateral.
|
|
27
|
+
* @param params.withdrawCollateralAssets - Collateral assets withdrawn; may be zero when only repaying.
|
|
28
|
+
* @param params.onBehalf - Position owner.
|
|
29
|
+
* @param params.collateralIndex - Optional collateral index for withdrawal; defaults to `0n`.
|
|
30
|
+
* @param params.deadline - Bundle execution deadline timestamp; pass `maxUint256` explicitly for no expiry.
|
|
31
|
+
* @param params.metadata - Optional analytics metadata appended to calldata.
|
|
32
|
+
* @returns A deep-frozen `Transaction<MidnightRepayWithdrawCollateralAction>` targeting `MidnightBundles`.
|
|
33
|
+
* @throws {NegativeInputError} when any amount, collateral index, or deadline is negative.
|
|
34
|
+
* @throws {NonPositiveInputError} when both repay and withdrawal amounts are zero.
|
|
35
|
+
* @throws {ChainIdMismatchError} when the market targets another chain.
|
|
36
|
+
* @throws {MidnightMarketAddressMismatchError} when the market targets another Midnight deployment.
|
|
37
|
+
* @throws {UnknownCollateralIndexError} when a positive withdrawal targets an unconfigured collateral index.
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* import { maxUint256 } from "viem";
|
|
41
|
+
* import { midnightRepayWithdrawCollateral } from "@morpho-org/morpho-sdk";
|
|
42
|
+
*
|
|
43
|
+
* const tx = midnightRepayWithdrawCollateral({
|
|
44
|
+
* chainId: 8453,
|
|
45
|
+
* market: marketData.params,
|
|
46
|
+
* repayAssets: 1_000_000n,
|
|
47
|
+
* withdrawCollateralAssets: 0n,
|
|
48
|
+
* onBehalf: user,
|
|
49
|
+
* deadline: maxUint256,
|
|
50
|
+
* });
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export declare const midnightRepayWithdrawCollateral: (params: MidnightRepayWithdrawCollateralParams) => Readonly<Transaction<MidnightRepayWithdrawCollateralAction>>;
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.midnightRepayWithdrawCollateral = void 0;
|
|
4
|
+
const midnight_sdk_1 = require("@morpho-org/midnight-sdk");
|
|
5
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
6
|
+
const viem_1 = require("viem");
|
|
7
|
+
const index_js_1 = require("../../helpers/index.js");
|
|
8
|
+
const validateMidnightMarket_js_1 = require("../../helpers/validateMidnightMarket.js");
|
|
9
|
+
const index_js_2 = require("../../types/index.js");
|
|
10
|
+
const types_js_1 = require("./types.js");
|
|
11
|
+
/**
|
|
12
|
+
* Encodes a Midnight bundle that repays debt, withdraws collateral, or both.
|
|
13
|
+
*
|
|
14
|
+
* Prefer `client.morpho.midnight(chainId).repayWithdrawCollateral(...)` in app
|
|
15
|
+
* flows so loan-token approval and Midnight authorization requirements are
|
|
16
|
+
* resolved first. Use this low-level builder only when those requirements and
|
|
17
|
+
* collateral-index checks are already handled by the caller.
|
|
18
|
+
*
|
|
19
|
+
* @param params.chainId - Chain id used to resolve `MidnightBundles`.
|
|
20
|
+
* @param params.market - Midnight market whose position is updated.
|
|
21
|
+
* @param params.repayAssets - Loan assets repaid; may be zero when only withdrawing collateral.
|
|
22
|
+
* @param params.withdrawCollateralAssets - Collateral assets withdrawn; may be zero when only repaying.
|
|
23
|
+
* @param params.onBehalf - Position owner.
|
|
24
|
+
* @param params.collateralIndex - Optional collateral index for withdrawal; defaults to `0n`.
|
|
25
|
+
* @param params.deadline - Bundle execution deadline timestamp; pass `maxUint256` explicitly for no expiry.
|
|
26
|
+
* @param params.metadata - Optional analytics metadata appended to calldata.
|
|
27
|
+
* @returns A deep-frozen `Transaction<MidnightRepayWithdrawCollateralAction>` targeting `MidnightBundles`.
|
|
28
|
+
* @throws {NegativeInputError} when any amount, collateral index, or deadline is negative.
|
|
29
|
+
* @throws {NonPositiveInputError} when both repay and withdrawal amounts are zero.
|
|
30
|
+
* @throws {ChainIdMismatchError} when the market targets another chain.
|
|
31
|
+
* @throws {MidnightMarketAddressMismatchError} when the market targets another Midnight deployment.
|
|
32
|
+
* @throws {UnknownCollateralIndexError} when a positive withdrawal targets an unconfigured collateral index.
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* import { maxUint256 } from "viem";
|
|
36
|
+
* import { midnightRepayWithdrawCollateral } from "@morpho-org/morpho-sdk";
|
|
37
|
+
*
|
|
38
|
+
* const tx = midnightRepayWithdrawCollateral({
|
|
39
|
+
* chainId: 8453,
|
|
40
|
+
* market: marketData.params,
|
|
41
|
+
* repayAssets: 1_000_000n,
|
|
42
|
+
* withdrawCollateralAssets: 0n,
|
|
43
|
+
* onBehalf: user,
|
|
44
|
+
* deadline: maxUint256,
|
|
45
|
+
* });
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
const midnightRepayWithdrawCollateral = (params) => {
|
|
49
|
+
if (params.repayAssets < 0n) {
|
|
50
|
+
throw new index_js_2.NegativeInputError("repayAssets", params.repayAssets);
|
|
51
|
+
}
|
|
52
|
+
if (params.withdrawCollateralAssets < 0n) {
|
|
53
|
+
throw new index_js_2.NegativeInputError("withdrawCollateralAssets", params.withdrawCollateralAssets);
|
|
54
|
+
}
|
|
55
|
+
if (params.deadline < 0n) {
|
|
56
|
+
throw new index_js_2.NegativeInputError("deadline", params.deadline);
|
|
57
|
+
}
|
|
58
|
+
const collateralWithdrawals = params.withdrawCollateralAssets > 0n
|
|
59
|
+
? [
|
|
60
|
+
{
|
|
61
|
+
collateralIndex: params.collateralIndex ?? 0n,
|
|
62
|
+
assets: params.withdrawCollateralAssets,
|
|
63
|
+
},
|
|
64
|
+
]
|
|
65
|
+
: [];
|
|
66
|
+
for (const [index, withdrawal] of collateralWithdrawals.entries()) {
|
|
67
|
+
if (withdrawal.collateralIndex < 0n) {
|
|
68
|
+
throw new index_js_2.NegativeInputError(`collateralWithdrawals[${index}].collateralIndex`, withdrawal.collateralIndex);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (params.repayAssets === 0n &&
|
|
72
|
+
collateralWithdrawals.every((withdrawal) => withdrawal.assets === 0n)) {
|
|
73
|
+
throw new index_js_2.NonPositiveInputError("repay or withdraw amount", 0n);
|
|
74
|
+
}
|
|
75
|
+
// Reject markets from another chain deployment before encoding the bundle.
|
|
76
|
+
(0, validateMidnightMarket_js_1.validateMidnightMarket)({ market: params.market, chainId: params.chainId });
|
|
77
|
+
const marketId = midnight_sdk_1.MarketUtils.toId(params.market);
|
|
78
|
+
const market = midnight_sdk_1.MarketUtils.toStruct(params.market);
|
|
79
|
+
for (const withdrawal of collateralWithdrawals) {
|
|
80
|
+
// Validate that every withdrawal targets a configured collateral.
|
|
81
|
+
midnight_sdk_1.MarketUtils.getCollateralByIndex(market, withdrawal.collateralIndex);
|
|
82
|
+
}
|
|
83
|
+
const midnightBundles = (0, morpho_ts_1.getChainAddress)(params.chainId, "midnightBundles");
|
|
84
|
+
let tx = {
|
|
85
|
+
to: midnightBundles,
|
|
86
|
+
value: 0n,
|
|
87
|
+
data: (0, viem_1.encodeFunctionData)({
|
|
88
|
+
abi: midnight_sdk_1.midnightBundlesAbi,
|
|
89
|
+
functionName: "midnightBundlesV1RepayAndWithdrawCollateral",
|
|
90
|
+
args: [
|
|
91
|
+
market,
|
|
92
|
+
params.repayAssets,
|
|
93
|
+
params.onBehalf,
|
|
94
|
+
{ kind: types_js_1.PermitKind.None, data: "0x" },
|
|
95
|
+
collateralWithdrawals,
|
|
96
|
+
params.onBehalf,
|
|
97
|
+
0n,
|
|
98
|
+
viem_1.zeroAddress,
|
|
99
|
+
params.deadline,
|
|
100
|
+
],
|
|
101
|
+
}),
|
|
102
|
+
};
|
|
103
|
+
if (params.metadata) {
|
|
104
|
+
tx = (0, index_js_1.addTransactionMetadata)(tx, params.metadata);
|
|
105
|
+
}
|
|
106
|
+
return (0, morpho_ts_1.deepFreeze)({
|
|
107
|
+
...tx,
|
|
108
|
+
action: {
|
|
109
|
+
type: "midnightRepayWithdrawCollateral",
|
|
110
|
+
args: {
|
|
111
|
+
market: marketId,
|
|
112
|
+
repayAssets: params.repayAssets,
|
|
113
|
+
collateralWithdrawals: collateralWithdrawals.length,
|
|
114
|
+
onBehalf: params.onBehalf,
|
|
115
|
+
collateralReceiver: params.onBehalf,
|
|
116
|
+
deadline: params.deadline,
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
};
|
|
121
|
+
exports.midnightRepayWithdrawCollateral = midnightRepayWithdrawCollateral;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { type Address, type Hex } from "viem";
|
|
2
|
+
import type { Metadata, SetterRatifierRatifyRootAction, Transaction } from "../../types/index.js";
|
|
3
|
+
/** Parameters for {@link setterRatifierRatifyRoot}. */
|
|
4
|
+
export interface SetterRatifierRatifyRootParams {
|
|
5
|
+
/** Chain id used to resolve the SetterRatifier deployment. */
|
|
6
|
+
readonly chainId: number;
|
|
7
|
+
/** Maker whose offer-tree root is being ratified. */
|
|
8
|
+
readonly maker: Address;
|
|
9
|
+
/** Offer-tree root to ratify or unratify. */
|
|
10
|
+
readonly root: Hex;
|
|
11
|
+
/** Whether the root should be ratified. Defaults to `true`. */
|
|
12
|
+
readonly isRootRatified?: boolean;
|
|
13
|
+
/** Optional metadata appended to the transaction calldata. */
|
|
14
|
+
readonly metadata?: Metadata;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Encodes a SetterRatifier root-ratification transaction.
|
|
18
|
+
*
|
|
19
|
+
* Use this after building a Setter-ratified offer tree and before submitting
|
|
20
|
+
* its mempool payload. Entity make-offer flows expose the same transaction
|
|
21
|
+
* through `getRequirements()` when the root is not already approved.
|
|
22
|
+
*
|
|
23
|
+
* @param params.chainId - Chain id used to resolve `SetterRatifier`.
|
|
24
|
+
* @param params.maker - Maker whose root approval is updated.
|
|
25
|
+
* @param params.root - Offer-tree root to approve or revoke.
|
|
26
|
+
* @param params.isRootRatified - Optional target state; defaults to `true`.
|
|
27
|
+
* @param params.metadata - Optional analytics metadata appended to calldata.
|
|
28
|
+
* @returns A deep-frozen `Transaction<SetterRatifierRatifyRootAction>` targeting `SetterRatifier`.
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* import { setterRatifierRatifyRoot } from "@morpho-org/morpho-sdk";
|
|
32
|
+
*
|
|
33
|
+
* const tx = setterRatifierRatifyRoot({
|
|
34
|
+
* chainId: 8453,
|
|
35
|
+
* maker,
|
|
36
|
+
* root,
|
|
37
|
+
* });
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export declare const setterRatifierRatifyRoot: (params: SetterRatifierRatifyRootParams) => Readonly<Transaction<SetterRatifierRatifyRootAction>>;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.setterRatifierRatifyRoot = void 0;
|
|
4
|
+
const midnight_sdk_1 = require("@morpho-org/midnight-sdk");
|
|
5
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
6
|
+
const viem_1 = require("viem");
|
|
7
|
+
const index_js_1 = require("../../helpers/index.js");
|
|
8
|
+
/**
|
|
9
|
+
* Encodes a SetterRatifier root-ratification transaction.
|
|
10
|
+
*
|
|
11
|
+
* Use this after building a Setter-ratified offer tree and before submitting
|
|
12
|
+
* its mempool payload. Entity make-offer flows expose the same transaction
|
|
13
|
+
* through `getRequirements()` when the root is not already approved.
|
|
14
|
+
*
|
|
15
|
+
* @param params.chainId - Chain id used to resolve `SetterRatifier`.
|
|
16
|
+
* @param params.maker - Maker whose root approval is updated.
|
|
17
|
+
* @param params.root - Offer-tree root to approve or revoke.
|
|
18
|
+
* @param params.isRootRatified - Optional target state; defaults to `true`.
|
|
19
|
+
* @param params.metadata - Optional analytics metadata appended to calldata.
|
|
20
|
+
* @returns A deep-frozen `Transaction<SetterRatifierRatifyRootAction>` targeting `SetterRatifier`.
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* import { setterRatifierRatifyRoot } from "@morpho-org/morpho-sdk";
|
|
24
|
+
*
|
|
25
|
+
* const tx = setterRatifierRatifyRoot({
|
|
26
|
+
* chainId: 8453,
|
|
27
|
+
* maker,
|
|
28
|
+
* root,
|
|
29
|
+
* });
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
const setterRatifierRatifyRoot = (params) => {
|
|
33
|
+
const isRootRatified = params.isRootRatified ?? true;
|
|
34
|
+
const setterRatifier = (0, morpho_ts_1.getChainAddress)(params.chainId, "setterRatifier");
|
|
35
|
+
let tx = {
|
|
36
|
+
to: setterRatifier,
|
|
37
|
+
value: 0n,
|
|
38
|
+
data: (0, viem_1.encodeFunctionData)({
|
|
39
|
+
abi: midnight_sdk_1.setterRatifierAbi,
|
|
40
|
+
functionName: "setIsRootRatified",
|
|
41
|
+
args: [params.maker, params.root, isRootRatified],
|
|
42
|
+
}),
|
|
43
|
+
};
|
|
44
|
+
if (params.metadata) {
|
|
45
|
+
tx = (0, index_js_1.addTransactionMetadata)(tx, params.metadata);
|
|
46
|
+
}
|
|
47
|
+
return (0, morpho_ts_1.deepFreeze)({
|
|
48
|
+
...tx,
|
|
49
|
+
action: {
|
|
50
|
+
type: "setterRatifierRatifyRoot",
|
|
51
|
+
args: {
|
|
52
|
+
maker: params.maker,
|
|
53
|
+
root: params.root,
|
|
54
|
+
isRootRatified,
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
};
|
|
59
|
+
exports.setterRatifierRatifyRoot = setterRatifierRatifyRoot;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { type MarketInput } from "@morpho-org/midnight-sdk";
|
|
2
|
+
import { type Address } from "viem";
|
|
3
|
+
import { type Metadata, type MidnightSupplyCollateralAction, type Transaction } from "../../types/index.js";
|
|
4
|
+
/** Parameters for encoding a direct Midnight collateral supply. */
|
|
5
|
+
export interface MidnightSupplyCollateralParams {
|
|
6
|
+
readonly chainId: number;
|
|
7
|
+
readonly market: MarketInput;
|
|
8
|
+
readonly collateralIndex?: bigint;
|
|
9
|
+
readonly assets: bigint;
|
|
10
|
+
readonly onBehalf: Address;
|
|
11
|
+
readonly metadata?: Metadata;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Encodes a direct Midnight collateral supply.
|
|
15
|
+
*
|
|
16
|
+
* Use this low-level builder when a flow already knows the market, collateral
|
|
17
|
+
* index, and approval state. App flows should usually call
|
|
18
|
+
* `client.morpho.midnight(chainId).supplyCollateral(...)`, which resolves the
|
|
19
|
+
* ERC20 approval requirement before exposing `buildTx`.
|
|
20
|
+
*
|
|
21
|
+
* @param params.chainId - Chain id used to resolve `Midnight`.
|
|
22
|
+
* @param params.market - Midnight market receiving collateral.
|
|
23
|
+
* @param params.collateralIndex - Optional collateral index; defaults to `0n`.
|
|
24
|
+
* @param params.assets - Collateral assets to supply.
|
|
25
|
+
* @param params.onBehalf - Account whose collateral balance increases.
|
|
26
|
+
* @param params.metadata - Optional analytics metadata appended to calldata.
|
|
27
|
+
* @returns A deep-frozen `Transaction<MidnightSupplyCollateralAction>` targeting `Midnight`.
|
|
28
|
+
* @throws {NonPositiveInputError} when `assets <= 0n`.
|
|
29
|
+
* @throws {ChainIdMismatchError} when the market targets another chain.
|
|
30
|
+
* @throws {MidnightMarketAddressMismatchError} when the market targets another Midnight deployment.
|
|
31
|
+
* @throws {UnknownCollateralIndexError} when `collateralIndex` is not configured on the market.
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* import { midnightSupplyCollateral } from "@morpho-org/morpho-sdk";
|
|
35
|
+
*
|
|
36
|
+
* const tx = midnightSupplyCollateral({
|
|
37
|
+
* chainId: 8453,
|
|
38
|
+
* market: marketData.params,
|
|
39
|
+
* collateralIndex: 0n,
|
|
40
|
+
* assets: 2_000_000n,
|
|
41
|
+
* onBehalf: user,
|
|
42
|
+
* });
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export declare const midnightSupplyCollateral: (params: MidnightSupplyCollateralParams) => Readonly<Transaction<MidnightSupplyCollateralAction>>;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.midnightSupplyCollateral = void 0;
|
|
4
|
+
const midnight_sdk_1 = require("@morpho-org/midnight-sdk");
|
|
5
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
6
|
+
const viem_1 = require("viem");
|
|
7
|
+
const index_js_1 = require("../../helpers/index.js");
|
|
8
|
+
const validateMidnightMarket_js_1 = require("../../helpers/validateMidnightMarket.js");
|
|
9
|
+
const index_js_2 = require("../../types/index.js");
|
|
10
|
+
/**
|
|
11
|
+
* Encodes a direct Midnight collateral supply.
|
|
12
|
+
*
|
|
13
|
+
* Use this low-level builder when a flow already knows the market, collateral
|
|
14
|
+
* index, and approval state. App flows should usually call
|
|
15
|
+
* `client.morpho.midnight(chainId).supplyCollateral(...)`, which resolves the
|
|
16
|
+
* ERC20 approval requirement before exposing `buildTx`.
|
|
17
|
+
*
|
|
18
|
+
* @param params.chainId - Chain id used to resolve `Midnight`.
|
|
19
|
+
* @param params.market - Midnight market receiving collateral.
|
|
20
|
+
* @param params.collateralIndex - Optional collateral index; defaults to `0n`.
|
|
21
|
+
* @param params.assets - Collateral assets to supply.
|
|
22
|
+
* @param params.onBehalf - Account whose collateral balance increases.
|
|
23
|
+
* @param params.metadata - Optional analytics metadata appended to calldata.
|
|
24
|
+
* @returns A deep-frozen `Transaction<MidnightSupplyCollateralAction>` targeting `Midnight`.
|
|
25
|
+
* @throws {NonPositiveInputError} when `assets <= 0n`.
|
|
26
|
+
* @throws {ChainIdMismatchError} when the market targets another chain.
|
|
27
|
+
* @throws {MidnightMarketAddressMismatchError} when the market targets another Midnight deployment.
|
|
28
|
+
* @throws {UnknownCollateralIndexError} when `collateralIndex` is not configured on the market.
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* import { midnightSupplyCollateral } from "@morpho-org/morpho-sdk";
|
|
32
|
+
*
|
|
33
|
+
* const tx = midnightSupplyCollateral({
|
|
34
|
+
* chainId: 8453,
|
|
35
|
+
* market: marketData.params,
|
|
36
|
+
* collateralIndex: 0n,
|
|
37
|
+
* assets: 2_000_000n,
|
|
38
|
+
* onBehalf: user,
|
|
39
|
+
* });
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
const midnightSupplyCollateral = (params) => {
|
|
43
|
+
if (params.assets <= 0n) {
|
|
44
|
+
throw new index_js_2.NonPositiveInputError("assets", params.assets);
|
|
45
|
+
}
|
|
46
|
+
// Reject markets from another chain deployment before encoding the call.
|
|
47
|
+
(0, validateMidnightMarket_js_1.validateMidnightMarket)({ market: params.market, chainId: params.chainId });
|
|
48
|
+
const marketId = midnight_sdk_1.MarketUtils.toId(params.market);
|
|
49
|
+
const midnight = (0, morpho_ts_1.getChainAddress)(params.chainId, "midnight");
|
|
50
|
+
const collateralIndex = params.collateralIndex ?? 0n;
|
|
51
|
+
// Validate that the supplied collateral index is configured before encoding.
|
|
52
|
+
midnight_sdk_1.MarketUtils.getCollateralByIndex(params.market, collateralIndex);
|
|
53
|
+
let tx = {
|
|
54
|
+
to: midnight,
|
|
55
|
+
value: 0n,
|
|
56
|
+
data: (0, viem_1.encodeFunctionData)({
|
|
57
|
+
abi: midnight_sdk_1.midnightAbi,
|
|
58
|
+
functionName: "supplyCollateral",
|
|
59
|
+
args: [
|
|
60
|
+
midnight_sdk_1.MarketUtils.toStruct(params.market),
|
|
61
|
+
collateralIndex,
|
|
62
|
+
params.assets,
|
|
63
|
+
params.onBehalf,
|
|
64
|
+
],
|
|
65
|
+
}),
|
|
66
|
+
};
|
|
67
|
+
if (params.metadata) {
|
|
68
|
+
tx = (0, index_js_1.addTransactionMetadata)(tx, params.metadata);
|
|
69
|
+
}
|
|
70
|
+
return (0, morpho_ts_1.deepFreeze)({
|
|
71
|
+
...tx,
|
|
72
|
+
action: {
|
|
73
|
+
type: "midnightSupplyCollateral",
|
|
74
|
+
args: {
|
|
75
|
+
market: marketId,
|
|
76
|
+
collateralIndex,
|
|
77
|
+
assets: params.assets,
|
|
78
|
+
onBehalf: params.onBehalf,
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
};
|
|
83
|
+
exports.midnightSupplyCollateral = midnightSupplyCollateral;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { type MidnightSupplyCollateralTakeBorrowAction, type Transaction } from "../../types/index.js";
|
|
2
|
+
import type { MidnightTakeBorrowParams } from "./takeBorrow.js";
|
|
3
|
+
/** Parameters for encoding a collateral supply followed by a Midnight borrow take. */
|
|
4
|
+
export interface MidnightSupplyCollateralTakeBorrowParams extends MidnightTakeBorrowParams {
|
|
5
|
+
readonly collateralAssets: bigint;
|
|
6
|
+
readonly collateralIndex?: bigint;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Encodes a Midnight bundle that supplies collateral and borrows in one call.
|
|
10
|
+
*
|
|
11
|
+
* Prefer `client.morpho.midnight(chainId).supplyCollateralTakeBorrow(...)` in
|
|
12
|
+
* app flows so collateral approval and Midnight authorization requirements are
|
|
13
|
+
* resolved before building the bundle. Use this low-level builder only after
|
|
14
|
+
* market data and API takeable offers are already available.
|
|
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.collateralAssets - Collateral assets supplied before taking offers.
|
|
24
|
+
* @param params.collateralIndex - Optional collateral index; defaults to `0n`.
|
|
25
|
+
* @returns A deep-frozen `Transaction<MidnightSupplyCollateralTakeBorrowAction>` targeting `MidnightBundles`.
|
|
26
|
+
* @throws {NonPositiveInputError} when collateral assets, loan assets, or `maxUnits` are non-positive.
|
|
27
|
+
* @throws {NegativeInputError} when `deadline` is negative.
|
|
28
|
+
* @throws {EmptyMidnightTakeableOffersError} when no offers are provided.
|
|
29
|
+
* @throws {MidnightOfferSideMismatchError} when any offer is not lend-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
|
+
* @throws {UnknownCollateralIndexError} when `collateralIndex` is not configured on the market.
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* import { maxUint256 } from "viem";
|
|
37
|
+
* import { midnightSupplyCollateralTakeBorrow } from "@morpho-org/morpho-sdk";
|
|
38
|
+
*
|
|
39
|
+
* const tx = midnightSupplyCollateralTakeBorrow({
|
|
40
|
+
* chainId: 8453,
|
|
41
|
+
* market: marketData.params,
|
|
42
|
+
* collateralAssets: 2_000_000n,
|
|
43
|
+
* loanAssets: BigInt(quote.data.availableAssets),
|
|
44
|
+
* maxUnits: BigInt(quote.data.availableUnits),
|
|
45
|
+
* taker: borrower,
|
|
46
|
+
* takeableOffers: quote.data.takeableOffers,
|
|
47
|
+
* deadline: maxUint256,
|
|
48
|
+
* });
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export declare const midnightSupplyCollateralTakeBorrow: (params: MidnightSupplyCollateralTakeBorrowParams) => Readonly<Transaction<MidnightSupplyCollateralTakeBorrowAction>>;
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.midnightSupplyCollateralTakeBorrow = void 0;
|
|
4
|
+
const midnight_sdk_1 = require("@morpho-org/midnight-sdk");
|
|
5
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
6
|
+
const viem_1 = require("viem");
|
|
7
|
+
const index_js_1 = require("../../helpers/index.js");
|
|
8
|
+
const validateMidnightMarket_js_1 = require("../../helpers/validateMidnightMarket.js");
|
|
9
|
+
const validateTakeableOffers_js_1 = require("../../helpers/validateTakeableOffers.js");
|
|
10
|
+
const index_js_2 = require("../../types/index.js");
|
|
11
|
+
const types_js_1 = require("./types.js");
|
|
12
|
+
/**
|
|
13
|
+
* Encodes a Midnight bundle that supplies collateral and borrows in one call.
|
|
14
|
+
*
|
|
15
|
+
* Prefer `client.morpho.midnight(chainId).supplyCollateralTakeBorrow(...)` in
|
|
16
|
+
* app flows so collateral approval and Midnight authorization requirements are
|
|
17
|
+
* resolved before building the bundle. Use this low-level builder only after
|
|
18
|
+
* market data and API takeable offers are already available.
|
|
19
|
+
*
|
|
20
|
+
* @param params.chainId - Chain id used to resolve `MidnightBundles`.
|
|
21
|
+
* @param params.market - Midnight market traded by every takeable offer.
|
|
22
|
+
* @param params.loanAssets - Loan assets the borrower receives.
|
|
23
|
+
* @param params.maxUnits - Maximum debt units accepted from the bundle quote.
|
|
24
|
+
* @param params.taker - Borrower address executing the bundle.
|
|
25
|
+
* @param params.deadline - Bundle execution deadline timestamp; pass `maxUint256` explicitly for no expiry.
|
|
26
|
+
* @param params.takeableOffers - ABI-ready lend-side offers returned by the Midnight API.
|
|
27
|
+
* @param params.collateralAssets - Collateral assets supplied before taking offers.
|
|
28
|
+
* @param params.collateralIndex - Optional collateral index; defaults to `0n`.
|
|
29
|
+
* @returns A deep-frozen `Transaction<MidnightSupplyCollateralTakeBorrowAction>` targeting `MidnightBundles`.
|
|
30
|
+
* @throws {NonPositiveInputError} when collateral assets, loan assets, or `maxUnits` are non-positive.
|
|
31
|
+
* @throws {NegativeInputError} when `deadline` is negative.
|
|
32
|
+
* @throws {EmptyMidnightTakeableOffersError} when no offers are provided.
|
|
33
|
+
* @throws {MidnightOfferSideMismatchError} when any offer is not lend-side.
|
|
34
|
+
* @throws {MidnightTakeableOfferMarketMismatchError} when any offer belongs to another market.
|
|
35
|
+
* @throws {ChainIdMismatchError} when the market targets another chain.
|
|
36
|
+
* @throws {MidnightMarketAddressMismatchError} when the market targets another Midnight deployment.
|
|
37
|
+
* @throws {UnknownCollateralIndexError} when `collateralIndex` is not configured on the market.
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* import { maxUint256 } from "viem";
|
|
41
|
+
* import { midnightSupplyCollateralTakeBorrow } from "@morpho-org/morpho-sdk";
|
|
42
|
+
*
|
|
43
|
+
* const tx = midnightSupplyCollateralTakeBorrow({
|
|
44
|
+
* chainId: 8453,
|
|
45
|
+
* market: marketData.params,
|
|
46
|
+
* collateralAssets: 2_000_000n,
|
|
47
|
+
* loanAssets: BigInt(quote.data.availableAssets),
|
|
48
|
+
* maxUnits: BigInt(quote.data.availableUnits),
|
|
49
|
+
* taker: borrower,
|
|
50
|
+
* takeableOffers: quote.data.takeableOffers,
|
|
51
|
+
* deadline: maxUint256,
|
|
52
|
+
* });
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
const midnightSupplyCollateralTakeBorrow = (params) => {
|
|
56
|
+
if (params.collateralAssets <= 0n) {
|
|
57
|
+
throw new index_js_2.NonPositiveInputError("collateralAssets", params.collateralAssets);
|
|
58
|
+
}
|
|
59
|
+
if (params.loanAssets <= 0n) {
|
|
60
|
+
throw new index_js_2.NonPositiveInputError("loanAssets", params.loanAssets);
|
|
61
|
+
}
|
|
62
|
+
if (params.maxUnits <= 0n) {
|
|
63
|
+
throw new index_js_2.NonPositiveInputError("maxUnits", params.maxUnits);
|
|
64
|
+
}
|
|
65
|
+
if (params.deadline < 0n) {
|
|
66
|
+
throw new index_js_2.NegativeInputError("deadline", params.deadline);
|
|
67
|
+
}
|
|
68
|
+
// Reject markets from another chain deployment before encoding the bundle.
|
|
69
|
+
(0, validateMidnightMarket_js_1.validateMidnightMarket)({ market: params.market, chainId: params.chainId });
|
|
70
|
+
const marketId = (0, validateTakeableOffers_js_1.validateTakeableOffers)({
|
|
71
|
+
market: params.market,
|
|
72
|
+
takeableOffers: params.takeableOffers,
|
|
73
|
+
expectedBuy: true,
|
|
74
|
+
});
|
|
75
|
+
const midnightBundles = (0, morpho_ts_1.getChainAddress)(params.chainId, "midnightBundles");
|
|
76
|
+
const collateralIndex = params.collateralIndex ?? 0n;
|
|
77
|
+
// Validate that the collateral index is configured before encoding the bundle.
|
|
78
|
+
midnight_sdk_1.MarketUtils.getCollateralByIndex(params.market, collateralIndex);
|
|
79
|
+
const collateralSupplies = [
|
|
80
|
+
{
|
|
81
|
+
collateralIndex,
|
|
82
|
+
assets: params.collateralAssets,
|
|
83
|
+
permit: { kind: types_js_1.PermitKind.None, data: "0x" },
|
|
84
|
+
},
|
|
85
|
+
];
|
|
86
|
+
let tx = {
|
|
87
|
+
to: midnightBundles,
|
|
88
|
+
value: 0n,
|
|
89
|
+
data: (0, viem_1.encodeFunctionData)({
|
|
90
|
+
abi: midnight_sdk_1.midnightBundlesAbi,
|
|
91
|
+
functionName: "midnightBundlesV1SupplyCollateralAndSellWithAssetsTarget",
|
|
92
|
+
args: [
|
|
93
|
+
params.loanAssets,
|
|
94
|
+
params.maxUnits,
|
|
95
|
+
params.taker,
|
|
96
|
+
false,
|
|
97
|
+
params.taker,
|
|
98
|
+
collateralSupplies,
|
|
99
|
+
params.takeableOffers,
|
|
100
|
+
0n,
|
|
101
|
+
viem_1.zeroAddress,
|
|
102
|
+
viem_1.maxUint256,
|
|
103
|
+
params.deadline,
|
|
104
|
+
],
|
|
105
|
+
}),
|
|
106
|
+
};
|
|
107
|
+
if (params.metadata) {
|
|
108
|
+
tx = (0, index_js_1.addTransactionMetadata)(tx, params.metadata);
|
|
109
|
+
}
|
|
110
|
+
return (0, morpho_ts_1.deepFreeze)({
|
|
111
|
+
...tx,
|
|
112
|
+
action: {
|
|
113
|
+
type: "midnightSupplyCollateralTakeBorrow",
|
|
114
|
+
args: {
|
|
115
|
+
market: marketId,
|
|
116
|
+
collateralAssets: params.collateralAssets,
|
|
117
|
+
loanAssets: params.loanAssets,
|
|
118
|
+
maxUnits: params.maxUnits,
|
|
119
|
+
taker: params.taker,
|
|
120
|
+
receiver: params.taker,
|
|
121
|
+
collateralSupplies: collateralSupplies.length,
|
|
122
|
+
takeableOffers: params.takeableOffers.length,
|
|
123
|
+
deadline: params.deadline,
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
};
|
|
128
|
+
exports.midnightSupplyCollateralTakeBorrow = midnightSupplyCollateralTakeBorrow;
|