<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Oluwafemi Alofe</title>
    <description>The latest articles on Forem by Oluwafemi Alofe (@oluwafemialofe).</description>
    <link>https://forem.com/oluwafemialofe</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F28485%2F72f40232-e7de-4da2-825c-f1720288b2b7.jpg</url>
      <title>Forem: Oluwafemi Alofe</title>
      <link>https://forem.com/oluwafemialofe</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/oluwafemialofe"/>
    <language>en</language>
    <item>
      <title>Build and Deploy an Onchain Calculator on Stacks Blockchain, using Clarity Programming Language.</title>
      <dc:creator>Oluwafemi Alofe</dc:creator>
      <pubDate>Fri, 04 Nov 2022 10:29:12 +0000</pubDate>
      <link>https://forem.com/oluwafemialofe/build-and-deploy-an-onchain-calculator-on-stacks-blockchain-using-clarity-programming-language-4dg0</link>
      <guid>https://forem.com/oluwafemialofe/build-and-deploy-an-onchain-calculator-on-stacks-blockchain-using-clarity-programming-language-4dg0</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Just 2 weeks ago, I didn't know about the &lt;strong&gt;stacks blockchain&lt;/strong&gt;, and beyond the usual holding of bitcoin as a store of wealth,  I didn't care to find out if its now possible to build dapps on the bitcoin blockchain.&lt;/p&gt;

&lt;p&gt;Well, you can't blame me, becuase since the emergence of ethereum which purpose was to allow buidling of programable apps due to bitcoin failure of allowing that. I pretty much closed my mind to that possibility. &lt;/p&gt;

&lt;p&gt;Certainly, I will put that kind of idealogy in check as should you. In the blockchain space, new technologies are emerging every now and then and to keep up with pace you have to stay open and keep up to date.&lt;/p&gt;

&lt;p&gt;That been said, lets get to it.&lt;/p&gt;

&lt;h3&gt;
  
  
  What you Will  Learn
&lt;/h3&gt;

&lt;p&gt;In this tutorial, you will learn the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is Stacks blockchain and why you should consider it&lt;/li&gt;
&lt;li&gt;Introduction to Clarity, Stacks own version of Solidity language&lt;/li&gt;
&lt;li&gt;How to build and deploy a smart contract on Clarity testnet&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Difficulty
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Beginner ✅&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Requirement
&lt;/h3&gt;

&lt;p&gt;For this tutorial, you would require.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Visual studio IDE &lt;/li&gt;
&lt;li&gt;A Linux or Mac terminal&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For Windows users, though I haven't tried to set up the Clarity development environment on a Windows computer. But if you took any extra steps that this article does not mention, be sure to leave a comment for other readers to see.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Content
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Overview

&lt;ul&gt;
&lt;li&gt;What is stacks&lt;/li&gt;
&lt;li&gt;Stacking&lt;/li&gt;
&lt;li&gt;How does stacks settle transactions&lt;/li&gt;
&lt;li&gt;Why you should consider using stacks&lt;/li&gt;
&lt;li&gt;What stacks blockchain is not&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Clarity Language&lt;/li&gt;

&lt;li&gt;Setup Clarity Software&lt;/li&gt;

&lt;li&gt;Setup Project&lt;/li&gt;

&lt;li&gt;Deploy Contract on Testnet

&lt;ul&gt;
&lt;li&gt; Install Hiro wallet&lt;/li&gt;
&lt;li&gt;Fund wallet from faucet&lt;/li&gt;
&lt;li&gt;Deploy&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is Stacks
&lt;/h3&gt;

&lt;p&gt;Stacks 2.0 is a layer-1 blockchain that connects to Bitcoin and brings smart contracts and decentralized apps to it. Smart contracts and apps developed on the Stacks platform are natively integrated with the security, stability, and economic power of Bitcoin.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stacking
&lt;/h3&gt;

&lt;p&gt;Bitcoins used for miner bids are sent to a set of specific addresses corresponding to Stacks (STX) token holders that are actively participating in consensus ("Stackers"). Thus, rather than being destroyed, the bitcoins consumed in the mining process go to productive Stacks holders as a reward based on their holdings of Stacks and participation in the Stacking algorithm.&lt;/p&gt;

&lt;h3&gt;
  
  
  How does stack settle transactions
&lt;/h3&gt;

