Browse Source

Fixes based on feedback

update-clarity-tutorial
Alex Graebe 5 years ago
parent
commit
b5d687538e
  1. 6
      _core/smart/overview.md
  2. 6
      _core/smart/sdk-quickstart.md
  3. 25
      _core/smart/tutorial-counter.md
  4. 175
      _core/smart/tutorial-token.md
  5. 29
      _core/smart/tutorial.md
  6. 1
      _data/navigation_core.yml

6
_core/smart/overview.md

@ -11,12 +11,6 @@ Clarity is Blockstack's smart contracting language for use with the Stacks block
* TOC
{:toc}
<div class="uk-card uk-card-default uk-card-body">
<h5>Early Release</h5>
<p>Clarity and its accompanying toolset are in early release. If you encounter issues with or have feature requests regarding Clarity, please create an issue on the <a href='https://github.com/blockstack/stacks-blockchain/issues' target='_blank'>blockstack/stacks-blockchain</a> repository. To read previous or join ongoing discussions about smart contracts in general and Clarity in particular, visit the <strong><a href='https://forum.blockstack.org/c/clarity' target='_blank'>Smart Contracts</a></strong> topic in the Blockstack Forum.
</p>
</div>
## Who should use smart contracts?
You can use Clarity to write standalone contracts or to write contracts that are part of decentralized applications (DApps) you write with the blockstack.js library. Smart contracts allow two parties to exchange anything of value (money, property, shares), in an automated, auditable, and secure way _without the services of a middleman_. Nick Szabo introduced the canonical metaphor for smart contracts, a vending machine.

6
_core/smart/sdk-quickstart.md

@ -11,12 +11,6 @@ You can use the software developer kit (SDK) to develop, test, and deploy Clarit
* TOC
{:toc}
<div class="uk-card uk-card-default uk-card-body">
<h5>Early Release</h5>
<p>Clarity and its accompanying toolset are in early release. If you encounter issues with or have feature requests regarding Clarity, please create an issue on the <a href='https://github.com/blockstack/stacks-blockchain/issues' target='_blank'>blockstack/stacks-blockchain</a> repository. To read previous or join ongoing discussions about smart contracts in general and Clarity in particular, visit the <strong><a href='https://forum.blockstack.org/c/clarity' target='_blank'>Smart Contracts</a></strong> topic in the Blockstack Forum.
</p>
</div>
## About this tutorial and the prerequisites you need
{% include note.html content="This tutorial was written on macOS High Sierra 10.13.4. If you use a Windows or Linux system, you can still follow along. However, you will need to \"translate\" appropriately for your operating system." %}

25
_core/smart/tutorial-counter.md

@ -8,7 +8,7 @@ permalink: /:collection/:path.html
| Experience | | **Intermediate** |
| Duration | | **30 minutes** |
In this tutorial, you learn how to implement a smart contract that stores and manipulates an integer value on the Stack2.0 Blockchain. By the end of this tutorial, you will ...
In this tutorial, you learn how to implement a smart contract that stores and manipulates an integer value on the Stacks 2.0 Blockchain. By the end of this tutorial, you will ...
* Have experienced test-driven development with Clarity
* Understand more Clarity language design principles
@ -19,12 +19,6 @@ In this tutorial, you learn how to implement a smart contract that stores and ma
* TOC
{:toc}
<div class="uk-card uk-card-default uk-card-body">
<h5>Early Release</h5>
<p>Clarity and its accompanying toolset are in early release. If you encounter issues with or have feature requests regarding Clarity, please create an issue on the <a href='https://github.com/blockstack/stacks-blockchain/issues' target='_blank'>blockstack/stacks-blockchain</a> repository. To read previous or join ongoing discussions about smart contracts in general and Clarity in particular, visit the <strong><a href='https://forum.blockstack.org/c/clarity' target='_blank'>Smart Contracts</a></strong> topic in the Blockstack Forum.
</p>
</div>
## Pre-requisites
Before you get started, you should complete the [Hello World tutorial](tutorial.html).
@ -36,16 +30,17 @@ In this step, you initialize a starter project with additional counter tutorial
1. Using your terminal, run the following command:
```bash
npm init clarity-starter --counter-tutorial
npm init clarity-starter
```
2. After the additional project files are loaded up, you can confirm the project name. Feel free to hit ENTER to accept the default suggestion - or enter a new one.
You have to select a template and a name for your local folder. For the counter template used in this tutorial, ensure to type `counter` and hit ENTER:
```bash
? Project name (clarity-counter-project)
? Template - one of [hello-world, counter]: counter
? Project name: (clarity-counter-project)
```
Finally, the project dependencies will install, and your project is ready for development. Because you already completed the Hello World tutorial, the project structure is familiar to you. The main difference is that we have additional tests for a new counter smart contract.
Finally, the project dependencies are installed and your project is ready for development. Because you already completed the Hello World tutorial, the project structure is familiar to you. The main difference is that we have additional tests for a new counter smart contract.
## Step 2: Running tests
@ -93,15 +88,15 @@ Let's get familiar with the tests to understand what the new smart contract shou
You should be familiar with the test set up from the Hello World tutorial. Notice how the instance of the smart contract is created on line 8:
```js
counterClient = new Client("SP3GWX3NE58KXHESRYE4DYQ1S31PQJTCRXB3PE9SB.counter", "sample/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/sample/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 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`.
2. Let's create the new file:
```shell
touch contracts/sample/counter.clar
touch contracts/counter.clar
```
3. With the editor of your choice, open the file and add the following lines of code:
@ -194,5 +189,5 @@ Let's get familiar with the tests to understand what the new smart contract shou
{:.no_toc}
* <a href="tutorial-token.html">Next tutorial: Writing a token management smart contract</a>
* <a href="principals.html">Guide: Understanding principals</a>
* <a href="clarityRef.html">Clarity language reference</a>

175
_core/smart/tutorial-token.md

@ -1,175 +0,0 @@
---
layout: core
description: "Blockstack Clarity: Token Tutorial"
permalink: /:collection/:path.html
---
# Tutorial: Token
| Experience | | **Advanced** |
| Duration | | **30 minutes** |
In this tutorial, you learn ... By the end of this tutorial, you will ...
*
## Overview
* TOC
{:toc}
<div class="uk-card uk-card-default uk-card-body">
<h5>Early Release</h5>
<p>Clarity and its accompanying toolset are in early release. If you encounter issues with or have feature requests regarding Clarity, please create an issue on the <a href='https://github.com/blockstack/stacks-blockchain/issues' target='_blank'>blockstack/stacks-blockchain</a> repository. To read previous or join ongoing discussions about smart contracts in general and Clarity in particular, visit the <strong><a href='https://forum.blockstack.org/c/clarity' target='_blank'>Smart Contracts</a></strong> topic in the Blockstack Forum.
</p>
</div>
## Pre-requisites
Before you get started with this tutorial, you should complete the [Hello World](tutorial.html) and the [Counter](tutorial-counter.html) tutorials.
## Step 1: Download token tutorial project
1. Initialize a starter project with additional token tutorial files:
```bash
npm init clarity-starter --token-tutorial
```
2. Confirm the new project name:
```bash
? Project name (clarity-token-project)
```
## Step 2: Running tests
Just like in the Counter tutorial, we will use test-driven development to implement our Token smart contract. Let's run the tests and review the results:
1. Run the following command:
```bash
npm test
```
You should see the following response:
```bash
...
```
## Step 3: Developing the smart contract
Let's get familiar with the tests to understand what the new smart contract should look like
1. Take a quick look at the test file associated with the counter smart contract:
```shell
cat test/counter.ts
```
You should be familiar with the test set up from the Hello World tutorial. Notice how the instance of the smart contract is created on line 8:
```js
counterClient = new Client("SP3GWX3NE58KXHESRYE4DYQ1S31PQJTCRXB3PE9SB.counter", "sample/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/sample/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:
```shell
touch contracts/sample/counter.clar
```
3. With the editor of your choice, open the file and add the following lines of code:
```cl
(define-data-var counter int 0)
(define-public (get-counter)
(ok (var-get counter))
)
```
The first line initializes a new integer variable `counter` with the value set to `0` using the [`define-data-var`](https://docs.blockstack.org/core/smart/clarityref#define-data-var) statement. It is important to note that all definition statements in Clarity need to be at the top of the file.
To provide access to the variable from outside of the current smart contract, we need to declare a public get method for this variable. The last lines of the code add a public `get-counter` method. The [`var-get`](https://docs.blockstack.org/core/smart/clarityref#var-get) statement looks for a variable in the contract's data map and returns it.
With that, you are ready to rerun the tests!
4. Run the tests and review the results:
```shell
npm test
```
You should now only see 2 failing tests! `should start at zero` is passing, and you successfully build your first part of the contract. Congrats!
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:
```cl
(define-public (increment)
(begin
(var-set counter (+ (var-get counter) 1))
(ok (var-get counter))
)
)
```
First, the [`begin`](https://docs.blockstack.org/core/smart/clarityref#begin) statement evaluates the multi-line expressions and returns the value of the last expression. In this case, it is used to set a new value and return the new value.
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!
Done? Great! Run the tests and make sure all of them are passing. You are looking for 7 successful tests:
```shell
counter contract test suite
✓ should have a valid syntax
deploying an instance of the contract
✓ should start at zero
✓ should increment (95ms)
✓ should decrement (92ms)
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)
```
**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:
```cl
(define-data-var counter int 0)
(define-public (increment)
(begin
(var-set counter (+ (var-get counter) 1))
(ok (var-get counter))
)
)
(define-public (decrement)
(begin
(var-set counter (- (var-get counter) 1))
(ok (var-get counter))
)
)
(define-public (get-counter)
(ok (var-get counter))
)
```
## Where to go next
{:.no_toc}
* <a href="tutorial-token.html">Next tutorial: Writing a token management smart contract</a>
* <a href="clarityRef.html">Clarity language reference</a>

29
_core/smart/tutorial.md

@ -20,12 +20,6 @@ In this tutorial, you learn how to use Clarity, Blockstack's smart contracting l
* TOC
{:toc}
<div class="uk-card uk-card-default uk-card-body">
<h5>Early Release</h5>
<p>Clarity and its accompanying toolset are in early release. If you encounter issues with or have feature requests regarding Clarity, please create an issue on the <a href='https://github.com/blockstack/stacks-blockchain/issues' target='_blank'>blockstack/stacks-blockchain</a> repository. To read previous or join ongoing discussions about smart contracts in general and Clarity in particular, visit the <strong><a href='https://forum.blockstack.org/c/clarity' target='_blank'>Smart Contracts</a></strong> topic in the Blockstack Forum.
</p>
</div>
## Pre-requisites
To complete the tutorial, you should have [NodeJS](https://nodejs.org/en/download/) installed on your workstation. You can verify your installation by opening up your terminal and run the following command:
@ -46,18 +40,19 @@ In this step, you initialize a starter project for Clarity development:
npm init clarity-starter
```
2. After the starter project was loaded up, you are asked to name the starter project. Feel free to hit ENTER to accept the default suggestion.
2. After the starter project was loaded up, have to select a template and a name for your local folder. Feel free to hit ENTER both times to accept the default suggestion.
```bash
? Project name (clarity-starter-project)
? Template - one of [hello-world, counter]: (hello-world)
? Project name: (clarity-hello-world-project)
```
Finally, the project dependencies are installed and your project is ready for development.
3. The project is located in a new folder, `clarity-starter-project` by default. Jump into the folder and have a look at the file structure:
3. The project is located in a new folder, `clarity-hello-world-project` by default. Jump into the folder and have a look at the file structure:
```bash
cd clarity-starter-project
cd clarity-hello-world-project
ls
```
@ -67,10 +62,10 @@ In this step, you initialize a starter project for Clarity development:
Now, let's have a look at a Clarity smart contract and get familiar with the basic language design characteristics.
1. Still inside the terminal, list the contents of the `contracts/sample` folder.
1. Still inside the terminal, list the contents of the `contracts` folder.
```bash
ls contracts/sample
ls contracts
```
This directory contains a hello world Clarity contract. Note that all Clarity files have a `.clar` suffix.
@ -78,7 +73,7 @@ Now, let's have a look at a Clarity smart contract and get familiar with the bas
2. Let's review the contents of `hello-world.clar` with the `cat` command.
```bash
cat contracts/sample/hello-world.clar
cat contracts/hello-world.clar
```
You should see the contract source code. Take a few seconds to review the content.
@ -155,19 +150,19 @@ import { Client, Provider, ProviderRegistry, Result } from "@blockstack/clarity"
### Initiliazing a client
At the test start, we are initializing contract instance `helloWorldClient` and a provider that forwards commands to the Rust CLI in order to interact with the Stack2.0 blockchain.
At the test start, we are initializing contract instance `helloWorldClient` and a provider that simulates interactions with the Stacks 2.0 Blockchain.
```js
let helloWorldClient: Client;
let provider: Provider;
...
(...)
provider = await ProviderRegistry.createProvider();
helloWorldClient = new Client("SP3GWX3NE58KXHESRYE4DYQ1S31PQJTCRXB3PE9SB.hello-world", "sample/hello-world", provider);
helloWorldClient = new Client("SP3GWX3NE58KXHESRYE4DYQ1S31PQJTCRXB3PE9SB.hello-world", "hello-world", provider);
```
Take a look at the client initialization. It requires a contract id and name in the following format: `{contract_id}.{contract_name}`. The second parameter indicates the location of the smart contract file, without the `.clar` suffix. By default, the location is assuming to be relative to the `contracts` folder.
Take a look at the client initialization. It requires a contract id and name in the following format: `{owner_stacks_address}.{contract_identifier}`. The second parameter indicates the location of the smart contract file, without the `.clar` suffix. By default, the location is assumed to be relative to the `contracts` folder.
### Checking syntax

1
_data/navigation_core.yml

@ -3,7 +3,6 @@
- core/smart/overview
- core/smart/tutorial
- core/smart/tutorial-counter
- core/smart/tutorial-token
- core/smart/principals
- core/smart/functions
- core/smart/clarityRef

Loading…
Cancel
Save