Skip to content

Commit 64fd8e5

Browse files
authored
Merge pull request #666 from gitcoinco/log-ip
Log ip
2 parents b6eb77b + b79efd4 commit 64fd8e5

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import type { EventHandlerContext } from "./indexer/indexer.js";
3939
import { handleEvent as handleAlloV1Event } from "./indexer/allo/v1/handleEvent.js";
4040
import { handleEvent as handleAlloV2Event } from "./indexer/allo/v2/handleEvent.js";
4141
import { Database } from "./database/index.js";
42-
import { decodeJsonWithBigInts } from "./utils/index.js";
42+
import { decodeJsonWithBigInts, getExternalIP } from "./utils/index.js";
4343
import { Block } from "chainsauce/dist/cache.js";
4444
import { createPublicClient, http } from "viem";
4545
import { IndexerEvents } from "chainsauce/dist/indexer.js";
@@ -121,6 +121,8 @@ async function main(): Promise<void> {
121121
return decodeJsonWithBigInts(val);
122122
});
123123

124+
await getExternalIP(baseLogger);
125+
124126
if (config.cacheDir) {
125127
if (config.removeCache) {
126128
await fs.rm(config.cacheDir, { recursive: true });

src/utils/index.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// TODO: why is eslint not recognizing type narrowing?
22
/* eslint-disable @typescript-eslint/no-unsafe-argument */
3+
4+
import { Logger } from "pino";
5+
36
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
47
export function encodeJsonWithBigInts(value: unknown): string {
58
return JSON.stringify(value, (_key, value) => {
@@ -31,3 +34,28 @@ export const UINT64_MAX = 18446744073709551615n;
3134
export const getDateFromTimestamp = (timestamp: bigint): Date | null => {
3235
return timestamp < UINT64_MAX ? new Date(Number(timestamp) * 1000) : null;
3336
};
37+
38+
export const getExternalIP = async (logger: Logger): Promise<string> => {
39+
const urls = ["https://api.ipify.org?format=json", "http://ipinfo.io/json"];
40+
for (const url of urls) {
41+
try {
42+
logger.debug(`Attempting to fetch IP address from: ${url}`);
43+
const response = await fetch(url);
44+
if (response.ok) {
45+
const { ip } = (await response.json()) as { ip: string };
46+
logger.info(`Successfully fetched IP address: ${ip}`);
47+
return ip;
48+
}
49+
throw new Error(`Request failed with status: ${response.status}`);
50+
} catch (error) {
51+
if (error instanceof Error) {
52+
logger.error(`Failed to fetch from ${url}: ${error.message}`);
53+
} else {
54+
logger.error(`Failed to fetch from ${url}`);
55+
}
56+
}
57+
}
58+
throw new Error(
59+
"Unable to fetch external IP address from both primary and fallback URLs."
60+
);
61+
};

0 commit comments

Comments
 (0)