import { type MarketParams } from "@morpho-org/blue-sdk"; import { type Address } from "viem"; import { type AuthorizationRequirementSignature, type BlueRepayWithdrawCollateralAction, type Metadata, type PermitRequirementSignature, type RepayActionAmountArgs, type Transaction } from "../../types/index.js"; /** Parameters for {@link blueRepayWithdrawCollateral}. */ export interface BlueRepayWithdrawCollateralParams { market: { readonly chainId: number; readonly marketParams: MarketParams; }; args: RepayActionAmountArgs & { /** Amount of collateral to withdraw. */ withdrawAmount: bigint; /** Address whose debt is being repaid. */ onBehalf: Address; /** Receives withdrawn collateral and residual loan tokens in shares mode. */ receiver: Address; /** Maximum repay share price (in ray). Protects against share price manipulation. */ maxSharePrice: bigint; /** Optional pre-signed permit/permit2 approval for the loan-token transfer. */ requirementSignature?: PermitRequirementSignature; /** * Optional signed Morpho authorization. When provided, a `setAuthorizationWithSig` call is * prepended to the bundle so GeneralAdapter1 is authorized in-bundle instead of via a * standalone `setAuthorization` transaction. */ authorizationSignature?: AuthorizationRequirementSignature; }; metadata?: Metadata; } /** * Prepares an atomic repay-and-withdraw-collateral transaction for a Morpho Blue market. * * Routed through bundler3. The bundle order is critical: * * 1. ERC-20 transfer of the loan token to `GeneralAdapter1`. * 2. `morphoRepay` — reduces debt **first**. * 3. `morphoWithdrawCollateral` — then withdraws collateral. * * If the order were reversed, Morpho would revert because the position would be insolvent at the * time of the withdraw. All amount arithmetic is done upstream (see * `MorphoBlue.repayWithdrawCollateral`); this builder just assembles the bundle from the * pre-resolved {@link RepayActionAmountArgs}. The mode is discriminated on `shares`, plus optional * native wrapping (when `nativeAmount > 0`, native ETH is wrapped via `GeneralAdapter1.wrapNative()` * before the repay; the loan token must be the chain's wNative): * * - **assets mode** (`shares` unset/`0n`): repays `transferAmount` assets (`= amount + nativeAmount`, * additive like `blueSupply`), pulling `amount` ERC-20. * - **shares mode** (`shares > 0n`): repays exact shares (full repay), pulling `transferAmount` * ERC-20 (already net of native); residual loan tokens are skimmed back to `receiver`. * * Prerequisites: ERC-20 approval for the loan token to `GeneralAdapter1` (for the repay) **and** * `GeneralAdapter1` must be authorized on Morpho (for the withdraw). Passing an * `authorizationSignature` prepends the authorization in-bundle instead. * * @param params.market.chainId - The chain the market lives on. * @param params.market.marketParams - Market params (loanToken, collateralToken, oracle, irm, lltv). * @param params.args.amount - (assets mode) ERC-20 loan tokens pulled from the payer. Defaults to `0n`. * @param params.args.shares - (shares mode) Repay amount in borrow shares. Discriminates the mode. * @param params.args.transferAmount - Loan tokens routed into `GeneralAdapter1`: assets mode = the * total repaid (`amount + nativeAmount`); shares mode = the ERC-20 pulled (net of native). * @param params.args.nativeAmount - Optional native token to wrap into wNative to fund the repay. * Requires the loan token to be the chain's wNative. * @param params.args.withdrawAmount - Amount of collateral to withdraw after the repay leg * completes. * @param params.args.onBehalf - Address whose Morpho debt is being repaid. * @param params.args.receiver - Address that receives the withdrawn collateral and any residual * loan tokens in shares mode. * @param params.args.maxSharePrice - Maximum acceptable repay share price (in ray). Slippage * protection. * @param params.args.requirementSignature - Optional pre-signed permit/permit2 approval for the * loan-token transfer. * @param params.args.authorizationSignature - Optional signed Morpho authorization; when present, * a `setAuthorizationWithSig` call is prepended to the bundle. * @param params.metadata - Optional analytics metadata attached to the bundle. * @returns A deep-frozen `Transaction` with `to`, * `value` (= `nativeAmount`), `data`, and the typed `action` discriminator the simulation layer consumes. * @throws {NonPositiveInputError} when `maxSharePrice <= 0n`, the total funding is zero, or * `withdrawAmount <= 0n`. * @throws {NegativeInputError} when `amount`, `shares`, `nativeAmount`, or `transferAmount` is negative. * @throws {MutuallyExclusiveRepayAmountsError} when both `amount` and `shares` are `> 0n`. * @throws {TransferAmountNotEqualToAssetsError} when in assets mode and * `transferAmount !== amount + nativeAmount`. * @throws {ChainWNativeMissingError} when `nativeAmount > 0n` but the chain has no configured wNative. * @throws {NativeAmountOnNonWNativeAssetError} when `nativeAmount > 0n` but the loan token is not * the chain's wNative. * @throws {DepositAssetMismatchError} from `getTokenRequirementActions` when `requirementSignature` * is provided and the signed asset differs from `marketParams.loanToken`. * @throws {DepositAmountMismatchError} from `getTokenRequirementActions` when `requirementSignature` * is provided and the signed amount differs from the ERC-20 amount pulled. * @throws {Permit2ExpirationMissingError} from `getTokenRequirementActions` when a Permit2 requirement * signature is missing its expiration. * @example * ```ts * import { blueRepayWithdrawCollateral } from "@morpho-org/morpho-sdk"; * * const tx = blueRepayWithdrawCollateral({ * market: { chainId: 1, marketParams }, // marketParams.loanToken === wNative * args: { * shares: 500_000_000_000_000_000_000_000n, * transferAmount: 310_000_000_000_000_000n, // ERC-20 pulled (net of native) * nativeAmount: 200_000_000_000_000_000n, // 0.2 funded by wrapping native ETH * withdrawAmount: 1_000_000_000_000_000_000n, * onBehalf: borrower, * receiver: borrower, * maxSharePrice: 1_010_000_000_000_000_000_000_000_000n, // RAY-scaled, 1.01x * }, * }); * // tx.value === 200_000_000_000_000_000n * ``` */ export declare const blueRepayWithdrawCollateral: ({ market: { chainId, marketParams }, args, metadata, }: BlueRepayWithdrawCollateralParams) => Readonly>;