Margin Router
User-facing interface for margin trades and leveraged positions
Margin Router
The Router is the primary user-facing contract for executing swaps and managing leveraged positions. It provides convenience functions for common operations.
See also: Lux DEX for the full CLOB trading infrastructure.
State Variables
address public gov; // Governance address
address public wlux; // Wrapped LUX address
address public vault; // Vault addressFunctions
Constructor
constructor(address _vault, address _wlux)Swap Functions
swap
function swap(
address[] memory _path,
uint256 _amountIn,
uint256 _minOut,
address _receiver
) public overrideSwaps tokens through a path.
swapLUXToTokens
function swapLUXToTokens(
address[] memory _path,
uint256 _minOut,
address _receiver
) external payableSwaps LUX to tokens.
swapTokensToLUX
function swapTokensToLUX(
address[] memory _path,
uint256 _amountIn,
uint256 _minOut,
address payable _receiver
) externalSwaps tokens to LUX.
Position Functions
increasePosition
function increasePosition(
address[] memory _path,
address _indexToken,
uint256 _amountIn,
uint256 _minOut,
uint256 _sizeDelta,
bool _isLong,
uint256 _price
) externalOpens or increases a leveraged position.
Parameters:
_path: Token path (collateral to swap)_indexToken: Token to go long/short on_amountIn: Amount of input collateral_minOut: Minimum output after swap_sizeDelta: Position size in USD (30 decimals)_isLong: True for long, false for short_price: Acceptable price (max for longs, min for shorts)
increasePositionLUX
function increasePositionLUX(
address[] memory _path,
address _indexToken,
uint256 _minOut,
uint256 _sizeDelta,
bool _isLong,
uint256 _price
) external payableOpens position with LUX as collateral.
decreasePosition
function decreasePosition(
address _collateralToken,
address _indexToken,
uint256 _collateralDelta,
uint256 _sizeDelta,
bool _isLong,
address _receiver,
uint256 _price
) externalCloses or decreases a position.
decreasePositionAndSwap
function decreasePositionAndSwap(
address[] memory _path,
address _indexToken,
uint256 _collateralDelta,
uint256 _sizeDelta,
bool _isLong,
address _receiver,
uint256 _price,
uint256 _minOut
) externalCloses position and swaps output to different token.
Plugin System
approvePlugin
function approvePlugin(address _plugin) externalApproves a plugin to trade on behalf of the user.
denyPlugin
function denyPlugin(address _plugin) externalRemoves plugin approval.
Usage Examples
Open Long Position
import "@luxfi/standard/src/margin/Router.sol";
// Approve router to spend LUXD
IERC20(luxd).approve(address(router), amountIn);
// Open 10x long LUX position
address[] memory path = new address[](2);
path[0] = luxd;
path[1] = wlux;
router.increasePosition(
path,
wlux, // index token (LUX)
1000e18, // 1000 LUXD collateral
0, // min output
10000e30, // $10,000 position size
true, // long
3500e30 // max acceptable LUX price
);Close Position
// Close entire position
router.decreasePosition(
wlux, // collateral token
wlux, // index token
0, // don't withdraw collateral
10000e30, // close full position size
true, // was long
msg.sender, // receiver
3000e30 // min acceptable price
);Swap Tokens
address[] memory path = new address[](2);
path[0] = luxd;
path[1] = wlux;
router.swap(
path,
1000e18, // 1000 LUXD
0.5e18, // min 0.5 LUX
msg.sender
);Events
event Swap(
address account,
address tokenIn,
address tokenOut,
uint256 amountIn,
uint256 amountOut
);