You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/app/Services/Payment/TronGridClient.php

105 lines
3.8 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Services\Payment;
use GuzzleHttp\Client;
class TronGridClient
{
private const MAX_RESPONSE_BYTES = 4194304;
private const MAX_PAGES = 50;
private $client;
private $settings;
public function __construct(array $settings)
{
$this->settings = $settings;
$headers = ['Accept' => 'application/json'];
if (trim((string) ($settings['usdt_trongrid_api_key'] ?? '')) !== '') {
$headers['TRON-PRO-API-KEY'] = trim((string) $settings['usdt_trongrid_api_key']);
}
$this->client = new Client([
'base_uri' => rtrim((string) $settings['usdt_trongrid_base_url'], '/') . '/',
'connect_timeout' => min(10, (int) $settings['usdt_request_timeout_seconds']),
'timeout' => (int) $settings['usdt_request_timeout_seconds'],
'http_errors' => false,
'allow_redirects' => false,
'headers' => $headers,
]);
}
public function transfersTo($address, $minimumTimestampMs, $maximumTimestampMs)
{
if (!TronAddress::isValid($address)) {
throw new \InvalidArgumentException('扫描目标不是有效的 TRON 地址');
}
$minimumTimestampMs = max(0, (int) $minimumTimestampMs);
$maximumTimestampMs = max($minimumTimestampMs, (int) $maximumTimestampMs);
$fingerprint = '';
$result = [];
for ($page = 0; $page < self::MAX_PAGES; $page++) {
$query = [
'only_confirmed' => 'true',
'only_to' => 'true',
'contract_address' => UsdtConfig::CONTRACT_ADDRESS,
'limit' => 200,
'min_timestamp' => $minimumTimestampMs,
'max_timestamp' => $maximumTimestampMs,
'order_by' => 'block_timestamp,asc',
];
if ($fingerprint !== '') {
$query['fingerprint'] = $fingerprint;
}
$payload = $this->request('GET', 'v1/accounts/' . rawurlencode($address) . '/transactions/trc20', [
'query' => $query,
]);
if (!isset($payload['data']) || !is_array($payload['data'])) {
throw new \RuntimeException('TRON API 返回缺少交易列表');
}
foreach ($payload['data'] as $transfer) {
if (is_array($transfer)) {
$result[] = $transfer;
}
}
$fingerprint = trim((string) ($payload['meta']['fingerprint'] ?? ''));
if ($fingerprint === '') {
return $result;
}
}
throw new \RuntimeException('TRON API 分页数量异常,已停止本轮扫描以避免漏记');
}
public function testConnection($address)
{
$now = (int) floor(microtime(true) * 1000);
$this->transfersTo($address, $now - 3600000, $now);
return true;
}
private function request($method, $path, array $options)
{
$response = $this->client->request($method, $path, $options);
$status = (int) $response->getStatusCode();
$body = (string) $response->getBody();
if (strlen($body) > self::MAX_RESPONSE_BYTES) {
throw new \RuntimeException('TRON API 返回内容过大');
}
if ($status < 200 || $status >= 300) {
throw new \RuntimeException('TRON API 请求失败(HTTP ' . $status . ')');
}
$payload = json_decode($body, true);
if (!is_array($payload)) {
throw new \RuntimeException('TRON API 返回的 JSON 无效');
}
if (isset($payload['success']) && $payload['success'] === false) {
throw new \RuntimeException('TRON API 拒绝请求:' . mb_substr((string) ($payload['error'] ?? 'unknown'), 0, 200));
}
return $payload;
}
}