import { Web3 } from "web3";

const NODE_URL =
	"YOUR_WEBSOCKET_USER";
const web3 = new Web3(NODE_URL);

const walletAddress = "ADDRESS_TO_MONITOR";

async function subscribeToNewBlocks() {
  try {
    const event = "newBlockHeaders";
    const subscription = await web3.eth.subscribe(event);

    console.log(`Connected to ${event}, Subscription ID: ${subscription.id}`);

    // Attach an event listener to the subscription object for 'data'
    subscription.on("data", handleNewBlock);
    subscription.on("error", handleError);
  } catch (error) {
    console.error(`Error subscribing to new blocks: ${error}`);
  }
}

async function handleNewBlock(blockHeader) {
  console.log(`Scanning for transactions to: ${walletAddress}`)
  //console.log("New block header:", blockHeader);

  // Fetch the full block data to access transactions
  const block = await web3.eth.getBlock(blockHeader.number, true);

  if (block && block.transactions) {
    block.transactions.forEach((tx) => {
      // Check if the transaction was to MY_ADDRESS
      if (tx.to && tx.to.toLowerCase() === walletAddress.toLowerCase()) {
        console.log("Incoming ETH transfer:", tx.hash);
        console.log(`From: ${tx.from}`);
        console.log(`To: ${walletAddress}`); // To confirm the address monitored
        console.log(`Value: ${web3.utils.fromWei(tx.value, 'ether')} ETH`);
        console.log("--------------------------------");
      }
    });
  }
}

function handleError(error) {
  console.error("Error when subscribing to new block headers:", error);
}

subscribeToNewBlocks();
New subscription: 0x345d18c7ccab37f303282b72638363f3
Got new block: 17793654
Incoming ETH transfer: 0xe339f5353bd5e139a45aa8e42b9c12189d5809ce79a645dccbc2908a70b79421
From: 0xae2Fc483527B8EF99EB5D9B44875F005ba1FaE13
Value: 0.000000000441920915 ETH
--------------------------------
Incoming ETH transfer: 0xcd5488f9985b44c7b4e0de3140c47ade95fa01c9ee5e32adf87d04f1e2afcb27
From: 0xae2Fc483527B8EF99EB5D9B44875F005ba1FaE13
Value: 0.000000000450311043 ETH
--------------------------------
import { Web3 } from "web3";

const NODE_URL =
	"YOUR_WEBSOCKET_USER";
const web3 = new Web3(NODE_URL);

const walletAddress = "ADDRESS_TO_MONITOR";

async function subscribeToNewBlocks() {
  try {
    const event = "newBlockHeaders";
    const subscription = await web3.eth.subscribe(event);

    console.log(`Connected to ${event}, Subscription ID: ${subscription.id}`);

    // Attach an event listener to the subscription object for 'data'
    subscription.on("data", handleNewBlock);
    subscription.on("error", handleError);
  } catch (error) {
    console.error(`Error subscribing to new blocks: ${error}`);
  }
}

async function handleNewBlock(blockHeader) {
  console.log(`Scanning for transactions to: ${walletAddress}`)
  //console.log("New block header:", blockHeader);

  // Fetch the full block data to access transactions
  const block = await web3.eth.getBlock(blockHeader.number, true);

  if (block && block.transactions) {
    block.transactions.forEach((tx) => {
      // Check if the transaction was to MY_ADDRESS
      if (tx.to && tx.to.toLowerCase() === walletAddress.toLowerCase()) {
        console.log("Incoming ETH transfer:", tx.hash);
        console.log(`From: ${tx.from}`);
        console.log(`To: ${walletAddress}`); // To confirm the address monitored
        console.log(`Value: ${web3.utils.fromWei(tx.value, 'ether')} ETH`);
        console.log("--------------------------------");
      }
    });
  }
}

function handleError(error) {
  console.error("Error when subscribing to new block headers:", error);
}

subscribeToNewBlocks();
New subscription: 0x345d18c7ccab37f303282b72638363f3
Got new block: 17793654
Incoming ETH transfer: 0xe339f5353bd5e139a45aa8e42b9c12189d5809ce79a645dccbc2908a70b79421
From: 0xae2Fc483527B8EF99EB5D9B44875F005ba1FaE13
Value: 0.000000000441920915 ETH
--------------------------------
Incoming ETH transfer: 0xcd5488f9985b44c7b4e0de3140c47ade95fa01c9ee5e32adf87d04f1e2afcb27
From: 0xae2Fc483527B8EF99EB5D9B44875F005ba1FaE13
Value: 0.000000000450311043 ETH
--------------------------------