Skip to main content

useContract

Use this hook to get a contract instance by providing the contract ABI and address. It enables you to interact with your contract methods for both reading data and sending transactions with integrated transaction signing.

const { data: yourContract } = useContract({
abi: YourContractABI,
address: "0x...",
});

// Read contract data
const result = await yourContract.read.yourReadFunction(["param1", "param2"]);

// Write to contract with transaction signing
const tx = await yourContract.write.yourWriteFunction(["param1", "param2"], {
value: parseEther("0.1"),
gasLimit: 300000n,
});

This example uses the useContract hook to obtain a contract instance for any smart contract, enabling both read and write operations with automatic transaction signing integration.

Configuration

ParameterTypeRequiredDescription
abiAbiYesThe contract ABI containing function definitions.
addressstringYesThe contract address on the blockchain.

Return Value

  • data : Object which can be used to call read and write functions of the contract.

Read Operations

Read functions are automatically detected from the ABI and provide a clean interface for contract calls:

// Simple read operation
const balance = await contract.read.balanceOf(["0x1234..."]);

// Read with options
const result = await contract.read.getData([], {
gasLimit: 100000n,
blockTag: "latest",
});

Write Operations

Write functions automatically integrate with the transaction signing modal:

// Simple write operation
const tx = await contract.write.transfer(["0x1234...", 100n]);

// Write with transaction options
const tx = await contract.write.mint([], {
value: parseEther("0.1"),
gasLimit: 500000n,
blockConfirmations: 3,
});

Contract Read Options

ParameterTypeDescription
gasLimitbigintGas limit for the transaction.
blockTagstring | numberBlock tag for the read operation.

Contract Write Options

ParameterTypeDescription
valuebigintETH value to send with the transaction.
gasLimitbigintGas limit for the transaction.
gasPricebigintGas price for the transaction.
maxFeePerGasbigintMaximum fee per gas (EIP-1559).
maxPriorityFeePerGasbigintMaximum priority fee per gas (EIP-1559).
noncenumberTransaction nonce.
blockConfirmationsnumberNumber of block confirmations to wait for.