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

Improve Etherscan data fetching reliability #150

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
22 changes: 17 additions & 5 deletions app/api/ethereum-data/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NextResponse } from 'next/server';

const CACHE_EXPIRATION_MS = 10000; // 10 seconds cache

interface ICache {
interface ICache {
blockNumber: number | null;
gasPrice: number | null;
ethPrice: number | null;
Expand All @@ -22,8 +22,8 @@ export async function GET() {
const apiKey = process.env.ETHERSCAN_API_KEY || "";
const etherscanAddress = process.env.NEXT_PUBLIC_ETHERSCAN_ADDRESS;

if (!apiKey) {
return NextResponse.json({ error: 'API key is not configured' }, { status: 500 });
if (!apiKey || !etherscanAddress) {
return NextResponse.json({ error: 'API key or Etherscan address is not configured' }, { status: 500 });
}

const now = new Date().getTime();
Expand All @@ -40,13 +40,22 @@ export async function GET() {
try {
isFetching = true;
console.log("Fetching new data from Etherscan");

const [blockResponse, gasResponse, priceResponse] = await Promise.all([
fetch(`${etherscanAddress}?module=proxy&action=eth_blockNumber&apikey=${apiKey}`, {cache: "no-store"}),
fetch(`${etherscanAddress}?module=proxy&action=eth_gasPrice&apikey=${apiKey}`, {cache: "no-store"}),
fetch(`${etherscanAddress}?module=stats&action=ethprice&apikey=${apiKey}`, {cache: "no-store"})
]);

if (!blockResponse.ok || !gasResponse.ok || !priceResponse.ok) {
const failedRequests = [
!blockResponse.ok && 'block',
!gasResponse.ok && 'gas',
!priceResponse.ok && 'price'
].filter(Boolean);
throw new Error(`API requests failed for: ${failedRequests.join(', ')}`);
}

const blockData = await blockResponse.json();
const gasData = await gasResponse.json();
const priceData = await priceResponse.json();
Expand All @@ -70,7 +79,10 @@ export async function GET() {
return NextResponse.json(cache);
} catch (error) {
console.error('Error fetching Ethereum data:', error);
return NextResponse.json(cache || { error: 'Failed to fetch Ethereum data' }, { status: 500 });
if (cache.timestamp) {
return NextResponse.json(cache);
}
return NextResponse.json({ error: 'Failed to fetch Ethereum data' }, { status: 500 });
} finally {
isFetching = false;
}
Expand Down
11 changes: 10 additions & 1 deletion app/api/get-transactions/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ import { NextResponse } from 'next/server';
const BRIDGE_CONTRACT_ADDRESS = process.env.NEXT_PUBLIC_BRIDGE_CONTRACT || '';
const API_KEY = process.env.ETHERSCAN_API_KEY || '';

interface EtherscanTransaction {
to: string;
from: string;
hash: string;
blockNumber: string;
timeStamp: string;
value: string;
}

export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
const address = searchParams.get('address');
Expand All @@ -21,7 +30,7 @@ export async function GET(request: Request) {
}

const deposits = data.result
.filter((tx: any) => tx.to.toLowerCase() === BRIDGE_CONTRACT_ADDRESS.toLowerCase());
.filter((tx: EtherscanTransaction) => tx.to.toLowerCase() === BRIDGE_CONTRACT_ADDRESS.toLowerCase());

return NextResponse.json(deposits);
} catch (error) {
Expand Down