You can query Flow for a list of events in a specific range by creating following helper method:
const getEvents = async (params) => {
// Define event type from params
const { contractAddress, contractName, eventName } = params;
const eventType = `A.${contractAddress}.${contractName}.${eventName}`;
const { from = 0, to } = params;
let toBlock;
if (to === undefined) {
// Get latest block
const blockResponse = await fcl.send(
await sdk.build([sdk.getLatestBlock()])
);
toBlock = blockResponse.latestBlock.height;
} else {
toBlock = to;
}
const response = await fcl.send(
await sdk.build([sdk.getEvents(eventType, from, toBlock)])
);
// Return a list of events
return response.events;
};
And then use it, for example, like this:
const getHelloEvents = async () => {
const events = await getEvents({
contractName: "HelloWorld",
contractAddress: "01cf0e2f2f715450", // note the address is without "0x" prefix
eventName: "CustomEvent",
});
console.log({ events });
};
I’ve also updated the repository, which this method and others to help you deploy contract, send transactions and execute scripts on Flow. You can reference it here: