|
1 | 1 | import { Static, Type } from "@sinclair/typebox";
|
2 | 2 | import { FastifyInstance } from "fastify";
|
3 | 3 | import { StatusCodes } from "http-status-codes";
|
| 4 | +import { getContract } from "thirdweb"; |
| 5 | +import { isContractDeployed } from "thirdweb/utils"; |
4 | 6 | import { upsertChainIndexer } from "../../../../db/chainIndexers/upsertChainIndexer";
|
5 | 7 | import { createContractSubscription } from "../../../../db/contractSubscriptions/createContractSubscription";
|
6 | 8 | import { getContractSubscriptionsUniqueChainIds } from "../../../../db/contractSubscriptions/getContractSubscriptions";
|
7 | 9 | import { insertWebhook } from "../../../../db/webhooks/createWebhook";
|
8 | 10 | import { WebhooksEventTypes } from "../../../../schema/webhooks";
|
9 | 11 | import { getSdk } from "../../../../utils/cache/getSdk";
|
| 12 | +import { getChain } from "../../../../utils/chain"; |
| 13 | +import { thirdwebClient } from "../../../../utils/sdk"; |
10 | 14 | import { createCustomError } from "../../../middleware/error";
|
11 | 15 | import {
|
12 | 16 | contractSubscriptionSchema,
|
@@ -104,6 +108,21 @@ export async function addContractSubscription(fastify: FastifyInstance) {
|
104 | 108 | );
|
105 | 109 | }
|
106 | 110 |
|
| 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 | + |
107 | 126 | // If not currently indexed, upsert the latest block number.
|
108 | 127 | const subscribedChainIds = await getContractSubscriptionsUniqueChainIds();
|
109 | 128 | if (!subscribedChainIds.includes(chainId)) {
|
|
0 commit comments