Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement eth.calculateFeeData (#6795)
* implement and test `calculateFeeData` * add integration test for `calculateFeeData` * update CHANGELOG.md file
- Loading branch information
1 parent
f696e47
commit ec65468
Showing
5 changed files
with
263 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
packages/web3-eth/test/unit/web3_eth_calculate_fee_data.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
This file is part of web3.js. | ||
web3.js is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
web3.js is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public License | ||
along with web3.js. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { ethRpcMethods } from 'web3-rpc-methods'; | ||
|
||
import Web3Eth from '../../src/index'; | ||
|
||
jest.mock('web3-rpc-methods'); | ||
|
||
describe('Web3Eth.calculateFeeData', () => { | ||
let web3Eth: Web3Eth; | ||
|
||
beforeAll(() => { | ||
web3Eth = new Web3Eth('http://127.0.0.1:8545'); | ||
}); | ||
|
||
it('should return call getBlockByNumber, getGasPrice and getMaxPriorityFeePerGas', async () => { | ||
await web3Eth.calculateFeeData(); | ||
// web3Eth.getBlock = jest.fn(); | ||
expect(ethRpcMethods.getBlockByNumber).toHaveBeenCalledWith( | ||
web3Eth.requestManager, | ||
'latest', | ||
false, | ||
); | ||
expect(ethRpcMethods.getGasPrice).toHaveBeenCalledWith(web3Eth.requestManager); | ||
expect(ethRpcMethods.getMaxPriorityFeePerGas).toHaveBeenCalledWith(web3Eth.requestManager); | ||
}); | ||
|
||
it('should calculate fee data', async () => { | ||
const gasPrice = BigInt(20 * 1000); | ||
const baseFeePerGas = BigInt(1000); | ||
const maxPriorityFeePerGas = BigInt(100); | ||
const baseFeePerGasFactor = BigInt(3); | ||
|
||
jest.spyOn(ethRpcMethods, 'getBlockByNumber').mockReturnValueOnce({ baseFeePerGas } as any); | ||
jest.spyOn(ethRpcMethods, 'getGasPrice').mockReturnValueOnce(gasPrice as any); | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call | ||
jest | ||
.spyOn(ethRpcMethods, 'getMaxPriorityFeePerGas') | ||
.mockReturnValueOnce(maxPriorityFeePerGas as any); | ||
|
||
const feeData = await web3Eth.calculateFeeData(baseFeePerGasFactor, maxPriorityFeePerGas); | ||
expect(feeData).toMatchObject({ | ||
gasPrice, | ||
maxFeePerGas: baseFeePerGas * baseFeePerGasFactor + maxPriorityFeePerGas, | ||
maxPriorityFeePerGas, | ||
baseFeePerGas, | ||
}); | ||
}); | ||
|
||
it('should calculate fee data based on `alternativeMaxPriorityFeePerGas` if `getMaxPriorityFeePerGas` did not return a value', async () => { | ||
const gasPrice = BigInt(20 * 1000); | ||
const baseFeePerGas = BigInt(1000); | ||
const alternativeMaxPriorityFeePerGas = BigInt(700); | ||
const baseFeePerGasFactor = BigInt(3); | ||
|
||
jest.spyOn(ethRpcMethods, 'getBlockByNumber').mockReturnValueOnce({ baseFeePerGas } as any); | ||
jest.spyOn(ethRpcMethods, 'getGasPrice').mockReturnValueOnce(gasPrice as any); | ||
const feeData = await web3Eth.calculateFeeData( | ||
baseFeePerGasFactor, | ||
alternativeMaxPriorityFeePerGas, | ||
); | ||
expect(feeData).toMatchObject({ | ||
gasPrice, | ||
maxFeePerGas: baseFeePerGas * baseFeePerGasFactor + alternativeMaxPriorityFeePerGas, | ||
maxPriorityFeePerGas: alternativeMaxPriorityFeePerGas, | ||
baseFeePerGas, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ec65468
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Benchmark
processingTx
9390
ops/sec (±4.95%
)9301
ops/sec (±4.81%
)0.99
processingContractDeploy
38448
ops/sec (±7.17%
)39129
ops/sec (±7.62%
)1.02
processingContractMethodSend
18987
ops/sec (±7.36%
)19443
ops/sec (±5.19%
)1.02
processingContractMethodCall
38741
ops/sec (±5.85%
)38971
ops/sec (±6.34%
)1.01
abiEncode
43859
ops/sec (±6.79%
)44252
ops/sec (±6.92%
)1.01
abiDecode
29305
ops/sec (±7.57%
)30419
ops/sec (±8.89%
)1.04
sign
1611
ops/sec (±1.06%
)1656
ops/sec (±4.08%
)1.03
verify
370
ops/sec (±0.68%
)373
ops/sec (±0.78%
)1.01
This comment was automatically generated by workflow using github-action-benchmark.