"""Reproduce the retained OKX arithmetic offline: python analyse_capture.py DIR."""
from pathlib import Path
from decimal import Decimal, localcontext
import datetime as dt, hashlib, json, sys
p=Path(sys.argv[1]);receipt=json.loads((p/'receipt.json').read_text());data={}
for name in ['ticker','open_interest','instrument']:
 raw=(p/(name+'.json')).read_bytes();assert hashlib.sha256(raw).hexdigest()==receipt['responses'][name]['sha256'],name+' hash mismatch'
 r=json.loads(raw);assert r['code']=='0' and len(r['data'])==1 and r['data'][0]['instId']=='BTC-USDT-SWAP';data[name]=r['data'][0]
 assert receipt['responses'][name]['http_status']==200
with localcontext() as c:
 c.prec=90;V=Decimal(data['ticker']['vol24h']);O=Decimal(data['open_interest']['oi']);assert V>=0 and O>0;ratio=str(V/O)
a=json.loads((p/'analysis.json').read_text());assert ratio==a['reported_field_ratio'];assert str(int(data['open_interest']['ts'])-int(data['ticker']['ts']))==a['source_timestamp_difference_ms_oi_minus_ticker']
print(json.dumps({'passed':True,'instrument':'BTC-USDT-SWAP','ratio':ratio,'ticker_generation_utc':dt.datetime.fromtimestamp(int(data['ticker']['ts'])/1000,dt.timezone.utc).isoformat(),'oi_ts_utc':dt.datetime.fromtimestamp(int(data['open_interest']['ts'])/1000,dt.timezone.utc).isoformat(),'limitations':a['limitations']},indent=2))
