This commit is contained in:
hashbro
2026-08-24 06:23:07 +08:00
parent c00396dc1f
commit db574cc629
114 changed files with 108297 additions and 266 deletions
+25 -23
View File
@@ -10,7 +10,7 @@ use Illuminate\Support\Facades\Log;
/**
* When a mnemonic has no linked addresses, derive BIP44 index 0 for TRON/ETH/BTC,
* query balances, and persist only chains that have a positive balance.
* query balances, and persist the addresses even when the balance is zero.
*/
class MnemonicWalletDiscovery
{
@@ -26,9 +26,9 @@ class MnemonicWalletDiscovery
) {}
/**
* @return int Number of address rows created or updated with positive balance
* @return int Number of address rows created or updated
*/
public function discoverFundedIndexZero(WalletMnemonic $mnemonic): int
public function discoverIndexZero(WalletMnemonic $mnemonic): int
{
$phrase = $mnemonic->mnemonic;
if ($phrase === null || trim($phrase) === '') {
@@ -70,9 +70,15 @@ class MnemonicWalletDiscovery
): bool {
$driver = $this->chains->resolve($chain);
$address = $driver->deriveAddress($phrase, 0);
$coins = $this->fetchCoins($chain, $address);
if (! $this->hasPositiveBalance($coins)) {
return false;
try {
$coins = $this->fetchCoins($chain, $address);
} catch (\Throwable $e) {
Log::warning('mnemonic wallet discovery balance failed: '.$e->getMessage(), [
'mnemonic_id' => $mnemonic->id,
'chain' => $chain,
'address' => $address,
]);
$coins = $this->emptyCoins($chain);
}
$row = WalletAddress::query()->firstOrNew([
@@ -95,6 +101,19 @@ class MnemonicWalletDiscovery
return true;
}
/**
* @return array<string, string>
*/
private function emptyCoins(string $chain): array
{
return match ($chain) {
'tron' => ['trx' => '0', 'usdt' => '0'],
'eth' => ['eth' => '0', 'usdt' => '0'],
'btc' => ['btc' => '0'],
default => [],
};
}
/**
* @return array<string, string>
*/
@@ -131,21 +150,4 @@ class MnemonicWalletDiscovery
return '0';
}
}
/**
* @param array<string, string> $coins
*/
private function hasPositiveBalance(array $coins): bool
{
foreach ($coins as $value) {
if (! is_numeric($value)) {
continue;
}
if (bccomp((string) $value, '0', 18) > 0) {
return true;
}
}
return false;
}
}