Learn Clarity - 10 Code Examples & CST Typing Practice Test
Clarity is a decidable smart contract language used on the Stacks blockchain. It enables predictable and secure smart contracts without gas estimation or unpredictable behavior.
Learn CLARITY with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
Clarity Simple Counter Contract
(define-data-var counter int 0)
(define-public (increment)
(begin
(var-set counter (+ (var-get counter) 1))
(ok (var-get counter))))
(define-public (reset)
(begin
(var-set counter 0)
(ok (var-get counter))))
A minimal Clarity contract defining a counter with increment and reset functionality.
Clarity Token Balance Store
(define-map balances principal int)
(define-public (credit user amount)
(begin
(map-set balances user (+ (default-to 0 (map-get? balances user)) amount))
(ok true)))
(define-public (debit user amount)
(let ((bal (default-to 0 (map-get? balances user))))
(if (>= bal amount)
(begin (map-set balances user (- bal amount)) (ok true))
(err u100))))
Stores simple token balances with read, credit, and debit functions.
Clarity Ownership Module
(define-data-var owner principal tx-sender)
(define-public (set-owner new-owner)
(begin
(asserts! (is-eq tx-sender (var-get owner)) u403)
(var-set owner new-owner)
(ok true)))
Defines a simple owner pattern with update functionality.
Clarity Simple Voting System
(define-data-var votes int 0)
(define-public (vote)
(begin
(var-set votes (+ (var-get votes) 1))
(ok (var-get votes))))
A basic voting module with upvote and total count.
Clarity Boolean Toggle
(define-data-var flag bool false)
(define-public (toggle)
(begin
(var-set flag (not (var-get flag)))
(ok (var-get flag))))
A simple boolean on/off toggle stored on-chain.
Clarity Fixed Supply Token
(define-constant total-supply u1000000)
(define-map balances principal uint)
(define-public (init user)
(begin
(map-set balances user total-supply)
(ok true)))
(define-public (transfer to amount)
(let ((bal (default-to u0 (map-get? balances tx-sender))))
(if (>= bal amount)
(begin
(map-set balances tx-sender (- bal amount))
(map-set balances to (+ (default-to u0 (map-get? balances to)) amount))
(ok true))
(err u1))))
A simple fixed-supply token with balances and transfer.
Clarity Timelock Contract
(define-data-var unlock-height uint u0)
(define-data-var locked-value uint u0)
(define-public (lock value height)
(begin
(var-set locked-value value)
(var-set unlock-height height)
(ok true)))
(define-public (withdraw)
(if (>= block-height (var-get unlock-height))
(ok (var-get locked-value))
(err u10)))
Locks a value until a given block height.
Clarity Allowlist Example
(define-map allowed principal bool)
(define-public (add user)
(begin
(map-set allowed user true)
(ok true)))
(define-public (remove user)
(begin
(map-set allowed user false)
(ok true)))
Stores allowed addresses with add/remove operations.
Clarity Simple Message Storage
(define-data-var message (string-ascii 50) "")
(define-public (set-message msg)
(begin
(var-set message msg)
(ok msg)))
Stores a public on-chain message.
Clarity Key-Value Store
(define-map store int int)
(define-public (set-value key value)
(begin
(map-set store key value)
(ok value)))
(define-read-only (get-value key)
(default-to 0 (map-get? store key)))
Generic key-value mapping for int -> int.
Frequently Asked Questions about Clarity
What is Clarity?
Clarity is a decidable smart contract language used on the Stacks blockchain. It enables predictable and secure smart contracts without gas estimation or unpredictable behavior.
What are the primary use cases for Clarity?
Building secure DeFi protocols on Stacks. NFT minting and trading. On-chain governance contracts. Bitcoin-integrated smart contracts. Deterministic financial applications
What are the strengths of Clarity?
Predictable smart contract execution. High security with decidability. Strong type system prevents many bugs. Integrates with Bitcoin safely. Simplifies auditing and formal verification
What are the limitations of Clarity?
Limited to Stacks blockchain. Lisp-like syntax may have learning curve. Fewer libraries compared to Solidity/EVM. Not general-purpose outside Stacks. Smaller community and tooling ecosystem
How can I practice Clarity typing speed?
CodeSpeedTest offers 10+ real Clarity code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.