@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
40 lines • 1.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useGasOptions = void 0;
const index_1 = require("@ledgerhq/coin-evm/network/gasTracker/index");
const react_1 = require("react");
/**
* React hook to get gas options for a given currency
* Used in the send flow to get the gas options for the currency,
* for example in SelectFeeStrategy.tsx file in the renderer/families/{currency} folder in LLD
*/
const useGasOptions = ({ currency, transaction,
// interval is the time in milliseconds between each call to the gas tracker
interval = 60 * 1000, }) => {
const shouldUseEip1559 = transaction.type === 2;
const gasTracker = (0, react_1.useMemo)(() => (0, index_1.getGasTracker)(currency), [currency]);
const [error, setError] = (0, react_1.useState)(null);
const [gasOptions, setGasOptions] = (0, react_1.useState)(undefined);
const [loading, setLoading] = (0, react_1.useState)(true);
(0, react_1.useEffect)(() => {
if (!gasTracker) {
setLoading(false);
return;
}
const getGasOptionsCallback = async () => gasTracker
.getGasOptions({ currency, options: { useEIP1559: shouldUseEip1559 } })
.then(setGasOptions)
.catch(setError)
.finally(() => setLoading(false));
getGasOptionsCallback();
if (interval > 0) {
const intervalId = setInterval(() => getGasOptionsCallback(), interval);
return () => {
clearInterval(intervalId);
};
}
}, [gasTracker, interval, currency, shouldUseEip1559]);
return [gasOptions, error, loading];
};
exports.useGasOptions = useGasOptions;
//# sourceMappingURL=react.js.map