This project listens for newly created PancakeSwap V2 pairs and, when the WBNB side of the pair is detected, attempts to buy the partner token with a fixed WBNB amount. The trade is executed through the router using a constant slippage buffer to compute amountOutMin before submission.
Warning This bot is created for testing purposes only. It is not optimized for mainnet or real trading, and using real funds may result in losing money.
- Subscribes to the factory’s
PairCreatedevent via go-ethereum filter streams. - Loads shared ABIs from generated Go bindings (
generated/). - Computes live reserves through the pair contract to quote the output for the fixed trade size.
- Applies a fixed 5 % slippage buffer when computing
amountOutMin. - Executes
swapExactETHForTokenson PancakeSwap V2 using an account private key supplied through.env.
- Go 1.25+
abigenfrom go-ethereum (go install github.com/ethereum/go-ethereum/cmd/abigen@latest)- A BSC RPC endpoint with websocket support.
- A funded wallet private key (exported in
.env– protect this file!).
main.go– entrypoint, log subscription, reserve/quote lookups, fixed-slippage trade execution.generated/– Go bindings produced byabigen(UniswapV2Factory,UniswapV2Router,UniswapV2Pair,Erc20).abis/– canonical ABI JSON sources..env.example– required environment variables.
-
Copy environment template
cp .env.example .env
-
Edit
.envVariable Description RPC_URLWebSocket RPC endpoint (e.g. wss://bsc-testnet.nodereal.io/ws/v1/...).PRIVATE_KEYHex-encoded private key (with or without 0x). Never commit this file. -
Install dependencies
go mod tidy
-
Generate Go bindings (only required if ABI changes)
abigen --abi abis/uniswap-v2-router.json --pkg generated --type UniswapV2Router --out generated/UniswapV2Router.go abigen --abi abis/uniswap-v2-factory.json --pkg generated --type UniswapV2Factory --out generated/UniswapV2Factory.go abigen --abi abis/uniswap-v2-pair.json --pkg generated --type UniswapV2Pair --out generated/UniswapV2Pair.go abigen --abi abis/erc20.json --pkg generated --type Erc20 --out generated/Erc20.go
go run .The process will:
- Load
.envand connect to the RPC endpoint. - Subscribe to
PairCreatedevents. - For each WBNB pair:
- Fetch reserves from the pair contract.
- Quote the output via the router.
- Apply a fixed 5 % slippage buffer to derive
amountOutMin. - Submit
swapExactETHForTokensswapping exactly 0.0005 WBNB.
Console logs include block/transaction identifiers, reserves, expected output, computed slippage, and transaction hashes for successful swaps.
Trade constants live in main.go:
| Constant | Default | Description |
|---|---|---|
tradeAmountWei |
0.0005 WBNB |
Fixed input amount for each swap. |
fixedSlippageBps |
500 |
Constant slippage buffer (5 %) used to compute amountOutMin. |
Adjust and rebuild if you need different parameters.
- Format + lint:
gofmt -w main.go
- Build:
go build ./...
- Use your own fork / private key for testing; consider running against a mainnet fork (e.g., Hardhat, Anvil) to avoid real losses.