The SwapFactory smart contract is a core component of the OKTC ecosystem, functioning as a factory that deploys and manages liquidity pools for token pairs. Understanding its functions is crucial for developers and users interacting with decentralized exchanges (DEXs) on the network.
This guide provides a detailed overview of the SwapFactory contract’s structure, read and write functions, events, and practical usage scenarios.
Contract Information
Contract Name: SwapFactory
Contract Address: 0x7b9F0a56cA7D20A44f603C03C6f45Db95b31e539
The contract is deployed on the OKTC chain and can be interacted with using standard Web3 tools or libraries like Ethers.js or Web3.py.
Core Read Functions
getHash Function
function getHash() public pure returns (bytes32)Returns the creation code hash of the SwapPair contract. This is useful for verifying the bytecode of newly created pair contracts.
getPair Function
function getPair(address tokenA, address tokenB) external view returns (address pair)Returns the address of the liquidity pool pair for two tokens, tokenA and tokenB. The order of the tokens does not matter. If no pair exists, it returns the zero address (0x000...000).
allPairs Function
function allPairs(uint index) external view returns (address pair)Returns the address of the pair at a specific index (zero-based). The index corresponds to the order in which pairs were created. If no pair exists at that index, it returns the zero address.
allPairsLength Function
function allPairsLength() external view returns (uint)Returns the total number of pairs created by the factory contract. This helps in iterating through all existing pairs.
feeTo Function
function feeTo() external view returns (address)Returns the address where protocol fees are sent. These fees are collected from trades and distributed to fee recipients instead of liquidity providers.
feeToSetter Function
function feeToSetter() external view returns (address)Returns the address that has the authority to set the feeTo address. This is typically a governance contract or a privileged account.
Core Write Functions
createPair Function
function createPair(address tokenA, address tokenB) external returns (address pair)Creates a new liquidity pool pair for tokenA and tokenB. The tokens must be different, and no existing pair should exist. The order of tokens is irrelevant. Upon success, it emits a PairCreated event.
👉 Explore more strategies for creating and managing pools
setFeeTo Function
function setFeeTo(address _feeTo) externalSets the address where protocol fees are sent. Only the feeToSetter can call this function.
setFeeToSetter Function
function setFeeToSetter(address _feeToSetter) externalSets the address allowed to change the feeTo address. This is a critical function and is usually restricted to governance mechanisms.
Events
PairCreated Event
event PairCreated(address indexed token0, address indexed token1, address pair, uint count)Emitted when a new pair is created. token0 and token1 are the sorted addresses of the tokens. The count parameter indicates the total number of pairs created so far (e.g., 1 for the first pair, 2 for the second).
Interface and Integration
The SwapFactory contract follows common standards for DEX factories, similar to Uniswap V2. Developers can integrate it into dApps for querying pair addresses, creating new pools, or monitoring events.
For advanced interactions, such as calculating liquidity or fees, consider using the pair contract directly or leveraging existing DeFi SDKs.
Frequently Asked Questions
What is the purpose of the SwapFactory contract?
The SwapFactory contract is used to create and manage liquidity pool pairs on OKTC. It acts as a registry for all pairs and handles critical functions like fee distribution.
How do I find the address of a token pair?
Use the getPair function by providing the addresses of the two tokens. The function will return the pair address if it exists.
Who can change the feeTo address?
Only the address set as feeToSetter can change the feeTo address. This is typically controlled by a governance process.
What happens if I try to create a pair that already exists?
The transaction will revert. Always check if a pair exists using getPair before attempting to create one.
Can I iterate through all pairs created by the factory?
Yes, use allPairsLength to get the total count and then loop through each index with allPairs to retrieve all addresses.
How are the tokens ordered in a pair?
The tokens are sorted by their address values, with the lower address as token0 and the higher as token1. This ensures consistency across all pairs.
Conclusion
The SwapFactory contract is a fundamental part of the OKTC DeFi ecosystem, enabling the creation and management of liquidity pools. By understanding its functions and events, developers can build powerful dApps and users can engage more effectively with the network.
For further technical details, refer to the official documentation or explore the contract on block explorers. Always test interactions on a testnet before deploying on mainnet.