Skip to content

Commit a828191

Browse files
authored
fix: handle blockTags for get-all-events (#840)
* fix: handle blockTags for get-all-events * fix lint
1 parent 110c782 commit a828191

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

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

+21-7
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ import { thirdwebClient } from "../../../../shared/utils/sdk";
1313
import { getChain } from "../../../../shared/utils/chain";
1414
import { getChainIdFromChain } from "../../../utils/chain";
1515
import { getContract, getContractEvents } from "thirdweb";
16-
import { maybeBigInt } from "../../../../shared/utils/primitive-types";
17-
import { toContractEventV4Schema } from "../../../schemas/event";
16+
import {
17+
type ContractEventV5,
18+
toContractEventV4Schema,
19+
} from "../../../schemas/event";
20+
import { createCustomError } from "../../../middleware/error";
21+
import { prettifyError } from "../../../../shared/utils/error";
1822

1923
const requestSchema = contractParamSchema;
2024

@@ -93,11 +97,21 @@ export async function getAllEvents(fastify: FastifyInstance) {
9397
chain: await getChain(chainId),
9498
});
9599

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

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

src/server/schemas/contract/index.ts

+17-10
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",

src/shared/utils/transaction/insert-transaction.ts

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
WalletDetailsError,
88
type ParsedWalletDetails,
99
} from "../../../shared/db/wallets/get-wallet-details";
10-
import { doesChainSupportService } from "../../lib/chain/chain-capabilities";
1110
import { createCustomError } from "../../../server/middleware/error";
1211
import { SendTransactionQueue } from "../../../worker/queues/send-transaction-queue";
1312
import { getChecksumAddress } from "../primitive-types";

0 commit comments

Comments
 (0)