Skip to content

Commit 60a3d6f

Browse files
committed
cache bridgevolume query
1 parent 00fbc20 commit 60a3d6f

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/utils/bridgeVolume.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ export const getDailyBridgeVolume = async (
4343
chain?: string,
4444
bridgeNetworkId?: number
4545
) => {
46+
const cacheKey = `daily_bridge_volume_${bridgeNetworkId ?? "all"}_${chain ?? "all"}_${startTimestamp ?? "default"}_${
47+
endTimestamp ?? "default"
48+
}`;
49+
const cachedData = await getCache(cacheKey);
50+
if (cachedData) {
51+
return cachedData;
52+
}
4653
let bridgeDbName = undefined as any;
4754
if (bridgeNetworkId) {
4855
const bridgeNetwork = importBridgeNetwork(undefined, bridgeNetworkId);
@@ -86,9 +93,6 @@ export const getDailyBridgeVolume = async (
8693
const { bridge_id, ts, total_deposited_usd, total_withdrawn_usd, total_deposit_txs, total_withdrawal_txs } =
8794
dailyData;
8895
if (isNaN(parseFloat(total_deposited_usd)) || !isFinite(parseFloat(total_deposited_usd))) {
89-
console.error(
90-
`Invalid deposited USD value for bridge_id ${bridge_id} at timestamp ${ts}: ${total_deposited_usd}`
91-
);
9296
return;
9397
}
9498
const timestamp = convertToUnixTimestamp(ts);
@@ -101,7 +105,6 @@ export const getDailyBridgeVolume = async (
101105
historicalDailySums[timestamp].withdrawTxs =
102106
(historicalDailySums[timestamp].withdrawTxs ?? 0) + total_withdrawal_txs;
103107
});
104-
// the deposits and withdrawals are swapped here
105108
sourceChainsHistoricalDailyData.map((dailyData) => {
106109
const { ts, total_deposited_usd, total_withdrawn_usd, total_deposit_txs, total_withdrawal_txs } = dailyData;
107110
const timestamp = convertToUnixTimestamp(ts);
@@ -121,6 +124,7 @@ export const getDailyBridgeVolume = async (
121124
};
122125
});
123126

127+
await setCache(cacheKey, dailyBridgeVolume, 3600);
124128
return dailyBridgeVolume;
125129
};
126130

src/utils/wrappa/postgres/query.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import bridgeNetworkData from "../../../data/bridgeNetworkData";
22
import { querySql as sql } from "../../db";
3+
import { getCache, setCache } from "../../cache";
34

45
interface IConfig {
56
id: string;
@@ -244,11 +245,14 @@ const queryLargeTransactionsTimestampRange = async (
244245
startTimestamp: number,
245246
endTimestamp: number
246247
) => {
248+
const cacheKey = `largeTxs:${chain ?? "all"}:${startTimestamp}:${endTimestamp}`;
249+
const cached = await getCache(cacheKey);
250+
if (cached) return cached;
247251
let chainEqual = sql``;
248252
if (chain) {
249253
chainEqual = sql`WHERE chain = ${chain} OR destination_chain = ${chain}`;
250254
}
251-
return await sql<ITransaction[]>`
255+
const result = await sql<ITransaction[]>`
252256
SELECT transactions.id, transactions.bridge_id, transactions.ts, transactions.tx_block, transactions.tx_hash, transactions.tx_from, transactions.tx_to, transactions.token, transactions.amount, transactions.is_deposit, transactions.chain, large_transactions.usd_value
253257
FROM bridges.transactions
254258
INNER JOIN bridges.large_transactions
@@ -265,6 +269,8 @@ const queryLargeTransactionsTimestampRange = async (
265269
FROM bridges.config ${chainEqual})
266270
ORDER BY ts DESC
267271
`;
272+
await setCache(cacheKey, result, 3600);
273+
return result;
268274
};
269275

270276
const queryTransactionsTimestampRangeByBridgeNetwork = async (

0 commit comments

Comments
 (0)