@morpho-org/morpho-sdk 10.5.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 +794 -1
- package/lib/cjs/abis.js +815 -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/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/abis.d.ts +1 -1
- package/lib/cjs/blue/abis.js +5 -1
- package/lib/cjs/blue/addresses.d.ts +1 -1
- package/lib/cjs/blue/addresses.js +6 -1
- package/lib/cjs/blue/entities.d.ts +4 -4
- package/lib/cjs/blue/entities.js +3 -1
- package/lib/cjs/blue/errors.d.ts +2 -2
- package/lib/cjs/blue/errors.js +5 -1
- package/lib/cjs/blue/fetch.d.ts +2 -2
- package/lib/cjs/blue/fetch.js +3 -1
- package/lib/cjs/blue/utils.d.ts +1 -1
- package/lib/cjs/blue/utils.js +2 -1
- package/lib/cjs/constants.d.ts +1 -1
- package/lib/cjs/constants.js +6 -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 +5 -5
- package/lib/cjs/entities/index.js +3 -1
- package/lib/cjs/entities/requirements/assertContractCode.d.ts +0 -23
- package/lib/cjs/entities/requirements/assertContractCode.js +3 -28
- package/lib/cjs/entities/vaultV2/vaultV2.d.ts +34 -8
- package/lib/cjs/entities/vaultV2/vaultV2.js +39 -1
- package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +120 -2
- package/lib/cjs/entities/vaultV2BlueReallocationData.js +232 -4
- package/lib/cjs/errors.d.ts +4 -4
- package/lib/cjs/errors.js +7 -5
- package/lib/cjs/fetch.d.ts +2 -2
- package/lib/cjs/fetch.js +3 -1
- package/lib/cjs/helpers/constant.d.ts +34 -0
- package/lib/cjs/helpers/constant.js +35 -1
- package/lib/cjs/helpers/deadShares.js +3 -0
- 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 +28 -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/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/midnight/errors.d.ts +2 -1
- package/lib/cjs/midnight/errors.js +5 -4
- 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/types/action.d.ts +130 -1
- package/lib/cjs/types/error.d.ts +59 -0
- package/lib/cjs/types/error.js +82 -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/utils.d.ts +2 -2
- package/lib/cjs/utils.js +4 -2
- package/lib/cjs/version.d.ts +1 -1
- package/lib/cjs/version.js +1 -1
- package/lib/esm/abis.d.ts +794 -1
- package/lib/esm/abis.js +810 -1
- 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/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/abis.d.ts +1 -1
- package/lib/esm/blue/abis.js +1 -1
- package/lib/esm/blue/addresses.d.ts +1 -1
- package/lib/esm/blue/addresses.js +1 -1
- package/lib/esm/blue/entities.d.ts +4 -4
- package/lib/esm/blue/entities.js +2 -2
- package/lib/esm/blue/errors.d.ts +2 -2
- package/lib/esm/blue/errors.js +2 -2
- package/lib/esm/blue/fetch.d.ts +2 -2
- package/lib/esm/blue/fetch.js +2 -2
- package/lib/esm/blue/utils.d.ts +1 -1
- package/lib/esm/blue/utils.js +1 -1
- package/lib/esm/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 +5 -5
- package/lib/esm/entities/index.js +2 -2
- package/lib/esm/entities/requirements/assertContractCode.d.ts +0 -23
- package/lib/esm/entities/requirements/assertContractCode.js +1 -25
- package/lib/esm/entities/vaultV2/vaultV2.d.ts +34 -8
- package/lib/esm/entities/vaultV2/vaultV2.js +40 -2
- package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +120 -2
- package/lib/esm/entities/vaultV2BlueReallocationData.js +233 -5
- package/lib/esm/errors.d.ts +4 -4
- package/lib/esm/errors.js +4 -4
- package/lib/esm/fetch.d.ts +2 -2
- package/lib/esm/fetch.js +2 -2
- package/lib/esm/helpers/constant.d.ts +34 -0
- package/lib/esm/helpers/constant.js +34 -0
- package/lib/esm/helpers/deadShares.js +4 -1
- 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/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/midnight/errors.d.ts +2 -1
- package/lib/esm/midnight/errors.js +2 -1
- 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/types/action.d.ts +130 -1
- package/lib/esm/types/error.d.ts +59 -0
- package/lib/esm/types/error.js +76 -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/utils.d.ts +2 -2
- package/lib/esm/utils.js +2 -2
- package/lib/esm/version.d.ts +1 -1
- package/lib/esm/version.js +1 -1
- package/package.json +7 -7
|
@@ -0,0 +1,763 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toZeroExSlippagePpm = exports.ZERO_EX_SLIPPAGE_PPM = void 0;
|
|
4
|
+
exports.getZeroExSwapRoute = getZeroExSwapRoute;
|
|
5
|
+
exports.getZeroExLeverageBundle = getZeroExLeverageBundle;
|
|
6
|
+
exports.validateZeroExExactOutputPair = validateZeroExExactOutputPair;
|
|
7
|
+
exports.resolveZeroExTxOrigin = resolveZeroExTxOrigin;
|
|
8
|
+
exports.validateZeroExQuote = validateZeroExQuote;
|
|
9
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
10
|
+
const viem_1 = require("viem");
|
|
11
|
+
const abis_js_1 = require("../abis.js");
|
|
12
|
+
const index_js_1 = require("../types/index.js");
|
|
13
|
+
const constant_js_1 = require("./constant.js");
|
|
14
|
+
const validate_js_1 = require("./validate.js");
|
|
15
|
+
/**
|
|
16
|
+
* Parts per million: the unit of the 0x `slippagePpm` parameter.
|
|
17
|
+
*
|
|
18
|
+
* @internal
|
|
19
|
+
*/
|
|
20
|
+
exports.ZERO_EX_SLIPPAGE_PPM = 1000000n;
|
|
21
|
+
/**
|
|
22
|
+
* Converts a WAD-scaled slippage to the whole parts per million 0x applies, rounded down as the
|
|
23
|
+
* `slippagePpm` request parameter carries it.
|
|
24
|
+
*
|
|
25
|
+
* @internal
|
|
26
|
+
* @param slippage - Caller slippage, WAD-scaled.
|
|
27
|
+
* @returns The slippage 0x applies, in parts per million.
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* toZeroExSlippagePpm(5_000_000_000_000_000n); // 5_000n
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
const toZeroExSlippagePpm = (slippage) => (slippage * exports.ZERO_EX_SLIPPAGE_PPM) / morpho_ts_1.MathLib.WAD;
|
|
34
|
+
exports.toZeroExSlippagePpm = toZeroExSlippagePpm;
|
|
35
|
+
/**
|
|
36
|
+
* Returns the tokens and quote mode of the 0x swap a ZeroExLeverageBundlesV1 action needs on a market.
|
|
37
|
+
*
|
|
38
|
+
* Lever buys exactly the collateral with loan tokens (exact output). Delever and Close sell collateral for
|
|
39
|
+
* loan tokens: Delever sells an exact amount (exact input), while Close buys exactly the debt plus its
|
|
40
|
+
* buffer (exact output).
|
|
41
|
+
*
|
|
42
|
+
* @see https://github.com/morpho-org/bundles/blob/3bf53141455051f8fc7a85cd00cc15bd89459238/src/leverage/ZeroExLeverageBundlesV1.sol#L60
|
|
43
|
+
* @see https://github.com/morpho-org/bundles/blob/3bf53141455051f8fc7a85cd00cc15bd89459238/src/leverage/ZeroExLeverageBundlesV1.sol#L128
|
|
44
|
+
* @see https://github.com/morpho-org/bundles/blob/3bf53141455051f8fc7a85cd00cc15bd89459238/src/leverage/ZeroExLeverageBundlesV1.sol#L159-L161
|
|
45
|
+
* @param kind - Swap the action needs.
|
|
46
|
+
* @param marketParams - Market whose tokens the swap trades.
|
|
47
|
+
* @returns The quote mode and the sell and buy tokens.
|
|
48
|
+
* @throws {ZeroExUnknownSwapKindError} when `kind` is not one of the three leverage swap kinds.
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* import { getZeroExSwapRoute } from "@morpho-org/morpho-sdk";
|
|
52
|
+
*
|
|
53
|
+
* declare const marketParams: { loanToken: `0x${string}`; collateralToken: `0x${string}` };
|
|
54
|
+
* const route = getZeroExSwapRoute("lever", marketParams);
|
|
55
|
+
* // { mode: "exactOutput", sellToken: marketParams.loanToken, buyToken: marketParams.collateralToken }
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
function getZeroExSwapRoute(kind, marketParams) {
|
|
59
|
+
switch (kind) {
|
|
60
|
+
case "lever":
|
|
61
|
+
return {
|
|
62
|
+
mode: "exactOutput",
|
|
63
|
+
sellToken: marketParams.loanToken,
|
|
64
|
+
buyToken: marketParams.collateralToken,
|
|
65
|
+
};
|
|
66
|
+
case "closeKeepCollateral":
|
|
67
|
+
return {
|
|
68
|
+
mode: "exactOutput",
|
|
69
|
+
sellToken: marketParams.collateralToken,
|
|
70
|
+
buyToken: marketParams.loanToken,
|
|
71
|
+
};
|
|
72
|
+
case "delever":
|
|
73
|
+
return {
|
|
74
|
+
mode: "exactInput",
|
|
75
|
+
sellToken: marketParams.collateralToken,
|
|
76
|
+
buyToken: marketParams.loanToken,
|
|
77
|
+
};
|
|
78
|
+
default:
|
|
79
|
+
throw new index_js_1.ZeroExUnknownSwapKindError({ kind });
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Returns the ZeroExLeverageBundlesV1 address registered for a chain where AllowanceHolder sits at its Cancun
|
|
84
|
+
* address.
|
|
85
|
+
*
|
|
86
|
+
* @internal
|
|
87
|
+
* @param chainId - Chain of the action.
|
|
88
|
+
* @returns The leverage bundle address.
|
|
89
|
+
* @throws {LeverageUnsupportedChainError} when AllowanceHolder is not at its Cancun address on the chain, or no
|
|
90
|
+
* bundle is registered there, including a bundle registered as the zero address.
|
|
91
|
+
*/
|
|
92
|
+
function getZeroExLeverageBundle(chainId) {
|
|
93
|
+
if (!morpho_ts_1.ZERO_EX_ALLOWANCE_HOLDER_CHAIN_IDS.has(chainId)) {
|
|
94
|
+
throw new index_js_1.LeverageUnsupportedChainError({
|
|
95
|
+
chainId,
|
|
96
|
+
reason: "noAllowanceHolder",
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
const bundle = (0, morpho_ts_1.getChainAddresses)(chainId).bundles?.zeroExLeverageBundlesV1;
|
|
100
|
+
if (bundle == null || !(0, morpho_ts_1.isZeroExLeverageChain)(chainId)) {
|
|
101
|
+
throw new index_js_1.LeverageUnsupportedChainError({ chainId, reason: "noBundle" });
|
|
102
|
+
}
|
|
103
|
+
return bundle;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Refuses an exact-output swap between the chain's native token and its wrapped-native token, which 0x does not
|
|
107
|
+
* support.
|
|
108
|
+
*
|
|
109
|
+
* @internal
|
|
110
|
+
* @param params - Swap details.
|
|
111
|
+
* @param params.chainId - Chain of the swap.
|
|
112
|
+
* @param params.mode - Quote mode.
|
|
113
|
+
* @param params.sellToken - Token the swap sells.
|
|
114
|
+
* @param params.buyToken - Token the swap buys.
|
|
115
|
+
* @throws {ZeroExExactOutputUnsupportedError} when the mode is exact output and the tokens are the native and
|
|
116
|
+
* the wrapped-native token, in either order.
|
|
117
|
+
*/
|
|
118
|
+
function validateZeroExExactOutputPair(params) {
|
|
119
|
+
const { chainId, mode, sellToken, buyToken } = params;
|
|
120
|
+
if (mode !== "exactOutput") {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
const wNative = (0, morpho_ts_1.getChainAddresses)(chainId).wNative;
|
|
124
|
+
if (wNative == null) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
const isNativeOrWrapped = (token) => (0, viem_1.isAddressEqual)(token, morpho_ts_1.NATIVE_ADDRESS) || (0, viem_1.isAddressEqual)(token, wNative);
|
|
128
|
+
if (isNativeOrWrapped(sellToken) &&
|
|
129
|
+
isNativeOrWrapped(buyToken) &&
|
|
130
|
+
!(0, viem_1.isAddressEqual)(sellToken, buyToken)) {
|
|
131
|
+
throw new index_js_1.ZeroExExactOutputUnsupportedError({ sellToken, buyToken });
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
function tryDecodeAllowanceHolderExec(data) {
|
|
135
|
+
try {
|
|
136
|
+
return (0, viem_1.decodeFunctionData)({ abi: abis_js_1.zeroExAllowanceHolderAbi, data }).args;
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
return undefined;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Returns the transaction origin of a leverage action: `txOrigin` when given, else the user. Only the 0x request
|
|
144
|
+
* and its echo use it; the taker, recipient, position owner and authorization stay the bundle and the user.
|
|
145
|
+
*
|
|
146
|
+
* @internal
|
|
147
|
+
* @param txOrigin - Optional sender of the outer onchain transaction, such as a Safe's executing owner or an ERC-4337 bundler.
|
|
148
|
+
* @param userAddress - Account whose position the action changes.
|
|
149
|
+
* @returns The transaction origin.
|
|
150
|
+
* @throws {ZeroExInvalidTxOriginError} when `txOrigin` is given and is not a valid address.
|
|
151
|
+
*/
|
|
152
|
+
function resolveZeroExTxOrigin(txOrigin, userAddress) {
|
|
153
|
+
if (txOrigin === undefined) {
|
|
154
|
+
return userAddress;
|
|
155
|
+
}
|
|
156
|
+
if (typeof txOrigin !== "string" || !(0, viem_1.isAddress)(txOrigin, { strict: false })) {
|
|
157
|
+
throw new index_js_1.ZeroExInvalidTxOriginError(txOrigin);
|
|
158
|
+
}
|
|
159
|
+
return txOrigin;
|
|
160
|
+
}
|
|
161
|
+
// Settler actions of the deployed Settlers, classed by what leverage accepts, as their Solidity signatures from
|
|
162
|
+
// ISettlerActions.sol at 0x-settler 243fb92; the older METATXN_* revisions and DODOV1 come from that file at
|
|
163
|
+
// 0x-settler 0d82cbc, 3af5605, 53b9603, 82b0c9f, a629bc4, c40b5cc. The selectors are derived from these signatures. `permit` pulls the sell
|
|
164
|
+
// token with the taker's permit, whose deadline is ABI word 4; `signedOrder` fills a maker's signed order; `timeCheck`
|
|
165
|
+
// reverts past its word 0; `opaqueCall` calls a venue with data Settler does not read; `select` holds candidate action
|
|
166
|
+
// lists; `amm` swaps through a pool with no time field (HANJI passes `expires = max`, Hanji.sol:77).
|
|
167
|
+
const SETTLER_ACTIONS = Object.fromEntries([
|
|
168
|
+
[
|
|
169
|
+
"permit",
|
|
170
|
+
"TRANSFER_FROM(address,((address,uint256),uint256,uint256),bytes)",
|
|
171
|
+
],
|
|
172
|
+
[
|
|
173
|
+
"permit",
|
|
174
|
+
"UNISWAPV3_VIP(address,((address,uint256),uint256,uint256),bytes,bytes,uint256)",
|
|
175
|
+
],
|
|
176
|
+
[
|
|
177
|
+
"permit",
|
|
178
|
+
"UNISWAPV4_VIP(address,((address,uint256),uint256,uint256),uint8,bool,uint256,uint256,bytes,bytes,uint256)",
|
|
179
|
+
],
|
|
180
|
+
[
|
|
181
|
+
"permit",
|
|
182
|
+
"BALANCERV3_VIP(address,((address,uint256),uint256,uint256),bool,uint256,uint256,bytes,bytes,uint256)",
|
|
183
|
+
],
|
|
184
|
+
[
|
|
185
|
+
"permit",
|
|
186
|
+
"PANCAKE_INFINITY_VIP(address,((address,uint256),uint256,uint256),uint8,bool,uint256,uint256,bytes,bytes,uint256)",
|
|
187
|
+
],
|
|
188
|
+
[
|
|
189
|
+
"permit",
|
|
190
|
+
"CURVE_TRICRYPTO_VIP(address,((address,uint256),uint256,uint256),uint80,bytes,uint256)",
|
|
191
|
+
],
|
|
192
|
+
[
|
|
193
|
+
"permit",
|
|
194
|
+
"EKUBOV3_VIP(address,((address,uint256),uint256,uint256),bool,uint256,uint256,bytes,bytes,uint256)",
|
|
195
|
+
],
|
|
196
|
+
[
|
|
197
|
+
"signedOrder",
|
|
198
|
+
"RFQ(address,((address,uint256),uint256,uint256),address,bytes,address,uint256)",
|
|
199
|
+
],
|
|
200
|
+
[
|
|
201
|
+
"signedOrder",
|
|
202
|
+
"RFQ_VIP(address,((address,uint256),uint256,uint256),((address,uint256),uint256,uint256),address,bytes,bytes)",
|
|
203
|
+
],
|
|
204
|
+
[
|
|
205
|
+
"signedOrder",
|
|
206
|
+
"BEBOP(address,address,(uint256,address,uint256,address,uint256,uint256,uint256),(bytes,uint256),uint256)",
|
|
207
|
+
],
|
|
208
|
+
[
|
|
209
|
+
"signedOrder",
|
|
210
|
+
"METATXN_TRANSFER_FROM(address,((address,uint256),uint256,uint256))",
|
|
211
|
+
],
|
|
212
|
+
[
|
|
213
|
+
"signedOrder",
|
|
214
|
+
"METATXN_RFQ_VIP(address,((address,uint256),uint256,uint256),((address,uint256),uint256,uint256),address,bytes)",
|
|
215
|
+
],
|
|
216
|
+
[
|
|
217
|
+
"signedOrder",
|
|
218
|
+
"METATXN_RFQ_VIP(address,((address,uint256),uint256,uint256),address,bytes,((address,uint256),uint256,uint256))",
|
|
219
|
+
],
|
|
220
|
+
[
|
|
221
|
+
"signedOrder",
|
|
222
|
+
"METATXN_UNISWAPV4_VIP(address,((address,uint256),uint256,uint256),bool,uint256,uint256,bytes,uint256)",
|
|
223
|
+
],
|
|
224
|
+
[
|
|
225
|
+
"signedOrder",
|
|
226
|
+
"METATXN_UNISWAPV4_VIP(address,bool,uint256,uint256,bytes,((address,uint256),uint256,uint256),uint256)",
|
|
227
|
+
],
|
|
228
|
+
[
|
|
229
|
+
"signedOrder",
|
|
230
|
+
"METATXN_UNISWAPV4_VIP(address,((address,uint256),uint256,uint256),uint8,bool,uint256,uint256,bytes,uint256)",
|
|
231
|
+
],
|
|
232
|
+
[
|
|
233
|
+
"signedOrder",
|
|
234
|
+
"METATXN_BALANCERV3_VIP(address,((address,uint256),uint256,uint256),bool,uint256,uint256,bytes,uint256)",
|
|
235
|
+
],
|
|
236
|
+
[
|
|
237
|
+
"signedOrder",
|
|
238
|
+
"METATXN_BALANCERV3_VIP(address,bool,uint256,uint256,bytes,((address,uint256),uint256,uint256),uint256)",
|
|
239
|
+
],
|
|
240
|
+
[
|
|
241
|
+
"signedOrder",
|
|
242
|
+
"METATXN_PANCAKE_INFINITY_VIP(address,((address,uint256),uint256,uint256),bool,uint256,uint256,bytes,uint256)",
|
|
243
|
+
],
|
|
244
|
+
[
|
|
245
|
+
"signedOrder",
|
|
246
|
+
"METATXN_PANCAKE_INFINITY_VIP(address,bool,uint256,uint256,bytes,((address,uint256),uint256,uint256),uint256)",
|
|
247
|
+
],
|
|
248
|
+
[
|
|
249
|
+
"signedOrder",
|
|
250
|
+
"METATXN_PANCAKE_INFINITY_VIP(address,((address,uint256),uint256,uint256),uint8,bool,uint256,uint256,bytes,uint256)",
|
|
251
|
+
],
|
|
252
|
+
[
|
|
253
|
+
"signedOrder",
|
|
254
|
+
"METATXN_UNISWAPV3_VIP(address,((address,uint256),uint256,uint256),bytes,uint256)",
|
|
255
|
+
],
|
|
256
|
+
[
|
|
257
|
+
"signedOrder",
|
|
258
|
+
"METATXN_UNISWAPV3_VIP(address,bytes,((address,uint256),uint256,uint256),uint256)",
|
|
259
|
+
],
|
|
260
|
+
[
|
|
261
|
+
"signedOrder",
|
|
262
|
+
"METATXN_CURVE_TRICRYPTO_VIP(address,((address,uint256),uint256,uint256),uint80,uint256)",
|
|
263
|
+
],
|
|
264
|
+
[
|
|
265
|
+
"signedOrder",
|
|
266
|
+
"METATXN_CURVE_TRICRYPTO_VIP(address,uint80,((address,uint256),uint256,uint256),uint256)",
|
|
267
|
+
],
|
|
268
|
+
[
|
|
269
|
+
"signedOrder",
|
|
270
|
+
"METATXN_EKUBOV3_VIP(address,((address,uint256),uint256,uint256),bool,uint256,uint256,bytes,uint256)",
|
|
271
|
+
],
|
|
272
|
+
[
|
|
273
|
+
"signedOrder",
|
|
274
|
+
"METATXN_EKUBOV3_VIP(address,bool,uint256,uint256,bytes,((address,uint256),uint256,uint256),uint256)",
|
|
275
|
+
],
|
|
276
|
+
[
|
|
277
|
+
"signedOrder",
|
|
278
|
+
"METATXN_MAVERICKV2_VIP(address,bytes32,bool,((address,uint256),uint256,uint256),int32,uint256)",
|
|
279
|
+
],
|
|
280
|
+
[
|
|
281
|
+
"signedOrder",
|
|
282
|
+
"METATXN_MAVERICKV2_VIP(address,((address,uint256),uint256,uint256),bytes32,bool,int32,uint256)",
|
|
283
|
+
],
|
|
284
|
+
[
|
|
285
|
+
"signedOrder",
|
|
286
|
+
"METATXN_MAVERICKV2_VIP(address,bytes32,bool,((address,uint256),uint256,uint256),uint256)",
|
|
287
|
+
],
|
|
288
|
+
[
|
|
289
|
+
"signedOrder",
|
|
290
|
+
"METATXN_EKUBO_VIP(address,bool,uint256,uint256,bytes,((address,uint256),uint256,uint256),uint256)",
|
|
291
|
+
],
|
|
292
|
+
[
|
|
293
|
+
"signedOrder",
|
|
294
|
+
"METATXN_EKUBO_VIP(address,((address,uint256),uint256,uint256),bool,uint256,uint256,bytes,uint256)",
|
|
295
|
+
],
|
|
296
|
+
[
|
|
297
|
+
"signedOrder",
|
|
298
|
+
"METATXN_TSUNAMI_VIP(address,((address,uint256),uint256,uint256),bool,uint256,uint256,bytes,uint256)",
|
|
299
|
+
],
|
|
300
|
+
["timeCheck", "NATIVE_CHECK(uint256,uint256)"],
|
|
301
|
+
["timeCheck", "ATTEST(uint256,bytes32,bytes32)"],
|
|
302
|
+
["opaqueCall", "BASIC(address,uint256,address,uint256,bytes)"],
|
|
303
|
+
["opaqueCall", "BASIC_PUSH(address,uint256,address,uint256,bytes)"],
|
|
304
|
+
["select", "SELECT(uint256,address,uint256[],bytes[][])"],
|
|
305
|
+
["amm", "UNISWAPV2(address,address,uint256,address,uint24,uint256)"],
|
|
306
|
+
["amm", "UNISWAPV3(address,uint256,bytes,uint256)"],
|
|
307
|
+
[
|
|
308
|
+
"amm",
|
|
309
|
+
"UNISWAPV4(address,address,uint256,uint8,bool,uint256,uint256,bytes,uint256)",
|
|
310
|
+
],
|
|
311
|
+
[
|
|
312
|
+
"amm",
|
|
313
|
+
"BALANCERV3(address,address,uint256,bool,uint256,uint256,bytes,uint256)",
|
|
314
|
+
],
|
|
315
|
+
[
|
|
316
|
+
"amm",
|
|
317
|
+
"PANCAKE_INFINITY(address,address,uint256,uint8,bool,uint256,uint256,bytes,uint256)",
|
|
318
|
+
],
|
|
319
|
+
[
|
|
320
|
+
"amm",
|
|
321
|
+
"EKUBO(address,address,uint256,bool,uint256,uint256,bytes,uint256)",
|
|
322
|
+
],
|
|
323
|
+
[
|
|
324
|
+
"amm",
|
|
325
|
+
"EKUBOV3(address,address,uint256,bool,uint256,uint256,bytes,uint256)",
|
|
326
|
+
],
|
|
327
|
+
["amm", "MAVERICKV2(address,address,uint256,address,bool,int32,uint256)"],
|
|
328
|
+
["amm", "VELODROME(address,uint256,address,uint24,uint256)"],
|
|
329
|
+
["amm", "DODOV1(address,uint256,address,bool,uint256)"],
|
|
330
|
+
["amm", "DODOV2(address,address,uint256,address,bool,uint256)"],
|
|
331
|
+
["amm", "MAKERPSM(address,uint256,bool,uint256,address,address)"],
|
|
332
|
+
["amm", "FLUXPOOL(address,uint256,bytes32,bool,uint256)"],
|
|
333
|
+
[
|
|
334
|
+
"amm",
|
|
335
|
+
"HANJI(address,uint256,address,uint256,uint256,bool,uint256,uint256)",
|
|
336
|
+
],
|
|
337
|
+
["amm", "DEEPSTATE(address,uint256,address,uint256,int32,uint256)"],
|
|
338
|
+
["amm", "POSITIVE_SLIPPAGE(address,address,uint256,uint256,uint256)"],
|
|
339
|
+
["amm", "CHECK_SLIPPAGE(bool)"],
|
|
340
|
+
].map(([kind, signature]) => [
|
|
341
|
+
(0, viem_1.toFunctionSelector)(signature),
|
|
342
|
+
[kind, signature.slice(0, signature.indexOf("("))],
|
|
343
|
+
]));
|
|
344
|
+
// SELECT levels walked: real routes nest one level, so a deeper SELECT refuses instead of going unchecked.
|
|
345
|
+
const SETTLER_SELECT_MAX_DEPTH = 3;
|
|
346
|
+
// How far before the deadline an opaque call's word still reads as a time bound: the deadline window plus one day,
|
|
347
|
+
// which covers the expiry of any quote fetched within the day before the leverage data block.
|
|
348
|
+
const SETTLER_TIME_BOUND_LOOKBACK = constant_js_1.LEVERAGE_DEADLINE_WINDOW + 86400n;
|
|
349
|
+
// Reads ABI word `word` of a Settler action's arguments, refusing an action too short to hold it.
|
|
350
|
+
function readSettlerWord(data, { word, action }) {
|
|
351
|
+
if ((0, viem_1.size)(data) < 4 + (word + 1) * 32) {
|
|
352
|
+
throw new index_js_1.ZeroExQuoteMalformedCalldataError({
|
|
353
|
+
reason: `Settler ${action} action is too short to hold its deadline`,
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
return (0, viem_1.hexToBigInt)((0, viem_1.sliceHex)(data, 4 + word * 32, 4 + (word + 1) * 32));
|
|
357
|
+
}
|
|
358
|
+
// Refuses an opaque call whose data, after its selector, holds a word in the time-bound window below the deadline:
|
|
359
|
+
// 0x writes router deadlines equal to the deadline, so a shorter one may be a venue's own expiry; the word is refused
|
|
360
|
+
// as one, since nothing tells whether it is a deadline.
|
|
361
|
+
function assertNoShortTimeBound(data, { action, path, deadline, }) {
|
|
362
|
+
let call;
|
|
363
|
+
try {
|
|
364
|
+
[, , , , call] = (0, viem_1.decodeFunctionData)({
|
|
365
|
+
abi: abis_js_1.zeroExSettlerBasicAbi,
|
|
366
|
+
data,
|
|
367
|
+
}).args;
|
|
368
|
+
}
|
|
369
|
+
catch (cause) {
|
|
370
|
+
throw new index_js_1.ZeroExQuoteMalformedCalldataError({ reason: `Settler ${action} does not decode` }, { cause });
|
|
371
|
+
}
|
|
372
|
+
for (let offset = 4; offset + 32 <= (0, viem_1.size)(call); offset += 32) {
|
|
373
|
+
const value = (0, viem_1.hexToBigInt)((0, viem_1.sliceHex)(call, offset, offset + 32));
|
|
374
|
+
if (value >= deadline - SETTLER_TIME_BOUND_LOOKBACK && value < deadline) {
|
|
375
|
+
throw new index_js_1.ZeroExQuoteShortTimeBoundError({
|
|
376
|
+
path,
|
|
377
|
+
word: (offset - 4) / 32,
|
|
378
|
+
value,
|
|
379
|
+
deadline,
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
// Returns the candidate action lists of a SELECT action, refusing one that does not decode or nests too deep.
|
|
385
|
+
function readSettlerSelectCandidates(data, depth) {
|
|
386
|
+
let candidates;
|
|
387
|
+
try {
|
|
388
|
+
[, , , candidates] = (0, viem_1.decodeFunctionData)({
|
|
389
|
+
abi: abis_js_1.zeroExSettlerSelectAbi,
|
|
390
|
+
data,
|
|
391
|
+
}).args;
|
|
392
|
+
}
|
|
393
|
+
catch (cause) {
|
|
394
|
+
throw new index_js_1.ZeroExQuoteMalformedCalldataError({ reason: "Settler SELECT does not decode" }, { cause });
|
|
395
|
+
}
|
|
396
|
+
if (depth === SETTLER_SELECT_MAX_DEPTH) {
|
|
397
|
+
throw new index_js_1.ZeroExQuoteMalformedCalldataError({
|
|
398
|
+
reason: `Settler SELECT nested deeper than ${SETTLER_SELECT_MAX_DEPTH} levels`,
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
return candidates;
|
|
402
|
+
}
|
|
403
|
+
// Walks Settler's `execute` actions and every SELECT candidate, accepting only AMM actions, the permit of the first
|
|
404
|
+
// top-level action when it carries `deadline`, a top-level NATIVE_CHECK or ATTEST reaching `deadline`, and opaque
|
|
405
|
+
// calls with no shorter time bound. Settler runs a permit (VIP) action only at index 0 (Settler.sol:173-178), and
|
|
406
|
+
// reverts past the permit deadline (Permit2Payment.sol:484), NATIVE_CHECK's deadline (Settler.sol:44-52) and
|
|
407
|
+
// ATTEST's (Attest.sol:73), all at 0x-settler 243fb92.
|
|
408
|
+
function assertSettlerActions(actions, { deadline, depth, path, }) {
|
|
409
|
+
for (const [index, data] of actions.entries()) {
|
|
410
|
+
const at = `${path}[${index}]`;
|
|
411
|
+
const selector = data.slice(0, 10).toLowerCase();
|
|
412
|
+
const entry = SETTLER_ACTIONS[selector];
|
|
413
|
+
if (entry === undefined) {
|
|
414
|
+
throw new index_js_1.ZeroExQuoteUnsupportedActionError({ selector, path: at });
|
|
415
|
+
}
|
|
416
|
+
const [kind, action] = entry;
|
|
417
|
+
if (kind === "amm") {
|
|
418
|
+
continue;
|
|
419
|
+
}
|
|
420
|
+
if (kind === "signedOrder") {
|
|
421
|
+
throw new index_js_1.ZeroExQuoteSignedOrderActionError({ action, path: at });
|
|
422
|
+
}
|
|
423
|
+
if (kind === "opaqueCall") {
|
|
424
|
+
assertNoShortTimeBound(data, { action, path: at, deadline });
|
|
425
|
+
continue;
|
|
426
|
+
}
|
|
427
|
+
if (kind === "select") {
|
|
428
|
+
for (const [candidate, candidateActions] of readSettlerSelectCandidates(data, depth).entries()) {
|
|
429
|
+
assertSettlerActions(candidateActions, {
|
|
430
|
+
deadline,
|
|
431
|
+
depth: depth + 1,
|
|
432
|
+
path: `${at}.candidates[${candidate}]`,
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
continue;
|
|
436
|
+
}
|
|
437
|
+
if (depth > 0 || (kind === "permit" && index > 0)) {
|
|
438
|
+
throw new index_js_1.ZeroExQuoteUnsupportedActionError({ selector, path: at });
|
|
439
|
+
}
|
|
440
|
+
const actual = readSettlerWord(data, {
|
|
441
|
+
word: kind === "permit" ? 4 : 0,
|
|
442
|
+
action,
|
|
443
|
+
});
|
|
444
|
+
if (kind === "permit" ? actual !== deadline : actual < deadline) {
|
|
445
|
+
throw new index_js_1.ZeroExQuoteDeadlineMismatchError({
|
|
446
|
+
expected: deadline,
|
|
447
|
+
actual,
|
|
448
|
+
action,
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
// Returns the quote's output floor and sell amount (`maxSellAmount` for an exact output), refusing a quote that lacks
|
|
454
|
+
// either before any calldata is decoded, so a swap whose calldata does not decode still carries both.
|
|
455
|
+
function readZeroExQuoteAmounts(quote) {
|
|
456
|
+
if (typeof quote.buyAmount !== "bigint") {
|
|
457
|
+
throw new index_js_1.InvalidBigIntInputError("quote.buyAmount", quote.buyAmount);
|
|
458
|
+
}
|
|
459
|
+
const exactOutput = quote.mode === "exactOutput";
|
|
460
|
+
const quotedMinAmountOut = exactOutput ? quote.buyAmount : quote.minBuyAmount;
|
|
461
|
+
if (typeof quotedMinAmountOut !== "bigint") {
|
|
462
|
+
throw new index_js_1.InvalidBigIntInputError("quote.minBuyAmount", quotedMinAmountOut);
|
|
463
|
+
}
|
|
464
|
+
const quotedSellAmount = exactOutput ? quote.maxSellAmount : quote.sellAmount;
|
|
465
|
+
if (typeof quotedSellAmount !== "bigint") {
|
|
466
|
+
throw new index_js_1.InvalidBigIntInputError(exactOutput ? "quote.maxSellAmount" : "quote.sellAmount", quotedSellAmount);
|
|
467
|
+
}
|
|
468
|
+
return { quotedMinAmountOut, quotedSellAmount };
|
|
469
|
+
}
|
|
470
|
+
// Refuses a malformed swap input cap or deadline before any quote field is read.
|
|
471
|
+
function validateZeroExSwapBounds(maxSwapInput, deadline) {
|
|
472
|
+
if (typeof maxSwapInput !== "bigint") {
|
|
473
|
+
throw new index_js_1.InvalidBigIntInputError("maxSwapInput", maxSwapInput);
|
|
474
|
+
}
|
|
475
|
+
if (maxSwapInput <= 0n) {
|
|
476
|
+
throw new index_js_1.NonPositiveInputError("maxSwapInput", maxSwapInput);
|
|
477
|
+
}
|
|
478
|
+
(0, validate_js_1.validateUint256Field)("maxSwapInput", maxSwapInput);
|
|
479
|
+
if (typeof deadline !== "bigint") {
|
|
480
|
+
throw new index_js_1.InvalidBigIntInputError("deadline", deadline);
|
|
481
|
+
}
|
|
482
|
+
(0, validate_js_1.validateDeadline)(deadline);
|
|
483
|
+
}
|
|
484
|
+
function tryDecodeSettlerExecute(data) {
|
|
485
|
+
try {
|
|
486
|
+
return (0, viem_1.decodeFunctionData)({ abi: abis_js_1.zeroExSettlerAbi, data }).args;
|
|
487
|
+
}
|
|
488
|
+
catch {
|
|
489
|
+
// Falls through to the other entry points.
|
|
490
|
+
}
|
|
491
|
+
let functionName;
|
|
492
|
+
try {
|
|
493
|
+
({ functionName } = (0, viem_1.decodeFunctionData)({
|
|
494
|
+
abi: abis_js_1.zeroExSettlerOtherEntryPointsAbi,
|
|
495
|
+
data,
|
|
496
|
+
}));
|
|
497
|
+
}
|
|
498
|
+
catch {
|
|
499
|
+
return undefined;
|
|
500
|
+
}
|
|
501
|
+
throw new index_js_1.ZeroExQuoteMalformedCalldataError({
|
|
502
|
+
reason: `Settler entry point is ${functionName}, not execute`,
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* Validates a 0x quote for a ZeroExLeverageBundlesV1 action and returns it as a {@link ValidatedZeroExSwap}.
|
|
507
|
+
*
|
|
508
|
+
* The bundle runs `swapData` through AllowanceHolder unchecked and only measures its own balance changes
|
|
509
|
+
* (ZeroExLeverageBundlesV1.sol:385-408), so this is the SDK's check of the calldata. It is strict on the envelope:
|
|
510
|
+
* the quote's chain, mode and tokens, its taker and txOrigin, no native value, and a call to AllowanceHolder. Every
|
|
511
|
+
* calldata layer must decode, whichever leverage bundle takes the quote: a layer that does not refuses with
|
|
512
|
+
* `ZeroExQuoteUndecodableCalldataError`, and a decoded layer that does not match refuses the swap.
|
|
513
|
+
* - AllowanceHolder layer: the `exec` call's operator and target are the same Settler, pulling the sell token, at most
|
|
514
|
+
* the action's swap input cap and exactly the quote's sell amount (`maxSellAmount` for an exact output).
|
|
515
|
+
* - Settler layer: the `exec` data is Settler's taker-submitted `execute`, whose slippage pays the bundle in the buy
|
|
516
|
+
* token with a minimum output at least the quote's floor. Another recognized entry point (`executeWithPermit` or
|
|
517
|
+
* `executeMetaTxn`) refuses.
|
|
518
|
+
* - Settler actions: every action, and every action of every `SELECT` candidate down to three levels, is classed by
|
|
519
|
+
* its selector, and anything not classed refuses. AMM actions pass. A signed-order fill (`RFQ`, `RFQ_VIP`, `BEBOP`,
|
|
520
|
+
* any `METATXN_*`) refuses at any depth, since its maker's expiry can come before the leverage deadline. The permit
|
|
521
|
+
* action (`TRANSFER_FROM` and the `*_VIP` actions) may only be the first top-level action, where Settler runs it,
|
|
522
|
+
* and its deadline must equal `deadline`. `NATIVE_CHECK` and `ATTEST` may only be top-level, with a deadline of at
|
|
523
|
+
* least `deadline`. An opaque `BASIC` or `BASIC_PUSH` call refuses when any 32-byte word of its data reads as a
|
|
524
|
+
* time bound in the hour and day before `deadline`; one equal to `deadline` is how 0x writes router deadlines. The
|
|
525
|
+
* scan cannot tell a deadline from another value, so an ordinary amount in that window refuses too; a new quote
|
|
526
|
+
* clears it only when its calldata differs, so a fixed value such as the caller's amount needs another amount, a
|
|
527
|
+
* later retry or a no-swap exit. A
|
|
528
|
+
* `SELECT` that does not decode, or nests more than three levels deep, refuses.
|
|
529
|
+
* So every time bound the calldata shows reaches `deadline`, whichever sources 0x routes through; this is the
|
|
530
|
+
* backstop for a short-lived source 0x adds after `ZERO_EX_LEVERAGE_EXCLUDED_SOURCES`. A venue whose validity depends
|
|
531
|
+
* on an offchain keeper, with no time bound in the calldata, is kept out only by the sources `fetchZeroExQuote`
|
|
532
|
+
* excludes. In every case the bundle's onchain bounds (`spent <= maxIn`, `received >= minOut`)
|
|
533
|
+
* still bind what the swap does inside Settler. A slippage struct that is all zero, which 0x sends for some
|
|
534
|
+
* single-pool routes, is accepted: Settler then checks no output, and the bundle's `received >= minOut` check
|
|
535
|
+
* protects the swap. The leverage limits compare the swap's worst price with the market oracle: a Lever refuses past
|
|
536
|
+
* the bound, while a Delever or a Close only reports the deviation. Whether that Settler is 0x's official one, and
|
|
537
|
+
* whether 0x paused it, is not checked here; the bundle's bounds limit what the swap can lose.
|
|
538
|
+
*
|
|
539
|
+
* The quote's `chainId`, `taker` and `txOrigin` are also checked against the action's chain, the leverage
|
|
540
|
+
* bundle and the action's `txOrigin` (`userAddress` by default), but these are request-context checks: `fetchZeroExQuote` fills them from its own request,
|
|
541
|
+
* so they show what was asked for, not whom 0x built the calldata for. The quote's `blockNumber` is not checked
|
|
542
|
+
* either: a quote has no age limit here beyond the leverage deadline the builders sign. Pure and synchronous:
|
|
543
|
+
* builders call it again on every build.
|
|
544
|
+
*
|
|
545
|
+
* @param params - Validation parameters.
|
|
546
|
+
* @param params.quote - Quote returned by `fetchZeroExQuote`.
|
|
547
|
+
* @param params.kind - Swap the action needs.
|
|
548
|
+
* @param params.chainId - Chain of the action.
|
|
549
|
+
* @param params.marketParams - Market of the action.
|
|
550
|
+
* @param params.userAddress - Account whose position the action changes (`msg.sender`).
|
|
551
|
+
* @param params.txOrigin - Transaction origin the quote must have been requested for, when it differs from the user,
|
|
552
|
+
* such as a Safe's executing owner or an ERC-4337 bundler sending the outer transaction. Defaults to `userAddress`.
|
|
553
|
+
* @param params.maxSwapInput - Largest amount of the sell token the action lets the swap spend: the swap input
|
|
554
|
+
* cap the transaction enforces: `maxIn` of the bundle's swap, as the builder signs it. When that cap comes from
|
|
555
|
+
* the quote, such as an exact-output quote's `maxSellAmount`, validation only proves that the calldata stays within
|
|
556
|
+
* the signed limit; deciding whether the quoted price is acceptable stays with the caller.
|
|
557
|
+
* @param params.deadline - Deadline the action signs, in seconds: for the leverage builders, the leverage data's
|
|
558
|
+
* block timestamp plus one hour. Fetch the quote with the same `deadline`: the permit must carry it, and every other
|
|
559
|
+
* time bound the Settler actions show must reach it.
|
|
560
|
+
* @returns The quote with its swap calldata, its Settler, its swap input and its Settler minimum output.
|
|
561
|
+
* @throws {LeverageUnsupportedChainError} when the chain has no AllowanceHolder at its Cancun address or no
|
|
562
|
+
* registered leverage bundle.
|
|
563
|
+
* @throws {ZeroExQuoteChainMismatchError} when the quote is for another chain.
|
|
564
|
+
* @throws {ZeroExQuoteTypeMismatchError} when the quote mode is not the one the action requires.
|
|
565
|
+
* @throws {ZeroExQuoteTokenMismatchError} when the quote, the AllowanceHolder token or Settler's buy token is
|
|
566
|
+
* not the action's sell or buy token.
|
|
567
|
+
* @throws {ZeroExExactOutputUnsupportedError} when an exact-output action swaps a native and wrapped-native pair.
|
|
568
|
+
* @throws {ZeroExQuoteTakerMismatchError} when the quote's taker is not the leverage bundle.
|
|
569
|
+
* @throws {ZeroExQuoteTxOriginMismatchError} when the quote's txOrigin is not `txOrigin`, or the user by default.
|
|
570
|
+
* @throws {ZeroExInvalidTxOriginError} when `txOrigin` is given and is not a valid address.
|
|
571
|
+
* @throws {ZeroExQuoteNonZeroValueError} when the quote sends native value.
|
|
572
|
+
* @throws {ZeroExQuoteTargetMismatchError} when the quote does not call AllowanceHolder.
|
|
573
|
+
* @throws {ZeroExUnknownSwapKindError} when `kind` is not one of the three leverage swap kinds.
|
|
574
|
+
* @throws {InvalidBigIntInputError} when `maxSwapInput`, the quote's `buyAmount`, its floor (`minBuyAmount` for an
|
|
575
|
+
* exact input, `buyAmount` for an exact output) or its sell amount (`sellAmount` for an exact input, `maxSellAmount` for an
|
|
576
|
+
* exact output) is missing or not a bigint, before any calldata is decoded.
|
|
577
|
+
* @throws {NonPositiveInputError} when `maxSwapInput` is not positive.
|
|
578
|
+
* @throws {InputExceedsMaxError} when `maxSwapInput` exceeds `uint256`.
|
|
579
|
+
* @throws {InvalidBigIntInputError} when `deadline` is missing or not a bigint.
|
|
580
|
+
* @throws {NonPositiveInputError} when `deadline` is not positive.
|
|
581
|
+
* @throws {InputExceedsMaxError} when `deadline` exceeds `uint256`.
|
|
582
|
+
* @throws {ZeroExSettlerOperatorTargetMismatchError} when the AllowanceHolder target is not the operator.
|
|
583
|
+
* @throws {ZeroExQuoteAmountExceedsCapError} when AllowanceHolder may pull more than `maxSwapInput`.
|
|
584
|
+
* @throws {ZeroExQuoteSellAmountMismatchError} when AllowanceHolder pulls another amount than the quote's sell
|
|
585
|
+
* amount.
|
|
586
|
+
* @throws {ZeroExQuoteMalformedCalldataError} when the Settler layer decodes as an entry point other than `execute`,
|
|
587
|
+
* a permit, `NATIVE_CHECK` or `ATTEST` action is too short to hold its deadline, a `BASIC` or `BASIC_PUSH` action
|
|
588
|
+
* does not decode, or a `SELECT` action does not decode or nests deeper than three levels.
|
|
589
|
+
* @throws {ZeroExQuoteUndecodableCalldataError} when the AllowanceHolder or Settler layer does not decode.
|
|
590
|
+
* @throws {ZeroExQuoteSignedOrderActionError} when a Settler action, or one in a `SELECT` candidate, fills a signed
|
|
591
|
+
* order (`RFQ`, `RFQ_VIP`, `BEBOP` or any `METATXN_*` action).
|
|
592
|
+
* @throws {ZeroExQuoteUnsupportedActionError} when a Settler action is not one the SDK classes, or stands where it may
|
|
593
|
+
* not: a permit action other than the first top-level one, or a `NATIVE_CHECK` or `ATTEST` in a `SELECT` candidate.
|
|
594
|
+
* @throws {ZeroExQuoteShortTimeBoundError} when a `BASIC` or `BASIC_PUSH` call holds a word that looks like a deadline
|
|
595
|
+
* before `deadline`.
|
|
596
|
+
* @throws {ZeroExQuoteDeadlineMismatchError} when the permit deadline is not `deadline`, or a `NATIVE_CHECK` or
|
|
597
|
+
* `ATTEST` deadline is below it.
|
|
598
|
+
* @throws {ZeroExQuoteRecipientMismatchError} when the Settler slippage pays anyone but the leverage bundle.
|
|
599
|
+
* @throws {ZeroExQuoteMinAmountOutMismatchError} when the Settler slippage's minimum output is zero or below the
|
|
600
|
+
* quote's floor (`minBuyAmount` for an exact input, `buyAmount` for an exact output).
|
|
601
|
+
* @example
|
|
602
|
+
* ```ts
|
|
603
|
+
* import { fetchZeroExQuote, validateZeroExQuote } from "@morpho-org/morpho-sdk";
|
|
604
|
+
* import { isBlueZeroExLeverageChain } from "@morpho-org/morpho-sdk/addresses";
|
|
605
|
+
*
|
|
606
|
+
* declare const marketParams: { loanToken: `0x${string}`; collateralToken: `0x${string}` };
|
|
607
|
+
* declare const userAddress: `0x${string}`;
|
|
608
|
+
* // Swap input cap the transaction enforces: the bundle's `maxIn`, as the builder signs it. The leverage builders
|
|
609
|
+
* // derive it from the action's limits (a Lever's `maxSwapIn`, a Delever's `maxCollateralToSell`, a Close's
|
|
610
|
+
* // `maxCollateralIn`); a direct caller must bound it on its own, not copy it from the quote.
|
|
611
|
+
* declare const maxSwapInput: bigint;
|
|
612
|
+
* // Deadline the action signs, in seconds: the leverage builders sign the leverage data's block timestamp plus one hour.
|
|
613
|
+
* declare const deadline: bigint;
|
|
614
|
+
*
|
|
615
|
+
* // Leverage needs a ZeroExLeverageBundlesV1 registered on the chain. None ships yet: register a verified
|
|
616
|
+
* // address with `registerCustomAddresses` first, or the call throws LeverageUnsupportedChainError before any request.
|
|
617
|
+
* if (!isBlueZeroExLeverageChain(8453)) throw new Error("No leverage bundle is registered on Base yet.");
|
|
618
|
+
* const quote = await fetchZeroExQuote({
|
|
619
|
+
* chainId: 8453,
|
|
620
|
+
* kind: "lever",
|
|
621
|
+
* marketParams,
|
|
622
|
+
* userAddress,
|
|
623
|
+
* amount: 10n ** 18n,
|
|
624
|
+
* slippage: 5_000_000_000_000_000n,
|
|
625
|
+
* deadline,
|
|
626
|
+
* apiKey: process.env.ZEROEX_API_KEY!,
|
|
627
|
+
* });
|
|
628
|
+
* const swap = validateZeroExQuote({
|
|
629
|
+
* quote,
|
|
630
|
+
* kind: "lever",
|
|
631
|
+
* chainId: 8453,
|
|
632
|
+
* marketParams,
|
|
633
|
+
* userAddress,
|
|
634
|
+
* maxSwapInput,
|
|
635
|
+
* deadline,
|
|
636
|
+
* });
|
|
637
|
+
* ```
|
|
638
|
+
*/
|
|
639
|
+
function validateZeroExQuote(params) {
|
|
640
|
+
const { quote, chainId, userAddress, maxSwapInput, deadline } = params;
|
|
641
|
+
const bundle = getZeroExLeverageBundle(chainId);
|
|
642
|
+
const txOrigin = resolveZeroExTxOrigin(params.txOrigin, userAddress);
|
|
643
|
+
validateZeroExSwapBounds(maxSwapInput, deadline);
|
|
644
|
+
if (quote.chainId !== chainId) {
|
|
645
|
+
throw new index_js_1.ZeroExQuoteChainMismatchError({
|
|
646
|
+
expected: chainId,
|
|
647
|
+
actual: quote.chainId,
|
|
648
|
+
});
|
|
649
|
+
}
|
|
650
|
+
const route = getZeroExSwapRoute(params.kind, params.marketParams);
|
|
651
|
+
if (quote.mode !== route.mode) {
|
|
652
|
+
throw new index_js_1.ZeroExQuoteTypeMismatchError({
|
|
653
|
+
expected: route.mode,
|
|
654
|
+
actual: quote.mode,
|
|
655
|
+
});
|
|
656
|
+
}
|
|
657
|
+
if (!(0, viem_1.isAddressEqual)(quote.sellToken, route.sellToken)) {
|
|
658
|
+
throw new index_js_1.ZeroExQuoteTokenMismatchError({
|
|
659
|
+
field: "sellToken",
|
|
660
|
+
expected: route.sellToken,
|
|
661
|
+
actual: quote.sellToken,
|
|
662
|
+
});
|
|
663
|
+
}
|
|
664
|
+
if (!(0, viem_1.isAddressEqual)(quote.buyToken, route.buyToken)) {
|
|
665
|
+
throw new index_js_1.ZeroExQuoteTokenMismatchError({
|
|
666
|
+
field: "buyToken",
|
|
667
|
+
expected: route.buyToken,
|
|
668
|
+
actual: quote.buyToken,
|
|
669
|
+
});
|
|
670
|
+
}
|
|
671
|
+
validateZeroExExactOutputPair({ chainId, ...route });
|
|
672
|
+
if (!(0, viem_1.isAddressEqual)(quote.taker, bundle)) {
|
|
673
|
+
throw new index_js_1.ZeroExQuoteTakerMismatchError({
|
|
674
|
+
expected: bundle,
|
|
675
|
+
actual: quote.taker,
|
|
676
|
+
});
|
|
677
|
+
}
|
|
678
|
+
if (!(0, viem_1.isAddressEqual)(quote.txOrigin, txOrigin)) {
|
|
679
|
+
throw new index_js_1.ZeroExQuoteTxOriginMismatchError({
|
|
680
|
+
expected: txOrigin,
|
|
681
|
+
actual: quote.txOrigin,
|
|
682
|
+
});
|
|
683
|
+
}
|
|
684
|
+
if (quote.transaction.value !== 0n) {
|
|
685
|
+
throw new index_js_1.ZeroExQuoteNonZeroValueError(quote.transaction.value);
|
|
686
|
+
}
|
|
687
|
+
if (!(0, viem_1.isAddressEqual)(quote.transaction.to, morpho_ts_1.ZERO_EX_ALLOWANCE_HOLDER_ADDRESS)) {
|
|
688
|
+
throw new index_js_1.ZeroExQuoteTargetMismatchError({
|
|
689
|
+
expected: morpho_ts_1.ZERO_EX_ALLOWANCE_HOLDER_ADDRESS,
|
|
690
|
+
actual: quote.transaction.to,
|
|
691
|
+
});
|
|
692
|
+
}
|
|
693
|
+
// Checked before decoding, so a quote without a floor or a sell amount is refused whether or not it decodes.
|
|
694
|
+
const { quotedMinAmountOut, quotedSellAmount } = readZeroExQuoteAmounts(quote);
|
|
695
|
+
const exec = tryDecodeAllowanceHolderExec(quote.transaction.data);
|
|
696
|
+
if (exec === undefined) {
|
|
697
|
+
throw new index_js_1.ZeroExQuoteUndecodableCalldataError({
|
|
698
|
+
layer: "allowanceHolder",
|
|
699
|
+
bundle,
|
|
700
|
+
});
|
|
701
|
+
}
|
|
702
|
+
const [operator, token, amount, target, settlerData] = exec;
|
|
703
|
+
if (!(0, viem_1.isAddressEqual)(target, operator)) {
|
|
704
|
+
throw new index_js_1.ZeroExSettlerOperatorTargetMismatchError({ operator, target });
|
|
705
|
+
}
|
|
706
|
+
if (!(0, viem_1.isAddressEqual)(token, route.sellToken)) {
|
|
707
|
+
throw new index_js_1.ZeroExQuoteTokenMismatchError({
|
|
708
|
+
field: "sellToken",
|
|
709
|
+
expected: route.sellToken,
|
|
710
|
+
actual: token,
|
|
711
|
+
});
|
|
712
|
+
}
|
|
713
|
+
if (amount > maxSwapInput) {
|
|
714
|
+
throw new index_js_1.ZeroExQuoteAmountExceedsCapError({ amount, cap: maxSwapInput });
|
|
715
|
+
}
|
|
716
|
+
if (amount !== quotedSellAmount) {
|
|
717
|
+
throw new index_js_1.ZeroExQuoteSellAmountMismatchError({
|
|
718
|
+
expected: quotedSellAmount,
|
|
719
|
+
actual: amount,
|
|
720
|
+
});
|
|
721
|
+
}
|
|
722
|
+
const execute = tryDecodeSettlerExecute(settlerData);
|
|
723
|
+
if (execute === undefined) {
|
|
724
|
+
throw new index_js_1.ZeroExQuoteUndecodableCalldataError({
|
|
725
|
+
layer: "settler",
|
|
726
|
+
bundle,
|
|
727
|
+
});
|
|
728
|
+
}
|
|
729
|
+
const [{ recipient, buyToken, minAmountOut }, actions] = execute;
|
|
730
|
+
assertSettlerActions(actions, { deadline, depth: 0, path: "" });
|
|
731
|
+
const validated = {
|
|
732
|
+
...quote,
|
|
733
|
+
settler: operator,
|
|
734
|
+
swapData: quote.transaction.data,
|
|
735
|
+
swapInput: amount,
|
|
736
|
+
settlerMinAmountOut: minAmountOut,
|
|
737
|
+
};
|
|
738
|
+
if ((0, viem_1.isAddressEqual)(recipient, viem_1.zeroAddress) &&
|
|
739
|
+
(0, viem_1.isAddressEqual)(buyToken, viem_1.zeroAddress) &&
|
|
740
|
+
minAmountOut === 0n) {
|
|
741
|
+
return validated;
|
|
742
|
+
}
|
|
743
|
+
if (!(0, viem_1.isAddressEqual)(recipient, bundle)) {
|
|
744
|
+
throw new index_js_1.ZeroExQuoteRecipientMismatchError({
|
|
745
|
+
expected: bundle,
|
|
746
|
+
actual: recipient,
|
|
747
|
+
});
|
|
748
|
+
}
|
|
749
|
+
if (!(0, viem_1.isAddressEqual)(buyToken, route.buyToken)) {
|
|
750
|
+
throw new index_js_1.ZeroExQuoteTokenMismatchError({
|
|
751
|
+
field: "buyToken",
|
|
752
|
+
expected: route.buyToken,
|
|
753
|
+
actual: buyToken,
|
|
754
|
+
});
|
|
755
|
+
}
|
|
756
|
+
if (minAmountOut === 0n || minAmountOut < quotedMinAmountOut) {
|
|
757
|
+
throw new index_js_1.ZeroExQuoteMinAmountOutMismatchError({
|
|
758
|
+
expected: quotedMinAmountOut,
|
|
759
|
+
actual: minAmountOut,
|
|
760
|
+
});
|
|
761
|
+
}
|
|
762
|
+
return validated;
|
|
763
|
+
}
|