UNPKG

bigblocks

Version:

Complete Bitcoin UI component library - authentication, social, wallet, market, inscriptions, and blockchain interactions for React

17 lines 4.21 kB
{ "name": "market-utils-broadcast", "type": "registry:component", "dependencies": [ "@bsv/sdk" ], "devDependencies": [], "registryDependencies": [], "files": [ { "path": "components/market/utils/broadcast.ts", "type": "registry:component", "content": "// Broadcast Utilities - Transaction broadcasting functionality\nimport type { Transaction } from \"@bsv/sdk\";\nimport type { BroadcastResult } from \"../types/market.js\";\n\nexport interface BroadcastConfig {\n\tapiEndpoint?: string;\n\ttimeout?: number;\n\tretries?: number;\n}\n\nconst DEFAULT_CONFIG: BroadcastConfig = {\n\tapiEndpoint: \"https://api.whatsonchain.com/v1/bsv/main/tx/raw\",\n\ttimeout: 30000,\n\tretries: 3,\n};\n\n/**\n * @deprecated Use BlockchainService from useBlockchainService() hook instead\n */\n\n/**\n * Broadcast a transaction to the Bitcoin SV network\n * @deprecated Use BlockchainService.broadcastTransaction() instead\n */\nexport async function broadcastTransaction(\n\ttx: Transaction | string,\n\tconfig: BroadcastConfig = {},\n): Promise<BroadcastResult> {\n\tconsole.warn(\n\t\t\"broadcastTransaction is deprecated. Use BlockchainService.broadcastTransaction() instead.\",\n\t);\n\tconst finalConfig = { ...DEFAULT_CONFIG, ...config };\n\tconst rawTx = typeof tx === \"string\" ? tx : tx.toHex();\n\n\tlet lastError: Error | null = null;\n\n\tfor (let attempt = 1; attempt <= (finalConfig.retries || 3); attempt++) {\n\t\ttry {\n\t\t\tif (!finalConfig.apiEndpoint) {\n\t\t\t\tthrow new Error(\"Broadcast API endpoint not configured\");\n\t\t\t}\n\t\t\tconst response = await fetch(finalConfig.apiEndpoint, {\n\t\t\t\tmethod: \"POST\",\n\t\t\t\theaders: {\n\t\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\t},\n\t\t\t\tbody: JSON.stringify({ txhex: rawTx }),\n\t\t\t\tsignal: AbortSignal.timeout(finalConfig.timeout || 30000),\n\t\t\t});\n\n\t\t\tif (!response.ok) {\n\t\t\t\tconst errorText = await response.text();\n\t\t\t\tthrow new Error(`HTTP ${response.status}: ${errorText}`);\n\t\t\t}\n\n\t\t\tconst result = await response.text();\n\n\t\t\t// WhatsOnChain returns the txid on success\n\t\t\tif (result && result.length === 64) {\n\t\t\t\treturn {\n\t\t\t\t\tsuccess: true,\n\t\t\t\t\ttxid: result,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tthrow new Error(`Invalid response: ${result}`);\n\t\t} catch (error) {\n\t\t\tlastError = error as Error;\n\t\t\tconsole.warn(`Broadcast attempt ${attempt} failed:`, error);\n\n\t\t\t// Wait before retrying (exponential backoff)\n\t\t\tif (attempt < (finalConfig.retries || 3)) {\n\t\t\t\tawait new Promise((resolve) => setTimeout(resolve, 1000 * attempt));\n\t\t\t}\n\t\t}\n\t}\n\n\treturn {\n\t\tsuccess: false,\n\t\terror: lastError?.message || \"Broadcast failed after all retries\",\n\t};\n}\n\n/**\n * Broadcast multiple transactions in sequence\n * @deprecated Use BlockchainService for broadcasting\n */\nexport async function broadcastTransactions(\n\ttransactions: (Transaction | string)[],\n\tconfig: BroadcastConfig = {},\n): Promise<BroadcastResult[]> {\n\tconsole.warn(\n\t\t\"broadcastTransactions is deprecated. Use BlockchainService for broadcasting.\",\n\t);\n\tconst results: BroadcastResult[] = [];\n\n\tfor (const tx of transactions) {\n\t\tconst result = await broadcastTransaction(tx, config);\n\t\tresults.push(result);\n\n\t\t// If a transaction fails, stop broadcasting the rest\n\t\tif (!result.success) {\n\t\t\tbreak;\n\t\t}\n\n\t\t// Small delay between broadcasts\n\t\tawait new Promise((resolve) => setTimeout(resolve, 100));\n\t}\n\n\treturn results;\n}\n\n/**\n * Estimate transaction fee based on size and fee rate\n */\nexport function estimateFee(txSize: number, feeRate = 0.5): number {\n\treturn Math.ceil(txSize * feeRate);\n}\n\n/**\n * Check if a transaction ID is valid format\n */\nexport function isValidTxid(txid: string): boolean {\n\treturn /^[a-fA-F0-9]{64}$/.test(txid);\n}\n\n/**\n * Create a WhatsOnChain explorer URL for a transaction\n */\nexport function createExplorerUrl(\n\ttxid: string,\n\t_network: \"main\" | \"test\" = \"main\",\n): string {\n\treturn `https://whatsonchain.com/tx/${txid}`;\n}\n", "target": "<%- config.aliases.components %>/broadcast.tsx" } ] }