simulateCalls
Simulates a set of calls for a block, and optionally provides asset changes. Internally uses the eth_simulateV1 JSON-RPC method.
Usage
import { } from 'viem'
import { } from './config'
const { } = await .({
: '0x5a0b54d5dc17e482fe8b0bdca5320161b95fb929',
: [
{
: '0xcb98643b8786950F0461f3B0edf99D88F274574D',
: ('2'),
},
{
: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
: ('1'),
},
],
})
.()
]Contract Calls
The calls property also accepts Contract Calls, and can be used via the abi, functionName, and args properties.
import { , } from 'viem'
import { } from './config'
const = ([
'function mint()',
'function transfer(address, uint256) returns (bool)',
])
const { } = await .({
: '0x5a0b54d5dc17e482fe8b0bdca5320161b95fb929',
: [
{
: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
: ('1')
},
{
: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
,
: 'mint',
},
{
: '0x95aD61b0a150d79219dCF64E1E6Cc01f0B64C4cE',
,
: 'transfer',
: [
'0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
100n
],
},
],
})
.()
]Asset Changes
Providing the traceAssetChanges parameter (with an account) will return asset balance changes for the calls.
import { , } from 'viem'
import { } from './config'
const = ([
'function mint()',
'function transfer(address, uint256) returns (bool)',
])
const { , } = await .({
: '0x5a0b54d5dc17e482fe8b0bdca5320161b95fb929',
: [
{
: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
: ('1.5')
},
{
: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
,
: 'mint',
},
{
: '0x95aD61b0a150d79219dCF64E1E6Cc01f0B64C4cE',
,
: 'transfer',
: [
'0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
100n
],
},
],
: true,
})
.()
]Reading Contracts
It is also worth noting that simulateCalls also supports "reading" contracts.
import { } from 'viem'
import { } from './config'
const = ([
'function totalSupply() returns (uint256)',
'function ownerOf(uint256) returns (address)',
])
const { } = await .({
: [
{
: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
,
: 'totalSupply',
},
{
: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
,
: 'ownerOf',
: [69420n],
},
{
: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
,
: 'ownerOf',
: [13371337n],
},
],
})
.()
]Return Value
SimulateCallsReturnType
Simulation results.
Parameters
calls
- Type:
Calls
Calls to simulate.
const { } = await .({
: '0x5a0b54d5dc17e482fe8b0bdca5320161b95fb929',
: [
{
: '0xcb98643b8786950F0461f3B0edf99D88F274574D',
: ('2'),
},
{
: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
: ('1'),
},
],
})calls.data
- Type:
Hex
Calldata to broadcast (typically a contract function selector with encoded arguments, or contract deployment bytecode).
const { } = await .({
: '0x5a0b54d5dc17e482fe8b0bdca5320161b95fb929',
: [
{
: '0xdeadbeef',
: '0xcb98643b8786950F0461f3B0edf99D88F274574D',
: ('2'),
},
],
})calls.to
- Type:
Address
The recipient address.
const { } = await .({
: '0x5a0b54d5dc17e482fe8b0bdca5320161b95fb929',
: [
{
: '0xcb98643b8786950F0461f3B0edf99D88F274574D',
: ('2'),
},
],
})calls.value
- Type:
Address
Value to send with the call.
const { } = await .({
: '0x5a0b54d5dc17e482fe8b0bdca5320161b95fb929',
: [
{
: '0xcb98643b8786950F0461f3B0edf99D88F274574D',
: ('2'),
},
],
})calls.dataSuffix
- Type: Hex
Data to append to the end of the calldata.
// @filename: config.ts
import { createPublicClient, http } from 'viem'
import { mainnet } from 'viem/chains'
export const client = createPublicClient({
chain: mainnet,
transport: http(),
})
// @filename: example.ts
// ---cut---
import { parseAbi } from 'viem'
import { client } from './config'
const abi = parseAbi([
'function approve(address, uint256) returns (bool)',
])
// ---cut---
const { id } = await client.simulateCalls({
calls: [
{
to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi,
functionName: 'approve',
args: [
'0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
100n
],
dataSuffix: '0xdeadbeef'
}
],
})account (optional)
- Type:
Account | Address
The account to simulate the calls from.
const { } = await .({
: '0x5a0b54d5dc17e482fe8b0bdca5320161b95fb929',
: [
{
: '0xcb98643b8786950F0461f3B0edf99D88F274574D',
: ('2'),
},
],
})blockNumber (optional)
- Type:
bigint
The block number to simulate the calls at.
const { } = await .({
: 17030000n,
: [
{
: '0xcb98643b8786950F0461f3B0edf99D88F274574D',
: ('2'),
},
],
})blockTag (optional)
- Type:
BlockTag
The block tag to simulate the calls at.
const { } = await .({
: 'pending',
: [
{
: '0xcb98643b8786950F0461f3B0edf99D88F274574D',
: ('2'),
},
],
})stateOverrides (optional)
- Type:
StateOverride
The state overrides to simulate the calls with.
const { } = await .({
: '0x5a0b54d5dc17e482fe8b0bdca5320161b95fb929',
: [
{
: '0xcb98643b8786950F0461f3B0edf99D88F274574D',
: ('2'),
},
],
: [{
: '0x5a0b54d5dc17e482fe8b0bdca5320161b95fb929',
: ('10000'),
}],
})traceAssetChanges (optional)
- Type:
boolean
Whether to trace asset changes.
const { } = await .({
: '0x5a0b54d5dc17e482fe8b0bdca5320161b95fb929',
: [
{
: '0xcb98643b8786950F0461f3B0edf99D88F274574D',
: ('2'),
},
],
: true,
})traceTransfers (optional)
- Type:
boolean
Whether to trace transfers.
const { } = await .({
: '0x5a0b54d5dc17e482fe8b0bdca5320161b95fb929',
: [
{
: '0xcb98643b8786950F0461f3B0edf99D88F274574D',
: ('2'),
},
],
: true,
})validation (optional)
- Type:
boolean
Whether to enable validation mode.
const { } = await .({
: '0x5a0b54d5dc17e482fe8b0bdca5320161b95fb929',
: [
{
: '0xcb98643b8786950F0461f3B0edf99D88F274574D',
: ('2'),
},
],
: true,
})