On-chain Limit Orders Using Covenants

sCrypt
3 min readNov 18, 2023

--

We have developed limit orders on chain. It is based on a type of smart contract called covenants, where an output can be spent only if the transaction that spends it meets specific criteria. To demonstrate how it works, we have implemented Ordinals tokens sell and buy limit orders paid in bitcoins, without a custodian. It can run on any Bitcoin protocol chains such as Bitcoin SV and MVC.

Limit Orders

A limit order empowers a buyer or seller, often referred to as a maker, to establish a specific price at which they are willing to buy or sell an asset within the market. Subsequently, another trader may choose to “take” this offer and carry out the trade. This process is commonly facilitated on custodial exchanges, where the exchange temporarily holds funds from both parties and employs an internal settlement mechanism to distribute funds accordingly. However, this approach involves placing trust in a third party with the funds, exposing them to potential losses through system hacks or internal theft, where the third party may vanish or misappropriate the funds.

Credit

Covenants

Bitcoin covenants restrict how UTXOs are allowed to be spent. It is realized in sCrypt using ScriptContext.

For instance, a maker can place a sell order that locks 10 PEPE tokens into a covenant contract, allowing any purchaser to withdraw up to 10 PEPE tokens so long as they pay the seller, say, 1000 satoshis per PEPE they remove.

Should the buyer wish to transact an amount below the 10 PEPE limit, say 7, they have the option to create a token change output constrained by the same covenant for the remaining 3 PEPEs. In this particular situation, the seller is identified as the maker, and the buyer takes on the role of the taker within the framework of the limit order. This mechanism facilitates the process of partially fulfilling limit orders.

This limit order can be regarded an extension of OrderLock for RUN tokens and OrdinalLock for Ordinals, where only the full amount can be traded.

The contract also allows the seller to cancel the order and take the remaining PEPEs if the order is not 100% filled.

Implementation

We use BSV-20 fungible tokens as an example for creating limit orders, traded for bitcoins.

Sell Limit Orders

A certain amount of BSV-20 tokens gets locked into a covenant smart contract. A buyer can take a fraction or the whole amount of tokens from the smart contract, provided he pays the seller the asking price within the same contract call transaction.

Transaction diagram depicting a sell limit order

A buyer can redeem BSV-20 tokens by transferring satoshis to the buyer’s address within the same contract call transaction. This is done when invoking the contract’s “buy” public method.

There is also a “cancel” method that allows the buyer to retrieve his funds if the order is not 100% filled.

BSV20SellLimitOrder

Buy Limit Orders

The sell limit order is quite similar. Only this time a certain amount of bitcoins gets locked into the smart contract, instead of BSV-20 tokens.

Transaction diagram depicting a buy limit order

Since the smart contract itself cannot confirm the authenticity of BSV-20 tokens, an oracle service is used. The oracle guarantees the validity of the tokens by signing its reference together with the amount and the token’s inscription. The smart contract validates this signature.

--

--

sCrypt

sCrypt (https://scrypt.io) is a web3 development platform specialized in UTXO-blockchains like Bitcoin