I run a small board where callers buy a coin, register a wallet, and every 6 hours the top 10 split a pot. The payout side is where all the interesting failures were, so here is what the thing actually does and what bit me.
Stack: PHP 8 on a shared host, flat JSON and JSONL files with flock, no MySQL, no indexer, no long-running process. Scheduled work is an HTTP endpoint the host calls. Balances come straight from public RPC, no third party.
Reading the chain
- getTokenAccountsByOwner with a mint filter per wallet, summing balances, plus getBalance for SOL.
- Free RPC 429s the moment you batch. Sequential calls with about 150ms between them, and two endpoints to rotate through when one starts refusing.
- One silent failure worth knowing about: a rotation bug meant the sampler only ever read the first 8 wallets. Ten callers, eight balances, and the two missing wallets read as zero. The payout sheet would have refused the round and paid nobody, which is at least loud. Add an assertion that the number of wallets read equals the number registered.
The payout transaction
- One Transaction, N SystemProgram.transfer instructions, single fee payer, single signature. Ten payees cost one 5000 lamport fee instead of ten.
- getLatestBlockhash, simulateTransaction, sendTransaction, then read the transaction back with getTransaction and compare every payee and amount against the sheet that generated it.
- Rent is the trap. A system account has to stay rent exempt, so a wallet drained to the last lamport fails with InsufficientFundsForRent even though the math said the pot fit. I keep 890,880 lamports plus a fee of headroom behind on purpose.
The rule that actually caused a bug
Callers must never drop below 50% of the highest balance their wallet has ever held. That has to be recomputed from raw samples, not from a closing balance.
First version compared every sample against the caller's final high. A caller who bought more mid-round looked like they had dumped, because their earlier samples were small against a high they had not reached yet. One wallet got flagged SOLD for buying. Fix was comparing each sample against the high reached up to that point in time, which is what "never dropped below half" actually means.
Second version reset the high every round, which let a caller sit at 100% while holding a quarter of their all-time peak. Now the bar is the all-time high and a dip costs one round, not permanently.
Two things I would tell anyone doing this
- Snapshot the pot at close, not at payout time. Money that lands in the wallet between the two belongs to the next round, otherwise a deposit during the two minute gap changes who gets paid.
- Treat payout as read-after-write. Every round ends with a pass that pulls the transaction from chain and asserts payees, amounts and referral totals match what the site shows. If they disagree, it halts instead of guessing.
Happy to go into the reconcile pass or the flat-file locking if anyone is building something similar.