-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
allowing to specify percentage-based factors (like 1.125 for 112.5%) #7332
Merged
jdevcs
merged 8 commits into
web3:4.x
from
TemirlanBasitov:Type-of-baseFeePerGasFactor-Should-Support-Decimals
Oct 30, 2024
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
00fd0b1
allowing to specify percentage-based factors (like 1.125 for 112.5%)
cf60ae1
Merge branch '4.x' into Type-of-baseFeePerGasFactor-Should-Support-De…
TemirlanBasitov 2109667
added change log
9af590e
implemented with backward compatibility for any existing users
dda6d7a
Adjusted method description
fe3714c
Merge branch '4.x' into Type-of-baseFeePerGasFactor-Should-Support-De…
TemirlanBasitov 2f2bea0
Merge branch '4.x' into Type-of-baseFeePerGasFactor-Should-Support-De…
TemirlanBasitov 1b92a4f
Merge branch '4.x' into Type-of-baseFeePerGasFactor-Should-Support-De…
TemirlanBasitov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -272,43 +272,57 @@ export class Web3Eth extends Web3Context<Web3EthExecutionAPI, RegisteredSubscrip | |
} | ||
|
||
/** | ||
* Calculates the current Fee Data. | ||
* If the node supports EIP-1559, then `baseFeePerGas` and `maxPriorityFeePerGas` will be returned along with the calculated `maxFeePerGas` value. | ||
* `maxFeePerGas` is calculated as `baseFeePerGas` * `baseFeePerGasFactor` + `maxPriorityFeePerGas`. | ||
* If the node does not support EIP-1559, then the `gasPrice` will be returned and the other values will be undefined. | ||
* | ||
* @param baseFeePerGasFactor (optional) The factor to multiply the `baseFeePerGas` with when calculating `maxFeePerGas`, if the node supports EIP-1559. The default value is 2. | ||
* @param alternativeMaxPriorityFeePerGas (optional) The alternative `maxPriorityFeePerGas` to use when calculating `maxFeePerGas`, if the node supports EIP-1559, but does not support the method `eth_maxPriorityFeePerGas`. The default value is 1 gwei. | ||
* @returns The current fee data. | ||
* | ||
* ```ts | ||
* web3.eth.calculateFeeData().then(console.log); | ||
* > { | ||
* gasPrice: 20000000000n, | ||
* maxFeePerGas: 60000000000n, | ||
* maxPriorityFeePerGas: 20000000000n, | ||
* baseFeePerGas: 20000000000n | ||
* } | ||
* | ||
* web3.eth.calculateFeeData(1n).then(console.log); | ||
* > { | ||
* gasPrice: 20000000000n, | ||
* maxFeePerGas: 40000000000n, | ||
* maxPriorityFeePerGas: 20000000000n, | ||
* baseFeePerGas: 20000000000n | ||
* } | ||
* | ||
* web3.eth.calculateFeeData(3n).then(console.log); | ||
* > { | ||
* gasPrice: 20000000000n, | ||
* maxFeePerGas: 80000000000n, | ||
* maxPriorityFeePerGas: 20000000000n, | ||
* baseFeePerGas: 20000000000n | ||
* } | ||
* ``` | ||
*/ | ||
* Calculates the current Fee Data. | ||
* If the node supports EIP-1559, then `baseFeePerGas` and `maxPriorityFeePerGas` will be returned along with the calculated `maxFeePerGas` value. | ||
* `maxFeePerGas` is calculated as `baseFeePerGas` * `baseFeePerGasFactor` + `maxPriorityFeePerGas`. | ||
* If the node does not support EIP-1559, then the `gasPrice` will be returned and the other values will be undefined. | ||
* | ||
* @param baseFeePerGasFactor (optional) The factor to multiply the `baseFeePerGas` with when calculating `maxFeePerGas`, if the node supports EIP-1559. This can be a `bigint` for precise calculation or a `number` to support decimals. The default value is 2 (BigInt). | ||
* If a `number` is provided, it will be converted to `bigint` with three decimal precision. | ||
* @param alternativeMaxPriorityFeePerGas (optional) The alternative `maxPriorityFeePerGas` to use when calculating `maxFeePerGas`, if the node supports EIP-1559 but does not support the method `eth_maxPriorityFeePerGas`. The default value is 1 gwei. | ||
* @returns The current fee data. | ||
* | ||
* @example | ||
* web3.eth.calculateFeeData().then(console.log); | ||
* > { | ||
* gasPrice: 20000000000n, | ||
* maxFeePerGas: 60000000000n, | ||
* maxPriorityFeePerGas: 20000000000n, | ||
* baseFeePerGas: 20000000000n | ||
* } | ||
* | ||
* @example | ||
* // Using a `bigint` for baseFeePerGasFactor | ||
* web3.eth.calculateFeeData(1n).then(console.log); | ||
* > { | ||
* gasPrice: 20000000000n, | ||
* maxFeePerGas: 40000000000n, | ||
* maxPriorityFeePerGas: 20000000000n, | ||
* baseFeePerGas: 20000000000n | ||
* } | ||
* | ||
* @example | ||
* // Using a `number` for baseFeePerGasFactor (with decimals) | ||
* web3.eth.calculateFeeData(1.5).then(console.log); | ||
* > { | ||
* gasPrice: 20000000000n, | ||
* maxFeePerGas: 50000000000n, // baseFeePerGasFactor is converted to BigInt(1.500) | ||
* maxPriorityFeePerGas: 20000000000n, | ||
* baseFeePerGas: 20000000000n | ||
* } | ||
* | ||
* @example | ||
* web3.eth.calculateFeeData(3n).then(console.log); | ||
* > { | ||
* gasPrice: 20000000000n, | ||
* maxFeePerGas: 80000000000n, | ||
* maxPriorityFeePerGas: 20000000000n, | ||
* baseFeePerGas: 20000000000n | ||
* } | ||
*/ | ||
|
||
public async calculateFeeData( | ||
baseFeePerGasFactor = BigInt(2), | ||
baseFeePerGasFactor: bigint | number = BigInt(2), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @luu-alex web3-eth package's changelog is not update for this PR but root. so In release for this PR, web3-eth will be patch bump if there are no other changes in eth. |
||
alternativeMaxPriorityFeePerGas = ethUnitMap.Gwei, | ||
): Promise<FeeData> { | ||
const block = await this.getBlock<{ number: FMT_NUMBER.BIGINT; bytes: FMT_BYTES.HEX }>( | ||
|
@@ -348,7 +362,15 @@ export class Web3Eth extends Web3Context<Web3EthExecutionAPI, RegisteredSubscrip | |
// and we multiply the `baseFeePerGas` by `baseFeePerGasFactor`, to allow | ||
// trying to include the transaction in the next few blocks even if the | ||
// baseFeePerGas is increasing fast | ||
maxFeePerGas = baseFeePerGas * baseFeePerGasFactor + maxPriorityFeePerGas; | ||
let baseFeeMultiplier: bigint; | ||
if (typeof baseFeePerGasFactor === 'number') { | ||
// Convert number to bigint with three decimal places | ||
baseFeeMultiplier = BigInt(Math.floor(baseFeePerGasFactor * 1000)) / BigInt(1000); | ||
} else { | ||
// It's already a BigInt, so just use it as-is | ||
baseFeeMultiplier = baseFeePerGasFactor; | ||
} | ||
maxFeePerGas = baseFeePerGas * baseFeeMultiplier + maxPriorityFeePerGas; | ||
} | ||
TemirlanBasitov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return { gasPrice, maxFeePerGas, maxPriorityFeePerGas, 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Thanks @TemirlanBasitov I'll suggest to also add few examples and short description in above documentation comments so with this PR merge it will update docs website. https://docs.web3js.org/libdocs/Web3Eth#calculatefeedata
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.
@jdevcs where i can add comment and short description here?
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.
I adjusted description of method with examples in the web3_eth.ts
Here also short explanation:
calculateFeeData method now supports both bigint and number types for the baseFeePerGasFactor parameter. If a number is provided, it will be converted to a bigint with three decimal precision. This allows for more flexible calculations when dealing with decimal values.
Ex:
await calculateFeeData(BigInt(2));
// baseFeePerGasFactor remains as 2n
await calculateFeeData(1.5);
// baseFeePerGasFactor is converted to BigInt(1.500) for precise calculations
These changes make the method more versatile while ensuring backward compatibility with previous bigint usage.