@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
|
@@ -104,11 +104,12 @@ exports.midnightTakeRepayWithdrawCollateral = midnightTakeRepayWithdrawCollatera
|
|
|
104
104
|
const parsedTakeRepayWithdrawCollateral = Symbol();
|
|
105
105
|
/** Parses the static TakeRepayWithdrawCollateral constraints in the standalone builder's existing order. @internal */
|
|
106
106
|
const parseMidnightTakeRepayWithdrawCollateral = (params) => {
|
|
107
|
-
|
|
108
|
-
|
|
107
|
+
const { market, target, takeableOffers } = params;
|
|
108
|
+
if (target.type === "assets" && target.assets <= 0n) {
|
|
109
|
+
throw new index_js_1.NonPositiveInputError("target.assets", target.assets);
|
|
109
110
|
}
|
|
110
111
|
// Reject markets from another chain deployment before checking offers against them.
|
|
111
|
-
(0, validateMidnightMarket_js_1.validateMidnightMarket)({ market
|
|
112
|
+
(0, validateMidnightMarket_js_1.validateMidnightMarket)({ market, chainId: params.chainId });
|
|
112
113
|
for (const [index, { collateralIndex, assets },] of params.collateralWithdrawals.entries()) {
|
|
113
114
|
// Validation only; rejects non-positive amounts.
|
|
114
115
|
(0, validatePositiveInput_js_1.validatePositiveInput)(`collateralWithdrawals[${index}].assets`, assets);
|
|
@@ -116,25 +117,24 @@ const parseMidnightTakeRepayWithdrawCollateral = (params) => {
|
|
|
116
117
|
(0, validateMidnightBounds_js_1.validateMidnightUint128Field)(`collateralWithdrawals[${index}].assets`, assets);
|
|
117
118
|
}
|
|
118
119
|
// Throws UnknownCollateralIndexError when the index is not configured on the market.
|
|
119
|
-
midnight_sdk_1.MarketUtils.getCollateralByIndex(
|
|
120
|
+
midnight_sdk_1.MarketUtils.getCollateralByIndex(market, collateralIndex);
|
|
120
121
|
}
|
|
121
122
|
(0, validateMidnightAccount_js_1.validateMidnightAddress)("collateralReceiver", params.collateralReceiver);
|
|
122
123
|
if (params.collateralWithdrawals.length > 0) {
|
|
123
124
|
// Rejects receivers that would strand the withdrawn collateral in a contract.
|
|
124
125
|
(0, validateMidnightReceiver_js_1.validateMidnightReceiver)({
|
|
125
126
|
chainId: params.chainId,
|
|
126
|
-
market
|
|
127
|
+
market,
|
|
127
128
|
field: "collateralReceiver",
|
|
128
129
|
receiver: params.collateralReceiver,
|
|
129
130
|
});
|
|
130
131
|
}
|
|
131
132
|
const marketId = (0, validateTakeableOffers_js_1.validateTakeableOffers)({
|
|
132
|
-
market
|
|
133
|
-
takeableOffers
|
|
133
|
+
market,
|
|
134
|
+
takeableOffers,
|
|
134
135
|
expectedBuy: false,
|
|
135
136
|
});
|
|
136
|
-
(0, validateMidnightBounds_js_1.validateMidnightTakeableOfferBounds)(
|
|
137
|
-
const { target } = params;
|
|
137
|
+
(0, validateMidnightBounds_js_1.validateMidnightTakeableOfferBounds)(takeableOffers);
|
|
138
138
|
switch (target.type) {
|
|
139
139
|
case "assets": {
|
|
140
140
|
// Validation only; rejects non-positive amounts.
|
|
@@ -168,23 +168,29 @@ const parseMidnightTakeRepayWithdrawCollateral = (params) => {
|
|
|
168
168
|
// Rejects fee recipients that would strand the fee in a contract.
|
|
169
169
|
(0, validateMidnightReceiver_js_1.validateMidnightReceiver)({
|
|
170
170
|
chainId: params.chainId,
|
|
171
|
-
market
|
|
171
|
+
market,
|
|
172
172
|
field: "referralFeeRecipient",
|
|
173
173
|
receiver: referralFeeRecipient,
|
|
174
174
|
});
|
|
175
175
|
}
|
|
176
|
-
const
|
|
176
|
+
const marketStruct = midnight_sdk_1.MarketUtils.toStruct(market);
|
|
177
177
|
const nativeFunding = (0, parseMidnight_js_1.parseMidnightNativeFunding)({
|
|
178
178
|
chainId: params.chainId,
|
|
179
179
|
nativeAmount: params.nativeAmount,
|
|
180
180
|
pulls: [
|
|
181
181
|
{
|
|
182
|
-
token:
|
|
182
|
+
token: marketStruct.loanToken,
|
|
183
183
|
assets: target.type === "assets" ? target.assets : target.maxBuyerAssets,
|
|
184
184
|
},
|
|
185
185
|
],
|
|
186
186
|
});
|
|
187
|
-
const snapshot = (0, parse_js_2.snapshotMidnightTakeParams)({
|
|
187
|
+
const snapshot = (0, parse_js_2.snapshotMidnightTakeParams)({
|
|
188
|
+
direction: "buy",
|
|
189
|
+
market,
|
|
190
|
+
target,
|
|
191
|
+
takeableOffers,
|
|
192
|
+
metadata: params.metadata,
|
|
193
|
+
});
|
|
188
194
|
return (0, morpho_ts_1.deepFreeze)({
|
|
189
195
|
...snapshot,
|
|
190
196
|
chainId: params.chainId,
|
|
@@ -81,23 +81,23 @@ exports.midnightTakeWithdraw = midnightTakeWithdraw;
|
|
|
81
81
|
const parsedTakeWithdraw = Symbol();
|
|
82
82
|
/** Parses the static TakeWithdraw constraints in the standalone builder's existing order. @internal */
|
|
83
83
|
const parseMidnightTakeWithdraw = (params) => {
|
|
84
|
+
const { market, target, takeableOffers } = params;
|
|
84
85
|
// Reject markets from another chain deployment before checking offers against them.
|
|
85
|
-
(0, validateMidnightMarket_js_1.validateMidnightMarket)({ market
|
|
86
|
+
(0, validateMidnightMarket_js_1.validateMidnightMarket)({ market, chainId: params.chainId });
|
|
86
87
|
const marketId = (0, validateTakeableOffers_js_1.validateTakeableOffers)({
|
|
87
|
-
market
|
|
88
|
-
takeableOffers
|
|
88
|
+
market,
|
|
89
|
+
takeableOffers,
|
|
89
90
|
expectedBuy: true,
|
|
90
91
|
});
|
|
91
92
|
(0, validateMidnightAccount_js_1.validateMidnightAddress)("receiver", params.receiver);
|
|
92
|
-
(0, validateMidnightBounds_js_1.validateMidnightTakeableOfferBounds)(
|
|
93
|
+
(0, validateMidnightBounds_js_1.validateMidnightTakeableOfferBounds)(takeableOffers);
|
|
93
94
|
// Rejects receivers that would strand the withdrawn assets in a contract.
|
|
94
95
|
(0, validateMidnightReceiver_js_1.validateMidnightReceiver)({
|
|
95
96
|
chainId: params.chainId,
|
|
96
|
-
market
|
|
97
|
+
market,
|
|
97
98
|
field: "receiver",
|
|
98
99
|
receiver: params.receiver,
|
|
99
100
|
});
|
|
100
|
-
const { target } = params;
|
|
101
101
|
switch (target.type) {
|
|
102
102
|
case "assets": {
|
|
103
103
|
// Validation only; preserve main's positive-target and finite-cap order.
|
|
@@ -135,12 +135,18 @@ const parseMidnightTakeWithdraw = (params) => {
|
|
|
135
135
|
// Rejects fee recipients that would strand the fee in a contract.
|
|
136
136
|
(0, validateMidnightReceiver_js_1.validateMidnightReceiver)({
|
|
137
137
|
chainId: params.chainId,
|
|
138
|
-
market
|
|
138
|
+
market,
|
|
139
139
|
field: "referralFeeRecipient",
|
|
140
140
|
receiver: referralFeeRecipient,
|
|
141
141
|
});
|
|
142
142
|
}
|
|
143
|
-
const snapshot = (0, parse_js_2.snapshotMidnightTakeParams)({
|
|
143
|
+
const snapshot = (0, parse_js_2.snapshotMidnightTakeParams)({
|
|
144
|
+
direction: "sell",
|
|
145
|
+
market,
|
|
146
|
+
target,
|
|
147
|
+
takeableOffers,
|
|
148
|
+
metadata: params.metadata,
|
|
149
|
+
});
|
|
144
150
|
return (0, morpho_ts_1.deepFreeze)({
|
|
145
151
|
...snapshot,
|
|
146
152
|
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,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>>[]>;
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getLeverageRequirements = 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 validate_js_1 = require("../../../helpers/validate.js");
|
|
9
|
+
const zeroExQuote_js_1 = require("../../../helpers/zeroExQuote.js");
|
|
10
|
+
const assertContractCode_js_1 = require("../../../reads/assertContractCode.js");
|
|
11
|
+
const index_js_1 = require("../../../types/index.js");
|
|
12
|
+
const validateClientChainId_js_1 = require("../validateClientChainId.js");
|
|
13
|
+
const encodeLeverageAuthorization_js_1 = require("./encodeLeverageAuthorization.js");
|
|
14
|
+
const getLeverageRequirementsApproval_js_1 = require("./getLeverageRequirementsApproval.js");
|
|
15
|
+
const LEVERAGE_OPERATION_TYPES = [
|
|
16
|
+
"lever",
|
|
17
|
+
"delever",
|
|
18
|
+
"close",
|
|
19
|
+
];
|
|
20
|
+
const LEVER_AMOUNT_FIELDS = [
|
|
21
|
+
"collateralAssets",
|
|
22
|
+
"loanAssets",
|
|
23
|
+
"maxBorrowAssets",
|
|
24
|
+
];
|
|
25
|
+
const getTokenPulls = (marketParams, operation) => {
|
|
26
|
+
if (operation.type !== "lever") {
|
|
27
|
+
return [];
|
|
28
|
+
}
|
|
29
|
+
(0, validate_js_1.validateUint256Field)("collateralAssets", operation.collateralAssets);
|
|
30
|
+
(0, validate_js_1.validateUint256Field)("loanAssets", operation.loanAssets);
|
|
31
|
+
if ((0, viem_1.isAddressEqual)(marketParams.collateralToken, marketParams.loanToken)) {
|
|
32
|
+
const amount = operation.collateralAssets + operation.loanAssets;
|
|
33
|
+
(0, validate_js_1.validateUint256Field)("amount", amount);
|
|
34
|
+
return [{ token: marketParams.loanToken, amount }];
|
|
35
|
+
}
|
|
36
|
+
return [
|
|
37
|
+
{
|
|
38
|
+
token: marketParams.collateralToken,
|
|
39
|
+
amount: operation.collateralAssets,
|
|
40
|
+
},
|
|
41
|
+
{ token: marketParams.loanToken, amount: operation.loanAssets },
|
|
42
|
+
];
|
|
43
|
+
};
|
|
44
|
+
const needsAuthorization = (operation) => {
|
|
45
|
+
if (operation.type !== "lever") {
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
(0, validate_js_1.validateUint256Field)("maxBorrowAssets", operation.maxBorrowAssets);
|
|
49
|
+
return operation.maxBorrowAssets > 0n;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Resolves the transactions a user submits before a ZeroExLeverageBundlesV1 action: ERC-20
|
|
53
|
+
* approvals for the tokens it pulls, then the Morpho Blue authorization when its entry point
|
|
54
|
+
* needs one.
|
|
55
|
+
*
|
|
56
|
+
* Reads the user's allowance for each pulled token and `Morpho.isAuthorized(user, bundle)` on the
|
|
57
|
+
* target chain. For the leverage bundle the SDK ships it reads no contract code: `MorphoBlue.lever`, `delever` and
|
|
58
|
+
* `close` check Morpho Blue's code before calling it, and the shipped bundle's code check is waived
|
|
59
|
+
* (`docs/EXCEPTIONS.md`). A bundle registered with `registerCustomAddresses` falls back to the default rules: its code
|
|
60
|
+
* is checked before any read or grant, and its approvals are exact.
|
|
61
|
+
*
|
|
62
|
+
* - **Lever** pulls `collateralAssets` of the collateral token and `loanAssets` of the loan token
|
|
63
|
+
* from the user; when the allowance is short, each gets an unlimited approval (`maxUint256`,
|
|
64
|
+
* capped per token by `MAX_TOKEN_APPROVALS`), as for the other bundles, or an approval of exactly the pulled
|
|
65
|
+
* amount for a bundle the SDK does not ship. It borrows on Blue only
|
|
66
|
+
* when `maxBorrowAssets > 0`, so only then does it need the authorization.
|
|
67
|
+
* - **Delever** and **Close** pull nothing from the user and always withdraw collateral on Blue,
|
|
68
|
+
* so they need the authorization and no approval.
|
|
69
|
+
*
|
|
70
|
+
* The allowance and authorization reads run in parallel, each at the latest block its request sees; they are not
|
|
71
|
+
* pinned to one block, so a block landing between them can mix two states. The result is a snapshot of those
|
|
72
|
+
* reads: a requirement missed that way makes the action revert, and a returned approval still calls `approve` and
|
|
73
|
+
* replaces the allowance, with the zero reset of an approve-once token such as USDT planned from the allowance
|
|
74
|
+
* read. If the allowance or the authorization may have changed before submission, resolve the requirements again.
|
|
75
|
+
*
|
|
76
|
+
* The authorization is global over the user's Blue positions and persists after the action, like
|
|
77
|
+
* the unlimited approvals. The bundle takes no signed authorization, so no signature requirement
|
|
78
|
+
* is returned.
|
|
79
|
+
*
|
|
80
|
+
* @param params.viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
|
|
81
|
+
* @param params.chainId - Target chain id.
|
|
82
|
+
* @param params.userAddress - The account that sends the leverage transaction.
|
|
83
|
+
* @param params.marketParams - The market's loan and collateral tokens.
|
|
84
|
+
* @param params.operation - The leverage entry point and, for Lever, its pulled amounts.
|
|
85
|
+
* @returns Deep-frozen transactions in submission order: approvals (each optionally preceded by a
|
|
86
|
+
* reset to zero), then the authorization grant. Empty when nothing is missing.
|
|
87
|
+
* @throws {InvalidLeverageOperationError} when `operation` is missing or not an object, its `type` is
|
|
88
|
+
* not `lever`, `delever` or `close`, a Lever amount is not a bigint, or `userAddress` or a market token is
|
|
89
|
+
* not an address, before any read.
|
|
90
|
+
* @throws {ChainIdMismatchError} when the client declares, or its RPC reports via `eth_chainId`, a
|
|
91
|
+
* chain other than `params.chainId`.
|
|
92
|
+
* @throws {LeverageUnsupportedChainError} when the chain has no usable leverage bundle; a chain absent from the
|
|
93
|
+
* address registry reports `noAllowanceHolder`.
|
|
94
|
+
* @throws {MissingContractCodeError} when the chain's leverage bundle is not the one the SDK ships and has no code.
|
|
95
|
+
* @throws {NegativeInputError} when a Lever amount is negative.
|
|
96
|
+
* @throws {InputExceedsMaxError} when a Lever amount exceeds `uint256`; before any read,
|
|
97
|
+
* with `field: "amount"`, when the sum of both amounts on a market whose loan and collateral
|
|
98
|
+
* tokens are equal exceeds `uint256`.
|
|
99
|
+
* @throws {viem.BaseError} when the `eth_chainId` request or an allowance or authorization read
|
|
100
|
+
* fails.
|
|
101
|
+
* @example
|
|
102
|
+
* ```ts
|
|
103
|
+
* import { createPublicClient, http } from "viem";
|
|
104
|
+
* import { base } from "viem/chains";
|
|
105
|
+
* import { getLeverageRequirements } from "@morpho-org/morpho-sdk";
|
|
106
|
+
*
|
|
107
|
+
* const client = createPublicClient({ chain: base, transport: http() });
|
|
108
|
+
* // `leverTx`: the built Lever transaction; its args carry the amounts the Lever signs.
|
|
109
|
+
* const { collateralAssets, loanAssets, maxBorrowAssets } = leverTx.action.args;
|
|
110
|
+
* // None ships yet: register a verified zeroExLeverageBundlesV1 address with `registerCustomAddresses` first,
|
|
111
|
+
* // otherwise it throws `LeverageUnsupportedChainError` with `reason: "noBundle"`.
|
|
112
|
+
* const requirements = await getLeverageRequirements({
|
|
113
|
+
* viemClient: client,
|
|
114
|
+
* chainId: base.id,
|
|
115
|
+
* userAddress,
|
|
116
|
+
* marketParams,
|
|
117
|
+
* // Here collateralAssets is 1_000000000000000000n and loanAssets 0n.
|
|
118
|
+
* operation: { type: "lever", collateralAssets, loanAssets, maxBorrowAssets },
|
|
119
|
+
* });
|
|
120
|
+
* // requirements: [approve(bundle, 1_000000000000000000n), setAuthorization(bundle, true)] when both are
|
|
121
|
+
* // missing: a registered bundle is approved exactly the pull; the bundle morpho-ts ships gets maxUint256.
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
const getLeverageRequirements = async (params) => {
|
|
125
|
+
const { viemClient, chainId, userAddress, marketParams, operation } = params;
|
|
126
|
+
// A JavaScript caller can pass any shape; only well-formed known entry points may get a grant.
|
|
127
|
+
if (operation == null || typeof operation !== "object") {
|
|
128
|
+
throw new index_js_1.InvalidLeverageOperationError("operation", operation);
|
|
129
|
+
}
|
|
130
|
+
if (!LEVERAGE_OPERATION_TYPES.includes(operation.type)) {
|
|
131
|
+
throw new index_js_1.InvalidLeverageOperationError("type", operation.type);
|
|
132
|
+
}
|
|
133
|
+
if (operation.type === "lever") {
|
|
134
|
+
for (const field of LEVER_AMOUNT_FIELDS) {
|
|
135
|
+
if (typeof operation[field] !== "bigint") {
|
|
136
|
+
throw new index_js_1.InvalidLeverageOperationError(field, operation[field]);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
// A JavaScript caller can pass any value; a malformed address must never reach a read or a grant.
|
|
141
|
+
for (const [field, address] of [
|
|
142
|
+
["userAddress", userAddress],
|
|
143
|
+
["marketParams.loanToken", marketParams?.loanToken],
|
|
144
|
+
["marketParams.collateralToken", marketParams?.collateralToken],
|
|
145
|
+
]) {
|
|
146
|
+
if (typeof address !== "string" || !(0, viem_1.isAddress)(address, { strict: false })) {
|
|
147
|
+
throw new index_js_1.InvalidLeverageOperationError(field, address);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
(0, validate_js_1.validateChainId)(viemClient.chain?.id, chainId);
|
|
151
|
+
const bundle = (0, zeroExQuote_js_1.getZeroExLeverageBundle)(chainId);
|
|
152
|
+
const pulls = getTokenPulls(marketParams, operation).filter(({ amount }) => amount > 0n);
|
|
153
|
+
const authorize = needsAuthorization(operation);
|
|
154
|
+
await (0, validateClientChainId_js_1.validateClientChainId)(viemClient, chainId);
|
|
155
|
+
const pc = viemClient.extend(viem_1.publicActions);
|
|
156
|
+
if (!(0, morpho_ts_1.isShippedZeroExLeverageBundle)(chainId, bundle)) {
|
|
157
|
+
// The leverage exceptions cover only the shipped bundle: another one is never granted anything without code.
|
|
158
|
+
await (0, assertContractCode_js_1.assertContractCode)(pc, {
|
|
159
|
+
address: bundle,
|
|
160
|
+
chainId,
|
|
161
|
+
role: "bundles.zeroExLeverageBundlesV1",
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
const [approvals, isAuthorized] = await Promise.all([
|
|
165
|
+
Promise.all(pulls.map(async ({ token, amount }) => (0, getLeverageRequirementsApproval_js_1.getLeverageRequirementsApproval)({
|
|
166
|
+
chainId,
|
|
167
|
+
token,
|
|
168
|
+
amount,
|
|
169
|
+
allowance: await pc.readContract({
|
|
170
|
+
address: token,
|
|
171
|
+
abi: viem_1.erc20Abi,
|
|
172
|
+
functionName: "allowance",
|
|
173
|
+
args: [userAddress, bundle],
|
|
174
|
+
}),
|
|
175
|
+
}))),
|
|
176
|
+
authorize
|
|
177
|
+
? pc.readContract({
|
|
178
|
+
address: (0, blue_sdk_1.getChainAddresses)(chainId).blue,
|
|
179
|
+
abi: blue_sdk_viem_1.blueAbi,
|
|
180
|
+
functionName: "isAuthorized",
|
|
181
|
+
args: [userAddress, bundle],
|
|
182
|
+
})
|
|
183
|
+
: true,
|
|
184
|
+
]);
|
|
185
|
+
if (isAuthorized) {
|
|
186
|
+
return approvals.flat();
|
|
187
|
+
}
|
|
188
|
+
return [
|
|
189
|
+
...approvals.flat(),
|
|
190
|
+
(0, encodeLeverageAuthorization_js_1.encodeLeverageAuthorization)({ chainId, isAuthorized: true }),
|
|
191
|
+
];
|
|
192
|
+
};
|
|
193
|
+
exports.getLeverageRequirements = getLeverageRequirements;
|
|
@@ -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,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getLeverageRequirementsApproval = void 0;
|
|
4
|
+
const validate_js_1 = require("../../../helpers/validate.js");
|
|
5
|
+
const zeroExQuote_js_1 = require("../../../helpers/zeroExQuote.js");
|
|
6
|
+
const index_js_1 = require("../../../types/index.js");
|
|
7
|
+
const getRequirementsApproval_js_1 = require("../getRequirementsApproval.js");
|
|
8
|
+
/**
|
|
9
|
+
* Computes the ERC-20 approvals a user gives ZeroExLeverageBundlesV1 so it can pull `amount` of
|
|
10
|
+
* `token`, given the existing allowance.
|
|
11
|
+
*
|
|
12
|
+
* For the leverage bundle the SDK ships (`isShippedZeroExLeverageBundle`), a short allowance is raised to
|
|
13
|
+
* `maxUint256`, capped per token by `MAX_TOKEN_APPROVALS`, like the other bundles' approvals, so one approval per token
|
|
14
|
+
* covers later leverage actions. A bundle registered with `registerCustomAddresses` gets an approval of exactly
|
|
15
|
+
* `amount` instead. For tokens in `APPROVE_ONLY_ONCE_TOKENS` (e.g. USDT) with a non-zero allowance, a reset to zero
|
|
16
|
+
* precedes it. The spender is always the chain's registered bundle, refused on chains where it cannot run.
|
|
17
|
+
*
|
|
18
|
+
* @param params.chainId - The chain the transaction targets.
|
|
19
|
+
* @param params.token - ERC-20 token the bundle pulls.
|
|
20
|
+
* @param params.amount - Amount the bundle pulls.
|
|
21
|
+
* @param params.allowance - The user's current allowance of `token` for the bundle.
|
|
22
|
+
* @returns Up to two deep-frozen `Transaction<ERC20ApprovalAction>` entries: an optional reset
|
|
23
|
+
* followed by the unlimited approval, or the exact one for a bundle the SDK does not ship. Empty when
|
|
24
|
+
* `allowance >= amount`.
|
|
25
|
+
* @throws {InvalidLeverageOperationError} when `amount` or `allowance` is not a bigint.
|
|
26
|
+
* @throws {NegativeInputError} when `amount` or `allowance` is negative.
|
|
27
|
+
* @throws {InputExceedsMaxError} when `amount` or `allowance` exceeds `uint256`.
|
|
28
|
+
* @throws {LeverageUnsupportedChainError} when the chain has no usable leverage bundle; a chain absent from the
|
|
29
|
+
* address registry reports `noAllowanceHolder`.
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* import { getLeverageRequirementsApproval } from "@morpho-org/morpho-sdk";
|
|
33
|
+
*
|
|
34
|
+
* // None ships yet: register a verified zeroExLeverageBundlesV1 address with `registerCustomAddresses` first.
|
|
35
|
+
* const txs = getLeverageRequirementsApproval({
|
|
36
|
+
* chainId: 8453,
|
|
37
|
+
* token: USDC,
|
|
38
|
+
* amount: 1_000_000n,
|
|
39
|
+
* allowance: 0n,
|
|
40
|
+
* });
|
|
41
|
+
* // txs: [approve(zeroExLeverageBundlesV1, 1_000_000n)], exactly the pull, for a registered bundle;
|
|
42
|
+
* // the bundle morpho-ts ships gets approve(zeroExLeverageBundlesV1, maxUint256) instead.
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
const getLeverageRequirementsApproval = (params) => {
|
|
46
|
+
const { chainId, token, amount, allowance } = params;
|
|
47
|
+
// A JavaScript caller can omit a field; an unknown pull or allowance must never get an approval.
|
|
48
|
+
for (const [field, value] of [
|
|
49
|
+
["amount", amount],
|
|
50
|
+
["allowance", allowance],
|
|
51
|
+
]) {
|
|
52
|
+
if (typeof value !== "bigint") {
|
|
53
|
+
throw new index_js_1.InvalidLeverageOperationError(field, value);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
(0, validate_js_1.validateUint256Field)("amount", amount);
|
|
57
|
+
(0, validate_js_1.validateUint256Field)("allowance", allowance);
|
|
58
|
+
const spender = (0, zeroExQuote_js_1.getZeroExLeverageBundle)(chainId);
|
|
59
|
+
return (0, getRequirementsApproval_js_1.getRequirementsApproval)({
|
|
60
|
+
address: token,
|
|
61
|
+
chainId,
|
|
62
|
+
args: { spendAmount: amount, spender },
|
|
63
|
+
allowances: allowance,
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
exports.getLeverageRequirementsApproval = getLeverageRequirementsApproval;
|