Files
coruna-lab/app/Services/Tokenview/TokenviewClient.php
T
hashbro 51336e346a Keep Tokenview and Telegram running when log files are not writable.
Daily logs created by root cron were blocking www from appending, which aborted webhook ingest and bot pushes. File channels now use 0664 plus exception-safe stacks, and writes go through SafeLog.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-15 11:40:44 +08:00

85 lines
2.4 KiB
PHP

<?php
namespace App\Services\Tokenview;
use App\Support\SafeLog;
use Illuminate\Support\Facades\Http;
class TokenviewClient
{
public function enabled(): bool
{
return (string) config('coruna.tokenview.api_key') !== '';
}
public function addAddress(string $coinAbbr, string $address): bool
{
return $this->mutateAddress('add', $coinAbbr, $address);
}
public function removeAddress(string $coinAbbr, string $address): bool
{
return $this->mutateAddress('remove', $coinAbbr, $address);
}
private function mutateAddress(string $action, string $coinAbbr, string $address): bool
{
$log = SafeLog::channel('tokenview');
if (! $this->enabled()) {
$log->warning('mutate skipped: api_key not configured', [
'action' => $action,
'coin' => $coinAbbr,
'address' => $address,
]);
return false;
}
$coin = strtolower($coinAbbr);
$addr = $coin === 'eth' || $coin === 'bsc' ? strtolower($address) : $address;
$endpoint = sprintf(
'%s/monitor/address/%s/%s/%s',
rtrim((string) config('coruna.tokenview.base_url'), '/'),
$action,
rawurlencode($coin),
rawurlencode($addr)
);
$log->info('tokenview request', [
'action' => $action,
'coin' => $coin,
'address' => $addr,
'endpoint' => $endpoint,
]);
try {
$resp = Http::timeout(20)->get($endpoint, [
'apikey' => (string) config('coruna.tokenview.api_key'),
]);
$ok = $resp->successful() && (int) $resp->json('code') === 1;
$log->info('tokenview response', [
'action' => $action,
'coin' => $coin,
'address' => $addr,
'http_status' => $resp->status(),
'body' => $resp->body(),
'ok' => $ok,
]);
return $ok;
} catch (\Throwable $e) {
$log->warning('tokenview request failed', [
'action' => $action,
'coin' => $coin,
'address' => $addr,
'endpoint' => $endpoint,
'error' => $e->getMessage(),
]);
return false;
}
}
}