Skip to content

Commit

Permalink
feat: remove isRegtest argument from getFeeRate, modify readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Polybius93 committed Dec 16, 2024
1 parent fe388de commit 6a5806d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 38 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

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
- Average fee rate from the last two blocks `/api/v1/mining/blocks/fee-rates/24h`
- Current mempool block's median fee rate `/api/v1/fees/mempool-blocks`
- Recommended "fastest" fee rate by API `/api/v1/fees/recommended`

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.
16 changes: 11 additions & 5 deletions src/dlc-handlers/abstract-dlc-handler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Transaction } from '@scure/btc-signer';
import { P2Ret, P2TROut } from '@scure/btc-signer/payment';
import { Network } from 'bitcoinjs-lib';
import { regtest } from 'bitcoinjs-lib/src/networks.js';

import {
createNativeSegwitPayment,
Expand Down Expand Up @@ -133,11 +134,16 @@ export abstract class AbstractDLCHandler {
}
}

private async getFeeRate(feeRateMultiplier?: number, customFeeRate?: bigint): Promise<bigint> {
return (
customFeeRate ??
BigInt(await getFeeRate(this.bitcoinBlockchainFeeRecommendationAPI, feeRateMultiplier))
);
protected async getFeeRate(feeRateMultiplier?: number, customFeeRate?: bigint): Promise<bigint> {
if (customFeeRate) {
return customFeeRate;
}

if (this.bitcoinNetwork === regtest) {
return BigInt(2);
}

return BigInt(await getFeeRate(this.bitcoinBlockchainFeeRecommendationAPI, feeRateMultiplier));
}

async createFundingPSBT(
Expand Down
13 changes: 3 additions & 10 deletions src/dlc-handlers/ledger-dlc-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
deriveUnhardenedPublicKey,
ecdsaPublicKeyToSchnorr,
getBalance,
getFeeRate,
getInputByPaymentTypeArray,
getUnspendableKeyCommittedToUUID,
} from '../functions/bitcoin/bitcoin-functions.js';
Expand Down Expand Up @@ -246,9 +245,7 @@ export class LedgerDLCHandler extends AbstractDLCHandler {
attestorGroupPublicKey
);

const feeRate =
customFeeRate ??
BigInt(await getFeeRate(this.bitcoinBlockchainFeeRecommendationAPI, feeRateMultiplier));
const feeRate = await this.getFeeRate(feeRateMultiplier, customFeeRate);

const addressBalance = await getBalance(fundingPayment, this.bitcoinBlockchainAPI);

Expand Down Expand Up @@ -327,9 +324,7 @@ export class LedgerDLCHandler extends AbstractDLCHandler {
attestorGroupPublicKey
);

const feeRate =
customFeeRate ??
BigInt(await getFeeRate(this.bitcoinBlockchainFeeRecommendationAPI, feeRateMultiplier));
const feeRate = await this.getFeeRate(feeRateMultiplier, customFeeRate);

const withdrawTransaction = await createWithdrawTransaction(
this.bitcoinBlockchainAPI,
Expand Down Expand Up @@ -388,9 +383,7 @@ export class LedgerDLCHandler extends AbstractDLCHandler {
const { fundingPayment, taprootDerivedPublicKey, fundingDerivedPublicKey, multisigPayment } =
await this.createPayment(vault.uuid, attestorGroupPublicKey);

const feeRate =
customFeeRate ??
BigInt(await getFeeRate(this.bitcoinBlockchainFeeRecommendationAPI, feeRateMultiplier));
const feeRate = await this.getFeeRate(feeRateMultiplier, customFeeRate);

const depositTransaction = await createDepositTransaction(
this.bitcoinBlockchainAPI,
Expand Down
34 changes: 14 additions & 20 deletions src/functions/bitcoin/bitcoin-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ export function createTaprootMultisigPayment(
*
* @returns A promise that resolves to the last two blocks' median fee rates.
*/
export async function getLastTwoBlocksFeeRate(
export async function getLastTwoBlocksFeeRateAverage(
bitcoinBlockchainAPIFeeURL: string
): Promise<number[]> {
): Promise<number> {
const dayFeeRateAPI = `${bitcoinBlockchainAPIFeeURL}/api/v1/mining/blocks/fee-rates/24h`;

const response = await fetch(dayFeeRateAPI);
Expand All @@ -162,7 +162,12 @@ export async function getLastTwoBlocksFeeRate(

const historicalFeeRates: HistoricalFeeRate[] = await response.json();

return historicalFeeRates.slice(historicalFeeRates.length - 2).map(rate => rate.avgFee_50);
return (
historicalFeeRates
.slice(historicalFeeRates.length - 2)
.map(rate => rate.avgFee_50)
.reduce((a, b) => a + b) / 2
);
}

/**
Expand Down Expand Up @@ -213,30 +218,19 @@ export async function getEstimatedFeeRate(bitcoinBlockchainAPIFeeURL: string): P
*/
export async function getFeeRate(
bitcoinBlockchainAPIFeeURL: string,
feeRateMultiplier = 1,
isRegtest = false
feeRateMultiplier = 1
): Promise<number> {
if (isRegtest) return 2;

const [lastTwoBlocksFeeRate, currentBlockFeeRate, estimatedFeeRate] = await Promise.all([
getLastTwoBlocksFeeRate(bitcoinBlockchainAPIFeeURL),
const [lastTwoBlocksFeeRateAverage, currentBlockFeeRate, estimatedFeeRate] = await Promise.all([
getLastTwoBlocksFeeRateAverage(bitcoinBlockchainAPIFeeURL),
getCurrentMempoolBlockFeeRate(bitcoinBlockchainAPIFeeURL),
getEstimatedFeeRate(bitcoinBlockchainAPIFeeURL),
]);

const currentBlockFeeRateMultiplied = currentBlockFeeRate * feeRateMultiplier;

const lastTwoBlocksfeeRateAverageMultiplied =
(lastTwoBlocksFeeRate.reduce((a, b) => a + b) / lastTwoBlocksFeeRate.length) *
feeRateMultiplier;

const estimatedFeeRateMultiplied = estimatedFeeRate * feeRateMultiplier;

return Math.ceil(
Math.max(
lastTwoBlocksfeeRateAverageMultiplied,
currentBlockFeeRateMultiplied,
estimatedFeeRateMultiplied
lastTwoBlocksFeeRateAverage * feeRateMultiplier,
currentBlockFeeRate * feeRateMultiplier,
estimatedFeeRate * feeRateMultiplier
)
);
}
Expand Down

0 comments on commit 6a5806d

Please sign in to comment.