games_app/backend/src/repositories/depositScannerRepository.ts
2026-06-29 15:01:23 +02:00

35 lines
1.3 KiB
TypeScript

import { query, getClient } from '../db/connection';
/**
* Repository for deposit scanner checkpoint state.
* Tracks the last processed blockchain block per wallet type.
*/
/**
* Retrieves the last scanned block for a wallet type.
* @param walletTypeId - ID of the wallet type to query.
* @returns The last scanned block number as text, or null if no state exists.
*/
export async function getState(walletTypeId: number): Promise<{ last_block: string } | null> {
const rows = await query<{ last_block: string }[]>(
'SELECT last_block::text as last_block FROM deposit_scanner_state WHERE wallet_type_id = $1',
[walletTypeId]
);
return rows[0] || null;
}
/**
* Upserts the last scanned block for a wallet type.
* @param walletTypeId - ID of the wallet type to update.
* @param lastBlock - Latest block number that has been fully processed.
* @returns Resolves when the state row has been inserted or updated.
*/
export async function updateState(walletTypeId: number, lastBlock: bigint): Promise<void> {
await query(
`INSERT INTO deposit_scanner_state (wallet_type_id, last_block, updated_at)
VALUES ($1, $2, NOW())
ON CONFLICT (wallet_type_id) DO UPDATE SET last_block = $2, updated_at = NOW()`,
[walletTypeId, lastBlock.toString()]
);
}