Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

LeverageRouter

Git Source

Inherits: LeverageRouterBase

Title: LeverageRouter

Leverage router for plDXY-BEAR positions via Morpho Blue.

Flash loans USDC from Morpho → swaps to plDXY-BEAR on Curve → stakes → deposits to Morpho as collateral. Requires user to authorize this contract in Morpho before use. Uses Morpho’s fee-free flash loans for capital efficiency.

Note: security-contact: contact@plether.com

Constants

STAKED_PLDXY_BEAR

StakedToken vault for plDXY-BEAR (used as Morpho collateral).

IERC4626 public immutable STAKED_PLDXY_BEAR

Functions

constructor

Deploys LeverageRouter with Morpho market configuration.

constructor(
    address _morpho,
    address _curvePool,
    address _usdc,
    address _plDxyBear,
    address _stakedPlDxyBear,
    MarketParams memory _marketParams
) LeverageRouterBase(_morpho, _curvePool, _usdc, _plDxyBear);

Parameters

NameTypeDescription
_morphoaddressMorpho Blue protocol address.
_curvePooladdressCurve USDC/plDXY-BEAR pool address.
_usdcaddressUSDC token address.
_plDxyBearaddressplDXY-BEAR token address.
_stakedPlDxyBearaddresssplDXY-BEAR staking vault address.
_marketParamsMarketParamsMorpho market parameters for splDXY-BEAR/USDC.

openLeverage

Open a Leveraged Position in one transaction.

function openLeverage(
    uint256 principal,
    uint256 leverage,
    uint256 maxSlippageBps,
    uint256 minAmountOut,
    uint256 deadline
) external nonReentrant whenNotPaused;

Parameters

NameTypeDescription
principaluint256Amount of USDC user sends.
leverageuint256Multiplier (e.g. 3x = 3e18).
maxSlippageBpsuint256Maximum slippage tolerance in basis points (e.g., 50 = 0.5%). Capped at MAX_SLIPPAGE_BPS (1%) to limit MEV extraction.
minAmountOutuint256
deadlineuint256Unix timestamp after which the transaction reverts.

openLeverageWithPermit

Open a leveraged position with a USDC permit signature (gasless approval).

function openLeverageWithPermit(
    uint256 principal,
    uint256 leverage,
    uint256 maxSlippageBps,
    uint256 minAmountOut,
    uint256 deadline,
    uint8 v,
    bytes32 r,
    bytes32 s
) external nonReentrant whenNotPaused;

Parameters

NameTypeDescription
principaluint256Amount of USDC user sends.
leverageuint256Multiplier (e.g. 3x = 3e18).
maxSlippageBpsuint256Maximum slippage tolerance in basis points.
minAmountOutuint256
deadlineuint256Unix timestamp after which the permit and transaction revert.
vuint8Signature recovery byte.
rbytes32Signature r component.
sbytes32Signature s component.

_openLeverageCore

function _openLeverageCore(
    uint256 principal,
    uint256 leverage,
    uint256 maxSlippageBps,
    uint256 minAmountOut,
    uint256 deadline
) internal;

closeLeverage

Close a Leveraged Position in one transaction.

Queries actual debt from Morpho to ensure full repayment even if interest accrued.

function closeLeverage(
    uint256 collateralToWithdraw,
    uint256 maxSlippageBps,
    uint256 minAmountOut,
    uint256 deadline
) external nonReentrant whenNotPaused;

Parameters

NameTypeDescription
collateralToWithdrawuint256Amount of splDXY-BEAR shares to withdraw from Morpho. NOTE: This is staked token shares, not underlying plDXY-BEAR amount. Use STAKED_PLDXY_BEAR.previewRedeem() to convert shares to underlying.
maxSlippageBpsuint256Maximum slippage tolerance in basis points (e.g., 50 = 0.5%). Capped at MAX_SLIPPAGE_BPS (1%) to limit MEV extraction.
minAmountOutuint256
deadlineuint256Unix timestamp after which the transaction reverts.

addCollateral

Add collateral to an existing leveraged position.

Swaps USDC to plDXY-BEAR on Curve, stakes, and deposits to Morpho.

function addCollateral(
    uint256 usdcAmount,
    uint256 maxSlippageBps,
    uint256 minAmountOut,
    uint256 deadline
) external nonReentrant whenNotPaused;

Parameters

NameTypeDescription
usdcAmountuint256Amount of USDC to add as collateral.
maxSlippageBpsuint256Maximum slippage tolerance in basis points. Capped at MAX_SLIPPAGE_BPS (1%) to limit MEV extraction.
minAmountOutuint256
deadlineuint256Unix timestamp after which the transaction reverts.

addCollateralWithPermit

Add collateral with a USDC permit signature (gasless approval).

function addCollateralWithPermit(
    uint256 usdcAmount,
    uint256 maxSlippageBps,
    uint256 minAmountOut,
    uint256 deadline,
    uint8 v,
    bytes32 r,
    bytes32 s
) external nonReentrant whenNotPaused;

Parameters

NameTypeDescription
usdcAmountuint256Amount of USDC to add as collateral.
maxSlippageBpsuint256Maximum slippage tolerance in basis points.
minAmountOutuint256
deadlineuint256Unix timestamp after which the permit and transaction revert.
vuint8Signature recovery byte.
rbytes32Signature r component.
sbytes32Signature s component.

_addCollateralCore

function _addCollateralCore(
    uint256 usdcAmount,
    uint256 maxSlippageBps,
    uint256 minAmountOut,
    uint256 deadline
) internal;

removeCollateral

Remove collateral from an existing leveraged position.

Withdraws from Morpho, unstakes, and swaps to USDC. Reverts if the resulting position would be unhealthy.

function removeCollateral(
    uint256 collateralToWithdraw,
    uint256 maxSlippageBps,
    uint256 minAmountOut,
    uint256 deadline
) external nonReentrant whenNotPaused;

Parameters

NameTypeDescription
collateralToWithdrawuint256Amount of splDXY-BEAR shares to withdraw. NOTE: This is staked token shares, not underlying plDXY-BEAR amount.
maxSlippageBpsuint256Maximum slippage tolerance in basis points. Capped at MAX_SLIPPAGE_BPS (1%) to limit MEV extraction.
minAmountOutuint256
deadlineuint256Unix timestamp after which the transaction reverts.

onMorphoFlashLoan

Morpho flash loan callback. Routes to open or close handler.

function onMorphoFlashLoan(
    uint256 amount,
    bytes calldata data
) external override;

Parameters

NameTypeDescription
amountuint256Amount of USDC borrowed.
databytesEncoded operation parameters.

onFlashLoan

ERC-3156 flash loan callback - not used by LeverageRouter.

Always reverts as LeverageRouter only uses Morpho flash loans.

function onFlashLoan(
    address,
    address,
    uint256,
    uint256,
    bytes calldata
) external pure override returns (bytes32);

_executeOpen

Executes open leverage operation within Morpho flash loan callback.

function _executeOpen(
    uint256 loanAmount,
    bytes calldata data
) private;

Parameters

NameTypeDescription
loanAmountuint256Amount of USDC borrowed from Morpho.
databytesEncoded parameters (op, user, deadline, principal, leverage, maxSlippageBps, minPlDxyBear, minAmountOut).

_executeClose

Executes close leverage operation within Morpho flash loan callback.

function _executeClose(
    uint256 loanAmount,
    bytes calldata data
) private;

Parameters

NameTypeDescription
loanAmountuint256Amount of USDC borrowed from Morpho to repay debt.
databytesEncoded parameters (op, user, deadline, collateralToWithdraw, borrowShares, maxSlippageBps, minUsdcOut, minAmountOut).

_executeCloseNoDebt

Closes position without flash loan when user has no Morpho debt.

function _executeCloseNoDebt(
    address user,
    uint256 collateralToWithdraw,
    uint256 maxSlippageBps,
    uint256 minUsdcOut,
    uint256 minAmountOut
) private;

