1

Is there a way to get wallets that made their first ever transaction less than 24 hours ago?

I'm looking for a way to screen for all addresses that have made their first ever transaction within the last 24 hours. Is there a way to do this? For example, if I have two wallets, one 100 days old and one 20 days old but both made their first transaction today, they should show up.

I know I'm asking a question similar to this, but I dont have the 50 rep needed to comment: How to obtain a list of addresses of 'fresh' wallets?.

The answer to the similar question above seems to only filter for addresses with 1 txn in the last 24 hours, but that doesn't necessarily mean it is the FIRST ever transaction for the account.

Thanks everybody!

2 Answers 2

0

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);
  });

4
  • 1
    this is awesome! thanks for the clarification. this makes sense :)
    – trademarc
    Commented Sep 8, 2022 at 1:48
  • actually after implementing the code more, I realised that the solution only gives addresses that have made 1 transaction within the last 24hours. I.e. it misses addresses that have made > 1. However, I'm looking for fresh wallets i.e. FIRST transaction within the last 24hours, but within that 24hours can make as many transactions as they want. If I'm right, passing in targetBlockNumber into web3.eth.getTransaction( ) should fix this :)
    – trademarc
    Commented Sep 8, 2022 at 4:56
  • Gatcha! In that case, you need to check that the tx.nonce === 0, if so, then that is the first transaction ever that tx.from did within 24 hours. ` if (tx.nonce == 0) { console.log("First transaction of address: ", tx.from); } ` Commented Sep 8, 2022 at 5:03
  • Yeap thats a much better way! Just another curious question - checking for tx.nonce === 0 works for getting the first tx.from transaction from an account. But is there a way to find out the first tx.to to an account?
    – trademarc
    Commented Sep 8, 2022 at 5:59
0

You need to filter that list by checking for account nonce using https://web3js.readthedocs.io/en/v1.2.11/web3-eth.html#gettransactioncount

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.