From 0ff395713972ba5aebdb141571279b884dc18210 Mon Sep 17 00:00:00 2001 From: michael1011 Date: Tue, 21 Jan 2025 01:01:27 +0100 Subject: [PATCH] refactor: cleanup external broadcast --- src/i18n/i18n.ts | 15 +++++++-------- src/utils/boltzClient.ts | 29 ++++++++++------------------- src/utils/helper.ts | 7 ++----- 3 files changed, 19 insertions(+), 32 deletions(-) diff --git a/src/i18n/i18n.ts b/src/i18n/i18n.ts index 6931cced..e6cf2c89 100644 --- a/src/i18n/i18n.ts +++ b/src/i18n/i18n.ts @@ -242,7 +242,7 @@ const dict = { routing_fee_limit: "Routing fee limit", broadcast_setting: "External Broadcast", broadcast_setting_tooltip: - "Use third-party block explorers for broadcasting claim and refund transactions in addition to Boltz backend", + "Also use the block explorer to broadcast transactions", }, de: { language: "Deutsch", @@ -495,9 +495,9 @@ const dict = { no_wallet_connected: "Kein Wallet verbunden", no_lockup_transaction: "Keine Lockup-Transaktion gefunden", routing_fee_limit: "Routing Gebühr Limit", - broadcast_setting: "Externe Sendung", + broadcast_setting: "Externer Broadcast", broadcast_setting_tooltip: - "Verwenden Sie Drittanbieter-Blockexplorer, um Anspruchs- und Rückerstattungstransaktionen zusätzlich zum Boltz-Backend zu senden", + "Verwende auch den Block Explorer, um Transaktionen zu senden", }, es: { language: "Español", @@ -748,7 +748,7 @@ const dict = { routing_fee_limit: "Límite de la tarifa de enrutamiento", broadcast_setting: "Transmisión externa", broadcast_setting_tooltip: - "Utilice exploradores de bloques de terceros para transmitir transacciones de reclamo y reembolso además del backend de Boltz", + "También use el explorador de bloques para transmitir transacciones", }, zh: { language: "中文", @@ -971,8 +971,7 @@ const dict = { no_lockup_transaction: "未找到锁仓交易", routing_fee_limit: "最大路由费用", broadcast_setting: "外部广播", - broadcast_setting_tooltip: - "除了Boltz后台外,还使用第三方区块浏览器广播认领和退款交易", + broadcast_setting_tooltip: "使用区块浏览器发送认领和退款交易", }, ja: { language: "日本語", @@ -1220,9 +1219,9 @@ const dict = { no_wallet_connected: "財布はつながっていない!", no_lockup_transaction: "ロックアップトランザクションが見つかりません", routing_fee_limit: "ルーティング料金の上限", - broadcast_setting: "外部ブロードキャスト", + broadcast_setting: "外部放送", broadcast_setting_tooltip: - "Boltzバックエンドに加えて、サードパーティのブロックエクスプローラーを使用して請求および返金取引をブロードキャストします", + "ブロック・エクスプローラーを使ってトランザクションをブロードキャストする", }, }; diff --git a/src/utils/boltzClient.ts b/src/utils/boltzClient.ts index 6c188c28..91b51285 100644 --- a/src/utils/boltzClient.ts +++ b/src/utils/boltzClient.ts @@ -355,41 +355,32 @@ export const getContracts = () => export const broadcastTransaction = async ( asset: string, txHex: string, - externalBroadcast: boolean, + externalBroadcast: boolean = false, ): Promise<{ id: string; }> => { const promises: Promise<{ id: string; - }>[] = []; - - // broadcast to Boltz backend - promises.push( + }>[] = [ fetcher<{ id: string }>(`/v2/chain/${asset}/transaction`, { hex: txHex, }), - ); + ]; - // broadcast to block explorer if (externalBroadcast) { promises.push(broadcastToExplorer(asset, txHex)); } - // Wait for all promises to settle const results = await Promise.allSettled(promises); - let reason = ""; - - // Process the results - for (const result of results) { - if (result.status === "fulfilled") { - // Return the first successful transaction ID - return result.value; - } - reason = result.reason; + const successfulResult = results.find( + (result) => result.status === "fulfilled", + ); + if (successfulResult) { + return (successfulResult as PromiseFulfilledResult<{ id: string }>) + .value; } - // If no promises resolved successfully, return the last reason - throw reason; + throw (results[0] as PromiseRejectedResult).reason; }; export const getLockupTransaction = async ( diff --git a/src/utils/helper.ts b/src/utils/helper.ts index 1c8b9de3..b5cdb9bb 100644 --- a/src/utils/helper.ts +++ b/src/utils/helper.ts @@ -138,14 +138,11 @@ export const broadcastToExplorer = async ( txHex: string, ): Promise<{ id: string }> => { const basePath = chooseUrl(config.assets[asset].blockExplorerUrl); - - const opts: RequestInit = { + const response = await fetch(`${basePath}/api/tx`, { method: "POST", body: txHex, - }; + }); - const apiUrl = basePath + "/api/tx"; - const response = await fetch(apiUrl, opts); if (!response.ok) { try { const body = await response.json();