diff --git a/blue_modules/BlueElectrum.js b/blue_modules/BlueElectrum.js index 79ede1458..47de4ca44 100644 --- a/blue_modules/BlueElectrum.js +++ b/blue_modules/BlueElectrum.js @@ -850,13 +850,18 @@ module.exports.estimateFees = async function () { clearTimeout(timeoutId); } - if (!histogram) throw new Error('timeout while getting mempool_getFeeHistogram'); - // fetching what electrum (which uses bitcoin core) thinks about fees: const _fast = await module.exports.estimateFee(1); const _medium = await module.exports.estimateFee(18); const _slow = await module.exports.estimateFee(144); + /** + * sanity check, see + * @see https://github.com/cculianu/Fulcrum/issues/197 + * (fallback to bitcoin core estimates) + */ + if (!histogram || histogram?.[0]?.[0] > 1000) return { fast: _fast, medium: _medium, slow: _slow }; + // calculating fast fees from mempool: const fast = Math.max(2, module.exports.calcEstimateFeeFromFeeHistorgam(1, histogram)); // recalculating medium and slow fees using bitcoincore estimations only like relative weights: diff --git a/models/networkTransactionFees.js b/models/networkTransactionFees.js deleted file mode 100644 index ce1ee5a81..000000000 --- a/models/networkTransactionFees.js +++ /dev/null @@ -1,44 +0,0 @@ -const BlueElectrum = require('../blue_modules/BlueElectrum'); - -export const NetworkTransactionFeeType = Object.freeze({ - FAST: 'Fast', - MEDIUM: 'MEDIUM', - SLOW: 'SLOW', - CUSTOM: 'CUSTOM', -}); - -export class NetworkTransactionFee { - static StorageKey = 'NetworkTransactionFee'; - - constructor(fastestFee = 2, mediumFee = 1, slowFee = 1) { - this.fastestFee = fastestFee; - this.mediumFee = mediumFee; - this.slowFee = slowFee; - } -} - -export default class NetworkTransactionFees { - static recommendedFees() { - // eslint-disable-next-line no-async-promise-executor - return new Promise(async resolve => { - try { - const isDisabled = await BlueElectrum.isDisabled(); - if (isDisabled) { - throw new Error('Electrum is disabled. Dont attempt to fetch fees'); - } - const response = await BlueElectrum.estimateFees(); - if (typeof response === 'object') { - const networkFee = new NetworkTransactionFee(response.fast, response.medium, response.slow); - resolve(networkFee); - } else { - const networkFee = new NetworkTransactionFee(2, 1, 1); - resolve(networkFee); - } - } catch (err) { - console.warn(err); - const networkFee = new NetworkTransactionFee(2, 1, 1); - resolve(networkFee); - } - }); - } -} diff --git a/models/networkTransactionFees.ts b/models/networkTransactionFees.ts new file mode 100644 index 000000000..18a5bef4f --- /dev/null +++ b/models/networkTransactionFees.ts @@ -0,0 +1,38 @@ +const BlueElectrum = require('../blue_modules/BlueElectrum'); + +export const NetworkTransactionFeeType = Object.freeze({ + FAST: 'Fast', + MEDIUM: 'MEDIUM', + SLOW: 'SLOW', + CUSTOM: 'CUSTOM', +}); + +export class NetworkTransactionFee { + static StorageKey = 'NetworkTransactionFee'; + + private fastestFee: number; + private mediumFee: number; + private slowFee: number; + + constructor(fastestFee = 2, mediumFee = 1, slowFee = 1) { + this.fastestFee = fastestFee; + this.mediumFee = mediumFee; + this.slowFee = slowFee; + } +} + +export default class NetworkTransactionFees { + static async recommendedFees(): Promise { + try { + const isDisabled = await BlueElectrum.isDisabled(); + if (isDisabled) { + throw new Error('Electrum is disabled. Dont attempt to fetch fees'); + } + const response = await BlueElectrum.estimateFees(); + return new NetworkTransactionFee(response.fast + 5, response.medium + 2, response.slow); + } catch (err) { + console.warn(err); + return new NetworkTransactionFee(2, 1, 1); + } + } +}