ShibaSwap v2’s core smart contracts provide the foundation for decentralized trading, liquidity provision, and pool management. This reference covers all core contracts, interfaces, and libraries.

ShibaSwapV2Factory

The factory contract deploys and manages all ShibaSwap v2 pools. It’s responsible for creating new pools and maintaining the registry of all deployed pools.
factory
address
required
The main factory contract address that deploys all ShibaSwap v2 pools.

Functions

createPool
function createPool(
    address tokenA,
    address tokenB,
    uint24 fee
) external returns (address pool)
Creates a new pool for the specified token pair and fee tier.
tokenA
address
required
First token in the pool pair
tokenB
address
required
Second token in the pool pair
fee
uint24
required
Fee tier for the pool (500, 3000, 10000 for 0.05%, 0.3%, 1%)
pool
address
required
Address of the newly created pool contract
Pool creation will revert if the pool already exists, fee is invalid, or token addresses are invalid.
setOwner
function setOwner(address _owner) external
Updates the factory owner. Only callable by the current owner.
_owner
address
required
New owner address for the factory
enableFeeAmount
function enableFeeAmount(uint24 fee, int24 tickSpacing) public
Enables a new fee tier with specified tick spacing.
fee
uint24
required
Fee amount in hundredths of a bip (1e-6)
tickSpacing
int24
required
Spacing between ticks for pools with this fee tier
Once enabled, fee amounts cannot be removed from the factory.

ShibaSwapV2Pool

The core pool contract that handles all trading logic, liquidity provision, and price calculations.

Key Functions

initialize
function initialize(uint160 sqrtPriceX96) external
Sets the initial price for the pool. Must be called before any trading can occur.
sqrtPriceX96
uint160
required
Initial square root price as Q64.96 fixed point number
mint
function mint(
    address recipient,
    int24 tickLower,
    int24 tickUpper,
    uint128 amount,
    bytes data
) external returns (uint256 amount0, uint256 amount1)
Adds liquidity to a specific price range.
recipient
address
required
Address that will receive the liquidity position
tickLower
int24
required
Lower tick boundary of the position
tickUpper
int24
required
Upper tick boundary of the position
amount
uint128
required
Amount of liquidity to mint
data
bytes
required
Additional data passed to the callback
amount0
uint256
required
Amount of token0 required for the liquidity
amount1
uint256
required
Amount of token1 required for the liquidity
swap
function swap(
    address recipient,
    bool zeroForOne,
    int256 amountSpecified,
    uint160 sqrtPriceLimitX96,
    bytes data
) external returns (int256 amount0, int256 amount1)
Executes a swap between the pool’s tokens.
recipient
address
required
Address to receive the swapped tokens
zeroForOne
bool
required
True for token0→token1, false for token1→token0
amountSpecified
int256
required
Amount to swap (positive for exact input, negative for exact output)
sqrtPriceLimitX96
uint160
required
Price limit to prevent excessive slippage
data
bytes
required
Data passed to the swap callback
amount0
int256
required
Delta of token0 balance (negative = exact, positive = minimum)
amount1
int256
required
Delta of token1 balance (negative = exact, positive = minimum)
collect
function collect(
    address recipient,
    int24 tickLower,
    int24 tickUpper,
    uint128 amount0Requested,
    uint128 amount1Requested
) external returns (uint128 amount0, uint128 amount1)
Collects accumulated fees from a liquidity position.
recipient
address
required
Address to receive the collected fees
tickLower
int24
required
Lower tick of the position
tickUpper
int24
required
Upper tick of the position
amount0Requested
uint128
required
Maximum amount of token0 to collect
amount1Requested
uint128
required
Maximum amount of token1 to collect
amount0
uint128
required
Amount of token0 fees collected
amount1
uint128
required
Amount of token1 fees collected
burn
function burn(
    int24 tickLower,
    int24 tickUpper,
    uint128 amount
) external returns (uint256 amount0, uint256 amount1)
Burns liquidity and returns underlying tokens to the caller.
tickLower
int24
required
Lower tick of the position
tickUpper
int24
required
Upper tick of the position
amount
uint128
required
Amount of liquidity to burn
amount0
uint256
required
Amount of token0 returned
amount1
uint256
required
Amount of token1 returned
flash
function flash(
    address recipient,
    uint256 amount0,
    uint256 amount1,
    bytes data
) external
Executes a flash loan of pool tokens.
recipient
address
required
Address to receive the flash loan
amount0
uint256
required
Amount of token0 to flash loan
amount1
uint256
required
Amount of token1 to flash loan
data
bytes
required
Data passed to the flash callback
Flash loans must be repaid with fees in the same transaction.

ShibaSwapV2PoolDeployer

Handles the deployment of new pool contracts.

Functions

deploy
function deploy(
    address factory,
    address token0,
    address token1,
    uint24 fee,
    int24 tickSpacing
) external returns (address pool)
Deploys a new pool contract.
factory
address
required
Factory contract address
token0
address
required
First token in the pool
token1
address
required
Second token in the pool
fee
uint24
required
Fee tier for the pool
tickSpacing
int24
required
Tick spacing for the pool
pool
address
required
Address of the deployed pool contract

Interfaces

IShibaSwapV2Factory

interface IShibaSwapV2Factory {
    event PoolCreated(
        address indexed token0,
        address indexed token1,
        uint24 indexed fee,
        int24 tickSpacing,
        address pool
    );
    
    function createPool(
        address tokenA,
        address tokenB,
        uint24 fee
    ) external returns (address pool);
    
    function setOwner(address _owner) external;
    
    function enableFeeAmount(uint24 fee, int24 tickSpacing) external;
    
    function getPool(
        address tokenA,
        address tokenB,
        uint24 fee
    ) external view returns (address pool);
    
    function owner() external view returns (address);
    
    function feeAmountTickSpacing(uint24 fee) external view returns (int24);
}

IShibaSwapV2Pool

interface IShibaSwapV2Pool {
    event Initialize(uint160 sqrtPriceX96, int24 tick);
    event Mint(
        address sender,
        address indexed owner,
        int24 indexed tickLower,
        int24 indexed tickUpper,
        uint128 amount,
        uint256 amount0,
        uint256 amount1
    );
    event Collect(
        address indexed owner,
        address recipient,
        int24 indexed tickLower,
        int24 indexed tickUpper,
        uint128 amount0,
        uint128 amount1
    );
    event Burn(
        address indexed owner,
        int24 indexed tickLower,
        int24 indexed tickUpper,
        uint128 amount,
        uint256 amount0,
        uint256 amount1
    );
    event Swap(
        address indexed sender,
        address indexed recipient,
        int256 amount0,
        int256 amount1,
        uint160 sqrtPriceX96,
        uint128 liquidity,
        int24 tick
    );
    event Flash(
        address indexed sender,
        address indexed recipient,
        uint256 amount0,
        uint256 amount1,
        uint256 paid0,
        uint256 paid1
    );
    
    function initialize(uint160 sqrtPriceX96) external;
    
    function mint(
        address recipient,
        int24 tickLower,
        int24 tickUpper,
        uint128 amount,
        bytes calldata data
    ) external returns (uint256 amount0, uint256 amount1);
    
    function collect(
        address recipient,
        int24 tickLower,
        int24 tickUpper,
        uint128 amount0Requested,
        uint128 amount1Requested
    ) external returns (uint128 amount0, uint128 amount1);
    
    function burn(
        int24 tickLower,
        int24 tickUpper,
        uint128 amount
    ) external returns (uint256 amount0, uint256 amount1);
    
    function swap(
        address recipient,
        bool zeroForOne,
        int256 amountSpecified,
        uint160 sqrtPriceLimitX96,
        bytes calldata data
    ) external returns (int256 amount0, int256 amount1);
    
    function flash(
        address recipient,
        uint256 amount0,
        uint256 amount1,
        bytes calldata data
    ) external;
    
    function slot0() external view returns (
        uint160 sqrtPriceX96,
        int24 tick,
        uint16 observationIndex,
        uint16 observationCardinality,
        uint16 observationCardinalityNext,
        uint8 feeProtocol,
        bool unlocked
    );
    
    function liquidity() external view returns (uint128);
    
    function ticks(int24) external view returns (
        uint128 liquidityGross,
        int128 liquidityNet,
        uint256 feeGrowthOutside0X128,
        uint256 feeGrowthOutside1X128,
        int56 tickCumulativeOutside,
        uint160 secondsPerLiquidityOutsideX128,
        uint32 secondsOutside,
        bool initialized
    );
}

IShibaSwapV2PoolDeployer

interface IShibaSwapV2PoolDeployer {
    function deploy(
        address factory,
        address token0,
        address token1,
        uint24 fee,
        int24 tickSpacing
    ) external returns (address pool);
}

Libraries

TickMath

