fix: use real totalCount from Subsquid instead of capped fetch limit

Add totalCount to the GraphQL query so transactionCount reflects the
true number of transfers in the period, not the 2000-item fetch cap.
This also fixes the average Ğ1/tx calculation.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
syoul
2026-03-22 18:40:07 +01:00
parent 2674b3891b
commit a6fc4a534f
2 changed files with 20 additions and 11 deletions

View File

@@ -37,10 +37,9 @@ async function fetchLiveTransactions(periodDays: number): Promise<{
totalCount: number;
totalVolume: number;
}> {
const rawTransfers = await fetchTransfers(periodDays);
const { transfers: rawTransfers, totalCount } = await fetchTransfers(periodDays);
if (rawTransfers.length === 0) return { geolocated: [], totalCount: 0, totalVolume: 0 };
const totalCount = rawTransfers.length;
const totalVolume = rawTransfers.reduce((s, t) => s + t.amount, 0);
// Carte SS58 courant → clé Duniter (= _id Cesium+)

View File

@@ -33,6 +33,7 @@ const SubsquidTransferNodeSchema = z.object({
const SubsquidResponseSchema = z.object({
data: z.object({
transfers: z.object({
totalCount: z.number().int(),
nodes: z.array(SubsquidTransferNodeSchema),
}),
}),
@@ -62,6 +63,7 @@ const TRANSFERS_QUERY = `
first: $limit
filter: { timestamp: { greaterThanOrEqualTo: $since } }
) {
totalCount
nodes {
id
blockNumber
@@ -151,10 +153,15 @@ export async function buildIdentityKeyMap(): Promise<Map<string, string>> {
return result;
}
export interface FetchTransfersResult {
transfers: RawTransfer[];
totalCount: number;
}
export async function fetchTransfers(
periodDays: number,
limit = 2000
): Promise<RawTransfer[]> {
): Promise<FetchTransfersResult> {
const since = new Date(
Date.now() - periodDays * 24 * 60 * 60 * 1000
).toISOString();
@@ -180,12 +187,15 @@ export async function fetchTransfers(
const parsed = SubsquidResponseSchema.parse(raw);
return parsed.data.transfers.nodes.map((node) => ({
id: node.id,
timestamp: new Date(node.timestamp).getTime(),
amount: parseInt(node.amount, 10) / 100,
fromId: node.fromId ?? '',
toId: node.toId ?? '',
fromName: node.from?.linkedIdentity?.name ?? '',
}));
return {
totalCount: parsed.data.transfers.totalCount,
transfers: parsed.data.transfers.nodes.map((node) => ({
id: node.id,
timestamp: new Date(node.timestamp).getTime(),
amount: parseInt(node.amount, 10) / 100,
fromId: node.fromId ?? '',
toId: node.toId ?? '',
fromName: node.from?.linkedIdentity?.name ?? '',
})),
};
}