UNPKG

@fleupold/dex-contracts

Version:

Contracts for dFusion multi-token batch auction exchange

152 lines (151 loc) 5.72 kB
/** * Module containing event based orderbook reading implementation. The entire * orderbook is contructed from Ethreum events fetched in two stages: first, * the past events are fetched in block-based pages, second an event subsciption * is setup to listen to new incomming events that get added to the orderbook * snapshot. * * The streamed orderbook can be queried at any time to get the current open * orders (that is orders and balances as it would they would be included for * the next batch) or the finalized orderbook (orders and balances that are * being considered for trading by the solver). * * @packageDocumentation */ import type Web3 from "web3"; import type { TransactionReceipt } from "web3-core"; import type { BaseContract } from "../../build/types/types"; import { ContractArtifact } from "../contracts"; import { IndexedOrder } from "../encoding"; /** * Configuration options for the streamed orderbook. */ export interface OrderbookOptions { /** * Optinally specify the last block to apply events for. This is useful for * testing the streamed orderbook and ensuring that it is producing a correct * account state for a known block. */ endBlock?: number; /** * Set the block page size used to query past events. * * @remarks * Nodes are usually configured to limit the number of events that can be * returned by a single call to retrieve past logs (10,000 for Infura) * so this parameter should be set accordingly. */ blockPageSize: number; /** * Sets the number of block confirmations required for an event to be * considered confirmed and not be subject to re-orgs. */ blockConfirmations: number; /** * Enable strict checking that performs additional integrity checks. * * @remarks * The additional integrity checks have a non-negligible runtime cost so * they are disabled by default, but can help diagnose issues and bugs in the * streamed orderbook. */ strict: boolean; /** * Set logging function for debug messages used by the streamed orderbook * module. */ debug: (msg: string) => void; } /** * The default orderbook options. */ export declare const DEFAULT_ORDERBOOK_OPTIONS: OrderbookOptions; /** * The streamed orderbook that manages incoming events, and applies them to the * account state. */ export declare class StreamedOrderbook { private readonly web3; private readonly contract; private readonly startBlock; private readonly options; private batch; private readonly confirmedState; private latestState?; private invalidState?; private constructor(); /** * Create and return a new streamed orderbook. * * @remarks * This method returns a promise that resolves once all past events have been * applied to the current account state and the orderbook. * * @param web3 - The web3 provider to use. * @param options - Optional settings for tweaking the streamed orderbook. */ static init(web3: Web3, options?: Partial<OrderbookOptions>): Promise<StreamedOrderbook>; /** * Retrieves the current open orders in the orderbook. */ getOpenOrders(): IndexedOrder<bigint>[]; /** * Apply all past events to the account state by querying the node for past * events with multiple queries to retrieve each block page at a time. */ private applyPastEvents; /** * Apply new confirmed events to the account state and store the remaining * events that are subject to reorgs into the `pendingEvents` array. * * @param toBlock - Optional block number until which to fetch events, capped at latest block number. * @returns The block number up until which the streamed orderbook is up to * date * * @remarks * If there is an error retrieving the latest events from the node, then the * account state remains unmodified. This allows the updating orderbook to be * more fault-tolerant and deal with nodes being temporarily down and some * intermittent errors. However, if an error applying confirmed events occur, * then the streamed orderbook becomes invalid and can no longer apply new * events as the actual auction state is unknown. */ update(toBlock?: number): Promise<number>; /** * Retrieves past events for the contract. */ private getPastEvents; /** * Retrieves the batch ID at a given block number. * * @remarks * The batch ID is locally calculated from the block header timestamp as it is * more reliable than executing an `eth_call` to calculate the batch ID on the * EVM since an archive node is required for sufficiently old blocks. */ private getBatchId; /** * Helper method to check for an unrecoverable invalid state in the current * streamed orderbook. * * @throws If the streamed orderbook is in an invalid state. */ private throwOnInvalidState; } /** * An error that is thrown on when the auction state is invalid and can no * longer be updated. */ export declare class InvalidAuctionStateError extends Error { readonly block: number; readonly inner: Error; constructor(block: number, inner: Error); } /** * Get a contract deployment, returning both the web3 contract object as well as * the transaction receipt for the contract deployment. * * @throws If the contract is not deployed on the network the web3 provider is * connected to. */ export declare function deployment<C extends BaseContract>(web3: Web3, { abi, networks }: ContractArtifact): Promise<[C, TransactionReceipt]>;