@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
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { type AccrualPosition, type Market, MarketParams } from "@morpho-org/blue-sdk";
|
|
2
2
|
import { type Address } from "viem";
|
|
3
|
+
import { type LeverageLimitsData } from "../../helpers/index.js";
|
|
3
4
|
import type { FetchParameters } from "../../types/data.js";
|
|
4
|
-
import { type ActionOutput, type ActionRequirement, type AssetsOrSharesArgs, type BlueBorrowAction, type BlueRefinanceAction, type BlueRepayAction, type BlueRepayWithdrawCollateralAction, type BlueSupplyAction, type BlueSupplyCollateralAction, type BlueSupplyCollateralBorrowAction, type BlueWithdrawAction, type BlueWithdrawBuyCallbackAction, type BlueWithdrawCollateralAction, type MorphoClientType, type ReferenceTimestampParams, type RequirementSignature, type VaultV2BluePublicAllocatorOptions, type VaultV2BlueReallocation } from "../../types/index.js";
|
|
5
|
+
import { type ActionOutput, type ActionRequirement, type AssetsOrSharesArgs, type BlueBorrowAction, type BlueCloseAction, type BlueDeleverAction, type BlueLeverAction, type BlueRefinanceAction, type BlueRepayAction, type BlueRepayWithdrawCollateralAction, type BlueSupplyAction, type BlueSupplyCollateralAction, type BlueSupplyCollateralBorrowAction, type BlueWithdrawAction, type BlueWithdrawBuyCallbackAction, type BlueWithdrawCollateralAction, type LeverageActionOutput, type MorphoClientType, type ReferenceTimestampParams, type RequirementSignature, type VaultV2BluePublicAllocatorOptions, type VaultV2BlueReallocation, type ZeroExQuote } from "../../types/index.js";
|
|
5
6
|
import { VaultV2BlueReallocationData } from "../vaultV2BlueReallocationData.js";
|
|
6
7
|
type VaultV2BlueReallocationsParams = {
|
|
7
8
|
readonly reallocationData: VaultV2BlueReallocationData;
|
|
@@ -23,9 +24,44 @@ export interface BlueTokenRequirementsParams {
|
|
|
23
24
|
/** @deprecated Ignored. The SDK no longer requests Permit2 funding; use ERC-2612 permits or ERC-20 approvals. */
|
|
24
25
|
readonly permit2Nonce?: bigint;
|
|
25
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* Morpho Blue market state a leverage action is priced and checked against, read at one block.
|
|
29
|
+
*
|
|
30
|
+
* Every field comes from the same block, so the SDK limits and the Actions API attestor recompute the same
|
|
31
|
+
* result from it. Builders use `block.timestamp` instead of a clock.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* import type { BlueLeverageData } from "@morpho-org/morpho-sdk/entities";
|
|
36
|
+
*
|
|
37
|
+
* // The position is as stored at the block; accrue it to the block for its current LTV.
|
|
38
|
+
* const ltvOf = (leverageData: BlueLeverageData) =>
|
|
39
|
+
* leverageData.position.accrueInterest(leverageData.block.timestamp).ltv;
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export interface BlueLeverageData extends LeverageLimitsData {
|
|
43
|
+
/** Chain the market lives on. */
|
|
44
|
+
readonly chainId: number;
|
|
45
|
+
/** Block number every field was read at, and that block's timestamp. */
|
|
46
|
+
readonly block: {
|
|
47
|
+
readonly number: bigint;
|
|
48
|
+
readonly timestamp: bigint;
|
|
49
|
+
};
|
|
50
|
+
/** ZeroExLeverageBundlesV1 address registered for the chain. */
|
|
51
|
+
readonly bundle: Address;
|
|
52
|
+
/** Market as stored at the block, not accrued to `block.timestamp`, with the market oracle's price at the block when the oracle could be read; `market.price` may be absent. */
|
|
53
|
+
readonly market: Market;
|
|
54
|
+
/** The user's position on `market`, as stored at the block: accrue a copy to `block.timestamp` for its current debt. */
|
|
55
|
+
readonly position: AccrualPosition;
|
|
56
|
+
/** WAD-scaled per-second borrow rate of the market's IRM at `block.timestamp`. */
|
|
57
|
+
readonly borrowRate: bigint;
|
|
58
|
+
/** Vault V2 public-allocator flows and penalties toward `market` at the block. */
|
|
59
|
+
readonly reallocationData: VaultV2BlueReallocationData;
|
|
60
|
+
}
|
|
26
61
|
/**
|
|
27
62
|
* Action surface for Morpho Blue reads and writes; writes route through BlueBundlesV1, except
|
|
28
|
-
* `withdrawBuyCallback`, which calls Morpho Blue directly
|
|
63
|
+
* `withdrawBuyCallback`, which calls Morpho Blue directly, and `lever`, `delever` and `close`, which call
|
|
64
|
+
* ZeroExLeverageBundlesV1.
|
|
29
65
|
*/
|
|
30
66
|
export interface BlueActions {
|
|
31
67
|
/**
|
|
@@ -71,6 +107,417 @@ export interface BlueActions {
|
|
|
71
107
|
* ```
|
|
72
108
|
*/
|
|
73
109
|
getPositionData: (userAddress: Address, parameters?: FetchParameters) => Promise<AccrualPosition>;
|
|
110
|
+
/**
|
|
111
|
+
* Reads the state a ZeroExLeverageBundlesV1 action on this market is priced and checked against, all at one block.
|
|
112
|
+
*
|
|
113
|
+
* Reads the block (latest unless `blockNumber` is given), then at that block number: whether AllowanceHolder has code
|
|
114
|
+
* (an existence check only: the code is not compared with 0x's AllowanceHolder, whose code hash is pinned by the fork
|
|
115
|
+
* tests, so the address constant is trusted), the
|
|
116
|
+
* user's position and the market with its oracle price, and the Vault V2 public-allocator flows and penalties toward this market. The market and position are
|
|
117
|
+
* returned as stored at that block, not accrued to its timestamp, because Blue accrues once from the stored last update
|
|
118
|
+
* and the leverage limits project the debt the same way; accrue a copy to `block.timestamp` for the current debt.
|
|
119
|
+
* `borrowRate` is the IRM rate at the block timestamp, read from such an accrued copy. The chain needs a registered
|
|
120
|
+
* ZeroExLeverageBundlesV1 address (built in, or added with `registerCustomAddresses`); until then the call throws
|
|
121
|
+
* `LeverageUnsupportedChainError` before any RPC request.
|
|
122
|
+
*
|
|
123
|
+
* Reads are pinned by block number, not block hash, and a replay of the leverage data reads by number too. If the head
|
|
124
|
+
* block is reorganized while the reads are in flight, they can mix the replaced block and its replacement; read at
|
|
125
|
+
* a block a few confirmations behind the head when that matters.
|
|
126
|
+
*
|
|
127
|
+
* @param params.userAddress - User whose position the action changes.
|
|
128
|
+
* @param params.vaultAddresses - Vault V2 vaults whose public-allocator flows Lever may use, as any iterable; none
|
|
129
|
+
* by default. Repeated addresses, in any casing, are read once. The SDK sets no cap on their number: each vault
|
|
130
|
+
* costs its own reads.
|
|
131
|
+
* @param params.blockNumber - Block to read at; the latest block by default.
|
|
132
|
+
* @returns The one-block leverage data, including the block number and timestamp it was read at. Its position
|
|
133
|
+
* carries no oracle price when the oracle cannot be read; Lever, Delever and a Close with debt then refuse with
|
|
134
|
+
* `LeverageOraclePriceUnavailableError`. A Close without debt refuses with `LeverageCloseWithoutDebtError` first.
|
|
135
|
+
* @throws {ChainIdMismatchError} when the client declares another chain or none, or its RPC reports another chain via `eth_chainId`.
|
|
136
|
+
* @throws {UnknownBlueMarketParamsError} (from `@morpho-org/morpho-sdk/errors`) when the market does not exist at
|
|
137
|
+
* the selected block: Morpho then returns an all-zero market that was never updated.
|
|
138
|
+
* @throws {LeverageUnsupportedChainError} when the chain has no registered ZeroExLeverageBundlesV1, 0x deploys
|
|
139
|
+
* AllowanceHolder elsewhere on it, or AllowanceHolder has no code at the block.
|
|
140
|
+
* @throws {UnsupportedBlueMarketIrmError} when the market's IRM is neither the zero address nor one the SDK can compute,
|
|
141
|
+
* whether or not the market has debt, because `borrowRate` is always computed.
|
|
142
|
+
* @throws {UnknownBlueOfFactory} when a vault address is not a Vault V2, or another error documented on
|
|
143
|
+
* {@link BlueActions.getVaultV2BlueReallocationData}.
|
|
144
|
+
* @throws {viem.BaseError} when an RPC request fails.
|
|
145
|
+
* @example
|
|
146
|
+
* ```ts
|
|
147
|
+
* import { markets } from "@morpho-org/morpho-test";
|
|
148
|
+
* import { createPublicClient, http, zeroAddress, type Address } from "viem";
|
|
149
|
+
* import { base } from "viem/chains";
|
|
150
|
+
* import { morphoViemExtension } from "@morpho-org/morpho-sdk";
|
|
151
|
+
* import { getChainAddresses, registerCustomAddresses } from "@morpho-org/morpho-sdk/addresses";
|
|
152
|
+
*
|
|
153
|
+
* // The ZeroExLeverageBundlesV1 you deployed and verified on Base; no bundle ships for Base in this release.
|
|
154
|
+
* declare const verifiedBaseLeverageBundle: Address;
|
|
155
|
+
* registerCustomAddresses({
|
|
156
|
+
* addresses: {
|
|
157
|
+
* [base.id]: {
|
|
158
|
+
* bundles: { ...getChainAddresses(base.id).bundles, zeroExLeverageBundlesV1: verifiedBaseLeverageBundle },
|
|
159
|
+
* },
|
|
160
|
+
* },
|
|
161
|
+
* });
|
|
162
|
+
*
|
|
163
|
+
* const client = createPublicClient({ chain: base, transport: http() })
|
|
164
|
+
* .extend(morphoViemExtension());
|
|
165
|
+
* const market = client.morpho.blue(markets[base.id].usdc_eth, base.id);
|
|
166
|
+
* const leverageData = await market.getLeverageData({ userAddress: zeroAddress });
|
|
167
|
+
* const { borrowAssets } = leverageData.position.accrueInterest(leverageData.block.timestamp);
|
|
168
|
+
* console.log(leverageData.block.number, borrowAssets);
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
171
|
+
getLeverageData: (params: {
|
|
172
|
+
readonly userAddress: Address;
|
|
173
|
+
readonly vaultAddresses?: Iterable<Address>;
|
|
174
|
+
readonly blockNumber?: bigint;
|
|
175
|
+
}) => Promise<BlueLeverageData>;
|
|
176
|
+
/**
|
|
177
|
+
* Prepares a ZeroExLeverageBundlesV1 Close of the user's position on this market: repays the
|
|
178
|
+
* whole debt by swapping collateral through 0x, then sends what is left to the user.
|
|
179
|
+
*
|
|
180
|
+
* An exact-output quote buys the debt plus the SDK's interest buffer and the unsold collateral
|
|
181
|
+
* returns to the user. A position without debt has no Close: it refuses with `LeverageCloseWithoutDebtError`, and
|
|
182
|
+
* `market.withdrawCollateral` (`blueWithdrawCollateral`) withdraws the collateral instead. Builds eagerly with {@link blueClose}, so every refusal throws here, before any read;
|
|
183
|
+
* see it for the bounds and checks.
|
|
184
|
+
*
|
|
185
|
+
* When a Close is refused or 0x is unavailable, exit without a swap with the existing methods:
|
|
186
|
+
* `repay` or `supplyCollateral` reduce risk, and `repayWithdrawCollateral` leaves the position.
|
|
187
|
+
*
|
|
188
|
+
* `getRequirements()` returns the Morpho authorization of the bundle when it is missing (Close
|
|
189
|
+
* pulls no token, so no approval). The authorization persists after the Close.
|
|
190
|
+
*
|
|
191
|
+
* @param params.userAddress - Wallet that owns the position and calls the bundle (`msg.sender`), such as the smart account itself; its position is closed.
|
|
192
|
+
* @param params.txOrigin - Sender of the outer onchain transaction (its `tx.origin`) when it is not `userAddress`:
|
|
193
|
+
* the owner EOA that executes a Safe transaction, or the bundler that submits an ERC-4337 UserOperation, not the
|
|
194
|
+
* wallet's signer. Fetch the quote with the same `txOrigin`. Approvals, authorization and the position
|
|
195
|
+
* stay `userAddress`'s. Defaults to `userAddress`.
|
|
196
|
+
* @param params.leverageData - Unmodified `getLeverageData({ userAddress })` result for this market.
|
|
197
|
+
* @param params.quote - Exact-output 0x quote of kind `closeKeepCollateral` from `fetchZeroExQuote`; required
|
|
198
|
+
* with debt. A position without debt refuses with `LeverageCloseWithoutDebtError`, with or without a quote.
|
|
199
|
+
* @param params.slippage - Caller slippage, WAD-scaled, at most 10%.
|
|
200
|
+
* @param params.now - Current timestamp in seconds (the latest block's) for the deadline check;
|
|
201
|
+
* `getRequirements()` advances it by the device time elapsed since the handle was built. The device
|
|
202
|
+
* clock by default. Never pass the leverage data's own timestamp.
|
|
203
|
+
* @returns Lazy requirements and the synchronous deep-frozen Close.
|
|
204
|
+
* @throws {ChainIdMismatchError} when the client or the leverage data targets another chain, or from
|
|
205
|
+
* `getRequirements()` when the RPC reports another chain via `eth_chainId`.
|
|
206
|
+
* @throws {InvalidBigIntInputError} when `slippage`, `quote.slippage`, a supplied `now` or `leverageData.block.timestamp` is
|
|
207
|
+
* not a bigint, or when the position has debt and `leverageData.borrowRate` is missing or not a bigint.
|
|
208
|
+
* @throws {NegativeInputError} when `now` or `slippage` is negative, or when the position has debt and
|
|
209
|
+
* `leverageData.borrowRate` is negative.
|
|
210
|
+
* @throws {AccrualPositionUserMismatchError} when the leverage data position belongs to another user.
|
|
211
|
+
* @throws {ExpiredLeverageDeadlineError} (an `ExpiredDeadlineError`) when the Close deadline (data block + 1 hour) has passed, at
|
|
212
|
+
* creation or in `getRequirements()`, before any authorization is requested; read a new leverage data
|
|
213
|
+
* and quote.
|
|
214
|
+
* @throws {MarketIdMismatchError} when the leverage data is for another market.
|
|
215
|
+
* @throws {LeverageCloseEmptyPositionError} when the position has neither debt nor collateral. It has no
|
|
216
|
+
* fallback: tell the user there is nothing to close.
|
|
217
|
+
* @throws {LeverageLimitError} for every other Close refusal, when the Close is outside an SDK bound: the
|
|
218
|
+
* quote's amounts, at most 10% slippage, the collateral cap and, with debt, a usable oracle price
|
|
219
|
+
* (`LeverageOraclePriceUnavailableError`, whose fallback is a no-swap exit); see {@link blueClose} for each subclass. The SDK
|
|
220
|
+
* does not refuse on the quoted price: `action.args.oracleDeviation` reports the worst fill's deviation from the
|
|
221
|
+
* oracle, so show it and the most collateral the Close can sell (`maxCollateralIn`) before the user signs.
|
|
222
|
+
* @throws {ZeroExQuoteError} when the quote or its calldata is unusable.
|
|
223
|
+
* @throws {MissingContractCodeError} from `getRequirements()` when Morpho Blue has no code, or when a leverage
|
|
224
|
+
* bundle registered with `registerCustomAddresses` has none; the shipped bundle's code is not read.
|
|
225
|
+
* @throws {viem.BaseError} from `getRequirements()` when a read fails.
|
|
226
|
+
* @throws {ZeroExInvalidTxOriginError} when `txOrigin` is given and is not a valid address; pass a valid one or omit
|
|
227
|
+
* it instead of fetching a new quote.
|
|
228
|
+
* @example
|
|
229
|
+
* ```ts
|
|
230
|
+
* import { createPublicClient, http } from "viem";
|
|
231
|
+
* import { base } from "viem/chains";
|
|
232
|
+
* import {
|
|
233
|
+
* computeCloseInterestBuffer,
|
|
234
|
+
* fetchZeroExQuote,
|
|
235
|
+
* leverageDeadline,
|
|
236
|
+
* morphoViemExtension,
|
|
237
|
+
* } from "@morpho-org/morpho-sdk";
|
|
238
|
+
* import { isBlueZeroExLeverageChain } from "@morpho-org/morpho-sdk/addresses";
|
|
239
|
+
*
|
|
240
|
+
* // Close needs a ZeroExLeverageBundlesV1 registered on the chain (built in, or added with
|
|
241
|
+
* // `registerCustomAddresses`); without one the leverage data read throws LeverageUnsupportedChainError.
|
|
242
|
+
* if (!isBlueZeroExLeverageChain(base.id)) throw new Error("No leverage bundle is registered on Base yet.");
|
|
243
|
+
*
|
|
244
|
+
* const client = createPublicClient({ chain: base, transport: http() })
|
|
245
|
+
* .extend(morphoViemExtension());
|
|
246
|
+
* const market = client.morpho.blue(marketParams, base.id);
|
|
247
|
+
* const leverageData = await market.getLeverageData({ userAddress });
|
|
248
|
+
* const slippage = 5_000_000_000_000_000n;
|
|
249
|
+
* // A position without debt has no Close (it refuses with LeverageCloseWithoutDebtError): withdraw its collateral
|
|
250
|
+
* // with market.withdrawCollateral instead. Check it before fetching a quote, whose amount would be zero.
|
|
251
|
+
* if (leverageData.position.borrowShares === 0n) {
|
|
252
|
+
* throw new Error("No debt: withdraw the collateral with market.withdrawCollateral instead of a Close.");
|
|
253
|
+
* }
|
|
254
|
+
* // The debt accrued to the data block, as the builder computes it.
|
|
255
|
+
* const { borrowAssets } = leverageData.position.accrueInterest(leverageData.block.timestamp);
|
|
256
|
+
* const quote = await fetchZeroExQuote({
|
|
257
|
+
* chainId: base.id,
|
|
258
|
+
* kind: "closeKeepCollateral",
|
|
259
|
+
* marketParams,
|
|
260
|
+
* userAddress,
|
|
261
|
+
* amount: borrowAssets + computeCloseInterestBuffer(leverageData),
|
|
262
|
+
* slippage,
|
|
263
|
+
* deadline: leverageDeadline(leverageData), // the deadline the Close signs
|
|
264
|
+
* apiKey,
|
|
265
|
+
* });
|
|
266
|
+
* const close = market.close({
|
|
267
|
+
* userAddress,
|
|
268
|
+
* leverageData,
|
|
269
|
+
* quote,
|
|
270
|
+
* slippage,
|
|
271
|
+
* });
|
|
272
|
+
* const calls = [...(await close.getRequirements()), close.buildTx()];
|
|
273
|
+
* ```
|
|
274
|
+
*/
|
|
275
|
+
close: (params: {
|
|
276
|
+
readonly userAddress: Address;
|
|
277
|
+
readonly txOrigin?: Address;
|
|
278
|
+
readonly leverageData: BlueLeverageData;
|
|
279
|
+
readonly quote?: ZeroExQuote;
|
|
280
|
+
readonly slippage: bigint;
|
|
281
|
+
} & ReferenceTimestampParams) => LeverageActionOutput<BlueCloseAction>;
|
|
282
|
+
/**
|
|
283
|
+
* Prepares a ZeroExLeverageBundlesV1 Lever on this market: deposits `collateralAssets` and
|
|
284
|
+
* `loanAssets`, borrows, and buys at least the quote's `buyAmount` of collateral through 0x, in one
|
|
285
|
+
* transaction to the chain's registered bundle. An optional `nativeAmount` is sent as the transaction's
|
|
286
|
+
* `value`; the bundle wraps it into the chain's wNative for the user before pulling the deposits.
|
|
287
|
+
*
|
|
288
|
+
* The handle is built eagerly: every guard runs now and throws its typed error unchanged, and the
|
|
289
|
+
* transaction is encoded by {@link blueLever} from the one-block `leverageData`, never from a clock or
|
|
290
|
+
* a read. When `leverageData` was read with `vaultAddresses`, the handle plans Vault V2
|
|
291
|
+
* BluePublicAllocator reallocations for the borrow with `VaultV2BlueReallocationData`, capped at the
|
|
292
|
+
* 1% penalty rate; a caller cannot pass its own reallocations. The plan only fills the part of the
|
|
293
|
+
* borrow the market's liquidity leaves uncovered, and is recomputed until the borrow it was sized
|
|
294
|
+
* for includes its own penalty.
|
|
295
|
+
*
|
|
296
|
+
* `getRequirements()` returns an ERC-20 approval for each pulled token whose allowance is short and, when the
|
|
297
|
+
* Lever borrows, the Blue authorization grant. For the ZeroExLeverageBundlesV1 that morpho-ts ships, each approval
|
|
298
|
+
* is unlimited (`maxUint256`, capped per token) and persists after the Lever. For a bundle registered with
|
|
299
|
+
* `registerCustomAddresses`, its code is checked first and each approval is exactly the pulled amount, which the
|
|
300
|
+
* Lever spends. In both cases the authorization persists after the Lever. With `nativeAmount`, the approval of the
|
|
301
|
+
* wNative token still covers its whole pull: the user holds the wrapped part only once the Lever runs, and the
|
|
302
|
+
* bundle pulls it back with the rest. A Safe or ERC-4337 account sends the value from its own balance, as
|
|
303
|
+
* `msg.sender`, so `txOrigin` is unchanged.
|
|
304
|
+
*
|
|
305
|
+
* @param params.userAddress - Wallet that owns the position and calls the bundle (`msg.sender`), such as the smart account itself; its position is levered.
|
|
306
|
+
* @param params.txOrigin - Sender of the outer onchain transaction (its `tx.origin`) when it is not `userAddress`:
|
|
307
|
+
* the owner EOA that executes a Safe transaction, or the bundler that submits an ERC-4337 UserOperation, not the
|
|
308
|
+
* wallet's signer. Fetch the quote with the same `txOrigin`. Approvals, authorization and the position
|
|
309
|
+
* stay `userAddress`'s. Defaults to `userAddress`.
|
|
310
|
+
* @param params.leverageData - Unmodified result of {@link BlueActions.getLeverageData} for this user.
|
|
311
|
+
* @param params.quote - Exact-output 0x quote buying the collateral, from `fetchZeroExQuote` with
|
|
312
|
+
* `kind: "lever"` (taker the bundle, txOrigin `txOrigin` or the user).
|
|
313
|
+
* @param params.slippage - Slippage passed to 0x, WAD-scaled, at most 10%.
|
|
314
|
+
* @param params.collateralAssets - Collateral the user deposits.
|
|
315
|
+
* @param params.loanAssets - Loan assets the user deposits.
|
|
316
|
+
* @param params.nativeAmount - Optional native token sent as `value` and wrapped into wNative before the pulls,
|
|
317
|
+
* only when the collateral or loan token is the chain's wNative, and at most that token's pull.
|
|
318
|
+
* @param params.reallocationOptions - Optional planner controls; `enabled: false` skips
|
|
319
|
+
* reallocations, and `maxPenalty` can only lower the 1% ceiling. Planning is pinned to the data block, so
|
|
320
|
+
* there is no `timestamp` option.
|
|
321
|
+
* @param params.now - Current timestamp in seconds (the latest block's) for the deadline check;
|
|
322
|
+
* `getRequirements()` advances it by the device time elapsed since the handle was built. The device
|
|
323
|
+
* clock by default. Never pass the leverage data's own timestamp.
|
|
324
|
+
* @returns Lazy requirements and a synchronous deep-frozen Lever transaction.
|
|
325
|
+
* @throws {ChainIdMismatchError} when the client or the leverage data is on another chain, or from
|
|
326
|
+
* `getRequirements()` when the RPC reports another chain.
|
|
327
|
+
* @throws {MarketIdMismatchError} when the leverage data position belongs to another market, or the
|
|
328
|
+
* market tuple does not hash to its cached id.
|
|
329
|
+
* @throws {NegativeInputError} when an intent amount, `now`, `slippage`, `reallocationOptions.maxPenalty`,
|
|
330
|
+
* `capAccrualBuffer`, `allocatorCapHeadroom` or `maxWithdrawalUtilization` is negative.
|
|
331
|
+
* @throws {ExpiredLeverageDeadlineError} (an `ExpiredDeadlineError`) when the Lever deadline (data block + 1 hour) has passed, at
|
|
332
|
+
* creation or in `getRequirements()`, before any allowance or authorization is requested; read a new
|
|
333
|
+
* leverage data and quote.
|
|
334
|
+
* @throws {LeverageOraclePriceUnavailableError} when the leverage data has no usable market oracle price.
|
|
335
|
+
* @throws {MissingContractCodeError} from `getRequirements()` when Morpho Blue has no code, or when a leverage
|
|
336
|
+
* bundle registered with `registerCustomAddresses` has none; the shipped bundle's code is not read.
|
|
337
|
+
* @throws {viem.BaseError} from `getRequirements()` when the `eth_chainId`, allowance or
|
|
338
|
+
* authorization read fails.
|
|
339
|
+
* @throws {AccrualPositionUserMismatchError} when the leverage data position belongs to another user.
|
|
340
|
+
* @throws {InsufficientSharedLiquidityError} when the planned reallocations cannot cover the borrow.
|
|
341
|
+
* @throws {LeverageInsufficientLiquidityError} when the market's liquidity plus the planned
|
|
342
|
+
* reallocations still cannot fund the borrow. A Lever that pays a penalty above `loanAssets` also flash-loans
|
|
343
|
+
* the difference from Blue, which the SDK does not check, so it can still revert onchain when Blue is short of
|
|
344
|
+
* cash.
|
|
345
|
+
* @throws {LeverageOraclePriceDeviationError} when the quote's worst price is more than 15% above the market's
|
|
346
|
+
* oracle price; fetch a new quote.
|
|
347
|
+
* @throws {LeverageLimitError} when the action is unsafe at current prices or outside a bound;
|
|
348
|
+
* see {@link blueLever} for each leaf.
|
|
349
|
+
* @throws {LeverageReallocationError} when a planned reallocation fails the leverage guard, or when a trimmed leg breaks a vault cap and the smallest amount the caps accept moves more than the borrow (`LeverageReallocationExceedsBorrowError`) or the leg breaks a cap even at its planned amount (`LeverageReallocationOutsideCapsError`).
|
|
350
|
+
* @throws {ZeroExInvalidTxOriginError} when `txOrigin` is given and is not a valid address; pass a valid one or omit
|
|
351
|
+
* it instead of fetching a new quote.
|
|
352
|
+
* @throws {ZeroExQuoteError} when the quote or its calldata is unusable: fetch a new quote.
|
|
353
|
+
* @throws {InvalidBigIntInputError} when an intent amount, `nativeAmount`, `slippage`, `quote.slippage`,
|
|
354
|
+
* `reallocationOptions.maxPenalty`, `allocatorCapHeadroom`, `maxWithdrawalUtilization` or `now` is not a bigint,
|
|
355
|
+
* or `capAccrualBuffer` is a value `BigInt` cannot convert, as the shared reallocation planner converts it.
|
|
356
|
+
* @throws {NativeAmountOnNonWNativeAssetError} when `nativeAmount` is positive and neither market token is the
|
|
357
|
+
* chain's wNative.
|
|
358
|
+
* @throws {ChainWNativeMissingError} when `nativeAmount` is positive and the chain registers no wNative.
|
|
359
|
+
* @throws {InputExceedsMaxError} when an amount exceeds its bound, `nativeAmount` exceeds the wNative pull,
|
|
360
|
+
* `reallocationOptions.allocatorCapHeadroom` or
|
|
361
|
+
* `maxWithdrawalUtilization` exceeds WAD, or from `getRequirements()`
|
|
362
|
+
* when the summed pulls on a market whose loan and collateral tokens are equal exceed `uint256`.
|
|
363
|
+
* A token with an approval cap gets an approval at that cap, so a larger pull reverts onchain.
|
|
364
|
+
* @example
|
|
365
|
+
* ```ts
|
|
366
|
+
* import { markets } from "@morpho-org/morpho-test";
|
|
367
|
+
* import { createPublicClient, http } from "viem";
|
|
368
|
+
* import { base } from "viem/chains";
|
|
369
|
+
* import {
|
|
370
|
+
* fetchZeroExQuote,
|
|
371
|
+
* leverageDeadline,
|
|
372
|
+
* morphoViemExtension,
|
|
373
|
+
* } from "@morpho-org/morpho-sdk";
|
|
374
|
+
* import { isBlueZeroExLeverageChain } from "@morpho-org/morpho-sdk/addresses";
|
|
375
|
+
*
|
|
376
|
+
* // Lever needs a ZeroExLeverageBundlesV1 registered on the chain (built in, or added with
|
|
377
|
+
* // `registerCustomAddresses`); without one the leverage data read throws LeverageUnsupportedChainError.
|
|
378
|
+
* if (!isBlueZeroExLeverageChain(base.id)) throw new Error("No leverage bundle is registered on Base yet.");
|
|
379
|
+
*
|
|
380
|
+
* const client = createPublicClient({ chain: base, transport: http() })
|
|
381
|
+
* .extend(morphoViemExtension());
|
|
382
|
+
* const marketParams = markets[base.id].usdc_eth;
|
|
383
|
+
* const market = client.morpho.blue(marketParams, base.id);
|
|
384
|
+
* const slippage = 5_000_000_000_000_000n;
|
|
385
|
+
*
|
|
386
|
+
* const leverageData = await market.getLeverageData({ userAddress });
|
|
387
|
+
* const quote = await fetchZeroExQuote({
|
|
388
|
+
* chainId: base.id,
|
|
389
|
+
* kind: "lever",
|
|
390
|
+
* marketParams,
|
|
391
|
+
* userAddress,
|
|
392
|
+
* amount: 2n * 10n ** 18n,
|
|
393
|
+
* slippage,
|
|
394
|
+
* deadline: leverageDeadline(leverageData), // the deadline the Lever signs
|
|
395
|
+
* apiKey,
|
|
396
|
+
* });
|
|
397
|
+
* const lever = market.lever({
|
|
398
|
+
* userAddress,
|
|
399
|
+
* leverageData,
|
|
400
|
+
* quote,
|
|
401
|
+
* slippage,
|
|
402
|
+
* collateralAssets: 10n ** 18n,
|
|
403
|
+
* loanAssets: 0n,
|
|
404
|
+
* });
|
|
405
|
+
* const batch = [...(await lever.getRequirements()), lever.buildTx()];
|
|
406
|
+
* ```
|
|
407
|
+
*/
|
|
408
|
+
lever: (params: {
|
|
409
|
+
readonly userAddress: Address;
|
|
410
|
+
readonly txOrigin?: Address;
|
|
411
|
+
readonly leverageData: BlueLeverageData;
|
|
412
|
+
readonly quote: ZeroExQuote;
|
|
413
|
+
readonly slippage: bigint;
|
|
414
|
+
readonly collateralAssets: bigint;
|
|
415
|
+
readonly loanAssets: bigint;
|
|
416
|
+
readonly nativeAmount?: bigint;
|
|
417
|
+
readonly reallocationOptions?: Omit<VaultV2BluePublicAllocatorOptions, "timestamp">;
|
|
418
|
+
} & ReferenceTimestampParams) => LeverageActionOutput<BlueLeverAction>;
|
|
419
|
+
/**
|
|
420
|
+
* Prepares a ZeroExLeverageBundlesV1 Delever ("pay back with collateral"): sell up to `quote.sellAmount` of
|
|
421
|
+
* the position's collateral through 0x and repay at least the quote's `buyAmount` minus slippage of its debt.
|
|
422
|
+
*
|
|
423
|
+
* The transaction is built eagerly and synchronously by {@link blueDelever}, so every guard runs when the
|
|
424
|
+
* handle is created and a failing one throws instead of returning a handle. `maxLtv` is the position's
|
|
425
|
+
* LTV with its debt accrued to the deadline (data block plus one hour) at the leverage data price, not the LTV
|
|
426
|
+
* the position shows now, so a position already past the LLTV buffer can still delever; the build refuses when the
|
|
427
|
+
* worst fill would raise the LTV. A repay above the debt refuses with `RepayExceedsDebtError`: offer the no-swap
|
|
428
|
+
* `repayWithdrawCollateral` exit instead.
|
|
429
|
+
* No reallocation is involved.
|
|
430
|
+
*
|
|
431
|
+
* The SDK does not refuse on the quoted price: `action.args.oracleDeviation` reports the worst fill's deviation
|
|
432
|
+
* from the oracle; show it and `maxCollateralToSell` before the user signs.
|
|
433
|
+
*
|
|
434
|
+
* Delever pulls no token from the user. `getRequirements()` returns the Morpho authorization of the bundle
|
|
435
|
+
* when missing; it persists after the Delever.
|
|
436
|
+
*
|
|
437
|
+
* @param params.userAddress - Wallet that owns the position and calls the bundle (`msg.sender`), such as the smart account itself; must own `leverageData.position`.
|
|
438
|
+
* @param params.txOrigin - Sender of the outer onchain transaction (its `tx.origin`) when it is not `userAddress`:
|
|
439
|
+
* the owner EOA that executes a Safe transaction, or the bundler that submits an ERC-4337 UserOperation, not the
|
|
440
|
+
* wallet's signer. Fetch the quote with the same `txOrigin`. Approvals, authorization and the position
|
|
441
|
+
* stay `userAddress`'s. Defaults to `userAddress`.
|
|
442
|
+
* @param params.leverageData - Unmodified result of {@link BlueActions.getLeverageData} for this market and chain.
|
|
443
|
+
* @param params.quote - Exact-input 0x quote from `fetchZeroExQuote` with kind `delever`.
|
|
444
|
+
* @param params.slippage - Slippage passed to 0x, WAD-scaled, at most 10%.
|
|
445
|
+
* @param params.now - Current timestamp in seconds (the latest block's) for the deadline check;
|
|
446
|
+
* `getRequirements()` advances it by the device time elapsed since the handle was built. The device
|
|
447
|
+
* clock by default. Never pass the leverage data's own timestamp.
|
|
448
|
+
* @returns Lazy requirement resolution and the synchronous deep-frozen transaction.
|
|
449
|
+
* @throws {ChainIdMismatchError} when the client or the leverage data targets another chain, or from
|
|
450
|
+
* `getRequirements()` when the RPC reports another chain via `eth_chainId`.
|
|
451
|
+
* @throws {MarketIdMismatchError} when the leverage data market or the leverage data position is for another market,
|
|
452
|
+
* or the position's market tuple does not hash to its cached id or names another market.
|
|
453
|
+
* @throws {InvalidBigIntInputError} when `slippage`, `quote.slippage` or a supplied `now` is not a bigint.
|
|
454
|
+
* @throws {NegativeInputError} when `now` or `slippage` is negative.
|
|
455
|
+
* @throws {ExpiredLeverageDeadlineError} (an `ExpiredDeadlineError`) when the leverage data is more than one hour
|
|
456
|
+
* old, at creation or in `getRequirements()`: read the leverage data again, fetch a new quote and rebuild.
|
|
457
|
+
* @throws {AccrualPositionUserMismatchError} when the leverage data position belongs to another user.
|
|
458
|
+
* @throws {LeverageOraclePriceUnavailableError} when the leverage data has no usable market oracle price.
|
|
459
|
+
* @throws {ZeroExQuoteRequiredError} when no quote is given.
|
|
460
|
+
* @throws {LeverageQuoteSlippageMismatchError} when the quote was fetched with another slippage than `slippage`.
|
|
461
|
+
* @throws {ZeroExQuoteMinBuyAmountBelowSlippageError} when the quote's `minBuyAmount` is zero or below its
|
|
462
|
+
* `buyAmount` less `slippage`, rounded down to whole parts per million: fetch a new quote with `slippage`.
|
|
463
|
+
* @throws {RepayExceedsDebtError} when the repay floor exceeds the debt; offer the no-swap
|
|
464
|
+
* `repayWithdrawCollateral` exit instead.
|
|
465
|
+
* @throws {LeverageLimitError} when the quote, slippage or worst-fill LTV fails a limit
|
|
466
|
+
* (see {@link blueDelever}).
|
|
467
|
+
* @throws {ZeroExInvalidTxOriginError} when `txOrigin` is given and is not a valid address; pass a valid one or omit
|
|
468
|
+
* it instead of fetching a new quote.
|
|
469
|
+
* @throws {ZeroExQuoteError} when the quote's calldata does not match the Delever (see `validateZeroExQuote`).
|
|
470
|
+
* @throws {MissingContractCodeError} from `getRequirements()` when Morpho Blue has no code, or when a leverage
|
|
471
|
+
* bundle registered with `registerCustomAddresses` has none; the shipped bundle's code is not read.
|
|
472
|
+
* @throws {viem.BaseError} from `getRequirements()` when the `eth_chainId` or authorization read fails.
|
|
473
|
+
* @example
|
|
474
|
+
* ```ts
|
|
475
|
+
* import { markets } from "@morpho-org/morpho-test";
|
|
476
|
+
* import { type Address, createPublicClient, http } from "viem";
|
|
477
|
+
* import { base } from "viem/chains";
|
|
478
|
+
* import {
|
|
479
|
+
* fetchZeroExQuote,
|
|
480
|
+
* leverageDeadline,
|
|
481
|
+
* morphoViemExtension,
|
|
482
|
+
* } from "@morpho-org/morpho-sdk";
|
|
483
|
+
* import { isBlueZeroExLeverageChain } from "@morpho-org/morpho-sdk/addresses";
|
|
484
|
+
*
|
|
485
|
+
* // Delever needs a ZeroExLeverageBundlesV1 registered on the chain; without one the leverage data read
|
|
486
|
+
* // throws LeverageUnsupportedChainError.
|
|
487
|
+
* if (!isBlueZeroExLeverageChain(8453)) throw new Error("No leverage bundle is registered on Base yet.");
|
|
488
|
+
* declare const userAddress: Address; // owner of the position to delever
|
|
489
|
+
* const marketParams = markets[base.id].usdc_eth;
|
|
490
|
+
* const client = createPublicClient({ chain: base, transport: http() });
|
|
491
|
+
* const market = client.extend(morphoViemExtension()).morpho.blue(marketParams, 8453);
|
|
492
|
+
* const leverageData = await market.getLeverageData({ userAddress });
|
|
493
|
+
* const quote = await fetchZeroExQuote({
|
|
494
|
+
* chainId: 8453,
|
|
495
|
+
* kind: "delever",
|
|
496
|
+
* marketParams,
|
|
497
|
+
* userAddress,
|
|
498
|
+
* amount: 10n ** 17n,
|
|
499
|
+
* slippage: 5_000_000_000_000_000n,
|
|
500
|
+
* deadline: leverageDeadline(leverageData), // the deadline the Delever signs
|
|
501
|
+
* apiKey: process.env.ZEROEX_API_KEY!,
|
|
502
|
+
* });
|
|
503
|
+
* const action = market.delever({
|
|
504
|
+
* userAddress,
|
|
505
|
+
* leverageData,
|
|
506
|
+
* quote,
|
|
507
|
+
* slippage: 5_000_000_000_000_000n,
|
|
508
|
+
* now: (await client.getBlock()).timestamp,
|
|
509
|
+
* });
|
|
510
|
+
* // Collect the requirements right away; to send them later, read the leverage data and quote again and rebuild.
|
|
511
|
+
* const calls = [...(await action.getRequirements()), action.buildTx()];
|
|
512
|
+
* ```
|
|
513
|
+
*/
|
|
514
|
+
delever: (params: {
|
|
515
|
+
readonly userAddress: Address;
|
|
516
|
+
readonly txOrigin?: Address;
|
|
517
|
+
readonly leverageData: BlueLeverageData;
|
|
518
|
+
readonly quote: ZeroExQuote;
|
|
519
|
+
readonly slippage: bigint;
|
|
520
|
+
} & ReferenceTimestampParams) => LeverageActionOutput<BlueDeleverAction>;
|
|
74
521
|
/**
|
|
75
522
|
* Prepares a direct BlueBundlesV1 collateral supply.
|
|
76
523
|
*
|
|
@@ -1091,7 +1538,10 @@ export declare class MorphoBlue implements BlueActions {
|
|
|
1091
1538
|
* @throws {MarketIdMismatchError} when `marketParams.id` does not hash from its tuple fields.
|
|
1092
1539
|
*/
|
|
1093
1540
|
constructor(client: MorphoClientType, marketParams: MarketParams, chainId: number);
|
|
1541
|
+
private validateLeverageNow;
|
|
1542
|
+
private elapsedLeverageNow;
|
|
1094
1543
|
private validateDeadline;
|
|
1544
|
+
private validateLeverageDeadline;
|
|
1095
1545
|
private validateWriteCommon;
|
|
1096
1546
|
/**
|
|
1097
1547
|
* Asserts that every registry contract this flow's transaction targets or names as
|
|
@@ -1108,6 +1558,40 @@ export declare class MorphoBlue implements BlueActions {
|
|
|
1108
1558
|
private getAuthorizationRequirements;
|
|
1109
1559
|
getMarketData(parameters?: FetchParameters): Promise<Market>;
|
|
1110
1560
|
getPositionData(userAddress: Address, parameters?: FetchParameters): Promise<AccrualPosition>;
|
|
1561
|
+
/** {@inheritDoc BlueActions.getLeverageData} */
|
|
1562
|
+
getLeverageData({ userAddress, vaultAddresses, blockNumber, }: {
|
|
1563
|
+
readonly userAddress: Address;
|
|
1564
|
+
readonly vaultAddresses?: Iterable<Address>;
|
|
1565
|
+
readonly blockNumber?: bigint;
|
|
1566
|
+
}): Promise<BlueLeverageData>;
|
|
1567
|
+
/** {@inheritDoc BlueActions.lever} */
|
|
1568
|
+
lever({ userAddress, txOrigin, leverageData, quote, slippage, collateralAssets, loanAssets, nativeAmount, reallocationOptions, now, }: {
|
|
1569
|
+
readonly userAddress: Address;
|
|
1570
|
+
readonly txOrigin?: Address;
|
|
1571
|
+
readonly leverageData: BlueLeverageData;
|
|
1572
|
+
readonly quote: ZeroExQuote;
|
|
1573
|
+
readonly slippage: bigint;
|
|
1574
|
+
readonly collateralAssets: bigint;
|
|
1575
|
+
readonly loanAssets: bigint;
|
|
1576
|
+
readonly nativeAmount?: bigint;
|
|
1577
|
+
readonly reallocationOptions?: Omit<VaultV2BluePublicAllocatorOptions, "timestamp">;
|
|
1578
|
+
} & ReferenceTimestampParams): LeverageActionOutput<BlueLeverAction>;
|
|
1579
|
+
/** {@inheritDoc BlueActions.delever} */
|
|
1580
|
+
delever(params: {
|
|
1581
|
+
readonly userAddress: Address;
|
|
1582
|
+
readonly txOrigin?: Address;
|
|
1583
|
+
readonly leverageData: BlueLeverageData;
|
|
1584
|
+
readonly quote: ZeroExQuote;
|
|
1585
|
+
readonly slippage: bigint;
|
|
1586
|
+
} & ReferenceTimestampParams): LeverageActionOutput<BlueDeleverAction>;
|
|
1587
|
+
/** {@inheritDoc BlueActions.close} */
|
|
1588
|
+
close(params: {
|
|
1589
|
+
readonly userAddress: Address;
|
|
1590
|
+
readonly txOrigin?: Address;
|
|
1591
|
+
readonly leverageData: BlueLeverageData;
|
|
1592
|
+
readonly quote?: ZeroExQuote;
|
|
1593
|
+
readonly slippage: bigint;
|
|
1594
|
+
} & ReferenceTimestampParams): LeverageActionOutput<BlueCloseAction>;
|
|
1111
1595
|
/** {@inheritDoc BlueActions.supply} */
|
|
1112
1596
|
supply(params: {
|
|
1113
1597
|
readonly userAddress: Address;
|