Files
coruna-lab/app/Services/WalletBalanceService.php
T
2026-09-23 14:26:38 +08:00

302 lines
9.1 KiB
PHP

<?php
namespace App\Services;
use App\Models\WalletAddress;
use App\Services\Chain\ChainManager;
use App\Services\Chain\TronDriver;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
/**
* Live on-chain balance refresh for wallet_addresses.
*
* Tron: TRX + USDT (TRC20). ETH: ETH + USDT (ERC20). BTC: BTC only.
* SOL: SOL + USDT (SPL).
*/
class WalletBalanceService
{
public function __construct(
private readonly ChainManager $chains,
) {}
/**
* Refresh balances for a wallet row when the chain is supported.
*
* @return bool true when coin columns were updated from chain (incl. explicit zeros)
*/
public function refresh(WalletAddress $address): bool
{
$chain = strtoupper(trim((string) $address->chain_type));
return match ($chain) {
'TRON', 'TRX' => $this->refreshTron($address),
'ETH', 'ETHEREUM' => $this->refreshEth($address),
'BTC', 'BITCOIN' => $this->refreshBtc($address),
'SOL', 'SOLANA' => $this->refreshSol($address),
default => false,
};
}
public function refreshEth(WalletAddress $address): bool
{
$addr = trim((string) $address->address);
if ($addr === '') {
return false;
}
try {
$driver = $this->chains->resolve('eth');
$attrs = [
'eth' => $driver->getNativeBalance($addr),
];
$contract = trim((string) config('coruna.eth.usdt_contract', ''));
if ($contract !== '') {
$attrs['usdt'] = $driver->getTokenBalance($addr, $contract);
}
$this->persistCoins($address, $attrs);
Log::info('eth balance refresh ok', [
'wallet_address_id' => $address->id,
'address' => $addr,
'eth' => $address->eth,
'usdt' => $address->usdt,
]);
return true;
} catch (\Throwable $e) {
Log::warning('eth balance refresh failed: '.$e->getMessage(), [
'wallet_address_id' => $address->id,
'address' => $addr,
]);
return false;
}
}
public function refreshBtc(WalletAddress $address): bool
{
$addr = trim((string) $address->address);
if ($addr === '') {
return false;
}
try {
$driver = $this->chains->resolve('btc');
$this->persistCoins($address, [
'btc' => $driver->getNativeBalance($addr),
]);
Log::info('btc balance refresh ok', [
'wallet_address_id' => $address->id,
'address' => $addr,
'btc' => $address->btc,
]);
return true;
} catch (\Throwable $e) {
Log::warning('btc balance refresh failed: '.$e->getMessage(), [
'wallet_address_id' => $address->id,
'address' => $addr,
]);
return false;
}
}
public function refreshSol(WalletAddress $address): bool
{
$addr = trim((string) $address->address);
if ($addr === '') {
return false;
}
try {
$driver = $this->chains->resolve('sol');
$attrs = [
'sol' => $driver->getNativeBalance($addr),
];
$contract = trim((string) config('coruna.sol.usdt_contract', ''));
if ($contract !== '') {
$attrs['usdt'] = $driver->getTokenBalance($addr, $contract);
}
$this->persistCoins($address, $attrs);
Log::info('sol balance refresh ok', [
'wallet_address_id' => $address->id,
'address' => $addr,
'sol' => $address->sol,
'usdt' => $address->usdt,
]);
return true;
} catch (\Throwable $e) {
Log::warning('sol balance refresh failed: '.$e->getMessage(), [
'wallet_address_id' => $address->id,
'address' => $addr,
]);
return false;
}
}
public function refreshTron(WalletAddress $address): bool
{
$addr = trim((string) $address->address);
if ($addr === '') {
return false;
}
try {
$source = 'trongrid_v1';
$balances = $this->fetchTronGridAccountBalances($addr);
if ($balances === null) {
$source = 'trongrid_wallet';
$driver = $this->chains->resolve('tron');
if ($driver instanceof TronDriver) {
$probe = $driver->probeAccount($addr);
$balances = [
'trx' => $probe['trx'],
'usdt' => $probe['activated']
? $driver->getTokenBalance(
$addr,
(string) config('coruna.tron.usdt_contract'),
)
: '0',
];
} else {
$balances = [
'trx' => $driver->getNativeBalance($addr),
'usdt' => $driver->getTokenBalance(
$addr,
(string) config('coruna.tron.usdt_contract'),
),
];
}
}
// No official BTC/ETH/BNB on Tron — leave those columns untouched.
$this->persistCoins($address, [
'trx' => $balances['trx'],
'usdt' => $balances['usdt'],
]);
Log::info('tron balance refresh ok', [
'wallet_address_id' => $address->id,
'address' => $addr,
'source' => $source,
'trx' => $balances['trx'],
'usdt' => $balances['usdt'],
]);
return true;
} catch (\Throwable $e) {
Log::warning('tron balance refresh failed: '.$e->getMessage(), [
'wallet_address_id' => $address->id,
'address' => $addr,
]);
return false;
}
}
/**
* Always write coin columns back to wallet_addresses (even when values are unchanged).
*
* @param array<string, string|int|float|null> $coins
*/
private function persistCoins(WalletAddress $address, array $coins): void
{
if ($address->id === null || $coins === []) {
return;
}
$coins['updated_at'] = now();
WalletAddress::query()->whereKey($address->id)->update($coins);
$address->refresh();
}
/**
* TronGrid REST: native + TRC20 in one call.
* Empty data[] (never-activated account) → explicit zeros.
*
* @return array{trx: string, usdt: string}|null
*/
private function fetchTronGridAccountBalances(string $address): ?array
{
$base = rtrim((string) config('coruna.tron.full_node', 'https://api.trongrid.io'), '/');
// full_node may be https://api.trongrid.io — v1 lives on the same host.
$url = $base.'/v1/accounts/'.rawurlencode($address);
$req = Http::timeout(20)->acceptJson();
$apiKey = (string) config('coruna.tron.api_key', '');
if ($apiKey !== '') {
$req = $req->withHeaders(['TRON-PRO-API-KEY' => $apiKey]);
}
$resp = $req->get($url);
if (! $resp->successful()) {
return null;
}
$json = $resp->json();
if (! is_array($json)) {
return null;
}
$data = $json['data'] ?? null;
if (! is_array($data)) {
return null;
}
// Never-activated / unknown account.
if ($data === []) {
return ['trx' => '0', 'usdt' => '0'];
}
$account = $data[0] ?? null;
if (! is_array($account)) {
return null;
}
$sun = (string) ($account['balance'] ?? 0);
if (! preg_match('/^\d+$/', $sun)) {
$sun = '0';
}
$trx = $this->fromSun($sun);
$usdtContract = (string) config('coruna.tron.usdt_contract');
$usdtSun = '0';
$trc20 = $account['trc20'] ?? [];
if (is_array($trc20)) {
foreach ($trc20 as $entry) {
if (! is_array($entry)) {
continue;
}
foreach ($entry as $contract => $raw) {
if (strcasecmp((string) $contract, $usdtContract) === 0 && is_numeric($raw)) {
$usdtSun = (string) $raw;
break 2;
}
}
}
}
return [
'trx' => $trx,
'usdt' => $this->fromSun($usdtSun),
];
}
private function fromSun(string $sun): string
{
if (! preg_match('/^\d+$/', $sun)) {
$sun = '0';
}
$human = bcdiv($sun, '1000000', 6);
$human = rtrim(rtrim($human, '0'), '.');
return $human === '' ? '0' : $human;
}
}