Help & Support

Viem SDK

Interact with the Lens Chain using Viem. A template web app can be found here.

The Lens Chain SDK offers first-class support for Viem, a popular TypeScript Interface for Ethereum. Specifically, it adopts Viem paradigms and provides stateless, low-level primitives for interacting with Lens Chain.

This section presumes that you are familiar with the client-action architecture of Viem.

Getting Started

1

Install Viem

First, install Viem package:

npm install viem@2

2

Install SDK

Then, install the @lens-chain/sdk package:

npm install @lens-chain/sdk@canary

3

Create a Client

Next, configure your Client by selecting the desired Transport and a Lens Chain Chain.

import { createPublicClient, http } from "viem";import { chains } from "@lens-chain/sdk/viem";
export const publicClient = createPublicClient({  chain: chains.testnet,  transport: http(),});

Actions

Viem enables interaction with the Lens Chain through various actions, including sending transactions, signing messages, reading contract data, managing wallet networks, and custom Lens Chain / ZKSync actions.

Transactions

To send transactions, you can use the sendTransaction or writeContract functions from Viem. The sendTransaction function is suitable for simple gas token transfers ($GRASS), while writeContract is used for executing contract functions that modify the blockchain state.

Sending a Transaction:

import { walletClient } from "./walletClient";
const hash = await walletClient.sendTransaction({  to: "0xRecipientAddress",  value: 1000000000000000000n, // Amount in wei});

Executing a Contract Function:

import { walletClient } from "./walletClient";import { contractAbi } from "./abi";
const hash = await walletClient.writeContract({  address: "0xContractAddress",  abi: contractAbi,  functionName: "functionName",  args: [arg1, arg2],});

Signatures

To sign messages for cryptographic authentication, the signMessage or signTypedData functions can be used.

Sign Message:

import { walletClient } from "./walletClient";
const signature = await walletClient.signMessage({  message: "Hello, Lens Chain!",});

Sign Typed Data:

import { walletClient } from "./walletClient";
const signature = await walletClient.signTypedData({  account: "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266",  domain: {    name: "Ether Mail",    version: "1",    chainId: 1,    verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",  },  types: {    Person: [      { name: "name", type: "string" },      { name: "wallet", type: "address" },    ],    Mail: [      { name: "from", type: "Person" },      { name: "to", type: "Person" },      { name: "contents", type: "string" },    ],  },  primaryType: "Mail",  message: {    from: {      name: "Cow",      wallet: "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826",    },    to: {      name: "Bob",      wallet: "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB",    },    contents: "Hello, Bob!",  },});

Contract Read

To call a smart contract view function, use the readContract function. The multicall function allows batching view function calls into a single request.

Read Contract Function:

import { publicClient } from "./publicClient";import { contractAbi } from "./abi";
const data = await publicClient.readContract({  address: "0xContractAddress",  abi: contractAbi,  functionName: "functionName",  args: [arg1],});

Multicall:

import { publicClient } from "./publicClient";import { contractAbi } from "./abi";
const results = await publicClient.multicall({  contracts: [    {      address: "0xContractAddress1",      abi: contractAbi,      functionName: "functionName1",      args: [arg1],    },    {      address: "0xContractAddress2",      abi: contractAbi,      functionName: "functionName2",      args: [arg2],    },  ],});

Wallet Network

To manage the connected wallet network, the addChain or switchChain functions can be used.

Switch chain to Lens Testnet:

import { chains } from "@lens-chain/sdk/viem";import { walletClient } from "./walletClient";
await walletClient.switchChain({ id: chains.testnet.id });

Add Lens Testnet chain to wallet:

import { chains } from "@lens-chain/sdk/viem";import { walletClient } from "./walletClient";
await walletClient.addChain({ chain: chains.testnet });

Lens Chain Actions

Example
import { sendRawTransactionWithDetailedOutput } from "@lens-chain/sdk/viem";
import { walletClient } from "./walletClient";
const result = await sendRawTransactionWithDetailedOutput(walletClient, {  serializedTransaction: "0x02f8500182031180…",});

ZKSync Actions

Custom RPC Node

If you want to use a Lens Chain RPC node other than the default one, you can specify the custom RPC node URL in the http transport.

import { createPublicClient, http } from "viem";import { chains } from "@lens-chain/sdk/viem";
export const publicClient = createPublicClient({  chain: chains.testnet,  transport: http("https://custom-rpc-node.com"),});