@morpho-org/morpho-sdk 5.9.0 → 6.0.0-next.1
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/MIGRATION-v5-to-v6.md +404 -0
- package/README.md +127 -69
- package/lib/cjs/abis.d.ts +210 -0
- package/lib/cjs/abis.js +142 -2
- package/lib/cjs/actions/blue/borrow.d.ts +62 -61
- package/lib/cjs/actions/blue/borrow.js +48 -80
- package/lib/cjs/actions/blue/common.d.ts +86 -0
- package/lib/cjs/actions/blue/common.js +205 -0
- package/lib/cjs/actions/blue/refinance.d.ts +72 -112
- package/lib/cjs/actions/blue/refinance.js +97 -199
- package/lib/cjs/actions/blue/repay.d.ts +72 -68
- package/lib/cjs/actions/blue/repay.js +56 -118
- package/lib/cjs/actions/blue/repayWithdrawCollateral.d.ts +85 -90
- package/lib/cjs/actions/blue/repayWithdrawCollateral.js +148 -137
- package/lib/cjs/actions/blue/supply.d.ts +62 -45
- package/lib/cjs/actions/blue/supply.js +94 -70
- package/lib/cjs/actions/blue/supplyCollateral.d.ts +63 -40
- package/lib/cjs/actions/blue/supplyCollateral.js +52 -69
- package/lib/cjs/actions/blue/supplyCollateralBorrow.d.ts +82 -90
- package/lib/cjs/actions/blue/supplyCollateralBorrow.js +133 -120
- package/lib/cjs/actions/blue/withdraw.d.ts +68 -79
- package/lib/cjs/actions/blue/withdraw.js +101 -97
- package/lib/cjs/actions/blue/withdrawCollateral.d.ts +56 -28
- package/lib/cjs/actions/blue/withdrawCollateral.js +45 -50
- package/lib/cjs/actions/bundles/common.d.ts +200 -0
- package/lib/cjs/actions/bundles/common.js +469 -0
- package/lib/cjs/actions/bundles/index.d.ts +3 -0
- package/lib/cjs/actions/bundles/index.js +23 -0
- package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.d.ts +76 -0
- package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.js +125 -0
- package/lib/cjs/actions/index.d.ts +2 -1
- package/lib/cjs/actions/index.js +1 -0
- package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.d.ts +2 -2
- package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +2 -2
- package/lib/cjs/actions/midnight/takeBorrow.d.ts +2 -2
- package/lib/cjs/actions/midnight/takeBorrow.js +2 -2
- package/lib/cjs/actions/midnight/takeLend.d.ts +2 -2
- package/lib/cjs/actions/midnight/takeLend.js +2 -2
- package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +29 -17
- package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +60 -19
- package/lib/cjs/actions/requirements/blue/getUnusedPermit2Nonce.d.ts +48 -0
- package/lib/cjs/actions/requirements/blue/getUnusedPermit2Nonce.js +72 -0
- package/lib/cjs/actions/requirements/blue/index.d.ts +1 -0
- package/lib/cjs/actions/requirements/blue/index.js +1 -0
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +10 -7
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +30 -5
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +2 -1
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +8 -2
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +16 -7
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +39 -6
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +61 -0
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +123 -0
- package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +4 -4
- package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +6 -5
- package/lib/cjs/actions/requirements/encode/index.d.ts +1 -0
- package/lib/cjs/actions/requirements/encode/index.js +1 -0
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +1 -1
- package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +6 -4
- package/lib/cjs/actions/requirements/getRequirementsApproval.js +6 -4
- package/lib/cjs/actions/signatures/getTokenRequirementActions.d.ts +1 -0
- package/lib/cjs/actions/signatures/getTokenRequirementActions.js +4 -0
- package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +22 -30
- package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.js +18 -66
- package/lib/cjs/actions/vaultV1/deposit.d.ts +73 -57
- package/lib/cjs/actions/vaultV1/deposit.js +110 -109
- package/lib/cjs/actions/vaultV1/migrateToV2.d.ts +43 -66
- package/lib/cjs/actions/vaultV1/migrateToV2.js +91 -104
- package/lib/cjs/actions/vaultV1/redeem.d.ts +30 -23
- package/lib/cjs/actions/vaultV1/redeem.js +56 -33
- package/lib/cjs/actions/vaultV1/withdraw.d.ts +58 -24
- package/lib/cjs/actions/vaultV1/withdraw.js +86 -34
- package/lib/cjs/actions/vaultV2/deposit.d.ts +74 -57
- package/lib/cjs/actions/vaultV2/deposit.js +111 -109
- package/lib/cjs/actions/vaultV2/redeem.d.ts +30 -24
- package/lib/cjs/actions/vaultV2/redeem.js +56 -34
- package/lib/cjs/actions/vaultV2/withdraw.d.ts +58 -25
- package/lib/cjs/actions/vaultV2/withdraw.js +86 -35
- package/lib/cjs/bundler/actions.d.ts +4 -4
- package/lib/cjs/bundler/actions.js +4 -4
- package/lib/cjs/client/morphoViemExtension.d.ts +2 -1
- package/lib/cjs/client/morphoViemExtension.js +2 -1
- package/lib/cjs/entities/blue/blue.d.ts +806 -374
- package/lib/cjs/entities/blue/blue.js +449 -696
- package/lib/cjs/entities/index.d.ts +2 -0
- package/lib/cjs/entities/index.js +15 -0
- package/lib/cjs/entities/midnight/midnight.d.ts +2 -2
- package/lib/cjs/entities/requirements/getBundlesTokenRequirements.d.ts +87 -0
- package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +203 -0
- package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +28 -0
- package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.js +74 -0
- package/lib/cjs/entities/requirements/index.d.ts +1 -0
- package/lib/cjs/entities/requirements/index.js +17 -0
- package/lib/cjs/entities/vaultV1/vaultV1.d.ts +348 -98
- package/lib/cjs/entities/vaultV1/vaultV1.js +322 -120
- package/lib/cjs/entities/vaultV2/vaultV2.d.ts +221 -68
- package/lib/cjs/entities/vaultV2/vaultV2.js +219 -77
- package/lib/cjs/helpers/computeVaultV1Reallocations.d.ts +3 -9
- package/lib/cjs/helpers/computeVaultV1Reallocations.js +3 -9
- package/lib/cjs/helpers/index.d.ts +2 -1
- package/lib/cjs/helpers/index.js +5 -1
- package/lib/cjs/helpers/referralFee.d.ts +21 -0
- package/lib/cjs/helpers/referralFee.js +35 -0
- package/lib/cjs/helpers/slippage.d.ts +90 -1
- package/lib/cjs/helpers/slippage.js +123 -0
- package/lib/cjs/helpers/validate.d.ts +16 -23
- package/lib/cjs/helpers/validate.js +41 -48
- package/lib/cjs/helpers/validateRequirementSpender.d.ts +2 -1
- package/lib/cjs/helpers/validateRequirementSpender.js +3 -0
- package/lib/cjs/index.d.ts +1 -0
- package/lib/cjs/index.js +1 -0
- package/lib/cjs/types/action.d.ts +303 -210
- package/lib/cjs/types/action.js +29 -2
- package/lib/cjs/types/client.d.ts +9 -0
- package/lib/cjs/types/error.d.ts +193 -18
- package/lib/cjs/types/error.js +267 -24
- package/lib/cjs/types/sharedLiquidity.d.ts +0 -7
- package/lib/esm/abis.d.ts +210 -0
- package/lib/esm/abis.js +140 -0
- package/lib/esm/actions/blue/borrow.d.ts +62 -61
- package/lib/esm/actions/blue/borrow.js +48 -80
- package/lib/esm/actions/blue/common.d.ts +86 -0
- package/lib/esm/actions/blue/common.js +193 -0
- package/lib/esm/actions/blue/refinance.d.ts +72 -112
- package/lib/esm/actions/blue/refinance.js +98 -200
- package/lib/esm/actions/blue/repay.d.ts +72 -68
- package/lib/esm/actions/blue/repay.js +56 -118
- package/lib/esm/actions/blue/repayWithdrawCollateral.d.ts +85 -90
- package/lib/esm/actions/blue/repayWithdrawCollateral.js +149 -138
- package/lib/esm/actions/blue/supply.d.ts +62 -45
- package/lib/esm/actions/blue/supply.js +94 -70
- package/lib/esm/actions/blue/supplyCollateral.d.ts +63 -40
- package/lib/esm/actions/blue/supplyCollateral.js +52 -69
- package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +82 -90
- package/lib/esm/actions/blue/supplyCollateralBorrow.js +134 -121
- package/lib/esm/actions/blue/withdraw.d.ts +68 -79
- package/lib/esm/actions/blue/withdraw.js +100 -96
- package/lib/esm/actions/blue/withdrawCollateral.d.ts +56 -28
- package/lib/esm/actions/blue/withdrawCollateral.js +45 -50
- package/lib/esm/actions/bundles/common.d.ts +200 -0
- package/lib/esm/actions/bundles/common.js +457 -0
- package/lib/esm/actions/bundles/index.d.ts +3 -0
- package/lib/esm/actions/bundles/index.js +2 -0
- package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.d.ts +76 -0
- package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.js +121 -0
- package/lib/esm/actions/index.d.ts +2 -1
- package/lib/esm/actions/index.js +1 -0
- package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.d.ts +2 -2
- package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +2 -2
- package/lib/esm/actions/midnight/takeBorrow.d.ts +2 -2
- package/lib/esm/actions/midnight/takeBorrow.js +2 -2
- package/lib/esm/actions/midnight/takeLend.d.ts +2 -2
- package/lib/esm/actions/midnight/takeLend.js +2 -2
- package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +29 -17
- package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +63 -22
- package/lib/esm/actions/requirements/blue/getUnusedPermit2Nonce.d.ts +48 -0
- package/lib/esm/actions/requirements/blue/getUnusedPermit2Nonce.js +68 -0
- package/lib/esm/actions/requirements/blue/index.d.ts +1 -0
- package/lib/esm/actions/requirements/blue/index.js +1 -0
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +10 -7
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +31 -6
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +2 -1
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +9 -3
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +16 -7
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +40 -7
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +61 -0
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +119 -0
- package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +4 -4
- package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +6 -5
- package/lib/esm/actions/requirements/encode/index.d.ts +1 -0
- package/lib/esm/actions/requirements/encode/index.js +1 -0
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +1 -1
- package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +6 -4
- package/lib/esm/actions/requirements/getRequirementsApproval.js +6 -4
- package/lib/esm/actions/signatures/getTokenRequirementActions.d.ts +1 -0
- package/lib/esm/actions/signatures/getTokenRequirementActions.js +5 -1
- package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +22 -30
- package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.js +19 -67
- package/lib/esm/actions/vaultV1/deposit.d.ts +73 -57
- package/lib/esm/actions/vaultV1/deposit.js +112 -111
- package/lib/esm/actions/vaultV1/migrateToV2.d.ts +43 -66
- package/lib/esm/actions/vaultV1/migrateToV2.js +93 -106
- package/lib/esm/actions/vaultV1/redeem.d.ts +30 -23
- package/lib/esm/actions/vaultV1/redeem.js +56 -33
- package/lib/esm/actions/vaultV1/withdraw.d.ts +58 -24
- package/lib/esm/actions/vaultV1/withdraw.js +86 -34
- package/lib/esm/actions/vaultV2/deposit.d.ts +74 -57
- package/lib/esm/actions/vaultV2/deposit.js +113 -111
- package/lib/esm/actions/vaultV2/redeem.d.ts +30 -24
- package/lib/esm/actions/vaultV2/redeem.js +56 -34
- package/lib/esm/actions/vaultV2/withdraw.d.ts +58 -25
- package/lib/esm/actions/vaultV2/withdraw.js +86 -35
- package/lib/esm/bundler/actions.d.ts +4 -4
- package/lib/esm/bundler/actions.js +4 -4
- package/lib/esm/client/morphoViemExtension.d.ts +2 -1
- package/lib/esm/client/morphoViemExtension.js +2 -1
- package/lib/esm/entities/blue/blue.d.ts +806 -374
- package/lib/esm/entities/blue/blue.js +455 -702
- package/lib/esm/entities/index.d.ts +2 -0
- package/lib/esm/entities/index.js +1 -0
- package/lib/esm/entities/midnight/midnight.d.ts +2 -2
- package/lib/esm/entities/requirements/getBundlesTokenRequirements.d.ts +87 -0
- package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +199 -0
- package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +28 -0
- package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.js +70 -0
- package/lib/esm/entities/requirements/index.d.ts +1 -0
- package/lib/esm/entities/requirements/index.js +1 -0
- package/lib/esm/entities/vaultV1/vaultV1.d.ts +348 -98
- package/lib/esm/entities/vaultV1/vaultV1.js +326 -124
- package/lib/esm/entities/vaultV2/vaultV2.d.ts +221 -68
- package/lib/esm/entities/vaultV2/vaultV2.js +222 -80
- package/lib/esm/helpers/computeVaultV1Reallocations.d.ts +3 -9
- package/lib/esm/helpers/computeVaultV1Reallocations.js +3 -9
- package/lib/esm/helpers/index.d.ts +2 -1
- package/lib/esm/helpers/index.js +2 -1
- package/lib/esm/helpers/referralFee.d.ts +21 -0
- package/lib/esm/helpers/referralFee.js +31 -0
- package/lib/esm/helpers/slippage.d.ts +90 -1
- package/lib/esm/helpers/slippage.js +122 -2
- package/lib/esm/helpers/validate.d.ts +16 -23
- package/lib/esm/helpers/validate.js +40 -48
- package/lib/esm/helpers/validateRequirementSpender.d.ts +2 -1
- package/lib/esm/helpers/validateRequirementSpender.js +3 -0
- package/lib/esm/index.d.ts +1 -0
- package/lib/esm/index.js +1 -0
- package/lib/esm/types/action.d.ts +303 -210
- package/lib/esm/types/action.js +28 -2
- package/lib/esm/types/client.d.ts +9 -0
- package/lib/esm/types/error.d.ts +193 -18
- package/lib/esm/types/error.js +252 -21
- package/lib/esm/types/sharedLiquidity.d.ts +0 -7
- package/package.json +6 -5
- package/lib/cjs/actions/blue/buildAssetFundingActions.d.ts +0 -47
- package/lib/cjs/actions/blue/buildAssetFundingActions.js +0 -63
- package/lib/cjs/actions/blue/buildReallocationActions.d.ts +0 -69
- package/lib/cjs/actions/blue/buildReallocationActions.js +0 -141
- package/lib/cjs/actions/blue/resolveRepayFunding.d.ts +0 -66
- package/lib/cjs/actions/blue/resolveRepayFunding.js +0 -85
- package/lib/esm/actions/blue/buildAssetFundingActions.d.ts +0 -47
- package/lib/esm/actions/blue/buildAssetFundingActions.js +0 -59
- package/lib/esm/actions/blue/buildReallocationActions.d.ts +0 -69
- package/lib/esm/actions/blue/buildReallocationActions.js +0 -135
- package/lib/esm/actions/blue/resolveRepayFunding.d.ts +0 -66
- package/lib/esm/actions/blue/resolveRepayFunding.js +0 -81
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { MathLib } from "@morpho-org/blue-sdk";
|
|
2
|
-
import { ExcessiveSlippageToleranceError, ShareDivideByZeroError, } from "../types/index.js";
|
|
1
|
+
import { AccrualVaultV2, MathLib, } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { ExcessiveSlippageToleranceError, NonPositiveInputError, ShareDivideByZeroError, } from "../types/index.js";
|
|
3
3
|
import { MAX_ABSOLUTE_SHARE_PRICE } from "./constant.js";
|
|
4
|
+
import { validateSlippageTolerance } from "./validate.js";
|
|
4
5
|
/**
|
|
5
6
|
* Computes the minimum borrow share price (in RAY, 1e27) for slippage protection.
|
|
6
7
|
*
|
|
@@ -147,3 +148,122 @@ export function computeMinWithdrawSharePrice(params) {
|
|
|
147
148
|
}
|
|
148
149
|
return MathLib.mulDivDown(assets, MathLib.wToRay(MathLib.WAD - slippageTolerance), shares);
|
|
149
150
|
}
|
|
151
|
+
/**
|
|
152
|
+
* Computes the RAY-scaled maximum share price for a VaultBundlesV1 deposit leg.
|
|
153
|
+
*
|
|
154
|
+
* Previews shares on both the supplied Vault V1 or Vault V2 snapshot and its deadline-accrued
|
|
155
|
+
* counterpart, dividing by the smaller of the two so the bound covers the highest price the
|
|
156
|
+
* bundle may execute at. Shares are previewed rounding down, mirroring the ERC-4626 `deposit()`
|
|
157
|
+
* shares the on-chain `maxSharePrice` check divides by.
|
|
158
|
+
*
|
|
159
|
+
* @param params - Vault snapshot, bundles execution deadline, net assets, and slippage.
|
|
160
|
+
* @param params.vaultData - Hydrated Vault V1 `AccrualVault` or Vault V2 `AccrualVaultV2` snapshot.
|
|
161
|
+
* @param params.deadline - Bundle execution deadline as a Unix timestamp in seconds, used for accrual.
|
|
162
|
+
* @param params.assets - Net assets deposited after referral fees, in the underlying token's smallest unit.
|
|
163
|
+
* @param params.slippageTolerance - Accepted share-price increase as a WAD-scaled fraction (`1e18` = 100%).
|
|
164
|
+
* @returns The capped maximum share price enforced by VaultBundlesV1.
|
|
165
|
+
* @throws {NonPositiveInputError} when `assets` or the previewed shares are not positive.
|
|
166
|
+
* @throws {NegativeInputError} when `slippageTolerance` is negative.
|
|
167
|
+
* @throws {ExcessiveSlippageToleranceError} when `slippageTolerance` exceeds the SDK maximum.
|
|
168
|
+
* @example
|
|
169
|
+
* ```ts
|
|
170
|
+
* import { fetchAccrualVault } from "@morpho-org/blue-sdk-viem";
|
|
171
|
+
* import { computeVaultMaxSharePrice } from "@morpho-org/morpho-sdk";
|
|
172
|
+
* import { createPublicClient, http } from "viem";
|
|
173
|
+
* import { mainnet } from "viem/chains";
|
|
174
|
+
*
|
|
175
|
+
* const client = createPublicClient({ chain: mainnet, transport: http() });
|
|
176
|
+
* const vaultData = await fetchAccrualVault(
|
|
177
|
+
* "0xBEEF01735c132Ada46AA9aA4c54623cAA92A64CB",
|
|
178
|
+
* client,
|
|
179
|
+
* );
|
|
180
|
+
* const { timestamp } = await client.getBlock();
|
|
181
|
+
* const maxSharePrice = computeVaultMaxSharePrice({
|
|
182
|
+
* vaultData,
|
|
183
|
+
* deadline: timestamp + 7_200n,
|
|
184
|
+
* assets: 1_000_000n,
|
|
185
|
+
* slippageTolerance: 500_000_000_000_000n,
|
|
186
|
+
* });
|
|
187
|
+
* // maxSharePrice satisfies bigint
|
|
188
|
+
* ```
|
|
189
|
+
*/
|
|
190
|
+
export const computeVaultMaxSharePrice = (params) => {
|
|
191
|
+
if (params.assets <= 0n) {
|
|
192
|
+
throw new NonPositiveInputError("assets", params.assets);
|
|
193
|
+
}
|
|
194
|
+
validateSlippageTolerance(params.slippageTolerance);
|
|
195
|
+
const accrualTimestamp = params.vaultData instanceof AccrualVaultV2
|
|
196
|
+
? MathLib.max(params.deadline, params.vaultData.lastUpdate)
|
|
197
|
+
: params.deadline;
|
|
198
|
+
const accruedVault = params.vaultData instanceof AccrualVaultV2
|
|
199
|
+
? params.vaultData.accrueInterest(accrualTimestamp).vault
|
|
200
|
+
: params.vaultData.accrueInterest(accrualTimestamp);
|
|
201
|
+
// The deadline snapshot is not necessarily the highest price over `[now, deadline]`: Vault V2
|
|
202
|
+
// charges its management fee regardless of yield, minting shares against an unchanged
|
|
203
|
+
// `_totalAssets`, so an idle vault's price *declines*. Bound at the maximum price across both
|
|
204
|
+
// endpoints — the minimum previewed shares — mirroring the `max(current, accrued)` shares the
|
|
205
|
+
// sibling `computeVaultMaxShareAllowance` authorizes.
|
|
206
|
+
const shares = MathLib.min(params.vaultData.toShares(params.assets, "Down"), accruedVault.toShares(params.assets, "Down"));
|
|
207
|
+
if (shares <= 0n) {
|
|
208
|
+
throw new NonPositiveInputError("shares", shares);
|
|
209
|
+
}
|
|
210
|
+
return MathLib.min(MathLib.mulDivUp(params.assets, MathLib.wToRay(MathLib.WAD + params.slippageTolerance), shares), MAX_ABSOLUTE_SHARE_PRICE);
|
|
211
|
+
};
|
|
212
|
+
/**
|
|
213
|
+
* Computes the exact vault-share authorization cap for an asset-denominated exit.
|
|
214
|
+
*
|
|
215
|
+
* The cap covers both the current and deadline-accrued preview. Vault V2 and MetaMorpho 1.0 divide
|
|
216
|
+
* by `1 - slippageTolerance`, rounding up, to cover a share-price decline before inclusion;
|
|
217
|
+
* MetaMorpho 1.1's `lostAssets` clamp keeps that preview upper-bounded without widening it.
|
|
218
|
+
*
|
|
219
|
+
* @param params - Vault snapshot, execution deadline, asset amount, and WAD-scaled slippage.
|
|
220
|
+
* @param params.vaultData - Hydrated Vault V1 `AccrualVault` or Vault V2 `AccrualVaultV2` snapshot.
|
|
221
|
+
* @param params.deadline - Bundle execution deadline as a Unix timestamp in seconds, used for accrual.
|
|
222
|
+
* @param params.assets - Assets withdrawn from the vault, in the underlying token's smallest unit.
|
|
223
|
+
* @param params.slippageTolerance - Accepted share-price decline as a WAD-scaled fraction (`1e18` = 100%).
|
|
224
|
+
* @returns The maximum shares the prepared operation may burn.
|
|
225
|
+
* @throws {NonPositiveInputError} when `assets` or the computed share cap is not positive.
|
|
226
|
+
* @throws {NegativeInputError} when `slippageTolerance` is negative.
|
|
227
|
+
* @throws {ExcessiveSlippageToleranceError} when `slippageTolerance` exceeds the SDK maximum.
|
|
228
|
+
* @example
|
|
229
|
+
* ```ts
|
|
230
|
+
* import { fetchAccrualVault } from "@morpho-org/blue-sdk-viem";
|
|
231
|
+
* import { computeVaultMaxShareAllowance } from "@morpho-org/morpho-sdk";
|
|
232
|
+
* import { createPublicClient, http } from "viem";
|
|
233
|
+
* import { mainnet } from "viem/chains";
|
|
234
|
+
*
|
|
235
|
+
* const client = createPublicClient({ chain: mainnet, transport: http() });
|
|
236
|
+
* const vaultData = await fetchAccrualVault(
|
|
237
|
+
* "0xBEEF01735c132Ada46AA9aA4c54623cAA92A64CB",
|
|
238
|
+
* client,
|
|
239
|
+
* );
|
|
240
|
+
* const { timestamp } = await client.getBlock();
|
|
241
|
+
* const shares = computeVaultMaxShareAllowance({
|
|
242
|
+
* vaultData,
|
|
243
|
+
* deadline: timestamp + 7_200n,
|
|
244
|
+
* assets: 1_000_000n,
|
|
245
|
+
* slippageTolerance: 500_000_000_000_000n,
|
|
246
|
+
* });
|
|
247
|
+
* // shares is the exact allowance or permit value for the prepared exit.
|
|
248
|
+
* ```
|
|
249
|
+
*/
|
|
250
|
+
export const computeVaultMaxShareAllowance = (params) => {
|
|
251
|
+
if (params.assets <= 0n) {
|
|
252
|
+
throw new NonPositiveInputError("assets", params.assets);
|
|
253
|
+
}
|
|
254
|
+
validateSlippageTolerance(params.slippageTolerance);
|
|
255
|
+
const isVaultV2 = params.vaultData instanceof AccrualVaultV2;
|
|
256
|
+
const accruedVault = isVaultV2
|
|
257
|
+
? params.vaultData.accrueInterest(MathLib.max(params.deadline, params.vaultData.lastUpdate)).vault
|
|
258
|
+
: params.vaultData.accrueInterest(params.deadline);
|
|
259
|
+
const previewedShares = MathLib.max(params.vaultData.toShares(params.assets, "Up"), accruedVault.toShares(params.assets, "Up"));
|
|
260
|
+
if (previewedShares <= 0n) {
|
|
261
|
+
throw new NonPositiveInputError("shares", previewedShares);
|
|
262
|
+
}
|
|
263
|
+
const needsLossBuffer = isVaultV2 ||
|
|
264
|
+
!("lostAssets" in params.vaultData) ||
|
|
265
|
+
params.vaultData.lostAssets == null;
|
|
266
|
+
return needsLossBuffer
|
|
267
|
+
? MathLib.wDivUp(previewedShares, MathLib.WAD - params.slippageTolerance)
|
|
268
|
+
: previewedShares;
|
|
269
|
+
};
|
|
@@ -1,9 +1,22 @@
|
|
|
1
1
|
import { type AccrualPosition, type MarketId } from "@morpho-org/blue-sdk";
|
|
2
2
|
import type { MarketInput as MidnightMarketInput } from "@morpho-org/midnight-sdk";
|
|
3
3
|
import { type Address } from "viem";
|
|
4
|
-
import { type
|
|
4
|
+
import { type VaultV1Reallocation, type VaultV2BlueReallocation } from "../types/index.js";
|
|
5
5
|
/** @internal */
|
|
6
6
|
export declare const compareMarketIds: (idA: MarketId, idB: MarketId) => -1 | 0 | 1;
|
|
7
|
+
/**
|
|
8
|
+
* Rejects a `bigint` field that falls outside the unsigned 256-bit ABI range so a direct caller
|
|
9
|
+
* gets the SDK's typed error instead of viem's `IntegerOutOfRangeError` at encode time. `maxUint256`
|
|
10
|
+
* stays valid — it is the accepted `maxLtv` / full-repay `repayShares` sentinel.
|
|
11
|
+
*
|
|
12
|
+
* @param field - Field name surfaced in the thrown error.
|
|
13
|
+
* @param value - Candidate value; must be in `[0, maxUint256]`.
|
|
14
|
+
* @returns Nothing when `value` is within range.
|
|
15
|
+
* @throws {NegativeInputError} when `value` is negative.
|
|
16
|
+
* @throws {InputExceedsMaxError} when `value` exceeds `maxUint256`.
|
|
17
|
+
* @internal
|
|
18
|
+
*/
|
|
19
|
+
export declare const validateUint256Field: (field: string, value: bigint) => void;
|
|
7
20
|
/**
|
|
8
21
|
* Validates that a raw or hydrated Midnight market belongs to the expected chain.
|
|
9
22
|
*
|
|
@@ -95,6 +108,8 @@ export declare const validateChainId: (clientChainId: number | undefined, expect
|
|
|
95
108
|
* @throws {NativeAmountOnNonWNativeAssetError} if the asset is not wNative.
|
|
96
109
|
*/
|
|
97
110
|
export declare const validateNativeAsset: (chainId: number, asset: Address) => void;
|
|
111
|
+
/** @internal Validates native funding against a vault's wrapped-native asset. */
|
|
112
|
+
export declare const validateNativeVaultAsset: (chainId: number, vaultAsset: Address) => void;
|
|
98
113
|
/**
|
|
99
114
|
* Validates that the resulting position stays within the safe LTV threshold
|
|
100
115
|
* (LLTV minus buffer) after withdrawing collateral.
|
|
@@ -162,28 +177,6 @@ export declare const validateRepayShares: (params: {
|
|
|
162
177
|
export declare const validateReallocations: (reallocations: Iterable<VaultV1Reallocation>, targetMarketId: MarketId) => void;
|
|
163
178
|
/** @internal */
|
|
164
179
|
export declare const validateVaultV2BlueReallocations: (reallocations: Iterable<VaultV2BlueReallocation>, targetMarketId: MarketId) => void;
|
|
165
|
-
/**
|
|
166
|
-
* Validates and normalizes a homogeneous Blue reallocation plan.
|
|
167
|
-
*
|
|
168
|
-
* @param params - Validation parameters.
|
|
169
|
-
* @param params.reallocations - Optional Vault V1 or Vault V2 reallocation plan.
|
|
170
|
-
* @param params.targetMarketId - Morpho Blue market receiving the liquidity.
|
|
171
|
-
* @param params.chainId - Chain whose allocator deployment is required for a V2 plan.
|
|
172
|
-
* @returns The validated plan tagged with its allocator version.
|
|
173
|
-
* @throws {BundlerErrors.UnexpectedAction} when a V2 plan is unsupported on the chain.
|
|
174
|
-
* @internal
|
|
175
|
-
*/
|
|
176
|
-
export declare const validateAndNormalizeReallocations: ({ reallocations, targetMarketId, chainId, }: {
|
|
177
|
-
readonly reallocations: BlueReallocationPlan | undefined;
|
|
178
|
-
readonly targetMarketId: MarketId;
|
|
179
|
-
readonly chainId: number;
|
|
180
|
-
}) => {
|
|
181
|
-
type: "vaultV2Blue";
|
|
182
|
-
reallocations: VaultV2BlueReallocation[];
|
|
183
|
-
} | {
|
|
184
|
-
type: "vaultV1";
|
|
185
|
-
reallocations: VaultV1Reallocation[];
|
|
186
|
-
};
|
|
187
180
|
/**
|
|
188
181
|
* Validates that a slippage tolerance is within an acceptable range.
|
|
189
182
|
*
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { getChainAddresses, MarketUtils, MathLib, ORACLE_PRICE_SCALE, } from "@morpho-org/blue-sdk";
|
|
2
2
|
import { isDefined } from "@morpho-org/morpho-ts";
|
|
3
|
-
import { isAddress, isAddressEqual, maxUint128 } from "viem";
|
|
4
|
-
import { AccrualPositionUserMismatchError, AddressMismatchError, BorrowExceedsSafeLtvError,
|
|
3
|
+
import { isAddress, isAddressEqual, maxUint128, maxUint256, } from "viem";
|
|
4
|
+
import { AccrualPositionUserMismatchError, AddressMismatchError, BorrowExceedsSafeLtvError, ChainIdMismatchError, ChainWNativeMissingError, EmptyReallocationWithdrawalsError, ExcessiveSlippageToleranceError, InconsistentReallocationPenaltyError, InputExceedsMaxError, InvalidReallocationAddressError, InvalidReallocationShapeError, InvalidReallocationSourceTypeError, MarketIdMismatchError, MissingClientPropertyError, MissingMarketPriceError, NativeAmountOnNonWNativeAssetError, NativeAmountOnNonWNativeVaultError, NegativeInputError, NonPositiveInputError, ReallocationWithdrawalOnTargetMarketError, RepayExceedsDebtError, RepaySharesExceedDebtError, UnsortedReallocationWithdrawalsError, WithdrawExceedsCollateralError, WithdrawExceedsSupplyError, WithdrawMakesPositionUnhealthyError, WithdrawSharesExceedSupplyError, } from "../types/index.js";
|
|
5
5
|
import { DEFAULT_LLTV_BUFFER, MAX_REALLOCATION_PENALTY, MAX_SLIPPAGE_TOLERANCE, } from "./constant.js";
|
|
6
6
|
/** @internal */
|
|
7
7
|
export const compareMarketIds = (idA, idB) => {
|
|
@@ -13,6 +13,25 @@ export const compareMarketIds = (idA, idB) => {
|
|
|
13
13
|
return -1;
|
|
14
14
|
return 0;
|
|
15
15
|
};
|
|
16
|
+
/**
|
|
17
|
+
* Rejects a `bigint` field that falls outside the unsigned 256-bit ABI range so a direct caller
|
|
18
|
+
* gets the SDK's typed error instead of viem's `IntegerOutOfRangeError` at encode time. `maxUint256`
|
|
19
|
+
* stays valid — it is the accepted `maxLtv` / full-repay `repayShares` sentinel.
|
|
20
|
+
*
|
|
21
|
+
* @param field - Field name surfaced in the thrown error.
|
|
22
|
+
* @param value - Candidate value; must be in `[0, maxUint256]`.
|
|
23
|
+
* @returns Nothing when `value` is within range.
|
|
24
|
+
* @throws {NegativeInputError} when `value` is negative.
|
|
25
|
+
* @throws {InputExceedsMaxError} when `value` exceeds `maxUint256`.
|
|
26
|
+
* @internal
|
|
27
|
+
*/
|
|
28
|
+
export const validateUint256Field = (field, value) => {
|
|
29
|
+
if (value < 0n)
|
|
30
|
+
throw new NegativeInputError(field, value);
|
|
31
|
+
if (value > maxUint256) {
|
|
32
|
+
throw new InputExceedsMaxError({ field, value, max: maxUint256 });
|
|
33
|
+
}
|
|
34
|
+
};
|
|
16
35
|
/**
|
|
17
36
|
* Validates that a raw or hydrated Midnight market belongs to the expected chain.
|
|
18
37
|
*
|
|
@@ -139,6 +158,16 @@ export const validateNativeAsset = (chainId, asset) => {
|
|
|
139
158
|
throw new NativeAmountOnNonWNativeAssetError(asset, wNative);
|
|
140
159
|
}
|
|
141
160
|
};
|
|
161
|
+
/** @internal Validates native funding against a vault's wrapped-native asset. */
|
|
162
|
+
export const validateNativeVaultAsset = (chainId, vaultAsset) => {
|
|
163
|
+
const { wNative } = getChainAddresses(chainId);
|
|
164
|
+
if (!isDefined(wNative)) {
|
|
165
|
+
throw new ChainWNativeMissingError(chainId);
|
|
166
|
+
}
|
|
167
|
+
if (!isAddressEqual(vaultAsset, wNative)) {
|
|
168
|
+
throw new NativeAmountOnNonWNativeVaultError(vaultAsset, wNative);
|
|
169
|
+
}
|
|
170
|
+
};
|
|
142
171
|
/**
|
|
143
172
|
* Validates that the resulting position stays within the safe LTV threshold
|
|
144
173
|
* (LLTV minus buffer) after withdrawing collateral.
|
|
@@ -267,6 +296,15 @@ export const validateReallocations = (reallocations, targetMarketId) => {
|
|
|
267
296
|
export const validateVaultV2BlueReallocations = (reallocations, targetMarketId) => {
|
|
268
297
|
const penaltyByVault = new Map();
|
|
269
298
|
for (const reallocation of reallocations) {
|
|
299
|
+
// Reject non-object entries and Vault V1-shaped entries (which carry
|
|
300
|
+
// `withdrawals`/`fee`) with a typed, instructive error instead of a raw
|
|
301
|
+
// TypeError or a misleading `to.adapter` address error.
|
|
302
|
+
if (typeof reallocation !== "object" ||
|
|
303
|
+
reallocation === null ||
|
|
304
|
+
"withdrawals" in reallocation ||
|
|
305
|
+
"fee" in reallocation) {
|
|
306
|
+
throw new InvalidReallocationShapeError();
|
|
307
|
+
}
|
|
270
308
|
if (typeof reallocation.vault !== "string" ||
|
|
271
309
|
!isAddress(reallocation.vault)) {
|
|
272
310
|
throw new InvalidReallocationAddressError("vault");
|
|
@@ -336,52 +374,6 @@ export const validateVaultV2BlueReallocations = (reallocations, targetMarketId)
|
|
|
336
374
|
}
|
|
337
375
|
}
|
|
338
376
|
};
|
|
339
|
-
/**
|
|
340
|
-
* Validates and normalizes a homogeneous Blue reallocation plan.
|
|
341
|
-
*
|
|
342
|
-
* @param params - Validation parameters.
|
|
343
|
-
* @param params.reallocations - Optional Vault V1 or Vault V2 reallocation plan.
|
|
344
|
-
* @param params.targetMarketId - Morpho Blue market receiving the liquidity.
|
|
345
|
-
* @param params.chainId - Chain whose allocator deployment is required for a V2 plan.
|
|
346
|
-
* @returns The validated plan tagged with its allocator version.
|
|
347
|
-
* @throws {BundlerErrors.UnexpectedAction} when a V2 plan is unsupported on the chain.
|
|
348
|
-
* @internal
|
|
349
|
-
*/
|
|
350
|
-
export const validateAndNormalizeReallocations = ({ reallocations, targetMarketId, chainId, }) => {
|
|
351
|
-
const vaultV1Reallocations = [];
|
|
352
|
-
const vaultV2Reallocations = [];
|
|
353
|
-
for (const reallocation of reallocations ?? []) {
|
|
354
|
-
if (typeof reallocation !== "object" || reallocation === null) {
|
|
355
|
-
throw new InvalidReallocationShapeError();
|
|
356
|
-
}
|
|
357
|
-
if ("from" in reallocation === "withdrawals" in reallocation) {
|
|
358
|
-
throw new InvalidReallocationShapeError();
|
|
359
|
-
}
|
|
360
|
-
if ("withdrawals" in reallocation) {
|
|
361
|
-
vaultV1Reallocations.push(reallocation);
|
|
362
|
-
}
|
|
363
|
-
else {
|
|
364
|
-
vaultV2Reallocations.push(reallocation);
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
if (vaultV1Reallocations.length > 0 && vaultV2Reallocations.length > 0) {
|
|
368
|
-
throw new MixedReallocationVersionsError();
|
|
369
|
-
}
|
|
370
|
-
if (vaultV2Reallocations.length > 0) {
|
|
371
|
-
validateVaultV2BlueReallocations(vaultV2Reallocations, targetMarketId);
|
|
372
|
-
if (getChainAddresses(chainId).vaultV2BluePublicAllocator == null) {
|
|
373
|
-
throw new BundlerErrors.UnexpectedAction(vaultV2Reallocations[0]?.from.type === "market"
|
|
374
|
-
? "vaultV2BluePublicAllocatorReallocate"
|
|
375
|
-
: "vaultV2BluePublicAllocatorAllocateFromIdle", chainId);
|
|
376
|
-
}
|
|
377
|
-
return {
|
|
378
|
-
type: "vaultV2Blue",
|
|
379
|
-
reallocations: vaultV2Reallocations,
|
|
380
|
-
};
|
|
381
|
-
}
|
|
382
|
-
validateReallocations(vaultV1Reallocations, targetMarketId);
|
|
383
|
-
return { type: "vaultV1", reallocations: vaultV1Reallocations };
|
|
384
|
-
};
|
|
385
377
|
/**
|
|
386
378
|
* Validates that a slippage tolerance is within an acceptable range.
|
|
387
379
|
*
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type Address } from "@morpho-org/blue-sdk";
|
|
2
2
|
/** Supported spender slots that can be validated against the chain address registry. */
|
|
3
|
-
export type RequirementSpenderKey = "generalAdapter1" | "permit2" | "midnight" | "midnightBundles" | "vaultExitBundlesV1";
|
|
3
|
+
export type RequirementSpenderKey = "generalAdapter1" | "permit2" | "midnight" | "midnightBundles" | "vaultExitBundlesV1" | "vaultBundlesV1" | "blueBundlesV1";
|
|
4
4
|
/**
|
|
5
5
|
* Validates that a requirement encoder spender matches one of the allowed chain addresses.
|
|
6
6
|
*
|
|
@@ -9,6 +9,7 @@ export type RequirementSpenderKey = "generalAdapter1" | "permit2" | "midnight" |
|
|
|
9
9
|
* @param params.spender - Spender address to validate.
|
|
10
10
|
* @param params.allowed - Allowed registry slots for this requirement.
|
|
11
11
|
* @returns Nothing after the spender matches an allowed chain address.
|
|
12
|
+
* @throws {UnsupportedChainIdError} when `chainId` is absent from the address registry.
|
|
12
13
|
* @throws {UnsupportedErc20ApprovalSpenderError} when `spender` does not match any allowed slot.
|
|
13
14
|
* @example
|
|
14
15
|
* ```ts
|
|
@@ -9,6 +9,7 @@ import { UnsupportedErc20ApprovalSpenderError } from "../types/index.js";
|
|
|
9
9
|
* @param params.spender - Spender address to validate.
|
|
10
10
|
* @param params.allowed - Allowed registry slots for this requirement.
|
|
11
11
|
* @returns Nothing after the spender matches an allowed chain address.
|
|
12
|
+
* @throws {UnsupportedChainIdError} when `chainId` is absent from the address registry.
|
|
12
13
|
* @throws {UnsupportedErc20ApprovalSpenderError} when `spender` does not match any allowed slot.
|
|
13
14
|
* @example
|
|
14
15
|
* ```ts
|
|
@@ -30,6 +31,8 @@ export const validateRequirementSpender = (params) => {
|
|
|
30
31
|
midnight,
|
|
31
32
|
midnightBundles,
|
|
32
33
|
vaultExitBundlesV1: bundles?.vaultExitBundlesV1,
|
|
34
|
+
vaultBundlesV1: bundles?.vaultBundlesV1,
|
|
35
|
+
blueBundlesV1: bundles?.blueBundlesV1,
|
|
33
36
|
};
|
|
34
37
|
const supportedSpenders = params.allowed.map((key) => addresses[key]);
|
|
35
38
|
if (!supportedSpenders.some((supported) => supported != null && isAddressEqual(params.spender, supported))) {
|
package/lib/esm/index.d.ts
CHANGED