Skip to content

Commit

Permalink
rerun adapters every 12h
Browse files Browse the repository at this point in the history
  • Loading branch information
vrtnd committed Nov 19, 2024
1 parent 610cf5e commit d845134
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
4 changes: 2 additions & 2 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ functions:
timeout: 900
memorySize: 1024
events:
- schedule: cron(1 * * * ? *)
- schedule: cron(0 0/12 * * ? *)
runAdapterByName:
handler: src/handlers/runAdapterByName.default
timeout: 900
Expand All @@ -168,7 +168,7 @@ functions:
timeout: 900
memorySize: 1024
events:
- schedule: cron(0 1 * * ? *)
- schedule: cron(30 0/12 * * ? *)
getNetflows:
handler: src/handlers/getNetflows.default
timeout: 30
Expand Down
45 changes: 30 additions & 15 deletions src/handlers/runAdapterFromTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { wrapScheduledLambda } from "../utils/wrap";
import bridgeNetworks from "../data/bridgeNetworkData";
import { runAdapterHistorical } from "../utils/adapter";
import { sql } from "../utils/db";
import { Chain } from "@defillama/sdk/build/general";
import { getBlockByTimestamp } from "../utils/blocks";
import { getBridgeID } from "../utils/wrappa/postgres/query";

const handler = async (event: any) => {
try {
Expand All @@ -25,25 +24,41 @@ const handler = async (event: any) => {

console.log(`Processing chain ${nChain} for ${bridgeName}`);

let fromBlock, toBlock;
if (bridgeName === "ibc") {
fromBlock = await getBlockByTimestamp(fromTimestamp, nChain as Chain, adapter, "First");
toBlock = await getBlockByTimestamp(toTimestamp, nChain as Chain, adapter, "Last");
} else {
fromBlock = await getBlockByTimestamp(fromTimestamp, nChain as Chain);
toBlock = await getBlockByTimestamp(toTimestamp, nChain as Chain);
const bridgeConfig = await getBridgeID(bridgeName, nChain);
if (!bridgeConfig) {
console.error(`Could not find bridge config for ${nChain} on ${bridgeName}`);
return;
}

if (!fromBlock || !toBlock) {
console.error(`Could not find blocks for ${nChain} on ${bridgeName}`);
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 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
`;

if (!fromTx.length || !toTx.length) {
console.error(`Could not find transactions with blocks for ${nChain} on ${bridgeName}`);
return;
}

await runAdapterHistorical(fromBlock.block, toBlock.block, adapter.id, nChain, true, false, "upsert");
const fromBlock = fromTx[0].tx_block;
const toBlock = toTx[0].tx_block;

await runAdapterHistorical(fromBlock, toBlock, adapter.id, nChain, true, false, "upsert");

console.log(
`Adapter ${bridgeName} ran successfully for chain ${nChain} from block ${fromBlock.block} to ${toBlock.block}`
);
console.log(`Adapter ${bridgeName} ran successfully for chain ${nChain} from block ${fromBlock} to ${toBlock}`);
});

await Promise.all(promises);
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/runAllAdaptersHistorical.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ async function invokeLambda(functionName: string, event: any) {
const handler = async (_event: any) => {
await closeIdleConnections();
const now = Math.floor(Date.now() / 1000);
const oneHourAgo = now - 3600;
const dayAgo = now - 86400;

for (const bridge of bridgeNetworks) {
await invokeLambda("llama-bridges-prod-runAdapterFromTo", {
bridgeName: bridge.bridgeDbName,
fromTimestamp: oneHourAgo,
fromTimestamp: dayAgo,
toTimestamp: now,
});
}
Expand Down

0 comments on commit d845134

Please sign in to comment.