Need a crash course on Bitcoin layers?
→ READ OUR FREE GUIDE
Need a crash course on Bitcoin layers?
→ READ OUR FREE GUIDE
Need a crash course on Bitcoin layers?
→ READ OUR FREE GUIDE
Need a crash course on Bitcoin layers?
→ READ OUR FREE GUIDE
Need a crash course on Bitcoin layers?
→ READ OUR FREE GUIDE

Bitcoin Ecosystem: An Overview of Bitcoin Smart Contracts and How They Work

Bitcoin is rarely featured in the discussion of smart contracts and “Web3” because the Bitcoin blockchain has a limited scripting language that prioritizes security over programmability. That’s changing: thanks to Bitcoin layers, Bitcoin smart contracts are here.

Type
Deep dive
Topic(s)
Bitcoin
Stacks
Published
January 25, 2024
Author(s)
Lead Content Manager
What are Bitcoin smart contracts? Thought you'd never ask
Contents

Today, developers have hundreds of blockchains that they can choose from when deciding where to build a Web3 app. Ethereum is no longer the only game in town when it comes to smart contracts, and most recently, developers can now build smart contracts and decentralized apps on the oldest blockchain—Bitcoin itself. Let’s get into how Bitcoin smart contracts work. 

What Exactly Is a Smart Contract?

Smart contracts are blockchain programs designed to run autonomously when predefined events or actions occur. The terms of a smart contract are specified in code to eliminate the need for human execution, arbitration, or enforcement, and since that code is stored on the blockchain, it can’t be tampered with.

These smart contracts power decentralized apps across all of Web3 and their logic can be as simple or complex as devs design them to be. A smart contract could be a relatively simple escrow contract or a contract that contains the pricing curve of an automated market maker in a decentralized exchange powering asset swaps.

At a high level, smart contracts require two things:

  1. Global state: In order to work autonomously, smart contracts need to be able to correctly read the state of the blockchain in order to function (e.g. the contract needs to be able to read accurate data, such as the token balances of a user’s account, the status of a particular transaction, etc).
  2. Computation: Smart contracts are pieces of code that execute logic. That execution requires an expressive programming language as well as computation. We’ll get back to this and how the network is incentivized to handle that computation in a bit.

The Programmability of Bitcoin

Bitcoin is a notoriously difficult blockchain to build on. It was designed to be a decentralized currency, one that is trustless, highly secure, and peer-to-peer. As part of that design, Bitcoin Script (its programming language) is very limited in what it can do. That reduces the surface area for vulnerabilities and thus makes Bitcoin more secure, but it comes at the cost of programmability.

Today, you can only do a few things on Bitcoin that are more complex than simply sending and receiving bitcoin. Those functionalities include:

  • Multi-signature scripts: Multiple addresses must sign a transaction before BTC held by that script can be sent.
  • Time-locked scripts: A certain period of time must elapse before BTC in the script can be sent.

The programming capabilities available on Bitcoin today are enabled by Opcodes (operation codes), the commands and functions of Bitcoin Script. However, the functionalities of opcodes are limited, and introducing new opcodes requires a BIP and a Bitcoin fork (it’s hard to do).

In short, you can’t build more complex smart contracts than this on the Bitcoin blockchain itself today. There is work being done on Bitcoin-native smart contracts in the R&D front, such as Covenants, Discreet Log Contracts (DLCs), RGB, BitVM, and more, but many these remain hypothetical, and it’s unclear which, if any, will ever see the light of mainnet.

Instead, when we talk about Bitcoin smart contracts today, we are talking about Bitcoin smart contracts that live on Bitcoin layers.

So What Is a Bitcoin Smart Contract?

Bitcoin smart contracts exist on Bitcoin layers, separate blockchains or execution environments that leverage Bitcoin’s security and latent capital. Bitcoin smart contracts, like other smart contracts, ensure trustless transactions and are autonomous pieces of logic that can execute complex, multi-step transactions without a human intermediary.

And further, the transactions of Bitcoin smart contracts ultimately settle on the Bitcoin blockchain, making the history of transactions more durable through Bitcoin’s battle-tested security. These contracts can also read and react to transactions occurring on the Bitcoin L1 and often interact with a tokenized version of BTC that has been bridged to the Bitcoin layer. All of this is possible today, without making any changes to the Bitcoin core protocol itself. 

Example of a Bitcoin Smart Contract at Work 

Let’s take a closer look at a Bitcoin smart contract on Stacks, the largest Bitcoin layer today (learn more about the projects building on Bitcoin), and in particular, at the Bitcoin DeFi app Arkadiko to see how Bitcoin smart contracts work in practice.

Arkadiko enables users to take out a self-repaying loan in USDA (a stablecoin) that is backed by the user’s deposited STX tokens. This is how it works at a high level.

  1. A user deposits STX tokens into an Arkadiko vault as collateral.
  2. The user can then borrow USDA, in an amount less than or equal to 25% of the initial value they deposited.
  3. The requested USDA is sent to the user’s wallet.
  4. When depositing into the vault, the user can choose to stack the STX collateral via Stacks’ Proof of Transfer mechanism to earn BTC yield.
  5. That BTC yield pays off the borrowed USDA. When the USDA loan is fully repaid, the assets in their vault unlock and the user can withdraw them.
  6.  In the event that the loan isn’t repaid, or the collateralized loan falls under the liquidation threshold, the user’s assets are sold in an auction until the debt and penalty fees are covered.

The reason I'm listing out all of these steps is to point out that all of this complexity happens autonomously. There are no individuals signing off at different stages. Importantly, this also means that individuals cannot interfere or manipulate the process. 

Once all of those transactions occur on Stacks, they then settle on Bitcoin through Stacks’ unique consensus mechanism Proof of Transfer, where a hash of all Stacks transactions is stored on the Bitcoin blockchain. This means that to change the history of those smart contract transactions, you’d need to change the history of Bitcoin itself. That is the power of Bitcoin smart contracts. 

Why Do Bitcoin Smart Contracts Need a “Gas” Asset?

One question we hear a lot is why do Bitcoin smart contracts, and by extension Bitcoin layers as a whole, need a second token (the first being BTC)?

This goes back to the earlier point about smart contracts needing two things to function: global state and computation. You need a global state, so that contracts can react to changes in that data layer (this is why Bitcoin layers like Lightning, which lack global state, are limited in the amount of complexity you can program with them).

As for computation, when it comes to smart contracts, someone, somewhere needs to execute, verify, and store transactions from those contracts, and computation isn’t free. You need a way to incentivize miners to provide that computation for the network.

Simply put, if miners are not incentivized, they won’t do the work, and the network will stall. The way you incentivize miners to do that computational work then is through a second token, often called a “gas” asset. This second token is used to pay for transaction fees on the network and is how you incentivize an anonymous open network of miners to do the work: you pay them.

This second token is what enables the open network to flourish in the first place. It:

  1. Provides a rational incentive for miners to maintain the network.
  2. Prevents spam transactions from clogging the network (users have to pay a fee for every transaction submitted to the network).
  3. Creates a healthy fee market, where the price of the gas asset rises and falls based on the supply and demand for the network’s processing capacity. For example, a user might pay higher fees if their transaction involves a lot of computation or is occurring at a time when block space is in high demand, such as during a popular NFT mint.

Open smart contract networks require gas assets to create healthy, open markets, and this is why Stacks has the (STX) token. Without a gas asset, it is still possible to create a functioning network in a closed-federated system—and there are Bitcoin layers that operate without a second token—but that brings its own risks. These models have a more centralized design and are often controlled by a federation of entities, and there are security risks that come with that.

Benefits of Bitcoin Smart Contracts 

So you can build smart contracts on Bitcoin. Big deal, right? Why would you want to build a decentralized app on Bitcoin instead of the hundreds of other chains you could build on? There are two core parts to the value proposition:

1. $800B+ of Latent Capital

One of the big benefits that Bitcoin smart contracts deliver is they make Bitcoin programmable for developers. There is a LOT of latent capital in Bitcoin with a market cap that currently sits around $850 billion, as of January 12th, 2024; yet the vast majority of that Bitcoin is just sitting in cold storage. For example, less than $15B of that total is locked in DeFi. And most of that is wrapped Bitcoin on other blockchains as opposed to native Bitcoin DeFi. 

Bitcoin smart contracts can tap into that capital and make it programmable, meaning devs can build apps on top of Bitcoin that can interact with Bitcoin, and users can put their BTC to work in various use cases.

That massive amount of latent capital benefits both devs and users:

  • Devs want cash inflow and new users for their apps. The Bitcoin ecosystem has $800B+ capital on the sidelines waiting to be deployed in apps, and there are 100M+ BTC holders who could become users. The opportunity is already there and just needs to be capitalized on.
  • Users can put their BTC to work instead of leaving it in cold storage and can benefit from deeper liquidity (which translates to better pricing and price discovery).

2. Bitcoin’s Battle-Tested Security

A smart contract’s most powerful feature is its trustless execution. Trustlessness in smart contracts means you don’t have to trust a third party, such as a bank, a person, or any other middleman, in your transactions. Instead, you trust the code.

However, for “trustless” systems to gain mass adoption, the system must be provably secure from manipulation. You still have to trust the code, and the underlying blockchain. This is where Bitcoin shines.

When it comes to smart contracts, where bugs and hacks can cause real-world consequences with no recourse, security is paramount. And when it comes to security, Bitcoin is king.

Bitcoin is the most secure and decentralized blockchain network in the world right now — it has never been hacked, and the financial resources required to pull off a successful attempt make such an endeavor unfeasible.

Furthermore, the Bitcoin network is open — anyone can contribute to Bitcoin and anyone can become a miner. Over the years, Bitcoin has grown to have a large, decentralized network of miners and developers globally. By design, it is practically impossible for miners or users to collude to alter Bitcoin’s state or ledger. This inherent decentralization and immutability gives Bitcoin smart contracts a secure foundation.

Harness the Power of Expressive Bitcoin Smart Contracts

Bitcoin has disrupted the world of finance by demonstrating how money can exist and function outside the direct control of governments. Now, thanks to Bitcoin layers like Stacks, Bitcoin can extend its disruption as developers create fully expressive smart contracts to create new types of decentralized applications and use cases. Welcome to a future denominated in Bitcoin.

Product updates & dev resources straight to your inbox
Your Email is in an invalid format
Checkbox is required.
Thanks for
subscribing.
Oops! Something went wrong while submitting the form.
Copy link
Mailbox
Hiro news & product updates straight to your inbox
Only relevant communications. We promise we won’t spam.

Related stories