"""Rebuild the Litecoin.watch Black Friday figures using Python 3.10+. Run python replay.py.txt to print JSON. Pass --output DIRECTORY to also write report.json, fridays.csv and fees.csv. No API key or third-party package is used. """ import argparse import csv import gzip import hashlib import io import json from collections import Counter from datetime import date, timedelta from decimal import Decimal from pathlib import Path from statistics import median from urllib.request import Request, urlopen DAY = date(2026, 11, 27) DAYS = [(DAY - timedelta(weeks=n)).isoformat() for n in range(4, -1, -1)] METRICS = ('https://community-api.coinmetrics.io/v4/timeseries/asset-metrics' '?assets=ltc&metrics=TxCnt,BlkCnt,FeeTotNtv&start_time=2026-10-30' '&end_time=2026-11-27&page_size=40') ARCHIVE = 'https://gz.blockchair.com/litecoin/{0}/blockchair_litecoin_{0}_20261127.tsv.gz' def download(url): request = Request(url, headers={'User-Agent': 'Litecoin.watch research/1.0'}) with urlopen(request, timeout=90) as response: return response.read() def daily_rows(): data = json.loads(download(METRICS))['data'] rows = [] for point in data: day = point['time'][:10] if day in DAYS: assert point['asset'] == 'ltc' rows.append(dict(date_utc=day, blocks=int(point['BlkCnt']), transactions=int(point['TxCnt']), fees_ltc=point['FeeTotNtv'])) rows.sort(key=lambda row: row['date_utc']) assert [row['date_utc'] for row in rows] == DAYS return rows def records(raw): with gzip.GzipFile(fileobj=io.BytesIO(raw)) as stream: yield from csv.DictReader(io.TextIOWrapper(stream, encoding='utf-8'), delimiter='\t') def block_totals(raw): blocks = {} for row in records(raw): height = int(row['id']) assert row['time'][:10] == DAYS[-1] and height not in blocks blocks[height] = (int(row['transaction_count']), int(row['fee_total'])) assert max(blocks) - min(blocks) + 1 == len(blocks) return dict(blocks=len(blocks), first_height=min(blocks), last_height=max(blocks), listed_transactions=sum(x[0] for x in blocks.values()), fees_litoshi=sum(x[1] for x in blocks.values())) def transaction_fees(raw): fees, miner_rows, hashes, heights = [], Counter(), set(), set() for row in records(raw): assert row['time'][:10] == DAYS[-1] and row['hash'] not in hashes hashes.add(row['hash']) height = int(row['block_id']) heights.add(height) assert row['is_coinbase'] in ('0', '1') if row['is_coinbase'] == '1': miner_rows[height] += 1 else: fee = int(row['fee']) assert fee >= 0 fees.append(fee) assert set(miner_rows) == heights and set(miner_rows.values()) == {1} fees.sort() # Median uses both middle observations for an even sample. Other printed # percentiles use nearest rank, counting equal values at their list positions. def nearest_rank(percent): return fees[(len(fees) * percent + 99) // 100 - 1] return dict(first_height=min(heights), last_height=max(heights), listed_rows=len(hashes), coinbase_rows=sum(miner_rows.values()), measured_rows=len(fees), zero_fee_rows=fees.count(0), fees_litoshi=sum(fees), mean_litoshi=round(sum(fees) / len(fees), 3), median_litoshi=median(fees), p90_litoshi=nearest_rank(90), p99_litoshi=nearest_rank(99)) def csv_file(path, rows): with path.open('w', encoding='utf-8', newline='') as stream: writer = csv.DictWriter(stream, fieldnames=rows[0], lineterminator='\n') writer.writeheader() writer.writerows(rows) def measure(): fridays = daily_rows() archives = {} for table, calculate in [('blocks', block_totals), ('transactions', transaction_fees)]: url = ARCHIVE.format(table) raw = download(url) archives[table] = dict(url=url, sha256=hashlib.sha256(raw).hexdigest(), **calculate(raw)) series = {} for field in ('transactions', 'fees_ltc', 'blocks'): values = [Decimal(str(r[field])) for r in fridays] baseline = median(values[:-1]) series[field] = dict(four_friday_median=str(baseline), black_friday_change_pct=round(float(100 * (values[-1] / baseline - 1)), 4)) return dict(utc_day=DAYS[-1], coin_metrics_url=METRICS, fridays=fridays, comparison=series, blockchair=archives) if __name__ == '__main__': parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--output', type=Path) args = parser.parse_args() report = measure() rendered = json.dumps(report, indent=2) + '\n' if args.output: args.output.mkdir(parents=True, exist_ok=True) (args.output / 'report.json').write_text(rendered, encoding='utf-8') csv_file(args.output / 'fridays.csv', report['fridays']) metrics = report['blockchair']['transactions'] csv_file(args.output / 'fees.csv', [ {'measure': key, 'value': metrics[key]} for key in ('listed_rows', 'coinbase_rows', 'measured_rows', 'zero_fee_rows', 'fees_litoshi', 'mean_litoshi', 'median_litoshi', 'p90_litoshi', 'p99_litoshi')]) print(rendered, end='')