Skip to content

chore: Return 4xx if invalid contract or chain in API call #527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/server/routes/backend-wallet/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
);
}
Expand Down
40 changes: 25 additions & 15 deletions src/server/utils/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number> => {
const inputSlug = input.toLowerCase();
Expand Down Expand Up @@ -40,21 +44,27 @@ export const getChainIdFromChain = async (input: string): Promise<number> => {
}
}

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",
);
}
};
14 changes: 12 additions & 2 deletions src/utils/cache/getContract.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { StatusCodes } from "http-status-codes";
import { createCustomError } from "../../server/middleware/error";
import { getSdk } from "./getSdk";

interface GetContractParams {
Expand All @@ -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 await sdk.getContract(contractAddress);
} catch (e) {
throw createCustomError(
`Contract metadata could not be resolved: ${e}`,
StatusCodes.BAD_REQUEST,
"INVALID_CONTRACT",
);
}
};
Loading