(
utxos: Utxo[],
options?: { amount?: bigint, fee?: bigint },
)
| 111 | } |
| 112 | |
| 113 | export function gatherUtxos( |
| 114 | utxos: Utxo[], |
| 115 | options?: { amount?: bigint, fee?: bigint }, |
| 116 | ): { utxos: Utxo[], total: bigint, changeAmount: bigint } { |
| 117 | const targetUtxos: Utxo[] = []; |
| 118 | let total = 0n; |
| 119 | |
| 120 | // 1000 for fees |
| 121 | const { amount = 0n, fee = 1000n } = options ?? {}; |
| 122 | |
| 123 | const minChangeAmount = 1000n; |
| 124 | |
| 125 | for (const utxo of utxos) { |
| 126 | if (total - fee - minChangeAmount > amount) break; |
| 127 | total += utxo.satoshis; |
| 128 | targetUtxos.push(utxo); |
| 129 | } |
| 130 | |
| 131 | const changeAmount = total - amount - fee; |
| 132 | |
| 133 | if (changeAmount < minChangeAmount) { |
| 134 | throw new Error('Not enough funds to cover transaction'); |
| 135 | } |
| 136 | |
| 137 | return { |
| 138 | utxos: targetUtxos, |
| 139 | total, |
| 140 | changeAmount, |
| 141 | }; |
| 142 | } |
no outgoing calls
no test coverage detected