Skip to main content

How To Mint A Music NFT on BNB Smart Chain

Updated on
Dec 11, 2023

6 min read

Overview

BNB Smart Chain provides a fast and affordable option for creators to bring music on-chain. Additionally, BSC allows you to use the ERC721 standard specification, so if you have worked with NFTs on Ethereum before, this should be familiar. This process is similar to creating an image NFT; the only difference is the file type. You can learn more about the ERC721 token standard here.

What You Will Do

In this guide, you will create and deploy a piece of music as an ERC721 NFT to the BSC Testnet using Remix and MetaMask.

  • You will upload your music file to a decentralized storage system (IFPS)
  • You will create and upload metadata for your NFT.
  • You will learn how to create, compile, and deploy a smart contract in Remix.

What You Will Need

  • Background knowledge of smart contracts and blockchain will be helpful but is not required.
  • Terminal
  • IFPS CLI
  • Metamask (integrated with BSC Testnet network).
  • BNB Testnet tokens (BNB Smart Chain Faucet)

Connect to BNB Smart Chain Testnet

Set Up Your QuickNode Endpoint

To build on BNB Smart Chain Testnet, you'll need an API endpoint to connect with the network. You're welcome to use public nodes or deploy and manage your own infrastructure; however, if you'd like faster response times, you can leave the heavy lifting to us. Sign up for a free account here.

Copy your HTTP Provider:

Configure Metamask

If you do not already have BNB Smart Chain Testnet in your Metamask, add it with the following settings:

Get Test BNB Tokens

Since we are deploying our smart contract on the BCS Testnet, we need some test BNB tokens to pay gas fees.

Visit BNB Smart Chain Faucet, and paste your BNB wallet address and click "Give Me BNB" to receive 0.5 BNB:

Upload Files to IPFS

Before writing your NFT contract, you must host your music file and create a metadata file. We will use IPFS, a peer-to-peer file storing and sharing distributed system. Download and install IPFS CLI based on your operating system by following the installation guide in IPFS docs.

Upload Song to IPFS

Start the IPFS repo by typing the following in a terminal/cmd window:

ipfs init

Next, start the IPFS daemon by opening a separate terminal/cmd window and type the following:

ipfs daemon

Finally, in your first terminal window and add the song to IPFS (make sure yo)

ipfs add <PATH_TO>/SpecialSong.mp3

On success upload, the terminal will generate a hash that starts with Qm. Copy the hash starting from Qm and add the “https://ipfs.io/ipfs/” prefix. It will look something like this https://ipfs.io/ipfs/QmYpgrCyfvEaec3DxwcQs5gaiw2dEEEcoaYKKYJ3j9fdF1, which will allow you to listen to your song in the browser:

Upload JSON file to IPFS

Create a JSON file nft.json and follow these steps to save it in the same directory as the song. Inside nft.json, paste the following code:

{
"name": "My first Song NFT",
"description": "My first music NFT project .",
"song": "https://ipfs.io/ipfs/YOUR_Qm_HASH",
}

Feel free to update the name and the description, and make sure to update the IPFS URI.

Now add upload the JSON file. In your command line enter:

ipfs add nft.json

Note the hash returned in your terminal. You will need this in the next step.

Create a Smart Contract in Remix

Head over to the Ethereum Remix IDE and make a new Solidity file, for example - music.sol.

Paste the following code into your new Solidity script:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.0;

import "https://github.com/0xcert/ethereum-erc721/src/contracts/tokens/nf-token-metadata.sol";
import "https://github.com/0xcert/ethereum-erc721/src/contracts/ownership/ownable.sol";

contract musicNft is NFTokenMetadata, Ownable {

constructor() {
nftName = "Quick Music NFT";
nftSymbol = "QMNFT";
}

function mint(address _to, uint256 _tokenId, string calldata _uri) external onlyOwner {
super._mint(_to, _tokenId);
super._setTokenUri(_tokenId, _uri);
}

}

Here's a an explanation of the code above:

  • Line 1: Specifying SPDX license type, which is an addition after Solidity ^0.6.8. Whenever the source code of a smart contract is made available to the public, these licenses can help resolve/avoid copyright issues. If you do not wish to specify any license type, you can use a special value UNLICENSED or simply skip the whole comment (it won’t result in an error, just a warning).
  • Line 2: Declaring the solidity version.
  • Line 4-5: Importing 0xcert/ethereum-erc721 contracts.
  • Line 7: Starting our Contract named newNFT and mentioning it’s extending NFTokenMetadata and Ownable contracts.
  • Line 9-12: Initializing the constructor and setting name, a symbol of our token.
  • Line 14: Declaring function mint with three arguments, variable _to of type address which will store the address of the receiver of NFT token, variable _tokenId of uint256 type which will hold the token id, variable _uri of type string which will store the URI of the JSON file. Declaring mint as external means, it can be accessed from other smart contracts and outside the self scope.
  • Line 15: Minting token using the address of the receiver and token id.
  • Line 16: Setting token URI using token id and URI of JSON file.

Once you're happy with your code, click Compile Contract to compile the smart contract.

Minting Your Music NFT

Click the Deploy and Run Transactions icon in the left panel (it looks like an Ethereum logo with a triangle).

Set your Environment as Injected Web3 and select the Smart Chain Testnet Wallet Metamask.

Click Deploy.

If you receive an error message before deployment, “This contract may be abstract,” this implies that the appropriate contract under the Contract tab was not selected. You will need to select the contract name that from the "Contract" drop-down that has the name of your song. Then click Deploy again.

You'll be prompted to approve the transaction from Metamask. Accept it:

Confirm the transaction in Metamask:

Once successful, you will see a note that looks like this.

Finally on the left side-bar scroll to the “Deployed Contracts” section in Remix and expand the deployed contract. You’ll see a few methods that you can call on your contract. Expand the mint function and add the following details:

Let's mint your NFT!

  1. Expand the Mint method by clicking the down arrow toggle.
  2. Enter your wallet address into the _to field.
  3. Enter an integer value into _tokenid. We're using 1.
  4. Enter the IPFS URI of the .json file you created earlier in the _uri field.

Then click Transact and approve the transaction in Metamask.

You should be able to see the succesful transaction in your Metamask and block explorer:

You can also double check that your metadata was stored correctly by scrolling down to the tokenURI method. Enter the same value you did in _tokenId above:

You should see a response that includes the URI to your .json file!

Great job! You've minted your first Music NFT!

Conclusion

You now have the tools to store your music file on decentralized storage and mint it as an NFT on BNB Smart Chain.

If you have any questions or want to reach out, you can find us on Discord and Twitter!

We <3 Feedback!

If you have any feedback or questions on this guide, let us know. We’d love to hear from you.

Share this guide