@ -21,7 +21,7 @@ If you are new to smart contracts concepts, you should read <a href="https://blo
## Use cases
## Use cases
Not every decentralized application requires smart contracts, but Clarity unlocks interesting capabilities for decentralized applications. Examples of interesting use cases include, but are not limited to:
Not every decentralized application requires smart contracts, but Clarity unlocks interesting capabilities for decentralized applications. Examples of use cases include, but are not limited to:
* Access control (e.g. pay to access)
* Access control (e.g. pay to access)
* Non-fungible and fungible tokens
* Non-fungible and fungible tokens
@ -31,7 +31,7 @@ Not every decentralized application requires smart contracts, but Clarity unlock
## Language design
## Language design
Clarity is a list processing (LISP) language and differs from most other smart contract languages in two essential ways:
Clarity is a [list processing (LISP) language](https://en.wikipedia.org/wiki/Lisp_(programming_language)) and differs from most other smart contract languages in two essential ways:
@ -27,7 +27,7 @@ Before you get started, you should complete the [Hello World tutorial](tutorial.
In this step, you initialize a starter project with additional counter tutorial files:
In this step, you initialize a starter project with additional counter tutorial files:
1. Using your terminal, run the following command:
Using your terminal, run the following command:
```bash
```bash
npm init clarity-starter
npm init clarity-starter
@ -46,7 +46,7 @@ In this step, you initialize a starter project with additional counter tutorial
Smart contracts are often developed in a test-driven approach to ensure code quality but also to speed up the development cycle by removing the need to push every change to the Blockchain before executing it. We will do the same in this project. Now, let's run the tests and review the results:
Smart contracts are often developed in a test-driven approach to ensure code quality but also to speed up the development cycle by removing the need to push every change to the Blockchain before executing it. We will do the same in this project. Now, let's run the tests and review the results:
1. Still in the project root directory, run the following command:
Still in the project root directory, run the following command:
```bash
```bash
npm test
npm test
@ -62,14 +62,8 @@ Smart contracts are often developed in a test-driven approach to ensure code qua
2) should increment
2) should increment
3) should decrement
3) should decrement
hello world contract test suite
✓ should have a valid syntax
deploying an instance of the contract
✓ should return 'hello world'
✓ should echo number
4 passing (455ms)
1 passing (734ms)
3 failing
3 failing
```
```
@ -91,15 +85,11 @@ Let's get familiar with the tests to understand what the new smart contract shou
counterClient = new Client("SP3GWX3NE58KXHESRYE4DYQ1S31PQJTCRXB3PE9SB.counter", "counter", provider);
counterClient = new Client("SP3GWX3NE58KXHESRYE4DYQ1S31PQJTCRXB3PE9SB.counter", "counter", provider);
```
```
That tells us that the new smart contract is named `counter` and that we need to start by creating a new file for the smart contract: `contracts/counter.clar`. Note that the `contracts` folder is assumed as the base folder and that every Clarity file has the suffix `.clar`.
That tells us that the new smart contract is named `counter` and that the referenced smart contract should be found in the following file: `contracts/counter.clar`. Note that the `contracts` folder is assumed as the base folder and that every Clarity file has the suffix `.clar`.
2. Let's create the new file:
The file was already created during the project setup.
```shell
touch contracts/counter.clar
```
3. With the editor of your choice, open the file and add the following lines of code:
2. With the editor of your choice, open the file and add the following lines of code:
```cl
```cl
(define-data-var counter int 0)
(define-data-var counter int 0)
@ -115,7 +105,7 @@ Let's get familiar with the tests to understand what the new smart contract shou
With that, you are ready to rerun the tests!
With that, you are ready to rerun the tests!
4. Run the tests and review the results:
3. Run the tests and review the results:
```shell
```shell
npm test
npm test
@ -125,7 +115,7 @@ Let's get familiar with the tests to understand what the new smart contract shou
However, we don't stop here. Let's implement increment and decrement methods.
However, we don't stop here. Let's implement increment and decrement methods.
5. Add the following lines to the bottom of the `counter.clar` file and take a few seconds to review them:
4. Add the following lines to the bottom of the `counter.clar` file and take a few seconds to review them:
```cl
```cl
(define-public (increment)
(define-public (increment)
@ -140,31 +130,25 @@ Let's get familiar with the tests to understand what the new smart contract shou
Next, a [`var-set`](https://docs.blockstack.org/core/smart/clarityref#var-set) is used to set a new value for the `counter` variable. The new value is constructed using the [`+`](https://docs.blockstack.org/core/smart/clarityref#-add) (add) statement. This statement takes a number of integers and returns the result. Along with add, Clarity provides statements to subtract, multiply, and divide integers. Find more details in the [Clarity language reference](https://docs.blockstack.org/core/smart/clarityref).
Next, a [`var-set`](https://docs.blockstack.org/core/smart/clarityref#var-set) is used to set a new value for the `counter` variable. The new value is constructed using the [`+`](https://docs.blockstack.org/core/smart/clarityref#-add) (add) statement. This statement takes a number of integers and returns the result. Along with add, Clarity provides statements to subtract, multiply, and divide integers. Find more details in the [Clarity language reference](https://docs.blockstack.org/core/smart/clarityref).
6. Finally, take a few minutes and implement a new public method `decrement` to subtract `1` from the `counter` variable. You should have all knowledge needed to succeed at this!
5. Finally, take a few minutes and implement a new public method `decrement` to subtract `1` from the `counter` variable. You should have all knowledge needed to succeed at this!
Done? Great! Run the tests and make sure all of them are passing. You are looking for 7 successful tests:
Done? Great! Run the tests and make sure all of them are passing. You are looking for 4 passed tests:
```shell
```shell
counter contract test suite
counter contract test suite
✓ should have a valid syntax
✓ should have a valid syntax (39ms)
deploying an instance of the contract
deploying an instance of the contract
✓ should start at zero
✓ should start at zero
✓ should increment (95ms)
✓ should increment (133ms)
✓ should decrement (92ms)
✓ should decrement (177ms)
hello world contract test suite
✓ should have a valid syntax
deploying an instance of the contract
✓ should return 'hello world'
✓ should echo number
7 passing (518ms)
4 passing (586ms)
```
```
**Congratulations! You just implemented your first Clarity smart contract.**
**Congratulations! You just implemented your first Clarity smart contract.**
7. Here is how the final smart contract file should look like. Note that you can find the `decrement` method in here - in case you want to compare with your own implementation:
6. Here is how the final smart contract file should look like. Note that you can find the `decrement` method in here - in case you want to compare with your own implementation: