Skip to main content

Calculator Smart Contract

Introduction

In this level we will write a very simple calculator smart contract in Clarity and deploy it on the Stacks Testnet. There will be functions in the smart contract for addition, subtraction, multiplication and division.

So let's just get into it ๐Ÿš€

Development

Follow the steps listed below:

  1. Navigate to Clarity Tools and clear all the pre-written code, we will write everything from scratch ;)

  2. Let's start by creating a read-only function called add that accepts two integers called x and y and returns their sum. You don't have to pay gas fees in order to call read-only functions. define-read-only is the keyword used to define read only functions.

(define-read-only (add (x int) (y int))  
(+ x y))

;; You don't have to write a return keyword to return a value in Clarity
;; Just write the expression that you want to return, like (+ x y) will return the sum of x and y
  1. Similarly, let's create functions for subtraction, multiplication and division as well. We just need to change the name of the functions and the operators.
(define-read-only (subtract (x int) (y int))  
(- x y))

(define-read-only (multiply (x int) (y int))
(* x y))

(define-read-only (divide (x int) (y int))
(/ x y))
note

Please note that Clarity does not support floating point numbers, so the division function will always return the integer part of the result. For example if we pass 5 and 2 to the divide function it will return 2 instead of 2.5

  1. Now let's test these functions by just calling them in ClarityTools, you should be able to see the returned value on the left side of the screen in green.

  2. Everything looks good! Let's just delete the function calls and deploy the contract.

Deployment

  1. We can deploy it through ClarityTools using the "deploy contract" option in the toolbox (in the top right corner) but we won't be using it becuase we can't specify a name for our contract in ClarityTools. It just picks up a random name for the smart contract by itself.

  2. Copy the whole code and navigate to Stacks Explorer Sandbox. Stacks Explorer Sandbox is a tool by Stacks Explorer where we can interact with the contracts and deploy them.

  3. Connect your wallet by clicking on the "Connect Stacks Wallet" buttton.

  4. Paste the whole code in Stacks Explorer Sandbox and give your contract a name in the "contract name" field above the "deploy" button. I am going to call it "calculator".

  5. Deploy the contract by clicking on the "Deploy" button and signing the transaction in your wallet.

  6. Once you sign the transaction, copy your address from your wallet and search for it in the explorer.

  7. You should see a pending transaction for the deployment of "calculator" smart contract. This will take 10 - 15 minutes to be confirmed. Wait for it to be confirmed.

Interacting with the contract

  1. Once the transaction is confirmed, click on it and copy the "contract name", it's basically youraddress.contractname.

  2. Navigate to this link (Stacks Explorer Sandbox for interacting with contracts) and paste your address and contract name in the input fields on left side. Then click "Get Contract".

  3. This should show you the calculator contract that we just deployed. On the right side click on any of the functions of the contract to call them.

  4. Call any function by passing parameters to it.

  5. It should give you the result back in response.

Congratulations ๐Ÿฅณ You did it!!

You just wrote a calculator smart contract and deployed it on the Stacks testnet.

LFG ๐Ÿš€๐Ÿš€๐Ÿš€

note

Please submit your deployed contract's principal by clicking here to qualify this level.

Anything unclear or issue in this track? Please connect at Twitter!