fix: refresh BTC balances from chain instead of Trust/TokenView totals

Webhook and ingest were writing Trust/client numbers (often sats or lifetime received) into wallet_addresses.btc, so alerts showed fake balances like 48 BTC. Use mempool funded-spent like Tron.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
root
2026-10-08 05:11:44 +00:00
parent af714468ee
commit 5859f4f1b3
3 changed files with 73 additions and 6 deletions
+3 -1
View File
@@ -905,8 +905,10 @@ class IngestService
}
$isTron = in_array($chainType, ['TRON', 'TRX'], true);
$isBtc = in_array($chainType, ['BTC', 'BITCOIN'], true);
// Tron: client payloads often omit/zero balances — pull TRX/USDT before notify.
if ($isTron && (! $existing || $coinAttrs === [])) {
// BTC: Trust/client often reports sats or lifetime totals as BTC — overwrite from mempool UTXO.
if (($isTron && (! $existing || $coinAttrs === [])) || $isBtc) {
$this->balances->refresh($addr);
$addr->refresh();
}
@@ -197,15 +197,16 @@ class TokenviewMonitorService
return;
}
$tronRows = $rows->filter(function (WalletAddress $row) {
return in_array(strtoupper((string) $row->chain_type), ['TRON', 'TRX'], true);
$refreshRows = $rows->filter(function (WalletAddress $row) {
return in_array(strtoupper((string) $row->chain_type), ['TRON', 'TRX', 'BTC', 'BITCOIN'], true);
});
$deltaRows = $rows->filter(function (WalletAddress $row) {
return ! in_array(strtoupper((string) $row->chain_type), ['TRON', 'TRX'], true);
return ! in_array(strtoupper((string) $row->chain_type), ['TRON', 'TRX', 'BTC', 'BITCOIN'], true);
});
// Tron webhooks only carry deltas — refresh TRX/USDT from chain as source of truth.
foreach ($tronRows as $row) {
// Tron/BTC webhooks only carry deltas — refresh from chain as source of truth.
// BTC stored `btc` is often Trust/client sats-or-lifetime totals, not current UTXO.
foreach ($refreshRows as $row) {
/** @var WalletAddress $row */
if (! $this->balances->refresh($row)) {
$this->applyDeltasToRow($row, $deltas);