Simple script to swap ETH for ERC20 tokens. Supports Uniswap V2 and V3.
Slippage is set to infinity.
The script simulates the transaction before sending it, if the simulation fails the corresponding error is shown and the execution stops, so you won't lose gas due to failed transactions.
v2 swap calls swapExactETHForTokens
and v3 swap calls exactInputSingle
.
To install Bun:
curl -fsSL https://bun.sh/install | bash
Clone the repo and install dependencies with
bun install
Run the script with:
bun run swap.ts v2|v3 --token <token_address> --amount <amount_in_eth> --pkey <privatekey>
Shorthand:
bun run swap.ts v2|v3 -t <token_address> -a <amount_in_eth> -p <privatekey>
v2 swap:
bun run swap.ts v2 --token 0x6982508145454Ce325dDbE47a25d4ec3d2311933 --amount 0.1 --pkey 0x736bd502819cd843c8d463cf5a50e5e2afa2362fdc5eb48867940b876919052a
v3 swap:
bun run swap.ts v3 -t 0x6982508145454Ce325dDbE47a25d4ec3d2311933 -a 0.1 -p 0x736bd502819cd843c8d463cf5a50e5e2afa2362fdc5eb48867940b876919052a
Before executing the swap the script asks for confirmation, example:
Selected Uniswap V2...
Swapping ETH for token 0x6982508145454Ce325dDbE47a25d4ec3d2311933
Input amount in ETH: 0.001
Recipient: 0xE2be2C8B1c7ea0d5e5ed0DCFc4015Aa531d01EDa
Do you want to continue? (y/n)