Skip to content

Commit 911340e

Browse files
authored
Merge pull request #736 from natebuch/main
Updates to Predicate Design and Addition of Bitcoin Scopes
2 parents 70fb847 + d25e57e commit 911340e

11 files changed

+793
-827
lines changed

content/docs/stacks/chainhook/concepts/bitcoin-predicates.mdx

-425
This file was deleted.

content/docs/stacks/chainhook/concepts/predicate-design.mdx

+316-31
Original file line numberDiff line numberDiff line change
@@ -3,62 +3,347 @@ title: Predicate design
33
description: Predicates are the building blocks of Chainhook.
44
---
55

6-
The core design of chainhooks revolves around the concept of predicates. Each individual chainhook has a customizable predicate that specifies what information you are scanning for.
6+
The core design of Chainhook revolves around the concept of predicates. Each individual Chainhook has a customizable predicate that specifies what the Bitcoin or Stacks blockchain data you are scanning for.
77

8-
Predicates are defined in an `if-this`, `then-that` format. You'll write your condition in the `if-this` condition template and use `then-that` to output the result.
8+
<Callout type="warn" title="Requirements">
9+
Commands outlined here will require that you have installed Chainhook [directly](/stacks/chainhook/installation).
10+
</Callout>
911

10-
## `if-this`
12+
## Predicate design
13+
Below is the general strucure of the `predicate` JSON with its primary elements. These elements and their values are required.
1114

12-
The `if-this` predicate design can use the following attributes to define the predicates. The 'scope' parameter is mandatory to use with any other parameters.
15+
- Chainhook will evaluate the predicate against the specfied blockchain specified in `chain`.
16+
- The `uuid` will be returned in the Chainhook payload, providing a record of the predicate that triggers it.
17+
- Identify your predicate for your DApp using `name` and `version`.
1318

1419
```json
15-
// [!code word:if_this]
16-
{
17-
"if_this": {
18-
"scope": "txid",
19-
"equals": "0xfaaac1833dc4883e7ec28f61e35b41f896c395f8d288b1a177155de2abd6052f"
20+
{
21+
"chain": "stacks",
22+
"uuid": "1",
23+
"name": "STX_Transfer_Predicate",
24+
"version": 1,
25+
"networks": {
26+
// Other configurations
27+
}
2028
}
21-
}
22-
```
29+
```
2330

24-
Other available parameters:
31+
<Callout type="info" title="Note">
32+
You can use the following command to verify your predicate:
33+
```
34+
chainhook predicate check your-predicate-file.json --mainnet
35+
```
36+
</Callout>
37+
38+
## Networks
2539

26-
- `equals`
27-
- `op_return`
28-
- `ends_with`
29-
- `p2pkh`
30-
- `p2sh`
31-
- `p2wpkh`
32-
- `operation`
40+
The `networks` element contains an object who's key determines the blockchain network you want Chainhook to scan.
41+
- This object's value will contain the `if_this` and then `then_that` specifications.
42+
- Multple networks can be specified in the `networks` element.
43+
44+
```json
45+
"networks": {
46+
"mainnet": {
47+
// if_this and then_that specifications
48+
// Other configurations
49+
},
50+
"testnet": {
51+
// if_this and then_that specifications
52+
// Other configurations
53+
},
54+
}
55+
```
3356

34-
<Callout title="Info">
35-
For more information on predicate definitions, refer to the [scopes](/stacks/chainhook/references/scopes) reference page.
57+
<Callout type="info" title="Note">
58+
For additional information, check out the [Bitcoin scopes](/stacks/chainhook/references/scopes/bitcoin) and [Stacks scopes](/stacks/chainhook/references/scopes/stacks) references pages.
3659
</Callout>
3760

38-
## `then-that`
61+
## if_this specification
62+
63+
The `predicate` identifies what data you want Chainhook to scan for using the `scope` define within the `if_this` specification. Additional arguments specific to the `scope` will also be passed here.
64+
65+
```json
66+
// [!code word:if_this]
67+
// [!code word:scope]
68+
{
69+
"if_this": {
70+
"scope": "contract_call",
71+
"contract_identifier": "STJ81C2WPQHFB6XTG518JKPABWM639R2X37VFKJV.simple-vote-v0",
72+
"method": "cast-vote"
73+
}
74+
}
75+
```
76+
77+
## then_that specification
78+
79+
Chainhook delivers the payload when it is triggered by your `predicate` using the `then_that` specification. There are 2 options available:
3980

40-
The `then-that` predicate design can use the following attributes to output the result based on the `if-this` condition defined.
81+
1. `file_append`
82+
2. `http_post`
4183

42-
```jsonc
84+
When choosing to use file_append, specify the path where Chainhook will post the payload data.
85+
86+
```json
87+
// [!code word:then_that]
4388
// [!code word:file_append]
4489
{
4590
"then_that": {
46-
"file_append": {
47-
"path": "/tmp/events.json",
48-
},
49-
},
91+
"file_append": {
92+
"path": "/tmp/events.json"
93+
}
94+
}
5095
}
5196
```
5297

