# Add an Arena54 ledger to this project

Goal: Keep this game’s own currency with Arena54: accounts, movements that happen once, and balances that cannot go below zero.

You are a coding agent working inside my project. Do the work. Do not ask me anything you can find out by reading the project. Do not invent an endpoint, a field or an error code that is not written below.

## 1. Look at the project first

- Find the framework and language, how the project is run and tested, and every place the server grants, spends or moves the game’s currency, and where a player’s balance is shown.
- Find how the project reads configuration and environment variables, and which files git ignores.
- Any backend or script will do. Use the HTTP client and the router the project already has, or the runtime’s own if it has none. If the project has nowhere to run code off the page, stop and tell me.
- If the project has no environment example file, add one, and load the environment the way the runtime provides.
- If the project already has an Arena54 client or key variable, reuse it.
- Tell me in two or three lines what you found and what you will change, then carry on.

## 2. The contract

Base URL: `https://developers.arena54.com/api/v1` (called $BASE below).

Every answer is JSON in one envelope:

```json
{ "success": true, "data": …, "meta": { "request_id": "req_…", "mode": "test" } }
{ "success": false, "error": { "code": "…", "message": "…" }, "meta": { "request_id": "req_…", "mode": "test" } }
```

Branch on `error.code`. Log `meta.request_id` with any failure.

Keys and headers:

- Use the game’s SECRET key (it starts `a54_test_` or `a54_live_`, with no `pub_`). Read it from the environment variable `ARENA54_SECRET_KEY`, on the server only. Never send it to a page and never commit it. A secret key that arrives from a browser is refused.
- Send `Authorization: Bearer <secret key>`. A body is JSON; a `Content-Type` header is optional.
- A test key writes to test data of its own that never touches live data. Build with the test key.
- A 429 carries `Retry-After` in seconds.

Calls:

- POST /ledger/accounts: Open an account: player, treasury or sink. Again returns the same one.
    data: { "id": "<uuid>", "kind": "player", "owner": "pl_…", "balance": 0, "frozen": false }   (body: { "kind": "player" | "treasury" | "sink", "owner": "…" })
- GET /ledger/accounts/{id}: Balance and whether it is frozen.
    data: { "account": "<uuid>", "balance": 60, "frozen": false, "currency": "coins" }
- GET /ledger/accounts/{id}/rows: Statement, newest first. limit up to 100; before = the last id you saw.
    data: [{ "id": 225, "kind": "buy", "ref": "order-8841", "delta": -40, "balance_after": 60, "meta": {}, "created_at": "…" }]
- POST /ledger/accounts/{id}/freeze: Freeze, with who and why. Also /unfreeze. Each writes an audit entry.
    data: { "account": "<uuid>", "frozen": true }   (body: { "actor": "who did it", "reason": "why" })
- POST /ledger/movements: One account: makes or removes currency, once per ref.
    data: { "id": 224, "balance_after": 100, "replay": false }   (body: { "account": "<uuid>", "delta": 100, "kind": "daily-grant", "ref": "your-own-id", "meta": {} })
- POST /ledger/transfers: Several accounts, summing to zero: all legs or none.
    data: { "legs": [{ "id": 225, "balance_after": 60 }, { "id": 226, "balance_after": 40 }], "replay": false }   (body: { "ref": "…", "legs": [{ "account", "delta", "kind" }, …] } summing to zero)
- GET /ledger/supply: What exists, and what was created and destroyed, by kind. balanced: true means they agree.
    data: { "players": 60, "treasuries": 40, "exists": 100, "frozen": 0, "created": 100, "destroyed": 0, "by_kind": [{ "kind", "created", "destroyed" }], "balanced": true, "currency": "coins" }
- GET /ledger/reconcile: Every account whose balance differs from its rows. whole: true means none.
    data: { "whole": true, "accounts": [] }
- GET /ledger/totals: Count, in and out by kind and by day (Lagos), up to 90 days with ?days=.
    data: [{ "day": "2026-10-12", "kind": "buy", "count": 2, "in": 40, "out": 40 }]
- POST /ledger/exchange: Swap the player’s currency in this game for another game’s, at the rate both makers agreed. Needs the player’s token.
    data: { "swap": "uuid", "to": "other-game", "amount_in": 100, "amount_out": 250, "rate": 2.5, "balance_after": 900, "currency_in": "…", "currency_out": "…", "moved": true, "replay": false }   (send { "to": "other-game", "amount": 100, "ref": "your-own-reference" } and the player’s token as X-Arena54-Player; a test key answers "moved": false, "test": true)
- GET /ledger/me: From a page: the signed-in player’s balance.
    data: { "account": "<uuid>" | null, "balance": 60, "frozen": false, "currency": "coins" }
- GET /ledger/me/rows: From a page: the signed-in player’s statement.
    data: [{ "id", "kind", "ref", "delta", "balance_after", "meta", "created_at" }]

How it works:

- An account is a `player`, a `treasury` (the game’s own holdings) or a `sink` (where currency goes to be destroyed; it alone may go below zero). Opening the same owner again returns the same account.
- A movement on one account makes or removes currency. A transfer moves it between accounts and makes none: its deltas must sum to zero, and all legs happen or none.
- `delta` is a whole number, positive in, negative out. `kind` is a lowercase word like `daily-grant`. `ref` is your own id for this one event, 1 to 200 characters.
- The same `kind` and `ref` again is a repeat. It is answered from the stored row with `replay: true`, whatever the repeat asked for. Retry freely with the same `ref`.
- There is no edit and no delete. A correction is a new movement with its own kind.
- Both legs of a transfer may carry the same `kind`. The answer’s `legs` are in the order you sent them.
- GET /ledger/me and /ledger/me/rows are the only calls a page may make, with the publishable key and the player’s token.

