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 🚀
Follow the steps listed below:
Navigate to Clarity Tools and clear all the pre-written code, we will write everything from scratch ;)
Let's start by creating a read-only function called
addthat accepts two integers called
yand returns their sum. You don't have to pay gas fees in order to call read-only functions.
define-read-onlyis 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
- 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))
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
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.
Everything looks good! Let's just delete the function calls and deploy the contract.
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.
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.
Connect your wallet by clicking on the "Connect Stacks Wallet" buttton.
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".
Deploy the contract by clicking on the "Deploy" button and signing the transaction in your wallet.
Once you sign the transaction, copy your address from your wallet and search for it in the explorer.
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
Once the transaction is confirmed, click on it and copy the "contract name", it's basically
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".
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.
Call any function by passing parameters to it.
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.
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!