Skip to content

Checking your reading list…

Retry

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Blockchain 9 min read

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-cli

Windows (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

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

TutorialSmart ContractFlowCadence

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion