I want to query all the events of my package but in provided time Range. How to solve this?
import { SuiClient } from "@mysten/sui.js/client";
async function fetchEventsByTimeRangeAndEventTypes(
limit: number = 50
) {
const rpcUrl = "";
const client = new SuiClient({ url: rpcUrl });
let allEvents: any[] = [];
let cursor: { txDigest: string; eventSeq: string } | null = null;
let iterations = 0;
const maxIterations = 1000;
try {
console.log(`Starting event fetch at ${new Date().toISOString()}`);
while (true) {
iterations++;
if (iterations > maxIterations) {
console.warn("Maximum iterations reached (1000) - stopping pagination");
break;
}
const response = await client.queryEvents({
query: {
MoveEventModule: {
module: "events",
package: "0x0e3c1f1104d1d413ad884ed6d136a70c78565d83b5a65a07cb1f1e86aab48f36",
},
TimeRange: {
startTime: "1729775849019",
endTime: "1729775849519",
}
},
cursor: cursor,
limit: limit,
order: "ascending",
});
console.log(`Page ${iterations}: Found ${response.data.length} events`);
allEvents.push(...response.data);
if (!response.hasNextPage || !response.nextCursor) {
console.log("No more pages available");
break;
}
cursor = response.nextCursor;
}
console.log(`Completed. Total events fetched: ${allEvents.length}`);
return allEvents;
} catch (error) {
console.error("Error fetching events:", error);
throw error;
}
}
// Example usage
(async () => {
const eventTypes = [
"0x0e3c1f1104d1d413ad884ed6d136a70c78565d83b5a65a07cb1f1e86aab48f36::events::FundsPaid",
"0x0e3c1f1104d1d413ad884ed6d136a70c78565d83b5a65a07cb1f1e86aab48f36::events::FundsDeposited",
];
const events = await fetchEventsByTimeRangeAndEventTypes();
console.log("Events:", events);
})();