dave on Nostr: function fetchCoinjoins() { // Fetch all TXIDs in the mempool const url = ...
function fetchCoinjoins() {
// Fetch all TXIDs in the mempool
const url = 'https://blockstream.info/api/mempool';;
fetch(url)
.then(res => res.json())
.then(data => {
const txids = data.txids;
// Iterate over every TXID and fetch the full transaction data
txids.forEach(txid => {
const txurl = `https://blockstream.info/api/tx/${txid}`;
fetch(txurl)
.then(res => res.json())
.then(txdata => {
// Determine if the transaction is likely a coinjoin
if (isLikelyCoinjoin(txdata)) {
console.log(txdata);
}
})
})
});
}
function isLikelyCoinjoin(txdata) {
// Check number of inputs/outputs
const numInputs = txdata.vin.length;
const numOutputs = txdata.vout.length;
if (numInputs > 1 && numOutputs > 2) {
// Check outputs have close to the same value
let outputValues = txdata.vout.map(o => o.value);
outputValues.sort((a, b) => a - b);
let difference = outputValues[numOutputs - 1] - outputValues[0];
if (difference < 0.001) {
return true;
}
}
return false;
}
// Fetch all TXIDs in the mempool
const url = 'https://blockstream.info/api/mempool';;
fetch(url)
.then(res => res.json())
.then(data => {
const txids = data.txids;
// Iterate over every TXID and fetch the full transaction data
txids.forEach(txid => {
const txurl = `https://blockstream.info/api/tx/${txid}`;
fetch(txurl)
.then(res => res.json())
.then(txdata => {
// Determine if the transaction is likely a coinjoin
if (isLikelyCoinjoin(txdata)) {
console.log(txdata);
}
})
})
});
}
function isLikelyCoinjoin(txdata) {
// Check number of inputs/outputs
const numInputs = txdata.vin.length;
const numOutputs = txdata.vout.length;
if (numInputs > 1 && numOutputs > 2) {
// Check outputs have close to the same value
let outputValues = txdata.vout.map(o => o.value);
outputValues.sort((a, b) => a - b);
let difference = outputValues[numOutputs - 1] - outputValues[0];
if (difference < 0.001) {
return true;
}
}
return false;
}