Provides mathematical functions for working with ticks and prices.
library TickMath {
    /// @dev The minimum tick that may be passed to #getSqrtRatioAtTick computed from log base 1.0001 of 2**-128
    int24 internal constant MIN_TICK = -887272;
    /// @dev The maximum tick that may be passed to #getSqrtRatioAtTick computed from log base 1.0001 of 2**128
    int24 internal constant MAX_TICK = -MIN_TICK;
    
    /// @dev The minimum value that can be returned from #getSqrtRatioAtTick. Equivalent to getSqrtRatioAtTick(MIN_TICK)
    uint160 internal constant MIN_SQRT_RATIO = 4295128739;
    /// @dev The maximum value that can be returned from #getSqrtRatioAtTick. Equivalent to getSqrtRatioAtTick(MAX_TICK)
    uint160 internal constant MAX_SQRT_RATIO = 1461446703485210103287273052203988822378723970342;
    
    /// @dev Returns the sqrt ratio as a Q64.96 for the given tick. The sqrt ratio is computed as sqrt(1.0001)^tick
    /// @param tick the tick for which to compute the sqrt ratio
    function getSqrtRatioAtTick(int24 tick) internal pure returns (uint160 sqrtPriceX96);
    
    /// @dev Returns the tick corresponding to a given sqrt ratio, reverting if input is not between MIN_SQRT_RATIO and MAX_SQRT_RATIO
    /// @param sqrtPriceX96 the sqrt ratio for which to compute the tick as a Q64.96
    function getTickAtSqrtRatio(uint160 sqrtPriceX96) internal pure returns (int24 tick);
}

FullMath

Provides mathematical functions for working with fixed-point numbers.
library FullMath {
    /// @dev Returns the full product of two unsigned 256-bit numbers
    function mulDiv(
        uint256 a,
        uint256 b,
        uint256 denominator
    ) internal pure returns (uint256 result);
    
    /// @dev Returns the full product of two unsigned 256-bit numbers, and the intermediate value
    function mulDivRoundingUp(
        uint256 a,
        uint256 b,
        uint256 denominator
    ) internal pure returns (uint256 result);
}

LiquidityMath

Provides functions for liquidity calculations.
library LiquidityMath {
    /// @dev Add a signed liquidity delta to liquidity and revert if it overflows or underflows
    function addDelta(uint128 x, int128 y) internal pure returns (uint128 z);
}

SqrtPriceMath

Provides functions for square root price calculations.
library SqrtPriceMath {
    /// @dev Returns the next sqrt price given an input amount of token0 or token1
    function getNextSqrtPriceFromInput(
        uint160 sqrtPriceX96,
        uint128 liquidity,
        uint256 amountIn,
        bool zeroForOne
    ) internal pure returns (uint160 sqrtPriceNextX96);
    
    /// @dev Returns the next sqrt price given an output amount of token0 or token1
    function getNextSqrtPriceFromOutput(
        uint160 sqrtPriceX96,
        uint128 liquidity,
        uint256 amountOut,
        bool zeroForOne
    ) internal pure returns (uint160 sqrtPriceNextX96);
    
    /// @dev Returns the amount0 delta between two prices
    function getAmount0Delta(
        uint160 sqrtPriceAX96,
        uint160 sqrtPriceBX96,
        uint128 liquidity,
        bool roundUp
    ) internal pure returns (uint256 amount0);
    
    /// @dev Returns the amount1 delta between two prices
    function getAmount1Delta(
        uint160 sqrtPriceAX96,
        uint160 sqrtPriceBX96,
        uint128 liquidity,
        bool roundUp
    ) internal pure returns (uint256 amount1);
}

Usage Examples

Creating a Pool

// Deploy a new pool
IShibaSwapV2Factory factory = IShibaSwapV2Factory(FACTORY_ADDRESS);
address pool = factory.createPool(SHIB_ADDRESS, WETH_ADDRESS, 3000);

// Initialize the pool with a price
IShibaSwapV2Pool(pool).initialize(encodeSqrtRatioX96(1, 1)); // 1:1 price

Adding Liquidity

// Mint liquidity in a specific range
IShibaSwapV2Pool pool = IShibaSwapV2Pool(POOL_ADDRESS);
(uint256 amount0, uint256 amount1) = pool.mint(
    recipient,
    tickLower,
    tickUpper,
    liquidity,
    data
);

Executing a Swap

// Swap token0 for token1
IShibaSwapV2Pool pool = IShibaSwapV2Pool(POOL_ADDRESS);
(int256 amount0, int256 amount1) = pool.swap(
    recipient,
    true, // zeroForOne
    amountSpecified,
    sqrtPriceLimitX96,
    data
);

Error Codes

Best Practices

1

Always check pool existence

Verify a pool exists before attempting operations.
Use factory.getPool(tokenA, tokenB, fee) to check pool existence.
2

Handle slippage properly

Set appropriate price limits for swaps.
Without proper slippage protection, trades may execute at unfavorable prices.
3

Validate tick ranges

Ensure tick ranges are within valid bounds.
Ticks must be between MIN_TICK (-887272) and MAX_TICK (887272).
4

Use safe math operations

Leverage the provided libraries for mathematical operations.
The libraries handle overflow/underflow protection automatically.