Smart Contracts for Flow Dapps
At its core, a decentralized application is defined by the smart contracts it uses on the blockchain. Rather than relying on centralized application servers and databases, dapps model their core application logic using smart contracts, often referred to as the “on-chain” code.
It is therefore helpful to develop a clear model for your dapp that takes into account the data and logic that will exist in your smart contracts. In particular, it is important to differentiate between the parts of your dapp that must live on chain and those that should live off chain.
Smart contracts on the Flow blockchain are implemented in Cadence, a resource-oriented programming language specifically designed for smart contract development.
To get started with Cadence, we recommended covering the introductory tutorials available in the Flow Playground, a simple web IDE designed for learning Cadence.
After learning the basics, check out the Kitty Items sample dapp to see Cadence contracts being used by a real web application. Inspired by CryptoKitties, Kitty Items demonstrates a simple NFT application that supports NFT minting, sales, and peer-to-peer trading.
To build confidently, you will want to set up the appropriate local environment and have an adequate test suite to ensure your smart contracts operate as intended. To do this, read the Kitty Items local development instructions to familiarize yourself with the following tools:
- Flow CLI: A utility to directly interact with the chain and manage accounts and contracts.
- Flow Emulator: A lightweight server that simulates the Flow blockchain (strongly recommended during development).
- FCL Dev Wallet: A utility to simulate user wallets in development.
- Visual Studio Code Extension: An IDE integration for developing smart contracts.
- JS Testing Framework: A framework to easily test your smart contracts.
All dapps will store important data on the blockchain, and some more than others -- especially NFT dapps. You’ll want to consider the following when storing data on the Flow blockchain.
Permanence is a key property of blockchains; users trust that the data they store will continue to exist for years to come, and this is a defining characteristic of assets like NFTs. Therefore, well-designed digital assets store the information necessary to retain their value without external dependencies.
However, there are practical constraints to storing data on a blockchain. Developer and user accounts must retain a small amount of FLOW tokens, known as the storage fee, for bytes of data stored in their accounts. The minimum storage fee will grant each account a minimum storage amount. If an account holds assets that demand more bytes of storage, the account will need to retain more FLOW tokens to increase the storage amount according to Flow's fee schedule. A more compact data model can keep storage needs down.
Furthermore, a single Flow transaction has a size limit of 4MB, which limits the rate at which large amounts of data can be transferred to the blockchain.
Lastly, a blockchain is not a content delivery network and therefore cannot serve media assets, such as videos, at the speeds expected by modern applications.
For these reasons, it usually isn’t practical to store large media assets such as videos and high-definition images on the Flow blockchain. Instead, consider using an external storage solution.
Decentralized storage networks such as IPFS allow you to store large digital assets off chain, but without relying on centralized servers. Rather than saving an entire asset to the Flow blockchain, you can save the content hash (known as a CID on IPFS) on the blockchain and then store the source file off-chain. This way, users can verify that the media file matches the digital asset.
IPFS files can be uploaded via a pinning service such as Pinata; see their NFT tutorial for an example of how to use Pinata with Flow.
It’s worth noting that IPFS files are served through gateways, many of which leverage caching to provide fast response times. Cloudflare provides a public IPFS Gateway, and Pinata also supports dedicated gateways with custom domains.
The Flow blockchain has existing smart contract standards for both fungible and non-fungible tokens that you should implement when building your contracts.
All NFTs on the Flow blockchain implement the NonFungibleToken interface, allowing them to be compatible with wallets, marketplaces and other cross-app experiences.
Flow has a standard contract to facilitate both the direct sales and peer-to-peer trading of NFTs. The NFT storefront contract is useful for dapps that want to provide an NFT marketplace experience.
Fungible tokens (i.e. coins, currencies) on the Flow blockchain, including the default cryptocurrency token FLOW, implement the FungibleToken interface.