@morpho-org/morpho-sdk 3.0.0 → 3.1.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/lib/cjs/actions/marketV1/index.d.ts +2 -0
- package/lib/cjs/actions/marketV1/index.js +2 -0
- package/lib/cjs/actions/marketV1/supply.d.ts +69 -0
- package/lib/cjs/actions/marketV1/supply.js +128 -0
- package/lib/cjs/actions/marketV1/supplyCollateral.d.ts +1 -1
- package/lib/cjs/actions/marketV1/supplyCollateral.js +2 -2
- package/lib/cjs/actions/marketV1/supplyCollateralBorrow.d.ts +1 -1
- package/lib/cjs/actions/marketV1/supplyCollateralBorrow.js +2 -2
- package/lib/cjs/actions/marketV1/withdraw.d.ts +83 -0
- package/lib/cjs/actions/marketV1/withdraw.js +110 -0
- package/lib/cjs/bundler/actions.d.ts +82 -0
- package/lib/cjs/bundler/actions.js +135 -0
- package/lib/cjs/bundler/types.d.ts +19 -0
- package/lib/cjs/entities/marketV1/marketV1.d.ts +121 -18
- package/lib/cjs/entities/marketV1/marketV1.js +144 -12
- package/lib/cjs/helpers/computeReallocations.d.ts +17 -6
- package/lib/cjs/helpers/computeReallocations.js +42 -11
- package/lib/cjs/helpers/index.d.ts +2 -2
- package/lib/cjs/helpers/index.js +6 -2
- package/lib/cjs/helpers/slippage.d.ts +57 -0
- package/lib/cjs/helpers/slippage.js +82 -0
- package/lib/cjs/helpers/validate.d.ts +38 -7
- package/lib/cjs/helpers/validate.js +54 -11
- package/lib/cjs/types/action.d.ts +27 -6
- package/lib/cjs/types/error.d.ts +68 -6
- package/lib/cjs/types/error.js +85 -12
- package/lib/cjs/utils.d.ts +1 -1
- package/lib/cjs/utils.js +2 -2
- package/lib/esm/actions/marketV1/index.d.ts +2 -0
- package/lib/esm/actions/marketV1/index.js +2 -0
- package/lib/esm/actions/marketV1/supply.d.ts +69 -0
- package/lib/esm/actions/marketV1/supply.js +124 -0
- package/lib/esm/actions/marketV1/supplyCollateral.d.ts +1 -1
- package/lib/esm/actions/marketV1/supplyCollateral.js +3 -3
- package/lib/esm/actions/marketV1/supplyCollateralBorrow.d.ts +1 -1
- package/lib/esm/actions/marketV1/supplyCollateralBorrow.js +3 -3
- package/lib/esm/actions/marketV1/withdraw.d.ts +83 -0
- package/lib/esm/actions/marketV1/withdraw.js +106 -0
- package/lib/esm/bundler/actions.d.ts +82 -0
- package/lib/esm/bundler/actions.js +135 -0
- package/lib/esm/bundler/types.d.ts +19 -0
- package/lib/esm/entities/marketV1/marketV1.d.ts +121 -18
- package/lib/esm/entities/marketV1/marketV1.js +147 -15
- package/lib/esm/helpers/computeReallocations.d.ts +17 -6
- package/lib/esm/helpers/computeReallocations.js +43 -12
- package/lib/esm/helpers/index.d.ts +2 -2
- package/lib/esm/helpers/index.js +2 -2
- package/lib/esm/helpers/slippage.d.ts +57 -0
- package/lib/esm/helpers/slippage.js +80 -0
- package/lib/esm/helpers/validate.d.ts +38 -7
- package/lib/esm/helpers/validate.js +51 -10
- package/lib/esm/types/action.d.ts +27 -6
- package/lib/esm/types/error.d.ts +68 -6
- package/lib/esm/types/error.js +73 -9
- package/lib/esm/utils.d.ts +1 -1
- package/lib/esm/utils.js +1 -1
- package/package.json +14 -10
|
@@ -165,9 +165,16 @@ var BundlerAction;
|
|
|
165
165
|
const [market, amount, onBehalf, onMorphoSupplyCollateral, skipRevert] = args;
|
|
166
166
|
return BundlerAction.morphoSupplyCollateral(chainId, market, amount, onBehalf, onMorphoSupplyCollateral.flatMap(BundlerAction.encode.bind(null, chainId)), skipRevert);
|
|
167
167
|
}
|
|
168
|
+
case "morphoSupply": {
|
|
169
|
+
const [market, assets, shares, slippageAmount, onBehalf, onMorphoSupply, skipRevert,] = args;
|
|
170
|
+
return BundlerAction.morphoSupply(chainId, market, assets, shares, slippageAmount, onBehalf, onMorphoSupply.flatMap(BundlerAction.encode.bind(null, chainId)), skipRevert);
|
|
171
|
+
}
|
|
168
172
|
case "morphoBorrow": {
|
|
169
173
|
return BundlerAction.morphoBorrow(chainId, ...args);
|
|
170
174
|
}
|
|
175
|
+
case "morphoWithdraw": {
|
|
176
|
+
return BundlerAction.morphoWithdraw(chainId, ...args);
|
|
177
|
+
}
|
|
171
178
|
case "morphoRepay": {
|
|
172
179
|
const [market, assets, shares, slippageAmount, onBehalf, onMorphoRepay, skipRevert,] = args;
|
|
173
180
|
return BundlerAction.morphoRepay(chainId, market, assets, shares, slippageAmount, onBehalf, onMorphoRepay.flatMap(BundlerAction.encode.bind(null, chainId)), skipRevert);
|
|
@@ -212,6 +219,17 @@ var BundlerAction;
|
|
|
212
219
|
nextValueState = encodedCallback.valueState;
|
|
213
220
|
}
|
|
214
221
|
}
|
|
222
|
+
if (action.type === "morphoSupply") {
|
|
223
|
+
const [, , , , , onMorphoSupply] = action.args;
|
|
224
|
+
for (const callbackAction of onMorphoSupply) {
|
|
225
|
+
const encodedCallback = encodeBundleAction({
|
|
226
|
+
chainId,
|
|
227
|
+
action: callbackAction,
|
|
228
|
+
valueState: nextValueState,
|
|
229
|
+
});
|
|
230
|
+
nextValueState = encodedCallback.valueState;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
215
233
|
if (action.type === "morphoRepay") {
|
|
216
234
|
const [, , , , , onMorphoRepay] = action.args;
|
|
217
235
|
for (const callbackAction of onMorphoRepay) {
|
|
@@ -681,6 +699,66 @@ var BundlerAction;
|
|
|
681
699
|
];
|
|
682
700
|
}
|
|
683
701
|
BundlerAction.morphoSupplyCollateral = morphoSupplyCollateral;
|
|
702
|
+
/**
|
|
703
|
+
* Encodes a GeneralAdapter1 Morpho Blue supply call.
|
|
704
|
+
*
|
|
705
|
+
* @param chainId - Chain where the action will execute.
|
|
706
|
+
* @param market - Morpho Blue market parameters.
|
|
707
|
+
* @param assets - Supply asset amount.
|
|
708
|
+
* @param shares - Supply share amount.
|
|
709
|
+
* @param slippageAmount - Slippage guard amount (max share price in RAY for share-price slippage).
|
|
710
|
+
* @param onBehalf - Account receiving the supply position.
|
|
711
|
+
* @param callbackCalls - Calls executed in Morpho's callback.
|
|
712
|
+
* @param skipRevert - Whether Bundler3 should tolerate a revert.
|
|
713
|
+
* @returns Encoded Bundler3 calls.
|
|
714
|
+
*
|
|
715
|
+
* @example
|
|
716
|
+
* ```ts
|
|
717
|
+
* import { BundlerAction } from "@morpho-org/morpho-sdk/bundler";
|
|
718
|
+
*
|
|
719
|
+
* const loanToken = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
|
|
720
|
+
* const collateralToken = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
|
|
721
|
+
* const oracle = "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419";
|
|
722
|
+
* const irm = "0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC";
|
|
723
|
+
* const onBehalf = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
|
|
724
|
+
* const marketParams = {
|
|
725
|
+
* loanToken,
|
|
726
|
+
* collateralToken,
|
|
727
|
+
* oracle,
|
|
728
|
+
* irm,
|
|
729
|
+
* lltv: 860_000000000000000000n,
|
|
730
|
+
* };
|
|
731
|
+
*
|
|
732
|
+
* const calls = BundlerAction.morphoSupply(
|
|
733
|
+
* 1,
|
|
734
|
+
* marketParams,
|
|
735
|
+
* 100n,
|
|
736
|
+
* 0n,
|
|
737
|
+
* 1_010000000000000000000000000n,
|
|
738
|
+
* onBehalf,
|
|
739
|
+
* [],
|
|
740
|
+
* );
|
|
741
|
+
* ```
|
|
742
|
+
*/
|
|
743
|
+
// biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
|
|
744
|
+
function morphoSupply(chainId, market, assets, shares, slippageAmount, onBehalf, callbackCalls, skipRevert = false) {
|
|
745
|
+
const { bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
|
|
746
|
+
const { callbackHash, reenterData } = encodeCallbackCalls(callbackCalls);
|
|
747
|
+
return [
|
|
748
|
+
{
|
|
749
|
+
to: generalAdapter1,
|
|
750
|
+
data: (0, viem_1.encodeFunctionData)({
|
|
751
|
+
abi: abis_js_1.generalAdapter1Abi,
|
|
752
|
+
functionName: "morphoSupply",
|
|
753
|
+
args: [market, assets, shares, slippageAmount, onBehalf, reenterData],
|
|
754
|
+
}),
|
|
755
|
+
value: 0n,
|
|
756
|
+
skipRevert,
|
|
757
|
+
callbackHash,
|
|
758
|
+
},
|
|
759
|
+
];
|
|
760
|
+
}
|
|
761
|
+
BundlerAction.morphoSupply = morphoSupply;
|
|
684
762
|
/**
|
|
685
763
|
* Encodes a GeneralAdapter1 Morpho Blue borrow call.
|
|
686
764
|
*
|
|
@@ -798,6 +876,63 @@ var BundlerAction;
|
|
|
798
876
|
];
|
|
799
877
|
}
|
|
800
878
|
BundlerAction.morphoRepay = morphoRepay;
|
|
879
|
+
/**
|
|
880
|
+
* Encodes a GeneralAdapter1 Morpho Blue withdraw call.
|
|
881
|
+
*
|
|
882
|
+
* @param chainId - Chain where the action will execute.
|
|
883
|
+
* @param market - Morpho Blue market parameters.
|
|
884
|
+
* @param assets - Withdraw asset amount.
|
|
885
|
+
* @param shares - Withdraw share amount.
|
|
886
|
+
* @param slippageAmount - Slippage guard amount (min share price in RAY for share-price slippage).
|
|
887
|
+
* @param receiver - Recipient of withdrawn assets.
|
|
888
|
+
* @param skipRevert - Whether Bundler3 should tolerate a revert.
|
|
889
|
+
* @returns Encoded Bundler3 calls.
|
|
890
|
+
*
|
|
891
|
+
* @example
|
|
892
|
+
* ```ts
|
|
893
|
+
* import { BundlerAction } from "@morpho-org/morpho-sdk/bundler";
|
|
894
|
+
*
|
|
895
|
+
* const loanToken = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
|
|
896
|
+
* const collateralToken = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
|
|
897
|
+
* const oracle = "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419";
|
|
898
|
+
* const irm = "0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC";
|
|
899
|
+
* const receiver = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
|
|
900
|
+
* const marketParams = {
|
|
901
|
+
* loanToken,
|
|
902
|
+
* collateralToken,
|
|
903
|
+
* oracle,
|
|
904
|
+
* irm,
|
|
905
|
+
* lltv: 860_000000000000000000n,
|
|
906
|
+
* };
|
|
907
|
+
*
|
|
908
|
+
* const calls = BundlerAction.morphoWithdraw(
|
|
909
|
+
* 1,
|
|
910
|
+
* marketParams,
|
|
911
|
+
* 100n,
|
|
912
|
+
* 0n,
|
|
913
|
+
* 990000000000000000000000000n,
|
|
914
|
+
* receiver,
|
|
915
|
+
* );
|
|
916
|
+
* ```
|
|
917
|
+
*/
|
|
918
|
+
// biome-ignore lint/complexity/useMaxParams: TODO refactor to ≤2 params
|
|
919
|
+
function morphoWithdraw(chainId, market, assets, shares, slippageAmount, receiver, skipRevert = false) {
|
|
920
|
+
const { bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
|
|
921
|
+
return [
|
|
922
|
+
{
|
|
923
|
+
to: generalAdapter1,
|
|
924
|
+
data: (0, viem_1.encodeFunctionData)({
|
|
925
|
+
abi: abis_js_1.generalAdapter1Abi,
|
|
926
|
+
functionName: "morphoWithdraw",
|
|
927
|
+
args: [market, assets, shares, slippageAmount, receiver],
|
|
928
|
+
}),
|
|
929
|
+
value: 0n,
|
|
930
|
+
skipRevert,
|
|
931
|
+
callbackHash: viem_1.zeroHash,
|
|
932
|
+
},
|
|
933
|
+
];
|
|
934
|
+
}
|
|
935
|
+
BundlerAction.morphoWithdraw = morphoWithdraw;
|
|
801
936
|
/**
|
|
802
937
|
* Encodes a GeneralAdapter1 Morpho Blue withdraw-collateral call.
|
|
803
938
|
*
|
|
@@ -121,6 +121,16 @@ export interface ActionArgs {
|
|
|
121
121
|
onMorphoSupplyCollateral: Action[],
|
|
122
122
|
skipRevert?: boolean
|
|
123
123
|
];
|
|
124
|
+
/** Morpho Blue supply call for `market`, `assets` or `shares`, `slippageAmount`, `onBehalf`, and callback actions; `skipRevert` controls Bundler3 revert handling. */
|
|
125
|
+
readonly morphoSupply: [
|
|
126
|
+
market: InputMarketParams,
|
|
127
|
+
assets: bigint,
|
|
128
|
+
shares: bigint,
|
|
129
|
+
slippageAmount: bigint,
|
|
130
|
+
onBehalf: Address,
|
|
131
|
+
onMorphoSupply: Action[],
|
|
132
|
+
skipRevert?: boolean
|
|
133
|
+
];
|
|
124
134
|
/** Morpho Blue borrow call for `market`, `assets` or `shares`, `slippageAmount`, and `receiver`; `skipRevert` controls Bundler3 revert handling. */
|
|
125
135
|
readonly morphoBorrow: [
|
|
126
136
|
market: InputMarketParams,
|
|
@@ -140,6 +150,15 @@ export interface ActionArgs {
|
|
|
140
150
|
onMorphoRepay: Action[],
|
|
141
151
|
skipRevert?: boolean
|
|
142
152
|
];
|
|
153
|
+
/** Morpho Blue withdraw call for `market`, `assets` or `shares`, `slippageAmount`, and `receiver`; `skipRevert` controls Bundler3 revert handling. */
|
|
154
|
+
readonly morphoWithdraw: [
|
|
155
|
+
market: InputMarketParams,
|
|
156
|
+
assets: bigint,
|
|
157
|
+
shares: bigint,
|
|
158
|
+
slippageAmount: bigint,
|
|
159
|
+
receiver: Address,
|
|
160
|
+
skipRevert?: boolean
|
|
161
|
+
];
|
|
143
162
|
/** Morpho Blue withdraw-collateral call for `market`, `assets`, and `receiver`; `skipRevert` controls Bundler3 revert handling. */
|
|
144
163
|
readonly morphoWithdrawCollateral: [
|
|
145
164
|
market: InputMarketParams,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type AccrualPosition, type Market, type MarketParams } from "@morpho-org/blue-sdk";
|
|
2
2
|
import type { Address } from "viem";
|
|
3
3
|
import type { FetchParameters } from "../../types/data.js";
|
|
4
|
-
import { type DepositAmountArgs, type ERC20ApprovalAction, type MarketV1BorrowAction, type MarketV1RepayAction, type MarketV1RepayWithdrawCollateralAction, type MarketV1SupplyCollateralAction, type MarketV1SupplyCollateralBorrowAction, type MarketV1WithdrawCollateralAction, type MorphoAuthorizationAction, type MorphoClientType, type ReallocationComputeOptions, type RepayAmountArgs, type Requirement, type RequirementSignature, type Transaction, type VaultReallocation } from "../../types/index.js";
|
|
4
|
+
import { type AssetsOrSharesArgs, type DepositAmountArgs, type ERC20ApprovalAction, type MarketV1BorrowAction, type MarketV1RepayAction, type MarketV1RepayWithdrawCollateralAction, type MarketV1SupplyAction, type MarketV1SupplyCollateralAction, type MarketV1SupplyCollateralBorrowAction, type MarketV1WithdrawAction, type MarketV1WithdrawCollateralAction, type MorphoAuthorizationAction, type MorphoClientType, type ReallocationComputeOptions, type RepayAmountArgs, type Requirement, type RequirementSignature, type Transaction, type VaultReallocation } from "../../types/index.js";
|
|
5
5
|
import { ReallocationData } from "../reallocationData.js";
|
|
6
6
|
export interface MarketV1Actions {
|
|
7
7
|
/**
|
|
@@ -37,6 +37,61 @@ export interface MarketV1Actions {
|
|
|
37
37
|
useSimplePermit?: boolean;
|
|
38
38
|
}) => Promise<(Readonly<Transaction<ERC20ApprovalAction>> | Requirement)[]>;
|
|
39
39
|
};
|
|
40
|
+
/**
|
|
41
|
+
* Prepares a loan-asset supply transaction.
|
|
42
|
+
*
|
|
43
|
+
* Routed through bundler via GeneralAdapter1. Computes `maxSharePrice` from market supply
|
|
44
|
+
* state and `slippageTolerance` to protect against share-price inflation.
|
|
45
|
+
* `getRequirements` returns ERC20 approval or permit for `GeneralAdapter1` on the loan token.
|
|
46
|
+
* When `nativeAmount` is provided, native token is wrapped; the loan token must be wNative.
|
|
47
|
+
*
|
|
48
|
+
* No Morpho authorization required (supplier is crediting, not withdrawing).
|
|
49
|
+
*
|
|
50
|
+
* @param params - Supply parameters.
|
|
51
|
+
* @returns Object with `buildTx` and `getRequirements`.
|
|
52
|
+
*/
|
|
53
|
+
supply: (params: {
|
|
54
|
+
userAddress: Address;
|
|
55
|
+
marketData: Market;
|
|
56
|
+
slippageTolerance?: bigint;
|
|
57
|
+
} & DepositAmountArgs) => {
|
|
58
|
+
buildTx: (requirementSignature?: RequirementSignature) => Readonly<Transaction<MarketV1SupplyAction>>;
|
|
59
|
+
getRequirements: (params?: {
|
|
60
|
+
useSimplePermit?: boolean;
|
|
61
|
+
}) => Promise<(Readonly<Transaction<ERC20ApprovalAction>> | Requirement)[]>;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Prepares a loan-asset withdraw transaction.
|
|
65
|
+
*
|
|
66
|
+
* Routed through bundler3 via `morphoWithdraw`. Supports two modes via {@link AssetsOrSharesArgs}:
|
|
67
|
+
* - **By assets** (`{ assets }`): withdraws an exact asset amount.
|
|
68
|
+
* - **By shares** (`{ shares }`): burns an exact share count (full close, immune to interest accrual).
|
|
69
|
+
*
|
|
70
|
+
* Computes `minSharePrice` from market supply state and `slippageTolerance`.
|
|
71
|
+
*
|
|
72
|
+
* When `reallocations` is provided, `reallocateTo` actions are prepended to the bundle,
|
|
73
|
+
* moving liquidity from other markets via the PublicAllocator before withdrawing — used to
|
|
74
|
+
* unblock withdraws that exceed on-market liquidity.
|
|
75
|
+
*
|
|
76
|
+
* `getRequirements` returns `morpho.setAuthorization(generalAdapter1, true)` if GA1 is not
|
|
77
|
+
* yet authorized on Morpho (returns `[]` when already authorized), since the bundler calls
|
|
78
|
+
* `withdraw(...,onBehalf=user,...)`.
|
|
79
|
+
*
|
|
80
|
+
* **Stale `positionData` may cause unexpected supply share calculations.**
|
|
81
|
+
*
|
|
82
|
+
* @param params - Withdraw parameters including pre-fetched `positionData`.
|
|
83
|
+
* @returns Object with `buildTx` and `getRequirements`.
|
|
84
|
+
*/
|
|
85
|
+
withdraw: (params: {
|
|
86
|
+
userAddress: Address;
|
|
87
|
+
receiver?: Address;
|
|
88
|
+
positionData: AccrualPosition;
|
|
89
|
+
slippageTolerance?: bigint;
|
|
90
|
+
reallocations?: readonly VaultReallocation[];
|
|
91
|
+
} & AssetsOrSharesArgs) => {
|
|
92
|
+
buildTx: () => Readonly<Transaction<MarketV1WithdrawAction>>;
|
|
93
|
+
getRequirements: () => Promise<Readonly<Transaction<MorphoAuthorizationAction>>[]>;
|
|
94
|
+
};
|
|
40
95
|
/**
|
|
41
96
|
* Prepares a borrow transaction.
|
|
42
97
|
*
|
|
@@ -201,29 +256,43 @@ export interface MarketV1Actions {
|
|
|
201
256
|
};
|
|
202
257
|
}) => Promise<ReallocationData>;
|
|
203
258
|
/**
|
|
204
|
-
* Computes vault reallocations for a borrow on this market.
|
|
259
|
+
* Computes vault reallocations for a borrow or withdraw on this market.
|
|
205
260
|
*
|
|
206
|
-
* Uses the shared
|
|
207
|
-
*
|
|
208
|
-
*
|
|
261
|
+
* Uses the shared-liquidity algorithm to determine which vaults should reallocate liquidity to
|
|
262
|
+
* this market via the PublicAllocator, based on the post-operation utilization target.
|
|
263
|
+
*
|
|
264
|
+
* Pass `{ borrowAmount }` for a borrow (legacy alias, equivalent to `{ operation: "borrow",
|
|
265
|
+
* amount }`) or `{ operation: "withdraw", amount }` for a loan-asset withdraw.
|
|
209
266
|
*
|
|
210
267
|
* @param params.reallocationData - The current on-chain state (from {@link getReallocationData}).
|
|
211
|
-
* @param params.
|
|
268
|
+
* @param params.operation - The operation driving the reallocation (`"borrow"` or `"withdraw"`).
|
|
269
|
+
* Defaults to `"borrow"` when `borrowAmount` is provided.
|
|
270
|
+
* @param params.amount - The borrow or withdraw amount used to compute the post-state utilization.
|
|
271
|
+
* @param params.borrowAmount - {@deprecated} Equivalent to `{ operation: "borrow", amount }`. Use the
|
|
272
|
+
* `operation` + `amount` form on new code.
|
|
212
273
|
* @param params.options - Optional reallocation computation options
|
|
213
274
|
* (timestamp, utilization targets, reallocatable vaults filter, etc.).
|
|
214
275
|
* Pass the fetched block timestamp to compute reallocations at the same block.
|
|
215
|
-
* @returns Array of vault reallocations ready to pass to `borrow()`
|
|
216
|
-
* `
|
|
276
|
+
* @returns Array of vault reallocations ready to pass to `borrow()`, `supplyCollateralBorrow()`,
|
|
277
|
+
* or `withdraw()`. Empty array if no reallocation is needed.
|
|
217
278
|
* @throws {ChainIdMismatchError} when `reallocationData` belongs to a different chain than this market.
|
|
218
|
-
* @throws {InsufficientSharedLiquidityError} when shared liquidity cannot cover the
|
|
279
|
+
* @throws {InsufficientSharedLiquidityError} when shared liquidity cannot cover the operation's absolute shortfall on the target market — preventing fee-bearing reallocations from being attached to a call that would still revert onchain.
|
|
219
280
|
* @throws {MissingPublicAllocatorConfigError} when a selected vault is missing its public allocator config.
|
|
220
281
|
* @throws {UnknownReallocationMarketError} when the target market is absent from the reallocation data.
|
|
221
282
|
*/
|
|
222
283
|
getReallocations: (params: {
|
|
223
284
|
reallocationData: ReallocationData;
|
|
224
|
-
borrowAmount: bigint;
|
|
225
285
|
options?: ReallocationComputeOptions;
|
|
226
|
-
}
|
|
286
|
+
} & ({
|
|
287
|
+
operation: "borrow" | "withdraw";
|
|
288
|
+
amount: bigint;
|
|
289
|
+
borrowAmount?: never;
|
|
290
|
+
} | {
|
|
291
|
+
/** @deprecated Pass `{ operation: "borrow", amount }` instead. */
|
|
292
|
+
borrowAmount: bigint;
|
|
293
|
+
operation?: never;
|
|
294
|
+
amount?: never;
|
|
295
|
+
})) => readonly VaultReallocation[];
|
|
227
296
|
}
|
|
228
297
|
export declare class MorphoMarketV1 implements MarketV1Actions {
|
|
229
298
|
private readonly client;
|
|
@@ -232,6 +301,26 @@ export declare class MorphoMarketV1 implements MarketV1Actions {
|
|
|
232
301
|
constructor(client: MorphoClientType, marketParams: MarketParams, chainId: number);
|
|
233
302
|
getMarketData(parameters?: FetchParameters): Promise<Market>;
|
|
234
303
|
getPositionData(userAddress: Address, parameters?: FetchParameters): Promise<AccrualPosition>;
|
|
304
|
+
supply({ amount, userAddress, nativeAmount, marketData, slippageTolerance, }: {
|
|
305
|
+
userAddress: Address;
|
|
306
|
+
marketData: Market;
|
|
307
|
+
slippageTolerance?: bigint;
|
|
308
|
+
} & DepositAmountArgs): {
|
|
309
|
+
getRequirements: (params?: {
|
|
310
|
+
useSimplePermit?: boolean;
|
|
311
|
+
}) => Promise<(Requirement | Readonly<Transaction<ERC20ApprovalAction>>)[]>;
|
|
312
|
+
buildTx: (requirementSignature?: RequirementSignature) => Readonly<Transaction<MarketV1SupplyAction>>;
|
|
313
|
+
};
|
|
314
|
+
withdraw(params: {
|
|
315
|
+
userAddress: Address;
|
|
316
|
+
receiver?: Address;
|
|
317
|
+
positionData: AccrualPosition;
|
|
318
|
+
slippageTolerance?: bigint;
|
|
319
|
+
reallocations?: readonly VaultReallocation[];
|
|
320
|
+
} & AssetsOrSharesArgs): {
|
|
321
|
+
getRequirements: () => Promise<Readonly<Transaction<MorphoAuthorizationAction>>[]>;
|
|
322
|
+
buildTx: () => Readonly<Transaction<MarketV1WithdrawAction>>;
|
|
323
|
+
};
|
|
235
324
|
supplyCollateral({ amount, userAddress, nativeAmount, }: {
|
|
236
325
|
userAddress: Address;
|
|
237
326
|
} & DepositAmountArgs): {
|
|
@@ -307,21 +396,35 @@ export declare class MorphoMarketV1 implements MarketV1Actions {
|
|
|
307
396
|
};
|
|
308
397
|
}): Promise<ReallocationData>;
|
|
309
398
|
/**
|
|
310
|
-
* Computes public allocator reallocations for a borrow on this market.
|
|
399
|
+
* Computes public allocator reallocations for a borrow or withdraw on this market.
|
|
400
|
+
*
|
|
401
|
+
* Pass `{ borrowAmount }` for a borrow (legacy alias, equivalent to `{ operation: "borrow", amount }`)
|
|
402
|
+
* or `{ operation, amount }` for a borrow or loan-asset withdraw.
|
|
311
403
|
*
|
|
312
404
|
* @param params - Reallocation computation parameters.
|
|
313
405
|
* @param params.reallocationData - State returned by {@link getReallocationData}.
|
|
314
|
-
* @param params.
|
|
406
|
+
* @param params.operation - The operation driving the reallocation (`"borrow"` or `"withdraw"`).
|
|
407
|
+
* @param params.amount - The borrow or withdraw amount used to compute the post-state utilization.
|
|
408
|
+
* @param params.borrowAmount - {@deprecated Pass `{ operation: "borrow", amount }` instead.}
|
|
315
409
|
* @param params.options - Optional allocator and utilization options.
|
|
316
|
-
* @returns Vault reallocations ready to pass to `borrow` or `
|
|
410
|
+
* @returns Vault reallocations ready to pass to `borrow`, `supplyCollateralBorrow`, or `withdraw`.
|
|
317
411
|
* @throws {ChainIdMismatchError} when `reallocationData` belongs to a different chain than this market.
|
|
318
|
-
* @throws {InsufficientSharedLiquidityError} when shared liquidity cannot cover the
|
|
412
|
+
* @throws {InsufficientSharedLiquidityError} when shared liquidity cannot cover the operation's absolute shortfall on the target market.
|
|
413
|
+
* @throws {ReallocationWithdrawExceedsMarketSupplyError} when `operation === "withdraw"` and `amount` exceeds the target market's `totalSupplyAssets`.
|
|
319
414
|
* @throws {MissingPublicAllocatorConfigError} when a selected vault is missing its public allocator config.
|
|
320
415
|
* @throws {UnknownReallocationMarketError} when the target market is absent from the reallocation data.
|
|
321
416
|
*/
|
|
322
|
-
getReallocations(
|
|
417
|
+
getReallocations(params: {
|
|
323
418
|
reallocationData: ReallocationData;
|
|
324
|
-
borrowAmount: bigint;
|
|
325
419
|
options?: ReallocationComputeOptions;
|
|
326
|
-
}
|
|
420
|
+
} & ({
|
|
421
|
+
operation: "borrow" | "withdraw";
|
|
422
|
+
amount: bigint;
|
|
423
|
+
borrowAmount?: never;
|
|
424
|
+
} | {
|
|
425
|
+
/** @deprecated Pass `{ operation: "borrow", amount }` instead. */
|
|
426
|
+
borrowAmount: bigint;
|
|
427
|
+
operation?: never;
|
|
428
|
+
amount?: never;
|
|
429
|
+
})): readonly VaultReallocation[];
|
|
327
430
|
}
|
|
@@ -34,6 +34,120 @@ class MorphoMarketV1 {
|
|
|
34
34
|
chainId: this.chainId,
|
|
35
35
|
});
|
|
36
36
|
}
|
|
37
|
+
supply({ amount = 0n, userAddress, nativeAmount, marketData, slippageTolerance = blue_sdk_1.DEFAULT_SLIPPAGE_TOLERANCE, }) {
|
|
38
|
+
(0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
|
|
39
|
+
if (amount < 0n) {
|
|
40
|
+
throw new index_js_3.NegativeSupplyAmountError(this.marketParams.id);
|
|
41
|
+
}
|
|
42
|
+
if (nativeAmount !== undefined && nativeAmount < 0n) {
|
|
43
|
+
throw new index_js_3.NegativeNativeAmountError(nativeAmount);
|
|
44
|
+
}
|
|
45
|
+
const totalAssets = amount + (nativeAmount ?? 0n);
|
|
46
|
+
if (totalAssets === 0n) {
|
|
47
|
+
throw new index_js_3.ZeroSupplyAmountError(this.marketParams.id);
|
|
48
|
+
}
|
|
49
|
+
if (marketData.id !== this.marketParams.id) {
|
|
50
|
+
throw new index_js_3.MarketIdMismatchError(marketData.id, this.marketParams.id);
|
|
51
|
+
}
|
|
52
|
+
(0, index_js_2.validateSlippageTolerance)(slippageTolerance);
|
|
53
|
+
if (nativeAmount !== undefined && nativeAmount > 0n) {
|
|
54
|
+
(0, index_js_2.validateNativeAsset)(this.chainId, this.marketParams.loanToken);
|
|
55
|
+
}
|
|
56
|
+
const maxSharePrice = (0, index_js_2.computeMaxSupplySharePrice)({
|
|
57
|
+
supplyAssets: totalAssets,
|
|
58
|
+
market: marketData,
|
|
59
|
+
slippageTolerance,
|
|
60
|
+
});
|
|
61
|
+
return {
|
|
62
|
+
getRequirements: (params) => (0, index_js_1.getRequirements)(this.client.viemClient, {
|
|
63
|
+
address: this.marketParams.loanToken,
|
|
64
|
+
chainId: this.chainId,
|
|
65
|
+
supportSignature: this.client.options.supportSignature,
|
|
66
|
+
supportDeployless: this.client.options.supportDeployless,
|
|
67
|
+
useSimplePermit: params?.useSimplePermit,
|
|
68
|
+
args: { amount, from: userAddress },
|
|
69
|
+
}),
|
|
70
|
+
buildTx: (requirementSignature) => (0, index_js_1.marketV1Supply)({
|
|
71
|
+
market: { chainId: this.chainId, marketParams: this.marketParams },
|
|
72
|
+
args: {
|
|
73
|
+
amount,
|
|
74
|
+
nativeAmount,
|
|
75
|
+
onBehalf: userAddress,
|
|
76
|
+
maxSharePrice,
|
|
77
|
+
requirementSignature,
|
|
78
|
+
},
|
|
79
|
+
metadata: this.client.options.metadata,
|
|
80
|
+
}),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
withdraw(params) {
|
|
84
|
+
(0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
|
|
85
|
+
const { userAddress, receiver = userAddress, positionData, slippageTolerance = blue_sdk_1.DEFAULT_SLIPPAGE_TOLERANCE, reallocations, } = params;
|
|
86
|
+
// Mode normalization: a missing or undefined `assets`/`shares` key collapses to `0n`
|
|
87
|
+
// so the mutual-exclusion and positivity checks below are pure value comparisons.
|
|
88
|
+
const assets = ("assets" in params ? params.assets : undefined) ?? 0n;
|
|
89
|
+
const shares = ("shares" in params ? params.shares : undefined) ?? 0n;
|
|
90
|
+
// Mode conflict comes first: detect "both values present" (either non-zero)
|
|
91
|
+
// before positivity, so `{ assets: -1n, shares: 5n }` reports the actual
|
|
92
|
+
// mode conflict instead of being misclassified as a positivity error.
|
|
93
|
+
// Mirrors action layer.
|
|
94
|
+
if (assets !== 0n && shares !== 0n) {
|
|
95
|
+
throw new index_js_3.MutuallyExclusiveWithdrawAmountsError(this.marketParams.id);
|
|
96
|
+
}
|
|
97
|
+
if (assets < 0n || shares < 0n || (assets === 0n && shares === 0n)) {
|
|
98
|
+
throw new index_js_3.NonPositiveWithdrawAmountError(this.marketParams.id);
|
|
99
|
+
}
|
|
100
|
+
(0, index_js_2.validateSlippageTolerance)(slippageTolerance);
|
|
101
|
+
if (!positionData) {
|
|
102
|
+
throw new index_js_3.MissingAccrualPositionError(this.marketParams.id);
|
|
103
|
+
}
|
|
104
|
+
(0, index_js_2.validateAccrualPosition)({
|
|
105
|
+
positionData,
|
|
106
|
+
expectedMarketId: this.marketParams.id,
|
|
107
|
+
expectedUser: userAddress,
|
|
108
|
+
});
|
|
109
|
+
if (shares > 0n) {
|
|
110
|
+
(0, index_js_2.validateWithdrawShares)({
|
|
111
|
+
positionData,
|
|
112
|
+
withdrawShares: shares,
|
|
113
|
+
marketId: this.marketParams.id,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
(0, index_js_2.validateWithdrawAmount)({
|
|
118
|
+
positionData,
|
|
119
|
+
withdrawAssets: assets,
|
|
120
|
+
marketId: this.marketParams.id,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
const minSharePrice = (0, index_js_2.computeMinWithdrawSharePrice)({
|
|
124
|
+
withdrawAssets: assets,
|
|
125
|
+
withdrawShares: shares,
|
|
126
|
+
market: positionData.market,
|
|
127
|
+
slippageTolerance,
|
|
128
|
+
});
|
|
129
|
+
return {
|
|
130
|
+
getRequirements: async () => {
|
|
131
|
+
const authTx = await (0, index_js_1.getMorphoAuthorizationRequirement)({
|
|
132
|
+
viemClient: this.client.viemClient,
|
|
133
|
+
chainId: this.chainId,
|
|
134
|
+
userAddress,
|
|
135
|
+
});
|
|
136
|
+
return authTx ? [authTx] : [];
|
|
137
|
+
},
|
|
138
|
+
buildTx: () => (0, index_js_1.marketV1Withdraw)({
|
|
139
|
+
market: { chainId: this.chainId, marketParams: this.marketParams },
|
|
140
|
+
args: {
|
|
141
|
+
assets,
|
|
142
|
+
shares,
|
|
143
|
+
receiver,
|
|
144
|
+
minSharePrice,
|
|
145
|
+
reallocations,
|
|
146
|
+
},
|
|
147
|
+
metadata: this.client.options.metadata,
|
|
148
|
+
}),
|
|
149
|
+
};
|
|
150
|
+
}
|
|
37
151
|
supplyCollateral({ amount = 0n, userAddress, nativeAmount, }) {
|
|
38
152
|
(0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
|
|
39
153
|
if (amount < 0n) {
|
|
@@ -47,7 +161,7 @@ class MorphoMarketV1 {
|
|
|
47
161
|
throw new index_js_3.ZeroCollateralAmountError(this.marketParams.id);
|
|
48
162
|
}
|
|
49
163
|
if (nativeAmount !== undefined && nativeAmount > 0n) {
|
|
50
|
-
(0, index_js_2.
|
|
164
|
+
(0, index_js_2.validateNativeAsset)(this.chainId, this.marketParams.collateralToken);
|
|
51
165
|
}
|
|
52
166
|
return {
|
|
53
167
|
getRequirements: (params) => (0, index_js_1.getRequirements)(this.client.viemClient, {
|
|
@@ -395,7 +509,7 @@ class MorphoMarketV1 {
|
|
|
395
509
|
}
|
|
396
510
|
(0, index_js_2.validateSlippageTolerance)(slippageTolerance);
|
|
397
511
|
if (nativeAmount !== undefined && nativeAmount > 0n) {
|
|
398
|
-
(0, index_js_2.
|
|
512
|
+
(0, index_js_2.validateNativeAsset)(this.chainId, this.marketParams.collateralToken);
|
|
399
513
|
}
|
|
400
514
|
(0, index_js_2.validatePositionHealth)({
|
|
401
515
|
positionData,
|
|
@@ -524,25 +638,43 @@ class MorphoMarketV1 {
|
|
|
524
638
|
});
|
|
525
639
|
}
|
|
526
640
|
/**
|
|
527
|
-
* Computes public allocator reallocations for a borrow on this market.
|
|
641
|
+
* Computes public allocator reallocations for a borrow or withdraw on this market.
|
|
642
|
+
*
|
|
643
|
+
* Pass `{ borrowAmount }` for a borrow (legacy alias, equivalent to `{ operation: "borrow", amount }`)
|
|
644
|
+
* or `{ operation, amount }` for a borrow or loan-asset withdraw.
|
|
528
645
|
*
|
|
529
646
|
* @param params - Reallocation computation parameters.
|
|
530
647
|
* @param params.reallocationData - State returned by {@link getReallocationData}.
|
|
531
|
-
* @param params.
|
|
648
|
+
* @param params.operation - The operation driving the reallocation (`"borrow"` or `"withdraw"`).
|
|
649
|
+
* @param params.amount - The borrow or withdraw amount used to compute the post-state utilization.
|
|
650
|
+
* @param params.borrowAmount - {@deprecated Pass `{ operation: "borrow", amount }` instead.}
|
|
532
651
|
* @param params.options - Optional allocator and utilization options.
|
|
533
|
-
* @returns Vault reallocations ready to pass to `borrow` or `
|
|
652
|
+
* @returns Vault reallocations ready to pass to `borrow`, `supplyCollateralBorrow`, or `withdraw`.
|
|
534
653
|
* @throws {ChainIdMismatchError} when `reallocationData` belongs to a different chain than this market.
|
|
535
|
-
* @throws {InsufficientSharedLiquidityError} when shared liquidity cannot cover the
|
|
654
|
+
* @throws {InsufficientSharedLiquidityError} when shared liquidity cannot cover the operation's absolute shortfall on the target market.
|
|
655
|
+
* @throws {ReallocationWithdrawExceedsMarketSupplyError} when `operation === "withdraw"` and `amount` exceeds the target market's `totalSupplyAssets`.
|
|
536
656
|
* @throws {MissingPublicAllocatorConfigError} when a selected vault is missing its public allocator config.
|
|
537
657
|
* @throws {UnknownReallocationMarketError} when the target market is absent from the reallocation data.
|
|
538
658
|
*/
|
|
539
|
-
getReallocations(
|
|
540
|
-
(0, index_js_2.validateChainId)(reallocationData.chainId, this.chainId);
|
|
659
|
+
getReallocations(params) {
|
|
660
|
+
(0, index_js_2.validateChainId)(params.reallocationData.chainId, this.chainId);
|
|
661
|
+
const marketId = this.marketParams.id;
|
|
662
|
+
const options = { enabled: true, ...params.options };
|
|
663
|
+
if (params.borrowAmount !== undefined) {
|
|
664
|
+
return (0, index_js_2.computeReallocations)({
|
|
665
|
+
reallocationData: params.reallocationData,
|
|
666
|
+
marketId,
|
|
667
|
+
operation: "borrow",
|
|
668
|
+
amount: params.borrowAmount,
|
|
669
|
+
options,
|
|
670
|
+
});
|
|
671
|
+
}
|
|
541
672
|
return (0, index_js_2.computeReallocations)({
|
|
542
|
-
reallocationData,
|
|
543
|
-
marketId
|
|
544
|
-
|
|
545
|
-
|
|
673
|
+
reallocationData: params.reallocationData,
|
|
674
|
+
marketId,
|
|
675
|
+
operation: params.operation,
|
|
676
|
+
amount: params.amount,
|
|
677
|
+
options,
|
|
546
678
|
});
|
|
547
679
|
}
|
|
548
680
|
}
|
|
@@ -2,19 +2,28 @@ import { type MarketId } from "@morpho-org/blue-sdk";
|
|
|
2
2
|
import type { ReallocationData } from "../entities/reallocationData.js";
|
|
3
3
|
import { type ReallocationComputeOptions, type VaultReallocation } from "../types/index.js";
|
|
4
4
|
/**
|
|
5
|
-
* Computes vault reallocations for a borrow
|
|
5
|
+
* Computes vault reallocations for a `borrow` or `withdraw` on a target market.
|
|
6
6
|
*
|
|
7
7
|
* First attempts "friendly" reallocations respecting withdrawal utilization
|
|
8
8
|
* targets, then falls back to aggressive reallocations (100% withdrawal
|
|
9
9
|
* utilization) if liquidity is still insufficient.
|
|
10
10
|
*
|
|
11
|
+
* Algebra branches on `operation`:
|
|
12
|
+
* - `"borrow"`: `S' = S`, `B' = B + amount` (additional borrow demand).
|
|
13
|
+
* - `"withdraw"`: `S' = S − amount`, `B' = B` (supply-side shrinkage).
|
|
14
|
+
*
|
|
15
|
+
* In both cases reallocated assets are added on the supply side; `requiredAssets`
|
|
16
|
+
* and `absoluteShortfall` are derived from the operation-specific post-state.
|
|
17
|
+
*
|
|
11
18
|
* @remarks Pass `options.timestamp` from the same block used to fetch `reallocationData`; when omitted, market accrual falls back to the target market's `lastUpdate`, which can diverge from the source rows' fetch block. Per-market `maxWithdrawalUtilization` overrides apply only to phase 1; phase 2 forces 100% utilization on every source market.
|
|
12
19
|
* @param params.reallocationData - The local state containing market, vault, and position data.
|
|
13
20
|
* @param params.marketId - The target market to reallocate liquidity into.
|
|
14
|
-
* @param params.
|
|
21
|
+
* @param params.operation - The operation driving the reallocation (`"borrow"` or `"withdraw"`).
|
|
22
|
+
* @param params.amount - The borrow or withdraw amount used to compute the post-state utilization.
|
|
15
23
|
* @param params.options - Optional reallocation computation options.
|
|
16
24
|
* @returns Array of vault reallocations, sorted with withdrawals in ascending market id order.
|
|
17
|
-
* @throws {InsufficientSharedLiquidityError} when shared liquidity cannot cover the
|
|
25
|
+
* @throws {InsufficientSharedLiquidityError} when shared liquidity cannot cover the operation's absolute shortfall on the target market — preventing fee-bearing reallocations from being attached to a call that would still revert onchain.
|
|
26
|
+
* @throws {ReallocationWithdrawExceedsMarketSupplyError} when `operation === "withdraw"` and `amount` exceeds the target market's `totalSupplyAssets` — the on-chain call would revert regardless of reallocations.
|
|
18
27
|
* @throws {MissingPublicAllocatorConfigError} when a selected vault is missing its public allocator config.
|
|
19
28
|
* @example
|
|
20
29
|
* ```ts
|
|
@@ -43,7 +52,8 @@ import { type ReallocationComputeOptions, type VaultReallocation } from "../type
|
|
|
43
52
|
* const reallocations = computeReallocations({
|
|
44
53
|
* reallocationData,
|
|
45
54
|
* marketId: marketParams.id,
|
|
46
|
-
*
|
|
55
|
+
* operation: "borrow",
|
|
56
|
+
* amount: borrowAmount,
|
|
47
57
|
* options: { timestamp: block.timestamp },
|
|
48
58
|
* });
|
|
49
59
|
* const positionData = await market.getPositionData(userAddress);
|
|
@@ -56,9 +66,10 @@ import { type ReallocationComputeOptions, type VaultReallocation } from "../type
|
|
|
56
66
|
* // borrow.buildTx() includes any required PublicAllocator reallocations.
|
|
57
67
|
* ```
|
|
58
68
|
*/
|
|
59
|
-
export declare const computeReallocations: ({ reallocationData: data, marketId,
|
|
69
|
+
export declare const computeReallocations: ({ reallocationData: data, marketId, operation, amount, options, }: {
|
|
60
70
|
readonly reallocationData: ReallocationData;
|
|
61
71
|
readonly marketId: MarketId;
|
|
62
|
-
readonly
|
|
72
|
+
readonly operation: "borrow" | "withdraw";
|
|
73
|
+
readonly amount: bigint;
|
|
63
74
|
readonly options?: ReallocationComputeOptions;
|
|
64
75
|
}) => readonly VaultReallocation[];
|