@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,272 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.blueLever = void 0;
|
|
4
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
5
|
+
const viem_1 = require("viem");
|
|
6
|
+
const abis_js_1 = require("../../abis.js");
|
|
7
|
+
const constant_js_1 = require("../../helpers/constant.js");
|
|
8
|
+
const limits_js_1 = require("../../helpers/leverage/limits.js");
|
|
9
|
+
const positionMarket_js_1 = require("../../helpers/leverage/positionMarket.js");
|
|
10
|
+
const reallocations_js_1 = require("../../helpers/leverage/reallocations.js");
|
|
11
|
+
const metadata_js_1 = require("../../helpers/metadata.js");
|
|
12
|
+
const parse_js_1 = require("../../helpers/parse.js");
|
|
13
|
+
const validate_js_1 = require("../../helpers/validate.js");
|
|
14
|
+
const zeroExQuote_js_1 = require("../../helpers/zeroExQuote.js");
|
|
15
|
+
const index_js_1 = require("../../types/index.js");
|
|
16
|
+
const common_js_1 = require("./common.js");
|
|
17
|
+
const leverageIntent_js_1 = require("./leverageIntent.js");
|
|
18
|
+
/**
|
|
19
|
+
* Encodes a ZeroExLeverageBundlesV1 Lever: pulls `collateralAssets` and `loanAssets` from the user,
|
|
20
|
+
* executes the planned BluePublicAllocator reallocations, borrows `maxBorrowAssets` and buys at least
|
|
21
|
+
* the quote's `buyAmount` (`minCollateralToBuy`) of collateral through 0x, all in one call to the chain's
|
|
22
|
+
* registered bundle. The bundle also accepts a 0x SELL quote, but the SDK builds Lever from exact-output quotes only.
|
|
23
|
+
*
|
|
24
|
+
* With a positive `nativeAmount`, the transaction sends it as `value` and the bundle wraps it into the chain's
|
|
25
|
+
* wNative (`wrappedNative`) and transfers it to the user before the pulls, so the user's wNative approval still
|
|
26
|
+
* covers the whole pull. Without it, `value` is zero and `wrappedNative` the zero address.
|
|
27
|
+
*
|
|
28
|
+
* Every bound is computed, none is taken from the caller:
|
|
29
|
+
* - `computeLeverLimits` takes the quote's `maxSellAmount` as the swap cap (0x already applied the
|
|
30
|
+
* slippage, and the Settler pulls that whole amount) and derives the borrow and `maxLtv` (LLTV minus `DEFAULT_LLTV_BUFFER`, never `type(uint256).max`). It
|
|
31
|
+
* refuses a worst fill that fails Blue's mid-flow health check (`check: "health"`) or `maxLtv`
|
|
32
|
+
* (`check: "maxLtv"`), refuses a worst swap price (`maxSwapIn` per `minCollateralToBuy`) more than
|
|
33
|
+
* `LEVERAGE_MAX_ORACLE_DEVIATION` (15%) above the market's oracle price (a lower price is never refused), and fixes the deadline one hour
|
|
34
|
+
* after the data block. The price and the debt are the data block's; until the deadline only the signed `maxLtv`
|
|
35
|
+
* and Blue's health check guard execution. The deviation compares `maxSwapIn` alone: reallocation penalties
|
|
36
|
+
* (`penaltyAssets`, at most 1% each) are paid on top.
|
|
37
|
+
* - `penaltyAssets` is each reallocation's penalty rounded up, then summed, as the bundle checks it. A Lever
|
|
38
|
+
* that borrows nothing takes no reallocation, as the bundle reverts with `InconsistentBorrowInput` otherwise.
|
|
39
|
+
* `validateLeverageReallocations` refuses a reallocation to another market, through a route the
|
|
40
|
+
* allocator does not allow at the data block, above the 1% penalty ceiling, or reallocations
|
|
41
|
+
* that move more than the borrow. It does not check remaining caps or IRM compatibility, so pass
|
|
42
|
+
* only reallocations `VaultV2BlueReallocationData` planned for this borrow, as `MorphoBlue.lever`
|
|
43
|
+
* does.
|
|
44
|
+
* - `validateZeroExQuote` decodes the swap against the market's tokens, the bundle and the swap cap. It does not
|
|
45
|
+
* check whether the swap's Settler is 0x's official one or paused; the bundle's `maxIn` bounds the swap.
|
|
46
|
+
*
|
|
47
|
+
* Each guard's typed error surfaces unchanged and nothing is encoded when one fails. The builder is
|
|
48
|
+
* synchronous and pure: identical inputs give identical calldata.
|
|
49
|
+
*
|
|
50
|
+
* At most one ZeroExLeverageBundlesV1 call succeeds per transaction: bundles `3bf5314` keeps its transient initiator
|
|
51
|
+
* set until the transaction ends (L36, L49), so a second Lever, Delever or Close to the bundle in the same smart-account
|
|
52
|
+
* batch or ERC-4337 bundle reverts with `Reentrancy`. Send each one in its own transaction.
|
|
53
|
+
*
|
|
54
|
+
* @param params - Lever encoding parameters.
|
|
55
|
+
* @param params.market.chainId - Chain of the market and the bundle.
|
|
56
|
+
* @param params.market.marketParams - Scoped Morpho Blue market parameters.
|
|
57
|
+
* @param params.args.userAddress - Wallet that owns the position and calls the bundle (`msg.sender`), such as the smart account itself.
|
|
58
|
+
* @param params.args.txOrigin - Optional sender of the outer onchain transaction (its `tx.origin`) when it is not
|
|
59
|
+
* `userAddress`, such as a Safe's executing owner or an ERC-4337 bundler. The quote must have been fetched with
|
|
60
|
+
* the same value. Defaults to `userAddress`.
|
|
61
|
+
* @param params.args.leverageData - One-block leverage data from `MorphoBlue.getLeverageData`.
|
|
62
|
+
* @param params.args.quote - Exact-output 0x quote for the collateral to buy.
|
|
63
|
+
* @param params.args.slippage - Caller slippage, WAD-scaled, at most 10%.
|
|
64
|
+
* @param params.args.collateralAssets - Collateral the user deposits.
|
|
65
|
+
* @param params.args.loanAssets - Loan assets the user deposits.
|
|
66
|
+
* @param params.args.reallocations - Planner-built reallocations toward this market.
|
|
67
|
+
* @param params.args.nativeAmount - Optional native token the bundle wraps into wNative before the pulls.
|
|
68
|
+
* @param params.metadata - Optional transaction metadata.
|
|
69
|
+
* @returns A deep-frozen `Readonly<Transaction<BlueLeverAction>>` calling
|
|
70
|
+
* `zeroExLeverageBundlesV1Lever` on the registered bundle.
|
|
71
|
+
* @throws {MarketIdMismatchError} when `marketParams` or the position's market tuple does not hash to its
|
|
72
|
+
* cached id, or the leverage data position or its market tuple is for another market.
|
|
73
|
+
* @throws {ChainIdMismatchError} when the leverage data was read on another chain.
|
|
74
|
+
* @throws {AccrualPositionUserMismatchError} when the leverage data position belongs to another user.
|
|
75
|
+
* @throws {InvalidBigIntInputError} when `collateralAssets`, `loanAssets`, `nativeAmount`, `slippage`,
|
|
76
|
+
* `quote.slippage`, or a reallocation's `assets` or `penalty` is not a bigint.
|
|
77
|
+
* @throws {NegativeInputError} when an intent amount, the slippage or a reallocation penalty is negative.
|
|
78
|
+
* @throws {InputExceedsMaxError} when an intent amount, a signed amount or the deadline exceeds `uint256`, when
|
|
79
|
+
* `loanAssets` exceeds the swap cap plus the penalty, when `nativeAmount` exceeds the pull of the wNative
|
|
80
|
+
* token, or when a reallocation exceeds its ABI bound.
|
|
81
|
+
* @throws {ZeroExQuoteTypeMismatchError} when the quote is not exact output.
|
|
82
|
+
* @throws {ZeroExQuoteRequiredError} when no quote is given.
|
|
83
|
+
* @throws {LeverageQuoteSlippageMismatchError} when the quote was fetched with another slippage than `slippage`.
|
|
84
|
+
* @throws {LeverageSlippageAboveLimitError} when `slippage` exceeds 10%.
|
|
85
|
+
* @throws {LeverageWorstFillLtvError} when the worst fill fails Blue's health check or `maxLtv`.
|
|
86
|
+
* @throws {LeverageOraclePriceDeviationError} when the quote's worst price is more than 15% above the market's
|
|
87
|
+
* oracle price.
|
|
88
|
+
* @throws {LeverageOraclePriceUnavailableError} when the market's oracle price is unknown or zero.
|
|
89
|
+
* @throws {NonPositiveInputError} when a quote amount, a reallocation amount or the deadline is not positive.
|
|
90
|
+
* @throws {LeverageReallocationMarketMismatchError} when a reallocation targets another market.
|
|
91
|
+
* @throws {LeverageReallocationNoFlowError} when the allocator does not allow a reallocation's route.
|
|
92
|
+
* @throws {LeverageReallocationPenaltyAboveLimitError} when a reallocation's penalty rate exceeds 1%.
|
|
93
|
+
* @throws {LeverageReallocationExceedsBorrowError} when the reallocations move more than the borrow, including any
|
|
94
|
+
* reallocation of a Lever that borrows nothing.
|
|
95
|
+
* @throws {NativeAmountOnNonWNativeAssetError} when `nativeAmount` is positive and neither market token is the
|
|
96
|
+
* chain's wNative.
|
|
97
|
+
* @throws {ChainWNativeMissingError} when `nativeAmount` is positive and the chain registers no wNative.
|
|
98
|
+
* @throws {LeverageInsufficientLiquidityError} when the market's liquidity at the data block plus
|
|
99
|
+
* the reallocated assets cannot fund the borrow. Passing it does not make the Lever land: when
|
|
100
|
+
* `penaltyAssets` exceeds `loanAssets`, the bundle flash-loans the difference from Blue before it borrows, and
|
|
101
|
+
* the SDK does not check Blue's loan-token balance, so a Lever that pays a penalty can still revert onchain when
|
|
102
|
+
* Blue is short of cash.
|
|
103
|
+
* @throws {InconsistentReallocationPenaltyError} when a reallocation's penalty is not its vault's.
|
|
104
|
+
* @throws {ReallocationWithdrawalOnTargetMarketError} when a reallocation withdraws from the Lever's market.
|
|
105
|
+
* @throws {ReallocationLoanTokenMismatchError} when a reallocation's source uses another loan token.
|
|
106
|
+
* @throws {ZeroExQuoteAmountExceedsCapError} when the amount the quote's calldata lets Settler pull
|
|
107
|
+
* exceeds the swap cap.
|
|
108
|
+
* @throws {ZeroExInvalidTxOriginError} when `txOrigin` is given and is not a valid address; pass a valid one or omit it
|
|
109
|
+
* instead of fetching a new quote.
|
|
110
|
+
* @throws {ZeroExQuoteError} for any other quote mismatch documented on `validateZeroExQuote`.
|
|
111
|
+
* @throws {LeverageUnsupportedChainError} when the chain has no usable leverage bundle.
|
|
112
|
+
* @example
|
|
113
|
+
* ```ts
|
|
114
|
+
* import { blueLever, fetchZeroExQuote, leverageDeadline } from "@morpho-org/morpho-sdk";
|
|
115
|
+
*
|
|
116
|
+
* // leverageData = await client.morpho.blue(marketParams, chainId).getLeverageData({ userAddress });
|
|
117
|
+
* const quote = await fetchZeroExQuote({
|
|
118
|
+
* chainId,
|
|
119
|
+
* kind: "lever",
|
|
120
|
+
* marketParams,
|
|
121
|
+
* userAddress,
|
|
122
|
+
* amount: 2n * 10n ** 18n,
|
|
123
|
+
* slippage: 5_000_000_000_000_000n, // 0.5%, WAD-scaled (1e18 = 100%), not bps
|
|
124
|
+
* deadline: leverageDeadline(leverageData), // the deadline the Lever signs
|
|
125
|
+
* apiKey,
|
|
126
|
+
* });
|
|
127
|
+
* const tx = blueLever({
|
|
128
|
+
* market: { chainId, marketParams },
|
|
129
|
+
* args: {
|
|
130
|
+
* userAddress,
|
|
131
|
+
* leverageData,
|
|
132
|
+
* quote,
|
|
133
|
+
* slippage: 5_000_000_000_000_000n,
|
|
134
|
+
* collateralAssets: 10n ** 18n,
|
|
135
|
+
* loanAssets: 0n,
|
|
136
|
+
* },
|
|
137
|
+
* });
|
|
138
|
+
* // tx.to is the registered ZeroExLeverageBundlesV1; tx.action.args.maxBorrowAssets is the borrow.
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
const blueLever = (params) => {
|
|
142
|
+
const { market: { chainId, marketParams }, args: { userAddress, leverageData, quote, slippage, collateralAssets, loanAssets, }, } = params;
|
|
143
|
+
(0, validate_js_1.validateMarketParamsId)(marketParams);
|
|
144
|
+
(0, validate_js_1.validateChainId)(leverageData.chainId, chainId);
|
|
145
|
+
(0, validate_js_1.validateAccrualPosition)({
|
|
146
|
+
positionData: leverageData.position,
|
|
147
|
+
expectedMarketId: marketParams.id,
|
|
148
|
+
expectedUser: userAddress,
|
|
149
|
+
});
|
|
150
|
+
// The bounds come from the position's own market tuple, so it must be the expected market's.
|
|
151
|
+
(0, positionMarket_js_1.validateLeveragePositionMarket)(leverageData.position, marketParams);
|
|
152
|
+
// Checked before the quote's slippage, so a requote advice never names a slippage the quote fetch refuses.
|
|
153
|
+
(0, leverageIntent_js_1.validateLeverSlippage)(slippage);
|
|
154
|
+
if (quote == null) {
|
|
155
|
+
throw new index_js_1.ZeroExQuoteRequiredError({ operation: "lever" });
|
|
156
|
+
}
|
|
157
|
+
(0, leverageIntent_js_1.validateLeverageQuoteSlippage)({
|
|
158
|
+
operation: "lever",
|
|
159
|
+
slippage,
|
|
160
|
+
quoteSlippage: quote.slippage,
|
|
161
|
+
});
|
|
162
|
+
(0, leverageIntent_js_1.validateLeverageIntentAmount)("collateralAssets", collateralAssets);
|
|
163
|
+
(0, leverageIntent_js_1.validateLeverageIntentAmount)("loanAssets", loanAssets);
|
|
164
|
+
const bundle = (0, zeroExQuote_js_1.getZeroExLeverageBundle)(chainId);
|
|
165
|
+
const nativeFunding = (0, leverageIntent_js_1.validateLeverNativeAmount)({
|
|
166
|
+
...params.args,
|
|
167
|
+
chainId,
|
|
168
|
+
marketParams,
|
|
169
|
+
});
|
|
170
|
+
const requested = params.args.reallocations ?? [];
|
|
171
|
+
// The penalty below is computed before validateLeverageReallocations, so refuse non-bigint legs first.
|
|
172
|
+
for (const leg of requested) {
|
|
173
|
+
// Non-object and Vault V1-shaped legs are left to the shared shape check, which throws its typed error.
|
|
174
|
+
if (typeof leg !== "object" ||
|
|
175
|
+
leg === null ||
|
|
176
|
+
"withdrawals" in leg ||
|
|
177
|
+
"fee" in leg) {
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
if (typeof leg.assets !== "bigint") {
|
|
181
|
+
throw new index_js_1.InvalidBigIntInputError("reallocation.assets", leg.assets);
|
|
182
|
+
}
|
|
183
|
+
if (typeof leg.penalty !== "bigint") {
|
|
184
|
+
throw new index_js_1.InvalidBigIntInputError("reallocation.penalty", leg.penalty);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
const { publicAllocations: reallocations } = (0, common_js_1.parseBlueAllocations)(requested, {
|
|
188
|
+
marketParams: (0, parse_js_1.parseBlueMarket)(marketParams),
|
|
189
|
+
maxPenalty: constant_js_1.MAX_REALLOCATION_PENALTY,
|
|
190
|
+
});
|
|
191
|
+
const limits = (0, limits_js_1.computeLeverLimits)({
|
|
192
|
+
intent: { collateralAssets, loanAssets },
|
|
193
|
+
quote,
|
|
194
|
+
slippage,
|
|
195
|
+
leverageData,
|
|
196
|
+
penaltyAssets: (0, reallocations_js_1.computeLeveragePenaltyAssets)(reallocations),
|
|
197
|
+
});
|
|
198
|
+
(0, reallocations_js_1.validateLeverageReallocations)({
|
|
199
|
+
reallocations,
|
|
200
|
+
marketParams,
|
|
201
|
+
allocator: leverageData.reallocationData,
|
|
202
|
+
maxBorrowAssets: limits.maxBorrowAssets,
|
|
203
|
+
});
|
|
204
|
+
const reallocatedAssets = reallocations.reduce((total, { assets }) => total + assets, 0n);
|
|
205
|
+
const { liquidity } = leverageData.position.market;
|
|
206
|
+
if (liquidity + reallocatedAssets < limits.maxBorrowAssets) {
|
|
207
|
+
throw new index_js_1.LeverageInsufficientLiquidityError({
|
|
208
|
+
liquidity,
|
|
209
|
+
reallocatedAssets,
|
|
210
|
+
maxBorrowAssets: limits.maxBorrowAssets,
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
const swap = (0, zeroExQuote_js_1.validateZeroExQuote)({
|
|
214
|
+
quote,
|
|
215
|
+
kind: "lever",
|
|
216
|
+
chainId,
|
|
217
|
+
marketParams,
|
|
218
|
+
userAddress,
|
|
219
|
+
txOrigin: params.args.txOrigin,
|
|
220
|
+
maxSwapInput: limits.maxSwapIn,
|
|
221
|
+
deadline: limits.deadline,
|
|
222
|
+
});
|
|
223
|
+
const transaction = {
|
|
224
|
+
to: bundle,
|
|
225
|
+
value: nativeFunding.value,
|
|
226
|
+
data: (0, viem_1.encodeFunctionData)({
|
|
227
|
+
abi: abis_js_1.zeroExLeverageBundlesV1Abi,
|
|
228
|
+
functionName: "zeroExLeverageBundlesV1Lever",
|
|
229
|
+
args: [
|
|
230
|
+
marketParams,
|
|
231
|
+
collateralAssets,
|
|
232
|
+
loanAssets,
|
|
233
|
+
limits.minCollateralToBuy,
|
|
234
|
+
limits.maxBorrowAssets,
|
|
235
|
+
swap.swapData,
|
|
236
|
+
limits.maxLtv,
|
|
237
|
+
reallocations,
|
|
238
|
+
limits.penaltyAssets,
|
|
239
|
+
limits.deadline,
|
|
240
|
+
nativeFunding.wrappedNative,
|
|
241
|
+
],
|
|
242
|
+
}),
|
|
243
|
+
};
|
|
244
|
+
return (0, morpho_ts_1.deepFreeze)({
|
|
245
|
+
...(params.metadata == null
|
|
246
|
+
? transaction
|
|
247
|
+
: (0, metadata_js_1.addTransactionMetadata)(transaction, params.metadata)),
|
|
248
|
+
action: {
|
|
249
|
+
type: "blueLever",
|
|
250
|
+
args: {
|
|
251
|
+
market: marketParams.id,
|
|
252
|
+
onBehalf: userAddress,
|
|
253
|
+
collateralAssets,
|
|
254
|
+
loanAssets,
|
|
255
|
+
minCollateralToBuy: limits.minCollateralToBuy,
|
|
256
|
+
maxBorrowAssets: limits.maxBorrowAssets,
|
|
257
|
+
maxSwapIn: limits.maxSwapIn,
|
|
258
|
+
maxLtv: limits.maxLtv,
|
|
259
|
+
reallocations: reallocations.length,
|
|
260
|
+
penaltyAssets: limits.penaltyAssets,
|
|
261
|
+
settler: swap.settler,
|
|
262
|
+
oracleDeviation: { status: "measured", ...limits.oracleDeviation },
|
|
263
|
+
deadline: limits.deadline,
|
|
264
|
+
block: {
|
|
265
|
+
number: leverageData.block.number,
|
|
266
|
+
timestamp: leverageData.block.timestamp,
|
|
267
|
+
},
|
|
268
|
+
},
|
|
269
|
+
},
|
|
270
|
+
});
|
|
271
|
+
};
|
|
272
|
+
exports.blueLever = blueLever;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { MarketParams } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { LeverageQuoteSlippageMismatchError } from "../../types/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* Refuses a Lever intent amount that is not a bigint or does not fit in `uint256`, before any bigint
|
|
5
|
+
* arithmetic mixes it with a number.
|
|
6
|
+
*
|
|
7
|
+
* @param field - Intent field name reported in the error.
|
|
8
|
+
* @param value - Intent amount.
|
|
9
|
+
* @throws {InvalidBigIntInputError} when `value` is not a bigint.
|
|
10
|
+
* @throws {NegativeInputError} when `value` is negative.
|
|
11
|
+
* @throws {InputExceedsMaxError} when `value` exceeds `uint256`.
|
|
12
|
+
*/
|
|
13
|
+
export declare const validateLeverageIntentAmount: (field: string, value: bigint) => void;
|
|
14
|
+
/**
|
|
15
|
+
* Refuses a Lever slippage that is not a bigint, is negative or exceeds the Lever's 10% limit. Callers run it
|
|
16
|
+
* before comparing the slippage with the quote's, so a requote advice never names a slippage the quote fetch
|
|
17
|
+
* refuses.
|
|
18
|
+
*
|
|
19
|
+
* @param slippage - Caller slippage, WAD-scaled.
|
|
20
|
+
* @throws {InvalidBigIntInputError} when `slippage` is not a bigint.
|
|
21
|
+
* @throws {NegativeInputError} when `slippage` is negative.
|
|
22
|
+
* @throws {LeverageSlippageAboveLimitError} when `slippage` exceeds `LEVERAGE_MAX_EXECUTION_LOSS.lever`.
|
|
23
|
+
*/
|
|
24
|
+
export declare const validateLeverSlippage: (slippage: bigint) => void;
|
|
25
|
+
/**
|
|
26
|
+
* Refuses a quote fetched with another slippage than the caller's. The quote's slippage is checked to be a bigint
|
|
27
|
+
* first, so a missing or malformed value is reported as such rather than as a slippage mismatch.
|
|
28
|
+
*
|
|
29
|
+
* @param params.operation - Leverage action the quote is for.
|
|
30
|
+
* @param params.slippage - Caller slippage, WAD-scaled, already validated.
|
|
31
|
+
* @param params.quoteSlippage - Slippage the quote was fetched with.
|
|
32
|
+
* @throws {InvalidBigIntInputError} when `quoteSlippage` is not a bigint (field `quote.slippage`).
|
|
33
|
+
* @throws {LeverageQuoteSlippageMismatchError} when `quoteSlippage` is not `slippage`.
|
|
34
|
+
*/
|
|
35
|
+
export declare const validateLeverageQuoteSlippage: (params: {
|
|
36
|
+
readonly operation: LeverageQuoteSlippageMismatchError["operation"];
|
|
37
|
+
readonly slippage: bigint;
|
|
38
|
+
readonly quoteSlippage: bigint;
|
|
39
|
+
}) => void;
|
|
40
|
+
/**
|
|
41
|
+
* Validates a Lever's `nativeAmount` and returns the transaction `value` and the `wrappedNative` argument. The bundle
|
|
42
|
+
* wraps `nativeAmount` into the chain's wNative for the user before pulling `collateralAssets` and `loanAssets`.
|
|
43
|
+
*
|
|
44
|
+
* @param params.chainId - Chain used to resolve wNative.
|
|
45
|
+
* @param params.marketParams - Market whose collateral and loan tokens the Lever pulls.
|
|
46
|
+
* @param params.collateralAssets - Collateral pulled from the user.
|
|
47
|
+
* @param params.loanAssets - Loan assets pulled from the user.
|
|
48
|
+
* @param params.nativeAmount - Optional native token to wrap; omitted or zero sends no value.
|
|
49
|
+
* @returns The transaction `value` and the `wrappedNative` Lever argument (`zeroAddress` without native).
|
|
50
|
+
* @throws {InvalidBigIntInputError} when `nativeAmount` is given and is not a bigint.
|
|
51
|
+
* @throws {NegativeInputError} when `nativeAmount` is negative.
|
|
52
|
+
* @throws {ChainWNativeMissingError} when the chain has no wNative.
|
|
53
|
+
* @throws {NativeAmountOnNonWNativeAssetError} when neither market token is wNative.
|
|
54
|
+
* @throws {InputExceedsMaxError} when `nativeAmount` exceeds the wNative pull.
|
|
55
|
+
*/
|
|
56
|
+
export declare const validateLeverNativeAmount: ({ chainId, marketParams, collateralAssets, loanAssets, nativeAmount, }: {
|
|
57
|
+
readonly chainId: number;
|
|
58
|
+
readonly marketParams: MarketParams;
|
|
59
|
+
readonly collateralAssets: bigint;
|
|
60
|
+
readonly loanAssets: bigint;
|
|
61
|
+
readonly nativeAmount?: bigint;
|
|
62
|
+
}) => {
|
|
63
|
+
value: bigint;
|
|
64
|
+
wrappedNative: import("viem").Address;
|
|
65
|
+
};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateLeverNativeAmount = exports.validateLeverageQuoteSlippage = exports.validateLeverSlippage = exports.validateLeverageIntentAmount = void 0;
|
|
4
|
+
const constant_js_1 = require("../../helpers/constant.js");
|
|
5
|
+
const validate_js_1 = require("../../helpers/validate.js");
|
|
6
|
+
const index_js_1 = require("../../types/index.js");
|
|
7
|
+
/**
|
|
8
|
+
* Refuses a Lever intent amount that is not a bigint or does not fit in `uint256`, before any bigint
|
|
9
|
+
* arithmetic mixes it with a number.
|
|
10
|
+
*
|
|
11
|
+
* @param field - Intent field name reported in the error.
|
|
12
|
+
* @param value - Intent amount.
|
|
13
|
+
* @throws {InvalidBigIntInputError} when `value` is not a bigint.
|
|
14
|
+
* @throws {NegativeInputError} when `value` is negative.
|
|
15
|
+
* @throws {InputExceedsMaxError} when `value` exceeds `uint256`.
|
|
16
|
+
*/
|
|
17
|
+
const validateLeverageIntentAmount = (field, value) => {
|
|
18
|
+
if (typeof value !== "bigint") {
|
|
19
|
+
throw new index_js_1.InvalidBigIntInputError(field, value);
|
|
20
|
+
}
|
|
21
|
+
(0, validate_js_1.validateUint256Field)(field, value);
|
|
22
|
+
};
|
|
23
|
+
exports.validateLeverageIntentAmount = validateLeverageIntentAmount;
|
|
24
|
+
/**
|
|
25
|
+
* Refuses a Lever slippage that is not a bigint, is negative or exceeds the Lever's 10% limit. Callers run it
|
|
26
|
+
* before comparing the slippage with the quote's, so a requote advice never names a slippage the quote fetch
|
|
27
|
+
* refuses.
|
|
28
|
+
*
|
|
29
|
+
* @param slippage - Caller slippage, WAD-scaled.
|
|
30
|
+
* @throws {InvalidBigIntInputError} when `slippage` is not a bigint.
|
|
31
|
+
* @throws {NegativeInputError} when `slippage` is negative.
|
|
32
|
+
* @throws {LeverageSlippageAboveLimitError} when `slippage` exceeds `LEVERAGE_MAX_EXECUTION_LOSS.lever`.
|
|
33
|
+
*/
|
|
34
|
+
const validateLeverSlippage = (slippage) => {
|
|
35
|
+
if (typeof slippage !== "bigint") {
|
|
36
|
+
throw new index_js_1.InvalidBigIntInputError("slippage", slippage);
|
|
37
|
+
}
|
|
38
|
+
if (slippage < 0n) {
|
|
39
|
+
throw new index_js_1.NegativeInputError("slippage", slippage);
|
|
40
|
+
}
|
|
41
|
+
if (slippage > constant_js_1.LEVERAGE_MAX_EXECUTION_LOSS.lever) {
|
|
42
|
+
throw new index_js_1.LeverageSlippageAboveLimitError({
|
|
43
|
+
operation: "lever",
|
|
44
|
+
slippage,
|
|
45
|
+
max: constant_js_1.LEVERAGE_MAX_EXECUTION_LOSS.lever,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
exports.validateLeverSlippage = validateLeverSlippage;
|
|
50
|
+
/**
|
|
51
|
+
* Refuses a quote fetched with another slippage than the caller's. The quote's slippage is checked to be a bigint
|
|
52
|
+
* first, so a missing or malformed value is reported as such rather than as a slippage mismatch.
|
|
53
|
+
*
|
|
54
|
+
* @param params.operation - Leverage action the quote is for.
|
|
55
|
+
* @param params.slippage - Caller slippage, WAD-scaled, already validated.
|
|
56
|
+
* @param params.quoteSlippage - Slippage the quote was fetched with.
|
|
57
|
+
* @throws {InvalidBigIntInputError} when `quoteSlippage` is not a bigint (field `quote.slippage`).
|
|
58
|
+
* @throws {LeverageQuoteSlippageMismatchError} when `quoteSlippage` is not `slippage`.
|
|
59
|
+
*/
|
|
60
|
+
const validateLeverageQuoteSlippage = (params) => {
|
|
61
|
+
const { operation, slippage, quoteSlippage } = params;
|
|
62
|
+
if (typeof quoteSlippage !== "bigint") {
|
|
63
|
+
throw new index_js_1.InvalidBigIntInputError("quote.slippage", quoteSlippage);
|
|
64
|
+
}
|
|
65
|
+
if (quoteSlippage !== slippage) {
|
|
66
|
+
throw new index_js_1.LeverageQuoteSlippageMismatchError({
|
|
67
|
+
operation,
|
|
68
|
+
slippage,
|
|
69
|
+
quoteSlippage,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
exports.validateLeverageQuoteSlippage = validateLeverageQuoteSlippage;
|
|
74
|
+
/**
|
|
75
|
+
* Validates a Lever's `nativeAmount` and returns the transaction `value` and the `wrappedNative` argument. The bundle
|
|
76
|
+
* wraps `nativeAmount` into the chain's wNative for the user before pulling `collateralAssets` and `loanAssets`.
|
|
77
|
+
*
|
|
78
|
+
* @param params.chainId - Chain used to resolve wNative.
|
|
79
|
+
* @param params.marketParams - Market whose collateral and loan tokens the Lever pulls.
|
|
80
|
+
* @param params.collateralAssets - Collateral pulled from the user.
|
|
81
|
+
* @param params.loanAssets - Loan assets pulled from the user.
|
|
82
|
+
* @param params.nativeAmount - Optional native token to wrap; omitted or zero sends no value.
|
|
83
|
+
* @returns The transaction `value` and the `wrappedNative` Lever argument (`zeroAddress` without native).
|
|
84
|
+
* @throws {InvalidBigIntInputError} when `nativeAmount` is given and is not a bigint.
|
|
85
|
+
* @throws {NegativeInputError} when `nativeAmount` is negative.
|
|
86
|
+
* @throws {ChainWNativeMissingError} when the chain has no wNative.
|
|
87
|
+
* @throws {NativeAmountOnNonWNativeAssetError} when neither market token is wNative.
|
|
88
|
+
* @throws {InputExceedsMaxError} when `nativeAmount` exceeds the wNative pull.
|
|
89
|
+
*/
|
|
90
|
+
const validateLeverNativeAmount = ({ chainId, marketParams, collateralAssets, loanAssets, nativeAmount, }) => {
|
|
91
|
+
if (nativeAmount !== undefined) {
|
|
92
|
+
(0, exports.validateLeverageIntentAmount)("nativeAmount", nativeAmount);
|
|
93
|
+
}
|
|
94
|
+
return (0, validate_js_1.validateMidnightNativeAmount)({
|
|
95
|
+
chainId,
|
|
96
|
+
nativeAmount,
|
|
97
|
+
pulls: [
|
|
98
|
+
{ token: marketParams.collateralToken, assets: collateralAssets },
|
|
99
|
+
{ token: marketParams.loanToken, assets: loanAssets },
|
|
100
|
+
],
|
|
101
|
+
});
|
|
102
|
+
};
|
|
103
|
+
exports.validateLeverNativeAmount = validateLeverNativeAmount;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type Address } from "viem";
|
|
2
|
+
import type { BlueAuthorizationAction, Transaction } from "../../../types/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* Encodes Morpho Blue `setAuthorization(zeroExLeverageBundlesV1, isAuthorized)` for the chain's
|
|
5
|
+
* registered ZeroExLeverageBundlesV1.
|
|
6
|
+
*
|
|
7
|
+
* Lever with a borrow, Delever and Close act on the sender's Blue position through the bundle, so
|
|
8
|
+
* Blue requires this authorization. It is global over every Blue position of the user and persists
|
|
9
|
+
* after the action; pass `isAuthorized: false` to revoke it. A revoke can name the `operator` to clear,
|
|
10
|
+
* such as a bundle the registry no longer lists or now lists at another address; a grant always names
|
|
11
|
+
* the registered bundle.
|
|
12
|
+
*
|
|
13
|
+
* @param params.chainId - The chain the transaction targets.
|
|
14
|
+
* @param params.isAuthorized - `true` to grant, `false` to revoke.
|
|
15
|
+
* @param params.operator - Revoke only: the address to deauthorize. Defaults to the registered bundle.
|
|
16
|
+
* @returns A deep-frozen `Transaction<BlueAuthorizationAction>` sent to Morpho Blue.
|
|
17
|
+
* @throws {UnsupportedChainIdError} when `chainId` is absent from the address registry.
|
|
18
|
+
* @throws {LeverageUnsupportedChainError} when the chain registers no leverage bundle and a revoke names
|
|
19
|
+
* no `operator`, or, for a grant only, when the bundle cannot run on the chain.
|
|
20
|
+
* @throws {viem.InvalidAddressError} when a revoke's `operator` is not an address.
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* import { encodeLeverageAuthorization } from "@morpho-org/morpho-sdk";
|
|
24
|
+
*
|
|
25
|
+
* // None ships yet: register a verified zeroExLeverageBundlesV1 address with `registerCustomAddresses` first,
|
|
26
|
+
* // otherwise it throws `LeverageUnsupportedChainError` with `reason: "noBundle"`.
|
|
27
|
+
* const grant = encodeLeverageAuthorization({ chainId: 8453, isAuthorized: true });
|
|
28
|
+
* // grant.action.args satisfies { authorized: zeroExLeverageBundlesV1, isAuthorized: true }
|
|
29
|
+
* const revokeOld = encodeLeverageAuthorization({ chainId: 8453, isAuthorized: false, operator: oldBundle });
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export declare const encodeLeverageAuthorization: (params: {
|
|
33
|
+
readonly chainId: number;
|
|
34
|
+
readonly isAuthorized: true;
|
|
35
|
+
} | {
|
|
36
|
+
readonly chainId: number;
|
|
37
|
+
readonly isAuthorized: false;
|
|
38
|
+
readonly operator?: Address;
|
|
39
|
+
}) => Readonly<Transaction<BlueAuthorizationAction>>;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.encodeLeverageAuthorization = void 0;
|
|
4
|
+
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
5
|
+
const blue_sdk_viem_1 = require("@morpho-org/blue-sdk-viem");
|
|
6
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
7
|
+
const viem_1 = require("viem");
|
|
8
|
+
const getZeroExLeverageBundle_js_1 = require("../../../helpers/getZeroExLeverageBundle.js");
|
|
9
|
+
const zeroExQuote_js_1 = require("../../../helpers/zeroExQuote.js");
|
|
10
|
+
/**
|
|
11
|
+
* Encodes Morpho Blue `setAuthorization(zeroExLeverageBundlesV1, isAuthorized)` for the chain's
|
|
12
|
+
* registered ZeroExLeverageBundlesV1.
|
|
13
|
+
*
|
|
14
|
+
* Lever with a borrow, Delever and Close act on the sender's Blue position through the bundle, so
|
|
15
|
+
* Blue requires this authorization. It is global over every Blue position of the user and persists
|
|
16
|
+
* after the action; pass `isAuthorized: false` to revoke it. A revoke can name the `operator` to clear,
|
|
17
|
+
* such as a bundle the registry no longer lists or now lists at another address; a grant always names
|
|
18
|
+
* the registered bundle.
|
|
19
|
+
*
|
|
20
|
+
* @param params.chainId - The chain the transaction targets.
|
|
21
|
+
* @param params.isAuthorized - `true` to grant, `false` to revoke.
|
|
22
|
+
* @param params.operator - Revoke only: the address to deauthorize. Defaults to the registered bundle.
|
|
23
|
+
* @returns A deep-frozen `Transaction<BlueAuthorizationAction>` sent to Morpho Blue.
|
|
24
|
+
* @throws {UnsupportedChainIdError} when `chainId` is absent from the address registry.
|
|
25
|
+
* @throws {LeverageUnsupportedChainError} when the chain registers no leverage bundle and a revoke names
|
|
26
|
+
* no `operator`, or, for a grant only, when the bundle cannot run on the chain.
|
|
27
|
+
* @throws {viem.InvalidAddressError} when a revoke's `operator` is not an address.
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* import { encodeLeverageAuthorization } from "@morpho-org/morpho-sdk";
|
|
31
|
+
*
|
|
32
|
+
* // None ships yet: register a verified zeroExLeverageBundlesV1 address with `registerCustomAddresses` first,
|
|
33
|
+
* // otherwise it throws `LeverageUnsupportedChainError` with `reason: "noBundle"`.
|
|
34
|
+
* const grant = encodeLeverageAuthorization({ chainId: 8453, isAuthorized: true });
|
|
35
|
+
* // grant.action.args satisfies { authorized: zeroExLeverageBundlesV1, isAuthorized: true }
|
|
36
|
+
* const revokeOld = encodeLeverageAuthorization({ chainId: 8453, isAuthorized: false, operator: oldBundle });
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
const encodeLeverageAuthorization = (params) => {
|
|
40
|
+
const { chainId, isAuthorized } = params;
|
|
41
|
+
// Only a grant needs the bundle to be usable; a revoke must still clear a delisted or replaced bundle.
|
|
42
|
+
const authorized = params.isAuthorized
|
|
43
|
+
? (0, zeroExQuote_js_1.getZeroExLeverageBundle)(chainId)
|
|
44
|
+
: (params.operator ?? (0, getZeroExLeverageBundle_js_1.getRegisteredZeroExLeverageBundle)(chainId));
|
|
45
|
+
return (0, morpho_ts_1.deepFreeze)({
|
|
46
|
+
to: (0, blue_sdk_1.getChainAddresses)(chainId).blue,
|
|
47
|
+
data: (0, viem_1.encodeFunctionData)({
|
|
48
|
+
abi: blue_sdk_viem_1.blueAbi,
|
|
49
|
+
functionName: "setAuthorization",
|
|
50
|
+
args: [authorized, isAuthorized],
|
|
51
|
+
}),
|
|
52
|
+
value: 0n,
|
|
53
|
+
action: {
|
|
54
|
+
type: "blueAuthorization",
|
|
55
|
+
args: { authorized, isAuthorized },
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
};
|
|
59
|
+
exports.encodeLeverageAuthorization = encodeLeverageAuthorization;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { type MarketParams } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { type Address, type Client } from "viem";
|
|
3
|
+
import { type BlueAuthorizationAction, type ERC20ApprovalAction, type Transaction } from "../../../types/index.js";
|
|
4
|
+
/**
|
|
5
|
+
* Leverage entry point whose prerequisites to resolve. Lever carries the amounts its entry point
|
|
6
|
+
* pulls from the user and its borrow cap; Delever and Close pull nothing from the user.
|
|
7
|
+
*/
|
|
8
|
+
export type LeverageRequirementsOperation = {
|
|
9
|
+
readonly type: "lever";
|
|
10
|
+
/** Collateral the bundle pulls from the user. */
|
|
11
|
+
readonly collateralAssets: bigint;
|
|
12
|
+
/** Loan assets the bundle pulls from the user. */
|
|
13
|
+
readonly loanAssets: bigint;
|
|
14
|
+
/**
|
|
15
|
+
* The built Lever's `maxBorrowAssets`: pass `tx.action.args.maxBorrowAssets` of the transaction the Lever
|
|
16
|
+
* builder returned, never an estimate. A borrow needs the Blue authorization, so a different value can skip
|
|
17
|
+
* the grant and make the Lever revert on Blue.
|
|
18
|
+
*/
|
|
19
|
+
readonly maxBorrowAssets: bigint;
|
|
20
|
+
} | {
|
|
21
|
+
readonly type: "delever";
|
|
22
|
+
} | {
|
|
23
|
+
readonly type: "close";
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Resolves the transactions a user submits before a ZeroExLeverageBundlesV1 action: ERC-20
|
|
27
|
+
* approvals for the tokens it pulls, then the Morpho Blue authorization when its entry point
|
|
28
|
+
* needs one.
|
|
29
|
+
*
|
|
30
|
+
* Reads the user's allowance for each pulled token and `Morpho.isAuthorized(user, bundle)` on the
|
|
31
|
+
* target chain. For the leverage bundle the SDK ships it reads no contract code: `MorphoBlue.lever`, `delever` and
|
|
32
|
+
* `close` check Morpho Blue's code before calling it, and the shipped bundle's code check is waived
|
|
33
|
+
* (`docs/EXCEPTIONS.md`). A bundle registered with `registerCustomAddresses` falls back to the default rules: its code
|
|
34
|
+
* is checked before any read or grant, and its approvals are exact.
|
|
35
|
+
*
|
|
36
|
+
* - **Lever** pulls `collateralAssets` of the collateral token and `loanAssets` of the loan token
|
|
37
|
+
* from the user; when the allowance is short, each gets an unlimited approval (`maxUint256`,
|
|
38
|
+
* capped per token by `MAX_TOKEN_APPROVALS`), as for the other bundles, or an approval of exactly the pulled
|
|
39
|
+
* amount for a bundle the SDK does not ship. It borrows on Blue only
|
|
40
|
+
* when `maxBorrowAssets > 0`, so only then does it need the authorization.
|
|
41
|
+
* - **Delever** and **Close** pull nothing from the user and always withdraw collateral on Blue,
|
|
42
|
+
* so they need the authorization and no approval.
|
|
43
|
+
*
|
|
44
|
+
* The allowance and authorization reads run in parallel, each at the latest block its request sees; they are not
|
|
45
|
+
* pinned to one block, so a block landing between them can mix two states. The result is a snapshot of those
|
|
46
|
+
* reads: a requirement missed that way makes the action revert, and a returned approval still calls `approve` and
|
|
47
|
+
* replaces the allowance, with the zero reset of an approve-once token such as USDT planned from the allowance
|
|
48
|
+
* read. If the allowance or the authorization may have changed before submission, resolve the requirements again.
|
|
49
|
+
*
|
|
50
|
+
* The authorization is global over the user's Blue positions and persists after the action, like
|
|
51
|
+
* the unlimited approvals. The bundle takes no signed authorization, so no signature requirement
|
|
52
|
+
* is returned.
|
|
53
|
+
*
|
|
54
|
+
* @param params.viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
|
|
55
|
+
* @param params.chainId - Target chain id.
|
|
56
|
+
* @param params.userAddress - The account that sends the leverage transaction.
|
|
57
|
+
* @param params.marketParams - The market's loan and collateral tokens.
|
|
58
|
+
* @param params.operation - The leverage entry point and, for Lever, its pulled amounts.
|
|
59
|
+
* @returns Deep-frozen transactions in submission order: approvals (each optionally preceded by a
|
|
60
|
+
* reset to zero), then the authorization grant. Empty when nothing is missing.
|
|
61
|
+
* @throws {InvalidLeverageOperationError} when `operation` is missing or not an object, its `type` is
|
|
62
|
+
* not `lever`, `delever` or `close`, a Lever amount is not a bigint, or `userAddress` or a market token is
|
|
63
|
+
* not an address, before any read.
|
|
64
|
+
* @throws {ChainIdMismatchError} when the client declares, or its RPC reports via `eth_chainId`, a
|
|
65
|
+
* chain other than `params.chainId`.
|
|
66
|
+
* @throws {LeverageUnsupportedChainError} when the chain has no usable leverage bundle; a chain absent from the
|
|
67
|
+
* address registry reports `noAllowanceHolder`.
|
|
68
|
+
* @throws {MissingContractCodeError} when the chain's leverage bundle is not the one the SDK ships and has no code.
|
|
69
|
+
* @throws {NegativeInputError} when a Lever amount is negative.
|
|
70
|
+
* @throws {InputExceedsMaxError} when a Lever amount exceeds `uint256`; before any read,
|
|
71
|
+
* with `field: "amount"`, when the sum of both amounts on a market whose loan and collateral
|
|
72
|
+
* tokens are equal exceeds `uint256`.
|
|
73
|
+
* @throws {viem.BaseError} when the `eth_chainId` request or an allowance or authorization read
|
|
74
|
+
* fails.
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* import { createPublicClient, http } from "viem";
|
|
78
|
+
* import { base } from "viem/chains";
|
|
79
|
+
* import { getLeverageRequirements } from "@morpho-org/morpho-sdk";
|
|
80
|
+
*
|
|
81
|
+
* const client = createPublicClient({ chain: base, transport: http() });
|
|
82
|
+
* // `leverTx`: the built Lever transaction; its args carry the amounts the Lever signs.
|
|
83
|
+
* const { collateralAssets, loanAssets, maxBorrowAssets } = leverTx.action.args;
|
|
84
|
+
* // None ships yet: register a verified zeroExLeverageBundlesV1 address with `registerCustomAddresses` first,
|
|
85
|
+
* // otherwise it throws `LeverageUnsupportedChainError` with `reason: "noBundle"`.
|
|
86
|
+
* const requirements = await getLeverageRequirements({
|
|
87
|
+
* viemClient: client,
|
|
88
|
+
* chainId: base.id,
|
|
89
|
+
* userAddress,
|
|
90
|
+
* marketParams,
|
|
91
|
+
* // Here collateralAssets is 1_000000000000000000n and loanAssets 0n.
|
|
92
|
+
* operation: { type: "lever", collateralAssets, loanAssets, maxBorrowAssets },
|
|
93
|
+
* });
|
|
94
|
+
* // requirements: [approve(bundle, 1_000000000000000000n), setAuthorization(bundle, true)] when both are
|
|
95
|
+
* // missing: a registered bundle is approved exactly the pull; the bundle morpho-ts ships gets maxUint256.
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
export declare const getLeverageRequirements: (params: {
|
|
99
|
+
readonly viemClient: Client;
|
|
100
|
+
readonly chainId: number;
|
|
101
|
+
readonly userAddress: Address;
|
|
102
|
+
readonly marketParams: Pick<MarketParams, "loanToken" | "collateralToken">;
|
|
103
|
+
readonly operation: LeverageRequirementsOperation;
|
|
104
|
+
}) => Promise<readonly Readonly<Transaction<ERC20ApprovalAction | BlueAuthorizationAction>>[]>;
|