useContract

Signature #1

Hook for connecting to a smart contract.

Provide your smart contract address as the first parameter. Once connected, the contract will be an instance of your smart contract.

The ABI of the smart contract is resolved automatically for contracts deployed or imported using the thirdweb dashboard .

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.

Example

const { contract, isLoading, error } = useContract(contractAddress);
function useContract(
contractAddress: RequiredParam<string>,

Parameters

The address of the deployed contract

Type

let contractAddress: RequiredParam<string>;

Returns

Query result object that includes the contract once it is resolved

Signature #2

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

Available contract types are:

  • "nft-drop"

  • "signature-drop"

  • "edition-drop"

  • "nft-collection"

  • "edition"

  • "multiwrap"

  • "pack"

  • "token-drop"

  • "token"

  • "marketplace"

  • "marketplace-v3"

  • "split"

  • "vote"

When a contract type is provided, the contract object will be typed as the contract's class. For example, if you provide the contract type "pack" , the contract object will be returned typed as an instance of the Pack class, unlocking all of the top-level functions specific to the pack.

Example

const { contract, isLoading, error } = useContract(
contractAddress,
"pack",
);
function useContract(
contractAddress: RequiredParam<string>,
_contractType: TContractType,
TContractType extends PrebuiltContractType
? ContractForPrebuiltContractType<TContractType>
>;

Parameters

The address of the deployed contract

Type

let contractAddress: RequiredParam<string>;

The type of the contract

Type

let _contractType: TContractType;

Returns

let returnType: UseContractResult<
TContractType extends PrebuiltContractType
? ContractForPrebuiltContractType<TContractType>
>;

Query result object that includes the contract once it is resolved

Signature #3

Optionally, (if you don’t want to use the dashboard import feature), you can provide your smart contract’s ABI to the second parameter of the useContract hook. This is 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 .

Example

const { contract, isLoading, error } = useContract(
contractAddress,
contractAbi,
);
function useContract(
contractAddress: RequiredParam<string>,
_abi: ContractInterface,

Parameters

The address of the deployed contract

Type

let contractAddress: RequiredParam<string>;

The ABI of the contract to use

Type

let _abi: ContractInterface;

Returns

Query result object that includes the contract once it is resolved

Signature #4

function useContract(
contractAddress: RequiredParam<TContractAddress>,
TContractAddress extends GeneratedContractAddress
>;

Parameters

Type

let contractAddress: RequiredParam<TContractAddress>;

Returns

let returnType: UseContractResult<
TContractAddress extends GeneratedContractAddress
>;