@@ -14,6 +14,8 @@ import {
1414 RetypeBigNumberToString ,
1515 TokenPair ,
1616 TradeData ,
17+ TradingFeeUpdate ,
18+ SyncedEvents ,
1719} from '../common/types' ;
1820import { BigNumberish } from '../utils/numerics' ;
1921import {
@@ -23,6 +25,7 @@ import {
2325 encodedStrategyStrToBN ,
2426} from '../utils/serializers' ;
2527import { Logger } from '../common/logger' ;
28+
2629const logger = new Logger ( 'ChainCache.ts' ) ;
2730
2831const schemeVersion = 6 ; // bump this when the serialization format changes
@@ -299,7 +302,7 @@ export class ChainCache extends (EventEmitter as new () => TypedEventEmitter<Cac
299302 /**
300303 * This method is to be used when all the existing strategies of a pair are
301304 * fetched and are to be stored in the cache.
302- * Once a pair is cached, the only way to update it is by using `applyBatchedUpdates `.
305+ * Once a pair is cached, the only way to update it is by using `applyEvents `.
303306 * If all the strategies of a pair are deleted, the pair remains in the cache and there's
304307 * no need to add it again.
305308 * @param {string } token0 - address of the first token of the pair
@@ -338,7 +341,7 @@ export class ChainCache extends (EventEmitter as new () => TypedEventEmitter<Cac
338341
339342 /**
340343 * This methods allows setting the trading fee of a pair.
341- * Note that fees can also be updated via `applyBatchedUpdates `.
344+ * Note that fees can also be updated via `applyEvents `.
342345 * This specific method is useful when the fees were fetched from the chain
343346 * as part of initialization or some other operation mode which doesn't
344347 * rely on even processing
@@ -370,53 +373,55 @@ export class ChainCache extends (EventEmitter as new () => TypedEventEmitter<Cac
370373 * The way to use this work flow is to first call `getLatestBlockNumber` to
371374 * get the latest block number that was already cached, then fetch all the
372375 * events from that block number to the latest block number, and finally
373- * call this method with the fetched events.
376+ * call this method with the fetched events. The events should be sorted by
377+ * block number and log index.
374378 * Note: the cache can handle a case of a strategy that was created and then updated and then deleted
375- * @param {number } latestBlockNumber - the latest block number that was fetched
376- * @param {TradeData[] } latestTrades - the trades that were conducted
377- * @param {EncodedStrategy[] } createdStrategies - the strategies that were created
378- * @param {EncodedStrategy[] } updatedStrategies - the strategies that were updated
379- * @param {EncodedStrategy[] } deletedStrategies - the strategies that were deleted
380- * @throws {Error } if the pair of a strategy is not cached
381- * @returns {void }
379+ * @param events - Array of events to apply
380+ * @param currentBlock - Current block number
382381 */
383- public applyBatchedUpdates (
384- latestBlockNumber : number ,
385- latestFeeUpdates : [ string , string , number ] [ ] ,
386- latestTrades : TradeData [ ] ,
387- createdStrategies : EncodedStrategy [ ] ,
388- updatedStrategies : EncodedStrategy [ ] ,
389- deletedStrategies : EncodedStrategy [ ]
390- ) : void {
391- logger . debug ( 'Applying batched updates to cache' , {
392- latestBlockNumber,
393- latestFeeUpdates,
394- latestTrades,
395- createdStrategies,
396- updatedStrategies,
397- deletedStrategies,
398- } ) ;
382+ public applyEvents ( events : SyncedEvents , currentBlock : number ) : void {
399383 const affectedPairs = new Set < string > ( ) ;
400- latestTrades . forEach ( ( trade ) => {
401- this . _setLatestTrade ( trade ) ;
402- affectedPairs . add ( toPairKey ( trade . sourceToken , trade . targetToken ) ) ;
403- } ) ;
404- createdStrategies . forEach ( ( strategy ) => {
405- this . _addStrategy ( strategy ) ;
406- affectedPairs . add ( toPairKey ( strategy . token0 , strategy . token1 ) ) ;
407- } ) ;
408- updatedStrategies . forEach ( ( strategy ) => {
409- this . _updateStrategy ( strategy ) ;
410- affectedPairs . add ( toPairKey ( strategy . token0 , strategy . token1 ) ) ;
411- } ) ;
412- deletedStrategies . forEach ( ( strategy ) => {
413- this . _deleteStrategy ( strategy ) ;
414- affectedPairs . add ( toPairKey ( strategy . token0 , strategy . token1 ) ) ;
415- } ) ;
416- latestFeeUpdates . forEach ( ( [ token0 , token1 , newFee ] ) => {
417- this . _tradingFeePPMByPair [ toPairKey ( token0 , token1 ) ] = newFee ;
418- } ) ;
419- this . _setLatestBlockNumber ( latestBlockNumber ) ;
384+ // Update latest block number
385+ this . _setLatestBlockNumber ( currentBlock ) ;
386+
387+ // Process events in order
388+ for ( const event of events ) {
389+ switch ( event . type ) {
390+ case 'StrategyCreated' : {
391+ const strategy = event . data as EncodedStrategy ;
392+ this . _addStrategy ( strategy ) ;
393+ affectedPairs . add ( toPairKey ( strategy . token0 , strategy . token1 ) ) ;
394+ break ;
395+ }
396+ case 'StrategyUpdated' : {
397+ const strategy = event . data as EncodedStrategy ;
398+ this . _updateStrategy ( strategy ) ;
399+ affectedPairs . add ( toPairKey ( strategy . token0 , strategy . token1 ) ) ;
400+ break ;
401+ }
402+ case 'StrategyDeleted' : {
403+ const strategy = event . data as EncodedStrategy ;
404+ this . _deleteStrategy ( strategy ) ;
405+ affectedPairs . add ( toPairKey ( strategy . token0 , strategy . token1 ) ) ;
406+ break ;
407+ }
408+ case 'TokensTraded' : {
409+ const trade = event . data as TradeData ;
410+ this . _setLatestTrade ( trade ) ;
411+ affectedPairs . add ( toPairKey ( trade . sourceToken , trade . targetToken ) ) ;
412+ break ;
413+ }
414+ case 'PairTradingFeePPMUpdated' : {
415+ const feeUpdate = event . data as TradingFeeUpdate ;
416+ this . addPairFees ( feeUpdate [ 0 ] , feeUpdate [ 1 ] , feeUpdate [ 2 ] ) ;
417+ break ;
418+ }
419+ case 'TradingFeePPMUpdated' : {
420+ // This event type is handled by the caller
421+ break ;
422+ }
423+ }
424+ }
420425
421426 if ( affectedPairs . size > 0 ) {
422427 logger . debug ( 'Emitting onPairDataChanged' , affectedPairs ) ;
@@ -433,12 +438,13 @@ export class ChainCache extends (EventEmitter as new () => TypedEventEmitter<Cac
433438
434439 private _setLatestTrade ( trade : TradeData ) : void {
435440 if ( ! this . hasCachedPair ( trade . sourceToken , trade . targetToken ) ) {
436- throw new Error (
441+ logger . error (
437442 `Pair ${ toPairKey (
438443 trade . sourceToken ,
439444 trade . targetToken
440445 ) } is not cached, cannot set latest trade`
441446 ) ;
447+ return ;
442448 }
443449 const key = toPairKey ( trade . sourceToken , trade . targetToken ) ;
444450 this . _latestTradesByPair [ key ] = trade ;
@@ -484,12 +490,13 @@ export class ChainCache extends (EventEmitter as new () => TypedEventEmitter<Cac
484490
485491 private _addStrategy ( strategy : EncodedStrategy ) : void {
486492 if ( ! this . hasCachedPair ( strategy . token0 , strategy . token1 ) ) {
487- throw new Error (
493+ logger . error (
488494 `Pair ${ toPairKey (
489495 strategy . token0 ,
490496 strategy . token1
491497 ) } is not cached, cannot add strategy`
492498 ) ;
499+ return ;
493500 }
494501 const key = toPairKey ( strategy . token0 , strategy . token1 ) ;
495502 if ( this . _strategiesById [ strategy . id . toString ( ) ] ) {
@@ -507,12 +514,13 @@ export class ChainCache extends (EventEmitter as new () => TypedEventEmitter<Cac
507514
508515 private _updateStrategy ( strategy : EncodedStrategy ) : void {
509516 if ( ! this . hasCachedPair ( strategy . token0 , strategy . token1 ) ) {
510- throw new Error (
517+ logger . error (
511518 `Pair ${ toPairKey (
512519 strategy . token0 ,
513520 strategy . token1
514521 ) } is not cached, cannot update strategy`
515522 ) ;
523+ return ;
516524 }
517525 const key = toPairKey ( strategy . token0 , strategy . token1 ) ;
518526 const strategies = ( this . _strategiesByPair [ key ] || [ ] ) . filter (
@@ -527,12 +535,13 @@ export class ChainCache extends (EventEmitter as new () => TypedEventEmitter<Cac
527535
528536 private _deleteStrategy ( strategy : EncodedStrategy ) : void {
529537 if ( ! this . hasCachedPair ( strategy . token0 , strategy . token1 ) ) {
530- throw new Error (
538+ logger . error (
531539 `Pair ${ toPairKey (
532540 strategy . token0 ,
533541 strategy . token1
534542 ) } is not cached, cannot delete strategy`
535543 ) ;
544+ return ;
536545 }
537546 const key = toPairKey ( strategy . token0 , strategy . token1 ) ;
538547 delete this . _strategiesById [ strategy . id . toString ( ) ] ;
0 commit comments