"""Check the linked synthetic ERC-20 unit examples with exact fractions. Python 3 standard library only. Reads one local JSON file; no network or writes. Usage: python3 check-units.py units.json """ import json import sys from fractions import Fraction from pathlib import Path def check(data): cases = data['cases'] if not isinstance(cases, list) or not cases: raise ValueError('cases must contain calculation rows') for index, row in enumerate(cases, 1): raw, places = row['raw_integer'], row['decimals'] if not isinstance(raw, str) or not raw.isascii() or not raw.isdigit(): raise ValueError('row %s: raw_integer must be a nonnegative integer string' % index) if type(places) is not int or not 0 <= places <= 255: raise ValueError('row %s: decimals must be an integer from 0 to 255' % index) if Fraction(row['display_exact']) != Fraction(int(raw), 10 ** places): raise ValueError('row %s: displayed quantity disagrees with exact calculation' % index) comparison = data['float_comparison'] rounded = str(int(float(comparison['raw_integer']))) if rounded != comparison['float_then_integer']: raise ValueError('float_comparison does not match this Python float conversion') return {'checked_rows': len(cases), 'exact_unit_cases': 'all_match', 'float_comparison': 'matches', 'scope': 'Existing synthetic inputs only; no token lookup, wallet, price or transfer.'} if __name__ == '__main__': if len(sys.argv) != 2: raise SystemExit('Usage: python3 check-units.py units.json') print(json.dumps(check(json.loads(Path(sys.argv[1]).read_text(encoding='utf-8'))), ensure_ascii=False, indent=2))