| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?php
- namespace App\Module\Blockchain\Services;
- use App\Module\Blockchain\Contracts\BlockchainServiceInterface;
- use App\Module\Blockchain\Enums\TokenType;
- use Illuminate\Support\Facades\Http;
- use Illuminate\Support\Facades\Cache;
- use Web3\Web3;
- use Web3\Providers\HttpProvider;
- use Web3\RequestManagers\HttpRequestManager;
- use InvalidArgumentException;
- class BnbChainService implements BlockchainServiceInterface
- {
- protected Web3 $web3;
- protected array $config;
- protected string $network;
- protected BscScanService $bscScanService;
- public function __construct(BscScanService $bscScanService, string $network = 'mainnet')
- {
- $this->config = config('blockchain.bnbchain');
- $this->network = $network;
- $this->bscScanService = $bscScanService;
- // 初始化 Web3 连接
- $provider = new HttpProvider(new HttpRequestManager(
- $this->config['networks'][$network]['rpc'][0],
- 10 // 超时时间(秒)
- ));
- $this->web3 = new Web3($provider);
- }
- /**
- * 获取当前网络配置
- */
- public function getNetwork(): array
- {
- return $this->config['networks'][$this->network];
- }
- /**
- * 获取当前链 ID
- */
- public function getChainId(): int
- {
- return $this->config['networks'][$this->network]['chain_id'];
- }
- /**
- * 获取区块浏览器 URL
- */
- public function getExplorerUrl(): string
- {
- return $this->config['networks'][$this->network]['explorer'];
- }
- /**
- * 获取交易浏览器链接
- */
- public function getTransactionUrl(string $txHash): string
- {
- return $this->getExplorerUrl() . '/tx/' . $txHash;
- }
- /**
- * 获取地址浏览器链接
- */
- public function getAddressUrl(string $address): string
- {
- return $this->getExplorerUrl() . '/address/' . $address;
- }
- /**
- * 获取代币合约浏览器链接
- */
- public function getTokenUrl(TokenType $tokenType): ?string
- {
- $contractAddress = $tokenType->contractAddress();
- if (!$contractAddress) {
- return null;
- }
- return $this->getExplorerUrl() . '/token/' . $contractAddress;
- }
- /**
- * 获取最新区块号
- */
- public function getLatestBlockNumber(): int
- {
- return Cache::remember('latest_block_number', 10, function () {
- $response = $this->web3->eth->blockNumber();
- return hexdec($response);
- });
- }
- /**
- * 获取当前推荐的 Gas 价格
- */
- public function getGasPrice(): array
- {
- return Cache::remember('gas_price', $this->config['cache']['gas_price_ttl'], function () {
- $response = $this->web3->eth->gasPrice();
- $gasPrice = hexdec($response) / 1e9; // 转换为 GWEI
- return [
- 'safe_low' => max($this->config['gas']['price']['safe_low'], $gasPrice),
- 'standard' => max($this->config['gas']['price']['standard'], $gasPrice * 1.1),
- 'fast' => max($this->config['gas']['price']['fast'], $gasPrice * 1.2),
- 'current' => $gasPrice
- ];
- });
- }
- /**
- * MEV 保护:获取带有保护的 Gas 价格
- */
- public function getMevProtectedGasPrice(): array
- {
- if (!$this->config['mev_protection']['enabled']) {
- return $this->getGasPrice();
- }
- $gasPrice = $this->getGasPrice();
- return [
- 'max_priority_fee_per_gas' => $this->config['mev_protection']['max_priority_fee'],
- 'max_fee_per_gas' => min(
- $this->config['mev_protection']['max_fee'],
- $gasPrice['standard'] + $this->config['mev_protection']['max_priority_fee']
- )
- ];
- }
- /**
- * 实现接口方法:获取余额
- */
- public function getBalance(string $address, TokenType $tokenType): float
- {
- return $this->bscScanService->getBalance($address, $tokenType);
- }
- /**
- * 实现接口方法:验证地址
- */
- public function isValidAddress(string $address): bool
- {
- return $this->bscScanService->isValidAddress($address);
- }
- /**
- * 实现接口方法:获取交易状态
- */
- public function getTransactionStatus(string $txHash): array
- {
- return $this->bscScanService->getTransactionStatus($txHash);
- }
- /**
- * 实现接口方法:获取交易收据
- */
- public function getTransactionReceipt(string $txHash): array
- {
- return $this->bscScanService->getTransactionReceipt($txHash);
- }
- /**
- * 实现接口方法:估算 Gas 费用
- */
- public function estimateGasFee(string $from, string $to, TokenType $tokenType, float $amount): float
- {
- return $this->bscScanService->estimateGasFee($from, $to, $tokenType, $amount);
- }
- /**
- * 实现接口方法:获取交易历史
- */
- public function getTransactionHistory(string $address, TokenType $tokenType, int $page = 1, int $limit = 10): array
- {
- return $this->bscScanService->getTransactionHistory($address, $tokenType, $page, $limit);
- }
- /**
- * 检查交易确认数
- */
- public function getTransactionConfirmations(string $txHash): int
- {
- $receipt = $this->getTransactionReceipt($txHash);
- if (!isset($receipt['blockNumber'])) {
- return 0;
- }
- $currentBlock = $this->getLatestBlockNumber();
- $txBlock = hexdec($receipt['blockNumber']);
- return max(0, $currentBlock - $txBlock);
- }
- /**
- * 检查交易是否已经安全确认
- */
- public function isTransactionSafe(string $txHash): bool
- {
- $confirmations = $this->getTransactionConfirmations($txHash);
- return $confirmations >= $this->config['confirmations']['safe'];
- }
- }
|