Cointime

Download App
iOS & Android

Creating ERC721 NFT Smart Contracts on Flow Blockchain with Cadence Programming Language: A Comprehensive Guide with Code Examples

Validated Individual Expert

The world of blockchain has been revolutionized by the emergence of non-fungible tokens (NFTs). These digital assets are unique and cannot be replicated, making them ideal for use cases such as art, collectibles, and gaming items. The ERC721 standard, developed by Ethereum, is one of the most popular NFT standards in use today. However, the limitations of the Ethereum network, such as high gas fees and scalability issues, have led to the emergence of alternative blockchain platforms for NFT development. One such platform is the Flow blockchain, which uses the Cadence programming language. In this article, we will explore how to create an ERC721 NFT smart contract on the Flow blockchain using Cadence, including code examples.

What is an ERC721 NFT?

An ERC721 NFT is a unique digital asset that is stored on the Ethereum blockchain. Each NFT is represented by a smart contract that contains metadata and ownership information. Unlike traditional cryptocurrencies, such as Bitcoin or Ethereum, which are fungible and interchangeable, each ERC721 NFT is unique and cannot be replicated. This uniqueness makes them ideal for use cases such as art, collectibles, and gaming items, where the value of an asset is determined by its uniqueness.

ERC721 smart contracts define a standard interface that enables NFTs to be created, owned, and traded on the Ethereum network. The ERC721 standard includes functions such as minting new tokens, transferring ownership of tokens, and querying token metadata.

What is the Flow Blockchain?

The Flow blockchain is a fast, decentralized, and developer-friendly blockchain platform designed for building high-performance applications and digital assets. Flow uses a unique architecture that separates the computation and storage layers, allowing for more efficient use of resources and faster transaction processing times. Flow also supports smart contracts written in the Cadence programming language, which is designed to be safe, easy to use, and secure.

Why Use Flow for NFT Development?

While Ethereum is the most popular blockchain platform for NFT development, it has several limitations that have led to the emergence of alternative platforms such as Flow. One of the main issues with Ethereum is the high gas fees required to execute transactions on the network. Gas fees are paid in Ethereum’s native currency, Ether, and can vary greatly depending on network congestion and transaction complexity. This can make it expensive to create, trade, and transfer NFTs on the Ethereum network.

Flow, on the other hand, has lower transaction fees and faster transaction processing times than Ethereum. This makes it more suitable for use cases such as gaming, where high transaction volumes are required. Additionally, Flow’s architecture is designed to be developer-friendly, with a focus on ease of use and security.

Creating an ERC721 NFT Smart Contract on Flow

To create an ERC721 NFT smart contract on the Flow blockchain, we will use Cadence. Cadence is a safe, easy-to-use, and secure programming language designed specifically for blockchain development. Cadence is built on top of the Flow blockchain, which means that developers can take advantage of Flow’s unique architecture and features.

The first step in creating an ERC721 NFT smart contract on Flow is to define the contract interface. The contract interface defines the functions and properties that will be exposed to the outside world. In our case, we will define the ERC721 standard interface, which includes functions such as minting new tokens, transferring ownership of tokens, and querying token metadata.

Here is an example of an ERC721 contract interface in Cadence:

pub contract ERC721 {  // Event that is emitted when a new token is minted  pub event Minted(tokenID: UInt64, owner: Address)  // Event that is emitted when a token is transferred  pub event Transferred(tokenID: UInt64, from: Address, to: Address)// Function to mint a new tokenpub fun mint(to: Address, tokenID: UInt64) {// TODO: Implement mint function}// Function to get the owner of a tokenpub fun ownerOf(tokenID: UInt64): Address? {// TODO: Implement ownerOf functionreturn nil}// Function to transfer ownership of a tokenpub fun transfer(from: Address, to: Address, tokenID: UInt64) {// TODO: Implement transfer function}}

This interface defines the functions and events that are required for an ERC721 smart contract. We have defined the `mint` function for minting new tokens, the `ownerOf` function for querying the owner of a token, and the `transfer` function for transferring ownership of a token.

Now that we have defined the interface for our ERC721 contract, we can start implementing the contract logic. In our implementation, we will use a dictionary to store the ownership information for each token. The key of the dictionary will be the token ID, and the value will be the address of the owner.

Here is an example of an ERC721 smart contract implementation in Cadence:

pub contract MyNFT: ERC721 {  // Dictionary to store the ownership information for each token  pub var tokenOwner: {UInt64: Address}  // Function to mint a new token  pub fun mint(to: Address, tokenID: UInt64) {    // Check that the token has not already been minted    if (tokenOwner[tokenID] != nil) {      panic("Token already exists")    }    // Mint the new token and set the owner to the specified address    tokenOwner[tokenID] = to    // Emit the Minted event    emit Minted(tokenID: tokenID, owner: to)  }  // Function to get the owner of a token  pub fun ownerOf(tokenID: UInt64): Address? {    // Return the owner address for the specified token ID    return tokenOwner[tokenID]  }  // Function to transfer ownership of a token  pub fun transfer(from: Address, to: Address, tokenID: UInt64) {    // Check that the sender is the current owner of the token    if (tokenOwner[tokenID] != from) {      panic("Sender is not the owner of the token")    }    // Transfer ownership of the token to the new address    tokenOwner[tokenID] = to    // Emit the Transferred event    emit Transferred(tokenID: tokenID, from: from, to: to)  }}

This implementation defines a contract called MyNFT that implements the ERC721 interface. We have defined the tokenOwner dictionary to store the ownership information for each token, and we have implemented the mint, ownerOf, and transfer functions.

The mint function checks that the token has not already been minted and then mints the new token by setting the owner to the specified address. We have also emitted the Minted event to notify external observers that a new token has been minted.

The ownerOf function simply returns the owner address for the specified token ID by looking up the value in the tokenOwner dictionary.

The transfer function checks that the sender is the current owner of the token and then transfers ownership of the token to the new address by updating the value in the tokenOwner dictionary. We have also emitted the Transferred event to notify external observers that the ownership of a token has been transferred.

We can now deploy this ERC721 smart contract on the Flow blockchain using the Flow CLI. Here are the steps to deploy the contract:

  1. Install the Flow CLI by following the instructions on the official Flow documentation.
  2. Create a new directory for your project and navigate to it in your terminal.
  3. Initialize a new Flow project by running flow init in your terminal. This will create a new flow.json file in your project directory.
  4. Create a new Cadence file in your project directory called MyNFT.cdc and paste in the ERC721 smart contract implementation that we just created.
  5. Deploy the contract by running the following command in your terminal: flow project deploy --network=emulator. This will deploy the contract on the Flow emulator network.

Once the contract is deployed, you can interact with it using the Flow CLI. Here are some examples of how to use the contract:

  1. Mint a new token:
flow transactions send ./transactions/mint.cdc

This command will send a transaction to the contract to mint a new token.

2. Query the owner of a token:

flow scripts execute ./scripts/ownerOf.cdc

This command will execute a script to query the owner of a token.

3. Transfer ownership of a token:

flow transactions send ./transactions/transfer.cdc

This command will send a transaction to the contract to transfer ownership of a token.

In order to execute transactions and scripts on the Flow blockchain, we need to write Cadence code that interacts with the contract. Here is an example of how to do that in Cadence:

import FungibleToken from "./FungibleToken.cdc"import MyNFT from "./MyNFT.cdc"// Address of the contractlet contractAddress: Address = 0x01// Address of the token ownerlet ownerAddress: Address = 0x02// Address of the token recipientlet recipientAddress: Address = 0x03// Token ID of the token to mintlet tokenID: UInt64 = 1// Amount of tokens to transferlet amount: UInt64 = 10// Instantiate the FungibleToken contractlet tokenContract = getAccount(contractAddress)  .getContract<FungibleToken.FungibleToken>("FungibleToken")// Instantiate the MyNFT contractlet nftContract = getAccount(contractAddress)  .getContract<MyNFT.MyNFT>("MyNFT")// Mint a new tokennftContract.mint(to: ownerAddress, tokenID: tokenID)// Get the owner of a tokenlet owner = nftContract.ownerOf(tokenID: tokenID)// Transfer ownership of a tokennftContract.transfer(from: ownerAddress, to: recipientAddress, tokenID: tokenID)// Transfer tokenstokenContract.transfer(from: ownerAddress, to: recipientAddress, amount: amount)

In this example, we are importing the FungibleToken and MyNFT contracts that we created earlier. We are then instantiating these contracts using the getAccount function and calling their functions to interact with them.

We first mint a new token by calling the mint function on the MyNFT contract. We then get the owner of the token by calling the ownerOf function on the MyNFT contract.

We then transfer ownership of the token by calling the transfer function on the MyNFT contract. Finally, we transfer some FLOW tokens by calling the transfer function on the FungibleToken contract.

In conclusion, ERC721 tokens are a powerful tool for creating unique, indivisible digital assets on the blockchain. By using smart contracts, we can create ERC721 tokens that have specific properties and behaviors, such as being non-fungible, transferrable, and ownable. The Flow blockchain and the Cadence programming language provide an ideal environment for creating and deploying ERC721 contracts, with a high degree of security and performance.

Here is the final implementation of the ERC721 smart contract in Cadence:

import FlowToken from 0xFLOWTOKENADDRESSimport FungibleToken from "./FungibleToken.cdc"pub contract MyNFT: FungibleToken {    // Event emitted when a new token is minted    pub event Mint(tokenID: UInt64, to: Address)    // Event emitted when ownership of a token is transferred    pub event Transfer(tokenID: UInt64, from: Address, to: Address)    // Storage for token owner information    pub var owners: {UInt64: Address}    // Storage for the total supply of tokens    pub var totalSupply: UInt64    // Initialize the contract with an empty owners mapping    init() {        self.owners = {}        self.totalSupply = 0    }    // Mint a new token and assign ownership to the specified address    pub fun mint(to: Address, tokenID: UInt64) {        // Make sure the token ID is not already owned        assert(!self.exists(tokenID), message: "Token already exists")        // Assign ownership to the specified address        self.owners[tokenID] = to        // Increase the total supply of tokens        self.totalSupply += 1        // Emit a Mint event        emit Mint(tokenID: tokenID, to: to)    }    // Check if a token exists    pub fun exists(tokenID: UInt64): Bool {        return self.owners[tokenID] != nil    }    // Get the owner of a token    pub fun ownerOf(tokenID: UInt64): Address? {        return self.owners[tokenID]    }    // Transfer ownership of a token from one address to another    pub fun transfer(from: Address, to: Address, tokenID: UInt64) {        // Make sure the sender is the current owner of the token        assert(self.ownerOf(tokenID) == from, message: "Sender does not own token")        // Assign ownership to the new address        self.owners[tokenID] = to        // Emit a Transfer event        emit Transfer(tokenID: tokenID, from: from, to: to)    }    // Override the FungibleToken implementation to prevent tokens from being transferred    pub fun transfer(from: Address, to: Address, amount: UFix64) {        panic("ERC721 tokens are not transferrable with FungibleToken")    }    // Override the FungibleToken implementation to prevent tokens from being approved for transfer    pub fun approve(to: Address, amount: UFix64) {        panic("ERC721 tokens are not approvable with FungibleToken")    }    // Override the FungibleToken implementation to prevent tokens from being transferred from one address to another    pub fun transferFrom(from: Address, to: Address, amount: UFix64) {        panic("ERC721 tokens are not transferrable with FungibleToken")    }}

I hope this article has been helpful in understanding how to create and deploy ERC721 smart contracts on the Flow blockchain using the Cadence programming language. By following the examples and guidelines presented here, you can create your own unique digital assets and interact with them on the blockchain.

It’s important to note that the implementation of ERC721 tokens can vary depending on the specific use case and requirements of the project. For example, some projects may require additional functionality such as the ability to burn tokens or to add metadata to each token. These features can be added to the contract by modifying the implementation or by creating additional functions.

Additionally, it’s important to thoroughly test your smart contracts before deploying them to the blockchain. This can be done by creating automated tests that cover all possible scenarios and edge cases. The Flow blockchain provides a robust testing framework that can be used to test your contracts before deploying them to the mainnet.

In summary, ERC721 tokens are a powerful tool for creating unique, indivisible digital assets on the blockchain. The Flow blockchain and the Cadence programming language provide an ideal environment for creating and deploying ERC721 contracts, with a high degree of security and performance. By following the guidelines and examples presented in this article, you can create your own ERC721 tokens and interact with them on the blockchain.

NFT
Comments

All Comments

Recommended for you

  • Walsh: Federal Reserve Meeting Schedule to Change Next Fall, No Rate Vote in 2027

    On October 29, Federal Reserve Chair Walsh stated that the schedule for the Federal Open Market Committee will begin to change next fall. In 2027, there will be one meeting that will neither conduct a rate vote nor issue a policy statement. It is expected that there will be two FOMC meetings in 2028 that will not conduct a rate vote.

  • BTC Surpasses $81,000

    Market data shows that BTC has surpassed $81,000, currently priced at $81,001.26, with a 24-hour increase of 0.54%. The market is highly volatile, so please ensure proper risk management.

  • Bank of Canada Raises Interest Rate by 25 Basis Points as Expected

    On October 28, the Bank of Canada raised its benchmark interest rate from 2.25% to 2.50%, marking the first rate hike in over three years and aligning with market expectations.

  • Tether Launches FASE Foundation with 11 Partners

    On October 28, Tether announced the establishment of the FASE (Financial Access and Systemic Empowerment Foundation) in Switzerland, a nonprofit multi-stakeholder foundation focused on four key pillars: financial security, financial freedom and inclusion, connectivity and clean energy, and access to end medical resources, as well as responsible AI and robotics. Founding stakeholders include Anchorage Digital, Bitfury, Bybit, Chainalysis, Fasanara Capital, Kraken, Opera, Pave Bank, Prosper Global (formerly Mercy Corps), Solana Foundation, and Tether, with technical consulting provided by the United Nations Office on Drugs and Crime (UNODC). The first three projects include: forming a global youth alliance with the social enterprise Goodwall to provide digital education and fraud prevention tools to up to 10 million young people in high-risk areas in Africa, Latin America, and Southeast Asia; piloting stablecoin humanitarian aid with Prosper Global, initially covering up to 1 million people as a template for future crisis response; and expanding cybercrime prevention networks in Africa, Asia, and Central America based on existing collaboration with UNODC, along with partnering with the United Nations Trust Fund for Victims of Trafficking to fund NGOs that assist survivors of human trafficking. Tether CEO Paolo Ardoino stated that the most vulnerable populations have long been overlooked by traditional financial systems. Prior to the official launch, the founding members held a meeting in Lugano, Switzerland, to establish strategic priorities.

  • Morgan Stanley Warns S&P 500 May Drop Another 8%

    On October 28, Mike Wilson, Chief U.S. Equity Strategist at Morgan Stanley, stated in a client report that a combination of AI financing pressures, the U.S. midterm elections on November 3, and tightening financial conditions could push the S&P 500 index down to around 7100 points in the coming weeks, representing a decline of about 8% from recent levels. This would mark the largest adjustment since the market turmoil triggered by the Iran war earlier this year. The firm noted that approximately 60% of the Russell 3000 index components have fallen over 20% from their peaks since early June, and about 96% of semiconductor stocks have also declined by more than 20%. They anticipate that the semiconductor sector may drop an additional 10% to 15% before mid-November. Credit spreads on bonds related to data centers have widened, with CoreWeave's debt entering distressed territory this month. The Federal Reserve raised interest rates to 3.75% to 4.00% in September, leading to rising bond yields. The firm maintains a year-end target of 8000 points for the S&P 500 and a fundamental target of 8300 points over the next 12 months, noting that markets typically perform better after midterm elections, with data from 1950 to 2022 showing an average increase of about 8% in the three months following elections.

  • Israeli Parliamentary Election Vote Counting Completed, Netanyahu's Camp Currently Leads

    On the 28th local time, according to Israeli sources, the vote counting for this round of Israeli parliamentary elections has been completed. The Likud party, led by current Prime Minister Netanyahu, remains in the lead with the highest percentage of valid votes counted so far. The Likud party continues to be the largest party in the parliament. However, there is a deadlock in the allocation of seats between Netanyahu's right-wing camp and the opposition camp led by Esenkot, with no single camp able to directly reach the majority of seats required to form a government.

  • Anthropic IPO Filing Warns of Risks from Trump Administration Retaliation

    On October 28, Bloomberg reported that in its IPO filing, Anthropic warned that its public advocacy for AI safety regulation and statements from executives could expose the company to risks such as government pressure and selective scrutiny. The document stated that even if other similarly situated companies are not treated equally, Anthropic could still become a target for specific government agencies, a risk associated with the company's multiple conflicts with the Trump administration: one stemming from disputes over the requirement for the U.S. Department of Defense to set safety limits when using its AI technology, and another related to disagreements over the safety and export controls of the Mythos model. The filing also indicated that future government interventions, export restrictions, and cybersecurity or biosafety incidents triggered by AI models could affect its product deployment, international expansion, and business growth. Furthermore, if the regulatory measures advocated by the company are implemented, compliance costs would rise, product development and release would be restricted, and its growth could slow down. Anthropic publicly submitted its S-1 filing on October 26, intending to list on Nasdaq under the ticker ANT.

  • AI Simulation System Sim Fed Previews October Rate Meeting: Seven Votes, Five for Pause

    On October 28, ahead of the Federal Reserve's upcoming interest rate decision, an AI simulation decision-making system called Sim Fed completed seven simulated votes: five opted to maintain the current interest rate, while two supported a rate hike, aligning closely with the market's mainstream expectation of a pause this week. The system was developed by Tara Sinclair, chair of the economics department at George Washington University, and Sophia Kazinnik, a research scientist at Stanford University's Digital Economy Lab. The two invested approximately a year and a half into the project, with a preliminary research paper set to be published by the end of 2025. Researchers clarified that the aforementioned votes are results of a simulation experiment and do not reflect the Federal Reserve's actual decisions; the real monetary policy decision is expected to be announced at 2 AM Beijing time on October 29.

  • Casap Completes $56 Million Series B Funding Led by Base10 Partners

    On October 28, Fortune reported that financial AI startup Casap announced the completion of $56 million in Series B funding, led by Base10 Partners, with Synchrony and Experian participating as strategic investors. The company's total funding has reached $90 million. Founded in 2023, Casap's AI tools assist banks and credit card companies in handling disputes and chargebacks. The company disclosed that clients using the tool have seen first-party fraud losses reduced by up to 51%, with chargeback win rates increasing to 96%, compared to the industry average of 60%. The average dispute resolution time has been shortened to 8 days, while traditional methods can take up to 90 days. The valuation for this round has not been disclosed.

  • Kalshi Plans to Tighten Deposit Restrictions for Users Under 21

    On October 28, Bloomberg reported that Kalshi CEO Tarek Mansour stated that the prediction market platform is discussing increased user protection measures with U.S. regulators due to research showing that most users are in a losing position. Kalshi is willing to accept raising the participation age threshold to 21 years. The specific measures include a monthly deposit limit of $2,500 for users under 21 and the establishment of special accounts to curb high-risk trading. Mansour noted that Kalshi is an exchange regulated by the U.S. Commodity Futures Trading Commission (CFTC) and does not directly act as a counterparty for user trades, with users' lost funds flowing to other traders. However, he also acknowledged the need for the platform to work with regulators to establish protective rules to address existing regulatory gaps.