@@ -18,11 +18,38 @@ import { getProvider } from "./provider";
1818import { sendDiscordText } from "./discord" ;
1919import { getConnection } from "../helpers/solana" ;
2020import { chainMappings } from "../helpers/tokenMappings" ;
21+ import { getCache , setCache } from "./cache" ;
2122const axios = require ( "axios" ) ;
2223const retry = require ( "async-retry" ) ;
2324
2425const SECONDS_IN_DAY = 86400 ;
2526
27+ interface AdapterProgress {
28+ lastSuccessfulBlock : number ;
29+ lastUpdated : number ;
30+ }
31+
32+ const getProgressKey = ( bridgeDbName : string , chain : string ) =>
33+ `adapter_progress:${ bridgeDbName . toLowerCase ( ) } :${ chain . toLowerCase ( ) } ` ;
34+
35+ const getAdapterProgress = async ( bridgeDbName : string , chain : string ) : Promise < AdapterProgress | null > => {
36+ return await getCache ( getProgressKey ( bridgeDbName , chain ) ) ;
37+ } ;
38+
39+ const PROGRESS_TTL = 60 * 60 * 24 * 7 ; // 7 days
40+
41+ const setAdapterProgress = async ( bridgeDbName : string , chain : string , lastBlock : number ) : Promise < void > => {
42+ const key = getProgressKey ( bridgeDbName , chain ) ;
43+ const current = await getAdapterProgress ( bridgeDbName , chain ) ;
44+ if ( current ?. lastSuccessfulBlock && current . lastSuccessfulBlock >= lastBlock ) {
45+ return ;
46+ }
47+ await setCache ( key , {
48+ lastSuccessfulBlock : lastBlock ,
49+ lastUpdated : Math . floor ( Date . now ( ) / 1000 )
50+ } , PROGRESS_TTL ) ;
51+ } ;
52+
2653// FIX timeout problems throughout functions here
2754
2855const getBlocksForRunningAdapter = async (
@@ -73,15 +100,14 @@ const getBlocksForRunningAdapter = async (
73100 lastRecordedEndBlock + 1
74101 } .`
75102 ) ;
76- } else {
77- // try {
78- // const lastTs = await getTimestamp(lastRecordedEndBlock, chain);
79- // const sixHoursBlock = await getBlock(chain, Number((currentTimestamp - SECONDS_IN_DAY / 4).toFixed()));
80- // lastRecordedEndBlock = currentTimestamp - lastTs > SECONDS_IN_DAY ? sixHoursBlock : lastRecordedEndBlock;
81- // } catch (e: any) {
82- // console.error("Get start block error");
83- // }
84103 }
104+
105+ const cachedProgress = await getAdapterProgress ( bridgeDbName , chain ) ;
106+ if ( cachedProgress ?. lastSuccessfulBlock && cachedProgress . lastSuccessfulBlock > lastRecordedEndBlock ) {
107+ console . log ( `[PROGRESS] Using Redis progress for ${ bridgeDbName } :${ chain } , advancing from block ${ lastRecordedEndBlock } to ${ cachedProgress . lastSuccessfulBlock } ` ) ;
108+ lastRecordedEndBlock = cachedProgress . lastSuccessfulBlock ;
109+ }
110+
85111 startBlock = lastRecordedEndBlock + 1 ;
86112 useRecordedBlocks = true ;
87113 } else {
@@ -328,7 +354,7 @@ export const runAllAdaptersTimestampRange = async (
328354 startBlock = ( await lookupBlock ( startTimestamp , { chain : chainContractsAreOn as Chain } ) ) . block ;
329355 endBlock = ( await lookupBlock ( endTimestamp , { chain : chainContractsAreOn as Chain } ) ) . block ;
330356 }
331- await runAdapterHistorical ( startBlock , endBlock , id , chain as Chain , allowNullTxValues , true , onConflict ) ;
357+ await runAdapterHistorical ( startBlock , endBlock , id , chain as Chain , allowNullTxValues , true , onConflict , false ) ;
332358 } catch ( e : any ) {
333359 const errString = `Adapter txs for ${ bridgeDbName } on chain ${ chain } failed, skipped. ${ JSON . stringify ( e ) } ` ;
334360 await insertErrorRow ( {
@@ -356,7 +382,8 @@ export const runAdapterHistorical = async (
356382 chain : string ,
357383 allowNullTxValues : boolean = false ,
358384 throwOnFailedInsert : boolean = true ,
359- onConflict : "ignore" | "error" | "upsert" = "error"
385+ onConflict : "ignore" | "error" | "upsert" = "error" ,
386+ updateProgress : boolean = true
360387) => {
361388 const currentTimestamp = await getCurrentUnixTimestamp ( ) ;
362389 const bridgeNetwork = bridgeNetworks . filter ( ( bridgeNetwork ) => bridgeNetwork . id === bridgeNetworkId ) [ 0 ] ;
@@ -366,6 +393,19 @@ export const runAdapterHistorical = async (
366393 return ;
367394 }
368395
396+ const cachedProgress = await getAdapterProgress ( bridgeDbName , chain ) ;
397+ if ( cachedProgress ?. lastSuccessfulBlock ) {
398+ if ( cachedProgress . lastSuccessfulBlock >= endBlock ) {
399+ console . log ( `[SKIP] ${ bridgeDbName } :${ chain } blocks ${ startBlock } -${ endBlock } already processed (last: ${ cachedProgress . lastSuccessfulBlock } )` ) ;
400+ return ;
401+ }
402+ if ( cachedProgress . lastSuccessfulBlock >= startBlock ) {
403+ const newStart = cachedProgress . lastSuccessfulBlock + 1 ;
404+ console . log ( `[PROGRESS] ${ bridgeDbName } :${ chain } adjusting start from ${ startBlock } to ${ newStart } (last: ${ cachedProgress . lastSuccessfulBlock } )` ) ;
405+ startBlock = newStart ;
406+ }
407+ }
408+
369409 let adapter = adapters [ bridgeDbName ] ;
370410 adapter = isAsyncAdapter ( adapter ) ? await adapter . build ( ) : adapter ;
371411
@@ -449,6 +489,9 @@ export const runAdapterHistorical = async (
449489 ) ;
450490
451491 if ( ! eventLogs || eventLogs . length === 0 ) {
492+ if ( updateProgress ) {
493+ await setAdapterProgress ( bridgeDbName , chain , endBlockForQuery ) ;
494+ }
452495 break ;
453496 }
454497
@@ -631,6 +674,9 @@ export const runAdapterHistorical = async (
631674 { retries : 3 , factor : 2 }
632675 ) ;
633676
677+ if ( updateProgress ) {
678+ await setAdapterProgress ( bridgeDbName , chain , endBlockForQuery ) ;
679+ }
634680 break ;
635681 } catch ( e : any ) {
636682 retryCount ++ ;
0 commit comments