clawchain/viem/site/pages/docs/contract/estimateContractGas.md

360 lines
7.9 KiB
Markdown

---
description: Estimates the gas required to successfully execute a contract write function call.
---
# estimateContractGas
Estimates the gas required to successfully execute a contract write function call.
Internally, `estimateContractGas` uses a [Public Client](/docs/clients/public) to call the [`estimateGas` action](/docs/actions/public/estimateGas) with [ABI-encoded `data`](/docs/contract/encodeFunctionData).
## Usage
Below is a very basic example of how to estimate gas (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 gas = await publicClient.estimateContractGas({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: wagmiAbi,
functionName: 'mint',
account,
})
// 69420n
```
```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] {8}
import { account, publicClient } from './config'
import { wagmiAbi } from './abi'
const gas = await publicClient.estimateContractGas({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: wagmiAbi,
functionName: 'mint',
args: [69420],
account,
})
// 69420n
```
```ts [abi.ts]
export const wagmiAbi = [
...
{
inputs: [{ name: "owner", type: "uint32" }],
name: "mint",
outputs: [{ name: "", type: "uint32" }],
stateMutability: "nonpayable",
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()
})
```
:::
## Return Value
`bigint`
The gas estimate.
## Parameters
### address
- **Type:** [`Address`](/docs/glossary/types#address)
The contract address.
```ts
const gas = await publicClient.estimateContractGas({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2', // [!code focus]
abi: wagmiAbi,
functionName: 'mint',
account,
})
```
### abi
- **Type:** [`Abi`](/docs/glossary/types#abi)
The contract's ABI.
```ts
const gas = await publicClient.estimateContractGas({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: wagmiAbi, // [!code focus]
functionName: 'mint',
account,
})
```
### functionName
- **Type:** `string`
A function to extract from the ABI.
```ts
const gas = await publicClient.estimateContractGas({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: wagmiAbi,
functionName: 'mint', // [!code focus]
account,
})
```
### account
- **Type:** `Account | Address`
The Account to estimate gas 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).
```ts
const gas = await publicClient.estimateContractGas({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: wagmiAbi,
functionName: 'mint',
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266' // [!code focus]
})
```
### accessList (optional)
- **Type:** [`AccessList`](/docs/glossary/types#accesslist)
The access list.
```ts
const gas = await publicClient.estimateContractGas({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: wagmiAbi,
functionName: 'mint',
args: [69420],
accessList: [{ // [!code focus:4]
address: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
storageKeys: ['0x1'],
}],
account,
})
```
### args (optional)
- **Type:** Inferred from ABI.
Arguments to pass to function call.
```ts
const gas = await publicClient.estimateContractGas({
address: '0x1dfe7ca09e99d10835bf73044a23b73fc20623df',
abi: wagmiAbi,
functionName: 'balanceOf',
args: ['0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC'], // [!code focus]
account,
})
```
### 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 gas = await publicClient.estimateContractGas({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: wagmiAbi,
functionName: 'mint',
args: [69420],
dataSuffix: '0xdeadbeef' // [!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 gas = await publicClient.estimateContractGas({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: wagmiAbi,
functionName: 'mint',
args: [69420],
account,
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 gas = await publicClient.estimateContractGas({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: wagmiAbi,
functionName: 'mint',
args: [69420],
account,
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 gas = await publicClient.estimateContractGas({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: wagmiAbi,
functionName: 'mint',
args: [69420],
account,
maxFeePerGas: parseGwei('20'),
maxPriorityFeePerGas: parseGwei('2'), // [!code focus]
})
```
### nonce (optional)
- **Type:** `number`
Unique number identifying this transaction.
```ts
const gas = await publicClient.estimateContractGas({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: wagmiAbi,
functionName: 'mint',
args: [69420],
account,
nonce: 69 // [!code focus]
})
```
### value (optional)
- **Type:** `number`
Value in wei sent with this transaction.
```ts
const gas = await publicClient.estimateContractGas({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: wagmiAbi,
functionName: 'mint',
args: [69420],
account,
value: parseEther('1') // [!code focus]
})
```
### blockNumber (optional)
- **Type:** `number`
The block number to perform the read against.
```ts
const gas = await publicClient.estimateContractGas({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: wagmiAbi,
functionName: 'mint',
account,
blockNumber: 15121123n, // [!code focus]
})
```
### blockTag (optional)
- **Type:** `'latest' | 'earliest' | 'pending' | 'safe' | 'finalized'`
- **Default:** `'latest'`
The block tag to perform the read against.
```ts
const gas = await publicClient.estimateContractGas({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: wagmiAbi,
functionName: 'mint',
account,
blockTag: 'safe', // [!code focus]
})
```