> For the complete documentation index, see [llms.txt](https://zenstake.gitbook.io/zenstake/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://zenstake.gitbook.io/zenstake/for-developers/token-swap-mechanism.md).

# Token Swap Mechanism

The **ZenStake Token Swap** allows users to seamlessly exchange ZEN tokens for USDT within the same blockchain network. This feature provides a fast, secure, and fee-free swapping experience, making it an essential tool for managing ZEN holdings.

### **Key Features**

* **Instant Swaps:** Convert ZEN to USDT within seconds on the same blockchain.
* **Zero Fees:** No swap fees or hidden costs.
* **Seamless Liquidity:** Always accessible within supported ZenStake networks.
* **Multi-Chain Support:** Available on Ethereum, Arbitrum, Polygon, BSC, and Optimism.

### **How It Works**

1. **User Initiates Swap**\
   The user selects the amount of ZEN to swap and confirms the transaction.

   ```
   function swapZENtoUSDT(uint256 amount) external {  
       require(IERC20(ZEN).balanceOf(msg.sender) >= amount, "Insufficient ZEN balance");  
       IERC20(ZEN).transferFrom(msg.sender, address(this), amount);  
       uint256 usdtAmount = calculateSwapRate(amount);  
       IERC20(USDT).transfer(msg.sender, usdtAmount);  
   }  
   ```
2. **Swap Rate Calculation**\
   The swap rate is calculated based on the current market price using an oracle (e.g., Chainlink).

   ```
   function calculateSwapRate(uint256 zenAmount) internal view returns (uint256) {  
       uint256 zenPrice = getZENPrice(); // Fetch ZEN price from oracle  
       return (zenAmount * zenPrice) / 1e18; // Convert ZEN to USDT equivalent  
   }  
   ```
3. **Liquidity Provision**\
   The swap uses liquidity from ZenStake's dedicated pools, ensuring smooth transactions.

   ```
   function provideLiquidity(uint256 zenAmount, uint256 usdtAmount) external {  
       IERC20(ZEN).transferFrom(msg.sender, address(this), zenAmount);  
       IERC20(USDT).transferFrom(msg.sender, address(this), usdtAmount);  
       liquidityPool.addLiquidity(zenAmount, usdtAmount);  
   }  
   ```
4. **Multi-Chain Support**\
   The swap mechanism is deployed on multiple chains (Ethereum, Arbitrum, Polygon, BSC, Optimism) using the same interface.

   ```
   function isSupportedChain(uint256 chainId) external pure returns (bool) {  
       return (chainId == 1 || chainId == 42161 || chainId == 137 || chainId == 56 || chainId == 10);  
   }  
   ```

### **Technical Details**

* **Gas Optimization:** Swaps are designed to minimize gas costs by using efficient smart contract logic.
* **Security:** Regular audits ensure the safety of the swap mechanism.
* **Oracle Integration:** Chainlink oracles provide real-time price feeds for accurate swap rates.
