Skip to content

Commit

Permalink
change fromTo job
Browse files Browse the repository at this point in the history
  • Loading branch information
vrtnd committed Jan 9, 2025
1 parent 1bf2de7 commit f014207
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 26 deletions.
31 changes: 5 additions & 26 deletions src/server/jobs/runAdaptersFromTo.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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}`);
Expand Down
39 changes: 39 additions & 0 deletions src/utils/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getLatestBlockForZoneFromMoz,
ibcGetBlockFromTimestamp,
} from "../adapters/ibc";
import bridgeNetworkData from "../data/bridgeNetworkData";
const retry = require("async-retry");

const connection = getConnection();
Expand Down Expand Up @@ -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<string, string>, 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<string, { startBlock: number; endBlock: number }> = {};
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;
};

0 comments on commit f014207

Please sign in to comment.