-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze_base.py
More file actions
296 lines (251 loc) · 11.1 KB
/
Copy pathanalyze_base.py
File metadata and controls
296 lines (251 loc) · 11.1 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
296
#!/usr/bin/env python3
"""NFT spend analysis for the user's wallet on Base (chainid=8453).
Same NFT-centric logic as analyze_eth.py:
- All incoming ERC-721 / ERC-1155 transfers since 2021-01-01
- For each parent tx: ETH value + same-tx WETH outflow as cost
- Convert at historical ETH/USD daily price
Base-specific:
- chainid=8453 via Etherscan V2 unified API
- WETH on Base: 0x4200000000000000000000000000000000000006
- fxhash V4 deploys per-project mint contracts — detect by parameter shape
is not viable for EVM (we'd need to decode calldata); instead rely on
Etherscan's `getsourcecode` ContractName lookup for unknowns.
"""
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
WALLET = get_evm_wallet()
# Etherscan V2 free tier doesn't cover L2s — use Blockscout (Etherscan-compatible, free, no key)
BLOCKSCOUT_API = "https://base.blockscout.com/api"
START_TS = get_start_ts()
WETH_BASE = "0x4200000000000000000000000000000000000006"
ZERO_ADDRESS = "0x0000000000000000000000000000000000000000"
# Known marketplace / aggregator routers on Base
BASE_MARKETPLACES: dict[str, str] = {
# OpenSea Seaport — deploys to same CREATE2 address on every chain
"0x00000000006c3852cbef3e08e8df289169ede581": "OpenSea (Seaport 1.1)",
"0x00000000000001ad428e4906ae43d8f9852d0dd6": "OpenSea (Seaport 1.4)",
"0x0000000000000068f116a894984e2db1123eb395": "OpenSea (Seaport 1.5)",
"0x0000000000000aaeb6d7670e522a718067333cd4": "OpenSea (Seaport 1.6)",
"0x00000000000000adc04c56bf30ac9d3c0aaf14dc": "OpenSea (Seaport new)",
# Aggregators
"0xc2c862322e9c97d6244a3506655da95f05246fd8": "Reservoir V6",
# Mint platforms common on Base
"0x7777777f279eba3d3ad8f4e708545291a6fdba8b": "Zora Protocol Rewards",
"0x04e2516a2c207e84a1839755675dfd8ef6302f0a": "Zora Creator 1155",
"0x777777722d078c97c6ad07d9f36801e653e356ae": "Zora Mints Manager",
"0xa8a9b5ec5d0aa72fe88d6dd86f9c54bc14b7d3ea": "Highlight (mint)",
"0xfc01ada1b5618fc88b50057b04bf0ef691e7bbed": "Highlight (mint v2)",
# Manifold variants
"0x26bbea7803dcac346d5f5f135b57cf2c752a02be": "Manifold (1155 Creator)",
"0xe7d3982e214f9dfd53d23a7f72851a7044072250": "Manifold (Lazy Claim 1155)",
# fxhash V4 multichain — uses deployer / factory addresses
# (per-project contracts won't be in this list — they're caught via mint heuristic)
}
def etherscan(module: str, action: str, **params) -> list | dict:
p = {"module": module, "action": action, **params}
for attempt in range(5):
r = requests.get(BLOCKSCOUT_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() or "too many" in res.lower():
time.sleep(2 + attempt * 2)
continue
print(f" warn: {d}", file=sys.stderr)
return []
return d.get("result", [])
raise RuntimeError(f"Blockscout 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]:
"""Daily ETH/USD from CryptoCompare. Base uses ETH as gas, so same price."""
out: dict[str, float] = {}
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"]
oldest = candles[0]["time"]
if oldest <= earliest or oldest >= to_ts:
break
to_ts = oldest - 1
time.sleep(0.2)
return out
def lookup_contract_name(addr: str, cache: dict) -> str:
"""Look up contract name via Blockscout v2 API (cached)."""
if addr in cache:
return cache[addr]
try:
r = requests.get(f"https://base.blockscout.com/api/v2/addresses/{addr}", timeout=15)
if r.status_code == 200:
d = r.json()
name = d.get("name") or (d.get("token") or {}).get("name") or ""
else:
name = ""
except Exception:
name = ""
cache[addr] = name
return name
def main() -> None:
print(f"Fetching Base wallet data for {WALLET} via Blockscout...", file=sys.stderr)
erc721 = fetch_paginated("tokennfttx", WALLET)
erc1155 = fetch_paginated("token1155tx", WALLET)
txlist = fetch_paginated("txlist", WALLET)
weth = fetch_paginated("tokentx", WALLET, contractaddress=WETH_BASE)
print(f" ERC-721: {len(erc721)}, ERC-1155: {len(erc1155)}, txlist: {len(txlist)}, WETH: {len(weth)}", file=sys.stderr)
tx_by_hash = {t["hash"].lower(): t for t in txlist}
weth_out_wei = defaultdict(int)
for w in weth:
if w["from"].lower() == WALLET and int(w["timeStamp"]) >= START_TS:
weth_out_wei[w["hash"].lower()] += int(w["value"])
nfts_in: list[dict] = []
for n in erc721:
if n["to"].lower() == WALLET and int(n["timeStamp"]) >= START_TS:
nfts_in.append({**n, "standard": "ERC-721"})
for n in erc1155:
if n["to"].lower() == WALLET 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)
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 = BASE_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"])
if not rows:
print(f"\nNo paid NFT acquisitions found on Base for {WALLET}.")
print(f" ({free_nft_count} free/airdrop NFTs found)")
return
csv_path = Path(__file__).parent / "nft_spend_base.csv"
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 (Base, 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.4f} 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) — looking up names ===")
name_cache: dict = {}
for addr, v in sorted(unknown_to_addrs.items(), key=lambda x: -x[1]["eth_eq"])[:25]:
name = lookup_contract_name(addr, name_cache)
print(f" {addr} {name[:30]:30s} {v['count']:>3d} tx {v['eth_eq']:>8.4f} ETH-eq")
time.sleep(0.25)
if __name__ == "__main__":
main()