"""Reproduce fictional small-purchase scenarios. Python 3 standard library only. Run: python analyze.py.txt . All prices, fees, minimums and holds are declared teaching inputs. No account, network, private key or market data is accessed. """ from decimal import Decimal, ROUND_DOWN, ROUND_UP, getcontext from fractions import Fraction from pathlib import Path import csv import hashlib import json import sys getcontext().prec = 50 D = Decimal UNIT = D("0.00000001") CENT = D("0.01") checks = 0 def check(condition, message): global checks assert condition, message checks += 1 def text(value): return format(value, "f") if isinstance(value, D) else value def calculate(row): b, p, ff, fr, tf, tr, w, m, o, held = [D(row[k]) for k in ( "budget_usd", "price_usd_per_ltc", "funding_fixed_usd", "funding_rate", "trade_fixed_usd", "trade_rate", "withdrawal_fee_ltc", "withdrawal_min_ltc", "order_min_ltc", "held_ltc")] check(b > 0 and p > 0 and 0 <= fr < 1 and tr >= 0, row["id"] + " valid inputs") funding = ff + b * fr trading_cash = b - funding - tf check(trading_cash > 0, row["id"] + " positive trading cash") candidate = (trading_cash / (p * (1 + tr))).quantize(UNIT, rounding=ROUND_DOWN) # Independently floor the same calculation using exact rational integers. exact = (Fraction(row["budget_usd"]) - Fraction(row["funding_fixed_usd"]) - Fraction(row["budget_usd"]) * Fraction(row["funding_rate"]) - Fraction(row["trade_fixed_usd"])) / ( Fraction(row["price_usd_per_ltc"]) * (1 + Fraction(row["trade_rate"]))) scaled = exact * 100000000 rational_floor = scaled.numerator // scaled.denominator check(candidate == D(rational_floor) / 100000000, row["id"] + " rational quantity") trade_percent_cash = candidate * p * tr residual = b - funding - tf - candidate * p - trade_percent_cash check(residual >= 0 and residual < UNIT * p * (1 + tr), row["id"] + " residual rounding cash") check(funding + tf + candidate * p + trade_percent_cash + residual == b, row["id"] + " cash reconciliation") order_ok = candidate >= o bought = candidate if order_ok else D(0) available = max(D(0), bought - held) gross_needed = max(m, w + UNIT) if row["minimum_basis"] == "gross" else m + w budget_floor = (ff + tf + max(o, gross_needed) * p * (1 + tr)) / (1 - fr) budget_cents = budget_floor.quantize(CENT, rounding=ROUND_UP) if not order_ok: status = "order minimum" elif available < gross_needed: status = "held balance" if held > 0 else "withdrawal minimum" else: status = "passes modeled checks" potential_net = max(D(0), available - w) delivered = potential_net if status == "passes modeled checks" else D(0) check(bought + held >= available and available <= bought, row["id"] + " available bounds") check(delivered == 0 or available - delivered == w, row["id"] + " quantity reconciliation") check(delivered == 0 or (available >= m if row["minimum_basis"] == "gross" else delivered >= m), row["id"] + " chosen minimum policy") check(candidate / UNIT == (candidate / UNIT).to_integral_value(), row["id"] + " litoshi precision") return {**row, "funding_total_usd": text(funding), "candidate_ltc": text(candidate), "bought_ltc": text(bought.quantize(UNIT)), "available_ltc": text(available.quantize(UNIT)), "trade_percent_usd": text(trade_percent_cash), "residual_cash_usd": text(residual), "potential_net_ltc": text(potential_net.quantize(UNIT)), "delivered_ltc": text(delivered.quantize(UNIT)), "gross_required_ltc": text(gross_needed.quantize(UNIT)), "minimum_budget_usd_no_hold": text(budget_cents), "status": status} def main(folder): inputs = json.loads((folder / "inputs.json").read_text(encoding="utf-8")) rows = [calculate(r) for r in inputs["scenarios"]] by_id = {r["id"]: r for r in rows} check(by_id["S01"]["bought_ltc"] == "0.06218905", "known 5 dollar quantity") check(by_id["S02"]["bought_ltc"] == "0.12437810", "known 10 dollar quantity") check(by_id["S03"]["minimum_budget_usd_no_hold"] == "5.02", "fixed funding threshold") check(by_id["S06"]["delivered_ltc"] == "0.09000000", "gross minimum receipt") check(by_id["S07"]["status"] == "withdrawal minimum", "net minimum rejects same gross") check(by_id["S07"]["minimum_budget_usd_no_hold"] == "8.85", "net minimum budget rounds up") check(by_id["S09"]["bought_ltc"] == "0.00000000", "below minimum means no modeled purchase") check(by_id["S10"]["delivered_ltc"] == "0.00000000", "full hold prevents withdrawal") check(by_id["S11"]["available_ltc"] == "0.03437810", "partial hold available balance") check(by_id["S12"]["delivered_ltc"] == by_id["S02"]["delivered_ltc"], "released hold matches no hold") splits = [] for scenario_id in ("S01", "S02"): row = by_id[scenario_id] balance, fee, test_gross, minimum = D(row["available_ltc"]), D(row["withdrawal_fee_ltc"]), D("0.05"), D(row["withdrawal_min_ltc"]) remainder = balance - test_gross second_ok = remainder >= minimum and remainder > fee test_receipt = test_gross - fee second_receipt = remainder - fee if second_ok else D(0) one_receipt = balance - fee split_receipt = test_receipt + second_receipt check(test_gross >= minimum and test_receipt > 0, scenario_id + " test permitted") if second_ok: check(split_receipt == balance - 2 * fee, scenario_id + " two fees reconciled") check(one_receipt - split_receipt == fee, scenario_id + " one extra fee") else: check(remainder < minimum, scenario_id + " second below minimum") check(split_receipt + remainder + fee == balance, scenario_id + " remaining balance reconciled") splits.append({"scenario_id": scenario_id, "balance_ltc": text(balance), "test_gross_ltc": text(test_gross), "test_receipt_ltc": text(test_receipt), "remainder_ltc": text(remainder), "second_withdrawal_passes": second_ok, "second_receipt_ltc": text(second_receipt), "one_withdrawal_receipt_ltc": text(one_receipt), "split_total_receipt_ltc": text(split_receipt)}) result = {"version": "1.0.0", "reviewed_date": "2026-10-11", "provenance": "fictional arithmetic scenarios, no accounts or trades", "assertions_passed": checks, "scenarios": rows, "split_tests": splits} (folder / "results.json").write_text(json.dumps(result, indent=2, ensure_ascii=False) + "\n", encoding="utf-8") for name, data in (("scenarios.csv", rows), ("split-tests.csv", splits)): with (folder / name).open("w", encoding="utf-8", newline="") as out: writer = csv.DictWriter(out, fieldnames=list(data[0])) writer.writeheader(); writer.writerows(data) assets = ["inputs.json", "analyze.py.txt", "results.json", "scenarios.csv", "split-tests.csv", "README.txt", "withdrawal-checklist.csv", "withdrawal-checklist.txt"] hashes = {name: hashlib.sha256((folder / name).read_bytes()).hexdigest() for name in assets if (folder / name).exists()} (folder / "SHA256SUMS.txt").write_text("".join(f"{digest} {name}\n" for name, digest in hashes.items()), encoding="utf-8") print(json.dumps({"assertions_passed": checks, "scenarios": len(rows), "split_tests": len(splits), "results": str(folder / "results.json")})) if __name__ == "__main__": main(Path(sys.argv[1] if len(sys.argv) > 1 else "."))