Marketplace has launched, further enabling blockchain developers! Learn more

How To Mint A Music NFT on Binance Smart Chain

September 30, 2022

Overview

Binance 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 (BCS faucet)

Connect to Binance Testnet

Set Up Your QuickNode Endpoint

To build on Binance 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 Binance 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 Binance 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:

upload files to ipfs

Copy
ipfs init

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

upload files to ipfs

Copy
ipfs daemon

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

upload files to ipfs

Copy
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:

upload json file to ipfs

Copy
{
    "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:

upload json file to ipfs

Copy
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:

create a smart contract in remix

Copy
// 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 Binance 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.

Related articles 26

Solidity vs Vyper
Published: Aug 18, 2021
Updated: Sep 23, 2022

With the introduction to smart contracts on the Ethereum blockchain, it was only a matter of time until a language other than Solidity was made to write smart contract code. Vyper is one such...

Continue reading
How to Create a BEP20 Token
Published: Jul 3, 2021
Updated: Sep 23, 2022

BEP20 is the Binance Smart Chain equivalent to the popular ERC20 specification for tokens on the Ethereum network. The difference is the BEP20 Token isn't run on the Ethereum Network, but the...

Continue reading
What is an ABI?
Published: Mar 20, 2021
Updated: Sep 23, 2022

While interacting with a smart contract ABI is one of the essential components. In this guide, let us understand what the ABI of smart contracts is.

Continue reading
How to create and deploy an ERC20 token
Published: Feb 3, 2021
Updated: Sep 23, 2022

Ethereum network’s launch in 2015 created a lot of buzz in the developer community and sprouted a lot of tokens on the network. Initially there weren’t any templates or guidelines for token...

Continue reading
How to Create and Deploy an ERC-1155 NFT
Published: Mar 14, 2022
Updated: Sep 23, 2022

ERC1155 has emerged as a gold standard to create NFTs; every major marketplace lists new tokens as an ERC1155 standard. In this guide, we will learn about the ERC1155 token standard and how to...

Continue reading
Como crear un Token BEP20
Published: Jan 10, 2022
Updated: Sep 23, 2022

BEP20 es en Binance Smart Chain el equivalente al popular token ERC20 de la red de Ethereum. La diferencia es que el token BEP20 no corre en la red de Ethereum sino en la red de Binance Smart...

Continue reading