New
Apps 6,289Makers 6,268This week 13
Promote your app−30%
4views in
the last 30 days

Trade Lab

Interactive NBA two-team trade simulator focused on salary cap finances

Built with Claude Code

by inch6134 · Unclaimed · added Nov 3, 2026
no ratings0 comments

An interactive NBA trade simulator focused on salary-cap and CBA consequences.

Live: trade-lab.fastapicloud.dev

Stack

  • FastAPI
  • Vue
  • TypeScript
  • PostgreSQL
  • SQLAlchemy

Development setup

Start PostgreSQL, apply migrations, and load the dataset:

docker compose up -d
uv run alembic upgrade head
uv run python -m app.ingestion.run

The database URL defaults to the compose service and can be overridden with DATABASE_URL.

Run the backend test suite. It creates trade_lab_test if missing and builds its schema by running the migrations:

uv run pytest
uv run ruff check app tests migrations

Run the API:

uv run uvicorn app.main:app --reload

Frontend

Vue 3, TypeScript and Vite. The only runtime dependencies are vue and a self-hosted variable font, so the built app needs no network at runtime.

cd frontend
npm install
npm run dev          # vite on :5173, proxies nowhere — talks to the API directly

The dev server reads VITE_API_URL and falls back to http://localhost:8000; the API's CORS allows http://localhost:5173 so the two can run side by side. Type-check and build:

cd frontend
npm run type-check
npm run build         # emits frontend/dist

