Path: blob/main/docs/versioned_docs/version-v0.25/guide/08-interchange/04-creating-order-books.md
1709 views
------Implement the Order Books
In this chapter, you implement the logic to create order books.
In the Cosmos SDK, the state is stored in a key-value store. Each order book is stored under a unique key that is composed of four values:
Port ID
Channel ID
Source denom
Target denom
For example, an order book for marscoin and venuscoin could be stored under dex-channel-4-marscoin-venuscoin.
First, define a function that returns an order book store key:
The send-create-pair command is used to create order books. This command:
Creates and broadcasts a transaction with a message of type
SendCreatePair.The message gets routed to the
dexmodule.Finally, a
SendCreatePairkeeper method is called.
You need the send-create-pair command to do the following:
When processing
SendCreatePairmessage on the source chain:Check that an order book with the given pair of denoms does not yet exist.
Transmit an IBC packet with information about port, channel, source denoms, and target denoms.
After the packet is received on the target chain:
Check that an order book with the given pair of denoms does not yet exist on the target chain.
Create a new order book for buy orders.
Transmit an IBC acknowledgement back to the source chain.
After the acknowledgement is received on the source chain:
Create a new order book for sell orders.
Message Handling in SendCreatePair
The SendCreatePair function was created during the IBC packet scaffolding. The function creates an IBC packet, populates it with source and target denoms, and transmits this packet over IBC.
Now, add the logic to check for an existing order book for a particular pair of denoms:
Lifecycle of an IBC Packet
During a successful transmission, an IBC packet goes through these stages:
Message processing before packet transmission on the source chain
Reception of a packet on the target chain
Acknowledgment of a packet on the source chain
Timeout of a packet on the source chain
In the following section, implement the packet reception logic in the OnRecvCreatePairPacket function and the packet acknowledgement logic in the OnAcknowledgementCreatePairPacket function.
Leave the Timeout function empty.
Receive an IBC packet
The protocol buffer definition defines the data that an order book contains.
Add the OrderBook and Order messages to the order.proto file.
First, add the proto buffer files to build the Go code files. You can modify these files for the purpose of your app.
Create a new order.proto file in the proto/dex directory and add the content:
Modify the buy_order_book.proto file to have the fields for creating a buy order on the order book. Don't forget to add the import as well.
Tip: Don't forget to add the import as well.
Modify the sell_order_book.proto file to add the order book into the buy order book.
The proto definition for the SellOrderBook looks like:
Now, use Ignite CLI to build the proto files for the send-create-pair command:
Start enhancing the functions for the IBC packets.
Create a new file x/dex/types/order_book.go.
Add the new order book function to the corresponding Go file:
To create a new buy order book type, define NewBuyOrderBook in a new file x/dex/types/buy_order_book.go :
When an IBC packet is received on the target chain, the module must check whether a book already exists. If not, then create a buy order book for the specified denoms.
Receive an IBC Acknowledgement
When an IBC acknowledgement is received on the source chain, the module must check whether a book already exists. If not, create a sell order book for the specified denoms.
Create a new file x/dex/types/sell_order_book.go. Insert the NewSellOrderBook function which creates a new sell order book.
Modify the Acknowledgement function in the x/dex/keeper/create_pair.go file:
In this section, you implemented the logic behind the new send-create-pair command:
When an IBC packet is received on the target chain,
send-create-paircommand creates a buy order book.When an IBC acknowledgement is received on the source chain, the
send-create-paircommand creates a sell order book.
Implement the appendOrder Function to Add Orders to the Order Book
The AppendOrder function initializes and appends a new order to an order book from the order information:
Implement the checkAmountAndPrice Function For an Order
The checkAmountAndPrice function checks for the correct amount or price:
Implement the GetNextOrderID Function
The GetNextOrderID function gets the ID of the next order to append:
Implement the IncrementNextOrderID Function
The IncrementNextOrderID function updates the ID count for orders:
Implement the insertOrder Function
The insertOrder function inserts the order in the book with the provided order:
This completes the order book setup.
Now is a good time to save the state of your implementation. Because your project is in a local repository, you can use git. Saving your current state lets you jump back and forth in case you introduce errors or need a break.
In the next chapter, you learn how to deal with vouchers by minting and burning vouchers and locking and unlocking native blockchain token in your app.