Parameters

NameTypeDescription
useraddressPosition owner receiving USDC.
collateralToWithdrawuint256Amount of splDXY-BEAR shares to withdraw.
maxSlippageBpsuint256Maximum slippage for Curve swap.
minUsdcOutuint256Minimum USDC to receive after swap.
minAmountOutuint256User-provided minimum USDC to receive (MEV protection).

previewOpenLeverage

Preview the result of opening a leveraged position.

function previewOpenLeverage(
    uint256 principal,
    uint256 leverage
) external view returns (uint256 loanAmount, uint256 totalUSDC, uint256 expectedPlDxyBear, uint256 expectedDebt);

Parameters

NameTypeDescription
principaluint256Amount of USDC user will send.
leverageuint256Multiplier (e.g. 3x = 3e18).

Returns

NameTypeDescription
loanAmountuint256Amount of USDC to flash loan.
totalUSDCuint256Total USDC to swap (principal + loan).
expectedPlDxyBearuint256Expected plDXY-BEAR (based on current curve price).
expectedDebtuint256Expected debt incurred (equals loan amount, no flash fee with Morpho).

previewCloseLeverage

Preview the result of closing a leveraged position.

function previewCloseLeverage(
    uint256 debtToRepay,
    uint256 collateralToWithdraw
) external view returns (uint256 expectedUSDC, uint256 flashFee, uint256 expectedReturn);

Parameters

NameTypeDescription
debtToRepayuint256Amount of USDC debt to repay.
collateralToWithdrawuint256Amount of plDXY-BEAR collateral to withdraw.

Returns

NameTypeDescription
expectedUSDCuint256Expected USDC from swap (based on current curve price).
flashFeeuint256Flash loan fee (always 0 with Morpho).
expectedReturnuint256Expected USDC returned to user after repaying flash loan.

previewAddCollateral

Preview the result of adding collateral.

function previewAddCollateral(
    uint256 usdcAmount
) external view returns (uint256 expectedPlDxyBear, uint256 expectedStakedShares);

Parameters

NameTypeDescription
usdcAmountuint256Amount of USDC to add as collateral.

Returns

NameTypeDescription
expectedPlDxyBearuint256Expected plDXY-BEAR from Curve swap.
expectedStakedSharesuint256Expected splDXY-BEAR shares to receive.

previewRemoveCollateral

Preview the result of removing collateral.

function previewRemoveCollateral(
    uint256 collateralToWithdraw
) external view returns (uint256 expectedPlDxyBear, uint256 expectedUSDC);

Parameters

NameTypeDescription
collateralToWithdrawuint256Amount of splDXY-BEAR shares to withdraw.

Returns

NameTypeDescription
expectedPlDxyBearuint256Expected plDXY-BEAR from unstaking.
expectedUSDCuint256Expected USDC from Curve swap.

Events

LeverageOpened

Emitted when a leveraged plDXY-BEAR position is opened.

event LeverageOpened(
    address indexed user,
    uint256 principal,
    uint256 leverage,
    uint256 loanAmount,
    uint256 plDxyBearReceived,
    uint256 debtIncurred,
    uint256 maxSlippageBps
);

LeverageClosed

Emitted when a leveraged plDXY-BEAR position is closed.

event LeverageClosed(
    address indexed user,
    uint256 debtRepaid,
    uint256 collateralWithdrawn,
    uint256 usdcReturned,
    uint256 maxSlippageBps
);

CollateralAdded

Emitted when collateral is added to a position.

usdcReturned is always 0 for BEAR router (full amount swapped to BEAR).

event CollateralAdded(
    address indexed user, uint256 usdcAmount, uint256 usdcReturned, uint256 collateralAdded, uint256 maxSlippageBps
);

CollateralRemoved

Emitted when collateral is removed from a position.

event CollateralRemoved(
    address indexed user, uint256 collateralWithdrawn, uint256 usdcReturned, uint256 maxSlippageBps
);