Skip to content

Commit

Permalink
fix: Add validation for getContract params (#5780)
Browse files Browse the repository at this point in the history
Co-authored-by: MananTank <manantankm@gmail.com>
  • Loading branch information
joaquim-verges and MananTank authored Dec 18, 2024
1 parent 59ec04d commit a2a61df
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/shy-bats-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Validate getContract params
16 changes: 16 additions & 0 deletions packages/thirdweb/src/contract/contract.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Abi } from "abitype";
import type { Chain } from "../chains/types.js";
import type { ThirdwebClient } from "../client/client.js";
import { isAddress } from "../utils/address.js";

/**
* @contract
Expand Down Expand Up @@ -42,5 +43,20 @@ export type ThirdwebContract<abi extends Abi = []> = Readonly<
export function getContract<const abi extends Abi = []>(
options: ContractOptions<abi>,
): ThirdwebContract<abi> {
if (!options.client) {
throw new Error(
`getContract validation error - invalid client: ${options.client}`,
);
}
if (!isAddress(options.address)) {
throw new Error(
`getContract validation error - invalid address: ${options.address}`,
);
}
if (!options.chain || !options.chain.id) {
throw new Error(
`getContract validation error - invalid chain: ${options.chain}`,
);
}
return options;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { beforeAll, describe, expect, it } from "vitest";
import { TEST_CLIENT } from "../../../test/src/test-clients.js";
import { arbitrumSepolia } from "../../chains/chain-definitions/arbitrum-sepolia.js";
import { type ThirdwebContract, getContract } from "../../contract/contract.js";
import { balanceOf } from "../../extensions/erc1155/__generated__/IERC1155/read/balanceOf.js";
import { claimTo } from "../../extensions/erc1155/drops/write/claimTo.js";
Expand All @@ -13,7 +14,6 @@ import type { Account, Wallet } from "../interfaces/wallet.js";
import { generateAccount } from "../utils/generateAccount.js";
import { smartWallet } from "./smart-wallet.js";
let wallet: Wallet;
import { arbitrumSepolia } from "../../chains/chain-definitions/arbitrum-sepolia.js";

let smartAccount: Account;
let smartWalletAddress: Address;
Expand Down

0 comments on commit a2a61df

Please sign in to comment.