Repository navigation
Replies: 2 comments
|
No, you do not need a wallet or signer to estimate gas. The key detail: you need to pass the import { createPublicClient, http, parseEther } from 'viem';
import { mainnet } from 'viem/chains';
const client = createPublicClient({
chain: mainnet,
transport: http(),
});
// Estimate gas WITHOUT a wallet — just pass the sender address
const gasEstimate = await client.estimateGas({
account: '0xYourAddressHere', // just an address string, not a signer
to: '0xRecipientAddress',
value: parseEther('0.1'),
});
console.log('Estimated gas:', gasEstimate); // bigintFor contract callsimport { parseAbi } from 'viem';
const abi = parseAbi(['function transfer(address to, uint256 amount) returns (bool)']);
const gas = await client.estimateContractGas({
address: '0xTokenContractAddress',
abi,
functionName: 'transfer',
args: ['0xRecipient', 1000000n],
account: '0xYourAddressHere', // address string is fine
});When you DO need a walletOnly if you want viem to automatically populate the import { createWalletClient, custom } from 'viem';
const walletClient = createWalletClient({
chain: mainnet,
transport: custom(window.ethereum!),
account: '0x...', // or from wallet
});
// Now `account` is implicit — pulled from walletClient
const gas = await walletClient.estimateGas({
to: '0xRecipient',
value: parseEther('0.1'),
// account is auto-populated
});TL;DR: |
|
You don't need a wallet, but the error depends on what kind of
With fee fields in the request, the node checks that the balance covers So if await client.estimateGas({ account: from.address, to, value: amount, data })
// or
await client.estimateGas({ account: from, to, value: amount, data, prepare: false })Both send the same request as your |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hello, was doing some research and it seems like viem requires you to have an account or a wallet on the publicClient for gas estimations, and it fails the call if it's not present saying the gas of the account is insufficient.
Even though I only want an estimate and not to execute anything. Though I've switched it to a RPC call and everything seems to work.
RPC
Viem
Is there some workaround or am I missing something with the viem call?
All reactions