Skip to content

Commit c49aa2a

Browse files
committed
fix: handle blockTags for get-all-events
1 parent 110c782 commit c49aa2a

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

src/server/routes/contract/events/get-all-events.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ import { getChain } from "../../../../shared/utils/chain";
1414
import { getChainIdFromChain } from "../../../utils/chain";
1515
import { getContract, getContractEvents } from "thirdweb";
1616
import { maybeBigInt } from "../../../../shared/utils/primitive-types";
17-
import { toContractEventV4Schema } from "../../../schemas/event";
17+
import {
18+
ContractEventV5,
19+
toContractEventV4Schema,
20+
} from "../../../schemas/event";
21+
import { createCustomError } from "../../../middleware/error";
22+
import { prettifyError } from "../../../../shared/utils/error";
1823

1924
const requestSchema = contractParamSchema;
2025

@@ -93,11 +98,21 @@ export async function getAllEvents(fastify: FastifyInstance) {
9398
chain: await getChain(chainId),
9499
});
95100

96-
const eventsV5 = await getContractEvents({
97-
contract: contract,
98-
fromBlock: maybeBigInt(fromBlock?.toString()),
99-
toBlock: maybeBigInt(toBlock?.toString()),
100-
});
101+
let eventsV5: ContractEventV5[];
102+
try {
103+
eventsV5 = await getContractEvents({
104+
contract: contract,
105+
fromBlock:
106+
typeof fromBlock === "number" ? BigInt(fromBlock) : fromBlock,
107+
toBlock: typeof toBlock === "number" ? BigInt(toBlock) : toBlock,
108+
});
109+
} catch (e) {
110+
throw createCustomError(
111+
`Failed to get events: ${prettifyError(e)}`,
112+
StatusCodes.BAD_REQUEST,
113+
"BAD_REQUEST",
114+
);
115+
}
101116

102117
reply.status(StatusCodes.OK).send({
103118
result: eventsV5.map(toContractEventV4Schema).sort((a, b) => {

src/server/schemas/contract/index.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,25 @@ export const rolesResponseSchema = Type.Object({
7979
signer: Type.Array(Type.String()),
8080
});
8181

82+
export const blockNumberOrTagSchema = Type.Union([
83+
Type.Integer({ minimum: 0 }),
84+
Type.Literal("latest"),
85+
Type.Literal("earliest"),
86+
Type.Literal("pending"),
87+
Type.Literal("safe"),
88+
Type.Literal("finalized"),
89+
]);
90+
8291
export const eventsQuerystringSchema = Type.Object(
8392
{
84-
fromBlock: Type.Optional(
85-
Type.Union([Type.Integer({ minimum: 0 }), Type.String()], {
86-
default: "0",
87-
}),
88-
),
89-
toBlock: Type.Optional(
90-
Type.Union([Type.Integer({ minimum: 0 }), Type.String()], {
91-
default: "latest",
92-
}),
93-
),
93+
fromBlock: Type.Optional({
94+
...blockNumberOrTagSchema,
95+
default: "0",
96+
}),
97+
toBlock: Type.Optional({
98+
...blockNumberOrTagSchema,
99+
default: "latest",
100+
}),
94101
order: Type.Optional(
95102
Type.Union([Type.Literal("asc"), Type.Literal("desc")], {
96103
default: "desc",

0 commit comments

Comments
 (0)