eth_getBalance

* eth_getBalance
* AddressValidator
* TagValidator
This commit is contained in:
sc0Vu 2017-12-13 16:49:26 +08:00
parent f3833e45b6
commit 31147172ec
4 changed files with 75 additions and 0 deletions

View File

@ -6,6 +6,8 @@ use Web3\Providers\Provider;
use Web3\Providers\HttpProvider;
use Web3\RequestManagers\RequestManager;
use Web3\RequestManagers\HttpRequestManager;
use Web3\Validators\AddressValidator;
use Web3\Validators\TagValidator;
class Eth
{
@ -30,6 +32,12 @@ class Eth
'eth_gasPrice' => [],
'eth_accounts' => [],
'eth_blockNumber' => [],
'eth_getBalance' => [
'params'=> [
AddressValidator::class,
TagValidator::class
]
],
];
/**

View File

@ -0,0 +1,22 @@
<?php
namespace Web3\Validators;
use Web3\Validators\IValidator;
class AddressValidator
{
/**
* validate
*
* @param string $value
* @return bool
*/
public static function validate($value)
{
if (!is_string($value)) {
return false;
}
return (preg_match('/^0x[a-fA-F0-9]{40}$/', $value) >= 1);
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace Web3\Validators;
use Web3\Validators\IValidator;
class TagValidator
{
/**
* validate
*
* @param string $value
* @return bool
*/
public static function validate($value)
{
$tags = [
'latest', 'earliest', 'pending'
];
// maybe change in_int future
return (is_int($value) || in_array($value, $tags));
}
}

View File

@ -199,6 +199,27 @@ class EthTest extends TestCase
});
}
/**
* testGetBalance
*
* @return void
*/
public function testGetBalance()
{
$eth = $this->web3->eth;
$eth->getBalance('0x407d73d8a49eeb85d32cf465507dd71d507100c1', 'latest', function ($err, $balance) {
if ($err !== null) {
return $this->fail($err->getMessage());
}
if (isset($balance->result)) {
$this->assertTrue(is_string($balance->result));
} else {
$this->fail($balance->error->message);
}
});
}
/**
* testUnallowedMethod
*