Clarity Smart Contracts
What is a smart contract?
In the 90s, Nick Szabo coined the term "Smart Contract" when describing the notion of a digital protocol designed to facilitate, verify, or enforce the terms of an agreement without the need for a third party, where the right inputs guarantee a certain output. The full possibilities of this protocol became a reality when blockchain technology emerged.
Smart contracts work by following simple "if/when...then..." statements that are written into code on a blockchain, meaning they are blockchain programs designed to run autonomously when predefined events or actions occur. There is no central authority necessary to run the software to function seamlessly: the terms of a smart contract are specified in code, thus eliminating the need for human intervention. Similarly to a vending machine, once you drop a coin in and select your option, a predefined automated routine is executed. Because the code is stored on the blockchain and cryptography protects all documents, the terms can't be tampered with. A blockchain-based smart contract can have several use cases and is visible to all users of said blockchain, and all Bitcoin transactions recorded on the blockchain are considered on-chain transactions.
What is the Clarity programming language?
Clarity is open-source and supported by the Stacks ecosystem, Hiro PBC, & Algorand. It is a decidable language, meaning you can know, with certainty, from the code itself, what the program will do. Clarity is interpreted (not compiled) & the source code is published on the blockchain. Clarity gives developers a safe way to build complex smart contracts for the world's most secure blockchain, bringing smart contracts to Bitcoin.
Bitcoin, created in 2009 when Satoshi Nakamoto minted the genesis block, was rarely a part of the discussion on smart contracts until just a few years ago. It was designed to be a decentralized cryptocurrency, leaving out smart contract functionality. It therefore has limited scripting language and prioritizes security over programmability, making it difficult for developers to work with the syntax.
Stacks 2.0 (formerly Blockstack), made bitcoin smart contracts possible, allowing developers to build Web3 decentralized apps beyond Ethereum and other blockchains, while enjoying the security of Bitcoin. Stacks functions as the smart contract layer for Bitcoin, through the Clarity smart contract programming language. Since Clarity is human readable, it allows users to verify the code, essentially making the Stacks blockchain a 'GitHub for smart contracts', and gives them confidence that the smart contract code is the same as the source code, which is harder to verify in a compiled language. The use of the Clarity programming language on the Stacks blockchain makes NFT marketplaces such as Gamma.io and other decentralized apps possible.
How does Clarity work?
The Clarity smart contract language supports programmatic control over digital assets within the Stacks blockchain (for example, BNS names, Stacks tokens, DeFi, NFTs and many other use cases). But how?ā
Clarity differs from most other smart contract languages in two essential ways: it is decidable (not Turing complete) and it is interpreted and broadcast on the blockchain as is (not compiled).
It is decidable
Clarity is a decidable language. A programming language is decidable if you can know, with certainty, from the code itself what the program will do. Clarity is intentionally Turing incomplete as it avoids "Turing complexity." This allows for complete static analysis of the entire call graph of a given smart contract. Further, its support for types and type checker can eliminate whole classes of bugs such as unintended casts, reentrancy bugs, and more. You can also analyze Clarity code for runtime cost and data usage. This empowers developers to predict what a given Clarity program will do, and how much it will cost.
When the DAO was famously hacked, it required a contentious hard fork of the underlying chain to remedy the situation. If a decidable language had been used instead, this might have been avoided. Solidity, the implementation language for the DAO inspired from Javascript language, is an undecidable language: it is impossible to know precisely how a contract will behave in certain situations without actually executing it in those situations. There are advantages to both types of smart contract languages, but minimizing risk is critical.
It is interpreted
Using an interpreted language ensures that the executed code is human-readable and auditable. A decidable language like Clarity makes it possible to determine precisely which code is executed, for any function.
The contract source code itself is published and executed by blockchain nodes. Removing any intermediate, compiled representation (e.g., EVM byte code for Solidity) further reduces the risk of introducing bugs. Publishing the contract source code also optimizes understandability. Compiler bugs are doubly damaging in blockchains because while the programmed source code may not have an error, the eventual program reaching the blockchain could. Any such errors would require contentious hard forks --- which are potentially infeasible --- to remedy.
Technical aspects
A Clarity smart contract consists of two parts, a data space and a set of functions. Only the associated smart contract can modify its corresponding data space on the blockchain. Functions may be private and thus callable only from within the smart contract, or public and thus callable from other contracts. Users call smart contracts' public functions by broadcasting a transaction on the blockchain which invokes the public function. Contracts can also call public functions from other smart contracts.
Here are some of the rules and limitations of the Clarity language: the only primitive types are booleans, integers, buffers, and principals; recursion is illegal and there are no anonymous functions; Looping is only performed via map
, filter
, or fold
; there is support for lists but there is no support for list operations like append or join; variables are immutable.
Read more
If you would like to further read about the Clarity programming language, you can head over to the Clarity site and find tutorials and docs about Clarinet, which is the go-to tool for local smart contract development and lets developers easily test and debug their contracts.