1. Home
  2. /
  3. Clarity
  4. /
  5. Token Balance Store

Token Balance Store - Clarity Typing CST Test

Loading…

Token Balance Store — Clarity Code

Stores simple token balances with read, credit, and debit functions.

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

Clarity Language Guide

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.

Primary Use Cases

  • ▸Building secure DeFi protocols on Stacks
  • ▸NFT minting and trading
  • ▸On-chain governance contracts
  • ▸Bitcoin-integrated smart contracts
  • ▸Deterministic financial applications

Notable Features

  • ▸Decidable language - predictable execution
  • ▸No gas estimation needed
  • ▸Strong static typing
  • ▸Direct Bitcoin integration
  • ▸Lisp-like syntax with functional constructs

Origin & Creator

Clarity was created by Blockstack PBC (now Hiro Systems) around 2018 to provide secure, predictable smart contracts that integrate with Bitcoin.

Industrial Note

Clarity is used in projects where security and predictability are paramount, such as DeFi protocols on Bitcoin, NFT platforms, and on-chain governance solutions.

More Clarity Typing Exercises

Clarity Simple Counter ContractClarity Ownership ModuleClarity Simple Voting SystemClarity Boolean ToggleClarity Fixed Supply TokenClarity Timelock ContractClarity Allowlist ExampleClarity Simple Message StorageClarity Key-Value Store

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher