SwapApp is a lightweight and secure smart contract that enables token swaps via Uniswap V2 using custom parameters for input amount, slippage tolerance, swap path, and deadline. Designed for composability and ease of integration, it provides a safe interface for users to interact with the swapExactTokensForTokens method from Uniswap's router. Developed and tested using Foundry.
- 🔁 Token Swapping: Executes swaps between any ERC-20 tokens using Uniswap V2.
- 🧾 Custom Parameters: Accepts user-defined path, amountIn, slippage (
amountOutMin), and deadline - 🛡️ Safe Transfers: Utilizes
SafeERC20for secure token operations - 🔄 Approvals & Transfers: Handles ERC-20 approvals and input transfers internally
- 📢 Event Emission: Emits
SwapTokensevent with swap metadata
- Design: Stateless contract that interacts with the external
IV2Router02interface from Uniswap - Events:
SwapTokens(address tokenIn, address tokenOut, uint256 amountIn, uint256 amountOut)
- Libraries:
- Uses
SafeERC20from OpenZeppelin to prevent unsafe token transfers
- Uses
- Patterns:
- CEI (Checks-Effects-Interactions)
- No storage modification other than the router address
- Security Practices:
- Safe token handling via
safeTransferFromandapprove - Avoids approvals of unlimited amounts to minimize risk
- Safe token handling via
- Solidity:
^0.8.24 - Smart Contract Tools: Foundry
- Libraries:
- OpenZeppelin
SafeERC20 - Custom interface
IV2Router02for Uniswap interaction
- OpenZeppelin
The contract is tested using Foundry on a forked Arbitrum mainnet environment. Addresses for real tokens (USDT and DAI) and the Uniswap router are used to simulate actual swaps.
| Test Function | Purpose |
|---|---|
testHasBeenDeployedCorrectly |
Asserts correct initialization of router address |
testSwapTokensCorrectly |
Validates end-to-end token swap between USDT and DAI |
- ✔️ Deployment with correct router
- ✔️ ERC-20
approveandsafeTransferFromintegration - ✔️ Token balances checked pre and post swap
- ✔️ Correct configuration of swap path and parameters
- ✔️ Successful event emission
- Install Foundry
git clone https://github.com/your-username/swap-app.git
cd swap-app
forge installforge test
forge test --match-test testSwapTokensCorrectly -vvvvThis project is licensed under the MIT License.