用到的composer包
"bitwasp/bitcoin-lib": "1.0.4",
"minter/minter-php-bip-44": "^1.0",
"web3p/ethereum-util": "^0.1.1",
"sc0vu/web3.php": "^0.1.4",
"digitaldonkey/ethereum-php": "dev-master",
"web3p/ethereum-tx": "^0.3.4"
自己封装好的类拿出来共享,是否能使用大家自行判断,使用的infura,不用自己搭建ETH
<?php
namespace app\common\library;
use BIP\BIP44;
use BitWasp\BitcoinLib\BIP39\BIP39;
use Ethereum\Ethereum;
use Web3\Providers\HttpProvider;
use Web3\RequestManagers\HttpRequestManager;
use Web3\Utils;
use Web3\Web3;
use Web3p\EthereumTx\Transaction;
use Web3p\EthereumUtil\Util;
class EtherLib
{
//protected static $eth_url = 'http://XXXXXX:8200';
protected static $eth_url = 'https://mainnet.infura.io/v3/XXX';
protected static $eth = null;
protected static $personal = null;
public static $web3 = null;
/**
* @var Ethereum
*/
public static $new_web = null;
/*
* 单例获取eth实例
*/
public static function getWeb3()
{
if (empty(self::$web3)) {
// 30 Second timeout
$timeout = 30;
self::$web3 = new Web3(new HttpProvider(new HttpRequestManager(self::$eth_url, $timeout)));
//self::$web3 = new Web3(self::$eth_url);
}
return self::$web3;
}
public static function getNewWeb3()
{
if (empty(self::$new_web)) {
self::$new_web = new Ethereum(self::$eth_url);
}
return self::$new_web;
}
/*
* 单例获取eth实例
*/
public static function getEth()
{
if (empty(self::$eth)) {
$web3 = self::getWeb3();
self::$eth = $web3->getEth();
}
return self::$eth;
}
/*
* 单例获取eth实例
*/
public static function getPersonal()
{
if (empty(self::$personal)) {
$web3 = self::getWeb3();
self::$personal = $web3->getPersonal();
}
return self::$personal;
}
/*
* 获取ETH价格
*/
public static function getEthPrice()
{
$price = cache('eth_price');
if (empty($price)) {
$info = file_get_contents('http://api.coindog.com/api/v1/currency/ranks');
if (!empty($info)) {
$info_json = json_decode($info, true);
foreach ($info_json as $item) {
if (strtoupper($item['currency']) == 'ETH') {
$price = $item['price'