1

What would be involved in watching a few specific contract addresses for Transfer event on Ethereum or Polygon?

Moralis Streams API is what I'm using now but it can get pricey with the number of daily computes and records.

1 Answer 1

1

Hi Developer advocate from Chainstack here.

At the lowest code implementation level, you can use Ethereum's subscription API, in your particular case, you can subscribe to the logs.

Below is a sample code you can try out. This code subscribes to USDT's transfer event. It is modified from this tutorial.

const Web3 = require("web3");
//visit Chainstack doc for more information:
//https://support.chainstack.com/hc/en-us/articles/4403518123161-Subscribing-to-smart-contract-events-with-web3-js?utm_term=&utm_campaign=co_search&utm_source=adwords&utm_medium=ppc&hsa_acc=7102112062&hsa_cam=17317323485&hsa_grp=142306614157&hsa_ad=627441674301&hsa_src=g&hsa_tgt=dsa-19959388920&hsa_kw=&hsa_mt=&hsa_net=adwords&hsa_ver=3&gclid=CjwKCAiAzp6eBhByEiwA_gGq5GUtO3UTrbzInqsITVNlC7Ov-GKZR1MH47BBkw5zqUA6he2BacqE0RoCn6MQAvD_BwE

// ws url
const ws_url = "wss://ws-nd-610-406-547.fill-in-your-ws-endpoint-here.com/fill-in-your-ws-endpoint-here"
const web3 = new Web3(ws_url)

// subscribe to logs
// for SushiSwap: MasterChef LP Staking Pool withdraw events on mainnet
var logs = web3.eth
    .subscribe(
        "logs", {
            //SushiSwap: MasterChef LP Staking Pool
            address: "0xdac17f958d2ee523a2206206994597c13d831ec7",
            topics: [
                //Transfer (index_topic_1 address from, index_topic_2 address to, uint256 value)
                "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
            ]
        },
        function(error, result) {
            if (!error) console.log(result);
        }
    )
    .on("connected", function(subscriptionId) {
        console.log("sub id:" + subscriptionId);
    })
    .on("data", function(log) {
        console.log(log);
    })
    .on("changed", function(log) {});

//keep this code alive
setInterval(() => {}, 1 << 30);

The WebSocket endpoint in this sample is just a dummy address, you will need to change that. If you need one, feel free to try our service, the developer plan is free and it comes with 3 million free requests.

Hope this helps.

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.