Browse Source

feat: added Developing with Clarinet page

feat/shiki-twoslash-rehype
Patrick Gray 4 years ago
committed by Patrick Gray
parent
commit
2eeda402af
  1. 2
      src/common/navigation.yaml
  2. 198
      src/pages/write-smart-contracts/clarinet.md
  3. 10
      src/pages/write-smart-contracts/overview.md

2
src/common/navigation.yaml

@ -27,10 +27,12 @@ sections:
- path: /stacking-using-CLI
- path: /write-smart-contracts
usePageTitles: true
pages:
- path: /overview
- path: /principals
- path: /values
- path: /clarinet
sections:
- title: Tutorials
pages:

198
src/pages/write-smart-contracts/clarinet.md

@ -0,0 +1,198 @@
---
title: Developing with Clarinet
description: Develop smart contracts locally with the Clarinet REPL and testing harness
---
## Introduction
[Clarinet][] is a local Clarity runtime packaged as a command-line application. It's designed to facilitate rapid smart
contract development, testing, and deployment. Clarinet consists of a Clarity REPL and a testing harness, which, when
used together, allow you to rapidly develop and test a Clarity smart contract, without the need to deploy the contract
to a local mocknet or testnet.
The local Clarity REPL is advantageous, because when learning Clarity or when developing a new smart contract, it's
useful to be able to exercise a contract without needing to wait for block times in a live blockchain. Clarinet allows
you to instantly initialize wallets and populate them with tokens, so that you can interactively or programmatically
test the behavior of the smart contract. Blocks are mined instantly, and you can control the number of blocks that
are mined between testing transactions.
Clarinet is a useful tool for developing smart contracts, and should be used as part of a larger development strategy
that involves building and testing the contract locally, deploying the final draft contract to a testnet environment
and testing on a live blockchain, and deploying the final contract to the mainnet.
## Installing Clarinet
The best way to install Clarinet is through the Rust package manager, Cargo. If you have a working installation of
[Rust](https://www.rust-lang.org/tools/install), use the following command to install Clarinet.
```sh
cargo install clarinet --locked
```
## Developing a Clarity smart contract
Once you have installed Clarinet, you can begin a new Clarinet project with the command:
```sh
clarinet new my-contract && cd my-project
```
This command creates a new directory and populates it with boilerplate configuration and testing files. The `toml` files
located in the `settings` directory control the Clarinet environment. For example, the `Development.toml` file contains
definitions for wallets in the local REPL environment, and their starting balances (in STX).
```toml
...
[accounts.deployer]
mnemonic = "fetch outside black test wash cover just actual execute nice door want airport betray quantum stamp fish act pen trust portion fatigue scissors vague"
balance = 1_000_000
[accounts.wallet_1]
mnemonic = "spoil sock coyote include verify comic jacket gain beauty tank flush victory illness edge reveal shallow plug hobby usual juice harsh pact wreck eight"
balance = 1_000_000
[accounts.wallet_2]
mnemonic = "arrange scale orient half ugly kid bike twin magnet joke hurt fiber ethics super receive version wreck media fluid much abstract reward street alter"
balance = 1_000_000
...
```
You can create a new contract in the project with the command:
```sh
clarinet contract new my-contract
```
This command creates a new `my-contract.clar` file in the `contracts` directory, and a `my-contract_test.ts` in the
`test` directory. Additionally, it adds the contract to the `Clarinet.toml` configuration file.
```toml
[contracts.my-contract]
path = "contracts/my-contract.clar"
depends_on = []
```
At this point, you can begin editing your smart contract in the `contracts` directory. At any point while you are
developing, you can use the command `clarinet check` to check the syntax of your smart contract.
## Testing with Clarinet
Clarinet provides several powerful methods to test and interact with your smart contracts. As mentioned in the previous
section, you can always check your Clarity syntax using the `clarinet check` command. This validates any smart contracts
you are currently developing in the active project.
There are two methods in Clarinet you can use to test smart contracts: the [console][], an interactive Clarity REPL, and
the [test harness][], a testing framework written in Typescript.
### Testing with the console
The Clarinet console is an interactive Clarity REPL that runs in-memory. Any contracts configured in the current project
are automatically loaded into memory. Additionally, wallets defined in the `settings/Development.toml` file are
initialized with STX tokens for testing purposes. When the console runs, it provides a summary of the deployed
contracts, their public functions, as well as wallet addresses and balances.
```
clarity-repl v0.11.0
Enter "::help" for usage hints.
Connected to a transient in-memory database.
Initialized contracts
+-------------------------------------------------------+-------------------------+
| Contract identifier | Public functions |
+-------------------------------------------------------+-------------------------+
| ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.my-contract | (echo-number (val int)) |
| | (say-hi) |
+-------------------------------------------------------+-------------------------+
Initialized balances
+------------------------------------------------------+---------+
| Address | STX |
+------------------------------------------------------+---------+
| ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE (deployer) | 1000000 |
+------------------------------------------------------+---------+
| ST1J4G6RR643BCG8G8SR6M2D9Z9KXT2NJDRK3FBTK (wallet_1) | 1000000 |
+------------------------------------------------------+---------+
...
```
You can use the `::help` command for valid console commands.
```
>> ::help
::help Display help
::list_functions Display all the native functions available in Clarity
::describe_function <function> Display documentation for a given native function fn-name
::mint_stx <principal> <amount> Mint STX balance for a given principal
::set_tx_sender <principal> Set tx-sender variable to principal
::get_assets_maps Get assets maps for active accounts
::get_costs <expr> Display the cost analysis
::get_contracts Get contracts
::get_block_height Get current block height
::advance_chain_tip <count> Simulate mining of <count> blocks
```
The console commands control the state of the REPL chain, and let you get information about it and advance the chain
tip. Additionally, you can enter Clarity commands into the console and observe the result of the command. The
`::list_functions` console command prints a cheat sheet of Clarity commands. For example, in the example contract,
you could use the REPL to call the `echo-number` function in the contract with the following command:
```
>> (contract-call? .my-contract echo-number 42)
(ok 42)
```
Note that by default commands are always executed as the `deployer` address, which means you can use the shorthand
`.my-contract` without specifying a full address to the contract. If you changed the transaction address with the
`::set_tx_sender` command, you would need to provide the full address to the contract in the contract call
(`ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.my-contract`).
You can refer to the [Clarity language reference][] for a complete overview of all Clarity functions.
### Testing with the test harness
The test harness is a Deno testing library that can simulate the blockchain, exercise functions of the contract, and
make testing assertions about the state of the contract or chain.
You can run any tests configured in the `tests` directory with the command
```sh
clarinet test
```
When you create a new contract, a test suite is automatically created for it. You can populate the test suite with
unit tests as you develop the contract.
An example unit test for the `echo-number` function is provided below:
```ts
Clarinet.test({
name: 'the echo-number function returns the input value ok',
async fn(chain: Chain) {
const testNum = '42';
let block = chain.mineBlock([
Tx.contractCall(
'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.my-contract',
'echo-number',
[testNum],
'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE'
),
]);
assertEquals(block.receipts.length, 1); // assert that the block received a single tx
assertEquals(block.receipts[0].result, `(ok ${testNum})`); // assert that the result of the tx was ok and the input number
assertEquals(block.height, 2); // assert that only a single block was mined
},
});
```
For more information on assertions, review [asserts][] in the Deno standard library. For more information on the available
Clarity calls in Deno, review the [Deno Clarity library][].
## Additional reading
- [Clarinet README](https://github.com/hirosystems/clarinet#clarinet)
[clarinet]: https://github.com/hirosystems/clarinet
[console]: #testing-with-the-console
[test harness]: #testing-with-the-test-harness
[clarity language reference]: http://localhost:3000/references/language-functions
[asserts]: https://deno.land/std@0.90.0/testing/asserts.ts
[Deno Clarity library]: https://deno.land/x/clarinet@v0.6.0/index.ts

10
src/pages/write-smart-contracts/overview.md

@ -1,5 +1,5 @@
---
title: Write smart contracts
title: Clarity overview
description: Overview and guides for getting started with Clarity
images:
large: /images/contract.svg
@ -48,9 +48,9 @@ Clarity differs from most other smart contract languages in two essential ways:
- The language is decidable (not Turing complete)
Using an interpreted language ensures that the executed code is human-readable and auditable. A decidable language
like Clarity makes it possible to determine precisely which code is going to be executed, for any function.
like Clarity makes it possible to determine precisely which code will be executed, for any function.
A Clarity smart contract is composed of two parts, a data space and a set of functions. Only the associated
A Clarity smart contract consists of two parts, a data space and a set of functions. Only the associated
smart contract may modify its corresponding data space on the blockchain. Functions may be private and thus callable
only from within the smart contract, or public and thus callable from other contracts. Users call smart contracts'
public functions by broadcasting a transaction on the blockchain which invokes the public function. Contracts
@ -60,7 +60,7 @@ Note some of the key Clarity language rules and limitations.
- The only primitive types are booleans, integers, buffers, and principals.
- Recursion is illegal and there are no anonymous functions.
- Looping may only be performed via `map`, `filter`, or `fold`.
- Looping is only performed via `map`, `filter`, or `fold`.
- There is support for lists, however, the only variable length lists in the language appear as function inputs; there is no support for list operations like append or join.
- Variables are immutable.
@ -71,7 +71,7 @@ Note some of the key Clarity language rules and limitations.
## Explore more
For language details and references, check out the following:
For language details and references, see the following:
[@page-reference | grid]
| /write-smart-contracts/principals, /write-smart-contracts/values, /references/language-overview

Loading…
Cancel
Save