From fe388def05bd86bc1ace73398a764ca1a1e8c488 Mon Sep 17 00:00:00 2001 From: Polybius93 Date: Mon, 16 Dec 2024 13:39:37 +0100 Subject: [PATCH] feat: add fee rate calculation readme section --- README.md | 10 ++++++++++ src/functions/bitcoin/bitcoin-functions.ts | 11 +++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index d5a872a..2c44c4d 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,13 @@ # dlc-btc-lib **dlc-btc-lib** is a comprehensive library for interacting with DLC.Link smart contracts and the Bitcoin blockchain. It includes functions for creating valid Partially Signed Bitcoin Transactions, handling setup, deposit, and withdrawal interactions, and interfacing with Attestors. This library provides all the essential tools and utilities for seamless blockchain and smart contract interactions. + +## Fee Rate Calculation + +The transaction fee rate is calculated by taking the maximum value among three metrics from the Bitcoin blockchain: + +- Average fee rate from the last two blocks +- Current mempool block's median fee rate +- Estimated "fastest" fee rate + +Each metric is adjusted by an optional multiplier (defaults to 1.0) and the final result is rounded up to the nearest whole number. For regtest environments, a fixed fee rate of 2 is used. diff --git a/src/functions/bitcoin/bitcoin-functions.ts b/src/functions/bitcoin/bitcoin-functions.ts index 4f3f895..b2d8dc6 100644 --- a/src/functions/bitcoin/bitcoin-functions.ts +++ b/src/functions/bitcoin/bitcoin-functions.ts @@ -213,25 +213,24 @@ export async function getEstimatedFeeRate(bitcoinBlockchainAPIFeeURL: string): P */ export async function getFeeRate( bitcoinBlockchainAPIFeeURL: string, - feeRateMultiplier?: number, + feeRateMultiplier = 1, isRegtest = false ): Promise { if (isRegtest) return 2; - const multiplier = feeRateMultiplier ?? 1; - const [lastTwoBlocksFeeRate, currentBlockFeeRate, estimatedFeeRate] = await Promise.all([ getLastTwoBlocksFeeRate(bitcoinBlockchainAPIFeeURL), getCurrentMempoolBlockFeeRate(bitcoinBlockchainAPIFeeURL), getEstimatedFeeRate(bitcoinBlockchainAPIFeeURL), ]); - const currentBlockFeeRateMultiplied = currentBlockFeeRate * multiplier; + const currentBlockFeeRateMultiplied = currentBlockFeeRate * feeRateMultiplier; const lastTwoBlocksfeeRateAverageMultiplied = - (lastTwoBlocksFeeRate.reduce((a, b) => a + b) / lastTwoBlocksFeeRate.length) * multiplier; + (lastTwoBlocksFeeRate.reduce((a, b) => a + b) / lastTwoBlocksFeeRate.length) * + feeRateMultiplier; - const estimatedFeeRateMultiplied = estimatedFeeRate * multiplier; + const estimatedFeeRateMultiplied = estimatedFeeRate * feeRateMultiplier; return Math.ceil( Math.max(