@morpho-org/morpho-sdk 9.8.0 → 10.0.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-v9-to-v10.md +114 -0
- package/README.md +109 -13
- package/lib/cjs/actions/blue/common.js +4 -2
- package/lib/cjs/actions/blue/withdrawBuyCallback.js +2 -1
- package/lib/cjs/actions/bundles/common.js +20 -14
- package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.d.ts +3 -4
- package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.js +3 -2
- package/lib/cjs/actions/midnight/cancelAndMake.d.ts +2 -1
- package/lib/cjs/actions/midnight/cancelAndMake.js +2 -1
- package/lib/cjs/actions/midnight/cancelOffers.d.ts +54 -0
- package/lib/cjs/actions/midnight/cancelOffers.js +108 -0
- package/lib/cjs/actions/midnight/index.d.ts +1 -0
- package/lib/cjs/actions/midnight/index.js +1 -0
- package/lib/cjs/actions/midnight/supplyCollateralBatch.js +4 -2
- package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.d.ts +1 -1
- package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +3 -2
- package/lib/cjs/actions/midnight/takeBorrow.d.ts +1 -1
- package/lib/cjs/actions/midnight/takeBorrow.js +3 -2
- package/lib/cjs/actions/midnight/takeLend.d.ts +1 -1
- package/lib/cjs/actions/midnight/takeLend.js +3 -2
- package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.d.ts +1 -1
- package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.js +3 -2
- package/lib/cjs/actions/midnight/takeWithdraw.d.ts +1 -1
- package/lib/cjs/actions/midnight/takeWithdraw.js +3 -2
- package/lib/cjs/actions/midnight/withdrawCollateralBatch.js +4 -2
- package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +3 -4
- package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +1 -1
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +4 -6
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +3 -3
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +4 -6
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +3 -3
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +5 -6
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +5 -13
- package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +3 -5
- package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +26 -23
- package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.js +2 -1
- package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +2 -1
- package/lib/cjs/actions/vaultV1/inKindRedeem.js +6 -3
- package/lib/cjs/actions/vaultV1/migrateToV2.js +1 -1
- package/lib/cjs/actions/vaultV2/forceWithdraw.js +4 -2
- package/lib/cjs/actions/vaultV2/inKindRedeem.js +4 -2
- package/lib/cjs/entities/blue/blue.d.ts +34 -28
- package/lib/cjs/entities/blue/blue.js +107 -77
- package/lib/cjs/entities/midnight/deadline.d.ts +2 -2
- package/lib/cjs/entities/midnight/deadline.js +4 -3
- package/lib/cjs/entities/midnight/midnight.d.ts +49 -51
- package/lib/cjs/entities/midnight/midnight.js +158 -138
- package/lib/cjs/entities/midnight/publication.d.ts +2 -0
- package/lib/cjs/entities/midnight/publication.js +193 -138
- package/lib/cjs/entities/midnight/types.d.ts +4 -4
- package/lib/cjs/entities/requirements/assertContractCode.js +4 -2
- package/lib/cjs/entities/requirements/getBundlesTokenRequirements.d.ts +3 -5
- package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +24 -28
- package/lib/cjs/entities/requirements/getErc2612Permit.js +10 -5
- package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +5 -4
- package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.js +8 -2
- package/lib/cjs/entities/requirements/getVaultV1InKindRedeemRequirements.d.ts +21 -0
- package/lib/cjs/entities/requirements/getVaultV1InKindRedeemRequirements.js +111 -0
- package/lib/cjs/entities/requirements/getVaultV2ForceWithdrawRequirements.d.ts +20 -0
- package/lib/cjs/entities/requirements/getVaultV2ForceWithdrawRequirements.js +76 -0
- package/lib/cjs/entities/requirements/getVaultV2InKindRedeemRequirements.d.ts +21 -0
- package/lib/cjs/entities/requirements/getVaultV2InKindRedeemRequirements.js +93 -0
- package/lib/cjs/entities/vaultV1/inKindRedeemQuote.d.ts +25 -0
- package/lib/cjs/entities/vaultV1/inKindRedeemQuote.js +85 -0
- package/lib/cjs/entities/vaultV1/migrateToV2Quote.d.ts +20 -0
- package/lib/cjs/entities/vaultV1/migrateToV2Quote.js +37 -0
- package/lib/cjs/entities/vaultV1/vaultV1.d.ts +12 -12
- package/lib/cjs/entities/vaultV1/vaultV1.js +90 -194
- package/lib/cjs/entities/vaultV2/forceWithdrawQuote.d.ts +25 -0
- package/lib/cjs/entities/vaultV2/forceWithdrawQuote.js +189 -0
- package/lib/cjs/entities/vaultV2/inKindRedeemQuote.d.ts +28 -0
- package/lib/cjs/entities/vaultV2/inKindRedeemQuote.js +110 -0
- package/lib/cjs/entities/vaultV2/vaultV2.d.ts +16 -16
- package/lib/cjs/entities/vaultV2/vaultV2.js +94 -385
- package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +14 -0
- package/lib/cjs/entities/vaultV2BlueReallocationData.js +1599 -883
- package/lib/cjs/errors.d.ts +1 -1
- package/lib/cjs/errors.js +2 -1
- package/lib/cjs/helpers/deadShares.js +83 -47
- package/lib/cjs/helpers/encodeMidnightCalldata.js +4 -2
- package/lib/cjs/helpers/metadata.d.ts +14 -2
- package/lib/cjs/helpers/metadata.js +49 -8
- package/lib/cjs/helpers/previewVaultV2ForceWithdraw.js +44 -27
- package/lib/cjs/helpers/previewVaultV2InKindRedeem.js +11 -2
- package/lib/cjs/helpers/utilization.js +10 -5
- package/lib/cjs/helpers/validate.js +88 -67
- package/lib/cjs/helpers/validateMidnightBounds.js +2 -1
- package/lib/cjs/helpers/validateMidnightCollateralBatch.js +2 -1
- package/lib/cjs/helpers/validateMidnightPublication.js +4 -2
- package/lib/cjs/helpers/validateMidnightTakeFeasibility.js +177 -104
- package/lib/cjs/helpers/validatePositiveInput.js +2 -1
- package/lib/cjs/helpers/validateTakeableOffers.js +2 -1
- package/lib/cjs/helpers/vaultV2ForceWithdrawPlan.js +40 -22
- package/lib/cjs/index.d.ts +2 -2
- package/lib/cjs/index.js +10 -1
- package/lib/cjs/transactionPlan/approval.js +12 -1
- package/lib/cjs/transactionPlan/deposit.d.ts +22 -0
- package/lib/cjs/transactionPlan/deposit.js +139 -0
- package/lib/cjs/transactionPlan/error.d.ts +10 -8
- package/lib/cjs/transactionPlan/error.js +10 -8
- package/lib/cjs/transactionPlan/getTokenPermit.js +2 -1
- package/lib/cjs/transactionPlan/index.d.ts +18 -0
- package/lib/cjs/transactionPlan/index.js +23 -5
- package/lib/cjs/transactionPlan/intent.d.ts +3 -3
- package/lib/cjs/transactionPlan/intent.js +14 -7
- package/lib/cjs/transactionPlan/permit.d.ts +1 -1
- package/lib/cjs/transactionPlan/permit.js +86 -20
- package/lib/cjs/transactionPlan/plan.d.ts +16 -6
- package/lib/cjs/transactionPlan/plan.js +77 -24
- package/lib/cjs/transactionPlan/redeem.d.ts +18 -0
- package/lib/cjs/transactionPlan/redeem.js +63 -0
- package/lib/cjs/transactionPlan/requirements.d.ts +4 -3
- package/lib/cjs/transactionPlan/requirements.js +12 -6
- package/lib/cjs/transactionPlan/serde.d.ts +2 -2
- package/lib/cjs/transactionPlan/serde.js +45 -19
- package/lib/cjs/transactionPlan/shareSpending.d.ts +18 -0
- package/lib/cjs/transactionPlan/shareSpending.js +50 -0
- package/lib/cjs/transactionPlan/types.d.ts +117 -11
- package/lib/cjs/transactionPlan/validateActions.js +11 -1
- package/lib/cjs/transactionPlan/validation.js +6 -3
- package/lib/cjs/transactionPlan/vaultv1/deposit.d.ts +93 -0
- package/lib/cjs/transactionPlan/vaultv1/deposit.js +94 -0
- package/lib/cjs/transactionPlan/vaultv1/inKindRedeem.d.ts +96 -0
- package/lib/cjs/transactionPlan/vaultv1/inKindRedeem.js +203 -0
- package/lib/cjs/transactionPlan/vaultv1/migrateToV2.d.ts +94 -0
- package/lib/cjs/transactionPlan/vaultv1/migrateToV2.js +217 -0
- package/lib/cjs/transactionPlan/vaultv1/redeem.d.ts +69 -0
- package/lib/cjs/transactionPlan/vaultv1/redeem.js +78 -0
- package/lib/cjs/transactionPlan/vaultv1/withdraw.d.ts +77 -0
- package/lib/cjs/transactionPlan/vaultv1/withdraw.js +88 -0
- package/lib/cjs/transactionPlan/vaultv2/deposit.d.ts +18 -33
- package/lib/cjs/transactionPlan/vaultv2/deposit.js +19 -143
- package/lib/cjs/transactionPlan/vaultv2/forceRedeem.d.ts +74 -0
- package/lib/cjs/transactionPlan/vaultv2/forceRedeem.js +155 -0
- package/lib/cjs/transactionPlan/vaultv2/forceWithdraw.d.ts +98 -0
- package/lib/cjs/transactionPlan/vaultv2/forceWithdraw.js +206 -0
- package/lib/cjs/transactionPlan/vaultv2/inKindRedeem.d.ts +113 -0
- package/lib/cjs/transactionPlan/vaultv2/inKindRedeem.js +206 -0
- package/lib/cjs/transactionPlan/vaultv2/redeem.d.ts +84 -0
- package/lib/cjs/transactionPlan/vaultv2/redeem.js +86 -0
- package/lib/cjs/transactionPlan/vaultv2/withdraw.d.ts +18 -29
- package/lib/cjs/transactionPlan/vaultv2/withdraw.js +21 -126
- package/lib/cjs/transactionPlan/version.js +2 -1
- package/lib/cjs/transactionPlan/withdraw.d.ts +18 -0
- package/lib/cjs/transactionPlan/withdraw.js +94 -0
- package/lib/cjs/types/action.d.ts +26 -2
- package/lib/cjs/types/action.js +17 -9
- package/lib/cjs/types/error.d.ts +8 -1
- package/lib/cjs/types/error.js +21 -6
- package/lib/cjs/types/metadata.d.ts +12 -2
- package/lib/cjs/version.d.ts +1 -1
- package/lib/cjs/version.js +1 -1
- package/lib/esm/actions/blue/common.js +4 -2
- package/lib/esm/actions/blue/withdrawBuyCallback.js +2 -1
- package/lib/esm/actions/bundles/common.js +20 -14
- package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.d.ts +3 -4
- package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.js +3 -2
- package/lib/esm/actions/midnight/cancelAndMake.d.ts +2 -1
- package/lib/esm/actions/midnight/cancelAndMake.js +2 -1
- package/lib/esm/actions/midnight/cancelOffers.d.ts +54 -0
- package/lib/esm/actions/midnight/cancelOffers.js +104 -0
- package/lib/esm/actions/midnight/index.d.ts +1 -0
- package/lib/esm/actions/midnight/index.js +1 -0
- package/lib/esm/actions/midnight/supplyCollateralBatch.js +4 -2
- package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.d.ts +1 -1
- package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +3 -2
- package/lib/esm/actions/midnight/takeBorrow.d.ts +1 -1
- package/lib/esm/actions/midnight/takeBorrow.js +3 -2
- package/lib/esm/actions/midnight/takeLend.d.ts +1 -1
- package/lib/esm/actions/midnight/takeLend.js +3 -2
- package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.d.ts +1 -1
- package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.js +3 -2
- package/lib/esm/actions/midnight/takeWithdraw.d.ts +1 -1
- package/lib/esm/actions/midnight/takeWithdraw.js +3 -2
- package/lib/esm/actions/midnight/withdrawCollateralBatch.js +4 -2
- package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +3 -4
- package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +1 -1
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +4 -6
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +3 -3
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +4 -6
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +3 -3
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +5 -6
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +7 -15
- package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +3 -5
- package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +26 -23
- package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.js +2 -1
- package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +2 -1
- package/lib/esm/actions/vaultV1/inKindRedeem.js +6 -3
- package/lib/esm/actions/vaultV1/migrateToV2.js +1 -1
- package/lib/esm/actions/vaultV2/forceWithdraw.js +4 -2
- package/lib/esm/actions/vaultV2/inKindRedeem.js +4 -2
- package/lib/esm/entities/blue/blue.d.ts +34 -28
- package/lib/esm/entities/blue/blue.js +107 -77
- package/lib/esm/entities/midnight/deadline.d.ts +2 -2
- package/lib/esm/entities/midnight/deadline.js +4 -3
- package/lib/esm/entities/midnight/midnight.d.ts +49 -51
- package/lib/esm/entities/midnight/midnight.js +159 -139
- package/lib/esm/entities/midnight/publication.d.ts +2 -0
- package/lib/esm/entities/midnight/publication.js +193 -138
- package/lib/esm/entities/midnight/types.d.ts +4 -4
- package/lib/esm/entities/requirements/assertContractCode.js +4 -2
- package/lib/esm/entities/requirements/getBundlesTokenRequirements.d.ts +3 -5
- package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +25 -29
- package/lib/esm/entities/requirements/getErc2612Permit.js +10 -5
- package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +5 -4
- package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.js +8 -2
- package/lib/esm/entities/requirements/getVaultV1InKindRedeemRequirements.d.ts +21 -0
- package/lib/esm/entities/requirements/getVaultV1InKindRedeemRequirements.js +108 -0
- package/lib/esm/entities/requirements/getVaultV2ForceWithdrawRequirements.d.ts +20 -0
- package/lib/esm/entities/requirements/getVaultV2ForceWithdrawRequirements.js +73 -0
- package/lib/esm/entities/requirements/getVaultV2InKindRedeemRequirements.d.ts +21 -0
- package/lib/esm/entities/requirements/getVaultV2InKindRedeemRequirements.js +90 -0
- package/lib/esm/entities/vaultV1/inKindRedeemQuote.d.ts +25 -0
- package/lib/esm/entities/vaultV1/inKindRedeemQuote.js +82 -0
- package/lib/esm/entities/vaultV1/migrateToV2Quote.d.ts +20 -0
- package/lib/esm/entities/vaultV1/migrateToV2Quote.js +34 -0
- package/lib/esm/entities/vaultV1/vaultV1.d.ts +12 -12
- package/lib/esm/entities/vaultV1/vaultV1.js +96 -200
- package/lib/esm/entities/vaultV2/forceWithdrawQuote.d.ts +25 -0
- package/lib/esm/entities/vaultV2/forceWithdrawQuote.js +186 -0
- package/lib/esm/entities/vaultV2/inKindRedeemQuote.d.ts +28 -0
- package/lib/esm/entities/vaultV2/inKindRedeemQuote.js +107 -0
- package/lib/esm/entities/vaultV2/vaultV2.d.ts +16 -16
- package/lib/esm/entities/vaultV2/vaultV2.js +102 -393
- package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +14 -0
- package/lib/esm/entities/vaultV2BlueReallocationData.js +1599 -883
- package/lib/esm/errors.d.ts +1 -1
- package/lib/esm/errors.js +1 -1
- package/lib/esm/helpers/deadShares.js +83 -47
- package/lib/esm/helpers/encodeMidnightCalldata.js +4 -2
- package/lib/esm/helpers/metadata.d.ts +14 -2
- package/lib/esm/helpers/metadata.js +48 -8
- package/lib/esm/helpers/previewVaultV2ForceWithdraw.js +44 -27
- package/lib/esm/helpers/previewVaultV2InKindRedeem.js +11 -2
- package/lib/esm/helpers/utilization.js +10 -5
- package/lib/esm/helpers/validate.js +88 -67
- package/lib/esm/helpers/validateMidnightBounds.js +2 -1
- package/lib/esm/helpers/validateMidnightCollateralBatch.js +2 -1
- package/lib/esm/helpers/validateMidnightPublication.js +4 -2
- package/lib/esm/helpers/validateMidnightTakeFeasibility.js +176 -103
- package/lib/esm/helpers/validatePositiveInput.js +2 -1
- package/lib/esm/helpers/validateTakeableOffers.js +2 -1
- package/lib/esm/helpers/vaultV2ForceWithdrawPlan.js +40 -22
- package/lib/esm/index.d.ts +2 -2
- package/lib/esm/index.js +1 -1
- package/lib/esm/transactionPlan/approval.js +13 -2
- package/lib/esm/transactionPlan/deposit.d.ts +22 -0
- package/lib/esm/transactionPlan/deposit.js +135 -0
- package/lib/esm/transactionPlan/error.d.ts +10 -8
- package/lib/esm/transactionPlan/error.js +10 -8
- package/lib/esm/transactionPlan/getTokenPermit.js +2 -1
- package/lib/esm/transactionPlan/index.d.ts +18 -0
- package/lib/esm/transactionPlan/index.js +9 -0
- package/lib/esm/transactionPlan/intent.d.ts +3 -3
- package/lib/esm/transactionPlan/intent.js +14 -7
- package/lib/esm/transactionPlan/permit.d.ts +1 -1
- package/lib/esm/transactionPlan/permit.js +86 -20
- package/lib/esm/transactionPlan/plan.d.ts +16 -6
- package/lib/esm/transactionPlan/plan.js +73 -20
- package/lib/esm/transactionPlan/redeem.d.ts +18 -0
- package/lib/esm/transactionPlan/redeem.js +59 -0
- package/lib/esm/transactionPlan/requirements.d.ts +4 -3
- package/lib/esm/transactionPlan/requirements.js +12 -6
- package/lib/esm/transactionPlan/serde.d.ts +2 -2
- package/lib/esm/transactionPlan/serde.js +45 -19
- package/lib/esm/transactionPlan/shareSpending.d.ts +18 -0
- package/lib/esm/transactionPlan/shareSpending.js +47 -0
- package/lib/esm/transactionPlan/types.d.ts +117 -11
- package/lib/esm/transactionPlan/validateActions.js +11 -1
- package/lib/esm/transactionPlan/validation.js +6 -3
- package/lib/esm/transactionPlan/vaultv1/deposit.d.ts +93 -0
- package/lib/esm/transactionPlan/vaultv1/deposit.js +90 -0
- package/lib/esm/transactionPlan/vaultv1/inKindRedeem.d.ts +96 -0
- package/lib/esm/transactionPlan/vaultv1/inKindRedeem.js +198 -0
- package/lib/esm/transactionPlan/vaultv1/migrateToV2.d.ts +94 -0
- package/lib/esm/transactionPlan/vaultv1/migrateToV2.js +212 -0
- package/lib/esm/transactionPlan/vaultv1/redeem.d.ts +69 -0
- package/lib/esm/transactionPlan/vaultv1/redeem.js +74 -0
- package/lib/esm/transactionPlan/vaultv1/withdraw.d.ts +77 -0
- package/lib/esm/transactionPlan/vaultv1/withdraw.js +84 -0
- package/lib/esm/transactionPlan/vaultv2/deposit.d.ts +18 -33
- package/lib/esm/transactionPlan/vaultv2/deposit.js +21 -144
- package/lib/esm/transactionPlan/vaultv2/forceRedeem.d.ts +74 -0
- package/lib/esm/transactionPlan/vaultv2/forceRedeem.js +150 -0
- package/lib/esm/transactionPlan/vaultv2/forceWithdraw.d.ts +98 -0
- package/lib/esm/transactionPlan/vaultv2/forceWithdraw.js +201 -0
- package/lib/esm/transactionPlan/vaultv2/inKindRedeem.d.ts +113 -0
- package/lib/esm/transactionPlan/vaultv2/inKindRedeem.js +201 -0
- package/lib/esm/transactionPlan/vaultv2/redeem.d.ts +84 -0
- package/lib/esm/transactionPlan/vaultv2/redeem.js +82 -0
- package/lib/esm/transactionPlan/vaultv2/withdraw.d.ts +18 -29
- package/lib/esm/transactionPlan/vaultv2/withdraw.js +23 -127
- package/lib/esm/transactionPlan/version.js +2 -1
- package/lib/esm/transactionPlan/withdraw.d.ts +18 -0
- package/lib/esm/transactionPlan/withdraw.js +90 -0
- package/lib/esm/types/action.d.ts +26 -2
- package/lib/esm/types/action.js +17 -9
- package/lib/esm/types/error.d.ts +8 -1
- package/lib/esm/types/error.js +21 -6
- package/lib/esm/types/metadata.d.ts +12 -2
- package/lib/esm/version.d.ts +1 -1
- package/lib/esm/version.js +1 -1
- package/package.json +15 -14
|
@@ -9,10 +9,12 @@ import { validateMidnightAddress } from "./validateMidnightAccount.js";
|
|
|
9
9
|
const compareMarketIds = (idA, idB) => {
|
|
10
10
|
const normalizedIdA = idA.toLowerCase();
|
|
11
11
|
const normalizedIdB = idB.toLowerCase();
|
|
12
|
-
if (normalizedIdA > normalizedIdB)
|
|
12
|
+
if (normalizedIdA > normalizedIdB) {
|
|
13
13
|
return 1;
|
|
14
|
-
|
|
14
|
+
}
|
|
15
|
+
if (normalizedIdA < normalizedIdB) {
|
|
15
16
|
return -1;
|
|
17
|
+
}
|
|
16
18
|
return 0;
|
|
17
19
|
};
|
|
18
20
|
/**
|
|
@@ -28,8 +30,9 @@ const compareMarketIds = (idA, idB) => {
|
|
|
28
30
|
* @internal
|
|
29
31
|
*/
|
|
30
32
|
export const validateUint256Field = (field, value) => {
|
|
31
|
-
if (value < 0n)
|
|
33
|
+
if (value < 0n) {
|
|
32
34
|
throw new NegativeInputError(field, value);
|
|
35
|
+
}
|
|
33
36
|
if (value > maxUint256) {
|
|
34
37
|
throw new InputExceedsMaxError({ field, value, max: maxUint256 });
|
|
35
38
|
}
|
|
@@ -61,8 +64,9 @@ export const validateMarketParamsId = (marketParams) => {
|
|
|
61
64
|
* @internal
|
|
62
65
|
*/
|
|
63
66
|
export const validateDeadline = (deadline) => {
|
|
64
|
-
if (deadline <= 0n)
|
|
67
|
+
if (deadline <= 0n) {
|
|
65
68
|
throw new NonPositiveInputError("deadline", deadline);
|
|
69
|
+
}
|
|
66
70
|
validateUint256Field("deadline", deadline);
|
|
67
71
|
};
|
|
68
72
|
/**
|
|
@@ -251,8 +255,9 @@ export const getMidnightMaxSafeDebt = (params) => {
|
|
|
251
255
|
let maxSafeDebt = 0n;
|
|
252
256
|
for (let index = 0; index < params.collateral.length; index += 1) {
|
|
253
257
|
const amount = params.collateral[index] ?? 0n;
|
|
254
|
-
if (amount === 0n)
|
|
258
|
+
if (amount === 0n) {
|
|
255
259
|
continue;
|
|
260
|
+
}
|
|
256
261
|
const collateral = MidnightMarketUtils.getCollateralByIndex(params.market.params, BigInt(index));
|
|
257
262
|
const price = params.collateralPrices[index];
|
|
258
263
|
if (price === undefined) {
|
|
@@ -414,8 +419,9 @@ export const validateMidnightNativeAmount = (params) => {
|
|
|
414
419
|
throw new ChainWNativeMissingError(chainId);
|
|
415
420
|
}
|
|
416
421
|
// Reject a malformed address before viem compares it with wNative.
|
|
417
|
-
for (const pull of pulls)
|
|
422
|
+
for (const pull of pulls) {
|
|
418
423
|
validateMidnightAddress("token", pull.token);
|
|
424
|
+
}
|
|
419
425
|
const wNativePulled = pulls.reduce((total, pull) => isAddressEqual(pull.token, wNative) ? total + pull.assets : total, 0n);
|
|
420
426
|
const firstPull = pulls[0];
|
|
421
427
|
if (firstPull && !pulls.some((pull) => isAddressEqual(pull.token, wNative))) {
|
|
@@ -578,67 +584,11 @@ export const validateRepayShares = (params) => {
|
|
|
578
584
|
export const validateVaultV2BlueReallocations = (reallocations, targetMarketId) => {
|
|
579
585
|
const penaltyByVault = new Map();
|
|
580
586
|
for (const reallocation of reallocations) {
|
|
581
|
-
//
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
"withdrawals" in reallocation ||
|
|
587
|
-
"fee" in reallocation) {
|
|
588
|
-
throw new InvalidVaultV2BlueReallocationShapeError();
|
|
589
|
-
}
|
|
590
|
-
if (typeof reallocation.vault !== "string" ||
|
|
591
|
-
!isAddress(reallocation.vault)) {
|
|
592
|
-
throw new InvalidReallocationAddressError("vault");
|
|
593
|
-
}
|
|
594
|
-
if (reallocation.to == null ||
|
|
595
|
-
typeof reallocation.to.adapter !== "string" ||
|
|
596
|
-
!isAddress(reallocation.to.adapter)) {
|
|
597
|
-
throw new InvalidReallocationAddressError("to.adapter");
|
|
598
|
-
}
|
|
599
|
-
const source = reallocation.from;
|
|
600
|
-
if (source == null) {
|
|
601
|
-
throw new InvalidReallocationSourceTypeError(undefined);
|
|
602
|
-
}
|
|
603
|
-
const sourceType = source.type;
|
|
604
|
-
if (sourceType !== "market" && sourceType !== "idle") {
|
|
605
|
-
throw new InvalidReallocationSourceTypeError(sourceType);
|
|
606
|
-
}
|
|
607
|
-
let sourceMarketId;
|
|
608
|
-
if (source.type === "market") {
|
|
609
|
-
if (typeof source.adapter !== "string" || !isAddress(source.adapter)) {
|
|
610
|
-
throw new InvalidReallocationAddressError("from.adapter");
|
|
611
|
-
}
|
|
612
|
-
if (source.marketParams == null ||
|
|
613
|
-
!isAddress(source.marketParams.loanToken) ||
|
|
614
|
-
!isAddress(source.marketParams.collateralToken) ||
|
|
615
|
-
!isAddress(source.marketParams.oracle) ||
|
|
616
|
-
!isAddress(source.marketParams.irm) ||
|
|
617
|
-
typeof source.marketParams.lltv !== "bigint") {
|
|
618
|
-
throw new InvalidReallocationSourceTypeError("market", "marketParams");
|
|
619
|
-
}
|
|
620
|
-
sourceMarketId = MarketUtils.getMarketId(source.marketParams);
|
|
621
|
-
}
|
|
622
|
-
if (reallocation.penalty < 0n) {
|
|
623
|
-
throw new NegativeInputError("reallocation.penalty", reallocation.penalty);
|
|
624
|
-
}
|
|
625
|
-
if (reallocation.penalty > MAX_REALLOCATION_PENALTY) {
|
|
626
|
-
throw new InputExceedsMaxError({
|
|
627
|
-
field: "reallocation.penalty",
|
|
628
|
-
value: reallocation.penalty,
|
|
629
|
-
max: MAX_REALLOCATION_PENALTY,
|
|
630
|
-
});
|
|
631
|
-
}
|
|
632
|
-
if (reallocation.assets <= 0n) {
|
|
633
|
-
throw new NonPositiveInputError("reallocation.assets", reallocation.assets);
|
|
634
|
-
}
|
|
635
|
-
if (reallocation.assets > maxUint128) {
|
|
636
|
-
throw new InputExceedsMaxError({
|
|
637
|
-
field: "reallocation.assets",
|
|
638
|
-
value: reallocation.assets,
|
|
639
|
-
max: maxUint128,
|
|
640
|
-
});
|
|
641
|
-
}
|
|
587
|
+
// Validate shape before source and amounts so malformed entries keep their original error.
|
|
588
|
+
assertReallocationShape(reallocation);
|
|
589
|
+
const sourceMarketId = resolveReallocationSourceMarketId(reallocation);
|
|
590
|
+
// Reject out-of-range amounts before comparing penalties across vault legs.
|
|
591
|
+
assertReallocationAmounts(reallocation);
|
|
642
592
|
const penaltyKey = reallocation.vault.toLowerCase();
|
|
643
593
|
const expectedPenalty = penaltyByVault.get(penaltyKey);
|
|
644
594
|
if (expectedPenalty !== undefined &&
|
|
@@ -749,3 +699,74 @@ export const validateWithdrawShares = (params) => {
|
|
|
749
699
|
});
|
|
750
700
|
}
|
|
751
701
|
};
|
|
702
|
+
/** Rejects malformed reallocation entries before reading source or amount fields. @internal */
|
|
703
|
+
function assertReallocationShape(reallocation) {
|
|
704
|
+
// Reject non-object entries and Vault V1-shaped entries (which carry
|
|
705
|
+
// `withdrawals`/`fee`) with a typed, instructive error instead of a raw
|
|
706
|
+
// TypeError or a misleading `to.adapter` address error.
|
|
707
|
+
if (typeof reallocation !== "object" ||
|
|
708
|
+
reallocation === null ||
|
|
709
|
+
"withdrawals" in reallocation ||
|
|
710
|
+
"fee" in reallocation) {
|
|
711
|
+
throw new InvalidVaultV2BlueReallocationShapeError();
|
|
712
|
+
}
|
|
713
|
+
if (typeof reallocation.vault !== "string" ||
|
|
714
|
+
!isAddress(reallocation.vault)) {
|
|
715
|
+
throw new InvalidReallocationAddressError("vault");
|
|
716
|
+
}
|
|
717
|
+
if (reallocation.to == null ||
|
|
718
|
+
typeof reallocation.to.adapter !== "string" ||
|
|
719
|
+
!isAddress(reallocation.to.adapter)) {
|
|
720
|
+
throw new InvalidReallocationAddressError("to.adapter");
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
/** Validates the reallocation source and resolves its market identifier. @internal */
|
|
724
|
+
function resolveReallocationSourceMarketId(reallocation) {
|
|
725
|
+
const source = reallocation.from;
|
|
726
|
+
if (source == null) {
|
|
727
|
+
throw new InvalidReallocationSourceTypeError(undefined);
|
|
728
|
+
}
|
|
729
|
+
const sourceType = source.type;
|
|
730
|
+
if (sourceType !== "market" && sourceType !== "idle") {
|
|
731
|
+
throw new InvalidReallocationSourceTypeError(sourceType);
|
|
732
|
+
}
|
|
733
|
+
let sourceMarketId;
|
|
734
|
+
if (source.type === "market") {
|
|
735
|
+
if (typeof source.adapter !== "string" || !isAddress(source.adapter)) {
|
|
736
|
+
throw new InvalidReallocationAddressError("from.adapter");
|
|
737
|
+
}
|
|
738
|
+
if (source.marketParams == null ||
|
|
739
|
+
!isAddress(source.marketParams.loanToken) ||
|
|
740
|
+
!isAddress(source.marketParams.collateralToken) ||
|
|
741
|
+
!isAddress(source.marketParams.oracle) ||
|
|
742
|
+
!isAddress(source.marketParams.irm) ||
|
|
743
|
+
typeof source.marketParams.lltv !== "bigint") {
|
|
744
|
+
throw new InvalidReallocationSourceTypeError("market", "marketParams");
|
|
745
|
+
}
|
|
746
|
+
sourceMarketId = MarketUtils.getMarketId(source.marketParams);
|
|
747
|
+
}
|
|
748
|
+
return sourceMarketId;
|
|
749
|
+
}
|
|
750
|
+
/** Checks penalty and asset amounts against the allocator's ABI bounds. @internal */
|
|
751
|
+
function assertReallocationAmounts(reallocation) {
|
|
752
|
+
if (reallocation.penalty < 0n) {
|
|
753
|
+
throw new NegativeInputError("reallocation.penalty", reallocation.penalty);
|
|
754
|
+
}
|
|
755
|
+
if (reallocation.penalty > MAX_REALLOCATION_PENALTY) {
|
|
756
|
+
throw new InputExceedsMaxError({
|
|
757
|
+
field: "reallocation.penalty",
|
|
758
|
+
value: reallocation.penalty,
|
|
759
|
+
max: MAX_REALLOCATION_PENALTY,
|
|
760
|
+
});
|
|
761
|
+
}
|
|
762
|
+
if (reallocation.assets <= 0n) {
|
|
763
|
+
throw new NonPositiveInputError("reallocation.assets", reallocation.assets);
|
|
764
|
+
}
|
|
765
|
+
if (reallocation.assets > maxUint128) {
|
|
766
|
+
throw new InputExceedsMaxError({
|
|
767
|
+
field: "reallocation.assets",
|
|
768
|
+
value: reallocation.assets,
|
|
769
|
+
max: maxUint128,
|
|
770
|
+
});
|
|
771
|
+
}
|
|
772
|
+
}
|
|
@@ -12,8 +12,9 @@ import { MIDNIGHT_MAX_REFERRAL_FEE_PCT } from "./constant.js";
|
|
|
12
12
|
* @internal
|
|
13
13
|
*/
|
|
14
14
|
export const validateMidnightUint128Field = (field, value) => {
|
|
15
|
-
if (value < 0n)
|
|
15
|
+
if (value < 0n) {
|
|
16
16
|
throw new NegativeInputError(field, value);
|
|
17
|
+
}
|
|
17
18
|
if (value > maxUint128) {
|
|
18
19
|
throw new InputExceedsMaxError({ field, value, max: maxUint128 });
|
|
19
20
|
}
|
|
@@ -13,8 +13,9 @@ import { DuplicateMidnightCollateralIndexError } from "../types/error.js";
|
|
|
13
13
|
* ```
|
|
14
14
|
*/
|
|
15
15
|
export function validateMidnightCollateralBatch(transfers, emptyError) {
|
|
16
|
-
if (transfers.length === 0)
|
|
16
|
+
if (transfers.length === 0) {
|
|
17
17
|
throw emptyError;
|
|
18
|
+
}
|
|
18
19
|
const indexes = new Set();
|
|
19
20
|
for (const { collateralIndex } of transfers) {
|
|
20
21
|
if (indexes.has(collateralIndex)) {
|
|
@@ -80,8 +80,9 @@ export const validateMidnightPublication = (params) => {
|
|
|
80
80
|
});
|
|
81
81
|
}
|
|
82
82
|
const publication = params.publication;
|
|
83
|
-
if (publication == null)
|
|
83
|
+
if (publication == null) {
|
|
84
84
|
return;
|
|
85
|
+
}
|
|
85
86
|
validateHexInput(publication.root, "publication.root", { exactBytes: 32 });
|
|
86
87
|
for (const [index, group] of publication.groups.entries()) {
|
|
87
88
|
validateHexInput(group, `publication.groups[${index}]`, {
|
|
@@ -99,8 +100,9 @@ export const validateMidnightPublication = (params) => {
|
|
|
99
100
|
exactBytes: 32,
|
|
100
101
|
});
|
|
101
102
|
}
|
|
102
|
-
if (publication.payload === "0x")
|
|
103
|
+
if (publication.payload === "0x") {
|
|
103
104
|
return;
|
|
105
|
+
}
|
|
104
106
|
validateHexInput(publication.payload, "publication.payload", {
|
|
105
107
|
maxBytes: Payload.MAX_PAYLOAD_BYTES,
|
|
106
108
|
});
|
|
@@ -74,42 +74,18 @@ export const validateMidnightSellTargetBounds = (params) => {
|
|
|
74
74
|
*/
|
|
75
75
|
export const validateMidnightTakeFeasibility = (params) => {
|
|
76
76
|
// The raw builder rejects an empty list with `EmptyMidnightTakeableOffersError`.
|
|
77
|
-
if (params.takeableOffers.length === 0)
|
|
77
|
+
if (params.takeableOffers.length === 0) {
|
|
78
78
|
return params.takeableOffers;
|
|
79
|
-
|
|
79
|
+
}
|
|
80
|
+
const { marketData, now, deadline } = params;
|
|
80
81
|
// The entity checked the deadline on an earlier clock read; recheck so the fee window is never empty.
|
|
81
|
-
if (deadline <= now)
|
|
82
|
+
if (deadline <= now) {
|
|
82
83
|
throw new ExpiredDeadlineError(deadline, now);
|
|
84
|
+
}
|
|
83
85
|
const maturity = marketData.params.maturity;
|
|
84
86
|
const marketFee = BigInt(marketData.continuousFee);
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
throw new MidnightContinuousFeeAboveMaxError({
|
|
88
|
-
maxContinuousFee: params.maxContinuousFee,
|
|
89
|
-
continuousFee: marketFee,
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
const { debt, target: buyTarget } = params;
|
|
93
|
-
if (debt !== undefined) {
|
|
94
|
-
// Reduce-only buys fill at most the debt, and an assets target pays at most one asset per unit.
|
|
95
|
-
const repay = buyTarget.type === "units"
|
|
96
|
-
? buyTarget.units === maxUint256
|
|
97
|
-
? undefined
|
|
98
|
-
: { field: "target.units", value: buyTarget.units }
|
|
99
|
-
: buyTarget.minUnits > debt
|
|
100
|
-
? {
|
|
101
|
-
field: "target.minUnits",
|
|
102
|
-
value: buyTarget.minUnits,
|
|
103
|
-
}
|
|
104
|
-
: {
|
|
105
|
-
field: "target.assets",
|
|
106
|
-
value: netOfReferralFee(buyTarget.assets, referralFeePct),
|
|
107
|
-
};
|
|
108
|
-
if (repay && repay.value > debt) {
|
|
109
|
-
throw new MidnightRepayExceedsDebtError({ ...repay, debt });
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
}
|
|
87
|
+
// Reject buy fee and debt violations before evaluating settlement fees or offers.
|
|
88
|
+
assertMidnightBuyBounds(params, marketFee);
|
|
113
89
|
const fees = [
|
|
114
90
|
maturity - deadline,
|
|
115
91
|
maturity - now,
|
|
@@ -122,62 +98,162 @@ export const validateMidnightTakeFeasibility = (params) => {
|
|
|
122
98
|
}));
|
|
123
99
|
const minFee = MathLib.min(...fees);
|
|
124
100
|
const maxFee = MathLib.max(...fees);
|
|
125
|
-
const dead = [];
|
|
126
|
-
const removed = new Set();
|
|
127
101
|
// Post-maturity sells cannot open debt, so offers only take the credit left after the withdrawal.
|
|
128
102
|
const sellableCredit = params.side === "sell" && params.increasesDebt && now > maturity
|
|
129
103
|
? MathLib.zeroFloorSub(params.credit ?? 0n, params.fallbackUnits)
|
|
130
104
|
: undefined;
|
|
105
|
+
const { dead, removed, liveUnits, minBuyerPrice } = collectOfferCapacity(params, {
|
|
106
|
+
minFee,
|
|
107
|
+
maxFee,
|
|
108
|
+
marketFee,
|
|
109
|
+
sellableCredit,
|
|
110
|
+
});
|
|
111
|
+
if (dead.length === params.takeableOffers.length &&
|
|
112
|
+
(params.fallbackUnits === 0n || removed.size === dead.length)) {
|
|
113
|
+
throw new MidnightTakeableOffersNotFillableError(dead);
|
|
114
|
+
}
|
|
115
|
+
const requiredUnits = resolveRequiredUnits(params);
|
|
116
|
+
const fillableUnits = (sellableCredit === undefined
|
|
117
|
+
? liveUnits
|
|
118
|
+
: MathLib.min(liveUnits, sellableCredit)) + params.fallbackUnits;
|
|
119
|
+
if (fillableUnits < requiredUnits) {
|
|
120
|
+
throw new MidnightTakeTargetUnreachableError({
|
|
121
|
+
field: "takeableOffers",
|
|
122
|
+
value: fillableUnits,
|
|
123
|
+
limit: requiredUnits,
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
// Capacity errors precede the buyer's maximum-pull bound.
|
|
127
|
+
assertBuyerAssetCapacity(params, { requiredUnits, minBuyerPrice });
|
|
128
|
+
return removed.size === 0
|
|
129
|
+
? params.takeableOffers
|
|
130
|
+
: params.takeableOffers.filter((_, index) => !removed.has(index));
|
|
131
|
+
};
|
|
132
|
+
/** Resolves a reduce-only buy's repay bound without changing target precedence. @internal */
|
|
133
|
+
function resolveRepayTarget(target, { debt, referralFeePct, }) {
|
|
134
|
+
if (target.type === "units") {
|
|
135
|
+
if (target.units === maxUint256) {
|
|
136
|
+
return undefined;
|
|
137
|
+
}
|
|
138
|
+
return { field: "target.units", value: target.units };
|
|
139
|
+
}
|
|
140
|
+
if (target.minUnits > debt) {
|
|
141
|
+
return { field: "target.minUnits", value: target.minUnits };
|
|
142
|
+
}
|
|
143
|
+
return {
|
|
144
|
+
field: "target.assets",
|
|
145
|
+
value: netOfReferralFee(target.assets, referralFeePct),
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
/** Rejects excessive continuous fees and reduce-only buys exceeding debt. @internal */
|
|
149
|
+
function assertMidnightBuyBounds(params, marketFee) {
|
|
150
|
+
if (params.side !== "buy") {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
if (params.maxContinuousFee < marketFee) {
|
|
154
|
+
throw new MidnightContinuousFeeAboveMaxError({
|
|
155
|
+
maxContinuousFee: params.maxContinuousFee,
|
|
156
|
+
continuousFee: marketFee,
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
const { debt, target: buyTarget } = params;
|
|
160
|
+
if (debt !== undefined) {
|
|
161
|
+
// Reduce-only buys fill at most the debt, and an assets target pays at most one asset per unit.
|
|
162
|
+
const repay = resolveRepayTarget(buyTarget, {
|
|
163
|
+
debt,
|
|
164
|
+
referralFeePct: params.referralFeePct,
|
|
165
|
+
});
|
|
166
|
+
if (repay && repay.value > debt) {
|
|
167
|
+
throw new MidnightRepayExceedsDebtError({ ...repay, debt });
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
/** Resolves price failures that abort bundle conversion outside the offer catch. @internal */
|
|
172
|
+
function getOfferPriceReason(params, { tickInRange, offerPrice, minFee, maxFee, }) {
|
|
173
|
+
// The bundle converts every buy assets target price outside its per-offer catch.
|
|
174
|
+
if (params.side === "buy" && params.target.type === "assets") {
|
|
175
|
+
if (!tickInRange) {
|
|
176
|
+
return "tickOutOfRange";
|
|
177
|
+
}
|
|
178
|
+
if (offerPrice + maxFee === 0n) {
|
|
179
|
+
return "zeroBuyerPrice";
|
|
180
|
+
}
|
|
181
|
+
if (offerPrice + minFee > MathLib.WAD) {
|
|
182
|
+
return "buyerPriceAboveOne";
|
|
183
|
+
}
|
|
184
|
+
return undefined;
|
|
185
|
+
}
|
|
186
|
+
// Every sell converts a buy offer's price outside the catch too.
|
|
187
|
+
if (params.side !== "sell") {
|
|
188
|
+
return undefined;
|
|
189
|
+
}
|
|
190
|
+
if (!tickInRange) {
|
|
191
|
+
return "tickOutOfRange";
|
|
192
|
+
}
|
|
193
|
+
if (offerPrice === 0n) {
|
|
194
|
+
return "zeroBuyerPrice";
|
|
195
|
+
}
|
|
196
|
+
// An assets target also divides by the seller price, which is zero at the fee.
|
|
197
|
+
if (offerPrice < minFee ||
|
|
198
|
+
(params.target.type === "assets" && offerPrice === minFee)) {
|
|
199
|
+
return "priceBelowSettlementFee";
|
|
200
|
+
}
|
|
201
|
+
return undefined;
|
|
202
|
+
}
|
|
203
|
+
/** Finds the first non-price condition preventing an offer from filling. @internal */
|
|
204
|
+
function getOfferDeadReason({ units, offer }, { params, tickInRange, marketFee, sellableCredit, }) {
|
|
205
|
+
if (units === 0n) {
|
|
206
|
+
return "zeroUnits";
|
|
207
|
+
}
|
|
208
|
+
if (!tickInRange) {
|
|
209
|
+
return "tickOutOfRange";
|
|
210
|
+
}
|
|
211
|
+
if (offer.expiry < params.now) {
|
|
212
|
+
return "expired";
|
|
213
|
+
}
|
|
214
|
+
if (offer.start > params.deadline) {
|
|
215
|
+
return "notStarted";
|
|
216
|
+
}
|
|
217
|
+
if (isAddressEqual(offer.maker, params.accountAddress)) {
|
|
218
|
+
return "selfTake";
|
|
219
|
+
}
|
|
220
|
+
if (offer.continuousFeeCap < marketFee) {
|
|
221
|
+
return "continuousFeeCapBelowMarketFee";
|
|
222
|
+
}
|
|
223
|
+
if (params.side === "sell" &&
|
|
224
|
+
!isAddressEqual(offer.receiverIfMakerIsSeller, zeroAddress)) {
|
|
225
|
+
return "receiverIfMakerIsSellerNotZero";
|
|
226
|
+
}
|
|
227
|
+
if (sellableCredit === 0n) {
|
|
228
|
+
return "marketMatured";
|
|
229
|
+
}
|
|
230
|
+
return undefined;
|
|
231
|
+
}
|
|
232
|
+
/** Accumulates live offer capacity and preserves removal and error order. @internal */
|
|
233
|
+
function collectOfferCapacity(params, { minFee, maxFee, marketFee, sellableCredit, }) {
|
|
234
|
+
const dead = [];
|
|
235
|
+
const removed = new Set();
|
|
131
236
|
let liveUnits = 0n;
|
|
132
237
|
let minBuyerPrice = params.fallbackUnits > 0n ? MathLib.WAD : maxUint256;
|
|
133
238
|
for (const [index, { units, offer }] of params.takeableOffers.entries()) {
|
|
134
239
|
const tickInRange = offer.tick >= 0n && offer.tick <= MAX_TICK;
|
|
135
240
|
const offerPrice = tickInRange ? TickLib.tickToPrice(offer.tick) : 0n;
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
else if (offerPrice + minFee > MathLib.WAD)
|
|
145
|
-
reason = "buyerPriceAboveOne";
|
|
146
|
-
if (reason)
|
|
147
|
-
removed.add(index);
|
|
148
|
-
}
|
|
149
|
-
// Every sell converts a buy offer's price outside the catch too, which underflows below the fee.
|
|
150
|
-
if (params.side === "sell") {
|
|
151
|
-
if (!tickInRange)
|
|
152
|
-
reason = "tickOutOfRange";
|
|
153
|
-
else if (offerPrice === 0n)
|
|
154
|
-
reason = "zeroBuyerPrice";
|
|
155
|
-
// An assets target also divides by the seller price, which is zero at the fee.
|
|
156
|
-
else if (offerPrice < minFee ||
|
|
157
|
-
(target.type === "assets" && offerPrice === minFee))
|
|
158
|
-
reason = "priceBelowSettlementFee";
|
|
159
|
-
if (reason)
|
|
160
|
-
removed.add(index);
|
|
161
|
-
}
|
|
162
|
-
if (!reason) {
|
|
163
|
-
if (units === 0n)
|
|
164
|
-
reason = "zeroUnits";
|
|
165
|
-
else if (!tickInRange)
|
|
166
|
-
reason = "tickOutOfRange";
|
|
167
|
-
else if (offer.expiry < now)
|
|
168
|
-
reason = "expired";
|
|
169
|
-
else if (offer.start > deadline)
|
|
170
|
-
reason = "notStarted";
|
|
171
|
-
else if (isAddressEqual(offer.maker, params.accountAddress))
|
|
172
|
-
reason = "selfTake";
|
|
173
|
-
else if (offer.continuousFeeCap < marketFee)
|
|
174
|
-
reason = "continuousFeeCapBelowMarketFee";
|
|
175
|
-
else if (params.side === "sell" &&
|
|
176
|
-
!isAddressEqual(offer.receiverIfMakerIsSeller, zeroAddress))
|
|
177
|
-
reason = "receiverIfMakerIsSellerNotZero";
|
|
178
|
-
else if (sellableCredit === 0n)
|
|
179
|
-
reason = "marketMatured";
|
|
241
|
+
const priceReason = getOfferPriceReason(params, {
|
|
242
|
+
tickInRange,
|
|
243
|
+
offerPrice,
|
|
244
|
+
minFee,
|
|
245
|
+
maxFee,
|
|
246
|
+
});
|
|
247
|
+
if (priceReason) {
|
|
248
|
+
removed.add(index);
|
|
180
249
|
}
|
|
250
|
+
const reason = priceReason ??
|
|
251
|
+
getOfferDeadReason({ units, offer }, {
|
|
252
|
+
params,
|
|
253
|
+
tickInRange,
|
|
254
|
+
marketFee,
|
|
255
|
+
sellableCredit,
|
|
256
|
+
});
|
|
181
257
|
if (reason) {
|
|
182
258
|
dead.push({ index, reason });
|
|
183
259
|
continue;
|
|
@@ -185,33 +261,33 @@ export const validateMidnightTakeFeasibility = (params) => {
|
|
|
185
261
|
liveUnits += units;
|
|
186
262
|
minBuyerPrice = MathLib.min(minBuyerPrice, offerPrice + minFee);
|
|
187
263
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
264
|
+
return { dead, removed, liveUnits, minBuyerPrice };
|
|
265
|
+
}
|
|
266
|
+
/** Converts the taker's target into its required gross unit capacity. @internal */
|
|
267
|
+
function resolveRequiredUnits(params) {
|
|
268
|
+
const { target, referralFeePct } = params;
|
|
269
|
+
if (params.side === "sell") {
|
|
270
|
+
if (target.type === "units") {
|
|
271
|
+
return target.units;
|
|
272
|
+
}
|
|
273
|
+
return grossFromNetAssets({ netAssets: target.assets, referralFeePct });
|
|
191
274
|
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
?
|
|
195
|
-
:
|
|
196
|
-
: params.target.type === "units"
|
|
197
|
-
? params.target.units === maxUint256
|
|
198
|
-
? (params.debt ?? 0n)
|
|
199
|
-
: params.target.units
|
|
200
|
-
: MathLib.max(netOfReferralFee(params.target.assets, referralFeePct), params.target.minUnits);
|
|
201
|
-
const fillableUnits = (sellableCredit === undefined
|
|
202
|
-
? liveUnits
|
|
203
|
-
: MathLib.min(liveUnits, sellableCredit)) + params.fallbackUnits;
|
|
204
|
-
if (fillableUnits < requiredUnits) {
|
|
205
|
-
throw new MidnightTakeTargetUnreachableError({
|
|
206
|
-
field: "takeableOffers",
|
|
207
|
-
value: fillableUnits,
|
|
208
|
-
limit: requiredUnits,
|
|
209
|
-
});
|
|
275
|
+
if (params.target.type === "units") {
|
|
276
|
+
return params.target.units === maxUint256
|
|
277
|
+
? (params.debt ?? 0n)
|
|
278
|
+
: params.target.units;
|
|
210
279
|
}
|
|
280
|
+
return MathLib.max(netOfReferralFee(params.target.assets, referralFeePct), params.target.minUnits);
|
|
281
|
+
}
|
|
282
|
+
/** Rejects a units buy whose cheapest possible asset pull exceeds its cap. @internal */
|
|
283
|
+
function assertBuyerAssetCapacity(params, { requiredUnits, minBuyerPrice, }) {
|
|
211
284
|
if (params.side === "buy" && params.target.type === "units") {
|
|
212
285
|
// Every unit costs at least the cheapest live price, and the fee is grossed up on top.
|
|
213
286
|
const minCost = MathLib.mulDivDown(requiredUnits, minBuyerPrice, MathLib.WAD);
|
|
214
|
-
const minPull = grossFromNetAssets({
|
|
287
|
+
const minPull = grossFromNetAssets({
|
|
288
|
+
netAssets: minCost,
|
|
289
|
+
referralFeePct: params.referralFeePct,
|
|
290
|
+
});
|
|
215
291
|
if (params.target.maxBuyerAssets < minPull) {
|
|
216
292
|
throw new MidnightTakeTargetUnreachableError({
|
|
217
293
|
field: "target.maxBuyerAssets",
|
|
@@ -220,7 +296,4 @@ export const validateMidnightTakeFeasibility = (params) => {
|
|
|
220
296
|
});
|
|
221
297
|
}
|
|
222
298
|
}
|
|
223
|
-
|
|
224
|
-
? params.takeableOffers
|
|
225
|
-
: params.takeableOffers.filter((_, index) => !removed.has(index));
|
|
226
|
-
};
|
|
299
|
+
}
|
|
@@ -31,8 +31,9 @@ export const validateTakeableOffers = (params) => {
|
|
|
31
31
|
actualMarket: actualMarketId,
|
|
32
32
|
});
|
|
33
33
|
}
|
|
34
|
-
if (take.offer.buy)
|
|
34
|
+
if (take.offer.buy) {
|
|
35
35
|
continue;
|
|
36
|
+
}
|
|
36
37
|
// Reject a malformed address before viem compares it.
|
|
37
38
|
validateMidnightAddress(`takeableOffers[${index}].offer.receiverIfMakerIsSeller`, take.offer.receiverIfMakerIsSeller);
|
|
38
39
|
if (isAddressEqual(take.offer.receiverIfMakerIsSeller, zeroAddress)) {
|