44 lines
1.3 KiB
TypeScript
44 lines
1.3 KiB
TypeScript
import type { MempoolTransaction } from './mempoolSpaceTypes'
|
|
|
|
const MEMPOOL_API_BASE = 'https://mempool.space/api'
|
|
|
|
export async function getTransaction(txid: string): Promise<MempoolTransaction | null> {
|
|
try {
|
|
const response = await fetch(`${MEMPOOL_API_BASE}/tx/${txid}`)
|
|
|
|
if (!response.ok) {
|
|
if (response.status === 404) {
|
|
console.warn('Transaction not found on mempool.space', { txid })
|
|
return null
|
|
}
|
|
throw new Error(`Failed to fetch transaction: ${response.status} ${response.statusText}`)
|
|
}
|
|
|
|
const transaction = await response.json() as MempoolTransaction
|
|
return transaction
|
|
} catch (error) {
|
|
console.error('Error fetching transaction from mempool.space', {
|
|
txid,
|
|
error: error instanceof Error ? error.message : 'Unknown error',
|
|
timestamp: new Date().toISOString(),
|
|
})
|
|
return null
|
|
}
|
|
}
|
|
|
|
export async function getConfirmations(blockHeight: number): Promise<number> {
|
|
try {
|
|
const response = await fetch(`${MEMPOOL_API_BASE}/blocks/tip/height`)
|
|
if (!response.ok) {
|
|
return 0
|
|
}
|
|
const currentHeight = await response.json() as number
|
|
return Math.max(0, currentHeight - blockHeight + 1)
|
|
} catch (error) {
|
|
console.error('Error getting current block height', {
|
|
error: error instanceof Error ? error.message : 'Unknown error',
|
|
})
|
|
return 0
|
|
}
|
|
}
|