@@ -485,8 +485,8 @@ const queryAggregatedTokenStatsTop30 = async (
485485 bridgeNetworkName ?: string ,
486486 limit : number = 30
487487) => {
488- let conditions = sql `WHERE ha.ts >= to_timestamp(${ startTimestamp } )::date
489- AND ha.ts <= to_timestamp(${ endTimestamp } )::date ` ;
488+ let conditions = sql `WHERE ha.ts >= to_timestamp(${ startTimestamp } )
489+ AND ha.ts < to_timestamp(${ endTimestamp } )` ;
490490
491491 if ( chain ) {
492492 conditions = sql `${ conditions } AND c.chain = ${ chain } ` ;
@@ -558,6 +558,182 @@ const queryAggregatedTokenStatsTop30 = async (
558558 ` ;
559559} ;
560560
561+ const queryAggregatedTokenStatsTop30Rolling = async (
562+ hours : number ,
563+ chain ?: string ,
564+ bridgeNetworkName ?: string ,
565+ limit : number = 30
566+ ) => {
567+ let latestConditions = sql `WHERE 1 = 1` ;
568+ let baseConditions = sql `` ;
569+
570+ if ( bridgeNetworkName ) {
571+ latestConditions = sql `${ latestConditions } AND c.bridge_name = ${ bridgeNetworkName } ` ;
572+ baseConditions = sql `${ baseConditions } AND c.bridge_name = ${ bridgeNetworkName } ` ;
573+ }
574+ if ( chain ) {
575+ baseConditions = sql `${ baseConditions } AND c.chain = ${ chain } ` ;
576+ if ( ! bridgeNetworkName ) {
577+ latestConditions = sql `${ latestConditions } AND c.chain = ${ chain } ` ;
578+ }
579+ }
580+
581+ return await sql <
582+ {
583+ kind : "dt" | "wt" | "da" | "wa" ;
584+ key : string ;
585+ amount : string | null ;
586+ usd_value : string ;
587+ txs : number | null ;
588+ } [ ]
589+ > `
590+ WITH latest_ts AS (
591+ SELECT MAX(ha.ts) AS max_ts
592+ FROM bridges.hourly_aggregated ha
593+ JOIN bridges.config c ON ha.bridge_id = c.id
594+ ${ latestConditions }
595+ ),
596+ base AS (
597+ SELECT ha.total_tokens_deposited, ha.total_tokens_withdrawn,
598+ ha.total_address_deposited, ha.total_address_withdrawn
599+ FROM bridges.hourly_aggregated ha
600+ JOIN bridges.config c ON ha.bridge_id = c.id
601+ CROSS JOIN latest_ts lt
602+ WHERE lt.max_ts IS NOT NULL
603+ AND ha.ts > lt.max_ts - make_interval(hours => ${ hours } )
604+ AND ha.ts <= lt.max_ts
605+ ${ baseConditions }
606+ ),
607+ dt AS (
608+ SELECT replace((tok).token, '''', '') AS key,
609+ trim_scale(SUM(replace(replace((tok).amount, '''', ''), ' ', '')::numeric))::text AS amount,
610+ SUM((tok).usd_value)::text AS usd_value
611+ FROM base CROSS JOIN LATERAL unnest(total_tokens_deposited) tok
612+ WHERE (tok).token IS NOT NULL
613+ GROUP BY replace((tok).token, '''', '')
614+ ORDER BY SUM((tok).usd_value) DESC NULLS LAST
615+ LIMIT ${ limit }
616+ ),
617+ wt AS (
618+ SELECT replace((tok).token, '''', '') AS key,
619+ trim_scale(SUM(replace(replace((tok).amount, '''', ''), ' ', '')::numeric))::text AS amount,
620+ SUM((tok).usd_value)::text AS usd_value
621+ FROM base CROSS JOIN LATERAL unnest(total_tokens_withdrawn) tok
622+ WHERE (tok).token IS NOT NULL
623+ GROUP BY replace((tok).token, '''', '')
624+ ORDER BY SUM((tok).usd_value) DESC NULLS LAST
625+ LIMIT ${ limit }
626+ ),
627+ da AS (
628+ SELECT replace((a).address, '''', '') AS key,
629+ SUM((a).usd_value)::text AS usd_value,
630+ COALESCE(SUM((a).txs), 0)::integer AS txs
631+ FROM base CROSS JOIN LATERAL unnest(total_address_deposited) a
632+ WHERE (a).address IS NOT NULL
633+ GROUP BY replace((a).address, '''', '')
634+ ORDER BY SUM((a).usd_value) DESC NULLS LAST
635+ LIMIT ${ limit }
636+ ),
637+ wa AS (
638+ SELECT replace((a).address, '''', '') AS key,
639+ SUM((a).usd_value)::text AS usd_value,
640+ COALESCE(SUM((a).txs), 0)::integer AS txs
641+ FROM base CROSS JOIN LATERAL unnest(total_address_withdrawn) a
642+ WHERE (a).address IS NOT NULL
643+ GROUP BY replace((a).address, '''', '')
644+ ORDER BY SUM((a).usd_value) DESC NULLS LAST
645+ LIMIT ${ limit }
646+ )
647+ SELECT 'dt' AS kind, key, amount, usd_value, NULL::integer AS txs FROM dt
648+ UNION ALL SELECT 'wt', key, amount, usd_value, NULL::integer FROM wt
649+ UNION ALL SELECT 'da', key, NULL::text, usd_value, txs FROM da
650+ UNION ALL SELECT 'wa', key, NULL::text, usd_value, txs FROM wa
651+ ` ;
652+ } ;
653+
654+ const queryAggregatedTotalsTimestampRange = async (
655+ startTimestamp : number ,
656+ endTimestamp : number ,
657+ chain ?: string ,
658+ bridgeNetworkName ?: string
659+ ) => {
660+ let conditions = sql `WHERE ha.ts >= to_timestamp(${ startTimestamp } )
661+ AND ha.ts < to_timestamp(${ endTimestamp } )` ;
662+
663+ if ( chain ) {
664+ conditions = sql `${ conditions } AND c.chain = ${ chain } ` ;
665+ }
666+ if ( bridgeNetworkName ) {
667+ conditions = sql `${ conditions } AND c.bridge_name = ${ bridgeNetworkName } ` ;
668+ }
669+
670+ return (
671+ await sql <
672+ {
673+ total_deposited_usd : string ;
674+ total_withdrawn_usd : string ;
675+ total_deposit_txs : number ;
676+ total_withdrawal_txs : number ;
677+ } [ ]
678+ > `
679+ SELECT
680+ COALESCE(SUM(ha.total_deposited_usd), 0)::text AS total_deposited_usd,
681+ COALESCE(SUM(ha.total_withdrawn_usd), 0)::text AS total_withdrawn_usd,
682+ COALESCE(SUM(ha.total_deposit_txs), 0)::integer AS total_deposit_txs,
683+ COALESCE(SUM(ha.total_withdrawal_txs), 0)::integer AS total_withdrawal_txs
684+ FROM bridges.hourly_aggregated ha
685+ JOIN bridges.config c ON ha.bridge_id = c.id
686+ ${ conditions }
687+ `
688+ ) [ 0 ] ;
689+ } ;
690+
691+ const queryAggregatedTotalsRolling = async ( hours : number , chain ?: string , bridgeNetworkName ?: string ) => {
692+ let latestConditions = sql `WHERE 1 = 1` ;
693+ let baseConditions = sql `` ;
694+
695+ if ( bridgeNetworkName ) {
696+ latestConditions = sql `${ latestConditions } AND c.bridge_name = ${ bridgeNetworkName } ` ;
697+ baseConditions = sql `${ baseConditions } AND c.bridge_name = ${ bridgeNetworkName } ` ;
698+ }
699+ if ( chain ) {
700+ baseConditions = sql `${ baseConditions } AND c.chain = ${ chain } ` ;
701+ if ( ! bridgeNetworkName ) {
702+ latestConditions = sql `${ latestConditions } AND c.chain = ${ chain } ` ;
703+ }
704+ }
705+
706+ return (
707+ await sql <
708+ {
709+ total_deposited_usd : string ;
710+ total_withdrawn_usd : string ;
711+ total_deposit_txs : number ;
712+ total_withdrawal_txs : number ;
713+ } [ ]
714+ > `
715+ WITH latest_ts AS (
716+ SELECT MAX(ha.ts) AS max_ts
717+ FROM bridges.hourly_aggregated ha
718+ JOIN bridges.config c ON ha.bridge_id = c.id
719+ ${ latestConditions }
720+ )
721+ SELECT
722+ COALESCE(SUM(ha.total_deposited_usd), 0)::text AS total_deposited_usd,
723+ COALESCE(SUM(ha.total_withdrawn_usd), 0)::text AS total_withdrawn_usd,
724+ COALESCE(SUM(ha.total_deposit_txs), 0)::integer AS total_deposit_txs,
725+ COALESCE(SUM(ha.total_withdrawal_txs), 0)::integer AS total_withdrawal_txs
726+ FROM bridges.hourly_aggregated ha
727+ JOIN bridges.config c ON ha.bridge_id = c.id
728+ CROSS JOIN latest_ts lt
729+ WHERE lt.max_ts IS NOT NULL
730+ AND ha.ts > lt.max_ts - make_interval(hours => ${ hours } )
731+ AND ha.ts <= lt.max_ts
732+ ${ baseConditions }
733+ `
734+ ) [ 0 ] ;
735+ } ;
736+
561737export {
562738 getBridgeID ,
563739 getConfigsWithDestChain ,
@@ -574,4 +750,7 @@ export {
574750 queryBridgeTxCounts24h ,
575751 getNetflows ,
576752 queryAggregatedTokenStatsTop30 ,
753+ queryAggregatedTokenStatsTop30Rolling ,
754+ queryAggregatedTotalsTimestampRange ,
755+ queryAggregatedTotalsRolling ,
577756} ;
0 commit comments