Skip to main content

Connect to a Contract

Connect to a deployed smart contract to start interacting with it.

Usage

// Instantiate the SDK in either read or read-write mode
const sdk = new ThirdWebSDK("ethereum");

// Connect to your smart contract using the contract address.
const contract = await sdk.getContract("0x...");

Configuration

address (required)

The address of the smart contract you want to connect to.

const contract = await sdk.getContract(
"0x...", // The address of your smart contract
);
Import Smart Contracts

If your smart contract was not deployed using thirdweb, youll need to import it on the dashboard.

contractType (optional)

If your contract is a prebuilt contract, it is strongly recommended that you provide the contract's name as the second argument to gain access to improved top-level functions and type inference.

Depending on the contract type you provide, the getContract method returns an instance of a different class with more specific methods available to use.

View available contract types
  • NFT Drop: "nft-drop"
  • Signature Drop: "signature-drop"
  • Edition Drop: "edition-drop"
  • NFT Collection: "nft-collection"
  • Edition: "edition"
  • Multiwrap: "multiwrap"
  • Pack: "pack"
  • Token Drop: "token-drop"
  • Token: "token"
  • Marketplace: "marketplace" | "marketplace-v3"
  • Split: "split"
  • Vote: "vote"
const contract = await sdk.getContract(
"0x...", // The address of your smart contract
"marketplace-v3", // The type of your smart contract
);

Using Contract ABI

Optionally, (if you dont want to use the import feature), you can provide your smart contracts ABI to the second parameter of the getContract method. Useful when developing on a local node, where it may be faster to use the ABI than to import the contract using the dashboard.

The ABI is only necessary if you have not deployed your contract with, or imported your contract to the thirdweb dashboard.

const contract = await sdk.getContract(
"0x...", // The address of your smart contract
abi, // The ABI of your smart contract
);

Return Value

By default, the getContract method returns an instance of the SmartContract class.

{
events: ContractEvents<TContract>; // See "Contract Events"
interceptor: ContractInterceptor<TContract>; // See "Contract Interceptor"
encoder: ContractEncoder<TContract>; // Internal usage.
estimator: GasCostEstimator<TContract>; // See "Gas Cost Estimator"
publishedMetadata: ContractPublishedMetadata<TContract>; // See "Publish" documentation
abi: ContractInterface; // The ABI of the smart contract
metadata: ContractMetadata<BaseContract, any>; // See "Contract Metadata" extension
royalties: ContractRoyalty<IRoyalty, any>; // See "Royalties" extension
roles: ContractRoles<IPermissions, any>; // See "Permissions" extension
sales: ContractPrimarySale<IPrimarySale>; // See "Primary Sale" extension
platformFees: ContractPlatformFee<IPlatformFee>; // See "Platform Fees" extension
owner: ContractOwner<Ownable>; // See "Ownable" extension
erc20: Erc20; // See "ERC20" extension section
erc721: Erc721; // See "ERC721" extension section
erc1155: Erc1155; // See "ERC1155" extension section
chainId: number; // The chain ID of the network the contract is deployed on
getAddress(): string; // Function to get the address of the contract
call(functionName: string, ...args: unknown[] | [...unknown[], CallOverrides]): Promise<any>; // Call a function on the contract
}

Depending on the contract type you provide, the getContract method returns an instance of a different class with more specific methods available to use. Below is a table of the different classes that can be returned when a contract type is provided.

Contract TypeDocumentationDescription
Signature DropSignatureDropFunctionality to claim using signature-based claiming
Edition DropEditionDropERC1155 & Drop functionality
NFT CollectionNFTCollectionERC721 functionality
EditionEditionERC1155 functionality
MultiwrapMultiwrapFunctionality to wrap tokens into a new multiwrap NFT
PackPackFunctionality to create, open, and add to pack NFTs
Token Drop`TokenDrop ERC20 & Drop functionality
TokenTokenERC20 functionality
MarketplaceMarketplaceMarketplace features for creating, buying, selling NFTs
Marketplace V3MarketplaceV3English auction, direct listing, and offer features
SplitSplitUnique top-level functions to the split contract
VoteVoteUnique top-level functions to the vote contract