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:
@@ -187,6 +187,70 @@ class TokenviewWebhookTest extends TestCase
|
||||
});
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function webhook_refreshes_btc_balance_from_chain_instead_of_delta(): void
|
||||
{
|
||||
config(['coruna.tokenview.sign_key' => '']);
|
||||
Http::fake(function ($request) {
|
||||
$url = $request->url();
|
||||
if (str_contains($url, 'mempool.space') && str_contains($url, '/address/')) {
|
||||
return Http::response([
|
||||
'chain_stats' => [
|
||||
'funded_txo_sum' => 61436,
|
||||
'spent_txo_sum' => 0,
|
||||
'tx_count' => 1,
|
||||
],
|
||||
'mempool_stats' => [
|
||||
'funded_txo_sum' => 0,
|
||||
'spent_txo_sum' => 0,
|
||||
'tx_count' => 0,
|
||||
],
|
||||
], 200);
|
||||
}
|
||||
if (str_contains($url, 'api.telegram.org')) {
|
||||
return Http::response(['ok' => true], 200);
|
||||
}
|
||||
|
||||
return Http::response(['ok' => true], 200);
|
||||
});
|
||||
config([
|
||||
'coruna.telegram.bot_token' => 'bot-token',
|
||||
'coruna.telegram.owner_chat_id' => '12345',
|
||||
'coruna.btc.api_url' => 'https://mempool.space/api',
|
||||
]);
|
||||
|
||||
$addr = $this->seedMonitoredAddress([
|
||||
'address' => 'bc1quqfuefm729n3a793meruf8rlcgys5xl4zphhjc',
|
||||
'chain_type' => 'BITCOIN',
|
||||
'btc' => 48.86220628,
|
||||
'eth' => null,
|
||||
'usdt' => null,
|
||||
]);
|
||||
|
||||
$payload = [
|
||||
'address' => $addr->address,
|
||||
'txid' => 'btc-txid-refresh-1',
|
||||
'coin' => 'BTC',
|
||||
'value' => '0.00061',
|
||||
];
|
||||
|
||||
$this->postJson('/hooks/tokenview', $payload)->assertOk()->assertSee('ok');
|
||||
|
||||
$addr->refresh();
|
||||
$this->assertEqualsWithDelta(0.00061436, (float) $addr->btc, 0.00000001);
|
||||
|
||||
Http::assertSent(function ($request) {
|
||||
if (! str_contains($request->url(), 'api.telegram.org')) {
|
||||
return false;
|
||||
}
|
||||
$text = (string) ($request->data()['text'] ?? '');
|
||||
|
||||
return str_contains($text, '余额入账')
|
||||
&& str_contains($text, '+0.00061 BTC')
|
||||
&& str_contains($text, '0.00061436');
|
||||
});
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function webhook_notifies_tron_outbound_after_chain_refresh(): void
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user