From dde97b6953138b047e06d87837b1376755c43ae3 Mon Sep 17 00:00:00 2001 From: Phillip Ho Date: Thu, 16 May 2024 04:57:28 +0800 Subject: [PATCH 1/2] chore: Return 4xx if invalid contract or chain in API call --- src/server/utils/chain.ts | 40 +++++++++++++++++++++------------- src/utils/cache/getContract.ts | 14 ++++++++++-- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/server/utils/chain.ts b/src/server/utils/chain.ts index 29d6f8bb9..1e085a7a0 100644 --- a/src/server/utils/chain.ts +++ b/src/server/utils/chain.ts @@ -3,12 +3,16 @@ import { getChainByChainIdAsync, getChainBySlugAsync, } from "@thirdweb-dev/chains"; +import { StatusCodes } from "http-status-codes"; import { getConfig } from "../../utils/cache/getConfig"; import { networkResponseSchema } from "../../utils/cache/getSdk"; import { logger } from "../../utils/logger"; +import { createCustomError } from "../middleware/error"; /** * Given a valid chain name ('Polygon') or ID ('137'), return the numeric chain ID. + * + * @throws if the chain is invalid or deprecated. */ export const getChainIdFromChain = async (input: string): Promise => { const inputSlug = input.toLowerCase(); @@ -40,21 +44,27 @@ export const getChainIdFromChain = async (input: string): Promise => { } } - if (!isNaN(inputId)) { - // Fetch by chain ID. - const chainData = await getChainByChainIdAsync(inputId); - if (chainData && chainData.status !== "deprecated") { - return chainData.chainId; - } - } else { - // Fetch by chain name. - const chainData = await getChainBySlugAsync(inputSlug); - if (chainData && chainData.status !== "deprecated") { - return chainData.chainId; + // Fetch by chain ID or slug. + // Throw if the chain is invalid or deprecated. + try { + const chain = !isNaN(inputId) + ? await getChainByChainIdAsync(inputId) + : await getChainBySlugAsync(inputSlug); + + if (chain.status === "deprecated") { + throw createCustomError( + `Chain ${input} is deprecated`, + StatusCodes.BAD_REQUEST, + "INVALID_CHAIN", + ); } - } - throw new Error( - `Invalid or deprecated chain. Please confirm this is a valid chain: https://thirdweb.com/${input}`, - ); + return chain.chainId; + } catch (e) { + throw createCustomError( + `Chain ${input} is not found`, + StatusCodes.BAD_REQUEST, + "INVALID_CHAIN", + ); + } }; diff --git a/src/utils/cache/getContract.ts b/src/utils/cache/getContract.ts index 823796439..379973b1f 100644 --- a/src/utils/cache/getContract.ts +++ b/src/utils/cache/getContract.ts @@ -1,3 +1,5 @@ +import { StatusCodes } from "http-status-codes"; +import { createCustomError } from "../../server/middleware/error"; import { getSdk } from "./getSdk"; interface GetContractParams { @@ -15,6 +17,14 @@ export const getContract = async ({ }: GetContractParams) => { const sdk = await getSdk({ chainId, walletAddress, accountAddress }); - // We don't need to maintain cache for contracts because sdk handles it already - return sdk.getContract(contractAddress); + try { + // SDK already handles caching. + return sdk.getContract(contractAddress); + } catch (e) { + throw createCustomError( + `Contract metadata could not be resolved: ${e}`, + StatusCodes.BAD_REQUEST, + "INVALID_CONTRACT", + ); + } }; From e084a6e3b1f448c2ff57c080429c8f1ebf471ede Mon Sep 17 00:00:00 2001 From: Phillip Ho Date: Thu, 16 May 2024 06:27:59 +0800 Subject: [PATCH 2/2] fix await --- src/server/routes/backend-wallet/transfer.ts | 4 ++-- src/utils/cache/getContract.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/server/routes/backend-wallet/transfer.ts b/src/server/routes/backend-wallet/transfer.ts index dedf9494d..9e3f20917 100644 --- a/src/server/routes/backend-wallet/transfer.ts +++ b/src/server/routes/backend-wallet/transfer.ts @@ -90,10 +90,10 @@ export async function transfer(fastify: FastifyInstance) { const balance = await sdk.getBalance(walletAddress); - if (balance.value.lt(normalizedValue)) { + if (balance.value.lte(normalizedValue)) { throw createCustomError( "Insufficient balance", - StatusCodes.BAD_GATEWAY, + StatusCodes.BAD_REQUEST, "INSUFFICIENT_BALANCE", ); } diff --git a/src/utils/cache/getContract.ts b/src/utils/cache/getContract.ts index 379973b1f..e9b785c20 100644 --- a/src/utils/cache/getContract.ts +++ b/src/utils/cache/getContract.ts @@ -19,7 +19,7 @@ export const getContract = async ({ try { // SDK already handles caching. - return sdk.getContract(contractAddress); + return await sdk.getContract(contractAddress); } catch (e) { throw createCustomError( `Contract metadata could not be resolved: ${e}`,