Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log ip #666

Merged
merged 4 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import type { EventHandlerContext } from "./indexer/indexer.js";
import { handleEvent as handleAlloV1Event } from "./indexer/allo/v1/handleEvent.js";
import { handleEvent as handleAlloV2Event } from "./indexer/allo/v2/handleEvent.js";
import { Database } from "./database/index.js";
import { decodeJsonWithBigInts } from "./utils/index.js";
import { decodeJsonWithBigInts, getExternalIP } from "./utils/index.js";
import { Block } from "chainsauce/dist/cache.js";
import { createPublicClient, http } from "viem";
import { IndexerEvents } from "chainsauce/dist/indexer.js";
Expand Down Expand Up @@ -121,6 +121,8 @@ async function main(): Promise<void> {
return decodeJsonWithBigInts(val);
});

await getExternalIP(baseLogger);

if (config.cacheDir) {
if (config.removeCache) {
await fs.rm(config.cacheDir, { recursive: true });
Expand Down Expand Up @@ -689,4 +691,4 @@ function monitorAndLogResources(config: {
});

resourceMonitor.start();
}
}
28 changes: 28 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// TODO: why is eslint not recognizing type narrowing?
/* eslint-disable @typescript-eslint/no-unsafe-argument */

import { Logger } from "pino";

/* eslint-disable @typescript-eslint/no-unsafe-member-access */
export function encodeJsonWithBigInts(value: unknown): string {
return JSON.stringify(value, (_key, value) => {
Expand Down Expand Up @@ -31,3 +34,28 @@ export const UINT64_MAX = 18446744073709551615n;
export const getDateFromTimestamp = (timestamp: bigint): Date | null => {
return timestamp < UINT64_MAX ? new Date(Number(timestamp) * 1000) : null;
};

export const getExternalIP = async (logger: Logger): Promise<string> => {
const urls = ["https://api.ipify.org?format=json", "http://ipinfo.io/json"];
for (const url of urls) {
try {
logger.debug(`Attempting to fetch IP address from: ${url}`);
const response = await fetch(url);
if (response.ok) {
const { ip } = await response.json();
logger.info(`Successfully fetched IP address: ${ip}`);
return ip;
}
throw new Error(`Request failed with status: ${response.status}`);
} catch (error) {
if (error instanceof Error) {
logger.error(`Failed to fetch from ${url}: ${error.message}`);
} else {
logger.error(`Failed to fetch from ${url}: ${error}`);
}
}
}
throw new Error(
"Unable to fetch external IP address from both primary and fallback URLs."
);
};
Loading