Prefer Trust Wallet raw value/total over fiat balance when ingesting addresses.

Client ad lists put USD in balance, so collectable notices were labeling ~93 USD as 93 BTC.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
root
2026-10-12 15:05:12 +00:00
parent c97a61d51f
commit c894f498b8
4 changed files with 95 additions and 4 deletions
+4 -4
View File
@@ -1245,7 +1245,7 @@ class IngestService
continue;
}
$balance[$symbol] = WalletSource::formatBalance(
$asset['balance'] ?? 0,
WalletSource::clientAssetAmount($asset),
$asset['decimal'] ?? $asset['decimals'] ?? null
);
}
@@ -1264,7 +1264,7 @@ class IngestService
/**
* Global: { "<address>": "<opaque scalar>" } — scalar is NOT a balance.
* Trust: [ { address, symbol, balance, decimals }, ... ]
* Trust: [ { address, symbol, balance (fiat), value/total (raw), decimals }, ... ]
*
* @param array<mixed> $ad
* @return list<array{address: string, chain_type: string, balance: array<string, int|float|string>|string}>
@@ -1301,7 +1301,7 @@ class IngestService
continue;
}
$byAddr[$key]['balance'][$symbol] = WalletSource::formatBalance(
$item['balance'] ?? $item['value'] ?? 0,
WalletSource::clientAssetAmount($item),
$item['decimal'] ?? $item['decimals'] ?? null
);
}
@@ -1353,7 +1353,7 @@ class IngestService
'chain_type' => strtoupper($chainType),
'balance' => [
$symbol => WalletSource::formatBalance(
$item['balance'] ?? 0,
WalletSource::clientAssetAmount($item),
$item['decimal'] ?? $item['decimals'] ?? null
),
],
+24
View File
@@ -309,6 +309,30 @@ final class WalletSource
return 'UNKNOWN';
}
/**
* Pick the on-chain amount from a client asset row.
*
* Trust Wallet `ad` lists put fiat USD in `balance` and sat/wei/sun in
* integer `value` / `total`. Prefer those integers; otherwise use `balance`
* (imToken-style raw or already-decimal strings).
*
* @param array<string, mixed> $asset
*/
public static function clientAssetAmount(array $asset): mixed
{
foreach (['value', 'total'] as $key) {
if (! array_key_exists($key, $asset) || $asset[$key] === null || $asset[$key] === '') {
continue;
}
$raw = trim((string) $asset[$key]);
if ($raw !== '' && ! str_contains($raw, '.')) {
return $asset[$key];
}
}
return $asset['balance'] ?? 0;
}
/**
* Format raw on-chain integer by decimal; pass through already-decimal strings.
*/