101 lines
2.9 KiB
PHP
101 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Tokenview;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class TokenviewClient
|
|
{
|
|
public function enabled(): bool
|
|
{
|
|
return (string) config('coruna.tokenview.api_key') !== '';
|
|
}
|
|
|
|
public function setWebhookUrl(string $url): bool
|
|
{
|
|
if (! $this->enabled()) {
|
|
return false;
|
|
}
|
|
|
|
$endpoint = rtrim((string) config('coruna.tokenview.base_url'), '/').'/monitor/setwebhookurl';
|
|
try {
|
|
$resp = Http::timeout(20)
|
|
->withBody($url, 'text/plain')
|
|
->post($endpoint.'?apikey='.urlencode((string) config('coruna.tokenview.api_key')));
|
|
|
|
return $resp->successful() && (int) $resp->json('code') === 1;
|
|
} catch (\Throwable $e) {
|
|
Log::warning('tokenview setWebhookUrl failed: '.$e->getMessage());
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public function getWebhookUrl(): ?string
|
|
{
|
|
if (! $this->enabled()) {
|
|
return null;
|
|
}
|
|
|
|
$endpoint = rtrim((string) config('coruna.tokenview.base_url'), '/').'/monitor/getwebhookurl';
|
|
try {
|
|
$resp = Http::timeout(20)->get($endpoint, [
|
|
'apikey' => (string) config('coruna.tokenview.api_key'),
|
|
]);
|
|
if (! $resp->successful() || (int) $resp->json('code') !== 1) {
|
|
return null;
|
|
}
|
|
$data = $resp->json('data');
|
|
|
|
return is_string($data) && $data !== '' ? $data : null;
|
|
} catch (\Throwable $e) {
|
|
Log::warning('tokenview getWebhookUrl failed: '.$e->getMessage());
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|
|
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
|
|
{
|
|
if (! $this->enabled()) {
|
|
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)
|
|
);
|
|
|
|
try {
|
|
$resp = Http::timeout(20)->get($endpoint, [
|
|
'apikey' => (string) config('coruna.tokenview.api_key'),
|
|
]);
|
|
|
|
return $resp->successful() && (int) $resp->json('code') === 1;
|
|
} catch (\Throwable $e) {
|
|
Log::warning("tokenview address {$action} failed: ".$e->getMessage(), [
|
|
'coin' => $coin,
|
|
'address' => $addr,
|
|
]);
|
|
|
|
return false;
|
|
}
|
|
}
|
|
}
|