<?php
class Ethcoin
{
protected $host, $version;
protected $id = 0;
protected $usrename = "nginx";
protected $pwd = "8ee2asfgshtfs0b6";
public $base = 1000000000000000000;//1e18 wei 基本单位
/**
* 构造函数
* Common constructor.
* @param $host
* @param string $port
* @param string $version
*/
function __construct($host, $version = "2.0")
{
$this->host = $host;
$this->version = $version;
}
/**
* 发送请求
* @author qiuphp2
* @since 2017-9-21
* @param $method
* @param array $params
* @return mixed
*/
function request($method, $params = array())
{
$data = array();
$data['jsonrpc'] = $this->version;
$data['id'] = $this->id + 1;
$data['method'] = $method;
$data['params'] = $params;
//print_r($method);exit;
// echo json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->host);
// curl_setopt($ch, CURLOPT_PORT, $this->port);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_USERPWD, "$this->usrename:$this->pwd");
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$ret = curl_exec($ch);
/* if("eth_sendTransaction" == $method){
print_r($ret);exit;
} */
//返回结果
try {
if ($ret) {
curl_close($ch);
unset($data);
return json_decode($ret, true);
} else {
$error = curl_errno($ch);
curl_close($ch);
unset($data);
return json_decode('ETH钱包服务器连接失败', true);
// throw new Exception("curl出错,错误码:$error");
}
} catch (\Throwable $th) {
unset($data);
return json_decode('ETH钱包服务器连接失败', true);
}
}
/**
* @author qiuphp2
* @since 2017-9-21
* @param $weiNumber 16进制wei单位
* @return float|int 10进制eth单位【正常单位】
*/
function fromWei($weiNumber)
{
$ethNumber = hexdec($weiNumber) / $this->base;
return $ethNumber;
}
function fromWeiEth($weiNumber)
{
$ethNumber = $weiNumber / $this->base;
return $ethNumber;
}
/**
* @author qiuphp2
* @since 2017-9-21
* @param $ethNumber 10进制eth单位
* @return string 16进制wei单位
*/
function toWei($ethNumber)
{
$weiNumber = base_convert($this->decimalNotation($ethNumber * $this->base),10,16);
// $weiNumber = float($weiNumber);
return $weiNumber;
}
function decimalNotation($num){
$parts = explode('E', $num);
if(count($parts) != 2){
return intval($num);
}
$exp = abs(end($parts)) + 3;
$decimal = number_format($num, $exp);
$decimal = rtrim($decimal, '0');
return rtrim($decimal, '.');
}
/**
* 判断是否是16进制
* @author qiuphp2
* @since 2017-9-21
* @param $a
* @return int
*/
function assertIsHex($a)
{
if (ctype_xdigit($a)) {
return true;
} else {
return false;
}
}
/**
* 获取版本信息,判断是否连接
* @author qiuphp2
* @since 2017-9-19
*/
function web3_clientVersion()
{
$params = array();
$data = $this->request(__FUNCTION__, $params);
if ($data['result']) {
return true;
} else {
return false;
}
//return $data['result'];
}
function txpool_status()
{
$params = array();
$data = $this->request(__FUNCTION__, $params);
if ($data['result']) {
return true;
} else {
return false;
}
//return $data['result'];
}
/**
* 获取主账户
* @author qiuphp2
* @since 2017-9-19
*/
function eth_coinbase()
{
$params = array();
$data = $this->request(__FUNCTION__, $params);
if ($data['result']) {
return $data['result'];
} else {
return $data['error']['message'];
}
// return $data['result'];
}
/**
* 返回客户端持有的地址列表。
* @author qiuphp2
* @since 2017-9-19
*/
function eth_accounts()
{
$params = array();
$data = $this->request(__FUNCTION__, $params);
if ($data['result']) {
return $data['result'];
} else {
return $data['error']['message'];
}
// return $data['result'];
}
/**
* 获取区块数量
* @author qiuphp2
* @since 2017-9-19
*/
function eth_blockNumber()
{
$params = array();
$data = $this->request(__FUNCTION__, $params);
if ($data['result']) {
return $data['result'];
} else {
return $data['error']['message'];
}
// return $data['result'];
}
/**
* 对于已经同步的客户端,该调用返回一个描述同步状态的对象;对于未同步客户端,返回false
* @author qiuphp2
* @since 2017-9-19
*/
function eth_syncing()
{
$params = array();
$data = $this->request(__FUNCTION__, $params);
if ($data['result']) {
return $data['result'];
} else {
return $data['error']['message'];
}
// return $data['result'];
}
/**
* 新建账号 有点耗时 最好给用户生成的之后,密码保存在数据库里面
* @author qiuphp2
* @since 2017-9-19
*/
function personal_newAccount($password='')//一般用账户名作为加密密码
{
// $password = &
USDT-RPC+归集
最新推荐文章于 2023-12-26 12:02:28 发布