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.
- ✅ Swap arbitrary ERC20 tokens via Uniswap V2 router
- 🔒 Safe token transfer using
SafeERC20utilities - 📦 Parametric input for amount, slippage, token route, and deadline
- ⚙️ Designed with extensibility and integration in mind
- 🧪 Tested with real token contracts on Arbitrum Mainnet
function swapTokens(
uint256 amountIn,
uint256 amountOutMin,
address[] memory path,
uint256 deadline
) external;amountIn: amount of input tokens to swapamountOutMin: 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
├── src/
│ ├── SwapTokensApp.sol # Main smart contract
│ └── interfaces/
│ └── IV2Router02.sol # Uniswap V2 router interface
├── test/
│ └── SwapTokensApp.t.sol # Full Foundry test suite
- ✅ 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.solutilities
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().
- Uses
SafeERC20from OpenZeppelin to prevent faulty token behavior - Validates router approval and input token transfer
- Emits a detailed
SwapTokensevent on every operation - Clean separation of concerns with external
IV2Router02interface
This project is licensed under the GNU Lesser General Public License v3.0. For more information, see the LICENSE file.
Created with a focus on clean architecture, DeFi interoperability, and real-world usability. Contributions, suggestions, and forks are welcome.