53-
```jsonc
98+
When using `http_post`, specify the endpoint's `url` and `authorization_header`.
99+
100+
```json
101+
// [!code word:then_that]
54102
// [!code word:http_post]
55103
{
56104
"then_that": {
57105
"http_post": {
58-
"url": "https://example.com/api/v1/events",
59-
"authorization_header": "Bearer 1234567890"
106+
"url": "https://webhook.site/abc123456-789e-0fgh-1ijk-23lmno456789",
107+
"authorization_header": "12345"
60108
}
61109
}
62110
}
63111
```
64112

113+
<Callout title="Note">
114+
Chainhook requires `https` to post to your endpoint. You can use a service like [LocalTunnel](https://github.com/localtunnel/localtunnel) to test locally or a site like [WebhookSite](https://webhook.site).
115+
</Callout>
116+
117+
## Blockchain specific configurations
118+
119+
<Tabs id="blockchain" items={["Bitcoin predicate", "Stacks predicate"]}>
120+
<Tab value="Bitcoin predicate">
121+
122+
```json
123+
{
124+
"chain": "bitcoin",
125+
"uuid": "1",
126+
"name": "Wrap BTC",
127+
"version": 1,
128+
"networks": {
129+
"testnet": {
130+
"if_this": {
131+
"scope": "ordinals_protocol",
132+
"operation": "inscription_feed"
133+
},
134+
"then_that": {
135+
"http_post": {
136+
"url": "http://localhost:3000/api/v1/ordinals",
137+
"authorization_header": "Bearer cn389ncoiwuencr"
138+
}
139+
},
140+
"start_block": 10200
141+
// Additional configurations
142+
},
143+
"mainnet": {
144+
"if_this": {
145+
"scope": "ordinals_protocol",
146+
"operation": "inscription_feed"
147+
},
148+
"then_that": {
149+
"http_post": {
150+
"url": "http://my-protocol.xyz/api/v1/ordinals",
151+
"authorization_header": "Bearer cn389ncoiwuencr"
152+
}
153+
},
154+
"start_block": 90232
155+
// Additional configurations
156+
}
157+
}
158+
}
159+
```
160+
161+
<Accordions>
162+
<Accordion title="Additional Bitcoin predicate configurations">
163+
The following additional configurations can be used to improve the performance of Chainhook by preventing a full scan of the blockchain.
164+
165+
Ignore any block before the given block:
166+
167+
```json
168+
"start_block": 101
169+
```
170+
171+
Ignore any block after the given block:
172+
173+
```json
174+
"end_block": 201
175+
```
176+
177+
Stop evaluating a chainhook after a given number of occurrences are found:
178+
179+
```json
180+
"expire_after_occurrence": 1
181+
```
182+
183+
Don't include proofs:
184+
185+
```json
186+
"include_proof": false
187+
```
188+
189+
Don't include proofs:
190+
191+
```json
192+
"include_proof": false
193+
```
194+
195+
Don't include Bitcoin transaction inputs in the payload:
196+
197+
```json
198+
"include_inputs": false
199+
```
200+
201+
Don't include Bitcoin transaction outputs in the payload:
202+
203+
```json
204+
"include_outputs": false
205+
```
206+
207+
Don't include Bitcoin transaction outputs in the payload:
208+
209+
```json
210+
"include_outputs": false
211+
```
212+
213+
Don't include Bitcoin transaction witnesses in the payload:
214+
215+
```json
216+
"include_witness": false
217+
```
218+
219+
Don't include Bitcoin transaction witnesses in the payload:
220+
221+
```json
222+
"include_witness": false
223+
```
224+
</Accordion>
225+
</Accordions>
226+
227+
<Callout type="info" title="Note">
228+
The following command allows you to generate a predicate template for the Bitcoin blockchain.
229+
```
230+
chainhook predicates new your-bitcoin-predicate.json --bitcoin
231+
```
232+
</Callout>
233+
234+
</Tab>
235+
236+
<Tab value="Stacks predicate">
237+
238+
```json
239+
{
240+
"chain": "stacks",
241+
"uuid": "1",
242+
"name": "Contract-Call-Chainhook",
243+
"version": 1,
244+
"networks": {
245+
"testnet": {
246+
"if_this": {
247+
"scope": "contract_call",
248+
"contract_identifier": "STJ81C2WPQHFB6XTG518JKPABWM639R2X37VFKJV.simple-vote-v0",
249+
"method": "cast-vote"
250+
},
251+
"then_that": {
252+
"file_append": {
253+
"path": "/tmp/events.json"
254+
}
255+
},
256+
"start_block": 21443
257+
// Additional configurations
258+
},
259+
"mainnet": {
260+
"if_this": {
261+
"scope": "contract_call",
262+
"contract_identifier": "STJ81C2WPQHFB6XTG518JKPABWM639R2X37VFKJV.simple-vote-v0",
263+
"method": "cast-vote"
264+
},
265+
"then_that": {
266+
"http_post": {
267+
"url": "http://my-protocol.xyz/api/v1/ordinals",
268+
"authorization_header": "Bearer cn389ncoiwuencr"
269+
}
270+
},
271+
"start_block": 142221
272+
// Additional configurations
273+
}
274+
}
275+
}
276+
```
277+
278+
<Accordions>
279+
<Accordion title="Additional Stacks predicate configurations">
280+
Thse additional configurations can be used to improve the performance of Chainhook by preventing a full scan of the blockchain:
281+
282+
Ignore any block before the given block:
283+
284+
```json
285+
"start_block": 101
286+
```
287+
288+
Ignore any block after the given block:
289+
290+
```json
291+
"end_block": 201
292+
```
293+
294+
Stop evaluating chainhook after a given number of occurrences found:
295+
296+
```json
297+
"expire_after_occurrence": 1
298+
```
299+
300+
Include decoded Clarity values in the payload:
301+
302+
```json
303+
"decode_clarity_values": true
304+
```
305+
306+
Include the contract ABI for transactions that deploy contracts:
307+
308+
```json
309+
"include_contract_abi": true
310+
```
311+
</Accordion>
312+
</Accordions>
313+
314+
<Callout type="info" title="Note">
315+
The following command allows you to generate a predicate template for the Stacks blockchain.
316+
```
317+
chainhook predicates new your-stacks-predicate.json --stacks
318+
```
319+
</Callout>
320+
321+
</Tab>
322+
323+
</Tabs>
324+
325+
## Next steps
326+
327+
<Cards>
328+
<Card
329+
href="/stacks/chainhook/guides/observing-contract-calls"
330+
title="Observing contract calls"
331+
description="Learn use a predicate to observe Stacks contracts calls with Chainhook."
332+
/>
333+
<Card
334+
href="/stacks/platform/guides/create-chainhooks"
335+
title="Create a Chainhook"
336+
description="Learn how to create a Chainhook using the Hiro Platform."
337+
/>
338+
<Card
339+
href="/stacks/chainhook/references/scopes/stacks"
340+
title="Stacks scopes"
341+
description="Learn how to use additional scopes to scan for specific Stacks blockchain events."
342+
/>
343+
<Card
344+
href="/stacks/chainhook/references/scopes/bitcoin"
345+
title="Bitcoin scopes"
346+
description="Learn how to use additional scopes to scan for specific Bitcoin blockchain events."
347+
/>
348+
</Cards>
349+

0 commit comments

Comments
 (0)