After trying to recuparate money from cold wallet16

This commit is contained in:
Your Name 2026-08-29 17:24:28 +02:00
parent 01e23b3817
commit 0548e64c42
2 changed files with 66 additions and 0 deletions

View File

@ -50,6 +50,27 @@ export async function upsertSnapshot(input: {
}
}
/** Best-effort: subtract a swept amount when live balance read fails after sweep. */
export async function decreaseSnapshotBy(input: {
userId: string;
walletTypeId: number;
amount: bigint;
}): Promise<void> {
if (input.amount <= 0n) return;
try {
await query(
`UPDATE deposit_hd_snapshots
SET on_chain_amount = GREATEST(0, on_chain_amount - $3::numeric),
last_checked_at = NOW()
WHERE user_id = $1 AND wallet_type_id = $2`,
[input.userId, input.walletTypeId, input.amount.toString()]
);
} catch (e) {
if (isUndefinedTable(e)) return;
throw e;
}
}
export async function listPositiveSnapshots(): Promise<HdSnapshotRow[]> {
try {
return await query<HdSnapshotRow[]>(

View File

@ -207,6 +207,51 @@ async function executeSweep(sweep: depositSweepRepository.DepositSweepRow): Prom
}
await depositSweepRepository.updateSweepStatus(sweep.id, 'completed', { blockchainTxId: txId });
// Treasury "USDT still on HD" reads deposit_hd_snapshots; refresh so the row clears
// without waiting for the next deposit-monitor poll / manual Scan & credit.
try {
let remaining = 0n;
if (walletType.network === 'TRON') {
const trc20Contract =
walletType.code === USDT_TRC20_CODE && config.blockchain.tron.usdtTrc20Contract
? config.blockchain.tron.usdtTrc20Contract
: walletType.contract_address;
if (trc20Contract) {
remaining = await tronService.getTrc20BalanceRaw(sweep.from_address, trc20Contract);
}
} else if (walletType.network === 'BSC' || walletType.code === USDT_BEP20_CODE) {
const contract = bscService.resolveUsdtContract(walletType.contract_address);
remaining = await bscService.getUsdtBalanceRaw(sweep.from_address, contract);
} else if (walletType.network === 'POLYGON') {
remaining = await polygonService.getUsdcBalanceRaw(sweep.from_address);
}
const { upsertSnapshot } = await import('../repositories/depositHdSnapshotRepository');
await upsertSnapshot({
userId: sweep.user_id,
walletTypeId: sweep.wallet_type_id,
address: sweep.from_address,
onChainAmount: remaining,
});
} catch (e) {
console.warn(
`[sweep] HD balance re-read failed after sweep ${sweep.id}:`,
e instanceof Error ? e.message : e
);
try {
const { decreaseSnapshotBy } = await import('../repositories/depositHdSnapshotRepository');
await decreaseSnapshotBy({
userId: sweep.user_id,
walletTypeId: sweep.wallet_type_id,
amount,
});
} catch (e2) {
console.warn(
`[sweep] HD snapshot decrease failed after sweep ${sweep.id}:`,
e2 instanceof Error ? e2.message : e2
);
}
}
}
export async function processPendingSweeps(opts?: {