Ethereum Uniswap Tokens Swap is a Node.js script that demonstrates how to swap ERC20 tokens on the Ethereum network using Uniswap V2. It leverages ethers.js to interact with the blockchain and the Uniswap Router contract for executing token swaps. The script automatically handles gas estimation, token approval, and transaction execution, making it a useful starting point for developers looking to integrate Uniswap swaps into their projects.
- Ethereum Connection: Connects to an Ethereum node using a configurable RPC URL.
- Wallet Integration: Loads a wallet from a mnemonic phrase to sign transactions.
- Token Management: Retrieves token balances, decimals, and names.
- Dynamic Swap Amount: Allows specifying swap amounts as a percentage of the token balance (e.g.,
100%
). - Uniswap Routing: Builds the optimal swap path—direct swap or through WETH.
- Gas Estimation: Estimates gas fees for both approval and swap transactions.
- Allowance Handling: Resets token allowance before approving the Uniswap Router if needed.
- Transaction Tracking: Outputs an Etherscan transaction link after a successful swap.
- Node.js: Version 12 or later is required.
- Mnemonic Phrase: A valid mnemonic with sufficient funds (ETH for gas fees and tokens for swapping).
- Ethereum RPC Provider: An RPC endpoint (e.g., Ankr, Infura).
-
Clone the Repository:
git clone https://github.com/vkidik/eth-uniswap-swap.git cd eth-uniswap-swap
-
Install Dependencies:
npm install
Create a .env
file in the root directory of the project with the following content:
MNEMONIC="your mnemonic phrase here"
Other configuration parameters are defined in index.js
:
- AMOUNT: The amount of the input token to swap (supports percentage format, e.g.,
100%
). - INPUT_MINT: Contract address of the input ERC20 token.
- OUTPUT_MINT: Contract address of the output ERC20 token.
- RPC_URL: Ethereum RPC endpoint (e.g.,
https://rpc.ankr.com/eth
). - UNISWAP_ROUTER_ADDRESS: Address of the Uniswap V2 Router (
0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D
by default). - WETH_ADDRESS: Address of the Wrapped Ether (WETH) token.
-
Initialization:
The script loads environment variables usingdotenv
and sets up the configuration parameters. -
Ethereum Connection:
It connects to the Ethereum network using a JSON RPC provider and creates a wallet from the provided mnemonic. -
Token Data Retrieval:
The script fetches the ETH balance (for gas fees) and the balance of the specified input token. It also retrieves token details such as decimals and name. -
Amount Calculation:
If the swap amount is given as a percentage, the script calculates the corresponding amount based on the token balance. -
Uniswap Router Setup:
The script configures the Uniswap Router contract and determines the swap route. If the output token is WETH, it uses a direct route; otherwise, it routes through WETH. -
Gas Estimation:
It estimates the gas required for token approval and the swap operation, ensuring the wallet has enough ETH to cover the transaction fees. -
Token Approval:
If the current allowance for the Uniswap Router is not zero, it resets it to zero before approving the new swap amount. -
Executing the Swap:
The script callsswapExactTokensForTokens
on the Uniswap Router to perform the token swap and waits for transaction confirmation. -
Transaction Output:
Upon success, it prints a link to view the transaction on Etherscan.
Execute the script using Node.js:
node index.js
Monitor the console output for balance information, gas estimates, and the final transaction link.
The script includes error handling for:
- Insufficient ETH balance for covering gas fees.
- Failures in token approval or swap execution.
- Issues in fetching token data or gas estimates.
Descriptive error messages will be logged to the console if any step fails.
- Educational Purposes: This script is provided as an example for educational and development purposes.
- Risk Notice: Use at your own risk. Ensure you test thoroughly on a testnet before using this script on the Ethereum mainnet.
- No Liability: The authors are not liable for any financial losses incurred using this code.
Contributions, issues, and feature requests are welcome! Please open an issue or submit a pull request on the GitHub repository.