MCPcopy Create free account
hub / github.com/CashScript/cashscript / gatherUtxos

Function gatherUtxos

packages/cashscript/test/test-util.ts:113–142  ·  view source on GitHub ↗
(
  utxos: Utxo[],
  options?: { amount?: bigint, fee?: bigint },
)

Source from the content-addressed store, hash-verified

111}
112
113export 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}

Callers 5

sendLiveAddUtxoFunction · 0.85
P2PKH.test.tsFile · 0.85
BigInt.test.tsFile · 0.85
HodlVault.test.tsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected