useSimulateContract
Hook for simulating/validating a contract interaction.
Import
import { useSimulateContract } from 'wagmi'
Usage
import { useSimulateContract } from 'wagmi'
import { abi } from './abi'
function App() {
const result = useSimulateContract({
abi,
address: '0x6b175474e89094c44da98b954eedeac495271d0f',
functionName: 'transferFrom',
args: [
'0xd2135CfB216b74109775236E36d4b433F1DF507B',
'0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
123n,
],
})
}
export const abi = [
{
type: 'function',
name: 'approve',
stateMutability: 'nonpayable',
inputs: [
{ name: 'spender', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
{
type: 'function',
name: 'transferFrom',
stateMutability: 'nonpayable',
inputs: [
{ name: 'sender', type: 'address' },
{ name: 'recipient', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
] as const
import { http, createConfig } from 'wagmi'
import { mainnet, sepolia } from 'wagmi/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
Parameters
import { type UseSimulateContractParameters } from 'wagmi'
abi
Abi | undefined
The contract's ABI. Check out the TypeScript docs for how to set up ABIs for maximum type inference and safety.
import { useSimulateContract } from 'wagmi'
import { abi } from './abi'
function App() {
const result = useSimulateContract({
abi,
address: '0x6b175474e89094c44da98b954eedeac495271d0f',
functionName: 'transferFrom',
args: [
'0xd2135CfB216b74109775236E36d4b433F1DF507B',
'0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
123n,
],
})
}
export const abi = [
{
type: 'function',
name: 'approve',
stateMutability: 'nonpayable',
inputs: [
{ name: 'spender', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
{
type: 'function',
name: 'transferFrom',
stateMutability: 'nonpayable',
inputs: [
{ name: 'sender', type: 'address' },
{ name: 'recipient', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
] as const
import { http, createConfig } from 'wagmi'
import { mainnet, sepolia } from 'wagmi/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
accessList
AccessList | undefined
The access list.
import { useSimulateContract } from 'wagmi'
import { abi } from './abi'
import { config } from './config'
function App() {
const result = useSimulateContract({
abi,
address: '0x6b175474e89094c44da98b954eedeac495271d0f',
functionName: 'transferFrom',
args: [
'0xd2135CfB216b74109775236E36d4b433F1DF507B',
'0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
123n,
],
accessList: [{
address: '0x1',
storageKeys: ['0x1'],
}],
})
}
export const abi = [
{
type: 'function',
name: 'approve',
stateMutability: 'nonpayable',
inputs: [
{ name: 'spender', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
{
type: 'function',
name: 'transferFrom',
stateMutability: 'nonpayable',
inputs: [
{ name: 'sender', type: 'address' },
{ name: 'recipient', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
] as const
import { http, createConfig } from 'wagmi'
import { mainnet, sepolia } from 'wagmi/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
account
Account | undefined
Account to use when calling the contract (msg.sender
). Throws if account is not found on connector
.
import { useSimulateContract } from 'wagmi'
import { abi } from './abi'
function App() {
const result = useSimulateContract({
abi,
address: '0x6b175474e89094c44da98b954eedeac495271d0f',
functionName: 'transferFrom',
args: [
'0xd2135CfB216b74109775236E36d4b433F1DF507B',
'0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
123n,
],
account: '0xd2135CfB216b74109775236E36d4b433F1DF507B',
})
}
export const abi = [
{
type: 'function',
name: 'approve',
stateMutability: 'nonpayable',
inputs: [
{ name: 'spender', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
{
type: 'function',
name: 'transferFrom',
stateMutability: 'nonpayable',
inputs: [
{ name: 'sender', type: 'address' },
{ name: 'recipient', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
] as const
import { http, createConfig } from 'wagmi'
import { mainnet, sepolia } from 'wagmi/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
address
Address | undefined
The contract's address.
import { useSimulateContract } from 'wagmi'
import { abi } from './abi'
function App() {
const result = useSimulateContract({
abi,
address: '0x6b175474e89094c44da98b954eedeac495271d0f',
functionName: 'transferFrom',
args: [
'0xd2135CfB216b74109775236E36d4b433F1DF507B',
'0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
123n,
],
})
}
export const abi = [
{
type: 'function',
name: 'approve',
stateMutability: 'nonpayable',
inputs: [
{ name: 'spender', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
{
type: 'function',
name: 'transferFrom',
stateMutability: 'nonpayable',
inputs: [
{ name: 'sender', type: 'address' },
{ name: 'recipient', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
] as const
import { http, createConfig } from 'wagmi'
import { mainnet, sepolia } from 'wagmi/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
args
readonly unknown[] | undefined
- Arguments to pass when calling the contract.
- Inferred from
abi
andfunctionName
.
import { useSimulateContract } from 'wagmi'
import { abi } from './abi'
function App() {
const result = useSimulateContract({
abi,
address: '0x6b175474e89094c44da98b954eedeac495271d0f',
functionName: 'transferFrom',
args: [
'0xd2135CfB216b74109775236E36d4b433F1DF507B',
'0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
123n,
],
})
}
export const abi = [
{
type: 'function',
name: 'approve',
stateMutability: 'nonpayable',
inputs: [
{ name: 'spender', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
{
type: 'function',
name: 'transferFrom',
stateMutability: 'nonpayable',
inputs: [
{ name: 'sender', type: 'address' },
{ name: 'recipient', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
] as const
import { http, createConfig } from 'wagmi'
import { mainnet, sepolia } from 'wagmi/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
blockNumber
bigint | undefined
Block number to call contract at.
import { useSimulateContract } from 'wagmi'
import { abi } from './abi'
function App() {
const result = useSimulateContract({
abi,
address: '0x6b175474e89094c44da98b954eedeac495271d0f',
functionName: 'transferFrom',
args: [
'0xd2135CfB216b74109775236E36d4b433F1DF507B',
'0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
123n,
],
blockNumber: 17829139n,
})
}
export const abi = [
{
type: 'function',
name: 'approve',
stateMutability: 'nonpayable',
inputs: [
{ name: 'spender', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
{
type: 'function',
name: 'transferFrom',
stateMutability: 'nonpayable',
inputs: [
{ name: 'sender', type: 'address' },
{ name: 'recipient', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
] as const
import { http, createConfig } from 'wagmi'
import { mainnet, sepolia } from 'wagmi/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
blockTag
'latest' | 'earliest' | 'pending' | 'safe' | 'finalized' | undefined
Block tag to call contract at.
import { useSimulateContract } from 'wagmi'
import { abi } from './abi'
function App() {
const result = useSimulateContract({
abi,
address: '0x6b175474e89094c44da98b954eedeac495271d0f',
functionName: 'transferFrom',
args: [
'0xd2135CfB216b74109775236E36d4b433F1DF507B',
'0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
123n,
],
blockTag: 'safe',
})
}
export const abi = [
{
type: 'function',
name: 'approve',
stateMutability: 'nonpayable',
inputs: [
{ name: 'spender', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
{
type: 'function',
name: 'transferFrom',
stateMutability: 'nonpayable',
inputs: [
{ name: 'sender', type: 'address' },
{ name: 'recipient', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
] as const
import { http, createConfig } from 'wagmi'
import { mainnet, sepolia } from 'wagmi/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
chainId
config['chains'][number]['id'] | undefined
ID of chain to use when fetching data.
import { useSimulateContract } from 'wagmi'
import { mainnet } from 'wagmi/chains'
import { abi } from './abi'
function App() {
const result = useSimulateContract({
abi,
address: '0x6b175474e89094c44da98b954eedeac495271d0f',
functionName: 'transferFrom',
args: [
'0xd2135CfB216b74109775236E36d4b433F1DF507B',
'0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
123n,
],
chainId: mainnet.id,
})
}
export const abi = [
{
type: 'function',
name: 'approve',
stateMutability: 'nonpayable',
inputs: [
{ name: 'spender', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
{
type: 'function',
name: 'transferFrom',
stateMutability: 'nonpayable',
inputs: [
{ name: 'sender', type: 'address' },
{ name: 'recipient', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
] as const
import { http, createConfig } from 'wagmi'
import { mainnet, sepolia } from 'wagmi/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
config
Config | undefined
Config
to use instead of retrieving from the nearest WagmiProvider
.
import { useSimulateContract } from 'wagmi'
import { abi } from './abi'
import { config } from './config'
function App() {
const result = useSimulateContract({
abi,
address: '0x6b175474e89094c44da98b954eedeac495271d0f',
functionName: 'transferFrom',
args: [
'0xd2135CfB216b74109775236E36d4b433F1DF507B',
'0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
123n,
],
config,
})
}
export const abi = [
{
type: 'function',
name: 'approve',
stateMutability: 'nonpayable',
inputs: [
{ name: 'spender', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
{
type: 'function',
name: 'transferFrom',
stateMutability: 'nonpayable',
inputs: [
{ name: 'sender', type: 'address' },
{ name: 'recipient', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
] as const
import { http, createConfig } from 'wagmi'
import { mainnet, sepolia } from 'wagmi/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
connector
Connector | undefined
Connector to simulate transaction with.
import { useConnectorClient, useSimulateContract } from 'wagmi'
import { abi } from './abi'
import { config } from './config'
function App() {
const { data: connector } = useConnectorClient()
const result = useSimulateContract({
abi,
address: '0x6b175474e89094c44da98b954eedeac495271d0f',
functionName: 'transferFrom',
args: [
'0xd2135CfB216b74109775236E36d4b433F1DF507B',
'0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
123n,
],
connector,
})
}
export const abi = [
{
type: 'function',
name: 'approve',
stateMutability: 'nonpayable',
inputs: [
{ name: 'spender', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
{
type: 'function',
name: 'transferFrom',
stateMutability: 'nonpayable',
inputs: [
{ name: 'sender', type: 'address' },
{ name: 'recipient', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
] as const
import { http, createConfig } from 'wagmi'
import { mainnet, sepolia } from 'wagmi/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
dataSuffix
`0x${string}` | undefined
Data to append to the end of the calldata. Useful for adding a "domain" tag.
import { useSimulateContract } from 'wagmi'
import { parseGwei } from 'viem'
import { abi } from './abi'
import { config } from './config'
function App() {
const result = useSimulateContract({
abi,
address: '0x6b175474e89094c44da98b954eedeac495271d0f',
functionName: 'transferFrom',
args: [
'0xd2135CfB216b74109775236E36d4b433F1DF507B',
'0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
123n,
],
dataSuffix: '0xdeadbeef',
})
}
export const abi = [
{
type: 'function',
name: 'approve',
stateMutability: 'nonpayable',
inputs: [
{ name: 'spender', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
{
type: 'function',
name: 'transferFrom',
stateMutability: 'nonpayable',
inputs: [
{ name: 'sender', type: 'address' },
{ name: 'recipient', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
] as const
import { http, createConfig } from 'wagmi'
import { mainnet, sepolia } from 'wagmi/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
functionName
string | undefined
- Function to call on the contract.
- Inferred from
abi
.
import { useSimulateContract } from 'wagmi'
import { abi } from './abi'
function App() {
const result = useSimulateContract({
abi,
address: '0x6b175474e89094c44da98b954eedeac495271d0f',
functionName: 'transferFrom',
args: [
'0xd2135CfB216b74109775236E36d4b433F1DF507B',
'0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
123n,
],
})
}
export const abi = [
{
type: 'function',
name: 'approve',
stateMutability: 'nonpayable',
inputs: [
{ name: 'spender', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
{
type: 'function',
name: 'transferFrom',
stateMutability: 'nonpayable',
inputs: [
{ name: 'sender', type: 'address' },
{ name: 'recipient', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
] as const
import { http, createConfig } from 'wagmi'
import { mainnet, sepolia } from 'wagmi/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
gas
bigint | undefined
Gas provided for transaction execution.
import { useSimulateContract } from 'wagmi'
import { parseGwei } from 'viem'
import { abi } from './abi'
import { config } from './config'
function App() {
const result = useSimulateContract({
abi,
address: '0x6b175474e89094c44da98b954eedeac495271d0f',
functionName: 'transferFrom',
args: [
'0xd2135CfB216b74109775236E36d4b433F1DF507B',
'0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
123n,
],
gas: parseGwei('20'),
})
}
export const abi = [
{
type: 'function',
name: 'approve',
stateMutability: 'nonpayable',
inputs: [
{ name: 'spender', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
{
type: 'function',
name: 'transferFrom',
stateMutability: 'nonpayable',
inputs: [
{ name: 'sender', type: 'address' },
{ name: 'recipient', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
] as const
import { http, createConfig } from 'wagmi'
import { mainnet, sepolia } from 'wagmi/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
gasPrice
bigint | undefined
The price in wei to pay per gas. Only applies to Legacy Transactions.
import { useSimulateContract } from 'wagmi'
import { parseGwei } from 'viem'
import { abi } from './abi'
import { config } from './config'
function App() {
const result = useSimulateContract({
abi,
address: '0x6b175474e89094c44da98b954eedeac495271d0f',
functionName: 'transferFrom',
args: [
'0xd2135CfB216b74109775236E36d4b433F1DF507B',
'0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
123n,
],
gasPrice: parseGwei('20'),
})
}
export const abi = [
{
type: 'function',
name: 'approve',
stateMutability: 'nonpayable',
inputs: [
{ name: 'spender', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
{
type: 'function',
name: 'transferFrom',
stateMutability: 'nonpayable',
inputs: [
{ name: 'sender', type: 'address' },
{ name: 'recipient', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
] as const
import { http, createConfig } from 'wagmi'
import { mainnet, sepolia } from 'wagmi/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
maxFeePerGas
bigint | undefined
Total fee per gas in wei, inclusive of maxPriorityFeePerGas
. Only applies to EIP-1559 Transactions.
import { useSimulateContract } from 'wagmi'
import { parseGwei } from 'viem'
import { abi } from './abi'
import { config } from './config'
function App() {
const result = useSimulateContract({
abi,
address: '0x6b175474e89094c44da98b954eedeac495271d0f',
functionName: 'transferFrom',
args: [
'0xd2135CfB216b74109775236E36d4b433F1DF507B',
'0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
123n,
],
maxFeePerGas: parseGwei('20'),
})
}
export const abi = [
{
type: 'function',
name: 'approve',
stateMutability: 'nonpayable',
inputs: [
{ name: 'spender', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
{
type: 'function',
name: 'transferFrom',
stateMutability: 'nonpayable',
inputs: [
{ name: 'sender', type: 'address' },
{ name: 'recipient', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
] as const
import { http, createConfig } from 'wagmi'
import { mainnet, sepolia } from 'wagmi/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
maxPriorityFeePerGas
bigint | undefined
Max priority fee per gas in wei. Only applies to EIP-1559 Transactions.
import { useSimulateContract } from 'wagmi'
import { parseGwei } from 'viem'
import { abi } from './abi'
import { config } from './config'
function App() {
const result = useSimulateContract({
abi,
address: '0x6b175474e89094c44da98b954eedeac495271d0f',
functionName: 'transferFrom',
args: [
'0xd2135CfB216b74109775236E36d4b433F1DF507B',
'0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
123n,
],
maxFeePerGas: parseGwei('20'),
maxPriorityFeePerGas: parseGwei('2'),
})
}
export const abi = [
{
type: 'function',
name: 'approve',
stateMutability: 'nonpayable',
inputs: [
{ name: 'spender', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
{
type: 'function',
name: 'transferFrom',
stateMutability: 'nonpayable',
inputs: [
{ name: 'sender', type: 'address' },
{ name: 'recipient', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
] as const
import { http, createConfig } from 'wagmi'
import { mainnet, sepolia } from 'wagmi/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
nonce
number
Unique number identifying this transaction.
import { useSimulateContract } from 'wagmi'
import { abi } from './abi'
import { config } from './config'
function App() {
const result = useSimulateContract({
abi,
address: '0x6b175474e89094c44da98b954eedeac495271d0f',
functionName: 'transferFrom',
args: [
'0xd2135CfB216b74109775236E36d4b433F1DF507B',
'0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
123n,
],
nonce: 123,
})
}
export const abi = [
{
type: 'function',
name: 'approve',
stateMutability: 'nonpayable',
inputs: [
{ name: 'spender', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
{
type: 'function',
name: 'transferFrom',
stateMutability: 'nonpayable',
inputs: [
{ name: 'sender', type: 'address' },
{ name: 'recipient', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
] as const
import { http, createConfig } from 'wagmi'
import { mainnet, sepolia } from 'wagmi/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
type
'legacy' | 'eip1559' | 'eip2930' | undefined
Optional transaction request type to narrow parameters.
import { useSimulateContract } from 'wagmi'
import { abi } from './abi'
import { config } from './config'
function App() {
const result = useSimulateContract({
abi,
address: '0x6b175474e89094c44da98b954eedeac495271d0f',
functionName: 'transferFrom',
args: [
'0xd2135CfB216b74109775236E36d4b433F1DF507B',
'0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
123n,
],
type: 'eip1559',
})
}
export const abi = [
{
type: 'function',
name: 'approve',
stateMutability: 'nonpayable',
inputs: [
{ name: 'spender', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
{
type: 'function',
name: 'transferFrom',
stateMutability: 'nonpayable',
inputs: [
{ name: 'sender', type: 'address' },
{ name: 'recipient', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
] as const
import { http, createConfig } from 'wagmi'
import { mainnet, sepolia } from 'wagmi/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
value
bigint | undefined
Value in wei sent with this transaction.
import { useSimulateContract } from 'wagmi'
import { parseEther } from 'viem'
import { abi } from './abi'
import { config } from './config'
function App() {
const result = useSimulateContract({
abi,
address: '0x6b175474e89094c44da98b954eedeac495271d0f',
functionName: 'transferFrom',
args: [
'0xd2135CfB216b74109775236E36d4b433F1DF507B',
'0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
123n,
],
value: parseEther('0.01'),
})
}
export const abi = [
{
type: 'function',
name: 'approve',
stateMutability: 'nonpayable',
inputs: [
{ name: 'spender', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
{
type: 'function',
name: 'transferFrom',
stateMutability: 'nonpayable',
inputs: [
{ name: 'sender', type: 'address' },
{ name: 'recipient', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
] as const
import { http, createConfig } from 'wagmi'
import { mainnet, sepolia } from 'wagmi/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
scopeKey
string | undefined
Scopes the cache to a given context. Hooks that have identical context will share the same cache.
import { useSimulateContract } from 'wagmi'
import { abi } from './abi'
import { config } from './config'
function App() {
const result = useSimulateContract({
abi,
address: '0x6b175474e89094c44da98b954eedeac495271d0f',
functionName: 'transferFrom',
args: [
'0xd2135CfB216b74109775236E36d4b433F1DF507B',
'0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
123n,
],
scopeKey: 'foo',
})
}
export const abi = [
{
type: 'function',
name: 'approve',
stateMutability: 'nonpayable',
inputs: [
{ name: 'spender', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
{
type: 'function',
name: 'transferFrom',
stateMutability: 'nonpayable',
inputs: [
{ name: 'sender', type: 'address' },
{ name: 'recipient', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
},
] as const
import { http, createConfig } from 'wagmi'
import { mainnet, sepolia } from 'wagmi/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
query
TanStack Query parameters. See the TanStack Query query docs for more info.
Wagmi does not support passing all TanStack Query parameters
TanStack Query parameters, like queryFn
and queryKey
, are used internally to make Wagmi work and you cannot override them. Check out the source to see what parameters are not supported. All parameters listed below are supported.
enabled
boolean | undefined
- Set this to
false
to disable this query from automatically running. - Can be used for Dependent Queries.
gcTime
number | Infinity | undefined
- Defaults to
5 * 60 * 1000
(5 minutes) orInfinity
during SSR - The time in milliseconds that unused/inactive cache data remains in memory. When a query's cache becomes unused or inactive, that cache data will be garbage collected after this duration. When different garbage collection times are specified, the longest one will be used.
- If set to
Infinity
, will disable garbage collection
initialData
SimulateContractReturnType | (() => SimulateContractReturnType) | undefined
- If set, this value will be used as the initial data for the query cache (as long as the query hasn't been created or cached yet)
- If set to a function, the function will be called once during the shared/root query initialization, and be expected to synchronously return the initialData
- Initial data is considered stale by default unless a
staleTime
has been set. initialData
is persisted to the cache
initialDataUpdatedAt
number | ((() => number | undefined)) | undefined
If set, this value will be used as the time (in milliseconds) of when the initialData
itself was last updated.
meta
Record<string, unknown> | undefined
If set, stores additional information on the query cache entry that can be used as needed. It will be accessible wherever the query
is available, and is also part of the QueryFunctionContext
provided to the queryFn
.
networkMode
online' | 'always' | 'offlineFirst' | undefined
- Defaults to
'online'
- see Network Mode for more information.
notifyOnChangeProps
string[] | 'all' | (() => string[] | 'all') | undefined
- If set, the component will only re-render if any of the listed properties change.
- If set to
['data', 'error']
for example, the component will only re-render when thedata
orerror
properties change. - If set to
'all'
, the component will opt-out of smart tracking and re-render whenever a query is updated. - If set to a function, the function will be executed to compute the list of properties.
- By default, access to properties will be tracked, and the component will only re-render when one of the tracked properties change.
placeholderData
SimulateContractReturnType | ((previousValue: SimulateContractReturnType | undefined; previousQuery: Query | undefined) => SimulateContractReturnType) | undefined
- If set, this value will be used as the placeholder data for this particular query observer while the query is still in the
pending
state. placeholderData
is not persisted to the cache- If you provide a function for
placeholderData
, as a first argument you will receive previously watched query data if available, and the second argument will be the complete previousQuery instance.
queryClient
QueryClient | undefined
Use this to use a custom QueryClient
. Otherwise, the one from the nearest context will be used.
refetchInterval
number | false | ((data: SimulateContractReturnType | undefined, query: Query) => number | false | undefined) | undefined
- If set to a number, all queries will continuously refetch at this frequency in milliseconds
- If set to a function, the function will be executed with the latest data and query to compute a frequency
refetchIntervalInBackground
boolean | undefined
If set to true
, queries that are set to continuously refetch with a refetchInterval
will continue to refetch while their tab/window is in the background
refetchOnMount
boolean | 'always' | ((query: Query) => boolean | 'always') | undefined
- Defaults to
true
- If set to
true
, the query will refetch on mount if the data is stale. - If set to
false
, the query will not refetch on mount. - If set to
'always'
, the query will always refetch on mount. - If set to a function, the function will be executed with the query to compute the value
refetchOnReconnect
boolean | 'always' | ((query: Query) => boolean | 'always') | undefined
- Defaults to
true
- If set to
true
, the query will refetch on reconnect if the data is stale. - If set to
false
, the query will not refetch on reconnect. - If set to
'always'
, the query will always refetch on reconnect. - If set to a function, the function will be executed with the query to compute the value
refetchOnWindowFocus
boolean | 'always' | ((query: Query) => boolean | 'always') | undefined
- Defaults to
true
- If set to
true
, the query will refetch on window focus if the data is stale. - If set to
false
, the query will not refetch on window focus. - If set to
'always'
, the query will always refetch on window focus. - If set to a function, the function will be executed with the query to compute the value
retry
boolean | number | ((failureCount: number, error: SimulateContractErrorType) => boolean) | undefined
- If
false
, failed queries will not retry by default. - If
true
, failed queries will retry infinitely. - If set to a
number
, e.g.3
, failed queries will retry until the failed query count meets that number. - Defaults to
3
on the client and0
on the server
retryDelay
number | ((retryAttempt: number, error: SimulateContractErrorType) => number) | undefined
- This function receives a
retryAttempt
integer and the actual Error and returns the delay to apply before the next attempt in milliseconds. - A function like
attempt => Math.min(attempt > 1 ? 2 ** attempt * 1000 : 1000, 30 * 1000)
applies exponential backoff. - A function like
attempt => attempt * 1000
applies linear backoff.
retryOnMount
boolean | undefined
If set to false
, the query will not be retried on mount if it contains an error. Defaults to true
.
select
((data: SimulateContractReturnType) => unknown) | undefined
This option can be used to transform or select a part of the data returned by the query function. It affects the returned data
value, but does not affect what gets stored in the query cache.
staleTime
number | Infinity | undefined
- Defaults to
0
- The time in milliseconds after data is considered stale. This value only applies to the hook it is defined on.
- If set to
Infinity
, the data will never be considered stale
structuralSharing
boolean | (((oldData: SimulateContractReturnType | undefined, newData: SimulateContractReturnType) => SimulateContractReturnType)) | undefined
- Defaults to
true
- If set to
false
, structural sharing between query results will be disabled. - If set to a function, the old and new data values will be passed through this function, which should combine them into resolved data for the query. This way, you can retain references from the old data to improve performance even when that data contains non-serializable values.
Return Type
import { type UseSimulateContractReturnType } from 'wagmi'
The return type's data
property is inferrable via the combination of abi
, functionName
, and args
. Check out the TypeScript docs for more info.
data
SimulateContractReturnType
- The last successfully resolved data for the query.
- Defaults to
undefined
.
dataUpdatedAt
number
The timestamp for when the query most recently returned the status
as 'success'
.
error
null | SimulateContractErrorType
- The error object for the query, if an error was thrown.
- Defaults to
null
errorUpdatedAt
number
The timestamp for when the query most recently returned the status
as 'error'
.
errorUpdateCount
number
The sum of all errors.
failureCount
number
- The failure count for the query.
- Incremented every time the query fails.
- Reset to
0
when the query succeeds.
failureReason
null | SimulateContractErrorType
- The failure reason for the query retry.
- Reset to
null
when the query succeeds.
fetchStatus
'fetching' | 'idle' | 'paused'
fetching
Istrue
whenever the queryFn is executing, which includes initialpending
as well as background refetches.paused
The query wanted to fetch, but has beenpaused
.idle
The query is not fetching.- See Network Mode for more information.
isError / isPending / isSuccess
boolean
Boolean variables derived from status
.
isFetched
boolean
Will be true
if the query has been fetched.
isFetchedAfterMount
boolean
- Will be
true
if the query has been fetched after the component mounted. - This property can be used to not show any previously cached data.
isFetching / isPaused
boolean
Boolean variables derived from fetchStatus
.
isLoading
boolean
- Is
true
whenever the first fetch for a query is in-flight - Is the same as
isFetching && isPending
isLoadingError
boolean
Will be true
if the query failed while fetching for the first time.
isPlaceholderData
boolean
Will be true
if the data shown is the placeholder data.
isRefetchError
boolean
Will be true
if the query failed while refetching.
isRefetching
boolean
- Is
true
whenever a background refetch is in-flight, which does not include initial'pending'
. - Is the same as
isFetching && !isPending
isStale
boolean
Will be true
if the data in the cache is invalidated or if the data is older than the given staleTime
.
refetch
(options: { cancelRefetch?: boolean | undefined; throwOnError?: boolean | undefined }) => Promise<UseQueryResult<SimulateContractReturnType, SimulateContractErrorType>>
- A function to manually refetch the query.
throwOnError
- When set to
true
, an error will be thrown if the query fails. - When set to
false
, an error will be logged if the query fails.
- When set to
cancelRefetch
- When set to
true
, a currently running request will be cancelled before a new request is made. - When set to
false
, no refetch will be made if there is already a request running. - Defaults to
true
- When set to
status
'error' | 'pending' | 'success'
pending
if there's no cached data and no query attempt was finished yet.error
if the query attempt resulted in an error. The correspondingerror
property has the error received from the attempted fetchsuccess
if the query has received a response with no errors and is ready to display its data. The correspondingdata
property on the query is the data received from the successful fetch or if the query'senabled
property is set tofalse
and has not been fetched yetdata
is the firstinitialData
supplied to the query on initialization.
Type Inference
With abi
setup correctly, TypeScript will infer the correct types for functionName
, args
, and value
. See the Wagmi TypeScript docs for more information.
TanStack Query
import {
type SimulateContractData,
type SimulateContractOptions,
type SimulateContractQueryFnData,
type SimulateContractQueryKey,
simulateContractQueryKey,
simulateContractQueryOptions,
} from 'wagmi/query'