Learn how to run Chainhook as a service to evaluate your "if this, then that" predicates against the Bitcoin and Stacks blockchains.
In order to build out a more robust and secure web app, you can run Chainhook as a service to stream your events continously to a server you designate.
In this guide, you will learn how to:
Configure an existing Bitcoin node to work with Chainhook.
Generate a Chainhook predicate to target specific transactions.
Scan the Bitcoin blockchain for transactions that match your predicate.
Initiate a Chainhook service to watch for matching transactions.
Dynamically register your predicate with Chainhook.
In this section, you will configure Chainhook to match the network configurations with the bitcoin config file. First, install the latest version of Chainhook.
Next, you will generate a Chainhook.toml file to connect Chainhook with your bitcoind node. Navigate to the directory where you want to generate the Chainhook.toml file and use the following command in your terminal:
Several network parameters in the generated Chainhook.toml configuration file need to match those in the bitcoin.conf file created earlier in the setting up a Bitcoin node section. Update the following parameters accordingly:
Update bitcoind_rpc_username with the username set for rpcuser in bitcoin.conf.
Update bitcoind_rpc_password with the password set for rpcpassword in bitcoin.conf.
Update bitcoind_rpc_url with the same host and port used for rpcport in bitcoin.conf.
Additionally, if you want to receive events from the configured Bitcoin node, substitute stacks_node_rpc_url with bitcoind_zmq_url, as follows:
Here is a table of the relevant parameters this guide changes in our configuration files.
Now that your bitcoind and Chainhook configurations are complete, you can define the Chainhook predicate scope you would like to scan for.
These predicates are where you specify the if_this / then_that pattern to trigger Chainhook to deliver a result (either a file appendation or an HTTP POST request).
To generate a sample JSON file with predicates, execute the following command in your terminal:
Replace the contents of the stacking-pool.json file with the following:
This example demonstrates scanning a portion of the Bitcoin blockchain to capture specific outputs from a Bitcoin address associated with a Stacking pool, in this case Friedgar Pool. Notice the then_that predicate specifying a file_append.
You can get blockchain height and current block by referring to the Stacks Explorer.
Now, use the following command to scan the blocks based on the predicates defined in the stacking-pool.json file.
The output of the above command will be a text file bitcoin-transactions.txt generated based on the predicate definition.
Now you will generate another sample predicate, but this time you will send the payload to an API endpoint:
Replace the contents of the stacking-pool-api.json file with the following:
The start_block is a required field when using the http_postthen_that predicate.
Once you are finished setting up your endpoint, use the following command to scan the blocks based on the predicates defined in the stacking-pool-api.json file.
The above command posts events to the URL http://localhost:3000/events, mentioned in the JSON file.
In the examples above, your Chainhook scanned historical blockchain data against predicates and delivered results. In this next section, you will learn how to set up a Chainhook that acts as an ongoing observer and event-streaming service.
You can start a Chainhook service with an existing predicate. You can also dynamically register new predicates by making an API call to your chainhook. In both of these instances, your predicates will be delivering their results to a server set up to receive results.
Initiate the chainhook service by passing the predicate path to the command as shown below:
The above command registers the predicate based on the predicate definition in the stacking-pool-api.json file.
You can also dynamically register new predicates with your Chainhook service. This means you can start a long-running process that exposes HTTP endpoints to register, deregister, and report on new predicates.
This section requires that you have Redis running locally. To install, refer to the Redis documentation.
First, ensure that the following lines of code are uncommented in the Chainhook.toml file to enable the predicate registration server:
If you have an instance of Redis running locally, you can now start the Chainhook service by running the following command:
To dynamically register a new predicate, send a POST request to the running predicate registration server at localhost:20456/v1/chainhooks.
The sample response should look like this:
To better understand the output of the above JSON file, let's break down some of the details:
The apply payload includes the block header and the transactions that triggered the predicate.
The rollback payload includes the block header and the transactions that triggered the predicate for a past block that is no longer part of the canonical chain and must be reverted. (Note: this is a chief component of Chainhook's reorg-aware functionality. Chainhook maintains rollback data for blocks near the chaintip.)
You can also run chainhook service by passing multiple predicates, ie chainhook service start --predicate-path=predicate_1.json --predicate-path=predicate_2.json --config-path=Chainhook.toml