Skip to content

DappsAtlas index · 14,224 dApps

DappsAtlas

Blockchain · Explainer

Building Smart Contracts and dApps on Bitcoin with Stacks 2.0

Stacks is a smart contract layer that works alongside Bitcoin, letting developers write applications that use BTC-backed assets. This guide explains how the network fits with Bitcoin, how Clarity contracts are built and tested, and how to deploy a Stacks app with care.

By the DappsAtlas editorial team · Updated

Illustration for Building Smart Contracts and dApps on Bitcoin with Stacks 2.0

What Stacks is and how it relates to Bitcoin

Stacks is a smart contract and application layer that sits next to Bitcoin rather than replacing it. Its project describes the network as a Bitcoin Layer-2 that lets contracts and applications use Bitcoin as a secure base layer. Some third-party sources describe it differently, calling it a Bitcoin layer or a separate chain that settles on Bitcoin. When you read about it, check how each source defines the relationship.

For developers the practical picture is the same either way:

  • Stacks has its own blocks, transactions and virtual machine for contract code.
  • Bitcoin provides the base-layer ledger that Stacks activity is anchored to.
  • Applications interact with Stacks addresses, contracts and assets, and some assets are backed by BTC through sBTC.

This means you write code for a different execution environment than Bitcoin's own scripting. Bitcoin's base layer is designed for simple, deliberate transactions, while Stacks adds a contract language, a state model and a set of token standards on top. Your app will call Stacks contracts, read Stacks state and sign Stacks transactions with a Stacks-compatible wallet. Keep that separation clear in your architecture and your user documentation, because users often confuse which chain holds which asset.

How Stacks consensus connects to Bitcoin

Stacks uses a consensus design that ties its block production to Bitcoin. In the Proof of Transfer model, participants commit BTC to compete for the right to produce Stacks blocks. STX holders can also take part in stacking, which locks STX for a set number of cycles in exchange for BTC rewards. The exact minimums, cycle lengths and reward rules change over time, so read the current stacking documentation before giving anyone guidance.

As an application developer, you do not need to run a miner or a stacker. The consensus model still matters to you in three ways:

  • Block timing and finality affect how long users wait for a transaction to be confirmed.
  • Fees are paid in STX, so your users need STX to interact with your contracts.
  • Major protocol upgrades can change behavior, so track the release notes for the network version you target.

The Nakamoto upgrade changed how Stacks blocks are produced and aimed to speed up block times and improve finality relative to earlier versions. If you read older tutorials that describe slower blocks or different confirmation rules, verify them against current documentation. Treat any claim about exact block times or finality depth as something to check on the official Stacks site, since these details are operational and can change.

Clarity: the smart contract language on Stacks

Clarity is the smart contract language on Stacks. It is designed so that what a program will do can be reasoned about before it runs. Several design choices follow from that goal:

  • Clarity is decidable, which means the language avoids unbounded loops. Iteration uses bounded helper functions instead.
  • Contracts are type checked when they are deployed, so many mistakes are caught before any user interacts with them.
  • Arithmetic is checked by default, so overflow and underflow produce errors rather than silent wraparound.
  • Contract source code is readable on chain, which lets users and reviewers inspect what they are interacting with.

A Clarity contract is made of functions and definitions. Public functions can change state and can be called by anyone, subject to their own logic. Read-only functions cannot change state and are useful for reporting balances or configuration. Private functions are helpers that only the contract itself can call. Data is stored in data variables, maps and token definitions. Functions return response types, where ok carries a value and err carries an error code, which lets callers handle failures explicitly.

If you come from Solidity or another language, expect a different mental model. Clarity does not use inheritance-style patterns, and it has no hidden fallback behavior. That can feel restrictive at first, but it makes contract behavior easier to audit and reason about.

Setting up a development environment for Stacks

A good Stacks setup has a few parts that work together. Use this order so that each step is testable before you move on.

  1. Install Node.js, since many Stacks tests and frontend tools use it.
  2. Install Clarinet, the standard local toolkit for Clarity projects. Confirm the installed version runs from your terminal.
  3. Create a new Clarinet project. It will include folders for contracts, tests and configuration.
  4. Open the project in a code editor that supports Clarity syntax highlighting, or install a Clarity extension for your editor.
  5. Install a Stacks-compatible browser wallet and create a test account. Never use a wallet holding real funds for development.
  6. Get testnet STX from an official or widely used testnet faucet. Check the current faucet rules, since limits and availability can change.
  7. Install the Stacks JavaScript library if you plan to build a web frontend.

When the project runs, you should be able to start a local development environment, see your accounts pre-funded in local test mode, and call a sample contract from the console. If a step fails, check the official Stacks documentation for the version you installed, because command names and project layouts change between releases.

Writing and testing your first Stacks contract

A small counter contract shows the main building blocks of Clarity: a data variable, a public function and a response value. The example below only illustrates syntax. Test and review anything you deploy.

(define-data-var counter uint u0) (define-public (increment) (begin (var-set counter (+ (var-get counter) u1)) (ok (var-get counter))))

To build a real contract, work through these steps in order:

  1. Define the state you need, such as data variables for settings and maps for per-user balances.
  2. Write read-only functions first so you can inspect state from the console.
  3. Add public functions that change state, and return ok or err for every path.
  4. Use private helper functions to keep logic small and easy to test.
  5. Write tests for normal use, edge cases and failures, including callers who are not authorized.
  6. Run the test suite after every change, and keep the tests in version control.

Keep access control simple. If a function should only be called by an administrator, check the caller at the start of the function and return an error for anyone else. Document each public function, including what it changes and what errors it can return. Good documentation helps frontend developers and auditors, and it makes your contract easier to maintain after launch.

Working with STX, sBTC and token standards

Stacks apps use a few standard asset types, and choosing the right one affects how users interact with your product.

  • STX is the native token. It pays transaction fees and is used for stacking.
  • sBTC is designed as a Bitcoin-backed asset on Stacks. Check its current status, supported operations and risks in official sources, because the design and rollout details can change.
  • Fungible tokens on Stacks follow the SIP-010 standard, which defines common functions for transfers, balances and metadata.
  • Non-fungible tokens follow the SIP-009 standard, which defines ownership and transfer functions for unique assets.

Using a standard helps wallets, explorers and other apps understand your token without custom integration. When you define a token, decide on its supply rules, who can mint or burn, and how users will receive it. Keep minting authority tightly controlled and document it.

When your app moves value, consider post-conditions, which describe the asset movements a transaction is expected to make. Wallets can display these conditions to users, and a transaction that does not match them will fail. This protects users from unexpected outcomes if a contract behaves differently than they expected. Always test your post-conditions against realistic amounts on testnet.

Connecting a web frontend and wallets

Most Stacks apps have a web frontend that lets users connect a wallet, read contract state and submit transactions. The Stacks JavaScript library provides helpers for these tasks. A typical flow looks like this:

  • Read-only calls fetch data from the chain without requiring a signature, so you can show balances and status before a user connects.
  • Wallet connection asks the user for permission to share an address with your app.
  • Transaction requests describe the contract call, its arguments, the fee and the post-conditions. The wallet shows this to the user for approval.
  • Confirmation tracking checks whether the transaction has been included and whether it succeeded or failed.

Show users plain-language summaries of what they are approving, including which contract they are calling, what assets will move and which fee applies. Handle rejected requests and failed transactions with clear messages instead of a generic error. If a transaction is pending for a long time, tell the user how to check its status on a public explorer.

Do not store private keys or seed phrases in your frontend, and do not ask users to type them into your site. Your app should never need custody of user funds to function. Keep dependencies updated, and review your frontend for code that could alter the transaction a user sees before signing.

Reading chain data and using oracles

Your app will need reliable access to Stacks data, including contract state, transaction history and balances. Start with the public Stacks API endpoints described in the official documentation. For heavier workloads, such as dashboards that track many contracts or historical events, consider an indexer that stores the data in a database you control. Evaluate any third-party provider on uptime history, rate limits, pricing and the chains and networks it supports.

If your app depends on outside data, such as asset prices, check whether the provider supports Stacks before you build on it. Oracle providers such as Pyth Network list the chains they support, and coverage can differ from one network to another. Do not assume that a provider covers Stacks because it covers Bitcoin or another chain.

For analytics about apps and protocols across chains, data aggregators such as DefiLlama can help you compare activity and value locked, but treat any figure as a snapshot and confirm it at the source before using it in your product or research. Build your app so that it degrades gracefully if a data source is slow or unavailable. Show users when data is stale, and avoid making irreversible decisions based on a single unverified feed.

Security checklist before you ship

Security is most important before launch. Work through this checklist:

  • Test every public function with normal inputs, boundary values and unauthorized callers.
  • Use strict post-conditions in transactions that move assets, so unexpected outcomes fail safely.
  • Keep admin keys separate from everyday accounts, and use multisig where your setup allows it.
  • Limit what administrators can change. If they can pause or upgrade the contract, document that power publicly.
  • Plan how you will respond to a bug, including how users will be told and how funds would be handled.
  • Get an independent security review for any contract that holds user funds, and fix findings before deployment.

Clarity's checked arithmetic and type checking remove some classes of bugs, but they do not replace careful design. Logic errors, unclear permissions and unsafe assumptions about external data still cause failures. Keep contracts small, reuse well-reviewed standards where possible and avoid clever code that is hard to read.

For users, the main risks are phishing and fake frontends. Tell users to check the domain they are on, to confirm contract names in the wallet before signing and to never share seed phrases. Publish your official contract addresses in a way that is hard to fake, and keep a clear support channel that does not ask for keys.

Deploying to testnet and mainnet safely

A safe launch follows a sequence. Deploying straight to mainnet is the most common avoidable mistake.

  1. Deploy to a testnet and run your full test suite against the deployed contracts.
  2. Test the frontend end to end with testnet accounts, including failed and rejected transactions.
  3. Ask a small group of trusted testers to try the app and report confusing steps.
  4. Complete an independent review and resolve findings.
  5. Prepare mainnet deployment with a funded deployer account. Deployment and contract calls are paid in STX, so check current fee levels before you send transactions.
  6. Publish the verified contract source and the exact contract addresses.
  7. Monitor contract activity after launch and watch for unusual calls.

Contracts on Stacks cannot be silently changed after deployment, so plan an upgrade strategy before launch. Some teams deploy new versions under new names and migrate users. Others use proxy-style patterns where a controlled contract points to implementation logic. Whichever you choose, document it and make sure users can see who controls upgrades.

Finally, write for your users. Explain what your app does, what it costs, what risks exist and how to get help. A well-documented, carefully tested Stacks app is more useful than a fast launch with unclear behavior.

Frequently asked questions

Is Stacks a Layer-2 or a separate blockchain?

Stacks is a smart contract and application layer that works alongside Bitcoin. Its project describes it as a Bitcoin Layer-2 that lets contracts and applications use Bitcoin as a secure base. Some third-party sources describe it as a Bitcoin layer or a separate chain that settles on Bitcoin, so check how each source defines the relationship before relying on it.

What language do I write Stacks smart contracts in?

Clarity is the smart contract language used on Stacks. It is designed so that a program's behavior can be checked before it runs, and it avoids unbounded loops. Developers write public, read-only and private functions and store state in data variables and maps. Contract source is readable on chain, which helps reviewers.

What tools do I need to start building on Stacks?

Clarinet is the standard local development toolkit for Clarity. It lets you run a local chain, call contract functions from a console, and write tests. Install it, create a project, write your contract, run the test suite, and only then move to a public testnet. Check the official Stacks documentation for current install steps.

How do I keep users safe when they sign Stacks transactions?

Stacks transactions can include post-conditions, which tell the wallet what asset movements a transaction should produce. If the outcome does not match, the transaction fails. Use strict post-conditions in your frontend, test them, and clearly explain to users what each signature will do. Never ask a user to share a seed phrase or private key.

Do I need STX to deploy or use an app?

Stacks has its own fee token, STX, and fees for deploying and calling contracts are paid in STX. Check the current network fee guidance and the wallet's transaction preview before signing, since fees and congestion can change over time.

Mentioned in this guide

dApps in this guide

Defillama

Analytics

Score 8.0 out of 10

DefiLlama is a data site that aggregates total value locked, trading volume and fees for DeFi protocols across many chains.

Pyth Network logo

Pyth Network

Infrastructure

Score 8.0 out of 10

Pyth Network is a first-party price oracle and market data provider for blockchain apps, trading venues and institutions, with its own PYTH token.

TVL $0Solana
Clarity logo

Clarity

DAO Service Providers

Score 7.4 out of 10

Clarity is a Cardano governance and DAO tooling platform for proposals, voting, treasuries and contributor rewards, aimed at Cardano communities and teams.

TVL $341K−5.2%Cardano

Keep reading

Related guides

All blockchain guides

Blockchain · How-to

How to Build Dapps on Telos EVM in 2026

Telos EVM lets Solidity developers reuse familiar Ethereum tooling on a low-fee chain. This guide walks through setup, contract work, testnet deployment, frontend connection, and the checks to run before mainnet.

Blockchain · Best list

Best Injective dApps for DeFi, NFTs and More in 2026

Injective is a Layer-1 chain built for finance, so most of its dApps are trading, lending and derivatives venues, with NFTs and games as smaller areas. This guide explains how to judge which Injective dApps are worth using and how to check their current status.

Blockchain · Chain guide

Boba Network in 2026: Future of Dapps and DeFi

Boba Network is an Ethereum layer-2 whose future for dapps and DeFi depends on liquidity, bridges and its current activity. This guide covers the stable facts, the factors to watch and how to check the network's status yourself.

Blockchain · Explainer

Can Ethereum Classic Support dApps? A 2026 Explainer

Ethereum Classic can run Solidity smart contracts, so it can host dapps, but its smaller user base, thinner tooling and proof-of-work security trade-offs shape what a builder should expect. This explainer covers how it works, where it fits, and how to check its current state.

Blockchain · Best list

10 Blockchains That Could Replace Ethereum in 2026

Ethereum is unlikely to be replaced outright, but several networks compete for its developers, users and capital. This guide explains what each rival offers, where the trade-offs sit and how to check current data yourself.