Web3
Web3 api: * clientVersion * sha3 Basic object: * HttpProvider * HttpRequestManager
This commit is contained in:
parent
1bb7746329
commit
eca04e1c91
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,6 +1,7 @@
|
|||||||
composer.phar
|
composer.phar
|
||||||
/vendor/
|
/vendor/
|
||||||
|
.phpintel/
|
||||||
|
|
||||||
# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
|
# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
|
||||||
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
|
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
|
||||||
# composer.lock
|
composer.lock
|
||||||
|
29
composer.json
Normal file
29
composer.json
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
{
|
||||||
|
"name": "sc0vu/web3.php",
|
||||||
|
"description": "Ethereum web3 interface.",
|
||||||
|
"type": "library",
|
||||||
|
"license": "MIT",
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "sc0Vu",
|
||||||
|
"email": "alk03073135@gmail.com"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"minimum-stability": "dev",
|
||||||
|
"require": {
|
||||||
|
"guzzlehttp/guzzle": "~6.0"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"phpunit/phpunit": "^7.0@dev"
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Web3\\": "src/"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"autoload-dev": {
|
||||||
|
"psr-4": {
|
||||||
|
"Test\\": "test/"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
22
phpunit.xml
Normal file
22
phpunit.xml
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<phpunit backupGlobals="false"
|
||||||
|
backupStaticAttributes="false"
|
||||||
|
bootstrap="vendor/autoload.php"
|
||||||
|
colors="true"
|
||||||
|
convertErrorsToExceptions="true"
|
||||||
|
convertNoticesToExceptions="true"
|
||||||
|
convertWarningsToExceptions="true"
|
||||||
|
processIsolation="false"
|
||||||
|
stopOnFailure="false"
|
||||||
|
syntaxCheck="false">
|
||||||
|
|
||||||
|
<testsuite name="Web3.php unit test">
|
||||||
|
<directory suffix="Test.php">./test/unit</directory>
|
||||||
|
</testsuite>
|
||||||
|
|
||||||
|
<filter>
|
||||||
|
<whitelist processUncoveredFilesFromWhitelist="true">
|
||||||
|
<directory suffix=".php">./app</directory>
|
||||||
|
</whitelist>
|
||||||
|
</filter>
|
||||||
|
</phpunit>
|
102
src/Eth.php
Normal file
102
src/Eth.php
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Web3;
|
||||||
|
|
||||||
|
use Web3\Providers\Provider;
|
||||||
|
|
||||||
|
class Eth
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* provider
|
||||||
|
*
|
||||||
|
* @var \Web3\Providers\Provider
|
||||||
|
*/
|
||||||
|
protected $provider;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* construct
|
||||||
|
*
|
||||||
|
* @param mixed string | provider $provider
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct($provider)
|
||||||
|
{
|
||||||
|
if (is_string($provider) && (filter_var($provider, FILTER_VALIDATE_URL) !== false)) {
|
||||||
|
// check the uri schema
|
||||||
|
if (preg_match('/^https?:\/\//', $provider) === 1) {
|
||||||
|
$this->provider = $provider;
|
||||||
|
}
|
||||||
|
} else if ($provider instanceof Provider) {
|
||||||
|
$this->provider = $provider;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* call
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* @param array $arguments
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __call($name, $arguments)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function __get($name)
|
||||||
|
{
|
||||||
|
$method = 'get' . ucfirst($name);
|
||||||
|
|
||||||
|
if (method_exists($this, $method)) {
|
||||||
|
return call_user_func_array([$this, $method], []);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* @param mixed $value
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function __set($name, $value)
|
||||||
|
{
|
||||||
|
$method = 'set' . ucfirst($name);
|
||||||
|
|
||||||
|
if (method_exists($this, $method)) {
|
||||||
|
return call_user_func_array([$this, $method], [$value]);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getProvider
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function getProvider()
|
||||||
|
{
|
||||||
|
return $this->provider;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* setProvider
|
||||||
|
*
|
||||||
|
* @param $provider
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function setProvider($provider)
|
||||||
|
{
|
||||||
|
if ($provider instanceof Provider) {
|
||||||
|
$this->provider = $provider;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
91
src/Providers/HttpProvider.php
Normal file
91
src/Providers/HttpProvider.php
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Web3\Providers;
|
||||||
|
|
||||||
|
use Web3\Providers\Provider;
|
||||||
|
use Web3\Providers\IProvider;
|
||||||
|
use Web3\RequestManagers\RequestManager;
|
||||||
|
|
||||||
|
class HttpProvider extends Provider implements IProvider
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* construct
|
||||||
|
*
|
||||||
|
* @param \Web3\RequestManagers\RequestManager $requestManager
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct(RequestManager $requestManager)
|
||||||
|
{
|
||||||
|
parent::__construct($requestManager);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* send
|
||||||
|
*
|
||||||
|
* @param string $method
|
||||||
|
* @param array $arguments
|
||||||
|
* @param callable $callback
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function send($method, $arguments, $callback)
|
||||||
|
{
|
||||||
|
$rpc = $this->createRpc($method, $arguments);
|
||||||
|
|
||||||
|
if (!$this->isBatch) {
|
||||||
|
$this->requestManager->sendPayload(json_encode($rpc), $callback);
|
||||||
|
} else {
|
||||||
|
$this->batch[] = json_encode($rpc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* batch
|
||||||
|
*
|
||||||
|
* @param bool $status
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function batch($status)
|
||||||
|
{
|
||||||
|
$status = is_bool($status);
|
||||||
|
|
||||||
|
$this->isBatch = $status;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* execute
|
||||||
|
*
|
||||||
|
* @param callable $callback
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function execute($callback)
|
||||||
|
{
|
||||||
|
if (!$this->isBatch) {
|
||||||
|
throw new \RuntimeException('Please batch json rpc first.');
|
||||||
|
}
|
||||||
|
$this->requestManager->sendPayload('[' . implode(',', $this->batch) . ']', $callback);
|
||||||
|
$this->batch = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* createRpc
|
||||||
|
*
|
||||||
|
* @param string $rpc
|
||||||
|
* @param array $arguments
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function createRpc($rpc, $arguments)
|
||||||
|
{
|
||||||
|
$this->id += 1;
|
||||||
|
|
||||||
|
$rpc = [
|
||||||
|
'id' => $this->id,
|
||||||
|
'jsonrpc' => $this->rpcVersion,
|
||||||
|
'method' => $rpc
|
||||||
|
];
|
||||||
|
|
||||||
|
if (count($arguments) > 0) {
|
||||||
|
$rpc['params'] = $arguments;
|
||||||
|
}
|
||||||
|
return $rpc;
|
||||||
|
}
|
||||||
|
}
|
32
src/Providers/IProvider.php
Normal file
32
src/Providers/IProvider.php
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Web3\Providers;
|
||||||
|
|
||||||
|
interface IProvider
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* send
|
||||||
|
*
|
||||||
|
* @param string $method
|
||||||
|
* @param array $arguments
|
||||||
|
* @param callable $callback
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function send($method, $arguments, $callback);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* batch
|
||||||
|
*
|
||||||
|
* @param bool $status
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function batch($status);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* execute
|
||||||
|
*
|
||||||
|
* @param callable $callback
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function execute($callback);
|
||||||
|
}
|
106
src/Providers/Provider.php
Normal file
106
src/Providers/Provider.php
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Web3\Providers;
|
||||||
|
|
||||||
|
use Web3\RequestManagers\RequestManager;
|
||||||
|
|
||||||
|
class Provider
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* requestManager
|
||||||
|
*
|
||||||
|
* @var \Web3\RequestManagers\RequestManager
|
||||||
|
*/
|
||||||
|
protected $requestManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* isBatch
|
||||||
|
*
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
protected $isBatch = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* batch
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $batch = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* rpcVersion
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $rpcVersion = '2.0';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* id
|
||||||
|
*
|
||||||
|
* @var integer
|
||||||
|
*/
|
||||||
|
protected $id = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* construct
|
||||||
|
*
|
||||||
|
* @param \Web3\RequestManagers\RequestManager $requestManager
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct(RequestManager $requestManager)
|
||||||
|
{
|
||||||
|
$this->requestManager = $requestManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function __get($name)
|
||||||
|
{
|
||||||
|
$method = 'get' . ucfirst($name);
|
||||||
|
|
||||||
|
if (method_exists($this, $method)) {
|
||||||
|
return call_user_func_array([$this, $method], []);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* @param mixed $value
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function __set($name, $value)
|
||||||
|
{
|
||||||
|
$method = 'set' . ucfirst($name);
|
||||||
|
|
||||||
|
if (method_exists($this, $method)) {
|
||||||
|
return call_user_func_array([$this, $method], [$value]);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getRequestManager
|
||||||
|
*
|
||||||
|
* @return \Web3\RequestManagers\RequestManager
|
||||||
|
*/
|
||||||
|
public function getRequestManager()
|
||||||
|
{
|
||||||
|
return $this->requestManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getIsBatch
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function getIsBatch()
|
||||||
|
{
|
||||||
|
return $this->isBatch;
|
||||||
|
}
|
||||||
|
}
|
79
src/RequestManagers/HttpRequestManager.php
Normal file
79
src/RequestManagers/HttpRequestManager.php
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Web3\RequestManagers;
|
||||||
|
|
||||||
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
use GuzzleHttp\Exception\RequestException;
|
||||||
|
use GuzzleHttp\Client;
|
||||||
|
use Web3\RequestManagers\RequestManager;
|
||||||
|
use Web3\RequestManagers\IRequestManager;
|
||||||
|
|
||||||
|
class HttpRequestManager extends RequestManager implements IRequestManager
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* client
|
||||||
|
*
|
||||||
|
* @var \GuzzleHttp
|
||||||
|
*/
|
||||||
|
protected $client;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* construct
|
||||||
|
*
|
||||||
|
* @param string $host
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct($host)
|
||||||
|
{
|
||||||
|
parent::__construct($host);
|
||||||
|
$this->client = new Client;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sendPayload
|
||||||
|
*
|
||||||
|
* @param string $payload
|
||||||
|
* @param callable $callback
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function sendPayload($payload, $callback)
|
||||||
|
{
|
||||||
|
if (!is_string($payload)) {
|
||||||
|
throw new \RuntimeException('Payload must be string.');
|
||||||
|
}
|
||||||
|
// $promise = $this->client->postAsync($this->host, [
|
||||||
|
// 'headers' => [
|
||||||
|
// 'content-type' => 'application/json'
|
||||||
|
// ],
|
||||||
|
// 'body' => $payload
|
||||||
|
// ]);
|
||||||
|
// $promise->then(
|
||||||
|
// function (ResponseInterface $res) use ($callback) {
|
||||||
|
// var_dump($res->body());
|
||||||
|
// call_user_func($callback, null, $res);
|
||||||
|
// },
|
||||||
|
// function (RequestException $err) use ($callback) {
|
||||||
|
// var_dump($err->getMessage());
|
||||||
|
// call_user_func($callback, $err, null);
|
||||||
|
// }
|
||||||
|
// );
|
||||||
|
try {
|
||||||
|
$res = $this->client->post($this->host, [
|
||||||
|
'headers' => [
|
||||||
|
'content-type' => 'application/json'
|
||||||
|
],
|
||||||
|
'body' => $payload
|
||||||
|
]);
|
||||||
|
$json = json_decode($res->getBody());
|
||||||
|
|
||||||
|
if (JSON_ERROR_NONE !== json_last_error()) {
|
||||||
|
throw new \InvalidArgumentException(
|
||||||
|
'json_decode error: ' . json_last_error_msg());
|
||||||
|
}
|
||||||
|
|
||||||
|
call_user_func($callback, null, $json);
|
||||||
|
} catch (RequestException $err) {
|
||||||
|
call_user_func($callback, $err, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
15
src/RequestManagers/IRequestManager.php
Normal file
15
src/RequestManagers/IRequestManager.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Web3\RequestManagers;
|
||||||
|
|
||||||
|
interface IRequestManager
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* sendPayload
|
||||||
|
*
|
||||||
|
* @param string $payload
|
||||||
|
* @param callable $callback
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function sendPayload($payload, $callback);
|
||||||
|
}
|
67
src/RequestManagers/RequestManager.php
Normal file
67
src/RequestManagers/RequestManager.php
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Web3\RequestManagers;
|
||||||
|
|
||||||
|
class RequestManager
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* host
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $host;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* construct
|
||||||
|
*
|
||||||
|
* @param string $host
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct($host)
|
||||||
|
{
|
||||||
|
$this->host = $host;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function __get($name)
|
||||||
|
{
|
||||||
|
$method = 'get' . ucfirst($name);
|
||||||
|
|
||||||
|
if (method_exists($this, $method)) {
|
||||||
|
return call_user_func_array([$this, $method], []);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* @param mixed $value
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function __set($name, $value)
|
||||||
|
{
|
||||||
|
$method = 'set' . ucfirst($name);
|
||||||
|
|
||||||
|
if (method_exists($this, $method)) {
|
||||||
|
return call_user_func_array([$this, $method], [$value]);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getHost
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function getHost()
|
||||||
|
{
|
||||||
|
return $this->host;
|
||||||
|
}
|
||||||
|
}
|
161
src/Web3.php
Normal file
161
src/Web3.php
Normal file
@ -0,0 +1,161 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Web3;
|
||||||
|
|
||||||
|
use Web3\Eth;
|
||||||
|
use Web3\Providers\Provider;
|
||||||
|
use Web3\Providers\HttpProvider;
|
||||||
|
use Web3\RequestManagers\RequestManager;
|
||||||
|
use Web3\RequestManagers\HttpRequestManager;
|
||||||
|
|
||||||
|
class Web3
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* provider
|
||||||
|
*
|
||||||
|
* @var \Web3\Providers\Provider
|
||||||
|
*/
|
||||||
|
protected $provider;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* eth
|
||||||
|
*
|
||||||
|
* @var \Web3\Eth
|
||||||
|
*/
|
||||||
|
protected $eth;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* construct
|
||||||
|
*
|
||||||
|
* @param mixed string | Web3\Providers\Provider $provider
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct($provider)
|
||||||
|
{
|
||||||
|
if (is_string($provider) && (filter_var($provider, FILTER_VALIDATE_URL) !== false)) {
|
||||||
|
// check the uri schema
|
||||||
|
if (preg_match('/^https?:\/\//', $provider) === 1) {
|
||||||
|
$requestManeger = new HttpRequestManager($provider);
|
||||||
|
|
||||||
|
$this->provider = new HttpProvider($requestManeger);
|
||||||
|
}
|
||||||
|
} else if ($provider instanceof Provider) {
|
||||||
|
$this->provider = $provider;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* call
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* @param array $arguments
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __call($name, $arguments)
|
||||||
|
{
|
||||||
|
if (empty($this->provider)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$class = explode('\\', get_class());
|
||||||
|
|
||||||
|
if (strtolower($class[0]) === 'web3' && preg_match('/^[a-zA-Z0-9]+$/', $name) === 1) {
|
||||||
|
$method = strtolower($class[1]) . '_' . $name;
|
||||||
|
|
||||||
|
if ($this->provider->isBatch) {
|
||||||
|
$this->provider->send($method, $arguments, null);
|
||||||
|
} else {
|
||||||
|
$callback = array_pop($arguments);
|
||||||
|
|
||||||
|
if (is_callable($callback) !== true) {
|
||||||
|
throw new \RuntimeException('The last param must be callback function.');
|
||||||
|
}
|
||||||
|
$this->provider->send($method, $arguments, $callback);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function __get($name)
|
||||||
|
{
|
||||||
|
$method = 'get' . ucfirst($name);
|
||||||
|
|
||||||
|
if (method_exists($this, $method)) {
|
||||||
|
return call_user_func_array([$this, $method], []);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* @param mixed $value
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function __set($name, $value)
|
||||||
|
{
|
||||||
|
$method = 'set' . ucfirst($name);
|
||||||
|
|
||||||
|
if (method_exists($this, $method)) {
|
||||||
|
return call_user_func_array([$this, $method], [$value]);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getProvider
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function getProvider()
|
||||||
|
{
|
||||||
|
return $this->provider;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* setProvider
|
||||||
|
*
|
||||||
|
* @param $provider
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function setProvider($provider)
|
||||||
|
{
|
||||||
|
if ($provider instanceof Provider) {
|
||||||
|
$this->provider = $provider;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getEth
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function getEth()
|
||||||
|
{
|
||||||
|
if (!isset($this->eth)) {
|
||||||
|
$eth = new Eth($this->provider);
|
||||||
|
$this->eth = $eth;
|
||||||
|
}
|
||||||
|
return $this->eth;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* batch
|
||||||
|
*
|
||||||
|
* @param bool $status
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function batch($status)
|
||||||
|
{
|
||||||
|
$status = is_bool($status);
|
||||||
|
|
||||||
|
$this->provider->batch($status);
|
||||||
|
}
|
||||||
|
}
|
22
test/TestCase.php
Normal file
22
test/TestCase.php
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Test;
|
||||||
|
|
||||||
|
use \PHPUnit\Framework\TestCase as BaseTestCase;
|
||||||
|
|
||||||
|
class TestCase extends BaseTestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* setUp
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function setUp() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* tearDown
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function tearDown() {}
|
||||||
|
}
|
13
test/unit/ProviderTest.php
Normal file
13
test/unit/ProviderTest.php
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Test\Unit;
|
||||||
|
|
||||||
|
use Test\TestCase;
|
||||||
|
|
||||||
|
class ProviderTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testHelloWorld()
|
||||||
|
{
|
||||||
|
$this->assertTrue(true);
|
||||||
|
}
|
||||||
|
}
|
106
test/unit/Web3Test.php
Normal file
106
test/unit/Web3Test.php
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Test\Unit;
|
||||||
|
|
||||||
|
use Test\TestCase;
|
||||||
|
use Web3\Web3;
|
||||||
|
use Web3\Eth;
|
||||||
|
use Web3\Providers\HttpProvider;
|
||||||
|
use Web3\RequestManagers\RequestManager;
|
||||||
|
|
||||||
|
class Web3Test extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* web3
|
||||||
|
*
|
||||||
|
* @var \Web3\Web3
|
||||||
|
*/
|
||||||
|
protected $web3;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* testHex
|
||||||
|
* 'hello world'
|
||||||
|
* you can check by call pack('H*', $hex)
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $testHex = '0x68656c6c6f20776f726c64';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* testHash
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $testHash = '0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* setUp
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
$web3 = new Web3('https://rinkeby.infura.io/vuethexplore');
|
||||||
|
$this->web3 = $web3;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* testInstance
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testInstance()
|
||||||
|
{
|
||||||
|
$web3 = $this->web3;
|
||||||
|
|
||||||
|
$this->assertTrue($web3->provider instanceof HttpProvider);
|
||||||
|
$this->assertTrue($web3->provider->requestManager instanceof RequestManager);
|
||||||
|
$this->assertTrue($web3->eth instanceof Eth);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* testSend
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testSend()
|
||||||
|
{
|
||||||
|
$web3 = $this->web3;
|
||||||
|
|
||||||
|
$web3->clientVersion(function ($err, $version) {
|
||||||
|
if ($err !== null) {
|
||||||
|
return $this->markTestIncomplete($err->getMessage());
|
||||||
|
}
|
||||||
|
$this->assertTrue(is_string($version->result));
|
||||||
|
});
|
||||||
|
|
||||||
|
$web3->sha3($this->testHex, function ($err, $hash) {
|
||||||
|
if ($err !== null) {
|
||||||
|
return $this->markTestIncomplete($err->getMessage());
|
||||||
|
}
|
||||||
|
$this->assertEquals($hash->result, $this->testHash);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* testBatch
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testBatch()
|
||||||
|
{
|
||||||
|
$web3 = $this->web3;
|
||||||
|
|
||||||
|
$web3->batch(true);
|
||||||
|
$web3->clientVersion();
|
||||||
|
$web3->sha3($this->testHex);
|
||||||
|
|
||||||
|
$web3->provider->execute(function ($err, $data) {
|
||||||
|
if ($err !== null) {
|
||||||
|
return $this->markTestIncomplete($err->getMessage());
|
||||||
|
}
|
||||||
|
$this->assertTrue(is_string($data[0]->result));
|
||||||
|
$this->assertEquals($data[1]->result, $this->testHash);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user