Recipes
Detect deposits to user wallets
Show deposits the moment they land and credit users safely once they are confirmed.
Overview
A classic setup: each user has a deposit address, and you want to show the deposit at once and credit it when it is final.
Setup
- Set
notifytoboth(the default) for fast feedback. - When you create a deposit address, add it with
labelset to the user ID andcategoriesset to["native_transfer", "token_transfer"]. - When the user opens the deposit screen, switch the address on in case it went quiet.
Handler
javascript
const USDC_BASE = "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913";
async function handleEvent(event) {
if (event.direction !== "in") return;
const isUsdc = event.type === "token_transfer" && event.data.token === USDC_BASE;
const isEth = event.type === "native_transfer";
if (!isUsdc && !isEth) return;
const user = await users.findByDepositAddress(event.chainId, event.watchedAddress);
if (!user) return;
if (!event.confirmed) {
await deposits.upsertPending({ eventId: event.id, userId: user.id, ...event.data });
await notify(user, "Deposit on its way");
return;
}
await db.transaction(async (tx) => {
const done = await tx.deposits.markConfirmed(event.id);
if (done.alreadyConfirmed) return;
await tx.balances.credit(user.id, isUsdc ? "USDC" : "ETH", BigInt(event.data.amountRaw));
});
}Notes
- Match tokens by contract address, not by symbol. Anyone can create a token called "USDC".
- Credit only on
confirmed: true, and make crediting idempotent with the eventid. - Coins forwarded by a contract, such as a withdrawal from an exchange's smart wallet, are not detected. If you must catch those, also check the balance with the Cloud API.