package
0.0.0-20190806135343-59eeb304638e
Repository: https://github.com/iov-one/tutorial.git
Documentation: pkg.go.dev

# README

Orderbook module


Requirements

This module defines the required components for orderbook. Orderbook works by matching the ask and bid orders.

State

  • Order state

    • Open: means order is still pending
    • Done: means order has been executed
    • Cancel: means order has been cancelled
  • Side

    • Ask: defines trader wants to buy
    • Bid defines trader wants to sell
  • Order

    • ID
    • Trader: identity of trader
    • OrderBookID: orderbook id of this order belongs to
    • Side: side of this order
    • OrderState: defines if order is done, cancelled, or open
    • OriginalOffer: amount of offer at the time of creation
    • RemainingOffer: amount of pending trade
    • Price: price of offer
    • TradeIDs: trades that have been executed
    • CreatedAt: creation time of offer
    • UpdatedAt: update time of offer. Updated whenever order state changes
  • Trade

    • ID
    • OrderBookID: ID of the orderbook trade happened at
    • OrderID: ID of order this trade is originated
    • Taker: identity of taker that accepted to buy
    • Maker: identity of maker that accepted to sell
    • MakerPaid: amount maker paid to settle the trade
    • TakerPaid: amount taker paid to settle the trade
    • ExecutedAt: defines the trade execution time
  • Order book

    • ID
    • MarketID: market this orderbook belongs to
    • AskTicker: Ticker of ask side
    • BidTicker: Ticker of bid side
    • TotalAskCount: number of available ask orders
    • TotalBidCount: number of available bid orders
  • Market

    • ID
    • Owner: identity of owner of this market
    • Name: name of the market

Messages

  • Post order

    • MarketID: market that order is posted to
    • AskTicker: Ticker of ask side
    • BidTicker: Ticker of bid side
  • Cancel order

    • OrderID: Order that wanted to be cancelled

Order and Trade relation

Trade is full/partial offer that happened between traders

Market and Orderbook relation

The blockchain may have multiple markets. Each market may have multiple orderbooks. Each token pair can only have one orderbook per market. There is no global chain owner, but each market has one that adds orderbooks and sets fees. This could be one person, a multisig, or a governance contract (Dao)

When adding an orderbook, market with given market id is checked, then look up the owner for that market. Rather than having one owner that can create orderbooks for all markets, each market stores who can update it.

Matching engine logic


First orders are sorted in ascending or descending order. Ask orders are sorted descendently so that the element with the highest index in the array has the lowest price and buy orders are sorted in ascending order so that the last element of the array has the highest price. When an order is posted it is inserted into one of the ask order or bid order arrays.

Strategies

  • Best price offer strategy
    • When a matching order by price and amount is found order is placed instantly.
  • Partially filled order
    • If an order cannot be fulfilled entirely in one transaction, the remaining lots become a “resting order” which is included in the order book. Resting orders are prioritised and fulfilled when matching orders are received.
  • No match
    • Recieved order becomes an resting order for future trades.
  • Multiple orders with same price
    • There are multiple ways to achieve this. This section is open for discussion

# Functions

BuildMarketIDTickersIndex indexByteSize = 8(MarketID) + ask ticker size + bid ticker size.
BuildOpenOrderIndex produces a compound index like: (OrderBookID, Side, Price) WHERE order.OrderState = Open Stored as - 8 bytes bigendian OrderBookID, 1 byte Side, 8 byte bigendian Price.Whole, 8 byte bigendian Price.Fractional We use Price.Lexographic() to produce a lexographic ordering, such than A.Lexographic() < B.Lexographic == A < B This is a very nice trick to get clean range queries over sensible value ranges in a key-value store.
BuildOpenOrderIndex produces 8 bytes OrderBookID || big-endian ExecutedAt This allows lexographical searches over the time ranges (or earliest or latest) of all trades within one orderbook.
NewAmount takes whole and fractional, returns Amount struct.
NewAmountp takes whole and fractional, returns Amount struct pointer.
No description provided by the author
NewOrderBookBucket initates orderbook with required indexes/ TODO remove marketIDindexer if proven unnecessary.
NewOrderBookHandler creates a handler that allows issuer to create orderbooks.
No description provided by the author
No description provided by the author
RegisterQuery registers exchange buckets for querying.
RegisterRoutes registers handlers for orderbook message processing.

# Constants

Cancelled orders were closed at the makers request before they were fulfilled.
Done orders have been fulfilled and are empty.
No description provided by the author
Open orders can create trades.
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

Amount is like a coin.Coin but without a ticker.
CancelOrderMsg will remove a standing order.
CreateOrderBookMsg creates a new orderbook in an existing market.
CreateOrderMsg will offer to sell some currency on an orderbook at a given price.
A market holds many Orderbooks and is just a grouping for now.
No description provided by the author
Order is a request to make a trade.
An Orderbook lives in a market and represents a ask/bid pair.
No description provided by the author
OrderBookHandler will handle creating orderbooks.
No description provided by the author
Trade is a settled partial/full order We store these as independent entities to help with queries to map the prices over time.
No description provided by the author

# Type aliases

OrderState defines if the proposal is active.
Side determines which side of the orderbook we are on (ask or bid) This defines the appropriate ticker (ask_ticker or bid_ticker).