Flow Journey: Where to go after the Flow Playground? The Flow Emulator.
One thing about learning development for the Flow Blockchain is you have a lot great resources. To start off, our team ran through the CryptoDappies Tutorials. This tutorial showed how you could take a traditional React App and get it to talk to the Flow Blockchain and Smart Contracts deployed on it. It's a great way to start if you're a seasoned web developer. The tutorial is great, but it's all about connecting to existing Smart Contracts. While you do learn some Cadence and how to execute transactions and scripts, you don't build your own Smart contracts.
So where do you go after that?
Easy. You go to the Flow Cadence Tutorials. These tutorials are great and walk you through writing your own Smart Contracts to build your own Fungible and Non-Fungible Tokens along with a NFT marketplace. After going through these tutorials, I had a super good sense of who I would now build a "full-stack" dApp on flow.
There was just one problem.
See, the Flow Cadence Tutorials have you build everything within the Flow Playground. This is perfect for learning, but it begs the question, where do I go next? How do I go from the Flow Playground to the "real" Flow blockchain?
After the playground, where do you go? Emulator
So where do you go after those first two tutorials? My answer was the Flow Emulator and the Flow CLI. The Flow Emulator is like running a copy of the Flow Blockchain on your local machine. It behaves like the TestNet and MainNet (the "real" Flow blockchains) but runs locally on your computer in memory.
If you're coming from the web world, you're probably used to running a dev database, or dev server on your machine. That's kind of what the Flow Emulator is like - it's the back-end to your App that runs on your machine.
So this was the answer.
After you install the Flow CLI, you can simply run flow emulator, and you'll have a blockchain running on your machine that you can play around with.
Some basics about the Flow Emulator
So the cool thing is, the Flow Emulator actually comes pre-built with some Smart Contracts that automatically get deployed when you fire it up. This includes things like the FLOW Network Token contract. That way you can build apps that use the FLOW Network Token right out of the box.
The Flow Emulator also comes with a whole bunch of configuration options and commands that allow you to easily create accounts on the Flow Emulator Blockchain and automatically deploy your Smart Contracts to it. The full details are outside the scope of this post, but I'll run over a couple of things that I did to just play around and get started.
Set up your Configuration
Before you do anything, you should set up your configuration file flow.json which gets created by simply running
flow init
Now you're ready to play around.
Create an account
So the default configuration of the Flow Emulator within the flow.json comes pre-built with an account that gets created when you launch the emulator, but in case you wanted to play around with creating your own you can! First start up the emulator...
flow emulator
Then in a separate Terminal tab, you could run something like this
flow keys generate
That will give you some keys you can use, then simply run (replacing 3b9cd... with the key that you generated in the last step)
flow accounts create --key 3b9cd...
Deploy Contract
Want to try deploying a really simple contract? Easy. Create a file in your project like the following...
/*
Description: Test NFT for BiteSite.
*/
pub contract TestBiteSiteNFT {
pub resource NFT {
pub let id: UInt64
pub var metadata: {String: String}
init(initID: UInt64) {
self.id = initID
self.metadata = {}
}
}
init() {
self.account.save<@NFT>(<-create NFT(initID: 1), to: /storage/TestBiteSiteNFT)
}
}
Then
flow accounts add-contract TestBiteSiteNFT ./contracts/TestBiteSiteNFT.cdc
Note, by default, this smart contract will be deployed to the Test Emulator Account that's set up in flow.json. This is an important point, you should definitely read the full documentation on the Flow CLI as it contains important things like "default" values when running commands like add-contract.
Conclusion
So there you go, now you know how to write Smart Contracts and play around with your own blockchain running as the Flow Emulator. In future posts, I'll show you how to have a React App talk to the emulator.