@morpho-org/morpho-sdk 9.5.1 → 9.6.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 +59 -0
- package/lib/cjs/actions/blue/repayWithdrawCollateral.js +2 -2
- package/lib/cjs/actions/bundles/common.js +39 -32
- package/lib/cjs/actions/midnight/cancelAndMake.js +20 -98
- package/lib/cjs/actions/midnight/index.d.ts +2 -0
- package/lib/cjs/actions/midnight/index.js +2 -0
- package/lib/cjs/actions/midnight/repayWithdrawCollateral.js +5 -6
- package/lib/cjs/actions/midnight/supplyCollateralBatch.d.ts +39 -0
- package/lib/cjs/actions/midnight/supplyCollateralBatch.js +91 -0
- package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +31 -32
- package/lib/cjs/actions/midnight/takeBorrow.js +24 -23
- package/lib/cjs/actions/midnight/takeLend.js +24 -23
- package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.js +20 -19
- package/lib/cjs/actions/midnight/takeWithdraw.js +24 -23
- package/lib/cjs/actions/midnight/withdrawCollateralBatch.d.ts +44 -0
- package/lib/cjs/actions/midnight/withdrawCollateralBatch.js +104 -0
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +1 -0
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +1 -0
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +1 -0
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +1 -0
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +1 -0
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +1 -0
- package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +1 -0
- package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +1 -0
- package/lib/cjs/actions/vaultV1/migrateToV2.js +7 -7
- package/lib/cjs/entities/index.d.ts +1 -1
- package/lib/cjs/entities/midnight/midnight.d.ts +77 -3
- package/lib/cjs/entities/midnight/midnight.js +174 -0
- package/lib/cjs/entities/midnight/types.d.ts +16 -0
- package/lib/cjs/errors.d.ts +1 -1
- package/lib/cjs/errors.js +3 -2
- package/lib/cjs/helpers/signAndVerifyTypedData.d.ts +1 -0
- package/lib/cjs/helpers/signAndVerifyTypedData.js +12 -5
- package/lib/cjs/helpers/validateMidnightCancelAndMakePublication.d.ts +68 -0
- package/lib/cjs/helpers/validateMidnightCancelAndMakePublication.js +129 -0
- package/lib/cjs/helpers/validateMidnightCollateralBatch.d.ts +14 -0
- package/lib/cjs/helpers/validateMidnightCollateralBatch.js +27 -0
- package/lib/cjs/helpers/validateMidnightCollateralSupplies.d.ts +17 -0
- package/lib/cjs/helpers/validateMidnightCollateralSupplies.js +28 -0
- package/lib/cjs/helpers/validatePositiveInput.d.ts +10 -0
- package/lib/cjs/helpers/validatePositiveInput.js +18 -0
- package/lib/cjs/midnight/errors.d.ts +1 -1
- package/lib/cjs/midnight/errors.js +2 -1
- package/lib/cjs/types/action.d.ts +18 -3
- package/lib/cjs/types/error.d.ts +23 -2
- package/lib/cjs/types/error.js +45 -5
- package/lib/esm/actions/blue/repayWithdrawCollateral.js +2 -2
- package/lib/esm/actions/bundles/common.js +39 -32
- package/lib/esm/actions/midnight/cancelAndMake.js +25 -103
- package/lib/esm/actions/midnight/index.d.ts +2 -0
- package/lib/esm/actions/midnight/index.js +2 -0
- package/lib/esm/actions/midnight/repayWithdrawCollateral.js +5 -6
- package/lib/esm/actions/midnight/supplyCollateralBatch.d.ts +39 -0
- package/lib/esm/actions/midnight/supplyCollateralBatch.js +87 -0
- package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +33 -34
- package/lib/esm/actions/midnight/takeBorrow.js +25 -24
- package/lib/esm/actions/midnight/takeLend.js +25 -24
- package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.js +20 -19
- package/lib/esm/actions/midnight/takeWithdraw.js +25 -24
- package/lib/esm/actions/midnight/withdrawCollateralBatch.d.ts +44 -0
- package/lib/esm/actions/midnight/withdrawCollateralBatch.js +100 -0
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +1 -0
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +1 -0
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +1 -0
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +1 -0
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +1 -0
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +1 -0
- package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +1 -0
- package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +1 -0
- package/lib/esm/actions/vaultV1/migrateToV2.js +8 -8
- package/lib/esm/entities/index.d.ts +1 -1
- package/lib/esm/entities/midnight/midnight.d.ts +77 -3
- package/lib/esm/entities/midnight/midnight.js +176 -2
- package/lib/esm/entities/midnight/types.d.ts +16 -0
- package/lib/esm/errors.d.ts +1 -1
- package/lib/esm/errors.js +1 -1
- package/lib/esm/helpers/signAndVerifyTypedData.d.ts +1 -0
- package/lib/esm/helpers/signAndVerifyTypedData.js +13 -6
- package/lib/esm/helpers/validateMidnightCancelAndMakePublication.d.ts +68 -0
- package/lib/esm/helpers/validateMidnightCancelAndMakePublication.js +124 -0
- package/lib/esm/helpers/validateMidnightCollateralBatch.d.ts +14 -0
- package/lib/esm/helpers/validateMidnightCollateralBatch.js +24 -0
- package/lib/esm/helpers/validateMidnightCollateralSupplies.d.ts +17 -0
- package/lib/esm/helpers/validateMidnightCollateralSupplies.js +24 -0
- package/lib/esm/helpers/validatePositiveInput.d.ts +10 -0
- package/lib/esm/helpers/validatePositiveInput.js +14 -0
- package/lib/esm/midnight/errors.d.ts +1 -1
- package/lib/esm/midnight/errors.js +1 -1
- package/lib/esm/types/action.d.ts +18 -3
- package/lib/esm/types/error.d.ts +23 -2
- package/lib/esm/types/error.js +40 -3
- package/package.json +5 -5
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { type AccrualPosition, type Market, type MidnightFetchParams } from "@morpho-org/midnight-sdk";
|
|
2
2
|
import { type Address, type Hex } from "viem";
|
|
3
3
|
import type { MorphoClientType } from "../../types/client.js";
|
|
4
|
-
import { type MidnightCancelAndMakeAction, type MidnightGroupCancellation, type MidnightRedeemAction, type MidnightRepayWithdrawCollateralAction, type MidnightSupplyCollateralAction, type MidnightSupplyCollateralTakeBorrowAction, type MidnightTakeBorrowAction, type MidnightTakeLendAction, type MidnightTakeRepayWithdrawCollateralAction, type MidnightTakeWithdrawAction, type MidnightWithdrawCollateralAction } from "../../types/index.js";
|
|
5
|
-
import type { GetOffersDataParams, GetPositionDataParams, MakeLendParams, MakeOffersOutput, MakeOffersParams, MidnightActionOutput, OffersData, RedeemParams, RepayWithdrawCollateralParams, SupplyBlueMakeLendParams, SupplyCollateralMakeBorrowParams, SupplyCollateralParams, SupplyCollateralTakeBorrowParams, TakeBorrowParams, TakeLendParams, TakeRepayWithdrawCollateralParams, TakeWithdrawParams, WithdrawCollateralParams } from "./types.js";
|
|
4
|
+
import { type MidnightCancelAndMakeAction, type MidnightGroupCancellation, type MidnightRedeemAction, type MidnightRepayWithdrawCollateralAction, type MidnightSupplyCollateralAction, type MidnightSupplyCollateralBatchAction, type MidnightSupplyCollateralTakeBorrowAction, type MidnightTakeBorrowAction, type MidnightTakeLendAction, type MidnightTakeRepayWithdrawCollateralAction, type MidnightTakeWithdrawAction, type MidnightWithdrawCollateralAction, type MidnightWithdrawCollateralBatchAction } from "../../types/index.js";
|
|
5
|
+
import type { GetOffersDataParams, GetPositionDataParams, MakeLendParams, MakeOffersOutput, MakeOffersParams, MidnightActionOutput, OffersData, RedeemParams, RepayWithdrawCollateralParams, SupplyBlueMakeLendParams, SupplyCollateralBatchParams, SupplyCollateralMakeBorrowParams, SupplyCollateralParams, SupplyCollateralTakeBorrowParams, TakeBorrowParams, TakeLendParams, TakeRepayWithdrawCollateralParams, TakeWithdrawParams, WithdrawCollateralBatchParams, WithdrawCollateralParams } from "./types.js";
|
|
6
6
|
/**
|
|
7
7
|
* Entity methods exposed by `client.morpho.midnight(chainId)`.
|
|
8
8
|
*
|
|
@@ -30,7 +30,7 @@ import type { GetOffersDataParams, GetPositionDataParams, MakeLendParams, MakeOf
|
|
|
30
30
|
* const tx = output.buildTx();
|
|
31
31
|
* ```
|
|
32
32
|
*/
|
|
33
|
-
export type MidnightActions = Pick<MorphoMidnight, "getMarketData" | "getPositionData" | "getOffersData" | "takeLend" | "takeBorrow" | "takeWithdraw" | "supplyCollateralTakeBorrow" | "takeRepayWithdrawCollateral" | "supplyCollateral" | "withdrawCollateral" | "makeLend" | "makeBorrow" | "supplyBlueMakeLend" | "makeWithdraw" | "makeRepay" | "supplyCollateralMakeBorrow" | "redeem" | "repayWithdrawCollateral" | "cancelOffers">;
|
|
33
|
+
export type MidnightActions = Pick<MorphoMidnight, "getMarketData" | "getPositionData" | "getOffersData" | "takeLend" | "takeBorrow" | "takeWithdraw" | "supplyCollateralTakeBorrow" | "takeRepayWithdrawCollateral" | "supplyCollateral" | "supplyCollateralBatch" | "withdrawCollateral" | "withdrawCollateralBatch" | "makeLend" | "makeBorrow" | "supplyBlueMakeLend" | "makeWithdraw" | "makeRepay" | "supplyCollateralMakeBorrow" | "redeem" | "repayWithdrawCollateral" | "cancelOffers">;
|
|
34
34
|
/**
|
|
35
35
|
* Entity facade for Midnight fixed-rate action flows.
|
|
36
36
|
*
|
|
@@ -637,6 +637,52 @@ export declare class MorphoMidnight {
|
|
|
637
637
|
* ```
|
|
638
638
|
*/
|
|
639
639
|
withdrawCollateral(params: WithdrawCollateralParams): MidnightActionOutput<MidnightWithdrawCollateralAction>;
|
|
640
|
+
/**
|
|
641
|
+
* Withdraws an ordered collateral batch atomically from Midnight without bundle authorization or token pulls.
|
|
642
|
+
*
|
|
643
|
+
* Uses the supplied position snapshot to resolve maxUint256 and check collateral health.
|
|
644
|
+
* Duplicates are rejected, unlike bundle flows which sum them.
|
|
645
|
+
* No requirements or RPC reads are needed. The signing account must be accountAddress.
|
|
646
|
+
*
|
|
647
|
+
* @param params - Market, owner-bound position, collateral prices and withdrawal amount.
|
|
648
|
+
* @param params.accountAddress - Collateral owner. This account must send the transaction.
|
|
649
|
+
* @param params.marketData - Hydrated market snapshot used for validation and transaction construction.
|
|
650
|
+
* @param params.positionData - Owner-bound position snapshot returned by getPositionData.
|
|
651
|
+
* @param params.collateralPrices - Oracle prices indexed like positionData.collateral and scaled by
|
|
652
|
+
* ORACLE_PRICE_SCALE; required when the position has debt, otherwise an empty array is safe.
|
|
653
|
+
* @param params.collateralWithdrawals - Nonempty ordered withdrawals, one per index.
|
|
654
|
+
* maxUint256 resolves that index's full snapshot balance.
|
|
655
|
+
* @param params.receiver - Account receiving the withdrawn collateral; defaults to accountAddress.
|
|
656
|
+
* @returns Lazy handles for a direct Midnight withdrawal.
|
|
657
|
+
* @throws {ChainIdMismatchError} when the client or market targets another chain.
|
|
658
|
+
* @throws {MidnightCalldataEncodingError} when accountAddress is malformed or an input cannot be ABI-encoded.
|
|
659
|
+
* @throws {MidnightZeroAccountError} when accountAddress is zero.
|
|
660
|
+
* @throws {MidnightMarketAddressMismatchError} when marketData targets another Midnight deployment.
|
|
661
|
+
* @throws {MissingMarketPriceError} when debt is positive and held collateral has no supplied price.
|
|
662
|
+
* @throws {MarketIdMismatchError} when positionData belongs to another market.
|
|
663
|
+
* @throws {AccrualPositionUserMismatchError} when positionData belongs to another account.
|
|
664
|
+
* @throws {UnknownCollateralIndexError} when collateralIndex is not configured.
|
|
665
|
+
* @throws {EmptyMidnightCollateralWithdrawalsError} when the list is empty.
|
|
666
|
+
* @throws {DuplicateMidnightCollateralIndexError} when an index repeats.
|
|
667
|
+
* @throws {NonPositiveInputError} when the resolved amount is not positive.
|
|
668
|
+
* @throws {InputExceedsMaxError} when assets exceeds uint128.
|
|
669
|
+
* @throws {WithdrawExceedsCollateralError} when assets exceeds the collateral balance.
|
|
670
|
+
* @throws {WithdrawMakesPositionUnhealthyError} when remaining collateral cannot safely cover debt.
|
|
671
|
+
* @throws {MidnightZeroReceiverError} when receiver is zero.
|
|
672
|
+
* @throws {MidnightContractReceiverError} when receiver cannot safely receive collateral.
|
|
673
|
+
* @example
|
|
674
|
+
* ```ts
|
|
675
|
+
* const output = midnight.withdrawCollateralBatch({
|
|
676
|
+
* accountAddress: user,
|
|
677
|
+
* marketData,
|
|
678
|
+
* positionData,
|
|
679
|
+
* collateralPrices,
|
|
680
|
+
* collateralWithdrawals: [{ collateralIndex: 0n, assets: 1_000n }],
|
|
681
|
+
* });
|
|
682
|
+
* const tx = output.buildTx();
|
|
683
|
+
* ```
|
|
684
|
+
*/
|
|
685
|
+
withdrawCollateralBatch(params: WithdrawCollateralBatchParams): MidnightActionOutput<MidnightWithdrawCollateralBatchAction>;
|
|
640
686
|
/**
|
|
641
687
|
* Prepares a direct collateral supply using a caller-provided market snapshot.
|
|
642
688
|
*
|
|
@@ -675,6 +721,34 @@ export declare class MorphoMidnight {
|
|
|
675
721
|
* ```
|
|
676
722
|
*/
|
|
677
723
|
supplyCollateral(params: SupplyCollateralParams): MidnightActionOutput<MidnightSupplyCollateralAction>;
|
|
724
|
+
/**
|
|
725
|
+
* Prepares ordered direct collateral supplies in one Midnight multicall.
|
|
726
|
+
* Pass one entry per index: duplicates are rejected, unlike bundle flows which sum them.
|
|
727
|
+
*
|
|
728
|
+
* @param params.accountAddress - Sender and collateral beneficiary.
|
|
729
|
+
* @param params.marketData - Snapshot of the target market.
|
|
730
|
+
* @param params.collateralSupplies - Ordered supplies with optional reserved allowance amounts.
|
|
731
|
+
* @returns A lazy approval resolver and synchronous stateless transaction builder.
|
|
732
|
+
* @throws {ChainIdMismatchError} when the client or market targets another chain.
|
|
733
|
+
* @throws {MidnightMarketAddressMismatchError} when the deployment differs.
|
|
734
|
+
* @throws {MidnightCalldataEncodingError} when the account is malformed.
|
|
735
|
+
* @throws {NegativeInputError} when reserved assets are negative.
|
|
736
|
+
* @throws {NonPositiveInputError} when assets are not positive.
|
|
737
|
+
* @throws {InputExceedsMaxError} when assets exceed uint128.
|
|
738
|
+
* @throws {UnknownCollateralIndexError} when an index is not configured.
|
|
739
|
+
* @throws {EmptyMidnightCollateralSuppliesError} when the list is empty.
|
|
740
|
+
* @throws {DuplicateMidnightCollateralIndexError} when an index repeats.
|
|
741
|
+
* @throws {MidnightZeroOnBehalfError} when requirements or construction uses the zero account.
|
|
742
|
+
* @example
|
|
743
|
+
* ```ts
|
|
744
|
+
* const output = midnight.supplyCollateralBatch({
|
|
745
|
+
* accountAddress: user, marketData,
|
|
746
|
+
* collateralSupplies: [{ collateralIndex: 0n, assets: 100n }],
|
|
747
|
+
* });
|
|
748
|
+
* const tx = output.buildTx();
|
|
749
|
+
* ```
|
|
750
|
+
*/
|
|
751
|
+
supplyCollateralBatch(params: SupplyCollateralBatchParams): MidnightActionOutput<MidnightSupplyCollateralBatchAction>;
|
|
678
752
|
/**
|
|
679
753
|
* Prepares an atomic lend-offer publication or repost through `MidnightBundlesV2`.
|
|
680
754
|
*
|
|
@@ -15,6 +15,7 @@ const encodeMidnightCalldata_js_1 = require("../../helpers/encodeMidnightCalldat
|
|
|
15
15
|
const index_js_3 = require("../../helpers/index.js");
|
|
16
16
|
const validateMidnightAccount_js_1 = require("../../helpers/validateMidnightAccount.js");
|
|
17
17
|
const validateMidnightBounds_js_1 = require("../../helpers/validateMidnightBounds.js");
|
|
18
|
+
const validateMidnightCollateralBatch_js_1 = require("../../helpers/validateMidnightCollateralBatch.js");
|
|
18
19
|
const validateMidnightMarket_js_1 = require("../../helpers/validateMidnightMarket.js");
|
|
19
20
|
const validateMidnightPublication_js_1 = require("../../helpers/validateMidnightPublication.js");
|
|
20
21
|
const validateMidnightTakeFeasibility_js_1 = require("../../helpers/validateMidnightTakeFeasibility.js");
|
|
@@ -1164,6 +1165,106 @@ class MorphoMidnight {
|
|
|
1164
1165
|
}
|
|
1165
1166
|
return { getRequirements: async () => [], buildTx: () => tx };
|
|
1166
1167
|
}
|
|
1168
|
+
/**
|
|
1169
|
+
* Withdraws an ordered collateral batch atomically from Midnight without bundle authorization or token pulls.
|
|
1170
|
+
*
|
|
1171
|
+
* Uses the supplied position snapshot to resolve maxUint256 and check collateral health.
|
|
1172
|
+
* Duplicates are rejected, unlike bundle flows which sum them.
|
|
1173
|
+
* No requirements or RPC reads are needed. The signing account must be accountAddress.
|
|
1174
|
+
*
|
|
1175
|
+
* @param params - Market, owner-bound position, collateral prices and withdrawal amount.
|
|
1176
|
+
* @param params.accountAddress - Collateral owner. This account must send the transaction.
|
|
1177
|
+
* @param params.marketData - Hydrated market snapshot used for validation and transaction construction.
|
|
1178
|
+
* @param params.positionData - Owner-bound position snapshot returned by getPositionData.
|
|
1179
|
+
* @param params.collateralPrices - Oracle prices indexed like positionData.collateral and scaled by
|
|
1180
|
+
* ORACLE_PRICE_SCALE; required when the position has debt, otherwise an empty array is safe.
|
|
1181
|
+
* @param params.collateralWithdrawals - Nonempty ordered withdrawals, one per index.
|
|
1182
|
+
* maxUint256 resolves that index's full snapshot balance.
|
|
1183
|
+
* @param params.receiver - Account receiving the withdrawn collateral; defaults to accountAddress.
|
|
1184
|
+
* @returns Lazy handles for a direct Midnight withdrawal.
|
|
1185
|
+
* @throws {ChainIdMismatchError} when the client or market targets another chain.
|
|
1186
|
+
* @throws {MidnightCalldataEncodingError} when accountAddress is malformed or an input cannot be ABI-encoded.
|
|
1187
|
+
* @throws {MidnightZeroAccountError} when accountAddress is zero.
|
|
1188
|
+
* @throws {MidnightMarketAddressMismatchError} when marketData targets another Midnight deployment.
|
|
1189
|
+
* @throws {MissingMarketPriceError} when debt is positive and held collateral has no supplied price.
|
|
1190
|
+
* @throws {MarketIdMismatchError} when positionData belongs to another market.
|
|
1191
|
+
* @throws {AccrualPositionUserMismatchError} when positionData belongs to another account.
|
|
1192
|
+
* @throws {UnknownCollateralIndexError} when collateralIndex is not configured.
|
|
1193
|
+
* @throws {EmptyMidnightCollateralWithdrawalsError} when the list is empty.
|
|
1194
|
+
* @throws {DuplicateMidnightCollateralIndexError} when an index repeats.
|
|
1195
|
+
* @throws {NonPositiveInputError} when the resolved amount is not positive.
|
|
1196
|
+
* @throws {InputExceedsMaxError} when assets exceeds uint128.
|
|
1197
|
+
* @throws {WithdrawExceedsCollateralError} when assets exceeds the collateral balance.
|
|
1198
|
+
* @throws {WithdrawMakesPositionUnhealthyError} when remaining collateral cannot safely cover debt.
|
|
1199
|
+
* @throws {MidnightZeroReceiverError} when receiver is zero.
|
|
1200
|
+
* @throws {MidnightContractReceiverError} when receiver cannot safely receive collateral.
|
|
1201
|
+
* @example
|
|
1202
|
+
* ```ts
|
|
1203
|
+
* const output = midnight.withdrawCollateralBatch({
|
|
1204
|
+
* accountAddress: user,
|
|
1205
|
+
* marketData,
|
|
1206
|
+
* positionData,
|
|
1207
|
+
* collateralPrices,
|
|
1208
|
+
* collateralWithdrawals: [{ collateralIndex: 0n, assets: 1_000n }],
|
|
1209
|
+
* });
|
|
1210
|
+
* const tx = output.buildTx();
|
|
1211
|
+
* ```
|
|
1212
|
+
*/
|
|
1213
|
+
withdrawCollateralBatch(params) {
|
|
1214
|
+
(0, index_js_3.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
|
|
1215
|
+
// Reject invalid owners before preparing the direct call.
|
|
1216
|
+
(0, validateMidnightAccount_js_1.validateMidnightAccount)("accountAddress", params.accountAddress);
|
|
1217
|
+
validateMarketData(params.marketData, this.chainId);
|
|
1218
|
+
validatePositionBinding(params);
|
|
1219
|
+
// Direct batches reject duplicates instead of summing them.
|
|
1220
|
+
(0, validateMidnightCollateralBatch_js_1.validateMidnightCollateralBatch)(params.collateralWithdrawals, new index_js_4.EmptyMidnightCollateralWithdrawalsError());
|
|
1221
|
+
const collateralWithdrawals = params.collateralWithdrawals.map(({ collateralIndex, assets }) => {
|
|
1222
|
+
// Reject unconfigured indexes before reading the position balance.
|
|
1223
|
+
midnight_sdk_1.MarketUtils.getCollateralByIndex(params.marketData.params, collateralIndex);
|
|
1224
|
+
return {
|
|
1225
|
+
collateralIndex,
|
|
1226
|
+
assets: assets === viem_1.maxUint256
|
|
1227
|
+
? (params.positionData.collateral[Number(collateralIndex)] ?? 0n)
|
|
1228
|
+
: assets,
|
|
1229
|
+
};
|
|
1230
|
+
});
|
|
1231
|
+
const tx = (0, index_js_1.midnightWithdrawCollateralBatch)({
|
|
1232
|
+
chainId: this.chainId,
|
|
1233
|
+
market: params.marketData.params,
|
|
1234
|
+
collateralWithdrawals,
|
|
1235
|
+
onBehalf: params.accountAddress,
|
|
1236
|
+
receiver: params.receiver ?? params.accountAddress,
|
|
1237
|
+
metadata: this.client.options.metadata,
|
|
1238
|
+
});
|
|
1239
|
+
const collateral = Array.from({ length: params.marketData.params.collateralParams.length }, (_, i) => params.positionData.collateral[i] ?? 0n);
|
|
1240
|
+
for (const { collateralIndex, assets } of collateralWithdrawals) {
|
|
1241
|
+
const index = Number(collateralIndex);
|
|
1242
|
+
const available = collateral[index];
|
|
1243
|
+
if (assets > available) {
|
|
1244
|
+
throw new index_js_4.WithdrawExceedsCollateralError({
|
|
1245
|
+
withdrawAmount: assets,
|
|
1246
|
+
available,
|
|
1247
|
+
market: params.marketData.id,
|
|
1248
|
+
});
|
|
1249
|
+
}
|
|
1250
|
+
collateral[index] = available - assets;
|
|
1251
|
+
}
|
|
1252
|
+
if (params.positionData.debt > 0n) {
|
|
1253
|
+
const maxSafeDebt = (0, index_js_3.getMidnightMaxSafeDebt)({
|
|
1254
|
+
market: params.marketData,
|
|
1255
|
+
collateral,
|
|
1256
|
+
collateralPrices: params.collateralPrices,
|
|
1257
|
+
});
|
|
1258
|
+
if (params.positionData.debt > maxSafeDebt) {
|
|
1259
|
+
throw new index_js_4.WithdrawMakesPositionUnhealthyError({
|
|
1260
|
+
withdrawAmount: collateralWithdrawals.reduce((sum, entry) => sum + entry.assets, 0n),
|
|
1261
|
+
borrowAssets: params.positionData.debt,
|
|
1262
|
+
maxSafeBorrow: maxSafeDebt,
|
|
1263
|
+
});
|
|
1264
|
+
}
|
|
1265
|
+
}
|
|
1266
|
+
return { getRequirements: async () => [], buildTx: () => tx };
|
|
1267
|
+
}
|
|
1167
1268
|
/**
|
|
1168
1269
|
* Prepares a direct collateral supply using a caller-provided market snapshot.
|
|
1169
1270
|
*
|
|
@@ -1238,6 +1339,79 @@ class MorphoMidnight {
|
|
|
1238
1339
|
}),
|
|
1239
1340
|
};
|
|
1240
1341
|
}
|
|
1342
|
+
/**
|
|
1343
|
+
* Prepares ordered direct collateral supplies in one Midnight multicall.
|
|
1344
|
+
* Pass one entry per index: duplicates are rejected, unlike bundle flows which sum them.
|
|
1345
|
+
*
|
|
1346
|
+
* @param params.accountAddress - Sender and collateral beneficiary.
|
|
1347
|
+
* @param params.marketData - Snapshot of the target market.
|
|
1348
|
+
* @param params.collateralSupplies - Ordered supplies with optional reserved allowance amounts.
|
|
1349
|
+
* @returns A lazy approval resolver and synchronous stateless transaction builder.
|
|
1350
|
+
* @throws {ChainIdMismatchError} when the client or market targets another chain.
|
|
1351
|
+
* @throws {MidnightMarketAddressMismatchError} when the deployment differs.
|
|
1352
|
+
* @throws {MidnightCalldataEncodingError} when the account is malformed.
|
|
1353
|
+
* @throws {NegativeInputError} when reserved assets are negative.
|
|
1354
|
+
* @throws {NonPositiveInputError} when assets are not positive.
|
|
1355
|
+
* @throws {InputExceedsMaxError} when assets exceed uint128.
|
|
1356
|
+
* @throws {UnknownCollateralIndexError} when an index is not configured.
|
|
1357
|
+
* @throws {EmptyMidnightCollateralSuppliesError} when the list is empty.
|
|
1358
|
+
* @throws {DuplicateMidnightCollateralIndexError} when an index repeats.
|
|
1359
|
+
* @throws {MidnightZeroOnBehalfError} when requirements or construction uses the zero account.
|
|
1360
|
+
* @example
|
|
1361
|
+
* ```ts
|
|
1362
|
+
* const output = midnight.supplyCollateralBatch({
|
|
1363
|
+
* accountAddress: user, marketData,
|
|
1364
|
+
* collateralSupplies: [{ collateralIndex: 0n, assets: 100n }],
|
|
1365
|
+
* });
|
|
1366
|
+
* const tx = output.buildTx();
|
|
1367
|
+
* ```
|
|
1368
|
+
*/
|
|
1369
|
+
supplyCollateralBatch(params) {
|
|
1370
|
+
(0, index_js_3.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
|
|
1371
|
+
// Reject malformed accounts before resolving token requirements.
|
|
1372
|
+
(0, validateMidnightAccount_js_1.validateMidnightAddress)("accountAddress", params.accountAddress);
|
|
1373
|
+
validateMarketData(params.marketData, this.chainId);
|
|
1374
|
+
// Reject empty and duplicate-index batches before resolving allowance thresholds.
|
|
1375
|
+
(0, validateMidnightCollateralBatch_js_1.validateMidnightCollateralBatch)(params.collateralSupplies, new index_js_4.EmptyMidnightCollateralSuppliesError());
|
|
1376
|
+
for (const supply of params.collateralSupplies) {
|
|
1377
|
+
assertPositiveAmount("assets", supply.assets);
|
|
1378
|
+
(0, validateMidnightBounds_js_1.validateMidnightUint128Field)("assets", supply.assets);
|
|
1379
|
+
assertNonNegativeAmount("reservedCollateralAssets", supply.reservedCollateralAssets ?? 0n);
|
|
1380
|
+
// Fail eagerly for collateral absent from the supplied snapshot.
|
|
1381
|
+
params.marketData.getCollateralByIndex(supply.collateralIndex);
|
|
1382
|
+
}
|
|
1383
|
+
const midnight = (0, morpho_ts_1.getChainAddress)(this.chainId, "midnight");
|
|
1384
|
+
return {
|
|
1385
|
+
getRequirements: async (_requirementsParams) => {
|
|
1386
|
+
if ((0, viem_1.isAddressEqual)(params.accountAddress, viem_1.zeroAddress)) {
|
|
1387
|
+
throw new index_js_4.MidnightZeroOnBehalfError("onBehalf");
|
|
1388
|
+
}
|
|
1389
|
+
const amounts = new Map();
|
|
1390
|
+
for (const supply of params.collateralSupplies) {
|
|
1391
|
+
const token = params.marketData.getCollateralByIndex(supply.collateralIndex).token;
|
|
1392
|
+
amounts.set(token, (amounts.get(token) ?? 0n) +
|
|
1393
|
+
supply.assets +
|
|
1394
|
+
(supply.reservedCollateralAssets ?? 0n));
|
|
1395
|
+
}
|
|
1396
|
+
const requirements = await Promise.all([...amounts].map(([token, amount]) => (0, index_js_2.getMidnightApprovalRequirements)({
|
|
1397
|
+
viemClient: this.client.viemClient,
|
|
1398
|
+
chainId: this.chainId,
|
|
1399
|
+
token,
|
|
1400
|
+
owner: params.accountAddress,
|
|
1401
|
+
spender: midnight,
|
|
1402
|
+
amount,
|
|
1403
|
+
})));
|
|
1404
|
+
return requirements.flat();
|
|
1405
|
+
},
|
|
1406
|
+
buildTx: () => (0, index_js_1.midnightSupplyCollateralBatch)({
|
|
1407
|
+
chainId: this.chainId,
|
|
1408
|
+
market: params.marketData.params,
|
|
1409
|
+
collateralSupplies: params.collateralSupplies.map(({ collateralIndex, assets }) => ({ collateralIndex, assets })),
|
|
1410
|
+
onBehalf: params.accountAddress,
|
|
1411
|
+
metadata: this.client.options.metadata,
|
|
1412
|
+
}),
|
|
1413
|
+
};
|
|
1414
|
+
}
|
|
1241
1415
|
/**
|
|
1242
1416
|
* Prepares an atomic lend-offer publication or repost through `MidnightBundlesV2`.
|
|
1243
1417
|
*
|
|
@@ -235,6 +235,15 @@ export interface SupplyCollateralParams extends MarketActionParams {
|
|
|
235
235
|
readonly reservedCollateralAssets?: bigint;
|
|
236
236
|
readonly collateralIndex?: bigint;
|
|
237
237
|
}
|
|
238
|
+
/** Parameters for an atomic direct Midnight collateral supply batch. */
|
|
239
|
+
export interface SupplyCollateralBatchParams extends MarketActionParams {
|
|
240
|
+
readonly collateralSupplies: readonly {
|
|
241
|
+
readonly collateralIndex: bigint;
|
|
242
|
+
readonly assets: bigint;
|
|
243
|
+
/** Additional assets included only in the allowance threshold. */
|
|
244
|
+
readonly reservedCollateralAssets?: bigint;
|
|
245
|
+
}[];
|
|
246
|
+
}
|
|
238
247
|
/** Parameters for a direct Midnight collateral withdrawal. */
|
|
239
248
|
export interface WithdrawCollateralParams extends MarketActionParams, MidnightPositionHealthParams {
|
|
240
249
|
readonly collateralIndex?: bigint;
|
|
@@ -243,6 +252,13 @@ export interface WithdrawCollateralParams extends MarketActionParams, MidnightPo
|
|
|
243
252
|
/** Defaults to accountAddress. */
|
|
244
253
|
readonly receiver?: Address;
|
|
245
254
|
}
|
|
255
|
+
/** Parameters for an atomic direct Midnight collateral withdrawal batch. */
|
|
256
|
+
export interface WithdrawCollateralBatchParams extends MarketActionParams, MidnightPositionHealthParams {
|
|
257
|
+
/** Ordered unique indexes; maxUint256 resolves that index's whole snapshot balance. */
|
|
258
|
+
readonly collateralWithdrawals: readonly MidnightCollateralTransfer[];
|
|
259
|
+
/** Defaults to accountAddress for every withdrawal. */
|
|
260
|
+
readonly receiver?: Address;
|
|
261
|
+
}
|
|
246
262
|
/** Parameters for the Midnight redeem flow. */
|
|
247
263
|
export interface RedeemParams {
|
|
248
264
|
readonly accountAddress: Address;
|
package/lib/cjs/errors.d.ts
CHANGED
|
@@ -2,6 +2,6 @@ export { BlueErrors, IncompleteChainRegistryError, InvalidMarketParamsError as I
|
|
|
2
2
|
/** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
|
|
3
3
|
UnsupportedPreLiquidationParamsError as UnsupportedBluePreLiquidationParamsError, UnsupportedVaultV2AdapterError as UnsupportedBlueVaultV2AdapterError, VaultV2Errors, } from "@morpho-org/blue-sdk";
|
|
4
4
|
export { getUnsupportedVaultV2Adapter as getBlueUnsupportedVaultV2Adapter, InvalidNumberError, InvalidPermitDomainChainIdError, InvalidPermitDomainVerifyingContractError, isUnknownOfFactoryError as isBlueUnknownOfFactoryError, MalformedTokenMetadataError, MarketParamsIdMismatchError as BlueMarketParamsIdMismatchError, MissingPermitDomainNameError, NonTupleReturnValueError, Permit2ExpirationOutOfRangeError, UnknownAbiFunctionError, UnnamedAbiOutputsError, UnsupportedPermitDomainExtensionsError, } from "@morpho-org/blue-sdk-viem";
|
|
5
|
-
export { ChainIdMismatchError as MidnightChainIdMismatchError, InvalidEcrecoverSignatureVError as InvalidMidnightEcrecoverSignatureVError, InvalidMarketParameterError as InvalidMidnightMarketParameterError, InvalidMidnightApiQuoteTargetError, InvalidMidnightApiResponseError, InvalidOfferGroupError as InvalidMidnightOfferGroupError, InvalidOfferParameterError as InvalidMidnightOfferParameterError, InvalidPositionAccrualStateError as InvalidMidnightPositionAccrualStateError, InvalidPositionAccrualTimestampError as InvalidMidnightPositionAccrualTimestampError, InvalidPositionLossFactorError as InvalidMidnightPositionLossFactorError, InvalidRateRatifierV1RateError as InvalidMidnightRateRatifierV1RateError, InvalidRateRatifierV1TickError as InvalidMidnightRateRatifierV1TickError, InvalidRateRatifierV1TimeError as InvalidMidnightRateRatifierV1TimeError, InvalidRatifierV1AddressError as InvalidMidnightRatifierV1AddressError, InvalidTickSpacingError as InvalidMidnightTickSpacingError, InvalidTreeError as InvalidMidnightTreeError, InvalidTreeHeightError as InvalidMidnightTreeHeightError, InvalidTypedDataSignatureError as InvalidMidnightTypedDataSignatureError, MidnightApiError, MidnightMempoolValidationError, PayloadDecodeError as MidnightPayloadDecodeError, PriceGreaterThanOneError as MidnightPriceGreaterThanOneError, RateRatifierV1BoundOverflowError as MidnightRateRatifierV1BoundOverflowError, RatifierV1TakerNotAllowedError as MidnightRatifierV1TakerNotAllowedError, SettlementFeeExceedsPriceError as MidnightSettlementFeeExceedsPriceError, TickOutOfRangeError as MidnightTickOutOfRangeError, UnknownCollateralIndexError as UnknownMidnightCollateralIndexError, } from "@morpho-org/midnight-sdk";
|
|
5
|
+
export { ChainIdMismatchError as MidnightChainIdMismatchError, InvalidEcrecoverSignatureError as InvalidMidnightEcrecoverSignatureError, InvalidEcrecoverSignatureVError as InvalidMidnightEcrecoverSignatureVError, InvalidMarketParameterError as InvalidMidnightMarketParameterError, InvalidMidnightApiQuoteTargetError, InvalidMidnightApiResponseError, InvalidOfferGroupError as InvalidMidnightOfferGroupError, InvalidOfferParameterError as InvalidMidnightOfferParameterError, InvalidPositionAccrualStateError as InvalidMidnightPositionAccrualStateError, InvalidPositionAccrualTimestampError as InvalidMidnightPositionAccrualTimestampError, InvalidPositionLossFactorError as InvalidMidnightPositionLossFactorError, InvalidRateRatifierV1RateError as InvalidMidnightRateRatifierV1RateError, InvalidRateRatifierV1TickError as InvalidMidnightRateRatifierV1TickError, InvalidRateRatifierV1TimeError as InvalidMidnightRateRatifierV1TimeError, InvalidRatifierV1AddressError as InvalidMidnightRatifierV1AddressError, InvalidTickSpacingError as InvalidMidnightTickSpacingError, InvalidTreeError as InvalidMidnightTreeError, InvalidTreeHeightError as InvalidMidnightTreeHeightError, InvalidTypedDataSignatureError as InvalidMidnightTypedDataSignatureError, MidnightApiError, MidnightMempoolValidationError, PayloadDecodeError as MidnightPayloadDecodeError, PriceGreaterThanOneError as MidnightPriceGreaterThanOneError, RateRatifierV1BoundOverflowError as MidnightRateRatifierV1BoundOverflowError, RatifierV1TakerNotAllowedError as MidnightRatifierV1TakerNotAllowedError, SettlementFeeExceedsPriceError as MidnightSettlementFeeExceedsPriceError, TickOutOfRangeError as MidnightTickOutOfRangeError, UnknownCollateralIndexError as UnknownMidnightCollateralIndexError, } from "@morpho-org/midnight-sdk";
|
|
6
6
|
export type { ErrorClass } from "@morpho-org/morpho-ts";
|
|
7
7
|
export { _try, DivisionByZeroError, InvalidAddressError, InvalidBitLengthError, NegativeValueError, RegistryValueAlreadyRegisteredError, RetriesExhaustedError, RetryStoppedError, } from "@morpho-org/morpho-ts";
|
package/lib/cjs/errors.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
exports.RetryStoppedError = exports.RetriesExhaustedError = exports.RegistryValueAlreadyRegisteredError = exports.NegativeValueError = exports.InvalidBitLengthError = exports.InvalidAddressError = exports.DivisionByZeroError = exports._try = exports.UnknownMidnightCollateralIndexError = exports.MidnightTickOutOfRangeError = exports.MidnightSettlementFeeExceedsPriceError = exports.MidnightRatifierV1TakerNotAllowedError = exports.MidnightRateRatifierV1BoundOverflowError = exports.MidnightPriceGreaterThanOneError = exports.MidnightPayloadDecodeError = void 0;
|
|
3
|
+
exports.MidnightApiError = exports.InvalidMidnightTypedDataSignatureError = exports.InvalidMidnightTreeHeightError = exports.InvalidMidnightTreeError = exports.InvalidMidnightTickSpacingError = exports.InvalidMidnightRatifierV1AddressError = exports.InvalidMidnightRateRatifierV1TimeError = exports.InvalidMidnightRateRatifierV1TickError = exports.InvalidMidnightRateRatifierV1RateError = exports.InvalidMidnightPositionLossFactorError = exports.InvalidMidnightPositionAccrualTimestampError = exports.InvalidMidnightPositionAccrualStateError = exports.InvalidMidnightOfferParameterError = exports.InvalidMidnightOfferGroupError = exports.InvalidMidnightApiResponseError = exports.InvalidMidnightApiQuoteTargetError = exports.InvalidMidnightMarketParameterError = exports.InvalidMidnightEcrecoverSignatureVError = exports.InvalidMidnightEcrecoverSignatureError = exports.MidnightChainIdMismatchError = exports.UnsupportedPermitDomainExtensionsError = exports.UnnamedAbiOutputsError = exports.UnknownAbiFunctionError = exports.Permit2ExpirationOutOfRangeError = exports.NonTupleReturnValueError = exports.MissingPermitDomainNameError = exports.BlueMarketParamsIdMismatchError = exports.MalformedTokenMetadataError = exports.isBlueUnknownOfFactoryError = exports.InvalidPermitDomainVerifyingContractError = exports.InvalidPermitDomainChainIdError = exports.InvalidNumberError = exports.getBlueUnsupportedVaultV2Adapter = exports.VaultV2Errors = exports.UnsupportedBlueVaultV2AdapterError = exports.UnsupportedBluePreLiquidationParamsError = exports.UnsupportedBlueMarketIrmError = exports.UnsupportedChainIdError = exports.UnknownBlueVaultConfigError = exports.UnknownBlueTokenPriceError = exports.UnknownBlueTokenError = exports.UnknownBlueOfFactory = exports.UnknownBlueMarketParamsError = exports.UnknownBlueMarketAllocationError = exports.UnknownBlueFactory = exports.UnknownBlueDataError = exports.UnknownAddressError = exports.InvalidBlueMarketParamsError = exports.IncompleteChainRegistryError = exports.BlueErrors = void 0;
|
|
4
|
+
exports.RetryStoppedError = exports.RetriesExhaustedError = exports.RegistryValueAlreadyRegisteredError = exports.NegativeValueError = exports.InvalidBitLengthError = exports.InvalidAddressError = exports.DivisionByZeroError = exports._try = exports.UnknownMidnightCollateralIndexError = exports.MidnightTickOutOfRangeError = exports.MidnightSettlementFeeExceedsPriceError = exports.MidnightRatifierV1TakerNotAllowedError = exports.MidnightRateRatifierV1BoundOverflowError = exports.MidnightPriceGreaterThanOneError = exports.MidnightPayloadDecodeError = exports.MidnightMempoolValidationError = void 0;
|
|
5
5
|
var blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
6
6
|
Object.defineProperty(exports, "BlueErrors", { enumerable: true, get: function () { return blue_sdk_1.BlueErrors; } });
|
|
7
7
|
Object.defineProperty(exports, "IncompleteChainRegistryError", { enumerable: true, get: function () { return blue_sdk_1.IncompleteChainRegistryError; } });
|
|
@@ -37,6 +37,7 @@ Object.defineProperty(exports, "UnnamedAbiOutputsError", { enumerable: true, get
|
|
|
37
37
|
Object.defineProperty(exports, "UnsupportedPermitDomainExtensionsError", { enumerable: true, get: function () { return blue_sdk_viem_1.UnsupportedPermitDomainExtensionsError; } });
|
|
38
38
|
var midnight_sdk_1 = require("@morpho-org/midnight-sdk");
|
|
39
39
|
Object.defineProperty(exports, "MidnightChainIdMismatchError", { enumerable: true, get: function () { return midnight_sdk_1.ChainIdMismatchError; } });
|
|
40
|
+
Object.defineProperty(exports, "InvalidMidnightEcrecoverSignatureError", { enumerable: true, get: function () { return midnight_sdk_1.InvalidEcrecoverSignatureError; } });
|
|
40
41
|
Object.defineProperty(exports, "InvalidMidnightEcrecoverSignatureVError", { enumerable: true, get: function () { return midnight_sdk_1.InvalidEcrecoverSignatureVError; } });
|
|
41
42
|
Object.defineProperty(exports, "InvalidMidnightMarketParameterError", { enumerable: true, get: function () { return midnight_sdk_1.InvalidMarketParameterError; } });
|
|
42
43
|
Object.defineProperty(exports, "InvalidMidnightApiQuoteTargetError", { enumerable: true, get: function () { return midnight_sdk_1.InvalidMidnightApiQuoteTargetError; } });
|
|
@@ -16,6 +16,7 @@ import type { Address, Hex, TypedDataDefinition, WalletClient } from "viem";
|
|
|
16
16
|
* @throws {AddressMismatchError} when the wallet client account differs from `userAddress`.
|
|
17
17
|
* @throws {ChainIdMismatchError} when the wallet client targets a different chain than the typed-data domain.
|
|
18
18
|
* @throws {InvalidSignatureError} when the signature does not recover to `userAddress`.
|
|
19
|
+
* @throws {MalformedSignatureError} when verification throws; preserves the original error as `cause` with redacted signature diagnostics. Extends `InvalidSignatureError`.
|
|
19
20
|
* @example
|
|
20
21
|
* ```ts
|
|
21
22
|
* import { signAndVerifyTypedData } from "@morpho-org/morpho-sdk";
|
|
@@ -22,6 +22,7 @@ const validate_js_1 = require("./validate.js");
|
|
|
22
22
|
* @throws {AddressMismatchError} when the wallet client account differs from `userAddress`.
|
|
23
23
|
* @throws {ChainIdMismatchError} when the wallet client targets a different chain than the typed-data domain.
|
|
24
24
|
* @throws {InvalidSignatureError} when the signature does not recover to `userAddress`.
|
|
25
|
+
* @throws {MalformedSignatureError} when verification throws; preserves the original error as `cause` with redacted signature diagnostics. Extends `InvalidSignatureError`.
|
|
25
26
|
* @example
|
|
26
27
|
* ```ts
|
|
27
28
|
* import { signAndVerifyTypedData } from "@morpho-org/morpho-sdk";
|
|
@@ -45,11 +46,17 @@ const signAndVerifyTypedData = async (params) => {
|
|
|
45
46
|
...typedData,
|
|
46
47
|
account,
|
|
47
48
|
});
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
49
|
+
let isValid;
|
|
50
|
+
try {
|
|
51
|
+
isValid = await (0, viem_1.verifyTypedData)({
|
|
52
|
+
...typedData,
|
|
53
|
+
address: userAddress,
|
|
54
|
+
signature,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
catch (cause) {
|
|
58
|
+
throw new index_js_1.MalformedSignatureError({ signature, cause });
|
|
59
|
+
}
|
|
53
60
|
if (!isValid) {
|
|
54
61
|
throw new index_js_1.InvalidSignatureError();
|
|
55
62
|
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { MidnightCancelAndMakeParams } from "../types/midnightCancelAndMake.js";
|
|
2
|
+
/**
|
|
3
|
+
* Validates publication content after snapshot and hex-shape validation.
|
|
4
|
+
*
|
|
5
|
+
* @param params - Publication, chain deployment, and groups cancelled by this bundle.
|
|
6
|
+
* @returns Nothing when publication content and funding inputs are valid.
|
|
7
|
+
* @throws {InvalidTreeError} when the root is zero, payload empty, or groups empty.
|
|
8
|
+
* @throws {UnsupportedChainIdError} when the chain is absent from the registry.
|
|
9
|
+
* @throws {MidnightCalldataEncodingError} when the ratifier or collateral market address is malformed.
|
|
10
|
+
* @throws {UnsupportedMidnightAuthorizationTargetError} when the ratifier is not supported.
|
|
11
|
+
* @throws {MidnightReplacementGroupCancelledError} when a published group is cancelled.
|
|
12
|
+
* @throws {NonPositiveInputError} when Blue funding or a collateral supply is not positive.
|
|
13
|
+
* @throws {InputExceedsMaxError} when funding or collateral supply totals exceed uint128.
|
|
14
|
+
* @throws {EmptyMidnightCollateralSuppliesError} when collateral supplies are empty.
|
|
15
|
+
* @throws {ChainIdMismatchError} when the collateral market targets another chain.
|
|
16
|
+
* @throws {MidnightMarketAddressMismatchError} when its Midnight deployment differs.
|
|
17
|
+
* @throws {UnknownCollateralIndexError} when a supply index is not configured.
|
|
18
|
+
* @internal
|
|
19
|
+
*/
|
|
20
|
+
export declare const validateMidnightCancelAndMakePublication: ({ publication, chainId, cancelledGroups, }: {
|
|
21
|
+
readonly publication: NonNullable<MidnightCancelAndMakeParams["publication"]>;
|
|
22
|
+
readonly chainId: number;
|
|
23
|
+
readonly cancelledGroups: ReadonlySet<string>;
|
|
24
|
+
}) => void;
|
|
25
|
+
/**
|
|
26
|
+
* Copies validated publication fields and supplies zero ABI defaults for omitted steps.
|
|
27
|
+
*
|
|
28
|
+
* @param publication - Snapshotted publication, or undefined for cancellation only.
|
|
29
|
+
* @returns ABI defaults and copied publication fields, without mutating the input.
|
|
30
|
+
* @internal
|
|
31
|
+
*/
|
|
32
|
+
export declare const normalizeMidnightCancelAndMakePublication: (publication: MidnightCancelAndMakeParams["publication"]) => {
|
|
33
|
+
supplies: {
|
|
34
|
+
collateralIndex: bigint;
|
|
35
|
+
assets: bigint;
|
|
36
|
+
}[];
|
|
37
|
+
blueSupply: {
|
|
38
|
+
market: {
|
|
39
|
+
loanToken: `0x${string}`;
|
|
40
|
+
collateralToken: `0x${string}`;
|
|
41
|
+
oracle: `0x${string}`;
|
|
42
|
+
irm: `0x${string}`;
|
|
43
|
+
lltv: bigint;
|
|
44
|
+
};
|
|
45
|
+
assets: bigint;
|
|
46
|
+
callbackSalt: `0x${string}`;
|
|
47
|
+
} | undefined;
|
|
48
|
+
signature: import("../index.js").MidnightRootActivationSignature;
|
|
49
|
+
blueMarket: {
|
|
50
|
+
readonly loanToken: "0x0000000000000000000000000000000000000000";
|
|
51
|
+
readonly collateralToken: "0x0000000000000000000000000000000000000000";
|
|
52
|
+
readonly oracle: "0x0000000000000000000000000000000000000000";
|
|
53
|
+
readonly irm: "0x0000000000000000000000000000000000000000";
|
|
54
|
+
readonly lltv: 0n;
|
|
55
|
+
} | {
|
|
56
|
+
loanToken: `0x${string}`;
|
|
57
|
+
collateralToken: `0x${string}`;
|
|
58
|
+
oracle: `0x${string}`;
|
|
59
|
+
irm: `0x${string}`;
|
|
60
|
+
lltv: bigint;
|
|
61
|
+
};
|
|
62
|
+
assetsToPark: bigint;
|
|
63
|
+
callbackSalt: `0x${string}`;
|
|
64
|
+
ratifier: `0x${string}`;
|
|
65
|
+
root: `0x${string}`;
|
|
66
|
+
groups: `0x${string}`[];
|
|
67
|
+
payload: `0x${string}`;
|
|
68
|
+
};
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normalizeMidnightCancelAndMakePublication = exports.validateMidnightCancelAndMakePublication = 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("../types/index.js");
|
|
8
|
+
const validateMidnightAccount_js_1 = require("./validateMidnightAccount.js");
|
|
9
|
+
const validateMidnightBounds_js_1 = require("./validateMidnightBounds.js");
|
|
10
|
+
const validateMidnightCollateralSupplies_js_1 = require("./validateMidnightCollateralSupplies.js");
|
|
11
|
+
const validateMidnightMarket_js_1 = require("./validateMidnightMarket.js");
|
|
12
|
+
const validatePositiveInput_js_1 = require("./validatePositiveInput.js");
|
|
13
|
+
/** Blue market argument for `MidnightBundlesV2` calls that park no loan assets. */
|
|
14
|
+
const emptyBlueMarket = {
|
|
15
|
+
loanToken: viem_1.zeroAddress,
|
|
16
|
+
collateralToken: viem_1.zeroAddress,
|
|
17
|
+
oracle: viem_1.zeroAddress,
|
|
18
|
+
irm: viem_1.zeroAddress,
|
|
19
|
+
lltv: 0n,
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Validates publication content after snapshot and hex-shape validation.
|
|
23
|
+
*
|
|
24
|
+
* @param params - Publication, chain deployment, and groups cancelled by this bundle.
|
|
25
|
+
* @returns Nothing when publication content and funding inputs are valid.
|
|
26
|
+
* @throws {InvalidTreeError} when the root is zero, payload empty, or groups empty.
|
|
27
|
+
* @throws {UnsupportedChainIdError} when the chain is absent from the registry.
|
|
28
|
+
* @throws {MidnightCalldataEncodingError} when the ratifier or collateral market address is malformed.
|
|
29
|
+
* @throws {UnsupportedMidnightAuthorizationTargetError} when the ratifier is not supported.
|
|
30
|
+
* @throws {MidnightReplacementGroupCancelledError} when a published group is cancelled.
|
|
31
|
+
* @throws {NonPositiveInputError} when Blue funding or a collateral supply is not positive.
|
|
32
|
+
* @throws {InputExceedsMaxError} when funding or collateral supply totals exceed uint128.
|
|
33
|
+
* @throws {EmptyMidnightCollateralSuppliesError} when collateral supplies are empty.
|
|
34
|
+
* @throws {ChainIdMismatchError} when the collateral market targets another chain.
|
|
35
|
+
* @throws {MidnightMarketAddressMismatchError} when its Midnight deployment differs.
|
|
36
|
+
* @throws {UnknownCollateralIndexError} when a supply index is not configured.
|
|
37
|
+
* @internal
|
|
38
|
+
*/
|
|
39
|
+
const validateMidnightCancelAndMakePublication = ({ publication, chainId, cancelledGroups, }) => {
|
|
40
|
+
if (publication.root === viem_1.zeroHash) {
|
|
41
|
+
throw new midnight_sdk_1.InvalidTreeError("Offer root cannot be zero.");
|
|
42
|
+
}
|
|
43
|
+
if (publication.payload === "0x") {
|
|
44
|
+
throw new midnight_sdk_1.InvalidTreeError("Offer payload cannot be empty.");
|
|
45
|
+
}
|
|
46
|
+
if (publication.groups.length === 0) {
|
|
47
|
+
throw new midnight_sdk_1.InvalidTreeError("Offer groups cannot be empty.");
|
|
48
|
+
}
|
|
49
|
+
const { priceRatifierV1, rateRatifierV1 } = (0, morpho_ts_1.getChainAddresses)(chainId);
|
|
50
|
+
const supportedTargets = [priceRatifierV1, rateRatifierV1].filter((target) => target != null);
|
|
51
|
+
(0, validateMidnightAccount_js_1.validateMidnightAddress)("publication.ratifier", publication.ratifier);
|
|
52
|
+
if (!supportedTargets.some((target) => (0, viem_1.isAddressEqual)(publication.ratifier, target))) {
|
|
53
|
+
throw new index_js_1.UnsupportedMidnightAuthorizationTargetError({
|
|
54
|
+
authorized: publication.ratifier,
|
|
55
|
+
chainId,
|
|
56
|
+
supportedTargets,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
for (const group of publication.groups) {
|
|
60
|
+
if (cancelledGroups.has(group.toLowerCase())) {
|
|
61
|
+
throw new index_js_1.MidnightReplacementGroupCancelledError({ group });
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (publication.blueSupply != null) {
|
|
65
|
+
// Validation only; rejects non-positive Blue supply amounts.
|
|
66
|
+
(0, validatePositiveInput_js_1.validatePositiveInput)("blueSupply.assets", publication.blueSupply.assets);
|
|
67
|
+
(0, validateMidnightBounds_js_1.validateMidnightUint128Field)("blueSupply.assets", publication.blueSupply.assets);
|
|
68
|
+
}
|
|
69
|
+
if (publication.collateral != null) {
|
|
70
|
+
if (publication.collateral.supplies.length === 0) {
|
|
71
|
+
throw new index_js_1.EmptyMidnightCollateralSuppliesError();
|
|
72
|
+
}
|
|
73
|
+
(0, validateMidnightMarket_js_1.validateMidnightMarket)({
|
|
74
|
+
market: publication.collateral.market,
|
|
75
|
+
chainId,
|
|
76
|
+
});
|
|
77
|
+
// Validation only; checks supply entries and their per-index totals.
|
|
78
|
+
(0, validateMidnightCollateralSupplies_js_1.validateMidnightCollateralSupplies)({
|
|
79
|
+
market: publication.collateral.market,
|
|
80
|
+
field: "collateral.supplies",
|
|
81
|
+
supplies: publication.collateral.supplies,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
exports.validateMidnightCancelAndMakePublication = validateMidnightCancelAndMakePublication;
|
|
86
|
+
/**
|
|
87
|
+
* Copies validated publication fields and supplies zero ABI defaults for omitted steps.
|
|
88
|
+
*
|
|
89
|
+
* @param publication - Snapshotted publication, or undefined for cancellation only.
|
|
90
|
+
* @returns ABI defaults and copied publication fields, without mutating the input.
|
|
91
|
+
* @internal
|
|
92
|
+
*/
|
|
93
|
+
const normalizeMidnightCancelAndMakePublication = (publication) => {
|
|
94
|
+
const supplies = (publication?.collateral?.supplies ?? []).map(({ collateralIndex, assets }) => ({ collateralIndex, assets }));
|
|
95
|
+
const blueSupply = publication?.blueSupply == null
|
|
96
|
+
? undefined
|
|
97
|
+
: {
|
|
98
|
+
market: {
|
|
99
|
+
loanToken: publication.blueSupply.market.loanToken,
|
|
100
|
+
collateralToken: publication.blueSupply.market.collateralToken,
|
|
101
|
+
oracle: publication.blueSupply.market.oracle,
|
|
102
|
+
irm: publication.blueSupply.market.irm,
|
|
103
|
+
lltv: publication.blueSupply.market.lltv,
|
|
104
|
+
},
|
|
105
|
+
assets: publication.blueSupply.assets,
|
|
106
|
+
callbackSalt: publication.blueSupply.callbackSalt,
|
|
107
|
+
};
|
|
108
|
+
const signature = publication?.rootSignature ?? {
|
|
109
|
+
height: 0n,
|
|
110
|
+
nonce: 0n,
|
|
111
|
+
deadline: 0n,
|
|
112
|
+
v: 0,
|
|
113
|
+
r: viem_1.zeroHash,
|
|
114
|
+
s: viem_1.zeroHash,
|
|
115
|
+
};
|
|
116
|
+
return {
|
|
117
|
+
supplies,
|
|
118
|
+
blueSupply,
|
|
119
|
+
signature,
|
|
120
|
+
blueMarket: blueSupply?.market ?? emptyBlueMarket,
|
|
121
|
+
assetsToPark: blueSupply?.assets ?? 0n,
|
|
122
|
+
callbackSalt: blueSupply?.callbackSalt ?? viem_1.zeroHash,
|
|
123
|
+
ratifier: publication?.ratifier ?? viem_1.zeroAddress,
|
|
124
|
+
root: publication?.root ?? viem_1.zeroHash,
|
|
125
|
+
groups: [...(publication?.groups ?? [])],
|
|
126
|
+
payload: publication?.payload ?? "0x",
|
|
127
|
+
};
|
|
128
|
+
};
|
|
129
|
+
exports.normalizeMidnightCancelAndMakePublication = normalizeMidnightCancelAndMakePublication;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { MidnightCollateralTransfer } from "../types/action.js";
|
|
2
|
+
/**
|
|
3
|
+
* Rejects empty batches and repeated collateral indexes.
|
|
4
|
+
* @internal
|
|
5
|
+
* @param transfers - Ordered collateral transfers.
|
|
6
|
+
* @param emptyError - Operation-specific error thrown for an empty batch.
|
|
7
|
+
* @returns Nothing.
|
|
8
|
+
* @throws {DuplicateMidnightCollateralIndexError} when an index appears twice.
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* validateMidnightCollateralBatch(supplies, new EmptyMidnightCollateralSuppliesError());
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export declare function validateMidnightCollateralBatch(transfers: readonly MidnightCollateralTransfer[], emptyError: Error): void;
|