xud
Version:
Exchange Union Daemon
131 lines (130 loc) • 6.37 kB
TypeScript
/// <reference types="node" />
import { EventEmitter } from 'events';
import FastPriorityQueue from 'fastpriorityqueue';
import { OrderingDirection } from '../constants/enums';
import Logger from '../Logger';
import { MatchingResult, Order, OrderPortion, OwnOrder, PeerOrder } from './types';
/** A map between orders and their order ids. */
declare type OrderMap<T extends Order> = Map<string, T>;
declare type OrderSidesMaps<T extends Order> = {
buyMap: OrderMap<T>;
sellMap: OrderMap<T>;
};
declare type OrderSidesArrays<T extends Order> = {
buyArray: T[];
sellArray: T[];
};
declare type OrderSidesQueues = {
buyQueue: FastPriorityQueue<Order>;
sellQueue: FastPriorityQueue<Order>;
};
interface TradingPair {
/** Adds a listener to be called when all or part of a remote order was removed due to not meeting dust minimum. */
on(event: 'peerOrder.dust', listener: (order: OrderPortion) => void): this;
/** Adds a listener to be called when all or part of a local order was removed due to not meeting dust minimum. */
on(event: 'ownOrder.dust', listener: (order: OrderPortion) => void): this;
/** Notifies listeners that a remote order was removed due to not meeting dust minimum. */
emit(event: 'peerOrder.dust', order: OrderPortion): boolean;
/** Notifies listeners that a local order was removed due to not meeting dust minimum. */
emit(event: 'ownOrder.dust', order: OrderPortion): boolean;
}
/**
* Represents a single trading pair in the order book. Responsible for managing all active orders
* and for matching orders according to their price and quantity.
*/
declare class TradingPair extends EventEmitter {
private logger;
pairId: string;
private nomatching;
/** A pair of priority queues for the buy and sell sides of this trading pair */
queues?: OrderSidesQueues;
/** A pair of maps between active own orders ids and orders for the buy and sell sides of this trading pair. */
ownOrders: OrderSidesMaps<OwnOrder>;
/** A map between peerPubKey and a pair of maps between active peer orders ids and orders for the buy and sell sides of this trading pair. */
peersOrders: Map<string, OrderSidesMaps<PeerOrder>>;
/** The minimum quantity for both sides of a trade that is considered swappable and not dust. */
static QUANTITY_DUST_LIMIT: number;
constructor(logger: Logger, pairId: string, nomatching?: boolean);
private static createPriorityQueue;
static getOrdersPriorityQueueComparator: (orderingDirection: OrderingDirection) => (a: Order, b: Order) => boolean;
/**
* Gets the quantity that can be matched between two orders.
* @returns the smaller of the quantity between the two orders if their price matches, 0 otherwise
*/
private static getMatchingQuantity;
/**
* Splits an order by quantity into a matched portion and subtracts the matched quantity from the original order.
* @param order the order that is being split
* @param matchingQuantity the quantity for the split order and to subtract from the original order
* @returns the split portion of the order with the matching quantity
*/
private static splitOrderByQuantity;
/**
* Adds a peer order for this trading pair.
* @returns `true` if the order was added, `false` if it could not be added because there
* already exists an order with the same order id
*/
addPeerOrder: (order: PeerOrder) => boolean;
/**
* Adds an own order for this trading pair.
* @returns `true` if the order was added, `false` if it could not be added because there
* already exists an order with the same order id
*/
addOwnOrder: (order: OwnOrder) => boolean;
/**
* Attempts to add an order for this trading pair.
* @returns `true` if the order was added, `false` if it could not be added because there
* already exists an order with the same order id
*/
private addOrder;
/**
* Removes all of a peer's orders.
* @param peerPubKey the node pub key of the peer
*/
removePeerOrders: (peerPubKey?: string | undefined) => PeerOrder[];
/**
* Removes all or part of a peer order.
* @param quantityToRemove the quantity to remove, if undefined or if greater than or equal to the available
* quantity then the entire order is removed
* @returns the portion of the order that was removed, and a flag indicating whether the entire order was removed
*/
removePeerOrder: (orderId: string, peerPubKey?: string | undefined, quantityToRemove?: number | undefined) => {
order: PeerOrder;
fullyRemoved: boolean;
};
/**
* Removes all or part of an own order.
* @param quantityToRemove the quantity to remove, if undefined or if greater than or equal to the available
* quantity then the entire order is removed
* @returns the portion of the order that was removed, and a flag indicating whether the entire order was removed
*/
removeOwnOrder: (orderId: string, quantityToRemove?: number | undefined) => {
order: OwnOrder;
fullyRemoved: boolean;
};
/**
* Removes all or part of an order.
* @param quantityToRemove the quantity to remove, if undefined or if greater than or equal to the available
* quantity then the entire order is removed
* @returns the portion of the order that was removed, and a flag indicating whether the entire order was removed
*/
private removeOrder;
private getOrderMap;
private getOrders;
getPeersOrders: () => OrderSidesArrays<PeerOrder>;
getOwnOrders: () => OrderSidesArrays<OwnOrder>;
getOwnOrder: (orderId: string) => OwnOrder;
getPeerOrder: (orderId: string, peerPubKey: string) => PeerOrder;
private getOrder;
addOrderHold: (orderId: string, holdAmount?: number | undefined) => void;
removeOrderHold: (orderId: string, holdAmount?: number | undefined) => void;
quoteBid: () => number;
quoteAsk: () => number;
/**
* Matches an order against its opposite queue. Matched maker orders are removed immediately.
* @returns a [[MatchingResult]] with the matches as well as the remaining, unmatched portion of the order
*/
match: (takerOrder: OwnOrder) => MatchingResult;
}
export default TradingPair;
export { OrderSidesArrays };