Skip to content

Commit

Permalink
Merge pull request #141 from bancorprotocol/support-max-block-age
Browse files Browse the repository at this point in the history
user can specify maxBlockAge to force skipping logs
  • Loading branch information
zavelevsky authored May 31, 2024
2 parents 5ab9616 + b9dbd6b commit aa92762
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 20 deletions.
28 changes: 15 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 4 additions & 2 deletions src/chain-cache/ChainCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export class ChainCache extends (EventEmitter as new () => TypedEventEmitter<Cac
logger.debug('Cache miss for pair', token0, token1, 'resolved');
}

public clear(): void {
public clear(silent: boolean = false): void {
const pairs = Object.keys(this._strategiesByPair).map(fromPairKey);
this._strategiesByPair = {};
this._strategiesById = {};
Expand All @@ -183,7 +183,9 @@ export class ChainCache extends (EventEmitter as new () => TypedEventEmitter<Cac
this._latestTradesByPair = {};
this._latestTradesByDirectedPair = {};
this._blocksMetadata = [];
this.emit('onPairDataChanged', pairs);
if (!silent) {
this.emit('onPairDataChanged', pairs);
}
}

//#region public getters
Expand Down
19 changes: 17 additions & 2 deletions src/chain-cache/ChainSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ export class ChainSync {
// keep the time stamp of last fetch
private _lastFetch: number = Date.now();
private _initialSyncDone: boolean = false;
private _maxBlockAge?: number;

constructor(fetcher: Fetcher, chainCache: ChainCache) {
constructor(fetcher: Fetcher, chainCache: ChainCache, maxBlockAge?: number) {
this._fetcher = fetcher;
this._chainCache = chainCache;
this._maxBlockAge = maxBlockAge;
}

public async startDataSync(): Promise<void> {
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/chain-cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export * from './types';
*/
export const initSyncedCache = (
fetcher: Fetcher,
cachedData?: string
cachedData?: string,
maxBlockAge?: number
): { cache: ChainCache; startDataSync: () => Promise<void> } => {
let cache: ChainCache | undefined;
if (cachedData) {
Expand All @@ -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) };
};

0 comments on commit aa92762

Please sign in to comment.