BSV
$51.05
Vol 42.38m
2.41%
BTC
$74890
Vol 70626.34m
0.84%
BCH
$376.25
Vol 390.84m
2.85%
LTC
$70.7
Vol 468.07m
1.37%
DOGE
$0.18
Vol 6117.14m
-4.72%
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

BSV joins Linux Foundation to advance open standards
The BSV Association has partnered with the Linux Foundation to advance its objective of promoting development that adheres to BSV...
November 6, 2024
How to construct transactions on BSV blockchain with Python
Python coders, it's time to start learning how to build Bitcoin transactions as nChain's Senior Software Engineer, Arthur Gordon, recently...
November 5, 2024
Advertisement
Advertisement
Advertisement