Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
@web3-onboard/gas
Advanced tools
Estimate the gas prices needed to get a transaction in to the next block for Ethereum Mainnet and Polygon Matic Mainnet.
Supports both Eth Mainnet and Polygon gas pricing.
NPM
npm i @web3-onboard/gas
Yarn
yarn add @web3-onboard/gas
import gas from '@web3-onboard/gas'
// subscribe to a single chain for estimates using the default poll rate of 5 secs
// API key is optional and if provided allows for faster poll rates
const ethMainnetGasBlockPrices = gas.stream({
chains: ['0x1'],
apiKey: '<OPTIONAL_API_KEY>',
endpoint: 'blockPrices'
})
const { unsubscribe: ethGasUnsub } = ethMainnetGasBlockPrices.subscribe(
estimates => console.log(estimates)
)
// .... sometime later, unsubscribe to stop polling
setTimeout(ethGasUnsub, 10000)
// OR you can subscribe to multiple chains at once:
const gasBlockPrices = gas.stream({
chains: ['0x1', '0x89'],
apiKey: '<OPTIONAL_API_KEY>',
endpoint: 'blockPrices',
// can override default poll rate as well
poll: 1000
})
const { unsubscribe } = gasBlockPrices.subscribe(estimates =>
console.log(estimates)
console.log(estimates[0].blockPrices[0].estimatedPrice)
// block inclusion confidence options: 70, 80, 90, 95, 99
console.log(bnGasPrices.find(gas => gas.confidence === 90))
)
// .... sometime later, unsubscribe to stop polling
setTimeout(unsubscribe, 10000)
// Can also just do a one time get rather than a stream
const gasBlockPrices = await gas.get({
chains: ['0x1', '0x89'],
apiKey: '<OPTIONAL_API_KEY>',
endpoint: 'blockPrices'
})
This example assumes you have already setup web3-onboard to connect wallets to your dapp. For more information see web3-onboard docs.
import gas from '@web3-onboard/gas'
import { ethers } from 'ethers'
// Set provider using the Web3-Onboard wallet.provider instance from the connected wallet
let provider = new ethers.providers.Web3Provider(wallet.provider, 'any')
let bnGasPrices
const ethMainnetGasBlockPrices = gas.stream({
chains: ['0x1'], // '0x89' can also be added/replaced here for Polygon gas data
apiKey: '<OPTIONAL_API_KEY>', // for faster refresh rates
endpoint: 'blockPrices'
})
ethMainnetGasBlockPrices.subscribe(estimates => {
console.log(estimates)
bnGasPrices = estimates[0].blockPrices[0].estimatedPrices
})
const gweiToWeiHex = gwei => {
return `0x${(gwei * 1e9).toString(16)}`
}
const sendTransaction = async () => {
if (!toAddress) {
alert('An Ethereum address to send Eth to is required.')
return
}
const signer = provider.getUncheckedSigner()
// define desired confidence for transaction inclusion in block and set in transaction
// block inclusion confidence options: 70, 80, 90, 95, 99
const bnGasForTransaction = bnGasPrices.find(gas => gas.confidence === 90)
const rc = await signer.sendTransaction({
to: toAddress,
value: 1000000000000000
// This will set the transaction gas based on desired confidence
maxPriorityFeePerGas: gweiToWeiHex(
bnGasForTransaction.maxPriorityFeePerGas
),
maxFeePerGas: gweiToWeiHex(bnGasForTransaction.maxFeePerGas)
})
console.log(rc)
}
FAQs
Estimate the gas prices needed to get a transaction in to the next block for Ethereum Mainnet and Polygon Matic Mainnet.
The npm package @web3-onboard/gas receives a total of 243 weekly downloads. As such, @web3-onboard/gas popularity was classified as not popular.
We found that @web3-onboard/gas demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.