The input parameters for retrieving the contract.
The contract instance.
Creates a Filter to retrieve event logs that can be used with getFilterChanges
or getFilterLogs
.
import { createPublicClient, getContract, http, parseAbi } from 'viem'
import { mainnet } from 'viem/chains'
const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
})
const contract = getContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: parseAbi(['event Transfer(address indexed, address indexed, uint256)']),
client: publicClient,
})
const filter = await contract.createEventFilter.Transfer()
Estimates the gas necessary to complete a transaction without submitting it to the network.
import { createPublicClient, getContract, http, parseAbi } from 'viem'
import { mainnet } from 'viem/chains'
const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
})
const contract = getContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: parseAbi(['function mint() public']),
client: publicClient,
})
const gas = await contract.estimateGas.mint({
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
})
Creates a Filter to retrieve event logs that can be used with getFilterChanges
or getFilterLogs
.
import { createPublicClient, getContract, http, parseAbi } from 'viem'
import { mainnet } from 'viem/chains'
const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
})
const contract = getContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: parseAbi(['event Transfer(address indexed, address indexed, uint256)']),
client: publicClient,
})
const filter = await contract.createEventFilter.Transfer()
Calls a read-only function on a contract, and returns the response.
A "read-only" function (constant function) on a Solidity contract is denoted by a view
or pure
keyword. They can only read the state of the contract, and cannot make any changes to it. Since read-only methods do not change the state of the contract, they do not require any gas to be executed, and can be called by any user without the need to pay for gas.
Internally, read
uses a Public Client to call the call
action with ABI-encoded data
.
import { createPublicClient, getContract, http, parseAbi } from 'viem'
import { mainnet } from 'viem/chains'
const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
})
const contract = getContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: parseAbi([
'function balanceOf(address owner) view returns (uint256)',
]),
client: publicClient,
})
const result = await contract.read.balanceOf(['0xA0Cf798816D4b9b9866b5330EEa46a18382f251e'])
// 424122n
Simulates/validates a contract interaction. This is useful for retrieving return data and revert reasons of contract write functions.
This function does not require gas to execute and does not change the state of the blockchain. It is almost identical to readContract
, but also supports contract write functions.
Internally, simulate
uses a Public Client to call the call
action with ABI-encoded data
.
import { createPublicClient, getContract, http, parseAbi } from 'viem'
import { mainnet } from 'viem/chains'
const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
})
const contract = getContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: parseAbi(['function mint() public']),
client: publicClient,
})
const result = await contract.simulate.mint({
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
})
Watches and returns emitted contract event logs.
This Action will batch up all the event logs found within the pollingInterval
, and invoke them via onLogs
.
watchEvent
will attempt to create an Event Filter and listen to changes to the Filter per polling interval, however, if the RPC Provider does not support Filters (e.g. eth_newFilter
), then watchEvent
will fall back to using getLogs
instead.
import { createPublicClient, getContract, http, parseAbi } from 'viem'
import { mainnet } from 'viem/chains'
const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
})
const contract = getContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: parseAbi(['event Transfer(address indexed, address indexed, uint256)']),
client: publicClient,
})
const filter = await contract.createEventFilter.Transfer()
const unwatch = contract.watchEvent.Transfer(
{ from: '0xc961145a54C96E3aE9bAA048c4F4D6b04C13916b' },
{ onLogs: (logs) => console.log(logs) },
)
Executes a write function on a contract.
A "write" function on a Solidity contract modifies the state of the blockchain. These types of functions require gas to be executed, and hence a Transaction is needed to be broadcast in order to change the state.
Internally, write
uses a Wallet Client to call the sendTransaction
action with ABI-encoded data
.
Warning: The write
internally sends a transaction – it does not validate if the contract write will succeed (the contract may throw an error). It is highly recommended to simulate the contract write with contract.simulate
before you execute it.
import { createWalletClient, getContract, http, parseAbi } from 'viem'
import { mainnet } from 'viem/chains'
const walletClient = createWalletClient({
chain: mainnet,
transport: http(),
})
const contract = getContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: parseAbi(['function mint(uint32 tokenId) nonpayable']),
client: walletClient,
})
const hash = await contract.write.min([69420], {
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
Creates a Filter to retrieve event logs that can be used with getFilterChanges
or getFilterLogs
.
import { createPublicClient, getContract, http, parseAbi } from 'viem'
import { mainnet } from 'viem/chains'
const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
})
const contract = getContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: parseAbi(['event Transfer(address indexed, address indexed, uint256)']),
client: publicClient,
})
const filter = await contract.createEventFilter.Transfer()
Estimates the gas necessary to complete a transaction without submitting it to the network.
import { createPublicClient, getContract, http, parseAbi } from 'viem'
import { mainnet } from 'viem/chains'
const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
})
const contract = getContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: parseAbi(['function mint() public']),
client: publicClient,
})
const gas = await contract.estimateGas.mint({
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
})
Creates a Filter to retrieve event logs that can be used with getFilterChanges
or getFilterLogs
.
import { createPublicClient, getContract, http, parseAbi } from 'viem'
import { mainnet } from 'viem/chains'
const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
})
const contract = getContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: parseAbi(['event Transfer(address indexed, address indexed, uint256)']),
client: publicClient,
})
const filter = await contract.createEventFilter.Transfer()
Calls a read-only function on a contract, and returns the response.
A "read-only" function (constant function) on a Solidity contract is denoted by a view
or pure
keyword. They can only read the state of the contract, and cannot make any changes to it. Since read-only methods do not change the state of the contract, they do not require any gas to be executed, and can be called by any user without the need to pay for gas.
Internally, read
uses a Public Client to call the call
action with ABI-encoded data
.
import { createPublicClient, getContract, http, parseAbi } from 'viem'
import { mainnet } from 'viem/chains'
const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
})
const contract = getContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: parseAbi([
'function balanceOf(address owner) view returns (uint256)',
]),
client: publicClient,
})
const result = await contract.read.balanceOf(['0xA0Cf798816D4b9b9866b5330EEa46a18382f251e'])
// 424122n
Simulates/validates a contract interaction. This is useful for retrieving return data and revert reasons of contract write functions.
This function does not require gas to execute and does not change the state of the blockchain. It is almost identical to readContract
, but also supports contract write functions.
Internally, simulate
uses a Public Client to call the call
action with ABI-encoded data
.
import { createPublicClient, getContract, http, parseAbi } from 'viem'
import { mainnet } from 'viem/chains'
const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
})
const contract = getContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: parseAbi(['function mint() public']),
client: publicClient,
})
const result = await contract.simulate.mint({
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
})
Watches and returns emitted contract event logs.
This Action will batch up all the event logs found within the pollingInterval
, and invoke them via onLogs
.
watchEvent
will attempt to create an Event Filter and listen to changes to the Filter per polling interval, however, if the RPC Provider does not support Filters (e.g. eth_newFilter
), then watchEvent
will fall back to using getLogs
instead.
import { createPublicClient, getContract, http, parseAbi } from 'viem'
import { mainnet } from 'viem/chains'
const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
})
const contract = getContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: parseAbi(['event Transfer(address indexed, address indexed, uint256)']),
client: publicClient,
})
const filter = await contract.createEventFilter.Transfer()
const unwatch = contract.watchEvent.Transfer(
{ from: '0xc961145a54C96E3aE9bAA048c4F4D6b04C13916b' },
{ onLogs: (logs) => console.log(logs) },
)
Executes a write function on a contract.
A "write" function on a Solidity contract modifies the state of the blockchain. These types of functions require gas to be executed, and hence a Transaction is needed to be broadcast in order to change the state.
Internally, write
uses a Wallet Client to call the sendTransaction
action with ABI-encoded data
.
Warning: The write
internally sends a transaction – it does not validate if the contract write will succeed (the contract may throw an error). It is highly recommended to simulate the contract write with contract.simulate
before you execute it.
import { createWalletClient, getContract, http, parseAbi } from 'viem'
import { mainnet } from 'viem/chains'
const walletClient = createWalletClient({
chain: mainnet,
transport: http(),
})
const contract = getContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: parseAbi(['function mint(uint32 tokenId) nonpayable']),
client: walletClient,
})
const hash = await contract.write.min([69420], {
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
Creates a Filter to retrieve event logs that can be used with getFilterChanges
or getFilterLogs
.
import { createPublicClient, getContract, http, parseAbi } from 'viem'
import { mainnet } from 'viem/chains'
const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
})
const contract = getContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: parseAbi(['event Transfer(address indexed, address indexed, uint256)']),
client: publicClient,
})
const filter = await contract.createEventFilter.Transfer()
Estimates the gas necessary to complete a transaction without submitting it to the network.
import { createPublicClient, getContract, http, parseAbi } from 'viem'
import { mainnet } from 'viem/chains'
const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
})
const contract = getContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: parseAbi(['function mint() public']),
client: publicClient,
})
const gas = await contract.estimateGas.mint({
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
})
Creates a Filter to retrieve event logs that can be used with getFilterChanges
or getFilterLogs
.
import { createPublicClient, getContract, http, parseAbi } from 'viem'
import { mainnet } from 'viem/chains'
const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
})
const contract = getContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: parseAbi(['event Transfer(address indexed, address indexed, uint256)']),
client: publicClient,
})
const filter = await contract.createEventFilter.Transfer()
Calls a read-only function on a contract, and returns the response.
A "read-only" function (constant function) on a Solidity contract is denoted by a view
or pure
keyword. They can only read the state of the contract, and cannot make any changes to it. Since read-only methods do not change the state of the contract, they do not require any gas to be executed, and can be called by any user without the need to pay for gas.
Internally, read
uses a Public Client to call the call
action with ABI-encoded data
.
import { createPublicClient, getContract, http, parseAbi } from 'viem'
import { mainnet } from 'viem/chains'
const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
})
const contract = getContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: parseAbi([
'function balanceOf(address owner) view returns (uint256)',
]),
client: publicClient,
})
const result = await contract.read.balanceOf(['0xA0Cf798816D4b9b9866b5330EEa46a18382f251e'])
// 424122n
Simulates/validates a contract interaction. This is useful for retrieving return data and revert reasons of contract write functions.
This function does not require gas to execute and does not change the state of the blockchain. It is almost identical to readContract
, but also supports contract write functions.
Internally, simulate
uses a Public Client to call the call
action with ABI-encoded data
.
import { createPublicClient, getContract, http, parseAbi } from 'viem'
import { mainnet } from 'viem/chains'
const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
})
const contract = getContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: parseAbi(['function mint() public']),
client: publicClient,
})
const result = await contract.simulate.mint({
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
})
Watches and returns emitted contract event logs.
This Action will batch up all the event logs found within the pollingInterval
, and invoke them via onLogs
.
watchEvent
will attempt to create an Event Filter and listen to changes to the Filter per polling interval, however, if the RPC Provider does not support Filters (e.g. eth_newFilter
), then watchEvent
will fall back to using getLogs
instead.
import { createPublicClient, getContract, http, parseAbi } from 'viem'
import { mainnet } from 'viem/chains'
const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
})
const contract = getContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: parseAbi(['event Transfer(address indexed, address indexed, uint256)']),
client: publicClient,
})
const filter = await contract.createEventFilter.Transfer()
const unwatch = contract.watchEvent.Transfer(
{ from: '0xc961145a54C96E3aE9bAA048c4F4D6b04C13916b' },
{ onLogs: (logs) => console.log(logs) },
)
Executes a write function on a contract.
A "write" function on a Solidity contract modifies the state of the blockchain. These types of functions require gas to be executed, and hence a Transaction is needed to be broadcast in order to change the state.
Internally, write
uses a Wallet Client to call the sendTransaction
action with ABI-encoded data
.
Warning: The write
internally sends a transaction – it does not validate if the contract write will succeed (the contract may throw an error). It is highly recommended to simulate the contract write with contract.simulate
before you execute it.
import { createWalletClient, getContract, http, parseAbi } from 'viem'
import { mainnet } from 'viem/chains'
const walletClient = createWalletClient({
chain: mainnet,
transport: http(),
})
const contract = getContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: parseAbi(['function mint(uint32 tokenId) nonpayable']),
client: walletClient,
})
const hash = await contract.write.min([69420], {
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
Creates a Filter to retrieve event logs that can be used with getFilterChanges
or getFilterLogs
.
import { createPublicClient, getContract, http, parseAbi } from 'viem'
import { mainnet } from 'viem/chains'
const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
})
const contract = getContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: parseAbi(['event Transfer(address indexed, address indexed, uint256)']),
client: publicClient,
})
const filter = await contract.createEventFilter.Transfer()
Estimates the gas necessary to complete a transaction without submitting it to the network.
import { createPublicClient, getContract, http, parseAbi } from 'viem'
import { mainnet } from 'viem/chains'
const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
})
const contract = getContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: parseAbi(['function mint() public']),
client: publicClient,
})
const gas = await contract.estimateGas.mint({
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
})
Creates a Filter to retrieve event logs that can be used with getFilterChanges
or getFilterLogs
.
import { createPublicClient, getContract, http, parseAbi } from 'viem'
import { mainnet } from 'viem/chains'
const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
})
const contract = getContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: parseAbi(['event Transfer(address indexed, address indexed, uint256)']),
client: publicClient,
})
const filter = await contract.createEventFilter.Transfer()
Calls a read-only function on a contract, and returns the response.
A "read-only" function (constant function) on a Solidity contract is denoted by a view
or pure
keyword. They can only read the state of the contract, and cannot make any changes to it. Since read-only methods do not change the state of the contract, they do not require any gas to be executed, and can be called by any user without the need to pay for gas.
Internally, read
uses a Public Client to call the call
action with ABI-encoded data
.
import { createPublicClient, getContract, http, parseAbi } from 'viem'
import { mainnet } from 'viem/chains'
const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
})
const contract = getContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: parseAbi([
'function balanceOf(address owner) view returns (uint256)',
]),
client: publicClient,
})
const result = await contract.read.balanceOf(['0xA0Cf798816D4b9b9866b5330EEa46a18382f251e'])
// 424122n
Simulates/validates a contract interaction. This is useful for retrieving return data and revert reasons of contract write functions.
This function does not require gas to execute and does not change the state of the blockchain. It is almost identical to readContract
, but also supports contract write functions.
Internally, simulate
uses a Public Client to call the call
action with ABI-encoded data
.
import { createPublicClient, getContract, http, parseAbi } from 'viem'
import { mainnet } from 'viem/chains'
const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
})
const contract = getContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: parseAbi(['function mint() public']),
client: publicClient,
})
const result = await contract.simulate.mint({
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
})
Watches and returns emitted contract event logs.
This Action will batch up all the event logs found within the pollingInterval
, and invoke them via onLogs
.
watchEvent
will attempt to create an Event Filter and listen to changes to the Filter per polling interval, however, if the RPC Provider does not support Filters (e.g. eth_newFilter
), then watchEvent
will fall back to using getLogs
instead.
import { createPublicClient, getContract, http, parseAbi } from 'viem'
import { mainnet } from 'viem/chains'
const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
})
const contract = getContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: parseAbi(['event Transfer(address indexed, address indexed, uint256)']),
client: publicClient,
})
const filter = await contract.createEventFilter.Transfer()
const unwatch = contract.watchEvent.Transfer(
{ from: '0xc961145a54C96E3aE9bAA048c4F4D6b04C13916b' },
{ onLogs: (logs) => console.log(logs) },
)
Executes a write function on a contract.
A "write" function on a Solidity contract modifies the state of the blockchain. These types of functions require gas to be executed, and hence a Transaction is needed to be broadcast in order to change the state.
Internally, write
uses a Wallet Client to call the sendTransaction
action with ABI-encoded data
.
Warning: The write
internally sends a transaction – it does not validate if the contract write will succeed (the contract may throw an error). It is highly recommended to simulate the contract write with contract.simulate
before you execute it.
import { createWalletClient, getContract, http, parseAbi } from 'viem'
import { mainnet } from 'viem/chains'
const walletClient = createWalletClient({
chain: mainnet,
transport: http(),
})
const contract = getContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: parseAbi(['function mint(uint32 tokenId) nonpayable']),
client: walletClient,
})
const hash = await contract.write.min([69420], {
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
Retrieves a contract instance using the provided address, ABI, and client.