Eth protocolVersion
This commit is contained in:
parent
829e65bbb9
commit
d94d42b61e
52
src/Eth.php
52
src/Eth.php
@ -3,6 +3,9 @@
|
|||||||
namespace Web3;
|
namespace Web3;
|
||||||
|
|
||||||
use Web3\Providers\Provider;
|
use Web3\Providers\Provider;
|
||||||
|
use Web3\Providers\HttpProvider;
|
||||||
|
use Web3\RequestManagers\RequestManager;
|
||||||
|
use Web3\RequestManagers\HttpRequestManager;
|
||||||
|
|
||||||
class Eth
|
class Eth
|
||||||
{
|
{
|
||||||
@ -13,10 +16,19 @@ class Eth
|
|||||||
*/
|
*/
|
||||||
protected $provider;
|
protected $provider;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* methods
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private $methods = [
|
||||||
|
'eth_protocolVersion' => [],
|
||||||
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* construct
|
* construct
|
||||||
*
|
*
|
||||||
* @param mixed string | provider $provider
|
* @param mixed string | Web3\Providers\Provider $provider
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function __construct($provider)
|
public function __construct($provider)
|
||||||
@ -24,7 +36,9 @@ class Eth
|
|||||||
if (is_string($provider) && (filter_var($provider, FILTER_VALIDATE_URL) !== false)) {
|
if (is_string($provider) && (filter_var($provider, FILTER_VALIDATE_URL) !== false)) {
|
||||||
// check the uri schema
|
// check the uri schema
|
||||||
if (preg_match('/^https?:\/\//', $provider) === 1) {
|
if (preg_match('/^https?:\/\//', $provider) === 1) {
|
||||||
$this->provider = $provider;
|
$requestManeger = new HttpRequestManager($provider);
|
||||||
|
|
||||||
|
$this->provider = new HttpProvider($requestManeger);
|
||||||
}
|
}
|
||||||
} else if ($provider instanceof Provider) {
|
} else if ($provider instanceof Provider) {
|
||||||
$this->provider = $provider;
|
$this->provider = $provider;
|
||||||
@ -40,7 +54,39 @@ class Eth
|
|||||||
*/
|
*/
|
||||||
public function __call($name, $arguments)
|
public function __call($name, $arguments)
|
||||||
{
|
{
|
||||||
//
|
if (empty($this->provider)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$class = explode('\\', get_class());
|
||||||
|
|
||||||
|
if (strtolower($class[1]) === 'eth' && preg_match('/^[a-zA-Z0-9]+$/', $name) === 1) {
|
||||||
|
$method = strtolower($class[1]) . '_' . $name;
|
||||||
|
|
||||||
|
if (!array_key_exists($method, $this->methods)) {
|
||||||
|
throw new \RuntimeException('Unallowed rpc method: ' . $method);
|
||||||
|
}
|
||||||
|
$allowedMethod = $this->methods[$method];
|
||||||
|
|
||||||
|
if (isset($allowedMethod['params'])) {
|
||||||
|
// validate params
|
||||||
|
foreach ($allowedMethod['params'] as $key => $rule) {
|
||||||
|
if (call_user_func([$rule, 'validate'], $arguments[$key]) === false) {
|
||||||
|
throw new \RuntimeException('Wrong type of ' . $name . ' method argument ' . $key . '.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($this->provider->isBatch) {
|
||||||
|
$this->provider->send($method, $arguments, null);
|
||||||
|
} else {
|
||||||
|
$callback = array_pop($arguments);
|
||||||
|
|
||||||
|
if (is_callable($callback) !== true) {
|
||||||
|
throw new \InvalidArgumentException('The last param must be callback function.');
|
||||||
|
}
|
||||||
|
$this->provider->send($method, $arguments, $callback);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -74,7 +74,7 @@ class Web3
|
|||||||
|
|
||||||
$class = explode('\\', get_class());
|
$class = explode('\\', get_class());
|
||||||
|
|
||||||
if (strtolower($class[0]) === 'web3' && preg_match('/^[a-zA-Z0-9]+$/', $name) === 1) {
|
if (strtolower($class[1]) === 'web3' && preg_match('/^[a-zA-Z0-9]+$/', $name) === 1) {
|
||||||
$method = strtolower($class[1]) . '_' . $name;
|
$method = strtolower($class[1]) . '_' . $name;
|
||||||
|
|
||||||
if (!array_key_exists($method, $this->methods)) {
|
if (!array_key_exists($method, $this->methods)) {
|
||||||
|
75
test/unit/EthTest.php
Normal file
75
test/unit/EthTest.php
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Test\Unit;
|
||||||
|
|
||||||
|
use RuntimeException;
|
||||||
|
use Test\TestCase;
|
||||||
|
use Web3\Web3;
|
||||||
|
use Web3\Eth;
|
||||||
|
use Web3\Providers\HttpProvider;
|
||||||
|
use Web3\RequestManagers\RequestManager;
|
||||||
|
|
||||||
|
class EthTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* web3
|
||||||
|
*
|
||||||
|
* @var \Web3\Web3
|
||||||
|
*/
|
||||||
|
protected $web3;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* setUp
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
$web3 = new Web3('https://rinkeby.infura.io/vuethexplore');
|
||||||
|
$this->web3 = $web3;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* testSend
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testSend()
|
||||||
|
{
|
||||||
|
$eth = $this->web3->eth;
|
||||||
|
|
||||||
|
$eth->protocolVersion(function ($err, $version) {
|
||||||
|
if ($err !== null) {
|
||||||
|
return $this->fail($err->getMessage());
|
||||||
|
}
|
||||||
|
if (isset($version->result)) {
|
||||||
|
$this->assertTrue(is_string($version->result));
|
||||||
|
} else {
|
||||||
|
$this->fail($version->error->message);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* testUnallowedMethod
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testUnallowedMethod()
|
||||||
|
{
|
||||||
|
$this->expectException(RuntimeException::class);
|
||||||
|
|
||||||
|
$eth = $this->web3->eth;
|
||||||
|
|
||||||
|
$eth->hello(function ($err, $hello) {
|
||||||
|
if ($err !== null) {
|
||||||
|
return $this->fail($err->getMessage());
|
||||||
|
}
|
||||||
|
if (isset($hello->result)) {
|
||||||
|
$this->assertTrue(true);
|
||||||
|
} else {
|
||||||
|
$this->fail($hello->error->message);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user