LeverageRouter
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
| Name | Type | Description |
|---|---|---|
_morpho | address | Morpho Blue protocol address. |
_curvePool | address | Curve USDC/plDXY-BEAR pool address. |
_usdc | address | USDC token address. |
_plDxyBear | address | plDXY-BEAR token address. |
_stakedPlDxyBear | address | splDXY-BEAR staking vault address. |
_marketParams | MarketParams | Morpho 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
| Name | Type | Description |
|---|---|---|
principal | uint256 | Amount of USDC user sends. |
leverage | uint256 | Multiplier (e.g. 3x = 3e18). |
maxSlippageBps | uint256 | Maximum slippage tolerance in basis points (e.g., 50 = 0.5%). Capped at MAX_SLIPPAGE_BPS (1%) to limit MEV extraction. |
minAmountOut | uint256 | |
deadline | uint256 | Unix 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
| Name | Type | Description |
|---|---|---|
principal | uint256 | Amount of USDC user sends. |
leverage | uint256 | Multiplier (e.g. 3x = 3e18). |
maxSlippageBps | uint256 | Maximum slippage tolerance in basis points. |
minAmountOut | uint256 | |
deadline | uint256 | Unix timestamp after which the permit and transaction revert. |
v | uint8 | Signature recovery byte. |
r | bytes32 | Signature r component. |
s | bytes32 | Signature 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
| Name | Type | Description |
|---|---|---|
collateralToWithdraw | uint256 | Amount 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. |
maxSlippageBps | uint256 | Maximum slippage tolerance in basis points (e.g., 50 = 0.5%). Capped at MAX_SLIPPAGE_BPS (1%) to limit MEV extraction. |
minAmountOut | uint256 | |
deadline | uint256 | Unix 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
| Name | Type | Description |
|---|---|---|
usdcAmount | uint256 | Amount of USDC to add as collateral. |
maxSlippageBps | uint256 | Maximum slippage tolerance in basis points. Capped at MAX_SLIPPAGE_BPS (1%) to limit MEV extraction. |
minAmountOut | uint256 | |
deadline | uint256 | Unix 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
| Name | Type | Description |
|---|---|---|
usdcAmount | uint256 | Amount of USDC to add as collateral. |
maxSlippageBps | uint256 | Maximum slippage tolerance in basis points. |
minAmountOut | uint256 | |
deadline | uint256 | Unix timestamp after which the permit and transaction revert. |
v | uint8 | Signature recovery byte. |
r | bytes32 | Signature r component. |
s | bytes32 | Signature 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
| Name | Type | Description |
|---|---|---|
collateralToWithdraw | uint256 | Amount of splDXY-BEAR shares to withdraw. NOTE: This is staked token shares, not underlying plDXY-BEAR amount. |
maxSlippageBps | uint256 | Maximum slippage tolerance in basis points. Capped at MAX_SLIPPAGE_BPS (1%) to limit MEV extraction. |
minAmountOut | uint256 | |
deadline | uint256 | Unix 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
| Name | Type | Description |
|---|---|---|
amount | uint256 | Amount of USDC borrowed. |
data | bytes | Encoded 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
| Name | Type | Description |
|---|---|---|
loanAmount | uint256 | Amount of USDC borrowed from Morpho. |
data | bytes | Encoded 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
| Name | Type | Description |
|---|---|---|
loanAmount | uint256 | Amount of USDC borrowed from Morpho to repay debt. |
data | bytes | Encoded 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
| Name | Type | Description |
|---|---|---|
user | address | Position owner receiving USDC. |
collateralToWithdraw | uint256 | Amount of splDXY-BEAR shares to withdraw. |
maxSlippageBps | uint256 | Maximum slippage for Curve swap. |
minUsdcOut | uint256 | Minimum USDC to receive after swap. |
minAmountOut | uint256 | User-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
| Name | Type | Description |
|---|---|---|
principal | uint256 | Amount of USDC user will send. |
leverage | uint256 | Multiplier (e.g. 3x = 3e18). |
Returns
| Name | Type | Description |
|---|---|---|
loanAmount | uint256 | Amount of USDC to flash loan. |
totalUSDC | uint256 | Total USDC to swap (principal + loan). |
expectedPlDxyBear | uint256 | Expected plDXY-BEAR (based on current curve price). |
expectedDebt | uint256 | Expected 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
| Name | Type | Description |
|---|---|---|
debtToRepay | uint256 | Amount of USDC debt to repay. |
collateralToWithdraw | uint256 | Amount of plDXY-BEAR collateral to withdraw. |
Returns
| Name | Type | Description |
|---|---|---|
expectedUSDC | uint256 | Expected USDC from swap (based on current curve price). |
flashFee | uint256 | Flash loan fee (always 0 with Morpho). |
expectedReturn | uint256 | Expected 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
| Name | Type | Description |
|---|---|---|
usdcAmount | uint256 | Amount of USDC to add as collateral. |
Returns
| Name | Type | Description |
|---|---|---|
expectedPlDxyBear | uint256 | Expected plDXY-BEAR from Curve swap. |
expectedStakedShares | uint256 | Expected splDXY-BEAR shares to receive. |
previewRemoveCollateral
Preview the result of removing collateral.
function previewRemoveCollateral(
uint256 collateralToWithdraw
) external view returns (uint256 expectedPlDxyBear, uint256 expectedUSDC);
Parameters
| Name | Type | Description |
|---|---|---|
collateralToWithdraw | uint256 | Amount of splDXY-BEAR shares to withdraw. |
Returns
| Name | Type | Description |
|---|---|---|
expectedPlDxyBear | uint256 | Expected plDXY-BEAR from unstaking. |
expectedUSDC | uint256 | Expected 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
);