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.
State Variables
STAKED_PLDXY_BEAR
StakedToken vault for plDXY-BEAR (used as Morpho collateral).
IERC4626 public immutable STAKED_PLDXY_BEAR
EXCHANGE_RATE_BUFFER_BPS
Buffer for exchange rate drift protection (1% = 100 bps).
uint256 public constant EXCHANGE_RATE_BUFFER_BPS = 100
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 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. |
deadline | uint256 | Unix timestamp after which the transaction reverts. |
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 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. |
deadline | uint256 | Unix timestamp after which the transaction reverts. |
getActualDebt
Returns the user’s current debt in this market (includes accrued interest).
function getActualDebt(
address user
) external view returns (uint256 debt);
Parameters
| Name | Type | Description |
|---|---|---|
user | address | The address to query debt for. |
Returns
| Name | Type | Description |
|---|---|---|
debt | uint256 | The actual debt amount in USDC (rounded up). |
_getActualDebt
Computes actual debt from Morpho position, rounded up to ensure full repayment.
function _getActualDebt(
address user
) internal view returns (uint256);
_getBorrowShares
Returns user’s borrow shares from Morpho position.
Used for shares-based repayment to avoid Morpho edge case when repaying exact debt.
function _getBorrowShares(
address user
) internal view returns (uint256);
_marketId
Computes market ID from marketParams.
function _marketId() internal view returns (bytes32);
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). |
_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). |
_executeCloseNoDebt
Closes position without flash loan when user has no Morpho debt.
function _executeCloseNoDebt(
address user,
uint256 collateralToWithdraw,
uint256 maxSlippageBps,
uint256 minUsdcOut
) 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. |
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. |
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
);