@morpho-org/morpho-sdk 6.0.0-next.1 → 6.0.0-next.2
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-v5-to-v6.md +127 -16
- package/README.md +17 -11
- package/lib/cjs/abis.d.ts +17 -2
- package/lib/cjs/abis.js +14 -1
- package/lib/cjs/actions/blue/common.js +5 -26
- package/lib/cjs/actions/blue/supply.js +2 -7
- package/lib/cjs/actions/bundles/common.d.ts +32 -2
- package/lib/cjs/actions/bundles/common.js +68 -30
- package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +3 -0
- package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +6 -10
- package/lib/cjs/actions/requirements/blue/index.d.ts +0 -1
- package/lib/cjs/actions/requirements/blue/index.js +0 -1
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +15 -8
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +20 -20
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +16 -7
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +33 -27
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2Approve.d.ts +4 -2
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2Approve.js +15 -10
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +4 -2
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +14 -9
- package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +9 -6
- package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +22 -17
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirements.js +1 -0
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +4 -1
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.js +5 -2
- package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +6 -0
- package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.js +2 -0
- package/lib/cjs/actions/vaultV1/inKindRedeem.d.ts +1 -0
- package/lib/cjs/actions/vaultV1/inKindRedeem.js +3 -2
- package/lib/cjs/actions/vaultV2/forceWithdraw.d.ts +58 -39
- package/lib/cjs/actions/vaultV2/forceWithdraw.js +87 -60
- package/lib/cjs/actions/vaultV2/inKindRedeem.d.ts +1 -0
- package/lib/cjs/actions/vaultV2/inKindRedeem.js +3 -2
- package/lib/cjs/augment/VaultMarketPublicAllocatorConfig.d.ts +4 -1
- package/lib/cjs/blue/abis.d.ts +5 -1
- package/lib/cjs/blue/abis.js +2 -0
- package/lib/cjs/blue/entities.d.ts +8 -2
- package/lib/cjs/blue/entities.js +1 -0
- package/lib/cjs/blue/errors.d.ts +1 -1
- package/lib/cjs/blue/errors.js +2 -1
- package/lib/cjs/blue/fetch.d.ts +3 -1
- package/lib/cjs/blue/fetch.js +1 -0
- package/lib/cjs/entities/blue/blue.d.ts +15 -6
- package/lib/cjs/entities/blue/blue.js +3 -0
- package/lib/cjs/entities/index.d.ts +8 -2
- package/lib/cjs/entities/index.js +1 -0
- package/lib/cjs/entities/midnight/midnight.js +20 -24
- package/lib/cjs/entities/requirements/getBundlesTokenRequirements.d.ts +7 -6
- package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +19 -14
- package/lib/cjs/{actions/requirements/blue → entities/requirements}/getUnusedPermit2Nonce.js +3 -3
- package/lib/cjs/entities/requirements/index.d.ts +1 -0
- package/lib/cjs/entities/requirements/index.js +1 -0
- package/lib/cjs/entities/vaultV1/vaultV1.d.ts +10 -5
- package/lib/cjs/entities/vaultV1/vaultV1.js +13 -1
- package/lib/cjs/entities/vaultV1ReallocationData.d.ts +6 -0
- package/lib/cjs/entities/vaultV1ReallocationData.js +20 -5
- package/lib/cjs/entities/vaultV2/vaultV2.d.ts +140 -34
- package/lib/cjs/entities/vaultV2/vaultV2.js +229 -16
- package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +8 -1
- package/lib/cjs/entities/vaultV2BlueReallocationData.js +46 -13
- package/lib/cjs/errors.d.ts +1 -1
- package/lib/cjs/errors.js +3 -2
- package/lib/cjs/fetch.d.ts +3 -1
- package/lib/cjs/fetch.js +1 -0
- package/lib/cjs/helpers/computeVaultV1Reallocations.d.ts +2 -0
- package/lib/cjs/helpers/computeVaultV1Reallocations.js +2 -0
- package/lib/cjs/helpers/index.d.ts +3 -1
- package/lib/cjs/helpers/index.js +10 -1
- package/lib/cjs/helpers/metadata.d.ts +3 -5
- package/lib/cjs/helpers/metadata.js +4 -6
- package/lib/cjs/helpers/previewVaultV2ForceWithdraw.d.ts +107 -0
- package/lib/cjs/helpers/previewVaultV2ForceWithdraw.js +164 -0
- package/lib/cjs/helpers/previewVaultV2InKindRedeem.d.ts +1 -0
- package/lib/cjs/helpers/previewVaultV2InKindRedeem.js +2 -0
- package/lib/cjs/helpers/slippage.d.ts +46 -0
- package/lib/cjs/helpers/slippage.js +65 -0
- package/lib/cjs/helpers/validate.d.ts +65 -8
- package/lib/cjs/helpers/validate.js +83 -9
- package/lib/cjs/helpers/vaultV2ForceWithdrawPlan.d.ts +274 -0
- package/lib/cjs/helpers/vaultV2ForceWithdrawPlan.js +369 -0
- package/lib/cjs/types/action.d.ts +95 -13
- package/lib/cjs/types/error.d.ts +290 -12
- package/lib/cjs/types/error.js +312 -24
- package/lib/cjs/types/sharedLiquidity.d.ts +2 -2
- package/lib/cjs/utils.d.ts +3 -1
- package/lib/cjs/utils.js +10 -1
- package/lib/esm/abis.d.ts +17 -2
- package/lib/esm/abis.js +17 -2
- package/lib/esm/actions/blue/common.js +7 -28
- package/lib/esm/actions/blue/supply.js +4 -9
- package/lib/esm/actions/bundles/common.d.ts +32 -2
- package/lib/esm/actions/bundles/common.js +68 -31
- package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +3 -0
- package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +8 -12
- package/lib/esm/actions/requirements/blue/index.d.ts +0 -1
- package/lib/esm/actions/requirements/blue/index.js +0 -1
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +15 -8
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +21 -21
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +16 -7
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +34 -28
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2Approve.d.ts +4 -2
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2Approve.js +15 -10
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +4 -2
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +14 -9
- package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +9 -6
- package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +22 -17
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirements.js +1 -0
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +4 -1
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.js +5 -2
- package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +6 -0
- package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.js +2 -0
- package/lib/esm/actions/vaultV1/inKindRedeem.d.ts +1 -0
- package/lib/esm/actions/vaultV1/inKindRedeem.js +3 -2
- package/lib/esm/actions/vaultV2/forceWithdraw.d.ts +58 -39
- package/lib/esm/actions/vaultV2/forceWithdraw.js +89 -62
- package/lib/esm/actions/vaultV2/inKindRedeem.d.ts +1 -0
- package/lib/esm/actions/vaultV2/inKindRedeem.js +3 -2
- package/lib/esm/augment/VaultMarketPublicAllocatorConfig.d.ts +4 -1
- package/lib/esm/augment/VaultMarketPublicAllocatorConfig.js +3 -1
- package/lib/esm/blue/abis.d.ts +5 -1
- package/lib/esm/blue/abis.js +5 -1
- package/lib/esm/blue/entities.d.ts +8 -2
- package/lib/esm/blue/entities.js +3 -1
- package/lib/esm/blue/errors.d.ts +1 -1
- package/lib/esm/blue/errors.js +1 -1
- package/lib/esm/blue/fetch.d.ts +3 -1
- package/lib/esm/blue/fetch.js +3 -1
- package/lib/esm/entities/blue/blue.d.ts +15 -6
- package/lib/esm/entities/blue/blue.js +3 -0
- package/lib/esm/entities/index.d.ts +8 -2
- package/lib/esm/entities/index.js +3 -1
- package/lib/esm/entities/midnight/midnight.js +20 -24
- package/lib/esm/entities/requirements/getBundlesTokenRequirements.d.ts +7 -6
- package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +20 -15
- package/lib/esm/{actions/requirements/blue → entities/requirements}/getUnusedPermit2Nonce.js +3 -3
- package/lib/esm/entities/requirements/index.d.ts +1 -0
- package/lib/esm/entities/requirements/index.js +1 -0
- package/lib/esm/entities/vaultV1/vaultV1.d.ts +10 -5
- package/lib/esm/entities/vaultV1/vaultV1.js +14 -2
- package/lib/esm/entities/vaultV1ReallocationData.d.ts +6 -0
- package/lib/esm/entities/vaultV1ReallocationData.js +20 -5
- package/lib/esm/entities/vaultV2/vaultV2.d.ts +140 -34
- package/lib/esm/entities/vaultV2/vaultV2.js +234 -21
- package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +8 -1
- package/lib/esm/entities/vaultV2BlueReallocationData.js +46 -13
- package/lib/esm/errors.d.ts +1 -1
- package/lib/esm/errors.js +1 -1
- package/lib/esm/fetch.d.ts +3 -1
- package/lib/esm/fetch.js +3 -1
- package/lib/esm/helpers/computeVaultV1Reallocations.d.ts +2 -0
- package/lib/esm/helpers/computeVaultV1Reallocations.js +2 -0
- package/lib/esm/helpers/index.d.ts +3 -1
- package/lib/esm/helpers/index.js +3 -1
- package/lib/esm/helpers/metadata.d.ts +3 -5
- package/lib/esm/helpers/metadata.js +4 -6
- package/lib/esm/helpers/previewVaultV2ForceWithdraw.d.ts +107 -0
- package/lib/esm/helpers/previewVaultV2ForceWithdraw.js +161 -0
- package/lib/esm/helpers/previewVaultV2InKindRedeem.d.ts +1 -0
- package/lib/esm/helpers/previewVaultV2InKindRedeem.js +2 -0
- package/lib/esm/helpers/slippage.d.ts +46 -0
- package/lib/esm/helpers/slippage.js +65 -1
- package/lib/esm/helpers/validate.d.ts +65 -8
- package/lib/esm/helpers/validate.js +82 -10
- package/lib/esm/helpers/vaultV2ForceWithdrawPlan.d.ts +274 -0
- package/lib/esm/helpers/vaultV2ForceWithdrawPlan.js +362 -0
- package/lib/esm/types/action.d.ts +95 -13
- package/lib/esm/types/error.d.ts +290 -12
- package/lib/esm/types/error.js +301 -19
- package/lib/esm/types/sharedLiquidity.d.ts +2 -2
- package/lib/esm/utils.d.ts +3 -1
- package/lib/esm/utils.js +3 -1
- package/package.json +7 -7
- /package/lib/cjs/{actions/requirements/blue → entities/requirements}/getUnusedPermit2Nonce.d.ts +0 -0
- /package/lib/esm/{actions/requirements/blue → entities/requirements}/getUnusedPermit2Nonce.d.ts +0 -0
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
import { AccrualVaultV2MorphoMarketV1AdapterV2, MarketParams, MathLib, } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { isAddressEqual, maxUint256, zeroAddress } from "viem";
|
|
3
|
+
import { NonPositiveInputError } from "../types/index.js";
|
|
4
|
+
/**
|
|
5
|
+
* Resolves whether a Vault V2 snapshot can be force-withdrawn through VaultExitBundlesV1.
|
|
6
|
+
*
|
|
7
|
+
* Mirrors the contract's four preconditions: `adaptersLength() == 1`, `isAdapter(adapter)`,
|
|
8
|
+
* `adapter.morpho() == BLUE` (implied by the adapter type, whose factory pins Blue), and a
|
|
9
|
+
* `liquidityAdapter` the contract can cast to `IMorphoMarketV1AdapterV2` and whose `liquidityData`
|
|
10
|
+
* decodes as `MarketParams`. Because the exit already requires a single adapter, the only
|
|
11
|
+
* resolvable liquidity configurations are unset or that same adapter.
|
|
12
|
+
*
|
|
13
|
+
* Pure: reads only the supplied snapshot and never throws.
|
|
14
|
+
*
|
|
15
|
+
* @param vaultData - Pre-fetched Vault V2 accrual snapshot.
|
|
16
|
+
* @param adapterOverride - Optional adapter the caller expects; defaults to the vault's sole adapter.
|
|
17
|
+
* @returns The `"eligible"` variant with the resolved adapter and liquidity market, or the tag
|
|
18
|
+
* describing which precondition failed.
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* import { resolveVaultV2ForceWithdrawEligibility } from "@morpho-org/morpho-sdk";
|
|
22
|
+
*
|
|
23
|
+
* const eligibility = resolveVaultV2ForceWithdrawEligibility(vaultData);
|
|
24
|
+
* if (eligibility.type === "eligible") {
|
|
25
|
+
* console.log(eligibility.adapter.address, eligibility.liquidityMarketId);
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export function resolveVaultV2ForceWithdrawEligibility(vaultData, adapterOverride) {
|
|
30
|
+
const [soleAdapter] = vaultData.accrualAdapters;
|
|
31
|
+
if (vaultData.accrualAdapters.length !== 1 || soleAdapter == null) {
|
|
32
|
+
return { type: "adapterCount", adapters: vaultData.accrualAdapters.length };
|
|
33
|
+
}
|
|
34
|
+
const adapter = adapterOverride ?? soleAdapter.address;
|
|
35
|
+
if (!isAddressEqual(adapter, soleAdapter.address)) {
|
|
36
|
+
return { type: "adapterMismatch", adapter };
|
|
37
|
+
}
|
|
38
|
+
if (!(soleAdapter instanceof AccrualVaultV2MorphoMarketV1AdapterV2)) {
|
|
39
|
+
return { type: "unsupportedAdapter", adapter };
|
|
40
|
+
}
|
|
41
|
+
const { liquidityAdapter } = vaultData;
|
|
42
|
+
if (isAddressEqual(liquidityAdapter, zeroAddress)) {
|
|
43
|
+
return {
|
|
44
|
+
type: "eligible",
|
|
45
|
+
adapter: soleAdapter,
|
|
46
|
+
liquidityMarketId: undefined,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
if (!isAddressEqual(liquidityAdapter, soleAdapter.address)) {
|
|
50
|
+
return { type: "unsupportedLiquidityAdapter", liquidityAdapter, adapter };
|
|
51
|
+
}
|
|
52
|
+
let liquidityMarketId;
|
|
53
|
+
try {
|
|
54
|
+
liquidityMarketId = MarketParams.fromHex(vaultData.liquidityData).id;
|
|
55
|
+
}
|
|
56
|
+
catch (cause) {
|
|
57
|
+
// Undecodable `liquidityData` reverts the contract's `abi.decode`. Reported separately from a
|
|
58
|
+
// foreign adapter: the adapter here is the right one, only its stored market params are unusable.
|
|
59
|
+
return {
|
|
60
|
+
type: "undecodableLiquidityData",
|
|
61
|
+
liquidityAdapter,
|
|
62
|
+
liquidityData: vaultData.liquidityData,
|
|
63
|
+
cause,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
return { type: "eligible", adapter: soleAdapter, liquidityMarketId };
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Computes the Vault V2 shares that accruing a vault snapshot mints to its owner as management
|
|
70
|
+
* and/or performance fees.
|
|
71
|
+
*
|
|
72
|
+
* Fee shares are minted to the matching recipient during the first withdrawal after accrual.
|
|
73
|
+
* When the owner is both recipients, both fee-share amounts are included. Timestamps before the
|
|
74
|
+
* snapshot's last update are clamped because Vault V2 cannot accrue backwards.
|
|
75
|
+
*
|
|
76
|
+
* @param params - Vault fee-share accrual inputs.
|
|
77
|
+
* @param params.vaultData - Pre-fetched Vault V2 accrual snapshot.
|
|
78
|
+
* @param params.owner - Account whose fee-recipient share mints are measured.
|
|
79
|
+
* @param params.timestamp - Target accrual timestamp, clamped to `vaultData.lastUpdate`.
|
|
80
|
+
* @returns Shares minted to `owner` by the management and/or performance fees, or `0n` when the
|
|
81
|
+
* owner is not a fee recipient.
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts
|
|
84
|
+
* import { computeVaultV2ForceWithdrawFeeSharesMinted } from "@morpho-org/morpho-sdk";
|
|
85
|
+
*
|
|
86
|
+
* const feeShares = computeVaultV2ForceWithdrawFeeSharesMinted({
|
|
87
|
+
* vaultData,
|
|
88
|
+
* owner,
|
|
89
|
+
* timestamp,
|
|
90
|
+
* });
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
export function computeVaultV2ForceWithdrawFeeSharesMinted(params) {
|
|
94
|
+
const { vault: accruedVault, managementFeeShares, performanceFeeShares, } = params.vaultData.accrueInterest(MathLib.max(params.timestamp, params.vaultData.lastUpdate));
|
|
95
|
+
return ((isAddressEqual(accruedVault.managementFeeRecipient, params.owner)
|
|
96
|
+
? managementFeeShares
|
|
97
|
+
: 0n) +
|
|
98
|
+
(isAddressEqual(accruedVault.performanceFeeRecipient, params.owner)
|
|
99
|
+
? performanceFeeShares
|
|
100
|
+
: 0n));
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Computes every Vault V2 force-withdraw amount from a vault snapshot, without any RPC.
|
|
104
|
+
*
|
|
105
|
+
* Reproduces `vaultExitBundlesV1ForceWithdrawVaultV2` arithmetic:
|
|
106
|
+
*
|
|
107
|
+
* ```solidity
|
|
108
|
+
* withdrawableAssets = idle + min(liquidityAdapterAssets, liquidityMarketLiquidity);
|
|
109
|
+
* assetsToWithdraw = min(exitAssets, withdrawableAssets);
|
|
110
|
+
* assetsToDeallocate = mulDivDown(exitAssets - assetsToWithdraw, WAD, WAD + penalty);
|
|
111
|
+
* ```
|
|
112
|
+
*
|
|
113
|
+
* `coveredAssets` sums `min(adapterPosition, marketLiquidity)` over the adapter's markets, minus
|
|
114
|
+
* whatever the penalty-free leg already drained from the liquidity market. The sum is deliberately
|
|
115
|
+
* order-independent: the contract's loop visits every market taking `min(available, remaining)`, so
|
|
116
|
+
* total capacity does not depend on the adapter's storage order — which matters because a drained
|
|
117
|
+
* market is removed from that list mid-loop.
|
|
118
|
+
*
|
|
119
|
+
* That order-independence assumes `VaultExitBundlesV1` copies `adapter.marketIds` into memory
|
|
120
|
+
* *before* the loop. If it instead re-read adapter storage each iteration,
|
|
121
|
+
* `MorphoMarketV1AdapterV2.deallocate`'s swap-and-pop removal of a drained market would make the
|
|
122
|
+
* loop skip markets, and this sum would overstate real capacity — letting an exit through that
|
|
123
|
+
* panics on-chain. The claim is load-bearing for the coverage guard and is verified only by the
|
|
124
|
+
* fork suite, not by any in-repo contract source.
|
|
125
|
+
*
|
|
126
|
+
* @param params - Plan inputs.
|
|
127
|
+
* @param params.vaultData - Pre-fetched Vault V2 accrual snapshot.
|
|
128
|
+
* @param params.adapter - The vault's sole MorphoMarketV1AdapterV2, from
|
|
129
|
+
* {@link resolveVaultV2ForceWithdrawEligibility}.
|
|
130
|
+
* @param params.liquidityMarketId - Market the vault routes penalty-free liquidity through, or
|
|
131
|
+
* `undefined` when the vault has no liquidity adapter.
|
|
132
|
+
* @param params.exitAssets - Penalty-inclusive amount to exit.
|
|
133
|
+
* @param params.timestamp - Timestamp used to accrue every Morpho Blue market before converting
|
|
134
|
+
* adapter shares.
|
|
135
|
+
* @returns The resolved plan. `coveredAssets < assetsToDeallocate` means the exit would overrun the
|
|
136
|
+
* contract's unbounded loop; the caller decides how to report that.
|
|
137
|
+
* @throws {NonPositiveInputError} when `exitAssets` is not positive.
|
|
138
|
+
* @example
|
|
139
|
+
* ```ts
|
|
140
|
+
* import {
|
|
141
|
+
* computeVaultV2ForceWithdrawPlan,
|
|
142
|
+
* resolveVaultV2ForceWithdrawEligibility,
|
|
143
|
+
* } from "@morpho-org/morpho-sdk";
|
|
144
|
+
*
|
|
145
|
+
* const eligibility = resolveVaultV2ForceWithdrawEligibility(vaultData);
|
|
146
|
+
* if (eligibility.type === "eligible") {
|
|
147
|
+
* const plan = computeVaultV2ForceWithdrawPlan({
|
|
148
|
+
* vaultData,
|
|
149
|
+
* adapter: eligibility.adapter,
|
|
150
|
+
* liquidityMarketId: eligibility.liquidityMarketId,
|
|
151
|
+
* exitAssets: 1_000_000n,
|
|
152
|
+
* timestamp: 1_800_000_000n,
|
|
153
|
+
* });
|
|
154
|
+
* // plan.withdrawnAssets is what the user receives before the referral fee
|
|
155
|
+
* }
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
158
|
+
export function computeVaultV2ForceWithdrawPlan(params) {
|
|
159
|
+
const { vaultData, adapter, liquidityMarketId, exitAssets, timestamp } = params;
|
|
160
|
+
// Guard the exported planner: a non-positive `exitAssets` would flow a negative through
|
|
161
|
+
// `min(exitAssets, withdrawable)` and return a plan with negative amounts. The entity and preview
|
|
162
|
+
// reject it upstream; this protects direct importers of the advertised helper.
|
|
163
|
+
if (exitAssets <= 0n)
|
|
164
|
+
throw new NonPositiveInputError("exitAssets", exitAssets);
|
|
165
|
+
const penalty = vaultData.forceDeallocatePenalties[adapter.address] ?? 0n;
|
|
166
|
+
const idleAssets = vaultData.assetBalance;
|
|
167
|
+
// `min(adapterPosition, marketLiquidity)` per market, on interest-accrued market state — the
|
|
168
|
+
// contract reads `expectedMarketBalances`, so the un-accrued `maxWithdraw` helpers on
|
|
169
|
+
// `AccrualVaultV2` would understate capacity.
|
|
170
|
+
const availableByMarket = new Map(adapter.markets.map((market) => {
|
|
171
|
+
const accrued = market.accrueInterest(MathLib.max(timestamp, market.lastUpdate));
|
|
172
|
+
return [
|
|
173
|
+
market.id,
|
|
174
|
+
MathLib.min(accrued.toSupplyAssets(adapter.supplyShares[market.id] ?? 0n), accrued.liquidity),
|
|
175
|
+
];
|
|
176
|
+
}));
|
|
177
|
+
const liquidityCapacity = liquidityMarketId == null
|
|
178
|
+
? 0n
|
|
179
|
+
: (availableByMarket.get(liquidityMarketId) ?? 0n);
|
|
180
|
+
const withdrawableAssets = idleAssets + liquidityCapacity;
|
|
181
|
+
const assetsToWithdraw = MathLib.min(exitAssets, withdrawableAssets);
|
|
182
|
+
const assetsToDeallocate = MathLib.mulDivDown(exitAssets - assetsToWithdraw, MathLib.WAD, MathLib.WAD + penalty);
|
|
183
|
+
// The penalty-free leg pulls its shortfall through the liquidity adapter, draining that market
|
|
184
|
+
// before the force-deallocation loop reaches it.
|
|
185
|
+
const drainedFromLiquidityMarket = MathLib.zeroFloorSub(assetsToWithdraw, idleAssets);
|
|
186
|
+
if (liquidityMarketId != null && drainedFromLiquidityMarket > 0n) {
|
|
187
|
+
availableByMarket.set(liquidityMarketId, MathLib.zeroFloorSub(liquidityCapacity, drainedFromLiquidityMarket));
|
|
188
|
+
}
|
|
189
|
+
let coveredAssets = 0n;
|
|
190
|
+
const nonEmpty = [];
|
|
191
|
+
for (const available of availableByMarket.values()) {
|
|
192
|
+
if (available <= 0n)
|
|
193
|
+
continue;
|
|
194
|
+
coveredAssets += available;
|
|
195
|
+
nonEmpty.push(available);
|
|
196
|
+
}
|
|
197
|
+
// `maxExitAssets` must not depend on the requested amount. At the ceiling the penalty-free leg
|
|
198
|
+
// always drains the liquidity market completely, so that market contributes nothing to the
|
|
199
|
+
// force-deallocation loop — whereas a small request leaves part of it, which would overstate the
|
|
200
|
+
// ceiling for callers using it to cap an input.
|
|
201
|
+
let saturatedCoveredAssets = 0n;
|
|
202
|
+
for (const [marketId, available] of availableByMarket) {
|
|
203
|
+
if (marketId === liquidityMarketId)
|
|
204
|
+
continue;
|
|
205
|
+
if (available > 0n)
|
|
206
|
+
saturatedCoveredAssets += available;
|
|
207
|
+
}
|
|
208
|
+
// Worst-case leg count is the ascending walk: the most markets the loop can touch is the number
|
|
209
|
+
// of smallest positions that together reach `assetsToDeallocate`. Deliberately an upper bound, not
|
|
210
|
+
// a replay of the contract's prefetched `marketIds` order (where an earlier market may cover the
|
|
211
|
+
// whole request in one leg): this count feeds the share-burn *upper* bound behind the slippage
|
|
212
|
+
// floor, so it must hold for any execution order. Replaying a snapshot order that then diverges at
|
|
213
|
+
// inclusion (a market added or drained between `getData()` and execution) would under-count legs,
|
|
214
|
+
// over-state the floor, and false-revert a valid distressed exit — a worse failure than the
|
|
215
|
+
// bounded looseness (≤ `penaltyLegs - 1` asset-units, absorbed by `slippageTolerance`) the
|
|
216
|
+
// ascending walk leaves. Tightening it to the exact walk is a separate change needing fork
|
|
217
|
+
// coverage of the snapshot-vs-execution divergence.
|
|
218
|
+
let penaltyLegs = 0;
|
|
219
|
+
let remaining = assetsToDeallocate;
|
|
220
|
+
for (const available of nonEmpty.toSorted((a, b) => a === b ? 0 : a > b ? 1 : -1)) {
|
|
221
|
+
if (remaining <= 0n)
|
|
222
|
+
break;
|
|
223
|
+
penaltyLegs += 1;
|
|
224
|
+
remaining -= MathLib.min(available, remaining);
|
|
225
|
+
}
|
|
226
|
+
return {
|
|
227
|
+
penalty,
|
|
228
|
+
exitAssets,
|
|
229
|
+
assetsToWithdraw,
|
|
230
|
+
assetsToDeallocate,
|
|
231
|
+
// `sum(ceil(assetsᵢ·penalty/WAD)) <= ceil(sum(assetsᵢ)·penalty/WAD) + legs - 1`. The per-leg
|
|
232
|
+
// slack only exists because each chunk rounds its own charge up; at a zero penalty every chunk
|
|
233
|
+
// charges exactly nothing, so carrying the slack would invent penalty assets the contract never
|
|
234
|
+
// debits — inflating the share-burn denominator and weakening the derived bound.
|
|
235
|
+
penaltyAssets: penaltyLegs === 0 || penalty === 0n
|
|
236
|
+
? 0n
|
|
237
|
+
: MathLib.wMulUp(assetsToDeallocate, penalty) + BigInt(penaltyLegs - 1),
|
|
238
|
+
withdrawnAssets: assetsToWithdraw + assetsToDeallocate,
|
|
239
|
+
coveredAssets,
|
|
240
|
+
// Both bounds keep the ceiling *actionable*, since callers cap an input field with it and the
|
|
241
|
+
// coverage error tells them to reduce to it. A vault with nothing to give reports zero: the
|
|
242
|
+
// inversion rounds up to `1n` at a positive penalty, and `1n` withdraws nothing, which the
|
|
243
|
+
// entity rejects. And the gross-up can exceed the ABI slot on an enormous snapshot, which the
|
|
244
|
+
// entity also rejects — so it saturates rather than advertising an unusable amount.
|
|
245
|
+
maxExitAssets: withdrawableAssets === 0n && saturatedCoveredAssets === 0n
|
|
246
|
+
? 0n
|
|
247
|
+
: MathLib.min(withdrawableAssets +
|
|
248
|
+
MathLib.wMulUp(saturatedCoveredAssets + 1n, MathLib.WAD + penalty) -
|
|
249
|
+
1n, maxUint256),
|
|
250
|
+
penaltyLegs,
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Upper-bounds the vault shares a Vault V2 force withdrawal burns.
|
|
255
|
+
*
|
|
256
|
+
* The contract splits the burn across up to `penaltyLegs + 2` separate `VaultV2.withdraw` calls (the
|
|
257
|
+
* penalty-free leg, one per `forceDeallocate`, and the final deallocated leg), each rounding its own
|
|
258
|
+
* share burn up. Converting the aggregate already spends one ceiling, so the bound adds one share
|
|
259
|
+
* per *additional* leg that moves a positive amount — a zero-amount leg burns `toShares(0, "Up")`
|
|
260
|
+
* and rounds nothing.
|
|
261
|
+
*
|
|
262
|
+
* The result is the **denominator of {@link computeMinForceWithdrawSharePrice}** — the largest share
|
|
263
|
+
* burn the realized exit price is measured against. It takes the max over the two supplied snapshots
|
|
264
|
+
* because interest lowers the burn while management fees raise it, so neither snapshot alone bounds
|
|
265
|
+
* execution. The entity passes both accrued to `now` (execution time): the raw `lastUpdate` snapshot
|
|
266
|
+
* underestimates the burn a stale fee-bearing vault realizes — the first withdrawal accrues pending
|
|
267
|
+
* management fees before burning shares — which lifts the floor above the faithful price and trips
|
|
268
|
+
* `SlippageExceeded`. Accruing to `now`, not the caller-chosen `deadline`, tracks execution without
|
|
269
|
+
* letting a long deadline silently weaken the guard; `slippageTolerance` absorbs the residual drift
|
|
270
|
+
* until inclusion. Pass distinct snapshots to bound the burn across an accrual window.
|
|
271
|
+
*
|
|
272
|
+
* This is **not** the share allowance to authorize. The price floor's denominator is this bound
|
|
273
|
+
* minus `computeVaultV2ForceWithdrawFeeSharesMinted({ vaultData, owner, timestamp: now })`: the
|
|
274
|
+
* contract measures the *net* burn, which fee mints to `owner` shrink. The allowance is
|
|
275
|
+
* `min(mulDivUp(exitAssets, RAY, minSharePriceE27) + computeVaultV2ForceWithdrawFeeSharesMinted({
|
|
276
|
+
* vaultData, owner, timestamp: deadline }), maxUint256)`: the permit pays for the *gross* burn.
|
|
277
|
+
*
|
|
278
|
+
* @param params - Share-bound inputs.
|
|
279
|
+
* @param params.vaultData - Pre-fetched Vault V2 accrual snapshot.
|
|
280
|
+
* @param params.deadlineVaultData - The same vault accrued to a second timestamp; the max of the two
|
|
281
|
+
* share burns is returned. The entity passes the `now`-accrued snapshot for both.
|
|
282
|
+
* @param params.plan - Plan from {@link computeVaultV2ForceWithdrawPlan}.
|
|
283
|
+
* @returns An upper bound, in vault shares, of what the exit burns.
|
|
284
|
+
* @example
|
|
285
|
+
* ```ts
|
|
286
|
+
* import { MathLib } from "@morpho-org/blue-sdk";
|
|
287
|
+
* import {
|
|
288
|
+
* computeVaultV2ForceWithdrawFeeSharesMinted,
|
|
289
|
+
* computeVaultV2ForceWithdrawSharesBurnt,
|
|
290
|
+
* } from "@morpho-org/morpho-sdk";
|
|
291
|
+
*
|
|
292
|
+
* const { vault: nowVaultData } = vaultData.accrueInterest(now);
|
|
293
|
+
* const sharesBurnt = computeVaultV2ForceWithdrawSharesBurnt({
|
|
294
|
+
* vaultData: nowVaultData,
|
|
295
|
+
* deadlineVaultData: nowVaultData,
|
|
296
|
+
* plan,
|
|
297
|
+
* });
|
|
298
|
+
* const feeSharesNow = computeVaultV2ForceWithdrawFeeSharesMinted({
|
|
299
|
+
* vaultData,
|
|
300
|
+
* owner,
|
|
301
|
+
* timestamp: now,
|
|
302
|
+
* });
|
|
303
|
+
* const sharesBurntForFloor = sharesBurnt - feeSharesNow;
|
|
304
|
+
* const requiredShareAllowance = MathLib.min(
|
|
305
|
+
* MathLib.mulDivUp(exitAssets, MathLib.RAY, minSharePriceE27) +
|
|
306
|
+
* computeVaultV2ForceWithdrawFeeSharesMinted({
|
|
307
|
+
* vaultData,
|
|
308
|
+
* owner,
|
|
309
|
+
* timestamp: deadline,
|
|
310
|
+
* }),
|
|
311
|
+
* maxUint256,
|
|
312
|
+
* );
|
|
313
|
+
* ```
|
|
314
|
+
*/
|
|
315
|
+
export function computeVaultV2ForceWithdrawSharesBurnt(params) {
|
|
316
|
+
const { vaultData, deadlineVaultData, plan } = params;
|
|
317
|
+
// `penaltyAssets` already carries the per-market ceil accumulation, so this is the full
|
|
318
|
+
// penalty-inclusive amount debited from the user's position.
|
|
319
|
+
const grossDebited = plan.withdrawnAssets + plan.penaltyAssets;
|
|
320
|
+
// Only withdrawals that move a positive amount round anything: `toShares(0, "Up")` is `0`. So the
|
|
321
|
+
// penalty-free leg counts only when it pays out, the `forceDeallocate` burns count only at a
|
|
322
|
+
// positive penalty, and the deallocated leg counts only when the loop deallocated something.
|
|
323
|
+
const positiveLegs = (plan.assetsToWithdraw > 0n ? 1 : 0) +
|
|
324
|
+
(plan.penalty > 0n ? plan.penaltyLegs : 0) +
|
|
325
|
+
(plan.assetsToDeallocate > 0n ? 1 : 0);
|
|
326
|
+
return (MathLib.max(vaultData.toShares(grossDebited, "Up"), deadlineVaultData.toShares(grossDebited, "Up")) +
|
|
327
|
+
// `sum(ceil(sharesᵢ)) <= ceil(sum(sharesᵢ)) + (positiveLegs - 1)`: the aggregate conversion above
|
|
328
|
+
// already spends one ceiling, so only the *additional* positive legs can each cost one more
|
|
329
|
+
// share. A fixed `penaltyLegs + 2` overcounts every zero-amount leg, and because this value is
|
|
330
|
+
// the slippage denominator an overcount silently widens the price drop the bound accepts —
|
|
331
|
+
// worst on a dust exit, where two phantom shares can dominate the real burn.
|
|
332
|
+
BigInt(positiveLegs > 0 ? positiveLegs - 1 : 0));
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Computes a lower bound of the Vault V2 shares that a force withdrawal burns.
|
|
336
|
+
*
|
|
337
|
+
* The penalty-free/penalised split can move as liquidity accrues between planning and inclusion.
|
|
338
|
+
* This lower bound is used to reject fee-recipient exits whose fee mints could reach the burn
|
|
339
|
+
* measured by VaultExitBundlesV1, independently of that split.
|
|
340
|
+
*
|
|
341
|
+
* @param params - Lower-bound inputs.
|
|
342
|
+
* @param params.vaultData - Vault V2 snapshot already accrued to the execution timestamp.
|
|
343
|
+
* @param params.plan - Plan from {@link computeVaultV2ForceWithdrawPlan}.
|
|
344
|
+
* @returns A lower bound, in vault shares, of the burn measured by the contract.
|
|
345
|
+
* @example
|
|
346
|
+
* ```ts
|
|
347
|
+
* import { computeVaultV2ForceWithdrawMinSharesBurnt } from "@morpho-org/morpho-sdk";
|
|
348
|
+
*
|
|
349
|
+
* const minSharesBurnt = computeVaultV2ForceWithdrawMinSharesBurnt({
|
|
350
|
+
* vaultData,
|
|
351
|
+
* plan,
|
|
352
|
+
* });
|
|
353
|
+
* ```
|
|
354
|
+
*/
|
|
355
|
+
export function computeVaultV2ForceWithdrawMinSharesBurnt(params) {
|
|
356
|
+
const { vaultData, plan } = params;
|
|
357
|
+
// The penalty-free/penalised split moves as the liquidity market accrues, but any split debits
|
|
358
|
+
// within `1 + penalty` (< 2 asset units) of `exitAssets`, so `exitAssets - 1` lower-bounds every
|
|
359
|
+
// execution-time split.
|
|
360
|
+
return vaultData.toShares(MathLib.min(plan.withdrawnAssets +
|
|
361
|
+
MathLib.wMulUp(plan.assetsToDeallocate, plan.penalty), MathLib.zeroFloorSub(plan.exitAssets, 1n)), "Down");
|
|
362
|
+
}
|
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
import type { InputMarketParams } from "@morpho-org/blue-sdk";
|
|
2
|
-
import type { Address, Hex, WalletClient } from "viem";
|
|
2
|
+
import type { Address, Hex, TypedDataDefinition, WalletClient } from "viem";
|
|
3
3
|
import type { Deallocation } from "./deallocation.js";
|
|
4
|
+
/**
|
|
5
|
+
* Serves as the common discriminated action-metadata base shared by both
|
|
6
|
+
* {@link TransactionAction} (actions that carry encoded calldata) and
|
|
7
|
+
* {@link SignatureRequirementAction} (signature requirements whose metadata is
|
|
8
|
+
* exposed before any calldata exists). Each concrete action narrows
|
|
9
|
+
* {@link BaseAction} on its literal `type` tag and carries an
|
|
10
|
+
* operation-specific `args` record surfaced for tracing.
|
|
11
|
+
*
|
|
12
|
+
* @typeParam TType - Literal discriminator identifying the action.
|
|
13
|
+
* @typeParam TArgs - The action's argument record.
|
|
14
|
+
*/
|
|
4
15
|
export interface BaseAction<TType extends string = string, TArgs extends Record<string, unknown> = Record<string, unknown>> {
|
|
5
16
|
readonly type: TType;
|
|
6
17
|
readonly args: TArgs;
|
|
@@ -77,14 +88,22 @@ export interface VaultV2InKindRedeemAction extends BaseAction<"vaultV2InKindRede
|
|
|
77
88
|
readonly deadline: bigint;
|
|
78
89
|
}> {
|
|
79
90
|
}
|
|
91
|
+
/**
|
|
92
|
+
* Metadata for a Vault V2 force withdrawal through VaultExitBundlesV1.
|
|
93
|
+
*
|
|
94
|
+
* `exitAssets` is penalty-inclusive: the assets actually delivered are
|
|
95
|
+
* `assetsToWithdraw + floor((exitAssets - assetsToWithdraw) * WAD / (WAD + penalty))`, minus the
|
|
96
|
+
* referral fee.
|
|
97
|
+
*/
|
|
80
98
|
export interface VaultV2ForceWithdrawAction extends BaseAction<"vaultV2ForceWithdraw", {
|
|
81
|
-
vault: Address;
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
onBehalf: Address;
|
|
99
|
+
readonly vault: Address;
|
|
100
|
+
readonly adapter: Address;
|
|
101
|
+
readonly exitAssets: bigint;
|
|
102
|
+
readonly minSharePriceE27: bigint;
|
|
103
|
+
readonly referralFeePct: bigint;
|
|
104
|
+
readonly referralFeeRecipient: Address;
|
|
105
|
+
readonly onBehalf: Address;
|
|
106
|
+
readonly deadline: bigint;
|
|
88
107
|
}> {
|
|
89
108
|
}
|
|
90
109
|
export interface VaultV2ForceRedeemAction extends BaseAction<"vaultV2ForceRedeem", {
|
|
@@ -379,8 +398,19 @@ export interface MidnightCancelOfferAction extends BaseAction<"midnightCancelOff
|
|
|
379
398
|
readonly onBehalf: Address;
|
|
380
399
|
}> {
|
|
381
400
|
}
|
|
382
|
-
/**
|
|
401
|
+
/**
|
|
402
|
+
* Enumerates every action a {@link Transaction} can describe across the VaultV1,
|
|
403
|
+
* VaultV2, Blue, and Midnight flows. The `type` tag discriminates the union so
|
|
404
|
+
* consumers can `switch` exhaustively on it.
|
|
405
|
+
*/
|
|
383
406
|
export type TransactionAction = ERC20ApprovalAction | VaultV2DepositAction | VaultV2WithdrawAction | VaultV2RedeemAction | VaultV2InKindRedeemAction | VaultV2ForceWithdrawAction | VaultV2ForceRedeemAction | VaultV1DepositAction | VaultV1WithdrawAction | VaultV1RedeemAction | VaultV1InKindRedeemAction | VaultV1MigrateToV2Action | BlueSupplyAction | BlueSupplyCollateralAction | BlueBorrowAction | BlueSupplyCollateralBorrowAction | BlueRepayAction | BlueWithdrawCollateralAction | BlueRepayWithdrawCollateralAction | BlueWithdrawAction | BlueRefinanceAction | BlueAuthorizationAction | MidnightAuthorizationAction | SetterRatifierRatifyRootAction | MidnightTakeLendAction | MidnightTakeBorrowAction | MidnightSupplyCollateralTakeBorrowAction | MidnightSupplyCollateralAction | MempoolSubmitOffersAction | MidnightRedeemAction | MidnightRepayWithdrawCollateralAction | MidnightCancelOfferAction;
|
|
407
|
+
/**
|
|
408
|
+
* Describes a single, immutable, deep-frozen transaction to submit on-chain:
|
|
409
|
+
* the target `to`, native `value`, encoded call `data`, and the originating
|
|
410
|
+
* {@link BaseAction} for tracing. Every action builder returns one.
|
|
411
|
+
*
|
|
412
|
+
* @typeParam TAction - The action that produced this transaction.
|
|
413
|
+
*/
|
|
384
414
|
export interface Transaction<TAction extends BaseAction = TransactionAction> {
|
|
385
415
|
readonly to: Address;
|
|
386
416
|
readonly value: bigint;
|
|
@@ -413,7 +443,7 @@ export type BundlesFundingArgs = {
|
|
|
413
443
|
export interface BundlesTokenRequirementsOptions {
|
|
414
444
|
/** Prefer ERC-2612 when the funded token exposes a compatible nonce. */
|
|
415
445
|
readonly useSimplePermit?: boolean;
|
|
416
|
-
/** Explicit unused Permit2 SignatureTransfer unordered nonce. */
|
|
446
|
+
/** Explicit unused Permit2 SignatureTransfer unordered nonce; defaults to the lowest unused nonce. */
|
|
417
447
|
readonly permit2Nonce?: bigint;
|
|
418
448
|
}
|
|
419
449
|
/** Mutually exclusive source amount accepted by a Vault V1 to Vault V2 migration. */
|
|
@@ -424,6 +454,13 @@ export type VaultV1MigrateToV2AmountArgs = {
|
|
|
424
454
|
readonly assets: bigint;
|
|
425
455
|
readonly shares?: never;
|
|
426
456
|
};
|
|
457
|
+
/**
|
|
458
|
+
* Holds the pre-resolved arguments for an ERC-2612 `permit`: the signed approval
|
|
459
|
+
* of `amount` of `asset` from `owner` to the permitted spender, bounded by the
|
|
460
|
+
* `deadline` timestamp and consuming the given `nonce`. The associated
|
|
461
|
+
* {@link PermitAction} identifies the permitted spender, including fixed-bundle
|
|
462
|
+
* contracts and the GeneralAdapter1 periphery.
|
|
463
|
+
*/
|
|
427
464
|
export interface PermitArgs {
|
|
428
465
|
readonly owner: Address;
|
|
429
466
|
readonly nonce: bigint;
|
|
@@ -432,6 +469,12 @@ export interface PermitArgs {
|
|
|
432
469
|
readonly amount: bigint;
|
|
433
470
|
readonly deadline: bigint;
|
|
434
471
|
}
|
|
472
|
+
/**
|
|
473
|
+
* Holds the pre-resolved arguments for a Permit2 `permit` bundler call. It
|
|
474
|
+
* mirrors {@link PermitArgs} but adds the Permit2 allowance `expiration` (when
|
|
475
|
+
* the on-chain allowance lapses) alongside the signature `deadline` (by when the
|
|
476
|
+
* signature must be submitted).
|
|
477
|
+
*/
|
|
435
478
|
export interface Permit2Args {
|
|
436
479
|
readonly owner: Address;
|
|
437
480
|
readonly nonce: bigint;
|
|
@@ -467,6 +510,8 @@ export interface MidnightOfferRootSignatureArgs {
|
|
|
467
510
|
readonly signature: Hex;
|
|
468
511
|
readonly payload: Hex;
|
|
469
512
|
}
|
|
513
|
+
/** EIP-712 payload carried by a signable requirement action. */
|
|
514
|
+
export type RequirementTypedData = TypedDataDefinition<Record<string, unknown>, string>;
|
|
470
515
|
export interface PermitAction extends BaseAction<"permit", {
|
|
471
516
|
readonly spender: Address;
|
|
472
517
|
readonly amount: bigint;
|
|
@@ -474,6 +519,13 @@ export interface PermitAction extends BaseAction<"permit", {
|
|
|
474
519
|
/** Permit nonce captured by a prepared requirement, when available. */
|
|
475
520
|
readonly nonce?: bigint;
|
|
476
521
|
}> {
|
|
522
|
+
/**
|
|
523
|
+
* EIP-712 payload `sign()` signs for this permit, exposed so it can be inspected or displayed
|
|
524
|
+
* before signing. Required on {@link Requirement.action}; optional here only so hand-built action
|
|
525
|
+
* metadata (e.g. test fixtures) need not supply it. The payload is deep-frozen and `sign()` signs
|
|
526
|
+
* this exact payload.
|
|
527
|
+
*/
|
|
528
|
+
readonly typedData?: RequirementTypedData;
|
|
477
529
|
}
|
|
478
530
|
export interface Permit2Action extends BaseAction<"permit2", {
|
|
479
531
|
spender: Address;
|
|
@@ -481,6 +533,8 @@ export interface Permit2Action extends BaseAction<"permit2", {
|
|
|
481
533
|
deadline: bigint;
|
|
482
534
|
expiration: bigint;
|
|
483
535
|
}> {
|
|
536
|
+
/** EIP-712 payload to sign for this Permit2 AllowanceTransfer. See {@link PermitAction.typedData}. */
|
|
537
|
+
readonly typedData?: RequirementTypedData;
|
|
484
538
|
}
|
|
485
539
|
/** Signable Permit2 SignatureTransfer requirement for a fixed bundles token pull. */
|
|
486
540
|
export interface Permit2SignatureTransferAction extends BaseAction<"permit2SignatureTransfer", {
|
|
@@ -489,6 +543,8 @@ export interface Permit2SignatureTransferAction extends BaseAction<"permit2Signa
|
|
|
489
543
|
readonly nonce: bigint;
|
|
490
544
|
readonly deadline: bigint;
|
|
491
545
|
}> {
|
|
546
|
+
/** EIP-712 payload to sign for this Permit2 SignatureTransfer. See {@link PermitAction.typedData}. */
|
|
547
|
+
readonly typedData?: RequirementTypedData;
|
|
492
548
|
}
|
|
493
549
|
/**
|
|
494
550
|
* Signable Morpho authorization requirement. Its `authorized` operator is route-specific:
|
|
@@ -499,6 +555,8 @@ export interface AuthorizationAction extends BaseAction<"authorization", {
|
|
|
499
555
|
isAuthorized: boolean;
|
|
500
556
|
deadline: bigint;
|
|
501
557
|
}> {
|
|
558
|
+
/** EIP-712 payload to sign for this Morpho authorization. See {@link PermitAction.typedData}. */
|
|
559
|
+
readonly typedData?: RequirementTypedData;
|
|
502
560
|
}
|
|
503
561
|
/** Metadata for a Midnight offer-root signature request. */
|
|
504
562
|
export interface MidnightOfferRootSignatureAction extends BaseAction<"midnightOfferRootSignature", {
|
|
@@ -506,6 +564,15 @@ export interface MidnightOfferRootSignatureAction extends BaseAction<"midnightOf
|
|
|
506
564
|
readonly ratifier: Address;
|
|
507
565
|
readonly offers: number;
|
|
508
566
|
}> {
|
|
567
|
+
/**
|
|
568
|
+
* EIP-712 offer-tree payload `sign()` signs for this Midnight ratification, exposed so it can be
|
|
569
|
+
* inspected or displayed before signing. See {@link PermitAction.typedData}.
|
|
570
|
+
*
|
|
571
|
+
* A bare signature over this payload is not enough to build the submit-offers transaction:
|
|
572
|
+
* `sign()` also derives the ratification payload (`MidnightOfferRootSignature.args.payload`) that
|
|
573
|
+
* `buildTx()` submits to the mempool.
|
|
574
|
+
*/
|
|
575
|
+
readonly typedData?: RequirementTypedData;
|
|
509
576
|
}
|
|
510
577
|
/** Action metadata supported by signature requirements. */
|
|
511
578
|
export type SignatureRequirementAction = PermitAction | Permit2Action | Permit2SignatureTransferAction | AuthorizationAction | MidnightOfferRootSignatureAction;
|
|
@@ -551,16 +618,31 @@ export type RequirementSignature<TAction extends SignatureRequirementAction | un
|
|
|
551
618
|
type RequirementResult<TSignatureOrAction extends RequirementSignature | SignatureRequirementAction, TArgs extends RequirementSignatureArgs | undefined> = TSignatureOrAction extends SignatureRequirementAction ? RequirementSignature<TSignatureOrAction, Extract<TArgs, RequirementSignatureArgs>> : Extract<TSignatureOrAction, RequirementSignature>;
|
|
552
619
|
/**
|
|
553
620
|
* A signable approval / authorization requirement. `sign()` returns the matching
|
|
554
|
-
* {@link RequirementSignature}; `action` describes the requirement without signing
|
|
621
|
+
* {@link RequirementSignature}; `action` describes the requirement without signing and carries the
|
|
622
|
+
* EIP-712 `typedData` payload so an integrator can inspect or display it before signing.
|
|
555
623
|
*
|
|
556
624
|
* Generic over the signature it produces so permit encoders narrow to
|
|
557
625
|
* {@link PermitRequirementSignature} and the authorization encoder to
|
|
558
626
|
* {@link AuthorizationRequirementSignature}; the two-parameter form is kept for
|
|
559
627
|
* Midnight action requirements that are parameterized by action and args.
|
|
628
|
+
*
|
|
629
|
+
* @example
|
|
630
|
+
* ```ts
|
|
631
|
+
* const requirement = (await output.getRequirements()).find(isRequirementSignature);
|
|
632
|
+
* if (requirement == null) return; // nothing to sign (only on-chain approvals, or none)
|
|
633
|
+
*
|
|
634
|
+
* const typedData = requirement.action.typedData; // exact EIP-712 payload `sign()` will sign
|
|
635
|
+
* const signed = await requirement.sign(walletClient, owner);
|
|
636
|
+
* const tx = output.buildTx([signed]); // Midnight outputs take the single signature instead
|
|
637
|
+
* ```
|
|
560
638
|
*/
|
|
561
639
|
export interface Requirement<TSignatureOrAction extends RequirementSignature | SignatureRequirementAction = RequirementSignature, TArgs extends RequirementSignatureArgs | undefined = undefined> {
|
|
562
|
-
|
|
563
|
-
|
|
640
|
+
/** Signs `action.typedData` with `client`, verifies the signature recovers `userAddress`, and returns the signed requirement. */
|
|
641
|
+
readonly sign: (client: WalletClient, userAddress: Address) => Promise<RequirementResult<TSignatureOrAction, TArgs>>;
|
|
642
|
+
/** Requirement metadata; `typedData` is always populated on SDK-built requirements. */
|
|
643
|
+
readonly action: RequirementResult<TSignatureOrAction, TArgs>["action"] & {
|
|
644
|
+
readonly typedData: RequirementTypedData;
|
|
645
|
+
};
|
|
564
646
|
}
|
|
565
647
|
/** Bundler3 token signature requirement. */
|
|
566
648
|
export type Bundler3TokenSignatureRequirement = Requirement<PermitRequirementSignature>;
|