Keywords reference

Complete reference for all Clarity keywords including constants, context variables, and blockchain data.


Clarity provides built-in keywords for accessing blockchain state, transaction context, and constant values. These keywords enable smart contracts to interact with the Stacks blockchain and make decisions based on current execution context.

Boolean constants

true

true represents the boolean true value in Clarity.

Type

bool
;; Direct usage
(define-constant is-active true)
;; In conditionals
(if true
(print "This will execute")
(print "This won't execute"))
;; Boolean operations
(and true true) ;; Returns true
(or false true) ;; Returns true

Common patterns include using true for:

  • Default enabled states in configuration
  • Success return values in public functions
  • Feature flags and toggles
(define-data-var protocol-enabled bool true)
(define-public (pause-protocol)
(begin
(asserts! (is-eq tx-sender contract-owner) (err u401))
(var-set protocol-enabled false)
(ok true)))
;; Feature toggle pattern
(define-map features (string-ascii 50) bool)
(map-set features "trading" true)
(map-set features "staking" true)

false

false represents the boolean false value in Clarity.

Type

bool
;; Direct usage
(define-constant is-paused false)
;; In conditionals
(if false
(print "This won't execute")
(print "This will execute"))
;; Boolean operations
(and true false) ;; Returns false
(or false false) ;; Returns false

Common patterns include using false for:

  • Default disabled states
  • Failure indicators
  • Initial unset states
(define-data-var is-initialized bool false)
(define-public (initialize)
(begin
(asserts! (not (var-get is-initialized)) (err u100))
(var-set is-initialized true)
(ok true)))
;; Access control pattern
(define-map admins principal bool)
(define-read-only (is-admin (user principal))
(default-to false (map-get? admins user)))

Optional constant

none

none represents an empty optional value.

Type

(optional ?)
;; Direct usage
(define-constant empty-value none)
;; Optional map returns
(map-get? non-existent-map non-existent-key) ;; Returns none
;; Pattern matching
(match (map-get? balances user)
balance (+ balance u100)
u0) ;; If none, return u0

Common patterns include:

  • Default values for optional types
  • Representing absence of data
  • Initial states before data is set
(define-map user-profiles principal
{
name: (string-ascii 50),
created-at: uint
})
(define-read-only (get-user-name (user principal))
(match (map-get? user-profiles user)
profile (some (get name profile))
none)) ;; Return none if profile doesn't exist
;; Clearing optional data
(define-data-var current-proposal (optional uint) none)
(define-public (clear-proposal)
(begin
(var-set current-proposal none)
(ok true)))

Block information

block-height

block-height returns the current Stacks block height.

Type

uint
;; Get current height
(print block-height)
;; Time-locked functionality
(define-public (claim-rewards)
(begin
(asserts! (> block-height u50000) (err u1))
(ok true)))

Use cases include:

  • Implementing vesting schedules
  • Creating time-based phases
  • Recording timestamps
  • Enforcing delays
(define-constant VESTING_START u100000)
(define-constant VESTING_PERIOD u4320) ;; ~30 days
(define-read-only (get-vested-amount (total uint))
(if (<= block-height VESTING_START)
u0
(let ((blocks-elapsed (- block-height VESTING_START)))
(if (>= blocks-elapsed VESTING_PERIOD)
total
(/ (* total blocks-elapsed) VESTING_PERIOD)))))
;; Phase-based protocol
(define-constant PHASE1_END u100000)
(define-constant PHASE2_END u200000)
(define-read-only (get-current-phase)
(if (<= block-height PHASE1_END)
u1
(if (<= block-height PHASE2_END)
u2
u3)))

burn-block-height

burn-block-height returns the current Bitcoin block height.

Type

uint
;; Get current Bitcoin height
(print burn-block-height)
;; Bitcoin-anchored timing
(define-read-only (get-burn-height-difference)
(- burn-block-height u700000))

Common uses:

  • Bitcoin-anchored timestamps
  • Cross-chain timing coordination
  • Proof-of-Transfer calculations
(define-map btc-price-at-height uint uint)
(define-public (record-btc-price (price uint))
(begin
(map-set btc-price-at-height burn-block-height price)
(ok burn-block-height)))
;; Time calculations based on Bitcoin blocks
(define-read-only (hours-since (btc-height uint))
(/ (- burn-block-height btc-height) u6)) ;; ~6 blocks per hour

stacks-block-height

stacks-block-height returns the current Stacks chain tip height (for Stacks 2.5+).

Type

uint
;; Stacks 2.5+ specific height
(print stacks-block-height)
;; Height comparison
(define-read-only (get-height-info)
{
stacks: stacks-block-height,
block: block-height,
difference: (- stacks-block-height block-height)
})

tenure-height

tenure-height returns the number of tenures (reward cycles) that have passed.

Type

uint
;; Get current tenure
(print tenure-height)
;; Tenure-based rewards
(define-map tenure-rewards uint uint)
(define-read-only (get-rewards-for-tenure (tenure uint))
(default-to u0 (map-get? tenure-rewards tenure)))

Transaction context

tx-sender

tx-sender returns the principal that initiated the current transaction.

Type

principal
;; Get transaction originator
(print tx-sender)
;; Authorization pattern
(define-constant contract-owner tx-sender)
(define-public (admin-only-function)
(begin
(asserts! (is-eq tx-sender contract-owner) (err u401))
(ok true)))

