BSV
$54.81
Vol 30.45m
2.18%
BTC
$97139
Vol 40666.75m
-0.11%
BCH
$457.48
Vol 355.69m
1.79%
LTC
$102.87
Vol 838.57m
3.15%
DOGE
$0.32
Vol 5052.57m
0.22%
Getting your Trinity Audio player ready...

This post was first published on Medium.

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.

Limit orders
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.

Sell limit orders
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.

Buy limit orders
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.

Watch: Bitcoin is a highly oiled, well running machine: sCrypt CEO Xiaohui Liu

Recommended for you

Google unveils ‘Willow’; Bernstein downplays quantum threat to Bitcoin
Google claims that Willow can eliminate common errors associated with quantum computing, while Bernstein analysts noted that Willow’s 105 qubits...
December 18, 2024
WhatsOnChain adds support for 1Sat Ordinals with new API set
WhatsOnChain now supports the 1Sat Ordinals with a set of APIs in beta testing; with this new development, developers can...
December 13, 2024
Advertisement
Advertisement
Advertisement