@@ -35,6 +35,29 @@ import { GlobalRpcProviders } from '../rpc/GlobalRpcProviders'
3535// so we will manually populate ALCHEMY_QUERY_KEY env var in the cron job lambda in cache-config.ts 
3636dotenv . config ( ) 
3737
38+ function  getEthToUsdRate ( pools : V4SubgraphPool [ ] ) : number  { 
39+   const  top3TvlPools  =  pools 
40+     . slice ( ) 
41+     . sort ( ( a ,  b )  =>  b . tvlUSD  -  a . tvlUSD ) 
42+     . slice ( 0 ,  3 ) 
43+   const  averageRate  =  top3TvlPools . reduce ( ( acc ,  pool )  =>  acc  +  pool . tvlUSD  /  pool . tvlETH ,  0 )  /  top3TvlPools . length 
44+   return  averageRate 
45+ } 
46+ 
47+ function  isUsdEquivalent ( symbol : string  |  undefined ) : boolean  { 
48+   if  ( ! symbol )  { 
49+     return  false 
50+   } 
51+   return  [ 'USDC' ,  'USDT' ,  'USDe' ] . includes ( symbol ) 
52+ } 
53+ 
54+ function  isEthEquivalent ( symbol : string  |  undefined ) : boolean  { 
55+   if  ( ! symbol )  { 
56+     return  false 
57+   } 
58+   return  [ 'WETH' ,  'WSTETH' ,  'ETH' ] . includes ( symbol ) 
59+ } 
60+ 
3861const  EULER_SWAP_ADDRESS  =  '0x786956C1Eb57C47052d1676ac5084fA08d8068A8' 
3962
4063const  handler : ScheduledHandler  =  metricScope ( ( metrics )  =>  async  ( event : EventBridgeEvent < string ,  void > )  =>  { 
@@ -307,6 +330,9 @@ const handler: ScheduledHandler = metricScope((metrics) => async (event: EventBr
307330      ] 
308331
309332      if  ( eulerHooksProvider )  { 
333+         const  ethToUsdRate  =  getEthToUsdRate ( pools  as  V4SubgraphPool [ ] ) 
334+         log . info ( `Eth to USD rate: ${ ethToUsdRate }  ) 
335+ 
310336        const  eulerHooks  =  await  eulerHooksProvider ?. getHooks ( ) 
311337        if  ( eulerHooks )  { 
312338          metric . putMetric ( 'eulerHooks.length' ,  eulerHooks . length ) 
@@ -320,10 +346,40 @@ const handler: ScheduledHandler = metricScope((metrics) => async (event: EventBr
320346                return  null 
321347              } 
322348
349+               log . info ( `Pool: ${ JSON . stringify ( pool ) }  ) 
323350              // Get the TVL from the EulerSwap contract 
324351              const  limits  =  await  contract . getLimits ( pool . token0 . id ,  pool . token1 . id ) 
325-               ; ( pool  as  V4SubgraphPool ) . tvlUSD  =  limits [ 0 ] . toNumber ( ) 
326-               ; ( pool  as  V4SubgraphPool ) . tvlETH  =  limits [ 1 ] . toNumber ( ) 
352+               log . info ( `Limits: ${ JSON . stringify ( limits ) }  ) 
353+ 
354+               const  token0Symbol  =  pool . token0 . symbol 
355+               const  token1Symbol  =  pool . token1 . symbol 
356+               if  ( isUsdEquivalent ( token0Symbol ) )  { 
357+                 pool . tvlUSD  =  limits [ 0 ] . toNumber ( ) 
358+                 pool . tvlETH  =  limits [ 0 ] . toNumber ( )  /  ethToUsdRate 
359+               }  else  if  ( isUsdEquivalent ( token1Symbol ) )  { 
360+                 pool . tvlUSD  =  limits [ 1 ] . toNumber ( ) 
361+                 pool . tvlETH  =  limits [ 1 ] . toNumber ( )  /  ethToUsdRate 
362+               }  else  if  ( isEthEquivalent ( token0Symbol ) )  { 
363+                 pool . tvlETH  =  limits [ 0 ] . toNumber ( ) 
364+                 pool . tvlUSD  =  limits [ 0 ] . toNumber ( )  *  ethToUsdRate 
365+               }  else  if  ( isEthEquivalent ( token1Symbol ) )  { 
366+                 pool . tvlETH  =  limits [ 1 ] . toNumber ( ) 
367+                 pool . tvlUSD  =  limits [ 1 ] . toNumber ( )  *  ethToUsdRate 
368+               }  else  { 
369+                 log . info ( 
370+                   `Unknown token symbol: pool ${ pool . id } ${ token0Symbol } ${ token1Symbol }  
371+                 ) 
372+                 log . info ( `Setting TVL to 1000 USD and 5500000 ETH` ) 
373+               } 
374+ 
375+               if  ( ( pool . tvlUSD  ===  0  &&  pool . tvlETH  ===  0 )  ||  ! pool . tvlUSD  ||  ! pool . tvlETH )  { 
376+                 log . info ( `Pool ${ pool . id }  ) 
377+                 // we need to inflate euler pool TVL from 0 to significant TVL, so that they have a chance to be picked up 
378+                 ; ( pool  as  V4SubgraphPool ) . tvlUSD  =  1000 
379+                 ; ( pool  as  V4SubgraphPool ) . tvlETH  =  5500000 
380+               } 
381+ 
382+               log . info ( `Pool TVL USD: ${ pool . tvlUSD } ${ pool . tvlETH }  ) 
327383
328384              return  pool 
329385            } ) 
0 commit comments