123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- <?php
- namespace qiniu\services\blockchain\bsc;
- use Exception;
- use qiniu\services\blockchain\bsc\src\Providers\HttpProvider;
- use qiniu\services\blockchain\bsc\src\RequestManagers\HttpRequestManager;
- use qiniu\services\blockchain\bsc\src\Web3;
- use think\facade\Config;
- class NodeClient extends Web3
- {
- const Main_Net = 'https://bsc-dataseed.binance.org/';
- const Test_Net = 'https://data-seed-prebsc-1-s1.binance.org:8545/';
- function __construct($url)
- {
- $provider = new HttpProvider(
- new HttpRequestManager($url, 300) //timeout
- );
- parent::__construct($provider);
- }
- static function create($network)
- {
- if ($network === 'mainNet') return self::mainNet();
- if ($network === 'testNet') return self::testNet();
- throw new Exception('unsupported network');
- }
- static function testNet()
- {
- return new self(self::Test_Net);
- }
- static function mainNet()
- {
- return new self(Config::get('blockchain.bsc.node', self::Main_Net));
- }
- function getBalance($addr)
- {
- $cb = new Callback;
- $this->getEth()->getBalance($addr, $cb);
- return $cb->result;
- }
- function broadcast($rawtx)
- {
- $cb = new Callback;
- $this->getEth()->sendRawTransaction($rawtx, $cb);
- return $cb->result;
- }
- function getReceipt($txid)
- {
- $cb = new Callback;
- $this->getEth()->getTransactionReceipt($txid, $cb);
- return $cb->result;
- }
- function waitForConfirmation($txid, $timeout = 300)
- {
- $expire = time() + $timeout;
- while (time() < $expire) {
- try {
- $receipt = $this->getReceipt($txid);
- if (!is_null($receipt)) return $receipt;
- sleep(2);
- } catch (Exception $e) {
- }
- }
- throw new Exception('tx not confirmed yet.');
- }
- function getBlockByNumber()
- {
- $cb = new Callback;
- $number = hex(436);
- $this->getEth()->getBlockByNumber($number, true, $cb);
- return $cb->result;
- }
- }
|