Skip to content

Commit 0ed591b

Browse files
authored
eth_feeHistory wrongly returns error (#832)
* Edit newest block logic Signed-off-by: nikolay <[email protected]> * Add tests Signed-off-by: nikolay <[email protected]> Signed-off-by: nikolay <[email protected]>
1 parent 1bc0157 commit 0ed591b

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

packages/relay/src/lib/eth.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,9 @@ export class EthImpl implements Eth {
172172

173173
try {
174174
const latestBlockNumber = await this.translateBlockTag(EthImpl.blockLatest, requestId);
175-
const newestBlockNumber = await this.translateBlockTag(newestBlock, requestId);
175+
const newestBlockNumber = (newestBlock == EthImpl.blockLatest || newestBlock == EthImpl.blockPending)
176+
? latestBlockNumber
177+
: await this.translateBlockTag(newestBlock, requestId);
176178

177179
if (newestBlockNumber > latestBlockNumber) {
178180
return predefined.REQUEST_BEYOND_HEAD_BLOCK(newestBlockNumber, latestBlockNumber);

packages/relay/tests/lib/eth.spec.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2048,6 +2048,88 @@ describe('Eth calls using MirrorNode', async function () {
20482048
expect(rewards[1]).to.equal('0x0');
20492049
});
20502050

2051+
it('eth_feeHistory with latest param', async function () {
2052+
const previousBlock = {...defaultBlock, number: blockNumber2, timestamp: {
2053+
from: '1651560386.060890948',
2054+
to: '1651560389.060890948'
2055+
}};
2056+
const latestBlock = {...defaultBlock, number: blockNumber3};
2057+
const previousFees = JSON.parse(JSON.stringify(defaultNetworkFees));
2058+
const latestFees = JSON.parse(JSON.stringify(defaultNetworkFees));
2059+
2060+
mock.onGet('blocks?limit=1&order=desc').reply(200, {blocks: [latestBlock]});
2061+
mock.onGet(`blocks/${previousBlock.number}`).reply(200, previousBlock);
2062+
mock.onGet(`blocks/${latestBlock.number}`).reply(200, latestBlock);
2063+
mock.onGet(`network/fees?timestamp=lte:${previousBlock.timestamp.to}`).reply(200, previousFees);
2064+
mock.onGet(`network/fees?timestamp=lte:${latestBlock.timestamp.to}`).reply(200, latestFees);
2065+
2066+
const feeHistory = await ethImpl.feeHistory(1, 'latest', [25, 75]);
2067+
expect(feeHistory).to.exist;
2068+
expect(feeHistory['oldestBlock']).to.eq('0x' + blockNumber3);
2069+
});
2070+
2071+
it('eth_feeHistory with pending param', async function () {
2072+
const previousBlock = {...defaultBlock, number: blockNumber2, timestamp: {
2073+
from: '1651560386.060890948',
2074+
to: '1651560389.060890948'
2075+
}};
2076+
const latestBlock = {...defaultBlock, number: blockNumber3};
2077+
const previousFees = JSON.parse(JSON.stringify(defaultNetworkFees));
2078+
const latestFees = JSON.parse(JSON.stringify(defaultNetworkFees));
2079+
2080+
mock.onGet('blocks?limit=1&order=desc').reply(200, {blocks: [latestBlock]});
2081+
mock.onGet(`blocks/${previousBlock.number}`).reply(200, previousBlock);
2082+
mock.onGet(`blocks/${latestBlock.number}`).reply(200, latestBlock);
2083+
mock.onGet(`network/fees?timestamp=lte:${previousBlock.timestamp.to}`).reply(200, previousFees);
2084+
mock.onGet(`network/fees?timestamp=lte:${latestBlock.timestamp.to}`).reply(200, latestFees);
2085+
2086+
const feeHistory = await ethImpl.feeHistory(1, 'pending', [25, 75]);
2087+
expect(feeHistory).to.exist;
2088+
expect(feeHistory['oldestBlock']).to.eq('0x' + blockNumber3);
2089+
});
2090+
2091+
it('eth_feeHistory with earliest param', async function () {
2092+
const firstBlockIndex = 0;
2093+
const secondBlockIndex = 1;
2094+
const previousBlock = {...defaultBlock, number: firstBlockIndex, timestamp: {
2095+
from: '1651560386.060890948',
2096+
to: '1651560389.060890948'
2097+
}};
2098+
const latestBlock = {...defaultBlock, number: secondBlockIndex};
2099+
const previousFees = JSON.parse(JSON.stringify(defaultNetworkFees));
2100+
const latestFees = JSON.parse(JSON.stringify(defaultNetworkFees));
2101+
2102+
mock.onGet('blocks?limit=1&order=desc').reply(200, {blocks: [latestBlock]});
2103+
mock.onGet(`blocks/${previousBlock.number}`).reply(200, previousBlock);
2104+
mock.onGet(`blocks/${latestBlock.number}`).reply(200, latestBlock);
2105+
mock.onGet(`network/fees?timestamp=lte:${previousBlock.timestamp.to}`).reply(200, previousFees);
2106+
mock.onGet(`network/fees?timestamp=lte:${latestBlock.timestamp.to}`).reply(200, latestFees);
2107+
2108+
const feeHistory = await ethImpl.feeHistory(1, 'earliest', [25, 75]);
2109+
expect(feeHistory).to.exist;
2110+
expect(feeHistory['oldestBlock']).to.eq('0x' + firstBlockIndex);
2111+
});
2112+
2113+
it('eth_feeHistory with number param', async function () {
2114+
const previousBlock = {...defaultBlock, number: blockNumber2, timestamp: {
2115+
from: '1651560386.060890948',
2116+
to: '1651560389.060890948'
2117+
}};
2118+
const latestBlock = {...defaultBlock, number: blockNumber3};
2119+
const previousFees = JSON.parse(JSON.stringify(defaultNetworkFees));
2120+
const latestFees = JSON.parse(JSON.stringify(defaultNetworkFees));
2121+
2122+
mock.onGet('blocks?limit=1&order=desc').reply(200, {blocks: [latestBlock]});
2123+
mock.onGet(`blocks/${previousBlock.number}`).reply(200, previousBlock);
2124+
mock.onGet(`blocks/${latestBlock.number}`).reply(200, latestBlock);
2125+
mock.onGet(`network/fees?timestamp=lte:${previousBlock.timestamp.to}`).reply(200, previousFees);
2126+
mock.onGet(`network/fees?timestamp=lte:${latestBlock.timestamp.to}`).reply(200, latestFees);
2127+
2128+
const feeHistory = await ethImpl.feeHistory(1, '0x'+blockNumber3, [25, 75]);
2129+
expect(feeHistory).to.exist;
2130+
expect(feeHistory['oldestBlock']).to.eq('0x' + blockNumber3);
2131+
});
2132+
20512133
it('eth_feeHistory with max results', async function () {
20522134
const maxResultsCap = Number(constants.DEFAULT_FEE_HISTORY_MAX_RESULTS);
20532135

0 commit comments

Comments
 (0)