Skip to content

Instantly share code, notes, and snippets.

@thatguyintech
Last active March 11, 2023 11:17
Show Gist options
  • Save thatguyintech/b525ea093cc4d624f61927392db834cf to your computer and use it in GitHub Desktop.
Save thatguyintech/b525ea093cc4d624f61927392db834cf to your computer and use it in GitHub Desktop.

Revisions

  1. ALBERT revised this gist Mar 23, 2022. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions watch-for-mints.js
    Original file line number Diff line number Diff line change
    @@ -16,6 +16,7 @@ const zeroTopic = "0x00000000000000000000000000000000000000000000000000000000000
    const nftContractAddress = "0xbd34d145fcfd3992a0def1057891d51339a90128";

    // Initialize alchemy-web3 object.
    // Docs: https://docs.alchemy.com/alchemy/documentation/subscription-api-websockets
    const web3 = createAlchemyWeb3(
    `wss://eth-rinkeby.ws.alchemyapi.io/ws/${alchemyApiKey}`
    );
  2. ALBERT created this gist Mar 23, 2022.
    33 changes: 33 additions & 0 deletions watch-for-mints.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    // watch-for-mints.js

    // npm install @alch/alchemy-web3
    const { createAlchemyWeb3 } = require("@alch/alchemy-web3");

    // TODO: Replace this with your key.
    const alchemyApiKey = "your api key";

    // NFT mints emit Transfer events with "from" set to 0x0.

    // This is the "transfer event" topic we want to watch.
    const mintTopic = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef";
    // This is the "from address" we want to watch.
    const zeroTopic = "0x0000000000000000000000000000000000000000000000000000000000000000";
    // This is the NFT contract we want to watch.
    const nftContractAddress = "0xbd34d145fcfd3992a0def1057891d51339a90128";

    // Initialize alchemy-web3 object.
    const web3 = createAlchemyWeb3(
    `wss://eth-rinkeby.ws.alchemyapi.io/ws/${alchemyApiKey}`
    );

    // Create the log options object.
    const hackrDaoMintEvents = {
    address: nftContractAddress,
    topics: [mintTopic, zeroTopic]
    }

    // TODO: Add whatever logic you want to run upon mint events.
    const doSomethingWithTxn = (txn) => console.log(txn);

    // Open the websocket and listen for events!
    web3.eth.subscribe("logs", hackrDaoMintEvents).on("data", doSomethingWithTxn);