Skip to content

Commit ec6da15

Browse files
authored
fix: Assert valid contract when adding contract subscription (#616)
1 parent 1ea498d commit ec6da15

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/server/routes/contract/subscriptions/addContractSubscription.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import { Static, Type } from "@sinclair/typebox";
22
import { FastifyInstance } from "fastify";
33
import { StatusCodes } from "http-status-codes";
4+
import { getContract } from "thirdweb";
5+
import { isContractDeployed } from "thirdweb/utils";
46
import { upsertChainIndexer } from "../../../../db/chainIndexers/upsertChainIndexer";
57
import { createContractSubscription } from "../../../../db/contractSubscriptions/createContractSubscription";
68
import { getContractSubscriptionsUniqueChainIds } from "../../../../db/contractSubscriptions/getContractSubscriptions";
79
import { insertWebhook } from "../../../../db/webhooks/createWebhook";
810
import { WebhooksEventTypes } from "../../../../schema/webhooks";
911
import { getSdk } from "../../../../utils/cache/getSdk";
12+
import { getChain } from "../../../../utils/chain";
13+
import { thirdwebClient } from "../../../../utils/sdk";
1014
import { createCustomError } from "../../../middleware/error";
1115
import {
1216
contractSubscriptionSchema,
@@ -104,6 +108,21 @@ export async function addContractSubscription(fastify: FastifyInstance) {
104108
);
105109
}
106110

111+
// Assert a valid contract.
112+
const contract = getContract({
113+
client: thirdwebClient,
114+
chain: await getChain(chainId),
115+
address: contractAddress,
116+
});
117+
const isValid = await isContractDeployed(contract);
118+
if (!isValid) {
119+
throw createCustomError(
120+
"Invalid contract.",
121+
StatusCodes.BAD_REQUEST,
122+
"INVALID_CONTRACT",
123+
);
124+
}
125+
107126
// If not currently indexed, upsert the latest block number.
108127
const subscribedChainIds = await getContractSubscriptionsUniqueChainIds();
109128
if (!subscribedChainIds.includes(chainId)) {

0 commit comments

Comments
 (0)