Table of Contents
- Create Pool
- Manage Liquidity
- Swap
- Margin Position
- Security Best Practices
1. Create Pool by LikwidVault
Process Flow
- Configure PoolKey with token pair parameters
- Call initialize() on LikwidVault
- Verify pool initialization status
ABI Interface
Contract: LikwidVault
Function:
solidity
function initialize(PoolKey memory key) external;Parameters:
solidity
struct PoolKey {
/// @notice The lower currency of the pool, sorted numerically
Currency currency0;
/// @notice The higher currency of the pool, sorted numerically
Currency currency1;
/// @notice The pool LP fee, capped at 1_000_000. E.g., 0.3% = 3_000
uint24 fee;
/// @notice The pool LP margin fee, capped at 1_000_000. E.g., 0.3% = 3_000
uint24 marginFee;
}2. Manage Liquidity by LikwidPairPosition
Process Flow
- Approve token transfers
- Add liquidity with addLiquidity()
- Remove liquidity with removeLiquidity()
ABI Interface
Contract: LikwidPairPosition
Functions:
solidity
// Add liquidity
function addLiquidity(
PoolKey memory key,
address recipient,
uint256 amount0,
uint256 amount1,
uint256 amount0Min,
uint256 amount1Min,
uint256 deadline
) external;
// Remove liquidity
function removeLiquidity(
uint256 tokenId,
uint128 liquidity,
uint256 amount0Min,
uint256 amount1Min,
uint256 deadline
) externalParameter Structs:
solidity
struct PoolKey {
/// @notice The lower currency of the pool, sorted numerically
Currency currency0;
/// @notice The higher currency of the pool, sorted numerically
Currency currency1;
/// @notice The pool LP fee, capped at 1_000_000. E.g., 0.3% = 3_000
uint24 fee;
/// @notice The pool LP margin fee, capped at 1_000_000. E.g., 0.3% = 3_000
uint24 marginFee;
}3. Swap by LikwidPairPosition
Process Flow
- Configure swap direction (zeroForOne)
- Set slippage tolerance
- Execute exactInput() swap
ABI Interface
Contract: LikwidPairPosition
Function:
solidity
function exactInput(SwapInputParams calldata params)
external
payable
ensure(params.deadline)
returns (uint24 swapFee, uint256 feeAmount, uint256 amountOut)Parameter Struct:
solidity
struct SwapInputParams {
PoolId poolId;
bool zeroForOne;
address to;
uint256 amountIn;
uint256 amountOutMin;
uint256 deadline;
}Code Example
typescript
// Method for obtaining poolId
/*
src/types/PoolId.sol
src/types/PoolKey.sol
*/
PoolKey key = PoolKey({currency0: currency0, currency1: currency1, fee: fee, marginFee: marginFee});
PoolId id = key.toId();4. Margin Position by LikwidMarginPosition
Process Flow
- Calculate margin requirements with getAmountIn()
- Open position with margin()
- Manage position via repay(), close()
ABI Interface
Contract: LikwidMarginPosition
Key Functions:
solidity
// Open position
function addMargin(PoolKey memory key, IMarginPositionManager.CreateParams calldata params)
external
payable
ensure(params.deadline)
returns (uint256 tokenId, uint256 borrowAmount, uint256 swapFeeAmount);
// Repay position
function repay(uint256 tokenId, uint256 repayAmount, uint256 deadline) external payable ensure(deadline);
// Close position
function close(uint256 tokenId, uint24 closeMillionth, uint256 closeAmountMin, uint256 deadline)
external
ensure(deadline);Parameter Struct:
solidity
struct CreateParams {
/// @notice true: currency1 is marginToken, false: currency0 is marginToken
bool marginForOne;
/// @notice Leverage factor of the margin position.
uint24 leverage;
/// @notice The amount of margin
uint256 marginAmount;
/// @notice The borrow amount of the margin position.When the parameter is passed in, it is 0.
uint256 borrowAmount;
/// @notice The maximum borrow amount of the margin position.
uint256 borrowAmountMax;
/// @notice The address of recipient
address recipient;
/// @notice Deadline for the transaction
uint256 deadline;
}5.Security Best Practices
- Slippage Control: Always use amountOutMin/borrowMaxAmount with 0.5% tolerance
- Liquidation Monitoring: Track position status regularly
- Gas Management: Set reasonable deadlines (≥5 minutes)
- Error Handling: Wrap calls in try/catch blocks for revert handling

