( provider: NetworkProvider, address: string, utxo: Utxo, )
| 56 | // transaction. |
| 57 | // Token UTXOs are only supported on MockNetworkProvider. |
| 58 | export async function addUtxo( |
| 59 | provider: NetworkProvider, |
| 60 | address: string, |
| 61 | utxo: Utxo, |
| 62 | ): Promise<Utxo> { |
| 63 | if (provider instanceof MockNetworkProvider) { |
| 64 | return provider.addUtxo(address, utxo); |
| 65 | } |
| 66 | |
| 67 | if (utxo.token) { |
| 68 | throw new Error('addUtxo: creating token UTXOs on a live network is not supported'); |
| 69 | } |
| 70 | |
| 71 | // Serialize all live addUtxo calls through a single-slot queue. Parallel calls would |
| 72 | // otherwise race to fetch and spend the same funder UTXO, causing txn-mempool-conflict. |
| 73 | // As a fallback, p-retry retries up to 10 times on mempool conflicts if one still slips |
| 74 | // through (e.g. the electrum server hadn't yet indexed the previous change UTXO). |
| 75 | const result = await liveAddUtxoQueue.add(() => pRetry( |
| 76 | () => sendLiveAddUtxo(provider, address, utxo), |
| 77 | { shouldRetry: ({ error }) => isMempoolConflictError(error) }, |
| 78 | )); |
| 79 | if (!result) throw new Error('addUtxo: live queue returned no result'); |
| 80 | return result; |
| 81 | } |
| 82 | |
| 83 | function isMempoolConflictError(error: unknown): boolean { |
| 84 | const message = error instanceof Error ? error.message : String(error); |
no test coverage detected