argument('action'))); return match ($action) { 'create' => $this->createWallet(), 'balance' => $this->showBalance($tron), 'address' => $this->addressFromKey(), default => $this->invalidAction($action), }; } private function createWallet(): int { $privateKey = bin2hex(random_bytes(32)); $address = TronDriver::addressFromPrivateKey($privateKey); $this->line('address : '.$address); $this->line('privateKey : '.$privateKey); $this->warn('请妥善保存私钥,丢失无法找回。'); return self::SUCCESS; } private function addressFromKey(): int { $key = trim((string) ($this->argument('value') ?? '')); if ($key === '') { $this->error('用法: php artisan coruna:tron address '); return self::FAILURE; } try { $address = TronDriver::addressFromPrivateKey($key); } catch (\Throwable $e) { $this->error('无效私钥: '.$e->getMessage()); return self::FAILURE; } $this->line('address : '.$address); $this->line('privateKey : '.strtolower(str_starts_with($key, '0x') ? substr($key, 2) : $key)); return self::SUCCESS; } private function showBalance(TronDriver $tron): int { $address = trim((string) ($this->argument('value') ?? '')); if ($address === '') { $this->error('用法: php artisan coruna:tron balance '); return self::FAILURE; } if (! $tron->isValidAddress($address)) { $this->error('无效 TRON 地址'); return self::FAILURE; } try { $trx = $tron->getNativeBalance($address); $contract = trim((string) config('coruna.tron.usdt_contract', '')); $usdt = $contract !== '' ? $tron->getTokenBalance($address, $contract) : '0'; } catch (RuntimeException $e) { $this->error($e->getMessage()); return self::FAILURE; } catch (\Throwable $e) { $this->error('查询失败: '.$e->getMessage()); return self::FAILURE; } $this->line('address : '.$address); $this->line('TRX : '.$trx); $this->line('USDT : '.$usdt); return self::SUCCESS; } private function invalidAction(string $action): int { $this->error("未知操作: {$action}"); $this->line('用法:'); $this->line(' php artisan coruna:tron create'); $this->line(' php artisan coruna:tron address '); $this->line(' php artisan coruna:tron balance '); return self::FAILURE; } }