app/main.py serves frontend/dist itself when the directory is present — /assets/* and any other file in dist directly, everything else falls back to index.html, and an unmatched /api/* path still 404s rather than returning HTML. When dist doesn't exist (a fresh checkout, or the test suite), that mount is skipped and only the API is served.

Deployment

The application deploys as one FastAPI Cloud service serving /api/* and the built frontend from the same origin, so no CORS entry or API base URL is needed in production.

  1. Build the frontend with an empty API base so the client calls its own origin instead of localhost:8000:

    cd frontend && VITE_API_URL= npm run build
    

    frontend/dist is gitignored as a build artifact; .fastapicloudignore un-ignores it so the deploy upload includes it.

  2. Provision a Postgres database, then deploy — this is what creates the FastAPI Cloud app, so env set has nothing to target before it:

    uv run fastapi deploy
    

    fastapi deploy uploads code — it does not build or install anything locally, and it does not run migrations or ingestion.

  3. Set DATABASE_URL on the app that deploy just created, and redeploy so it picks the value up (fastapi cloud env set --help covers --secret):

    uv run fastapi cloud env set DATABASE_URL <url>
    uv run fastapi deploy
    
  4. Point a local DATABASE_URL at that same database and run the schema and dataset load from your machine, once:

    DATABASE_URL=<deployed database URL> uv run alembic upgrade head
    DATABASE_URL=<deployed database URL> uv run python -m app.ingestion.run
    

    Ingestion clears and rebuilds every table and reassigns ids, so re-running it against the same database is always safe.

  5. Verify against the live URL: load the app, select two teams, add a player and a pick from each side, simulate, and confirm the verdict, cap charts and impact list render.

API

Seven endpoints, all read-only except the simulation, which stores nothing.

GET /api/teams all 30 teams
GET /api/teams/{id}/roster contracts with a 2026-27 salary, each carrying contract salary, outgoing trade salary and incoming trade salary as separate figures
GET /api/teams/{id}/cap the three salary totals, the distance from each threshold, apron status, hard cap and cash ledgers
GET /api/teams/{id}/draft-assets picks the team currently owns and swaps it holds, each carrying whether it can move and why not
GET /api/players/{id} contract timeline, guarantee status, options, trade eligibility and last season's statistics
GET /api/dataset when the underlying dataset was last ingested
POST /api/trades/simulate both complete post-trade rosters and financial states, plus issues, warnings and insights

Money is serialised as a string because it is Decimal throughout; the frontend parses it for display and performs no CBA arithmetic of its own. The cap and simulate responses both carry the season's five league thresholds, so the shared threshold visualisation needs no separate call.

Handlers contain no CBA logic. app/db/queries.py loads rows and assembles a TradeProposal; app/domain/trade.py decides everything else.

Dataset

data/README.md documents every input file and its required fields.

uv run python -m app.ingestion.run

The run is one transaction. A missing file, a missing required field or an unknown identifier aborts it with a message naming the file and the record; nothing is partially loaded. Ingested tables are cleared and rebuilt each run, so the same inputs always produce the same database.

Every player carries a Basketball Reference id, which is the only join key; names are never matched. Three CSV exports cover rosters, contracts and statistics; the five files in data/seed/ carry draft-pick ownership, team cap state and the handful of contract exceptions the rules need.

Database

Season identity (seasons) is separate from league financial constants (season_cap_thresholds). Seasons exist for player statistics (2025-26) and for future contract salaries; only 2026-27 has modeled cap thresholds. No CBA threshold is hard-coded in application logic.

All monetary columns are NUMERIC(14, 2). A test asserts the schema contains no floating-point columns.

Domain layer

CBA calculations live in app/domain and depend on no HTTP or request context.

  • constants.py — values fixed by the CBA rather than by the season: the 2023-24 reference Salary Cap, the $7.5M expanded-allowance base, the $250,000 allowance, the band multipliers, and the 5.15% cash rate.
  • cap.py — SeasonCapContext carries a season's five seeded thresholds and derives the expanded TPE allowance, both salary-matching band thresholds and the annual cash limit from the Salary Cap. Also holds apron status and the three separate salary totals the CBA measures thresholds against: Team Salary against the Salary Cap, what a team actually pays against the Tax Level, and Apron Team Salary against both Aprons.
  • salary.py — contract salary, outgoing trade salary and incoming trade salary as three separate figures per player, plus their aggregates.
  • cba/salary_matching.py — which band applies and the resulting maximum incoming salary.
  • cba/apron_rules.py — hard caps created by a transaction and the Apron Team Salary limit each implies.
  • trade.py — TradeProposal in, TradeResult out: projects both complete post-trade rosters and financial states, runs every validator from each team's perspective, and sorts the output into errors, warnings and insights.
  • issues.py — the structured result vocabulary. Validators return Issue records with a stable code and an explanatory message, never a bare boolean.
  • cba/player_restrictions.py, cba/cash_rules.py, cba/draft_rules.py — trade eligibility, cash limits and the Second Apron cash restriction, the seven-year pick horizon, frozen picks and the Stepien Rule.

No band threshold or apron value is hard-coded in a calculation; all five come from season_cap_thresholds.

Dataset assumption

On July 31 only guaranteed compensation counts as outgoing trade salary, and the contract pages name the seasons that are not guaranteed, so those carry zero. They do not give the amount of a partially guaranteed season: three such seasons in the dataset load as fully guaranteed, which overstates what their team may send out. Guarantee dates and amendments to compensation protection are not modelled. See CBA_RULES.md "Non-Guaranteed Salary".

CBA Rules

CBA_RULES.md is the implementation specification for the supported CBA behavior.

The document defines:

  • Supported rules
  • Explicit exclusions
  • Salary matching formulas
  • Apron restrictions
  • Draft-pick rules
  • Cash rules
  • Player eligibility rules
  • Required domain test cases
  • Source hierarchy

Changes to CBA behavior should update CBA_RULES.md and its corresponding tests.

Scope and deliberate omissions

The MVP targets the CBA behavior CBA_RULES.md specifies and stops there. Deliberately out of scope, not overlooked:

  • Luxury tax bill. Tax rates and a team's repeater status aren't in CBA_RULES.md and aren't seeded, so no dollar tax figure is computed — only the Tax Level distance.
  • Incomplete-roster charge. Minimum Team Salary is seeded per team but unread; the MVP doesn't model charges for rosters under 12.
  • Team exceptions (MLE/BAE/Room). These are free-agent signing tools, outside a trade simulator's scope.
  • A trade cannot carry a team's Apron Team Salary up across an apron it was already below. That falls out of the apron rules themselves and is pinned by a test, not a separate feature.
  • Poison pill. Implemented and tested, but on July 31 extensions have generally begun, so it likely has no live case in this dataset.
  • Three-team trades, sign-and-trades, waivers, buyouts, extensions, renegotiations, existing TPEs, and draft-pick valuation or lottery simulation — excluded by PROMPT.md from the start.

Two dataset choices worth knowing before reading a number as wrong:

  • A team's Team Salary or Apron Team Salary can differ from the payroll total on its Basketball Reference contracts page. The page is a straight salary sum; Team Salary adds cap holds, and Apron Team Salary removes them. The three are expected to disagree.
  • Guarantee status is read from contract notes clause by clause. A partially guaranteed season needs a seeded amount; three seasons in this dataset (one in 2026-27) load as fully guaranteed because no seeded amount exists for them yet, which overstates what those teams could actually send out in a trade.

AI assistance

This project was built with Claude Code as a pair-programming assistant across every phase: the domain model, the CBA validators, ingestion, the API, the Vue frontend, and deployment. The trade-simulator concept, the CBA rule scope (CBA_RULES.md), and every product and architecture decision were the applicant's; Claude Code implemented against those specs, with the applicant reviewing and approving each change before it was committed. The raw dataset — contracts, cap holds, picks, protections, swaps, cash — was sourced by hand from Basketball Reference and Spotrac, not generated.

Description taken from the repository README.

Ratings

–
0 ratings
5 0
4 0
3 0
2 0
1 0

Rate this app

Sign in to rate

Comments 0

Sign in to comment

No comments yet.

Related apps

#sports · #nba · #simulator