@morpho-org/morpho-sdk 5.9.0 → 6.0.0-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/MIGRATION-v5-to-v6.md +201 -0
- package/README.md +58 -40
- 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 +99 -0
- package/lib/cjs/actions/blue/common.js +318 -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/index.d.ts +1 -1
- 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/getBlueBundlesV1TokenRequirements.d.ts +80 -0
- package/lib/cjs/actions/requirements/blue/getBlueBundlesV1TokenRequirements.js +207 -0
- 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 +2 -0
- package/lib/cjs/actions/requirements/blue/index.js +2 -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 +7 -2
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +16 -7
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +33 -6
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2TransferFrom.d.ts +61 -0
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2TransferFrom.js +123 -0
- 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 +5 -4
- package/lib/cjs/actions/requirements/getRequirementsApproval.js +5 -4
- package/lib/cjs/actions/signatures/getTokenRequirementActions.d.ts +1 -0
- package/lib/cjs/actions/signatures/getTokenRequirementActions.js +4 -0
- 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 +447 -696
- package/lib/cjs/entities/index.d.ts +1 -0
- package/lib/cjs/helpers/computeVaultV1Reallocations.d.ts +3 -9
- package/lib/cjs/helpers/computeVaultV1Reallocations.js +3 -9
- package/lib/cjs/helpers/validate.d.ts +14 -23
- package/lib/cjs/helpers/validate.js +30 -48
- package/lib/cjs/helpers/validateRequirementSpender.d.ts +2 -1
- package/lib/cjs/helpers/validateRequirementSpender.js +2 -0
- package/lib/cjs/types/action.d.ts +146 -162
- package/lib/cjs/types/action.js +29 -2
- package/lib/cjs/types/client.d.ts +9 -0
- package/lib/cjs/types/error.d.ts +141 -14
- package/lib/cjs/types/error.js +198 -21
- package/lib/cjs/types/sharedLiquidity.d.ts +0 -7
- 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 +99 -0
- package/lib/esm/actions/blue/common.js +306 -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/index.d.ts +1 -1
- 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/getBlueBundlesV1TokenRequirements.d.ts +80 -0
- package/lib/esm/actions/requirements/blue/getBlueBundlesV1TokenRequirements.js +203 -0
- 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 +2 -0
- package/lib/esm/actions/requirements/blue/index.js +2 -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 +8 -3
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +16 -7
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +34 -7
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2TransferFrom.d.ts +61 -0
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2TransferFrom.js +119 -0
- 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 +5 -4
- package/lib/esm/actions/requirements/getRequirementsApproval.js +5 -4
- package/lib/esm/actions/signatures/getTokenRequirementActions.d.ts +1 -0
- package/lib/esm/actions/signatures/getTokenRequirementActions.js +5 -1
- 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 +453 -702
- package/lib/esm/entities/index.d.ts +1 -0
- package/lib/esm/helpers/computeVaultV1Reallocations.d.ts +3 -9
- package/lib/esm/helpers/computeVaultV1Reallocations.js +3 -9
- package/lib/esm/helpers/validate.d.ts +14 -23
- package/lib/esm/helpers/validate.js +30 -48
- package/lib/esm/helpers/validateRequirementSpender.d.ts +2 -1
- package/lib/esm/helpers/validateRequirementSpender.js +2 -0
- package/lib/esm/types/action.d.ts +146 -162
- package/lib/esm/types/action.js +28 -2
- package/lib/esm/types/client.d.ts +9 -0
- package/lib/esm/types/error.d.ts +141 -14
- package/lib/esm/types/error.js +184 -17
- package/lib/esm/types/sharedLiquidity.d.ts +0 -7
- package/package.json +4 -3
- 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,167 +1,178 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import { NonPositiveInputError, } from "../../types/index.js";
|
|
7
|
-
import { getBlueAuthorizationAction } from "../signatures/getBlueAuthorizationAction.js";
|
|
8
|
-
import { buildAssetFundingActions } from "./buildAssetFundingActions.js";
|
|
9
|
-
import { resolveRepayFunding } from "./resolveRepayFunding.js";
|
|
1
|
+
import { encodeFunctionData } from "viem";
|
|
2
|
+
import { blueBundlesV1Abi } from "../../abis.js";
|
|
3
|
+
import { validateUint256Field } from "../../helpers/validate.js";
|
|
4
|
+
import { InputExceedsMaxError, MaxRepayAssetsBelowRepayAssetsError, MutuallyExclusiveRepayAmountsError, NonPositiveInputError, UnexpectedRequirementSignatureError, } from "../../types/index.js";
|
|
5
|
+
import { finalizeBlueBundlesV1Transaction, getBlueBundlesV1ReferralFeeAssets, getBlueBundlesV1SignedAuthorization, getBlueBundlesV1TokenPermit, normalizeBlueBundlesV1CommonParams, validateBlueBundlesV1NativeFunding, } from "./common.js";
|
|
10
6
|
/**
|
|
11
|
-
*
|
|
7
|
+
* Encodes a direct BlueBundlesV1 repay and/or collateral-withdrawal transaction.
|
|
12
8
|
*
|
|
13
|
-
*
|
|
9
|
+
* `maxRepayAssets` funds the live repayment plus fee and BlueBundlesV1 refunds the unused amount.
|
|
10
|
+
* A saturated `repayShares = maxUint256` closes the live remaining debt. This route has no
|
|
11
|
+
* Bundler3 share-price bound or `slippageTolerance` input.
|
|
14
12
|
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
* `
|
|
33
|
-
* `
|
|
34
|
-
*
|
|
35
|
-
* @
|
|
36
|
-
* @
|
|
37
|
-
* @
|
|
38
|
-
* @
|
|
39
|
-
* @
|
|
40
|
-
*
|
|
41
|
-
* @
|
|
42
|
-
*
|
|
43
|
-
* @
|
|
44
|
-
*
|
|
45
|
-
* @
|
|
46
|
-
* @
|
|
47
|
-
*
|
|
48
|
-
* @
|
|
49
|
-
*
|
|
50
|
-
* @param params.args.requirementSignature - Optional pre-signed permit/permit2 approval for the
|
|
51
|
-
* loan-token transfer.
|
|
52
|
-
* @param params.args.authorizationSignature - Optional signed Morpho authorization; when present,
|
|
53
|
-
* a `setAuthorizationWithSig` call is prepended to the bundle.
|
|
54
|
-
* @param params.metadata - Optional analytics metadata attached to the bundle.
|
|
55
|
-
* @returns A deep-frozen `Transaction<BlueRepayWithdrawCollateralAction>` with `to`,
|
|
56
|
-
* `value` (= `nativeAmount`), `data`, and the typed `action` discriminator the simulation layer consumes.
|
|
57
|
-
* @throws {NonPositiveInputError} when `maxSharePrice <= 0n`, the total funding is zero, or
|
|
58
|
-
* `withdrawAmount <= 0n`.
|
|
59
|
-
* @throws {NegativeInputError} when `amount`, `shares`, `nativeAmount`, or `transferAmount` is negative.
|
|
60
|
-
* @throws {MutuallyExclusiveRepayAmountsError} when both `amount` and `shares` are `> 0n`.
|
|
61
|
-
* @throws {TransferAmountNotEqualToAssetsError} when in assets mode and
|
|
62
|
-
* `transferAmount !== amount + nativeAmount`.
|
|
63
|
-
* @throws {ChainWNativeMissingError} when `nativeAmount > 0n` but the chain has no configured wNative.
|
|
64
|
-
* @throws {NativeAmountOnNonWNativeAssetError} when `nativeAmount > 0n` but the loan token is not
|
|
65
|
-
* the chain's wNative.
|
|
66
|
-
* @throws {DepositAssetMismatchError} from `getTokenRequirementActions` when `requirementSignature`
|
|
67
|
-
* is provided and the signed asset differs from `marketParams.loanToken`.
|
|
68
|
-
* @throws {DepositAmountMismatchError} from `getTokenRequirementActions` when `requirementSignature`
|
|
69
|
-
* is provided and the signed amount differs from the ERC-20 amount pulled.
|
|
70
|
-
* @throws {Permit2ExpirationMissingError} from `getTokenRequirementActions` when a Permit2 requirement
|
|
71
|
-
* signature is missing its expiration.
|
|
13
|
+
* @param params - Combined-operation encoding parameters.
|
|
14
|
+
* @param params.market.chainId - Chain containing BlueBundlesV1.
|
|
15
|
+
* @param params.market.marketParams - Scoped Morpho Blue market parameters.
|
|
16
|
+
* @param params.args.userAddress - User whose position is changed.
|
|
17
|
+
* @param params.args.repayAssets - Exact assets repaid, exclusive with shares.
|
|
18
|
+
* @param params.args.repayShares - Exact shares or `maxUint256` for full repay.
|
|
19
|
+
* @param params.args.maxRepayAssets - Gross repayment and fee funding cap.
|
|
20
|
+
* @param params.args.collateralAssets - Collateral withdrawn, or zero.
|
|
21
|
+
* @param params.args.maxLtv - Maximum post-operation LTV.
|
|
22
|
+
* @param params.args.nativeAmount - Exclusive native repay funding.
|
|
23
|
+
* @param params.args.deadline - Final call deadline in Unix seconds.
|
|
24
|
+
* @param params.args.referralFeePct - Optional WAD-scaled fee below 100%.
|
|
25
|
+
* @param params.args.referralFeeRecipient - Recipient required for a positive fee.
|
|
26
|
+
* @param params.args.requirementSignature - Optional loan-token signature.
|
|
27
|
+
* @param params.args.authorizationSignature - Optional Blue authorization signature.
|
|
28
|
+
* @param params.metadata - Optional transaction metadata.
|
|
29
|
+
* @returns A deep-frozen `Readonly<Transaction<BlueRepayWithdrawCollateralAction>>`
|
|
30
|
+
* whose `to` address is BlueBundlesV1 and whose `action` records the normalized inputs.
|
|
31
|
+
* @throws {NegativeInputError} when an amount, `maxLtv`, native value, or fee is negative.
|
|
32
|
+
* @throws {MutuallyExclusiveRepayAmountsError} when assets and shares are both nonzero.
|
|
33
|
+
* @throws {NonPositiveInputError} when both operation legs are zero, repay funding is absent, or the deadline is invalid.
|
|
34
|
+
* @throws {MaxRepayAssetsBelowRepayAssetsError} when exact-asset funding cannot cover the repayment and referral fee.
|
|
35
|
+
* @throws {InputExceedsMaxError} when the referral fee is at least WAD.
|
|
36
|
+
* @throws {MissingReferralFeeRecipientError} when a positive fee has no recipient.
|
|
37
|
+
* @throws {NativeFundingAmountMismatchError} when native funding is partial or mixed.
|
|
38
|
+
* @throws {ChainWNativeMissingError} when native funding is requested on a chain without wNative.
|
|
39
|
+
* @throws {NativeAmountOnNonWNativeAssetError} when native funding targets another token.
|
|
40
|
+
* @throws {UnexpectedRequirementSignatureError} when a signature is supplied for an inactive leg.
|
|
41
|
+
* @throws {DepositOwnerMismatchError} when a signed owner differs from `userAddress`.
|
|
42
|
+
* @throws {DepositAssetMismatchError} when the signed asset differs from the loan token.
|
|
43
|
+
* @throws {DepositAmountMismatchError} when the signed amount differs from `maxRepayAssets`.
|
|
44
|
+
* @throws {DepositSpenderMismatchError} when the signed spender is not BlueBundlesV1.
|
|
45
|
+
* @throws {BlueBundlesV1RequirementSignatureMismatchError} when a signature cannot be bound safely.
|
|
46
|
+
* @throws {UnsupportedChainIdError} when the chain is absent from the registry.
|
|
47
|
+
* @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
|
|
72
48
|
* @example
|
|
73
49
|
* ```ts
|
|
50
|
+
* import { markets } from "@morpho-org/morpho-test";
|
|
74
51
|
* import { blueRepayWithdrawCollateral } from "@morpho-org/morpho-sdk";
|
|
52
|
+
* import { maxUint256, zeroAddress } from "viem";
|
|
53
|
+
* import { mainnet } from "viem/chains";
|
|
75
54
|
*
|
|
55
|
+
* const marketParams = markets[mainnet.id].usdc_wbtc;
|
|
76
56
|
* const tx = blueRepayWithdrawCollateral({
|
|
77
|
-
* market: { chainId:
|
|
57
|
+
* market: { chainId: mainnet.id, marketParams },
|
|
78
58
|
* args: {
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
59
|
+
* userAddress: zeroAddress,
|
|
60
|
+
* repayAssets: 1_000_000n,
|
|
61
|
+
* repayShares: 0n,
|
|
62
|
+
* maxRepayAssets: 1_000_000n,
|
|
63
|
+
* collateralAssets: 0n,
|
|
64
|
+
* maxLtv: maxUint256,
|
|
65
|
+
* deadline: 1_900_000_000n,
|
|
86
66
|
* },
|
|
87
67
|
* });
|
|
88
|
-
* // tx
|
|
68
|
+
* // tx satisfies Readonly<Transaction<BlueRepayWithdrawCollateralAction>>
|
|
89
69
|
* ```
|
|
90
70
|
*/
|
|
91
|
-
export const blueRepayWithdrawCollateral = (
|
|
92
|
-
const {
|
|
93
|
-
const {
|
|
94
|
-
|
|
95
|
-
|
|
71
|
+
export const blueRepayWithdrawCollateral = (params) => {
|
|
72
|
+
const { chainId, marketParams } = params.market;
|
|
73
|
+
const { userAddress, repayAssets, repayShares, maxRepayAssets, collateralAssets, maxLtv, requirementSignature, authorizationSignature, } = params.args;
|
|
74
|
+
for (const [field, value] of [
|
|
75
|
+
["repayAssets", repayAssets],
|
|
76
|
+
["repayShares", repayShares],
|
|
77
|
+
["maxRepayAssets", maxRepayAssets],
|
|
78
|
+
["collateralAssets", collateralAssets],
|
|
79
|
+
["maxLtv", maxLtv],
|
|
80
|
+
]) {
|
|
81
|
+
validateUint256Field(field, value);
|
|
82
|
+
}
|
|
83
|
+
if (repayAssets > 0n && repayShares > 0n) {
|
|
84
|
+
throw new MutuallyExclusiveRepayAmountsError(marketParams.id);
|
|
85
|
+
}
|
|
86
|
+
const hasRepay = repayAssets > 0n || repayShares > 0n;
|
|
87
|
+
if (!hasRepay && collateralAssets === 0n) {
|
|
88
|
+
throw new NonPositiveInputError("repayAssets, repayShares, or collateralAssets", 0n);
|
|
89
|
+
}
|
|
90
|
+
if (hasRepay && maxRepayAssets === 0n) {
|
|
91
|
+
throw new NonPositiveInputError("maxRepayAssets", maxRepayAssets);
|
|
96
92
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
93
|
+
if (!hasRepay && maxRepayAssets > 0n) {
|
|
94
|
+
// Withdrawal-only has no repay leg to fund: reject stray funding that would encode a phantom
|
|
95
|
+
// repay. Native funding is transitively rejected — `validateBlueBundlesV1NativeFunding`
|
|
96
|
+
// requires `nativeAmount === maxRepayAssets` (= 0).
|
|
97
|
+
throw new InputExceedsMaxError({
|
|
98
|
+
field: "maxRepayAssets",
|
|
99
|
+
value: maxRepayAssets,
|
|
100
|
+
max: 0n,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
if (!hasRepay && requirementSignature != null) {
|
|
104
|
+
throw new UnexpectedRequirementSignatureError(requirementSignature.action.type);
|
|
105
|
+
}
|
|
106
|
+
if (collateralAssets === 0n && authorizationSignature != null) {
|
|
107
|
+
throw new UnexpectedRequirementSignatureError("authorization");
|
|
108
|
+
}
|
|
109
|
+
const common = {
|
|
110
|
+
chainId,
|
|
111
|
+
userAddress,
|
|
112
|
+
deadline: params.args.deadline,
|
|
113
|
+
referralFeePct: params.args.referralFeePct,
|
|
114
|
+
referralFeeRecipient: params.args.referralFeeRecipient,
|
|
115
|
+
metadata: params.metadata,
|
|
116
|
+
};
|
|
117
|
+
const { referralFeePct, referralFeeRecipient } = normalizeBlueBundlesV1CommonParams(common);
|
|
118
|
+
const minimumRepayAssets = repayAssets +
|
|
119
|
+
getBlueBundlesV1ReferralFeeAssets(repayAssets, referralFeePct);
|
|
120
|
+
if (minimumRepayAssets > maxRepayAssets) {
|
|
121
|
+
throw new MaxRepayAssetsBelowRepayAssetsError(maxRepayAssets, minimumRepayAssets);
|
|
102
122
|
}
|
|
103
|
-
|
|
104
|
-
actions.push(...buildAssetFundingActions({
|
|
123
|
+
const value = validateBlueBundlesV1NativeFunding({
|
|
105
124
|
chainId,
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
nativeAmount,
|
|
125
|
+
token: marketParams.loanToken,
|
|
126
|
+
fundedAmount: maxRepayAssets,
|
|
127
|
+
nativeAmount: params.args.nativeAmount,
|
|
109
128
|
requirementSignature,
|
|
110
|
-
}));
|
|
111
|
-
// REPAY FIRST — reduces debt before withdrawing collateral
|
|
112
|
-
actions.push({
|
|
113
|
-
type: "morphoRepay",
|
|
114
|
-
args: [
|
|
115
|
-
marketParams,
|
|
116
|
-
repayAssets,
|
|
117
|
-
repayShares,
|
|
118
|
-
maxSharePrice,
|
|
119
|
-
onBehalf,
|
|
120
|
-
[],
|
|
121
|
-
false,
|
|
122
|
-
],
|
|
123
129
|
});
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
+
const loanTokenPermit = getBlueBundlesV1TokenPermit({
|
|
131
|
+
chainId,
|
|
132
|
+
userAddress,
|
|
133
|
+
token: marketParams.loanToken,
|
|
134
|
+
amount: maxRepayAssets,
|
|
135
|
+
requirementSignature,
|
|
136
|
+
});
|
|
137
|
+
const signedAuthorization = getBlueBundlesV1SignedAuthorization({
|
|
138
|
+
chainId,
|
|
139
|
+
userAddress,
|
|
140
|
+
authorizationSignature,
|
|
141
|
+
});
|
|
142
|
+
return finalizeBlueBundlesV1Transaction({
|
|
143
|
+
common,
|
|
144
|
+
value,
|
|
145
|
+
data: encodeFunctionData({
|
|
146
|
+
abi: blueBundlesV1Abi,
|
|
147
|
+
functionName: "blueBundlesV1RepayAndWithdrawCollateral",
|
|
130
148
|
args: [
|
|
131
|
-
marketParams
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
149
|
+
marketParams,
|
|
150
|
+
repayAssets,
|
|
151
|
+
repayShares,
|
|
152
|
+
maxRepayAssets,
|
|
153
|
+
collateralAssets,
|
|
154
|
+
maxLtv,
|
|
155
|
+
loanTokenPermit,
|
|
156
|
+
signedAuthorization,
|
|
157
|
+
referralFeePct,
|
|
158
|
+
referralFeeRecipient,
|
|
159
|
+
params.args.deadline,
|
|
136
160
|
],
|
|
137
|
-
})
|
|
138
|
-
}
|
|
139
|
-
actions.push({
|
|
140
|
-
type: "morphoWithdrawCollateral",
|
|
141
|
-
args: [marketParams, withdrawAmount, receiver, false],
|
|
142
|
-
});
|
|
143
|
-
let tx = {
|
|
144
|
-
...BundlerAction.encodeBundle(chainId, actions),
|
|
145
|
-
value: nativeAmount,
|
|
146
|
-
};
|
|
147
|
-
if (metadata) {
|
|
148
|
-
tx = addTransactionMetadata(tx, metadata);
|
|
149
|
-
}
|
|
150
|
-
return deepFreeze({
|
|
151
|
-
...tx,
|
|
161
|
+
}),
|
|
152
162
|
action: {
|
|
153
163
|
type: "blueRepayWithdrawCollateral",
|
|
154
164
|
args: {
|
|
155
165
|
market: marketParams.id,
|
|
156
166
|
repayAssets,
|
|
157
167
|
repayShares,
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
168
|
+
maxRepayAssets,
|
|
169
|
+
collateralAssets,
|
|
170
|
+
maxLtv,
|
|
171
|
+
onBehalf: userAddress,
|
|
172
|
+
nativeAmount: value > 0n ? value : undefined,
|
|
173
|
+
referralFeePct,
|
|
174
|
+
referralFeeRecipient,
|
|
175
|
+
deadline: params.args.deadline,
|
|
165
176
|
},
|
|
166
177
|
},
|
|
167
178
|
});
|
|
@@ -1,67 +1,84 @@
|
|
|
1
1
|
import type { MarketParams } from "@morpho-org/blue-sdk";
|
|
2
|
-
import type
|
|
3
|
-
import {
|
|
2
|
+
import { type Address } from "viem";
|
|
3
|
+
import type { BlueBundlesV1TokenRequirementSignature, BlueSupplyAction, Metadata, Transaction } from "../../types/index.js";
|
|
4
4
|
/** Parameters for {@link blueSupply}. */
|
|
5
5
|
export interface BlueSupplyParams {
|
|
6
|
-
market
|
|
6
|
+
/** Chain and scoped Morpho Blue market. */
|
|
7
|
+
readonly market: {
|
|
7
8
|
readonly chainId: number;
|
|
8
9
|
readonly marketParams: MarketParams;
|
|
9
10
|
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
/** Direct BlueBundlesV1 supply arguments. */
|
|
12
|
+
readonly args: {
|
|
13
|
+
/** User funding and receiving the supply position. */
|
|
14
|
+
readonly userAddress: Address;
|
|
15
|
+
/** Gross loan-token assets funded before any referral fee. */
|
|
16
|
+
readonly assets: bigint;
|
|
17
|
+
/** Full native funding amount; must equal `assets` and requires a wNative loan token. */
|
|
18
|
+
readonly nativeAmount?: bigint;
|
|
19
|
+
/** Final call deadline in Unix seconds. */
|
|
20
|
+
readonly deadline: bigint;
|
|
21
|
+
/** Optional WAD-scaled referral fee, strictly below 100%. */
|
|
22
|
+
readonly referralFeePct?: bigint;
|
|
23
|
+
/** Recipient required when `referralFeePct` is positive. */
|
|
24
|
+
readonly referralFeeRecipient?: Address;
|
|
25
|
+
/** Optional ERC-2612 or Permit2 SignatureTransfer requirement result. */
|
|
26
|
+
readonly requirementSignature?: BlueBundlesV1TokenRequirementSignature;
|
|
17
27
|
};
|
|
18
|
-
metadata
|
|
28
|
+
/** Optional transaction metadata suffix. */
|
|
29
|
+
readonly metadata?: Metadata;
|
|
19
30
|
}
|
|
20
31
|
/**
|
|
21
|
-
*
|
|
32
|
+
* Encodes a direct BlueBundlesV1 loan-asset supply transaction.
|
|
22
33
|
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
* wNative for that path. Uses `maxSharePrice` to protect against share-price inflation between
|
|
26
|
-
* transaction construction and execution.
|
|
34
|
+
* `assets` is the gross funded amount; BlueBundlesV1 deducts the referral fee before supplying.
|
|
35
|
+
* This route has no Bundler3 share-price bound or `slippageTolerance` input.
|
|
27
36
|
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
* @param params.market.
|
|
31
|
-
* @param params.
|
|
32
|
-
* @param params.args.
|
|
33
|
-
*
|
|
34
|
-
* @param params.args.
|
|
35
|
-
* @param params.args.
|
|
36
|
-
*
|
|
37
|
-
* @param params.args.
|
|
38
|
-
*
|
|
39
|
-
* @
|
|
40
|
-
*
|
|
41
|
-
* @
|
|
42
|
-
*
|
|
43
|
-
* @throws {
|
|
44
|
-
* @throws {
|
|
45
|
-
* @throws {
|
|
46
|
-
* @throws {
|
|
47
|
-
*
|
|
48
|
-
* @throws {
|
|
49
|
-
*
|
|
50
|
-
* @throws {
|
|
51
|
-
*
|
|
37
|
+
* @param params - Supply encoding parameters.
|
|
38
|
+
* @param params.market.chainId - Chain containing the BlueBundlesV1 deployment.
|
|
39
|
+
* @param params.market.marketParams - Scoped Morpho Blue market parameters.
|
|
40
|
+
* @param params.args.userAddress - User funding and receiving the position.
|
|
41
|
+
* @param params.args.assets - Gross loan-token funding amount.
|
|
42
|
+
* @param params.args.nativeAmount - Exclusive native funding; must equal `assets`.
|
|
43
|
+
* @param params.args.deadline - Final call deadline in Unix seconds.
|
|
44
|
+
* @param params.args.referralFeePct - Optional WAD-scaled fee below 100%.
|
|
45
|
+
* @param params.args.referralFeeRecipient - Recipient required for a positive fee.
|
|
46
|
+
* @param params.args.requirementSignature - Optional ERC-2612 or Permit2 SignatureTransfer result.
|
|
47
|
+
* @param params.metadata - Optional transaction metadata.
|
|
48
|
+
* @returns A deep-frozen `Readonly<Transaction<BlueSupplyAction>>` whose `to` address is
|
|
49
|
+
* BlueBundlesV1 and whose `action` records the normalized supply inputs.
|
|
50
|
+
* @throws {NonPositiveInputError} when `assets` or `deadline` is not positive.
|
|
51
|
+
* @throws {NegativeInputError} when `nativeAmount` or `referralFeePct` is negative.
|
|
52
|
+
* @throws {InputExceedsMaxError} when `assets` exceeds `uint256` or `referralFeePct` is at least WAD.
|
|
53
|
+
* @throws {MissingReferralFeeRecipientError} when a positive fee has no recipient.
|
|
54
|
+
* @throws {NativeFundingAmountMismatchError} when native funding does not equal `assets`.
|
|
55
|
+
* @throws {ChainWNativeMissingError} when native funding is requested on a chain without wNative.
|
|
56
|
+
* @throws {NativeAmountOnNonWNativeAssetError} when native funding targets another token.
|
|
57
|
+
* @throws {UnexpectedRequirementSignatureError} when native funding also supplies a token signature.
|
|
58
|
+
* @throws {DepositOwnerMismatchError} when the signed owner differs from `userAddress`.
|
|
59
|
+
* @throws {DepositAssetMismatchError} when the signed asset differs from the loan token.
|
|
60
|
+
* @throws {DepositAmountMismatchError} when the signed amount differs from `assets`.
|
|
61
|
+
* @throws {DepositSpenderMismatchError} when the signed spender is not BlueBundlesV1.
|
|
62
|
+
* @throws {BlueBundlesV1RequirementSignatureMismatchError} when the signature kind or encoding is invalid.
|
|
63
|
+
* @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
|
|
64
|
+
* @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
|
|
52
65
|
* @example
|
|
53
66
|
* ```ts
|
|
67
|
+
* import { markets } from "@morpho-org/morpho-test";
|
|
54
68
|
* import { blueSupply } from "@morpho-org/morpho-sdk";
|
|
69
|
+
* import { zeroAddress } from "viem";
|
|
70
|
+
* import { mainnet } from "viem/chains";
|
|
55
71
|
*
|
|
72
|
+
* const marketParams = markets[mainnet.id].usdc_wbtc;
|
|
56
73
|
* const tx = blueSupply({
|
|
57
|
-
* market: { chainId:
|
|
74
|
+
* market: { chainId: mainnet.id, marketParams },
|
|
58
75
|
* args: {
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
76
|
+
* userAddress: zeroAddress,
|
|
77
|
+
* assets: 1_000_000n,
|
|
78
|
+
* deadline: 1_900_000_000n,
|
|
62
79
|
* },
|
|
63
80
|
* });
|
|
64
81
|
* // tx satisfies Readonly<Transaction<BlueSupplyAction>>
|
|
65
82
|
* ```
|
|
66
83
|
*/
|
|
67
|
-
export declare const blueSupply: (
|
|
84
|
+
export declare const blueSupply: (params: BlueSupplyParams) => Readonly<Transaction<BlueSupplyAction>>;
|