I replied to your other questions. And yes, the solution I posted guarantees that you will get the wallet/address that has made exactly 1 transaction in the last 24 hours.
The web3.eth.getTransactionCount
function counts all the transactions an address has ever sent, not at that block, but ever, even for more recent transactions. So, if, for example, we are checking the last 10 blocks first and we see the transaction that made its first transaction at that block, but it has also made another transaction in a more recent block, the web3.eth.getTransactionCount
will show more than 1 transaction and it will be left out, which is what we want.
Do you need to get all addresses in the Ethereum blockchain that have ever made only 1 transaction in the last 24 hours? The solution I proposed in the other answer is the correct one. Also, I only checked the block mined about 24 hours ago. To check all the addresses from the block mined 24 hours ago to the current block, you would need to loop from that block mined 24 hours ago to the current block, going block by block in between them and checking all transactions and checking the web3.eth.getTransactionCount
of each tx.from
address.
For example, this would be a full solution (probably slow because it will be checking about 5000 blocks and for each block it will check all its transactions. I'm not sure if Infura will let you make so many requests):
Remember to replace <yourInfuraKey>
with your own infura key.
const Web3 = require("web3");
const web3 = new Web3(
"wss://rinkeby.infura.io/ws/v3/72033898e71b4d2e9874b5a9c12d1cb6"
);
/**
* A new block is mined about every 15 seconds. We can assume that 4 blocks are mined every minute.
* 4 block multiplied by 60 minutes yields the number of blocks mined in an hour (240).
* 240 multiplied by 24 hours yields the number of blocks mined in the last 24 hours (5760).
* We can use this as an approximation to know what block was mined 24 hours ago and sync.
*/
const blockMinedWithin24Hours = 4 * 60 * 24;
const alreadyProcessed = {};
console.log("blockMinedWithin24Hours: ", blockMinedWithin24Hours);
async function getFreshAddressesMined24HoursAgo() {
const latestBlockNumber = await web3.eth.getBlockNumber();
const freshAddresses = [];
let targetBlockNumber = latestBlockNumber - blockMinedWithin24Hours;
while (targetBlockNumber < latestBlockNumber) {
const block = await web3.eth.getBlock(targetBlockNumber);
console.log("Processing block.number: ", block.number);
console.log("Renaming blocks: ", latestBlockNumber - targetBlockNumber);
for (let i = 0; i < block.transactions.length; i++) {
const txHash = block.transactions[i];
const tx = await web3.eth.getTransaction(txHash);
// Caching the tx.from address to avoid processing it multiple times it case it has sent more transactions.
if (alreadyProcessed[tx.from]) {
continue;
}
const txCount = await web3.eth.getTransactionCount(tx.from);
// If this address has only made one transaction, then it's a fresh address.
if (txCount === 1) {
const record = {
address: tx.from,
firstTransactionAtBlock: block.number,
};
freshAddresses.push(record);
console.log("-----------------------");
console.log("record: ", record);
console.log("-----------------------");
}
}
targetBlockNumber++;
}
return freshAddresses;
}
getFreshAddressesMined24HoursAgo()
.then((freshAddresses) => {
console.log("freshAddresses: ", freshAddresses);
})
.catch((error) => {
console.log("Error: ", error);
});