@fleupold/dex-contracts
Version:
Contracts for dFusion multi-token batch auction exchange
192 lines (191 loc) • 5.92 kB
TypeScript
import { ContractEventLog } from "../../build/types/types";
import { BatchExchange } from "../contracts";
import { IndexedOrder } from "../encoding";
import { OrderbookOptions } from ".";
import { AnyEvent } from "./events";
/**
* An exchange token ID.
*/
declare type TokenId = number;
/**
* JSON representation of the account state.
*/
export interface AuctionStateJson {
tokens: string[];
accounts: {
[key: string]: {
balances: {
[key: string]: string;
};
pendingWithdrawals: {
[key: string]: {
batchId: number;
amount: string;
};
};
orders: {
buyToken: TokenId;
sellToken: TokenId;
validFrom: number;
validUntil: number | null;
priceNumerator: string;
priceDenominator: string;
remainingAmount: string;
}[];
};
};
}
/**
* Manage the exchange's auction state by incrementally applying events.
*/
export declare class AuctionState {
private readonly options;
private lastBlock;
private readonly tokens;
private readonly accounts;
private lastSolution?;
constructor(options: OrderbookOptions);
/**
* Creates a copy of the auction state that can apply events independently
* without modifying the original state.
*/
copy(): AuctionState;
/**
* Create an object representation of the current account state for JSON
* serialization.
*/
toJSON(): AuctionStateJson;
/**
* Gets the current auction state in the standard order list format.
*
* @param batch - The batch to get the orders for.
*/
getOrders(batch: number): IndexedOrder<bigint>[];
/**
* Retrieves a users effective balance for the specified token at a given
* batch.
*
* @param batch - The batch to get the balance for.
* @param user - The user account to retrieve the balance for.
* @param token - The token ID or address to retrieve the balance for.
*/
private getEffectiveBalance;
/**
* Retrieves block number the account state is accepting events for.
*/
get nextBlock(): number;
/**
* Apply all specified ordered events.
*
* @remarks
* This method expects that once events have been applied up until a certain
* block number, then no new events for that block number (or an earlier block
* number) are applied.
*/
applyEvents(events: AnyEvent<BatchExchange>[]): void;
/**
* Applies a deposit event to the auction state.
*/
private applyDeposit;
/**
* Applies an order cancellation event to the auction state.
*/
private applyOrderCancellation;
/**
* Applies an order deletion event to the auction state.
*/
private applyOrderDeletion;
/**
* Applies an order placement event to the auction state.
*
* @throws
* In strict mode, throws if the order ID from the event does not match the
* expected order ID based on the number of previously placed orders.
*/
private applyOrderPlacement;
/**
* Applies an emulated solution reversion event.
*/
private applySolutionReversion;
/**
* Applies a solution submission event to the auction state.
*
* @throws
* In strict mode, throws if the account balances are left in an invalid state
* while applying a solution. This check has to be done after applying all
* trades, as a user balance can temporarily go below 0 when a solution is
* being applied.
*/
private applySolutionSubmission;
/**
* Applies a token listing event and adds a token to the account state.
*
* @throws
* In strict mode, throws either if the token has already been listed or if
* it was listed out of order.
*/
private applyTokenListing;
/**
* Applies a trade event to the auction state.
*/
private applyTrade;
/**
* Applies a trade reversion event to the auction state.
*/
private applyTradeReversion;
/**
* Applies a withdraw event to the auction state.
*
* @throws
* In strict mode, throws if the withdrawing user's balance would be overdrawn
* as a result of this event.
*/
private applyWithdraw;
/**
* Applies a withdraw request event to the auction state.
*
* @throws
* In strict mode, throws if the withdraw request is placed over top of an
* existing unapplied request.
*/
private applyWithdrawRequest;
/**
* Normalizes a token ID or address to a token address.
*
* @throws If the token address is an invalid address or if the token id is
* not registered. This is done because event token IDs and addresses are both
* strings and can both be parsed into integers which is hard to enforce with
* just type-safety.
*/
private tokenAddr;
/**
* Gets the account data for the specified user, creating an empty one if it
* does not already exist.
*/
private account;
/**
* Updates a user's account balance.
*/
private updateBalance;
/**
* Retrieves an existing user order by user address and order ID.
*
* @throws If the order does not exist.
*/
private order;
/**
* Updates a user's order's remaining amount. For unlimited orders, the
* remaining amount remains unchanged.
*
* @throws If the order does not exist.
*/
private updateOrderRemainingAmount;
}
/**
* An error that is thrown on a unhandled contract event.
*/
export declare class UnhandledEventError extends Error {
readonly ev: ContractEventLog<unknown>;
constructor(ev: ContractEventLog<unknown>);
}
export {};