BSV
$67.98
Vol 164.11m
-11.89%
BTC
$98890
Vol 111556.51m
1.79%
BCH
$495.73
Vol 1749.87m
-6.1%
LTC
$90.6
Vol 1333.41m
2.55%
DOGE
$0.39
Vol 9747.23m
2.59%
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

Blockchain enables autonomous AI agents to learn
Utilizing blockchain tech, a group of Belgian scientists enabled autonomous AI agents to learn and communicate securely, contributing to the...
September 17, 2024
WhatsOnChain gets own UTXO endpoints for BSV blockchain services
With ElectrumX set to retire in October, WhatsOnChain is gearing up to implement a new UTXO set of API endpoints,...
September 16, 2024
Advertisement