Skip to content
Merged
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
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ PORT=3000

CONFIG_APP_URL=https://app.deuro.com
CONFIG_INDEXER_URL=https://ponder.deuro.com
CONFIG_INDEXER_FALLBACK_URL=https://dev.ponder.deuro.com
CONFIG_CHAIN=mainnet

RPC_URL_MAINNET=https://eth-mainnet.g.alchemy.com/v2/[API-KEY]
Expand Down
40 changes: 34 additions & 6 deletions api.apollo.config.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,51 @@
import { ApolloClient, ApolloLink, createHttpLink, InMemoryCache } from '@apollo/client/core';
import { onError } from '@apollo/client/link/error';
import { Logger } from '@nestjs/common';
import fetch from 'cross-fetch';
import { CONFIG } from './api.config';

const logger = new Logger('ApiApolloConfig');

// Fallback URL management
let fallbackUntil: number | null = null;

function getIndexerUrl(): string {
if (fallbackUntil && Date.now() < fallbackUntil) return CONFIG.indexerFallback;
if (fallbackUntil) fallbackUntil = null; // Reset expired fallback
return CONFIG.indexer;
}

function activateFallback(): void {
if (!fallbackUntil) {
fallbackUntil = Date.now() + 10 * 60 * 1000; // 10 minutes
logger.log(`[Ponder] Switching to fallback for 10min: ${CONFIG.indexerFallback}`);
}
}

const errorLink = onError(({ graphQLErrors, networkError, operation }) => {
if (graphQLErrors) {
graphQLErrors.forEach((error) => {
console.error(`[GraphQL error in operation: ${operation?.operationName || 'unknown'}]`, {
logger.error(`[GraphQL error in operation: ${operation?.operationName || 'unknown'}]`, {
message: error.message,
locations: error.locations,
path: error.path,
});
});
}
if (networkError) {
console.error(`[Network error in operation: ${operation?.operationName || 'unknown'}]`, {
logger.error(`[Network error in operation: ${operation?.operationName || 'unknown'}]`, {
message: networkError.message,
name: networkError.name,
stack: networkError.stack,
});

// Activate fallback on network errors
if (getIndexerUrl() === CONFIG.indexer) activateFallback();
}
});

const httpLink = createHttpLink({
uri: CONFIG.indexer,
uri: getIndexerUrl,
fetch: (uri: RequestInfo | URL, options?: RequestInit) => {
const controller = new AbortController();
const timeout = setTimeout(() => {
Expand All @@ -33,9 +55,15 @@ const httpLink = createHttpLink({
return fetch(uri, {
...options,
signal: controller.signal,
}).finally(() => {
clearTimeout(timeout);
});
})
.catch((error) => {
// Activate fallback on http errors
if (getIndexerUrl() === CONFIG.indexer) activateFallback();
throw error;
})
.finally(() => {
clearTimeout(timeout);
});
},
});

Expand Down
8 changes: 6 additions & 2 deletions api.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Chain, createPublicClient, http } from 'viem';
import { mainnet, polygon } from 'viem/chains';

import { Logger } from '@nestjs/common';
import * as dotenv from 'dotenv';
dotenv.config();

Expand All @@ -13,6 +14,7 @@ if (process.env.COINGECKO_API_KEY === undefined) throw new Error('COINGECKO_API_
export type ConfigType = {
app: string;
indexer: string;
indexerFallback: string;
coingeckoApiKey: string;
chain: Chain;
network: {
Expand All @@ -38,6 +40,7 @@ export type ConfigType = {
export const CONFIG: ConfigType = {
app: process.env.CONFIG_APP_URL || 'https://app.deuro.com',
indexer: process.env.CONFIG_INDEXER_URL || 'https://ponder.deuro.com/',
indexerFallback: process.env.CONFIG_INDEXER_FALLBACK_URL || 'https://dev.ponder.deuro.com/',
coingeckoApiKey: process.env.COINGECKO_API_KEY,
chain: process.env.CONFIG_CHAIN === 'polygon' ? polygon : mainnet, // @dev: default mainnet
network: {
Expand All @@ -60,8 +63,9 @@ export const CONFIG: ConfigType = {
};

// Start up message
console.log(`Starting API with this config:`);
console.log(CONFIG);
const logger = new Logger('ApiConfig');
logger.log(`Starting API with this config:`);
logger.log(CONFIG);

// Refer to https://github.com/yagop/node-telegram-bot-api/blob/master/doc/usage.md#sending-files
process.env.NTBA_FIX_350 = 'true';
Expand Down