UNPKG

@bancor/carbon-sdk

Version:

The SDK is a READ-ONLY tool, intended to facilitate working with Carbon contracts. It's a convenient wrapper around our matching algorithm, allowing programs and users get a ready to use transaction data that will allow them to manage strategies and fulfi

105 lines 5.19 kB
import { CacheEvents, TypedEventEmitter } from './types'; import { BlockMetadata, EncodedStrategy, OrdersMap, TokenPair, SyncedEvents } from '../common/types'; import { BigIntish } from '../utils/numerics'; declare const ChainCache_base: new () => TypedEventEmitter<CacheEvents>; export declare class ChainCache extends ChainCache_base { private _strategiesByPair; private _strategiesById; private _ordersByDirectedPair; private _latestBlockNumber; private _blocksMetadata; private _tradingFeePPMByPair; private _isCacheInitialized; private _handleCacheMiss; static fromSerialized(serializedCache: string): ChainCache; private _deserialize; serialize(): string; setCacheMissHandler(handler: (token0: string, token1: string) => Promise<void>): void; private _checkAndHandleCacheMiss; /** * Returns true if the cache is initialized with data for the first time by `bulkAddPairs` or when loaded from serialized data * @returns {boolean} true if the cache is initialized, false otherwise */ isCacheInitialized(): boolean; clear(): void; getStrategiesByPair(token0: string, token1: string): Promise<EncodedStrategy[] | undefined>; getStrategiesByPairs(pairs: TokenPair[]): Promise<{ pair: TokenPair; strategies: EncodedStrategy[]; }[]>; getStrategyById(id: BigIntish): EncodedStrategy | undefined; getCachedPairs(onlyWithStrategies?: boolean): TokenPair[]; /** * returns the orders that sell targetToken for sourceToken */ getOrdersByPair(sourceToken: string, targetToken: string, keepNonTradable?: boolean): Promise<OrdersMap>; hasCachedPair(token0: string, token1: string): boolean; getLatestBlockNumber(): number; getTradingFeePPMByPair(token0: string, token1: string): Promise<number | undefined>; get blocksMetadata(): BlockMetadata[]; set blocksMetadata(blocks: BlockMetadata[]); private _addPair; /** * This method is to be used when all the existing strategies of a pair are * fetched and are to be stored in the cache. * Once a pair is cached, the only way to update it is by using `applyEvents`. * If all the strategies of a pair are deleted, the pair remains in the cache and there's * no need to add it again. * It emits an event `onPairAddedToCache` with the pair info. * @param {string} token0 - address of the first token of the pair * @param {string} token1 - address of the second token of the pair * @param {EncodedStrategy[]} strategies - the strategies to be cached * @throws {Error} if the pair is already cached * @returns {void} * @emits {onPairAddedToCache} - when the pair is added to the cache */ addPair(token0: string, token1: string, strategies: EncodedStrategy[]): void; /** * This method is used when a number of pairs are fetched and are to be stored in the cache. * If this is the first time it is called with a non empty list of pairs it emits an event * to let know that the cache was initialized with data for the first time. * * @param {Array<{pair: TokenPair, strategies: EncodedStrategy[]}>} pairs - the pairs to add to the cache * @emits {onCacheInitialized} - when the cache is initialized with data for the first time * @throws {Error} if any pair is already cached * @returns {void} */ bulkAddPairs(pairs: { pair: TokenPair; strategies: EncodedStrategy[]; }[]): void; /** * This methods allows setting the trading fee of a pair. * Note that fees can also be updated via `applyEvents`. * This specific method is useful when the fees were fetched from the chain * as part of initialization or some other operation mode which doesn't * rely on even processing * * @param {string} token0 - address of the first token of the pair * @param {string} token1 - address of the second token of the pair * @param tradingFeePPM - the pair's trading fee */ addPairFees(token0: string, token1: string, tradingFeePPM: number): void; /** * This method is to be used when events from a range of blocks are fetched * and are to be applied to the cache. * All the events should belong to pairs that are already cached. * The way to use this work flow is to first call `getLatestBlockNumber` to * get the latest block number that was already cached, then fetch all the * events from that block number to the latest block number, and finally * call this method with the fetched events. The events should be sorted by * block number and log index. * Note: the cache can handle a case of a strategy that was created and then updated and then deleted * @param events - Array of events to apply * @param currentBlock - Current block number */ applyEvents(events: SyncedEvents, currentBlock: number): void; private _setLatestBlockNumber; private _addStrategyOrders; private _removeStrategyOrders; private _addStrategy; private _updateStrategy; private _deleteStrategy; } export {}; //# sourceMappingURL=ChainCache.d.ts.map