Refusals:

- 409 no_route: No route both makers agreed swaps this game’s currency for that game’s.
- 409 cannot_exchange: The player cannot make that swap: they hold nothing here, or do not play that game. One answer for both.
- 409 over_route_limit: The amount is more than one swap on this route may take.
- 409 no_reserves: The other game’s exchange reserve cannot pay the swap out. Nothing moved.
- 409 ref_reused: The reference was already used for a different swap.
- 400 too_small: At the route’s rate the amount buys less than one unit.
- 429 swap_limit: The player has made as many swaps out of this game as one day allows.
- 401 missing_key: No Authorization header, or what follows “Bearer” is not shaped like a game’s key.
- 401 invalid_key: The key is shaped like one but is unknown or revoked.
- 503 module_off: Arena54 has the module switched off.
- 403 module_not_enabled: The module is not ticked for this game in the dashboard.
- 403 secret_key_required: A publishable key asked for something only a secret key may do.
- 403 secret_key_in_browser: A secret key arrived from a page.
- 409 not_enough: The account does not hold enough. Nothing moved.
- 409 frozen: The account is frozen. Nothing moved.
- 409 id_conflict: That kind and ref were already used for a different account.
- 404 no_account: This game has no account with that id.
- 400 bad_amount: A delta that is not a whole number other than zero, or a missing kind or ref.
- 400 unbalanced: A transfer whose deltas do not sum to zero.
- 400 bad_request: A body that is not the shape asked for.
- 429 limit: The plan’s allowance for the day is used up. Nothing was written.
- 429 rate_limited: Too many calls this minute. Retry-After says how long to wait.

## 3. Do this, in order

1. Add ARENA54_SECRET_KEY to the project’s environment example file with an empty value. Read it only in server code.
2. Write one small server client `ledger(method, path, body)` that returns `data` and throws an error carrying `error.code`.
3. Only if I ask for our currency to be swapped for another game’s: write a server function `exchange(playerToken, toGame, amount, ref)` that calls POST /ledger/exchange with `{ to, amount, ref }` and the player’s token as `X-Arena54-Player`. `ref` is our own id for the swap, so a retry is the same swap. Show the player `amount_out` and `currency_out` from the answer. On 409 `no_route` stop and tell me both makers must agree the route in the Arena54 dashboard. On 409 `no_reserves` tell the player to try later.
4. When a player first needs a balance, open their account: POST /ledger/accounts with kind `player` and the player’s id as owner. Store the returned account id beside the player.
5. Open one `treasury` account for what the game itself holds (for example a shop). Opening is safe to repeat, so open it when first needed and keep its id in memory or config.
6. Replace each place that grants currency with POST /ledger/movements, and each purchase with POST /ledger/transfers from the player to the treasury. `ref` must be different for every real event and the same when one event is sent twice: use an order id or a round id. If the project has no such id, add one. Do not build a ref from the player and the item alone: a second real purchase would be answered as a repeat of the first.
7. Decide every amount on the server. Never take a delta or a price from the page.
8. Show a balance from `balance_after` in the answer, or with GET /ledger/accounts/{id}. Treat 409 `not_enough` as “cannot afford”, not as an error. On 429 `limit`, answer your own caller with a 503.

## 4. Prove it works

Set `BASE` and `ARENA54_SECRET_KEY` (my TEST secret key) in the shell. If I have not given you a key, ask me for a TEST secret key once, and for nothing else. `BASE=https://developers.arena54.com/api/v1`.

- Open an account with an owner that is new each run, so the check can be repeated: `curl -X POST "$BASE/ledger/accounts" -H "Authorization: Bearer $ARENA54_SECRET_KEY" -d '{"kind":"player","owner":"prove-'$(date +%s)'"}'`. Expect HTTP 200 and an `id`.
- Grant 100: POST /ledger/movements with `{ "account": <that id>, "delta": 100, "kind": "prove-grant", "ref": "prove-1" }`, then send the same call again. Expect `balance_after: 100` both times, and `replay: true` the second time.
- Post a movement of -150 on the same account with a new ref. Expect HTTP 409 `not_enough`, and the balance still 100.
- Run: `curl "$BASE/ledger/reconcile" -H "Authorization: Bearer $ARENA54_SECRET_KEY"`. Expect `"whole": true`.
- A 503 `module_off` means Arena54 has not opened the ledger, and a 403 `module_not_enabled` means I must tick “Ledger” for the game. Tell me which.

Show me the output of each check.

## 5. Rules you must not break

- The secret key never reaches a page, a game build or a commit.
- Every amount is decided on the server.
- Every movement carries a `ref` that is the same if the same event is sent twice.
- Do not put a name, an email or a phone number in `meta`: a row can never be removed.
- This is the game’s own currency. Arena54 does not buy or sell it for naira, and nothing here moves between games.
- On 429 `limit`, the day’s movements for the plan are used up and nothing was written: tell me, do not retry in a loop.

## 6. Done when

- Grants and purchases go through the ledger with a stable ref.
- A repeat of the same event does not move currency twice.
- An overdraft is refused and shown as “cannot afford”.
- The curl checks give the answers above.

Then give me a short list of the files you changed and anything I must do by hand.

For a person, not needed for this task: https://developers.arena54.com/docs · This prompt: https://developers.arena54.com/prompts/ledger.server.md
