Skip to main content

CAT20 DEX

CAT20 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 CAT20 tokens to get satoshis back, and a buy order as a user who places an order that wants to buy CAT20 tokens using satoshis.

As described in the CAT20 specification document, the CAT20 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

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 CAT20Sell 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. A CAT20 sell order supports a partial deal, and this sell covenant change output can be used in the next trades.

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. Note the scalePrice is designed to scale the unit price because the number type is restricted to int32 and we need a way to extend the top value of a sell order.

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.

Buy Order

Feature API

Create

Cancel

Take

Design

A buy order is a user who places an order that wants to buy tokens using satoshis. We introduce a new smart contract CAT20Buy here. The main idea of a buying contract is almost the same as a selling contract, that is confine transaction outputs to distribute satoshis and tokens respectively to the token seller and the token buyer according to the buy order parameters.

The token buyer first locks satoshis into the order (marked as dark green). Anyone who wants to sell tokens to this buy covenant then adds his/her token UTXO (marked as light green) as the transaction input and builds the transfer guard output (marked as blue). The buy 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 token buyer correctly. Note that there might be a buy covenant change output. A CAT20 buy order supports a partial deal, and this buy covenant change output can be used in the next trades.

Dive into details

Let's dive into the contract implementation.

These parameters are almost the same as a sell order. cat20Script marks a specific CAT20 token, buyerAddress is used for canceling an order, price and scalePrice are combined to calculate the satoshis when taking the order. To cancel or take a buy order has no big difference from the sell order, so we will not describe them again here.