Shibaswap v1 is built on a system of smart contracts that work together to provide decentralized exchange functionality. This page provides an overview of the core contracts and their interactions.

Technical Reference

Shibaswap v1 consists of several core smart contracts that work together to enable decentralized token swapping:
  • Factory Contract: Creates and manages trading pairs
  • Pair Contract: Handles the actual trading logic and liquidity provision
  • Pair ERC-20 Contract: Represents liquidity provider tokens
  • Library Contract: Provides utility functions for calculations and price queries
  • Router02 Contract: Offers user-friendly interface for all trading and liquidity operations
Shibaswap v1 is based on the Uniswap v2 protocol architecture, adapted specifically for the Shiba Inu ecosystem and Shibarium network.

Factory Contract

The Factory contract is responsible for creating and managing all trading pairs on Shibaswap v1. It serves as the central registry for all pair contracts.

Key Functions

Creating a New Pair

When you want to create a new trading pair, the Factory contract:
  1. Validates that the token addresses are different
  2. Sorts the token addresses to ensure consistent pair addresses
  3. Checks if the pair already exists
  4. Deploys a new Pair contract with the specified tokens
  5. Registers the new pair in its internal mapping
Token addresses are sorted alphabetically to ensure consistent pair addresses regardless of the order they’re provided.

Pair Address Calculation

The Factory uses a deterministic address calculation to ensure the same pair address is generated for the same token combination:

Pair Contract

The Pair contract handles the core trading logic, including:
  • Token swaps
  • Liquidity provision and removal
  • Price calculations
  • Fee collection

Core State Variables

Swap Function

The main swap function handles token exchanges:

Liquidity Functions

The Pair contract provides functions for adding and removing liquidity:
Always use the Router contract for adding and removing liquidity. Direct interaction with Pair contracts can result in suboptimal amounts or failed transactions.

Pair ERC-20 Contract

Each trading pair has an associated ERC-20 token that represents liquidity provider (LP) positions. These tokens are minted when liquidity is added and burned when liquidity is removed.

Token Properties

Minting and Burning

LP tokens are minted when liquidity is added and burned when liquidity is removed:

ERC-20 Functions

The Pair contract implements standard ERC-20 functions:
LP tokens represent your share of the liquidity pool. The number of LP tokens you receive when adding liquidity is proportional to your contribution relative to the existing liquidity.

Library Contract

The Library contract provides utility functions for calculating amounts, reserves, and pair addresses. It’s used by the Router and other periphery contracts to perform calculations without making external calls.

Key Functions

Usage Examples

The Library is primarily used for:
  • Price calculations before executing swaps
  • Multi-hop routing to find optimal swap paths
  • Liquidity calculations for adding/removing liquidity
  • Reserve queries without direct pair contract calls
The Library contract contains pure and view functions only, making it gas-efficient for calculations and safe to use without state changes.

Router02 Contract

The Router02 contract is the main interface for users to interact with Shibaswap v1. It provides user-friendly functions for all trading and liquidity operations, handling the complexity of direct contract interactions.

Key Properties

Liquidity Functions

Adding Liquidity

Removing Liquidity

Swap Functions

Exact Input Swaps

Exact Output Swaps

Fee-On-Transfer Support

Router02 includes special functions for tokens with transfer fees:
Use the fee-on-transfer supporting functions when swapping tokens that have transfer fees (like some rebase tokens). These functions account for the actual amount received after fees.

Calculation Functions

The Router02 also provides the same calculation functions as the Library:

Contract Interactions

The smart contracts work together in the following flow:
  1. Factory creates new Pair contracts for token combinations
  2. Pair contracts handle swaps and liquidity operations
  3. Pair ERC-20 tokens represent LP positions and can be traded or staked
  4. Library provides calculation utilities for amounts and reserves
  5. Router02 provides user-friendly interface for all operations
  6. All contracts interact through the Router for user-friendly operations
Flow diagram showing how Factory, Pair, Pair ERC-20, Library, and Router02 contracts interact
For most users, interaction with these contracts should be done through the Shibaswap Router02 contract, which provides a simplified interface and handles complex operations like optimal swap routing.

Security Considerations

When interacting with Shibaswap v1 smart contracts:
  • Always verify contract addresses from official sources
  • Use the Router contract for swaps and liquidity operations
  • Be aware of slippage and set appropriate limits
  • Consider gas costs when performing multiple operations
  • Verify token approvals before transactions
Never send tokens directly to contract addresses. Always use the proper Router functions or approved interfaces.