Skip to content

Commit

Permalink
Close connections
Browse files Browse the repository at this point in the history
  • Loading branch information
vrtnd committed Sep 3, 2024
1 parent 2647390 commit a370dc5
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/handlers/dailyAggregateAllAdapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { wrapScheduledLambda } from "../utils/wrap";
import { convertToUnixTimestamp } from "../utils/date";
import { runAggregateDataHistorical } from "../utils/aggregate";
import bridgeNetworkData from "../data/bridgeNetworkData";
import { sql } from "../utils/db";

export default wrapScheduledLambda(async (_event) => {
const currentDate = new Date();
Expand All @@ -19,4 +20,9 @@ export default wrapScheduledLambda(async (_event) => {
for (const adapter of bridgeNetworkData) {
await runAggregateDataHistorical(startTimestamp, endTimestamp, adapter.id, false);
}
try {
await sql.end();
} catch (e) {
console.error(e);
}
});
6 changes: 6 additions & 0 deletions src/handlers/getBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import getAggregatedDataClosestToTimestamp from "../utils/getRecordClosestToTime
import { importBridgeNetwork } from "../data/importBridgeNetwork";
import { normalizeChain } from "../utils/normalizeChain";
import { getLast24HVolume } from "../utils/wrappa/postgres/query";
import { sql } from "../utils/db";

const getBridge = async (bridgeNetworkId?: number) => {
const bridgeNetwork = importBridgeNetwork(undefined, bridgeNetworkId);
Expand Down Expand Up @@ -160,6 +161,11 @@ const getBridge = async (bridgeNetworkId?: number) => {
const handler = async (event: AWSLambda.APIGatewayEvent): Promise<IResponse> => {
const bridgeNetworkId = parseInt(event.pathParameters?.id ?? "0");
const response = await getBridge(bridgeNetworkId);
try {
await sql.end();
} catch (e) {
console.error(e);
}
return successResponse(response, 10 * 60); // 10 mins cache
};

Expand Down
6 changes: 6 additions & 0 deletions src/handlers/getBridgeChains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getChainDisplayName, chainCoingeckoIds } from "../utils/normalizeChain"
import { getCurrentUnixTimestamp, secondsInDay } from "../utils/date";
import bridgeNetworks from "../data/bridgeNetworkData";
import { normalizeChain } from "../utils/normalizeChain";
import { sql } from "../utils/db";

export async function craftBridgeChainsResponse() {
const chainsSet = new Set<string>();
Expand Down Expand Up @@ -51,6 +52,11 @@ export async function craftBridgeChainsResponse() {

const handler = async (_event: AWSLambda.APIGatewayEvent): Promise<IResponse> => {
const chainData = await craftBridgeChainsResponse();
try {
await sql.end();
} catch (e) {
console.error(e);
}
return successResponse(chainData, 10 * 60); // 10 mins cache
};

Expand Down
11 changes: 8 additions & 3 deletions src/handlers/getBridgeVolume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getDailyBridgeVolume, getHourlyBridgeVolume } from "../utils/bridgeVolu
import { importBridgeNetwork } from "../data/importBridgeNetwork";
import { secondsInDay, getCurrentUnixTimestamp } from "../utils/date";
import { normalizeChain } from "../utils/normalizeChain";
import { sql } from "../utils/db";

const getBridgeVolume = async (chain?: string, bridgeNetworkId?: string) => {
if (!chain) {
Expand All @@ -13,11 +14,10 @@ const getBridgeVolume = async (chain?: string, bridgeNetworkId?: string) => {
}
let bridgeNetwork;
if (bridgeNetworkId) {

bridgeNetwork = importBridgeNetwork(undefined, parseInt(bridgeNetworkId));
bridgeNetwork = importBridgeNetwork(undefined, parseInt(bridgeNetworkId));
}
const destinationChain = bridgeNetwork?.destinationChain;
if (destinationChain && chain === destinationChain?.toLowerCase()){
if (destinationChain && chain === destinationChain?.toLowerCase()) {
chain = "all";
}
const queryChain = chain === "all" ? undefined : normalizeChain(chain);
Expand Down Expand Up @@ -95,6 +95,11 @@ const handler = async (event: AWSLambda.APIGatewayEvent): Promise<IResponse> =>
const chain = event.pathParameters?.chain?.toLowerCase().replace(/%20/g, " ");
const bridgeNetworkId = event.queryStringParameters?.id;
const response = await getBridgeVolume(chain, bridgeNetworkId);
try {
await sql.end();
} catch (e) {
console.error(e);
}
return successResponse(response, 10 * 60); // 10 mins cache
};

Expand Down
6 changes: 6 additions & 0 deletions src/handlers/getBridges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import getAggregatedDataClosestToTimestamp from "../utils/getRecordClosestToTime
import bridgeNetworks from "../data/bridgeNetworkData";
import { normalizeChain } from "../utils/normalizeChain";
import { getLast24HVolume } from "../utils/wrappa/postgres/query";
import { sql } from "../utils/db";

const getBridges = async () => {
const response = (
Expand Down Expand Up @@ -126,6 +127,11 @@ const handler = async (event: AWSLambda.APIGatewayEvent): Promise<IResponse> =>
const chainData = await craftBridgeChainsResponse();
response.chains = chainData;
}
try {
await sql.end();
} catch (e) {
console.error(e);
}
return successResponse(response, 10 * 60); // 10 mins cache
};

Expand Down
6 changes: 6 additions & 0 deletions src/handlers/getLargeTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getLlamaPrices } from "../utils/prices";
import { transformTokens } from "../helpers/tokenMappings";
import { importBridgeNetwork } from "../data/importBridgeNetwork";
import { normalizeChain } from "../utils/normalizeChain";
import { sql } from "../utils/db";

const getLargeTransactions = async (
chain: string = "all",
Expand Down Expand Up @@ -71,6 +72,11 @@ const handler = async (event: AWSLambda.APIGatewayEvent): Promise<IResponse> =>
const startTimestamp = event.queryStringParameters?.starttimestamp;
const endTimestamp = event.queryStringParameters?.endtimestamp;
const response = await getLargeTransactions(chain, startTimestamp, endTimestamp);
try {
await sql.end();
} catch (e) {
console.error(e);
}
return successResponse(response, 10 * 60); // 10 mins cache
};

Expand Down
7 changes: 5 additions & 2 deletions src/handlers/getLastBlocks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
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";

Expand All @@ -26,6 +24,11 @@ const handler = async () => {
acc[`${bridgeConfigById[bridgeId].bridge_name}-${bridgeConfigById[bridgeId].chain}`] = lastRecordedBlocks[bridgeId];
return acc;
}, {});
try {
await sql.end();
} catch (e) {
console.error(e);
}
return successResponse(lastBlocksByName);
};

Expand Down
6 changes: 6 additions & 0 deletions src/handlers/getTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import wrap from "../utils/wrap";
import { queryTransactionsTimestampRangeByBridgeNetwork } from "../utils/wrappa/postgres/query";
import { importBridgeNetwork } from "../data/importBridgeNetwork";
import { normalizeChain } from "../utils/normalizeChain";
import { sql } from "../utils/db";

const maxResponseTxs = 6000; // maximum number of transactions to return

Expand Down Expand Up @@ -86,6 +87,11 @@ const handler = async (event: AWSLambda.APIGatewayEvent): Promise<IResponse> =>
const address = event.queryStringParameters?.address?.toLowerCase();
const limit = event.queryStringParameters?.limit;
const response = await getTransactions(startTimestamp, endTimestamp, id, chain, sourceChain, address, limit);
try {
await sql.end();
} catch (e) {
console.error(e);
}
return successResponse(response, 10 * 60); // 10 mins cache
};

Expand Down
6 changes: 6 additions & 0 deletions src/handlers/runAggregateAllAdapters.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { wrapScheduledLambda } from "../utils/wrap";
import { convertToUnixTimestamp } from "../utils/date";
import { runAggregateDataAllAdapters } from "../utils/aggregate";
import { sql } from "../utils/db";

export default wrapScheduledLambda(async (_event) => {
const currentDate = new Date();
Expand All @@ -10,4 +11,9 @@ export default wrapScheduledLambda(async (_event) => {
if (currentHour === 0) {
await runAggregateDataAllAdapters(currentTimestamp, false);
}
try {
await sql.end();
} catch (e) {
console.error(e);
}
});
5 changes: 5 additions & 0 deletions src/handlers/runAllAdapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,9 @@ export default wrapScheduledLambda(async (event) => {
lastRecordedBlocks: lastRecordedBlocks[0].result,
});
}
try {
await sql.end();
} catch (e) {
console.error(e);
}
});
6 changes: 6 additions & 0 deletions src/handlers/runAllAdaptersHistorical.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { wrapScheduledLambda } from "../utils/wrap";
import bridgeNetworks from "../data/bridgeNetworkData";
import { LambdaClient, InvokeCommand } from "@aws-sdk/client-lambda";
import { sql } from "../utils/db";

const lambdaClient = new LambdaClient({});

Expand Down Expand Up @@ -42,6 +43,11 @@ const handler = async (_event: any) => {
}

console.log("Initiated historical runs for all adapters");
try {
await sql.end();
} catch (e) {
console.error(e);
}
};

export default wrapScheduledLambda(handler);

0 comments on commit a370dc5

Please sign in to comment.