Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html>
<head>
    <meta charset="utf-8" />
    <title>Get all NFTs in the NFT collection</title>
    <script type="module" src="./app.js"></script>
</head>

<body>
  <div id='form'>
    <h2>Your NFT Marketplace </h2>
        <h4>Enter any collection address: </h4>
    <input type="text" id="address" placeholder="Enter collection address" value='0x524cab2ec69124574082676e6f654a18df49a048'/>
    <button id="check-balance">Load Collection</button>
  </div>
  <div id="total-nfts"></div>
  <div id="balance-container">
  </div>
</body>

</html>
              
            
!

CSS

              
                body {
  font-family: Poppins, sans-serif;
  font-size: 35 px;
  text-align: center;
  position: relative;
}

h4 {
  font-size: 16px;
}

body::before {
  content: "";
  position: absolute;
  top: -30px;
  right: 0;
  bottom: 0;
  left: 0;
  background-image: url('https://uploads-ssl.webflow.com/62624e283b503f3e68275638/65f483b79bca9e794461836d_NFT%20Market%20Place%20Background%20(1).png');
  min-width: 1200px;
  width: 100%;
  background-size: contain;
  background-position: top;
  background-repeat: no-repeat;
  opacity: 0.1;
  z-index: -1;
}

input[type="text"] {
  width: 20%;
  padding: 13px 10px;
  font-size: 12px;
  border-radius: 5px;
  border: 1px solid #e4e4e4;
  margin-bottom: 30px;
}

#check-balance {
  font-family: Poppins, sans-serif;
  padding: 10px 20px;
  font-size: 12px;
  border-radius: 5px;
  border: 1px solid transparent;
  background-color: #4237ff;
  color: white;
  cursor: pointer;
  transition: background-color 0.3s, color 0.3s, border-color 0.3s;
}

#check-balance:hover {
  background-color: white;
  color: #4237ff;
  border-color: #4237ff;
}

#balance-container {
  margin-top: 20px;
  font-size: 16px;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

#balance-container div {
  flex: 0 0 24%;
  margin: 0.4em;
  box-sizing: border-box;
}

#balance-container img {
  width: 100%;
  height: auto;
}

              
            
!

JS

              
                import { TatumSDK, Network, Ethereum } from "https://esm.sh/@tatumio/tatum";

document.addEventListener("DOMContentLoaded", async () => {
  const tatum = await TatumSDK.init<Ethereum>({ network: Network.ETHEREUM });
  const addressInput = document.getElementById('address');
  const balanceContainer = document.getElementById('balance-container');
  const totalNFTs = document.getElementById('total-nfts');

  // Function to fetch and display NFTs
  const fetchAndDisplayNFTs = async () => {
    try {
      // Fetch NFTs in the collection
      const balance = await tatum.nft.getNftsInCollection({
        collectionAddress: [addressInput.value],
      });

      // Display the total number of NFTs found
      totalNFTs.textContent = `Total Number of NFTs Found: ${balance.data.length}`;

      // Sort the array so NFTs without metadata images are at the end
      const sortedBalanceData = balance.data.sort((a, b) => (b.metadata && b.metadata.image) ? 1 : -1);

      // Remove previous entries
      balanceContainer.innerHTML = '';

      // Create a new entry for each NFT
      sortedBalanceData.forEach(nft => {
        const entry = document.createElement('div');
        const image = document.createElement('img');
        const name = document.createElement('p');
        
        // Create a new Image object
        const imgObj = new Image();

        // Set a loading image
        image.src = 'https://loading.io/spinners/dual-ring/lg.dual-ring-loader.gif'; // Use any loading image or gif you prefer

        imgObj.onload = function() {
          // Once the image is loaded, replace the source of the image element
          image.src = imgObj.src;
        }

        imgObj.onerror = function() {
          // If there is an error loading the image, replace the source with the unavailable image
          image.src = 'https://user-images.githubusercontent.com/10515204/56117400-9a911800-5f85-11e9-878b-3f998609a6c8.jpg';
        }

        // If there is a metadata image, load the image
        if (nft.metadata && nft.metadata.image) {
          imgObj.src = nft.metadata.image;
        } else {
          // If there is no metadata image, set the image as not available
          image.src = 'https://user-images.githubusercontent.com/10515204/56117400-9a911800-5f85-11e9-878b-3f998609a6c8.jpg';
        }

        // If there is a metadata name, use it. Otherwise, use 'Name not given'
        name.textContent = nft.metadata && nft.metadata.name ? nft.metadata.name : 'Name not given';
        
        entry.appendChild(image);
        entry.appendChild(name);
        balanceContainer.appendChild(entry);
      });
    } catch (error) {
      console.error("Error fetching NFTs:", error);
      // Handle errors here
    }
  };

  // Initial call to fetch and display NFTs
  fetchAndDisplayNFTs();

  // Add event listener to the button
  const button = document.getElementById("check-balance");
  button.addEventListener("click", fetchAndDisplayNFTs);
});

              
            
!
999px

Console