Skip to content

Commit 9023ce2

Browse files
kenrogersgitbook-bot
authored andcommitted
GITBOOK-331: Add sBTC builder quickstart
1 parent 0b60a2d commit 9023ce2

File tree

4 files changed

+152
-28
lines changed

4 files changed

+152
-28
lines changed

.gitbook/assets/image (25).png

721 KB
Loading

SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
* [Best practices for running a Signer](guides-and-tutorials/running-a-signer/best-practices-to-run-a-signer.md)
9898
* [OpSec Best Practices](guides-and-tutorials/running-a-signer/opsec-best-practices.md)
9999
* [sBTC](guides-and-tutorials/sbtc/README.md)
100+
* [sBTC Builder Quickstart](guides-and-tutorials/sbtc/sbtc-builder-quickstart.md)
100101
* [How to Run an sBTC Signer](guides-and-tutorials/sbtc/how-to-run-sbtc-signer.md)
101102
* [Best practices for running an sBTC Signer](guides-and-tutorials/sbtc/best-practices-for-running-an-sbtc-signer.md)
102103
* [How to Use the sBTC Bridge](guides-and-tutorials/sbtc/how-to-use-the-sbtc-bridge.md)

guides-and-tutorials/hello-stacks-quickstart-tutorial.md

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Hello Stacks Quickstart Tutorial
1+
# Developer Quickstart
22

33
## Build Your First Stacks App in 30 Minutes
44

@@ -10,16 +10,16 @@ This tutorial will help you build a working Stacks application in just 30 minute
1010

1111
**What you'll learn:**
1212

13-
- How to write a Clarity smart contract
14-
- How to deploy contracts to Stacks testnet
15-
- How to connect a wallet to your app
16-
- How to interact with contracts from a frontend
13+
* How to write a Clarity smart contract
14+
* How to deploy contracts to Stacks testnet
15+
* How to connect a wallet to your app
16+
* How to interact with contracts from a frontend
1717

1818
**Prerequisites:**
1919

20-
- Basic familiarity with web development (HTML, CSS, JavaScript)
21-
- A modern web browser
22-
- 30 minutes of your time
20+
* Basic familiarity with web development (HTML, CSS, JavaScript)
21+
* A modern web browser
22+
* 30 minutes of your time
2323

2424
Let's get started!
2525

@@ -50,7 +50,7 @@ Clarity is Stacks' smart contract language, designed for safety and predictabili
5050

5151
Clarity is inspired by LISP and uses a functional programming approach. Everything in Clarity is an expression wrapped in parentheses. This can be a bit overwhelming at first if you are used to languages like JavaScript or Solidity, but the learning curve is short and Clarity is a simple language to understand once you dive in and start using it.
5252

53-
For a more detailed introduction, check out the [Clarity Crash Course](./clarity-crash-course.md) in the docs.
53+
For a more detailed introduction, check out the [Clarity Crash Course](clarity-crash-course.md) in the docs.
5454

5555
### Write the Contract
5656

@@ -135,11 +135,12 @@ The `tx-sender` variable is particularly useful because it's automatically set b
135135
Finally, every public function in Clarity must return a response type, which is why you see `ok` wrapping our return values. This ensures that every function call has a clear success or failure outcome, making your contracts much more predictable and easier to debug.
136136

137137
<details>
138+
138139
<summary><strong>🔍 Deep Dive: Understanding the Contract Code (Optional)</strong></summary>
139140

140141
Want to understand exactly what each part of the contract is doing? Let's walk through every function and concept used in our message board contract. Links to the official documentation are included for each function, so you may dive deeper if you want.
141142

142-
### How We Store Data on the Blockchain
143+
#### How We Store Data on the Blockchain
143144

