Merge pull request #5704 from BlueWallet/fix-fees

FIX: incorrect fees occasionally (for some electrum servers); better …
This commit is contained in:
GLaDOS 2023-09-29 11:16:50 +01:00 committed by GitHub
commit 77ab792a5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 46 deletions

View File

@ -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:

View File

@ -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);
}
});
}
}

View File

@ -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<NetworkTransactionFee> {
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);
}
}
}