CAT721 DEX
CAT721 standard supports instantly on-chain orders to buy and sell tokens, that is
- sell tokens to get satoshis
- buy tokens using satoshis
Here, we define a sell order as a user who places an order that wants to sell CAT721 tokens to get satoshis back, and a buy order as a user who places an order that wants to buy CAT721 tokens using satoshis.
As described in the CAT721 specification document, the CAT721 smart contract enables the token UTXO to be unlocked with a user's signature or a smart contract. This is the basis of implementing trade orders.
Sell Order
Feature API
You can easily call the corresponding features in the SDK to create, cancel, and take sell orders.
Create
Cancel
Take
Update
Design
A sell order is a user who places an order that wants to sell tokens to get satoshis. We introduce a new smart contract CAT721Sell
here. The main idea of a selling contract is that it restricts transaction outputs of distributing tokens and satoshis respectively to the token seller and the token buyer.
As described above, a token UTXO can be unlocked by a smart contract. So the token seller first sends his tokens into a sell covenant (marked as grass green), leading his tokens controlled by a smart contract. Anyone who wants to buy tokens owned by this sell covenant then builds the sell covenant output (marked as dark green) and the transfer guard output (marked as blue) and prepares enough satoshis for the transaction inputs. The sell covenant restricts the outputs of this transaction, it can only be unlocked when this transaction outputs distribute tokens and satoshis respectively to the token seller and the token buyer correctly. Note that there might be a sell covenant change output.
Dive into details
Let's dive into the contract implementation.
The parameters to build a sell covenant output are easy to understand according to the diagram above.
The token seller can also cancel a sell order as wish, that is the reason we need the token seller's address here as the initial argument. We need to check the token seller's signature as well as the match of address and public key hash when he/she wants to cancel the order.
To unlock a sell covenant, besides checking the transaction context, as usual, we still need to confine the outputs.