estimateTotalGas
Estimates the amount of L1 + L2 gas required to execute an L2 transaction.
It is the sum of estimateL1Gas
(L1 Gas) and estimateGas
(L2 Gas).
Usage
ts
import { account, publicClient } from './config'
const gas = await publicClient.estimateTotalGas({
account,
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: parseEther('1')
})
import { account, publicClient } from './config'
const gas = await publicClient.estimateTotalGas({
account,
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: parseEther('1')
})
ts
import { createPublicClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { base } from 'viem/chains'
import { opStackPublicActions } from 'viem/opStack'
// JSON-RPC Account
export const account = '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'
// Local Account
export const account = privateKeyToAccount(...)
export const publicClient = createPublicClient({
chain: base,
transport: http()
}).extend(opStackPublicActions)
import { createPublicClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { base } from 'viem/chains'
import { opStackPublicActions } from 'viem/opStack'
// JSON-RPC Account
export const account = '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'
// Local Account
export const account = privateKeyToAccount(...)
export const publicClient = createPublicClient({
chain: base,
transport: http()
}).extend(opStackPublicActions)
Returns
bigint
The total (L1 + L2) gas estimate.
Parameters
account
- Type:
Account | Address
The Account to estimate gas from.
Accepts a JSON-RPC Account or Local Account (Private Key, etc).
ts
const gas = await publicClient.estimateTotalGas({
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: parseEther('1')
})
const gas = await publicClient.estimateTotalGas({
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: parseEther('1')
})
data (optional)
- Type:
0x${string}
Contract code or a hashed method call with encoded args.
ts
const gas = await publicClient.estimateTotalGas({
data: '0x...',
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: parseEther('1')
})
const gas = await publicClient.estimateTotalGas({
data: '0x...',
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: parseEther('1')
})
gasPriceOracleAddress (optional)
- Type:
Address
Address of the Gas Price Oracle predeploy contract.
ts
const gas = await publicClient.estimateTotalGas({
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
gasPriceOracleAddress: '0x420000000000000000000000000000000000000F',
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: parseEther('1')
})
const gas = await publicClient.estimateTotalGas({
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
gasPriceOracleAddress: '0x420000000000000000000000000000000000000F',
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: parseEther('1')
})
maxFeePerGas (optional)
- Type:
bigint
Total fee per gas (in wei), inclusive of maxPriorityFeePerGas
.
ts
const gas = await publicClient.estimateTotalGas({
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
maxFeePerGas: parseGwei('20'),
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: parseEther('1')
})
const gas = await publicClient.estimateTotalGas({
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
maxFeePerGas: parseGwei('20'),
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: parseEther('1')
})
maxPriorityFeePerGas (optional)
- Type:
bigint
Max priority fee per gas (in wei).
ts
const gas = await publicClient.estimateTotalGas({
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
maxFeePerGas: parseGwei('20'),
maxPriorityFeePerGas: parseGwei('2'),
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: parseEther('1')
})
const gas = await publicClient.estimateTotalGas({
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
maxFeePerGas: parseGwei('20'),
maxPriorityFeePerGas: parseGwei('2'),
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: parseEther('1')
})
nonce (optional)
- Type:
number
Unique number identifying this transaction.
ts
const fee = await publicClient.estimateTotalGas({
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
maxFeePerGas: parseGwei('20'),
maxPriorityFeePerGas: parseGwei('2'),
nonce: 69,
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: parseEther('1')
})
const fee = await publicClient.estimateTotalGas({
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
maxFeePerGas: parseGwei('20'),
maxPriorityFeePerGas: parseGwei('2'),
nonce: 69,
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: parseEther('1')
})
to (optional)
- Type:
Address
Transaction recipient.
ts
const gas = await publicClient.estimateTotalGas({
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: parseEther('1')
})
const gas = await publicClient.estimateTotalGas({
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: parseEther('1')
})
value (optional)
- Type:
bigint
Value (in wei) sent with this transaction.
ts
const gas = await publicClient.estimateTotalGas({
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: parseEther('1')
})
const gas = await publicClient.estimateTotalGas({
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: parseEther('1')
})