Skip to content

Commit

Permalink
Update fetchFeeHistory
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmire committed Oct 29, 2021
1 parent 883b2fe commit b864f9e
Show file tree
Hide file tree
Showing 2 changed files with 223 additions and 126 deletions.
289 changes: 188 additions & 101 deletions src/gas/fetchFeeHistory.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BN } from 'ethereumjs-util';
import fetchFeeHistory, { EthFeeHistoryResponse } from './fetchFeeHistory';

describe('fetchFeeHistory', () => {
Expand All @@ -16,120 +17,206 @@ describe('fetchFeeHistory', () => {
};
}

it('should return a representation of fee history from the Ethereum network, organized by block rather than type of data', async () => {
// To reproduce:
//
// curl -X POST --data '{
// "id": 1,
// "jsonrpc": "2.0",
// "method": "eth_feeHistory",
// "params": ["0x5", "latest", [10, 20, 30]]
// }' https://mainnet.infura.io/v3/<PROJECT_ID>
const ethQuery = buildEthQuery({
oldestBlock: '0xcb1939',
// Note that this array contains 6 items when we requested 5. Per
// <https://github.com/ethereum/go-ethereum/blob/57a3fab8a75eeb9c2f4fab770b73b51b9fe672c5/eth/gasprice/feehistory.go#L191-L192>,
// baseFeePerGas will always include an extra item which is the calculated base fee for the
// next (future) block.
baseFeePerGas: [
'0x16eb46a3bb',
'0x14cd6f0628',
'0x1763700ef2',
'0x1477020d14',
'0x129c9eb46b',
'0x134002f480',
],
gasUsedRatio: [
0.13060046666666666,
0.9972395333333334,
0,
0.13780313333333333,
0.6371707333333333,
],
reward: [
['0x59682f00', '0x59682f00', '0x59682f00'],
['0x540ae480', '0x59682f00', '0x59682f00'],
['0x0', '0x0', '0x0'],
['0x3b9aca00', '0x3b9aca00', '0x3b9aca00'],
['0x59682f00', '0x59682f00', '0x59682f00'],
],
});
describe('assuming that percentiles are not given', () => {
it('should return a representation of fee history from the Ethereum network, organized by block rather than type of data', async () => {
// To reproduce:
//
// curl -X POST --data '{
// "id": 1,
// "jsonrpc": "2.0",
// "method": "eth_feeHistory",
// "params": ["0x5", "latest", [10, 20, 30]]
// }' https://mainnet.infura.io/v3/<PROJECT_ID>
const ethQuery = buildEthQuery({
oldestBlock: '0xcb1939',
// Note that this array contains 6 items when we requested 5. Per
// <https://github.com/ethereum/go-ethereum/blob/57a3fab8a75eeb9c2f4fab770b73b51b9fe672c5/eth/gasprice/feehistory.go#L191-L192>,
// baseFeePerGas will always include an extra item which is the calculated base fee for the
// next (future) block.
baseFeePerGas: [
'0x16eb46a3bb',
'0x14cd6f0628',
'0x1763700ef2',
'0x1477020d14',
'0x129c9eb46b',
'0x134002f480',
],
gasUsedRatio: [
0.13060046666666666,
0.9972395333333334,
0,
0.13780313333333333,
0.6371707333333333,
],
});

const feeHistory = await fetchFeeHistory({
ethQuery,
numberOfBlocks: 5,
percentiles: [10, 20, 30],
});
const feeHistory = await fetchFeeHistory({
ethQuery,
numberOfBlocks: 5,
});

expect(feeHistory).toStrictEqual({
startBlockId: '0xcb1939',
blocks: [
{
baseFeePerGas: 98436555707,
gasUsedRatio: 0.13060046666666666,
priorityFeesByPercentile: {
10: 1500000000,
20: 1500000000,
30: 1500000000,
expect(feeHistory).toStrictEqual({
startBlockId: new BN('13310265'),
blocks: [
{
baseFeePerGas: new BN('98436555707'),
gasUsedRatio: 0.13060046666666666,
},
},
{
baseFeePerGas: 89345951272,
gasUsedRatio: 0.9972395333333334,
priorityFeesByPercentile: {
10: 1410000000,
20: 1500000000,
30: 1500000000,
{
baseFeePerGas: new BN('89345951272'),
gasUsedRatio: 0.9972395333333334,
},
},
{
baseFeePerGas: 100452536050,
gasUsedRatio: 0,
priorityFeesByPercentile: {
10: 0,
20: 0,
30: 0,
{
baseFeePerGas: new BN('100452536050'),
gasUsedRatio: 0,
},
},
{
baseFeePerGas: 87895969044,
gasUsedRatio: 0.13780313333333333,
priorityFeesByPercentile: {
10: 1000000000,
20: 1000000000,
30: 1000000000,
{
baseFeePerGas: new BN('87895969044'),
gasUsedRatio: 0.13780313333333333,
},
},
{
baseFeePerGas: 79937057899,
gasUsedRatio: 0.6371707333333333,
priorityFeesByPercentile: {
10: 1500000000,
20: 1500000000,
30: 1500000000,
{
baseFeePerGas: new BN('79937057899'),
gasUsedRatio: 0.6371707333333333,
},
},
],
],
});
});
});

it('should handle an "empty" response from eth_feeHistory', async () => {
const ethQuery = buildEthQuery({
oldestBlock: '0x0',
baseFeePerGas: [],
gasUsedRatio: [],
reward: [],
it('should handle an "empty" response from eth_feeHistory', async () => {
const ethQuery = buildEthQuery({
oldestBlock: '0x0',
baseFeePerGas: [],
gasUsedRatio: [],
});

const feeHistory = await fetchFeeHistory({
ethQuery,
numberOfBlocks: 5,
});

expect(feeHistory).toStrictEqual({
startBlockId: new BN('0'),
blocks: [],
});
});
});

describe('when percentiles are given', () => {
it('should match each item in the "reward" key from the response to its percentile', async () => {
// To reproduce:
//
// curl -X POST --data '{
// "id": 1,
// "jsonrpc": "2.0",
// "method": "eth_feeHistory",
// "params": ["0x5", "latest", [10, 20, 30]]
// }' https://mainnet.infura.io/v3/<PROJECT_ID>
const ethQuery = buildEthQuery({
oldestBlock: '0xcb1939',
// Note that this array contains 6 items when we requested 5. Per
// <https://github.com/ethereum/go-ethereum/blob/57a3fab8a75eeb9c2f4fab770b73b51b9fe672c5/eth/gasprice/feehistory.go#L191-L192>,
// baseFeePerGas will always include an extra item which is the calculated base fee for the
// next (future) block.
baseFeePerGas: [
'0x16eb46a3bb',
'0x14cd6f0628',
'0x1763700ef2',
'0x1477020d14',
'0x129c9eb46b',
'0x134002f480',
],
gasUsedRatio: [
0.13060046666666666,
0.9972395333333334,
0,
0.13780313333333333,
0.6371707333333333,
],
reward: [
['0x59682f00', '0x59682f00', '0x59682f00'],
['0x540ae480', '0x59682f00', '0x59682f00'],
['0x0', '0x0', '0x0'],
['0x3b9aca00', '0x3b9aca00', '0x3b9aca00'],
['0x59682f00', '0x59682f00', '0x59682f00'],
],
});

const feeHistory = await fetchFeeHistory({
ethQuery,
numberOfBlocks: 5,
percentiles: [10, 20, 30],
});

const feeHistory = await fetchFeeHistory({
ethQuery,
numberOfBlocks: 5,
percentiles: [10, 20, 30],
expect(feeHistory).toStrictEqual({
startBlockId: new BN('13310265'),
blocks: [
{
baseFeePerGas: new BN('98436555707'),
gasUsedRatio: 0.13060046666666666,
priorityFeesByPercentile: {
10: new BN('1500000000'),
20: new BN('1500000000'),
30: new BN('1500000000'),
},
},
{
baseFeePerGas: new BN('89345951272'),
gasUsedRatio: 0.9972395333333334,
priorityFeesByPercentile: {
10: new BN('1410000000'),
20: new BN('1500000000'),
30: new BN('1500000000'),
},
},
{
baseFeePerGas: new BN('100452536050'),
gasUsedRatio: 0,
priorityFeesByPercentile: {
10: new BN('0'),
20: new BN('0'),
30: new BN('0'),
},
},
{
baseFeePerGas: new BN('87895969044'),
gasUsedRatio: 0.13780313333333333,
priorityFeesByPercentile: {
10: new BN('1000000000'),
20: new BN('1000000000'),
30: new BN('1000000000'),
},
},
{
baseFeePerGas: new BN('79937057899'),
gasUsedRatio: 0.6371707333333333,
priorityFeesByPercentile: {
10: new BN('1500000000'),
20: new BN('1500000000'),
30: new BN('1500000000'),
},
},
],
});
});

expect(feeHistory).toStrictEqual({
startBlockId: '0x0',
blocks: [],
it('should handle an "empty" response from eth_feeHistory', async () => {
const ethQuery = buildEthQuery({
oldestBlock: '0x0',
baseFeePerGas: [],
gasUsedRatio: [],
reward: [],
});

const feeHistory = await fetchFeeHistory({
ethQuery,
numberOfBlocks: 5,
percentiles: [10, 20, 30],
});

expect(feeHistory).toStrictEqual({
startBlockId: new BN('0'),
blocks: [],
});
});
});
});
Loading

0 comments on commit b864f9e

Please sign in to comment.