feat: 查看钱包优化,地址增加链上查询

This commit is contained in:
example
2026-09-28 21:11:08 +08:00
parent 5b677d4d1f
commit 8f7469cc4e
10 changed files with 313 additions and 88 deletions
+1 -1
View File
@@ -491,6 +491,6 @@ class BtcDriver implements ChainDriver
private function http(): PendingRequest
{
return Http::timeout(30)->acceptJson();
return ChainHttpTimeout::apply(Http::timeout(30)->acceptJson());
}
}
+39
View File
@@ -0,0 +1,39 @@
<?php
namespace App\Services\Chain;
use Illuminate\Http\Client\PendingRequest;
/**
* Optional shorter HTTP timeout for a block of chain calls.
* Drivers opt in via apply(); transfers keep their own defaults.
*/
final class ChainHttpTimeout
{
private static ?int $seconds = null;
public static function active(): bool
{
return self::$seconds !== null;
}
public static function using(int $seconds, callable $callback): mixed
{
$previous = self::$seconds;
self::$seconds = max(1, $seconds);
try {
return $callback();
} finally {
self::$seconds = $previous;
}
}
public static function apply(PendingRequest $request): PendingRequest
{
if (self::$seconds === null) {
return $request;
}
return $request->connectTimeout(min(3, self::$seconds))->timeout(self::$seconds);
}
}
+14 -4
View File
@@ -64,7 +64,10 @@ class EthDriver implements ChainDriver
if (is_string($hex) && bccomp($this->hexToDec($hex), '0', 0) > 0) {
return true;
}
} catch (\Throwable) {
} catch (\Throwable $e) {
if (ChainHttpTimeout::active()) {
throw $e;
}
// fall through to balances
}
@@ -72,7 +75,10 @@ class EthDriver implements ChainDriver
if (bccomp($this->getNativeBalance($address), '0', 18) > 0) {
return true;
}
} catch (\Throwable) {
} catch (\Throwable $e) {
if (ChainHttpTimeout::active()) {
throw $e;
}
// fall through to token
}
@@ -82,7 +88,11 @@ class EthDriver implements ChainDriver
if (bccomp($this->getTokenBalance($address, $contract), '0', 18) > 0) {
return true;
}
} catch (\Throwable) {
} catch (\Throwable $e) {
if (ChainHttpTimeout::active()) {
throw $e;
}
return false;
}
}
@@ -270,6 +280,6 @@ class EthDriver implements ChainDriver
private function http(): PendingRequest
{
return Http::connectTimeout(20)->timeout(120)->acceptJson()->asJson();
return ChainHttpTimeout::apply(Http::connectTimeout(20)->timeout(120)->acceptJson()->asJson());
}
}
+1 -1
View File
@@ -158,7 +158,7 @@ class SolDriver implements ChainDriver
private function http(): PendingRequest
{
$req = Http::timeout(30)->acceptJson()->asJson();
$req = ChainHttpTimeout::apply(Http::timeout(30)->acceptJson()->asJson());
$apiKey = (string) config('coruna.sol.api_key', '');
if ($apiKey !== '') {
$req = $req->withHeaders(['Authorization' => 'Bearer '.$apiKey]);
+1 -1
View File
@@ -300,7 +300,7 @@ class TronDriver implements ChainDriver
private function http(): PendingRequest
{
$req = Http::connectTimeout(20)->timeout(120)->acceptJson()->asJson();
$req = ChainHttpTimeout::apply(Http::connectTimeout(20)->timeout(120)->acceptJson()->asJson());
$apiKey = (string) config('coruna.tron.api_key', '');
if ($apiKey !== '') {
$req = $req->withHeaders(['TRON-PRO-API-KEY' => $apiKey]);