1import { NETWORK_TYPES } from './network';
2import { DEFAULT_GAS_PRICE_GWEI, MAX_GAS_PRICE_GWEI } from './constants';
3import Web3 from 'web3';
5// ethgasstation returns values in floating point, one order of magitude
6// more than gwei. see: https://docs.ethgasstation.info
7// we don't want to charge users more than the gas tank funds
8const minGas = gas => Math.min(Math.ceil(gas), MAX_GAS_PRICE_GWEI);
10const feeToInt = (f) => f < 1 ? 1 : Math.round(f);
12const feeToWei = (fee) => Web3.utils.toHex(Web3.utils.toWei(String(fee), 'gwei' ))
14const calculateMaxFee = (baseFee, maxPriorityFee) => feeToWei(Math.round((2 * baseFee) + maxPriorityFee))
16// Convert seconds to minutes prettily
17const formatWait = (wait) => Math.round((wait * 100) / 60) / 100;
19export const defaultGasValues = value => ({
24 maxPriorityFeePerGas: 1,
25 suggestedBaseFeePerGas: value > 2 ? value -1 : 1,
31 maxPriorityFeePerGas: 1,
32 suggestedBaseFeePerGas: value > 2 ? value -1 : 1,
38 maxPriorityFeePerGas: 1,
39 suggestedBaseFeePerGas: value > 2 ? value -1 : 1,
43const getGasForNetwork = async (providerUrl) => {
45 const [feeResponse, waitResponse] = await Promise.all([
47 `${providerUrl}/api` +
48 '?module=gastracker' +
50 '&apikey=CG52E4R96W56GIKUI4IJ8CH9EZIXPUW1W8',
57 'https://ethereum-api.xyz/gas-prices',
65 const [feeJson, waitJson] = await Promise.all([
70 const suggestedBaseFeePerGas = Number(feeJson.result.suggestBaseFee);
72 // Calculations derived from:
73 // https://www.blocknative.com/blog/eip-1559-fees
76 price: minGas(feeJson.result.FastGasPrice),
77 wait: formatWait(waitJson.result.fast.time),
78 maxFeePerGas: calculateMaxFee(suggestedBaseFeePerGas, feeJson.result.FastGasPrice - suggestedBaseFeePerGas),
79 maxPriorityFeePerGas: feeToInt((feeJson.result.FastGasPrice - suggestedBaseFeePerGas)),
80 suggestedBaseFeePerGas
83 price: minGas(feeJson.result.ProposeGasPrice),
84 wait: formatWait(waitJson.result.average.time),
85 maxFeePerGas: calculateMaxFee(suggestedBaseFeePerGas, feeJson.result.ProposeGasPrice - suggestedBaseFeePerGas),
86 maxPriorityFeePerGas: feeToInt((feeJson.result.ProposeGasPrice - suggestedBaseFeePerGas)),
87 suggestedBaseFeePerGas
90 price: minGas(feeJson.result.SafeGasPrice),
91 wait: formatWait(waitJson.result.slow.time),
92 maxFeePerGas: calculateMaxFee(suggestedBaseFeePerGas, feeJson.result.SafeGasPrice - suggestedBaseFeePerGas),
93 maxPriorityFeePerGas: feeToInt((feeJson.result.SafeGasPrice - suggestedBaseFeePerGas)),
94 suggestedBaseFeePerGas
99 return defaultGasValues(DEFAULT_GAS_PRICE_GWEI);
103export default async function getSuggestedGasPrice(networkType) {
104 switch (networkType) {
105 case NETWORK_TYPES.GOERLI:
106 return getGasForNetwork('https://api-goerli.etherscan.io')
107 case NETWORK_TYPES.OFFLINE:
108 return defaultGasValues(DEFAULT_GAS_PRICE_GWEI);
109 case NETWORK_TYPES.LOCAL:
111 return getGasForNetwork('https://api.etherscan.io/');
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list