Weaving Clarity: Safe Smart Contracts for SmartWeave

Image for post
Image for post

TL;DR: At the beginning of September, Arweave and Blockstack announced a joint bounty for enabling use of the safe, decidable Clarity smart contract language on Arweave’s smart contract protocol, SmartWeave. Community member Arto Bendiken, known for creating Gitstamp, won the bounty. This is a guest blog post by Arto describing the need for Clarity and introducing Sworn, his compiler for Clarity on SmartWeave.

The Demo

I’m happy to announce that after a month of hard work, I have released Sworn 1.0, the first stable version of my Clarity to SmartWeave compiler that’s ready for people to kick its (metaphorical) tires. If you’re technically inclined, see a short 2-minute demo of Sworn in action:

Image for post
Image for post

The Bounty

For an erstwhile compiler hacker like me, this was a dream project. It’s not often that I’ve seen bounties for developing a whole compiler, so I knew I had to jump on it. I burned the midnight oil producing a functional proof-of-concept in the shortest possible order, and was gratified to be awarded the bounty. And now, just a little more than a month later, we are able to compile the full Clarity language for deployment on SmartWeave.

The Promise

Why am I so excited about this project? I believe that Clarity is a much-needed evolution towards safer smart contracts. As is already widely understood, poorly-designed smart contracts have resulted in hundreds of millions of dollars worth of mayhem in the broader blockchain ecosystem. The programming language we write smart contracts in matters: a language with fewer footguns would be highly preferable. Clarity is that language.

It’s for these reasons that Arweave decided to adopt Clarity, which was originally developed by Blockstack and Algorand. SmartWeave offers unparallelled flexibility by moving computation from network validators to the contract users themselves. This eliminates gas fees and removes the computation bottleneck that currently plagues Ethereum in particular.

SmartWeave also allows developers to deploy JavaScript contracts that require significant amounts of computation. It’s not a problem, computation-wise, for a contract to get stuck in an infinite loop on SmartWeave, because that would tend to affect mainly the party that wrote that contract. However, from a composability and correctness perspective, a safer language is desirable. (JavaScript contains even more footguns than Solidity.)

The Language

Why is Clarity special? To start with, unlike kitchen-sink smart contract languages such as Solidity, Clarity isn’t Turing complete. Instead, Clarity is decidable, which — put most simply — means that it isn’t possible to write an infinite loop in Clarity. Contracts written in Clarity will definitely halt, and will generally behave nicely and predictably, with good manners.

Despite that, smart contracts in Clarity are easy to write, and the language doesn’t feel restrictive. Indeed, people familiar with functional programming will feel right at home, and programmers familiar with Clojure will love the Lisp-derived simplicity of the syntax. (For others, that may take a few moments to get used to. Don’t worry, the road to enlightenment is littered with irritating, superfluous parentheses.)

Here’s counter.clar, a simple example contract that implements a persistent counter:

(define-data-var counter int 0)
(define-read-only (get-counter) (ok (var-get counter)))
(define-public (increment) (begin (var-set counter (+ (var-get counter) 1)) (ok (var-get counter))))
(define-public (decrement) (begin (var-set counter (- (var-get counter) 1)) (ok (var-get counter))))

The Future

Broader adoption of Clarity is a good thing for safer blockchain services as well as knowledge transfer across different blockchains. Indeed, there is a chance that with sufficient and serious backing, Clarity could in time become an important industry standard for smart contracts across a range of blockchains. As it stands, Clarity is now available on Blockstack and Arweave, and will be soon coming to Ethereum as well. It’s a great time to be learning to program with Clarity.

As for Sworn, this is just the beginning. On the Sworn roadmap, we have more Arweave-specific Clarity functions for easily building profit-sharing tokens and communities. And we have big plans for Sworn 2.0: to facilitate Arweave adoption by developers already familiar with Ethereum, I am exploring how we might ingest existing Solidity contracts as well, preserving sunk investment.

If you’re as excited about these developments as I am, hop on over to #smartweave on Discord and join the conversation!

By Arto Bendiken

A novel data storage blockchain protocol enabling a permanent serverless web and creating truly permanent data storage for the first time.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store