"""Offline educational example; no wallet calls.""" def available(now, window, ceiling, settled, pending): if any(type(x) is not int or x <= 0 for x in (window, ceiling)): raise ValueError("invalid policy") if any(type(t) is not int or t > now or type(a) is not int or a <= 0 for t,a in settled) or any(type(a) is not int or a <= 0 for a in pending): raise ValueError("invalid ledger") return max(0, ceiling - sum(a for t,a in settled if now-window < t <= now) - sum(pending)) assert available(100, 20, 1000, [(80, 200), (81, 300)], [400]) == 300 assert available(1000, 20, 1000, [], [400]) == 600