From 8e3974acf8fb6f4703a7ed33917ad330f99f8349 Mon Sep 17 00:00:00 2001 From: vrtnd Date: Sat, 28 Sep 2024 14:36:55 +0300 Subject: [PATCH] Run adapter by name --- serverless.yml | 4 ++++ src/handlers/runAdapterByName.ts | 40 ++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 src/handlers/runAdapterByName.ts diff --git a/serverless.yml b/serverless.yml index 81464d7d..9af09626 100644 --- a/serverless.yml +++ b/serverless.yml @@ -159,6 +159,10 @@ functions: memorySize: 1024 events: - schedule: cron(1 * * * ? *) + runAdapterByName: + handler: src/handlers/runAdapterByName.default + timeout: 900 + memorySize: 1024 dailyAggregateAllAdapters: handler: src/handlers/dailyAggregateAllAdapters.default timeout: 900 diff --git a/src/handlers/runAdapterByName.ts b/src/handlers/runAdapterByName.ts new file mode 100644 index 00000000..41256a5b --- /dev/null +++ b/src/handlers/runAdapterByName.ts @@ -0,0 +1,40 @@ +import { wrapScheduledLambda } from "../utils/wrap"; +import bridgeNetworks from "../data/bridgeNetworkData"; +import { runAdapterToCurrentBlock } from "../utils/adapter"; +import { sql } from "../utils/db"; +import { BridgeNetwork } from "../data/types"; + +const handler = async (event: { bridgeName: string }) => { + try { + const { bridgeName } = event; + + if (!bridgeName) { + throw new Error("Bridge name is required"); + } + + const bridge = bridgeNetworks.find((b: BridgeNetwork) => b.bridgeDbName === bridgeName); + + if (!bridge) { + throw new Error(`Bridge ${bridgeName} not found`); + } + + const lastRecordedBlocks = await sql`SELECT jsonb_object_agg(bridge_id::text, subresult) as result + FROM ( + SELECT bridge_id, jsonb_build_object('startBlock', MIN(tx_block), 'endBlock', MAX(tx_block)) as subresult + FROM bridges.transactions + GROUP BY bridge_id + ) subquery; + `; + + console.log(`Running adapter for ${bridgeName}`); + await runAdapterToCurrentBlock(bridge, true, "ignore", lastRecordedBlocks); + console.log(`Adapter for ${bridgeName} ran successfully`); + } catch (e) { + console.error(`Adapter ${event.bridgeName} failed: ${JSON.stringify(e)}`); + throw e; + } finally { + await sql.end(); + } +}; + +export default wrapScheduledLambda(handler);