-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze_eth.py
More file actions
295 lines (254 loc) · 11.4 KB
/
Copy pathanalyze_eth.py
File metadata and controls
295 lines (254 loc) · 11.4 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env python3
"""NFT spend analysis for an Ethereum wallet.
NFT-centric: for each ERC-721 / ERC-1155 token received by the wallet, find the
originating tx and sum ETH value + same-tx WETH outflow as the buy cost. WETH
is included because OpenSea Seaport / Blur / LooksRare etc. settle bids in WETH.
Costs convert to USD at the historical ETH/USD daily price (CoinGecko).
"""
from __future__ import annotations
import csv
import sys
import time
from collections import defaultdict
from datetime import datetime, timezone
from pathlib import Path
sys.stdout.reconfigure(encoding="utf-8")
sys.stderr.reconfigure(encoding="utf-8")
try:
import truststore
truststore.inject_into_ssl()
except ImportError:
pass
import requests
from _config import get_evm_wallet, get_start_ts, get_etherscan_key
WALLET_ETH = get_evm_wallet()
ETHERSCAN_API = "https://api.etherscan.io/v2/api"
ETHERSCAN_KEY = get_etherscan_key()
CHAIN_ID = 1 # Ethereum mainnet
START_TS = get_start_ts()
WETH_ADDRESS = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
ZERO_ADDRESS = "0x0000000000000000000000000000000000000000"
# Lowercase contract -> marketplace label
ETH_MARKETPLACES: dict[str, str] = {
# OpenSea
"0x7be8076f4ea4a4ad08075c2508e481d6c946d12b": "OpenSea (Wyvern v1)",
"0x7f268357a8c2552623316e2562d90e642bb538e5": "OpenSea (Wyvern v2)",
"0x00000000006c3852cbef3e08e8df289169ede581": "OpenSea (Seaport 1.1)",
"0x00000000000001ad428e4906ae43d8f9852d0dd6": "OpenSea (Seaport 1.4)",
"0x0000000000000068f116a894984e2db1123eb395": "OpenSea (Seaport 1.5)",
"0x0000000000000aaeb6d7670e522a718067333cd4": "OpenSea (Seaport 1.6)",
"0x00000000000000adc04c56bf30ac9d3c0aaf14dc": "OpenSea (Seaport new)",
"0xc2c862322e9c97d6244a3506655da95f05246fd8": "Reservoir V6 aggregator",
"0x5d303a17883c442603b8afb54921d1b7dcc0c831": "Feral File (Exhibition V4_1)",
# LooksRare
"0x59728544b08ab483533076417fbbb2fd0b17ce3a": "LooksRare v1",
"0x0000000000e655fae4d56241588680f86e3b2377": "LooksRare v2",
# X2Y2
"0x74312363e45dcaba76c59ec49a7aa8a65a67eed3": "X2Y2",
# Blur
"0x000000000000ad05ccc4f10045630fb830b95127": "Blur Marketplace v1",
"0xb2ecfe4e4d61f8790bbb9de2d1259b9e2410cea5": "Blur Marketplace v2",
"0x39da41747a83aee658334415666f3ef92dd0d541": "Blur Marketplace v3",
"0x0000000000a39bb272e79075ade125fd351887ac": "Blur Pool",
# SuperRare / Foundation / Zora / Rarible
"0x65b49f7aee40347f5a90b714be4ef086f3fe5e2c": "SuperRare v2",
"0xcda72070e455bb31c7690a170224ce43623d0b6f": "Foundation",
"0xe468ce99444174bd3bbbed09209577d25d1ad673": "Zora v1",
"0x9757f2d2b135150bbeb65308d4a91804107cd8d6": "Rarible v2",
# Manifold / Highlight / Sound / Catalog
"0xa20a3da3f31a3a8b21b3c10cd9bcdcfe89cee0e5": "Manifold",
"0x33fd426905f149f8376e227d0c9d3340aad17af1": "fxhash Mint Pass (eth)",
# ENS (NFT names)
"0x283af0b28c62c092c9727f1ee09c02ca627eb7f5": "ENS ETHRegistrarController v1",
"0x253553366da8546fc250f225fe3d25d0c782303b": "ENS ETHRegistrarController v2",
"0x57f1887a8bf19b14fc0df6fd9b2acc9af147ea85": "ENS BaseRegistrar (NFT)",
}
def etherscan(module: str, action: str, **params) -> list | dict:
p = {"chainid": CHAIN_ID, "module": module, "action": action,
"apikey": ETHERSCAN_KEY, **params}
for attempt in range(5):
r = requests.get(ETHERSCAN_API, params=p, timeout=60)
r.raise_for_status()
d = r.json()
msg = d.get("message", "")
if d.get("status") == "0" and msg not in ("No transactions found", "No records found"):
res = str(d.get("result", ""))
if "rate limit" in res.lower() or "max rate" in res.lower():
time.sleep(2 + attempt * 2)
continue
if "invalid api key" in res.lower() or "missing api key" in res.lower():
raise RuntimeError(
"Etherscan V2 requires an API key. Get a free one at "
"https://etherscan.io/myapikey and set ETHERSCAN_API_KEY env var."
)
print(f" warn: {d}", file=sys.stderr)
return []
return d.get("result", [])
raise RuntimeError(f"Etherscan repeatedly failed for {action}")
def fetch_paginated(action: str, address: str, contractaddress: str | None = None) -> list[dict]:
out: list[dict] = []
page = 1
while True:
params: dict = {
"address": address,
"startblock": 0,
"endblock": 99999999,
"page": page,
"offset": 10000,
"sort": "asc",
}
if contractaddress:
params["contractaddress"] = contractaddress
batch = etherscan("account", action, **params)
if not isinstance(batch, list) or not batch:
break
out.extend(batch)
print(f" {action} page {page}: +{len(batch)} (total {len(out)})", file=sys.stderr)
if len(batch) < 10000:
break
page += 1
time.sleep(0.3)
return out
def fetch_eth_prices() -> dict[str, float]:
"""Fetch daily ETH/USD prices from CryptoCompare (free, no key, max 2000 days)."""
out: dict[str, float] = {}
# CryptoCompare returns last N days back from `toTs`. Walk backwards in chunks.
to_ts = int(time.time())
earliest = START_TS
while to_ts > earliest:
url = (
"https://min-api.cryptocompare.com/data/v2/histoday"
f"?fsym=ETH&tsym=USD&limit=2000&toTs={to_ts}"
)
r = requests.get(url, timeout=60)
r.raise_for_status()
data = r.json()
if data.get("Response") != "Success":
raise RuntimeError(f"CryptoCompare error: {data}")
candles = data.get("Data", {}).get("Data", [])
if not candles:
break
for c in candles:
day = datetime.fromtimestamp(c["time"], tz=timezone.utc).strftime("%Y-%m-%d")
out[day] = c["close"]
# Next page: oldest candle timestamp - 1
oldest = candles[0]["time"]
if oldest <= earliest or oldest >= to_ts:
break
to_ts = oldest - 1
time.sleep(0.2)
return out
def main() -> None:
print(f"Fetching ETH wallet data for {WALLET_ETH}...", file=sys.stderr)
erc721 = fetch_paginated("tokennfttx", WALLET_ETH)
erc1155 = fetch_paginated("token1155tx", WALLET_ETH)
txlist = fetch_paginated("txlist", WALLET_ETH)
weth = fetch_paginated("tokentx", WALLET_ETH, contractaddress=WETH_ADDRESS)
print(f" ERC-721: {len(erc721)}, ERC-1155: {len(erc1155)}, txlist: {len(txlist)}, WETH: {len(weth)}", file=sys.stderr)
# Index parent txs by hash
tx_by_hash = {t["hash"].lower(): t for t in txlist}
# Sum WETH outflow per tx hash
weth_out_wei = defaultdict(int)
for w in weth:
if w["from"].lower() == WALLET_ETH and int(w["timeStamp"]) >= START_TS:
weth_out_wei[w["hash"].lower()] += int(w["value"])
# NFT inflows (filter incoming + within date range)
nfts_in: list[dict] = []
for n in erc721:
if n["to"].lower() == WALLET_ETH and int(n["timeStamp"]) >= START_TS:
nfts_in.append({**n, "standard": "ERC-721"})
for n in erc1155:
if n["to"].lower() == WALLET_ETH and int(n["timeStamp"]) >= START_TS:
nfts_in.append({**n, "standard": "ERC-1155"})
print(f" NFT inflows since 2021: {len(nfts_in)}", file=sys.stderr)
# Group by tx
nfts_by_tx: dict[str, list[dict]] = defaultdict(list)
for n in nfts_in:
nfts_by_tx[n["hash"].lower()].append(n)
print("Fetching ETH historical USD prices...", file=sys.stderr)
eth_prices = fetch_eth_prices()
print(f" {len(eth_prices)} daily prices", file=sys.stderr)
def price_on(ts: int) -> float | None:
d = datetime.fromtimestamp(ts, tz=timezone.utc).strftime("%Y-%m-%d")
return eth_prices.get(d)
rows: list[dict] = []
free_nft_count = 0
unknown_to_addrs: dict[str, dict] = defaultdict(lambda: {"count": 0, "eth_eq": 0.0})
for tx_hash, nfts in nfts_by_tx.items():
parent = tx_by_hash.get(tx_hash)
eth_wei = int(parent["value"]) if parent else 0
weth_wei = weth_out_wei.get(tx_hash, 0)
total_eth = (eth_wei + weth_wei) / 1e18
if total_eth <= 0:
free_nft_count += 1
continue
ts = int(nfts[0]["timeStamp"])
usd_rate = price_on(ts)
usd = total_eth * usd_rate if usd_rate else None
if parent:
to_addr = parent["to"].lower()
marketplace = ETH_MARKETPLACES.get(to_addr)
is_mint = any(n["from"].lower() == ZERO_ADDRESS for n in nfts)
if marketplace is None:
if is_mint:
marketplace = "mint (direct NFT contract)"
else:
marketplace = f"unknown router ({to_addr[:10]}…)"
unknown_to_addrs[to_addr]["count"] += 1
unknown_to_addrs[to_addr]["eth_eq"] += total_eth
kind = "mint" if is_mint else "purchase"
else:
marketplace = "received via someone-else's tx"
kind = "purchase"
to_addr = None
rows.append({
"tx_hash": tx_hash,
"timestamp": datetime.fromtimestamp(ts, tz=timezone.utc).isoformat(),
"marketplace": marketplace,
"kind": kind,
"nft_count": len(nfts),
"eth": eth_wei / 1e18,
"weth": weth_wei / 1e18,
"total_eth_eq": total_eth,
"eth_usd_rate": usd_rate,
"usd": usd,
"to_address": to_addr,
})
rows.sort(key=lambda r: r["timestamp"])
csv_path = Path(__file__).parent / "nft_spend_eth.csv"
if not rows:
print("No paid NFT acquisitions found.", file=sys.stderr)
return
with csv_path.open("w", newline="", encoding="utf-8") as f:
w = csv.DictWriter(f, fieldnames=list(rows[0].keys()))
w.writeheader()
w.writerows(rows)
print(f"\nWrote {csv_path} ({len(rows)} paid acquisitions, {free_nft_count} free/airdrop NFTs skipped)\n")
total_eth = sum(r["total_eth_eq"] for r in rows)
total_usd = sum(r["usd"] for r in rows if r["usd"] is not None)
print(f"=== TOTAL NFT SPEND (ETH wallet, since 2021-01-01) ===")
print(f" {len(rows):5d} acq {total_eth:>10.4f} ETH/WETH ${total_usd:>12,.2f}\n")
def grouped(key_fn):
d: dict = defaultdict(lambda: {"eth": 0.0, "usd": 0.0, "count": 0, "nfts": 0})
for r in rows:
k = key_fn(r)
d[k]["eth"] += r["total_eth_eq"]
d[k]["usd"] += r["usd"] or 0
d[k]["count"] += 1
d[k]["nfts"] += r["nft_count"]
return d
def show(title: str, d: dict, sort_usd=True):
items = sorted(d.items(), key=lambda x: -x[1]["usd"]) if sort_usd else sorted(d.items())
print(f"=== {title} ===")
for k, v in items:
print(f" {str(k):44s} {v['count']:>4d} acq / {v['nfts']:>4d} NFTs {v['eth']:>8.3f} ETH ${v['usd']:>11,.2f}")
print()
show("BY KIND", grouped(lambda r: r["kind"]))
show("BY MARKETPLACE", grouped(lambda r: r["marketplace"]))
show("BY YEAR", grouped(lambda r: r["timestamp"][:4]), sort_usd=False)
if unknown_to_addrs:
print(f"=== UNCLASSIFIED ROUTERS ({len(unknown_to_addrs)} addresses) ===")
for addr, v in sorted(unknown_to_addrs.items(), key=lambda x: -x[1]["eth_eq"])[:25]:
print(f" {addr} {v['count']:>4d} tx {v['eth_eq']:>8.4f} ETH-eq")
if __name__ == "__main__":
main()