description: Learn how to deal with Clarity Values in JavaScript.
description: Learn how to deal with Clarity Values in JavaScript.
tags:
- tutorial
---
---
## Introduction
## Introduction
The Clarity language makes use of a strong static [type system](https://docs.blockstack.org/references/language-clarity#clarity-type-system). This simply means that every function defined in Clarity expects arguments of specific types, and that a failure to provide properly typed arguments will result in your code failing to compile, or your `contract-call` transaction failing prior to execution.
The Clarity language makes use of a strong static [type system](https://docs.blockstack.org/references/language-clarity#clarity-type-system). This simply means that every function defined in Clarity expects arguments of specific types, and that a failure to provide properly typed arguments will result in your code failing to compile, or your `contract-call` transaction failing prior to execution.
In order to build web applications that interact with Clarity contracts, you will need to learn how to construct and use `ClarityValues`. The [@stacks/transactions](https://github.com/blockstack/stacks.js/tree/master/packages/transactions) library makes this easy, as we will demonstrate below.
In order to build web applications that interact with Clarity contracts, you will need to learn how to construct and use `ClarityValues`. The [@stacks/transactions](https://github.com/blockstack/stacks.js/tree/master/packages/transactions) library makes this easy, as we will demonstrate below.
## Clarity Types
## Clarity Types
Values in Clarity can have the following types:
Values in Clarity can have the following types:
- `(tuple (key-name-0 key-type-0) (key-name-1 key-type-1) ...)` - a typed tuple with named fields.
- `(tuple (key-name-0 key-type-0) (key-name-1 key-type-1) ...)` - a typed tuple with named fields.
@ -26,6 +26,7 @@ Values in Clarity can have the following types:
- `uint` - unsigned 128-bit integer
- `uint` - unsigned 128-bit integer
## Constructing Clarity Values and accessing their data
## Constructing Clarity Values and accessing their data
Clarity values can be constructed with functions provided by the **@stacks/transactions** library. These functions simply output javascript objects that contain a value and a numerical representation of the Clarity type information.
Clarity values can be constructed with functions provided by the **@stacks/transactions** library. These functions simply output javascript objects that contain a value and a numerical representation of the Clarity type information.
```typescript
```typescript
@ -53,6 +54,7 @@ export enum ClarityType {
Here are examples of how to construct each type of Clarity value, and how to access its data if it has any:
Here are examples of how to construct each type of Clarity value, and how to access its data if it has any:
### Booleans
### Booleans
```typescript
```typescript
const t = trueCV();
const t = trueCV();
const f = falseCV();
const f = falseCV();
@ -61,6 +63,7 @@ const f = falseCV();
Boolean Clarity Values don't contain any underlying data. They are simply objects with `type` information.
Boolean Clarity Values don't contain any underlying data. They are simply objects with `type` information.
### Optional Values
### Optional Values
```typescript
```typescript
const nothing: NoneCV = noneCV();
const nothing: NoneCV = noneCV();
const something: SomeCV = someCV(trueCV());
const something: SomeCV = someCV(trueCV());
@ -84,6 +87,7 @@ if (maybeVal.type === ClarityType.OptionalSome) {
```
```
### Buffers
### Buffers
```typescript
```typescript
const buffer = Buffer.from('foo');
const buffer = Buffer.from('foo');
const bufCV: BufferCV = bufferCV(buffer);
const bufCV: BufferCV = bufferCV(buffer);
@ -93,6 +97,7 @@ console.log(bufCV.buffer);
```
```
### Integers
### Integers
```typescript
```typescript
const i: IntCV = intCV(-10);
const i: IntCV = intCV(-10);
const u: UIntCV = uintCV(10);
const u: UIntCV = uintCV(10);
@ -107,6 +112,7 @@ console.log(u.value);
Clarity supports both integers and unsigned integers.
Clarity supports both integers and unsigned integers.
Both kinds of Clarity principal values contain `type` information and an `address` object. Contract principals also contain a `contractName`.
Both kinds of Clarity principal values contain `type` information and an `address` object. Contract principals also contain a `contractName`.
### Response Values
### Response Values
```typescript
```typescript
const errCV = responseErrorCV(trueCV());
const errCV = responseErrorCV(trueCV());
const okCV = responseOkCV(falseCV());
const okCV = responseOkCV(falseCV());
@ -172,12 +180,13 @@ console.log(okCV.value);
Response Clarity Values will either have the type `ClarityType.ResponseOk` or `ClarityType.ResponseErr`. They both contain a Clarity Value. Often this value will be an integer error code if the response is an `Error`.
Response Clarity Values will either have the type `ClarityType.ResponseOk` or `ClarityType.ResponseErr`. They both contain a Clarity Value. Often this value will be an integer error code if the response is an `Error`.
### Tuples
### Tuples
```typescript
```typescript
const tupCV = tupleCV({
const tupCV = tupleCV({
'a': intCV(1),
a: intCV(1),
'b': trueCV(),
b: trueCV(),
'c': falseCV()
c: falseCV(),
})
});
console.log(tupCV.data['b']);
console.log(tupCV.data['b']);
// { type: ClarityType.BoolTrue }
// { type: ClarityType.BoolTrue }
@ -188,8 +197,9 @@ Tuples in Clarity are typed and contain named fields. The tuple above, for examp
Clarity tuples are represented in JavaScript as objects and a tuple's data can be accessed by its `data` field, where the underlying JS object is stored.
Clarity tuples are represented in JavaScript as objects and a tuple's data can be accessed by its `data` field, where the underlying JS object is stored.
### Lists
### Lists
```typescript
```typescript
const l = listCV([trueCV(), falseCV()])
const l = listCV([trueCV(), falseCV()]);
console.log(l.list[0]);
console.log(l.list[0]);
// { type: ClarityType.BoolTrue }
// { type: ClarityType.BoolTrue }
@ -200,12 +210,14 @@ Lists, in Clarity, are homogeneous, meaning they can only contain elements of a
A Clarity lists underlying data can be accessed via its `list` field.
A Clarity lists underlying data can be accessed via its `list` field.
## Using Clarity Values
## Using Clarity Values
Now that you know how to construct _and_ deconstruct Clarity values, you can use them to build `contract-call` transactions that call smart contract functions.
Now that you know how to construct _and_ deconstruct Clarity values, you can use them to build `contract-call` transactions that call smart contract functions.
This is covered [here](https://docs.blockstack.org/stacks-blockchain/transactions#construction).
This is covered [here](https://docs.blockstack.org/stacks-blockchain/transactions#construction).
## Utilizing Clarity Values from Transaction Responses
## Utilizing Clarity Values from Transaction Responses
`Read-only` Clarity functions can return Clarity values as a response. These `read-only` functions can be accessed easily in JavaScript via the [`callReadOnlyFunction()`](https://github.com/blockstack/stacks.js/tree/master/packages/transactions#calling-read-only-contract-function) function included in `@stacks/transactions`, which returns a `ClarityValue`.
`Read-only` Clarity functions can return Clarity values as a response. These `read-only` functions can be accessed easily in JavaScript via the [`callReadOnlyFunction()`](https://github.com/blockstack/stacks.js/tree/master/packages/transactions#calling-read-only-contract-function) function included in `@stacks/transactions`, which returns a `ClarityValue`.
As mentioned above, `ClarityValues` are simply javascript objects containing a value and its associated Clarity type information. These object types are defined [here](https://github.com/blockstack/stacks.js/tree/1f2b5fd8bdf1c2b5866e8171163594d7708a8c7a/packages/transactions/src/clarity/types).
As mentioned above, `ClarityValues` are simply javascript objects containing a value and its associated Clarity type information. These object types are defined [here](https://github.com/blockstack/stacks.js/tree/1f2b5fd8bdf1c2b5866e8171163594d7708a8c7a/packages/transactions/src/clarity/types).
@ -244,6 +256,7 @@ if (result.type === ClarityType.ResponseOk) {
```
```
### Deserializing Clarity Values from Hex
### Deserializing Clarity Values from Hex
If you receive a response from a transaction in the form of a Hex string, you can deserialize it into a Clarity value like so:
If you receive a response from a transaction in the form of a Hex string, you can deserialize it into a Clarity value like so:
```javascript
```javascript
@ -253,6 +266,7 @@ let cv = hexToCV('hex_string');
```
```
## Debugging Clarity Values
## Debugging Clarity Values
Sometimes you might receive a Clarity value that you were not expecting. Logging the value to your console won't always prove to be useful, unless you have memorized the Clarity value type enum values.
Sometimes you might receive a Clarity value that you were not expecting. Logging the value to your console won't always prove to be useful, unless you have memorized the Clarity value type enum values.
In order to figure out what kind of value you are dealing with, you can use the `cvToString()` function to convert the Clarity value to a more easily readable string.
In order to figure out what kind of value you are dealing with, you can use the `cvToString()` function to convert the Clarity value to a more easily readable string.
@ -260,19 +274,19 @@ In order to figure out what kind of value you are dealing with, you can use the
For example, calling `cvToString()` on a large `tuple` might yield something like:
For example, calling `cvToString()` on a large `tuple` might yield something like: