Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Module 0x7::order_book

use 0x1::option;
use 0x1::string;
use 0x5::bulk_order_types;
use 0x5::order_book_types;
use 0x5::order_match_types;
use 0x5::single_order_types;
use 0x7::bulk_order_book;
use 0x7::price_time_index;
use 0x7::single_order_book;

Enum OrderBook

enum OrderBook<M: copy, drop, store> has store
Variants
UnifiedV1
Fields
single_order_book: single_order_book::SingleOrderBook<M>
bulk_order_book: bulk_order_book::BulkOrderBook<M>
price_time_idx: price_time_index::PriceTimeIndex

Constants

const E_REINSERT_ORDER_MISMATCH: u64 = 8;

Function new_order_book

public fun new_order_book<M: copy, drop, store>(): order_book::OrderBook<M>
Implementation
public fun new_order_book<M: store + copy + drop>(): OrderBook<M> {
    OrderBook::UnifiedV1 {
        single_order_book: new_single_order_book(),
        bulk_order_book: new_bulk_order_book(),
        price_time_idx: new_price_time_idx()
    }
}

Function client_order_id_exists

public(friend) fun client_order_id_exists<M: copy, drop, store>(self: &order_book::OrderBook<M>, order_creator: address, client_order_id: string::String): bool
Implementation
public(friend) fun client_order_id_exists<M: store + copy + drop>(
    self: &OrderBook<M>, order_creator: address, client_order_id: String
): bool {
    self.single_order_book.client_order_id_exists(order_creator, client_order_id)
}

Function get_single_order_metadata

public(friend) fun get_single_order_metadata<M: copy, drop, store>(self: &order_book::OrderBook<M>, order_id: order_book_types::OrderId): option::Option<M>
Implementation
public(friend) fun get_single_order_metadata<M: store + copy + drop>(
    self: &OrderBook<M>, order_id: OrderId
): Option<M> {
    self.single_order_book.get_order_metadata(order_id)
}

Function get_order_id_by_client_id

public(friend) fun get_order_id_by_client_id<M: copy, drop, store>(self: &order_book::OrderBook<M>, order_creator: address, client_order_id: string::String): option::Option<order_book_types::OrderId>
Implementation
public(friend) fun get_order_id_by_client_id<M: store + copy + drop>(
    self: &OrderBook<M>, order_creator: address, client_order_id: String
): Option<OrderId> {
    self.single_order_book.get_order_id_by_client_id(order_creator, client_order_id)
}

Function get_single_order

public(friend) fun get_single_order<M: copy, drop, store>(self: &order_book::OrderBook<M>, order_id: order_book_types::OrderId): option::Option<single_order_types::OrderWithState<M>>
Implementation
public(friend) fun get_single_order<M: store + copy + drop>(
    self: &OrderBook<M>, order_id: OrderId
): Option<aptos_trading::single_order_types::OrderWithState<M>> {
    self.single_order_book.get_order(order_id)
}

Function get_single_remaining_size

public fun get_single_remaining_size<M: copy, drop, store>(self: &order_book::OrderBook<M>, order_id: order_book_types::OrderId): u64
Implementation
public fun get_single_remaining_size<M: store + copy + drop>(
    self: &OrderBook<M>, order_id: OrderId
): u64 {
    self.single_order_book.get_remaining_size(order_id)
}

Function cancel_single_order

public fun cancel_single_order<M: copy, drop, store>(self: &mut order_book::OrderBook<M>, order_creator: address, order_id: order_book_types::OrderId): single_order_types::SingleOrder<M>
Implementation
public fun cancel_single_order<M: store + copy + drop>(
    self: &mut OrderBook<M>, order_creator: address, order_id: OrderId
): SingleOrder<M> {
    self.single_order_book.cancel_order(
        &mut self.price_time_idx, order_creator, order_id
    )
}

Function try_cancel_single_order

public(friend) fun try_cancel_single_order<M: copy, drop, store>(self: &mut order_book::OrderBook<M>, order_creator: address, order_id: order_book_types::OrderId): option::Option<single_order_types::SingleOrder<M>>
Implementation
public(friend) fun try_cancel_single_order<M: store + copy + drop>(
    self: &mut OrderBook<M>, order_creator: address, order_id: OrderId
): Option<SingleOrder<M>> {
    self.single_order_book.try_cancel_order(
        &mut self.price_time_idx, order_creator, order_id
    )
}

Function try_cancel_single_order_with_client_order_id

public(friend) fun try_cancel_single_order_with_client_order_id<M: copy, drop, store>(self: &mut order_book::OrderBook<M>, order_creator: address, client_order_id: string::String): option::Option<single_order_types::SingleOrder<M>>
Implementation
public(friend) fun try_cancel_single_order_with_client_order_id<M: store + copy + drop>(
    self: &mut OrderBook<M>, order_creator: address, client_order_id: String
): Option<SingleOrder<M>> {
    self.single_order_book.try_cancel_order_with_client_order_id(
        &mut self.price_time_idx, order_creator, client_order_id
    )
}

Function place_maker_order

public fun place_maker_order<M: copy, drop, store>(self: &mut order_book::OrderBook<M>, order_req: single_order_types::SingleOrderRequest<M>)
Implementation
public fun place_maker_order<M: store + copy + drop>(
    self: &mut OrderBook<M>, order_req: SingleOrderRequest<M>
) {
    self.single_order_book.place_maker_or_pending_order(
        &mut self.price_time_idx, order_req
    );
}

Function decrease_single_order_size

public(friend) fun decrease_single_order_size<M: copy, drop, store>(self: &mut order_book::OrderBook<M>, order_creator: address, order_id: order_book_types::OrderId, size_delta: u64)
Implementation
public(friend) fun decrease_single_order_size<M: store + copy + drop>(
    self: &mut OrderBook<M>,
    order_creator: address,
    order_id: OrderId,
    size_delta: u64
) {
    self.single_order_book.decrease_order_size(
        &mut self.price_time_idx,
        order_creator,
        order_id,
        size_delta
    )
}

Function set_single_order_metadata

public(friend) fun set_single_order_metadata<M: copy, drop, store>(self: &mut order_book::OrderBook<M>, order_id: order_book_types::OrderId, metadata: M)
Implementation
public(friend) fun set_single_order_metadata<M: store + copy + drop>(
    self: &mut OrderBook<M>, order_id: OrderId, metadata: M
) {
    self.single_order_book.set_order_metadata(order_id, metadata)
}

Function take_ready_price_based_orders

public(friend) fun take_ready_price_based_orders<M: copy, drop, store>(self: &mut order_book::OrderBook<M>, oracle_price: u64, order_limit: u64): vector<single_order_types::SingleOrder<M>>
Implementation
public(friend) fun take_ready_price_based_orders<M: store + copy + drop>(
    self: &mut OrderBook<M>, oracle_price: u64, order_limit: u64
): vector<SingleOrder<M>> {
    self.single_order_book.take_ready_price_based_orders(oracle_price, order_limit)
}

Function take_ready_time_based_orders

public(friend) fun take_ready_time_based_orders<M: copy, drop, store>(self: &mut order_book::OrderBook<M>, order_limit: u64): vector<single_order_types::SingleOrder<M>>
Implementation
public(friend) fun take_ready_time_based_orders<M: store + copy + drop>(
    self: &mut OrderBook<M>, order_limit: u64
): vector<SingleOrder<M>> {
    self.single_order_book.take_ready_time_based_orders(order_limit)
}

Function best_bid_price

public(friend) fun best_bid_price<M: copy, drop, store>(self: &order_book::OrderBook<M>): option::Option<u64>
Implementation
public(friend) fun best_bid_price<M: store + copy + drop>(
    self: &OrderBook<M>
): Option<u64> {
    self.price_time_idx.best_bid_price()
}

Function best_ask_price

public(friend) fun best_ask_price<M: copy, drop, store>(self: &order_book::OrderBook<M>): option::Option<u64>
Implementation
public(friend) fun best_ask_price<M: store + copy + drop>(
    self: &OrderBook<M>
): Option<u64> {
    self.price_time_idx.best_ask_price()
}

Function get_slippage_price

public fun get_slippage_price<M: copy, drop, store>(self: &order_book::OrderBook<M>, is_bid: bool, slippage_bps: u64): option::Option<u64>
Implementation
public fun get_slippage_price<M: store + copy + drop>(
    self: &OrderBook<M>, is_bid: bool, slippage_bps: u64
): Option<u64> {
    self.price_time_idx.get_slippage_price(is_bid, slippage_bps)
}

Function is_taker_order

Checks if the order is a taker order i.e., matched immediately with the active order book.

