VanillaJS library for Ethereum blockchain interactions with reactive primitives and utilities for building Ethereum applications
—
Comprehensive blockchain data fetching including balances, blocks, transactions, and contract state. Core read operations for dApps.
Retrieves native token or ERC-20 token balance for an account.
/**
* Gets account balance for native token or ERC-20 tokens
* @param config - Wagmi configuration
* @param parameters - Balance query parameters
* @returns Balance information with formatted value
*/
function getBalance<config extends Config>(
config: config,
parameters: GetBalanceParameters<config>
): Promise<GetBalanceReturnType>;
interface GetBalanceParameters<config extends Config> {
/** Account address to check balance for */
address: Address;
/** ERC-20 token contract address (optional, defaults to native token) */
token?: Address;
/** Block number or tag to query at */
blockNumber?: bigint;
/** Block tag ('latest', 'earliest', 'pending', 'safe', 'finalized') */
blockTag?: 'latest' | 'earliest' | 'pending' | 'safe' | 'finalized';
/** Chain ID to query on */
chainId?: config['chains'][number]['id'];
/** Unit for formatting (deprecated) */
unit?: Unit;
}
interface GetBalanceReturnType {
/** Token decimals */
decimals: number;
/** Formatted balance string */
formatted: string;
/** Token symbol */
symbol: string;
/** Raw balance value */
value: bigint;
}
type GetBalanceErrorType = BaseErrorType | viem_GetBalanceErrorType;
/** @deprecated Use getBalance instead */
const fetchBalance = getBalance;Usage Example:
import { getBalance } from '@wagmi/core'
// Get native ETH balance
const ethBalance = await getBalance(config, {
address: '0x742d35Cc6601C2F3Ac5e5c7A9d16e4e6Be4e6e9e',
})
console.log(`ETH Balance: ${ethBalance.formatted} ${ethBalance.symbol}`)
// Get ERC-20 token balance (USDC)
const usdcBalance = await getBalance(config, {
address: '0x742d35Cc6601C2F3Ac5e5c7A9d16e4e6Be4e6e9e',
token: '0xA0b86a33E6411c0B7f8C4b5d3e1B9d3e8b4a4e6f', // USDC contract
})
console.log(`USDC Balance: ${usdcBalance.formatted} ${usdcBalance.symbol}`)
// Query at specific block
const historicalBalance = await getBalance(config, {
address: '0x742d35Cc6601C2F3Ac5e5c7A9d16e4e6Be4e6e9e',
blockNumber: 18500000n,
})Retrieves block information by number, hash, or tag.
/**
* Gets block information
* @param config - Wagmi configuration
* @param parameters - Block query parameters (optional)
* @returns Block information
*/
function getBlock<config extends Config>(
config: config,
parameters?: GetBlockParameters<config>
): Promise<GetBlockReturnType>;
interface GetBlockParameters<config extends Config> {
/** Block number to retrieve */
blockNumber?: bigint;
/** Block hash to retrieve */
blockHash?: Hash;
/** Block tag */
blockTag?: 'latest' | 'earliest' | 'pending' | 'safe' | 'finalized';
/** Include transaction details */
includeTransactions?: boolean;
/** Chain ID to query */
chainId?: config['chains'][number]['id'];
}
interface GetBlockReturnType {
/** Block hash */
hash: Hash;
/** Block number */
number: bigint;
/** Parent block hash */
parentHash: Hash;
/** Timestamp */
timestamp: bigint;
/** Gas limit */
gasLimit: bigint;
/** Gas used */
gasUsed: bigint;
/** Miner/validator address */
miner: Address;
/** Transactions (hashes or full transactions) */
transactions: Hash[] | Transaction[];
/** Base fee per gas (EIP-1559) */
baseFeePerGas?: bigint;
/** Extra data */
extraData: Hex;
}
type GetBlockErrorType = BaseErrorType | viem_GetBlockErrorType;Usage Example:
import { getBlock } from '@wagmi/core'
// Get latest block
const latestBlock = await getBlock(config)
console.log('Latest block number:', latestBlock.number)
console.log('Block timestamp:', new Date(Number(latestBlock.timestamp) * 1000))
// Get specific block with transaction details
const blockWithTxs = await getBlock(config, {
blockNumber: 18500000n,
includeTransactions: true,
})
console.log('Transaction count:', blockWithTxs.transactions.length)
// Get block by hash
const blockByHash = await getBlock(config, {
blockHash: '0x1234567890abcdef...',
})Retrieves the latest block number.
/**
* Gets current block number
* @param config - Wagmi configuration
* @param parameters - Block number query parameters (optional)
* @returns Current block number
*/
function getBlockNumber<config extends Config>(
config: config,
parameters?: GetBlockNumberParameters<config>
): Promise<GetBlockNumberReturnType>;
interface GetBlockNumberParameters<config extends Config> {
/** Chain ID to query */
chainId?: config['chains'][number]['id'];
/** Cache time in milliseconds */
cacheTime?: number;
}
type GetBlockNumberReturnType = bigint;
type GetBlockNumberErrorType = BaseErrorType | viem_GetBlockNumberErrorType;
/** @deprecated Use getBlockNumber instead */
const fetchBlockNumber = getBlockNumber;Retrieves transaction details by hash.
/**
* Gets transaction details by hash
* @param config - Wagmi configuration
* @param parameters - Transaction query parameters
* @returns Transaction information
*/
function getTransaction<config extends Config>(
config: config,
parameters: GetTransactionParameters<config>
): Promise<GetTransactionReturnType>;
interface GetTransactionParameters<config extends Config> {
/** Transaction hash */
hash: Hash;
/** Chain ID to query */
chainId?: config['chains'][number]['id'];
}
interface GetTransactionReturnType {
/** Transaction hash */
hash: Hash;
/** Block hash */
blockHash?: Hash;
/** Block number */
blockNumber?: bigint;
/** Transaction index in block */
transactionIndex?: number;
/** From address */
from: Address;
/** To address */
to?: Address;
/** Value sent */
value: bigint;
/** Gas limit */
gas: bigint;
/** Gas price */
gasPrice?: bigint;
/** Max fee per gas (EIP-1559) */
maxFeePerGas?: bigint;
/** Max priority fee per gas (EIP-1559) */
maxPriorityFeePerGas?: bigint;
/** Transaction data */
input: Hex;
/** Nonce */
nonce: number;
/** Transaction type */
type: 'legacy' | 'eip2930' | 'eip1559';
}
type GetTransactionErrorType = BaseErrorType | viem_GetTransactionErrorType;
/** @deprecated Use getTransaction instead */
const fetchTransaction = getTransaction;Retrieves transaction receipt with execution results.
/**
* Gets transaction receipt
* @param config - Wagmi configuration
* @param parameters - Receipt query parameters
* @returns Transaction receipt
*/
function getTransactionReceipt<config extends Config>(
config: config,
parameters: GetTransactionReceiptParameters<config>
): Promise<GetTransactionReceiptReturnType>;
interface GetTransactionReceiptParameters<config extends Config> {
/** Transaction hash */
hash: Hash;
/** Chain ID to query */
chainId?: config['chains'][number]['id'];
}
interface GetTransactionReceiptReturnType {
/** Transaction hash */
transactionHash: Hash;
/** Block hash */
blockHash: Hash;
/** Block number */
blockNumber: bigint;
/** Transaction index */
transactionIndex: number;
/** From address */
from: Address;
/** To address */
to?: Address;
/** Gas used */
gasUsed: bigint;
/** Effective gas price */
effectiveGasPrice: bigint;
/** Status (1 = success, 0 = failure) */
status: 'success' | 'reverted';
/** Event logs */
logs: Log[];
/** Contract address (if deployment) */
contractAddress?: Address;
/** Cumulative gas used */
cumulativeGasUsed: bigint;
}
type GetTransactionReceiptErrorType = BaseErrorType | viem_GetTransactionReceiptErrorType;Usage Example:
import { getTransaction, getTransactionReceipt } from '@wagmi/core'
// Get transaction details
const tx = await getTransaction(config, {
hash: '0x1234567890abcdef...',
})
console.log('Transaction from:', tx.from)
console.log('Transaction to:', tx.to)
console.log('Value:', tx.value.toString())
// Get transaction receipt
const receipt = await getTransactionReceipt(config, {
hash: '0x1234567890abcdef...',
})
console.log('Transaction status:', receipt.status)
console.log('Gas used:', receipt.gasUsed.toString())
console.log('Logs:', receipt.logs.length)Retrieves current gas pricing information.
/**
* Gets current gas price
* @param config - Wagmi configuration
* @param parameters - Gas price query parameters (optional)
* @returns Current gas price
*/
function getGasPrice<config extends Config>(
config: config,
parameters?: GetGasPriceParameters<config>
): Promise<GetGasPriceReturnType>;
interface GetGasPriceParameters<config extends Config> {
/** Chain ID to query */
chainId?: config['chains'][number]['id'];
}
type GetGasPriceReturnType = bigint;
/**
* Gets EIP-1559 fee estimates
* @param config - Wagmi configuration
* @param parameters - Fee estimation parameters (optional)
* @returns Fee per gas estimates
*/
function estimateFeesPerGas<config extends Config>(
config: config,
parameters?: EstimateFeesPerGasParameters<config>
): Promise<EstimateFeesPerGasReturnType>;
interface EstimateFeesPerGasParameters<config extends Config> {
/** Chain ID to query */
chainId?: config['chains'][number]['id'];
/** Fee multiplier */
formatUnits?: number;
}
interface EstimateFeesPerGasReturnType {
/** Max fee per gas */
maxFeePerGas: bigint;
/** Max priority fee per gas */
maxPriorityFeePerGas: bigint;
}
/**
* Gets max priority fee per gas estimate
* @param config - Wagmi configuration
* @param parameters - Priority fee parameters (optional)
* @returns Max priority fee per gas
*/
function estimateMaxPriorityFeePerGas<config extends Config>(
config: config,
parameters?: EstimateMaxPriorityFeePerGasParameters<config>
): Promise<EstimateMaxPriorityFeePerGasReturnType>;
interface EstimateMaxPriorityFeePerGasParameters<config extends Config> {
/** Chain ID to query */
chainId?: config['chains'][number]['id'];
}
type EstimateMaxPriorityFeePerGasReturnType = bigint;Usage Example:
import { getGasPrice, estimateFeesPerGas, estimateMaxPriorityFeePerGas } from '@wagmi/core'
// Get current gas price (legacy)
const gasPrice = await getGasPrice(config)
console.log('Gas price:', gasPrice.toString(), 'wei')
// Get EIP-1559 fees
const fees = await estimateFeesPerGas(config)
console.log('Max fee per gas:', fees.maxFeePerGas.toString())
console.log('Max priority fee:', fees.maxPriorityFeePerGas.toString())
// Get just priority fee
const priorityFee = await estimateMaxPriorityFeePerGas(config)
console.log('Priority fee:', priorityFee.toString())Retrieves historical fee data for gas estimation.
/**
* Gets fee history for gas estimation
* @param config - Wagmi configuration
* @param parameters - Fee history parameters
* @returns Historical fee data
*/
function getFeeHistory<config extends Config>(
config: config,
parameters: GetFeeHistoryParameters<config>
): Promise<GetFeeHistoryReturnType>;
interface GetFeeHistoryParameters<config extends Config> {
/** Number of blocks to include */
blockCount: number;
/** Reward percentiles to calculate */
rewardPercentiles: number[];
/** Chain ID to query */
chainId?: config['chains'][number]['id'];
}
interface GetFeeHistoryReturnType {
/** Base fee per gas for each block */
baseFeePerGas: bigint[];
/** Gas used ratio for each block */
gasUsedRatio: number[];
/** Reward percentiles for each block */
reward?: bigint[][];
}
type GetFeeHistoryErrorType = BaseErrorType | viem_GetFeeHistoryErrorType;Access contract storage and bytecode.
/**
* Gets storage value at specific slot
* @param config - Wagmi configuration
* @param parameters - Storage query parameters
* @returns Storage value
*/
function getStorageAt<config extends Config>(
config: config,
parameters: GetStorageAtParameters<config>
): Promise<GetStorageAtReturnType>;
interface GetStorageAtParameters<config extends Config> {
/** Contract address */
address: Address;
/** Storage slot */
slot: Hex;
/** Block number to query at */
blockNumber?: bigint;
/** Block tag */
blockTag?: 'latest' | 'earliest' | 'pending' | 'safe' | 'finalized';
/** Chain ID to query */
chainId?: config['chains'][number]['id'];
}
type GetStorageAtReturnType = Hex;
/**
* Gets contract bytecode
* @param config - Wagmi configuration
* @param parameters - Bytecode query parameters
* @returns Contract bytecode
*/
function getBytecode<config extends Config>(
config: config,
parameters: GetBytecodeParameters<config>
): Promise<GetBytecodeReturnType>;
interface GetBytecodeParameters<config extends Config> {
/** Contract address */
address: Address;
/** Block number to query at */
blockNumber?: bigint;
/** Block tag */
blockTag?: 'latest' | 'earliest' | 'pending' | 'safe' | 'finalized';
/** Chain ID to query */
chainId?: config['chains'][number]['id'];
}
type GetBytecodeReturnType = Hex | undefined;
type GetStorageAtErrorType = BaseErrorType | viem_GetStorageAtErrorType;
type GetBytecodeErrorType = BaseErrorType | viem_GetBytecodeErrorType;Retrieves ERC-20 token metadata.
/**
* Gets ERC-20 token information
* @param config - Wagmi configuration
* @param parameters - Token query parameters
* @returns Token information
*/
function getToken<config extends Config>(
config: config,
parameters: GetTokenParameters<config>
): Promise<GetTokenReturnType>;
interface GetTokenParameters<config extends Config> {
/** Token contract address */
address: Address;
/** Chain ID to query */
chainId?: config['chains'][number]['id'];
/** Format units for decimals */
formatUnits?: 'ether' | 'gwei' | 'wei' | number;
}
interface GetTokenReturnType {
/** Token contract address */
address: Address;
/** Token decimals */
decimals: number;
/** Token name */
name: string;
/** Token symbol */
symbol: string;
/** Total supply */
totalSupply: {
formatted: string;
value: bigint;
};
}
type GetTokenErrorType = BaseErrorType | viem_GetTokenErrorType;
/** @deprecated Use getToken instead */
const fetchToken = getToken;Usage Example:
import { getToken } from '@wagmi/core'
// Get USDC token information
const usdc = await getToken(config, {
address: '0xA0b86a33E6411c0B7f8C4b5d3e1B9d3e8b4a4e6f',
})
console.log('Token info:', {
name: usdc.name,
symbol: usdc.symbol,
decimals: usdc.decimals,
totalSupply: usdc.totalSupply.formatted,
})Additional blockchain data queries.
/**
* Gets transaction count (nonce) for account
* @param config - Wagmi configuration
* @param parameters - Transaction count parameters
* @returns Transaction count
*/
function getTransactionCount<config extends Config>(
config: config,
parameters: GetTransactionCountParameters<config>
): Promise<GetTransactionCountReturnType>;
interface GetTransactionCountParameters<config extends Config> {
/** Account address */
address: Address;
/** Block number or tag */
blockNumber?: bigint;
blockTag?: 'latest' | 'earliest' | 'pending' | 'safe' | 'finalized';
/** Chain ID to query */
chainId?: config['chains'][number]['id'];
}
type GetTransactionCountReturnType = number;
/**
* Gets transaction confirmations
* @param config - Wagmi configuration
* @param parameters - Confirmation query parameters
* @returns Number of confirmations
*/
function getTransactionConfirmations<config extends Config>(
config: config,
parameters: GetTransactionConfirmationsParameters<config>
): Promise<GetTransactionConfirmationsReturnType>;
interface GetTransactionConfirmationsParameters<config extends Config> {
/** Transaction hash */
hash: Hash;
/** Chain ID to query */
chainId?: config['chains'][number]['id'];
}
type GetTransactionConfirmationsReturnType = bigint;
/**
* Gets block transaction count
* @param config - Wagmi configuration
* @param parameters - Block transaction count parameters
* @returns Transaction count in block
*/
function getBlockTransactionCount<config extends Config>(
config: config,
parameters?: GetBlockTransactionCountParameters<config>
): Promise<GetBlockTransactionCountReturnType>;
interface GetBlockTransactionCountParameters<config extends Config> {
/** Block number */
blockNumber?: bigint;
/** Block hash */
blockHash?: Hash;
/** Block tag */
blockTag?: 'latest' | 'earliest' | 'pending' | 'safe' | 'finalized';
/** Chain ID to query */
chainId?: config['chains'][number]['id'];
}
type GetBlockTransactionCountReturnType = number;Usage Example:
import {
getTransactionCount,
getTransactionConfirmations,
getBlockTransactionCount
} from '@wagmi/core'
// Get account nonce
const nonce = await getTransactionCount(config, {
address: '0x742d35Cc6601C2F3Ac5e5c7A9d16e4e6Be4e6e9e',
})
console.log('Account nonce:', nonce)
// Get transaction confirmations
const confirmations = await getTransactionConfirmations(config, {
hash: '0x1234567890abcdef...',
})
console.log('Confirmations:', confirmations.toString())
// Get transaction count in latest block
const txCount = await getBlockTransactionCount(config)
console.log('Transactions in latest block:', txCount)Install with Tessl CLI
npx tessl i tessl/npm-wagmi--core