When building a Stacks app, you can query on-chain state from a Stacks contract by calling "read-only functions". These are functions exposed by the contract to help apps and other contracts integrate with the contract.
Setting up the ClarigenClient
The @clarigen/core
package exposes a ClarigenClient
instance, which provides an easy way to call read-only functions in your app.
To construct the ClarigenClient
, you need to include either:
- A
string
pointing to a Stacks API endpoint - A
StacksNetwork
instance from@stacks/network
To use an API endpoint:
Or, using a StacksNetwork
instance:
Using ClarigenClient
To call individual contract functions, you'll need to have a contract factory setup. In these examples, we'll use a hypothetical NFT contract, nftContract
to call functions.
ro
In Clarigen, "ro" stands for "read-only". It's a shorthand for calling read-only methods.
Using ro
returns the exact value returns from the contract function, but typed and converted to JS-native values.
roOk
roOk
is a shorthand to call a read-only function and automatically scope to the ok
type of the contract's response. It is only suitable for contract functions that return the (response)
Clarity type.
In the above example, we were manually checking to see that the contract returned an ok
response. We can shorten that to be:
roErr
Sometimes, the assumption is that an err
is returned from a function, otherwise you want to throw an error. roErr
is a shorthand for that.
Fetching microblock-based state
If you want to query the state of a contract based on the most recent microblock, you can include the latest
option. This is provided by default.
Returning JSON-valid types
By default, Clarigen returns bigint
for integers and UInt8Array
values for buffers. If you only want to receive valid JSON, you can use the json
option.
When json: true
is specified, integers are returned as string
(to prevent overflows), and buffers are returned as hex-encoded string
.
For example, a u1000
integer would be "1000"
, and 0xdeadbeef
would be "deadbeef"
.
Calling read-only functions without a ClarigenClient
instance
If you want to call read-only functions without having to instantiate a ClarigenClient
instance, the @clarigen/core
package exposes pure functions for you. These are available as ro
, roOk
, and roErr
.
When using these functions, you must specify a url
or network
option.
Using url
:
Or using network
: