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
37 changes: 22 additions & 15 deletions api.apollo.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@ 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;
return fallbackUntil && Date.now() < fallbackUntil
? CONFIG.indexerFallback
: CONFIG.indexer;
}

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

const errorLink = onError(({ graphQLErrors, networkError, operation }) => {
const errorLink = onError(({ graphQLErrors, networkError, operation, forward }) => {
if (graphQLErrors) {
graphQLErrors.forEach((error) => {
logger.error(`[GraphQL error in operation: ${operation?.operationName || 'unknown'}]`, {
Expand All @@ -32,35 +31,43 @@ const errorLink = onError(({ graphQLErrors, networkError, operation }) => {
});
});
}

if (networkError) {
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();
if (getIndexerUrl() === CONFIG.indexer) {
const is503 =
(networkError as any)?.response?.status === 503 ||
(networkError as any)?.statusCode === 503 ||
(networkError as any)?.result?.status === 503;

if (is503) {
logger.log('[Ponder] 503 Service Unavailable - Ponder is syncing, switching to fallback');
} else {
logger.log('[Ponder] Network error detected, activating fallback');
}
activateFallback();
return forward(operation);
}
}
});

const httpLink = createHttpLink({
uri: getIndexerUrl,
uri: () => getIndexerUrl(),
fetch: (uri: RequestInfo | URL, options?: RequestInit) => {
const controller = new AbortController();
const timeout = setTimeout(() => {
controller.abort();
}, 10000); // 10 second timeout
}, 10000);

return fetch(uri, {
...options,
signal: controller.signal,
})
.catch((error) => {
// Activate fallback on http errors
if (getIndexerUrl() === CONFIG.indexer) activateFallback();
throw error;
})
.finally(() => {
clearTimeout(timeout);
});
Expand Down
6 changes: 3 additions & 3 deletions ecosystem/ecosystem.minter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ export class EcosystemMinterService {
return list;
}

async getRecentMints(timestamp: Date, minValue: bigint): Promise<EcosystemMintQueryItem[]> {
async getRecentMintsFromPositions(timestamp: Date, minValue: bigint): Promise<EcosystemMintQueryItem[]> {
const checkTimestamp = Math.trunc(timestamp.getTime() / 1000);

const mintsFetched = await PONDER_CLIENT.query({
fetchPolicy: 'no-cache',
query: gql`
query {
mints(
positionMints(
orderBy: "timestamp", orderDirection: "desc"
where: {
timestamp_gt: "${checkTimestamp}"
Expand All @@ -111,6 +111,6 @@ export class EcosystemMinterService {
}
`,
});
return mintsFetched?.data?.mints?.items ?? [];
return mintsFetched?.data?.positionMints?.items ?? [];
}
}
2 changes: 1 addition & 1 deletion socialmedia/socialmedia.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export class SocialMediaService {
const checkDate = new Date(this.socialMediaState.mintUpdates);
const minAmount = BigInt(MIN_MINTING_AMOUNT * 10 ** 18);

const requestedMints = await this.mints.getRecentMints(checkDate, minAmount);
const requestedMints = await this.mints.getRecentMintsFromPositions(checkDate, minAmount);

if (requestedMints.length > 0) {
this.socialMediaState.mintUpdates = Date.now();
Expand Down