A library that has a collection of onchain and offchain gas price oracle URLs
Current offchain list:
- https://ethgasstation.info/json/ethgasAPI.json
- https://etherchain.org/api/gasnow
- https://blockscout.com/eth/mainnet/api/v1/gas-price-oracle
Current onchain list:
Current offchain list:
Current offchain list:
Current offchain list:
npm i gas-price-oracle
const { GasPriceOracle } = require('gas-price-oracle');
const options = {
chainId: 1,
defaultRpc: 'https://api.mycryptoapi.com/eth',
timeout: 10000,
defaultFallbackGasPrices: {
instant: 28,
fast: 22,
standard: 17,
low: 11,
},
};
const oracle = new GasPriceOracle(options);
// optional fallbackGasPrices
const fallbackGasPrices = {
instant: 70,
fast: 31,
standard: 20,
low: 7,
};
oracle.gasPrices(fallbackGasPrices).then(gasPrices => {
console.log(gasPrices); // { instant: 50, fast: 21, standard: 10, low: 3 }
});
The gasPrices
method also accepts median
argument (true
) by default. For more details see below.
Under the hood it's a combination of fetchMedianGasPriceOffChain
(fetchGasPricesOffChain
) and fetchGasPricesOnChain
methods.
const oracle = new GasPriceOracle();
oracle.fetchGasPricesOffChain().then(gasPrices => {
console.log(gasPrices); // { instant: 50, fast: 21, standard: 10, low: 3 }
});
const oracle = new GasPriceOracle();
oracle.fetchMedianGasPriceOffChain().then(gasPrices => {
console.log(gasPrices); // { instant: 50, fast: 21, standard: 10, low: 3 }
});
it returns the median gas price of all the oracles configured.
const defaultRpc = 'https://mainnet.infura.io/v3/<API_KEY>';
const oracle = new GasPriceOracle({ defaultRpc });
oracle.fetchGasPricesOnChain().then(gasPrices => {
console.log(gasPrices); // 21
});