Write a Simple Smart contract using Clarity.

Satyam
5 min readNov 13, 2022

--

Clarity is a new language for writing smart contracts on Stacks Blockchain. If you don’t already know, Stacks is a layer-1 blockchain based on bitcoin, enabling us to write smart contracts and build decentralised apps.

First of all, what you will learn. You will learn to set up a wallet, get some faucets (unreal real tokens on testnet) and write & upload smart contracts to testnet using Clarity right from your browser.

More articles related to Stacks will be coming soon on my medium and my website. Let us get started.

Clarity Language Logo

Setting up your Wallet

We will use stack’s sandbox to directly upload our stacks smart contract. To sign in, we need a stacks wallet. In our case, we will use the Hiro wallet. Btw, what are wallets, “crypto wallets”?

Crypto wallets store your private keys, keeping your crypto safe and accessible. They also allow you to send, receive, and spend cryptocurrencies like Bitcoin and Ethereum.

These are the steps to follow,

  1. Go ahead and download the Hiro Wallet Extension for Chrome from here or Firefox from here.
  2. After installing, click on “Create new wallet”.

3. You will now see your secret key; back it up somewhere very safe and click “I’ve backed it up”.

4. Set a strong password. You will need this when you want to access the wallet. Then, click “Continue”.

5. Click “Skip” in the top-right corner. If you want to buy STX right now, you can XD.

6. Congrats, you have your own “Wallet” now. 🥳 🥳

Getting some Faucets

  1. Go to https://explorer.stacks.co/sandbox/faucet?chain=testnet.

2. Click on “Connect Stacks Wallet”. And Select the Account you want the faucet in.

3. After that, click “Request STX”.

4. If you go to your Wallet extension in the top-right corner (in the browser bar for this case). Click on the menu button in the top-right corner.

5. Now, click on “Change network” inside the menubar. And select “Testnet”.

6. If you have done every step properly, you will have some faucets in your account.

Hurray 🥳 🥳

Writing and Deploying code.

This is our “Hello World” contract.

(define-public (say-hi)
(ok "hello world"))

(define-read-only (echo-number (val int))
(ok val))

The first function is a public function called say-hi.

Public functions in Clarity are callable from other smart contracts, which enables you to break complex tasks into smaller, simpler smart contracts.

The function doesn’t take any parameters and simply returns “hello world” using the okresponse constructor.

The second function is a read-only function called echo-number

Read-only functions are also public functions, but as the name implies, they can’t change any variables or datamaps.

echo-number takes an input parameter of type int and uses an okresponse to return the value passed to the function.

Now, deploy your code. Go to the Stacks Sandbox: https://explorer.stacks.co/sandbox/deploy?chain=testnet

  1. If you have signed in using your wallet, you will see the Clarity Code Editor.
  2. Just change the already-written code to the hello-world code you wrote above.
  3. Change the contract name to what you prefer.

4. Click “Deploy”.

5. Confirm the transaction by clicking “Confirm” in the pop-up.

6. If you go to the menu of Stacks Explorer Sandbox, you can find the transaction details. You might need to reload the website to see the required results.

7. You can get your Transaction details by clicking “View transaction”.

Here is my contract link for example https://explorer.stacks.co/txid/0xc92c65882bf8826cf0debe27b60c66872f8df394c331ec18f29afe4327030dd4?chain=testnet

You just deployed your contract, LFG 🚀🚀

We can even interact with your contract.

  1. First, go to your transaction details on the explorer from the last step. And copy the Contract Name.

2. Select the “f” icon on the left side of the Sandbox. That stands for functions, by the way. And paste the copied name to the first box.

3. Click “Get Contract”.

4. You will see two Functions. Click on the first one, called say-hi . And proceed with the “Call function”. And in the right-hand side Menu, you can see the function has been called.

In my case, the function called transaction id was this.

5. If you use the other echo-number function, give any number. And call the function.

Your response would look like this,

Sheesh, you did it. Congrats on completing this tutorial. 🎉 🎉

You can also try https://clarity.tools/ as an alternative for Stacks Sandbox.

Thank you for reading. If this was helpful, follow me on Medium or Twitter.

Some Resources:

  1. Clarity Book: https://book.clarity-lang.org/ch01-00-getting-started.html
  2. Stacks Docs: https://docs.stacks.co/docs/intro
  3. Hiro’s Docs: https://docs.hiro.so/intro
  4. Stack’s Resources: https://community.stacks.org/stacks-series-resources
  5. A Great Article about what Stacks is? : https://www.hiro.so/blog/crypto-stx-and-hiro-how-i-explain-them-to-my-grandmother

Want to join in the journey to learn Clarity? Do it here.

--

--

Satyam

A Dilettante Writer / Blockchain Developer. (satyam.btc, satyvm.eth)