How to subscribe to OwnershipTransferred event for any smart contract [SOLVED]

You’ll have to set your speedy node web socket url in the following HTML page:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Static Template</title>
    <script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
  </head>
  <body>
    <h1>
      This is a static template, there is no bundler or bundling involved!
    </h1>
  </body>
  <script>
    const options = {
      // Enable auto reconnection
      reconnect: {
        auto: true,
        delay: 5000, // ms
        maxAttempts: 5,
        onTimeout: false
      }
    };

    var web3 = new Web3(
      new Web3.providers.WebsocketProvider(
        "wss://speedy-nodes-nyc.moralis.io/zzzzzz/polygon/mainnet/ws",
        options
      )
    );

    function task() {
      let subscription = web3.eth.subscribe("logs", {
      topics:  [Web3.utils.sha3('OwnershipTransferred(address,address)'), ]
      },
      function (
        error,
        result
      ) {
        if (result) {
          console.log(result);
        } else if (error) {
          console.log(error);
        }
      });
    }
    task();
  </script>
</html>
3 Likes

Oops, was supposed to respond here few hours ago :smiley:
This is working very nice!

1 Like