1

How to obtain a list of addresses of 'fresh' wallets?

Hi everyone, how do I go about extracting a list of wallets that had their first transaction within the past 24 hours?

I know that wallets are never 'created', and private key generation can be done offline but what I'm after is a way to pull up a list of wallets that made their first transaction less than 24 hours ago

Thanks everyone!

1 Answer 1

0

Yes, you can certainly do something like that.

First, you have to figure out what was the block mined about 24 hours ago. For this, you can do the following calculation:

We know that a block is mined about every 15 seconds, so about 4 blocks are mined per minute. Then we want to know how many are mined per hour and then how many are mined per day (24 hours). With this simple formula we get our number:

4 * 60 * 24.

Then we get the latest block number and subtract the result of the formula above:

lastestBlockNumber - blockMinedWithin24Hours

With this, we get that block and get all its transaction details, one by one, and check the tx.from address to see how many transactions it has sent. If it has only sent 1 transaction within 24 hours, then we can consider that a fresh account.

Take a look at this simple implementation (remember to replace infuraKey with your own infura Rinkeby key):

const Web3 = require("web3");

const web3 = new Web3("wss://rinkeby.infura.io/ws/v3/<infuraKey>");

/**
 * A new block is mined about every 15 seconds. We can assume that 4 blocks are mined every minute.
 * 4 blocks 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;

console.log("blockMinedWithin24Hours: ", blockMinedWithin24Hours);

async function getFreshAddressesMined24HoursAgo() {
  const latestBlockNumber = await web3.eth.getBlockNumber();

  console.log("latestBlockNumber: ", latestBlockNumber);

  const blockNumberOfBlockMined24HoursAgo =
    latestBlockNumber - blockMinedWithin24Hours;

  console.log(
    "blockNumberOfBlockMined24HoursAgo: ",
    blockNumberOfBlockMined24HoursAgo
  );

  const block = await web3.eth.getBlock(blockNumberOfBlockMined24HoursAgo);

  const freshAddresses = [];

  for (let i = 0; i < block.transactions.length; i++) {
    const txHash = block.transactions[i];
    const tx = await web3.eth.getTransaction(txHash);

    const txCount = await web3.eth.getTransactionCount(tx.from);

    console.log("-----------------------------");
    console.log("tx.from: ", tx.from);
    console.log("txCount: ", txCount);
    console.log("-----------------------------");

    // If this address has only made one transaction, then it's a fresh address.
    if (txCount === 1) {
      freshAddresses.push(tx.from);
    }
  }

  return freshAddresses;
}

getFreshAddressesMined24HoursAgo()
  .then((freshAddresses) => {
    console.log("freshAddresses: ", freshAddresses);
  })
  .catch((error) => {
    console.log("Error: ", error);
  });

The following version uses caching to avoid processing the same address:

const Web3 = require("web3");

const web3 = new Web3(
  "wss://rinkeby.infura.io/ws/v3/<infuraKey>"
);

/**
 * 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();

  console.log("latestBlockNumber: ", latestBlockNumber);

  const blockNumberOfBlockMined24HoursAgo =
    latestBlockNumber - blockMinedWithin24Hours;

  console.log(
    "blockNumberOfBlockMined24HoursAgo: ",
    blockNumberOfBlockMined24HoursAgo
  );

  const block = await web3.eth.getBlock(blockNumberOfBlockMined24HoursAgo);

  const freshAddresses = [];

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

    console.log("-----------------------------");
    console.log("tx.from: ", tx.from);
    console.log("txCount: ", txCount);
    console.log("-----------------------------");

    // If this address has only made one transaction, then it's a fresh address.
    if (txCount === 1) {
      freshAddresses.push(tx.from);
    }
  }

  return freshAddresses;
}

getFreshAddressesMined24HoursAgo()
  .then((freshAddresses) => {
    console.log("freshAddresses: ", freshAddresses);
  })
  .catch((error) => {
    console.log("Error: ", error);
  });

You could change this approach so you check each block from blockNumberOfBlockMined24HoursAgo block to the lastest block, checking all the transactions in the blocks in between that range or do some more interesting logic.

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.