|
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"; |
3 | 4 | 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"; |
5 | 15 | import {
|
6 | 16 | contractEventSchema,
|
7 | 17 | eventsQuerystringSchema,
|
@@ -78,23 +88,46 @@ export async function getAllEvents(fastify: FastifyInstance) {
|
78 | 88 | },
|
79 | 89 | },
|
80 | 90 | handler: async (request, reply) => {
|
81 |
| - const { chain, contractAddress } = request.params; |
| 91 | + const { chain: chainSlug, contractAddress } = request.params; |
82 | 92 | const { fromBlock, toBlock, order } = request.query;
|
83 | 93 |
|
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); |
89 | 96 |
|
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, |
94 | 101 | });
|
95 | 102 |
|
| 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 | + |
96 | 129 | reply.status(StatusCodes.OK).send({
|
97 |
| - result: returnData, |
| 130 | + result: events, |
98 | 131 | });
|
99 | 132 | },
|
100 | 133 | });
|
|
0 commit comments