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.
address
required
The main factory contract address that deploys all ShibaSwap v2 pools.

Functions

createPool
Creates a new pool for the specified token pair and fee tier.
address
required
First token in the pool pair
address
required
Second token in the pool pair
uint24
required
Fee tier for the pool (500, 3000, 10000 for 0.05%, 0.3%, 1%)
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
Updates the factory owner. Only callable by the current owner.
address
required
New owner address for the factory
enableFeeAmount
Enables a new fee tier with specified tick spacing.
uint24
required
Fee amount in hundredths of a bip (1e-6)
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
Sets the initial price for the pool. Must be called before any trading can occur.
uint160
required
Initial square root price as Q64.96 fixed point number
mint
Adds liquidity to a specific price range.
address
required
Address that will receive the liquidity position
int24
required
Lower tick boundary of the position
int24
required
Upper tick boundary of the position
uint128
required
Amount of liquidity to mint
bytes
required
Additional data passed to the callback
uint256
required
Amount of token0 required for the liquidity
uint256
required
Amount of token1 required for the liquidity
swap
Executes a swap between the pool’s tokens.
address
required
Address to receive the swapped tokens
bool
required
True for token0→token1, false for token1→token0
int256
required
Amount to swap (positive for exact input, negative for exact output)
uint160
required
Price limit to prevent excessive slippage
bytes
required
Data passed to the swap callback
int256
required
Delta of token0 balance (negative = exact, positive = minimum)
int256
required
Delta of token1 balance (negative = exact, positive = minimum)
collect
Collects accumulated fees from a liquidity position.
address
required
Address to receive the collected fees
int24
required
Lower tick of the position
int24
required
Upper tick of the position
uint128
required
Maximum amount of token0 to collect
uint128
required
Maximum amount of token1 to collect
uint128
required
Amount of token0 fees collected
uint128
required
Amount of token1 fees collected
burn
Burns liquidity and returns underlying tokens to the caller.
int24
required
Lower tick of the position
int24
required
Upper tick of the position
uint128
required
Amount of liquidity to burn
uint256
required
Amount of token0 returned
uint256
required
Amount of token1 returned
flash
Executes a flash loan of pool tokens.
address
required
Address to receive the flash loan
uint256
required
Amount of token0 to flash loan
uint256
required
Amount of token1 to flash loan
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
Deploys a new pool contract.
address
required
Factory contract address
address
required
First token in the pool
address
required
Second token in the pool
uint24
required
Fee tier for the pool
int24
required
Tick spacing for the pool
address
required
Address of the deployed pool contract

Interfaces

IShibaSwapV2Factory

IShibaSwapV2Pool

IShibaSwapV2PoolDeployer

Libraries

TickMath

Provides mathematical functions for working with ticks and prices.

FullMath

Provides mathematical functions for working with fixed-point numbers.

LiquidityMath

Provides functions for liquidity calculations.

SqrtPriceMath

Provides functions for square root price calculations.

Usage Examples

Creating a Pool

Adding Liquidity

Executing a Swap

Error Codes

  • LOK: Pool is locked
  • TLM: Tick limit exceeded
  • SPL: Slippage limit exceeded
  • IIA: Invalid input amount
  • OF: Overflow
  • ZERO_LIQUIDITY: No liquidity in range
  • POOL_EXISTS: Pool already exists
  • INVALID_FEE: Fee tier not enabled
  • INVALID_TOKENS: Invalid token addresses
  • NOT_OWNER: Only owner can call
  • T: Tick out of bounds
  • PRICE_OVERFLOW: Price calculation overflow
  • LIQUIDITY_OVERFLOW: Liquidity calculation overflow

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.