Skip to content

Commit

Permalink
Add fee history api
Browse files Browse the repository at this point in the history
  • Loading branch information
sc0Vu committed Sep 4, 2023
1 parent 31101fe commit 8eaf266
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/Eth.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Eth
* @var array
*/
private $allowedMethods = [
'eth_protocolVersion', 'eth_syncing', 'eth_coinbase', 'eth_mining', 'eth_hashrate', 'eth_gasPrice', 'eth_accounts', 'eth_blockNumber', 'eth_getBalance', 'eth_getStorageAt', 'eth_getTransactionCount', 'eth_getBlockTransactionCountByHash', 'eth_getBlockTransactionCountByNumber', 'eth_getUncleCountByBlockHash', 'eth_getUncleCountByBlockNumber', 'eth_getUncleByBlockHashAndIndex', 'eth_getUncleByBlockNumberAndIndex', 'eth_getCode', 'eth_sign', 'eth_sendTransaction', 'eth_sendRawTransaction', 'eth_call', 'eth_estimateGas', 'eth_getBlockByHash', 'eth_getBlockByNumber', 'eth_getTransactionByHash', 'eth_getTransactionByBlockHashAndIndex', 'eth_getTransactionByBlockNumberAndIndex', 'eth_getTransactionReceipt', 'eth_compileSolidity', 'eth_compileLLL', 'eth_compileSerpent', 'eth_getWork', 'eth_newFilter', 'eth_newBlockFilter', 'eth_newPendingTransactionFilter', 'eth_uninstallFilter', 'eth_getFilterChanges', 'eth_getFilterLogs', 'eth_getLogs', 'eth_submitWork', 'eth_submitHashrate'
'eth_protocolVersion', 'eth_syncing', 'eth_coinbase', 'eth_mining', 'eth_hashrate', 'eth_gasPrice', 'eth_accounts', 'eth_blockNumber', 'eth_getBalance', 'eth_getStorageAt', 'eth_getTransactionCount', 'eth_getBlockTransactionCountByHash', 'eth_getBlockTransactionCountByNumber', 'eth_getUncleCountByBlockHash', 'eth_getUncleCountByBlockNumber', 'eth_getUncleByBlockHashAndIndex', 'eth_getUncleByBlockNumberAndIndex', 'eth_getCode', 'eth_sign', 'eth_sendTransaction', 'eth_sendRawTransaction', 'eth_call', 'eth_estimateGas', 'eth_getBlockByHash', 'eth_getBlockByNumber', 'eth_getTransactionByHash', 'eth_getTransactionByBlockHashAndIndex', 'eth_getTransactionByBlockNumberAndIndex', 'eth_getTransactionReceipt', 'eth_compileSolidity', 'eth_compileLLL', 'eth_compileSerpent', 'eth_getWork', 'eth_newFilter', 'eth_newBlockFilter', 'eth_newPendingTransactionFilter', 'eth_uninstallFilter', 'eth_getFilterChanges', 'eth_getFilterLogs', 'eth_getLogs', 'eth_submitWork', 'eth_submitHashrate', 'eth_feeHistory'
];

/**
Expand Down
34 changes: 0 additions & 34 deletions src/Formatters/NumberFormatter.php

This file was deleted.

70 changes: 70 additions & 0 deletions src/Methods/Eth/FeeHistory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/**
* This file is part of web3.php package.
*
* (c) Kuan-Cheng,Lai <[email protected]>
*
* @author Peter Lai <[email protected]>
* @license MIT
*/

namespace Web3\Methods\Eth;

use InvalidArgumentException;
use Web3\Methods\EthMethod;
use Web3\Validators\TagValidator;
use Web3\Validators\QuantityValidator;
use Web3\Validators\ArrayNumberValidator;
use Web3\Formatters\QuantityFormatter;
use Web3\Formatters\OptionalQuantityFormatter;

class FeeHistory extends EthMethod
{
/**
* validators
*
* @var array
*/
protected $validators = [
QuantityValidator::class, [
TagValidator::class, QuantityValidator::class
], ArrayNumberValidator::class
];

/**
* inputFormatters
*
* @var array
*/
protected $inputFormatters = [
QuantityFormatter::class, OptionalQuantityFormatter::class
];

/**
* outputFormatters
*
* @var array
*/
protected $outputFormatters = [
];

/**
* defaultValues
*
* @var array
*/
protected $defaultValues = [];

/**
* construct
*
* @param string $method
* @param array $arguments
* @return void
*/
// public function __construct($method='', $arguments=[])
// {
// parent::__construct($method, $arguments);
// }
}
37 changes: 37 additions & 0 deletions src/Validators/ArrayNumberValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* This file is part of web3.php package.
*
* (c) Kuan-Cheng,Lai <[email protected]>
*
* @author Peter Lai <[email protected]>
* @license MIT
*/

namespace Web3\Validators;

use Web3\Validators\IValidator;

class ArrayNumberValidator
{
/**
* validate
* TODO: add min & max validation
*
* @param array[int|float] $value
* @return bool
*/
public static function validate($value)
{
if (!is_array($value)) {
return false;
}
foreach ($value as $val) {
if (!(is_int($val) || is_float($val))) {
return false;
}
}
return true;
}
}
2 changes: 1 addition & 1 deletion src/Validators/TagValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static function validate($value)
{
$value = Utils::toString($value);
$tags = [
'latest', 'earliest', 'pending'
'latest', 'earliest', 'pending', 'safe', 'finalized'
];

return in_array($value, $tags);
Expand Down
16 changes: 16 additions & 0 deletions test/unit/EthApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,22 @@ public function testSubmitHashrate()
});
}

/**
* testFeeHistory
*
* @return void
*/
public function testFeeHistory()
{
$eth = $this->eth;

$eth->feeHistory(1, 'latest', [ 1 ], function ($err, $feeHistory) {
if ($err !== null) {
return $this->fail($err->getMessage());
}
});
}

/**
* testGetBlockByNumberAsync
*
Expand Down

0 comments on commit 8eaf266

Please sign in to comment.