diff --git a/README.md b/README.md index 76bcd23..be87f39 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ let sdkCache: ChainCache; let carbonSDK: Toolkit; let isInitialized = false; let isInitializing = false; +const MAX_BLOCK_AGE = 2000; // past this many blocks, the SDK won't attempt to catch up by processing events and instead call the contracts for strategy info. const init = async ( rpcUrl: string, @@ -56,7 +57,7 @@ const init = async ( 1 ); api = new ContractsApi(provider, config); - const { cache, startDataSync } = initSyncedCache(api.reader, cachedData); + const { cache, startDataSync } = initSyncedCache(api.reader, cachedData, MAX_BLOCK_AGE); sdkCache = cache; carbonSDK = new Toolkit( api, @@ -80,22 +81,23 @@ const init = async ( ## Notes ### 1. The SDK Logger supports 3 verbosity levels: - - `0` (default) only prints errors and important logs. - - `1` (debug) prints highly verbose logs. - - `2` (debug readable) is same as `1` but also converts any BigNumber to an easy to read string (impacting performance). - - To use it in Node, set the environment variable `CARBON_DEFI_SDK_VERBOSITY` to the desired level. - To use it from a browser app do, before importing the SDK: - ```js - window.CARBON_DEFI_SDK_VERBOSITY = 2; - ``` +- `0` (default) only prints errors and important logs. +- `1` (debug) prints highly verbose logs. +- `2` (debug readable) is same as `1` but also converts any BigNumber to an easy to read string (impacting performance). + +To use it in Node, set the environment variable `CARBON_DEFI_SDK_VERBOSITY` to the desired level. +To use it from a browser app do, before importing the SDK: + +```js +window.CARBON_DEFI_SDK_VERBOSITY = 2; +``` ### 2. For usage with contracts version < 5, without the enhanced range for trade by source: - ```js - window.LEGACY_TRADE_BY_SOURCE_RANGE = true; - ``` +```js +window.LEGACY_TRADE_BY_SOURCE_RANGE = true; +``` ## Authors diff --git a/package.json b/package.json index 952a4b4..11ec85b 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "@bancor/carbon-sdk", "type": "module", "source": "src/index.ts", - "version": "0.0.97-DEV", + "version": "0.0.98-DEV", "description": "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 fulfill trades", "main": "dist/index.cjs", "module": "dist/index.js", diff --git a/src/chain-cache/ChainCache.ts b/src/chain-cache/ChainCache.ts index 85cdc6a..ae1cd70 100644 --- a/src/chain-cache/ChainCache.ts +++ b/src/chain-cache/ChainCache.ts @@ -174,7 +174,7 @@ export class ChainCache extends (EventEmitter as new () => TypedEventEmitter TypedEventEmitter { @@ -35,10 +37,23 @@ export class ChainSync { } this._syncCalled = true; const blockNumber = await this._fetcher.getBlockNumber(); - if (this._chainCache.getLatestBlockNumber() === 0) { + const latestBlockInCache = this._chainCache.getLatestBlockNumber(); + + if (latestBlockInCache === 0) { logger.debug('startDataSync - cache is new', arguments); // cache starts from scratch so we want to avoid getting events from the beginning of time this._chainCache.applyBatchedUpdates(blockNumber, [], [], [], [], []); + } else if ( + this._maxBlockAge !== undefined && + blockNumber - latestBlockInCache > this._maxBlockAge + ) { + logger.debug( + `startDataSync - cache is too old: current block ${blockNumber}, cache block ${latestBlockInCache}`, + arguments + ); + // cache is too old so we want to clear it and avoid getting events from the beginning of time + this._chainCache.clear(true); + this._chainCache.applyBatchedUpdates(blockNumber, [], [], [], [], []); } // let's fetch all pairs from the chain and set them to the cache - to be used by the following syncs diff --git a/src/chain-cache/index.ts b/src/chain-cache/index.ts index 5b624bf..648fe02 100644 --- a/src/chain-cache/index.ts +++ b/src/chain-cache/index.ts @@ -19,7 +19,8 @@ export * from './types'; */ export const initSyncedCache = ( fetcher: Fetcher, - cachedData?: string + cachedData?: string, + maxBlockAge?: number ): { cache: ChainCache; startDataSync: () => Promise } => { let cache: ChainCache | undefined; if (cachedData) { @@ -30,7 +31,7 @@ export const initSyncedCache = ( cache = new ChainCache(); } - const syncer = new ChainSync(fetcher, cache); + const syncer = new ChainSync(fetcher, cache, maxBlockAge); cache.setCacheMissHandler(syncer.syncPairData.bind(syncer)); return { cache, startDataSync: syncer.startDataSync.bind(syncer) }; };