Skip to content

Commit 1dbbfb1

Browse files
authored
chore: Return 4xx if invalid contract or chain in API call (#527)
* chore: Return 4xx if invalid contract or chain in API call * fix await
1 parent b6266e4 commit 1dbbfb1

File tree

3 files changed

+39
-19
lines changed

3 files changed

+39
-19
lines changed

src/server/routes/backend-wallet/transfer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ export async function transfer(fastify: FastifyInstance) {
9090

9191
const balance = await sdk.getBalance(walletAddress);
9292

93-
if (balance.value.lt(normalizedValue)) {
93+
if (balance.value.lte(normalizedValue)) {
9494
throw createCustomError(
9595
"Insufficient balance",
96-
StatusCodes.BAD_GATEWAY,
96+
StatusCodes.BAD_REQUEST,
9797
"INSUFFICIENT_BALANCE",
9898
);
9999
}

src/server/utils/chain.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ import {
33
getChainByChainIdAsync,
44
getChainBySlugAsync,
55
} from "@thirdweb-dev/chains";
6+
import { StatusCodes } from "http-status-codes";
67
import { getConfig } from "../../utils/cache/getConfig";
78
import { networkResponseSchema } from "../../utils/cache/getSdk";
89
import { logger } from "../../utils/logger";
10+
import { createCustomError } from "../middleware/error";
911

1012
/**
1113
* Given a valid chain name ('Polygon') or ID ('137'), return the numeric chain ID.
14+
*
15+
* @throws if the chain is invalid or deprecated.
1216
*/
1317
export const getChainIdFromChain = async (input: string): Promise<number> => {
1418
const inputSlug = input.toLowerCase();
@@ -40,21 +44,27 @@ export const getChainIdFromChain = async (input: string): Promise<number> => {
4044
}
4145
}
4246

43-
if (!isNaN(inputId)) {
44-
// Fetch by chain ID.
45-
const chainData = await getChainByChainIdAsync(inputId);
46-
if (chainData && chainData.status !== "deprecated") {
47-
return chainData.chainId;
48-
}
49-
} else {
50-
// Fetch by chain name.
51-
const chainData = await getChainBySlugAsync(inputSlug);
52-
if (chainData && chainData.status !== "deprecated") {
53-
return chainData.chainId;
47+
// Fetch by chain ID or slug.
48+
// Throw if the chain is invalid or deprecated.
49+
try {
50+
const chain = !isNaN(inputId)
51+
? await getChainByChainIdAsync(inputId)
52+
: await getChainBySlugAsync(inputSlug);
53+
54+
if (chain.status === "deprecated") {
55+
throw createCustomError(
56+
`Chain ${input} is deprecated`,
57+
StatusCodes.BAD_REQUEST,
58+
"INVALID_CHAIN",
59+
);
5460
}
55-
}
5661

57-
throw new Error(
58-
`Invalid or deprecated chain. Please confirm this is a valid chain: https://thirdweb.com/${input}`,
59-
);
62+
return chain.chainId;
63+
} catch (e) {
64+
throw createCustomError(
65+
`Chain ${input} is not found`,
66+
StatusCodes.BAD_REQUEST,
67+
"INVALID_CHAIN",
68+
);
69+
}
6070
};

src/utils/cache/getContract.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { StatusCodes } from "http-status-codes";
2+
import { createCustomError } from "../../server/middleware/error";
13
import { getSdk } from "./getSdk";
24

35
interface GetContractParams {
@@ -15,6 +17,14 @@ export const getContract = async ({
1517
}: GetContractParams) => {
1618
const sdk = await getSdk({ chainId, walletAddress, accountAddress });
1719

18-
// We don't need to maintain cache for contracts because sdk handles it already
19-
return sdk.getContract(contractAddress);
20+
try {
21+
// SDK already handles caching.
22+
return await sdk.getContract(contractAddress);
23+
} catch (e) {
24+
throw createCustomError(
25+
`Contract metadata could not be resolved: ${e}`,
26+
StatusCodes.BAD_REQUEST,
27+
"INVALID_CONTRACT",
28+
);
29+
}
2030
};

0 commit comments

Comments
 (0)