@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,11 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { MathLib, } from "@morpho-org/blue-sdk";
|
|
2
2
|
import { fetchAccrualPosition, fetchAccrualVaultV2, fetchMarket, fetchPosition, fetchVault, fetchVaultMarketConfig, fetchVaultV2BluePublicAllocatorData, } from "@morpho-org/blue-sdk-viem";
|
|
3
|
-
import { Time } from "@morpho-org/morpho-ts";
|
|
4
|
-
import { isAddressEqual } from "viem";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
3
|
+
import { getChainAddress, Time } from "@morpho-org/morpho-ts";
|
|
4
|
+
import { getAddress, isAddressEqual, maxUint256 } from "viem";
|
|
5
|
+
import { getBlueBundlesV1PenaltyAssets, getBlueBundlesV1PublicAllocations, getBlueBundlesV1ReferralFeeAssets, normalizeBlueBundlesV1CommonParams, selectBlueBundlesV1RequirementSignatures, validateBlueBundlesV1NativeFunding, } from "../../actions/blue/common.js";
|
|
6
|
+
import { blueBorrow, blueRefinance, blueRepay, blueRepayWithdrawCollateral, blueSupply, blueSupplyCollateral, blueSupplyCollateralBorrow, blueWithdraw, blueWithdrawCollateral, getBlueAuthorizationRequirement, } from "../../actions/index.js";
|
|
7
|
+
import { computeVaultV1Reallocations, DEFAULT_LLTV_BUFFER, MAX_TOKEN_APPROVALS, validateAccrualPosition, validateChainId, validatePositionHealth, validatePositionHealthAfterWithdraw, validateRepayAmount, validateRepayShares, validateWithdrawAmount, validateWithdrawShares, } from "../../helpers/index.js";
|
|
8
|
+
import { ExpiredDeadlineError, InputExceedsMaxError, MaxRepayAssetsBelowRepayAssetsError, MissingAccrualPositionError, MutuallyExclusiveRepayAmountsError, MutuallyExclusiveWithdrawAmountsError, NegativeInputError, NonPositiveInputError, ReallocationsRequireBorrowError, RefinanceSameMarketError, RefinanceTokenMismatchError, } from "../../types/index.js";
|
|
9
|
+
import { getBundlesTokenRequirements } from "../requirements/index.js";
|
|
9
10
|
import { VaultV1ReallocationData } from "../vaultV1ReallocationData.js";
|
|
10
11
|
import { VaultV2BlueReallocationData } from "../vaultV2BlueReallocationData.js";
|
|
11
12
|
export class MorphoBlue {
|
|
@@ -18,22 +19,61 @@ export class MorphoBlue {
|
|
|
18
19
|
this.marketParams = marketParams;
|
|
19
20
|
this.chainId = chainId;
|
|
20
21
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
22
|
+
validateDeadline(deadline) {
|
|
23
|
+
const timestamp = Time.timestamp();
|
|
24
|
+
if (deadline <= timestamp) {
|
|
25
|
+
throw new ExpiredDeadlineError(deadline, timestamp);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
validateWriteCommon(params) {
|
|
29
|
+
validateChainId(this.client.viemClient.chain?.id, this.chainId);
|
|
30
|
+
this.validateDeadline(params.deadline);
|
|
31
|
+
// Resolve at handle creation so unsupported deployments fail before any RPC prerequisite work.
|
|
32
|
+
getChainAddress(this.chainId, "bundles.blueBundlesV1");
|
|
33
|
+
// Validate the referral inputs eagerly; the normalized result is intentionally discarded here
|
|
34
|
+
// because the pure action re-normalizes it at encode time.
|
|
35
|
+
normalizeBlueBundlesV1CommonParams({
|
|
32
36
|
chainId: this.chainId,
|
|
33
|
-
|
|
34
|
-
|
|
37
|
+
userAddress: params.userAddress,
|
|
38
|
+
deadline: params.deadline,
|
|
39
|
+
referralFeePct: params.referralFeePct,
|
|
40
|
+
referralFeeRecipient: params.referralFeeRecipient,
|
|
35
41
|
});
|
|
36
42
|
}
|
|
43
|
+
getMaxLtv(marketParams = this.marketParams) {
|
|
44
|
+
return MathLib.zeroFloorSub(marketParams.lltv, DEFAULT_LLTV_BUFFER);
|
|
45
|
+
}
|
|
46
|
+
getBlueBundlesV1QuoteTimestamp(lastUpdate) {
|
|
47
|
+
return MathLib.max(Time.timestamp(), lastUpdate) + Time.s.from.h(2n);
|
|
48
|
+
}
|
|
49
|
+
async getTokenRequirements(params) {
|
|
50
|
+
this.validateDeadline(params.deadline);
|
|
51
|
+
return getBundlesTokenRequirements(this.client.viemClient, {
|
|
52
|
+
token: params.token,
|
|
53
|
+
spender: getChainAddress(this.chainId, "bundles.blueBundlesV1"),
|
|
54
|
+
amount: params.amount,
|
|
55
|
+
owner: params.userAddress,
|
|
56
|
+
chainId: this.chainId,
|
|
57
|
+
deadline: params.deadline,
|
|
58
|
+
supportSignature: this.client.options.supportSignature,
|
|
59
|
+
supportDeployless: this.client.options.supportDeployless,
|
|
60
|
+
useSimplePermit: params.useSimplePermit,
|
|
61
|
+
permit2Nonce: params.permit2Nonce,
|
|
62
|
+
approvalAmount: params.approvalAmount,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
async getAuthorizationRequirements(params) {
|
|
66
|
+
this.validateDeadline(params.deadline);
|
|
67
|
+
const requirement = await getBlueAuthorizationRequirement({
|
|
68
|
+
viemClient: this.client.viemClient,
|
|
69
|
+
chainId: this.chainId,
|
|
70
|
+
userAddress: params.userAddress,
|
|
71
|
+
supportSignature: this.client.options.supportSignature,
|
|
72
|
+
authorized: getChainAddress(this.chainId, "bundles.blueBundlesV1"),
|
|
73
|
+
deadline: params.deadline,
|
|
74
|
+
});
|
|
75
|
+
return requirement == null ? [] : [requirement];
|
|
76
|
+
}
|
|
37
77
|
async getMarketData(parameters) {
|
|
38
78
|
validateChainId(this.client.viemClient.chain?.id, this.chainId);
|
|
39
79
|
return fetchMarket(this.marketParams.id, this.client.viemClient, {
|
|
@@ -50,91 +90,70 @@ export class MorphoBlue {
|
|
|
50
90
|
chainId: this.chainId,
|
|
51
91
|
});
|
|
52
92
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}
|
|
65
|
-
if (marketData.id !== this.marketParams.id) {
|
|
66
|
-
throw new MarketIdMismatchError(marketData.id, this.marketParams.id);
|
|
67
|
-
}
|
|
68
|
-
validateSlippageTolerance(slippageTolerance);
|
|
69
|
-
if (nativeAmount !== undefined && nativeAmount > 0n) {
|
|
70
|
-
validateNativeAsset(this.chainId, this.marketParams.loanToken);
|
|
71
|
-
}
|
|
72
|
-
// Forward-accrue (2h) before deriving `maxSharePrice`: on-chain `morphoSupply` accrues `lastUpdate → execution`, raising the supply share price, so an un-accrued bound reverts on quiet markets.
|
|
73
|
-
const accrualTimestamp = MathLib.max(Time.timestamp(), marketData.lastUpdate) + Time.s.from.h(2n);
|
|
74
|
-
const marketForSupply = marketData.accrueInterest(accrualTimestamp);
|
|
75
|
-
const maxSharePrice = computeMaxSupplySharePrice({
|
|
76
|
-
supplyAssets: totalAssets,
|
|
77
|
-
market: marketForSupply,
|
|
78
|
-
slippageTolerance,
|
|
93
|
+
/** {@inheritDoc BlueActions.supply} */
|
|
94
|
+
supply(params) {
|
|
95
|
+
const { userAddress, assets, nativeAmount, deadline, referralFeePct, referralFeeRecipient, } = params;
|
|
96
|
+
this.validateWriteCommon(params);
|
|
97
|
+
if (assets <= 0n)
|
|
98
|
+
throw new NonPositiveInputError("assets", assets);
|
|
99
|
+
const nativeValue = validateBlueBundlesV1NativeFunding({
|
|
100
|
+
chainId: this.chainId,
|
|
101
|
+
token: this.marketParams.loanToken,
|
|
102
|
+
fundedAmount: assets,
|
|
103
|
+
nativeAmount,
|
|
79
104
|
});
|
|
80
105
|
return {
|
|
81
|
-
getRequirements: (
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
106
|
+
getRequirements: (requirementsParams) => {
|
|
107
|
+
this.validateDeadline(deadline);
|
|
108
|
+
if (nativeValue > 0n)
|
|
109
|
+
return Promise.resolve([]);
|
|
110
|
+
return this.getTokenRequirements({
|
|
111
|
+
token: this.marketParams.loanToken,
|
|
112
|
+
amount: assets,
|
|
113
|
+
userAddress,
|
|
114
|
+
deadline,
|
|
115
|
+
useSimplePermit: requirementsParams?.useSimplePermit,
|
|
116
|
+
permit2Nonce: requirementsParams?.permit2Nonce,
|
|
117
|
+
approvalAmount: requirementsParams?.approvalAmount,
|
|
118
|
+
});
|
|
119
|
+
},
|
|
89
120
|
buildTx: (signatures) => {
|
|
90
|
-
const {
|
|
91
|
-
|
|
121
|
+
const { token } = selectBlueBundlesV1RequirementSignatures(signatures, {
|
|
122
|
+
token: nativeValue === 0n,
|
|
92
123
|
});
|
|
93
124
|
return blueSupply({
|
|
94
125
|
market: { chainId: this.chainId, marketParams: this.marketParams },
|
|
95
126
|
args: {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
127
|
+
userAddress,
|
|
128
|
+
assets,
|
|
129
|
+
nativeAmount: nativeValue > 0n ? nativeValue : undefined,
|
|
130
|
+
deadline,
|
|
131
|
+
referralFeePct,
|
|
132
|
+
referralFeeRecipient,
|
|
133
|
+
requirementSignature: token,
|
|
101
134
|
},
|
|
102
135
|
metadata: this.client.options.metadata,
|
|
103
136
|
});
|
|
104
137
|
},
|
|
105
138
|
};
|
|
106
139
|
}
|
|
140
|
+
/** {@inheritDoc BlueActions.withdraw} */
|
|
107
141
|
withdraw(params) {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
const reallocationPlan = validateAndNormalizeReallocations({
|
|
111
|
-
reallocations,
|
|
112
|
-
targetMarketId: this.marketParams.id,
|
|
113
|
-
chainId: this.chainId,
|
|
114
|
-
});
|
|
115
|
-
const reallocationList = reallocationPlan.reallocations;
|
|
116
|
-
// Mode normalization: a missing or undefined `assets`/`shares` key collapses to `0n`
|
|
117
|
-
// so the mutual-exclusion and positivity checks below are pure value comparisons.
|
|
142
|
+
const { userAddress, positionData, deadline, referralFeePct, referralFeeRecipient, } = params;
|
|
143
|
+
this.validateWriteCommon(params);
|
|
118
144
|
const assets = ("assets" in params ? params.assets : undefined) ?? 0n;
|
|
119
145
|
const shares = ("shares" in params ? params.shares : undefined) ?? 0n;
|
|
120
|
-
|
|
121
|
-
// before positivity, so `{ assets: -1n, shares: 5n }` reports the actual
|
|
122
|
-
// mode conflict instead of being misclassified as a positivity error.
|
|
123
|
-
// Mirrors action layer.
|
|
124
|
-
if (assets !== 0n && shares !== 0n) {
|
|
125
|
-
throw new MutuallyExclusiveWithdrawAmountsError(this.marketParams.id);
|
|
126
|
-
}
|
|
127
|
-
if (assets < 0n) {
|
|
146
|
+
if (assets < 0n)
|
|
128
147
|
throw new NegativeInputError("assets", assets);
|
|
129
|
-
|
|
130
|
-
if (shares < 0n) {
|
|
148
|
+
if (shares < 0n)
|
|
131
149
|
throw new NegativeInputError("shares", shares);
|
|
150
|
+
if (assets > 0n && shares > 0n) {
|
|
151
|
+
throw new MutuallyExclusiveWithdrawAmountsError(this.marketParams.id);
|
|
132
152
|
}
|
|
133
153
|
if (assets === 0n && shares === 0n) {
|
|
134
154
|
throw new NonPositiveInputError("assets or shares", 0n);
|
|
135
155
|
}
|
|
136
|
-
|
|
137
|
-
if (!positionData) {
|
|
156
|
+
if (positionData == null) {
|
|
138
157
|
throw new MissingAccrualPositionError(this.marketParams.id);
|
|
139
158
|
}
|
|
140
159
|
validateAccrualPosition({
|
|
@@ -156,37 +175,34 @@ export class MorphoBlue {
|
|
|
156
175
|
marketId: this.marketParams.id,
|
|
157
176
|
});
|
|
158
177
|
}
|
|
159
|
-
const
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
178
|
+
const reallocations = [...(params.reallocations ?? [])];
|
|
179
|
+
// Validate the exact PublicAllocations mapping before requirement reads.
|
|
180
|
+
const publicAllocations = getBlueBundlesV1PublicAllocations(reallocations, this.marketParams);
|
|
181
|
+
const reallocationPenaltyAssets = getBlueBundlesV1PenaltyAssets(publicAllocations);
|
|
182
|
+
const expectedWithdrawAssets = assets > 0n ? assets : positionData.market.toSupplyAssets(shares, "Down");
|
|
183
|
+
if (reallocationPenaltyAssets > expectedWithdrawAssets) {
|
|
184
|
+
throw new InputExceedsMaxError({
|
|
185
|
+
field: "reallocationPenaltyAssets",
|
|
186
|
+
value: reallocationPenaltyAssets,
|
|
187
|
+
max: expectedWithdrawAssets,
|
|
188
|
+
});
|
|
189
|
+
}
|
|
165
190
|
return {
|
|
166
|
-
getRequirements:
|
|
167
|
-
const [penaltyRequirements, authTx] = await Promise.all([
|
|
168
|
-
this.getReallocationPenaltyRequirements(userAddress, reallocationPlan),
|
|
169
|
-
getBlueAuthorizationRequirement({
|
|
170
|
-
viemClient: this.client.viemClient,
|
|
171
|
-
chainId: this.chainId,
|
|
172
|
-
userAddress,
|
|
173
|
-
supportSignature: this.client.options.supportSignature,
|
|
174
|
-
}),
|
|
175
|
-
]);
|
|
176
|
-
return [...penaltyRequirements, ...(authTx ? [authTx] : [])];
|
|
177
|
-
},
|
|
191
|
+
getRequirements: () => this.getAuthorizationRequirements({ userAddress, deadline }),
|
|
178
192
|
buildTx: (signatures) => {
|
|
179
|
-
const { authorization } =
|
|
193
|
+
const { authorization } = selectBlueBundlesV1RequirementSignatures(signatures, {
|
|
180
194
|
authorization: true,
|
|
181
195
|
});
|
|
182
196
|
return blueWithdraw({
|
|
183
197
|
market: { chainId: this.chainId, marketParams: this.marketParams },
|
|
184
198
|
args: {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
199
|
+
userAddress,
|
|
200
|
+
withdrawAssets: assets,
|
|
201
|
+
withdrawShares: shares,
|
|
202
|
+
reallocations,
|
|
203
|
+
deadline,
|
|
204
|
+
referralFeePct,
|
|
205
|
+
referralFeeRecipient,
|
|
190
206
|
authorizationSignature: authorization,
|
|
191
207
|
},
|
|
192
208
|
metadata: this.client.options.metadata,
|
|
@@ -194,109 +210,68 @@ export class MorphoBlue {
|
|
|
194
210
|
},
|
|
195
211
|
};
|
|
196
212
|
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
}
|
|
209
|
-
if (nativeAmount !== undefined && nativeAmount > 0n) {
|
|
210
|
-
validateNativeAsset(this.chainId, this.marketParams.collateralToken);
|
|
211
|
-
}
|
|
213
|
+
/** {@inheritDoc BlueActions.supplyCollateral} */
|
|
214
|
+
supplyCollateral(params) {
|
|
215
|
+
const { userAddress, collateralAssets, nativeAmount, deadline, referralFeePct, referralFeeRecipient, } = params;
|
|
216
|
+
const combinedAction = this.supplyCollateralBorrow({
|
|
217
|
+
userAddress,
|
|
218
|
+
collateralAssets,
|
|
219
|
+
borrowAssets: 0n,
|
|
220
|
+
nativeAmount,
|
|
221
|
+
deadline,
|
|
222
|
+
referralFeePct,
|
|
223
|
+
referralFeeRecipient,
|
|
224
|
+
});
|
|
212
225
|
return {
|
|
213
|
-
getRequirements:
|
|
214
|
-
address: this.marketParams.collateralToken,
|
|
215
|
-
chainId: this.chainId,
|
|
216
|
-
supportSignature: this.client.options.supportSignature,
|
|
217
|
-
supportDeployless: this.client.options.supportDeployless,
|
|
218
|
-
useSimplePermit: params?.useSimplePermit,
|
|
219
|
-
args: { amount, from: userAddress },
|
|
220
|
-
}),
|
|
226
|
+
getRequirements: combinedAction.getRequirements,
|
|
221
227
|
buildTx: (signatures) => {
|
|
222
|
-
const {
|
|
223
|
-
|
|
228
|
+
const { token } = selectBlueBundlesV1RequirementSignatures(signatures, {
|
|
229
|
+
token: (nativeAmount ?? 0n) === 0n,
|
|
224
230
|
});
|
|
225
231
|
return blueSupplyCollateral({
|
|
226
|
-
market: {
|
|
227
|
-
chainId: this.chainId,
|
|
228
|
-
marketParams: this.marketParams,
|
|
229
|
-
},
|
|
232
|
+
market: { chainId: this.chainId, marketParams: this.marketParams },
|
|
230
233
|
args: {
|
|
231
|
-
|
|
234
|
+
userAddress,
|
|
235
|
+
collateralAssets,
|
|
232
236
|
nativeAmount,
|
|
233
|
-
|
|
234
|
-
|
|
237
|
+
deadline,
|
|
238
|
+
referralFeePct,
|
|
239
|
+
referralFeeRecipient,
|
|
240
|
+
requirementSignature: token,
|
|
235
241
|
},
|
|
236
242
|
metadata: this.client.options.metadata,
|
|
237
243
|
});
|
|
238
244
|
},
|
|
239
245
|
};
|
|
240
246
|
}
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
const
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
if (amount <= 0n) {
|
|
250
|
-
throw new NonPositiveInputError("amount", amount);
|
|
251
|
-
}
|
|
252
|
-
validateSlippageTolerance(slippageTolerance);
|
|
253
|
-
if (!positionData) {
|
|
254
|
-
throw new MissingAccrualPositionError(this.marketParams.id);
|
|
255
|
-
}
|
|
256
|
-
validateAccrualPosition({
|
|
247
|
+
/** {@inheritDoc BlueActions.borrow} */
|
|
248
|
+
borrow(params) {
|
|
249
|
+
const { userAddress, borrowAssets, positionData, reallocations, deadline, referralFeePct, referralFeeRecipient, } = params;
|
|
250
|
+
const normalizedReallocations = [...(reallocations ?? [])];
|
|
251
|
+
const combinedAction = this.supplyCollateralBorrow({
|
|
252
|
+
userAddress,
|
|
253
|
+
collateralAssets: 0n,
|
|
254
|
+
borrowAssets,
|
|
257
255
|
positionData,
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
positionData,
|
|
263
|
-
additionalCollateral: 0n,
|
|
264
|
-
borrowAmount: amount,
|
|
265
|
-
marketId: this.marketParams.id,
|
|
266
|
-
lltv: this.marketParams.lltv,
|
|
267
|
-
});
|
|
268
|
-
const minSharePrice = computeMinBorrowSharePrice({
|
|
269
|
-
borrowAmount: amount,
|
|
270
|
-
market: positionData.market,
|
|
271
|
-
slippageTolerance,
|
|
256
|
+
reallocations: normalizedReallocations,
|
|
257
|
+
deadline,
|
|
258
|
+
referralFeePct,
|
|
259
|
+
referralFeeRecipient,
|
|
272
260
|
});
|
|
273
261
|
return {
|
|
274
|
-
getRequirements:
|
|
275
|
-
const [penaltyRequirements, authTx] = await Promise.all([
|
|
276
|
-
this.getReallocationPenaltyRequirements(userAddress, reallocationPlan),
|
|
277
|
-
getBlueAuthorizationRequirement({
|
|
278
|
-
viemClient: this.client.viemClient,
|
|
279
|
-
chainId: this.chainId,
|
|
280
|
-
userAddress,
|
|
281
|
-
supportSignature: this.client.options.supportSignature,
|
|
282
|
-
}),
|
|
283
|
-
]);
|
|
284
|
-
return [...penaltyRequirements, ...(authTx ? [authTx] : [])];
|
|
285
|
-
},
|
|
262
|
+
getRequirements: () => combinedAction.getRequirements(),
|
|
286
263
|
buildTx: (signatures) => {
|
|
287
|
-
const { authorization } =
|
|
288
|
-
authorization: true,
|
|
289
|
-
});
|
|
264
|
+
const { authorization } = selectBlueBundlesV1RequirementSignatures(signatures, { authorization: true });
|
|
290
265
|
return blueBorrow({
|
|
291
|
-
market: {
|
|
292
|
-
chainId: this.chainId,
|
|
293
|
-
marketParams: this.marketParams,
|
|
294
|
-
},
|
|
266
|
+
market: { chainId: this.chainId, marketParams: this.marketParams },
|
|
295
267
|
args: {
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
reallocations:
|
|
268
|
+
userAddress,
|
|
269
|
+
borrowAssets,
|
|
270
|
+
maxLtv: this.getMaxLtv(),
|
|
271
|
+
reallocations: normalizedReallocations,
|
|
272
|
+
deadline,
|
|
273
|
+
referralFeePct,
|
|
274
|
+
referralFeeRecipient,
|
|
300
275
|
authorizationSignature: authorization,
|
|
301
276
|
},
|
|
302
277
|
metadata: this.client.options.metadata,
|
|
@@ -304,209 +279,98 @@ export class MorphoBlue {
|
|
|
304
279
|
},
|
|
305
280
|
};
|
|
306
281
|
}
|
|
282
|
+
/** {@inheritDoc BlueActions.repay} */
|
|
307
283
|
repay(params) {
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
if (nativeAmount < 0n) {
|
|
312
|
-
throw new NegativeInputError("nativeAmount", nativeAmount);
|
|
313
|
-
}
|
|
314
|
-
const amount = ("amount" in params ? params.amount : undefined) ?? 0n;
|
|
315
|
-
const shares = ("shares" in params ? params.shares : undefined) ?? 0n;
|
|
316
|
-
if (amount < 0n) {
|
|
317
|
-
throw new NegativeInputError("amount", amount);
|
|
318
|
-
}
|
|
319
|
-
if (shares < 0n) {
|
|
320
|
-
throw new NegativeInputError("shares", shares);
|
|
321
|
-
}
|
|
322
|
-
if (amount > 0n && shares > 0n) {
|
|
323
|
-
throw new MutuallyExclusiveRepayAmountsError(this.marketParams.id);
|
|
324
|
-
}
|
|
325
|
-
if ("shares" in params) {
|
|
326
|
-
if (shares === 0n) {
|
|
327
|
-
throw new NonPositiveInputError("shares", shares);
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
else {
|
|
331
|
-
if (amount + nativeAmount <= 0n) {
|
|
332
|
-
throw new NonPositiveInputError("amount + nativeAmount", amount + nativeAmount);
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
validateSlippageTolerance(slippageTolerance);
|
|
336
|
-
if (!positionData) {
|
|
337
|
-
throw new MissingAccrualPositionError(this.marketParams.id);
|
|
338
|
-
}
|
|
339
|
-
validateAccrualPosition({
|
|
340
|
-
positionData,
|
|
341
|
-
expectedMarketId: this.marketParams.id,
|
|
342
|
-
expectedUser: userAddress,
|
|
343
|
-
});
|
|
344
|
-
if (nativeAmount > 0n) {
|
|
345
|
-
validateNativeAsset(this.chainId, this.marketParams.loanToken);
|
|
346
|
-
}
|
|
347
|
-
let repayAssets;
|
|
348
|
-
let repayShares;
|
|
349
|
-
let erc20Amount;
|
|
350
|
-
// Forward-accrue (2h) before deriving `maxSharePrice` (both modes): on-chain `morphoRepay` accrues `lastUpdate → execution`, so an un-accrued bound reverts on quiet markets.
|
|
351
|
-
const accrualTimestamp = MathLib.max(Time.timestamp(), positionData.market.lastUpdate) +
|
|
352
|
-
Time.s.from.h(2n);
|
|
353
|
-
const marketForRepay = positionData.market.accrueInterest(accrualTimestamp);
|
|
354
|
-
if ("shares" in params) {
|
|
355
|
-
validateRepayShares({
|
|
356
|
-
positionData,
|
|
357
|
-
repayShares: shares,
|
|
358
|
-
marketId: this.marketParams.id,
|
|
359
|
-
});
|
|
360
|
-
repayAssets = 0n;
|
|
361
|
-
repayShares = shares;
|
|
362
|
-
const borrowAssets = marketForRepay.toBorrowAssets(shares, "Up");
|
|
363
|
-
// Native funds the transfer first; the ERC-20 pulled is the remainder.
|
|
364
|
-
// When native covers the full (2h-forward-accrued, rounded-up) borrow
|
|
365
|
-
// assets, nothing is pulled as ERC-20 — the bundle wraps the native and
|
|
366
|
-
// skims any residual wNative back to the receiver. So a fully-native
|
|
367
|
-
// shares repay pulls no ERC-20 and emits no loan-token approval requirement.
|
|
368
|
-
erc20Amount = MathLib.zeroFloorSub(borrowAssets, nativeAmount);
|
|
369
|
-
}
|
|
370
|
-
else {
|
|
371
|
-
// Assets mode is additive, like supply: repaid = amount + nativeAmount.
|
|
372
|
-
repayAssets = amount + nativeAmount;
|
|
373
|
-
validateRepayAmount({
|
|
374
|
-
positionData,
|
|
375
|
-
repayAssets,
|
|
376
|
-
marketId: this.marketParams.id,
|
|
377
|
-
});
|
|
378
|
-
repayShares = 0n;
|
|
379
|
-
erc20Amount = amount;
|
|
380
|
-
}
|
|
381
|
-
const maxSharePrice = computeMaxRepaySharePrice({
|
|
382
|
-
repayAssets,
|
|
383
|
-
repayShares,
|
|
384
|
-
market: marketForRepay,
|
|
385
|
-
slippageTolerance,
|
|
284
|
+
const combinedAction = this.repayWithdrawCollateral({
|
|
285
|
+
...params,
|
|
286
|
+
collateralAssets: 0n,
|
|
386
287
|
});
|
|
387
288
|
return {
|
|
388
|
-
getRequirements:
|
|
389
|
-
// Fully native repay pulls no ERC-20, so it needs no approval/permit.
|
|
390
|
-
if (erc20Amount === 0n)
|
|
391
|
-
return Promise.resolve([]);
|
|
392
|
-
return getGeneralAdapterRequirements(this.client.viemClient, {
|
|
393
|
-
address: this.marketParams.loanToken,
|
|
394
|
-
chainId: this.chainId,
|
|
395
|
-
supportSignature: this.client.options.supportSignature,
|
|
396
|
-
supportDeployless: this.client.options.supportDeployless,
|
|
397
|
-
useSimplePermit: reqParams?.useSimplePermit,
|
|
398
|
-
args: { amount: erc20Amount, from: userAddress },
|
|
399
|
-
});
|
|
400
|
-
},
|
|
289
|
+
getRequirements: combinedAction.getRequirements,
|
|
401
290
|
buildTx: (signatures) => {
|
|
402
|
-
const
|
|
403
|
-
|
|
291
|
+
const combinedTransaction = combinedAction.buildTx(signatures);
|
|
292
|
+
const { token } = selectBlueBundlesV1RequirementSignatures(signatures, {
|
|
293
|
+
token: combinedTransaction.value === 0n,
|
|
404
294
|
});
|
|
295
|
+
const { repayAssets, repayShares, maxRepayAssets, nativeAmount, referralFeePct, referralFeeRecipient, deadline, } = combinedTransaction.action.args;
|
|
405
296
|
return blueRepay({
|
|
406
|
-
market: {
|
|
407
|
-
|
|
408
|
-
|
|
297
|
+
market: { chainId: this.chainId, marketParams: this.marketParams },
|
|
298
|
+
args: {
|
|
299
|
+
userAddress: params.userAddress,
|
|
300
|
+
repayAssets,
|
|
301
|
+
repayShares,
|
|
302
|
+
maxRepayAssets,
|
|
303
|
+
nativeAmount,
|
|
304
|
+
deadline,
|
|
305
|
+
referralFeePct,
|
|
306
|
+
referralFeeRecipient,
|
|
307
|
+
requirementSignature: token,
|
|
409
308
|
},
|
|
410
|
-
// Shares mode: repay `shares`, ERC-20 to pull = `erc20Amount`.
|
|
411
|
-
// Assets mode: repay `repayAssets` (= amount + native), pull `erc20Amount`.
|
|
412
|
-
args: repayShares > 0n
|
|
413
|
-
? {
|
|
414
|
-
shares: repayShares,
|
|
415
|
-
transferAmount: erc20Amount,
|
|
416
|
-
nativeAmount,
|
|
417
|
-
onBehalf: userAddress,
|
|
418
|
-
receiver: userAddress,
|
|
419
|
-
maxSharePrice,
|
|
420
|
-
requirementSignature: permit,
|
|
421
|
-
}
|
|
422
|
-
: {
|
|
423
|
-
amount: erc20Amount,
|
|
424
|
-
transferAmount: repayAssets,
|
|
425
|
-
nativeAmount,
|
|
426
|
-
onBehalf: userAddress,
|
|
427
|
-
receiver: userAddress,
|
|
428
|
-
maxSharePrice,
|
|
429
|
-
requirementSignature: permit,
|
|
430
|
-
},
|
|
431
309
|
metadata: this.client.options.metadata,
|
|
432
310
|
});
|
|
433
311
|
},
|
|
434
312
|
};
|
|
435
313
|
}
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
if (!positionData) {
|
|
442
|
-
throw new MissingAccrualPositionError(this.marketParams.id);
|
|
443
|
-
}
|
|
444
|
-
validateAccrualPosition({
|
|
314
|
+
/** {@inheritDoc BlueActions.withdrawCollateral} */
|
|
315
|
+
withdrawCollateral(params) {
|
|
316
|
+
const { userAddress, positionData, collateralAssets, deadline, referralFeePct, referralFeeRecipient, } = params;
|
|
317
|
+
const combinedAction = this.repayWithdrawCollateral({
|
|
318
|
+
userAddress,
|
|
445
319
|
positionData,
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
withdrawAmount: amount,
|
|
452
|
-
available: positionData.collateral,
|
|
453
|
-
market: positionData.marketId,
|
|
454
|
-
});
|
|
455
|
-
}
|
|
456
|
-
validatePositionHealthAfterWithdraw({
|
|
457
|
-
positionData,
|
|
458
|
-
withdrawAmount: amount,
|
|
459
|
-
lltv: this.marketParams.lltv,
|
|
460
|
-
marketId: this.marketParams.id,
|
|
320
|
+
repayAssets: 0n,
|
|
321
|
+
collateralAssets,
|
|
322
|
+
deadline,
|
|
323
|
+
referralFeePct,
|
|
324
|
+
referralFeeRecipient,
|
|
461
325
|
});
|
|
462
326
|
return {
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
327
|
+
getRequirements: () => combinedAction.getRequirements(),
|
|
328
|
+
buildTx: (signatures) => {
|
|
329
|
+
const combinedTransaction = combinedAction.buildTx(signatures);
|
|
330
|
+
const { authorization } = selectBlueBundlesV1RequirementSignatures(signatures, { authorization: true });
|
|
331
|
+
const combinedArgs = combinedTransaction.action.args;
|
|
332
|
+
return blueWithdrawCollateral({
|
|
333
|
+
market: { chainId: this.chainId, marketParams: this.marketParams },
|
|
334
|
+
args: {
|
|
335
|
+
userAddress,
|
|
336
|
+
collateralAssets: combinedArgs.collateralAssets,
|
|
337
|
+
maxLtv: combinedArgs.maxLtv,
|
|
338
|
+
deadline: combinedArgs.deadline,
|
|
339
|
+
referralFeePct: combinedArgs.referralFeePct,
|
|
340
|
+
referralFeeRecipient: combinedArgs.referralFeeRecipient,
|
|
341
|
+
authorizationSignature: authorization,
|
|
342
|
+
},
|
|
343
|
+
metadata: this.client.options.metadata,
|
|
344
|
+
});
|
|
345
|
+
},
|
|
475
346
|
};
|
|
476
347
|
}
|
|
348
|
+
/** {@inheritDoc BlueActions.repayWithdrawCollateral} */
|
|
477
349
|
repayWithdrawCollateral(params) {
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
const
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
350
|
+
const { userAddress, positionData, collateralAssets, nativeAmount, deadline, referralFeeRecipient, } = params;
|
|
351
|
+
this.validateWriteCommon(params);
|
|
352
|
+
// `validateWriteCommon` returns void, so default an omitted `referralFeePct` to `0n`
|
|
353
|
+
// here, matching the pure action's normalization.
|
|
354
|
+
const referralFeePct = params.referralFeePct ?? 0n;
|
|
355
|
+
const repayAssets = params.repayAssets ?? 0n;
|
|
356
|
+
const repayShares = params.repayShares ?? 0n;
|
|
357
|
+
if (repayAssets < 0n) {
|
|
358
|
+
throw new NegativeInputError("repayAssets", repayAssets);
|
|
359
|
+
}
|
|
360
|
+
if (repayShares < 0n) {
|
|
361
|
+
throw new NegativeInputError("repayShares", repayShares);
|
|
362
|
+
}
|
|
363
|
+
if (collateralAssets < 0n) {
|
|
364
|
+
throw new NegativeInputError("collateralAssets", collateralAssets);
|
|
365
|
+
}
|
|
366
|
+
if (repayAssets > 0n && repayShares > 0n) {
|
|
493
367
|
throw new MutuallyExclusiveRepayAmountsError(this.marketParams.id);
|
|
494
368
|
}
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
}
|
|
369
|
+
const hasRepay = repayAssets > 0n || repayShares > 0n;
|
|
370
|
+
if (!hasRepay && collateralAssets === 0n) {
|
|
371
|
+
throw new NonPositiveInputError("repayAssets, repayShares, or collateralAssets", 0n);
|
|
499
372
|
}
|
|
500
|
-
|
|
501
|
-
if (amount + nativeAmount <= 0n) {
|
|
502
|
-
throw new NonPositiveInputError("amount + nativeAmount", amount + nativeAmount);
|
|
503
|
-
}
|
|
504
|
-
}
|
|
505
|
-
if (withdrawAmount <= 0n) {
|
|
506
|
-
throw new NonPositiveInputError("withdrawAmount", withdrawAmount);
|
|
507
|
-
}
|
|
508
|
-
validateSlippageTolerance(slippageTolerance);
|
|
509
|
-
if (!positionData) {
|
|
373
|
+
if (positionData == null) {
|
|
510
374
|
throw new MissingAccrualPositionError(this.marketParams.id);
|
|
511
375
|
}
|
|
512
376
|
validateAccrualPosition({
|
|
@@ -514,250 +378,222 @@ export class MorphoBlue {
|
|
|
514
378
|
expectedMarketId: this.marketParams.id,
|
|
515
379
|
expectedUser: userAddress,
|
|
516
380
|
});
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
// Forward-accrue (2h) for `maxSharePrice` (both modes) and the post-repay health check: on-chain `morphoRepay` accrues `lastUpdate → execution`, so an un-accrued bound reverts on quiet markets.
|
|
524
|
-
const accrualTimestamp = MathLib.max(Time.timestamp(), positionData.market.lastUpdate) +
|
|
525
|
-
Time.s.from.h(2n);
|
|
526
|
-
const marketForRepay = positionData.market.accrueInterest(accrualTimestamp);
|
|
527
|
-
if ("shares" in params) {
|
|
528
|
-
validateRepayShares({
|
|
381
|
+
const saturatedRepay = repayShares === maxUint256;
|
|
382
|
+
const simulatedRepayShares = saturatedRepay
|
|
383
|
+
? positionData.borrowShares
|
|
384
|
+
: repayShares;
|
|
385
|
+
if (repayAssets > 0n) {
|
|
386
|
+
validateRepayAmount({
|
|
529
387
|
positionData,
|
|
530
|
-
|
|
388
|
+
repayAssets,
|
|
531
389
|
marketId: this.marketParams.id,
|
|
532
390
|
});
|
|
533
|
-
repayAssets = 0n;
|
|
534
|
-
repayShares = shares;
|
|
535
|
-
const borrowAssets = marketForRepay.toBorrowAssets(shares, "Up");
|
|
536
|
-
// Native funds the transfer first; the ERC-20 pulled is the remainder.
|
|
537
|
-
// When native covers the full (2h-forward-accrued, rounded-up) borrow
|
|
538
|
-
// assets, nothing is pulled as ERC-20 — the bundle wraps the native and
|
|
539
|
-
// skims any residual wNative back to the receiver. So a fully-native
|
|
540
|
-
// shares repay pulls no ERC-20 and emits no loan-token approval requirement.
|
|
541
|
-
erc20Amount = MathLib.zeroFloorSub(borrowAssets, nativeAmount);
|
|
542
391
|
}
|
|
543
|
-
else {
|
|
544
|
-
|
|
545
|
-
repayAssets = amount + nativeAmount;
|
|
546
|
-
validateRepayAmount({
|
|
392
|
+
else if (repayShares > 0n && !saturatedRepay) {
|
|
393
|
+
validateRepayShares({
|
|
547
394
|
positionData,
|
|
548
|
-
|
|
395
|
+
repayShares,
|
|
549
396
|
marketId: this.marketParams.id,
|
|
550
397
|
});
|
|
551
|
-
repayShares = 0n;
|
|
552
|
-
erc20Amount = amount;
|
|
553
398
|
}
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
399
|
+
const quoteHorizon = this.getBlueBundlesV1QuoteTimestamp(positionData.market.lastUpdate);
|
|
400
|
+
if (repayShares > 0n && deadline > quoteHorizon) {
|
|
401
|
+
throw new InputExceedsMaxError({
|
|
402
|
+
field: "deadline",
|
|
403
|
+
value: deadline,
|
|
404
|
+
max: quoteHorizon,
|
|
559
405
|
});
|
|
560
406
|
}
|
|
561
|
-
const
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
const
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
407
|
+
const accrualTimestamp = repayShares > 0n ? deadline : quoteHorizon;
|
|
408
|
+
const accruedPosition = positionData.accrueInterest(accrualTimestamp);
|
|
409
|
+
const expectedRepayAssets = repayAssets > 0n
|
|
410
|
+
? repayAssets
|
|
411
|
+
: hasRepay
|
|
412
|
+
? accruedPosition.market.toBorrowAssets(simulatedRepayShares, "Up")
|
|
413
|
+
: 0n;
|
|
414
|
+
const referralFeeAssets = getBlueBundlesV1ReferralFeeAssets(expectedRepayAssets, referralFeePct);
|
|
415
|
+
const maxRepayAssets = expectedRepayAssets + referralFeeAssets;
|
|
416
|
+
const nativeValue = validateBlueBundlesV1NativeFunding({
|
|
417
|
+
chainId: this.chainId,
|
|
418
|
+
token: this.marketParams.loanToken,
|
|
419
|
+
fundedAmount: maxRepayAssets,
|
|
420
|
+
nativeAmount,
|
|
573
421
|
});
|
|
422
|
+
if (collateralAssets > 0n) {
|
|
423
|
+
const postRepayPosition = hasRepay
|
|
424
|
+
? accruedPosition.repay(repayAssets, simulatedRepayShares).position
|
|
425
|
+
: accruedPosition;
|
|
426
|
+
validatePositionHealthAfterWithdraw({
|
|
427
|
+
positionData: postRepayPosition,
|
|
428
|
+
withdrawAmount: collateralAssets,
|
|
429
|
+
lltv: this.marketParams.lltv,
|
|
430
|
+
marketId: this.marketParams.id,
|
|
431
|
+
});
|
|
432
|
+
}
|
|
574
433
|
return {
|
|
575
|
-
getRequirements: async (
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
?
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
434
|
+
getRequirements: async (requirementsParams) => {
|
|
435
|
+
this.validateDeadline(deadline);
|
|
436
|
+
const [tokenRequirements, authorizationRequirements] = await Promise.all([
|
|
437
|
+
hasRepay && nativeValue === 0n
|
|
438
|
+
? this.getTokenRequirements({
|
|
439
|
+
token: this.marketParams.loanToken,
|
|
440
|
+
amount: maxRepayAssets,
|
|
441
|
+
// A caller-requested reusable approval wins; a full repay otherwise defaults to
|
|
442
|
+
// the token's reusable cap. Checksum the key so a differently-cased loan token
|
|
443
|
+
// (common from subgraphs/APIs) resolves that cap instead of falling back to
|
|
444
|
+
// maxUint256, which UNI/ONDO/COMP/FLUID reject.
|
|
445
|
+
approvalAmount: requirementsParams?.approvalAmount ??
|
|
446
|
+
(saturatedRepay
|
|
447
|
+
? (MAX_TOKEN_APPROVALS[this.chainId]?.[getAddress(this.marketParams.loanToken)] ?? maxUint256)
|
|
448
|
+
: undefined),
|
|
449
|
+
userAddress,
|
|
450
|
+
deadline,
|
|
451
|
+
useSimplePermit: requirementsParams?.useSimplePermit,
|
|
452
|
+
permit2Nonce: requirementsParams?.permit2Nonce,
|
|
453
|
+
})
|
|
454
|
+
: Promise.resolve([]),
|
|
455
|
+
collateralAssets > 0n
|
|
456
|
+
? this.getAuthorizationRequirements({ userAddress, deadline })
|
|
457
|
+
: Promise.resolve([]),
|
|
594
458
|
]);
|
|
595
|
-
return [...
|
|
459
|
+
return [...tokenRequirements, ...authorizationRequirements];
|
|
596
460
|
},
|
|
597
461
|
buildTx: (signatures) => {
|
|
598
|
-
const {
|
|
462
|
+
const { token, authorization } = selectBlueBundlesV1RequirementSignatures(signatures, {
|
|
463
|
+
token: hasRepay && nativeValue === 0n,
|
|
464
|
+
authorization: collateralAssets > 0n,
|
|
465
|
+
});
|
|
466
|
+
const selectedMaxRepayAssets = repayShares > 0n && token != null
|
|
467
|
+
? token.args.amount
|
|
468
|
+
: maxRepayAssets;
|
|
469
|
+
if (selectedMaxRepayAssets < maxRepayAssets) {
|
|
470
|
+
throw new MaxRepayAssetsBelowRepayAssetsError(selectedMaxRepayAssets, maxRepayAssets);
|
|
471
|
+
}
|
|
599
472
|
return blueRepayWithdrawCollateral({
|
|
600
|
-
market: {
|
|
601
|
-
|
|
602
|
-
|
|
473
|
+
market: { chainId: this.chainId, marketParams: this.marketParams },
|
|
474
|
+
args: {
|
|
475
|
+
userAddress,
|
|
476
|
+
repayAssets,
|
|
477
|
+
repayShares,
|
|
478
|
+
maxRepayAssets: selectedMaxRepayAssets,
|
|
479
|
+
collateralAssets,
|
|
480
|
+
maxLtv: collateralAssets > 0n ? this.getMaxLtv() : maxUint256,
|
|
481
|
+
nativeAmount: nativeValue > 0n ? nativeValue : undefined,
|
|
482
|
+
deadline,
|
|
483
|
+
referralFeePct,
|
|
484
|
+
referralFeeRecipient,
|
|
485
|
+
requirementSignature: token,
|
|
486
|
+
authorizationSignature: authorization,
|
|
603
487
|
},
|
|
604
|
-
// Shares mode: repay `shares`, ERC-20 to pull = `erc20Amount`.
|
|
605
|
-
// Assets mode: repay `repayAssets` (= amount + native), pull `erc20Amount`.
|
|
606
|
-
args: repayShares > 0n
|
|
607
|
-
? {
|
|
608
|
-
shares: repayShares,
|
|
609
|
-
transferAmount: erc20Amount,
|
|
610
|
-
nativeAmount,
|
|
611
|
-
withdrawAmount,
|
|
612
|
-
onBehalf: userAddress,
|
|
613
|
-
receiver: userAddress,
|
|
614
|
-
maxSharePrice,
|
|
615
|
-
requirementSignature: permit,
|
|
616
|
-
authorizationSignature: authorization,
|
|
617
|
-
}
|
|
618
|
-
: {
|
|
619
|
-
amount: erc20Amount,
|
|
620
|
-
transferAmount: repayAssets,
|
|
621
|
-
nativeAmount,
|
|
622
|
-
withdrawAmount,
|
|
623
|
-
onBehalf: userAddress,
|
|
624
|
-
receiver: userAddress,
|
|
625
|
-
maxSharePrice,
|
|
626
|
-
requirementSignature: permit,
|
|
627
|
-
authorizationSignature: authorization,
|
|
628
|
-
},
|
|
629
488
|
metadata: this.client.options.metadata,
|
|
630
489
|
});
|
|
631
490
|
},
|
|
632
491
|
};
|
|
633
492
|
}
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
const
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
}
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
if (
|
|
649
|
-
throw new
|
|
650
|
-
}
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
493
|
+
/** {@inheritDoc BlueActions.supplyCollateralBorrow} */
|
|
494
|
+
supplyCollateralBorrow(params) {
|
|
495
|
+
const { userAddress, collateralAssets, borrowAssets, positionData, nativeAmount, deadline, referralFeePct, referralFeeRecipient, } = params;
|
|
496
|
+
this.validateWriteCommon(params);
|
|
497
|
+
if (collateralAssets < 0n) {
|
|
498
|
+
throw new NegativeInputError("collateralAssets", collateralAssets);
|
|
499
|
+
}
|
|
500
|
+
if (borrowAssets < 0n) {
|
|
501
|
+
throw new NegativeInputError("borrowAssets", borrowAssets);
|
|
502
|
+
}
|
|
503
|
+
if (collateralAssets === 0n && borrowAssets === 0n) {
|
|
504
|
+
throw new NonPositiveInputError("collateralAssets or borrowAssets", 0n);
|
|
505
|
+
}
|
|
506
|
+
const reallocations = [...(params.reallocations ?? [])];
|
|
507
|
+
if (borrowAssets === 0n && reallocations.length > 0) {
|
|
508
|
+
throw new ReallocationsRequireBorrowError();
|
|
509
|
+
}
|
|
510
|
+
// Validate the exact PublicAllocations mapping before requirement reads.
|
|
511
|
+
const publicAllocations = getBlueBundlesV1PublicAllocations(reallocations, this.marketParams);
|
|
512
|
+
const reallocationPenaltyAssets = getBlueBundlesV1PenaltyAssets(publicAllocations);
|
|
513
|
+
if (reallocationPenaltyAssets > borrowAssets) {
|
|
514
|
+
throw new InputExceedsMaxError({
|
|
515
|
+
field: "reallocationPenaltyAssets",
|
|
516
|
+
value: reallocationPenaltyAssets,
|
|
517
|
+
max: borrowAssets,
|
|
518
|
+
});
|
|
658
519
|
}
|
|
659
|
-
|
|
660
|
-
positionData
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
520
|
+
if (borrowAssets > 0n) {
|
|
521
|
+
if (positionData == null) {
|
|
522
|
+
throw new MissingAccrualPositionError(this.marketParams.id);
|
|
523
|
+
}
|
|
524
|
+
validateAccrualPosition({
|
|
525
|
+
positionData,
|
|
526
|
+
expectedMarketId: this.marketParams.id,
|
|
527
|
+
expectedUser: userAddress,
|
|
528
|
+
});
|
|
529
|
+
validatePositionHealth({
|
|
530
|
+
positionData: positionData.accrueInterest(this.getBlueBundlesV1QuoteTimestamp(positionData.market.lastUpdate)),
|
|
531
|
+
additionalCollateral: collateralAssets,
|
|
532
|
+
borrowAmount: borrowAssets,
|
|
533
|
+
marketId: this.marketParams.id,
|
|
534
|
+
lltv: this.marketParams.lltv,
|
|
535
|
+
});
|
|
666
536
|
}
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
lltv: this.marketParams.lltv,
|
|
673
|
-
});
|
|
674
|
-
const minSharePrice = computeMinBorrowSharePrice({
|
|
675
|
-
borrowAmount,
|
|
676
|
-
market: positionData.market,
|
|
677
|
-
slippageTolerance,
|
|
537
|
+
const nativeValue = validateBlueBundlesV1NativeFunding({
|
|
538
|
+
chainId: this.chainId,
|
|
539
|
+
token: this.marketParams.collateralToken,
|
|
540
|
+
fundedAmount: collateralAssets,
|
|
541
|
+
nativeAmount,
|
|
678
542
|
});
|
|
679
543
|
return {
|
|
680
|
-
getRequirements: async (
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
}),
|
|
698
|
-
usesSharedFundingToken
|
|
699
|
-
? []
|
|
700
|
-
: this.getReallocationPenaltyRequirements(userAddress, reallocationPlan),
|
|
701
|
-
getBlueAuthorizationRequirement({
|
|
702
|
-
viemClient: this.client.viemClient,
|
|
703
|
-
chainId: this.chainId,
|
|
704
|
-
userAddress,
|
|
705
|
-
supportSignature: this.client.options.supportSignature,
|
|
706
|
-
}),
|
|
544
|
+
getRequirements: async (requirementsParams) => {
|
|
545
|
+
this.validateDeadline(deadline);
|
|
546
|
+
const [tokenRequirements, authorizationRequirements] = await Promise.all([
|
|
547
|
+
collateralAssets > 0n && nativeValue === 0n
|
|
548
|
+
? this.getTokenRequirements({
|
|
549
|
+
token: this.marketParams.collateralToken,
|
|
550
|
+
amount: collateralAssets,
|
|
551
|
+
approvalAmount: requirementsParams?.approvalAmount,
|
|
552
|
+
userAddress,
|
|
553
|
+
deadline,
|
|
554
|
+
useSimplePermit: requirementsParams?.useSimplePermit,
|
|
555
|
+
permit2Nonce: requirementsParams?.permit2Nonce,
|
|
556
|
+
})
|
|
557
|
+
: Promise.resolve([]),
|
|
558
|
+
borrowAssets > 0n
|
|
559
|
+
? this.getAuthorizationRequirements({ userAddress, deadline })
|
|
560
|
+
: Promise.resolve([]),
|
|
707
561
|
]);
|
|
708
|
-
return [
|
|
709
|
-
...erc20Requirements,
|
|
710
|
-
...penaltyRequirements,
|
|
711
|
-
...(authTx ? [authTx] : []),
|
|
712
|
-
];
|
|
562
|
+
return [...tokenRequirements, ...authorizationRequirements];
|
|
713
563
|
},
|
|
714
564
|
buildTx: (signatures) => {
|
|
715
|
-
const {
|
|
565
|
+
const { token, authorization } = selectBlueBundlesV1RequirementSignatures(signatures, {
|
|
566
|
+
token: collateralAssets > 0n && nativeValue === 0n,
|
|
567
|
+
authorization: borrowAssets > 0n,
|
|
568
|
+
});
|
|
716
569
|
return blueSupplyCollateralBorrow({
|
|
717
|
-
market: {
|
|
718
|
-
chainId: this.chainId,
|
|
719
|
-
marketParams: this.marketParams,
|
|
720
|
-
},
|
|
570
|
+
market: { chainId: this.chainId, marketParams: this.marketParams },
|
|
721
571
|
args: {
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
572
|
+
userAddress,
|
|
573
|
+
collateralAssets,
|
|
574
|
+
borrowAssets,
|
|
575
|
+
maxLtv: borrowAssets > 0n ? this.getMaxLtv() : maxUint256,
|
|
576
|
+
nativeAmount: nativeValue > 0n ? nativeValue : undefined,
|
|
577
|
+
reallocations,
|
|
578
|
+
deadline,
|
|
579
|
+
referralFeePct,
|
|
580
|
+
referralFeeRecipient,
|
|
581
|
+
requirementSignature: token,
|
|
729
582
|
authorizationSignature: authorization,
|
|
730
|
-
reallocations: reallocationList,
|
|
731
583
|
},
|
|
732
584
|
metadata: this.client.options.metadata,
|
|
733
585
|
});
|
|
734
586
|
},
|
|
735
587
|
};
|
|
736
588
|
}
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
const targetReallocationList = targetReallocationPlan.reallocations;
|
|
746
|
-
if (collateralAmount <= 0n) {
|
|
747
|
-
throw new NonPositiveInputError("collateralAmount", collateralAmount);
|
|
748
|
-
}
|
|
749
|
-
const requestedAssets = borrowAssets ?? 0n;
|
|
750
|
-
const requestedShares = borrowShares ?? 0n;
|
|
751
|
-
if (requestedAssets < 0n) {
|
|
752
|
-
throw new NegativeInputError("borrowAssets", requestedAssets);
|
|
753
|
-
}
|
|
754
|
-
if (requestedShares < 0n) {
|
|
755
|
-
throw new NegativeInputError("borrowShares", requestedShares);
|
|
756
|
-
}
|
|
757
|
-
if (requestedAssets > 0n && requestedShares > 0n) {
|
|
758
|
-
throw new BorrowAmountAndSharesExclusiveError(this.marketParams.id);
|
|
759
|
-
}
|
|
760
|
-
if (!positionData) {
|
|
589
|
+
/** {@inheritDoc BlueActions.refinance} */
|
|
590
|
+
refinance(params) {
|
|
591
|
+
const { userAddress, positionData, destination, deadline, referralFeeRecipient, } = params;
|
|
592
|
+
this.validateWriteCommon(params);
|
|
593
|
+
// `validateWriteCommon` returns void, so default an omitted `referralFeePct` to `0n`
|
|
594
|
+
// here, matching the pure action's normalization.
|
|
595
|
+
const referralFeePct = params.referralFeePct ?? 0n;
|
|
596
|
+
if (positionData == null) {
|
|
761
597
|
throw new MissingAccrualPositionError(this.marketParams.id);
|
|
762
598
|
}
|
|
763
599
|
validateAccrualPosition({
|
|
@@ -765,140 +601,57 @@ export class MorphoBlue {
|
|
|
765
601
|
expectedMarketId: this.marketParams.id,
|
|
766
602
|
expectedUser: userAddress,
|
|
767
603
|
});
|
|
768
|
-
if (
|
|
604
|
+
if (positionData.borrowShares <= 0n) {
|
|
605
|
+
throw new NonPositiveInputError("positionData.borrowShares", positionData.borrowShares);
|
|
606
|
+
}
|
|
607
|
+
if (this.marketParams.id === destination.marketParams.id) {
|
|
769
608
|
throw new RefinanceSameMarketError(this.marketParams.id);
|
|
770
609
|
}
|
|
771
|
-
if (!isAddressEqual(this.marketParams.
|
|
772
|
-
!isAddressEqual(this.marketParams.
|
|
773
|
-
throw new RefinanceTokenMismatchError(this.marketParams.id,
|
|
610
|
+
if (!isAddressEqual(this.marketParams.loanToken, destination.marketParams.loanToken) ||
|
|
611
|
+
!isAddressEqual(this.marketParams.collateralToken, destination.marketParams.collateralToken)) {
|
|
612
|
+
throw new RefinanceTokenMismatchError(this.marketParams.id, destination.marketParams.id);
|
|
613
|
+
}
|
|
614
|
+
if (destination.positionData == null) {
|
|
615
|
+
throw new MissingAccrualPositionError(destination.marketParams.id);
|
|
774
616
|
}
|
|
775
617
|
validateAccrualPosition({
|
|
776
|
-
positionData:
|
|
777
|
-
expectedMarketId:
|
|
618
|
+
positionData: destination.positionData,
|
|
619
|
+
expectedMarketId: destination.marketParams.id,
|
|
778
620
|
expectedUser: userAddress,
|
|
779
621
|
});
|
|
780
|
-
const
|
|
781
|
-
const
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
});
|
|
795
|
-
}
|
|
796
|
-
if (requestedAssets > positionData.borrowAssets) {
|
|
797
|
-
throw new RefinanceExceedsBorrowAssetsError({
|
|
798
|
-
market: this.marketParams.id,
|
|
799
|
-
requested: requestedAssets,
|
|
800
|
-
available: positionData.borrowAssets,
|
|
801
|
-
});
|
|
802
|
-
}
|
|
803
|
-
// Forward-accrue both markets to now (clamped to lastUpdate). Source gets a 2h buffer in
|
|
804
|
-
// shares mode (as in repay()) for repay headroom; target accrues without buffer to avoid
|
|
805
|
-
// tightening minBorrowSharePrice past on-chain reality.
|
|
806
|
-
const sourceAccrualTimestamp = MathLib.max(Time.timestamp(), positionData.market.lastUpdate) +
|
|
807
|
-
(sharesMode ? Time.s.from.h(2n) : 0n);
|
|
808
|
-
const targetAccrualTimestamp = MathLib.max(Time.timestamp(), target.positionData.market.lastUpdate);
|
|
809
|
-
const accruedSource = positionData.market.accrueInterest(sourceAccrualTimestamp);
|
|
810
|
-
const accruedTarget = target.positionData.market.accrueInterest(targetAccrualTimestamp);
|
|
811
|
-
// Shares burned by the source repay: exact in shares mode, else mirror Morpho's toSharesDown.
|
|
812
|
-
const repaidShares = sharesMode
|
|
813
|
-
? requestedShares
|
|
814
|
-
: accruedSource.toBorrowShares(requestedAssets, "Down");
|
|
815
|
-
// Post-state source health: any remaining debt must stay healthy (accrued market).
|
|
816
|
-
const remainingCollateral = positionData.collateral - collateralAmount;
|
|
817
|
-
const remainingShares = positionData.borrowShares - repaidShares;
|
|
818
|
-
if (remainingShares > 0n) {
|
|
819
|
-
const residualPosition = new AccrualPosition({
|
|
820
|
-
user: positionData.user,
|
|
821
|
-
supplyShares: positionData.supplyShares,
|
|
822
|
-
borrowShares: remainingShares,
|
|
823
|
-
collateral: remainingCollateral,
|
|
824
|
-
}, accruedSource);
|
|
825
|
-
validatePositionHealth({
|
|
826
|
-
positionData: residualPosition,
|
|
827
|
-
additionalCollateral: 0n,
|
|
828
|
-
borrowAmount: 0n,
|
|
829
|
-
marketId: this.marketParams.id,
|
|
830
|
-
lltv: this.marketParams.lltv,
|
|
831
|
-
});
|
|
832
|
-
}
|
|
833
|
-
const projectedBorrowAssets = sharesMode
|
|
834
|
-
? accruedSource.toBorrowAssets(requestedShares, "Up")
|
|
835
|
-
: requestedAssets;
|
|
836
|
-
// Shares-mode overshoot covers target drift + accrual on the borrow leg. Computed before the
|
|
837
|
-
// LLTV check so health validates the actual on-chain borrow, not the smaller projected value.
|
|
838
|
-
const borrowAssetsAdjusted = sharesMode
|
|
839
|
-
? MathLib.wMulUp(projectedBorrowAssets, MathLib.WAD + slippageTolerance)
|
|
840
|
-
: projectedBorrowAssets;
|
|
841
|
-
// Post-state target health: aggregate must respect LLTV − buffer. Skipped for collat-only
|
|
842
|
-
// refinances, which can't degrade target health and would fail on missing-oracle markets.
|
|
843
|
-
if (shouldMigrateBorrow) {
|
|
844
|
-
const accruedTargetPosition = target.positionData.accrueInterest(targetAccrualTimestamp);
|
|
845
|
-
validatePositionHealth({
|
|
846
|
-
positionData: accruedTargetPosition,
|
|
847
|
-
additionalCollateral: collateralAmount,
|
|
848
|
-
borrowAmount: borrowAssetsAdjusted,
|
|
849
|
-
marketId: target.marketParams.id,
|
|
850
|
-
lltv: target.marketParams.lltv,
|
|
851
|
-
});
|
|
852
|
-
}
|
|
853
|
-
// Share-price bounds only when a debt leg exists (helpers throw on zero inputs); else 0n.
|
|
854
|
-
// Derived from borrowAssetsAdjusted (the encoded value) so rounding can't push the on-chain
|
|
855
|
-
// ratio below a guard computed from the smaller projected amount.
|
|
856
|
-
const minBorrowSharePrice = shouldMigrateBorrow
|
|
857
|
-
? computeMinBorrowSharePrice({
|
|
858
|
-
borrowAmount: borrowAssetsAdjusted,
|
|
859
|
-
market: accruedTarget,
|
|
860
|
-
slippageTolerance,
|
|
861
|
-
})
|
|
862
|
-
: 0n;
|
|
863
|
-
const maxRepaySharePrice = shouldMigrateBorrow
|
|
864
|
-
? computeMaxRepaySharePrice({
|
|
865
|
-
repayAssets: requestedAssets,
|
|
866
|
-
repayShares: requestedShares,
|
|
867
|
-
market: accruedSource,
|
|
868
|
-
slippageTolerance,
|
|
869
|
-
})
|
|
870
|
-
: 0n;
|
|
622
|
+
const reallocations = [...(params.reallocations ?? [])];
|
|
623
|
+
const publicAllocations = getBlueBundlesV1PublicAllocations(reallocations, destination.marketParams);
|
|
624
|
+
const penaltyAssets = getBlueBundlesV1PenaltyAssets(publicAllocations);
|
|
625
|
+
const accrualTimestamp = this.getBlueBundlesV1QuoteTimestamp(MathLib.max(positionData.market.lastUpdate, destination.positionData.market.lastUpdate));
|
|
626
|
+
const accruedSourcePosition = positionData.accrueInterest(accrualTimestamp);
|
|
627
|
+
const accruedDestinationPosition = destination.positionData.accrueInterest(accrualTimestamp);
|
|
628
|
+
const referralFeeAssets = getBlueBundlesV1ReferralFeeAssets(accruedSourcePosition.borrowAssets, referralFeePct);
|
|
629
|
+
validatePositionHealth({
|
|
630
|
+
positionData: accruedDestinationPosition,
|
|
631
|
+
additionalCollateral: accruedSourcePosition.collateral,
|
|
632
|
+
borrowAmount: accruedSourcePosition.borrowAssets + penaltyAssets + referralFeeAssets,
|
|
633
|
+
marketId: destination.marketParams.id,
|
|
634
|
+
lltv: destination.marketParams.lltv,
|
|
635
|
+
});
|
|
871
636
|
return {
|
|
872
|
-
getRequirements:
|
|
873
|
-
const [penaltyRequirements, authTx] = await Promise.all([
|
|
874
|
-
this.getReallocationPenaltyRequirements(userAddress, targetReallocationPlan),
|
|
875
|
-
getBlueAuthorizationRequirement({
|
|
876
|
-
viemClient: this.client.viemClient,
|
|
877
|
-
chainId: this.chainId,
|
|
878
|
-
userAddress,
|
|
879
|
-
supportSignature: this.client.options.supportSignature,
|
|
880
|
-
}),
|
|
881
|
-
]);
|
|
882
|
-
return [...penaltyRequirements, ...(authTx ? [authTx] : [])];
|
|
883
|
-
},
|
|
637
|
+
getRequirements: () => this.getAuthorizationRequirements({ userAddress, deadline }),
|
|
884
638
|
buildTx: (signatures) => {
|
|
885
|
-
const { authorization } =
|
|
639
|
+
const { authorization } = selectBlueBundlesV1RequirementSignatures(signatures, {
|
|
886
640
|
authorization: true,
|
|
887
641
|
});
|
|
888
642
|
return blueRefinance({
|
|
889
|
-
|
|
643
|
+
market: {
|
|
890
644
|
chainId: this.chainId,
|
|
891
|
-
|
|
645
|
+
sourceMarketParams: this.marketParams,
|
|
646
|
+
destinationMarketParams: destination.marketParams,
|
|
892
647
|
},
|
|
893
|
-
target: { marketParams: target.marketParams },
|
|
894
648
|
args: {
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
targetReallocations: targetReallocationList,
|
|
649
|
+
userAddress,
|
|
650
|
+
maxLtv: this.getMaxLtv(destination.marketParams),
|
|
651
|
+
reallocations,
|
|
652
|
+
deadline,
|
|
653
|
+
referralFeePct,
|
|
654
|
+
referralFeeRecipient,
|
|
902
655
|
authorizationSignature: authorization,
|
|
903
656
|
},
|
|
904
657
|
metadata: this.client.options.metadata,
|
|
@@ -1110,7 +863,7 @@ export class MorphoBlue {
|
|
|
1110
863
|
* @param params.amount - The borrow or withdraw amount used to compute the post-state utilization.
|
|
1111
864
|
* @param params.borrowAmount - {@deprecated Pass `{ operation: "borrow", amount }` instead.}
|
|
1112
865
|
* @param params.options - Optional allocator and utilization options.
|
|
1113
|
-
* @returns Vault reallocations
|
|
866
|
+
* @returns Vault V1 reallocations for explicit low-level Bundler3 composition.
|
|
1114
867
|
* @throws {ChainIdMismatchError} when `reallocationData` belongs to a different chain than this market.
|
|
1115
868
|
* @throws {InsufficientSharedLiquidityError} when shared liquidity cannot cover the operation's absolute shortfall on the target market.
|
|
1116
869
|
* @throws {ReallocationWithdrawExceedsMarketSupplyError} when `operation === "withdraw"` and `amount` exceeds the target market's `totalSupplyAssets`.
|
|
@@ -1156,7 +909,7 @@ export class MorphoBlue {
|
|
|
1156
909
|
* @param params.amount - The borrow or withdraw amount used to compute post-state utilization.
|
|
1157
910
|
* @param params.borrowAmount - Deprecated borrow amount alias.
|
|
1158
911
|
* @param params.options - Optional allocator and utilization options.
|
|
1159
|
-
* @returns Vault V1 reallocations
|
|
912
|
+
* @returns Vault V1 reallocations for explicit low-level Bundler3 composition.
|
|
1160
913
|
* @throws {ChainIdMismatchError} when `reallocationData` belongs to another chain.
|
|
1161
914
|
* @throws {InsufficientSharedLiquidityError} when shared liquidity cannot cover the operation.
|
|
1162
915
|
* @throws {ReallocationWithdrawExceedsMarketSupplyError} when a withdrawal exceeds market supply.
|