Skip to content

Commit

Permalink
Merge pull request #45 from LN-Zap/feat/height
Browse files Browse the repository at this point in the history
Provide current block height data
  • Loading branch information
mrfelton authored Feb 18, 2024
2 parents 72b61dd + 326b70d commit 931ec5e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/custom.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type FeeByBlockTarget = {
// Estimates represents the current block hash and fee by block target.
type Estimates = {
current_block_hash: string | null; // current block hash
current_block_height: number | null; // current block height
fee_by_block_target: FeeByBlockTarget; // fee by block target (in sat/kb)
};

Expand Down
49 changes: 47 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,37 @@ export const FEE_MINIMUM = config.get<number>("settings.feeMinimum");
export const CACHE_STDTTL = config.get<number>("cache.stdTTL");
export const CACHE_CHECKPERIOD = config.get<number>("cache.checkperiod");

// Constants
// Primary URLs
export const MEMPOOL_TIP_HASH_URL =
MEMPOOL_BASE_URL && `${MEMPOOL_BASE_URL}/api/blocks/tip/hash`;
export const ESPLORA_TIP_HASH_URL =
ESPLORA_BASE_URL && `${ESPLORA_BASE_URL}/api/blocks/tip/hash`;

export const MEMPOOL_TIP_HEIGHT_URL =
MEMPOOL_BASE_URL && `${MEMPOOL_BASE_URL}/api/blocks/tip/height`;
export const ESPLORA_TIP_HEIGHT_URL =
ESPLORA_BASE_URL && `${ESPLORA_BASE_URL}/api/blocks/tip/height`;

export const MEMPOOL_FEES_URL =
MEMPOOL_BASE_URL && `${MEMPOOL_BASE_URL}/api/v1/fees/recommended`;
export const ESPLORA_FEE_ESTIMATES_URL =
ESPLORA_BASE_URL && `${ESPLORA_BASE_URL}/api/fee-estimates`;

// Fallback URLs
export const MEMPOOL_TIP_HASH_URL_FALLBACK =
MEMPOOL_FALLBACK_BASE_URL &&
`${MEMPOOL_FALLBACK_BASE_URL}/api/blocks/tip/hash`;
export const ESPLORA_TIP_HASH_URL_FALLBACK =
ESPLORA_FALLBACK_BASE_URL &&
`${ESPLORA_FALLBACK_BASE_URL}/api/blocks/tip/hash`;

export const MEMPOOL_TIP_HEIGHT_URL_FALLBACK =
MEMPOOL_FALLBACK_BASE_URL &&
`${MEMPOOL_FALLBACK_BASE_URL}/api/blocks/tip/height`;
export const ESPLORA_TIP_HEIGHT_URL_FALLBACK =
ESPLORA_FALLBACK_BASE_URL &&
`${ESPLORA_FALLBACK_BASE_URL}/api/blocks/tip/height`;

export const MEMPOOL_FEES_URL_FALLBACK =
MEMPOOL_FALLBACK_BASE_URL &&
`${MEMPOOL_FALLBACK_BASE_URL}/api/v1/fees/recommended`;
Expand Down Expand Up @@ -337,6 +352,33 @@ export async function fetchBlocksTipHash(): Promise<string | null> {
return res0 || res1 || null;
}

/**
* Fetches the current block height.
*/
export async function fetchBlocksTipHeight(): Promise<number | null> {
const tasks = [
(MEMPOOL_TIP_HEIGHT_URL || MEMPOOL_TIP_HEIGHT_URL_FALLBACK) &&
fetchAndHandle(
MEMPOOL_TIP_HEIGHT_URL,
"text",
MEMPOOL_TIP_HEIGHT_URL_FALLBACK,
),
(ESPLORA_TIP_HEIGHT_URL || ESPLORA_TIP_HEIGHT_URL_FALLBACK) &&
fetchAndHandle(
ESPLORA_TIP_HEIGHT_URL,
"text",
ESPLORA_TIP_HEIGHT_URL_FALLBACK,
),
].filter(Boolean);
const res = await Promise.allSettled(tasks);

let res0 = getValueFromFulfilledPromise(res[0]);
let res1 = getValueFromFulfilledPromise(res[1]);

const height = res0 || res1 || null;
return height ? Number(height) : null;
}

/**
* Gets the current fee estimates from the cache or fetches them if they are not cached.
*/
Expand All @@ -353,19 +395,22 @@ export async function getEstimates(): Promise<Estimates> {
await fetchEsploraData(),
await fetchBitcoindData(),
await fetchBlocksTipHash(),
await fetchBlocksTipHeight(),
];
const [result1, result2, result3, result4] = await Promise.allSettled(tasks);
const [result1, result2, result3, result4, result5] = await Promise.allSettled(tasks);
const mempoolFeeEstimates = getValueFromFulfilledPromise(result1);
const esploraFeeEstimates = getValueFromFulfilledPromise(result2);
const bitcoindFeeEstimates = getValueFromFulfilledPromise(result3);
const blocksTipHash = getValueFromFulfilledPromise(result4);
const blocksTipHeight = getValueFromFulfilledPromise(result5);

// Get the minimum fee. If the mempool fee estimates are not available, use a default value of FEE_MINIMUM sat/vbyte as a safety net.
const feeMinimum = (mempoolFeeEstimates?.minimumFee ?? FEE_MINIMUM) * 1000;
log.info({ message: "Using minimum fee: {feeMinimum}", feeMinimum });

estimates = {
current_block_hash: blocksTipHash,
current_block_height: blocksTipHeight,
fee_by_block_target: calculateFees(
mempoolFeeEstimates,
esploraFeeEstimates,
Expand Down

0 comments on commit 931ec5e

Please sign in to comment.