public fun is_taker_order<M: copy, drop, store>(self: &order_book::OrderBook<M>, price: u64, is_bid: bool, trigger_condition: option::Option<order_book_types::TriggerCondition>): bool
Implementation
public fun is_taker_order<M: store + copy + drop>(
    self: &OrderBook<M>,
    price: u64,
    is_bid: bool,
    trigger_condition: Option<TriggerCondition>
): bool {
    if (trigger_condition.is_some()) {
        return false;
    };
    self.price_time_idx.is_taker_order(price, is_bid)
}

Function get_single_match_for_taker

public fun get_single_match_for_taker<M: copy, drop, store>(self: &mut order_book::OrderBook<M>, price: u64, size: u64, is_bid: bool): order_match_types::OrderMatch<M>
Implementation
public fun get_single_match_for_taker<M: store + copy + drop>(
    self: &mut OrderBook<M>,
    price: u64,
    size: u64,
    is_bid: bool
): OrderMatch<M> {
    let result = self.price_time_idx.get_single_match_result(price, size, is_bid);
    if (result.is_active_matched_book_type_single_order()) {
        self.single_order_book.get_single_match_for_taker(result)
    } else {
        self.bulk_order_book.get_single_match_for_taker(
            &mut self.price_time_idx, result, is_bid
        )
    }
}

Function reinsert_order

public(friend) fun reinsert_order<M: copy, drop, store>(self: &mut order_book::OrderBook<M>, reinsert_order: order_match_types::OrderMatchDetails<M>, original_order: &order_match_types::OrderMatchDetails<M>)
Implementation
public(friend) fun reinsert_order<M: store + copy + drop>(
    self: &mut OrderBook<M>,
    reinsert_order: OrderMatchDetails<M>,
    original_order: &OrderMatchDetails<M>
) {
    if (reinsert_order.is_single_order_from_match_details()) {
        self.single_order_book.reinsert_order(
            &mut self.price_time_idx, reinsert_order, original_order
        )
    } else {
        self.bulk_order_book.reinsert_order(
            &mut self.price_time_idx, reinsert_order, original_order
        );
    }
}

Function get_bulk_order_remaining_size

public(friend) fun get_bulk_order_remaining_size<M: copy, drop, store>(self: &order_book::OrderBook<M>, order_creator: address, is_bid: bool): u64
Implementation
public(friend) fun get_bulk_order_remaining_size<M: store + copy + drop>(
    self: &OrderBook<M>, order_creator: address, is_bid: bool
): u64 {
    self.bulk_order_book.get_remaining_size(order_creator, is_bid)
}

Function place_bulk_order

public(friend) fun place_bulk_order<M: copy, drop, store>(self: &mut order_book::OrderBook<M>, order_req: bulk_order_types::BulkOrderRequest<M>): bulk_order_types::BulkOrderPlaceResponse<M>
Implementation
public(friend) fun place_bulk_order<M: store + copy + drop>(
    self: &mut OrderBook<M>, order_req: BulkOrderRequest<M>
): BulkOrderPlaceResponse<M> {
    self.bulk_order_book.place_bulk_order(&mut self.price_time_idx, order_req)
}

Function get_bulk_order

public(friend) fun get_bulk_order<M: copy, drop, store>(self: &order_book::OrderBook<M>, order_creator: address): bulk_order_types::BulkOrder<M>
Implementation
public(friend) fun get_bulk_order<M: store + copy + drop>(
    self: &OrderBook<M>, order_creator: address
): BulkOrder<M> {
    self.bulk_order_book.get_bulk_order(order_creator)
}

Function cancel_bulk_order

public(friend) fun cancel_bulk_order<M: copy, drop, store>(self: &mut order_book::OrderBook<M>, order_creator: address): bulk_order_types::BulkOrder<M>
Implementation
public(friend) fun cancel_bulk_order<M: store + copy + drop>(
    self: &mut OrderBook<M>, order_creator: address
): BulkOrder<M> {
    self.bulk_order_book.cancel_bulk_order(&mut self.price_time_idx, order_creator)
}

Function cancel_bulk_order_at_price

public(friend) fun cancel_bulk_order_at_price<M: copy, drop, store>(self: &mut order_book::OrderBook<M>, order_creator: address, price: u64, is_bid: bool): (u64, bulk_order_types::BulkOrder<M>)
Implementation
public(friend) fun cancel_bulk_order_at_price<M: store + copy + drop>(
    self: &mut OrderBook<M>,
    order_creator: address,
    price: u64,
    is_bid: bool
): (u64, BulkOrder<M>) {
    self.bulk_order_book.cancel_bulk_order_at_price(
        &mut self.price_time_idx,
        order_creator,
        price,
        is_bid
    )
}