@morpho-org/morpho-sdk 9.6.0-next.0 → 9.7.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/MIGRATION-v6-to-v7.md +1 -1
- package/README.md +123 -3
- package/lib/cjs/abis.d.ts +1 -1
- package/lib/cjs/abis.js +1 -5
- 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/blue/abis.d.ts +1 -1
- package/lib/cjs/blue/abis.js +1 -5
- package/lib/cjs/blue/entities.d.ts +4 -4
- package/lib/cjs/blue/entities.js +1 -3
- package/lib/cjs/blue/errors.d.ts +2 -2
- package/lib/cjs/blue/errors.js +1 -5
- package/lib/cjs/blue/fetch.d.ts +2 -2
- package/lib/cjs/blue/fetch.js +1 -3
- package/lib/cjs/blue/utils.d.ts +1 -1
- package/lib/cjs/blue/utils.js +1 -2
- package/lib/cjs/entities/index.d.ts +5 -5
- package/lib/cjs/entities/index.js +1 -3
- 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/entities/requirements/getBundlesTokenRequirements.js +12 -46
- package/lib/cjs/entities/requirements/getErc2612Permit.d.ts +7 -0
- package/lib/cjs/entities/requirements/getErc2612Permit.js +60 -0
- package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +2 -2
- package/lib/cjs/entities/vaultV1/vaultV1.d.ts +3 -3
- package/lib/cjs/entities/vaultV2/vaultV2.d.ts +10 -35
- package/lib/cjs/entities/vaultV2/vaultV2.js +1 -33
- package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +3 -5
- package/lib/cjs/entities/vaultV2BlueReallocationData.js +3 -9
- package/lib/cjs/errors.d.ts +4 -4
- package/lib/cjs/errors.js +6 -7
- package/lib/cjs/fetch.d.ts +2 -2
- package/lib/cjs/fetch.js +1 -3
- package/lib/cjs/helpers/deadShares.js +0 -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 +15 -0
- package/lib/cjs/helpers/validateMidnightCollateralBatch.js +28 -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/index.d.ts +2 -0
- package/lib/cjs/index.js +18 -1
- package/lib/cjs/midnight/errors.d.ts +1 -2
- package/lib/cjs/midnight/errors.js +5 -5
- package/lib/cjs/transactionPlan/approval.d.ts +2 -0
- package/lib/cjs/transactionPlan/approval.js +16 -0
- package/lib/cjs/transactionPlan/error.d.ts +145 -0
- package/lib/cjs/transactionPlan/error.js +186 -0
- package/lib/cjs/transactionPlan/getTokenPermit.d.ts +11 -0
- package/lib/cjs/transactionPlan/getTokenPermit.js +18 -0
- package/lib/cjs/transactionPlan/index.d.ts +9 -0
- package/lib/cjs/transactionPlan/index.js +24 -0
- package/lib/cjs/transactionPlan/intent.d.ts +51 -0
- package/lib/cjs/transactionPlan/intent.js +54 -0
- package/lib/cjs/transactionPlan/permit.d.ts +13 -0
- package/lib/cjs/transactionPlan/permit.js +145 -0
- package/lib/cjs/transactionPlan/plan.d.ts +99 -0
- package/lib/cjs/transactionPlan/plan.js +232 -0
- package/lib/cjs/transactionPlan/requirements.d.ts +44 -0
- package/lib/cjs/transactionPlan/requirements.js +79 -0
- package/lib/cjs/transactionPlan/serde.d.ts +33 -0
- package/lib/cjs/transactionPlan/serde.js +205 -0
- package/lib/cjs/transactionPlan/types.d.ts +91 -0
- package/lib/cjs/transactionPlan/types.js +2 -0
- package/lib/cjs/transactionPlan/validateActions.d.ts +8 -0
- package/lib/cjs/transactionPlan/validateActions.js +19 -0
- package/lib/cjs/transactionPlan/validation.d.ts +17 -0
- package/lib/cjs/transactionPlan/validation.js +70 -0
- package/lib/cjs/transactionPlan/vaultv2/deposit.d.ts +81 -0
- package/lib/cjs/transactionPlan/vaultv2/deposit.js +211 -0
- package/lib/cjs/transactionPlan/vaultv2/withdraw.d.ts +77 -0
- package/lib/cjs/transactionPlan/vaultv2/withdraw.js +190 -0
- package/lib/cjs/transactionPlan/version.d.ts +2 -0
- package/lib/cjs/transactionPlan/version.js +9 -0
- 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/cjs/utils.d.ts +2 -2
- package/lib/cjs/utils.js +2 -4
- package/lib/esm/abis.d.ts +1 -1
- package/lib/esm/abis.js +1 -1
- 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/blue/abis.d.ts +1 -1
- package/lib/esm/blue/abis.js +1 -1
- package/lib/esm/blue/entities.d.ts +4 -4
- package/lib/esm/blue/entities.js +2 -2
- package/lib/esm/blue/errors.d.ts +2 -2
- package/lib/esm/blue/errors.js +2 -2
- package/lib/esm/blue/fetch.d.ts +2 -2
- package/lib/esm/blue/fetch.js +2 -2
- package/lib/esm/blue/utils.d.ts +1 -1
- package/lib/esm/blue/utils.js +1 -1
- package/lib/esm/entities/index.d.ts +5 -5
- package/lib/esm/entities/index.js +2 -2
- 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/entities/requirements/getBundlesTokenRequirements.js +14 -48
- package/lib/esm/entities/requirements/getErc2612Permit.d.ts +7 -0
- package/lib/esm/entities/requirements/getErc2612Permit.js +57 -0
- package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +2 -2
- package/lib/esm/entities/vaultV1/vaultV1.d.ts +3 -3
- package/lib/esm/entities/vaultV2/vaultV2.d.ts +10 -35
- package/lib/esm/entities/vaultV2/vaultV2.js +1 -33
- package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +3 -5
- package/lib/esm/entities/vaultV2BlueReallocationData.js +3 -9
- package/lib/esm/errors.d.ts +4 -4
- package/lib/esm/errors.js +4 -4
- package/lib/esm/fetch.d.ts +2 -2
- package/lib/esm/fetch.js +2 -2
- package/lib/esm/helpers/deadShares.js +1 -3
- 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 +15 -0
- package/lib/esm/helpers/validateMidnightCollateralBatch.js +25 -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/index.d.ts +2 -0
- package/lib/esm/index.js +1 -0
- package/lib/esm/midnight/errors.d.ts +1 -2
- package/lib/esm/midnight/errors.js +1 -2
- package/lib/esm/transactionPlan/approval.d.ts +2 -0
- package/lib/esm/transactionPlan/approval.js +13 -0
- package/lib/esm/transactionPlan/error.d.ts +145 -0
- package/lib/esm/transactionPlan/error.js +175 -0
- package/lib/esm/transactionPlan/getTokenPermit.d.ts +11 -0
- package/lib/esm/transactionPlan/getTokenPermit.js +15 -0
- package/lib/esm/transactionPlan/index.d.ts +9 -0
- package/lib/esm/transactionPlan/index.js +5 -0
- package/lib/esm/transactionPlan/intent.d.ts +51 -0
- package/lib/esm/transactionPlan/intent.js +51 -0
- package/lib/esm/transactionPlan/permit.d.ts +13 -0
- package/lib/esm/transactionPlan/permit.js +140 -0
- package/lib/esm/transactionPlan/plan.d.ts +99 -0
- package/lib/esm/transactionPlan/plan.js +226 -0
- package/lib/esm/transactionPlan/requirements.d.ts +44 -0
- package/lib/esm/transactionPlan/requirements.js +74 -0
- package/lib/esm/transactionPlan/serde.d.ts +33 -0
- package/lib/esm/transactionPlan/serde.js +201 -0
- package/lib/esm/transactionPlan/types.d.ts +91 -0
- package/lib/esm/transactionPlan/types.js +1 -0
- package/lib/esm/transactionPlan/validateActions.d.ts +8 -0
- package/lib/esm/transactionPlan/validateActions.js +16 -0
- package/lib/esm/transactionPlan/validation.d.ts +17 -0
- package/lib/esm/transactionPlan/validation.js +64 -0
- package/lib/esm/transactionPlan/vaultv2/deposit.d.ts +81 -0
- package/lib/esm/transactionPlan/vaultv2/deposit.js +206 -0
- package/lib/esm/transactionPlan/vaultv2/withdraw.d.ts +77 -0
- package/lib/esm/transactionPlan/vaultv2/withdraw.js +185 -0
- package/lib/esm/transactionPlan/version.d.ts +2 -0
- package/lib/esm/transactionPlan/version.js +6 -0
- 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/lib/esm/utils.d.ts +2 -2
- package/lib/esm/utils.js +2 -2
- package/package.json +8 -7
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { MarketUtils, midnightAbi, } from "@morpho-org/midnight-sdk";
|
|
2
|
+
import { deepFreeze, getChainAddress } from "@morpho-org/morpho-ts";
|
|
3
|
+
import { isAddressEqual, zeroAddress } from "viem";
|
|
4
|
+
import { encodeMidnightCalldata, encodeMidnightMarketId, } from "../../helpers/encodeMidnightCalldata.js";
|
|
5
|
+
import { addTransactionMetadata } from "../../helpers/index.js";
|
|
6
|
+
import { validateMidnightAddress } from "../../helpers/validateMidnightAccount.js";
|
|
7
|
+
import { validateMidnightUint128Field } from "../../helpers/validateMidnightBounds.js";
|
|
8
|
+
import { validateMidnightCollateralBatch } from "../../helpers/validateMidnightCollateralBatch.js";
|
|
9
|
+
import { validateMidnightMarket } from "../../helpers/validateMidnightMarket.js";
|
|
10
|
+
import { EmptyMidnightCollateralSuppliesError, MidnightZeroOnBehalfError, NonPositiveInputError, } from "../../types/index.js";
|
|
11
|
+
/**
|
|
12
|
+
* Encodes ordered collateral supplies in one Midnight multicall, even for one entry.
|
|
13
|
+
* Duplicates are rejected: pass one entry per index, unlike bundle flows which sum them.
|
|
14
|
+
*
|
|
15
|
+
* @param params.chainId - Chain containing Midnight.
|
|
16
|
+
* @param params.market - Market receiving collateral.
|
|
17
|
+
* @param params.collateralSupplies - Nonempty ordered supplies, with unique collateral indexes.
|
|
18
|
+
* @param params.onBehalf - Account receiving collateral; this account or an authorized operator must send the transaction.
|
|
19
|
+
* @param params.metadata - Optional analytics metadata appended to calldata.
|
|
20
|
+
* @returns A deep-frozen transaction targeting Midnight.
|
|
21
|
+
* @throws {EmptyMidnightCollateralSuppliesError} when the list is empty.
|
|
22
|
+
* @throws {DuplicateMidnightCollateralIndexError} when an index repeats.
|
|
23
|
+
* @throws {NonPositiveInputError} when assets are not positive.
|
|
24
|
+
* @throws {InputExceedsMaxError} when assets exceed uint128.
|
|
25
|
+
* @throws {MidnightZeroOnBehalfError} when onBehalf is zero.
|
|
26
|
+
* @throws {ChainIdMismatchError} when the market targets another chain.
|
|
27
|
+
* @throws {MidnightMarketAddressMismatchError} when the deployment differs.
|
|
28
|
+
* @throws {UnknownCollateralIndexError} when an index is not configured.
|
|
29
|
+
* @throws {MidnightCalldataEncodingError} when an address or ABI input is malformed.
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* const tx = midnightSupplyCollateralBatch({
|
|
33
|
+
* chainId: 8453, market: marketData.params, onBehalf: user,
|
|
34
|
+
* collateralSupplies: [{ collateralIndex: 0n, assets: 100n }],
|
|
35
|
+
* });
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export const midnightSupplyCollateralBatch = (params) => {
|
|
39
|
+
// Reject malformed addresses before comparing with zero.
|
|
40
|
+
validateMidnightAddress("onBehalf", params.onBehalf);
|
|
41
|
+
if (isAddressEqual(params.onBehalf, zeroAddress)) {
|
|
42
|
+
throw new MidnightZeroOnBehalfError("onBehalf");
|
|
43
|
+
}
|
|
44
|
+
// Reject markets from another deployment before encoding.
|
|
45
|
+
validateMidnightMarket({ market: params.market, chainId: params.chainId });
|
|
46
|
+
// Direct batches require one entry per index rather than summing duplicates.
|
|
47
|
+
validateMidnightCollateralBatch(params.collateralSupplies, new EmptyMidnightCollateralSuppliesError());
|
|
48
|
+
const calls = params.collateralSupplies.map(({ collateralIndex, assets }) => {
|
|
49
|
+
if (assets <= 0n)
|
|
50
|
+
throw new NonPositiveInputError("assets", assets);
|
|
51
|
+
validateMidnightUint128Field("assets", assets);
|
|
52
|
+
// Reject indexes not configured on the market.
|
|
53
|
+
MarketUtils.getCollateralByIndex(params.market, collateralIndex);
|
|
54
|
+
return encodeMidnightCalldata({
|
|
55
|
+
abi: midnightAbi,
|
|
56
|
+
functionName: "supplyCollateral",
|
|
57
|
+
args: [
|
|
58
|
+
MarketUtils.toStruct(params.market),
|
|
59
|
+
collateralIndex,
|
|
60
|
+
assets,
|
|
61
|
+
params.onBehalf,
|
|
62
|
+
],
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
let tx = {
|
|
66
|
+
to: getChainAddress(params.chainId, "midnight"),
|
|
67
|
+
value: 0n,
|
|
68
|
+
data: encodeMidnightCalldata({
|
|
69
|
+
abi: midnightAbi,
|
|
70
|
+
functionName: "multicall",
|
|
71
|
+
args: [calls],
|
|
72
|
+
}),
|
|
73
|
+
};
|
|
74
|
+
if (params.metadata)
|
|
75
|
+
tx = addTransactionMetadata(tx, params.metadata);
|
|
76
|
+
return deepFreeze({
|
|
77
|
+
...tx,
|
|
78
|
+
action: {
|
|
79
|
+
type: "midnightSupplyCollateralBatch",
|
|
80
|
+
args: {
|
|
81
|
+
market: encodeMidnightMarketId(params.market),
|
|
82
|
+
collateralSupplies: params.collateralSupplies.map(({ collateralIndex, assets }) => ({ collateralIndex, assets })),
|
|
83
|
+
onBehalf: params.onBehalf,
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
};
|
|
@@ -5,12 +5,14 @@ import { encodeMidnightCalldata } from "../../helpers/encodeMidnightCalldata.js"
|
|
|
5
5
|
import { addTransactionMetadata } from "../../helpers/index.js";
|
|
6
6
|
import { validateDeadline, validateMidnightNativeAmount, validateReferralFee, } from "../../helpers/validate.js";
|
|
7
7
|
import { validateMidnightAddress } from "../../helpers/validateMidnightAccount.js";
|
|
8
|
-
import {
|
|
8
|
+
import { validateMidnightReferralFeeCap, validateMidnightTakeableOfferBounds, validateMidnightUint128Field, } from "../../helpers/validateMidnightBounds.js";
|
|
9
|
+
import { validateMidnightCollateralSupplies } from "../../helpers/validateMidnightCollateralSupplies.js";
|
|
9
10
|
import { validateMidnightMarket } from "../../helpers/validateMidnightMarket.js";
|
|
10
11
|
import { validateMidnightReceiver } from "../../helpers/validateMidnightReceiver.js";
|
|
11
12
|
import { validateMidnightSellTargetBounds } from "../../helpers/validateMidnightTakeFeasibility.js";
|
|
13
|
+
import { validatePositiveInput } from "../../helpers/validatePositiveInput.js";
|
|
12
14
|
import { validateTakeableOffers } from "../../helpers/validateTakeableOffers.js";
|
|
13
|
-
import { EmptyMidnightCollateralSuppliesError,
|
|
15
|
+
import { EmptyMidnightCollateralSuppliesError, UnboundedMidnightTakeInputError, } from "../../types/index.js";
|
|
14
16
|
/**
|
|
15
17
|
* Encodes a `MidnightBundlesV2` sell that supplies collateral and borrows from lend-side offers
|
|
16
18
|
* for `msg.sender` in one call.
|
|
@@ -95,15 +97,12 @@ export const midnightSupplyCollateralTakeBorrow = (params) => {
|
|
|
95
97
|
if (params.collateralSupplies.length === 0) {
|
|
96
98
|
throw new EmptyMidnightCollateralSuppliesError();
|
|
97
99
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
MarketUtils.getCollateralByIndex(params.market, collateralIndex);
|
|
105
|
-
}
|
|
106
|
-
validateMidnightCollateralSupplySums("collateralSupplies", params.collateralSupplies);
|
|
100
|
+
// Validation only; checks supply entries and their per-index totals.
|
|
101
|
+
validateMidnightCollateralSupplies({
|
|
102
|
+
market: params.market,
|
|
103
|
+
field: "collateralSupplies",
|
|
104
|
+
supplies: params.collateralSupplies,
|
|
105
|
+
});
|
|
107
106
|
const marketId = validateTakeableOffers({
|
|
108
107
|
market: params.market,
|
|
109
108
|
takeableOffers: params.takeableOffers,
|
|
@@ -118,31 +117,31 @@ export const midnightSupplyCollateralTakeBorrow = (params) => {
|
|
|
118
117
|
receiver: params.receiver,
|
|
119
118
|
});
|
|
120
119
|
const { target } = params;
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
}
|
|
134
|
-
else {
|
|
135
|
-
if (target.units === maxUint256) {
|
|
136
|
-
throw new UnboundedMidnightTakeInputError("target.units");
|
|
137
|
-
}
|
|
138
|
-
if (target.units <= 0n) {
|
|
139
|
-
throw new NonPositiveInputError("target.units", target.units);
|
|
120
|
+
switch (target.type) {
|
|
121
|
+
case "assets": {
|
|
122
|
+
// Validation only; rejects non-positive amounts.
|
|
123
|
+
validatePositiveInput("target.assets", target.assets);
|
|
124
|
+
if (target.maxUnits === maxUint256) {
|
|
125
|
+
throw new UnboundedMidnightTakeInputError("target.maxUnits");
|
|
126
|
+
}
|
|
127
|
+
// Validation only; rejects non-positive amounts.
|
|
128
|
+
validatePositiveInput("target.maxUnits", target.maxUnits);
|
|
129
|
+
validateMidnightUint128Field("target.assets", target.assets);
|
|
130
|
+
validateMidnightUint128Field("target.maxUnits", target.maxUnits);
|
|
131
|
+
break;
|
|
140
132
|
}
|
|
141
|
-
|
|
142
|
-
|
|
133
|
+
default: {
|
|
134
|
+
if (target.units === maxUint256) {
|
|
135
|
+
throw new UnboundedMidnightTakeInputError("target.units");
|
|
136
|
+
}
|
|
137
|
+
// Validation only; rejects non-positive amounts.
|
|
138
|
+
validatePositiveInput("target.units", target.units);
|
|
139
|
+
// Validation only; rejects non-positive amounts.
|
|
140
|
+
validatePositiveInput("target.minSellerAssets", target.minSellerAssets);
|
|
141
|
+
validateMidnightUint128Field("target.units", target.units);
|
|
142
|
+
validateMidnightUint128Field("target.minSellerAssets", target.minSellerAssets);
|
|
143
|
+
break;
|
|
143
144
|
}
|
|
144
|
-
validateMidnightUint128Field("target.units", target.units);
|
|
145
|
-
validateMidnightUint128Field("target.minSellerAssets", target.minSellerAssets);
|
|
146
145
|
}
|
|
147
146
|
if (params.deadline === maxUint256) {
|
|
148
147
|
throw new UnboundedMidnightTakeInputError("deadline");
|
|
@@ -9,8 +9,9 @@ import { validateMidnightReferralFeeCap, validateMidnightTakeableOfferBounds, va
|
|
|
9
9
|
import { validateMidnightMarket } from "../../helpers/validateMidnightMarket.js";
|
|
10
10
|
import { validateMidnightReceiver } from "../../helpers/validateMidnightReceiver.js";
|
|
11
11
|
import { validateMidnightSellTargetBounds } from "../../helpers/validateMidnightTakeFeasibility.js";
|
|
12
|
+
import { validatePositiveInput } from "../../helpers/validatePositiveInput.js";
|
|
12
13
|
import { validateTakeableOffers } from "../../helpers/validateTakeableOffers.js";
|
|
13
|
-
import {
|
|
14
|
+
import { UnboundedMidnightTakeInputError, } from "../../types/index.js";
|
|
14
15
|
/**
|
|
15
16
|
* Encodes a `MidnightBundlesV2` sell that borrows from lend-side offers for `msg.sender`.
|
|
16
17
|
*
|
|
@@ -94,31 +95,31 @@ export const midnightTakeBorrow = (params) => {
|
|
|
94
95
|
receiver: params.receiver,
|
|
95
96
|
});
|
|
96
97
|
const { target } = params;
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
98
|
+
switch (target.type) {
|
|
99
|
+
case "assets": {
|
|
100
|
+
// Validation only; rejects non-positive amounts.
|
|
101
|
+
validatePositiveInput("target.assets", target.assets);
|
|
102
|
+
if (target.maxUnits === maxUint256) {
|
|
103
|
+
throw new UnboundedMidnightTakeInputError("target.maxUnits");
|
|
104
|
+
}
|
|
105
|
+
// Validation only; rejects non-positive amounts.
|
|
106
|
+
validatePositiveInput("target.maxUnits", target.maxUnits);
|
|
107
|
+
validateMidnightUint128Field("target.assets", target.assets);
|
|
108
|
+
validateMidnightUint128Field("target.maxUnits", target.maxUnits);
|
|
109
|
+
break;
|
|
100
110
|
}
|
|
101
|
-
|
|
102
|
-
|
|
111
|
+
default: {
|
|
112
|
+
if (target.units === maxUint256) {
|
|
113
|
+
throw new UnboundedMidnightTakeInputError("target.units");
|
|
114
|
+
}
|
|
115
|
+
// Validation only; rejects non-positive amounts.
|
|
116
|
+
validatePositiveInput("target.units", target.units);
|
|
117
|
+
// Validation only; rejects non-positive amounts.
|
|
118
|
+
validatePositiveInput("target.minSellerAssets", target.minSellerAssets);
|
|
119
|
+
validateMidnightUint128Field("target.units", target.units);
|
|
120
|
+
validateMidnightUint128Field("target.minSellerAssets", target.minSellerAssets);
|
|
121
|
+
break;
|
|
103
122
|
}
|
|
104
|
-
if (target.maxUnits <= 0n) {
|
|
105
|
-
throw new NonPositiveInputError("target.maxUnits", target.maxUnits);
|
|
106
|
-
}
|
|
107
|
-
validateMidnightUint128Field("target.assets", target.assets);
|
|
108
|
-
validateMidnightUint128Field("target.maxUnits", target.maxUnits);
|
|
109
|
-
}
|
|
110
|
-
else {
|
|
111
|
-
if (target.units === maxUint256) {
|
|
112
|
-
throw new UnboundedMidnightTakeInputError("target.units");
|
|
113
|
-
}
|
|
114
|
-
if (target.units <= 0n) {
|
|
115
|
-
throw new NonPositiveInputError("target.units", target.units);
|
|
116
|
-
}
|
|
117
|
-
if (target.minSellerAssets <= 0n) {
|
|
118
|
-
throw new NonPositiveInputError("target.minSellerAssets", target.minSellerAssets);
|
|
119
|
-
}
|
|
120
|
-
validateMidnightUint128Field("target.units", target.units);
|
|
121
|
-
validateMidnightUint128Field("target.minSellerAssets", target.minSellerAssets);
|
|
122
123
|
}
|
|
123
124
|
if (params.deadline === maxUint256) {
|
|
124
125
|
throw new UnboundedMidnightTakeInputError("deadline");
|
|
@@ -8,8 +8,9 @@ import { validateMidnightAddress } from "../../helpers/validateMidnightAccount.j
|
|
|
8
8
|
import { validateMidnightReferralFeeCap, validateMidnightTakeableOfferBounds, validateMidnightUint128Field, } from "../../helpers/validateMidnightBounds.js";
|
|
9
9
|
import { validateMidnightMarket } from "../../helpers/validateMidnightMarket.js";
|
|
10
10
|
import { validateMidnightReceiver } from "../../helpers/validateMidnightReceiver.js";
|
|
11
|
+
import { validatePositiveInput } from "../../helpers/validatePositiveInput.js";
|
|
11
12
|
import { validateTakeableOffers } from "../../helpers/validateTakeableOffers.js";
|
|
12
|
-
import {
|
|
13
|
+
import { UnboundedMidnightTakeInputError, } from "../../types/index.js";
|
|
13
14
|
/**
|
|
14
15
|
* Encodes a `MidnightBundlesV2` buy that lends into borrow-side offers for `msg.sender`.
|
|
15
16
|
*
|
|
@@ -84,31 +85,31 @@ export const midnightTakeLend = (params) => {
|
|
|
84
85
|
});
|
|
85
86
|
validateMidnightTakeableOfferBounds(params.takeableOffers);
|
|
86
87
|
const { target } = params;
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
88
|
+
switch (target.type) {
|
|
89
|
+
case "assets": {
|
|
90
|
+
// Validation only; rejects non-positive amounts.
|
|
91
|
+
validatePositiveInput("target.assets", target.assets);
|
|
92
|
+
// Validation only; rejects non-positive amounts.
|
|
93
|
+
validatePositiveInput("target.minUnits", target.minUnits);
|
|
94
|
+
validateMidnightUint128Field("target.assets", target.assets);
|
|
95
|
+
validateMidnightUint128Field("target.minUnits", target.minUnits);
|
|
96
|
+
break;
|
|
90
97
|
}
|
|
91
|
-
|
|
92
|
-
|
|
98
|
+
default: {
|
|
99
|
+
if (target.units === maxUint256) {
|
|
100
|
+
throw new UnboundedMidnightTakeInputError("target.units");
|
|
101
|
+
}
|
|
102
|
+
if (target.maxBuyerAssets === maxUint256) {
|
|
103
|
+
throw new UnboundedMidnightTakeInputError("target.maxBuyerAssets");
|
|
104
|
+
}
|
|
105
|
+
// Validation only; rejects non-positive amounts.
|
|
106
|
+
validatePositiveInput("target.units", target.units);
|
|
107
|
+
// Validation only; rejects non-positive amounts.
|
|
108
|
+
validatePositiveInput("target.maxBuyerAssets", target.maxBuyerAssets);
|
|
109
|
+
validateMidnightUint128Field("target.units", target.units);
|
|
110
|
+
validateMidnightUint128Field("target.maxBuyerAssets", target.maxBuyerAssets);
|
|
111
|
+
break;
|
|
93
112
|
}
|
|
94
|
-
validateMidnightUint128Field("target.assets", target.assets);
|
|
95
|
-
validateMidnightUint128Field("target.minUnits", target.minUnits);
|
|
96
|
-
}
|
|
97
|
-
else {
|
|
98
|
-
if (target.units === maxUint256) {
|
|
99
|
-
throw new UnboundedMidnightTakeInputError("target.units");
|
|
100
|
-
}
|
|
101
|
-
if (target.maxBuyerAssets === maxUint256) {
|
|
102
|
-
throw new UnboundedMidnightTakeInputError("target.maxBuyerAssets");
|
|
103
|
-
}
|
|
104
|
-
if (target.units <= 0n) {
|
|
105
|
-
throw new NonPositiveInputError("target.units", target.units);
|
|
106
|
-
}
|
|
107
|
-
if (target.maxBuyerAssets <= 0n) {
|
|
108
|
-
throw new NonPositiveInputError("target.maxBuyerAssets", target.maxBuyerAssets);
|
|
109
|
-
}
|
|
110
|
-
validateMidnightUint128Field("target.units", target.units);
|
|
111
|
-
validateMidnightUint128Field("target.maxBuyerAssets", target.maxBuyerAssets);
|
|
112
113
|
}
|
|
113
114
|
validateUint256Field("maxContinuousFee", params.maxContinuousFee);
|
|
114
115
|
if (params.deadline === maxUint256) {
|
|
@@ -8,6 +8,7 @@ import { validateMidnightAddress } from "../../helpers/validateMidnightAccount.j
|
|
|
8
8
|
import { validateMidnightReferralFeeCap, validateMidnightTakeableOfferBounds, validateMidnightUint128Field, } from "../../helpers/validateMidnightBounds.js";
|
|
9
9
|
import { validateMidnightMarket } from "../../helpers/validateMidnightMarket.js";
|
|
10
10
|
import { validateMidnightReceiver } from "../../helpers/validateMidnightReceiver.js";
|
|
11
|
+
import { validatePositiveInput } from "../../helpers/validatePositiveInput.js";
|
|
11
12
|
import { validateTakeableOffers } from "../../helpers/validateTakeableOffers.js";
|
|
12
13
|
import { NonPositiveInputError, UnboundedMidnightTakeInputError, } from "../../types/index.js";
|
|
13
14
|
/**
|
|
@@ -100,9 +101,8 @@ export const midnightTakeRepayWithdrawCollateral = (params) => {
|
|
|
100
101
|
// Reject markets from another chain deployment before checking offers against them.
|
|
101
102
|
validateMidnightMarket({ market: params.market, chainId: params.chainId });
|
|
102
103
|
for (const [index, { collateralIndex, assets },] of params.collateralWithdrawals.entries()) {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
}
|
|
104
|
+
// Validation only; rejects non-positive amounts.
|
|
105
|
+
validatePositiveInput(`collateralWithdrawals[${index}].assets`, assets);
|
|
106
106
|
if (assets !== maxUint256) {
|
|
107
107
|
validateMidnightUint128Field(`collateralWithdrawals[${index}].assets`, assets);
|
|
108
108
|
}
|
|
@@ -126,24 +126,25 @@ export const midnightTakeRepayWithdrawCollateral = (params) => {
|
|
|
126
126
|
});
|
|
127
127
|
validateMidnightTakeableOfferBounds(params.takeableOffers);
|
|
128
128
|
const { target } = params;
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
else {
|
|
137
|
-
if (target.units <= 0n) {
|
|
138
|
-
throw new NonPositiveInputError("target.units", target.units);
|
|
139
|
-
}
|
|
140
|
-
if (target.maxBuyerAssets <= 0n) {
|
|
141
|
-
throw new NonPositiveInputError("target.maxBuyerAssets", target.maxBuyerAssets);
|
|
129
|
+
switch (target.type) {
|
|
130
|
+
case "assets": {
|
|
131
|
+
// Validation only; rejects non-positive amounts.
|
|
132
|
+
validatePositiveInput("target.minUnits", target.minUnits);
|
|
133
|
+
validateMidnightUint128Field("target.assets", target.assets);
|
|
134
|
+
validateMidnightUint128Field("target.minUnits", target.minUnits);
|
|
135
|
+
break;
|
|
142
136
|
}
|
|
143
|
-
|
|
144
|
-
|
|
137
|
+
default: {
|
|
138
|
+
// Validation only; rejects non-positive amounts.
|
|
139
|
+
validatePositiveInput("target.units", target.units);
|
|
140
|
+
// Validation only; rejects non-positive amounts.
|
|
141
|
+
validatePositiveInput("target.maxBuyerAssets", target.maxBuyerAssets);
|
|
142
|
+
if (target.units !== maxUint256) {
|
|
143
|
+
validateMidnightUint128Field("target.units", target.units);
|
|
144
|
+
}
|
|
145
|
+
validateMidnightUint128Field("target.maxBuyerAssets", target.maxBuyerAssets);
|
|
146
|
+
break;
|
|
145
147
|
}
|
|
146
|
-
validateMidnightUint128Field("target.maxBuyerAssets", target.maxBuyerAssets);
|
|
147
148
|
}
|
|
148
149
|
validateUint256Field("maxContinuousFee", params.maxContinuousFee);
|
|
149
150
|
if (params.deadline === maxUint256) {
|
|
@@ -9,8 +9,9 @@ import { validateMidnightReferralFeeCap, validateMidnightTakeableOfferBounds, va
|
|
|
9
9
|
import { validateMidnightMarket } from "../../helpers/validateMidnightMarket.js";
|
|
10
10
|
import { validateMidnightReceiver } from "../../helpers/validateMidnightReceiver.js";
|
|
11
11
|
import { validateMidnightSellTargetBounds } from "../../helpers/validateMidnightTakeFeasibility.js";
|
|
12
|
+
import { validatePositiveInput } from "../../helpers/validatePositiveInput.js";
|
|
12
13
|
import { validateTakeableOffers } from "../../helpers/validateTakeableOffers.js";
|
|
13
|
-
import {
|
|
14
|
+
import { UnboundedMidnightTakeInputError, } from "../../types/index.js";
|
|
14
15
|
const validateParams = (params) => {
|
|
15
16
|
// Reject markets from another chain deployment before checking offers against them.
|
|
16
17
|
validateMidnightMarket({ market: params.market, chainId: params.chainId });
|
|
@@ -29,31 +30,31 @@ const validateParams = (params) => {
|
|
|
29
30
|
receiver: params.receiver,
|
|
30
31
|
});
|
|
31
32
|
const { target } = params;
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
switch (target.type) {
|
|
34
|
+
case "assets": {
|
|
35
|
+
// Validation only; rejects non-positive amounts.
|
|
36
|
+
validatePositiveInput("target.assets", target.assets);
|
|
37
|
+
if (target.maxUnits === maxUint256) {
|
|
38
|
+
throw new UnboundedMidnightTakeInputError("target.maxUnits");
|
|
39
|
+
}
|
|
40
|
+
// Validation only; rejects non-positive amounts.
|
|
41
|
+
validatePositiveInput("target.maxUnits", target.maxUnits);
|
|
42
|
+
validateMidnightUint128Field("target.assets", target.assets);
|
|
43
|
+
validateMidnightUint128Field("target.maxUnits", target.maxUnits);
|
|
44
|
+
break;
|
|
35
45
|
}
|
|
36
|
-
|
|
37
|
-
|
|
46
|
+
default: {
|
|
47
|
+
if (target.units === maxUint256) {
|
|
48
|
+
throw new UnboundedMidnightTakeInputError("target.units");
|
|
49
|
+
}
|
|
50
|
+
// Validation only; rejects non-positive amounts.
|
|
51
|
+
validatePositiveInput("target.units", target.units);
|
|
52
|
+
// Validation only; rejects non-positive amounts.
|
|
53
|
+
validatePositiveInput("target.minSellerAssets", target.minSellerAssets);
|
|
54
|
+
validateMidnightUint128Field("target.units", target.units);
|
|
55
|
+
validateMidnightUint128Field("target.minSellerAssets", target.minSellerAssets);
|
|
56
|
+
break;
|
|
38
57
|
}
|
|
39
|
-
if (target.maxUnits <= 0n) {
|
|
40
|
-
throw new NonPositiveInputError("target.maxUnits", target.maxUnits);
|
|
41
|
-
}
|
|
42
|
-
validateMidnightUint128Field("target.assets", target.assets);
|
|
43
|
-
validateMidnightUint128Field("target.maxUnits", target.maxUnits);
|
|
44
|
-
}
|
|
45
|
-
else {
|
|
46
|
-
if (target.units === maxUint256) {
|
|
47
|
-
throw new UnboundedMidnightTakeInputError("target.units");
|
|
48
|
-
}
|
|
49
|
-
if (target.units <= 0n) {
|
|
50
|
-
throw new NonPositiveInputError("target.units", target.units);
|
|
51
|
-
}
|
|
52
|
-
if (target.minSellerAssets <= 0n) {
|
|
53
|
-
throw new NonPositiveInputError("target.minSellerAssets", target.minSellerAssets);
|
|
54
|
-
}
|
|
55
|
-
validateMidnightUint128Field("target.units", target.units);
|
|
56
|
-
validateMidnightUint128Field("target.minSellerAssets", target.minSellerAssets);
|
|
57
58
|
}
|
|
58
59
|
if (params.deadline === maxUint256) {
|
|
59
60
|
throw new UnboundedMidnightTakeInputError("deadline");
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { type MarketInput } from "@morpho-org/midnight-sdk";
|
|
2
|
+
import { type Address } from "viem";
|
|
3
|
+
import { type Metadata, type MidnightCollateralTransfer, type MidnightWithdrawCollateralBatchAction, type Transaction } from "../../types/index.js";
|
|
4
|
+
/** Parameters for an atomic direct Midnight collateral withdrawal batch. */
|
|
5
|
+
export interface MidnightWithdrawCollateralBatchParams {
|
|
6
|
+
readonly chainId: number;
|
|
7
|
+
readonly market: MarketInput;
|
|
8
|
+
readonly collateralWithdrawals: readonly MidnightCollateralTransfer[];
|
|
9
|
+
readonly onBehalf: Address;
|
|
10
|
+
readonly receiver: Address;
|
|
11
|
+
readonly metadata?: Metadata;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Encodes ordered collateral withdrawals in one Midnight multicall, even for one entry.
|
|
15
|
+
* Duplicates are rejected: pass one entry per index, unlike bundle flows which sum them.
|
|
16
|
+
*
|
|
17
|
+
* @param params - Market, ordered withdrawals, owner, shared receiver and optional metadata.
|
|
18
|
+
* @param params.chainId - Chain containing Midnight.
|
|
19
|
+
* @param params.market - Market holding collateral.
|
|
20
|
+
* @param params.collateralWithdrawals - Nonempty ordered withdrawals, with unique collateral indexes.
|
|
21
|
+
* @param params.onBehalf - Collateral owner; this account or an authorized operator must send the transaction.
|
|
22
|
+
* @param params.receiver - Shared recipient of all withdrawn collateral.
|
|
23
|
+
* @param params.metadata - Optional analytics metadata appended to calldata.
|
|
24
|
+
* @returns A deep-frozen transaction targeting Midnight.
|
|
25
|
+
* @throws {EmptyMidnightCollateralWithdrawalsError} when the list is empty.
|
|
26
|
+
* @throws {DuplicateMidnightCollateralIndexError} when an index repeats.
|
|
27
|
+
* @throws {MidnightZeroReceiverError} when receiver is zero.
|
|
28
|
+
* @throws {MidnightContractReceiverError} when receiver cannot safely receive collateral.
|
|
29
|
+
* @throws {NonPositiveInputError} when assets are not positive.
|
|
30
|
+
* @throws {InputExceedsMaxError} when assets exceed uint128.
|
|
31
|
+
* @throws {MidnightZeroOnBehalfError} when onBehalf is zero.
|
|
32
|
+
* @throws {ChainIdMismatchError} when the market targets another chain.
|
|
33
|
+
* @throws {MidnightMarketAddressMismatchError} when the deployment differs.
|
|
34
|
+
* @throws {UnknownCollateralIndexError} when an index is not configured.
|
|
35
|
+
* @throws {MidnightCalldataEncodingError} when an address or ABI input is malformed.
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* const tx = midnightWithdrawCollateralBatch({
|
|
39
|
+
* chainId: 8453, market: marketData.params, onBehalf: user, receiver: user,
|
|
40
|
+
* collateralWithdrawals: [{ collateralIndex: 0n, assets: 100n }],
|
|
41
|
+
* });
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export declare const midnightWithdrawCollateralBatch: (params: MidnightWithdrawCollateralBatchParams) => Readonly<Transaction<MidnightWithdrawCollateralBatchAction>>;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { MarketUtils, midnightAbi, } from "@morpho-org/midnight-sdk";
|
|
2
|
+
import { deepFreeze, getChainAddress } from "@morpho-org/morpho-ts";
|
|
3
|
+
import { isAddressEqual, zeroAddress } from "viem";
|
|
4
|
+
import { encodeMidnightCalldata, encodeMidnightMarketId, } from "../../helpers/encodeMidnightCalldata.js";
|
|
5
|
+
import { addTransactionMetadata } from "../../helpers/index.js";
|
|
6
|
+
import { validateMidnightAddress } from "../../helpers/validateMidnightAccount.js";
|
|
7
|
+
import { validateMidnightUint128Field } from "../../helpers/validateMidnightBounds.js";
|
|
8
|
+
import { validateMidnightCollateralBatch } from "../../helpers/validateMidnightCollateralBatch.js";
|
|
9
|
+
import { validateMidnightMarket } from "../../helpers/validateMidnightMarket.js";
|
|
10
|
+
import { validateMidnightReceiver } from "../../helpers/validateMidnightReceiver.js";
|
|
11
|
+
import { EmptyMidnightCollateralWithdrawalsError, MidnightZeroOnBehalfError, NonPositiveInputError, } from "../../types/index.js";
|
|
12
|
+
/**
|
|
13
|
+
* Encodes ordered collateral withdrawals in one Midnight multicall, even for one entry.
|
|
14
|
+
* Duplicates are rejected: pass one entry per index, unlike bundle flows which sum them.
|
|
15
|
+
*
|
|
16
|
+
* @param params - Market, ordered withdrawals, owner, shared receiver and optional metadata.
|
|
17
|
+
* @param params.chainId - Chain containing Midnight.
|
|
18
|
+
* @param params.market - Market holding collateral.
|
|
19
|
+
* @param params.collateralWithdrawals - Nonempty ordered withdrawals, with unique collateral indexes.
|
|
20
|
+
* @param params.onBehalf - Collateral owner; this account or an authorized operator must send the transaction.
|
|
21
|
+
* @param params.receiver - Shared recipient of all withdrawn collateral.
|
|
22
|
+
* @param params.metadata - Optional analytics metadata appended to calldata.
|
|
23
|
+
* @returns A deep-frozen transaction targeting Midnight.
|
|
24
|
+
* @throws {EmptyMidnightCollateralWithdrawalsError} when the list is empty.
|
|
25
|
+
* @throws {DuplicateMidnightCollateralIndexError} when an index repeats.
|
|
26
|
+
* @throws {MidnightZeroReceiverError} when receiver is zero.
|
|
27
|
+
* @throws {MidnightContractReceiverError} when receiver cannot safely receive collateral.
|
|
28
|
+
* @throws {NonPositiveInputError} when assets are not positive.
|
|
29
|
+
* @throws {InputExceedsMaxError} when assets exceed uint128.
|
|
30
|
+
* @throws {MidnightZeroOnBehalfError} when onBehalf is zero.
|
|
31
|
+
* @throws {ChainIdMismatchError} when the market targets another chain.
|
|
32
|
+
* @throws {MidnightMarketAddressMismatchError} when the deployment differs.
|
|
33
|
+
* @throws {UnknownCollateralIndexError} when an index is not configured.
|
|
34
|
+
* @throws {MidnightCalldataEncodingError} when an address or ABI input is malformed.
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* const tx = midnightWithdrawCollateralBatch({
|
|
38
|
+
* chainId: 8453, market: marketData.params, onBehalf: user, receiver: user,
|
|
39
|
+
* collateralWithdrawals: [{ collateralIndex: 0n, assets: 100n }],
|
|
40
|
+
* });
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export const midnightWithdrawCollateralBatch = (params) => {
|
|
44
|
+
// Reject malformed addresses before comparing with zero.
|
|
45
|
+
validateMidnightAddress("onBehalf", params.onBehalf);
|
|
46
|
+
if (isAddressEqual(params.onBehalf, zeroAddress)) {
|
|
47
|
+
throw new MidnightZeroOnBehalfError("onBehalf");
|
|
48
|
+
}
|
|
49
|
+
// Reject markets from another deployment before encoding.
|
|
50
|
+
validateMidnightMarket({ market: params.market, chainId: params.chainId });
|
|
51
|
+
validateMidnightReceiver({
|
|
52
|
+
chainId: params.chainId,
|
|
53
|
+
market: params.market,
|
|
54
|
+
field: "receiver",
|
|
55
|
+
receiver: params.receiver,
|
|
56
|
+
});
|
|
57
|
+
// Direct batches require one entry per index rather than summing duplicates.
|
|
58
|
+
validateMidnightCollateralBatch(params.collateralWithdrawals, new EmptyMidnightCollateralWithdrawalsError());
|
|
59
|
+
const calls = params.collateralWithdrawals.map(({ collateralIndex, assets }) => {
|
|
60
|
+
if (assets <= 0n)
|
|
61
|
+
throw new NonPositiveInputError("assets", assets);
|
|
62
|
+
validateMidnightUint128Field("assets", assets);
|
|
63
|
+
// Reject indexes not configured on the market.
|
|
64
|
+
MarketUtils.getCollateralByIndex(params.market, collateralIndex);
|
|
65
|
+
return encodeMidnightCalldata({
|
|
66
|
+
abi: midnightAbi,
|
|
67
|
+
functionName: "withdrawCollateral",
|
|
68
|
+
args: [
|
|
69
|
+
MarketUtils.toStruct(params.market),
|
|
70
|
+
collateralIndex,
|
|
71
|
+
assets,
|
|
72
|
+
params.onBehalf,
|
|
73
|
+
params.receiver,
|
|
74
|
+
],
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
let tx = {
|
|
78
|
+
to: getChainAddress(params.chainId, "midnight"),
|
|
79
|
+
value: 0n,
|
|
80
|
+
data: encodeMidnightCalldata({
|
|
81
|
+
abi: midnightAbi,
|
|
82
|
+
functionName: "multicall",
|
|
83
|
+
args: [calls],
|
|
84
|
+
}),
|
|
85
|
+
};
|
|
86
|
+
if (params.metadata)
|
|
87
|
+
tx = addTransactionMetadata(tx, params.metadata);
|
|
88
|
+
return deepFreeze({
|
|
89
|
+
...tx,
|
|
90
|
+
action: {
|
|
91
|
+
type: "midnightWithdrawCollateralBatch",
|
|
92
|
+
args: {
|
|
93
|
+
market: encodeMidnightMarketId(params.market),
|
|
94
|
+
collateralWithdrawals: params.collateralWithdrawals.map(({ collateralIndex, assets }) => ({ collateralIndex, assets })),
|
|
95
|
+
onBehalf: params.onBehalf,
|
|
96
|
+
receiver: params.receiver,
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
};
|
|
@@ -55,6 +55,7 @@ interface EncodeBlueSignatureAuthorizationParams {
|
|
|
55
55
|
* @throws {AddressMismatchError} from `sign()` when `userAddress` differs from `owner`, or when the
|
|
56
56
|
* client account differs from `userAddress`.
|
|
57
57
|
* @throws {InvalidSignatureError} from `sign()` when EIP-712 verification fails.
|
|
58
|
+
* @throws {MalformedSignatureError} from `sign()` when the wallet returns a malformed signature.
|
|
58
59
|
* @example
|
|
59
60
|
* ```ts
|
|
60
61
|
* import { createWalletClient, http } from "viem";
|
|
@@ -41,6 +41,7 @@ import { ChainIdMismatchError, ExpiredDeadlineError, UnsupportedAuthorizationOpe
|
|
|
41
41
|
* @throws {AddressMismatchError} from `sign()` when `userAddress` differs from `owner`, or when the
|
|
42
42
|
* client account differs from `userAddress`.
|
|
43
43
|
* @throws {InvalidSignatureError} from `sign()` when EIP-712 verification fails.
|
|
44
|
+
* @throws {MalformedSignatureError} from `sign()` when the wallet returns a malformed signature.
|
|
44
45
|
* @example
|
|
45
46
|
* ```ts
|
|
46
47
|
* import { createWalletClient, http } from "viem";
|
|
@@ -62,6 +62,7 @@ interface EncodeErc20PermitParams {
|
|
|
62
62
|
* @throws {AddressMismatchError} from `sign()` when `userAddress` differs from `owner`, or when the
|
|
63
63
|
* client account differs from `userAddress`.
|
|
64
64
|
* @throws {InvalidSignatureError} from `sign()` when EIP-712 verification fails.
|
|
65
|
+
* @throws {MalformedSignatureError} from `sign()` when the wallet returns a malformed signature.
|
|
65
66
|
* @example
|
|
66
67
|
* ```ts
|
|
67
68
|
* import { createWalletClient, http } from "viem";
|
|
@@ -52,6 +52,7 @@ import { validateClientChainId } from "../validateClientChainId.js";
|
|
|
52
52
|
* @throws {AddressMismatchError} from `sign()` when `userAddress` differs from `owner`, or when the
|
|
53
53
|
* client account differs from `userAddress`.
|
|
54
54
|
* @throws {InvalidSignatureError} from `sign()` when EIP-712 verification fails.
|
|
55
|
+
* @throws {MalformedSignatureError} from `sign()` when the wallet returns a malformed signature.
|
|
55
56
|
* @example
|
|
56
57
|
* ```ts
|
|
57
58
|
* import { createWalletClient, http } from "viem";
|
|
@@ -48,6 +48,7 @@ export interface EncodeErc20Permit2SignatureTransferParams {
|
|
|
48
48
|
* @throws {AddressMismatchError} from `sign()` when the wallet account differs from `userAddress`.
|
|
49
49
|
* @throws {ChainIdMismatchError} from `sign()` when the wallet chain differs from `chainId`.
|
|
50
50
|
* @throws {InvalidSignatureError} from `sign()` when EIP-712 verification fails.
|
|
51
|
+
* @throws {MalformedSignatureError} from `sign()` when the wallet returns a malformed signature.
|
|
51
52
|
* @example
|
|
52
53
|
* ```ts
|
|
53
54
|
* import { addressesRegistry } from "@morpho-org/blue-sdk";
|
|
@@ -33,6 +33,7 @@ import { ExpiredDeadlineError, InputExceedsMaxError, NonPositiveInputError, } fr
|
|
|
33
33
|
* @throws {AddressMismatchError} from `sign()` when the wallet account differs from `userAddress`.
|
|
34
34
|
* @throws {ChainIdMismatchError} from `sign()` when the wallet chain differs from `chainId`.
|
|
35
35
|
* @throws {InvalidSignatureError} from `sign()` when EIP-712 verification fails.
|
|
36
|
+
* @throws {MalformedSignatureError} from `sign()` when the wallet returns a malformed signature.
|
|
36
37
|
* @example
|
|
37
38
|
* ```ts
|
|
38
39
|
* import { addressesRegistry } from "@morpho-org/blue-sdk";
|