2

I know I can track events for smart contracts, such as Transfer and Approval for ERC20 and ERC721 or even ApprovalForAll for ERC721.

I also know that I can track my own defined events for any smart contract I deploy.

But is there a way to track an NFT sale in, let's say, OpenSea?

I was hoping that if I know the address of the OpenSea escrow smart contract (or any other marketplace), it is possible for me to know:

  1. When a user transfers his/her token to it (using the Transfer event - which would mean "publishing a sale")

  2. Immediately after, that an effective sale occurred, if there's another Transfer event from that escrow marketplace to an address different to the original

Does this make sense or am I just not seeing something?

3
  • There is a way to track the transactions in real-time using streams API provided by Moralis. If you are ok with using API services for this I can share an example on how to do it.
    – JohnVersus
    Mar 18 at 7:07
  • Yes, I am ok using APIs... Mar 18 at 7:18
  • I added the answer with js code. Hope that helps
    – JohnVersus
    Mar 18 at 8:39

1 Answer 1

1

Here is an example code on how you can track NFT transfer events on a smart contract using Moralis streams API.

import Moralis from "moralis";

const addStream = async () => {
  if (!Moralis.Core.isStarted) {
    await Moralis.start({
      apiKey: process.env.API_KEY,
    });
  }

  const options = {
    webhookUrl: "...", // Stream data will be sent to this webhook url
    description: "NFTTransfers",
    tag: "transfers",
    topic0: ["Transfer(address,address,uint256)"], // topic0 of your tracking
    allAddresses: false,
    includeNativeTxs: false,
    includeContractLogs: true,
    includeInternalTxs: false,
    getNativeBalances: [],
    triggers: [],
    abi: [
      {
        // ... Paste ABI as value
      },
    ],
    advancedOptions: null,
    chainIds: ["0x1", "0x5"], // chains on which you want to track events
  };

  const stream = await Moralis.Streams.add(options);

  // Attach the contract address to the stream
  await Moralis.Streams.addAddress({
    id: stream.raw.id,
    address: "0x..", //contract address on which you want to listen to streams to
  });
};
addStream();

The data received from the streams will have logs from the NFT transactions using which you can verify the marketplace address.

p.s. I work at Moralis

1
  • This example uses the js sdk of Moralis which is a wrapper for its API.
    – JohnVersus
    Mar 18 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.