Need help understanding Bitcoin DeFi?
→ START HERE
Need help understanding Bitcoin DeFi?
→ START HERE
Need help understanding Bitcoin DeFi?
→ START HERE
Need help understanding Bitcoin DeFi?
→ START HERE
Need help understanding Bitcoin DeFi?
→ START HERE

Breaking Down NFT Code Snippets in Clarity

Stacks NFTs are skyrocketing in popularity. In the past year, we’ve seen over hundreds of thousands of NFTs minted on Stacks with over 4M STX in total trading volume. Let’s take a look at some of the things developers have been building for Bitcoin NFTs.

Type
Tutorial
Topic(s)
NFTs
Published
March 23, 2022
Author(s)
Community Manager
Breaking Down NFT Code Snippets in Clarity
Contents

To bring NFTs to Bitcoin, developers use Clarity, a powerful programming language designed specifically for the creation of Bitcoin smart contracts. Clarity itself is a relatively new programming language. It was created in 2020, but was just recently recognized as an official programming language on GitHub.

As developers have gone from tinkering to launching products, we’re beginning to see common programming patterns and useful reusable code snippets start to emerge from various projects in the ecosystem.

Let’s take a look at a few of those snippets that are surfacing for NFTs.

Boom Sets the Metadata for Their NFT

Boom is one of the earliest NFT projects on Stacks. Boom is a wallet that can manage all of a user’s assets on Stacks, including NFTs, and Boom can also help users create their own custom NFTs too. One of the more interesting NFT applications that Boom has developed is their creation of “boomboxes”, which are NFTs that act as a certificate of redemption for pooled Stacking rewards.

Let’s pop the hood, take a look at some of Boom’s Clarity code, and investigate the functionality being performed.

A line of code

This single line of code lets the Stacks network know that this smart contract adheres to the requirements for non-fungible assets as defined in the NFT standard, SIP-009. It provides the address of another contract, already live on the blockchain, that serves as a template (referred to as a “trait”) that other contracts can extend.

The code defining NFT traits

By implementing the SIP-009 NFT trait, the boomboxes contract ensures interoperability, enabling a default set of NFT operations such as identifying the owner (get-owner function) or transferring an asset (transfer function).

The code adding metadata

This two-line function is another simple but powerful code snippet that adds metadata to the non-fungible asset. It includes a link to the image used for the specific Boombox NFT, which is hosted by the company itself, as you can see in the provided URI: https://boom.money/images/boom10.png. This designates a visual accompaniment to the NFT certificate, allowing the NFT to function as a piece of digital artwork. It’s also awesome that Boom is hosting the image on their own website: breaking content free from AWS or Imgur is another step towards web decentralization!

There are loads more valuable snippets throughout the Clarity contract. You can see Boom’s contract in full on GitHub here.

Bulk Transfer Your NFTs

Some of you avid NFT collectors might be amassing quite the large collection by now. Well now there’s a script out there that could help save hours of painstakingly transferring virtual monkeys one at a time. 

Megapont is the creator of the premier NFT project on Stacks: the Megapont Apes. To date, the NFT collection has generated over 3M STX in trading volume (~75% of the NFT market volume on Stacks).

Check out this compartmentalized utility contract that popped up in the Stacks explorer. It performs two basic functions that could be extremely valuable to NFT collectors.

Code that enables bulk transfer

As you can see in this snippet, there are two functions in this code. The first function identifies all the NFT IDs that a particular Stacks address owns, in this case NFTs from the Megapont Ape collection. The second function transfers all of them, one by one, to a different wallet. 

Someone even uploaded a similar contract, but instead of transferring Megapont Apes, they transferred another Stacks NFT collection, Stacks MFers.

Another sample of the code

The most significant change between the two snippets is the replacement of the address stipulating which NFT collection to transfer. In the first snippet, it’s checking for NFTs minted from the megapont-ape-club-nft contract. In the second snippet, it’s checking for NFTs minted from the stacks-mfers contract. 

You can view the bulk transfer contract for both NFT collections on the explorer here and the Stacks Mfers contract here.

Activism On-Chain

The geopolitical landscape is quite chaotic at the moment to say the least, and one developer decided to leverage technology to have a positive impact. 

The war-is-devastating Clarity contract is an interesting implementation of a charity fundraiser using NFTs. It’s a non-fungible collection that conveys different styles of art, dedicated to raising awareness against war and violence. In an effort to give back, the proceeds of this collection are reserved for future projects with the goal of a better future for humankind.

In the spirit of decentralization, all of the images are hosted on IPFS (InterPlanetary File System). IPFS is a peer-to-peer hypermedia protocol designed to preserve and grow humanity's knowledge by making the web upgradeable, resilient, and more open.

The contract code for war-is-devastating

Instead of linking directly to the image on IPFS, this links to a JSON file which holds an expanded collection of metadata on top of the mere graphic. This allows the developer to include more details like a description, attributes, price, and more.

More of the contract code

You can view the contract yourself on the Stacks Explorer here.

Conclusion

It’s exciting to see all of the new code being released by developers throughout the ecosystem. I can’t wait to see what developers build throughout the rest of this year.

Discover more NFT innovation on Stacks and learn how Byzantion built an NFT order book and how Gamma built the Create portal for NFT artists.

If you’re inspired by these snippets and ready to start building, download our free guide to developing smart contracts to kickstart your journey into Web3.

Download the Guide
Copy link
Mailbox
Hiro news & product updates straight to your inbox
Only relevant communications. We promise we won’t spam.

Related stories