Key characteristics:

  • Remains constant throughout all contract calls
  • Always represents the original transaction initiator
  • Used for authorization and ownership
(define-map balances principal uint)
(define-public (deposit (amount uint))
(begin
(try! (stx-transfer? amount tx-sender (as-contract tx-sender)))
(map-set balances tx-sender
(+ (default-to u0 (map-get? balances tx-sender)) amount))
(ok amount)))
;; Multi-signature pattern
(define-map signers principal bool)
(define-public (add-signature (proposal-id uint))
(begin
(asserts! (default-to false (map-get? signers tx-sender)) (err u401))
;; Add signature logic
(ok true)))

tx-sponsor

tx-sponsor returns the principal paying for the transaction fees (optional).

Type

(optional principal)
;; Check if transaction is sponsored
(match tx-sponsor
sponsor (print { sponsored-by: sponsor })
(print "Not a sponsored transaction"))
;; Get sponsor or sender
(define-read-only (get-fee-payer)
(default-to tx-sender tx-sponsor))

Use cases:

  • Fee subsidy programs
  • Gasless transactions
  • Sponsored onboarding
  • User acquisition
(define-map sponsored-actions { user: principal, sponsor: principal } uint)
(define-public (sponsored-action)
(match tx-sponsor
sponsor (begin
;; Record sponsored action
(map-set sponsored-actions
{ user: tx-sender, sponsor: sponsor }
(+ (default-to u0 (map-get? sponsored-actions
{ user: tx-sender, sponsor: sponsor })) u1))
(ok { user: tx-sender, sponsor: sponsor }))
(err u1))) ;; Must be sponsored
;; Subsidized operations with approved sponsors
(define-map approved-sponsors principal bool)
(define-public (subsidized-transfer (recipient principal) (amount uint))
(match tx-sponsor
sponsor (begin
(asserts! (default-to false (map-get? approved-sponsors sponsor)) (err u401))
(ft-transfer? my-token amount tx-sender recipient))
;; Not sponsored - user pays normally
(ft-transfer? my-token amount tx-sender recipient)))

contract-caller

contract-caller returns the immediate calling principal (contract or user).

Type

principal
;; Get immediate caller
(print contract-caller)
;; Direct-call only
(define-public (direct-only)
(begin
(asserts! (is-eq tx-sender contract-caller) (err u405))
(ok true)))

Important distinctions:

  • Changes with each contract call
  • Equals tx-sender for direct calls
  • Represents calling contract in contract-to-contract calls
;; Contract A
(define-public (call-contract-b)
(contract-call? .contract-b check-callers))
;; Contract B
(define-public (check-callers)
(begin
(print {
tx-sender: tx-sender, ;; Original user
contract-caller: contract-caller ;; Contract A
})
(ok true)))
;; Allowlist pattern
(define-map allowed-contracts principal bool)
(define-public (restricted-function)
(begin
(asserts!
(or
(is-eq tx-sender contract-caller) ;; Direct call
(default-to false (map-get? allowed-contracts contract-caller)))
(err u401))
(ok true)))

Chain information

chain-id

chain-id returns the 32-byte chain identifier.

Type

uint
;; Get chain ID
(print chain-id)
;; Mainnet: 0x00000001
;; Testnet: 0x80000000

is-in-mainnet

is-in-mainnet returns true if contract is running on mainnet.

Type

bool
;; Check if on mainnet
(asserts! is-in-mainnet (err u500))
;; Environment-specific constants
(define-constant TOKEN-SUPPLY
(if is-in-mainnet
u1000000000 ;; 1 billion on mainnet
u1000000)) ;; 1 million on testnet

is-in-regtest

is-in-regtest returns true if contract is running on regtest.

Type

bool
;; Development-only functions
(define-public (debug-function)
(begin
(asserts! is-in-regtest (err u403))
;; Debug logic
(ok true)))

Economic data

stx-liquid-supply

stx-liquid-supply returns the total liquid STX supply in micro-STX.

Type

uint
;; Get current liquid supply
(print stx-liquid-supply)
;; Calculate percentage of supply
(define-read-only (get-percent-of-liquid (amount uint))
(/ (* amount u10000) stx-liquid-supply)) ;; Returns basis points

Use cases:

  • Economic calculations
  • Supply-based limits
  • Tokenomics implementations
  • Governance thresholds
;; Dynamic fee based on supply percentage
(define-constant FEE_BASIS_POINTS u50) ;; 0.5%
(define-read-only (calculate-dynamic-fee (amount uint))
(let (
(supply-ratio (/ (* amount u1000000) stx-liquid-supply))
(multiplier (+ u100 supply-ratio)))
(/ (* amount FEE_BASIS_POINTS multiplier) u10000)))
;; Governance with supply-based thresholds
(define-constant PROPOSAL_THRESHOLD_BASIS u100) ;; 1% of liquid supply
(define-public (create-proposal (description (string-utf8 500)))
(let (
(required-stake (/ (* stx-liquid-supply PROPOSAL_THRESHOLD_BASIS) u10000))
(user-balance (stx-get-balance tx-sender)))
(asserts! (>= user-balance required-stake) (err u402))
(try! (stx-transfer? required-stake tx-sender (as-contract tx-sender)))
(ok required-stake)))