Quickstart
Create a simple counter contract
You will learn the following:
- Generate a new project
- Create a smart contract with Clarity
- Test and validate your smart contract code
Install clarinet
To get started, install the package on your machine:
Generate a new counter
project
Navigate to a directory in your terminal where you want to create your project. Then in your terminal, run the following command:
You should have a new counter
directory resembling the following:
Create a counter
contract
- Navigate to your project by running
cd counter
in your terminal. - Inside your project, use the
clarinet contract new
command to generate a new contract.
This will add 2 files to your project: counter.clar
and counter.test.ts
.
It also updates the Clarinet.toml
file with your new contract.
Variables and functions
In Clarity, you can define variables and functions to store and manipulate data. To complete a working counter
example:
- Define a map called
Counters
to store the count associated with each user. - Define a public function called
count-up
that increments the count of the user who calls it. - Add a read-only function called
get-count
that returns the count of the user who calls it.
Validate your contract
In order to verify that the syntax of your code is valid, run clarinet check
inside of your project.
And now to test the code, you can run clarinet console
to bring up a REPL where you can interact with your contract directly.
- Run
clarinet console
in your terminal.
- In the console, run
(contract-call? .counter count-up)
to call thecount-up
function.
- Then verify the count of the user has been incremented by calling the
get-count
function with thetx-sender
as the argument.
Next steps
Last updated on