Skip to content

Commit

Permalink
Format the response from eth_feeHistory
Browse files Browse the repository at this point in the history
  • Loading branch information
sc0Vu committed Sep 4, 2023
1 parent 85ee68a commit 3cef7fc
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
46 changes: 46 additions & 0 deletions src/Formatters/FeeHistoryFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

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

namespace Web3\Formatters;

use InvalidArgumentException;
use Web3\Utils;
use Web3\Formatters\IFormatter;
use Web3\Formatters\BigNumberFormatter;

class FeeHistoryFormatter implements IFormatter
{
/**
* format
*
* @param mixed $value
* @return string
*/
public static function format($value)
{
if (isset($value->oldestBlock)) {
$value->oldestBlock = BigNumberFormatter::format($value->oldestBlock);
}
if (isset($value->baseFeePerGas)) {
foreach ($value->baseFeePerGas as $key => $baseFeePerGas) {
$value->baseFeePerGas[$key] = BigNumberFormatter::format($baseFeePerGas);
}
}
if (isset($value->reward)) {
foreach ($value->reward as $keyOut => $rewards) {
foreach ($rewards as $keyIn => $reward) {
$value->reward[$keyOut][$keyIn] = BigNumberFormatter::format($reward);
}
}
}
return $value;
}
}
2 changes: 2 additions & 0 deletions src/Methods/Eth/FeeHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Web3\Validators\ArrayNumberValidator;
use Web3\Formatters\QuantityFormatter;
use Web3\Formatters\OptionalQuantityFormatter;
use Web3\Formatters\FeeHistoryFormatter;

class FeeHistory extends EthMethod
{
Expand Down Expand Up @@ -47,6 +48,7 @@ class FeeHistory extends EthMethod
* @var array
*/
protected $outputFormatters = [
FeeHistoryFormatter::class
];

/**
Expand Down
43 changes: 43 additions & 0 deletions test/unit/ArrayNumberValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Test\Unit;

use Test\TestCase;
use Web3\Validators\ArrayNumberValidator;

class ArrayNumberValidatorTest extends TestCase
{
/**
* validator
*
* @var \Web3\Validators\ArrayNumberValidator
*/
protected $validator;

/**
* setUp
*
* @return void
*/
public function setUp(): void
{
parent::setUp();
$this->validator = new ArrayNumberValidator;
}

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

$this->assertEquals(false, $validator->validate(1));
$this->assertEquals(false, $validator->validate(0.1));
$this->assertEquals(false, $validator->validate('test'));
$this->assertEquals(false, $validator->validate([1, 0.1, 'test']));
$this->assertEquals(true, $validator->validate([1, 0.1]));
}
}
8 changes: 7 additions & 1 deletion test/unit/EthApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -727,11 +727,17 @@ public function testFeeHistory()
{
$eth = $this->eth;

$eth->feeHistory(1, 'latest', [ 1 ], function ($err, $feeHistory) {
$eth->feeHistory(1, 'latest', [ 1, 40, 50 ], function ($err, $feeHistory) {
if ($err !== null) {
return $this->fail($err->getMessage());
}
$this->assertTrue($feeHistory->oldestBlock !== null);
$this->assertTrue($feeHistory->baseFeePerGas !== null);
$this->assertTrue($feeHistory->gasUsedRatio !== null);
$this->assertEquals(count($feeHistory->gasUsedRatio), 1);
$this->assertTrue($feeHistory->reward !== null);
$this->assertEquals(count($feeHistory->reward), 1);
$this->assertEquals(count($feeHistory->reward[0]), 3);
});
}

Expand Down

0 comments on commit 3cef7fc

Please sign in to comment.