Files
web/apps/base-docs/docs/tools/viem.md
taycaldwell 0650a43c93 Add description and keyword metadata for SEO (#148)
* Add description and keyword metadata for SEO

* Update deploy-smart-contracts.mdx

* Update metadata

* Update connecting-to-the-blockchain.md

* Attempt #123 to wakeup Heimdall

* Fix formatting

* Remove file
2023-11-22 15:57:03 -05:00

3.7 KiB

title, slug, description, keywords
title slug description keywords
viem /tools/viem Documentation for using Viem, a TypeScript interface for EVM-compatible blockchains. This page covers installation, setup, and various functionalities such as reading and writing blockchain data and interacting with smart contracts on Base.
Viem
Base
Base mainnet
Base testnet
Ethereum
smart contracts
blockchain
RPC URL
JavaScript
TypeScript

viem

:::info

Viem is currently only available on Base Goerli testnet.

:::

viem a TypeScript interface for Ethereum that provides low-level stateless primitives for interacting with Ethereum.

You can use viem to interact with smart contracts deployed on Base.


Install

To install viem run the following command:

npm install --save viem

Setup

Before you can start using viem, you need to setup a Client with a desired Transport and Chain.

import { createPublicClient, http } from 'viem';
import { base } from 'viem/chains';

const client = createPublicClient({
  chain: base,
  transport: http(),
});

:::info

To use Base, you must specify base as the chain when creating a Client.

To use Base Goerli (testnet), replace base with baseGoerli.

:::

Reading data from the blockchain

Once you have created a client, you can use it to read and access data from Base using Public Actions

Public Actions are client methods that map one-to-one with a "public" Ethereum RPC method (eth_blockNumber, eth_getBalance, etc.)

For example, you can use the getBlockNumber client method to get the latest block:

const blockNumber = await client.getBlockNumber();

Writing data to the blockchain

In order to write data to Base, you need to create a Wallet client (createWalletClient) and specify an Account to use.

import { createWalletClient, custom } from 'viem'
import { base } from 'viem/chains'

//highlight-start
const [account] = await window.ethereum.request({ method: 'eth_requestAccounts' })
//highlight-end

const client = createWalletClient({
  //highlight-next-line
  account,
  chain: base,
  transport: custom(window.ethereum)
})

client.sendTransaction({ ... })

:::info

In addition to making a JSON-RPC request (eth_requestAccounts) to get an Account, viem provides various helper methods for creating an Account, including: privateKeyToAccount, mnemonicToAccount, and hdKeyToAccount.

To use Base Goerli (testnet), replace base with baseGoerli.

:::

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 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();

:::info

CONTRACT_ADDRESS is the address of the deployed contract.

:::