Interacting with Rootstock using Viem
Viem offers a minimalist, lightweight, and more efficient Typescript interface alternative to Ethers.js and Web3.js for interacting with Ethereum nodes. It maintains a bundle size of 35KB compared to Ethers and Web3.js, which are over 300KB and 600KB, respectively. Smaller bundle size implies faster page loads and better performance on mobile. Viem also offers more granular control over APIs in contrast to Ethers.js, which abstracts APIs for more functionalities.
This tutorial will teach you how to use Viem to interact with your contracts on Rootstock.
Installation and Setup
In this guide, you will learn how to interact with an existing lending contract on the Rootstock Explorer, rLending RBTC. The full code of the working demo is available on GitHub.
You can use the following command to install the Viem npm package:
npm install viem
For more installation options, you can check out the official installation guide.
Using the Public Client
You need to set up your Client in Viem, which would have been your Provider in a typical Ethers.js project. A Client allows you to access Viem actions for interacting with the blockchain. One type of Client is the Public Client which provides access to public actions like the getBlockNumber and getBalance. The following snippet indicates how you would use the Public Client in an existing project.
import { createPublicClient, http } from 'viem'
import { rootstockTestnet } from 'viem/chains';
const RPC_URL = process.env.RPC_URL;
const publicClient = createPublicClient({
chain: rootstockTestnet,
transport: http(RPC_URL),
})
Explanation:
- You need to define a
ChainandTransportfor Rootstock-specific values. - Chain values for the Rootstock chain could be
rootstockfor the mainnet orrootstockTestnetfor the Testnet - It is recommended to set your
RPC_URLas an environmental variable whose value can be obtained from the Rootstock RPC API dashboard.
RPC_URL='https://rpc.testnet.rootstock.io/<API_KEY>'
CONTRACT_ADDRESS='0x...' // You can also add the contract address as an environment variable
If you are working on a Next.js project, environment variables must be prepended with NEXT_PUBLIC_ as NEXT_PUBLIC_RPC_URL for the RPC URL variable.
To access a deployed contract with its ABI using the Viem's getContract method.
import { profileContractAbi } from './contractABI';
const lendingContract = getContract({
abi: profileContractAbi,
address: getAddress(CONTRACT_ADDRESS),
client: publicClient
})
Then, read the borrow rate:
export async function fetchBorrowRatePerBlock(): Promise<bigint> {
const result = await publicClient.readContract({
address: getAddress(CONTRACT_ADDRESS),
abi: profileContractAbi,
functionName: 'borrowRatePerBlock',
})
return result as bigint;
}
You would get a response like the following in bigint data type:
67627655645n
To use some other read contract functions like fetchTotalSupply and fetchTotalBorrows:
export async function fetchTotalSupply(): Promise<bigint> {
return await lendingContract.read.totalSupply() as bigint;
}
export async function fetchTotalBorrows(): Promise<bigint> {
return await lendingContract.read.totalBorrows() as bigint;
}
export async function fetchSymbol(): Promise<string> {
return await lendingContract.read.symbol() as string;
}