@morpho-org/morpho-sdk 10.5.0-next.0 → 10.6.0-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -1
- package/lib/cjs/abis.d.ts +793 -0
- package/lib/cjs/abis.js +811 -1
- package/lib/cjs/actions/blue/close.d.ts +169 -0
- package/lib/cjs/actions/blue/close.js +270 -0
- package/lib/cjs/actions/blue/common.js +2 -2
- package/lib/cjs/actions/blue/delever.d.ts +156 -0
- package/lib/cjs/actions/blue/delever.js +223 -0
- package/lib/cjs/actions/blue/index.d.ts +3 -0
- package/lib/cjs/actions/blue/index.js +3 -0
- package/lib/cjs/actions/blue/lever.d.ts +184 -0
- package/lib/cjs/actions/blue/lever.js +272 -0
- package/lib/cjs/actions/blue/leverageIntent.d.ts +65 -0
- package/lib/cjs/actions/blue/leverageIntent.js +103 -0
- package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +16 -11
- package/lib/cjs/actions/midnight/takeBorrow.js +14 -8
- package/lib/cjs/actions/midnight/takeLend.js +15 -9
- package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.js +19 -13
- package/lib/cjs/actions/midnight/takeWithdraw.js +14 -8
- package/lib/cjs/actions/requirements/blue/encodeLeverageAuthorization.d.ts +39 -0
- package/lib/cjs/actions/requirements/blue/encodeLeverageAuthorization.js +59 -0
- package/lib/cjs/actions/requirements/blue/getLeverageRequirements.d.ts +104 -0
- package/lib/cjs/actions/requirements/blue/getLeverageRequirements.js +193 -0
- package/lib/cjs/actions/requirements/blue/getLeverageRequirementsApproval.d.ts +45 -0
- package/lib/cjs/actions/requirements/blue/getLeverageRequirementsApproval.js +66 -0
- package/lib/cjs/actions/requirements/blue/index.d.ts +3 -0
- package/lib/cjs/actions/requirements/blue/index.js +17 -0
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +2 -2
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +3 -2
- package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +11 -6
- package/lib/cjs/actions/requirements/getRequirementsApproval.js +29 -5
- package/lib/cjs/addresses.d.ts +1 -1
- package/lib/cjs/addresses.js +6 -1
- package/lib/cjs/blue/addresses.d.ts +1 -1
- package/lib/cjs/blue/addresses.js +6 -1
- package/lib/cjs/constants.d.ts +1 -1
- package/lib/cjs/constants.js +7 -2
- package/lib/cjs/entities/blue/blue.d.ts +486 -2
- package/lib/cjs/entities/blue/blue.js +529 -34
- package/lib/cjs/entities/index.d.ts +1 -1
- package/lib/cjs/entities/midnight/midnight.d.ts +6 -0
- package/lib/cjs/entities/midnight/midnight.js +21 -2
- package/lib/cjs/entities/requirements/assertContractCode.d.ts +0 -23
- package/lib/cjs/entities/requirements/assertContractCode.js +3 -28
- package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +117 -0
- package/lib/cjs/entities/vaultV2BlueReallocationData.js +226 -2
- package/lib/cjs/helpers/constant.d.ts +41 -0
- package/lib/cjs/helpers/constant.js +42 -1
- package/lib/cjs/helpers/getZeroExLeverageBundle.d.ts +13 -0
- package/lib/cjs/helpers/getZeroExLeverageBundle.js +27 -0
- package/lib/cjs/helpers/index.d.ts +4 -1
- package/lib/cjs/helpers/index.js +29 -1
- package/lib/cjs/helpers/leverage/index.d.ts +1 -0
- package/lib/cjs/helpers/leverage/index.js +11 -0
- package/lib/cjs/helpers/leverage/limits.d.ts +472 -0
- package/lib/cjs/helpers/leverage/limits.js +715 -0
- package/lib/cjs/helpers/leverage/positionMarket.d.ts +17 -0
- package/lib/cjs/helpers/leverage/positionMarket.js +28 -0
- package/lib/cjs/helpers/leverage/reallocations.d.ts +159 -0
- package/lib/cjs/helpers/leverage/reallocations.js +276 -0
- package/lib/cjs/helpers/validate.d.ts +14 -0
- package/lib/cjs/helpers/validate.js +25 -1
- package/lib/cjs/helpers/validateRequirementSpender.d.ts +1 -1
- package/lib/cjs/helpers/validateRequirementSpender.js +4 -0
- package/lib/cjs/helpers/zeroExQuote.d.ts +250 -0
- package/lib/cjs/helpers/zeroExQuote.js +763 -0
- package/lib/cjs/index.d.ts +1 -0
- package/lib/cjs/index.js +5 -2
- package/lib/cjs/reads/assertContractCode.d.ts +25 -0
- package/lib/cjs/reads/assertContractCode.js +30 -0
- package/lib/cjs/reads/fetchZeroExQuote.d.ts +123 -0
- package/lib/cjs/reads/fetchZeroExQuote.js +330 -0
- package/lib/cjs/transactionPlan/midnight/takeBorrow.d.ts +4 -0
- package/lib/cjs/transactionPlan/midnight/takeBorrow.js +8 -2
- package/lib/cjs/types/action.d.ts +130 -1
- package/lib/cjs/types/error.d.ts +84 -0
- package/lib/cjs/types/error.js +110 -3
- package/lib/cjs/types/index.d.ts +2 -0
- package/lib/cjs/types/index.js +2 -0
- package/lib/cjs/types/leverageError.d.ts +1552 -0
- package/lib/cjs/types/leverageError.js +1711 -0
- package/lib/cjs/types/zeroEx.d.ts +171 -0
- package/lib/cjs/types/zeroEx.js +2 -0
- package/lib/cjs/version.d.ts +1 -1
- package/lib/cjs/version.js +1 -1
- package/lib/esm/abis.d.ts +793 -0
- package/lib/esm/abis.js +809 -0
- package/lib/esm/actions/blue/close.d.ts +169 -0
- package/lib/esm/actions/blue/close.js +266 -0
- package/lib/esm/actions/blue/common.js +3 -3
- package/lib/esm/actions/blue/delever.d.ts +156 -0
- package/lib/esm/actions/blue/delever.js +219 -0
- package/lib/esm/actions/blue/index.d.ts +3 -0
- package/lib/esm/actions/blue/index.js +3 -0
- package/lib/esm/actions/blue/lever.d.ts +184 -0
- package/lib/esm/actions/blue/lever.js +268 -0
- package/lib/esm/actions/blue/leverageIntent.d.ts +65 -0
- package/lib/esm/actions/blue/leverageIntent.js +96 -0
- package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +16 -11
- package/lib/esm/actions/midnight/takeBorrow.js +14 -8
- package/lib/esm/actions/midnight/takeLend.js +15 -9
- package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.js +19 -13
- package/lib/esm/actions/midnight/takeWithdraw.js +14 -8
- package/lib/esm/actions/requirements/blue/encodeLeverageAuthorization.d.ts +39 -0
- package/lib/esm/actions/requirements/blue/encodeLeverageAuthorization.js +55 -0
- package/lib/esm/actions/requirements/blue/getLeverageRequirements.d.ts +104 -0
- package/lib/esm/actions/requirements/blue/getLeverageRequirements.js +189 -0
- package/lib/esm/actions/requirements/blue/getLeverageRequirementsApproval.d.ts +45 -0
- package/lib/esm/actions/requirements/blue/getLeverageRequirementsApproval.js +62 -0
- package/lib/esm/actions/requirements/blue/index.d.ts +3 -0
- package/lib/esm/actions/requirements/blue/index.js +3 -0
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +2 -2
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +3 -2
- package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +11 -6
- package/lib/esm/actions/requirements/getRequirementsApproval.js +29 -5
- package/lib/esm/addresses.d.ts +1 -1
- package/lib/esm/addresses.js +1 -1
- package/lib/esm/blue/addresses.d.ts +1 -1
- package/lib/esm/blue/addresses.js +1 -1
- package/lib/esm/constants.d.ts +1 -1
- package/lib/esm/constants.js +1 -1
- package/lib/esm/entities/blue/blue.d.ts +486 -2
- package/lib/esm/entities/blue/blue.js +501 -6
- package/lib/esm/entities/index.d.ts +1 -1
- package/lib/esm/entities/midnight/midnight.d.ts +6 -0
- package/lib/esm/entities/midnight/midnight.js +21 -2
- package/lib/esm/entities/requirements/assertContractCode.d.ts +0 -23
- package/lib/esm/entities/requirements/assertContractCode.js +1 -25
- package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +117 -0
- package/lib/esm/entities/vaultV2BlueReallocationData.js +227 -3
- package/lib/esm/helpers/constant.d.ts +41 -0
- package/lib/esm/helpers/constant.js +41 -0
- package/lib/esm/helpers/getZeroExLeverageBundle.d.ts +13 -0
- package/lib/esm/helpers/getZeroExLeverageBundle.js +23 -0
- package/lib/esm/helpers/index.d.ts +4 -1
- package/lib/esm/helpers/index.js +4 -1
- package/lib/esm/helpers/leverage/index.d.ts +1 -0
- package/lib/esm/helpers/leverage/index.js +1 -0
- package/lib/esm/helpers/leverage/limits.d.ts +472 -0
- package/lib/esm/helpers/leverage/limits.js +705 -0
- package/lib/esm/helpers/leverage/positionMarket.d.ts +17 -0
- package/lib/esm/helpers/leverage/positionMarket.js +24 -0
- package/lib/esm/helpers/leverage/reallocations.d.ts +159 -0
- package/lib/esm/helpers/leverage/reallocations.js +271 -0
- package/lib/esm/helpers/validate.d.ts +14 -0
- package/lib/esm/helpers/validate.js +25 -2
- package/lib/esm/helpers/validateRequirementSpender.d.ts +1 -1
- package/lib/esm/helpers/validateRequirementSpender.js +5 -1
- package/lib/esm/helpers/zeroExQuote.d.ts +250 -0
- package/lib/esm/helpers/zeroExQuote.js +754 -0
- package/lib/esm/index.d.ts +1 -0
- package/lib/esm/index.js +1 -0
- package/lib/esm/reads/assertContractCode.d.ts +25 -0
- package/lib/esm/reads/assertContractCode.js +26 -0
- package/lib/esm/reads/fetchZeroExQuote.d.ts +123 -0
- package/lib/esm/reads/fetchZeroExQuote.js +326 -0
- package/lib/esm/transactionPlan/midnight/takeBorrow.d.ts +4 -0
- package/lib/esm/transactionPlan/midnight/takeBorrow.js +9 -3
- package/lib/esm/types/action.d.ts +130 -1
- package/lib/esm/types/error.d.ts +84 -0
- package/lib/esm/types/error.js +103 -0
- package/lib/esm/types/index.d.ts +2 -0
- package/lib/esm/types/index.js +2 -0
- package/lib/esm/types/leverageError.d.ts +1552 -0
- package/lib/esm/types/leverageError.js +1660 -0
- package/lib/esm/types/zeroEx.d.ts +171 -0
- package/lib/esm/types/zeroEx.js +1 -0
- package/lib/esm/version.d.ts +1 -1
- package/lib/esm/version.js +1 -1
- package/package.json +7 -7
|
@@ -100,11 +100,12 @@ export const midnightTakeRepayWithdrawCollateral = (params) => encodeMidnightTak
|
|
|
100
100
|
const parsedTakeRepayWithdrawCollateral = Symbol();
|
|
101
101
|
/** Parses the static TakeRepayWithdrawCollateral constraints in the standalone builder's existing order. @internal */
|
|
102
102
|
export const parseMidnightTakeRepayWithdrawCollateral = (params) => {
|
|
103
|
-
|
|
104
|
-
|
|
103
|
+
const { market, target, takeableOffers } = params;
|
|
104
|
+
if (target.type === "assets" && target.assets <= 0n) {
|
|
105
|
+
throw new NonPositiveInputError("target.assets", target.assets);
|
|
105
106
|
}
|
|
106
107
|
// Reject markets from another chain deployment before checking offers against them.
|
|
107
|
-
validateMidnightMarket({ market
|
|
108
|
+
validateMidnightMarket({ market, chainId: params.chainId });
|
|
108
109
|
for (const [index, { collateralIndex, assets },] of params.collateralWithdrawals.entries()) {
|
|
109
110
|
// Validation only; rejects non-positive amounts.
|
|
110
111
|
validatePositiveInput(`collateralWithdrawals[${index}].assets`, assets);
|
|
@@ -112,25 +113,24 @@ export const parseMidnightTakeRepayWithdrawCollateral = (params) => {
|
|
|
112
113
|
validateMidnightUint128Field(`collateralWithdrawals[${index}].assets`, assets);
|
|
113
114
|
}
|
|
114
115
|
// Throws UnknownCollateralIndexError when the index is not configured on the market.
|
|
115
|
-
MarketUtils.getCollateralByIndex(
|
|
116
|
+
MarketUtils.getCollateralByIndex(market, collateralIndex);
|
|
116
117
|
}
|
|
117
118
|
validateMidnightAddress("collateralReceiver", params.collateralReceiver);
|
|
118
119
|
if (params.collateralWithdrawals.length > 0) {
|
|
119
120
|
// Rejects receivers that would strand the withdrawn collateral in a contract.
|
|
120
121
|
validateMidnightReceiver({
|
|
121
122
|
chainId: params.chainId,
|
|
122
|
-
market
|
|
123
|
+
market,
|
|
123
124
|
field: "collateralReceiver",
|
|
124
125
|
receiver: params.collateralReceiver,
|
|
125
126
|
});
|
|
126
127
|
}
|
|
127
128
|
const marketId = validateTakeableOffers({
|
|
128
|
-
market
|
|
129
|
-
takeableOffers
|
|
129
|
+
market,
|
|
130
|
+
takeableOffers,
|
|
130
131
|
expectedBuy: false,
|
|
131
132
|
});
|
|
132
|
-
validateMidnightTakeableOfferBounds(
|
|
133
|
-
const { target } = params;
|
|
133
|
+
validateMidnightTakeableOfferBounds(takeableOffers);
|
|
134
134
|
switch (target.type) {
|
|
135
135
|
case "assets": {
|
|
136
136
|
// Validation only; rejects non-positive amounts.
|
|
@@ -164,23 +164,29 @@ export const parseMidnightTakeRepayWithdrawCollateral = (params) => {
|
|
|
164
164
|
// Rejects fee recipients that would strand the fee in a contract.
|
|
165
165
|
validateMidnightReceiver({
|
|
166
166
|
chainId: params.chainId,
|
|
167
|
-
market
|
|
167
|
+
market,
|
|
168
168
|
field: "referralFeeRecipient",
|
|
169
169
|
receiver: referralFeeRecipient,
|
|
170
170
|
});
|
|
171
171
|
}
|
|
172
|
-
const
|
|
172
|
+
const marketStruct = MarketUtils.toStruct(market);
|
|
173
173
|
const nativeFunding = parseMidnightNativeFunding({
|
|
174
174
|
chainId: params.chainId,
|
|
175
175
|
nativeAmount: params.nativeAmount,
|
|
176
176
|
pulls: [
|
|
177
177
|
{
|
|
178
|
-
token:
|
|
178
|
+
token: marketStruct.loanToken,
|
|
179
179
|
assets: target.type === "assets" ? target.assets : target.maxBuyerAssets,
|
|
180
180
|
},
|
|
181
181
|
],
|
|
182
182
|
});
|
|
183
|
-
const snapshot = snapshotMidnightTakeParams({
|
|
183
|
+
const snapshot = snapshotMidnightTakeParams({
|
|
184
|
+
direction: "buy",
|
|
185
|
+
market,
|
|
186
|
+
target,
|
|
187
|
+
takeableOffers,
|
|
188
|
+
metadata: params.metadata,
|
|
189
|
+
});
|
|
184
190
|
return deepFreeze({
|
|
185
191
|
...snapshot,
|
|
186
192
|
chainId: params.chainId,
|
|
@@ -77,23 +77,23 @@ export const midnightTakeWithdraw = (params) => encodeMidnightTakeWithdraw(parse
|
|
|
77
77
|
const parsedTakeWithdraw = Symbol();
|
|
78
78
|
/** Parses the static TakeWithdraw constraints in the standalone builder's existing order. @internal */
|
|
79
79
|
export const parseMidnightTakeWithdraw = (params) => {
|
|
80
|
+
const { market, target, takeableOffers } = params;
|
|
80
81
|
// Reject markets from another chain deployment before checking offers against them.
|
|
81
|
-
validateMidnightMarket({ market
|
|
82
|
+
validateMidnightMarket({ market, chainId: params.chainId });
|
|
82
83
|
const marketId = validateTakeableOffers({
|
|
83
|
-
market
|
|
84
|
-
takeableOffers
|
|
84
|
+
market,
|
|
85
|
+
takeableOffers,
|
|
85
86
|
expectedBuy: true,
|
|
86
87
|
});
|
|
87
88
|
validateMidnightAddress("receiver", params.receiver);
|
|
88
|
-
validateMidnightTakeableOfferBounds(
|
|
89
|
+
validateMidnightTakeableOfferBounds(takeableOffers);
|
|
89
90
|
// Rejects receivers that would strand the withdrawn assets in a contract.
|
|
90
91
|
validateMidnightReceiver({
|
|
91
92
|
chainId: params.chainId,
|
|
92
|
-
market
|
|
93
|
+
market,
|
|
93
94
|
field: "receiver",
|
|
94
95
|
receiver: params.receiver,
|
|
95
96
|
});
|
|
96
|
-
const { target } = params;
|
|
97
97
|
switch (target.type) {
|
|
98
98
|
case "assets": {
|
|
99
99
|
// Validation only; preserve main's positive-target and finite-cap order.
|
|
@@ -131,12 +131,18 @@ export const parseMidnightTakeWithdraw = (params) => {
|
|
|
131
131
|
// Rejects fee recipients that would strand the fee in a contract.
|
|
132
132
|
validateMidnightReceiver({
|
|
133
133
|
chainId: params.chainId,
|
|
134
|
-
market
|
|
134
|
+
market,
|
|
135
135
|
field: "referralFeeRecipient",
|
|
136
136
|
receiver: referralFeeRecipient,
|
|
137
137
|
});
|
|
138
138
|
}
|
|
139
|
-
const snapshot = snapshotMidnightTakeParams({
|
|
139
|
+
const snapshot = snapshotMidnightTakeParams({
|
|
140
|
+
direction: "sell",
|
|
141
|
+
market,
|
|
142
|
+
target,
|
|
143
|
+
takeableOffers,
|
|
144
|
+
metadata: params.metadata,
|
|
145
|
+
});
|
|
140
146
|
return deepFreeze({
|
|
141
147
|
...snapshot,
|
|
142
148
|
chainId: params.chainId,
|
|
@@ -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,55 @@
|
|
|
1
|
+
import { getChainAddresses } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { blueAbi } from "@morpho-org/blue-sdk-viem";
|
|
3
|
+
import { deepFreeze } from "@morpho-org/morpho-ts";
|
|
4
|
+
import { encodeFunctionData } from "viem";
|
|
5
|
+
import { getRegisteredZeroExLeverageBundle } from "../../../helpers/getZeroExLeverageBundle.js";
|
|
6
|
+
import { getZeroExLeverageBundle } from "../../../helpers/zeroExQuote.js";
|
|
7
|
+
/**
|
|
8
|
+
* Encodes Morpho Blue `setAuthorization(zeroExLeverageBundlesV1, isAuthorized)` for the chain's
|
|
9
|
+
* registered ZeroExLeverageBundlesV1.
|
|
10
|
+
*
|
|
11
|
+
* Lever with a borrow, Delever and Close act on the sender's Blue position through the bundle, so
|
|
12
|
+
* Blue requires this authorization. It is global over every Blue position of the user and persists
|
|
13
|
+
* after the action; pass `isAuthorized: false` to revoke it. A revoke can name the `operator` to clear,
|
|
14
|
+
* such as a bundle the registry no longer lists or now lists at another address; a grant always names
|
|
15
|
+
* the registered bundle.
|
|
16
|
+
*
|
|
17
|
+
* @param params.chainId - The chain the transaction targets.
|
|
18
|
+
* @param params.isAuthorized - `true` to grant, `false` to revoke.
|
|
19
|
+
* @param params.operator - Revoke only: the address to deauthorize. Defaults to the registered bundle.
|
|
20
|
+
* @returns A deep-frozen `Transaction<BlueAuthorizationAction>` sent to Morpho Blue.
|
|
21
|
+
* @throws {UnsupportedChainIdError} when `chainId` is absent from the address registry.
|
|
22
|
+
* @throws {LeverageUnsupportedChainError} when the chain registers no leverage bundle and a revoke names
|
|
23
|
+
* no `operator`, or, for a grant only, when the bundle cannot run on the chain.
|
|
24
|
+
* @throws {viem.InvalidAddressError} when a revoke's `operator` is not an address.
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* import { encodeLeverageAuthorization } from "@morpho-org/morpho-sdk";
|
|
28
|
+
*
|
|
29
|
+
* // None ships yet: register a verified zeroExLeverageBundlesV1 address with `registerCustomAddresses` first,
|
|
30
|
+
* // otherwise it throws `LeverageUnsupportedChainError` with `reason: "noBundle"`.
|
|
31
|
+
* const grant = encodeLeverageAuthorization({ chainId: 8453, isAuthorized: true });
|
|
32
|
+
* // grant.action.args satisfies { authorized: zeroExLeverageBundlesV1, isAuthorized: true }
|
|
33
|
+
* const revokeOld = encodeLeverageAuthorization({ chainId: 8453, isAuthorized: false, operator: oldBundle });
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export const encodeLeverageAuthorization = (params) => {
|
|
37
|
+
const { chainId, isAuthorized } = params;
|
|
38
|
+
// Only a grant needs the bundle to be usable; a revoke must still clear a delisted or replaced bundle.
|
|
39
|
+
const authorized = params.isAuthorized
|
|
40
|
+
? getZeroExLeverageBundle(chainId)
|
|
41
|
+
: (params.operator ?? getRegisteredZeroExLeverageBundle(chainId));
|
|
42
|
+
return deepFreeze({
|
|
43
|
+
to: getChainAddresses(chainId).blue,
|
|
44
|
+
data: encodeFunctionData({
|
|
45
|
+
abi: blueAbi,
|
|
46
|
+
functionName: "setAuthorization",
|
|
47
|
+
args: [authorized, isAuthorized],
|
|
48
|
+
}),
|
|
49
|
+
value: 0n,
|
|
50
|
+
action: {
|
|
51
|
+
type: "blueAuthorization",
|
|
52
|
+
args: { authorized, isAuthorized },
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
};
|
|
@@ -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>>[]>;
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { getChainAddresses } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { blueAbi } from "@morpho-org/blue-sdk-viem";
|
|
3
|
+
import { isShippedZeroExLeverageBundle } from "@morpho-org/morpho-ts";
|
|
4
|
+
import { erc20Abi, isAddress, isAddressEqual, publicActions, } from "viem";
|
|
5
|
+
import { validateChainId, validateUint256Field, } from "../../../helpers/validate.js";
|
|
6
|
+
import { getZeroExLeverageBundle } from "../../../helpers/zeroExQuote.js";
|
|
7
|
+
import { assertContractCode } from "../../../reads/assertContractCode.js";
|
|
8
|
+
import { InvalidLeverageOperationError, } from "../../../types/index.js";
|
|
9
|
+
import { validateClientChainId } from "../validateClientChainId.js";
|
|
10
|
+
import { encodeLeverageAuthorization } from "./encodeLeverageAuthorization.js";
|
|
11
|
+
import { getLeverageRequirementsApproval } from "./getLeverageRequirementsApproval.js";
|
|
12
|
+
const LEVERAGE_OPERATION_TYPES = [
|
|
13
|
+
"lever",
|
|
14
|
+
"delever",
|
|
15
|
+
"close",
|
|
16
|
+
];
|
|
17
|
+
const LEVER_AMOUNT_FIELDS = [
|
|
18
|
+
"collateralAssets",
|
|
19
|
+
"loanAssets",
|
|
20
|
+
"maxBorrowAssets",
|
|
21
|
+
];
|
|
22
|
+
const getTokenPulls = (marketParams, operation) => {
|
|
23
|
+
if (operation.type !== "lever") {
|
|
24
|
+
return [];
|
|
25
|
+
}
|
|
26
|
+
validateUint256Field("collateralAssets", operation.collateralAssets);
|
|
27
|
+
validateUint256Field("loanAssets", operation.loanAssets);
|
|
28
|
+
if (isAddressEqual(marketParams.collateralToken, marketParams.loanToken)) {
|
|
29
|
+
const amount = operation.collateralAssets + operation.loanAssets;
|
|
30
|
+
validateUint256Field("amount", amount);
|
|
31
|
+
return [{ token: marketParams.loanToken, amount }];
|
|
32
|
+
}
|
|
33
|
+
return [
|
|
34
|
+
{
|
|
35
|
+
token: marketParams.collateralToken,
|
|
36
|
+
amount: operation.collateralAssets,
|
|
37
|
+
},
|
|
38
|
+
{ token: marketParams.loanToken, amount: operation.loanAssets },
|
|
39
|
+
];
|
|
40
|
+
};
|
|
41
|
+
const needsAuthorization = (operation) => {
|
|
42
|
+
if (operation.type !== "lever") {
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
validateUint256Field("maxBorrowAssets", operation.maxBorrowAssets);
|
|
46
|
+
return operation.maxBorrowAssets > 0n;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Resolves the transactions a user submits before a ZeroExLeverageBundlesV1 action: ERC-20
|
|
50
|
+
* approvals for the tokens it pulls, then the Morpho Blue authorization when its entry point
|
|
51
|
+
* needs one.
|
|
52
|
+
*
|
|
53
|
+
* Reads the user's allowance for each pulled token and `Morpho.isAuthorized(user, bundle)` on the
|
|
54
|
+
* target chain. For the leverage bundle the SDK ships it reads no contract code: `MorphoBlue.lever`, `delever` and
|
|
55
|
+
* `close` check Morpho Blue's code before calling it, and the shipped bundle's code check is waived
|
|
56
|
+
* (`docs/EXCEPTIONS.md`). A bundle registered with `registerCustomAddresses` falls back to the default rules: its code
|
|
57
|
+
* is checked before any read or grant, and its approvals are exact.
|
|
58
|
+
*
|
|
59
|
+
* - **Lever** pulls `collateralAssets` of the collateral token and `loanAssets` of the loan token
|
|
60
|
+
* from the user; when the allowance is short, each gets an unlimited approval (`maxUint256`,
|
|
61
|
+
* capped per token by `MAX_TOKEN_APPROVALS`), as for the other bundles, or an approval of exactly the pulled
|
|
62
|
+
* amount for a bundle the SDK does not ship. It borrows on Blue only
|
|
63
|
+
* when `maxBorrowAssets > 0`, so only then does it need the authorization.
|
|
64
|
+
* - **Delever** and **Close** pull nothing from the user and always withdraw collateral on Blue,
|
|
65
|
+
* so they need the authorization and no approval.
|
|
66
|
+
*
|
|
67
|
+
* The allowance and authorization reads run in parallel, each at the latest block its request sees; they are not
|
|
68
|
+
* pinned to one block, so a block landing between them can mix two states. The result is a snapshot of those
|
|
69
|
+
* reads: a requirement missed that way makes the action revert, and a returned approval still calls `approve` and
|
|
70
|
+
* replaces the allowance, with the zero reset of an approve-once token such as USDT planned from the allowance
|
|
71
|
+
* read. If the allowance or the authorization may have changed before submission, resolve the requirements again.
|
|
72
|
+
*
|
|
73
|
+
* The authorization is global over the user's Blue positions and persists after the action, like
|
|
74
|
+
* the unlimited approvals. The bundle takes no signed authorization, so no signature requirement
|
|
75
|
+
* is returned.
|
|
76
|
+
*
|
|
77
|
+
* @param params.viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
|
|
78
|
+
* @param params.chainId - Target chain id.
|
|
79
|
+
* @param params.userAddress - The account that sends the leverage transaction.
|
|
80
|
+
* @param params.marketParams - The market's loan and collateral tokens.
|
|
81
|
+
* @param params.operation - The leverage entry point and, for Lever, its pulled amounts.
|
|
82
|
+
* @returns Deep-frozen transactions in submission order: approvals (each optionally preceded by a
|
|
83
|
+
* reset to zero), then the authorization grant. Empty when nothing is missing.
|
|
84
|
+
* @throws {InvalidLeverageOperationError} when `operation` is missing or not an object, its `type` is
|
|
85
|
+
* not `lever`, `delever` or `close`, a Lever amount is not a bigint, or `userAddress` or a market token is
|
|
86
|
+
* not an address, before any read.
|
|
87
|
+
* @throws {ChainIdMismatchError} when the client declares, or its RPC reports via `eth_chainId`, a
|
|
88
|
+
* chain other than `params.chainId`.
|
|
89
|
+
* @throws {LeverageUnsupportedChainError} when the chain has no usable leverage bundle; a chain absent from the
|
|
90
|
+
* address registry reports `noAllowanceHolder`.
|
|
91
|
+
* @throws {MissingContractCodeError} when the chain's leverage bundle is not the one the SDK ships and has no code.
|
|
92
|
+
* @throws {NegativeInputError} when a Lever amount is negative.
|
|
93
|
+
* @throws {InputExceedsMaxError} when a Lever amount exceeds `uint256`; before any read,
|
|
94
|
+
* with `field: "amount"`, when the sum of both amounts on a market whose loan and collateral
|
|
95
|
+
* tokens are equal exceeds `uint256`.
|
|
96
|
+
* @throws {viem.BaseError} when the `eth_chainId` request or an allowance or authorization read
|
|
97
|
+
* fails.
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* import { createPublicClient, http } from "viem";
|
|
101
|
+
* import { base } from "viem/chains";
|
|
102
|
+
* import { getLeverageRequirements } from "@morpho-org/morpho-sdk";
|
|
103
|
+
*
|
|
104
|
+
* const client = createPublicClient({ chain: base, transport: http() });
|
|
105
|
+
* // `leverTx`: the built Lever transaction; its args carry the amounts the Lever signs.
|
|
106
|
+
* const { collateralAssets, loanAssets, maxBorrowAssets } = leverTx.action.args;
|
|
107
|
+
* // None ships yet: register a verified zeroExLeverageBundlesV1 address with `registerCustomAddresses` first,
|
|
108
|
+
* // otherwise it throws `LeverageUnsupportedChainError` with `reason: "noBundle"`.
|
|
109
|
+
* const requirements = await getLeverageRequirements({
|
|
110
|
+
* viemClient: client,
|
|
111
|
+
* chainId: base.id,
|
|
112
|
+
* userAddress,
|
|
113
|
+
* marketParams,
|
|
114
|
+
* // Here collateralAssets is 1_000000000000000000n and loanAssets 0n.
|
|
115
|
+
* operation: { type: "lever", collateralAssets, loanAssets, maxBorrowAssets },
|
|
116
|
+
* });
|
|
117
|
+
* // requirements: [approve(bundle, 1_000000000000000000n), setAuthorization(bundle, true)] when both are
|
|
118
|
+
* // missing: a registered bundle is approved exactly the pull; the bundle morpho-ts ships gets maxUint256.
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
export const getLeverageRequirements = async (params) => {
|
|
122
|
+
const { viemClient, chainId, userAddress, marketParams, operation } = params;
|
|
123
|
+
// A JavaScript caller can pass any shape; only well-formed known entry points may get a grant.
|
|
124
|
+
if (operation == null || typeof operation !== "object") {
|
|
125
|
+
throw new InvalidLeverageOperationError("operation", operation);
|
|
126
|
+
}
|
|
127
|
+
if (!LEVERAGE_OPERATION_TYPES.includes(operation.type)) {
|
|
128
|
+
throw new InvalidLeverageOperationError("type", operation.type);
|
|
129
|
+
}
|
|
130
|
+
if (operation.type === "lever") {
|
|
131
|
+
for (const field of LEVER_AMOUNT_FIELDS) {
|
|
132
|
+
if (typeof operation[field] !== "bigint") {
|
|
133
|
+
throw new InvalidLeverageOperationError(field, operation[field]);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
// A JavaScript caller can pass any value; a malformed address must never reach a read or a grant.
|
|
138
|
+
for (const [field, address] of [
|
|
139
|
+
["userAddress", userAddress],
|
|
140
|
+
["marketParams.loanToken", marketParams?.loanToken],
|
|
141
|
+
["marketParams.collateralToken", marketParams?.collateralToken],
|
|
142
|
+
]) {
|
|
143
|
+
if (typeof address !== "string" || !isAddress(address, { strict: false })) {
|
|
144
|
+
throw new InvalidLeverageOperationError(field, address);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
validateChainId(viemClient.chain?.id, chainId);
|
|
148
|
+
const bundle = getZeroExLeverageBundle(chainId);
|
|
149
|
+
const pulls = getTokenPulls(marketParams, operation).filter(({ amount }) => amount > 0n);
|
|
150
|
+
const authorize = needsAuthorization(operation);
|
|
151
|
+
await validateClientChainId(viemClient, chainId);
|
|
152
|
+
const pc = viemClient.extend(publicActions);
|
|
153
|
+
if (!isShippedZeroExLeverageBundle(chainId, bundle)) {
|
|
154
|
+
// The leverage exceptions cover only the shipped bundle: another one is never granted anything without code.
|
|
155
|
+
await assertContractCode(pc, {
|
|
156
|
+
address: bundle,
|
|
157
|
+
chainId,
|
|
158
|
+
role: "bundles.zeroExLeverageBundlesV1",
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
const [approvals, isAuthorized] = await Promise.all([
|
|
162
|
+
Promise.all(pulls.map(async ({ token, amount }) => getLeverageRequirementsApproval({
|
|
163
|
+
chainId,
|
|
164
|
+
token,
|
|
165
|
+
amount,
|
|
166
|
+
allowance: await pc.readContract({
|
|
167
|
+
address: token,
|
|
168
|
+
abi: erc20Abi,
|
|
169
|
+
functionName: "allowance",
|
|
170
|
+
args: [userAddress, bundle],
|
|
171
|
+
}),
|
|
172
|
+
}))),
|
|
173
|
+
authorize
|
|
174
|
+
? pc.readContract({
|
|
175
|
+
address: getChainAddresses(chainId).blue,
|
|
176
|
+
abi: blueAbi,
|
|
177
|
+
functionName: "isAuthorized",
|
|
178
|
+
args: [userAddress, bundle],
|
|
179
|
+
})
|
|
180
|
+
: true,
|
|
181
|
+
]);
|
|
182
|
+
if (isAuthorized) {
|
|
183
|
+
return approvals.flat();
|
|
184
|
+
}
|
|
185
|
+
return [
|
|
186
|
+
...approvals.flat(),
|
|
187
|
+
encodeLeverageAuthorization({ chainId, isAuthorized: true }),
|
|
188
|
+
];
|
|
189
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { Address } from "viem";
|
|
2
|
+
import { type ERC20ApprovalAction, type Transaction } from "../../../types/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* Computes the ERC-20 approvals a user gives ZeroExLeverageBundlesV1 so it can pull `amount` of
|
|
5
|
+
* `token`, given the existing allowance.
|
|
6
|
+
*
|
|
7
|
+
* For the leverage bundle the SDK ships (`isShippedZeroExLeverageBundle`), a short allowance is raised to
|
|
8
|
+
* `maxUint256`, capped per token by `MAX_TOKEN_APPROVALS`, like the other bundles' approvals, so one approval per token
|
|
9
|
+
* covers later leverage actions. A bundle registered with `registerCustomAddresses` gets an approval of exactly
|
|
10
|
+
* `amount` instead. For tokens in `APPROVE_ONLY_ONCE_TOKENS` (e.g. USDT) with a non-zero allowance, a reset to zero
|
|
11
|
+
* precedes it. The spender is always the chain's registered bundle, refused on chains where it cannot run.
|
|
12
|
+
*
|
|
13
|
+
* @param params.chainId - The chain the transaction targets.
|
|
14
|
+
* @param params.token - ERC-20 token the bundle pulls.
|
|
15
|
+
* @param params.amount - Amount the bundle pulls.
|
|
16
|
+
* @param params.allowance - The user's current allowance of `token` for the bundle.
|
|
17
|
+
* @returns Up to two deep-frozen `Transaction<ERC20ApprovalAction>` entries: an optional reset
|
|
18
|
+
* followed by the unlimited approval, or the exact one for a bundle the SDK does not ship. Empty when
|
|
19
|
+
* `allowance >= amount`.
|
|
20
|
+
* @throws {InvalidLeverageOperationError} when `amount` or `allowance` is not a bigint.
|
|
21
|
+
* @throws {NegativeInputError} when `amount` or `allowance` is negative.
|
|
22
|
+
* @throws {InputExceedsMaxError} when `amount` or `allowance` exceeds `uint256`.
|
|
23
|
+
* @throws {LeverageUnsupportedChainError} when the chain has no usable leverage bundle; a chain absent from the
|
|
24
|
+
* address registry reports `noAllowanceHolder`.
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* import { getLeverageRequirementsApproval } from "@morpho-org/morpho-sdk";
|
|
28
|
+
*
|
|
29
|
+
* // None ships yet: register a verified zeroExLeverageBundlesV1 address with `registerCustomAddresses` first.
|
|
30
|
+
* const txs = getLeverageRequirementsApproval({
|
|
31
|
+
* chainId: 8453,
|
|
32
|
+
* token: USDC,
|
|
33
|
+
* amount: 1_000_000n,
|
|
34
|
+
* allowance: 0n,
|
|
35
|
+
* });
|
|
36
|
+
* // txs: [approve(zeroExLeverageBundlesV1, 1_000_000n)], exactly the pull, for a registered bundle;
|
|
37
|
+
* // the bundle morpho-ts ships gets approve(zeroExLeverageBundlesV1, maxUint256) instead.
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export declare const getLeverageRequirementsApproval: (params: {
|
|
41
|
+
readonly chainId: number;
|
|
42
|
+
readonly token: Address;
|
|
43
|
+
readonly amount: bigint;
|
|
44
|
+
readonly allowance: bigint;
|
|
45
|
+
}) => readonly Readonly<Transaction<ERC20ApprovalAction>>[];
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { validateUint256Field } from "../../../helpers/validate.js";
|
|
2
|
+
import { getZeroExLeverageBundle } from "../../../helpers/zeroExQuote.js";
|
|
3
|
+
import { InvalidLeverageOperationError, } from "../../../types/index.js";
|
|
4
|
+
import { getRequirementsApproval } from "../getRequirementsApproval.js";
|
|
5
|
+
/**
|
|
6
|
+
* Computes the ERC-20 approvals a user gives ZeroExLeverageBundlesV1 so it can pull `amount` of
|
|
7
|
+
* `token`, given the existing allowance.
|
|
8
|
+
*
|
|
9
|
+
* For the leverage bundle the SDK ships (`isShippedZeroExLeverageBundle`), a short allowance is raised to
|
|
10
|
+
* `maxUint256`, capped per token by `MAX_TOKEN_APPROVALS`, like the other bundles' approvals, so one approval per token
|
|
11
|
+
* covers later leverage actions. A bundle registered with `registerCustomAddresses` gets an approval of exactly
|
|
12
|
+
* `amount` instead. For tokens in `APPROVE_ONLY_ONCE_TOKENS` (e.g. USDT) with a non-zero allowance, a reset to zero
|
|
13
|
+
* precedes it. The spender is always the chain's registered bundle, refused on chains where it cannot run.
|
|
14
|
+
*
|
|
15
|
+
* @param params.chainId - The chain the transaction targets.
|
|
16
|
+
* @param params.token - ERC-20 token the bundle pulls.
|
|
17
|
+
* @param params.amount - Amount the bundle pulls.
|
|
18
|
+
* @param params.allowance - The user's current allowance of `token` for the bundle.
|
|
19
|
+
* @returns Up to two deep-frozen `Transaction<ERC20ApprovalAction>` entries: an optional reset
|
|
20
|
+
* followed by the unlimited approval, or the exact one for a bundle the SDK does not ship. Empty when
|
|
21
|
+
* `allowance >= amount`.
|
|
22
|
+
* @throws {InvalidLeverageOperationError} when `amount` or `allowance` is not a bigint.
|
|
23
|
+
* @throws {NegativeInputError} when `amount` or `allowance` is negative.
|
|
24
|
+
* @throws {InputExceedsMaxError} when `amount` or `allowance` exceeds `uint256`.
|
|
25
|
+
* @throws {LeverageUnsupportedChainError} when the chain has no usable leverage bundle; a chain absent from the
|
|
26
|
+
* address registry reports `noAllowanceHolder`.
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* import { getLeverageRequirementsApproval } from "@morpho-org/morpho-sdk";
|
|
30
|
+
*
|
|
31
|
+
* // None ships yet: register a verified zeroExLeverageBundlesV1 address with `registerCustomAddresses` first.
|
|
32
|
+
* const txs = getLeverageRequirementsApproval({
|
|
33
|
+
* chainId: 8453,
|
|
34
|
+
* token: USDC,
|
|
35
|
+
* amount: 1_000_000n,
|
|
36
|
+
* allowance: 0n,
|
|
37
|
+
* });
|
|
38
|
+
* // txs: [approve(zeroExLeverageBundlesV1, 1_000_000n)], exactly the pull, for a registered bundle;
|
|
39
|
+
* // the bundle morpho-ts ships gets approve(zeroExLeverageBundlesV1, maxUint256) instead.
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export const getLeverageRequirementsApproval = (params) => {
|
|
43
|
+
const { chainId, token, amount, allowance } = params;
|
|
44
|
+
// A JavaScript caller can omit a field; an unknown pull or allowance must never get an approval.
|
|
45
|
+
for (const [field, value] of [
|
|
46
|
+
["amount", amount],
|
|
47
|
+
["allowance", allowance],
|
|
48
|
+
]) {
|
|
49
|
+
if (typeof value !== "bigint") {
|
|
50
|
+
throw new InvalidLeverageOperationError(field, value);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
validateUint256Field("amount", amount);
|
|
54
|
+
validateUint256Field("allowance", allowance);
|
|
55
|
+
const spender = getZeroExLeverageBundle(chainId);
|
|
56
|
+
return getRequirementsApproval({
|
|
57
|
+
address: token,
|
|
58
|
+
chainId,
|
|
59
|
+
args: { spendAmount: amount, spender },
|
|
60
|
+
allowances: allowance,
|
|
61
|
+
});
|
|
62
|
+
};
|
|
@@ -24,8 +24,8 @@ export declare const parseErc20Approval: (params: EncodeErc20ApprovalParams) =>
|
|
|
24
24
|
* @param params - Encoding parameters.
|
|
25
25
|
* @param params.token - ERC-20 token address to approve.
|
|
26
26
|
* @param params.spender - Address granted the allowance. Must be Permit2, Midnight,
|
|
27
|
-
* MidnightBundlesV2, VaultExitBundlesV1, VaultBundlesV1,
|
|
28
|
-
* the chain.
|
|
27
|
+
* MidnightBundlesV2, VaultExitBundlesV1, VaultBundlesV1, BlueBundlesV1, or
|
|
28
|
+
* ZeroExLeverageBundlesV1 for the chain.
|
|
29
29
|
* @param params.amount - Allowance amount before per-token cap.
|
|
30
30
|
* @param params.chainId - The chain the transaction targets (used to resolve supported spenders and the per-token cap).
|
|
31
31
|
* @returns A deep-frozen `Transaction<ERC20ApprovalAction>` with the capped approval amount.
|