In The Neighborhood, our goal is to meet the data where it’s at (in its raw form) and you wherever you might find yourself (with a db, webhook, etc).

Transformations are how we bridge that gap. These are simple (or complex) JavaScript functions that run throughout the distributed network. We call this Just In Time Indexing.

Here’s a simple example. This function simply takes in a new EVM block and returns its number and an ISO formatted timestamp.

function allEVMBlocks(block) {
  return {
    number: block.number,
    timestamp: new Date(block.timestamp * 1000).toISOString(),
  };
}

Here’s a more complex example counting the number of times each Solana account is found in a block:

function solanaAccountCount(block) {
  const accountCounts = {};

  for (const tx of block.transactions) {
    if (tx.meta.err) continue;

    const allAccounts = tx.transaction.message.accountKeys
      .concat(tx.meta.loadedAddresses.writable)
      .concat(tx.meta.loadedAddresses.readonly);

    for (const acc of allAccounts) {
      if (!accountCounts[acc]) {
        accountCounts[acc] = 0;
      }
    }

    const allInstructions = tx.transaction.message.instructions.concat(
      tx.meta.innerInstructions.map((ii) => ii.instructions).flat()
    );
    for (const inst of allInstructions) {
      for (const accIdx of inst.accounts) {
        accountCounts[allAccounts[accIdx]] += 1;
      }
    }
  }

  return Object.entries(accountCounts).map(([account, count]) => ({ account, count }));
}