Recipes
Canonical patterns for composing Lotus actions including leveraged loops, tranche refinancing, collateral swaps, and liquidation bot recipes with code examples.
Leveraged Loop with Flash Actions
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {ILotus, MarketParams} from "lotus/src/interfaces/ILotus.sol";
import {ILotusSupplyCollateralCallback} from "lotus/src/interfaces/ILotusCallbacks.sol";
import {IERC20} from "openzeppelin/token/ERC20/IERC20.sol";
contract LeverageLoop is ILotusSupplyCollateralCallback {
ILotus public immutable lotus;
address public immutable dex; // swap router
struct Params {
MarketParams marketParams;
uint256 trancheIndex;
uint256 borrowAmount;
uint256 minCollateralOut;
address user;
}
constructor(address _lotus, address _dex) {
lotus = ILotus(_lotus);
dex = _dex;
}
/// @notice Open a leveraged position in one transaction.
function lever(
MarketParams memory mp,
uint256 trancheIndex,
uint256 initialCollateral,
uint256 borrowAmount,
uint256 minCollateralOut
) external {
bytes memory data = abi.encode(Params(mp, trancheIndex, borrowAmount, minCollateralOut, msg.sender));
lotus.supplyCollateral(mp, trancheIndex, initialCollateral, msg.sender, data);
}
function onLotusSupplyCollateral(uint256 assets, bytes calldata data) external {
require(msg.sender == address(lotus), "unauthorized");
Params memory p = abi.decode(data, (Params));
address collateralToken = p.marketParams.collateralTokens[p.trancheIndex];
// 1. Borrow loan tokens against the collateral being supplied.
lotus.borrow(p.marketParams, p.trancheIndex, p.borrowAmount, 0, p.user, address(this), "");
// 2. Swap borrowed loan tokens → collateral token via DEX.
IERC20(p.marketParams.loanToken).approve(dex, p.borrowAmount);
// swapExactIn(tokenIn, tokenOut, amountIn, minOut, receiver)
// Replace with your router's interface.
// 3. Transfer initial collateral from user and approve Lotus.
IERC20(collateralToken).transferFrom(p.user, address(this), assets);
IERC20(collateralToken).approve(address(lotus), type(uint256).max);
// 4. Supply additional collateral from swap proceeds.
// lotus.supplyCollateral(p.marketParams, p.trancheIndex, additionalCollateral, p.user, "");
}
}Step
Action
Notes
Refinance Across Tranches
Collateral Swap with Bounds
Liquidation Bot
See Also
Last updated

