diff --git a/serverless.yml b/serverless.yml index f4f727c8..f5c04243 100644 --- a/serverless.yml +++ b/serverless.yml @@ -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 @@ -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 diff --git a/src/handlers/runAdapterFromTo.ts b/src/handlers/runAdapterFromTo.ts index a6f1d76e..12a2cd70 100644 --- a/src/handlers/runAdapterFromTo.ts +++ b/src/handlers/runAdapterFromTo.ts @@ -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 { @@ -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); diff --git a/src/handlers/runAllAdaptersHistorical.ts b/src/handlers/runAllAdaptersHistorical.ts index dfb9c62b..4e8cea3b 100644 --- a/src/handlers/runAllAdaptersHistorical.ts +++ b/src/handlers/runAllAdaptersHistorical.ts @@ -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, }); }