mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-05 08:23:35 +00:00
Coin selector and custody badge
This commit is contained in:
1 parent
015e7b8b90
commit
e3cebf3a34
5 files changed
+308
-37
No files matched your search
@@ -1,5 +1,68 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { fetchUTXOs, type UTXO } from '@/lib/bitcoin';
|
||||
import type { BitcoinTransaction } from '@/components/wallet/TransactionHistory';
|
||||
|
||||
/**
|
||||
* Fetch Bitcoin transaction history for an address from Blockstream API
|
||||
*/
|
||||
async function fetchBitcoinTransactions(address: string): Promise<BitcoinTransaction[]> {
|
||||
const response = await fetch(`https://blockstream.info/api/address/${address}/txs`);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to fetch Bitcoin transactions');
|
||||
}
|
||||
|
||||
const txs = await response.json();
|
||||
|
||||
// Transform Blockstream API response to our BitcoinTransaction format
|
||||
return txs.map((tx: any, index: number) => {
|
||||
// Determine if this is a sent or received transaction
|
||||
// For simplicity, we'll consider it received if the address appears in outputs
|
||||
// and sent if it appears in inputs
|
||||
const isReceived = tx.vout.some((output: any) =>
|
||||
output.scriptpubkey_address === address
|
||||
);
|
||||
const isSent = tx.vin.some((input: any) =>
|
||||
input.prevout?.scriptpubkey_address === address
|
||||
);
|
||||
|
||||
// Calculate amount for this address
|
||||
let amount = 0;
|
||||
let fee = 0;
|
||||
|
||||
if (isReceived) {
|
||||
// Sum all outputs to this address
|
||||
amount = tx.vout
|
||||
.filter((output: any) => output.scriptpubkey_address === address)
|
||||
.reduce((sum: number, output: any) => sum + output.value, 0);
|
||||
} else if (isSent) {
|
||||
// Sum all inputs from this address minus outputs to this address
|
||||
const inputsFromAddress = tx.vin
|
||||
.filter((input: any) => input.prevout?.scriptpubkey_address === address)
|
||||
.reduce((sum: number, input: any) => sum + input.prevout.value, 0);
|
||||
|
||||
const outputsToAddress = tx.vout
|
||||
.filter((output: any) => output.scriptpubkey_address === address)
|
||||
.reduce((sum: number, output: any) => sum + output.value, 0);
|
||||
|
||||
amount = inputsFromAddress - outputsToAddress;
|
||||
fee = tx.fee || 0;
|
||||
}
|
||||
|
||||
return {
|
||||
id: `${tx.txid}-${index}`,
|
||||
txid: tx.txid,
|
||||
type: isReceived ? 'received' : 'sent',
|
||||
amount: Math.abs(amount),
|
||||
fee: isSent ? fee : undefined,
|
||||
address: address,
|
||||
confirmations: tx.status.confirmed ? (tx.status.block_height ? 1 : 0) : 0, // Simplified
|
||||
blockHeight: tx.status.block_height,
|
||||
timestamp: tx.status.block_time * 1000, // Convert to milliseconds
|
||||
status: tx.status.confirmed ? 'confirmed' : 'unconfirmed'
|
||||
} as BitcoinTransaction;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook to fetch and manage Bitcoin balance from UTXOs
|
||||
@@ -22,3 +85,22 @@ export function useBitcoinBalance(address: string | null) {
|
||||
staleTime: 10000, // Consider data stale after 10 seconds
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook to fetch and manage Bitcoin transaction history
|
||||
*/
|
||||
export function useBitcoinTransactionHistory(address: string | null) {
|
||||
return useQuery({
|
||||
queryKey: ['bitcoin-transactions', address],
|
||||
queryFn: async (): Promise<BitcoinTransaction[]> => {
|
||||
if (!address) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return fetchBitcoinTransactions(address);
|
||||
},
|
||||
enabled: !!address,
|
||||
refetchInterval: 30000, // Refetch every 30 seconds
|
||||
staleTime: 10000, // Consider data stale after 10 seconds
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user