144145
Let's start with how we're storing our messages. We use [`define-map`](../reference/functions.md#define-map) to create what's essentially a database table on the blockchain:
145146

@@ -165,7 +166,7 @@ Finally, we need a way to keep track of how many messages we've posted so far:
165166

166167
The [`define-data-var`](../reference/functions.md#define-data-var) creates a single variable that persists on the blockchain. We start it at `u0` (that's how you write the number 0 for unsigned integers in Clarity). The `u` prefix might look weird if you're coming from other languages, but it's just Clarity's way of saying "this is a positive integer."
167168

168-
### The Heart of Our Contract: Adding Messages
169+
#### The Heart of Our Contract: Adding Messages
169170

170171
Now let's break down the most important function, the one that actually adds messages to our board:
171172

@@ -184,17 +185,17 @@ When you call [`define-public`](../reference/functions.md#define-public), you're
184185

185186
Inside the function, we use [`let`](../reference/functions.md#let) to create a local variable. This is like declaring a variable inside a function in other languages, but with Clarity's unique syntax. We're creating a variable called `id` and setting it to the current message count plus 1.
186187

187-
*Here's where Clarity might trip you up if you're coming from other languages.* Notice how we write `(+ (var-get message-count) u1)` instead of something like `message-count + 1`. In Clarity, operators like `+`, `-`, `>`, and `<` are actually functions that use prefix notation. So `(+ 2 3)` means "add 2 and 3" (instead of `2 + 3` like you'd write in JavaScript or Python). This is part of Clarity's LISP-inspired syntax where everything is a function call.
188+
_Here's where Clarity might trip you up if you're coming from other languages._ Notice how we write `(+ (var-get message-count) u1)` instead of something like `message-count + 1`. In Clarity, operators like `+`, `-`, `>`, and `<` are actually functions that use prefix notation. So `(+ 2 3)` means "add 2 and 3" (instead of `2 + 3` like you'd write in JavaScript or Python). This is part of Clarity's LISP-inspired syntax where everything is a function call.
188189

189190
The [`var-get`](../reference/functions.md#var-get) function reads the current value of our message counter, and [`+`](../reference/functions.md#add) adds 1 to create the next message ID.
190191

191192
Next, we store the message content using [`map-set`](../reference/functions.md#map-set), which is like inserting a row into a database table. We store the message content with the new ID we just created.
192193

193-
We also store who posted the message using another [`map-set`](../reference/functions.md#map-set) call (*Notice how we use `tx-sender` here*). This is a special variable that Clarity automatically sets to the address of whoever called the function. You can't fake this or manipulate it, which makes it perfect for tracking message authors.
194+
We also store who posted the message using another [`map-set`](../reference/functions.md#map-set) call (_Notice how we use `tx-sender` here_). This is a special variable that Clarity automatically sets to the address of whoever called the function. You can't fake this or manipulate it, which makes it perfect for tracking message authors.
194195

195196
We update our message counter using [`var-set`](../reference/functions.md#var-set), and finally return [`ok`](../reference/functions.md#ok) with the new message ID. In Clarity, all public functions must return either `(ok value)` for success or `(err error)` for failure. This ensures that every function call has a predictable outcome.
196197

197-
### Reading Messages Back
198+
#### Reading Messages Back
198199

199200
Now let's look at how we read messages back from the blockchain. Our simplest function is:
200201

@@ -219,7 +220,7 @@ We have similar functions for getting the message author and the total message c
219220

220221
The message count function is particularly simple because it just reads our counter variable and returns it. No parameters needed since there's only one counter.
221222

222-
### A More Complex Function: Getting Recent Messages
223+
#### A More Complex Function: Getting Recent Messages
223224

224225
Let's look at our most complex function:
225226

@@ -242,7 +243,7 @@ The [`map`](../reference/functions.md#map) function applies another function to
242243

243244
We use [`list`](../reference/functions.md#list) to create a list of message IDs, and [`-`](../reference/functions.md#subtract) for subtraction to calculate which recent messages to fetch.
244245

245-
### What Makes Clarity Special
246+
#### What Makes Clarity Special
246247

247248
Now that you've seen the code, let me explain some of the key concepts that make Clarity different from other smart contract languages.
248249

@@ -678,26 +679,26 @@ Now that you have the basics down, here are some ways to continue your Stacks de
678679

679680
### Learn More About Clarity
680681

681-
- **[Clarity Book](https://book.clarity-lang.org/)**: Comprehensive guide to Clarity development
682-
- **[Clarity Reference](https://docs.stacks.co/docs/clarity)**: Complete documentation of Clarity functions
683-
- **[Clarity Crash Course](https://docs.stacks.co/docs/clarity-crash-course)**: Quick introduction to Clarity concepts
682+
* [**Clarity Book**](https://book.clarity-lang.org/): Comprehensive guide to Clarity development
683+
* [**Clarity Reference**](https://docs.stacks.co/docs/clarity): Complete documentation of Clarity functions
684+
* [**Clarity Crash Course**](https://docs.stacks.co/docs/clarity-crash-course): Quick introduction to Clarity concepts
684685

685686
### Explore Advanced Features
686687

687-
- **Error Handling**: Learn about Clarity's `try!` and `unwrap!` functions
688-
- **Access Control**: Implement admin functions and permissions
689-
- **Token Standards**: Build fungible (SIP-010) and non-fungible (SIP-009) tokens
688+
* **Error Handling**: Learn about Clarity's `try!` and `unwrap!` functions
689+
* **Access Control**: Implement admin functions and permissions
690+
* **Token Standards**: Build fungible (SIP-010) and non-fungible (SIP-009) tokens
690691

691692
### Development Tools
692693

693-
- **[Clarinet](https://github.com/hirosystems/clarinet)**: Local development environment for Clarity
694-
- **[Hiro Platform](https://platform.hiro.so)**: Hosted development environment
695-
- **[Stacks Explorer](https://explorer.stacks.co)**: View transactions and contracts on mainnet
694+
* [**Clarinet**](https://github.com/hirosystems/clarinet): Local development environment for Clarity
695+
* [**Hiro Platform**](https://platform.hiro.so): Hosted development environment
696+
* [**Stacks Explorer**](https://explorer.stacks.co): View transactions and contracts on mainnet
696697

697698
### Community Resources
698699

699-
- **[Stacks Discord](https://discord.gg/stacks)**: Connect with other developers
700-
- **[Stacks Forum](https://forum.stacks.org)**: Ask questions and share projects
701-
- **[Stacks GitHub](https://github.com/stacks-network)**: Contribute to the ecosystem
700+
* [**Stacks Discord**](https://discord.gg/stacks): Connect with other developers
701+
* [**Stacks Forum**](https://forum.stacks.org): Ask questions and share projects
702+
* [**Stacks GitHub**](https://github.com/stacks-network): Contribute to the ecosystem
702703

703704
Happy building! 🚀
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# sBTC Builder Quickstart
2+
3+
Get up and running with sBTC in 30 minutes or less. This guide covers the essentials for working with sBTC as a SIP-010 token in your smart contracts.
4+
5+
### What is sBTC?
6+
7+
sBTC is Bitcoin on Stacks. It's a SIP-010 fungible token that maintains a 1:1 peg with Bitcoin, enabling you to use Bitcoin in smart contracts and DeFi applications on the Stacks blockchain.
8+
9+
Key points:
10+
11+
* **1:1 Bitcoin peg**: 1 sBTC always equals 1 BTC
12+
* **SIP-010 token**: Works like any other fungible token on Stacks
13+
* **Programmable**: Use Bitcoin in smart contracts, DeFi protocols, and dApps
14+
15+
### Quick Setup
16+
17+
#### Prerequisites
18+
19+
In order to get the most from this quickstart, you should familiarize yourself with Clarity, Clarinet, Stacks.js, and the Hiro Platform. These are the fundamental building blocks of building Stacks applications.
20+
21+
* [Stacks Developer Quickstart](../hello-stacks-quickstart-tutorial.md) - For a quick holistic introduction to the Stacks development process, tools, and fundamentals
22+
* [Clarity Crash Course](../clarity-crash-course.md) - For a quick introduction to Clarity
23+
* [Clarinet Docs](https://docs.hiro.so/tools/clarinet)
24+
* [Stacks.js Docs](https://docs.hiro.so/reference/stacks.js)
25+
26+
Choose your preferred development environment:
27+
28+
#### Option 1: Hiro Platform (Recommended)
29+
30+
The fastest way to start building with sBTC is using the Hiro Platform's hosted devnet. The Platform integrates with your GitHub account. You can either import an existing project from GitHub or start with a Platform template and have it synced with your GitHub account.
31+
32+
After you create the project in the Platform, you can clone it locally and work with the Platform's cloud devnet by connecting your API key as described in the template's README files. This will allow you to work on your code locally but let Platform handle the complexities of actually running the devnet.
33+
34+
1. **Create an account** at [platform.hiro.so](https://platform.hiro.so)
35+
2. **Create or import a project**
36+
* Select a template or import your own project from GitHub. There are several templates available to use as a starting point. Some have only smart contracts and some are full-stack dapp templates. Starting with one of these ensures you are building with best practices.
37+
* Navigate to your project dashboard
38+
3. **Start devnet**
39+
* Click the "Devnet" tab
40+
* Click "Start Devnet"
41+
* Wait for status to show "Active"
42+
4. **Connect your wallet**
43+
* Your devnet wallets are automatically funded with STX and sBTC
44+
* Use the provided wallet addresses within the templates
45+
* The platform templates are automatically connected to Devnet, and there are instructions in the READMEs of the templates for how to connect your frontend to your Devnet instance
46+
47+
#### Option 2: Local with Clarinet
48+
49+
If you would prefer to have your devnet running locally instead of in the Platform cloud, you can run one yourself
50+
51+
1. **Install Clarinet** (version 3.x)
52+
53+
```bash
54+
brew install clarinet
55+
```
56+
2. **Create a new project**
57+
58+
```bash
59+
clarinet new my-sbtc-project
60+
cd my-sbtc-project
61+
```
62+
3. **Add sBTC requirements**
63+
64+
```bash
65+
clarinet requirements add SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4.sbtc-deposit
66+
```
67+
68+
This automatically includes the sBTC token contract in your Clarinet context so you can reference it within your contracts.
69+
4. **Start devnet**
70+
71+
```bash
72+
clarinet devnet start
73+
```
74+
75+
With either of these options, your Devnet wallets are automatically funded with sBTC. You just need to include the sBTC contract in your contract requirements as shown above.
76+
77+
### Working with sBTC in Smart Contracts
78+
79+
sBTC follows the SIP-010 standard, making it easy to integrate into your contracts.
80+
81+
The primary function you'll be using is the `transfer` function. That's because sBTC exists as a 1:1 Bitcoin peg via a SIP-010 token. Minting is handled by the protocol, the main function of writing smart contracts that use sBTC is to move it around, which means using the `transfer` function.
82+
83+
Here's a very basic example of how to transfer sBTC within your contract.
84+
85+
#### Basic Transfer Example
86+
87+
Create a new contract that accepts sBTC payments. You can do this within the Clarinet project folder with `clarinet contract new sbtc-payment`.
88+
89+
```clarity
90+
;; contracts/sbtc-payment.clar
91+
92+
;; Define the sBTC token contract reference
93+
(define-constant sbtc-token 'SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4.sbtc-token)
94+
95+
;; Error codes
96+
(define-constant err-insufficient-balance (err u100))
97+
(define-constant err-transfer-failed (err u101))
98+
99+
;; Accept sBTC payment
100+
(define-public (pay-with-sbtc (amount uint) (recipient principal))
101+
(contract-call? sbtc-token transfer
102+
amount
103+
tx-sender
104+
recipient
105+
none))
106+
```
107+
108+
You can test out this contract by either using the UI within the Platform to call the functions directly if you have devnet running or by opening the console with `clarinet console`.
109+
110+
Once you do that you'll see that your devnet accounts have automatically been funded with sBTC.&#x20;
111+
112+
<figure><img src="../../.gitbook/assets/image (25).png" alt=""><figcaption></figcaption></figure>
113+
114+
Once you are ready to deploy to testnet, you can do so by editing your deployment plan as outlined in [this guide](https://docs.hiro.so/tools/clarinet/sbtc-integration).
115+
116+
### Conclusion
117+
118+
You can build pretty much anything you want using this simple foundation, as all of the complexity of sBTC is handled behind the scenes by the protocol.
119+
120+
What's needed now is for builders to take this foundation and build interesting, useful things with it. sBTC unlocks a lot of additional functionality for Bitcoin that previously would have only been possible with either custodied solutions or slow, complex solutions with poor UX.
121+
122+
If you are interested, you can read more about how sBTC works in the [sBTC Concept Guide](../../concepts/sbtc/).

0 commit comments

Comments
 (0)