Skip to content

Instantly share code, notes, and snippets.

@DappaDanDev
Last active March 29, 2023 14:56
Show Gist options
  • Save DappaDanDev/3df2537d9f25e787fad0fee4b7500aed to your computer and use it in GitHub Desktop.
Save DappaDanDev/3df2537d9f25e787fad0fee4b7500aed to your computer and use it in GitHub Desktop.
How to Create and Deploy a Smart Contract on Flow
# How to Create and Deploy a Smart Contract on Flow
## Overview
[Flow](https://flow.com/) is a blockchain designed from the ground up to deliver the performance required of mainstream consumer apps. It does this through its approach to smart contracts, account access, and a native programming language called Cadence.
In this guide, you will learn how to deploy a smart contract on Flow written in Cadence.
Before you "go with the Flow", here are some important concepts about the Flow blockchain. This will help you better understand the smart contract code and deployment later:
## Important Flow Concepts
- **Cadence Programming Language** - Cadence is the language used to write all smart contracts on Flow. It takes inspiration from other popular languages like Rust, TypeScript, and Swift. Cadence uses linear types like in Rust to manage asset ownership directly within the languages. It also uses `events` similar to Solidity to emit on-chain activities.
- **Account** - Accounts on Flow store both keys and smart contract code. There is no limit to the number of contracts an account can store. Each account receives a unique address when it is created.
- **Transactions** - Transactions are Cadence code blocks that run after receiving authorization from one or more accounts. The authorization is a cryptographic signature with allows the code access to the account's storage.
- **Capability-Based Access** - Cadence creates specific storage paths called `Capability` paths that are linked to objects written in a contract. This path allows for different accounts to have access to only certain objects within another account's storage.
## What You Will Do
In this guide, you will:
- Gain a basic understanding of the Cadence Programming Language
- Deploy a Cadence Smart Contract using the Cadence Playground
- Send a transaction to your Smart Contract with a "Hello World" Message
## What You Will Need
- Access to the Cadence Playground
- Basic programming knowledge (functions/variables/scope/etc)
- A modern browser (eg [Google Chrome](https://www.google.com/chrome/)) running in Desktop mode
## Open Your Project
The Flow Playground is a test environment to write and test Cadence code. It also provides a sample Hello World project to help you understand the parts of a smart contract and deployment.
You can access this project by creating a new project on the [Flow Playground](https://play.flow.com/).
## Understanding the Flow Playground
After creating a project, you will see a list of accounts, transaction templates, and script templates on the left side of the Playground UI:
![A List of Flow Projects](https://github.com/DappaDanDev/gist-images/blob/main/flow-projects.png?raw=true)
As mentioned earlier, accounts on Flow store both keys and smart contract code. By clicking on any of the accounts (0x01 - 0x05) you can see the smart code contract it is storing. The Flow Playground currently only allows one contract per account.
Before deploying the contract, let us explore the smart contract in detail:
## Smart Contract Code
Below is the code that is in the HelloWorld contract. You can see this in the Playground UI by clicking on the first account called `0x01`
````
access(all) contract HelloWorld {
    // Declare a public field of type String.
    //
    // All fields must be initialized in the init() function.
    access(all) let greeting: String
    // The init() function is required if the contract contains any fields.
    init() {
        self.greeting = "Hello, World!"
    }
    // Public function that returns our friendly greeting!
    access(all) fun hello(): String {
        return self.greeting
    }
}
````
`access(all) contract HelloWorld` - Declares that the contract - `HelloWorld` - is accessible in the public scope. `access(all)` can also be replaced by the `pub` keyword which has the same function.
`access(all) let greeting: String` - The let keyword is used to define a variable that is a constant. The `var` keyword is used for variables that will change.
`init()` - Initializes the function when the contract is first created.
`self.greeting = "Hello World"` - Creates a string with the value "Hello World!"
`access(all) fun hello(): String` - Ceates a function that will return a string.
`return self.greeting` - Sets the string that will be returned by the function to the "Hello World" string that was created earlier in the contract.
Now that you have an understanding of what is going on in the contract, it is time to deploy it!
## Deploying the Smart Contract
To deploy a smart contract on the Flow playground, you need to click on the 'Deploy' button on the right side of your screen.
![Deploy Button in Flow](https://github.com/DappaDanDev/gist-images/blob/main/flow-deploy.png?raw=true)
If you have any errors in your contract code, you will instead see some helpful suggestions that may fix these problems before being able to deploy:
![Code Error Message](https://github.com/DappaDanDev/gist-images/blob/main/flow-error.png?raw=true)
Once you have successfully deployed your contract, the `Deployed Contract To: 0x01`
message will be in the `Deployment Result` window at the bottom of the Playground UI:
![Deployment Result](https://github.com/DappaDanDev/gist-images/blob/main/flow-deployed.png?raw=true)
Now that you have a deployed contract, the next step is to create a transaction.
## Creating a Transaction
A transaction is a Cadence code block. The Flow Playground has a pre-written transaction template that you can use to create your first transaction.
![Flow Playground Transaction Templates](https://github.com/DappaDanDev/gist-images/blob/main/flow-transaction.png?raw=true)
After clicking on this file, you will see the transaction template code. Let us look at this in more detail:
````
import HelloWorld from 0x01
transaction {
  prepare(acct: AuthAccount) {}
  execute {
    log(HelloWorld.hello())
  }
}
````
`import HelloWorld from 0x01` - Imports the code of the HelloWorld smart contract from the 0x01 account you deployed from.
`prepare(acct: AuthAccount)` - Creates the prepare phase of the transaction which is required when access is needed from the private `Auth Account`
`execute {log(HelloWorld.hello())} ` - Excecutes the public function in the HelloWorld contract and logs the response to the console.
Now you can send the transaction by clicking the `Send` button on the `Transaction Signers` window of the Playground UI.
![Send Transaction](https://github.com/DappaDanDev/gist-images/blob/main/signers.png?raw=true)
After the transaction has been sent successfully, you will see your `Hello, World` message shown in the `Transaction Result` window at the bottom of the playground.
![Completed Transaction](https://github.com/DappaDanDev/gist-images/blob/main/transaction_complete.png?raw=true)
## Conclusion
Congrats, you have now learned more about the Flow blockchain and the Cadence programming language by deploying and transacting with a smart contract. Want to keep building with Flow? Check out how to create a [Flow endpoint using QuickNode](https://www.quicknode.com/chains/flow). (Currently in Open Beta)
If you're stuck, have questions, or just want to talk about what you're building, drop us a line on [Discord](https://discord.gg/ahckhyA) or [Twitter](https://twitter.com/QuickNode)!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment