@morpho-org/morpho-sdk 10.5.0-next.0 → 10.6.0-next.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 +44 -1
- package/lib/cjs/abis.d.ts +793 -0
- package/lib/cjs/abis.js +811 -1
- package/lib/cjs/actions/blue/close.d.ts +169 -0
- package/lib/cjs/actions/blue/close.js +270 -0
- package/lib/cjs/actions/blue/common.js +2 -2
- package/lib/cjs/actions/blue/delever.d.ts +156 -0
- package/lib/cjs/actions/blue/delever.js +223 -0
- package/lib/cjs/actions/blue/index.d.ts +3 -0
- package/lib/cjs/actions/blue/index.js +3 -0
- package/lib/cjs/actions/blue/lever.d.ts +184 -0
- package/lib/cjs/actions/blue/lever.js +272 -0
- package/lib/cjs/actions/blue/leverageIntent.d.ts +65 -0
- package/lib/cjs/actions/blue/leverageIntent.js +103 -0
- package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +16 -11
- package/lib/cjs/actions/midnight/takeBorrow.js +14 -8
- package/lib/cjs/actions/midnight/takeLend.js +15 -9
- package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.js +19 -13
- package/lib/cjs/actions/midnight/takeWithdraw.js +14 -8
- package/lib/cjs/actions/requirements/blue/encodeLeverageAuthorization.d.ts +39 -0
- package/lib/cjs/actions/requirements/blue/encodeLeverageAuthorization.js +59 -0
- package/lib/cjs/actions/requirements/blue/getLeverageRequirements.d.ts +104 -0
- package/lib/cjs/actions/requirements/blue/getLeverageRequirements.js +193 -0
- package/lib/cjs/actions/requirements/blue/getLeverageRequirementsApproval.d.ts +45 -0
- package/lib/cjs/actions/requirements/blue/getLeverageRequirementsApproval.js +66 -0
- package/lib/cjs/actions/requirements/blue/index.d.ts +3 -0
- package/lib/cjs/actions/requirements/blue/index.js +17 -0
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +2 -2
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +3 -2
- package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +11 -6
- package/lib/cjs/actions/requirements/getRequirementsApproval.js +29 -5
- package/lib/cjs/addresses.d.ts +1 -1
- package/lib/cjs/addresses.js +6 -1
- package/lib/cjs/blue/addresses.d.ts +1 -1
- package/lib/cjs/blue/addresses.js +6 -1
- package/lib/cjs/constants.d.ts +1 -1
- package/lib/cjs/constants.js +7 -2
- package/lib/cjs/entities/blue/blue.d.ts +486 -2
- package/lib/cjs/entities/blue/blue.js +529 -34
- package/lib/cjs/entities/index.d.ts +1 -1
- package/lib/cjs/entities/midnight/midnight.d.ts +6 -0
- package/lib/cjs/entities/midnight/midnight.js +21 -2
- package/lib/cjs/entities/requirements/assertContractCode.d.ts +0 -23
- package/lib/cjs/entities/requirements/assertContractCode.js +3 -28
- package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +117 -0
- package/lib/cjs/entities/vaultV2BlueReallocationData.js +226 -2
- package/lib/cjs/helpers/constant.d.ts +41 -0
- package/lib/cjs/helpers/constant.js +42 -1
- package/lib/cjs/helpers/getZeroExLeverageBundle.d.ts +13 -0
- package/lib/cjs/helpers/getZeroExLeverageBundle.js +27 -0
- package/lib/cjs/helpers/index.d.ts +4 -1
- package/lib/cjs/helpers/index.js +29 -1
- package/lib/cjs/helpers/leverage/index.d.ts +1 -0
- package/lib/cjs/helpers/leverage/index.js +11 -0
- package/lib/cjs/helpers/leverage/limits.d.ts +472 -0
- package/lib/cjs/helpers/leverage/limits.js +715 -0
- package/lib/cjs/helpers/leverage/positionMarket.d.ts +17 -0
- package/lib/cjs/helpers/leverage/positionMarket.js +28 -0
- package/lib/cjs/helpers/leverage/reallocations.d.ts +159 -0
- package/lib/cjs/helpers/leverage/reallocations.js +276 -0
- package/lib/cjs/helpers/validate.d.ts +14 -0
- package/lib/cjs/helpers/validate.js +25 -1
- package/lib/cjs/helpers/validateRequirementSpender.d.ts +1 -1
- package/lib/cjs/helpers/validateRequirementSpender.js +4 -0
- package/lib/cjs/helpers/zeroExQuote.d.ts +250 -0
- package/lib/cjs/helpers/zeroExQuote.js +763 -0
- package/lib/cjs/index.d.ts +1 -0
- package/lib/cjs/index.js +5 -2
- package/lib/cjs/reads/assertContractCode.d.ts +25 -0
- package/lib/cjs/reads/assertContractCode.js +30 -0
- package/lib/cjs/reads/fetchZeroExQuote.d.ts +123 -0
- package/lib/cjs/reads/fetchZeroExQuote.js +330 -0
- package/lib/cjs/transactionPlan/midnight/takeBorrow.d.ts +4 -0
- package/lib/cjs/transactionPlan/midnight/takeBorrow.js +8 -2
- package/lib/cjs/types/action.d.ts +130 -1
- package/lib/cjs/types/error.d.ts +84 -0
- package/lib/cjs/types/error.js +110 -3
- package/lib/cjs/types/index.d.ts +2 -0
- package/lib/cjs/types/index.js +2 -0
- package/lib/cjs/types/leverageError.d.ts +1552 -0
- package/lib/cjs/types/leverageError.js +1711 -0
- package/lib/cjs/types/zeroEx.d.ts +171 -0
- package/lib/cjs/types/zeroEx.js +2 -0
- package/lib/cjs/version.d.ts +1 -1
- package/lib/cjs/version.js +1 -1
- package/lib/esm/abis.d.ts +793 -0
- package/lib/esm/abis.js +809 -0
- package/lib/esm/actions/blue/close.d.ts +169 -0
- package/lib/esm/actions/blue/close.js +266 -0
- package/lib/esm/actions/blue/common.js +3 -3
- package/lib/esm/actions/blue/delever.d.ts +156 -0
- package/lib/esm/actions/blue/delever.js +219 -0
- package/lib/esm/actions/blue/index.d.ts +3 -0
- package/lib/esm/actions/blue/index.js +3 -0
- package/lib/esm/actions/blue/lever.d.ts +184 -0
- package/lib/esm/actions/blue/lever.js +268 -0
- package/lib/esm/actions/blue/leverageIntent.d.ts +65 -0
- package/lib/esm/actions/blue/leverageIntent.js +96 -0
- package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +16 -11
- package/lib/esm/actions/midnight/takeBorrow.js +14 -8
- package/lib/esm/actions/midnight/takeLend.js +15 -9
- package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.js +19 -13
- package/lib/esm/actions/midnight/takeWithdraw.js +14 -8
- package/lib/esm/actions/requirements/blue/encodeLeverageAuthorization.d.ts +39 -0
- package/lib/esm/actions/requirements/blue/encodeLeverageAuthorization.js +55 -0
- package/lib/esm/actions/requirements/blue/getLeverageRequirements.d.ts +104 -0
- package/lib/esm/actions/requirements/blue/getLeverageRequirements.js +189 -0
- package/lib/esm/actions/requirements/blue/getLeverageRequirementsApproval.d.ts +45 -0
- package/lib/esm/actions/requirements/blue/getLeverageRequirementsApproval.js +62 -0
- package/lib/esm/actions/requirements/blue/index.d.ts +3 -0
- package/lib/esm/actions/requirements/blue/index.js +3 -0
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +2 -2
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +3 -2
- package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +11 -6
- package/lib/esm/actions/requirements/getRequirementsApproval.js +29 -5
- package/lib/esm/addresses.d.ts +1 -1
- package/lib/esm/addresses.js +1 -1
- package/lib/esm/blue/addresses.d.ts +1 -1
- package/lib/esm/blue/addresses.js +1 -1
- package/lib/esm/constants.d.ts +1 -1
- package/lib/esm/constants.js +1 -1
- package/lib/esm/entities/blue/blue.d.ts +486 -2
- package/lib/esm/entities/blue/blue.js +501 -6
- package/lib/esm/entities/index.d.ts +1 -1
- package/lib/esm/entities/midnight/midnight.d.ts +6 -0
- package/lib/esm/entities/midnight/midnight.js +21 -2
- package/lib/esm/entities/requirements/assertContractCode.d.ts +0 -23
- package/lib/esm/entities/requirements/assertContractCode.js +1 -25
- package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +117 -0
- package/lib/esm/entities/vaultV2BlueReallocationData.js +227 -3
- package/lib/esm/helpers/constant.d.ts +41 -0
- package/lib/esm/helpers/constant.js +41 -0
- package/lib/esm/helpers/getZeroExLeverageBundle.d.ts +13 -0
- package/lib/esm/helpers/getZeroExLeverageBundle.js +23 -0
- package/lib/esm/helpers/index.d.ts +4 -1
- package/lib/esm/helpers/index.js +4 -1
- package/lib/esm/helpers/leverage/index.d.ts +1 -0
- package/lib/esm/helpers/leverage/index.js +1 -0
- package/lib/esm/helpers/leverage/limits.d.ts +472 -0
- package/lib/esm/helpers/leverage/limits.js +705 -0
- package/lib/esm/helpers/leverage/positionMarket.d.ts +17 -0
- package/lib/esm/helpers/leverage/positionMarket.js +24 -0
- package/lib/esm/helpers/leverage/reallocations.d.ts +159 -0
- package/lib/esm/helpers/leverage/reallocations.js +271 -0
- package/lib/esm/helpers/validate.d.ts +14 -0
- package/lib/esm/helpers/validate.js +25 -2
- package/lib/esm/helpers/validateRequirementSpender.d.ts +1 -1
- package/lib/esm/helpers/validateRequirementSpender.js +5 -1
- package/lib/esm/helpers/zeroExQuote.d.ts +250 -0
- package/lib/esm/helpers/zeroExQuote.js +754 -0
- package/lib/esm/index.d.ts +1 -0
- package/lib/esm/index.js +1 -0
- package/lib/esm/reads/assertContractCode.d.ts +25 -0
- package/lib/esm/reads/assertContractCode.js +26 -0
- package/lib/esm/reads/fetchZeroExQuote.d.ts +123 -0
- package/lib/esm/reads/fetchZeroExQuote.js +326 -0
- package/lib/esm/transactionPlan/midnight/takeBorrow.d.ts +4 -0
- package/lib/esm/transactionPlan/midnight/takeBorrow.js +9 -3
- package/lib/esm/types/action.d.ts +130 -1
- package/lib/esm/types/error.d.ts +84 -0
- package/lib/esm/types/error.js +103 -0
- package/lib/esm/types/index.d.ts +2 -0
- package/lib/esm/types/index.js +2 -0
- package/lib/esm/types/leverageError.d.ts +1552 -0
- package/lib/esm/types/leverageError.js +1660 -0
- package/lib/esm/types/zeroEx.d.ts +171 -0
- package/lib/esm/types/zeroEx.js +1 -0
- package/lib/esm/version.d.ts +1 -1
- package/lib/esm/version.js +1 -1
- package/package.json +7 -7
|
@@ -0,0 +1,1660 @@
|
|
|
1
|
+
const CLOSE_NO_SWAP_EXITS = "Or exit without a swap: repay or supplyCollateral to reduce risk, repayWithdrawCollateral to leave the position.";
|
|
2
|
+
/**
|
|
3
|
+
* Base class for leverage refusals caused by an unusable 0x quote or swap calldata.
|
|
4
|
+
* Fallback: fetch a new 0x quote and build again, except for five errors a new quote alone does not clear:
|
|
5
|
+
* {@link ZeroExExactOutputUnsupportedError}, refused before any request for a
|
|
6
|
+
* native and wrapped-native exact-output pair, {@link ZeroExQuoteNoLiquidityError}, which calls for a smaller
|
|
7
|
+
* amount or a no-swap exit, {@link ZeroExQuoteRequestError}, which a retry clears only when no response arrived,
|
|
8
|
+
* a 2xx response's body failed to download, or 0x answered 429 or 5xx, {@link ZeroExUnknownSwapKindError}, refused for an invalid action kind, and
|
|
9
|
+
* {@link ZeroExInvalidTxOriginError}, refused for a malformed `txOrigin`. Handle them before the generic requote
|
|
10
|
+
* branch. The leverage builders never throw {@link ZeroExSettlerPausedError} or {@link ZeroExSettlerNotOfficialError}:
|
|
11
|
+
* the `@morpho-org/evm-simulation` 0x Settler registry check reports the same conditions as non-blocking
|
|
12
|
+
* `ZERO_EX_SETTLER_UNVERIFIED` entries in `report.warnings`, with `reason` `"paused"` or `"notOfficial"`, read from
|
|
13
|
+
* the registry at the simulation block. The simulation still runs, and it returns no instance of these classes: read
|
|
14
|
+
* the warning's `code` and `reason`, not `instanceof`.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* import {
|
|
19
|
+
* ZeroExExactOutputUnsupportedError,
|
|
20
|
+
* ZeroExInvalidTxOriginError,
|
|
21
|
+
* ZeroExQuoteChainMismatchError,
|
|
22
|
+
* ZeroExQuoteError,
|
|
23
|
+
* ZeroExQuoteNoLiquidityError,
|
|
24
|
+
* ZeroExQuoteRequestError,
|
|
25
|
+
* ZeroExQuoteShortTimeBoundError,
|
|
26
|
+
* ZeroExUnknownSwapKindError,
|
|
27
|
+
* } from "@morpho-org/morpho-sdk";
|
|
28
|
+
*
|
|
29
|
+
* const controller = new AbortController(); // the caller aborts it to cancel the quote request
|
|
30
|
+
*
|
|
31
|
+
* function build(): never {
|
|
32
|
+
* throw new ZeroExQuoteChainMismatchError({ expected: 1, actual: 8453 });
|
|
33
|
+
* }
|
|
34
|
+
*
|
|
35
|
+
* try {
|
|
36
|
+
* build();
|
|
37
|
+
* } catch (error) {
|
|
38
|
+
* if (controller.signal.aborted) {
|
|
39
|
+
* // The caller cancelled the request (a fetch then fails with status null): do not retry.
|
|
40
|
+
* } else if (error instanceof ZeroExExactOutputUnsupportedError) {
|
|
41
|
+
* // 0x cannot buy an exact amount across a native and wrapped-native pair: offer an exit that needs no swap.
|
|
42
|
+
* } else if (error instanceof ZeroExQuoteNoLiquidityError) {
|
|
43
|
+
* // 0x has no route for this size: offer a smaller amount or an exit that needs no swap.
|
|
44
|
+
* } else if (
|
|
45
|
+
* error instanceof ZeroExQuoteRequestError &&
|
|
46
|
+
* error.status !== null &&
|
|
47
|
+
* error.status !== 429 &&
|
|
48
|
+
* error.status < 500
|
|
49
|
+
* ) {
|
|
50
|
+
* // 0x refused the request itself: fix the API key or the request; requoting hits the same refusal.
|
|
51
|
+
* } else if (error instanceof ZeroExUnknownSwapKindError) {
|
|
52
|
+
* // Fix the action kind; requoting hits the same refusal.
|
|
53
|
+
* } else if (error instanceof ZeroExInvalidTxOriginError) {
|
|
54
|
+
* // Pass a valid txOrigin, or omit it; requoting hits the same refusal.
|
|
55
|
+
* } else if (error instanceof ZeroExQuoteShortTimeBoundError) {
|
|
56
|
+
* // Fetch a new 0x quote; if the same word is refused again, offer a different amount, retry later or an exit
|
|
57
|
+
* // that needs no swap.
|
|
58
|
+
* } else if (error instanceof ZeroExQuoteError) {
|
|
59
|
+
* // Fetch a new 0x quote and build again.
|
|
60
|
+
* } else {
|
|
61
|
+
* throw error;
|
|
62
|
+
* }
|
|
63
|
+
* }
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
export class ZeroExQuoteError extends Error {
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Base class for leverage refusals where the action is unsafe at current prices or outside an SDK bound.
|
|
70
|
+
* Fallback: offer a no-swap exit (`repay` or `supplyCollateral`), a smaller action or a new quote. Five
|
|
71
|
+
* subclasses are exceptions: for {@link LeverageSlippageAboveLimitError}, the limit depends on neither the size
|
|
72
|
+
* nor the quote, so lower the slippage to `error.max` or less and build again, fetching a new quote with that
|
|
73
|
+
* slippage. For {@link LeverageCloseEmptyPositionError}, the position holds nothing, so there is no transaction to
|
|
74
|
+
* offer: tell the user there is nothing to close. For {@link LeverageCloseWithoutDebtError}, the position has
|
|
75
|
+
* collateral but no debt: offer `withdrawCollateral` instead. For {@link LeverageUnsupportedChainError},
|
|
76
|
+
* leverage is not available on the chain: offer only the no-swap actions, if Blue is available on the chain. For
|
|
77
|
+
* {@link LeverageOraclePriceUnavailableError}, the market has no usable oracle price, so a smaller action or a new
|
|
78
|
+
* quote fails the same way: on a Lever, read the leverage data again once the oracle answers; on a Delever or a
|
|
79
|
+
* Close, offer only the no-swap exits.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```ts
|
|
83
|
+
* import {
|
|
84
|
+
* InsufficientSharedLiquidityError,
|
|
85
|
+
* LeverageCloseEmptyPositionError,
|
|
86
|
+
* LeverageCloseWithoutDebtError,
|
|
87
|
+
* LeverageLimitError,
|
|
88
|
+
* LeverageOraclePriceUnavailableError,
|
|
89
|
+
* LeverageSlippageAboveLimitError,
|
|
90
|
+
* LeverageUnsupportedChainError,
|
|
91
|
+
* } from "@morpho-org/morpho-sdk";
|
|
92
|
+
*
|
|
93
|
+
* function build(): never {
|
|
94
|
+
* throw new LeverageSlippageAboveLimitError({
|
|
95
|
+
* operation: "lever",
|
|
96
|
+
* slippage: 20_000_000_000_000_000n,
|
|
97
|
+
* max: 10_000_000_000_000_000n,
|
|
98
|
+
* });
|
|
99
|
+
* }
|
|
100
|
+
*
|
|
101
|
+
* try {
|
|
102
|
+
* build();
|
|
103
|
+
* } catch (error) {
|
|
104
|
+
* if (error instanceof LeverageSlippageAboveLimitError) {
|
|
105
|
+
* // Lower the slippage to error.max or less and build again; fetch a new quote only if the action swaps.
|
|
106
|
+
* } else if (error instanceof LeverageCloseEmptyPositionError) {
|
|
107
|
+
* // Nothing to close: tell the user instead of offering another transaction.
|
|
108
|
+
* } else if (error instanceof LeverageCloseWithoutDebtError) {
|
|
109
|
+
* // Collateral but no debt: offer withdrawCollateral instead of a Close.
|
|
110
|
+
* } else if (error instanceof LeverageUnsupportedChainError) {
|
|
111
|
+
* // No leverage on this chain: if Blue is available here, offer repay, supplyCollateral or withdrawCollateral.
|
|
112
|
+
* } else if (error instanceof LeverageOraclePriceUnavailableError) {
|
|
113
|
+
* // No oracle price: on a Lever, read the leverage data again once the oracle answers; on a Delever or a
|
|
114
|
+
* // Close, offer only repay, supplyCollateral or repayWithdrawCollateral.
|
|
115
|
+
* } else if (error instanceof LeverageLimitError) {
|
|
116
|
+
* // Offer a smaller action, a no-swap exit or a new quote.
|
|
117
|
+
* } else if (error instanceof InsufficientSharedLiquidityError) {
|
|
118
|
+
* // Outside this base: a Lever's planned reallocations cannot fund the borrow, the shortfall the pure
|
|
119
|
+
* // builder reports as LeverageInsufficientLiquidityError. Offer a smaller Lever or other reallocations.
|
|
120
|
+
* } else {
|
|
121
|
+
* throw error;
|
|
122
|
+
* }
|
|
123
|
+
* }
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
export class LeverageLimitError extends Error {
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Base class for leverage refusals caused by an unsafe public-allocator liquidity route.
|
|
130
|
+
* Fallback: drop or replace the reallocations and build again.
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```ts
|
|
134
|
+
* import {
|
|
135
|
+
* InconsistentReallocationPenaltyError,
|
|
136
|
+
* LeverageReallocationError,
|
|
137
|
+
* LeverageReallocationExceedsBorrowError,
|
|
138
|
+
* ReallocationLoanTokenMismatchError,
|
|
139
|
+
* ReallocationWithdrawalOnTargetMarketError,
|
|
140
|
+
* } from "@morpho-org/morpho-sdk";
|
|
141
|
+
*
|
|
142
|
+
* function build(): never {
|
|
143
|
+
* throw new LeverageReallocationExceedsBorrowError({
|
|
144
|
+
* total: 101n,
|
|
145
|
+
* maxBorrowAssets: 100n,
|
|
146
|
+
* });
|
|
147
|
+
* }
|
|
148
|
+
*
|
|
149
|
+
* try {
|
|
150
|
+
* build();
|
|
151
|
+
* } catch (error) {
|
|
152
|
+
* // Three existing classes outside this base also call for other reallocations: a penalty that differs
|
|
153
|
+
* // from the vault's configured one, a source market with another loan token, and a source that is
|
|
154
|
+
* // the Lever's own market.
|
|
155
|
+
* if (
|
|
156
|
+
* !(error instanceof LeverageReallocationError) &&
|
|
157
|
+
* !(error instanceof InconsistentReallocationPenaltyError) &&
|
|
158
|
+
* !(error instanceof ReallocationLoanTokenMismatchError) &&
|
|
159
|
+
* !(error instanceof ReallocationWithdrawalOnTargetMarketError)
|
|
160
|
+
* ) {
|
|
161
|
+
* throw error;
|
|
162
|
+
* }
|
|
163
|
+
* // Build again with other reallocations or none.
|
|
164
|
+
* }
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
export class LeverageReallocationError extends Error {
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Thrown when a 0x quote was built for another chain than the leverage action's market.
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* ```ts
|
|
174
|
+
* import { ZeroExQuoteChainMismatchError } from "@morpho-org/morpho-sdk";
|
|
175
|
+
*
|
|
176
|
+
* const error = new ZeroExQuoteChainMismatchError({ expected: 1, actual: 8453 });
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
179
|
+
export class ZeroExQuoteChainMismatchError extends ZeroExQuoteError {
|
|
180
|
+
/**
|
|
181
|
+
* @param params - Chain comparison.
|
|
182
|
+
* @param params.expected - Chain id of the action's market.
|
|
183
|
+
* @param params.actual - Chain id of the quote.
|
|
184
|
+
*/
|
|
185
|
+
constructor(params) {
|
|
186
|
+
super(`0x quote is for chain "${params.actual}", but the action runs on chain "${params.expected}". Fetch a new quote for chain "${params.expected}".`);
|
|
187
|
+
this.expected = params.expected;
|
|
188
|
+
this.actual = params.actual;
|
|
189
|
+
this.name = "ZeroExQuoteChainMismatchError";
|
|
190
|
+
}
|
|
191
|
+
/** Chain id of the action's market. */
|
|
192
|
+
expected;
|
|
193
|
+
/** Chain id of the quote. */
|
|
194
|
+
actual;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Thrown when a 0x quote sells or buys another token than the leverage action swaps, in its `sellToken` or `buyToken`
|
|
198
|
+
* field or in its calldata: the AllowanceHolder sell token or Settler's buy token.
|
|
199
|
+
*
|
|
200
|
+
* @example
|
|
201
|
+
* ```ts
|
|
202
|
+
* import { ZeroExQuoteTokenMismatchError } from "@morpho-org/morpho-sdk";
|
|
203
|
+
*
|
|
204
|
+
* const error = new ZeroExQuoteTokenMismatchError({
|
|
205
|
+
* field: "sellToken",
|
|
206
|
+
* expected: weth,
|
|
207
|
+
* actual: quotedSellToken,
|
|
208
|
+
* });
|
|
209
|
+
* ```
|
|
210
|
+
*/
|
|
211
|
+
export class ZeroExQuoteTokenMismatchError extends ZeroExQuoteError {
|
|
212
|
+
/**
|
|
213
|
+
* @param params - Token comparison.
|
|
214
|
+
* @param params.field - Swap side that differs: the input (`sellToken`) or output (`buyToken`), in the quote's fields or
|
|
215
|
+
* its calldata.
|
|
216
|
+
* @param params.expected - Token the action swaps on that side.
|
|
217
|
+
* @param params.actual - Token the quote's fields or calldata use on that side.
|
|
218
|
+
*/
|
|
219
|
+
constructor(params) {
|
|
220
|
+
super(`0x quote ${params.field === "sellToken" ? "sells" : "buys"} "${params.actual}" in its ${params.field} field or its calldata, expected "${params.expected}". Fetch a new quote for the action's tokens.`);
|
|
221
|
+
this.field = params.field;
|
|
222
|
+
this.expected = params.expected;
|
|
223
|
+
this.actual = params.actual;
|
|
224
|
+
this.name = "ZeroExQuoteTokenMismatchError";
|
|
225
|
+
}
|
|
226
|
+
/** Swap side that differs, in the quote's fields or its calldata. */
|
|
227
|
+
field;
|
|
228
|
+
/** Token the action swaps on that side. */
|
|
229
|
+
expected;
|
|
230
|
+
/** Token the quote's fields or calldata use on that side. */
|
|
231
|
+
actual;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Thrown when a 0x quote fixes the wrong side of the swap for the leverage action:
|
|
235
|
+
* Lever and Close need an exact-output (`buyAmount`) quote, Delever an exact-input
|
|
236
|
+
* (`sellAmount`) quote.
|
|
237
|
+
*
|
|
238
|
+
* @example
|
|
239
|
+
* ```ts
|
|
240
|
+
* import { ZeroExQuoteTypeMismatchError } from "@morpho-org/morpho-sdk";
|
|
241
|
+
*
|
|
242
|
+
* const error = new ZeroExQuoteTypeMismatchError({
|
|
243
|
+
* expected: "exactOutput",
|
|
244
|
+
* actual: "exactInput",
|
|
245
|
+
* });
|
|
246
|
+
* ```
|
|
247
|
+
*/
|
|
248
|
+
export class ZeroExQuoteTypeMismatchError extends ZeroExQuoteError {
|
|
249
|
+
/**
|
|
250
|
+
* @param params - Quote type comparison.
|
|
251
|
+
* @param params.expected - Quote type the action requires.
|
|
252
|
+
* @param params.actual - Quote type supplied.
|
|
253
|
+
*/
|
|
254
|
+
constructor(params) {
|
|
255
|
+
super(`0x quote is "${params.actual}", but the action requires "${params.expected}". Fetch a new 0x quote of that type for this action.`);
|
|
256
|
+
this.expected = params.expected;
|
|
257
|
+
this.actual = params.actual;
|
|
258
|
+
this.name = "ZeroExQuoteTypeMismatchError";
|
|
259
|
+
}
|
|
260
|
+
/** Quote type the action requires. */
|
|
261
|
+
expected;
|
|
262
|
+
/** Quote type supplied. */
|
|
263
|
+
actual;
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Thrown when a 0x quote's taker is not the leverage bundle that executes the swap.
|
|
267
|
+
*
|
|
268
|
+
* @example
|
|
269
|
+
* ```ts
|
|
270
|
+
* import { ZeroExQuoteTakerMismatchError } from "@morpho-org/morpho-sdk";
|
|
271
|
+
*
|
|
272
|
+
* const error = new ZeroExQuoteTakerMismatchError({
|
|
273
|
+
* expected: zeroExLeverageBundle,
|
|
274
|
+
* actual: quotedTaker,
|
|
275
|
+
* });
|
|
276
|
+
* ```
|
|
277
|
+
*/
|
|
278
|
+
export class ZeroExQuoteTakerMismatchError extends ZeroExQuoteError {
|
|
279
|
+
/**
|
|
280
|
+
* @param params - Taker comparison.
|
|
281
|
+
* @param params.expected - Leverage bundle address.
|
|
282
|
+
* @param params.actual - Taker the quote was built for.
|
|
283
|
+
*/
|
|
284
|
+
constructor(params) {
|
|
285
|
+
super(`0x quote taker is "${params.actual}", expected the leverage bundle "${params.expected}". Fetch a new quote with the bundle as taker.`);
|
|
286
|
+
this.expected = params.expected;
|
|
287
|
+
this.actual = params.actual;
|
|
288
|
+
this.name = "ZeroExQuoteTakerMismatchError";
|
|
289
|
+
}
|
|
290
|
+
/** Leverage bundle address. */
|
|
291
|
+
expected;
|
|
292
|
+
/** Taker the quote was built for. */
|
|
293
|
+
actual;
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Thrown when a 0x quote's transaction carries native value; leverage swaps are ERC-20 only.
|
|
297
|
+
*
|
|
298
|
+
* @example
|
|
299
|
+
* ```ts
|
|
300
|
+
* import { ZeroExQuoteNonZeroValueError } from "@morpho-org/morpho-sdk";
|
|
301
|
+
*
|
|
302
|
+
* const error = new ZeroExQuoteNonZeroValueError(1n);
|
|
303
|
+
* ```
|
|
304
|
+
*/
|
|
305
|
+
export class ZeroExQuoteNonZeroValueError extends ZeroExQuoteError {
|
|
306
|
+
value;
|
|
307
|
+
/**
|
|
308
|
+
* @param value - Native value carried by the quote's transaction.
|
|
309
|
+
*/
|
|
310
|
+
constructor(value) {
|
|
311
|
+
super(`0x quote transaction carries value "${value}", expected "0". Fetch a new ERC-20 quote without native value.`);
|
|
312
|
+
this.value = value;
|
|
313
|
+
this.name = "ZeroExQuoteNonZeroValueError";
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Thrown when a 0x quote's transaction does not call the chain's AllowanceHolder.
|
|
318
|
+
*
|
|
319
|
+
* @example
|
|
320
|
+
* ```ts
|
|
321
|
+
* import { ZeroExQuoteTargetMismatchError } from "@morpho-org/morpho-sdk";
|
|
322
|
+
* import { ZERO_EX_ALLOWANCE_HOLDER_ADDRESS } from "@morpho-org/morpho-sdk/addresses";
|
|
323
|
+
*
|
|
324
|
+
* const error = new ZeroExQuoteTargetMismatchError({
|
|
325
|
+
* expected: ZERO_EX_ALLOWANCE_HOLDER_ADDRESS,
|
|
326
|
+
* actual: quotedTarget,
|
|
327
|
+
* });
|
|
328
|
+
* ```
|
|
329
|
+
*/
|
|
330
|
+
export class ZeroExQuoteTargetMismatchError extends ZeroExQuoteError {
|
|
331
|
+
/**
|
|
332
|
+
* @param params - Transaction target comparison.
|
|
333
|
+
* @param params.expected - AllowanceHolder address.
|
|
334
|
+
* @param params.actual - Address the quote's transaction calls.
|
|
335
|
+
*/
|
|
336
|
+
constructor(params) {
|
|
337
|
+
super(`0x quote transaction calls "${params.actual}", expected AllowanceHolder "${params.expected}". Fetch a new allowance-holder quote.`);
|
|
338
|
+
this.expected = params.expected;
|
|
339
|
+
this.actual = params.actual;
|
|
340
|
+
this.name = "ZeroExQuoteTargetMismatchError";
|
|
341
|
+
}
|
|
342
|
+
/** AllowanceHolder address. */
|
|
343
|
+
expected;
|
|
344
|
+
/** Address the quote's transaction calls. */
|
|
345
|
+
actual;
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Raised when the AllowanceHolder call's operator is not a Settler the 0x registry names for the taker
|
|
349
|
+
* feature (current, or previous during rotation). The leverage builders never throw it: the
|
|
350
|
+
* `@morpho-org/evm-simulation` 0x Settler registry check reports the same condition as a non-blocking
|
|
351
|
+
* `ZERO_EX_SETTLER_UNVERIFIED` entry in `report.warnings` with `reason: "notOfficial"`, from the registry state at
|
|
352
|
+
* the simulation block, not as an instance of this class; the simulation still runs. A new quote clears it once the registry names an eligible
|
|
353
|
+
* Settler; reading the leverage data again does not, because the leverage data holds no registry state.
|
|
354
|
+
*
|
|
355
|
+
* @example
|
|
356
|
+
* ```ts
|
|
357
|
+
* import { ZeroExSettlerNotOfficialError } from "@morpho-org/morpho-sdk";
|
|
358
|
+
*
|
|
359
|
+
* const error = new ZeroExSettlerNotOfficialError({
|
|
360
|
+
* field: "operator",
|
|
361
|
+
* actual: quotedOperator,
|
|
362
|
+
* official: [currentSettler],
|
|
363
|
+
* });
|
|
364
|
+
* ```
|
|
365
|
+
*/
|
|
366
|
+
export class ZeroExSettlerNotOfficialError extends ZeroExQuoteError {
|
|
367
|
+
/**
|
|
368
|
+
* @param params - Settler comparison.
|
|
369
|
+
* @param params.field - AllowanceHolder `exec` argument that differs.
|
|
370
|
+
* @param params.actual - Address found in the calldata.
|
|
371
|
+
* @param params.official - Settlers the registry accepts at the block it was read.
|
|
372
|
+
*/
|
|
373
|
+
constructor(params) {
|
|
374
|
+
super(params.official.length === 0
|
|
375
|
+
? `0x swap ${params.field} "${params.actual}" is not an official Settler, and the registry names no eligible Settler. Fetch a new quote only once the 0x registry names an eligible Settler.`
|
|
376
|
+
: `0x swap ${params.field} "${params.actual}" is not an official Settler, expected one of "${params.official.join('", "')}". Fetch a new quote.`);
|
|
377
|
+
this.field = params.field;
|
|
378
|
+
this.actual = params.actual;
|
|
379
|
+
this.official = params.official;
|
|
380
|
+
this.name = "ZeroExSettlerNotOfficialError";
|
|
381
|
+
}
|
|
382
|
+
/** AllowanceHolder `exec` argument that differs. */
|
|
383
|
+
field;
|
|
384
|
+
/** Address found in the calldata. */
|
|
385
|
+
actual;
|
|
386
|
+
/** Settlers the registry accepts at the block it was read. */
|
|
387
|
+
official;
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* Thrown when the AllowanceHolder call's target is not its operator: an address mismatch only. Neither
|
|
391
|
+
* address is checked against the 0x Settler registry here, so this refusal says nothing about whether the
|
|
392
|
+
* operator is an official Settler. Fetch a new quote.
|
|
393
|
+
*
|
|
394
|
+
* @example
|
|
395
|
+
* ```ts
|
|
396
|
+
* import { ZeroExSettlerOperatorTargetMismatchError } from "@morpho-org/morpho-sdk";
|
|
397
|
+
*
|
|
398
|
+
* const error = new ZeroExSettlerOperatorTargetMismatchError({
|
|
399
|
+
* operator: currentSettler,
|
|
400
|
+
* target: previousSettler,
|
|
401
|
+
* });
|
|
402
|
+
* ```
|
|
403
|
+
*/
|
|
404
|
+
export class ZeroExSettlerOperatorTargetMismatchError extends ZeroExQuoteError {
|
|
405
|
+
/**
|
|
406
|
+
* @param params - AllowanceHolder `exec` arguments.
|
|
407
|
+
* @param params.operator - Settler AllowanceHolder approves for the swap.
|
|
408
|
+
* @param params.target - Contract AllowanceHolder calls.
|
|
409
|
+
*/
|
|
410
|
+
constructor(params) {
|
|
411
|
+
super(`0x swap target "${params.target}" is not its operator "${params.operator}": AllowanceHolder must call the Settler it approves. Fetch a new quote.`);
|
|
412
|
+
this.operator = params.operator;
|
|
413
|
+
this.target = params.target;
|
|
414
|
+
this.name = "ZeroExSettlerOperatorTargetMismatchError";
|
|
415
|
+
}
|
|
416
|
+
/** Settler AllowanceHolder approves for the swap. */
|
|
417
|
+
operator;
|
|
418
|
+
/** Contract AllowanceHolder calls. */
|
|
419
|
+
target;
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* Raised when the 0x registry lookup for the taker Settler reverts, which means 0x paused it. The leverage
|
|
423
|
+
* builders never throw it: the `@morpho-org/evm-simulation` 0x Settler registry check reports the same condition as
|
|
424
|
+
* a non-blocking `ZERO_EX_SETTLER_UNVERIFIED` entry in `report.warnings` with `reason: "paused"`, from the registry
|
|
425
|
+
* state at the simulation block, not as an instance of this class; the simulation still runs. Unlike the other
|
|
426
|
+
* {@link ZeroExQuoteError} subclasses, a new quote cannot clear it. Offer the no-swap exits instead (`repay`,
|
|
427
|
+
* `supplyCollateral`, `repayWithdrawCollateral`), or retry the swap once the registry shows 0x resumed it.
|
|
428
|
+
*
|
|
429
|
+
* @example
|
|
430
|
+
* ```ts
|
|
431
|
+
* import { ZeroExSettlerPausedError } from "@morpho-org/morpho-sdk";
|
|
432
|
+
* import { ZERO_EX_SETTLER_REGISTRY_ADDRESS } from "@morpho-org/morpho-sdk/addresses";
|
|
433
|
+
*
|
|
434
|
+
* const error = new ZeroExSettlerPausedError({
|
|
435
|
+
* registry: ZERO_EX_SETTLER_REGISTRY_ADDRESS,
|
|
436
|
+
* });
|
|
437
|
+
* ```
|
|
438
|
+
*/
|
|
439
|
+
export class ZeroExSettlerPausedError extends ZeroExQuoteError {
|
|
440
|
+
/**
|
|
441
|
+
* @param params - Registry details.
|
|
442
|
+
* @param params.registry - 0x Settler registry address whose lookup reverted.
|
|
443
|
+
* @param options - Standard error options; `cause` carries the revert.
|
|
444
|
+
*/
|
|
445
|
+
constructor(params, options) {
|
|
446
|
+
super(`0x Settler registry "${params.registry}" reverted for the taker Settler, so 0x has paused swaps and no new quote can clear it. Exit without a swap with repay, supplyCollateral or repayWithdrawCollateral, or retry once the 0x registry shows swaps resumed.`, options);
|
|
447
|
+
this.registry = params.registry;
|
|
448
|
+
this.name = "ZeroExSettlerPausedError";
|
|
449
|
+
}
|
|
450
|
+
/** 0x Settler registry address whose lookup reverted. */
|
|
451
|
+
registry;
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* Thrown when the 0x swap pays its output to anyone but the leverage bundle.
|
|
455
|
+
*
|
|
456
|
+
* @example
|
|
457
|
+
* ```ts
|
|
458
|
+
* import { ZeroExQuoteRecipientMismatchError } from "@morpho-org/morpho-sdk";
|
|
459
|
+
*
|
|
460
|
+
* const error = new ZeroExQuoteRecipientMismatchError({
|
|
461
|
+
* expected: zeroExLeverageBundle,
|
|
462
|
+
* actual: quotedRecipient,
|
|
463
|
+
* });
|
|
464
|
+
* ```
|
|
465
|
+
*/
|
|
466
|
+
export class ZeroExQuoteRecipientMismatchError extends ZeroExQuoteError {
|
|
467
|
+
/**
|
|
468
|
+
* @param params - Recipient comparison.
|
|
469
|
+
* @param params.expected - Leverage bundle address.
|
|
470
|
+
* @param params.actual - Recipient found in the swap calldata. A zero recipient means the Settler slippage is only
|
|
471
|
+
* partly zero (a nonzero buy token or minimum output), since only an all-zero slippage skips the recipient check;
|
|
472
|
+
* the message says so.
|
|
473
|
+
*/
|
|
474
|
+
constructor(params) {
|
|
475
|
+
super(/^0x0{40}$/i.test(params.actual)
|
|
476
|
+
? `0x swap's Settler slippage has a zero recipient but a nonzero buy token or minimum output, so it pays "${params.actual}", expected the leverage bundle "${params.expected}". Fetch a new quote that pays the leverage bundle.`
|
|
477
|
+
: `0x swap pays "${params.actual}", expected the leverage bundle "${params.expected}". Fetch a new quote that pays the leverage bundle.`);
|
|
478
|
+
this.expected = params.expected;
|
|
479
|
+
this.actual = params.actual;
|
|
480
|
+
this.name = "ZeroExQuoteRecipientMismatchError";
|
|
481
|
+
}
|
|
482
|
+
/** Leverage bundle address. */
|
|
483
|
+
expected;
|
|
484
|
+
/** Recipient found in the swap calldata. */
|
|
485
|
+
actual;
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* Thrown when the amount the AllowanceHolder call may pull exceeds the action's swap input cap.
|
|
489
|
+
*
|
|
490
|
+
* @example
|
|
491
|
+
* ```ts
|
|
492
|
+
* import { ZeroExQuoteAmountExceedsCapError } from "@morpho-org/morpho-sdk";
|
|
493
|
+
*
|
|
494
|
+
* const error = new ZeroExQuoteAmountExceedsCapError({ amount: 101n, cap: 100n });
|
|
495
|
+
* ```
|
|
496
|
+
*/
|
|
497
|
+
export class ZeroExQuoteAmountExceedsCapError extends ZeroExQuoteError {
|
|
498
|
+
/**
|
|
499
|
+
* @param params - Amount comparison.
|
|
500
|
+
* @param params.amount - Amount in the AllowanceHolder `exec` call.
|
|
501
|
+
* @param params.cap - Largest swap input the action signs.
|
|
502
|
+
*/
|
|
503
|
+
constructor(params) {
|
|
504
|
+
super(`0x swap amount "${params.amount}" exceeds the action's swap input cap "${params.cap}". Fetch a new quote for the action's amounts.`);
|
|
505
|
+
this.amount = params.amount;
|
|
506
|
+
this.cap = params.cap;
|
|
507
|
+
this.name = "ZeroExQuoteAmountExceedsCapError";
|
|
508
|
+
}
|
|
509
|
+
/** Amount in the AllowanceHolder `exec` call. */
|
|
510
|
+
amount;
|
|
511
|
+
/** Largest swap input the action signs. */
|
|
512
|
+
cap;
|
|
513
|
+
}
|
|
514
|
+
/**
|
|
515
|
+
* Thrown when the amount the AllowanceHolder call pulls is not the amount the quote sells: `sellAmount` for an
|
|
516
|
+
* exact-input quote, `maxSellAmount` for an exact-output quote. The calldata does not match the quote's amounts.
|
|
517
|
+
*
|
|
518
|
+
* @example
|
|
519
|
+
* ```ts
|
|
520
|
+
* import { ZeroExQuoteSellAmountMismatchError } from "@morpho-org/morpho-sdk";
|
|
521
|
+
*
|
|
522
|
+
* const error = new ZeroExQuoteSellAmountMismatchError({ expected: 100n, actual: 99n });
|
|
523
|
+
* ```
|
|
524
|
+
*/
|
|
525
|
+
export class ZeroExQuoteSellAmountMismatchError extends ZeroExQuoteError {
|
|
526
|
+
/**
|
|
527
|
+
* @param params - Amount comparison.
|
|
528
|
+
* @param params.expected - Sell amount the quote reports.
|
|
529
|
+
* @param params.actual - Amount in the AllowanceHolder `exec` call.
|
|
530
|
+
*/
|
|
531
|
+
constructor(params) {
|
|
532
|
+
super(`0x swap pulls "${params.actual}" of the sell token, expected the quote's sell amount "${params.expected}". Fetch a new quote.`);
|
|
533
|
+
this.expected = params.expected;
|
|
534
|
+
this.actual = params.actual;
|
|
535
|
+
this.name = "ZeroExQuoteSellAmountMismatchError";
|
|
536
|
+
}
|
|
537
|
+
/** Sell amount the quote reports. */
|
|
538
|
+
expected;
|
|
539
|
+
/** Amount in the AllowanceHolder `exec` call. */
|
|
540
|
+
actual;
|
|
541
|
+
}
|
|
542
|
+
/**
|
|
543
|
+
* Thrown when the Settler `execute` slippage asks for a minimum output that is zero or below the quote's own floor:
|
|
544
|
+
* `minBuyAmount` for an exact-input quote, `buyAmount` for an exact-output quote. An all-zero slippage struct is not
|
|
545
|
+
* this error: Settler then checks nothing and the bundle's onchain output check protects the swap.
|
|
546
|
+
*
|
|
547
|
+
* @example
|
|
548
|
+
* ```ts
|
|
549
|
+
* import { ZeroExQuoteMinAmountOutMismatchError } from "@morpho-org/morpho-sdk";
|
|
550
|
+
*
|
|
551
|
+
* const error = new ZeroExQuoteMinAmountOutMismatchError({ expected: 100n, actual: 0n });
|
|
552
|
+
* ```
|
|
553
|
+
*/
|
|
554
|
+
export class ZeroExQuoteMinAmountOutMismatchError extends ZeroExQuoteError {
|
|
555
|
+
/**
|
|
556
|
+
* @param params - Minimum output comparison.
|
|
557
|
+
* @param params.expected - Smallest minimum output the quote allows.
|
|
558
|
+
* @param params.actual - Minimum output in the Settler `execute` slippage.
|
|
559
|
+
*/
|
|
560
|
+
constructor(params) {
|
|
561
|
+
super(params.actual === 0n
|
|
562
|
+
? "0x swap's Settler minimum output is zero, so Settler checks no output. Fetch a new quote."
|
|
563
|
+
: `0x swap's Settler minimum output "${params.actual}" is below the quote's floor "${params.expected}". Fetch a new quote.`);
|
|
564
|
+
this.expected = params.expected;
|
|
565
|
+
this.actual = params.actual;
|
|
566
|
+
this.name = "ZeroExQuoteMinAmountOutMismatchError";
|
|
567
|
+
}
|
|
568
|
+
/** Smallest minimum output the quote allows. */
|
|
569
|
+
expected;
|
|
570
|
+
/** Minimum output in the Settler `execute` slippage. */
|
|
571
|
+
actual;
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* Thrown when a Delever quote's `minBuyAmount` is zero or below `buyAmount × (1 − slippage)`, with the caller's
|
|
575
|
+
* slippage rounded down to whole parts per million as 0x rounds it. The Delever signs the quote's `minBuyAmount`
|
|
576
|
+
* as is, so a lower floor would let the swap lose more than the caller's slippage. 0x builds its floor from the
|
|
577
|
+
* slippage it was sent: fetch a new quote with the caller's slippage.
|
|
578
|
+
*
|
|
579
|
+
* @example
|
|
580
|
+
* ```ts
|
|
581
|
+
* import { ZeroExQuoteMinBuyAmountBelowSlippageError } from "@morpho-org/morpho-sdk";
|
|
582
|
+
*
|
|
583
|
+
* const error = new ZeroExQuoteMinBuyAmountBelowSlippageError({
|
|
584
|
+
* buyAmount: 1_658_936n,
|
|
585
|
+
* minBuyAmount: 1_650_640n,
|
|
586
|
+
* slippage: 5_000_000_000_000_000n,
|
|
587
|
+
* slippageFloor: 1_650_641n,
|
|
588
|
+
* });
|
|
589
|
+
* ```
|
|
590
|
+
*/
|
|
591
|
+
export class ZeroExQuoteMinBuyAmountBelowSlippageError extends ZeroExQuoteError {
|
|
592
|
+
/**
|
|
593
|
+
* @param params - Floor comparison, in the buy token's smallest unit.
|
|
594
|
+
* @param params.buyAmount - Quote's expected output before slippage.
|
|
595
|
+
* @param params.minBuyAmount - Quote's minimum output.
|
|
596
|
+
* @param params.slippage - Slippage supplied by the caller, WAD-scaled.
|
|
597
|
+
* @param params.slippageFloor - Smallest `minBuyAmount` the caller's slippage accepts: `buyAmount` reduced by
|
|
598
|
+
* the slippage rounded down to whole parts per million, rounded down.
|
|
599
|
+
*/
|
|
600
|
+
constructor(params) {
|
|
601
|
+
super(`Delever quote's minBuyAmount "${params.minBuyAmount}" is below "${params.slippageFloor}", its buyAmount "${params.buyAmount}" less the caller's slippage "${params.slippage}" (WAD), or is zero. Fetch a new quote with the caller's slippage.`);
|
|
602
|
+
this.buyAmount = params.buyAmount;
|
|
603
|
+
this.minBuyAmount = params.minBuyAmount;
|
|
604
|
+
this.slippage = params.slippage;
|
|
605
|
+
this.slippageFloor = params.slippageFloor;
|
|
606
|
+
this.name = "ZeroExQuoteMinBuyAmountBelowSlippageError";
|
|
607
|
+
}
|
|
608
|
+
/** Quote's expected output before slippage. */
|
|
609
|
+
buyAmount;
|
|
610
|
+
/** Quote's minimum output. */
|
|
611
|
+
minBuyAmount;
|
|
612
|
+
/** Slippage supplied by the caller, WAD-scaled. */
|
|
613
|
+
slippage;
|
|
614
|
+
/** Smallest `minBuyAmount` the caller's slippage accepts. */
|
|
615
|
+
slippageFloor;
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* Thrown when 0x quote calldata decodes as a call the leverage bundle cannot run, such as a Settler entry point
|
|
619
|
+
* other than the taker-submitted `execute` (`executeWithPermit` or a metatransaction). It is also thrown for a
|
|
620
|
+
* Settler `SELECT` that does not decode or is nested more than three levels deep, since its candidates would go
|
|
621
|
+
* unchecked, and for a time-bound action (`BASIC`, `BASIC_PUSH`, a permit action, `NATIVE_CHECK` or `ATTEST`) too
|
|
622
|
+
* short or malformed to read. `validateZeroExQuote` throws {@link ZeroExQuoteUndecodableCalldataError} instead for a
|
|
623
|
+
* calldata layer that does not decode at all.
|
|
624
|
+
*
|
|
625
|
+
* @example
|
|
626
|
+
* ```ts
|
|
627
|
+
* import { ZeroExQuoteMalformedCalldataError } from "@morpho-org/morpho-sdk";
|
|
628
|
+
*
|
|
629
|
+
* const error = new ZeroExQuoteMalformedCalldataError({
|
|
630
|
+
* reason: "Settler entry point is executeWithPermit, not execute",
|
|
631
|
+
* });
|
|
632
|
+
* ```
|
|
633
|
+
*/
|
|
634
|
+
export class ZeroExQuoteMalformedCalldataError extends ZeroExQuoteError {
|
|
635
|
+
/**
|
|
636
|
+
* @param params - Decoding details.
|
|
637
|
+
* @param params.reason - What failed to decode.
|
|
638
|
+
* @param options - Standard error options; `cause` carries the decoding error.
|
|
639
|
+
*/
|
|
640
|
+
constructor(params, options) {
|
|
641
|
+
super(`0x quote calldata is malformed: ${params.reason}. Fetch a new allowance-holder quote.`, options);
|
|
642
|
+
this.reason = params.reason;
|
|
643
|
+
this.name = "ZeroExQuoteMalformedCalldataError";
|
|
644
|
+
}
|
|
645
|
+
/** What failed to decode. */
|
|
646
|
+
reason;
|
|
647
|
+
}
|
|
648
|
+
/**
|
|
649
|
+
* Thrown when a layer of 0x quote calldata does not decode, whichever leverage bundle takes the quote: calldata the SDK
|
|
650
|
+
* cannot read cannot be shown to carry no signed order or short time bound, so every layer must decode.
|
|
651
|
+
* `layer` is `"allowanceHolder"` when the calldata is not an AllowanceHolder `exec` call, `"settler"` when the
|
|
652
|
+
* `exec` data is not a Settler `execute` call.
|
|
653
|
+
*
|
|
654
|
+
* @example
|
|
655
|
+
* ```ts
|
|
656
|
+
* import { ZeroExQuoteUndecodableCalldataError } from "@morpho-org/morpho-sdk";
|
|
657
|
+
*
|
|
658
|
+
* const error = new ZeroExQuoteUndecodableCalldataError({
|
|
659
|
+
* layer: "settler",
|
|
660
|
+
* bundle: "0x000000000000000000000000000000000000bE01",
|
|
661
|
+
* });
|
|
662
|
+
* ```
|
|
663
|
+
*/
|
|
664
|
+
export class ZeroExQuoteUndecodableCalldataError extends ZeroExQuoteError {
|
|
665
|
+
/**
|
|
666
|
+
* @param params - Decoding details.
|
|
667
|
+
* @param params.layer - Calldata layer that does not decode.
|
|
668
|
+
* @param params.bundle - Leverage bundle taking the quote.
|
|
669
|
+
* @param options - Standard error options.
|
|
670
|
+
*/
|
|
671
|
+
constructor(params, options) {
|
|
672
|
+
super(`0x quote ${params.layer === "allowanceHolder" ? "AllowanceHolder" : "Settler"} calldata does not decode, so it cannot be checked for leverage bundle "${params.bundle}". Fetch a new allowance-holder quote.`, options);
|
|
673
|
+
this.layer = params.layer;
|
|
674
|
+
this.bundle = params.bundle;
|
|
675
|
+
this.name = "ZeroExQuoteUndecodableCalldataError";
|
|
676
|
+
}
|
|
677
|
+
/** Calldata layer that does not decode. */
|
|
678
|
+
layer;
|
|
679
|
+
/** Leverage bundle taking the quote. */
|
|
680
|
+
bundle;
|
|
681
|
+
}
|
|
682
|
+
/**
|
|
683
|
+
* Thrown when decoded 0x Settler calldata fills a signed order instead of onchain AMM liquidity: an RFQ order (`RFQ`,
|
|
684
|
+
* `RFQ_VIP`), a Bebop order (`BEBOP`) or any metatransaction action (`METATXN_*`), at the top level of Settler's
|
|
685
|
+
* `execute` or in a candidate of a `SELECT` action at any depth. A signed order carries its maker's own expiry, which
|
|
686
|
+
* can fall before the leverage deadline, so the SDK refuses the quote. `path` names the action: `[1]` is
|
|
687
|
+
* the second top-level action, `[1].candidates[0][2]` the third action of the first candidate of that `SELECT`.
|
|
688
|
+
*
|
|
689
|
+
* @example
|
|
690
|
+
* ```ts
|
|
691
|
+
* import { ZeroExQuoteSignedOrderActionError } from "@morpho-org/morpho-sdk";
|
|
692
|
+
*
|
|
693
|
+
* const error = new ZeroExQuoteSignedOrderActionError({ action: "BEBOP", path: "[1]" });
|
|
694
|
+
* ```
|
|
695
|
+
*/
|
|
696
|
+
export class ZeroExQuoteSignedOrderActionError extends ZeroExQuoteError {
|
|
697
|
+
/**
|
|
698
|
+
* @param params - Signed-order action details.
|
|
699
|
+
* @param params.action - Settler action that fills the signed order.
|
|
700
|
+
* @param params.path - Position of the action in Settler's `execute` actions and `SELECT` candidates.
|
|
701
|
+
*/
|
|
702
|
+
constructor(params) {
|
|
703
|
+
super(`0x quote fills a signed order through Settler action ${params.action} at ${params.path}, whose expiry can fall before the leverage deadline. Fetch a new quote with fetchZeroExQuote, which excludes 0x's short-lived sources.`);
|
|
704
|
+
this.action = params.action;
|
|
705
|
+
this.path = params.path;
|
|
706
|
+
this.name = "ZeroExQuoteSignedOrderActionError";
|
|
707
|
+
}
|
|
708
|
+
/** Settler action that fills the signed order. */
|
|
709
|
+
action;
|
|
710
|
+
/** Position of the action in Settler's `execute` actions and `SELECT` candidates. */
|
|
711
|
+
path;
|
|
712
|
+
}
|
|
713
|
+
/**
|
|
714
|
+
* Thrown when a decoded 0x Settler `BASIC` or `BASIC_PUSH` action, an opaque call to a venue, holds a 32-byte word
|
|
715
|
+
* that looks like a deadline shorter than the leverage deadline: a value from one hour and one day before `deadline`
|
|
716
|
+
* up to, but not including, `deadline`. The SDK cannot tell whether the word is a deadline. 0x writes router
|
|
717
|
+
* deadlines equal to `deadline`, so such a word may be a venue's own expiry, such as a signed quote, after which the
|
|
718
|
+
* swap would revert; the SDK refuses it as one. An ordinary value that falls in that window, such as an amount, is
|
|
719
|
+
* refused too. A new quote clears it only when it returns different calldata: a word that depends on the route,
|
|
720
|
+
* such as a minimum output, may move, but a fixed value such as the caller's own amount keeps refusing for up to a
|
|
721
|
+
* day, so offer a different amount, a retry later, or a no-swap exit. `word` is the index of the word in the call's
|
|
722
|
+
* data after its selector.
|
|
723
|
+
*
|
|
724
|
+
* @example
|
|
725
|
+
* ```ts
|
|
726
|
+
* import { ZeroExQuoteShortTimeBoundError } from "@morpho-org/morpho-sdk";
|
|
727
|
+
*
|
|
728
|
+
* const error = new ZeroExQuoteShortTimeBoundError({
|
|
729
|
+
* path: "[5]",
|
|
730
|
+
* word: 7,
|
|
731
|
+
* value: 1_700_000_060n,
|
|
732
|
+
* deadline: 1_700_003_600n,
|
|
733
|
+
* });
|
|
734
|
+
* ```
|
|
735
|
+
*/
|
|
736
|
+
export class ZeroExQuoteShortTimeBoundError extends ZeroExQuoteError {
|
|
737
|
+
/**
|
|
738
|
+
* @param params - Time bound details.
|
|
739
|
+
* @param params.path - Position of the action in Settler's `execute` actions and `SELECT` candidates.
|
|
740
|
+
* @param params.word - Index of the word in the call's data after its selector.
|
|
741
|
+
* @param params.value - Value of the word, in seconds.
|
|
742
|
+
* @param params.deadline - Leverage bundle deadline the action signs, in seconds.
|
|
743
|
+
*/
|
|
744
|
+
constructor(params) {
|
|
745
|
+
super(`0x quote's Settler call at ${params.path} holds "${params.value}" in word ${params.word}, which looks like a deadline before the bundle deadline "${params.deadline}". Fetch a new quote with fetchZeroExQuote, which excludes 0x's short-lived sources. If the same word is refused again, offer a different amount, retry later, or exit without a swap.`);
|
|
746
|
+
this.path = params.path;
|
|
747
|
+
this.word = params.word;
|
|
748
|
+
this.value = params.value;
|
|
749
|
+
this.deadline = params.deadline;
|
|
750
|
+
this.name = "ZeroExQuoteShortTimeBoundError";
|
|
751
|
+
}
|
|
752
|
+
/** Position of the action in Settler's `execute` actions and `SELECT` candidates. */
|
|
753
|
+
path;
|
|
754
|
+
/** Index of the word in the call's data after its selector. */
|
|
755
|
+
word;
|
|
756
|
+
/** Value of the word, in seconds. */
|
|
757
|
+
value;
|
|
758
|
+
/** Leverage bundle deadline the action signs, in seconds. */
|
|
759
|
+
deadline;
|
|
760
|
+
}
|
|
761
|
+
/**
|
|
762
|
+
* Thrown when decoded 0x Settler calldata holds an action leverage does not accept where it stands: a selector that is
|
|
763
|
+
* no AMM, permit, time-check or opaque-call action of the deployed Settlers, a permit action anywhere but the first
|
|
764
|
+
* top-level action (Settler only runs one there), or a `NATIVE_CHECK` or `ATTEST` inside a `SELECT` candidate.
|
|
765
|
+
* Leverage refuses what it cannot classify rather than pass it unchecked.
|
|
766
|
+
*
|
|
767
|
+
* @example
|
|
768
|
+
* ```ts
|
|
769
|
+
* import { ZeroExQuoteUnsupportedActionError } from "@morpho-org/morpho-sdk";
|
|
770
|
+
*
|
|
771
|
+
* const error = new ZeroExQuoteUnsupportedActionError({ selector: "0xdeadbeef", path: "[2]" });
|
|
772
|
+
* ```
|
|
773
|
+
*/
|
|
774
|
+
export class ZeroExQuoteUnsupportedActionError extends ZeroExQuoteError {
|
|
775
|
+
/**
|
|
776
|
+
* @param params - Action details.
|
|
777
|
+
* @param params.selector - Four-byte selector of the action.
|
|
778
|
+
* @param params.path - Position of the action in Settler's `execute` actions and `SELECT` candidates.
|
|
779
|
+
*/
|
|
780
|
+
constructor(params) {
|
|
781
|
+
super(`0x quote's Settler action ${params.selector} at ${params.path} is not one leverage accepts there. Fetch a new quote with fetchZeroExQuote.`);
|
|
782
|
+
this.selector = params.selector;
|
|
783
|
+
this.path = params.path;
|
|
784
|
+
this.name = "ZeroExQuoteUnsupportedActionError";
|
|
785
|
+
}
|
|
786
|
+
/** Four-byte selector of the action. */
|
|
787
|
+
selector;
|
|
788
|
+
/** Position of the action in Settler's `execute` actions and `SELECT` candidates. */
|
|
789
|
+
path;
|
|
790
|
+
}
|
|
791
|
+
/**
|
|
792
|
+
* Thrown when a decoded Settler time bound does not match the leverage bundle deadline the action signs: the permit
|
|
793
|
+
* of the first top-level action (`TRANSFER_FROM` or a `*_VIP` action) must equal it, and a top-level `NATIVE_CHECK`
|
|
794
|
+
* or `ATTEST` deadline must be at least it. Settler reverts once either has passed, so the quote must be fetched with
|
|
795
|
+
* `deadline` set to the bundle deadline; 0x otherwise sets the permit deadline to its quote block's timestamp plus
|
|
796
|
+
* 300 seconds.
|
|
797
|
+
*
|
|
798
|
+
* @example
|
|
799
|
+
* ```ts
|
|
800
|
+
* import { ZeroExQuoteDeadlineMismatchError } from "@morpho-org/morpho-sdk";
|
|
801
|
+
*
|
|
802
|
+
* const error = new ZeroExQuoteDeadlineMismatchError({
|
|
803
|
+
* expected: 1_700_003_600n,
|
|
804
|
+
* actual: 1_700_000_300n,
|
|
805
|
+
* action: "UNISWAPV3_VIP",
|
|
806
|
+
* });
|
|
807
|
+
* ```
|
|
808
|
+
*/
|
|
809
|
+
export class ZeroExQuoteDeadlineMismatchError extends ZeroExQuoteError {
|
|
810
|
+
/**
|
|
811
|
+
* @param params - Deadline comparison.
|
|
812
|
+
* @param params.expected - Leverage bundle deadline the action signs, in seconds.
|
|
813
|
+
* @param params.actual - Deadline in the Settler action, in seconds.
|
|
814
|
+
* @param params.action - Settler action that carries the deadline.
|
|
815
|
+
*/
|
|
816
|
+
constructor(params) {
|
|
817
|
+
super(`0x quote's Settler ${params.action} deadline is "${params.actual}", which does not match the bundle deadline "${params.expected}". Fetch a new quote with fetchZeroExQuote and deadline set to the bundle deadline.`);
|
|
818
|
+
this.expected = params.expected;
|
|
819
|
+
this.actual = params.actual;
|
|
820
|
+
this.action = params.action;
|
|
821
|
+
this.name = "ZeroExQuoteDeadlineMismatchError";
|
|
822
|
+
}
|
|
823
|
+
/** Leverage bundle deadline the action signs, in seconds. */
|
|
824
|
+
expected;
|
|
825
|
+
/** Deadline in the Settler action, in seconds. */
|
|
826
|
+
actual;
|
|
827
|
+
/** Settler action that carries the deadline. */
|
|
828
|
+
action;
|
|
829
|
+
}
|
|
830
|
+
/**
|
|
831
|
+
* Thrown when the 0x quote was requested for another transaction origin than the action expects: the
|
|
832
|
+
* `txOrigin` the action is built with, or the user when none is given.
|
|
833
|
+
*
|
|
834
|
+
* @example
|
|
835
|
+
* ```ts
|
|
836
|
+
* import { ZeroExQuoteTxOriginMismatchError } from "@morpho-org/morpho-sdk";
|
|
837
|
+
*
|
|
838
|
+
* const error = new ZeroExQuoteTxOriginMismatchError({
|
|
839
|
+
* expected: userAddress,
|
|
840
|
+
* actual: quotedTxOrigin,
|
|
841
|
+
* });
|
|
842
|
+
* ```
|
|
843
|
+
*/
|
|
844
|
+
export class ZeroExQuoteTxOriginMismatchError extends ZeroExQuoteError {
|
|
845
|
+
/**
|
|
846
|
+
* @param params - txOrigin comparison.
|
|
847
|
+
* @param params.expected - Expected transaction origin: the action's `txOrigin`, or the user by default.
|
|
848
|
+
* @param params.actual - txOrigin the quote was requested for.
|
|
849
|
+
*/
|
|
850
|
+
constructor(params) {
|
|
851
|
+
super(`0x quote txOrigin "${params.actual}" is not the expected transaction origin "${params.expected}". Fetch a new quote with the same txOrigin the action is built with (the user by default).`);
|
|
852
|
+
this.expected = params.expected;
|
|
853
|
+
this.actual = params.actual;
|
|
854
|
+
this.name = "ZeroExQuoteTxOriginMismatchError";
|
|
855
|
+
}
|
|
856
|
+
/** Expected transaction origin: the action's `txOrigin`, or the user by default. */
|
|
857
|
+
expected;
|
|
858
|
+
/** txOrigin the quote was requested for. */
|
|
859
|
+
actual;
|
|
860
|
+
}
|
|
861
|
+
/**
|
|
862
|
+
* Thrown when a leverage action's optional `txOrigin` is not a valid address. No new quote clears it: pass a
|
|
863
|
+
* valid address, or omit it to use the user.
|
|
864
|
+
*
|
|
865
|
+
* @example
|
|
866
|
+
* ```ts
|
|
867
|
+
* import { ZeroExInvalidTxOriginError } from "@morpho-org/morpho-sdk";
|
|
868
|
+
*
|
|
869
|
+
* const error = new ZeroExInvalidTxOriginError("0x1234");
|
|
870
|
+
* ```
|
|
871
|
+
*/
|
|
872
|
+
export class ZeroExInvalidTxOriginError extends ZeroExQuoteError {
|
|
873
|
+
/**
|
|
874
|
+
* @param txOrigin - Value passed as `txOrigin`.
|
|
875
|
+
*/
|
|
876
|
+
constructor(txOrigin) {
|
|
877
|
+
super(`txOrigin ${typeof txOrigin === "string" ? `"${txOrigin}"` : `of type ${typeof txOrigin}`} is not a valid address. Pass the sender of the outer onchain transaction (a Safe's executing owner or an ERC-4337 bundler), or omit it to use the user.`);
|
|
878
|
+
this.txOrigin = txOrigin;
|
|
879
|
+
this.name = "ZeroExInvalidTxOriginError";
|
|
880
|
+
}
|
|
881
|
+
/** Value passed as `txOrigin`. */
|
|
882
|
+
txOrigin;
|
|
883
|
+
}
|
|
884
|
+
/**
|
|
885
|
+
* Thrown when an exact-output quote is requested for a native and wrapped-native pair, which 0x does not support.
|
|
886
|
+
*
|
|
887
|
+
* @example
|
|
888
|
+
* ```ts
|
|
889
|
+
* import { ZeroExExactOutputUnsupportedError } from "@morpho-org/morpho-sdk";
|
|
890
|
+
* import { NATIVE_ADDRESS } from "@morpho-org/morpho-sdk/addresses";
|
|
891
|
+
*
|
|
892
|
+
* const error = new ZeroExExactOutputUnsupportedError({
|
|
893
|
+
* sellToken: NATIVE_ADDRESS,
|
|
894
|
+
* buyToken: weth,
|
|
895
|
+
* });
|
|
896
|
+
* ```
|
|
897
|
+
*/
|
|
898
|
+
export class ZeroExExactOutputUnsupportedError extends ZeroExQuoteError {
|
|
899
|
+
/**
|
|
900
|
+
* @param params - Pair details.
|
|
901
|
+
* @param params.sellToken - Token the swap sells.
|
|
902
|
+
* @param params.buyToken - Token the swap buys.
|
|
903
|
+
*/
|
|
904
|
+
constructor(params) {
|
|
905
|
+
super(`0x does not support exact-output quotes between "${params.sellToken}" and "${params.buyToken}", a native and wrapped-native pair. Use a market whose tokens are not a native and wrapped-native pair. ${CLOSE_NO_SWAP_EXITS}`);
|
|
906
|
+
this.sellToken = params.sellToken;
|
|
907
|
+
this.buyToken = params.buyToken;
|
|
908
|
+
this.name = "ZeroExExactOutputUnsupportedError";
|
|
909
|
+
}
|
|
910
|
+
/** Token the swap sells. */
|
|
911
|
+
sellToken;
|
|
912
|
+
/** Token the swap buys. */
|
|
913
|
+
buyToken;
|
|
914
|
+
}
|
|
915
|
+
/**
|
|
916
|
+
* Thrown when a 0x swap is requested for a value that is not a leverage swap kind, before any request or
|
|
917
|
+
* validation runs.
|
|
918
|
+
*
|
|
919
|
+
* @example
|
|
920
|
+
* ```ts
|
|
921
|
+
* import { ZeroExUnknownSwapKindError } from "@morpho-org/morpho-sdk";
|
|
922
|
+
*
|
|
923
|
+
* const error = new ZeroExUnknownSwapKindError({ kind: "leverage" });
|
|
924
|
+
* ```
|
|
925
|
+
*/
|
|
926
|
+
export class ZeroExUnknownSwapKindError extends ZeroExQuoteError {
|
|
927
|
+
/**
|
|
928
|
+
* @param params - Swap kind details.
|
|
929
|
+
* @param params.kind - Value supplied as the swap kind.
|
|
930
|
+
*/
|
|
931
|
+
constructor(params) {
|
|
932
|
+
super(`Swap kind ${typeof params.kind === "string" ? `"${params.kind}"` : `of type ${typeof params.kind}`} is not a leverage swap kind. Use "lever", "delever" or "closeKeepCollateral".`);
|
|
933
|
+
this.kind = params.kind;
|
|
934
|
+
this.name = "ZeroExUnknownSwapKindError";
|
|
935
|
+
}
|
|
936
|
+
/** Value supplied as the swap kind. */
|
|
937
|
+
kind;
|
|
938
|
+
}
|
|
939
|
+
/**
|
|
940
|
+
* Thrown when the 0x quote request fails: a network error or a non-2xx response from the 0x API.
|
|
941
|
+
*
|
|
942
|
+
* @example
|
|
943
|
+
* ```ts
|
|
944
|
+
* import { ZeroExQuoteRequestError } from "@morpho-org/morpho-sdk";
|
|
945
|
+
*
|
|
946
|
+
* const error = new ZeroExQuoteRequestError({ status: 400, code: "INPUT_INVALID" });
|
|
947
|
+
* ```
|
|
948
|
+
*/
|
|
949
|
+
export class ZeroExQuoteRequestError extends ZeroExQuoteError {
|
|
950
|
+
/**
|
|
951
|
+
* @param params - Request failure.
|
|
952
|
+
* @param params.status - HTTP status, or `null` when no response arrived or a 2xx response's body failed to
|
|
953
|
+
* download. A non-2xx response keeps its status, with `code: null` when its body could not be read.
|
|
954
|
+
* @param params.code - 0x error name from the response body, or `null` when it has none.
|
|
955
|
+
* @param options - Standard error options; `cause` carries the underlying failure.
|
|
956
|
+
*/
|
|
957
|
+
constructor(params, options) {
|
|
958
|
+
const retryable = params.status === null || params.status === 429 || params.status >= 500;
|
|
959
|
+
super(`0x quote request failed with status "${params.status ?? "no response or an incomplete response"}" and code "${params.code ?? "none"}". ${retryable ? "Fetch the quote again." : "Check the API key and the request, then fetch the quote again."}`, options);
|
|
960
|
+
this.status = params.status;
|
|
961
|
+
this.code = params.code;
|
|
962
|
+
this.name = "ZeroExQuoteRequestError";
|
|
963
|
+
}
|
|
964
|
+
/** HTTP status, or `null` when no response arrived or a 2xx response's body failed to download. */
|
|
965
|
+
status;
|
|
966
|
+
/** 0x error name from the response body, or `null` when it has none. */
|
|
967
|
+
code;
|
|
968
|
+
}
|
|
969
|
+
/**
|
|
970
|
+
* Thrown when 0x reports no liquidity for the requested swap.
|
|
971
|
+
*
|
|
972
|
+
* @example
|
|
973
|
+
* ```ts
|
|
974
|
+
* import { ZeroExQuoteNoLiquidityError } from "@morpho-org/morpho-sdk";
|
|
975
|
+
*
|
|
976
|
+
* const error = new ZeroExQuoteNoLiquidityError({
|
|
977
|
+
* sellToken: weth,
|
|
978
|
+
* buyToken: usdc,
|
|
979
|
+
* });
|
|
980
|
+
* ```
|
|
981
|
+
*/
|
|
982
|
+
export class ZeroExQuoteNoLiquidityError extends ZeroExQuoteError {
|
|
983
|
+
/**
|
|
984
|
+
* @param params - Pair details.
|
|
985
|
+
* @param params.sellToken - Token the swap sells.
|
|
986
|
+
* @param params.buyToken - Token the swap buys.
|
|
987
|
+
*/
|
|
988
|
+
constructor(params) {
|
|
989
|
+
super(`0x has no liquidity to swap "${params.sellToken}" for "${params.buyToken}". Try a smaller amount or exit without a swap.`);
|
|
990
|
+
this.sellToken = params.sellToken;
|
|
991
|
+
this.buyToken = params.buyToken;
|
|
992
|
+
this.name = "ZeroExQuoteNoLiquidityError";
|
|
993
|
+
}
|
|
994
|
+
/** Token the swap sells. */
|
|
995
|
+
sellToken;
|
|
996
|
+
/** Token the swap buys. */
|
|
997
|
+
buyToken;
|
|
998
|
+
}
|
|
999
|
+
/**
|
|
1000
|
+
* Thrown when a 0x quote response does not have the documented allowance-holder quote shape, or does not answer the request that was sent.
|
|
1001
|
+
*
|
|
1002
|
+
* @example
|
|
1003
|
+
* ```ts
|
|
1004
|
+
* import { ZeroExQuoteMalformedResponseError } from "@morpho-org/morpho-sdk";
|
|
1005
|
+
*
|
|
1006
|
+
* const error = new ZeroExQuoteMalformedResponseError({ reason: "buyAmount is not an integer string" });
|
|
1007
|
+
* ```
|
|
1008
|
+
*/
|
|
1009
|
+
export class ZeroExQuoteMalformedResponseError extends ZeroExQuoteError {
|
|
1010
|
+
/**
|
|
1011
|
+
* @param params - Parsing details.
|
|
1012
|
+
* @param params.reason - What is wrong with the response.
|
|
1013
|
+
* @param options - Standard error options; `cause` carries the underlying failure.
|
|
1014
|
+
*/
|
|
1015
|
+
constructor(params, options) {
|
|
1016
|
+
super(`0x quote response is malformed: ${params.reason}. Fetch a new allowance-holder quote.`, options);
|
|
1017
|
+
this.reason = params.reason;
|
|
1018
|
+
this.name = "ZeroExQuoteMalformedResponseError";
|
|
1019
|
+
}
|
|
1020
|
+
/** What is wrong with the response. */
|
|
1021
|
+
reason;
|
|
1022
|
+
}
|
|
1023
|
+
/**
|
|
1024
|
+
* Thrown when a leverage action that swaps is built without a 0x quote, such as a Close of a
|
|
1025
|
+
* position that still has debt.
|
|
1026
|
+
*
|
|
1027
|
+
* @example
|
|
1028
|
+
* ```ts
|
|
1029
|
+
* import { ZeroExQuoteRequiredError } from "@morpho-org/morpho-sdk";
|
|
1030
|
+
*
|
|
1031
|
+
* const error = new ZeroExQuoteRequiredError({ operation: "close" });
|
|
1032
|
+
* ```
|
|
1033
|
+
*/
|
|
1034
|
+
export class ZeroExQuoteRequiredError extends ZeroExQuoteError {
|
|
1035
|
+
/**
|
|
1036
|
+
* @param params - Refused action.
|
|
1037
|
+
* @param params.operation - Leverage action that needs a quote.
|
|
1038
|
+
*/
|
|
1039
|
+
constructor(params) {
|
|
1040
|
+
super(`${params.operation} needs a 0x quote ${params.operation === "lever" ? "to buy collateral with the borrowed assets" : "to swap collateral for the debt it repays"}. Fetch a quote with fetchZeroExQuote and build again.${params.operation === "close" ? ` ${CLOSE_NO_SWAP_EXITS}` : ""}`);
|
|
1041
|
+
this.operation = params.operation;
|
|
1042
|
+
this.name = "ZeroExQuoteRequiredError";
|
|
1043
|
+
}
|
|
1044
|
+
/** Leverage action that needs a quote. */
|
|
1045
|
+
operation;
|
|
1046
|
+
}
|
|
1047
|
+
/**
|
|
1048
|
+
* Thrown when the caller's slippage exceeds the SDK's execution-loss limit for the action.
|
|
1049
|
+
*
|
|
1050
|
+
* @example
|
|
1051
|
+
* ```ts
|
|
1052
|
+
* import { LeverageSlippageAboveLimitError } from "@morpho-org/morpho-sdk";
|
|
1053
|
+
*
|
|
1054
|
+
* const error = new LeverageSlippageAboveLimitError({
|
|
1055
|
+
* operation: "lever",
|
|
1056
|
+
* slippage: 20_000_000_000_000_000n,
|
|
1057
|
+
* max: 10_000_000_000_000_000n,
|
|
1058
|
+
* });
|
|
1059
|
+
* ```
|
|
1060
|
+
*/
|
|
1061
|
+
export class LeverageSlippageAboveLimitError extends LeverageLimitError {
|
|
1062
|
+
/**
|
|
1063
|
+
* @param params - Slippage comparison, WAD-scaled.
|
|
1064
|
+
* @param params.operation - Leverage action refused.
|
|
1065
|
+
* @param params.slippage - Slippage supplied by the caller.
|
|
1066
|
+
* @param params.max - Largest slippage the SDK accepts for the action.
|
|
1067
|
+
*/
|
|
1068
|
+
constructor(params) {
|
|
1069
|
+
super(`${params.operation} slippage "${params.slippage}" exceeds the SDK limit "${params.max}" (WAD). Lower the slippage.`);
|
|
1070
|
+
this.operation = params.operation;
|
|
1071
|
+
this.slippage = params.slippage;
|
|
1072
|
+
this.max = params.max;
|
|
1073
|
+
this.name = "LeverageSlippageAboveLimitError";
|
|
1074
|
+
}
|
|
1075
|
+
/** Leverage action refused. */
|
|
1076
|
+
operation;
|
|
1077
|
+
/** Slippage supplied by the caller, WAD-scaled. */
|
|
1078
|
+
slippage;
|
|
1079
|
+
/** Largest slippage the SDK accepts for the action, WAD-scaled. */
|
|
1080
|
+
max;
|
|
1081
|
+
}
|
|
1082
|
+
/**
|
|
1083
|
+
* Thrown when the 0x quote was fetched with a slippage other than the one the caller passes. The
|
|
1084
|
+
* quote and the build must use the same slippage, or the transaction would carry a tolerance the
|
|
1085
|
+
* caller did not choose. For an exact-output quote (Lever, Close) the signed swap
|
|
1086
|
+
* cap is the quote's `maxSellAmount`, which carries the quote's own slippage. A Delever quote's
|
|
1087
|
+
* `minBuyAmount` below the caller's slippage throws {@link ZeroExQuoteMinBuyAmountBelowSlippageError} instead.
|
|
1088
|
+
*
|
|
1089
|
+
* @example
|
|
1090
|
+
* ```ts
|
|
1091
|
+
* import { LeverageQuoteSlippageMismatchError } from "@morpho-org/morpho-sdk";
|
|
1092
|
+
*
|
|
1093
|
+
* const error = new LeverageQuoteSlippageMismatchError({
|
|
1094
|
+
* operation: "lever",
|
|
1095
|
+
* slippage: 5_000_000_000_000_000n,
|
|
1096
|
+
* quoteSlippage: 8_000_000_000_000_000n,
|
|
1097
|
+
* });
|
|
1098
|
+
* ```
|
|
1099
|
+
*/
|
|
1100
|
+
export class LeverageQuoteSlippageMismatchError extends LeverageLimitError {
|
|
1101
|
+
/**
|
|
1102
|
+
* @param params - Slippage comparison, WAD-scaled.
|
|
1103
|
+
* @param params.operation - Leverage action refused.
|
|
1104
|
+
* @param params.slippage - Slippage supplied by the caller.
|
|
1105
|
+
* @param params.quoteSlippage - Slippage the quote was fetched with.
|
|
1106
|
+
*/
|
|
1107
|
+
constructor(params) {
|
|
1108
|
+
super(`${params.operation} quote was fetched with slippage "${params.quoteSlippage}", not the caller's "${params.slippage}" (WAD). Fetch the quote with the same slippage.`);
|
|
1109
|
+
this.operation = params.operation;
|
|
1110
|
+
this.slippage = params.slippage;
|
|
1111
|
+
this.quoteSlippage = params.quoteSlippage;
|
|
1112
|
+
this.name = "LeverageQuoteSlippageMismatchError";
|
|
1113
|
+
}
|
|
1114
|
+
/** Leverage action refused. */
|
|
1115
|
+
operation;
|
|
1116
|
+
/** Slippage supplied by the caller, WAD-scaled. */
|
|
1117
|
+
slippage;
|
|
1118
|
+
/** Slippage the quote was fetched with, WAD-scaled. */
|
|
1119
|
+
quoteSlippage;
|
|
1120
|
+
}
|
|
1121
|
+
/**
|
|
1122
|
+
* Thrown when a Lever's or a Delever's position LTV at the worst fill exceeds a bound: the action's signed
|
|
1123
|
+
* `maxLtv` (`check: "maxLtv"`), or the market LLTV at Blue's health check, which the contract
|
|
1124
|
+
* runs before repaying any surplus (`check: "health"`). Close never throws it: it signs no `maxLtv`, and Blue
|
|
1125
|
+
* clears the borrow shares before the repay callback withdraws the collateral, so that withdrawal's health check
|
|
1126
|
+
* always passes.
|
|
1127
|
+
*
|
|
1128
|
+
* @example
|
|
1129
|
+
* ```ts
|
|
1130
|
+
* import { LeverageWorstFillLtvError } from "@morpho-org/morpho-sdk";
|
|
1131
|
+
*
|
|
1132
|
+
* const error = new LeverageWorstFillLtvError({
|
|
1133
|
+
* operation: "lever",
|
|
1134
|
+
* check: "maxLtv",
|
|
1135
|
+
* ltv: 860_000_000_000_000_000n,
|
|
1136
|
+
* maxLtv: 855_000_000_000_000_000n,
|
|
1137
|
+
* });
|
|
1138
|
+
* ```
|
|
1139
|
+
*/
|
|
1140
|
+
export class LeverageWorstFillLtvError extends LeverageLimitError {
|
|
1141
|
+
/**
|
|
1142
|
+
* @param params - LTV comparison, WAD-scaled.
|
|
1143
|
+
* @param params.operation - Leverage action refused.
|
|
1144
|
+
* @param params.check - Bound exceeded: the signed `maxLtv` or Blue's mid-flow health check.
|
|
1145
|
+
* @param params.ltv - Position LTV at the worst fill.
|
|
1146
|
+
* @param params.maxLtv - Bound for that check.
|
|
1147
|
+
*/
|
|
1148
|
+
constructor(params) {
|
|
1149
|
+
const bound = params.check === "maxLtv"
|
|
1150
|
+
? "signed maxLtv"
|
|
1151
|
+
: "LLTV at Blue's health check";
|
|
1152
|
+
const fallback = {
|
|
1153
|
+
lever: "Lever a smaller amount or add collateral.",
|
|
1154
|
+
delever: "Fetch a better quote, or reduce risk without a swap with repay or supplyCollateral.",
|
|
1155
|
+
}[params.operation];
|
|
1156
|
+
super(`${params.operation} worst-fill LTV "${params.ltv}" exceeds the ${bound} "${params.maxLtv}" (WAD). ${fallback}`);
|
|
1157
|
+
this.operation = params.operation;
|
|
1158
|
+
this.check = params.check;
|
|
1159
|
+
this.ltv = params.ltv;
|
|
1160
|
+
this.maxLtv = params.maxLtv;
|
|
1161
|
+
this.name = "LeverageWorstFillLtvError";
|
|
1162
|
+
}
|
|
1163
|
+
/** Leverage action refused. */
|
|
1164
|
+
operation;
|
|
1165
|
+
/** Bound exceeded: the signed `maxLtv` or Blue's mid-flow health check. */
|
|
1166
|
+
check;
|
|
1167
|
+
/** Position LTV at the worst fill, WAD-scaled. */
|
|
1168
|
+
ltv;
|
|
1169
|
+
/** Bound for that check, WAD-scaled. */
|
|
1170
|
+
maxLtv;
|
|
1171
|
+
}
|
|
1172
|
+
/**
|
|
1173
|
+
* Thrown when a Lever's worst swap price, the signed `maxSwapIn` per `minCollateralToBuy`, is more than
|
|
1174
|
+
* `LEVERAGE_MAX_ORACLE_DEVIATION` (15%) above the market's oracle price. A price below the oracle favours the user
|
|
1175
|
+
* and is never refused. Both prices are
|
|
1176
|
+
* loan-token units per collateral-token unit scaled by `ORACLE_PRICE_SCALE` (1e36), as Morpho oracles report
|
|
1177
|
+
* them, so token decimals are already accounted for. Only Lever checks it; Delever and Close do not. The worst price
|
|
1178
|
+
* uses `maxSwapIn`, which already includes the caller's slippage, so the room left for a gap between 0x's price and
|
|
1179
|
+
* the oracle is about 15% minus that slippage (5% at the 10% ceiling).
|
|
1180
|
+
*
|
|
1181
|
+
* @example
|
|
1182
|
+
* ```ts
|
|
1183
|
+
* import { LeverageOraclePriceDeviationError } from "@morpho-org/morpho-sdk";
|
|
1184
|
+
*
|
|
1185
|
+
* const error = new LeverageOraclePriceDeviationError({
|
|
1186
|
+
* quotePrice: 3_500n * 10n ** 24n,
|
|
1187
|
+
* oraclePrice: 3_000n * 10n ** 24n,
|
|
1188
|
+
* deviation: 166_666_666_666_666_667n,
|
|
1189
|
+
* max: 150_000_000_000_000_000n,
|
|
1190
|
+
* });
|
|
1191
|
+
* ```
|
|
1192
|
+
*/
|
|
1193
|
+
export class LeverageOraclePriceDeviationError extends LeverageLimitError {
|
|
1194
|
+
/**
|
|
1195
|
+
* @param params - Price comparison.
|
|
1196
|
+
* @param params.quotePrice - Worst swap price the Lever signs, scaled by `ORACLE_PRICE_SCALE`, rounded down.
|
|
1197
|
+
* @param params.oraclePrice - Market oracle price at the data block, scaled by `ORACLE_PRICE_SCALE`.
|
|
1198
|
+
* @param params.deviation - How far the quote price is above the oracle price, relative to it, WAD-scaled, rounded up.
|
|
1199
|
+
* @param params.max - Largest deviation accepted, WAD-scaled.
|
|
1200
|
+
*/
|
|
1201
|
+
constructor(params) {
|
|
1202
|
+
super(`lever quote price "${params.quotePrice}" exceeds the oracle price "${params.oraclePrice}" by "${params.deviation}", above the limit "${params.max}" (WAD). Fetch a new quote, or lever a smaller amount.`);
|
|
1203
|
+
this.quotePrice = params.quotePrice;
|
|
1204
|
+
this.oraclePrice = params.oraclePrice;
|
|
1205
|
+
this.deviation = params.deviation;
|
|
1206
|
+
this.max = params.max;
|
|
1207
|
+
this.name = "LeverageOraclePriceDeviationError";
|
|
1208
|
+
}
|
|
1209
|
+
/** Worst swap price the Lever signs, scaled by `ORACLE_PRICE_SCALE`. */
|
|
1210
|
+
quotePrice;
|
|
1211
|
+
/** Market oracle price at the data block, scaled by `ORACLE_PRICE_SCALE`. */
|
|
1212
|
+
oraclePrice;
|
|
1213
|
+
/** Deviation from the oracle price, WAD-scaled. */
|
|
1214
|
+
deviation;
|
|
1215
|
+
/** Largest deviation accepted, WAD-scaled. */
|
|
1216
|
+
max;
|
|
1217
|
+
}
|
|
1218
|
+
/**
|
|
1219
|
+
* Thrown when a Lever, a Delever or a Close with debt is built from leverage data without a usable oracle price: the
|
|
1220
|
+
* oracle call reverted or returned zero. Without it the SDK can derive neither the oracle deviation nor `maxLtv` and
|
|
1221
|
+
* the worst-fill checks, so it never signs such a swap. A Close without debt refuses with `LeverageCloseWithoutDebtError` first. For an
|
|
1222
|
+
* exit, the fallback is a no-swap exit, which needs no price once the debt is repaid.
|
|
1223
|
+
*
|
|
1224
|
+
* @example
|
|
1225
|
+
* ```ts
|
|
1226
|
+
* import { LeverageOraclePriceUnavailableError } from "@morpho-org/morpho-sdk";
|
|
1227
|
+
*
|
|
1228
|
+
* // `marketParams`: the market's `MarketParams`.
|
|
1229
|
+
* const error = new LeverageOraclePriceUnavailableError({
|
|
1230
|
+
* operation: "delever",
|
|
1231
|
+
* marketId: marketParams.id,
|
|
1232
|
+
* price: undefined,
|
|
1233
|
+
* });
|
|
1234
|
+
* ```
|
|
1235
|
+
*/
|
|
1236
|
+
export class LeverageOraclePriceUnavailableError extends LeverageLimitError {
|
|
1237
|
+
/**
|
|
1238
|
+
* @param params - Missing price.
|
|
1239
|
+
* @param params.operation - Leverage action refused.
|
|
1240
|
+
* @param params.marketId - Market whose oracle price is missing.
|
|
1241
|
+
* @param params.price - Price the leverage data holds: `undefined` when the oracle call reverted, else zero.
|
|
1242
|
+
*/
|
|
1243
|
+
constructor(params) {
|
|
1244
|
+
super(`${params.operation} needs the oracle price of market "${params.marketId}", but the leverage data holds "${params.price}". ${params.operation === "lever" ? "Read the leverage data again once the oracle answers." : "Exit without a swap: repay or supplyCollateral to reduce risk, repayWithdrawCollateral to leave the position."}`);
|
|
1245
|
+
this.operation = params.operation;
|
|
1246
|
+
this.marketId = params.marketId;
|
|
1247
|
+
this.price = params.price;
|
|
1248
|
+
this.name = "LeverageOraclePriceUnavailableError";
|
|
1249
|
+
}
|
|
1250
|
+
/** Leverage action refused. */
|
|
1251
|
+
operation;
|
|
1252
|
+
/** Market whose oracle price is missing. */
|
|
1253
|
+
marketId;
|
|
1254
|
+
/** Price the leverage data holds: `undefined` when the oracle call reverted, else zero. */
|
|
1255
|
+
price;
|
|
1256
|
+
}
|
|
1257
|
+
/**
|
|
1258
|
+
* Thrown when a Close's quote-based `minLoanOut` floor does not exceed the debt, or is below the debt
|
|
1259
|
+
* plus the interest buffer. The debt is never used as the floor.
|
|
1260
|
+
*
|
|
1261
|
+
* @example
|
|
1262
|
+
* ```ts
|
|
1263
|
+
* import { LeverageCloseFloorBelowDebtPlusBufferError } from "@morpho-org/morpho-sdk";
|
|
1264
|
+
*
|
|
1265
|
+
* const error = new LeverageCloseFloorBelowDebtPlusBufferError({ floor: 100n, debt: 100n });
|
|
1266
|
+
* ```
|
|
1267
|
+
*/
|
|
1268
|
+
export class LeverageCloseFloorBelowDebtPlusBufferError extends LeverageLimitError {
|
|
1269
|
+
/**
|
|
1270
|
+
* @param params - Floor comparison, in loan-token assets.
|
|
1271
|
+
* @param params.floor - Quote-based `minLoanOut`.
|
|
1272
|
+
* @param params.debt - Position's debt.
|
|
1273
|
+
* @param params.interestBuffer - Interest buffer the floor must also cover.
|
|
1274
|
+
*/
|
|
1275
|
+
constructor(params) {
|
|
1276
|
+
super(`Close output floor "${params.floor}" ${params.interestBuffer === undefined ? `does not exceed debt "${params.debt}"` : `is below debt "${params.debt}" plus the interest buffer "${params.interestBuffer}"`}. Fetch a quote buying exactly the accrued debt plus the interest buffer, from the current leverage data. ${CLOSE_NO_SWAP_EXITS}`);
|
|
1277
|
+
this.floor = params.floor;
|
|
1278
|
+
this.debt = params.debt;
|
|
1279
|
+
this.interestBuffer = params.interestBuffer;
|
|
1280
|
+
this.name = "LeverageCloseFloorBelowDebtPlusBufferError";
|
|
1281
|
+
}
|
|
1282
|
+
/** Quote-based `minLoanOut`, in loan-token assets. */
|
|
1283
|
+
floor;
|
|
1284
|
+
/** Position's debt, in loan-token assets. */
|
|
1285
|
+
debt;
|
|
1286
|
+
/**
|
|
1287
|
+
* Interest buffer the floor must also cover, in loan-token assets (`computeCloseInterestBuffer`).
|
|
1288
|
+
* `computeCloseLimits` and `blueClose` always set it.
|
|
1289
|
+
*/
|
|
1290
|
+
interestBuffer;
|
|
1291
|
+
}
|
|
1292
|
+
/**
|
|
1293
|
+
* Thrown when a keep-collateral Close's exact-output quote buys more than the debt plus the
|
|
1294
|
+
* interest buffer. The extra loan token would come back to the user, but selling the collateral
|
|
1295
|
+
* for it costs price impact and fees, and gives up the collateral exposure the mode keeps. The
|
|
1296
|
+
* required amount moves whenever the leverage data does: a repay, accrued interest or a change in the borrow
|
|
1297
|
+
* rate (which scales the buffer) can raise or lower it, and a quote sized for another position usually
|
|
1298
|
+
* won't match it. Fetch a new quote for exactly `debt + interestBuffer` from the current leverageData.
|
|
1299
|
+
*
|
|
1300
|
+
* @example
|
|
1301
|
+
* ```ts
|
|
1302
|
+
* import { LeverageCloseBuyAboveDebtError } from "@morpho-org/morpho-sdk";
|
|
1303
|
+
*
|
|
1304
|
+
* const error = new LeverageCloseBuyAboveDebtError({ buyAmount: 70n, debt: 20n, interestBuffer: 1n });
|
|
1305
|
+
* ```
|
|
1306
|
+
*/
|
|
1307
|
+
export class LeverageCloseBuyAboveDebtError extends LeverageLimitError {
|
|
1308
|
+
/**
|
|
1309
|
+
* @param params - Buy comparison, in loan-token assets.
|
|
1310
|
+
* @param params.buyAmount - Quote's exact-output `buyAmount`.
|
|
1311
|
+
* @param params.debt - Position's accrued debt.
|
|
1312
|
+
* @param params.interestBuffer - Interest buffer from `computeCloseInterestBuffer`.
|
|
1313
|
+
*/
|
|
1314
|
+
constructor(params) {
|
|
1315
|
+
super(`Keep-collateral Close quote buys "${params.buyAmount}" but the debt plus the interest buffer is "${params.debt + params.interestBuffer}". Fetch a quote buying exactly that amount. ${CLOSE_NO_SWAP_EXITS}`);
|
|
1316
|
+
this.buyAmount = params.buyAmount;
|
|
1317
|
+
this.debt = params.debt;
|
|
1318
|
+
this.interestBuffer = params.interestBuffer;
|
|
1319
|
+
this.name = "LeverageCloseBuyAboveDebtError";
|
|
1320
|
+
}
|
|
1321
|
+
/** Quote's exact-output `buyAmount`, in loan-token assets. */
|
|
1322
|
+
buyAmount;
|
|
1323
|
+
/** Position's accrued debt, in loan-token assets. */
|
|
1324
|
+
debt;
|
|
1325
|
+
/** Interest buffer the quote may add to the debt, in loan-token assets. */
|
|
1326
|
+
interestBuffer;
|
|
1327
|
+
}
|
|
1328
|
+
/**
|
|
1329
|
+
* Thrown when a Close targets a position with neither debt nor collateral. The bundle would
|
|
1330
|
+
* withdraw zero collateral, which Morpho Blue rejects, so the transaction could only revert.
|
|
1331
|
+
*
|
|
1332
|
+
* @example
|
|
1333
|
+
* ```ts
|
|
1334
|
+
* import type { MarketId } from "@morpho-org/blue-sdk";
|
|
1335
|
+
* import { LeverageCloseEmptyPositionError } from "@morpho-org/morpho-sdk";
|
|
1336
|
+
*
|
|
1337
|
+
* const error = new LeverageCloseEmptyPositionError({
|
|
1338
|
+
* marketId: "0x3b3769cfca57be2eaed03fcc5299c25691b77781a1e124e7a8d520eb9a7eabb5" as MarketId,
|
|
1339
|
+
* user: userAddress,
|
|
1340
|
+
* });
|
|
1341
|
+
* ```
|
|
1342
|
+
*/
|
|
1343
|
+
export class LeverageCloseEmptyPositionError extends LeverageLimitError {
|
|
1344
|
+
/**
|
|
1345
|
+
* @param params - The empty position.
|
|
1346
|
+
* @param params.marketId - Market of the position.
|
|
1347
|
+
* @param params.user - Owner of the position.
|
|
1348
|
+
*/
|
|
1349
|
+
constructor(params) {
|
|
1350
|
+
super(`Position of "${params.user}" on market "${params.marketId}" has no debt and no collateral, so there is nothing to close.`);
|
|
1351
|
+
this.marketId = params.marketId;
|
|
1352
|
+
this.user = params.user;
|
|
1353
|
+
this.name = "LeverageCloseEmptyPositionError";
|
|
1354
|
+
}
|
|
1355
|
+
/** Market of the position. */
|
|
1356
|
+
marketId;
|
|
1357
|
+
/** Owner of the position. */
|
|
1358
|
+
user;
|
|
1359
|
+
}
|
|
1360
|
+
/**
|
|
1361
|
+
* Thrown when a Close targets a position that has collateral but no debt. ZeroExLeverageBundlesV1 still runs the
|
|
1362
|
+
* swap on such a position, and 0x AllowanceHolder reverts on the empty swap calldata a Close without debt would
|
|
1363
|
+
* sign, so the transaction could only revert. Withdraw the collateral with `market.withdrawCollateral` (`blueWithdrawCollateral`) instead.
|
|
1364
|
+
*
|
|
1365
|
+
* @see https://github.com/morpho-org/bundles/blob/3bf53141455051f8fc7a85cd00cc15bd89459238/src/leverage/ZeroExLeverageBundlesV1.sol#L180-L188
|
|
1366
|
+
* @example
|
|
1367
|
+
* ```ts
|
|
1368
|
+
* import type { MarketId } from "@morpho-org/blue-sdk";
|
|
1369
|
+
* import { LeverageCloseWithoutDebtError } from "@morpho-org/morpho-sdk";
|
|
1370
|
+
*
|
|
1371
|
+
* const error = new LeverageCloseWithoutDebtError({
|
|
1372
|
+
* marketId: "0x3b3769cfca57be2eaed03fcc5299c25691b77781a1e124e7a8d520eb9a7eabb5" as MarketId,
|
|
1373
|
+
* user: userAddress,
|
|
1374
|
+
* collateral: 10n ** 18n,
|
|
1375
|
+
* });
|
|
1376
|
+
* ```
|
|
1377
|
+
*/
|
|
1378
|
+
export class LeverageCloseWithoutDebtError extends LeverageLimitError {
|
|
1379
|
+
/**
|
|
1380
|
+
* @param params - The position without debt.
|
|
1381
|
+
* @param params.marketId - Market of the position.
|
|
1382
|
+
* @param params.user - Owner of the position.
|
|
1383
|
+
* @param params.collateral - Collateral of the position, in collateral-token assets.
|
|
1384
|
+
*/
|
|
1385
|
+
constructor(params) {
|
|
1386
|
+
super(`Position of "${params.user}" on market "${params.marketId}" has no debt, so a Close would revert in its 0x swap. Withdraw its "${params.collateral}" collateral with market.withdrawCollateral (blueWithdrawCollateral) instead.`);
|
|
1387
|
+
this.marketId = params.marketId;
|
|
1388
|
+
this.user = params.user;
|
|
1389
|
+
this.collateral = params.collateral;
|
|
1390
|
+
this.name = "LeverageCloseWithoutDebtError";
|
|
1391
|
+
}
|
|
1392
|
+
/** Market of the position. */
|
|
1393
|
+
marketId;
|
|
1394
|
+
/** Owner of the position. */
|
|
1395
|
+
user;
|
|
1396
|
+
/** Collateral of the position, in collateral-token assets. */
|
|
1397
|
+
collateral;
|
|
1398
|
+
}
|
|
1399
|
+
/**
|
|
1400
|
+
* Thrown when a Close's quote-based `maxCollateralIn` cap exceeds the position's collateral.
|
|
1401
|
+
*
|
|
1402
|
+
* @example
|
|
1403
|
+
* ```ts
|
|
1404
|
+
* import { LeverageCloseInputExceedsCollateralError } from "@morpho-org/morpho-sdk";
|
|
1405
|
+
*
|
|
1406
|
+
* const error = new LeverageCloseInputExceedsCollateralError({ cap: 101n, collateral: 100n });
|
|
1407
|
+
* ```
|
|
1408
|
+
*/
|
|
1409
|
+
export class LeverageCloseInputExceedsCollateralError extends LeverageLimitError {
|
|
1410
|
+
/**
|
|
1411
|
+
* @param params - Cap comparison, in collateral-token assets.
|
|
1412
|
+
* @param params.cap - Quote-based `maxCollateralIn`.
|
|
1413
|
+
* @param params.collateral - Position's collateral.
|
|
1414
|
+
*/
|
|
1415
|
+
constructor(params) {
|
|
1416
|
+
super(`Close input cap "${params.cap}" exceeds position collateral "${params.collateral}". Fetch a quote selling at most the position's collateral. ${CLOSE_NO_SWAP_EXITS}`);
|
|
1417
|
+
this.cap = params.cap;
|
|
1418
|
+
this.collateral = params.collateral;
|
|
1419
|
+
this.name = "LeverageCloseInputExceedsCollateralError";
|
|
1420
|
+
}
|
|
1421
|
+
/** Quote-based `maxCollateralIn`, in collateral-token assets. */
|
|
1422
|
+
cap;
|
|
1423
|
+
/** Position's collateral, in collateral-token assets. */
|
|
1424
|
+
collateral;
|
|
1425
|
+
}
|
|
1426
|
+
/**
|
|
1427
|
+
* Thrown when leverage is requested on a chain without a registered leverage bundle, or whose
|
|
1428
|
+
* AllowanceHolder is not at its Cancun address (excluded by the SDK's chain list, or no code at
|
|
1429
|
+
* the data block).
|
|
1430
|
+
*
|
|
1431
|
+
* @example
|
|
1432
|
+
* ```ts
|
|
1433
|
+
* import { LeverageUnsupportedChainError } from "@morpho-org/morpho-sdk";
|
|
1434
|
+
*
|
|
1435
|
+
* const error = new LeverageUnsupportedChainError({ chainId: 56, reason: "noBundle" });
|
|
1436
|
+
* ```
|
|
1437
|
+
*/
|
|
1438
|
+
export class LeverageUnsupportedChainError extends LeverageLimitError {
|
|
1439
|
+
/**
|
|
1440
|
+
* @param params - Chain details.
|
|
1441
|
+
* @param params.chainId - Chain id requested.
|
|
1442
|
+
* @param params.reason - `noBundle` when no leverage bundle is registered; `noAllowanceHolder` when AllowanceHolder is not at its Cancun address on the chain, either because the SDK's chain list excludes the chain or because a code read at the data block found none.
|
|
1443
|
+
*/
|
|
1444
|
+
constructor(params) {
|
|
1445
|
+
const detail = params.reason === "noBundle"
|
|
1446
|
+
? "has no registered leverage bundle"
|
|
1447
|
+
: "has no AllowanceHolder at its Cancun address";
|
|
1448
|
+
super(`Chain "${params.chainId}" ${detail}. Offer leverage only on supported chains.`);
|
|
1449
|
+
this.chainId = params.chainId;
|
|
1450
|
+
this.reason = params.reason;
|
|
1451
|
+
this.name = "LeverageUnsupportedChainError";
|
|
1452
|
+
}
|
|
1453
|
+
/** Chain id requested. */
|
|
1454
|
+
chainId;
|
|
1455
|
+
/** Why the chain is unsupported. */
|
|
1456
|
+
reason;
|
|
1457
|
+
}
|
|
1458
|
+
/**
|
|
1459
|
+
* Thrown when a leverage reallocation targets another market than the action's market.
|
|
1460
|
+
* The contract checks only the loan token, so the SDK requires the exact market.
|
|
1461
|
+
*
|
|
1462
|
+
* @example
|
|
1463
|
+
* ```ts
|
|
1464
|
+
* import { LeverageReallocationMarketMismatchError } from "@morpho-org/morpho-sdk";
|
|
1465
|
+
*
|
|
1466
|
+
* const error = new LeverageReallocationMarketMismatchError({
|
|
1467
|
+
* vault: vaultAddress,
|
|
1468
|
+
* expected: leverMarketParams.id,
|
|
1469
|
+
* actual: reallocationMarketParams.id,
|
|
1470
|
+
* });
|
|
1471
|
+
* ```
|
|
1472
|
+
*/
|
|
1473
|
+
export class LeverageReallocationMarketMismatchError extends LeverageReallocationError {
|
|
1474
|
+
/**
|
|
1475
|
+
* @param params - Market comparison.
|
|
1476
|
+
* @param params.vault - Vault whose reallocation targets the wrong market.
|
|
1477
|
+
* @param params.expected - Action's market id.
|
|
1478
|
+
* @param params.actual - Market id the reallocation targets.
|
|
1479
|
+
*/
|
|
1480
|
+
constructor(params) {
|
|
1481
|
+
super(`Reallocation from vault "${params.vault}" targets market "${params.actual}", expected the action's market "${params.expected}". Reallocate only into the action's market.`);
|
|
1482
|
+
this.vault = params.vault;
|
|
1483
|
+
this.expected = params.expected;
|
|
1484
|
+
this.actual = params.actual;
|
|
1485
|
+
this.name = "LeverageReallocationMarketMismatchError";
|
|
1486
|
+
}
|
|
1487
|
+
/** Vault whose reallocation targets the wrong market. */
|
|
1488
|
+
vault;
|
|
1489
|
+
/** Action's market id. */
|
|
1490
|
+
expected;
|
|
1491
|
+
/** Market id the reallocation targets. */
|
|
1492
|
+
actual;
|
|
1493
|
+
}
|
|
1494
|
+
/**
|
|
1495
|
+
* Thrown when a leverage reallocation's route is unavailable at the data block: the vault has no
|
|
1496
|
+
* public-allocator flow into the action's market, or the selected idle or source-market leg cannot be
|
|
1497
|
+
* pulled. Another source in the same vault may still work.
|
|
1498
|
+
*
|
|
1499
|
+
* @example
|
|
1500
|
+
* ```ts
|
|
1501
|
+
* import { LeverageReallocationNoFlowError } from "@morpho-org/morpho-sdk";
|
|
1502
|
+
*
|
|
1503
|
+
* const error = new LeverageReallocationNoFlowError({
|
|
1504
|
+
* vault: vaultAddress,
|
|
1505
|
+
* marketId: leverMarketParams.id,
|
|
1506
|
+
* });
|
|
1507
|
+
* ```
|
|
1508
|
+
*/
|
|
1509
|
+
export class LeverageReallocationNoFlowError extends LeverageReallocationError {
|
|
1510
|
+
/**
|
|
1511
|
+
* @param params - Flow details.
|
|
1512
|
+
* @param params.vault - Vault whose selected route is unavailable.
|
|
1513
|
+
* @param params.marketId - Action's market id.
|
|
1514
|
+
*/
|
|
1515
|
+
constructor(params) {
|
|
1516
|
+
super(`Vault "${params.vault}" has no usable public-allocator route into market "${params.marketId}" from the selected source. Choose another source in this vault, or reallocate from another vault.`);
|
|
1517
|
+
this.vault = params.vault;
|
|
1518
|
+
this.marketId = params.marketId;
|
|
1519
|
+
this.name = "LeverageReallocationNoFlowError";
|
|
1520
|
+
}
|
|
1521
|
+
/** Vault whose selected route is unavailable. */
|
|
1522
|
+
vault;
|
|
1523
|
+
/** Action's market id. */
|
|
1524
|
+
marketId;
|
|
1525
|
+
}
|
|
1526
|
+
/**
|
|
1527
|
+
* Thrown when a leverage reallocation's penalty rate exceeds the SDK's ceiling.
|
|
1528
|
+
*
|
|
1529
|
+
* @example
|
|
1530
|
+
* ```ts
|
|
1531
|
+
* import { LeverageReallocationPenaltyAboveLimitError } from "@morpho-org/morpho-sdk";
|
|
1532
|
+
*
|
|
1533
|
+
* const error = new LeverageReallocationPenaltyAboveLimitError({
|
|
1534
|
+
* vault: vaultAddress,
|
|
1535
|
+
* penalty: 20_000_000_000_000_000n,
|
|
1536
|
+
* max: 10_000_000_000_000_000n,
|
|
1537
|
+
* });
|
|
1538
|
+
* ```
|
|
1539
|
+
*/
|
|
1540
|
+
export class LeverageReallocationPenaltyAboveLimitError extends LeverageReallocationError {
|
|
1541
|
+
/**
|
|
1542
|
+
* @param params - Penalty comparison, WAD-scaled.
|
|
1543
|
+
* @param params.vault - Vault charging the penalty.
|
|
1544
|
+
* @param params.penalty - Vault's penalty rate.
|
|
1545
|
+
* @param params.max - SDK penalty ceiling.
|
|
1546
|
+
*/
|
|
1547
|
+
constructor(params) {
|
|
1548
|
+
super(`Penalty "${params.penalty}" of vault "${params.vault}" exceeds the SDK ceiling "${params.max}" (WAD). Drop this vault's reallocation.`);
|
|
1549
|
+
this.vault = params.vault;
|
|
1550
|
+
this.penalty = params.penalty;
|
|
1551
|
+
this.max = params.max;
|
|
1552
|
+
this.name = "LeverageReallocationPenaltyAboveLimitError";
|
|
1553
|
+
}
|
|
1554
|
+
/** Vault charging the penalty. */
|
|
1555
|
+
vault;
|
|
1556
|
+
/** Vault's penalty rate, WAD-scaled. */
|
|
1557
|
+
penalty;
|
|
1558
|
+
/** SDK penalty ceiling, WAD-scaled. */
|
|
1559
|
+
max;
|
|
1560
|
+
}
|
|
1561
|
+
/**
|
|
1562
|
+
* Thrown when leverage reallocations together move more liquidity than the action's borrow cap
|
|
1563
|
+
* (`maxBorrowAssets`).
|
|
1564
|
+
*
|
|
1565
|
+
* @example
|
|
1566
|
+
* ```ts
|
|
1567
|
+
* import { LeverageReallocationExceedsBorrowError } from "@morpho-org/morpho-sdk";
|
|
1568
|
+
*
|
|
1569
|
+
* const error = new LeverageReallocationExceedsBorrowError({
|
|
1570
|
+
* total: 101n,
|
|
1571
|
+
* maxBorrowAssets: 100n,
|
|
1572
|
+
* });
|
|
1573
|
+
* ```
|
|
1574
|
+
*/
|
|
1575
|
+
export class LeverageReallocationExceedsBorrowError extends LeverageReallocationError {
|
|
1576
|
+
/**
|
|
1577
|
+
* @param params - Liquidity comparison, in loan-token assets.
|
|
1578
|
+
* @param params.total - Assets moved by all reallocations.
|
|
1579
|
+
* @param params.maxBorrowAssets - Action's signed borrow cap.
|
|
1580
|
+
*/
|
|
1581
|
+
constructor(params) {
|
|
1582
|
+
super(`Reallocations move "${params.total}", more than the borrow cap "${params.maxBorrowAssets}". Reallocate only the liquidity the borrow needs.`);
|
|
1583
|
+
this.total = params.total;
|
|
1584
|
+
this.maxBorrowAssets = params.maxBorrowAssets;
|
|
1585
|
+
this.name = "LeverageReallocationExceedsBorrowError";
|
|
1586
|
+
}
|
|
1587
|
+
/** Assets moved by all reallocations. */
|
|
1588
|
+
total;
|
|
1589
|
+
/** Action's signed borrow cap (`maxBorrowAssets`), in loan-token assets. */
|
|
1590
|
+
maxBorrowAssets;
|
|
1591
|
+
}
|
|
1592
|
+
/**
|
|
1593
|
+
* Thrown when the market's liquidity at the data block plus the assets the reallocations move
|
|
1594
|
+
* into it cannot fund the action's borrow (`maxBorrowAssets`), so the transaction would revert.
|
|
1595
|
+
*
|
|
1596
|
+
* @example
|
|
1597
|
+
* ```ts
|
|
1598
|
+
* import { LeverageInsufficientLiquidityError } from "@morpho-org/morpho-sdk";
|
|
1599
|
+
*
|
|
1600
|
+
* const error = new LeverageInsufficientLiquidityError({
|
|
1601
|
+
* liquidity: 40n,
|
|
1602
|
+
* reallocatedAssets: 50n,
|
|
1603
|
+
* maxBorrowAssets: 100n,
|
|
1604
|
+
* });
|
|
1605
|
+
* ```
|
|
1606
|
+
*/
|
|
1607
|
+
export class LeverageInsufficientLiquidityError extends LeverageLimitError {
|
|
1608
|
+
/**
|
|
1609
|
+
* @param params - Liquidity comparison, in loan-token assets.
|
|
1610
|
+
* @param params.liquidity - Market liquidity at the data block.
|
|
1611
|
+
* @param params.reallocatedAssets - Assets moved into the market by all reallocations.
|
|
1612
|
+
* @param params.maxBorrowAssets - Action's signed borrow cap.
|
|
1613
|
+
*/
|
|
1614
|
+
constructor(params) {
|
|
1615
|
+
super(`Market liquidity "${params.liquidity}" plus reallocated assets "${params.reallocatedAssets}" cannot fund the borrow "${params.maxBorrowAssets}". Borrow less or reallocate more liquidity into the market.`);
|
|
1616
|
+
this.liquidity = params.liquidity;
|
|
1617
|
+
this.reallocatedAssets = params.reallocatedAssets;
|
|
1618
|
+
this.maxBorrowAssets = params.maxBorrowAssets;
|
|
1619
|
+
this.name = "LeverageInsufficientLiquidityError";
|
|
1620
|
+
}
|
|
1621
|
+
/** Market liquidity at the data block, in loan-token assets. */
|
|
1622
|
+
liquidity;
|
|
1623
|
+
/** Assets moved into the market by all reallocations, in loan-token assets. */
|
|
1624
|
+
reallocatedAssets;
|
|
1625
|
+
/** Action's signed borrow cap (`maxBorrowAssets`), in loan-token assets. */
|
|
1626
|
+
maxBorrowAssets;
|
|
1627
|
+
}
|
|
1628
|
+
/**
|
|
1629
|
+
* Thrown when a Lever's planned reallocations, replayed in order at the data block, leave a
|
|
1630
|
+
* target cap of the market's vault over its cap: a leg trimmed below the amount the planner chose
|
|
1631
|
+
* failed the cap check, and the planner's own amounts no longer fit once trimming dropped other
|
|
1632
|
+
* legs. Executing the plan would revert.
|
|
1633
|
+
*
|
|
1634
|
+
* @example
|
|
1635
|
+
* ```ts
|
|
1636
|
+
* import { LeverageReallocationOutsideCapsError } from "@morpho-org/morpho-sdk";
|
|
1637
|
+
*
|
|
1638
|
+
* const error = new LeverageReallocationOutsideCapsError({
|
|
1639
|
+
* vault: vaultAddress,
|
|
1640
|
+
* assets: 100n,
|
|
1641
|
+
* });
|
|
1642
|
+
* ```
|
|
1643
|
+
*/
|
|
1644
|
+
export class LeverageReallocationOutsideCapsError extends LeverageReallocationError {
|
|
1645
|
+
/**
|
|
1646
|
+
* @param params - Leg that breaks a cap.
|
|
1647
|
+
* @param params.vault - Vault the leg reallocates from.
|
|
1648
|
+
* @param params.assets - Assets the leg moves, in loan-token assets.
|
|
1649
|
+
*/
|
|
1650
|
+
constructor(params) {
|
|
1651
|
+
super(`Reallocating "${params.assets}" from vault "${params.vault}" leaves one of its target caps exceeded. Borrow a different amount or exclude this vault from reallocatableVaults.`);
|
|
1652
|
+
this.vault = params.vault;
|
|
1653
|
+
this.assets = params.assets;
|
|
1654
|
+
this.name = "LeverageReallocationOutsideCapsError";
|
|
1655
|
+
}
|
|
1656
|
+
/** Vault the leg reallocates from. */
|
|
1657
|
+
vault;
|
|
1658
|
+
/** Assets the leg moves, in loan-token assets. */
|
|
1659
|
+
assets;
|
|
1660
|
+
}
|