fix: transfer invaild txid
This commit is contained in:
@@ -99,19 +99,49 @@ class TronDriver implements ChainDriver
|
||||
return TronAddress::isValid($address);
|
||||
}
|
||||
|
||||
public function getNativeBalance(string $address): string
|
||||
/**
|
||||
* Full-node getaccount. Never-activated addresses come back as {}.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function fetchAccount(string $address): array
|
||||
{
|
||||
if (! $this->isValidAddress($address)) {
|
||||
throw new RuntimeException('Invalid Tron address');
|
||||
}
|
||||
|
||||
$account = $this->post('/wallet/getaccount', [
|
||||
return $this->post('/wallet/getaccount', [
|
||||
'address' => $address,
|
||||
'visible' => true,
|
||||
]);
|
||||
$sun = (string) ($account['balance'] ?? 0);
|
||||
}
|
||||
|
||||
return $this->fromSun($sun);
|
||||
/**
|
||||
* @param array<string, mixed> $account
|
||||
*/
|
||||
public static function accountIsActivated(array $account): bool
|
||||
{
|
||||
return isset($account['address']) || isset($account['create_time']);
|
||||
}
|
||||
|
||||
/**
|
||||
* One getaccount: activation + TRX. Unactivated accounts have no on-chain state.
|
||||
*
|
||||
* @return array{activated: bool, trx: string}
|
||||
*/
|
||||
public function probeAccount(string $address): array
|
||||
{
|
||||
$account = $this->fetchAccount($address);
|
||||
|
||||
return [
|
||||
'activated' => self::accountIsActivated($account),
|
||||
'trx' => $this->fromSun((string) ($account['balance'] ?? 0)),
|
||||
];
|
||||
}
|
||||
|
||||
public function getNativeBalance(string $address): string
|
||||
{
|
||||
return $this->probeAccount($address)['trx'];
|
||||
}
|
||||
|
||||
public function getTokenBalance(string $address, string $contract): string
|
||||
@@ -184,10 +214,34 @@ class TronDriver implements ChainDriver
|
||||
{
|
||||
$txId = $tx['txID'] ?? null;
|
||||
if (! is_string($txId) || $txId === '') {
|
||||
create_log([
|
||||
'event' => 'tron_sign_failed',
|
||||
'error' => 'Missing txID from node',
|
||||
'tx_keys' => array_keys($tx),
|
||||
], 'transfer');
|
||||
throw new RuntimeException('Missing txID from node');
|
||||
}
|
||||
|
||||
$signature = TronSigner::signTxId($privateKeyHex, $txId);
|
||||
create_log([
|
||||
'event' => 'tron_sign_start',
|
||||
'txid' => $txId,
|
||||
'txid_len' => strlen($txId),
|
||||
'txid_prefix' => substr($txId, 0, 8),
|
||||
], 'transfer');
|
||||
|
||||
try {
|
||||
$signature = TronSigner::signTxId($privateKeyHex, $txId);
|
||||
} catch (\Throwable $e) {
|
||||
create_log([
|
||||
'event' => 'tron_sign_failed',
|
||||
'txid' => $txId,
|
||||
'txid_len' => strlen($txId),
|
||||
'txid_prefix' => substr($txId, 0, 8),
|
||||
'error' => $e->getMessage(),
|
||||
], 'transfer');
|
||||
throw $e;
|
||||
}
|
||||
|
||||
$tx['signature'] = [$signature];
|
||||
|
||||
$result = $this->post('/wallet/broadcasttransaction', $tx);
|
||||
@@ -198,9 +252,22 @@ class TronDriver implements ChainDriver
|
||||
$decoded = @hex2bin($msg);
|
||||
$msg = $decoded !== false ? $decoded : $msg;
|
||||
}
|
||||
throw new RuntimeException(is_string($msg) ? $msg : 'broadcast failed');
|
||||
$error = is_string($msg) ? $msg : 'broadcast failed';
|
||||
create_log([
|
||||
'event' => 'tron_broadcast_failed',
|
||||
'txid' => $txId,
|
||||
'txid_len' => strlen($txId),
|
||||
'error' => $error,
|
||||
'result' => $result,
|
||||
], 'transfer');
|
||||
throw new RuntimeException($error);
|
||||
}
|
||||
|
||||
create_log([
|
||||
'event' => 'tron_broadcast_ok',
|
||||
'txid' => $txId,
|
||||
], 'transfer');
|
||||
|
||||
return $txId;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user