commit
451d7ba117
34
src/Formatters/NumberFormatter.php
Normal file
34
src/Formatters/NumberFormatter.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of web3.php package.
|
||||
*
|
||||
* (c) Kuan-Cheng,Lai <alk03073135@gmail.com>
|
||||
*
|
||||
* @author Peter Lai <alk03073135@gmail.com>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
namespace Web3\Formatters;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use Web3\Utils;
|
||||
use Web3\Formatters\IFormatter;
|
||||
|
||||
class NumberFormatter implements IFormatter
|
||||
{
|
||||
/**
|
||||
* format
|
||||
*
|
||||
* @param mixed $value
|
||||
* @return int
|
||||
*/
|
||||
public static function format($value)
|
||||
{
|
||||
$value = Utils::toString($value);
|
||||
$bn = Utils::toBn($value);
|
||||
$int = (int) $bn->toString();
|
||||
|
||||
return $int;
|
||||
}
|
||||
}
|
@ -84,7 +84,7 @@ class EthMethod extends JSONRPC implements IMethod
|
||||
* @param array $params
|
||||
* @return bool
|
||||
*/
|
||||
public function validate($params)
|
||||
public function validate(&$params)
|
||||
{
|
||||
if (!is_array($params)) {
|
||||
throw new InvalidArgumentException('Please use array params when call validate.');
|
||||
@ -147,22 +147,22 @@ class EthMethod extends JSONRPC implements IMethod
|
||||
if (!is_array($rules)) {
|
||||
throw new InvalidArgumentException('Please use array rules when call transform.');
|
||||
}
|
||||
if (count($params) < count($rules)) {
|
||||
if (!isset($this->defaultValues) || empty($this->defaultValues)) {
|
||||
throw new \InvalidArgumentException('The params are less than inputFormatters.');
|
||||
}
|
||||
$defaultValues = $this->defaultValues;
|
||||
// $defaultValues = $this->defaultValues;
|
||||
|
||||
foreach ($defaultValues as $key => $value) {
|
||||
if (!isset($params[$key])) {
|
||||
$params[$key] = $value;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
foreach ($params as $key => $param) {
|
||||
if (isset($rules[$key])) {
|
||||
$params[$key] = call_user_func([$rules[$key], 'format'], $param);
|
||||
$formatted = call_user_func([$rules[$key], 'format'], $param);
|
||||
|
||||
// if (is_int($formatted)) {
|
||||
// if ($formatted >= 0) {
|
||||
// $params[$key] = $formatted;
|
||||
// } elseif (isset($defaultValues[$key])) {
|
||||
// $params[$key] = $defaultValues[$key];
|
||||
// }
|
||||
// } else {
|
||||
// $params[$key] = $formatted;
|
||||
// }
|
||||
$params[$key] = $formatted;
|
||||
}
|
||||
}
|
||||
return $params;
|
||||
|
@ -18,7 +18,7 @@ use Web3\Validators\StringValidator;
|
||||
use Web3\Validators\QuantityValidator;
|
||||
use Web3\Formatters\AddressFormatter;
|
||||
use Web3\Formatters\StringFormatter;
|
||||
use Web3\Formatters\QuantityFormatter;
|
||||
use Web3\Formatters\NumberFormatter;
|
||||
|
||||
class UnlockAccount extends EthMethod
|
||||
{
|
||||
@ -37,7 +37,7 @@ class UnlockAccount extends EthMethod
|
||||
* @var array
|
||||
*/
|
||||
protected $inputFormatters = [
|
||||
AddressFormatter::class, StringFormatter::class, QuantityFormatter::class
|
||||
AddressFormatter::class, StringFormatter::class, NumberFormatter::class
|
||||
];
|
||||
|
||||
/**
|
||||
|
@ -88,8 +88,33 @@ class PersonalApiTest extends TestCase
|
||||
|
||||
$personal->unlockAccount($this->newAccount, '123456', function ($err, $unlocked) {
|
||||
if ($err !== null) {
|
||||
// infura banned us to use unlock account
|
||||
return $this->assertTrue($err->getCode() === 405);
|
||||
return $this->fail($err->getMessage());
|
||||
}
|
||||
$this->assertTrue($unlocked);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* testUnlockAccountWithDuration
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testUnlockAccountWithDuration()
|
||||
{
|
||||
$personal = $this->personal;
|
||||
|
||||
// create account
|
||||
$personal->newAccount('123456', function ($err, $account) {
|
||||
if ($err !== null) {
|
||||
return $this->fail($e->getMessage());
|
||||
}
|
||||
$this->newAccount = $account;
|
||||
$this->assertTrue(is_string($account));
|
||||
});
|
||||
|
||||
$personal->unlockAccount($this->newAccount, '123456', 100, function ($err, $unlocked) {
|
||||
if ($err !== null) {
|
||||
return $this->fail($err->getMessage());
|
||||
}
|
||||
$this->assertTrue($unlocked);
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user