Skip to content

Commit

Permalink
chore: extract crate utxo nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
slavastartsev committed Dec 13, 2024
1 parent b9a8ce8 commit 6de85a7
Showing 1 changed file with 15 additions and 30 deletions.
45 changes: 15 additions & 30 deletions sdk/src/wallet/utxo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ class TreeNode<T> {
}
}

const createUtxoNodes = (utxos: UTXO[], cardinalOutputsSet: Set<string>) =>
utxos.reduce(
(acc, utxo) => {
if (!cardinalOutputsSet.has(OutPoint.toString(utxo)))
acc.push(new TreeNode<OutputNodeData>({ ...utxo, cardinal: true }));
else acc.push(null);

return acc;
},
[] as (TreeNode<OutputNodeData> | null)[]
);

const processNodes = async (rootNodes: (TreeNode<OutputNodeData> | null)[], esploraClient: EsploraClient) => {
const queue = Array.from(rootNodes);

Expand Down Expand Up @@ -159,16 +171,7 @@ export async function createBitcoinPsbt(

const cardinalOutputsSet = new Set(cardinalOutputs.map((output) => output.outpoint));

const rootUtxoNodes = utxos.reduce(
(acc, utxo) => {
if (!cardinalOutputsSet.has(OutPoint.toString(utxo)))
acc.push(new TreeNode<OutputNodeData>({ ...utxo, cardinal: true }));
else acc.push(null);

return acc;
},
[] as (TreeNode<OutputNodeData> | null)[]
);
const rootUtxoNodes = createUtxoNodes(utxos, cardinalOutputsSet);

await processNodes(rootUtxoNodes, esploraClient);

Expand Down Expand Up @@ -384,16 +387,7 @@ export async function estimateTxFee(

const cardinalOutputsSet = new Set(cardinalOutputs.map((output) => output.outpoint));

const rootUtxoNodes = utxos.reduce(
(acc, utxo) => {
if (!cardinalOutputsSet.has(OutPoint.toString(utxo)))
acc.push(new TreeNode<OutputNodeData>({ ...utxo, cardinal: true }));
else acc.push(null);

return acc;
},
[] as (TreeNode<OutputNodeData> | null)[]
);
const rootUtxoNodes = createUtxoNodes(utxos, cardinalOutputsSet);

await processNodes(rootUtxoNodes, esploraClient);

Expand Down Expand Up @@ -509,16 +503,7 @@ export async function getBalance(address?: string) {

const cardinalOutputsSet = new Set(cardinalOutputs.map((output) => output.outpoint));

const rootUtxoNodes = utxos.reduce(
(acc, utxo) => {
if (!cardinalOutputsSet.has(OutPoint.toString(utxo)))
acc.push(new TreeNode<OutputNodeData>({ ...utxo, cardinal: true }));
else acc.push(null);

return acc;
},
[] as (TreeNode<OutputNodeData> | null)[]
);
const rootUtxoNodes = createUtxoNodes(utxos, cardinalOutputsSet);

await processNodes(rootUtxoNodes, esploraClient);

Expand Down

0 comments on commit 6de85a7

Please sign in to comment.