Skip to content

Commit

Permalink
chore: check confirmed utxo contains ordinals
Browse files Browse the repository at this point in the history
  • Loading branch information
slavastartsev committed Dec 19, 2024
1 parent 08aff7d commit b5922db
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
28 changes: 17 additions & 11 deletions sdk/src/wallet/utxo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,20 @@ 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 }));
// mark node as containing ordinals
acc.push(new TreeNode<OutputNodeData>({ ...utxo, cardinal: false }));
else acc.push(null);

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

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

while (queue.length > 0) {
Expand All @@ -53,21 +58,22 @@ const processNodes = async (rootNodes: (TreeNode<OutputNodeData> | null)[], espl
if (txInscriptions.length === 0) {
const transaction = await esploraClient.getTransaction(childNode.val.txid);

// if not confirmed check inputs for current utxo
if (!transaction.status.confirmed) {
if (transaction.status.confirmed) {
// if confirmed check if it contains ordinals
childNode.val.cardinal = cardinalOutputsSet.has(OutPoint.toString(childNode.val));
} else {
// if not confirmed check inputs for current utxo
childNode.children = transaction.vin.map((vin) => {
return new TreeNode<OutputNodeData>({
vout: vin.vout,
txid: vin.txid,
cardinal: true,
// mark node as containing ordinals
cardinal: false,
});
});

queue.push(...childNode.children);
}
} else {
// mark node as containing ordinals
childNode.val.cardinal = false;
}
}
};
Expand Down Expand Up @@ -173,7 +179,7 @@ export async function createBitcoinPsbt(

const rootUtxoNodes = createUtxoNodes(utxos, cardinalOutputsSet);

await processNodes(rootUtxoNodes, esploraClient);
await processNodes(rootUtxoNodes, cardinalOutputsSet, esploraClient);

// To construct the spending transaction and estimate the fee, we need the transactions for the UTXOs
const possibleInputs: Input[] = [];
Expand Down Expand Up @@ -389,7 +395,7 @@ export async function estimateTxFee(

const rootUtxoNodes = createUtxoNodes(utxos, cardinalOutputsSet);

await processNodes(rootUtxoNodes, esploraClient);
await processNodes(rootUtxoNodes, cardinalOutputsSet, esploraClient);

const possibleInputs: Input[] = [];

Expand Down Expand Up @@ -505,7 +511,7 @@ export async function getBalance(address?: string) {

const rootUtxoNodes = createUtxoNodes(utxos, cardinalOutputsSet);

await processNodes(rootUtxoNodes, esploraClient);
await processNodes(rootUtxoNodes, cardinalOutputsSet, esploraClient);

const total = utxos.reduce((acc, utxo, index) => {
// there will be a match if output is confirmed and has no ordinals
Expand Down
4 changes: 2 additions & 2 deletions sdk/test/utxo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ describe('UTXO Tests', () => {
})
)
)
).not.toEqual([]);
).toEqual([]);
});

it('throws an error if insufficient balance', { timeout: 50000 }, async () => {
Expand Down Expand Up @@ -471,7 +471,7 @@ describe('UTXO Tests', () => {

const balanceData = await getBalance(taprootAddress);

expect(balanceData.total).toEqual(BigInt(total));
expect(balanceData.total).toBeLessThan(BigInt(total));
expect(balanceData.confirmed).toBeLessThan(BigInt(confirmed));
});
});

0 comments on commit b5922db

Please sign in to comment.