Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 436e77a

Browse files
spacesailor24Alexalex
authored
eth_feeHistory (EIP 1559) (#4191)
* WIP * Add missing fields for test runner * Correct function arguments for getFeeHistory * getFeeHistory tests with correct arguments * Init utils.toNumber function * Rename toNumber test to hexToNumber * Add inputFormatters to getFeeHistory * Rename newestBlock to lastBlock. Update types for blockCount and lastBlock * Add additional tests with different input types * Add missing function export * eth-feehistory docs (#4190) * updating docs * updating example * updating types and adding example of list * Update docs/web3-eth.rst * Update docs/web3-eth.rst * Update docs/web3-eth.rst Co-authored-by: alex <[email protected]> Co-authored-by: Wyatt Barnes <[email protected]> Co-authored-by: Alex <[email protected]> Co-authored-by: alex <[email protected]>
1 parent 7d03412 commit 436e77a

File tree

13 files changed

+333
-19
lines changed

13 files changed

+333
-19
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ Released with 1.0.0-beta.37 code base.
407407
### Added
408408

409409
- London transaction support (#4155)
410+
- RPC support `eth_feehistory` call
410411

411412
### Changed
412413
- Grammar fix (#4088) and updated Swarm (#4151)and Whisper doc links (#4170)

docs/web3-eth.rst

+37
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,43 @@ Example
716716
717717
------------------------------------------------------------------------------
718718

719+
.. _eth-feehistory:
720+
721+
722+
getFeeHistory
723+
=====================
724+
725+
.. code-block:: javascript
726+
727+
web3.eth.getFeeHistory(blockCount, newestBlock, rewardPercentiles, [callback])
728+
729+
Transaction fee history
730+
Returns base fee per gas and transaction effective priority fee per gas history for the requested block range if available.
731+
The range between headBlock-4 and headBlock is guaranteed to be available while retrieving data from the pending block and older
732+
history are optional to support. For pre-EIP-1559 blocks the gas prices are returned as rewards and zeroes are returned for the base fee per gas.
733+
734+
----------
735+
Parameters
736+
----------
737+
738+
1. ``String|Number|BN|BigNumber`` - Number of blocks in the requested range. Between 1 and 1024 blocks can be requested in a single query. Less than requested may be returned if not all blocks are available.
739+
2. ``String|Number|BN|BigNumber`` - Highest number block of the requested range.
740+
3. ``Array of numbers`` - A monotonically increasing list of percentile values to sample from each block's effective priority fees per gas in ascending order, weighted by gas used. Example: `["0", "25", "50", "75", "100"]` or `["0", "0.5", "1", "1.5", "3", "80"]`
741+
4. ``Function`` - (optional) Optional callback, returns an error object as first parameter and the result as second.
742+
743+
-------
744+
Returns
745+
-------
746+
747+
``Promise`` returns ``Object`` - Fee history for the returned block range. The object:
748+
749+
- ``Number`` oldestBlock - Lowest number block of the returned range.
750+
- ``Array of strings`` baseFeePerGas - An array of block base fees per gas. This includes the next block after the newest of the returned range, because this value can be derived from the newest block. Zeroes are returned for pre-EIP-1559 blocks.
751+
- ``Array of numbers`` gasUsedRatio - An array of block gas used ratios. These are calculated as the ratio of gasUsed and gasLimit.
752+
- ``Array of string arrays`` reward - An array of effective priority fee per gas data points from a single block. All zeroes are returned if the block is empty.
753+
754+
------------------------------------------------------------------------------
755+
719756

720757
getAccounts
721758
=====================

packages/web3-eth/src/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,12 @@ var Eth = function Eth() {
385385
params: 0,
386386
outputFormatter: formatter.outputBigNumberFormatter
387387
}),
388+
new Method({
389+
name: 'getFeeHistory',
390+
call: 'eth_feeHistory',
391+
params: 3,
392+
inputFormatter: [utils.toNumber, utils.toHex, function(value) {return value}]
393+
}),
388394
new Method({
389395
name: 'getAccounts',
390396
call: 'eth_accounts',

packages/web3-eth/types/index.d.ts

+14
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,13 @@ export class Eth {
149149
callback?: (error: Error, gasPrice: string) => void
150150
): Promise<string>;
151151

152+
getFeeHistory(
153+
blockCount: number | BigNumber | BN | string,
154+
lastBlock: number | BigNumber | BN | string,
155+
rewardPercentiles: number[],
156+
callback?: (error: Error, feeHistory: FeeHistoryResult) => void
157+
): Promise<FeeHistoryResult>;
158+
152159
getAccounts(
153160
callback?: (error: Error, accounts: string[]) => void
154161
): Promise<string[]>;
@@ -439,3 +446,10 @@ export interface StorageProof {
439446
value: string;
440447
proof: string[];
441448
}
449+
450+
export interface FeeHistoryResult {
451+
baseFeePerGas: string[];
452+
gasUsedRatio: number[];
453+
oldestBlock: number;
454+
reward: string[][];
455+
}

packages/web3-eth/types/tests/eth.tests.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ import {
3232
TransactionConfig,
3333
hardfork,
3434
Common,
35-
chain
35+
chain,
36+
FeeHistoryResult
3637
} from 'web3-eth';
3738
import BN = require('bn.js');
3839
import BigNumber from 'bignumber.js';
@@ -178,6 +179,15 @@ eth.getGasPrice();
178179
// $ExpectType Promise<string>
179180
eth.getGasPrice((error: Error, gasPrice: string) => {});
180181

182+
// $ExpectType Promise<FeeHistoryResult>
183+
eth.getFeeHistory(
184+
4, "0xA30953", []
185+
);
186+
// $ExpectType Promise<FeeHistoryResult>
187+
eth.getFeeHistory(
188+
4, "0xA30953", [],
189+
(error: Error, feeHistory: FeeHistoryResult) => {});
190+
181191
// $ExpectType Promise<string[]>
182192
eth.getAccounts();
183193
// $ExpectType Promise<string[]>

packages/web3-utils/src/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -436,5 +436,7 @@ module.exports = {
436436
isTopicInBloom: utils.isTopicInBloom,
437437
isInBloom: utils.isInBloom,
438438

439-
compareBlockNumbers: compareBlockNumbers
439+
compareBlockNumbers: compareBlockNumbers,
440+
441+
toNumber: utils.toNumber
440442
};

packages/web3-utils/src/utils.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,17 @@ var sha3Raw = function(value) {
521521
return value;
522522
};
523523

524+
/**
525+
* Auto converts any given value into it's hex representation,
526+
* then converts hex to number.
527+
*
528+
* @method toNumber
529+
* @param {String|Number|BN} value
530+
* @return {Number}
531+
*/
532+
var toNumber = function(value) {
533+
return typeof value === 'number' ? value : hexToNumber(toHex(value));
534+
}
524535

525536
module.exports = {
526537
BN: BN,
@@ -550,5 +561,6 @@ module.exports = {
550561
rightPad: rightPad,
551562
toTwosComplement: toTwosComplement,
552563
sha3: sha3,
553-
sha3Raw: sha3Raw
564+
sha3Raw: sha3Raw,
565+
toNumber: toNumber
554566
};

packages/web3-utils/types/index.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ export function testAddress(bloom: string, address: string): boolean;
122122
export function testTopic(bloom: string, topic: string): boolean;
123123
export function getSignatureParameters(signature: string): {r: string; s: string; v: number};
124124
export function stripHexPrefix(str: string): string;
125+
export function toNumber(value: number | string | BN): number;
125126

126127
// interfaces
127128
export interface Utils {
@@ -178,6 +179,7 @@ export interface Utils {
178179
testTopic(bloom: string, topic: string): boolean;
179180
getSignatureParameters(signature: string): {r: string; s: string; v: number};
180181
stripHexPrefix(str: string): string;
182+
toNumber(value: number | string | BN): number;
181183
}
182184

183185
export interface Units {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
This file is part of web3.js.
3+
4+
web3.js is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU Lesser General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
web3.js is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public License
15+
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
/**
18+
* @file to-number-test.ts
19+
* @author Josh Stevens <[email protected]>
20+
* @date 2018
21+
*/
22+
23+
import BN = require('bn.js');
24+
import {toNumber} from 'web3-utils';
25+
26+
// $ExpectType number
27+
toNumber('234');
28+
// $ExpectType number
29+
toNumber(234);
30+
// $ExpectType number
31+
toNumber(new BN(3));
32+
33+
// $ExpectError
34+
toNumber(['string']);
35+
// $ExpectError
36+
toNumber(true);
37+
// $ExpectError
38+
toNumber([4]);
39+
// $ExpectError
40+
toNumber({});
41+
// $ExpectError
42+
toNumber(null);
43+
// $ExpectError
44+
toNumber(undefined);

test/eth.feeHistory.js

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
var BigNumber = require('bignumber.js');
2+
3+
var testMethod = require('./helpers/test.method.js');
4+
5+
var method = 'getFeeHistory';
6+
var methodCall = 'eth_feeHistory';
7+
8+
var tests = [
9+
{
10+
args: [4, "0xA30953", []],
11+
formattedArgs: [4, "0xA30953", []],
12+
result: {
13+
"baseFeePerGas": [
14+
"0xa",
15+
"0x9",
16+
"0x8",
17+
"0x9",
18+
"0x9"
19+
],
20+
"gasUsedRatio": [
21+
0.003920375,
22+
0.002625,
23+
0.904999125,
24+
0.348347625
25+
],
26+
"oldestBlock": 10684752
27+
},
28+
formattedResult: {
29+
"baseFeePerGas": [
30+
"0xa",
31+
"0x9",
32+
"0x8",
33+
"0x9",
34+
"0x9"
35+
],
36+
"gasUsedRatio": [
37+
0.003920375,
38+
0.002625,
39+
0.904999125,
40+
0.348347625
41+
],
42+
"oldestBlock": 10684752
43+
},
44+
call: methodCall
45+
},
46+
{
47+
args: ['0x4', 10684755, []],
48+
formattedArgs: [4, "0xa30953", []],
49+
result: {
50+
"baseFeePerGas": [
51+
"0xa",
52+
"0x9",
53+
"0x8",
54+
"0x9",
55+
"0x9"
56+
],
57+
"gasUsedRatio": [
58+
0.003920375,
59+
0.002625,
60+
0.904999125,
61+
0.348347625
62+
],
63+
"oldestBlock": 10684752
64+
},
65+
formattedResult: {
66+
"baseFeePerGas": [
67+
"0xa",
68+
"0x9",
69+
"0x8",
70+
"0x9",
71+
"0x9"
72+
],
73+
"gasUsedRatio": [
74+
0.003920375,
75+
0.002625,
76+
0.904999125,
77+
0.348347625
78+
],
79+
"oldestBlock": 10684752
80+
},
81+
call: methodCall
82+
},
83+
{
84+
args: [new BigNumber(4), '10684755', []],
85+
formattedArgs: [4, "0xa30953", []],
86+
result: {
87+
"baseFeePerGas": [
88+
"0xa",
89+
"0x9",
90+
"0x8",
91+
"0x9",
92+
"0x9"
93+
],
94+
"gasUsedRatio": [
95+
0.003920375,
96+
0.002625,
97+
0.904999125,
98+
0.348347625
99+
],
100+
"oldestBlock": 10684752
101+
},
102+
formattedResult: {
103+
"baseFeePerGas": [
104+
"0xa",
105+
"0x9",
106+
"0x8",
107+
"0x9",
108+
"0x9"
109+
],
110+
"gasUsedRatio": [
111+
0.003920375,
112+
0.002625,
113+
0.904999125,
114+
0.348347625
115+
],
116+
"oldestBlock": 10684752
117+
},
118+
call: methodCall
119+
}
120+
];
121+
122+
123+
testMethod.runTests('eth', method, tests);
124+

test/eth_methods.js

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ describe('eth', function() {
2727
u.methodExists(eth, 'isMining');
2828
u.methodExists(eth, 'getCoinbase');
2929
u.methodExists(eth, 'getGasPrice');
30+
u.methodExists(eth, 'getFeeHistory');
3031
u.methodExists(eth, 'getHashrate');
3132
u.methodExists(eth, 'getAccounts');
3233
u.methodExists(eth, 'getBlockNumber');

test/utils.hexToNumber.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var assert = require('assert');
2+
var utils = require('../packages/web3-utils');
3+
4+
describe('lib/utils/utils', function () {
5+
describe('hexToNumber', function () {
6+
it('should return the correct value', function () {
7+
8+
assert.equal(utils.hexToNumber("0x3e8"), 1000);
9+
assert.equal(utils.hexToNumber('0x1f0fe294a36'), 2134567897654);
10+
// allow compatiblity
11+
assert.equal(utils.hexToNumber(100000), 100000);
12+
});
13+
14+
it('should validate hex strings', function() {
15+
try {
16+
utils.hexToNumber('100000');
17+
assert.fail();
18+
} catch (error){
19+
assert(error.message.includes('is not a valid hex string'))
20+
}
21+
})
22+
});
23+
});

0 commit comments

Comments
 (0)