&lt;p&gt;Stacks layer 1 blockchain connects to Bitcoin by its consensus mechanism called PoX(Proof of Transfer), which spans the two chains. This enables Stacks to leverage Bitcoin’s security and enables Stacks apps to use Bitcoin’s state, despite being a separate blockchain.&lt;/p&gt;

&lt;p&gt;PoX connects to Bitcoin with a 1:1 block ratio, meaning anything that happens on the Stacks blockchain can be verified on the Bitcoin Blockchain.&lt;/p&gt;

&lt;p&gt;Transactions are the fundamental unit of execution in the Stacks blockchain. Each transaction is originates from a Stacks 2.0 account, and is retained in the Stacks blockchain history for eternity. This guide helps you understand Stacks 2.0 transactions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdocs.stacks.co%2Fassets%2Fimages%2Ftx-lifecycle-6cd7e053b6eff31fdc23d3e51f6e96fb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdocs.stacks.co%2Fassets%2Fimages%2Ftx-lifecycle-6cd7e053b6eff31fdc23d3e51f6e96fb.png" alt="How does stack settles transaction"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Why you should consider using stacks
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Unlocking Bitcoin capital:&lt;/strong&gt; Until recently, Bitcoin could not be used on the Defi platform in the same way that its counterpart, Ethereum, could. Stacks unlocks the hundreds of billions of dollars in Bitcoin capital, giving Bitcoin users new ways to use and earn BTC while giving dapp developers new opportunities..  &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Bitcoin’s security:&lt;/strong&gt; A transaction is nearly impossible to reverse or change once it has settled on Bitcoin. These settlement assurances are critical to the base layer of a network of decentralized apps. Settling the system on Bitcoin grants Stacks novel security properties not seen in other blockchains — it guarantees that all Stacks forks are public and helps nodes identify the canonical Stacks fork and find Stacks blocks they have not yet downloaded.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Bitcoin’s network effects:&lt;/strong&gt; The world is converging on Bitcoin as a standard, and demand for use cases around Bitcoin is increasing. There is hundreds of billions of capital locked into Bitcoin, most of it as a passive store of value. Instead of competing with Bitcoin, Stacks can provide novel use cases for holders of bitcoin, such as bitcoin-backed loans, bitcoin DeFi or using your bitcoin to trade NFTs.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What Stack blockchain is not
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Stacks is NOT a PoS chain:&lt;/strong&gt; The act of block production requires an extrinsic expenditure — it is not tied to owning the native token, as would be required in PoS. The only way to produce blocks in the Stacks chain is to transfer Bitcoin to a predetermined randomized list of Bitcoin addresses.  The Stacks blockchain can fork, and there exists a protocol to rank forks by quality independent of the set of miners and the tokens they hold, unlike PoS chain which cannot fork due to the inability to identify a canonical fork without trusting a 3rd party to decree a particular fork as valid.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Stacks is NOT a sidechain:&lt;/strong&gt; First, the history of all Stacks blocks produced is recorded to Bitcoin. This means that the act of producing a private Stacks fork is at least as hard as reorging the Bitcoin chain. Second, the Stacks blockchain has its own token; it does not represent pegged Bitcoin. This means that the safety of the canonical fork of the Stacks blockchain is underpinned by its entire token economy’s value, whereas the safety of a sidechain’s canonical fork is underpinned only by whatever system-specific measures incentivize its validators to produce blocks honestly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Stacks is NOT a layer-2 system for Bitcoin:&lt;/strong&gt;  it's a sovereign system in its own right. The Stacks blockchain state is distinct from Bitcoin, and is wholly maintained by and for Stacks nodes. Stacks transactions are separate from Bitcoin transactions. Layer-2 systems like Lightning are designed to help scale Bitcoin payment transactions, whereas Stacks is designed to bring new use-cases to Bitcoin through smart contracts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Stacks is NOT a merged-mined chain:&lt;/strong&gt;  The only block producers on the Stacks chain are Stacks miners. Bitcoin miners do not participate in Stacks block validation, and do not claim Stacks block rewards.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Clarity Language
&lt;/h2&gt;

&lt;p&gt;Clarity is a new language for smart contracts on the Stacks blockchain. The Clarity smart contract language optimizes for predictability and security.&lt;/p&gt;

&lt;p&gt;Clarity is distinct from other languages designed for writing smart contracts in a few ways:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Predictability&lt;/strong&gt;: The Clarity language uses precise and unambiguous syntax that allows developers to predict exactly how their contracts will be executed.&lt;br&gt;
&lt;strong&gt;Security&lt;/strong&gt;: The Clarity language allows users to supply their own conditions for transactions that ensure that a contract may never unexpectedly transfer a token owned by a user.&lt;br&gt;
&lt;strong&gt;No compiler&lt;/strong&gt;: Contracts written in Clarity are broadcasted on the Stacks blockchain exactly as they are written by developers. This ensures that the code developers wrote, analyzed, and tested, is exactly what gets executed.&lt;/p&gt;

&lt;p&gt;One of the first thing you need to understand while learning a new language is the Types. In Clarity there are 11 Types system listed on the documentation which includes the following.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;int&lt;/td&gt;
&lt;td&gt;signed 128-bit integer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;uint&lt;/td&gt;
&lt;td&gt;unsigned 128-bit integer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;bool&lt;/td&gt;
&lt;td&gt;boolean value (&lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;principal&lt;/td&gt;
&lt;td&gt;object representing a principal (whether a contract principal or standard principal)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;(buff max-len)&lt;/td&gt;
&lt;td&gt;byte buffer of maximum length &lt;code&gt;max-len&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;(string-ascii max-len)&lt;/td&gt;
&lt;td&gt;ASCII string of maximum length &lt;code&gt;max-len&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;(string-utf8 max-len)&lt;/td&gt;
&lt;td&gt;UTF-8 string of maximum length &lt;code&gt;max-len&lt;/code&gt; (u"A smiley face emoji \u{1F600} as a utf8 string")&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;(list max-len entry-type)&lt;/td&gt;
&lt;td&gt;list of maximum length &lt;code&gt;max-len&lt;/code&gt;, with entries of type &lt;code&gt;entry-type&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;{label-0: value-type-0, label-1: value-type-1, ...}&lt;/td&gt;
&lt;td&gt;tuple, group of data values with named fields&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;(optional some-type)&lt;/td&gt;
&lt;td&gt;an option type for objects that can either be &lt;code&gt;(some value)&lt;/code&gt; or &lt;code&gt;none&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;(response ok-type err-type)&lt;/td&gt;
&lt;td&gt;object used by public functions to commit their changes or abort. May be returned or used by other functions as well, however, only public functions have the commit/abort behavior.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If you are coming from the Solidity programming world to Clarity. Things can be a little bit weired at first. With no opening and closing curly braces, instead many closing and opening brackets, it can tend to get confusing, but stick with me, you will soon deploy your first contract on Stacks.&lt;/p&gt;

&lt;p&gt;Comment in Clarity&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;;;&lt;/span&gt; &lt;span class="nx"&gt;This&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;comment&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Adding two numbers in clarity&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;;&lt;/span&gt; &lt;span class="nx"&gt;Returns&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;;&lt;/span&gt; &lt;span class="nx"&gt;Returns&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same above sytax applies several mathematical expression.&lt;/p&gt;

&lt;p&gt;Get a variable value&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;define&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;cursor&lt;/span&gt; &lt;span class="nx"&gt;int&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt; &lt;span class="nx"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;;&lt;/span&gt; &lt;span class="nx"&gt;Returns&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Set a variable value&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;define&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;cursor&lt;/span&gt; &lt;span class="nx"&gt;int&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt; &lt;span class="nx"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;;&lt;/span&gt; &lt;span class="nx"&gt;Returns&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt; &lt;span class="nf"&gt;cursor &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt; &lt;span class="nx"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;;;&lt;/span&gt; &lt;span class="nx"&gt;Returns&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt; &lt;span class="nx"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;;&lt;/span&gt; &lt;span class="nx"&gt;Returns&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If statement example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;;&lt;/span&gt; &lt;span class="nx"&gt;Returns&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; 

&lt;span class="p"&gt;;;&lt;/span&gt; &lt;span class="nx"&gt;If&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="nf"&gt;expression &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; 
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;;&lt;/span&gt; &lt;span class="nx"&gt;Returns&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Defining a private function with name &lt;code&gt;max-of&lt;/code&gt; that takes two parameters &lt;code&gt;i1&lt;/code&gt; and &lt;code&gt;i2&lt;/code&gt; of type integer and return which is greater will be defined as below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;define&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nf"&gt;private &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;max&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="k"&gt;of &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i1&lt;/span&gt; &lt;span class="nx"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i2&lt;/span&gt; &lt;span class="nx"&gt;int&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;i1&lt;/span&gt; &lt;span class="nx"&gt;i2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="nx"&gt;i1&lt;/span&gt;
      &lt;span class="nx"&gt;i2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;max&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;;&lt;/span&gt; &lt;span class="nx"&gt;Returns&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You get the Idea? &lt;/p&gt;

&lt;p&gt;In Clarity, you can define functions as &lt;code&gt;define-private&lt;/code&gt; , &lt;code&gt;define-public&lt;/code&gt;, &lt;code&gt;define-read-only&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now that we have touched enough bascis of what Stack blockchain is and Clarity programming language. Let us write and deploy a smart contract on Clarity testnet, our smart contract is a simple one with functions to add, multiply, subtract and divide numbers of type integers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup Clarity Software
&lt;/h2&gt;

&lt;p&gt;Before we start writing any code, we need to setup twi things.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Install &lt;strong&gt;Clarinet&lt;/strong&gt;, runtime packaged as a command line tool, designed to facilitate smart contract development. Think of it as hardhat console or Truffle console.&lt;/li&gt;
&lt;li&gt;Install &lt;strong&gt;Clarity plugin for Visaul studio&lt;/strong&gt; editor for syntax higlighting and auto suggestions.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Clarinet Installation
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Install on macOS (Homebrew)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;brew &lt;span class="nb"&gt;install &lt;/span&gt;clarinet
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Install on Windows
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;winget &lt;span class="nb"&gt;install &lt;/span&gt;clarinet
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Install from a pre-built binary
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;wget &lt;span class="nt"&gt;-nv&lt;/span&gt; https://github.com/hirosystems/clarinet/releases/download/v0.27.0/clarinet-linux-x64-glibc.tar.gz &lt;span class="nt"&gt;-O&lt;/span&gt; clarinet-linux-x64.tar.gz
&lt;span class="nb"&gt;tar&lt;/span&gt; &lt;span class="nt"&gt;-xf&lt;/span&gt; clarinet-linux-x64.tar.gz
&lt;span class="nb"&gt;chmod&lt;/span&gt; +x ./clarinet
&lt;span class="nb"&gt;mv&lt;/span&gt; ./clarinet /usr/local/bin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can read more about other installation options &lt;a href="https://github.com/hirosystems/clarinet" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Upon successful installation, open your terminal and run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;clarinet
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Usage of how to use the command will be printed in the terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;clarinet-cli 0.33.0

USAGE:
    clarinet &amp;lt;SUBCOMMAND&amp;gt;

OPTIONS:
    &lt;span class="nt"&gt;-h&lt;/span&gt;, &lt;span class="nt"&gt;--help&lt;/span&gt;       Print &lt;span class="nb"&gt;help &lt;/span&gt;information
    &lt;span class="nt"&gt;-V&lt;/span&gt;, &lt;span class="nt"&gt;--version&lt;/span&gt;    Print version information

SUBCOMMANDS:
    chainhooks      Subcommands &lt;span class="k"&gt;for &lt;/span&gt;working with chainhooks
    check           Check contracts syntax
    completions     Generate shell completions scripts
    console         Load contracts &lt;span class="k"&gt;in &lt;/span&gt;a REPL &lt;span class="k"&gt;for &lt;/span&gt;an interactive session
    contracts       Subcommands &lt;span class="k"&gt;for &lt;/span&gt;working with contracts
    dap             Step by step debugging and breakpoints from your code editor &lt;span class="o"&gt;(&lt;/span&gt;VSCode, vim,
                        emacs, etc&lt;span class="o"&gt;)&lt;/span&gt;
    deployments     Manage contracts deployments on Simnet/Devnet/Testnet/Mainnet
    &lt;span class="nb"&gt;help            &lt;/span&gt;Print this message or the &lt;span class="nb"&gt;help &lt;/span&gt;of the given subcommand&lt;span class="o"&gt;(&lt;/span&gt;s&lt;span class="o"&gt;)&lt;/span&gt;
    integrate       Start a &lt;span class="nb"&gt;local &lt;/span&gt;Devnet network &lt;span class="k"&gt;for &lt;/span&gt;interacting with your contracts from your
                        browser
    lsp             Get Clarity autocompletion and inline errors from your code editor &lt;span class="o"&gt;(&lt;/span&gt;VSCode,
                        vim, emacs, etc&lt;span class="o"&gt;)&lt;/span&gt;
    new             Create and scaffold a new project
    requirements    Interact with contracts deployed on Mainnet
    run             Execute Clarinet extension
    &lt;span class="nb"&gt;test            &lt;/span&gt;Execute &lt;span class="nb"&gt;test &lt;/span&gt;suite

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Visual Studio Clarity Setup
&lt;/h3&gt;

&lt;p&gt;Open your visual studio&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup Project
&lt;/h2&gt;

&lt;p&gt;Navigate to your development project directory and run the following command. Enter &lt;strong&gt;Y&lt;/strong&gt; when requested to do so, you will get a bunch of logs about directory and files created if this was successful.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;clarinet new clarity-calculator-demo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The log may look somewhat like this&lt;br&gt;
 &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fs3.amazonaws.com%2Falofe.oluwafemi%2FScreen%2BShot%2B2022-09-24%2Bat%2B6.34.15%2BPM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fs3.amazonaws.com%2Falofe.oluwafemi%2FScreen%2BShot%2B2022-09-24%2Bat%2B6.34.15%2BPM.png" alt="Clarinet output"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Navigate to the newly created directory from your terminal&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;clarity-calculator-demo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And inside the clarity-calculator-demo directory, create a new smart contract with name summation by running.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;clarinet contract new summation
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create &lt;code&gt;summation.clar&lt;/code&gt; in the contracts directory and &lt;code&gt;summation_test.ts&lt;/code&gt; in the tests directory. Open the your Visual studio IDE and add the first function to add two numbers of type integer in &lt;code&gt;contracts/summation.clar&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;By default the place holder comment there will be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;;;&lt;/span&gt; &lt;span class="nx"&gt;summation&lt;/span&gt;
&lt;span class="p"&gt;;;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;description&lt;/span&gt; &lt;span class="nx"&gt;here&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="p"&gt;;;&lt;/span&gt; &lt;span class="nx"&gt;constants&lt;/span&gt;
&lt;span class="p"&gt;;;&lt;/span&gt;

&lt;span class="p"&gt;;;&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="nx"&gt;maps&lt;/span&gt; &lt;span class="nx"&gt;and&lt;/span&gt; &lt;span class="nx"&gt;vars&lt;/span&gt;
&lt;span class="p"&gt;;;&lt;/span&gt;

&lt;span class="p"&gt;;;&lt;/span&gt; &lt;span class="kr"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;functions&lt;/span&gt;
&lt;span class="p"&gt;;;&lt;/span&gt;

&lt;span class="p"&gt;;;&lt;/span&gt; &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;functions&lt;/span&gt;
&lt;span class="p"&gt;;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace it with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;;;&lt;/span&gt; &lt;span class="nx"&gt;summation&lt;/span&gt;
&lt;span class="p"&gt;;;&lt;/span&gt; &lt;span class="nx"&gt;Calculator&lt;/span&gt; &lt;span class="nx"&gt;contract&lt;/span&gt;

&lt;span class="p"&gt;;;&lt;/span&gt; &lt;span class="nx"&gt;Adds&lt;/span&gt; &lt;span class="nx"&gt;two&lt;/span&gt; &lt;span class="nx"&gt;integer&lt;/span&gt; &lt;span class="nx"&gt;inputs&lt;/span&gt; &lt;span class="nx"&gt;and&lt;/span&gt; &lt;span class="nx"&gt;returns&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; 
&lt;span class="p"&gt;;;&lt;/span&gt; &lt;span class="nx"&gt;In&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;an&lt;/span&gt; &lt;span class="nx"&gt;overflow&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;throws&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;runtime&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;define&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;read&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nf"&gt;only &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nf"&gt;number &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="nx"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;ok &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we define a &lt;code&gt;read-only&lt;/code&gt; function &lt;code&gt;add-number&lt;/code&gt; i.e it cannot modify data. The function takes in two integer &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; and sum them together. It then returns the result back. Thekeyword &lt;code&gt;ok&lt;/code&gt; is used to return value in this function as seen above.&lt;/p&gt;

&lt;p&gt;Similarly we can add more functions to subtract, multiply and divide two number of type integers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;;;&lt;/span&gt; &lt;span class="nx"&gt;Subtract&lt;/span&gt; &lt;span class="nx"&gt;two&lt;/span&gt; &lt;span class="nx"&gt;integer&lt;/span&gt; &lt;span class="nx"&gt;inputs&lt;/span&gt; &lt;span class="nx"&gt;and&lt;/span&gt; &lt;span class="nx"&gt;returns&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; 
&lt;span class="p"&gt;;;&lt;/span&gt; &lt;span class="nx"&gt;In&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;an&lt;/span&gt; &lt;span class="nx"&gt;overflow&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;throws&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;runtime&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;define&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nf"&gt;public &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sub&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nf"&gt;number &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="nx"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;ok &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;

&lt;span class="p"&gt;;;&lt;/span&gt; &lt;span class="nx"&gt;Multiply&lt;/span&gt; &lt;span class="nx"&gt;two&lt;/span&gt; &lt;span class="nx"&gt;integer&lt;/span&gt; &lt;span class="nx"&gt;inputs&lt;/span&gt; &lt;span class="nx"&gt;and&lt;/span&gt; &lt;span class="nx"&gt;returns&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; 
&lt;span class="p"&gt;;;&lt;/span&gt; &lt;span class="nx"&gt;In&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;an&lt;/span&gt; &lt;span class="nx"&gt;overflow&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;throws&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;runtime&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;define&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nf"&gt;public &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mul&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nf"&gt;number &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="nx"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;ok &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;

&lt;span class="p"&gt;;;&lt;/span&gt; &lt;span class="nx"&gt;Divide&lt;/span&gt; &lt;span class="nx"&gt;two&lt;/span&gt; &lt;span class="nx"&gt;integer&lt;/span&gt; &lt;span class="nx"&gt;inputs&lt;/span&gt; &lt;span class="nx"&gt;and&lt;/span&gt; &lt;span class="nx"&gt;returns&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; 
&lt;span class="p"&gt;;;&lt;/span&gt; &lt;span class="nx"&gt;In&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;an&lt;/span&gt; &lt;span class="nx"&gt;overflow&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;throws&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;runtime&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;define&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nf"&gt;public &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nf"&gt;number &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="nx"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;ok &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/ a b&lt;/span&gt;&lt;span class="se"&gt;))&lt;/span&gt;&lt;span class="err"&gt;)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open your Visual studio IDE terminal in the project directory and open &lt;code&gt;clarinet console&lt;/code&gt; by running.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;clarinet console
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fs3.amazonaws.com%2Falofe.oluwafemi%2FScreen%2BShot%2B2022-09-24%2Bat%2B6.51.20%2BPM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fs3.amazonaws.com%2Falofe.oluwafemi%2FScreen%2BShot%2B2022-09-24%2Bat%2B6.51.20%2BPM.png" alt="Clarity console"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Table one list contracts we have and their respective functions and in our case we only have &lt;code&gt;summation&lt;/code&gt; contract, while table two list prefunded addresses.&lt;/p&gt;

&lt;p&gt;To interact with the function inside clarinet console. Let us try add two numbers, to do so run this in your console.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Add Function&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;(&lt;/span&gt;contract-call? .summation add-number 1 2&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;(&lt;/span&gt;ok 3&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Subtract Function&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;(&lt;/span&gt;contract-call? .summation sub-number 5 2&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;(&lt;/span&gt;ok 3&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Multiply Function&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;(&lt;/span&gt;contract-call? .summation mul-number 5 2&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;(&lt;/span&gt;ok 10&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Divide Function&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;(&lt;/span&gt;contract-call? .summation div-number 6 2&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;(&lt;/span&gt;ok 3&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Deploy Contract on Testnet
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Install Hiro Wallet
&lt;/h3&gt;

&lt;p&gt;To deploy on Stacks blockchain, we would be needing a deployer wallet with some test token. &lt;/p&gt;

&lt;p&gt;Stacks blockchain is compatitble with Hiro Wallet, so head to chrome store and download the &lt;a href="https://chrome.google.com/webstore/detail/hiro-wallet/ldinpeekobnhjjdofggfgjlcehhmanlj" rel="noopener noreferrer"&gt;browser extension&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Setup &lt;strong&gt;Hiro Wallet&lt;/strong&gt; by clicking on &lt;strong&gt;Create New Wallet&lt;/strong&gt; and following the prompt. Copy the seed phrase of the wallet and use it to replace mnemonic value in &lt;code&gt;settings/Testnet.toml&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight toml"&gt;&lt;code&gt;&lt;span class="py"&gt;mnemonic&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"&amp;lt;YOUR PRIVATE TESTNET MNEMONIC HERE&amp;gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Fund Wallet in Faucet
&lt;/h3&gt;

&lt;p&gt;Switch your wallet network from mainnet to testnet.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fs3.amazonaws.com%2Falofe.oluwafemi%2FScreen%2BShot%2B2022-09-19%2Bat%2B7.56.39%2BPM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fs3.amazonaws.com%2Falofe.oluwafemi%2FScreen%2BShot%2B2022-09-19%2Bat%2B7.56.39%2BPM.png" alt="Request Stacks token"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Visit &lt;a href="https://explorer.stacks.co/sandbox/faucet?chain=testnet" rel="noopener noreferrer"&gt;https://explorer.stacks.co/sandbox/faucet?chain=testnet&lt;/a&gt; to request some test STX tokens. Connect your Hiro wallet and click on &lt;strong&gt;Request STX&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fs3.amazonaws.com%2Falofe.oluwafemi%2FScreen%2BShot%2B2022-09-19%2Bat%2B7.54.50%2BPM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fs3.amazonaws.com%2Falofe.oluwafemi%2FScreen%2BShot%2B2022-09-19%2Bat%2B7.54.50%2BPM.png" alt="enter image description here"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fs3.amazonaws.com%2Falofe.oluwafemi%2FScreen%2BShot%2B2022-09-19%2Bat%2B7.55.13%2BPM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fs3.amazonaws.com%2Falofe.oluwafemi%2FScreen%2BShot%2B2022-09-19%2Bat%2B7.55.13%2BPM.png" alt="enter image description here"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You will receive &lt;strong&gt;500 STX&lt;/strong&gt; in your wallet.  Now we are ready to deploy our smart contract to testnet.&lt;/p&gt;

&lt;h3&gt;
  
  
  Deploy
&lt;/h3&gt;

&lt;p&gt;In your terminal, run teh below command to generate the deployment file for testnet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;clarinet deployments generate &lt;span class="nt"&gt;--testnet&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Generated file deployments/default.testnet-plan.yaml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use this deployment file, to deploy to testnet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;clarinet deployments apply &lt;span class="nt"&gt;-p&lt;/span&gt; deployments/default.testnet-plan.yaml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;The following deployment plan will be applied:
&lt;span class="nt"&gt;---&lt;/span&gt;
&lt;span class="nb"&gt;id&lt;/span&gt;: 0
name: Testnet deployment
network: testnet
stacks-node: &lt;span class="s2"&gt;"https://stacks-node-api.testnet.stacks.co"&lt;/span&gt;
bitcoin-node: &lt;span class="s2"&gt;"http://blockstack:blockstacksystem@bitcoind.testnet.stacks.co:18332"&lt;/span&gt;
plan:
  batches:
    - &lt;span class="nb"&gt;id&lt;/span&gt;: 0
      transactions:
        - contract-publish:
            contract-name: summation
            expected-sender: ST1HBAYT92N9T7XW7PZ20FTDQKKPSTTCQRZ63VR4S
            cost: 7200
            path: contracts/summation.clar
            anchor-block-only: &lt;span class="nb"&gt;true


&lt;/span&gt;Total cost: 0.00007200 STX
Duration:   1 blocks

Continue &lt;span class="o"&gt;[&lt;/span&gt;Y/n]?
y
✔ Transactions successfully confirmed on Testnet
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Congratulations 🎉🎉
&lt;/h3&gt;

&lt;p&gt;You have successfully written and deployed a smart contract on stacks blockchain.&lt;/p&gt;

&lt;h3&gt;
  
  
  Confirmation
&lt;/h3&gt;

&lt;p&gt;If you go back to the sandbox &lt;a href="https://explorer.stacks.co/sandbox/faucet?chain=testnet" rel="noopener noreferrer"&gt;https://explorer.stacks.co/sandbox/faucet?chain=testnet&lt;/a&gt; and clikc on the left hamburger menu to see the last transaction history. You will see its the new deployed contract and the balance has reduced by a very small fraction because of the fee paid for the deployment.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fs3.amazonaws.com%2Falofe.oluwafemi%2FScreen%2BShot%2B2022-09-24%2Bat%2B8.17.17%2BPM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fs3.amazonaws.com%2Falofe.oluwafemi%2FScreen%2BShot%2B2022-09-24%2Bat%2B8.17.17%2BPM.png" alt="Hiro Wallet"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>blockchain</category>
      <category>stacks</category>
      <category>web3</category>
      <category>clarity</category>
    </item>
  </channel>
</rss>
