FUNDING PLAN
funding-plan.ts
Treasury funding pipeline: state machine, policy engine, rebalancing.
StarkWHAT THIS PATTERN TEACHES
How to model financial plans as a finite state machine: DRAFT → APPROVED → EXECUTING → SETTLED, with branded currency types (Cents), a policy engine that enforces approval thresholds and cooling periods, runway forecasting, automatic rebalancing triggers, and 3-way reconciliation matching across provider transfers, bank settlements, and platform spend.
WHEN TO USE THIS
When building a treasury planner that needs to track funding lifecycle from off-ramp initiation through bank settlement and platform spend reconciliation.
AT A GLANCE
type FundingPlanState =
| 'DRAFT' | 'APPROVED'
| 'EXECUTING' | 'SETTLED' | 'FAILED';
interface FundingPlan {
id: string;
state: FundingPlanState;
amountCents: Cents;
reason: string;
}FRAMEWORK IMPLEMENTATIONS
Python
from enum import Enum
from dataclasses import dataclass, field
from datetime import datetime
from typing import NewType
Cents = NewType("Cents", int)
class PlanState(str, Enum):
DRAFT = "DRAFT"
APPROVED = "APPROVED"
EXECUTING = "EXECUTING"
PENDING_SETTLEMENT = "PENDING_SETTLEMENT"
SETTLED = "SETTLED"
FAILED = "FAILED"
VALID_TRANSITIONS: dict[PlanState, list[PlanState]] = {
PlanState.DRAFT: [PlanState.APPROVED, PlanState.FAILED],
PlanState.APPROVED: [PlanState.EXECUTING, PlanState.FAILED],
PlanState.EXECUTING: [PlanState.PENDING_SETTLEMENT, PlanState.FAILED],
PlanState.PENDING_SETTLEMENT: [PlanState.SETTLED, PlanState.FAILED],