diff --git a/serverless.yml b/serverless.yml index 6ade5f09..6dc5d174 100644 --- a/serverless.yml +++ b/serverless.yml @@ -118,6 +118,14 @@ functions: - http: path: largetransactions/{chain} method: get + lastBlocks: + handler: src/handlers/getLastBlocks.default + timeout: 120 + memorySize: 2000 + events: + - http: + path: lastblocks + method: get runAdapter: handler: src/handlers/runAdapter.default timeout: 900 diff --git a/src/handlers/getLastBlocks.ts b/src/handlers/getLastBlocks.ts new file mode 100644 index 00000000..02490c2a --- /dev/null +++ b/src/handlers/getLastBlocks.ts @@ -0,0 +1,32 @@ +import wrap, { wrapScheduledLambda } from "../utils/wrap"; +import bridgeNetworks from "../data/bridgeNetworkData"; +import { runAdapterToCurrentBlock } from "../utils/adapter"; +import { sql } from "../utils/db"; +import { successResponse } from "../utils/lambda-response"; + +const handler = async () => { + 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; + ` + )[0].result; + + const bridgeConfig = await sql`SELECT * FROM bridges.config`; + + const bridgeConfigById = bridgeConfig.reduce((acc: any, config: any) => { + acc[config.id] = config; + return acc; + }, {}); + + const lastBlocksByName = Object.keys(lastRecordedBlocks).reduce((acc: any, bridgeId: any) => { + acc[`${bridgeConfigById[bridgeId].bridge_name}-${bridgeConfigById[bridgeId].chain}`] = lastRecordedBlocks[bridgeId]; + return acc; + }, {}); + return successResponse(lastBlocksByName); +}; + +export default wrap(handler);