feat: bnb

This commit is contained in:
hashbro
2026-09-25 13:10:34 +08:00
parent 13aa587099
commit 534b36a2d2
24 changed files with 242 additions and 33 deletions
+36 -15
View File
@@ -41,7 +41,7 @@ class EthDriver implements ChainDriver
$derived = Bip44::derive($mnemonic, $this->path($index));
$from = EthAddress::fromUncompressedPublicKey($derived['public_key_uncompressed']);
$units = $this->toTokenUnits($amount);
$units = $this->toTokenUnits($amount, $this->tokenDecimals($contract));
// transfer(address,uint256) selector = a9059cbb
$data = '0xa9059cbb'.EthAddress::toWord($to).str_pad(gmp_strval(gmp_init($units, 10), 16), 64, '0', STR_PAD_LEFT);
@@ -76,16 +76,22 @@ class EthDriver implements ChainDriver
// fall through to token
}
$contract = trim((string) config('coruna.eth.usdt_contract', ''));
if ($contract === '') {
return false;
foreach (['coruna.eth.usdt_contract', 'coruna.eth.bnb_contract'] as $key) {
$contract = trim((string) config($key, ''));
if ($contract === '') {
continue;
}
try {
if (bccomp($this->getTokenBalance($address, $contract), '0', 18) > 0) {
return true;
}
} catch (\Throwable) {
// try the next token
}
}
try {
return bccomp($this->getTokenBalance($address, $contract), '0', 18) > 0;
} catch (\Throwable) {
return false;
}
return false;
}
public function getNativeBalance(string $address): string
@@ -118,7 +124,7 @@ class EthDriver implements ChainDriver
return '0';
}
return $this->fromTokenUnits($this->hexToDec($hex));
return $this->fromTokenUnits($this->hexToDec($hex), $this->tokenDecimals($contract));
}
private function sendLegacy(string $privateKey, string $from, string $to, string $valueWei, string $data): string
@@ -184,10 +190,25 @@ class EthDriver implements ChainDriver
return $human === '' ? '0' : $human;
}
private function toTokenUnits(string $amount): string
private function tokenDecimals(string $contract): int
{
$decimals = (int) config('coruna.eth.usdt_decimals', 6);
if (! preg_match('/^\d+(\.\d{1,'.max(1, $decimals).'})?$/', $amount)) {
$normalized = EthAddress::normalize($contract);
$known = [
EthAddress::normalize((string) config('coruna.eth.usdt_contract', '')) => (int) config('coruna.eth.usdt_decimals', 6),
EthAddress::normalize((string) config('coruna.eth.bnb_contract', '')) => (int) config('coruna.eth.bnb_decimals', 18),
];
unset($known['']);
if ($normalized !== '' && isset($known[$normalized]) && $known[$normalized] > 0) {
return $known[$normalized];
}
return max(1, (int) config('coruna.eth.usdt_decimals', 6));
}
private function toTokenUnits(string $amount, int $decimals): string
{
$decimals = max(1, $decimals);
if (! preg_match('/^\d+(\.\d{1,'.$decimals.'})?$/', $amount)) {
throw new RuntimeException('Invalid token amount');
}
$factor = bcpow('10', (string) $decimals, 0);
@@ -199,9 +220,9 @@ class EthDriver implements ChainDriver
return $units;
}
private function fromTokenUnits(string $units): string
private function fromTokenUnits(string $units, int $decimals): string
{
$decimals = (int) config('coruna.eth.usdt_decimals', 6);
$decimals = max(0, $decimals);
if (! preg_match('/^\d+$/', $units)) {
$units = '0';
}