bigblocks
Version:
Complete Bitcoin UI component library - authentication, social, wallet, market, inscriptions, and blockchain interactions for React
96 lines (95 loc) • 3.41 kB
JavaScript
const DEFAULT_CONFIG = {
apiEndpoint: "https://api.whatsonchain.com/v1/bsv/main/tx/raw",
timeout: 30000,
retries: 3,
};
/**
* @deprecated Use BlockchainService from useBlockchainService() hook instead
*/
/**
* Broadcast a transaction to the Bitcoin SV network
* @deprecated Use BlockchainService.broadcastTransaction() instead
*/
export async function broadcastTransaction(tx, config = {}) {
console.warn("broadcastTransaction is deprecated. Use BlockchainService.broadcastTransaction() instead.");
const finalConfig = { ...DEFAULT_CONFIG, ...config };
const rawTx = typeof tx === "string" ? tx : tx.toHex();
let lastError = null;
for (let attempt = 1; attempt <= (finalConfig.retries || 3); attempt++) {
try {
if (!finalConfig.apiEndpoint) {
throw new Error("Broadcast API endpoint not configured");
}
const response = await fetch(finalConfig.apiEndpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ txhex: rawTx }),
signal: AbortSignal.timeout(finalConfig.timeout || 30000),
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`HTTP ${response.status}: ${errorText}`);
}
const result = await response.text();
// WhatsOnChain returns the txid on success
if (result && result.length === 64) {
return {
success: true,
txid: result,
};
}
throw new Error(`Invalid response: ${result}`);
}
catch (error) {
lastError = error;
console.warn(`Broadcast attempt ${attempt} failed:`, error);
// Wait before retrying (exponential backoff)
if (attempt < (finalConfig.retries || 3)) {
await new Promise((resolve) => setTimeout(resolve, 1000 * attempt));
}
}
}
return {
success: false,
error: lastError?.message || "Broadcast failed after all retries",
};
}
/**
* Broadcast multiple transactions in sequence
* @deprecated Use BlockchainService for broadcasting
*/
export async function broadcastTransactions(transactions, config = {}) {
console.warn("broadcastTransactions is deprecated. Use BlockchainService for broadcasting.");
const results = [];
for (const tx of transactions) {
const result = await broadcastTransaction(tx, config);
results.push(result);
// If a transaction fails, stop broadcasting the rest
if (!result.success) {
break;
}
// Small delay between broadcasts
await new Promise((resolve) => setTimeout(resolve, 100));
}
return results;
}
/**
* Estimate transaction fee based on size and fee rate
*/
export function estimateFee(txSize, feeRate = 0.5) {
return Math.ceil(txSize * feeRate);
}
/**
* Check if a transaction ID is valid format
*/
export function isValidTxid(txid) {
return /^[a-fA-F0-9]{64}$/.test(txid);
}
/**
* Create a WhatsOnChain explorer URL for a transaction
*/
export function createExplorerUrl(txid, _network = "main") {
return `https://whatsonchain.com/tx/${txid}`;
}