Product updates & dev resources straight to your inbox
Developers in the Stacks ecosystem recently demonstrated that Bitcoin DeFi is possible today. Longtime Stacks community members Friedger Muffke, Asteria, and Jude Nelson in collaboration with other Stacks community developers, deployed working Bitcoin swaps with NFTs and other crypto-assets.
Native BTC swaps to new assets are a foundational building block of Bitcoin DeFi. In essence, Ethereum-like functionality is now possible directly on Bitcoin. This will lead to an explosion of advanced decentralized apps and Uniswap-like AMMs built around native BTC swaps, all using pure Bitcoin transactions and backed by the security of Bitcoin.
With this development, users will soon be able to do trust-less BTC swaps to stablecoins, derivatives, perpetuals, and other crypto assets built on Stacks, as well as purchase any digital assets (like NFTs and decentralized domains), all by pure Bitcoin transactions on the Bitcoin chain. With USDC coming to Stacks, trust-less BTC/USDC swaps will be possible. This is a huge market — nearly $150M in daily volume that can be traded without exchanges! Similarly, xBTC will enable trust-less swaps of BTC for xBTC, which can then be used in a range of DeFi products.
In this post, I’ll focus on the recent trust-less swap development pioneered by Friedger and fellow devs in the Stacks ecosystem, and I’ll show you how you can try it yourself using Clarity.
But first, let’s talk about how we got to this point.
True Bitcoin DeFi
Bitcoin DeFi has historically been difficult due to Bitcoin’s limited scripting language, which doubles as its strength. Bitcoin is not easily programmable. Bitcoin purposefully hasn’t provided smart contract abilities as we’ve seen in other ecosystems. This was an intentional tradeoff to provide security and reliability for Bitcoin as a decentralized store of value.
Developers have tried to tackle this challenge by creating derivative Bitcoin assets that require centralized counter-parties or separate blockchains with wrapped BTC. But wrapped BTC is not Bitcoin, and many Bitcoiners don't trust the security and decentralization of separate blockchains or one-party custody solutions. There has been demand for DeFi-like products for Bitcoin on centralized alternatives, but true Bitcoin DeFi hasn’t been cracked til now.
And what do we mean by true Bitcoin DeFi? The key component is the ability to send an on-chain Bitcoin transaction and execute logic in a smart contract, thus removing the need for a central intermediary.
Stacks is uniquely positioned to enable true Bitcoin DeFi, given Stacks contracts' visibility into the Bitcoin state as well as Stacks’ inherent ability to leverage Bitcoin’s security and settlement assurances. The Stacks chain is tethered to Bitcoin through its Proof of Transfer consensus mechanism, so all Stacks transactions settle on Bitcoin. This ensures that Stacks shares Bitcoin’s long-term, unparalleled security for transaction reorgs.
Introducing Catamaran Trust-Less Swaps
Last month, Stacks community developers deployed working Bitcoin swaps with contracts that have unique visibility into Bitcoin state and that can trigger actions in Stacks-based smart contracts driven by pure Bitcoin transactions. These are currently being called “Catamaran swaps,” transactions that demonstrate a world of opportunity for Bitcoin DeFi applications enabled through Stacks. These are trust-less swaps, meaning there is zero reliance on any third party or custodial service to execute the swap — no KYC, no counter-party risk.
The term is an analogous extension of the more well-known concept of Submarine swaps in the Lightning community. Submarine swaps are atomic on-chain to off-chain swaps. Catamaran swaps are three leg swaps where two transactions happen on the Stacks chain and one transaction happens on the Bitcoin chain. In contrast to Submarine swaps where some actions happen on-chain (over water) and some off-chain (under water), all actions happen on-chain just on two different blockchains, hence Catamaran.
This implementation of Catamaran swaps relies on another Clarity contract that verifies that a given Bitcoin transaction was mined at a given block on the Bitcoin chain. The verification happens by comparing the hash of the provided block details with the hash viewable via Clarity's block info function. Then the merkle root of the Bitcoin transaction and the provided merkle proof is compared with the merkle root of the verified block. If these hashes are identical it can be concluded that the Bitcoin transaction was indeed included in the block on the Bitcoin chain.
Diving a bit deeper into the mechanics, in the first Stacks transaction a digital asset is placed into escrow of the smart contract and parameters like the Bitcoin receiver address, amount of BTC, time limit, etc., are defined for the swap. A Bitcoin transaction is then performed. Once confirmed, the second Stacks transaction verifies the Bitcoin transaction and releases the Stacks asset.
Written in Clarity, these swap contracts are only 70 lines long and use the verification logic of the Bitcoin library contract. A typical contract function that verifies BTC transactions looks like the following:
More contracts on https://github.com/friedger/clarity-friedger-pool
Similar to swaps, other logic can be triggered when a Bitcoin transaction is triggered, e. g. pay back a loan, mint new tokens... (e.g. Friedger Pool mints wrapped stacking reward tokens for each Bitcoin reward transaction).
Currently there are some technical restrictions that can make a trust-less swap fail. Bitcoin transactions that are too big (>1024 bytes, >8 ins or outs) or transactions that happen during a flash block cannot be verified on chain. Improvements will come with Stacks 2.1. Therefore, the current version of the swap contracts have a cancel function that releases the assets to the seller after a certain time limit. In these cases, the asset needs to be transferred manually.
Improvements to Swaps
These are the earliest days of development, and improvements can be made with Stacks 2.1 to reduce the number of transactions required in these interactions from 3 to 2, improve on flash block failures, unblock any other existing network limitations, and smooth the UI of such transactions both for end users and developers alike.
Stacks is not the only project that is targeting Bitcoin DeFi, and rightly so given what is at stake for the Bitcoin ecosystem. Alternative wrapped Bitcoin assets on the market today have brought innovation to the space and in some cases have demonstrated meaningful traction. That said, none of these assets to date actually settles on Bitcoin — they settle on other networks, and some developers and users have questions about the assets’ security and decentralization.
There are also ongoing experiments such as RSK which bridges to the Ethereum Virtual Machine, Lightning which scales Bitcoin, and Liquid which uses a Bitcoin side chain. Developments such as Taproot and Sapio also enable more functionality for Bitcoin at lower levels.
Come Build with Us
This is just the beginning of an entire open financial world being created around Bitcoin. We’re planning to do further posts on Bitcoin DeFi, so look out for additional posts to come. In the meantime, check out the Catamaran swap implementation on GitHub, join the Stacks Community Discord for more info, and get building with our easy-to-use tools.