From 3cef7fce09ac0a0f0a6be99af2e26192b89a3982 Mon Sep 17 00:00:00 2001 From: sc0vu Date: Mon, 4 Sep 2023 15:56:47 +0800 Subject: [PATCH] Format the response from eth_feeHistory --- src/Formatters/FeeHistoryFormatter.php | 46 ++++++++++++++++++++++++++ src/Methods/Eth/FeeHistory.php | 2 ++ test/unit/ArrayNumberValidatorTest.php | 43 ++++++++++++++++++++++++ test/unit/EthApiTest.php | 8 ++++- 4 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 src/Formatters/FeeHistoryFormatter.php create mode 100644 test/unit/ArrayNumberValidatorTest.php diff --git a/src/Formatters/FeeHistoryFormatter.php b/src/Formatters/FeeHistoryFormatter.php new file mode 100644 index 0000000..cb388d2 --- /dev/null +++ b/src/Formatters/FeeHistoryFormatter.php @@ -0,0 +1,46 @@ + + * + * @author Peter Lai + * @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; + } +} \ No newline at end of file diff --git a/src/Methods/Eth/FeeHistory.php b/src/Methods/Eth/FeeHistory.php index 75819ab..42763bc 100644 --- a/src/Methods/Eth/FeeHistory.php +++ b/src/Methods/Eth/FeeHistory.php @@ -18,6 +18,7 @@ use Web3\Validators\ArrayNumberValidator; use Web3\Formatters\QuantityFormatter; use Web3\Formatters\OptionalQuantityFormatter; +use Web3\Formatters\FeeHistoryFormatter; class FeeHistory extends EthMethod { @@ -47,6 +48,7 @@ class FeeHistory extends EthMethod * @var array */ protected $outputFormatters = [ + FeeHistoryFormatter::class ]; /** diff --git a/test/unit/ArrayNumberValidatorTest.php b/test/unit/ArrayNumberValidatorTest.php new file mode 100644 index 0000000..c7f6851 --- /dev/null +++ b/test/unit/ArrayNumberValidatorTest.php @@ -0,0 +1,43 @@ +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])); + } +} \ No newline at end of file diff --git a/test/unit/EthApiTest.php b/test/unit/EthApiTest.php index ce7dd76..f764580 100644 --- a/test/unit/EthApiTest.php +++ b/test/unit/EthApiTest.php @@ -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); }); }