How to Connect to Somnia Network via Viem Library.
Last updated
Last updated
Somnia empowers developers to build applications for mass adoption. Smart Contracts deployed on Somnia will require front-end user interfaces to interact with them. These front-end user interfaces will require middleware libraries to establish a connection to the Somnia Network and enable interaction with Smart Contracts. In this Guide, you will learn how to use the Viem Library to establish a connection between your deployed Smart Contracts on Somnia Network and your Front-end User application. You will also learn how to perform READ and WRITE operations using Viem. Viem is a TypeScript interface for Ethereum that provides low-level stateless primitives for interacting with Ethereum.
When a Smart Contract is programmed using any development tool such as RemixIDE, Hardhat or Foundry, the Smart Contract undergoes a “compilation” stage. Compiling a Smart Contract, among other things will convert the Solidity code into machine-readable bytecode. An ABI file is also produced when a Smart Contrac is compiled. ABI stand for Application Binary Interface. You can think of an ABI like the Interface that make it possible for a User Interface to connect with the Smart Contract functions in a way similar to how an API makes it possible to to connect a UI and Backend server in web2.
Here is an example `Greeter.sol` Smart Contract:
When the Greeter Smart Contract is compiled, below is its ABI:
The ABI is an array of JSON objects containing the constructor, event, and four functions in the Smart Contract. Using a Library such as Viem, you can perform READ and WRITE operations, for each of the ABI objects. You can READ the events, and other “view” only methods. You can perform WRITE operations on the “changeName” function. A cursory look at each ABI method will help you understand the function and what can be accomplished by interacting with the method. For example:
To use Viem, it has to be installed in the project directory where you want to perform READ and WRITE operations. First, create a directory and initialize a new project using npm.
Initialize a project in the directory by running the command:
Install Viem by running the following command.
To connect to the deployed Example Greeter Smart Contract using Viem, it is necessary to have access to the Smart Contract’s ABI and its Contract Address. Viem sets up a “transport
” infrastructure to connect with a node in the EVM Network and the deployed Smart Contracts.
We will use some Viem methods to connect to your Smart Contract deployed on the Somnia Network. Viem has a `createPublicClient` and a `createWalletClient` method. The PublicClient is used to perform READ operations, while the WalletClient is used to perform WRITE operations.
Create a new file index.js
Import the method classes from the Library:
The http
is the transport protocol for interacting with the Node of the Somnia Blockchain via RPC. It uses the default Somnia RPC URL: https://dream-rpc.somnia.network
. In the future developers can use RPC providers to avoid rate limiting.
To connect to Somnia, we will use the defineChain
method. Add the following chain definition variable:
We will start with setting up the publicClient
to read view
only methods. Set up a publicClient
where the default Transport is http
and chain
is SOMNIA
network created using the defineChain
method.
Now that you have a Client set up, you can interact with Somnia Blockchain and consume Actions!
An example will be to call the greet
method on the deployed Smart Contract.
To do this, we have to create a file name abi.js
and add the exported ABI in the file.
In the index.js
we can import the ABI file and start calling methods on the deployed Smart Contract. Import the ABI:
Set Contract Address
This is an example Greeter Smart Contract deployed on Somnia DevNet
Write a Function `interactWithContract`:
Open your terminal and run the following:
You will see the response from the Smart Contract logged into the Console!
Congratulations, you have successfully performed a READ operation on your Smart Contract deployed on Somnia.
To perform a write operation, we will parse the `createWalletClient` method to a `walletClient` variable. It is important to understand that carrying out WRITE operations changes the state of the Blockchain, unlike READ operations, where you read the state of the Blockchain. So, to perform WRITE operations, a user will have to spend Gas, and to be able to spend Gas, a user will have to parse his Private Key from an EOA to give the Library masked permission to carry out transactions on behalf of the user. To read the Private Key from an EOA, we will use a Viem method:
Then, create a variable walletClient
The variable `$YOUR_PRIVATE_KEY` variable can be parsed using a dotenv file.
After sending a WRITE operation, we also have to be able to read the transaction to see the state changes. We will rely on a READ method to read a transaction, `waitForTransactionReceipt`. Update the `interactWithContract` function with the code below:
Save the file and run the node command to see your responses logged into the console.
Congratulations, you have successfully performed a WRITE operation on your Smart Contract deployed on Somnia. 🎉