-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhale_scope.py
More file actions
183 lines (154 loc) Β· 6.86 KB
/
whale_scope.py
File metadata and controls
183 lines (154 loc) Β· 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import curses
import asyncio
import json
import websockets
import time
import os
from dotenv import load_dotenv
from datetime import datetime
from decimal import Decimal
from eth_utils import decode_hex
from web3 import Web3
# Load .env file
load_dotenv()
# Program name
PROGRAM_NAME = "WhaleScope"
# Output file
OUTPUT_FILE = "whale_transactions_log.txt"
# Minimum ETH value for whale alert
MIN_ETH_VALUE = 50
# Known DEX router addresses (e.g., Uniswap, SushiSwap)
DEX_ROUTERS = {
"0xf164fc0ec4e93095b804a4795bbe1e041497b92a": "UniswapV2",
"0xe592427a0aece92de3edee1f18e0157c05861564": "UniswapV3",
"0x1b02da8cb0d097eb8d57a175b88c7d8b47997506": "SushiSwap"
}
# Infura Project ID from .env
INFURA_PROJECT_ID = os.getenv("INFURA_PROJECT_ID")
INFURA_WSS_URL = f"wss://mainnet.infura.io/ws/v3/{INFURA_PROJECT_ID}"
INFURA_HTTPS_URL = f"https://mainnet.infura.io/v3/{INFURA_PROJECT_ID}"
# Initialize Web3 HTTP instance for token name lookup
w3 = Web3(Web3.HTTPProvider(INFURA_HTTPS_URL))
# Global transaction buffer
recent_whale_txs = []
# Helper to decode token being bought from Uniswap input and fetch name if unknown
def identify_token_from_input(input_data):
try:
if len(input_data) < 138:
return None
token_address = "0x" + input_data[-40:].lower()
try:
contract = w3.eth.contract(address=token_address, abi=[{"constant":True,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"type":"function"}])
symbol = contract.functions.symbol().call()
return symbol
except Exception:
return token_address[:10] + "..."
except Exception:
return None
# Save to file
def save_transaction_to_file(tx):
with open(OUTPUT_FILE, "a") as f:
timestamp = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
line = f"[{timestamp}] From: {tx['from']} -> To: {tx['to']} | Value: {tx['value']:.2f} ETH | Type: {tx['type']} | Hash: {tx['hash']}\n"
f.write(line)
# Async function to listen to mempool transactions
def listen_to_mempool():
async def run():
global recent_whale_txs
if not INFURA_PROJECT_ID:
return # Skip if no valid key
async with websockets.connect(INFURA_WSS_URL) as ws:
subscribe_msg = json.dumps({
"jsonrpc": "2.0",
"id": 1,
"method": "eth_subscribe",
"params": ["newPendingTransactions"]
})
await ws.send(subscribe_msg)
while True:
try:
message = await asyncio.wait_for(ws.recv(), timeout=60)
data = json.loads(message)
if 'params' in data:
tx_hash = data['params']['result']
get_tx_msg = json.dumps({
"jsonrpc": "2.0",
"id": 2,
"method": "eth_getTransactionByHash",
"params": [tx_hash]
})
await ws.send(get_tx_msg)
tx_detail_msg = await asyncio.wait_for(ws.recv(), timeout=10)
tx_data = json.loads(tx_detail_msg)
tx = tx_data.get("result")
if tx:
eth_value = int(tx.get("value", "0x0"), 16) / 1e18
to_addr = tx.get("to", "").lower()
tx_input = tx.get("input", "0x")
is_token_swap = to_addr in DEX_ROUTERS
is_large_eth = eth_value >= MIN_ETH_VALUE
token_name = None
if is_token_swap and eth_value > 0:
token_name = identify_token_from_input(tx_input)
if is_large_eth or is_token_swap:
tx_note = "ETH Transfer"
if is_token_swap and eth_value > 0:
tx_note = f"Swap via {DEX_ROUTERS[to_addr]}"
if token_name:
tx_note += f" (buying {token_name})"
entry = {
"from": tx.get("from", "N/A"),
"to": to_addr,
"value": eth_value,
"hash": tx.get("hash", ""),
"type": tx_note
}
recent_whale_txs.append(entry)
save_transaction_to_file(entry)
recent_whale_txs = recent_whale_txs[-50:]
except Exception:
continue
return run()
# Format a whale transaction for display
def format_whale_transaction(tx):
return f"From: {tx['from'][:12]}... To: {tx['to'][:12]}... Value: {tx['value']:>8.2f} ETH Type: {tx['type']} Hash: {tx['hash'][:10]}..."
# Curses UI for displaying transactions
def display_whale_transactions(screen):
global recent_whale_txs
curses.curs_set(0)
screen.nodelay(True)
screen.timeout(100)
while True:
screen.clear()
screen.addstr(0, 0, f"{PROGRAM_NAME} - LIVE MEMPOOL WHALE WATCHER - Showing transactions over {MIN_ETH_VALUE} ETH", curses.color_pair(1) | curses.A_BOLD)
screen.addstr(1, 0, "=" * 106, curses.color_pair(1) | curses.A_BOLD)
screen.addstr(2, 0, "Listening to mempool...", curses.color_pair(1))
screen.addstr(3, 0, "=" * 106, curses.color_pair(1))
screen.addstr(4, 0, "This tool is for informational purposes only. Always verify on-chain data independently.", curses.color_pair(1))
screen.addstr(5, 0, "=" * 106, curses.color_pair(1))
if not INFURA_PROJECT_ID:
screen.addstr(7, 0, "β οΈ Please provide a valid Infura WebSocket Project ID in .env", curses.color_pair(2))
elif not recent_whale_txs:
screen.addstr(7, 0, "No large ETH transfers or token swaps detected yet...", curses.color_pair(2))
else:
for idx, tx in enumerate(recent_whale_txs[-(curses.LINES - 8):]):
screen.addstr(7 + idx, 0, format_whale_transaction(tx), curses.color_pair(2))
screen.refresh()
time.sleep(1)
# Initialize curses colors
def init_colors():
curses.start_color()
curses.init_pair(1, curses.COLOR_YELLOW, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLACK)
# Main entry point (runs asyncio and curses in parallel)
def main(screen):
init_colors()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.create_task(listen_to_mempool())
try:
display_whale_transactions(screen)
finally:
loop.stop()
loop.close()
curses.wrapper(main)