viem
viem is a TypeScript Interface for Ethereum that provides low-level stateless primitives for interacting with Ethereum. An alternative to ethers.js and web3.js with a focus on reliability, efficiency, and excellent developer experience.
Instsall
npm i viem
Setup
Before you can start using viem, you need to setup a Client with a desired Transport and Chain.
// 1. Import modules.
import { createPublicClient, http } from 'viem'
import { telos } from 'viem/chains'
// import { telosTestnet } from 'viem/chains' for Telos testnet
// 2. Set up your client with desired chain & transport.
const client = createPublicClient({
chain: telos,
transport: http(),
})
Reading Data
Now that the Client is set up, you can perform traditional RPC calls, or what viem calls Public Actions like getting the block number.
const blockNumber = await client.getBlockNumber()
Writing Data
In order to write data to Telos, you need to create a Wallet Client and specify an Account to use.
import { createWalletClient, custom } from 'viem'
import { telos } from 'viem/chains'
const [account] = await window.ethereum.request({ method: 'eth_requestAccounts' })
const client = createWalletClient({
account,
chain: telos,
transport: custom(window.ethereum)
})
client.sendTransaction({ ... })
Interacting With Smart Contracts
You can use viem to interact with a smart contract on Base by creating a Contract instance using getContract
and passing it the contract ABI, contract address, and Public and/or Wallet Client:
import { getContract } from 'viem';
import { wagmiAbi } from './abi';
import { publicClient } from './client';
// 1. Create contract instance
const contract = getContract({
address: 'CONTRACT_ADDRESS',
abi: wagmiAbi,
publicClient,
});
// 2. Call contract methods, listen to events, etc.
const result = await contract.read.totalSupply();