on 44dc2fe help sign in
bridge/src/lib/useGasPrice.js
48 lines 1.3 kB history raw
1import { useCallback, useEffect, useState } from 'react';
3import { useNetwork } from 'store/network';
5import getSuggestedGasPrice from 'lib/getSuggestedGasPrice';
6import { DEFAULT_GAS_PRICE_GWEI } from './constants';
8/**
9 * handles all of the state of components that manage gas prices
10 * + simple get/set
11 * + lazy fetching optimal gas from oracle
12 * + resetting to oracle (or sync) default
13 * @param {Number} initialGasPrice the initial gas price available instantly
14 */
15export default function useGasPrice(initialGasPrice = DEFAULT_GAS_PRICE_GWEI) {
16 const { networkType } = useNetwork();
17 const [suggestedGasPrice, setSuggestedGasPrice] = useState(initialGasPrice); // gwei
18 const [gasPrice, setGasPrice] = useState(initialGasPrice); // gwei
20 useEffect(() => {
21 let mounted = true;
23 (async () => {
24 const suggestedGasPrice = await getSuggestedGasPrice(networkType);
26 if (!mounted) {
27 return;
28 }
30 // TODO: BN
31 setSuggestedGasPrice(suggestedGasPrice);
32 setGasPrice(suggestedGasPrice);
33 })();
35 return () => (mounted = true);
36 }, [networkType]);
38 const resetGasPrice = useCallback(() => setGasPrice(suggestedGasPrice), [
39 suggestedGasPrice,
40 ]);
42 return {
43 suggestedGasPrice,
44 gasPrice,
45 setGasPrice,
46 resetGasPrice,
47 };