pool/web/yaamp/core/rpc/wallet-rpc.php
Tanguy Pruvot 35220977ef new wallet rpc class to handle martians
add some bitcoin rpc commands to query geth wallet informations (balance,diff,connections..)

todo: handle explorer and transactions list
2016-05-29 09:39:04 +02:00

122 lines
3.4 KiB
PHP

<?php
/**
* Common yiimp Wallet RPC object
*/
class WalletRPC {
public $type = 'Bitcoin';
private $rpc;
// cache
private $accounts;
private $info;
private $height = 0;
// Information and debugging
// public $status;
// public $error;
// public $raw_response;
// public $response;
function __construct($userOrCoin, $pw='', $host='localhost', $port=8332, $url=null)
{
if (is_object($userOrCoin)) {
$coin = $userOrCoin;
switch ($coin->rpcencoding) {
case 'GETH':
$this->type = 'Ethereum';
$this->rpc = new Ethereum($coin->rpchost, $coin->rpcport);
break;
default:
$this->type = 'Bitcoin';
$this->rpc = new Bitcoin($coin->rpcuser, $coin->rpcpasswd, $coin->rpchost, $coin->rpcport, $url);
}
} else {
// backward compat
$user = $userOrCoin;
$this->rpc = new Bitcoin($user, $pw, $host, $port, $url);
}
}
function __call($method, $params)
{
if ($this->type == 'Ethereum') {
// convert common methods used by yiimp
switch ($method) {
case 'getaccountaddress':
if (!isset($this->accounts))
$this->accounts = $this->rpc->eth_accounts();
return arraySafeVal($this->accounts, 0, $params);
case 'getinfo':
if (!isset($this->info)) {
$info = array();
//$eth = $this->rpc->request_object('eth'); // todo when possible
//var_dump($eth);
if (!isset($this->accounts))
$this->accounts = $this->rpc->eth_accounts();
$balances = 0;
$info['accounts'] = array();
foreach ($this->accounts as $addr) {
// web3.fromWei(eth.getBalance("0x..."), "ether")
$balance = (double) $this->rpc->eth_getBalance($addr,'latest', true);
$balance /= 1e18;
$balances += $balance;
$info['accounts'][$addr] = $balance;
}
$info['balance'] = $balances;
$this->height = $this->height ? $this->height : $this->rpc->eth_blockNumber();
$info['blocks'] = $this->height;
$info['gasprice'] = (double) $this->rpc->eth_gasPrice();
$info['gasprice'] /= 1e18;
$info['connections'] = $this->rpc->net_peerCount();
$info['version'] = $this->rpc->web3_clientVersion();
$this->info = $info;
}
return $this->info;
case 'getdifficulty':
$this->height = $this->height ? $this->height : $this->rpc->eth_blockNumber();
$block = $this->rpc->eth_getBlockByNumber($this->height);
$difficulty = objSafeVal($block, 'difficulty', 0);
return $this->rpc->decode_hex($difficulty);
case 'getmininginfo':
$info = array();
$this->height = $this->height ? $this->height : $this->rpc->eth_blockNumber();
$info['blocks'] = $this->height;
$block = $this->rpc->eth_getBlockByNumber($info['blocks']);
$difficulty = objSafeVal($block, 'difficulty', 0);
$info['difficulty'] = $this->rpc->decode_hex($difficulty);
$info['generate'] = $this->rpc->eth_mining();
$info['errors'] = '';
return $info;
case 'getpeerinfo':
$peers = array();
return $peers;
case 'getwork':
return $this->rpc->eth_getWork();
case 'listtransactions':
$txs = array();
// todo...
return $txs;
default:
return $this->rpc->$method($params);
}
}
return $this->rpc->__call($method,$params);
}
function __get($prop)
{
//debuglog("wallet get $prop ".json_encode($this->rpc->$prop));
return $this->rpc->$prop;
}
function __set($prop, $value)
{
//debuglog("wallet set $prop ".json_encode($value));
$this->rpc->$prop = $value;
}
}