Crediting crypto deposits exactly once: webhooks, confirmations and reorgs
The most expensive bug in a crypto product is crediting a deposit twice. The second most expensive is never crediting it. Both usually come from the same place: treating a webhook as a single, reliable, in-order message. It isn't. This guide lists the rules that make crediting safe whatever the network or your servers do.
Rule 1: deduplicate on the transaction, not the event
A webhook can arrive more than once: after a retry, because your server answered slowly, or by design. BlocksReader, for example, sends deposit.confirmed at 1 confirmation and again at 3, each with its own event id.
So the event id is the wrong key. The deposit itself is identified by (chain, tx_hash, address). Make that unique in your database:
CREATE TABLE crypto_deposits (
id BIGSERIAL PRIMARY KEY,
chain TEXT NOT NULL,
tx_hash TEXT NOT NULL,
address TEXT NOT NULL,
customer_ref TEXT NOT NULL,
asset TEXT NOT NULL,
amount NUMERIC(38, 18) NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
UNIQUE (chain, tx_hash, address)
);One transaction can occasionally pay the same address more than once: two outputs on a Bitcoin-like chain, or several token transfers in one batch. It's rare, but with this key only the first is credited. Compare credited totals with your on-chain balances regularly and review any difference by hand.
Rule 2: insert and credit in one transaction
BEGIN;
INSERT INTO crypto_deposits (chain, tx_hash, address, customer_ref, asset, amount)
VALUES ($1, $2, $3, $4, $5, $6)
ON CONFLICT (chain, tx_hash, address) DO NOTHING
RETURNING id;
-- only if a row came back:
INSERT INTO balances (customer_ref, asset, amount) VALUES ($4, $5, $6)
ON CONFLICT (customer_ref, asset) DO UPDATE SET amount = balances.amount + EXCLUDED.amount;
COMMIT;The balance statement creates the row if it's the customer's first deposit. A plain UPDATE would match nothing and lose the credit. It needs a unique key on balances (customer_ref, asset).
If the deposit insert finds a duplicate, nothing is credited. If anything fails, neither happens. This one pattern makes duplicates, retries and replays all harmless.
Rule 3: verify the signature on the raw body
Anyone can POST to your webhook URL. Check the signature before trusting anything, and compute it over the raw request bytes, not re-serialized JSON (which can change spacing and key order).
// Laravel: exclude this route from CSRF
Route::post('/webhooks/blocksreader', function (Illuminate\Http\Request $request) {
parse_str(str_replace(',', '&', (string) $request->header('BR-Webhook-Signature')), $p);
$t = (int) ($p['t'] ?? 0);
abort_if(abs(time() - $t) > 300, 401);
$expected = hash_hmac('sha256', $t . '.' . $request->getContent(), config('services.blocksreader.webhook_secret'));
abort_unless(hash_equals($expected, (string) ($p['v1'] ?? '')), 401);
// ... credit once, then:
return response()->noContent(200);
});The 300-second window stops someone replaying an old, validly signed request.
Rule 4: reply fast, work after
Most webhook senders time out after a few seconds and retry (BlocksReader waits 10 seconds and makes up to 11 attempts over about a day). If your handler calls other services before replying, a slow day turns into a retry storm. Verify, store the event, reply 2xx, and do slow work in a background job.
Rule 5: respect confirmations and reorgs
A transaction in the latest block can disappear if the chain reorganizes. How many confirmations to wait for depends on the amount and the chain. A common approach is to show the deposit as pending straight away, and credit it at a confirmation count you're comfortable with. One confirmation doesn't mean the same thing on every chain, so choose a number per chain, and a higher one for large amounts.
If you credit at 1 confirmation, re-check large deposits later, on a block explorer or your own node, before you let the customer withdraw them. Waiting for the 3-confirmation event is the simpler choice when the amount matters. To do that, ignore deposit.confirmed events with fewer than 3 in confirmations, and apply the same rule in your catch-up job.
Rule 6: never trust that you received everything
Your server will be down at some point: a deploy, an outage, an expired certificate. Run a catch-up job every few minutes:
- Fetch recent confirmed deposits from your provider, newest first (
GET /v1/deposits?status=confirmed). - Go back over a fixed window, such as the last 48 hours. Don't stop at the first deposit you've already credited: an older one may still be missing.
- Pass every one through exactly the same function as the webhook. The unique key skips those already credited.
Alert when the catch-up job credits something. It means a webhook was missed, and you want to know why.
Rule 7: keep amounts exact
Don't use floating point for money. USDT has 6 decimals on Tron and Ethereum but 18 on BNB Chain; ETH has 18. Keep amounts as strings or decimal types from the webhook all the way into your database.
A checklist
- Unique key on (chain, tx_hash, address), credit in the same transaction.
- Signature checked on the raw body, with a time window.
- 2xx within a few seconds, slow work in the background.
- Confirmations chosen per chain and amount; large deposits re-checked before withdrawal.
- Catch-up job on a schedule, with an alert.
- Exact decimals end to end.
BlocksReader's starter projects for Next.js and Laravel already do all of this, and you can test them for free with simulated deposits.