Build Your First Flow Blockchain Smart Contract with Cadence
This tutorial introduces the Flow blockchain, its unique sharding architecture, and ecosystem, then walks through installing the Flow CLI, using the Cadence Playground, writing and deploying a HelloWorld smart contract, and extending it with additional functions and scripts.
Flow Overview
Flow is an open‑world blockchain developed by Dapper Labs. It uses a multi‑shard architecture where the network is divided into independent shards that communicate via a protocol, providing high throughput and low transaction costs.
Key Features
Sharding for scalability and decentralization.
Designed for NFT and DeFi applications.
Ecosystem includes smart‑contract execution, wallets, exchanges, and developer tooling.
Cadence Language
Cadence is Flow’s resource‑oriented smart‑contract language. It is inspired by Swift, Kotlin and TypeScript and provides a strong type system, resource safety, and built‑in support for NFTs.
Official documentation: https://developers.flow.com/cadence
HelloWorld Contract Example
The following contract defines a simple greeting stored in a public string field and provides two public functions to change and read the greeting.
pub contract HelloWorld {
// Public string field
pub var greeting: String
// Change the greeting (public)
access(all) fun changeGreeting(newGreeting: String) {
self.greeting = newGreeting
}
// Return the current greeting (public)
access(all) fun hello(): String {
return self.greeting
}
// Initialize the field
init() {
self.greeting = "Hello, World!"
}
}Contract Structure
Contract name : HelloWorld Public field : greeting of type String, initialized in init.
Public functions : changeGreeting(newGreeting: String) – updates greeting. Access is access(all) (replace with stricter control in production). hello() – returns the current greeting. Access is access(all).
Initializer : init() sets greeting to “Hello, World!”.
Extending the Contract
Adding a new function:
pub fun sayHello(name: String): String {
return name
}Deploy the updated contract and invoke it from a Cadence script:
import HelloWorld from 0x05
pub fun main(): String {
return HelloWorld.sayHello(name: "hi,JavaEdge")
}Using the Flow Playground
The Flow Playground is a web‑based IDE for writing, compiling, and deploying Cadence contracts. It provides demo projects and a console for executing scripts.
Local Environment Installation
Install the Flow CLI to interact with a local or remote Flow network.
Linux / macOS
sh -ci "$(curl -fsSL https://raw.githubusercontent.com/onflow/flow-cli/master/install.sh)"macOS (Homebrew)
brew upgrade flow-cliWindows (PowerShell)
iex "&{$(irm 'https://storage.googleapis.com/flow-cli/install.ps1')}"Verify Installation
flow cadence
log(" Hello, World!")CLI documentation: https://docs.onflow.org/flow-cli/
References
Flow official site: https://zh.onflow.org/
Flow technical paper: https://zh.onflow.org/technical-paper
Cadence language guide: https://developers.flow.com/cadence#cadences-programming-language-pillars
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
JavaEdge
First‑line development experience at multiple leading tech firms; now a software architect at a Shanghai state‑owned enterprise and founder of Programming Yanxuan. Nearly 300k followers online; expertise in distributed system design, AIGC application development, and quantitative finance investing.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
