670 lines
17 KiB
Markdown
670 lines
17 KiB
Markdown
# simulateContract [Simulates & validates a contract interaction.]
|
|
|
|
The `simulateContract` function **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`](/docs/contract/readContract), but also supports contract write functions.
|
|
|
|
Internally, `simulateContract` uses a [Public Client](/docs/clients/public) to call the [`call` action](/docs/actions/public/call) with [ABI-encoded `data`](/docs/contract/encodeFunctionData).
|
|
|
|
## Usage
|
|
|
|
Below is a very basic example of how to simulate a write function on a contract (with no arguments).
|
|
|
|
The `mint` function accepts no arguments, and returns a token ID.
|
|
|
|
:::code-group
|
|
|
|
```ts [example.ts]
|
|
import { account, publicClient } from './config'
|
|
import { wagmiAbi } from './abi'
|
|
|
|
const { result } = await publicClient.simulateContract({
|
|
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
|
|
abi: wagmiAbi,
|
|
functionName: 'mint',
|
|
account,
|
|
})
|
|
```
|
|
|
|
```ts [abi.ts]
|
|
export const wagmiAbi = [
|
|
...
|
|
{
|
|
inputs: [],
|
|
name: "mint",
|
|
outputs: [{ name: "", type: "uint32" }],
|
|
stateMutability: "view",
|
|
type: "function",
|
|
},
|
|
...
|
|
] as const;
|
|
```
|
|
|
|
```ts [config.ts]
|
|
import { createPublicClient, http } from 'viem'
|
|
import { privateKeyToAccount } from 'viem/accounts'
|
|
import { mainnet } from 'viem/chains'
|
|
|
|
// JSON-RPC Account
|
|
export const account = '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'
|
|
// Local Account
|
|
export const account = privateKeyToAccount(...)
|
|
|
|
export const publicClient = createPublicClient({
|
|
chain: mainnet,
|
|
transport: http()
|
|
})
|
|
```
|
|
|
|
:::
|
|
|
|
### Passing Arguments
|
|
|
|
If your function requires argument(s), you can pass them through with the `args` attribute.
|
|
|
|
TypeScript types for `args` will be inferred from the function name & ABI, to guard you from inserting the wrong values.
|
|
|
|
For example, the `mint` function name below requires a **tokenId** argument, and it is typed as `[number]`.
|
|
|
|
:::code-group
|
|
|
|
```ts [example.ts]
|
|
import { account, publicClient } from './config'
|
|
import { wagmiAbi } from './abi'
|
|
|
|
const { result } = await publicClient.simulateContract({
|
|
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
|
|
abi: wagmiAbi,
|
|
functionName: 'mint',
|
|
args: [69420], // [!code focus]
|
|
account,
|
|
})
|
|
```
|
|
|
|
```ts [abi.ts]
|
|
export const wagmiAbi = [
|
|
...
|
|
{
|
|
inputs: [{ name: "owner", type: "uint32" }],
|
|
name: "mint",
|
|
outputs: [{ name: "", type: "uint32" }],
|
|
stateMutability: "view",
|
|
type: "function",
|
|
},
|
|
...
|
|
] as const;
|
|
```
|
|
|
|
```ts [config.ts]
|
|
import { createPublicClient, http } from 'viem'
|
|
import { privateKeyToAccount } from 'viem/accounts'
|
|
import { mainnet } from 'viem/chains'
|
|
|
|
// JSON-RPC Account
|
|
export const account = '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'
|
|
// Local Account
|
|
export const account = privateKeyToAccount(...)
|
|
|
|
export const publicClient = createPublicClient({
|
|
chain: mainnet,
|
|
transport: http()
|
|
})
|
|
```
|
|
|
|
:::
|
|
|
|
### Pairing with `writeContract`
|
|
|
|
The `simulateContract` function also pairs well with `writeContract`.
|
|
|
|
In the example below, we are **validating** if the contract write will be successful via `simulateContract`. If no errors are thrown, then we are all good. After that, we perform a contract write to execute the transaction.
|
|
|
|
:::code-group
|
|
|
|
```ts [example.ts]
|
|
import { account, walletClient, publicClient } from './config'
|
|
import { wagmiAbi } from './abi'
|
|
|
|
const { request } = await publicClient.simulateContract({
|
|
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
|
|
abi: wagmiAbi,
|
|
functionName: 'mint',
|
|
account,
|
|
})
|
|
const hash = await walletClient.writeContract(request)
|
|
```
|
|
|
|
```ts [abi.ts]
|
|
export const wagmiAbi = [
|
|
...
|
|
{
|
|
inputs: [],
|
|
name: "mint",
|
|
outputs: [{ name: "", type: "uint32" }],
|
|
stateMutability: "view",
|
|
type: "function",
|
|
},
|
|
...
|
|
] as const;
|
|
```
|
|
|
|
```ts [config.ts]
|
|
import { createPublicClient, custom, http } from 'viem'
|
|
import { privateKeyToAccount } from 'viem/accounts'
|
|
import { mainnet } from 'viem/chains'
|
|
|
|
// JSON-RPC Account
|
|
export const account = '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'
|
|
// Local Account
|
|
export const account = privateKeyToAccount(...)
|
|
|
|
export const walletClient = createWalletClient({
|
|
chain: mainnet,
|
|
transport: custom(window.ethereum)
|
|
})
|
|
|
|
export const publicClient = createPublicClient({
|
|
chain: mainnet,
|
|
transport: http()
|
|
})
|
|
```
|
|
|
|
:::
|
|
|
|
### Handling Custom Errors
|
|
|
|
In the example below, we are **catching** a [custom error](https://blog.soliditylang.org/2021/04/21/custom-errors/) thrown by the `simulateContract`. It is important to include the custom error item in the contract `abi`.
|
|
|
|
You can access the custom error through the `data` attribute of the error:
|
|
|
|
:::code-group
|
|
|
|
```ts [example.ts] {13-27}
|
|
import { BaseError, ContractFunctionRevertedError } from 'viem';
|
|
import { account, walletClient, publicClient } from './config'
|
|
import { wagmiAbi } from './abi'
|
|
|
|
try {
|
|
await publicClient.simulateContract({
|
|
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
|
|
abi: wagmiAbi,
|
|
functionName: 'mint',
|
|
account,
|
|
})
|
|
} catch (err) {
|
|
if (err instanceof BaseError) {
|
|
const revertError = err.walk(err => err instanceof ContractFunctionRevertedError)
|
|
if (revertError instanceof ContractFunctionRevertedError) {
|
|
const errorName = revertError.data?.errorName ?? ''
|
|
// do something with `errorName`
|
|
}
|
|
}
|
|
}
|
|
|
|
```
|
|
|
|
```ts [abi.ts]
|
|
export const wagmiAbi = [
|
|
...
|
|
{
|
|
inputs: [],
|
|
name: "mint",
|
|
outputs: [{ name: "", type: "uint32" }],
|
|
stateMutability: "view",
|
|
type: "function",
|
|
},
|
|
// Custom solidity error
|
|
{
|
|
type: 'error',
|
|
inputs: [],
|
|
name: 'MintIsDisabled'
|
|
},
|
|
...
|
|
] as const;
|
|
```
|
|
|
|
```solidity [WagmiExample.sol]
|
|
// ...
|
|
error MintIsDisabled();
|
|
contract WagmiExample {
|
|
// ...
|
|
|
|
function mint() public {
|
|
// ...
|
|
revert MintIsDisabled();
|
|
// ...
|
|
}
|
|
|
|
// ...
|
|
}
|
|
```
|
|
|
|
```ts [config.ts]
|
|
import { createPublicClient, custom, http } from 'viem'
|
|
import { privateKeyToAccount } from 'viem/accounts'
|
|
import { mainnet } from 'viem/chains'
|
|
|
|
// JSON-RPC Account
|
|
export const account = '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'
|
|
// Local Account
|
|
export const account = privateKeyToAccount(...)
|
|
|
|
export const walletClient = createWalletClient({
|
|
chain: mainnet,
|
|
transport: custom(window.ethereum)
|
|
})
|
|
|
|
export const publicClient = createPublicClient({
|
|
chain: mainnet,
|
|
transport: http()
|
|
})
|
|
```
|
|
|
|
:::
|
|
|
|
### State Overrides
|
|
|
|
When using `simulateContract`, there sometimes needs to be an initial state change to make the transaction pass. A common use case would be an approval. For that, there are [state overrides](https://geth.ethereum.org/docs/interacting-with-geth/rpc/ns-eth#eth-call). In the example below, we are simulating sending a token on behalf of another user. To do this, we need to modify the state of the token contract to have maximum approve from the token owner.
|
|
|
|
:::code-group
|
|
|
|
```ts twoslash [example.ts]
|
|
import { account, publicClient } from './config'
|
|
import { abi, address } from './contract'
|
|
|
|
// Allowance slot: A 32 bytes hex string representing the allowance slot of the sender.
|
|
const allowanceSlot = '0x....'
|
|
|
|
// Max allowance: A 32 bytes hex string representing the maximum allowance (2^256 - 1)
|
|
const maxAllowance = numberToHex(maxUint256)
|
|
|
|
const { result } = await publicClient.simulateContract({
|
|
abi,
|
|
address,
|
|
account,
|
|
functionName: 'transferFrom',
|
|
args: [
|
|
'0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
|
|
account.address,
|
|
69420n
|
|
],
|
|
stateOverride: [ // [!code hl]
|
|
{ // [!code hl]
|
|
// modifying the state of the token contract // [!code hl]
|
|
address, // [!code hl]
|
|
stateDiff: [ // [!code hl]
|
|
{ // [!code hl]
|
|
slot: allowanceSlot, // [!code hl]
|
|
value: maxAllowance, // [!code hl]
|
|
}, // [!code hl]
|
|
], // [!code hl]
|
|
}, // [!code hl]
|
|
], // [!code hl]
|
|
})
|
|
|
|
console.log(result)
|
|
// @log: Output: true
|
|
```
|
|
|
|
```ts twoslash [contract.ts] filename="contract.ts"
|
|
export const address = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'
|
|
|
|
export const abi = [
|
|
{
|
|
type: 'function',
|
|
name: 'transferFrom',
|
|
stateMutability: 'nonpayable',
|
|
inputs: [
|
|
{
|
|
name: 'sender',
|
|
type: 'address',
|
|
},
|
|
{
|
|
name: 'recipient',
|
|
type: 'address',
|
|
},
|
|
{
|
|
name: 'amount',
|
|
type: 'uint256',
|
|
},
|
|
],
|
|
outputs: [
|
|
{
|
|
type: 'bool',
|
|
},
|
|
],
|
|
},
|
|
] as const
|
|
```
|
|
|
|
```ts twoslash [config.ts] filename="config.ts"
|
|
import { createPublicClient, custom, http } from 'viem'
|
|
import { privateKeyToAccount } from 'viem/accounts'
|
|
import { mainnet } from 'viem/chains'
|
|
|
|
export const account = privateKeyToAccount('0x...')
|
|
|
|
export const publicClient = createPublicClient({
|
|
chain: mainnet,
|
|
transport: http()
|
|
})
|
|
```
|
|
|
|
:::
|
|
|
|
## Return Value
|
|
|
|
The simulation result and write request. Type is inferred.
|
|
|
|
## Parameters
|
|
|
|
### address
|
|
|
|
- **Type:** [`Address`](/docs/glossary/types#address)
|
|
|
|
The contract address.
|
|
|
|
```ts
|
|
const { result } = await publicClient.simulateContract({
|
|
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2', // [!code focus]
|
|
abi: wagmiAbi,
|
|
functionName: 'mint',
|
|
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'
|
|
})
|
|
```
|
|
|
|
### abi
|
|
|
|
- **Type:** [`Abi`](/docs/glossary/types#abi)
|
|
|
|
The contract's ABI.
|
|
|
|
```ts
|
|
const { result } = await publicClient.simulateContract({
|
|
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
|
|
abi: wagmiAbi, // [!code focus]
|
|
functionName: 'mint',
|
|
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'
|
|
})
|
|
```
|
|
|
|
### functionName
|
|
|
|
- **Type:** `string`
|
|
|
|
A function to extract from the ABI.
|
|
|
|
```ts
|
|
const { result } = await publicClient.simulateContract({
|
|
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
|
|
abi: wagmiAbi,
|
|
functionName: 'mint', // [!code focus]
|
|
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'
|
|
})
|
|
```
|
|
|
|
### account
|
|
|
|
- **Type:** `Account | Address | null`
|
|
|
|
The Account to simulate the contract method from.
|
|
|
|
Accepts a [JSON-RPC Account](/docs/clients/wallet#json-rpc-accounts) or [Local Account (Private Key, etc)](/docs/clients/wallet#local-accounts-private-key-mnemonic-etc). If set to `null`, it is assumed that the transport will handle the filling the sender of the transaction.
|
|
|
|
```ts
|
|
const { result } = await publicClient.simulateContract({
|
|
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
|
|
abi: wagmiAbi,
|
|
functionName: 'mint',
|
|
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266' // [!code focus]
|
|
})
|
|
```
|
|
|
|
### accessList (optional)
|
|
|
|
- **Type:** [`AccessList`](/docs/glossary/types#accesslist)
|
|
|
|
The access list.
|
|
|
|
```ts
|
|
const { result } = await publicClient.simulateContract({
|
|
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
|
|
abi: wagmiAbi,
|
|
functionName: 'mint',
|
|
args: [69420],
|
|
accessList: [{ // [!code focus:4]
|
|
address: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
|
|
storageKeys: ['0x1'],
|
|
}],
|
|
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'
|
|
})
|
|
```
|
|
|
|
### authorizationList (optional)
|
|
|
|
- **Type:** `AuthorizationList`
|
|
|
|
Signed EIP-7702 Authorization list.
|
|
|
|
```ts
|
|
const authorization = await walletClient.signAuthorization({
|
|
contractAddress: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
|
|
})
|
|
|
|
const { result } = await publicClient.simulateContract({
|
|
address: account.address,
|
|
abi: wagmiAbi,
|
|
functionName: 'mint',
|
|
args: [69420],
|
|
authorizationList: [authorization], // [!code focus]
|
|
})
|
|
```
|
|
|
|
:::note
|
|
**References**
|
|
- [EIP-7702 Overview](/docs/eip7702)
|
|
- [`signAuthorization` Docs](/docs/eip7702/signAuthorization)
|
|
:::
|
|
|
|
### args (optional)
|
|
|
|
- **Type:** Inferred from ABI.
|
|
|
|
Arguments to pass to function call.
|
|
|
|
```ts
|
|
const { result } = await publicClient.simulateContract({
|
|
address: '0x1dfe7ca09e99d10835bf73044a23b73fc20623df',
|
|
abi: wagmiAbi,
|
|
functionName: 'balanceOf',
|
|
args: ['0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC'], // [!code focus]
|
|
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'
|
|
})
|
|
```
|
|
|
|
### blockNumber (optional)
|
|
|
|
- **Type:** `number`
|
|
|
|
The block number to perform the read against.
|
|
|
|
```ts
|
|
const { result } = await publicClient.simulateContract({
|
|
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
|
|
abi: wagmiAbi,
|
|
functionName: 'mint',
|
|
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'
|
|
blockNumber: 15121123n, // [!code focus]
|
|
})
|
|
```
|
|
|
|
### blockTag (optional)
|
|
|
|
- **Type:** `'latest' | 'earliest' | 'pending' | 'safe' | 'finalized'`
|
|
- **Default:** `'latest'`
|
|
|
|
The block tag to perform the read against.
|
|
|
|
```ts
|
|
const { result } = await publicClient.simulateContract({
|
|
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
|
|
abi: wagmiAbi,
|
|
functionName: 'mint',
|
|
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'
|
|
blockTag: 'safe', // [!code focus]
|
|
})
|
|
```
|
|
|
|
### dataSuffix (optional)
|
|
|
|
- **Type:** `Hex`
|
|
|
|
Data to append to the end of the calldata. Useful for adding a ["domain" tag](https://opensea.notion.site/opensea/Seaport-Order-Attributions-ec2d69bf455041a5baa490941aad307f).
|
|
|
|
```ts
|
|
const { result } = await publicClient.simulateContract({
|
|
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
|
|
abi: wagmiAbi,
|
|
functionName: 'mint',
|
|
args: [69420],
|
|
dataSuffix: '0xdeadbeef' // [!code focus]
|
|
})
|
|
```
|
|
|
|
### gas (optional)
|
|
|
|
- **Type:** `bigint`
|
|
|
|
The gas limit for the transaction.
|
|
|
|
```ts
|
|
await walletClient.writeContract({
|
|
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
|
|
abi: wagmiAbi,
|
|
functionName: 'mint',
|
|
args: [69420],
|
|
gas: 69420n, // [!code focus]
|
|
})
|
|
```
|
|
|
|
### gasPrice (optional)
|
|
|
|
- **Type:** `bigint`
|
|
|
|
The price (in wei) to pay per gas. Only applies to [Legacy Transactions](/docs/glossary/terms#legacy-transaction).
|
|
|
|
```ts
|
|
const { result } = await publicClient.simulateContract({
|
|
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
|
|
abi: wagmiAbi,
|
|
functionName: 'mint',
|
|
args: [69420],
|
|
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'
|
|
gasPrice: parseGwei('20'), // [!code focus]
|
|
})
|
|
```
|
|
|
|
### maxFeePerGas (optional)
|
|
|
|
- **Type:** `bigint`
|
|
|
|
Total fee per gas (in wei), inclusive of `maxPriorityFeePerGas`. Only applies to [EIP-1559 Transactions](/docs/glossary/terms#eip-1559-transaction)
|
|
|
|
```ts
|
|
const { result } = await publicClient.simulateContract({
|
|
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
|
|
abi: wagmiAbi,
|
|
functionName: 'mint',
|
|
args: [69420],
|
|
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'
|
|
maxFeePerGas: parseGwei('20'), // [!code focus]
|
|
})
|
|
```
|
|
|
|
### maxPriorityFeePerGas (optional)
|
|
|
|
- **Type:** `bigint`
|
|
|
|
Max priority fee per gas (in wei). Only applies to [EIP-1559 Transactions](/docs/glossary/terms#eip-1559-transaction)
|
|
|
|
```ts
|
|
const { result } = await publicClient.simulateContract({
|
|
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
|
|
abi: wagmiAbi,
|
|
functionName: 'mint',
|
|
args: [69420],
|
|
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'
|
|
maxFeePerGas: parseGwei('20'),
|
|
maxPriorityFeePerGas: parseGwei('2'), // [!code focus]
|
|
})
|
|
```
|
|
|
|
### nonce (optional)
|
|
|
|
- **Type:** `number`
|
|
|
|
Unique number identifying this transaction.
|
|
|
|
```ts
|
|
const { result } = await publicClient.simulateContract({
|
|
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
|
|
abi: wagmiAbi,
|
|
functionName: 'mint',
|
|
args: [69420],
|
|
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'
|
|
nonce: 69 // [!code focus]
|
|
})
|
|
```
|
|
|
|
### stateOverride (optional)
|
|
|
|
- **Type:** [`StateOverride`](/docs/glossary/types#stateoverride)
|
|
|
|
The state override set is an optional address-to-state mapping, where each entry specifies some state to be ephemerally overridden prior to executing the call.
|
|
|
|
> Note: By using state overrides, you simulate the contract based on a fake state. Using this is useful for testing purposes, but making a transaction based on the returned `request` object might fail regardless of the simulation result.
|
|
|
|
```ts
|
|
const data = await publicClient.simulateContract({
|
|
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
|
|
abi: wagmiAbi,
|
|
functionName: 'mint',
|
|
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'
|
|
stateOverride: [ // [!code focus]
|
|
{ // [!code focus]
|
|
address: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC', // [!code focus]
|
|
balance: parseEther('1'), // [!code focus]
|
|
stateDiff: [ // [!code focus]
|
|
{ // [!code focus]
|
|
slot: '0x3ea2f1d0abf3fc66cf29eebb70cbd4e7fe762ef8a09bcc06c8edf641230afec0', // [!code focus]
|
|
value: '0x00000000000000000000000000000000000000000000000000000000000001a4', // [!code focus]
|
|
}, // [!code focus]
|
|
], // [!code focus]
|
|
} // [!code focus]
|
|
], // [!code focus]
|
|
})
|
|
```
|
|
|
|
### value (optional)
|
|
|
|
- **Type:** `number`
|
|
|
|
Value in wei sent with this transaction.
|
|
|
|
```ts
|
|
const { result } = await publicClient.simulateContract({
|
|
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
|
|
abi: wagmiAbi,
|
|
functionName: 'mint',
|
|
args: [69420],
|
|
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'
|
|
value: parseEther('1') // [!code focus]
|
|
})
|
|
```
|
|
|
|
## Live Example
|
|
|
|
Check out the usage of `simulateContract` in the live [Writing to Contracts Example](https://stackblitz.com/github/wevm/viem/tree/main/examples/contracts_writing-to-contracts) below.
|
|
|
|
<iframe frameBorder="0" width="100%" height="500px" src="https://stackblitz.com/github/wevm/viem/tree/main/examples/contracts_writing-to-contracts?embed=1&file=index.ts&hideNavigation=1&hideDevTools=true&terminalHeight=0&ctl=1"></iframe>
|