@morpho-org/morpho-sdk 4.2.0 → 5.1.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/README.md +16 -16
- package/lib/cjs/actions/blue/borrow.d.ts +12 -2
- package/lib/cjs/actions/blue/borrow.js +7 -1
- package/lib/cjs/actions/blue/buildAssetFundingActions.d.ts +47 -0
- package/lib/cjs/actions/blue/buildAssetFundingActions.js +63 -0
- package/lib/cjs/actions/blue/refinance.d.ts +13 -3
- package/lib/cjs/actions/blue/refinance.js +8 -2
- package/lib/cjs/actions/blue/repay.d.ts +44 -43
- package/lib/cjs/actions/blue/repay.js +70 -59
- package/lib/cjs/actions/blue/repayWithdrawCollateral.d.ts +50 -41
- package/lib/cjs/actions/blue/repayWithdrawCollateral.js +75 -54
- package/lib/cjs/actions/blue/resolveRepayFunding.d.ts +69 -0
- package/lib/cjs/actions/blue/resolveRepayFunding.js +82 -0
- package/lib/cjs/actions/blue/supply.d.ts +8 -8
- package/lib/cjs/actions/blue/supply.js +11 -35
- package/lib/cjs/actions/blue/supplyCollateral.d.ts +9 -9
- package/lib/cjs/actions/blue/supplyCollateral.js +12 -36
- package/lib/cjs/actions/blue/supplyCollateralBorrow.d.ts +19 -7
- package/lib/cjs/actions/blue/supplyCollateralBorrow.js +17 -32
- package/lib/cjs/actions/blue/withdraw.d.ts +10 -2
- package/lib/cjs/actions/blue/withdraw.js +7 -1
- package/lib/cjs/actions/index.d.ts +1 -0
- package/lib/cjs/actions/index.js +1 -0
- package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +49 -0
- package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +111 -0
- package/lib/cjs/actions/requirements/blue/index.d.ts +1 -0
- package/lib/cjs/actions/requirements/blue/index.js +17 -0
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +55 -0
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +89 -0
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +6 -5
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +17 -5
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +9 -7
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +18 -4
- package/lib/cjs/actions/requirements/encode/{encodeErc20Permit2.d.ts → encodeErc20Permit2Approve.d.ts} +11 -13
- package/lib/cjs/actions/requirements/encode/{encodeErc20Permit2.js → encodeErc20Permit2Approve.js} +10 -14
- package/lib/cjs/actions/requirements/encode/index.d.ts +2 -1
- package/lib/cjs/actions/requirements/encode/index.js +2 -1
- package/lib/cjs/actions/requirements/{getRequirements.d.ts → generalAdapter/getGeneralAdapterRequirements.d.ts} +34 -20
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirements.js +151 -0
- package/lib/{esm/actions/requirements/getRequirementsPermit.d.ts → cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts} +6 -14
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.js +50 -0
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.d.ts +57 -0
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.js +74 -0
- package/lib/cjs/actions/requirements/generalAdapter/index.d.ts +3 -0
- package/lib/cjs/actions/requirements/generalAdapter/index.js +19 -0
- package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +3 -2
- package/lib/cjs/actions/requirements/getRequirementsApproval.js +3 -2
- package/lib/cjs/actions/requirements/index.d.ts +2 -5
- package/lib/cjs/actions/requirements/index.js +2 -5
- package/lib/cjs/actions/signatures/getBlueAuthorizationAction.d.ts +29 -0
- package/lib/cjs/actions/signatures/getBlueAuthorizationAction.js +48 -0
- package/lib/{esm/actions/requirements/getRequirementsAction.d.ts → cjs/actions/signatures/getTokenRequirementActions.d.ts} +17 -17
- package/lib/cjs/actions/{requirements/getRequirementsAction.js → signatures/getTokenRequirementActions.js} +26 -13
- package/lib/cjs/actions/signatures/index.d.ts +2 -0
- package/lib/cjs/actions/signatures/index.js +18 -0
- package/lib/cjs/actions/vaultV1/deposit.d.ts +6 -8
- package/lib/cjs/actions/vaultV1/deposit.js +11 -21
- package/lib/cjs/actions/vaultV1/migrateToV2.d.ts +6 -6
- package/lib/cjs/actions/vaultV1/migrateToV2.js +12 -20
- package/lib/cjs/actions/vaultV2/deposit.d.ts +6 -8
- package/lib/cjs/actions/vaultV2/deposit.js +11 -21
- package/lib/cjs/addresses.d.ts +2 -2
- package/lib/cjs/addresses.js +2 -1
- package/lib/cjs/bundler/actions.d.ts +44 -4
- package/lib/cjs/bundler/actions.js +75 -2
- package/lib/cjs/bundler/types.d.ts +6 -0
- package/lib/cjs/constants.d.ts +2 -2
- package/lib/cjs/constants.js +3 -3
- package/lib/cjs/entities/blue/blue.d.ts +58 -33
- package/lib/cjs/entities/blue/blue.js +309 -189
- package/lib/cjs/entities/reallocationData.d.ts +0 -44
- package/lib/cjs/entities/reallocationData.js +3 -50
- package/lib/cjs/entities/vaultV1/vaultV1.d.ts +9 -9
- package/lib/cjs/entities/vaultV1/vaultV1.js +44 -34
- package/lib/cjs/entities/vaultV2/vaultV2.d.ts +6 -6
- package/lib/cjs/entities/vaultV2/vaultV2.js +21 -16
- package/lib/cjs/errors.d.ts +3 -2
- package/lib/cjs/errors.js +5 -2
- package/lib/cjs/helpers/index.d.ts +1 -1
- package/lib/cjs/helpers/index.js +1 -2
- package/lib/cjs/helpers/validate.d.ts +2 -20
- package/lib/cjs/helpers/validate.js +2 -39
- package/lib/cjs/types/action.d.ts +164 -12
- package/lib/cjs/types/action.js +79 -5
- package/lib/cjs/types/error.d.ts +97 -2
- package/lib/cjs/types/error.js +113 -7
- package/lib/cjs/types.d.ts +2 -1
- package/lib/cjs/utils.d.ts +2 -2
- package/lib/cjs/utils.js +2 -4
- package/lib/esm/actions/blue/borrow.d.ts +12 -2
- package/lib/esm/actions/blue/borrow.js +7 -1
- package/lib/esm/actions/blue/buildAssetFundingActions.d.ts +47 -0
- package/lib/esm/actions/blue/buildAssetFundingActions.js +59 -0
- package/lib/esm/actions/blue/refinance.d.ts +13 -3
- package/lib/esm/actions/blue/refinance.js +8 -2
- package/lib/esm/actions/blue/repay.d.ts +44 -43
- package/lib/esm/actions/blue/repay.js +71 -60
- package/lib/esm/actions/blue/repayWithdrawCollateral.d.ts +50 -41
- package/lib/esm/actions/blue/repayWithdrawCollateral.js +76 -55
- package/lib/esm/actions/blue/resolveRepayFunding.d.ts +69 -0
- package/lib/esm/actions/blue/resolveRepayFunding.js +78 -0
- package/lib/esm/actions/blue/supply.d.ts +8 -8
- package/lib/esm/actions/blue/supply.js +12 -36
- package/lib/esm/actions/blue/supplyCollateral.d.ts +9 -9
- package/lib/esm/actions/blue/supplyCollateral.js +13 -37
- package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +19 -7
- package/lib/esm/actions/blue/supplyCollateralBorrow.js +18 -33
- package/lib/esm/actions/blue/withdraw.d.ts +10 -2
- package/lib/esm/actions/blue/withdraw.js +7 -1
- package/lib/esm/actions/index.d.ts +1 -0
- package/lib/esm/actions/index.js +1 -0
- package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +49 -0
- package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +107 -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 +55 -0
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +85 -0
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +6 -5
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +19 -7
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +9 -7
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +20 -6
- package/lib/esm/actions/requirements/encode/{encodeErc20Permit2.d.ts → encodeErc20Permit2Approve.d.ts} +11 -13
- package/lib/esm/actions/requirements/encode/{encodeErc20Permit2.js → encodeErc20Permit2Approve.js} +8 -12
- package/lib/esm/actions/requirements/encode/index.d.ts +2 -1
- package/lib/esm/actions/requirements/encode/index.js +2 -1
- package/lib/esm/actions/requirements/{getRequirements.d.ts → generalAdapter/getGeneralAdapterRequirements.d.ts} +34 -20
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirements.js +147 -0
- package/lib/{cjs/actions/requirements/getRequirementsPermit.d.ts → esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts} +6 -14
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.js +46 -0
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.d.ts +57 -0
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.js +70 -0
- package/lib/esm/actions/requirements/generalAdapter/index.d.ts +3 -0
- package/lib/esm/actions/requirements/generalAdapter/index.js +3 -0
- package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +3 -2
- package/lib/esm/actions/requirements/getRequirementsApproval.js +3 -2
- package/lib/esm/actions/requirements/index.d.ts +2 -5
- package/lib/esm/actions/requirements/index.js +2 -5
- package/lib/esm/actions/signatures/getBlueAuthorizationAction.d.ts +29 -0
- package/lib/esm/actions/signatures/getBlueAuthorizationAction.js +44 -0
- package/lib/{cjs/actions/requirements/getRequirementsAction.d.ts → esm/actions/signatures/getTokenRequirementActions.d.ts} +17 -17
- package/lib/esm/actions/{requirements/getRequirementsAction.js → signatures/getTokenRequirementActions.js} +24 -11
- package/lib/esm/actions/signatures/index.d.ts +2 -0
- package/lib/esm/actions/signatures/index.js +2 -0
- package/lib/esm/actions/vaultV1/deposit.d.ts +6 -8
- package/lib/esm/actions/vaultV1/deposit.js +11 -21
- package/lib/esm/actions/vaultV1/migrateToV2.d.ts +6 -6
- package/lib/esm/actions/vaultV1/migrateToV2.js +12 -20
- package/lib/esm/actions/vaultV2/deposit.d.ts +6 -8
- package/lib/esm/actions/vaultV2/deposit.js +11 -21
- package/lib/esm/addresses.d.ts +2 -2
- package/lib/esm/addresses.js +1 -1
- package/lib/esm/bundler/actions.d.ts +44 -4
- package/lib/esm/bundler/actions.js +77 -4
- package/lib/esm/bundler/types.d.ts +6 -0
- package/lib/esm/constants.d.ts +2 -2
- package/lib/esm/constants.js +2 -2
- package/lib/esm/entities/blue/blue.d.ts +58 -33
- package/lib/esm/entities/blue/blue.js +311 -191
- package/lib/esm/entities/reallocationData.d.ts +0 -44
- package/lib/esm/entities/reallocationData.js +2 -49
- package/lib/esm/entities/vaultV1/vaultV1.d.ts +9 -9
- package/lib/esm/entities/vaultV1/vaultV1.js +46 -36
- package/lib/esm/entities/vaultV2/vaultV2.d.ts +6 -6
- package/lib/esm/entities/vaultV2/vaultV2.js +23 -18
- package/lib/esm/errors.d.ts +3 -2
- package/lib/esm/errors.js +2 -1
- package/lib/esm/helpers/index.d.ts +1 -1
- package/lib/esm/helpers/index.js +1 -1
- package/lib/esm/helpers/validate.d.ts +2 -20
- package/lib/esm/helpers/validate.js +2 -38
- package/lib/esm/types/action.d.ts +164 -12
- package/lib/esm/types/action.js +75 -4
- package/lib/esm/types/error.d.ts +97 -2
- package/lib/esm/types/error.js +107 -5
- package/lib/esm/types.d.ts +2 -1
- package/lib/esm/utils.d.ts +2 -2
- package/lib/esm/utils.js +2 -2
- package/package.json +22 -21
- package/lib/cjs/actions/requirements/getMorphoAuthorizationRequirement.d.ts +0 -37
- package/lib/cjs/actions/requirements/getMorphoAuthorizationRequirement.js +0 -71
- package/lib/cjs/actions/requirements/getRequirements.js +0 -112
- package/lib/cjs/actions/requirements/getRequirementsPermit.js +0 -57
- package/lib/cjs/actions/requirements/getRequirementsPermit2.d.ts +0 -63
- package/lib/cjs/actions/requirements/getRequirementsPermit2.js +0 -85
- package/lib/esm/actions/requirements/getMorphoAuthorizationRequirement.d.ts +0 -37
- package/lib/esm/actions/requirements/getMorphoAuthorizationRequirement.js +0 -67
- package/lib/esm/actions/requirements/getRequirements.js +0 -108
- package/lib/esm/actions/requirements/getRequirementsPermit.js +0 -53
- package/lib/esm/actions/requirements/getRequirementsPermit2.d.ts +0 -63
- package/lib/esm/actions/requirements/getRequirementsPermit2.js +0 -81
- package/src/index.ts +0 -4
|
@@ -2,29 +2,34 @@ import { getChainAddresses } from "@morpho-org/blue-sdk";
|
|
|
2
2
|
import { deepFreeze } from "@morpho-org/morpho-ts";
|
|
3
3
|
import { maxUint256 } from "viem";
|
|
4
4
|
import { BundlerAction } from "../../bundler/index.js";
|
|
5
|
-
import { addTransactionMetadata
|
|
6
|
-
import {
|
|
5
|
+
import { addTransactionMetadata } from "../../helpers/index.js";
|
|
6
|
+
import { buildAssetFundingActions } from "./buildAssetFundingActions.js";
|
|
7
|
+
import { resolveRepayFunding } from "./resolveRepayFunding.js";
|
|
7
8
|
/**
|
|
8
9
|
* Prepares a repay transaction for a Morpho Blue market.
|
|
9
10
|
*
|
|
10
|
-
* Routed through bundler3 via `GeneralAdapter1`.
|
|
11
|
+
* Routed through bundler3 via `GeneralAdapter1`. All amount arithmetic is done upstream (see
|
|
12
|
+
* `MorphoBlue.repay`); this builder just assembles the bundle from the pre-resolved
|
|
13
|
+
* {@link RepayActionAmountArgs}. The mode is discriminated on `shares`, plus optional native wrapping
|
|
14
|
+
* (when `nativeAmount > 0`, native ETH is wrapped via `GeneralAdapter1.wrapNative()` before the
|
|
15
|
+
* repay; the loan token must be the chain's wNative):
|
|
11
16
|
*
|
|
12
|
-
* - **
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
17
|
+
* - **assets mode** (`shares` unset/`0n`): repays `transferAmount` assets (`= amount + nativeAmount`,
|
|
18
|
+
* additive like `blueSupply`), pulling `amount` ERC-20 and wrapping `nativeAmount`. No residual.
|
|
19
|
+
* - **shares mode** (`shares > 0n`): repays exact shares (full repay), pulling `transferAmount`
|
|
20
|
+
* ERC-20 (already net of native) and wrapping `nativeAmount`. Residual loan tokens are skimmed
|
|
21
|
+
* back to `receiver` after the call.
|
|
16
22
|
*
|
|
17
|
-
*
|
|
18
|
-
* share price manipulation between transaction construction and execution.
|
|
23
|
+
* Uses `maxSharePrice` to protect against share price manipulation between construction and execution.
|
|
19
24
|
*
|
|
20
25
|
* @param params.market.chainId - The chain the market lives on.
|
|
21
26
|
* @param params.market.marketParams - Market params (loanToken, collateralToken, oracle, irm, lltv).
|
|
22
|
-
* @param params.args.
|
|
23
|
-
* @param params.args.shares - Repay amount in borrow shares.
|
|
24
|
-
* @param params.args.transferAmount -
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
27
|
+
* @param params.args.amount - (assets mode) ERC-20 loan tokens pulled from the payer. Defaults to `0n`.
|
|
28
|
+
* @param params.args.shares - (shares mode) Repay amount in borrow shares. Discriminates the mode.
|
|
29
|
+
* @param params.args.transferAmount - Loan tokens routed into `GeneralAdapter1`: assets mode = the
|
|
30
|
+
* total repaid (`amount + nativeAmount`); shares mode = the ERC-20 pulled (net of native).
|
|
31
|
+
* @param params.args.nativeAmount - Optional native token to wrap into wNative to fund the repay.
|
|
32
|
+
* Requires the loan token to be the chain's wNative.
|
|
28
33
|
* @param params.args.onBehalf - Address whose Morpho debt is being repaid.
|
|
29
34
|
* @param params.args.receiver - Address that receives residual loan tokens in shares mode.
|
|
30
35
|
* @param params.args.maxSharePrice - Maximum acceptable repay share price (in ray). Slippage
|
|
@@ -32,70 +37,71 @@ import { getRequirementsAction } from "../requirements/getRequirementsAction.js"
|
|
|
32
37
|
* @param params.args.requirementSignature - Optional pre-signed permit/permit2 approval for the
|
|
33
38
|
* loan-token transfer.
|
|
34
39
|
* @param params.metadata - Optional analytics metadata attached to the bundle.
|
|
35
|
-
* @returns A deep-frozen `Transaction<BlueRepayAction>` with `to`, `value
|
|
36
|
-
* typed `action` discriminator the simulation layer consumes.
|
|
40
|
+
* @returns A deep-frozen `Transaction<BlueRepayAction>` with `to`, `value` (= `nativeAmount`),
|
|
41
|
+
* `data`, and the typed `action` discriminator the simulation layer consumes.
|
|
37
42
|
* @throws {NonPositiveRepayMaxSharePriceError} when `maxSharePrice <= 0n`.
|
|
38
|
-
* @throws {
|
|
39
|
-
*
|
|
40
|
-
* @throws {
|
|
41
|
-
*
|
|
42
|
-
* @throws {
|
|
43
|
-
*
|
|
43
|
+
* @throws {NegativeNativeAmountError} when `nativeAmount < 0n`.
|
|
44
|
+
* @throws {MutuallyExclusiveRepayAmountsError} when both `amount` and `shares` are `> 0n`.
|
|
45
|
+
* @throws {TransferAmountNotEqualToAssetsError} when in assets mode and
|
|
46
|
+
* `transferAmount !== amount + nativeAmount`.
|
|
47
|
+
* @throws {NonPositiveRepayAmountError} when `amount` or `shares` is negative, when in assets mode
|
|
48
|
+
* and `transferAmount <= 0n`, or when in shares mode and `transferAmount` is negative or the total
|
|
49
|
+
* funding (`transferAmount + nativeAmount`) is `<= 0n`.
|
|
50
|
+
* @throws {ChainWNativeMissingError} when `nativeAmount > 0n` but the chain has no configured wNative.
|
|
51
|
+
* @throws {NativeAmountOnNonWNativeAssetError} when `nativeAmount > 0n` but the loan token is not
|
|
52
|
+
* the chain's wNative.
|
|
53
|
+
* @throws {DepositAssetMismatchError} from `getTokenRequirementActions` when `requirementSignature`
|
|
44
54
|
* is provided and the signed asset differs from `marketParams.loanToken`.
|
|
45
|
-
* @throws {DepositAmountMismatchError} from `
|
|
46
|
-
* is provided and the signed amount differs from
|
|
47
|
-
* @throws {Permit2ExpirationMissingError} from `
|
|
55
|
+
* @throws {DepositAmountMismatchError} from `getTokenRequirementActions` when `requirementSignature`
|
|
56
|
+
* is provided and the signed amount differs from the ERC-20 amount pulled.
|
|
57
|
+
* @throws {Permit2ExpirationMissingError} from `getTokenRequirementActions` when a Permit2 requirement
|
|
48
58
|
* signature is missing its expiration.
|
|
49
59
|
* @example
|
|
50
60
|
* ```ts
|
|
51
61
|
* import { blueRepay } from "@morpho-org/morpho-sdk";
|
|
52
62
|
*
|
|
63
|
+
* // Repay 0.5 loan-asset units, 0.2 of them funded by wrapping native ETH.
|
|
53
64
|
* const tx = blueRepay({
|
|
54
|
-
* market: { chainId: 1, marketParams },
|
|
65
|
+
* market: { chainId: 1, marketParams }, // marketParams.loanToken === wNative
|
|
55
66
|
* args: {
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
* transferAmount:
|
|
67
|
+
* amount: 300_000_000_000_000_000n, // ERC-20 part
|
|
68
|
+
* nativeAmount: 200_000_000_000_000_000n, // wrapped ETH part
|
|
69
|
+
* transferAmount: 500_000_000_000_000_000n, // total repaid = amount + nativeAmount
|
|
59
70
|
* onBehalf: borrower,
|
|
60
71
|
* receiver: borrower,
|
|
61
72
|
* maxSharePrice: 1_010_000_000_000_000_000_000_000_000n, // RAY-scaled, 1.01x
|
|
62
73
|
* },
|
|
63
74
|
* });
|
|
64
|
-
* // tx
|
|
75
|
+
* // tx.value === 200_000_000_000_000_000n
|
|
65
76
|
* ```
|
|
66
77
|
*/
|
|
67
|
-
export const blueRepay = ({ market: { chainId, marketParams }, args
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
shares,
|
|
71
|
-
transferAmount,
|
|
72
|
-
maxSharePrice,
|
|
73
|
-
marketId: marketParams.id,
|
|
74
|
-
});
|
|
78
|
+
export const blueRepay = ({ market: { chainId, marketParams }, args, metadata, }) => {
|
|
79
|
+
const { amount = 0n, shares = 0n, nativeAmount = 0n, transferAmount, onBehalf, receiver, maxSharePrice, requirementSignature, } = args;
|
|
80
|
+
const { isSharesMode, repayAssets, repayShares, erc20Amount } = resolveRepayFunding({ amount, shares, nativeAmount, transferAmount, maxSharePrice }, marketParams.id);
|
|
75
81
|
const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
|
|
76
|
-
const actions =
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}));
|
|
84
|
-
}
|
|
85
|
-
else {
|
|
86
|
-
actions.push({
|
|
87
|
-
type: "erc20TransferFrom",
|
|
88
|
-
args: [marketParams.loanToken, transferAmount, generalAdapter1, false],
|
|
89
|
-
});
|
|
90
|
-
}
|
|
82
|
+
const actions = buildAssetFundingActions({
|
|
83
|
+
chainId,
|
|
84
|
+
asset: marketParams.loanToken,
|
|
85
|
+
erc20Amount,
|
|
86
|
+
nativeAmount,
|
|
87
|
+
requirementSignature,
|
|
88
|
+
});
|
|
91
89
|
actions.push({
|
|
92
90
|
type: "morphoRepay",
|
|
93
|
-
args: [
|
|
91
|
+
args: [
|
|
92
|
+
marketParams,
|
|
93
|
+
repayAssets,
|
|
94
|
+
repayShares,
|
|
95
|
+
maxSharePrice,
|
|
96
|
+
onBehalf,
|
|
97
|
+
[],
|
|
98
|
+
false,
|
|
99
|
+
],
|
|
94
100
|
});
|
|
95
101
|
// Skim residual loan tokens back to the payer when repaying by shares.
|
|
96
102
|
// In shares mode, transferAmount is an upper-bound estimate; morphoRepay
|
|
97
103
|
// consumes only the exact amount needed, leaving a residual in the adapter.
|
|
98
|
-
if (
|
|
104
|
+
if (isSharesMode) {
|
|
99
105
|
actions.push({
|
|
100
106
|
type: "erc20Transfer",
|
|
101
107
|
args: [
|
|
@@ -107,7 +113,10 @@ export const blueRepay = ({ market: { chainId, marketParams }, args: { assets, s
|
|
|
107
113
|
],
|
|
108
114
|
});
|
|
109
115
|
}
|
|
110
|
-
let tx =
|
|
116
|
+
let tx = {
|
|
117
|
+
...BundlerAction.encodeBundle(chainId, actions),
|
|
118
|
+
value: nativeAmount,
|
|
119
|
+
};
|
|
111
120
|
if (metadata) {
|
|
112
121
|
tx = addTransactionMetadata(tx, metadata);
|
|
113
122
|
}
|
|
@@ -117,12 +126,14 @@ export const blueRepay = ({ market: { chainId, marketParams }, args: { assets, s
|
|
|
117
126
|
type: "blueRepay",
|
|
118
127
|
args: {
|
|
119
128
|
market: marketParams.id,
|
|
120
|
-
assets,
|
|
121
|
-
shares,
|
|
122
|
-
|
|
129
|
+
assets: repayAssets,
|
|
130
|
+
shares: repayShares,
|
|
131
|
+
// Total loan tokens routed to the adapter: ERC-20 pulled + native wrapped.
|
|
132
|
+
transferAmount: erc20Amount + nativeAmount,
|
|
123
133
|
onBehalf,
|
|
124
134
|
receiver,
|
|
125
135
|
maxSharePrice,
|
|
136
|
+
nativeAmount: nativeAmount > 0n ? nativeAmount : undefined,
|
|
126
137
|
},
|
|
127
138
|
},
|
|
128
139
|
});
|
|
@@ -1,24 +1,13 @@
|
|
|
1
1
|
import { type MarketParams } from "@morpho-org/blue-sdk";
|
|
2
2
|
import { type Address } from "viem";
|
|
3
|
-
import { type BlueRepayWithdrawCollateralAction, type Metadata, type
|
|
3
|
+
import { type AuthorizationRequirementSignature, type BlueRepayWithdrawCollateralAction, type Metadata, type PermitRequirementSignature, type RepayActionAmountArgs, type Transaction } from "../../types/index.js";
|
|
4
4
|
/** Parameters for {@link blueRepayWithdrawCollateral}. */
|
|
5
5
|
export interface BlueRepayWithdrawCollateralParams {
|
|
6
6
|
market: {
|
|
7
7
|
readonly chainId: number;
|
|
8
8
|
readonly marketParams: MarketParams;
|
|
9
9
|
};
|
|
10
|
-
args: {
|
|
11
|
-
/** Repay assets amount (0n when repaying by shares). */
|
|
12
|
-
assets: bigint;
|
|
13
|
-
/** Repay shares amount (0n when repaying by assets). */
|
|
14
|
-
shares: bigint;
|
|
15
|
-
/**
|
|
16
|
-
* ERC-20 amount to pull into `GeneralAdapter1`. In assets mode, must equal `assets`
|
|
17
|
-
* exactly (`TransferAmountNotEqualToAssetsError` fires otherwise). In shares mode, an
|
|
18
|
-
* upper-bound estimate to absorb share-price drift; residual loan tokens are skimmed
|
|
19
|
-
* back to `receiver`.
|
|
20
|
-
*/
|
|
21
|
-
transferAmount: bigint;
|
|
10
|
+
args: RepayActionAmountArgs & {
|
|
22
11
|
/** Amount of collateral to withdraw. */
|
|
23
12
|
withdrawAmount: bigint;
|
|
24
13
|
/** Address whose debt is being repaid. */
|
|
@@ -27,7 +16,14 @@ export interface BlueRepayWithdrawCollateralParams {
|
|
|
27
16
|
receiver: Address;
|
|
28
17
|
/** Maximum repay share price (in ray). Protects against share price manipulation. */
|
|
29
18
|
maxSharePrice: bigint;
|
|
30
|
-
|
|
19
|
+
/** Optional pre-signed permit/permit2 approval for the loan-token transfer. */
|
|
20
|
+
requirementSignature?: PermitRequirementSignature;
|
|
21
|
+
/**
|
|
22
|
+
* Optional signed Morpho authorization. When provided, a `setAuthorizationWithSig` call is
|
|
23
|
+
* prepended to the bundle so GeneralAdapter1 is authorized in-bundle instead of via a
|
|
24
|
+
* standalone `setAuthorization` transaction.
|
|
25
|
+
*/
|
|
26
|
+
authorizationSignature?: AuthorizationRequirementSignature;
|
|
31
27
|
};
|
|
32
28
|
metadata?: Metadata;
|
|
33
29
|
}
|
|
@@ -41,23 +37,29 @@ export interface BlueRepayWithdrawCollateralParams {
|
|
|
41
37
|
* 3. `morphoWithdrawCollateral` — then withdraws collateral.
|
|
42
38
|
*
|
|
43
39
|
* If the order were reversed, Morpho would revert because the position would be insolvent at the
|
|
44
|
-
* time of the withdraw.
|
|
40
|
+
* time of the withdraw. All amount arithmetic is done upstream (see
|
|
41
|
+
* `MorphoBlue.repayWithdrawCollateral`); this builder just assembles the bundle from the
|
|
42
|
+
* pre-resolved {@link RepayActionAmountArgs}. The mode is discriminated on `shares`, plus optional
|
|
43
|
+
* native wrapping (when `nativeAmount > 0`, native ETH is wrapped via `GeneralAdapter1.wrapNative()`
|
|
44
|
+
* before the repay; the loan token must be the chain's wNative):
|
|
45
45
|
*
|
|
46
|
-
* - **
|
|
47
|
-
*
|
|
48
|
-
*
|
|
46
|
+
* - **assets mode** (`shares` unset/`0n`): repays `transferAmount` assets (`= amount + nativeAmount`,
|
|
47
|
+
* additive like `blueSupply`), pulling `amount` ERC-20.
|
|
48
|
+
* - **shares mode** (`shares > 0n`): repays exact shares (full repay), pulling `transferAmount`
|
|
49
|
+
* ERC-20 (already net of native); residual loan tokens are skimmed back to `receiver`.
|
|
49
50
|
*
|
|
50
51
|
* Prerequisites: ERC-20 approval for the loan token to `GeneralAdapter1` (for the repay) **and**
|
|
51
|
-
* `GeneralAdapter1` must be authorized on Morpho (for the withdraw).
|
|
52
|
+
* `GeneralAdapter1` must be authorized on Morpho (for the withdraw). Passing an
|
|
53
|
+
* `authorizationSignature` prepends the authorization in-bundle instead.
|
|
52
54
|
*
|
|
53
55
|
* @param params.market.chainId - The chain the market lives on.
|
|
54
56
|
* @param params.market.marketParams - Market params (loanToken, collateralToken, oracle, irm, lltv).
|
|
55
|
-
* @param params.args.
|
|
56
|
-
* @param params.args.shares - Repay amount in borrow shares.
|
|
57
|
-
* @param params.args.transferAmount -
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
57
|
+
* @param params.args.amount - (assets mode) ERC-20 loan tokens pulled from the payer. Defaults to `0n`.
|
|
58
|
+
* @param params.args.shares - (shares mode) Repay amount in borrow shares. Discriminates the mode.
|
|
59
|
+
* @param params.args.transferAmount - Loan tokens routed into `GeneralAdapter1`: assets mode = the
|
|
60
|
+
* total repaid (`amount + nativeAmount`); shares mode = the ERC-20 pulled (net of native).
|
|
61
|
+
* @param params.args.nativeAmount - Optional native token to wrap into wNative to fund the repay.
|
|
62
|
+
* Requires the loan token to be the chain's wNative.
|
|
61
63
|
* @param params.args.withdrawAmount - Amount of collateral to withdraw after the repay leg
|
|
62
64
|
* completes.
|
|
63
65
|
* @param params.args.onBehalf - Address whose Morpho debt is being repaid.
|
|
@@ -67,39 +69,46 @@ export interface BlueRepayWithdrawCollateralParams {
|
|
|
67
69
|
* protection.
|
|
68
70
|
* @param params.args.requirementSignature - Optional pre-signed permit/permit2 approval for the
|
|
69
71
|
* loan-token transfer.
|
|
72
|
+
* @param params.args.authorizationSignature - Optional signed Morpho authorization; when present,
|
|
73
|
+
* a `setAuthorizationWithSig` call is prepended to the bundle.
|
|
70
74
|
* @param params.metadata - Optional analytics metadata attached to the bundle.
|
|
71
75
|
* @returns A deep-frozen `Transaction<BlueRepayWithdrawCollateralAction>` with `to`,
|
|
72
|
-
* `value
|
|
76
|
+
* `value` (= `nativeAmount`), `data`, and the typed `action` discriminator the simulation layer consumes.
|
|
73
77
|
* @throws {NonPositiveRepayMaxSharePriceError} when `maxSharePrice <= 0n`.
|
|
74
|
-
* @throws {
|
|
75
|
-
*
|
|
76
|
-
* @throws {
|
|
77
|
-
*
|
|
78
|
-
* @throws {
|
|
78
|
+
* @throws {NegativeNativeAmountError} when `nativeAmount < 0n`.
|
|
79
|
+
* @throws {MutuallyExclusiveRepayAmountsError} when both `amount` and `shares` are `> 0n`.
|
|
80
|
+
* @throws {TransferAmountNotEqualToAssetsError} when in assets mode and
|
|
81
|
+
* `transferAmount !== amount + nativeAmount`.
|
|
82
|
+
* @throws {NonPositiveRepayAmountError} when `amount` or `shares` is negative, when in assets mode
|
|
83
|
+
* and `transferAmount <= 0n`, or when in shares mode and `transferAmount` is negative or the total
|
|
84
|
+
* funding (`transferAmount + nativeAmount`) is `<= 0n`.
|
|
79
85
|
* @throws {NonPositiveWithdrawCollateralAmountError} when `withdrawAmount <= 0n`.
|
|
80
|
-
* @throws {
|
|
86
|
+
* @throws {ChainWNativeMissingError} when `nativeAmount > 0n` but the chain has no configured wNative.
|
|
87
|
+
* @throws {NativeAmountOnNonWNativeAssetError} when `nativeAmount > 0n` but the loan token is not
|
|
88
|
+
* the chain's wNative.
|
|
89
|
+
* @throws {DepositAssetMismatchError} from `getTokenRequirementActions` when `requirementSignature`
|
|
81
90
|
* is provided and the signed asset differs from `marketParams.loanToken`.
|
|
82
|
-
* @throws {DepositAmountMismatchError} from `
|
|
83
|
-
* is provided and the signed amount differs from
|
|
84
|
-
* @throws {Permit2ExpirationMissingError} from `
|
|
91
|
+
* @throws {DepositAmountMismatchError} from `getTokenRequirementActions` when `requirementSignature`
|
|
92
|
+
* is provided and the signed amount differs from the ERC-20 amount pulled.
|
|
93
|
+
* @throws {Permit2ExpirationMissingError} from `getTokenRequirementActions` when a Permit2 requirement
|
|
85
94
|
* signature is missing its expiration.
|
|
86
95
|
* @example
|
|
87
96
|
* ```ts
|
|
88
97
|
* import { blueRepayWithdrawCollateral } from "@morpho-org/morpho-sdk";
|
|
89
98
|
*
|
|
90
99
|
* const tx = blueRepayWithdrawCollateral({
|
|
91
|
-
* market: { chainId: 1, marketParams },
|
|
100
|
+
* market: { chainId: 1, marketParams }, // marketParams.loanToken === wNative
|
|
92
101
|
* args: {
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
102
|
+
* shares: 500_000_000_000_000_000_000_000n,
|
|
103
|
+
* transferAmount: 310_000_000_000_000_000n, // ERC-20 pulled (net of native)
|
|
104
|
+
* nativeAmount: 200_000_000_000_000_000n, // 0.2 funded by wrapping native ETH
|
|
96
105
|
* withdrawAmount: 1_000_000_000_000_000_000n,
|
|
97
106
|
* onBehalf: borrower,
|
|
98
107
|
* receiver: borrower,
|
|
99
108
|
* maxSharePrice: 1_010_000_000_000_000_000_000_000_000n, // RAY-scaled, 1.01x
|
|
100
109
|
* },
|
|
101
110
|
* });
|
|
102
|
-
* // tx
|
|
111
|
+
* // tx.value === 200_000_000_000_000_000n
|
|
103
112
|
* ```
|
|
104
113
|
*/
|
|
105
|
-
export declare const blueRepayWithdrawCollateral: ({ market: { chainId, marketParams }, args
|
|
114
|
+
export declare const blueRepayWithdrawCollateral: ({ market: { chainId, marketParams }, args, metadata, }: BlueRepayWithdrawCollateralParams) => Readonly<Transaction<BlueRepayWithdrawCollateralAction>>;
|
|
@@ -2,9 +2,11 @@ import { getChainAddresses } from "@morpho-org/blue-sdk";
|
|
|
2
2
|
import { deepFreeze } from "@morpho-org/morpho-ts";
|
|
3
3
|
import { maxUint256 } from "viem";
|
|
4
4
|
import { BundlerAction } from "../../bundler/index.js";
|
|
5
|
-
import { addTransactionMetadata
|
|
5
|
+
import { addTransactionMetadata } from "../../helpers/index.js";
|
|
6
6
|
import { NonPositiveWithdrawCollateralAmountError, } from "../../types/index.js";
|
|
7
|
-
import {
|
|
7
|
+
import { getBlueAuthorizationAction } from "../signatures/getBlueAuthorizationAction.js";
|
|
8
|
+
import { buildAssetFundingActions } from "./buildAssetFundingActions.js";
|
|
9
|
+
import { resolveRepayFunding } from "./resolveRepayFunding.js";
|
|
8
10
|
/**
|
|
9
11
|
* Prepares an atomic repay-and-withdraw-collateral transaction for a Morpho Blue market.
|
|
10
12
|
*
|
|
@@ -15,23 +17,29 @@ import { getRequirementsAction } from "../requirements/getRequirementsAction.js"
|
|
|
15
17
|
* 3. `morphoWithdrawCollateral` — then withdraws collateral.
|
|
16
18
|
*
|
|
17
19
|
* If the order were reversed, Morpho would revert because the position would be insolvent at the
|
|
18
|
-
* time of the withdraw.
|
|
20
|
+
* time of the withdraw. All amount arithmetic is done upstream (see
|
|
21
|
+
* `MorphoBlue.repayWithdrawCollateral`); this builder just assembles the bundle from the
|
|
22
|
+
* pre-resolved {@link RepayActionAmountArgs}. The mode is discriminated on `shares`, plus optional
|
|
23
|
+
* native wrapping (when `nativeAmount > 0`, native ETH is wrapped via `GeneralAdapter1.wrapNative()`
|
|
24
|
+
* before the repay; the loan token must be the chain's wNative):
|
|
19
25
|
*
|
|
20
|
-
* - **
|
|
21
|
-
*
|
|
22
|
-
*
|
|
26
|
+
* - **assets mode** (`shares` unset/`0n`): repays `transferAmount` assets (`= amount + nativeAmount`,
|
|
27
|
+
* additive like `blueSupply`), pulling `amount` ERC-20.
|
|
28
|
+
* - **shares mode** (`shares > 0n`): repays exact shares (full repay), pulling `transferAmount`
|
|
29
|
+
* ERC-20 (already net of native); residual loan tokens are skimmed back to `receiver`.
|
|
23
30
|
*
|
|
24
31
|
* Prerequisites: ERC-20 approval for the loan token to `GeneralAdapter1` (for the repay) **and**
|
|
25
|
-
* `GeneralAdapter1` must be authorized on Morpho (for the withdraw).
|
|
32
|
+
* `GeneralAdapter1` must be authorized on Morpho (for the withdraw). Passing an
|
|
33
|
+
* `authorizationSignature` prepends the authorization in-bundle instead.
|
|
26
34
|
*
|
|
27
35
|
* @param params.market.chainId - The chain the market lives on.
|
|
28
36
|
* @param params.market.marketParams - Market params (loanToken, collateralToken, oracle, irm, lltv).
|
|
29
|
-
* @param params.args.
|
|
30
|
-
* @param params.args.shares - Repay amount in borrow shares.
|
|
31
|
-
* @param params.args.transferAmount -
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
37
|
+
* @param params.args.amount - (assets mode) ERC-20 loan tokens pulled from the payer. Defaults to `0n`.
|
|
38
|
+
* @param params.args.shares - (shares mode) Repay amount in borrow shares. Discriminates the mode.
|
|
39
|
+
* @param params.args.transferAmount - Loan tokens routed into `GeneralAdapter1`: assets mode = the
|
|
40
|
+
* total repaid (`amount + nativeAmount`); shares mode = the ERC-20 pulled (net of native).
|
|
41
|
+
* @param params.args.nativeAmount - Optional native token to wrap into wNative to fund the repay.
|
|
42
|
+
* Requires the loan token to be the chain's wNative.
|
|
35
43
|
* @param params.args.withdrawAmount - Amount of collateral to withdraw after the repay leg
|
|
36
44
|
* completes.
|
|
37
45
|
* @param params.args.onBehalf - Address whose Morpho debt is being repaid.
|
|
@@ -41,77 +49,85 @@ import { getRequirementsAction } from "../requirements/getRequirementsAction.js"
|
|
|
41
49
|
* protection.
|
|
42
50
|
* @param params.args.requirementSignature - Optional pre-signed permit/permit2 approval for the
|
|
43
51
|
* loan-token transfer.
|
|
52
|
+
* @param params.args.authorizationSignature - Optional signed Morpho authorization; when present,
|
|
53
|
+
* a `setAuthorizationWithSig` call is prepended to the bundle.
|
|
44
54
|
* @param params.metadata - Optional analytics metadata attached to the bundle.
|
|
45
55
|
* @returns A deep-frozen `Transaction<BlueRepayWithdrawCollateralAction>` with `to`,
|
|
46
|
-
* `value
|
|
56
|
+
* `value` (= `nativeAmount`), `data`, and the typed `action` discriminator the simulation layer consumes.
|
|
47
57
|
* @throws {NonPositiveRepayMaxSharePriceError} when `maxSharePrice <= 0n`.
|
|
48
|
-
* @throws {
|
|
49
|
-
*
|
|
50
|
-
* @throws {
|
|
51
|
-
*
|
|
52
|
-
* @throws {
|
|
58
|
+
* @throws {NegativeNativeAmountError} when `nativeAmount < 0n`.
|
|
59
|
+
* @throws {MutuallyExclusiveRepayAmountsError} when both `amount` and `shares` are `> 0n`.
|
|
60
|
+
* @throws {TransferAmountNotEqualToAssetsError} when in assets mode and
|
|
61
|
+
* `transferAmount !== amount + nativeAmount`.
|
|
62
|
+
* @throws {NonPositiveRepayAmountError} when `amount` or `shares` is negative, when in assets mode
|
|
63
|
+
* and `transferAmount <= 0n`, or when in shares mode and `transferAmount` is negative or the total
|
|
64
|
+
* funding (`transferAmount + nativeAmount`) is `<= 0n`.
|
|
53
65
|
* @throws {NonPositiveWithdrawCollateralAmountError} when `withdrawAmount <= 0n`.
|
|
54
|
-
* @throws {
|
|
66
|
+
* @throws {ChainWNativeMissingError} when `nativeAmount > 0n` but the chain has no configured wNative.
|
|
67
|
+
* @throws {NativeAmountOnNonWNativeAssetError} when `nativeAmount > 0n` but the loan token is not
|
|
68
|
+
* the chain's wNative.
|
|
69
|
+
* @throws {DepositAssetMismatchError} from `getTokenRequirementActions` when `requirementSignature`
|
|
55
70
|
* is provided and the signed asset differs from `marketParams.loanToken`.
|
|
56
|
-
* @throws {DepositAmountMismatchError} from `
|
|
57
|
-
* is provided and the signed amount differs from
|
|
58
|
-
* @throws {Permit2ExpirationMissingError} from `
|
|
71
|
+
* @throws {DepositAmountMismatchError} from `getTokenRequirementActions` when `requirementSignature`
|
|
72
|
+
* is provided and the signed amount differs from the ERC-20 amount pulled.
|
|
73
|
+
* @throws {Permit2ExpirationMissingError} from `getTokenRequirementActions` when a Permit2 requirement
|
|
59
74
|
* signature is missing its expiration.
|
|
60
75
|
* @example
|
|
61
76
|
* ```ts
|
|
62
77
|
* import { blueRepayWithdrawCollateral } from "@morpho-org/morpho-sdk";
|
|
63
78
|
*
|
|
64
79
|
* const tx = blueRepayWithdrawCollateral({
|
|
65
|
-
* market: { chainId: 1, marketParams },
|
|
80
|
+
* market: { chainId: 1, marketParams }, // marketParams.loanToken === wNative
|
|
66
81
|
* args: {
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
82
|
+
* shares: 500_000_000_000_000_000_000_000n,
|
|
83
|
+
* transferAmount: 310_000_000_000_000_000n, // ERC-20 pulled (net of native)
|
|
84
|
+
* nativeAmount: 200_000_000_000_000_000n, // 0.2 funded by wrapping native ETH
|
|
70
85
|
* withdrawAmount: 1_000_000_000_000_000_000n,
|
|
71
86
|
* onBehalf: borrower,
|
|
72
87
|
* receiver: borrower,
|
|
73
88
|
* maxSharePrice: 1_010_000_000_000_000_000_000_000_000n, // RAY-scaled, 1.01x
|
|
74
89
|
* },
|
|
75
90
|
* });
|
|
76
|
-
* // tx
|
|
91
|
+
* // tx.value === 200_000_000_000_000_000n
|
|
77
92
|
* ```
|
|
78
93
|
*/
|
|
79
|
-
export const blueRepayWithdrawCollateral = ({ market: { chainId, marketParams }, args
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
shares,
|
|
83
|
-
transferAmount,
|
|
84
|
-
maxSharePrice,
|
|
85
|
-
marketId: marketParams.id,
|
|
86
|
-
});
|
|
94
|
+
export const blueRepayWithdrawCollateral = ({ market: { chainId, marketParams }, args, metadata, }) => {
|
|
95
|
+
const { amount = 0n, shares = 0n, nativeAmount = 0n, transferAmount, withdrawAmount, onBehalf, receiver, maxSharePrice, requirementSignature, authorizationSignature, } = args;
|
|
96
|
+
const { isSharesMode, repayAssets, repayShares, erc20Amount } = resolveRepayFunding({ amount, shares, nativeAmount, transferAmount, maxSharePrice }, marketParams.id);
|
|
87
97
|
if (withdrawAmount <= 0n) {
|
|
88
98
|
throw new NonPositiveWithdrawCollateralAmountError(marketParams.id);
|
|
89
99
|
}
|
|
90
100
|
const { bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
|
|
91
101
|
const actions = [];
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
amount: transferAmount,
|
|
96
|
-
recipient: generalAdapter1,
|
|
97
|
-
requirementSignature,
|
|
98
|
-
}));
|
|
99
|
-
}
|
|
100
|
-
else {
|
|
101
|
-
actions.push({
|
|
102
|
-
type: "erc20TransferFrom",
|
|
103
|
-
args: [marketParams.loanToken, transferAmount, generalAdapter1, false],
|
|
104
|
-
});
|
|
102
|
+
// Authorize GeneralAdapter1 in-bundle when a signed authorization is supplied.
|
|
103
|
+
if (authorizationSignature) {
|
|
104
|
+
actions.push(getBlueAuthorizationAction(chainId, authorizationSignature));
|
|
105
105
|
}
|
|
106
|
+
// Fund the repay: wrap native (if any) then pull the ERC-20 remainder.
|
|
107
|
+
actions.push(...buildAssetFundingActions({
|
|
108
|
+
chainId,
|
|
109
|
+
asset: marketParams.loanToken,
|
|
110
|
+
erc20Amount,
|
|
111
|
+
nativeAmount,
|
|
112
|
+
requirementSignature,
|
|
113
|
+
}));
|
|
106
114
|
// REPAY FIRST — reduces debt before withdrawing collateral
|
|
107
115
|
actions.push({
|
|
108
116
|
type: "morphoRepay",
|
|
109
|
-
args: [
|
|
117
|
+
args: [
|
|
118
|
+
marketParams,
|
|
119
|
+
repayAssets,
|
|
120
|
+
repayShares,
|
|
121
|
+
maxSharePrice,
|
|
122
|
+
onBehalf,
|
|
123
|
+
[],
|
|
124
|
+
false,
|
|
125
|
+
],
|
|
110
126
|
});
|
|
111
127
|
// Skim residual loan tokens back to the payer when repaying by shares.
|
|
112
128
|
// In shares mode, transferAmount is an upper-bound estimate; morphoRepay
|
|
113
129
|
// consumes only the exact amount needed, leaving a residual in the adapter.
|
|
114
|
-
if (
|
|
130
|
+
if (isSharesMode) {
|
|
115
131
|
actions.push({
|
|
116
132
|
type: "erc20Transfer",
|
|
117
133
|
args: [
|
|
@@ -127,7 +143,10 @@ export const blueRepayWithdrawCollateral = ({ market: { chainId, marketParams },
|
|
|
127
143
|
type: "morphoWithdrawCollateral",
|
|
128
144
|
args: [marketParams, withdrawAmount, receiver, false],
|
|
129
145
|
});
|
|
130
|
-
let tx =
|
|
146
|
+
let tx = {
|
|
147
|
+
...BundlerAction.encodeBundle(chainId, actions),
|
|
148
|
+
value: nativeAmount,
|
|
149
|
+
};
|
|
131
150
|
if (metadata) {
|
|
132
151
|
tx = addTransactionMetadata(tx, metadata);
|
|
133
152
|
}
|
|
@@ -137,13 +156,15 @@ export const blueRepayWithdrawCollateral = ({ market: { chainId, marketParams },
|
|
|
137
156
|
type: "blueRepayWithdrawCollateral",
|
|
138
157
|
args: {
|
|
139
158
|
market: marketParams.id,
|
|
140
|
-
repayAssets
|
|
141
|
-
repayShares
|
|
142
|
-
|
|
159
|
+
repayAssets,
|
|
160
|
+
repayShares,
|
|
161
|
+
// Total loan tokens routed to the adapter: ERC-20 pulled + native wrapped.
|
|
162
|
+
transferAmount: erc20Amount + nativeAmount,
|
|
143
163
|
withdrawAmount,
|
|
144
164
|
maxSharePrice,
|
|
145
165
|
onBehalf,
|
|
146
166
|
receiver,
|
|
167
|
+
nativeAmount: nativeAmount > 0n ? nativeAmount : undefined,
|
|
147
168
|
},
|
|
148
169
|
},
|
|
149
170
|
});
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { MarketId } from "@morpho-org/blue-sdk";
|
|
2
|
+
/**
|
|
3
|
+
* Pre-resolved repay amounts (plus `maxSharePrice`) validated by {@link resolveRepayFunding}.
|
|
4
|
+
*
|
|
5
|
+
* These are the fields the entity layer (`MorphoBlue.repay` / `repayWithdrawCollateral`) resolves
|
|
6
|
+
* from live market/position state before either repay builder assembles the bundle.
|
|
7
|
+
*
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
export interface RepayFundingInput {
|
|
11
|
+
/** (assets mode) ERC-20 loan tokens pulled from the payer. */
|
|
12
|
+
readonly amount: bigint;
|
|
13
|
+
/** (shares mode) Borrow shares to repay. Discriminates the mode when `> 0n`. */
|
|
14
|
+
readonly shares: bigint;
|
|
15
|
+
/** Native ETH wrapped into wNative to help fund the repay. */
|
|
16
|
+
readonly nativeAmount: bigint;
|
|
17
|
+
/** Loan tokens routed into `GeneralAdapter1` (see `RepayActionAmountArgs`). */
|
|
18
|
+
readonly transferAmount: bigint;
|
|
19
|
+
/** Maximum acceptable repay share price (in ray). */
|
|
20
|
+
readonly maxSharePrice: bigint;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Derived repay funding values consumed by the repay builders.
|
|
24
|
+
*
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
export interface RepayFunding {
|
|
28
|
+
/** `true` when repaying an exact share count (full close); `false` in assets mode. */
|
|
29
|
+
readonly isSharesMode: boolean;
|
|
30
|
+
/** Assets passed to `morphoRepay` (`transferAmount` in assets mode, `0n` in shares mode). */
|
|
31
|
+
readonly repayAssets: bigint;
|
|
32
|
+
/** Shares passed to `morphoRepay` (`shares` in shares mode, `0n` in assets mode). */
|
|
33
|
+
readonly repayShares: bigint;
|
|
34
|
+
/** ERC-20 loan tokens pulled into `GeneralAdapter1` (net of any wrapped native). */
|
|
35
|
+
readonly erc20Amount: bigint;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Resolves and validates the repay funding envelope shared by `blueRepay` and
|
|
39
|
+
* `blueRepayWithdrawCollateral`.
|
|
40
|
+
*
|
|
41
|
+
* Encode-only and synchronous — reads no on-chain state and does no amount arithmetic beyond
|
|
42
|
+
* deriving the bundle values from the pre-resolved args (the entity layer resolves the amounts
|
|
43
|
+
* upstream). Runs the shared mode/amount guards, discriminates assets vs shares mode on
|
|
44
|
+
* `shares > 0n`, then validates the funding so the assembled bundle neither strands over-pulled
|
|
45
|
+
* tokens nor under-funds the repay:
|
|
46
|
+
*
|
|
47
|
+
* - **assets mode** (`shares` `0n`): repays `transferAmount` (`= amount + nativeAmount`, additive
|
|
48
|
+
* like `blueSupply`); ERC-20 pulled = `amount`.
|
|
49
|
+
* - **shares mode** (`shares > 0n`): repays exact `shares`; ERC-20 pulled = `transferAmount` (already
|
|
50
|
+
* net of native).
|
|
51
|
+
*
|
|
52
|
+
* Single source of truth for the funding math both repay builders depend on — a correctness fix here
|
|
53
|
+
* applies to both paths instead of risking a silent divergence between two copies.
|
|
54
|
+
*
|
|
55
|
+
* @param input - The pre-resolved repay amounts plus the repay `maxSharePrice`.
|
|
56
|
+
* @param marketId - Market id used to tag the thrown errors.
|
|
57
|
+
* @returns `{ isSharesMode, repayAssets, repayShares, erc20Amount }` — the mode discriminator and the
|
|
58
|
+
* amounts the repay builders assemble the bundle from.
|
|
59
|
+
* @throws {NonPositiveRepayMaxSharePriceError} when `maxSharePrice <= 0n`.
|
|
60
|
+
* @throws {NegativeNativeAmountError} when `nativeAmount < 0n`.
|
|
61
|
+
* @throws {NonPositiveRepayAmountError} when `amount` or `shares` is negative, when in assets mode and
|
|
62
|
+
* the repaid assets are `<= 0n`, or when in shares mode and `transferAmount` is negative or the total
|
|
63
|
+
* funding (`transferAmount + nativeAmount`) is `<= 0n`.
|
|
64
|
+
* @throws {MutuallyExclusiveRepayAmountsError} when both `amount` and `shares` are `> 0n`.
|
|
65
|
+
* @throws {TransferAmountNotEqualToAssetsError} when in assets mode and
|
|
66
|
+
* `transferAmount !== amount + nativeAmount`.
|
|
67
|
+
* @internal
|
|
68
|
+
*/
|
|
69
|
+
export declare const resolveRepayFunding: ({ amount, shares, nativeAmount, transferAmount, maxSharePrice, }: RepayFundingInput, marketId: MarketId) => RepayFunding;
|