Skip to content

A minimal and secure token swapping interface built in Solidity, integrating with Uniswap V2-compatible routers. Enables users to safely swap ERC20 tokens with parameterized control over slippage, deadline, and route — all using Foundry for testing and development.

License

Notifications You must be signed in to change notification settings

ralphmcralph/SwapTokensApp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🔄 SwapTokensApp – ERC20 Token Swapping via Uniswap V2

Solidity License Framework


📌 Description

SwapTokensApp is a lightweight smart contract written in Solidity that enables ERC20 token swapping using a Uniswap V2-compatible router. It demonstrates secure integration with an external DEX protocol, safe token handling via SafeERC20, and complete test coverage using the Foundry toolchain.

This project is ideal as a foundational primitive for DeFi applications, enabling token swaps with customizable routes, slippage controls, and deadlines — all while maintaining security and minimal gas usage.


🔧 Features

  • ✅ Swap arbitrary ERC20 tokens via Uniswap V2 router
  • 🔒 Safe token transfer using SafeERC20 utilities
  • 📦 Parametric input for amount, slippage, token route, and deadline
  • ⚙️ Designed with extensibility and integration in mind
  • 🧪 Tested with real token contracts on Arbitrum Mainnet

🧱 Contract Overview

SwapTokensApp.sol

function swapTokens(
    uint256 amountIn,
    uint256 amountOutMin,
    address[] memory path,
    uint256 deadline
) external;

Parameters:

  • amountIn: amount of input tokens to swap
  • amountOutMin: minimum output tokens expected (slippage control)
  • path: array of token addresses representing swap route (e.g., [USDT, DAI])
  • deadline: UNIX timestamp after which the transaction will revert

📁 Project Structure

├── src/
│   ├── SwapTokensApp.sol           # Main smart contract
│   └── interfaces/
│       └── IV2Router02.sol         # Uniswap V2 router interface
├── test/
│   └── SwapTokensApp.t.sol         # Full Foundry test suite

🧪 Testing Details

  • ✅ Deployed with real mainnet token addresses (USDT, DAI) on Arbitrum
  • 🔄 Simulates swaps using a real Uniswap V2 router
  • 📉 Validates token balance changes pre- and post-swap
  • 🛠 Built using forge-std/Test.sol utilities
IERC20(USDT).approve(address(swapApp), amountIn);
swapApp.swapTokens(amountIn, amountOutMin, path, deadline);

NOTE: Ensure the test account is funded and impersonation is enabled via vm.startPrank().


🔐 Security & Best Practices

  • Uses SafeERC20 from OpenZeppelin to prevent faulty token behavior
  • Validates router approval and input token transfer
  • Emits a detailed SwapTokens event on every operation
  • Clean separation of concerns with external IV2Router02 interface

📄 License

This project is licensed under the GNU Lesser General Public License v3.0. For more information, see the LICENSE file.


🙋 Author

Created with a focus on clean architecture, DeFi interoperability, and real-world usability. Contributions, suggestions, and forks are welcome.

About

A minimal and secure token swapping interface built in Solidity, integrating with Uniswap V2-compatible routers. Enables users to safely swap ERC20 tokens with parameterized control over slippage, deadline, and route — all using Foundry for testing and development.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published