Connect to a Contract
Connect to a deployed smart contract to start interacting with it.
The ABI of the smart contract is resolved automatically for contracts deployed or imported using the dashboard.
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...");
To cache the ABI of the smart contract, use thirdweb generate.
This is recommended to improve performance and provide type-safety when interacting with your smart contract.
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
);
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
);
abi (optional)
Optionally, (if you don’t want to use the import feature),
you can provide your smart contract’s 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
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 Type | Documentation | Description |
---|---|---|
Signature Drop | SignatureDrop | Functionality to claim using signature-based claiming |
Edition Drop | EditionDrop | ERC1155 & Drop functionality |
NFT Collection | NFTCollection | ERC721 functionality |
Edition | Edition | ERC1155 functionality |
Multiwrap | Multiwrap | Functionality to wrap tokens into a new multiwrap NFT |
Pack | Pack | Functionality to create, open, and add to pack NFTs |
Token Drop | TokenDrop | ERC20 & Drop functionality |
Token | Token | ERC20 functionality |
Marketplace | Marketplace | Marketplace features for creating, buying, selling NFTs |
Marketplace V3 | MarketplaceV3 | English auction, direct listing, and offer features |
Split | Split | Unique top-level functions to the split contract |
Vote | Vote | Unique top-level functions to the vote contract |