Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c87e448

Browse files
committed
upgrade get all events to v5 SDK
1 parent 07b29de commit c87e448

File tree

1 file changed

+47
-14
lines changed

1 file changed

+47
-14
lines changed

src/server/routes/contract/events/getAllEvents.ts

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1-
import { Static, Type } from "@sinclair/typebox";
2-
import { FastifyInstance } from "fastify";
1+
import { Type, type Static } from "@sinclair/typebox";
2+
import type { AbiEvent } from "abitype";
3+
import type { FastifyInstance } from "fastify";
34
import { StatusCodes } from "http-status-codes";
4-
import { getContract } from "../../../../utils/cache/getContract";
5+
import {
6+
getAddress,
7+
getContract,
8+
getContractEvents,
9+
prepareEvent,
10+
} from "thirdweb";
11+
import { resolveContractAbi } from "thirdweb/contract";
12+
import { getChain } from "../../../../utils/chain";
13+
import { thirdwebClient } from "../../../../utils/sdk";
14+
import { createCustomError } from "../../../middleware/error";
515
import {
616
contractEventSchema,
717
eventsQuerystringSchema,
@@ -78,23 +88,46 @@ export async function getAllEvents(fastify: FastifyInstance) {
7888
},
7989
},
8090
handler: async (request, reply) => {
81-
const { chain, contractAddress } = request.params;
91+
const { chain: chainSlug, contractAddress } = request.params;
8292
const { fromBlock, toBlock, order } = request.query;
8393

84-
const chainId = await getChainIdFromChain(chain);
85-
const contract = await getContract({
86-
chainId,
87-
contractAddress,
88-
});
94+
const chainId = await getChainIdFromChain(chainSlug);
95+
const chain = await getChain(chainId);
8996

90-
let returnData = await contract.events.getAllEvents({
91-
fromBlock,
92-
toBlock,
93-
order,
97+
const contract = getContract({
98+
address: getAddress(contractAddress),
99+
chain,
100+
client: thirdwebClient,
94101
});
95102

103+
const abi: AbiEvent[] = await resolveContractAbi(contract);
104+
105+
const eventSignatures = abi.filter((item) => item.type === "event");
106+
if (eventSignatures.length === 0) {
107+
throw createCustomError(
108+
"No events found in contract or could not resolve contract ABI",
109+
StatusCodes.BAD_REQUEST,
110+
"NO_EVENTS_FOUND",
111+
);
112+
}
113+
114+
const preparedEvents = eventSignatures.map((signature) =>
115+
prepareEvent({ signature }),
116+
);
117+
let events: any;
118+
try {
119+
events = await getContractEvents({
120+
contract,
121+
fromBlock: fromBlock ? BigInt(fromBlock) : 0n,
122+
toBlock: toBlock ? BigInt(toBlock) : "latest",
123+
events: preparedEvents,
124+
});
125+
} catch (e) {
126+
console.error(e);
127+
}
128+
96129
reply.status(StatusCodes.OK).send({
97-
result: returnData,
130+
result: events,
98131
});
99132
},
100133
});

0 commit comments

Comments
 (0)