@morpho-org/morpho-sdk 9.6.0-next.0 → 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/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/vaultV2/vaultV2.d.ts +8 -33
- 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 +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 -2
- package/lib/cjs/midnight/errors.js +5 -5
- 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/vaultV2/vaultV2.d.ts +8 -33
- 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 +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 -2
- package/lib/esm/midnight/errors.js +1 -2
- 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 +6 -6
package/README.md
CHANGED
|
@@ -57,6 +57,7 @@ BlueBundlesV1, and the remaining rows identify their destination.
|
|
|
57
57
|
| **Blue** | `withdrawBuyCallback` | Morpho Blue (direct `withdraw` for a BlueBuyCallback's owner) |
|
|
58
58
|
| **Midnight** | `takeLend`, `takeBorrow`, `supplyCollateralTakeBorrow`, `takeRepayWithdrawCollateral`, `repayWithdrawCollateral`, `takeWithdraw` | MidnightBundlesV2 (Ethereum, Base, Arc, Robinhood, Tempo) |
|
|
59
59
|
| | `supplyCollateral`, `withdrawCollateral`, `redeem` | Direct call |
|
|
60
|
+
| | `supplyCollateralBatch`, `withdrawCollateralBatch` | Midnight multicall |
|
|
60
61
|
| | `makeLend`, `makeBorrow`, `makeWithdraw`, `makeRepay`, `supplyCollateralMakeBorrow`, `supplyBlueMakeLend`, `cancelOffers` | MidnightBundlesV2 (Ethereum, Base, Arc, Robinhood, Tempo) |
|
|
61
62
|
|
|
62
63
|
`makeBorrow` publishes without collateral; `supplyCollateralMakeBorrow` requires collateral and
|
|
@@ -290,6 +291,62 @@ const requirements = await output.getRequirements();
|
|
|
290
291
|
const tx = output.buildTx();
|
|
291
292
|
```
|
|
292
293
|
|
|
294
|
+
### Midnight: collateral batches
|
|
295
|
+
|
|
296
|
+
`supplyCollateralBatch` and `withdrawCollateralBatch` move several collaterals of one position in
|
|
297
|
+
one transaction: a Midnight core `multicall` of direct `supplyCollateral` or `withdrawCollateral`
|
|
298
|
+
calls that reverts as a whole. Pass one entry per collateral index; duplicates throw
|
|
299
|
+
`DuplicateMidnightCollateralIndexError`, unlike the bundle flows, which sum them.
|
|
300
|
+
|
|
301
|
+
A supply batch returns one unlimited Midnight approval per collateral token whose allowance is
|
|
302
|
+
below that token's total:
|
|
303
|
+
|
|
304
|
+
```typescript
|
|
305
|
+
const supply = midnight.supplyCollateralBatch({
|
|
306
|
+
accountAddress: user,
|
|
307
|
+
marketData,
|
|
308
|
+
collateralSupplies: [
|
|
309
|
+
{ collateralIndex: 0n, assets: 1_000_000n },
|
|
310
|
+
{ collateralIndex: 1n, assets: 2_000_000_000_000_000_000n },
|
|
311
|
+
],
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
for (const approval of await supply.getRequirements()) {
|
|
315
|
+
const hash = await walletClient.sendTransaction({ ...approval, account: user });
|
|
316
|
+
await client.waitForTransactionReceipt({ hash });
|
|
317
|
+
}
|
|
318
|
+
await walletClient.sendTransaction({ ...supply.buildTx(), account: user });
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
A withdraw batch checks every entry against `positionData` and, when the position has debt, checks
|
|
322
|
+
the remaining collateral against the LLTV buffer using `collateralPrices`. `maxUint256` withdraws
|
|
323
|
+
that index's whole balance. It needs no requirements:
|
|
324
|
+
|
|
325
|
+
```typescript
|
|
326
|
+
import { blueOracleAbi } from "@morpho-org/morpho-sdk/abis";
|
|
327
|
+
import { maxUint256 } from "viem";
|
|
328
|
+
|
|
329
|
+
const positionData = await midnight.getPositionData({ marketId, accountAddress: user });
|
|
330
|
+
const collateralPrices = await Promise.all(
|
|
331
|
+
marketData.params.collateralParams.map(({ oracle }) =>
|
|
332
|
+
client.readContract({ address: oracle, abi: blueOracleAbi, functionName: "price" }),
|
|
333
|
+
),
|
|
334
|
+
);
|
|
335
|
+
|
|
336
|
+
const withdrawal = midnight.withdrawCollateralBatch({
|
|
337
|
+
accountAddress: user,
|
|
338
|
+
marketData,
|
|
339
|
+
positionData,
|
|
340
|
+
collateralPrices,
|
|
341
|
+
collateralWithdrawals: [
|
|
342
|
+
{ collateralIndex: 0n, assets: 500_000n },
|
|
343
|
+
{ collateralIndex: 1n, assets: maxUint256 },
|
|
344
|
+
],
|
|
345
|
+
receiver: user,
|
|
346
|
+
});
|
|
347
|
+
await walletClient.sendTransaction({ ...withdrawal.buildTx(), account: user });
|
|
348
|
+
```
|
|
349
|
+
|
|
293
350
|
See the [documentation](https://docs.morpho.org/developers/sdks/morpho-sdk/) for the full API:
|
|
294
351
|
native wrapping, Vault V2 BluePublicAllocator reallocations, borrow-position migration, vault V1 →
|
|
295
352
|
V2 migration, force withdraw/redeem, and Midnight maker flows.
|
|
@@ -368,10 +425,12 @@ graph LR
|
|
|
368
425
|
MN1 --> MNM[maker offer submission]
|
|
369
426
|
MN1 --> MNR[repayWithdrawCollateral]
|
|
370
427
|
MN1 --> MNP[collateral supply / redeem]
|
|
428
|
+
MN1 --> MNCB[collateral batches]
|
|
371
429
|
MNT --> MNB2[MidnightBundlesV2]
|
|
372
430
|
MNM --> MNB2
|
|
373
431
|
MNR --> MNB2
|
|
374
432
|
MNP --> MNC[Midnight]
|
|
433
|
+
MNCB -->|multicall| MNC
|
|
375
434
|
end
|
|
376
435
|
|
|
377
436
|
|
package/lib/cjs/abis.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { marketParamsAbi as blueMarketParamsAbi } from "@morpho-org/blue-sdk";
|
|
2
|
-
export { adaptiveCurveIrmAbi as blueAdaptiveCurveIrmAbi, blueAbi, blueOracleAbi, erc2612Abi, erc3643Abi, erc3643IdentityRegistryAbi, erc5267Abi, metaMorphoAbi, metaMorphoFactoryAbi,
|
|
2
|
+
export { adaptiveCurveIrmAbi as blueAdaptiveCurveIrmAbi, blueAbi, blueOracleAbi, erc2612Abi, erc3643Abi, erc3643IdentityRegistryAbi, erc5267Abi, metaMorphoAbi, metaMorphoFactoryAbi, morphoMarketV1AdapterAbi, morphoMarketV1AdapterFactoryAbi, morphoMarketV1AdapterV2Abi, morphoMarketV1AdapterV2FactoryAbi, morphoVaultV1AdapterAbi, morphoVaultV1AdapterFactoryAbi, permissionedErc20WrapperAbi, permit2Abi,
|
|
3
3
|
/** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
|
|
4
4
|
preLiquidationAbi as bluePreLiquidationAbi,
|
|
5
5
|
/** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
|
package/lib/cjs/abis.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.blueBundlesV1Abi = exports.vaultBundlesV1Abi = exports.vaultExitBundlesV1Abi = exports.midnightSetterRatifierAbi = exports.midnightRateRatifierV1Abi = exports.midnightPriceRatifierV1Abi = exports.midnightEventsAbi = exports.midnightBundlesV2Abi = exports.midnightAbi = exports.midnightEcrecoverRatifierAbi = exports.midnightEcrecoverAuthorizerAbi = exports.midnightBlueBuyCallbackFactoryAbi = exports.midnightBlueBuyCallbackAbi = exports.wstEthAbi = exports.wrappedBackedTokenAbi = exports.whitelistControllerAggregatorV2Abi = exports.vaultV2FactoryAbi = exports.vaultV2BluePublicAllocatorAbi = exports.vaultV2Abi = exports.bluePreLiquidationFactoryAbi = exports.bluePreLiquidationAbi = exports.permit2Abi = exports.permissionedErc20WrapperAbi = exports.morphoVaultV1AdapterFactoryAbi = exports.morphoVaultV1AdapterAbi = exports.morphoMarketV1AdapterV2FactoryAbi = exports.morphoMarketV1AdapterV2Abi = exports.morphoMarketV1AdapterFactoryAbi = exports.morphoMarketV1AdapterAbi = exports.
|
|
3
|
+
exports.blueBundlesV1Abi = exports.vaultBundlesV1Abi = exports.vaultExitBundlesV1Abi = exports.midnightSetterRatifierAbi = exports.midnightRateRatifierV1Abi = exports.midnightPriceRatifierV1Abi = exports.midnightEventsAbi = exports.midnightBundlesV2Abi = exports.midnightAbi = exports.midnightEcrecoverRatifierAbi = exports.midnightEcrecoverAuthorizerAbi = exports.midnightBlueBuyCallbackFactoryAbi = exports.midnightBlueBuyCallbackAbi = exports.wstEthAbi = exports.wrappedBackedTokenAbi = exports.whitelistControllerAggregatorV2Abi = exports.vaultV2FactoryAbi = exports.vaultV2BluePublicAllocatorAbi = exports.vaultV2Abi = exports.bluePreLiquidationFactoryAbi = exports.bluePreLiquidationAbi = exports.permit2Abi = exports.permissionedErc20WrapperAbi = exports.morphoVaultV1AdapterFactoryAbi = exports.morphoVaultV1AdapterAbi = exports.morphoMarketV1AdapterV2FactoryAbi = exports.morphoMarketV1AdapterV2Abi = exports.morphoMarketV1AdapterFactoryAbi = exports.morphoMarketV1AdapterAbi = exports.metaMorphoFactoryAbi = exports.metaMorphoAbi = exports.erc5267Abi = exports.erc3643IdentityRegistryAbi = exports.erc3643Abi = exports.erc2612Abi = exports.blueOracleAbi = exports.blueAbi = exports.blueAdaptiveCurveIrmAbi = exports.blueMarketParamsAbi = void 0;
|
|
4
4
|
var blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
5
5
|
Object.defineProperty(exports, "blueMarketParamsAbi", { enumerable: true, get: function () { return blue_sdk_1.marketParamsAbi; } });
|
|
6
6
|
var blue_sdk_viem_1 = require("@morpho-org/blue-sdk-viem");
|
|
@@ -13,10 +13,6 @@ Object.defineProperty(exports, "erc3643IdentityRegistryAbi", { enumerable: true,
|
|
|
13
13
|
Object.defineProperty(exports, "erc5267Abi", { enumerable: true, get: function () { return blue_sdk_viem_1.erc5267Abi; } });
|
|
14
14
|
Object.defineProperty(exports, "metaMorphoAbi", { enumerable: true, get: function () { return blue_sdk_viem_1.metaMorphoAbi; } });
|
|
15
15
|
Object.defineProperty(exports, "metaMorphoFactoryAbi", { enumerable: true, get: function () { return blue_sdk_viem_1.metaMorphoFactoryAbi; } });
|
|
16
|
-
Object.defineProperty(exports, "midnightAdapterAbi", { enumerable: true, get: function () { return blue_sdk_viem_1.midnightAdapterAbi; } });
|
|
17
|
-
Object.defineProperty(exports, "midnightAdapterFactoryAbi", { enumerable: true, get: function () { return blue_sdk_viem_1.midnightAdapterFactoryAbi; } });
|
|
18
|
-
Object.defineProperty(exports, "midnightAdapterPriceRatifierV1Abi", { enumerable: true, get: function () { return blue_sdk_viem_1.midnightAdapterPriceRatifierV1Abi; } });
|
|
19
|
-
Object.defineProperty(exports, "midnightAdapterRateRatifierV1Abi", { enumerable: true, get: function () { return blue_sdk_viem_1.midnightAdapterRateRatifierV1Abi; } });
|
|
20
16
|
Object.defineProperty(exports, "morphoMarketV1AdapterAbi", { enumerable: true, get: function () { return blue_sdk_viem_1.morphoMarketV1AdapterAbi; } });
|
|
21
17
|
Object.defineProperty(exports, "morphoMarketV1AdapterFactoryAbi", { enumerable: true, get: function () { return blue_sdk_viem_1.morphoMarketV1AdapterFactoryAbi; } });
|
|
22
18
|
Object.defineProperty(exports, "morphoMarketV1AdapterV2Abi", { enumerable: true, get: function () { return blue_sdk_viem_1.morphoMarketV1AdapterV2Abi; } });
|
|
@@ -88,8 +88,8 @@ const blueRepayWithdrawCollateral = (params) => {
|
|
|
88
88
|
if (repayAssets > 0n && repayShares > 0n) {
|
|
89
89
|
throw new index_js_1.MutuallyExclusiveRepayAmountsError(marketParams.id);
|
|
90
90
|
}
|
|
91
|
-
const hasRepay = repayAssets
|
|
92
|
-
if (
|
|
91
|
+
const hasRepay = repayAssets + repayShares > 0n;
|
|
92
|
+
if (repayAssets + repayShares + collateralAssets === 0n) {
|
|
93
93
|
throw new index_js_1.NonPositiveInputError("repayAssets, repayShares, or collateralAssets", 0n);
|
|
94
94
|
}
|
|
95
95
|
if (hasRepay && maxRepayAssets === 0n) {
|
|
@@ -222,6 +222,15 @@ const normalizeBundlesSignature = (serializedSignature, onInvalid = (details) =>
|
|
|
222
222
|
return { v: Number(normalizedV), r: parsed.r, s: parsed.s };
|
|
223
223
|
};
|
|
224
224
|
exports.normalizeBundlesSignature = normalizeBundlesSignature;
|
|
225
|
+
const validateBundlesPermitValue = (params) => {
|
|
226
|
+
if (params.actual !== params.expected) {
|
|
227
|
+
throw new index_js_2.BundlesPermitMismatchError({
|
|
228
|
+
field: params.field,
|
|
229
|
+
expected: String(params.expected),
|
|
230
|
+
actual: String(params.actual),
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
};
|
|
225
234
|
/**
|
|
226
235
|
* Converts an optional vault-share ERC-2612 requirement into the ABI-ready Permit tuple.
|
|
227
236
|
*
|
|
@@ -253,10 +262,6 @@ const getBundlesSharesPermit = (params) => {
|
|
|
253
262
|
const { requirementSignature } = params;
|
|
254
263
|
// Require a positive uint256 for the fallback permit deadline.
|
|
255
264
|
(0, validate_js_1.validateDeadline)(params.deadline);
|
|
256
|
-
if (requirementSignature != null) {
|
|
257
|
-
// Require a positive uint256 for the signed permit deadline too.
|
|
258
|
-
(0, validate_js_1.validateDeadline)(requirementSignature.args.deadline);
|
|
259
|
-
}
|
|
260
265
|
if (requirementSignature == null) {
|
|
261
266
|
return {
|
|
262
267
|
value: 0n,
|
|
@@ -267,6 +272,9 @@ const getBundlesSharesPermit = (params) => {
|
|
|
267
272
|
s: viem_1.zeroHash,
|
|
268
273
|
};
|
|
269
274
|
}
|
|
275
|
+
// Require a positive uint256 for the signed permit deadline too.
|
|
276
|
+
(0, validate_js_1.validateDeadline)(requirementSignature.args.deadline);
|
|
277
|
+
const validatesOperation = params.owner != null || params.spender != null || params.amount != null;
|
|
270
278
|
if (requirementSignature.action.type !== "permit") {
|
|
271
279
|
throw new index_js_2.BundlesPermitMismatchError({
|
|
272
280
|
field: "type",
|
|
@@ -306,29 +314,26 @@ const getBundlesSharesPermit = (params) => {
|
|
|
306
314
|
actual: String(requirementSignature.args.amount),
|
|
307
315
|
});
|
|
308
316
|
}
|
|
309
|
-
if (
|
|
310
|
-
|
|
311
|
-
|
|
317
|
+
if (validatesOperation) {
|
|
318
|
+
// Validation only; checks signed values against their operation descriptors.
|
|
319
|
+
validateBundlesPermitValue({
|
|
312
320
|
field: "amount",
|
|
313
|
-
expected:
|
|
314
|
-
actual:
|
|
321
|
+
expected: requirementSignature.args.amount,
|
|
322
|
+
actual: requirementSignature.action.args.amount,
|
|
315
323
|
});
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
requirementSignature.action.args.deadline !==
|
|
319
|
-
requirementSignature.args.deadline) {
|
|
320
|
-
throw new index_js_2.BundlesPermitMismatchError({
|
|
324
|
+
// Validation only; checks the signed deadline against its operation descriptor.
|
|
325
|
+
validateBundlesPermitValue({
|
|
321
326
|
field: "deadline",
|
|
322
|
-
expected:
|
|
323
|
-
actual:
|
|
327
|
+
expected: requirementSignature.args.deadline,
|
|
328
|
+
actual: requirementSignature.action.args.deadline,
|
|
324
329
|
});
|
|
325
330
|
}
|
|
326
|
-
if (requirementSignature.action.args.nonce != null
|
|
327
|
-
|
|
328
|
-
|
|
331
|
+
if (requirementSignature.action.args.nonce != null) {
|
|
332
|
+
// Validation only; checks the optional operation nonce against the signed nonce.
|
|
333
|
+
validateBundlesPermitValue({
|
|
329
334
|
field: "nonce",
|
|
330
|
-
expected:
|
|
331
|
-
actual:
|
|
335
|
+
expected: requirementSignature.args.nonce,
|
|
336
|
+
actual: requirementSignature.action.args.nonce,
|
|
332
337
|
});
|
|
333
338
|
}
|
|
334
339
|
const signature = (0, exports.normalizeBundlesSignature)(requirementSignature.args.signature);
|
|
@@ -357,7 +362,9 @@ exports.getBundlesSharesPermit = getBundlesSharesPermit;
|
|
|
357
362
|
* from the operation's, or when a carried action nonce is inconsistent with the signed nonce.
|
|
358
363
|
*/
|
|
359
364
|
const selectBundlesTokenRequirementSignature = (signatures, expected) => {
|
|
360
|
-
if (signatures == null
|
|
365
|
+
if (signatures == null)
|
|
366
|
+
return undefined;
|
|
367
|
+
if (signatures.length === 0)
|
|
361
368
|
return undefined;
|
|
362
369
|
const selected = signatures.filter((signature) => signature.action.type === "permit" ||
|
|
363
370
|
signature.action.type === "permit2SignatureTransfer");
|
|
@@ -380,24 +387,24 @@ const selectBundlesTokenRequirementSignature = (signatures, expected) => {
|
|
|
380
387
|
actual: action.args.spender,
|
|
381
388
|
});
|
|
382
389
|
}
|
|
383
|
-
|
|
384
|
-
|
|
390
|
+
const actualAmount = selectedSignature.args.amount !== expected.amount
|
|
391
|
+
? selectedSignature.args.amount
|
|
392
|
+
: action.args.amount;
|
|
393
|
+
if (actualAmount !== expected.amount) {
|
|
385
394
|
throw new index_js_2.BundlesPermitMismatchError({
|
|
386
395
|
field: "amount",
|
|
387
396
|
expected: String(expected.amount),
|
|
388
|
-
actual: String(
|
|
389
|
-
? selectedSignature.args.amount
|
|
390
|
-
: action.args.amount),
|
|
397
|
+
actual: String(actualAmount),
|
|
391
398
|
});
|
|
392
399
|
}
|
|
393
|
-
|
|
394
|
-
|
|
400
|
+
const actualDeadline = selectedSignature.args.deadline !== expected.deadline
|
|
401
|
+
? selectedSignature.args.deadline
|
|
402
|
+
: action.args.deadline;
|
|
403
|
+
if (actualDeadline !== expected.deadline) {
|
|
395
404
|
throw new index_js_2.BundlesPermitMismatchError({
|
|
396
405
|
field: "deadline",
|
|
397
406
|
expected: String(expected.deadline),
|
|
398
|
-
actual: String(
|
|
399
|
-
? selectedSignature.args.deadline
|
|
400
|
-
: action.args.deadline),
|
|
407
|
+
actual: String(actualDeadline),
|
|
401
408
|
});
|
|
402
409
|
}
|
|
403
410
|
if (action.args.nonce != null &&
|
|
@@ -7,19 +7,10 @@ const viem_1 = require("viem");
|
|
|
7
7
|
const encodeMidnightCalldata_js_1 = require("../../helpers/encodeMidnightCalldata.js");
|
|
8
8
|
const index_js_1 = require("../../helpers/index.js");
|
|
9
9
|
const validate_js_1 = require("../../helpers/validate.js");
|
|
10
|
-
const validateMidnightAccount_js_1 = require("../../helpers/validateMidnightAccount.js");
|
|
11
10
|
const validateMidnightBounds_js_1 = require("../../helpers/validateMidnightBounds.js");
|
|
12
|
-
const
|
|
11
|
+
const validateMidnightCancelAndMakePublication_js_1 = require("../../helpers/validateMidnightCancelAndMakePublication.js");
|
|
13
12
|
const validateMidnightPublication_js_1 = require("../../helpers/validateMidnightPublication.js");
|
|
14
13
|
const index_js_2 = require("../../types/index.js");
|
|
15
|
-
/** Blue market argument for `MidnightBundlesV2` calls that park no loan assets. */
|
|
16
|
-
const emptyBlueMarket = {
|
|
17
|
-
loanToken: viem_1.zeroAddress,
|
|
18
|
-
collateralToken: viem_1.zeroAddress,
|
|
19
|
-
oracle: viem_1.zeroAddress,
|
|
20
|
-
irm: viem_1.zeroAddress,
|
|
21
|
-
lltv: 0n,
|
|
22
|
-
};
|
|
23
14
|
/** Midnight market argument for `MidnightBundlesV2` calls that supply no collateral. */
|
|
24
15
|
const emptyMidnightMarket = {
|
|
25
16
|
chainId: 0n,
|
|
@@ -136,15 +127,8 @@ const midnightCancelAndMake = (params) => {
|
|
|
136
127
|
const cancelledGroups = new Set();
|
|
137
128
|
for (const [index, { group, maxConsumed },] of snapshot.cancellations.entries()) {
|
|
138
129
|
const field = `cancellations[${index}].maxConsumed`;
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
if (maxConsumed > viem_1.maxUint128) {
|
|
142
|
-
throw new index_js_2.InputExceedsMaxError({
|
|
143
|
-
field,
|
|
144
|
-
value: maxConsumed,
|
|
145
|
-
max: viem_1.maxUint128,
|
|
146
|
-
});
|
|
147
|
-
}
|
|
130
|
+
// Validation only; enforces the cancellation ceiling uint128 range.
|
|
131
|
+
(0, validateMidnightBounds_js_1.validateMidnightUint128Field)(field, maxConsumed);
|
|
148
132
|
const key = group.toLowerCase();
|
|
149
133
|
if (cancelledGroups.has(key)) {
|
|
150
134
|
throw new index_js_2.DuplicateMidnightGroupCancellationError({ index, group });
|
|
@@ -158,54 +142,12 @@ const midnightCancelAndMake = (params) => {
|
|
|
158
142
|
}
|
|
159
143
|
}
|
|
160
144
|
else {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
}
|
|
167
|
-
if (publication.groups.length === 0) {
|
|
168
|
-
throw new midnight_sdk_1.InvalidTreeError("Offer groups cannot be empty.");
|
|
169
|
-
}
|
|
170
|
-
const { priceRatifierV1, rateRatifierV1 } = (0, morpho_ts_1.getChainAddresses)(snapshot.chainId);
|
|
171
|
-
const supportedTargets = [priceRatifierV1, rateRatifierV1].filter((target) => target != null);
|
|
172
|
-
(0, validateMidnightAccount_js_1.validateMidnightAddress)("publication.ratifier", publication.ratifier);
|
|
173
|
-
if (!supportedTargets.some((target) => (0, viem_1.isAddressEqual)(publication.ratifier, target))) {
|
|
174
|
-
throw new index_js_2.UnsupportedMidnightAuthorizationTargetError({
|
|
175
|
-
authorized: publication.ratifier,
|
|
176
|
-
chainId: snapshot.chainId,
|
|
177
|
-
supportedTargets,
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
for (const group of publication.groups) {
|
|
181
|
-
if (cancelledGroups.has(group.toLowerCase())) {
|
|
182
|
-
throw new index_js_2.MidnightReplacementGroupCancelledError({ group });
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
if (publication.blueSupply != null) {
|
|
186
|
-
if (publication.blueSupply.assets <= 0n) {
|
|
187
|
-
throw new index_js_2.NonPositiveInputError("blueSupply.assets", publication.blueSupply.assets);
|
|
188
|
-
}
|
|
189
|
-
(0, validateMidnightBounds_js_1.validateMidnightUint128Field)("blueSupply.assets", publication.blueSupply.assets);
|
|
190
|
-
}
|
|
191
|
-
if (publication.collateral != null) {
|
|
192
|
-
if (publication.collateral.supplies.length === 0) {
|
|
193
|
-
throw new index_js_2.EmptyMidnightCollateralSuppliesError();
|
|
194
|
-
}
|
|
195
|
-
(0, validateMidnightMarket_js_1.validateMidnightMarket)({
|
|
196
|
-
market: publication.collateral.market,
|
|
197
|
-
chainId: snapshot.chainId,
|
|
198
|
-
});
|
|
199
|
-
for (const [index, { collateralIndex, assets },] of publication.collateral.supplies.entries()) {
|
|
200
|
-
if (assets <= 0n) {
|
|
201
|
-
throw new index_js_2.NonPositiveInputError(`collateral.supplies[${index}].assets`, assets);
|
|
202
|
-
}
|
|
203
|
-
(0, validateMidnightBounds_js_1.validateMidnightUint128Field)(`collateral.supplies[${index}].assets`, assets);
|
|
204
|
-
// Throws UnknownCollateralIndexError when the index is not configured on the market.
|
|
205
|
-
midnight_sdk_1.MarketUtils.getCollateralByIndex(publication.collateral.market, collateralIndex);
|
|
206
|
-
}
|
|
207
|
-
(0, validateMidnightBounds_js_1.validateMidnightCollateralSupplySums)("collateral.supplies", publication.collateral.supplies);
|
|
208
|
-
}
|
|
145
|
+
// Validation only; checks publication content and funding inputs.
|
|
146
|
+
(0, validateMidnightCancelAndMakePublication_js_1.validateMidnightCancelAndMakePublication)({
|
|
147
|
+
publication,
|
|
148
|
+
chainId: snapshot.chainId,
|
|
149
|
+
cancelledGroups,
|
|
150
|
+
});
|
|
209
151
|
}
|
|
210
152
|
const collateral = publication?.collateral;
|
|
211
153
|
const collateralPulls = collateral == null
|
|
@@ -233,28 +175,8 @@ const midnightCancelAndMake = (params) => {
|
|
|
233
175
|
group,
|
|
234
176
|
maxConsumed,
|
|
235
177
|
}));
|
|
236
|
-
const
|
|
237
|
-
const
|
|
238
|
-
? undefined
|
|
239
|
-
: {
|
|
240
|
-
market: {
|
|
241
|
-
loanToken: publication.blueSupply.market.loanToken,
|
|
242
|
-
collateralToken: publication.blueSupply.market.collateralToken,
|
|
243
|
-
oracle: publication.blueSupply.market.oracle,
|
|
244
|
-
irm: publication.blueSupply.market.irm,
|
|
245
|
-
lltv: publication.blueSupply.market.lltv,
|
|
246
|
-
},
|
|
247
|
-
assets: publication.blueSupply.assets,
|
|
248
|
-
callbackSalt: publication.blueSupply.callbackSalt,
|
|
249
|
-
};
|
|
250
|
-
const signature = publication?.rootSignature ?? {
|
|
251
|
-
height: 0n,
|
|
252
|
-
nonce: 0n,
|
|
253
|
-
deadline: 0n,
|
|
254
|
-
v: 0,
|
|
255
|
-
r: viem_1.zeroHash,
|
|
256
|
-
s: viem_1.zeroHash,
|
|
257
|
-
};
|
|
178
|
+
const normalized = (0, validateMidnightCancelAndMakePublication_js_1.normalizeMidnightCancelAndMakePublication)(publication);
|
|
179
|
+
const { supplies, blueSupply, signature } = normalized;
|
|
258
180
|
let tx = {
|
|
259
181
|
to: (0, morpho_ts_1.getChainAddress)(snapshot.chainId, "midnightBundlesV2"),
|
|
260
182
|
value: nativeFunding.value,
|
|
@@ -262,15 +184,15 @@ const midnightCancelAndMake = (params) => {
|
|
|
262
184
|
abi: midnight_sdk_1.midnightBundlesV2Abi,
|
|
263
185
|
functionName: "midnightBundlesV2CancelAndMake",
|
|
264
186
|
args: [
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
187
|
+
normalized.blueMarket, // blueMarket
|
|
188
|
+
normalized.assetsToPark, // assetsToPark
|
|
189
|
+
normalized.callbackSalt, // callbackSalt
|
|
268
190
|
publication?.collateral == null
|
|
269
191
|
? emptyMidnightMarket
|
|
270
192
|
: midnight_sdk_1.MarketUtils.toStruct(publication.collateral.market), // market
|
|
271
193
|
supplies, // collateralSupplies
|
|
272
|
-
|
|
273
|
-
|
|
194
|
+
normalized.ratifier, // ratifier
|
|
195
|
+
normalized.root, // newRoot
|
|
274
196
|
signature.height, // signatureHeight
|
|
275
197
|
signature.nonce, // signatureNonce
|
|
276
198
|
signature.deadline, // signatureDeadline
|
|
@@ -278,7 +200,7 @@ const midnightCancelAndMake = (params) => {
|
|
|
278
200
|
signature.r, // r
|
|
279
201
|
signature.s, // s
|
|
280
202
|
cancellations, // groupsToCancel
|
|
281
|
-
|
|
203
|
+
normalized.payload, // payload
|
|
282
204
|
snapshot.deadline, // deadline
|
|
283
205
|
nativeFunding.wrappedNative, // wrappedNative
|
|
284
206
|
],
|
|
@@ -295,9 +217,9 @@ const midnightCancelAndMake = (params) => {
|
|
|
295
217
|
market: publication?.collateral == null
|
|
296
218
|
? viem_1.zeroHash
|
|
297
219
|
: (0, encodeMidnightCalldata_js_1.encodeMidnightMarketId)(publication.collateral.market),
|
|
298
|
-
ratifier:
|
|
299
|
-
root:
|
|
300
|
-
groups:
|
|
220
|
+
ratifier: normalized.ratifier,
|
|
221
|
+
root: normalized.root,
|
|
222
|
+
groups: normalized.groups,
|
|
301
223
|
cancellations,
|
|
302
224
|
collateralSupplies: supplies,
|
|
303
225
|
...(blueSupply && { blueSupply }),
|
|
@@ -3,6 +3,7 @@ export * from "./cancelAndMake.js";
|
|
|
3
3
|
export * from "./redeem.js";
|
|
4
4
|
export * from "./repayWithdrawCollateral.js";
|
|
5
5
|
export * from "./supplyCollateral.js";
|
|
6
|
+
export * from "./supplyCollateralBatch.js";
|
|
6
7
|
export * from "./supplyCollateralTakeBorrow.js";
|
|
7
8
|
export * from "./takeBorrow.js";
|
|
8
9
|
export * from "./takeLend.js";
|
|
@@ -10,3 +11,4 @@ export * from "./takeRepayWithdrawCollateral.js";
|
|
|
10
11
|
export * from "./takeWithdraw.js";
|
|
11
12
|
export * from "./types.js";
|
|
12
13
|
export * from "./withdrawCollateral.js";
|
|
14
|
+
export * from "./withdrawCollateralBatch.js";
|
|
@@ -19,6 +19,7 @@ __exportStar(require("./cancelAndMake.js"), exports);
|
|
|
19
19
|
__exportStar(require("./redeem.js"), exports);
|
|
20
20
|
__exportStar(require("./repayWithdrawCollateral.js"), exports);
|
|
21
21
|
__exportStar(require("./supplyCollateral.js"), exports);
|
|
22
|
+
__exportStar(require("./supplyCollateralBatch.js"), exports);
|
|
22
23
|
__exportStar(require("./supplyCollateralTakeBorrow.js"), exports);
|
|
23
24
|
__exportStar(require("./takeBorrow.js"), exports);
|
|
24
25
|
__exportStar(require("./takeLend.js"), exports);
|
|
@@ -26,3 +27,4 @@ __exportStar(require("./takeRepayWithdrawCollateral.js"), exports);
|
|
|
26
27
|
__exportStar(require("./takeWithdraw.js"), exports);
|
|
27
28
|
__exportStar(require("./types.js"), exports);
|
|
28
29
|
__exportStar(require("./withdrawCollateral.js"), exports);
|
|
30
|
+
__exportStar(require("./withdrawCollateralBatch.js"), exports);
|
|
@@ -10,6 +10,7 @@ const validate_js_1 = require("../../helpers/validate.js");
|
|
|
10
10
|
const validateMidnightAccount_js_1 = require("../../helpers/validateMidnightAccount.js");
|
|
11
11
|
const validateMidnightMarket_js_1 = require("../../helpers/validateMidnightMarket.js");
|
|
12
12
|
const validateMidnightReceiver_js_1 = require("../../helpers/validateMidnightReceiver.js");
|
|
13
|
+
const validatePositiveInput_js_1 = require("../../helpers/validatePositiveInput.js");
|
|
13
14
|
const index_js_2 = require("../../types/index.js");
|
|
14
15
|
/**
|
|
15
16
|
* Encodes a reduce-only `MidnightBundlesV2` buy with no offers, so the sender's debt is repaid
|
|
@@ -79,9 +80,8 @@ const midnightRepayWithdrawCollateral = (params) => {
|
|
|
79
80
|
throw new index_js_2.NonPositiveInputError("repay or withdraw amount", 0n);
|
|
80
81
|
}
|
|
81
82
|
if (repay.type === "full") {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
}
|
|
83
|
+
// Validation only; rejects non-positive amounts.
|
|
84
|
+
(0, validatePositiveInput_js_1.validatePositiveInput)("repay.maxBuyerAssets", repay.maxBuyerAssets);
|
|
85
85
|
if (repay.maxBuyerAssets >= viem_1.maxUint256) {
|
|
86
86
|
throw new index_js_2.InputExceedsMaxError({
|
|
87
87
|
field: "repay.maxBuyerAssets",
|
|
@@ -92,9 +92,8 @@ const midnightRepayWithdrawCollateral = (params) => {
|
|
|
92
92
|
}
|
|
93
93
|
(0, validateMidnightMarket_js_1.validateMidnightMarket)({ market: params.market, chainId: params.chainId });
|
|
94
94
|
for (const [index, { collateralIndex, assets },] of params.collateralWithdrawals.entries()) {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
95
|
+
// Validation only; rejects non-positive amounts.
|
|
96
|
+
(0, validatePositiveInput_js_1.validatePositiveInput)(`collateralWithdrawals[${index}].assets`, assets);
|
|
98
97
|
(0, validate_js_1.validateUint256Field)(`collateralWithdrawals[${index}].assets`, assets);
|
|
99
98
|
// Throws UnknownCollateralIndexError when the index is not configured on the market.
|
|
100
99
|
midnight_sdk_1.MarketUtils.getCollateralByIndex(params.market, collateralIndex);
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type MarketInput } from "@morpho-org/midnight-sdk";
|
|
2
|
+
import { type Address } from "viem";
|
|
3
|
+
import { type Metadata, type MidnightCollateralTransfer, type MidnightSupplyCollateralBatchAction, type Transaction } from "../../types/index.js";
|
|
4
|
+
/** Parameters for an atomic direct Midnight collateral supply batch. */
|
|
5
|
+
export interface MidnightSupplyCollateralBatchParams {
|
|
6
|
+
readonly chainId: number;
|
|
7
|
+
readonly market: MarketInput;
|
|
8
|
+
readonly collateralSupplies: readonly MidnightCollateralTransfer[];
|
|
9
|
+
readonly onBehalf: Address;
|
|
10
|
+
readonly metadata?: Metadata;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Encodes ordered collateral supplies 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.chainId - Chain containing Midnight.
|
|
17
|
+
* @param params.market - Market receiving collateral.
|
|
18
|
+
* @param params.collateralSupplies - Nonempty ordered supplies, with unique collateral indexes.
|
|
19
|
+
* @param params.onBehalf - Account receiving collateral; this account or an authorized operator must send the transaction.
|
|
20
|
+
* @param params.metadata - Optional analytics metadata appended to calldata.
|
|
21
|
+
* @returns A deep-frozen transaction targeting Midnight.
|
|
22
|
+
* @throws {EmptyMidnightCollateralSuppliesError} when the list is empty.
|
|
23
|
+
* @throws {DuplicateMidnightCollateralIndexError} when an index repeats.
|
|
24
|
+
* @throws {NonPositiveInputError} when assets are not positive.
|
|
25
|
+
* @throws {InputExceedsMaxError} when assets exceed uint128.
|
|
26
|
+
* @throws {MidnightZeroOnBehalfError} when onBehalf is zero.
|
|
27
|
+
* @throws {ChainIdMismatchError} when the market targets another chain.
|
|
28
|
+
* @throws {MidnightMarketAddressMismatchError} when the deployment differs.
|
|
29
|
+
* @throws {UnknownCollateralIndexError} when an index is not configured.
|
|
30
|
+
* @throws {MidnightCalldataEncodingError} when an address or ABI input is malformed.
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* const tx = midnightSupplyCollateralBatch({
|
|
34
|
+
* chainId: 8453, market: marketData.params, onBehalf: user,
|
|
35
|
+
* collateralSupplies: [{ collateralIndex: 0n, assets: 100n }],
|
|
36
|
+
* });
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export declare const midnightSupplyCollateralBatch: (params: MidnightSupplyCollateralBatchParams) => Readonly<Transaction<MidnightSupplyCollateralBatchAction>>;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.midnightSupplyCollateralBatch = 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 encodeMidnightCalldata_js_1 = require("../../helpers/encodeMidnightCalldata.js");
|
|
8
|
+
const index_js_1 = require("../../helpers/index.js");
|
|
9
|
+
const validateMidnightAccount_js_1 = require("../../helpers/validateMidnightAccount.js");
|
|
10
|
+
const validateMidnightBounds_js_1 = require("../../helpers/validateMidnightBounds.js");
|
|
11
|
+
const validateMidnightCollateralBatch_js_1 = require("../../helpers/validateMidnightCollateralBatch.js");
|
|
12
|
+
const validateMidnightMarket_js_1 = require("../../helpers/validateMidnightMarket.js");
|
|
13
|
+
const index_js_2 = require("../../types/index.js");
|
|
14
|
+
/**
|
|
15
|
+
* Encodes ordered collateral supplies in one Midnight multicall, even for one entry.
|
|
16
|
+
* Duplicates are rejected: pass one entry per index, unlike bundle flows which sum them.
|
|
17
|
+
*
|
|
18
|
+
* @param params.chainId - Chain containing Midnight.
|
|
19
|
+
* @param params.market - Market receiving collateral.
|
|
20
|
+
* @param params.collateralSupplies - Nonempty ordered supplies, with unique collateral indexes.
|
|
21
|
+
* @param params.onBehalf - Account receiving collateral; this account or an authorized operator must send the transaction.
|
|
22
|
+
* @param params.metadata - Optional analytics metadata appended to calldata.
|
|
23
|
+
* @returns A deep-frozen transaction targeting Midnight.
|
|
24
|
+
* @throws {EmptyMidnightCollateralSuppliesError} when the list is empty.
|
|
25
|
+
* @throws {DuplicateMidnightCollateralIndexError} when an index repeats.
|
|
26
|
+
* @throws {NonPositiveInputError} when assets are not positive.
|
|
27
|
+
* @throws {InputExceedsMaxError} when assets exceed uint128.
|
|
28
|
+
* @throws {MidnightZeroOnBehalfError} when onBehalf is zero.
|
|
29
|
+
* @throws {ChainIdMismatchError} when the market targets another chain.
|
|
30
|
+
* @throws {MidnightMarketAddressMismatchError} when the deployment differs.
|
|
31
|
+
* @throws {UnknownCollateralIndexError} when an index is not configured.
|
|
32
|
+
* @throws {MidnightCalldataEncodingError} when an address or ABI input is malformed.
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* const tx = midnightSupplyCollateralBatch({
|
|
36
|
+
* chainId: 8453, market: marketData.params, onBehalf: user,
|
|
37
|
+
* collateralSupplies: [{ collateralIndex: 0n, assets: 100n }],
|
|
38
|
+
* });
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
const midnightSupplyCollateralBatch = (params) => {
|
|
42
|
+
// Reject malformed addresses before comparing with zero.
|
|
43
|
+
(0, validateMidnightAccount_js_1.validateMidnightAddress)("onBehalf", params.onBehalf);
|
|
44
|
+
if ((0, viem_1.isAddressEqual)(params.onBehalf, viem_1.zeroAddress)) {
|
|
45
|
+
throw new index_js_2.MidnightZeroOnBehalfError("onBehalf");
|
|
46
|
+
}
|
|
47
|
+
// Reject markets from another deployment before encoding.
|
|
48
|
+
(0, validateMidnightMarket_js_1.validateMidnightMarket)({ market: params.market, chainId: params.chainId });
|
|
49
|
+
// Direct batches require one entry per index rather than summing duplicates.
|
|
50
|
+
(0, validateMidnightCollateralBatch_js_1.validateMidnightCollateralBatch)(params.collateralSupplies, new index_js_2.EmptyMidnightCollateralSuppliesError());
|
|
51
|
+
const calls = params.collateralSupplies.map(({ collateralIndex, assets }) => {
|
|
52
|
+
if (assets <= 0n)
|
|
53
|
+
throw new index_js_2.NonPositiveInputError("assets", assets);
|
|
54
|
+
(0, validateMidnightBounds_js_1.validateMidnightUint128Field)("assets", assets);
|
|
55
|
+
// Reject indexes not configured on the market.
|
|
56
|
+
midnight_sdk_1.MarketUtils.getCollateralByIndex(params.market, collateralIndex);
|
|
57
|
+
return (0, encodeMidnightCalldata_js_1.encodeMidnightCalldata)({
|
|
58
|
+
abi: midnight_sdk_1.midnightAbi,
|
|
59
|
+
functionName: "supplyCollateral",
|
|
60
|
+
args: [
|
|
61
|
+
midnight_sdk_1.MarketUtils.toStruct(params.market),
|
|
62
|
+
collateralIndex,
|
|
63
|
+
assets,
|
|
64
|
+
params.onBehalf,
|
|
65
|
+
],
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
let tx = {
|
|
69
|
+
to: (0, morpho_ts_1.getChainAddress)(params.chainId, "midnight"),
|
|
70
|
+
value: 0n,
|
|
71
|
+
data: (0, encodeMidnightCalldata_js_1.encodeMidnightCalldata)({
|
|
72
|
+
abi: midnight_sdk_1.midnightAbi,
|
|
73
|
+
functionName: "multicall",
|
|
74
|
+
args: [calls],
|
|
75
|
+
}),
|
|
76
|
+
};
|
|
77
|
+
if (params.metadata)
|
|
78
|
+
tx = (0, index_js_1.addTransactionMetadata)(tx, params.metadata);
|
|
79
|
+
return (0, morpho_ts_1.deepFreeze)({
|
|
80
|
+
...tx,
|
|
81
|
+
action: {
|
|
82
|
+
type: "midnightSupplyCollateralBatch",
|
|
83
|
+
args: {
|
|
84
|
+
market: (0, encodeMidnightCalldata_js_1.encodeMidnightMarketId)(params.market),
|
|
85
|
+
collateralSupplies: params.collateralSupplies.map(({ collateralIndex, assets }) => ({ collateralIndex, assets })),
|
|
86
|
+
onBehalf: params.onBehalf,
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
};
|
|
91
|
+
exports.midnightSupplyCollateralBatch = midnightSupplyCollateralBatch;
|