Enforce purging rate minimum on recommended fees

This commit is contained in:
Mononaut 2023-10-25 16:53:05 +00:00
parent 413941f5ac
commit 83fde600f1
No known key found for this signature in database
GPG Key ID: A3F058E41374C04E

View File

@ -3,21 +3,30 @@ import { Common } from './common';
import mempool from './mempool'; import mempool from './mempool';
import projectedBlocks from './mempool-blocks'; import projectedBlocks from './mempool-blocks';
interface RecommendedFees {
fastestFee: number,
halfHourFee: number,
hourFee: number,
economyFee: number,
minimumFee: number,
}
class FeeApi { class FeeApi {
constructor() { } constructor() { }
defaultFee = Common.isLiquid() ? 0.1 : 1; defaultFee = Common.isLiquid() ? 0.1 : 1;
public getRecommendedFee() { public getRecommendedFee(): RecommendedFees {
const pBlocks = projectedBlocks.getMempoolBlocks(); const pBlocks = projectedBlocks.getMempoolBlocks();
const mPool = mempool.getMempoolInfo(); const mPool = mempool.getMempoolInfo();
const minimumFee = Math.ceil(mPool.mempoolminfee * 100000); const minimumFee = Math.ceil(mPool.mempoolminfee * 100000);
const defaultMinFee = Math.max(minimumFee, this.defaultFee);
if (!pBlocks.length) { if (!pBlocks.length) {
return { return {
'fastestFee': this.defaultFee, 'fastestFee': defaultMinFee,
'halfHourFee': this.defaultFee, 'halfHourFee': defaultMinFee,
'hourFee': this.defaultFee, 'hourFee': defaultMinFee,
'economyFee': minimumFee, 'economyFee': minimumFee,
'minimumFee': minimumFee, 'minimumFee': minimumFee,
}; };
@ -27,11 +36,15 @@ class FeeApi {
const secondMedianFee = pBlocks[1] ? this.optimizeMedianFee(pBlocks[1], pBlocks[2], firstMedianFee) : this.defaultFee; const secondMedianFee = pBlocks[1] ? this.optimizeMedianFee(pBlocks[1], pBlocks[2], firstMedianFee) : this.defaultFee;
const thirdMedianFee = pBlocks[2] ? this.optimizeMedianFee(pBlocks[2], pBlocks[3], secondMedianFee) : this.defaultFee; const thirdMedianFee = pBlocks[2] ? this.optimizeMedianFee(pBlocks[2], pBlocks[3], secondMedianFee) : this.defaultFee;
// explicitly enforce a minimum of ceil(mempoolminfee) on all recommendations.
// simply rounding up recommended rates is insufficient, as the purging rate
// can exceed the median rate of projected blocks in some extreme scenarios
// (see https://bitcoin.stackexchange.com/a/120024)
return { return {
'fastestFee': firstMedianFee, 'fastestFee': Math.max(minimumFee, firstMedianFee),
'halfHourFee': secondMedianFee, 'halfHourFee': Math.max(minimumFee, secondMedianFee),
'hourFee': thirdMedianFee, 'hourFee': Math.max(minimumFee, thirdMedianFee),
'economyFee': Math.min(2 * minimumFee, thirdMedianFee), 'economyFee': Math.max(minimumFee, Math.min(2 * minimumFee, thirdMedianFee)),
'minimumFee': minimumFee, 'minimumFee': minimumFee,
}; };
} }