fix: link memo

This commit is contained in:
hashbro
2026-09-13 17:17:34 +08:00
parent bca2eb4602
commit b49755c900
17 changed files with 751 additions and 202 deletions
+65
View File
@@ -97,7 +97,63 @@ class BtcDriver implements ChainDriver
return BtcAddress::isValid($address);
}
public function isActivated(string $address): bool
{
try {
return $this->probeAddress($address)['activated'];
} catch (\Throwable) {
return false;
}
}
/**
* One address lookup: activation + confirmed BTC.
*
* @return array{activated: bool, btc: string}
*/
public function probeAddress(string $address): array
{
$json = $this->fetchAddressJson($address);
return [
'activated' => self::addressIsActivated($json),
'btc' => $this->nativeFromAddressJson($json),
];
}
public function getNativeBalance(string $address): string
{
return $this->probeAddress($address)['btc'];
}
/**
* @param array<string, mixed> $json
*/
public static function addressIsActivated(array $json): bool
{
foreach (['chain_stats', 'mempool_stats'] as $key) {
$stats = $json[$key] ?? [];
if (! is_array($stats)) {
continue;
}
if ((int) ($stats['tx_count'] ?? 0) > 0) {
return true;
}
if ((int) ($stats['funded_txo_count'] ?? 0) > 0) {
return true;
}
if ((int) ($stats['funded_txo_sum'] ?? 0) > 0) {
return true;
}
}
return false;
}
/**
* @return array<string, mixed>
*/
private function fetchAddressJson(string $address): array
{
if (! $this->isValidAddress($address)) {
throw new RuntimeException('Invalid BTC address');
@@ -112,6 +168,15 @@ class BtcDriver implements ChainDriver
if (! is_array($json)) {
throw new RuntimeException('Invalid BTC balance response');
}
return $json;
}
/**
* @param array<string, mixed> $json
*/
private function nativeFromAddressJson(array $json): string
{
$stats = $json['chain_stats'] ?? [];
$funded = (string) ($stats['funded_txo_sum'] ?? 0);
$spent = (string) ($stats['spent_txo_sum'] ?? 0);