Skip to content

Commit

Permalink
optimize netflows
Browse files Browse the repository at this point in the history
  • Loading branch information
vrtnd committed Dec 3, 2024
1 parent da84f52 commit 2060d08
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/utils/cache.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { LRUCache } from "lru-cache";
import hash from "object-hash";

const MAX_SIZE_BYTES = 100 * 1024 * 1024;
const MAX_SIZE_BYTES = 300 * 1024 * 1024;

export const cache = new LRUCache({
maxSize: MAX_SIZE_BYTES,
sizeCalculation: (value: any) => {
return Buffer.byteLength(JSON.stringify(value), "utf8");
},
ttl: 1000 * 600,
ttl: 100 * 60 * 12,
});

interface APIEvent {
Expand Down
20 changes: 9 additions & 11 deletions src/utils/wrappa/postgres/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,19 +366,17 @@ const getNetflows = async (period: TimePeriod) => {
return await sql<{ chain: string; net_flow: string }[]>`
WITH period_flows AS (
SELECT
c.chain,
hv.chain,
SUM(CASE
-- Deposits are outflows (-), withdrawals are inflows (+)
WHEN c.destination_chain IS NULL THEN (ha.total_withdrawn_usd - ha.total_deposited_usd)
-- For bridges with fixed destination chains, count flows on destination chain (inverted)
ELSE (ha.total_deposited_usd - ha.total_withdrawn_usd)
WHEN c.destination_chain IS NULL THEN (hv.total_withdrawn_usd - hv.total_deposited_usd)
ELSE (hv.total_deposited_usd - hv.total_withdrawn_usd)
END) as net_flow
FROM bridges.hourly_aggregated ha
JOIN bridges.config c ON ha.bridge_id = c.id
WHERE ha.ts >= date_trunc(${period}, NOW() AT TIME ZONE 'UTC') - ${intervalPeriod}
AND ha.ts < date_trunc(${period}, NOW() AT TIME ZONE 'UTC')
AND LOWER(c.chain) NOT LIKE '%dydx%'
GROUP BY c.chain
FROM bridges.hourly_volume hv
JOIN bridges.config c ON hv.bridge_id = c.id
WHERE hv.ts >= date_trunc(${period}, NOW() AT TIME ZONE 'UTC') - ${intervalPeriod}
AND hv.ts < date_trunc(${period}, NOW() AT TIME ZONE 'UTC')
AND LOWER(hv.chain) NOT LIKE '%dydx%'
GROUP BY hv.chain
)
SELECT
chain,
Expand Down

0 comments on commit 2060d08

Please sign in to comment.