diff --git a/src/server/jobs/runAdaptersFromTo.ts b/src/server/jobs/runAdaptersFromTo.ts index 7ecbfe3a..17a16bea 100644 --- a/src/server/jobs/runAdaptersFromTo.ts +++ b/src/server/jobs/runAdaptersFromTo.ts @@ -1,13 +1,13 @@ import bridgeNetworks from "../../data/bridgeNetworkData"; import { chainMappings } from "../../helpers/tokenMappings"; import { runAdapterHistorical } from "../../utils/adapter"; -import { sql } from "../../utils/db"; -import { getBridgeID } from "../../utils/wrappa/postgres/query"; +import { getBlocksByAllChains } from "../../utils/blocks"; import dayjs from "dayjs"; export const runAdaptersFromTo = async () => { - const fromTimestamp = dayjs().subtract(4, "hour").unix(); + const fromTimestamp = dayjs().subtract(12, "hour").unix(); const toTimestamp = dayjs().unix(); + const blockByChain = await getBlocksByAllChains(fromTimestamp, toTimestamp); await Promise.all( bridgeNetworks.map(async (adapter) => { @@ -25,29 +25,8 @@ export const runAdaptersFromTo = async () => { console.log(`Processing chain ${nChain} for ${bridgeName}`); - const bridgeConfig = await getBridgeID(bridgeName, nChain); - if (!bridgeConfig) { - console.error(`Could not find bridge config for ${nChain} on ${bridgeName}`); - return; - } - const fromTx = await sql<{ tx_block: number }[]>` - SELECT tx_block FROM bridges.transactions - WHERE bridge_id = ${bridgeConfig.id} - AND chain = ${nChain} - AND tx_block IS NOT NULL - AND ts <= to_timestamp(${fromTimestamp}) - ORDER BY ts DESC LIMIT 1 - `; - const fromBlock = fromTx[0].tx_block; - const toTx = await sql<{ tx_block: number }[]>` - SELECT tx_block FROM bridges.transactions - WHERE bridge_id = ${bridgeConfig.id} - AND chain = ${nChain} - AND tx_block IS NOT NULL - AND ts >= to_timestamp(${toTimestamp}) - ORDER BY ts ASC LIMIT 1 - `; - const toBlock = toTx[0].tx_block; + const fromBlock = blockByChain[nChain].startBlock; + const toBlock = blockByChain[nChain].endBlock; if (!fromBlock || !toBlock) { console.error(`Could not find transactions with blocks for ${nChain} on ${bridgeName}`); diff --git a/src/utils/blocks.ts b/src/utils/blocks.ts index e898fec3..b4fd6e0e 100644 --- a/src/utils/blocks.ts +++ b/src/utils/blocks.ts @@ -10,6 +10,7 @@ import { getLatestBlockForZoneFromMoz, ibcGetBlockFromTimestamp, } from "../adapters/ibc"; +import bridgeNetworkData from "../data/bridgeNetworkData"; const retry = require("async-retry"); const connection = getConnection(); @@ -133,3 +134,41 @@ export async function getTimestampBySolanaSlot( const { timestamp: latestTimestamp, number: latestSlot } = latestBlock; return latestTimestamp - ((latestSlot - slot) * 400) / 1000; } + +const allChains = bridgeNetworkData.flatMap((x) => x.chains.map((y) => y.toLowerCase())); +const allMappings = bridgeNetworkData.reduce((acc: Record, x: any) => { + if (x.chainMapping) { + Object.keys(x.chainMapping).forEach((y) => (acc[y.toLowerCase()] = x.chainMapping[y])); + } + return acc; +}, {}); + +const allMappedChains = [...new Set(allChains.map((x) => allMappings[x] || x))]; +export const getBlocksByAllChains = async (startTs: number, endTs: number) => { + const blockByChain: Record = {}; + const errorChains: string[] = []; + await Promise.all( + allMappedChains.map(async (chain) => { + try { + const startBlock = await retry(() => getBlockByTimestamp(startTs, chain as Chain), { + retries: 3, + factor: 1, + }); + const endBlock = await retry(() => getBlockByTimestamp(endTs, chain as Chain), { + retries: 3, + factor: 1, + }); + blockByChain[chain] = { + startBlock: startBlock.block, + endBlock: endBlock.block, + }; + console.log(`Set blocks for ${chain}: ${startBlock.block} to ${endBlock.block}`); + } catch (e) { + errorChains.push(chain); + } + }) + ); + console.log(blockByChain); + console.log(`Error chains: ${errorChains.join(", ")}`); + return blockByChain; +};