Viem

Viem version of the connect sdk

Install​

npm i @cometh/connect-sdk-viem wagmi viem

Init a Connect Viem Client

After creating (or reconnecting) to a connect wallet, you can now create a connectViemClient:

import {
  ComethWallet,
  ConnectAdaptor,
  SupportedNetworks
} from '@cometh/connect-sdk'
import { getConnectViemClient } from '@cometh/connect-sdk-viem'

const walletAdaptor = new ConnectAdaptor({
  chainId: SupportedNetworks.POLYGON,
  apiKey: API_KEY
})

const wallet = new ComethWallet({
  authAdapter: walletAdaptor,
  apiKey: API_KEY,
  rpcUrl: RPC_URL
})

await wallet.connect()

const connectViemClient = getConnectViemClient(wallet)

Client methods

All Public actions available through a Public viem Client are available. (see viem docs). On top of that, we added 2 custom functions that you can call through the connectViemClient:

  • Send transaction

import { encodeFunctionData } from 'viem'

const txCallData = encodeFunctionData({
  abi: CONTRACT_ABI,
  functionName: CONTRACT_METHOD
})

const tx = { to: DESTINATION, value: VALUE, data: txCallData }
const hash = await connectViemClient.sendTransaction(tx)
  • Send Batch transactions

const txBatch = [
  { to: DESTINATION, value: VALUE, data: txCallData },
  { to: DESTINATION, value: VALUE, data: txCallData }
]
const hash = await connectViemClient.sendTransaction(txBatch)
  • Get Transaction Receipt

const receipt = await connectViemClient.getTransaction(hash)
import { getContract } from 'viem'

const connectViemClient = getConnectViemClient(wallet)

const counterContract = getContract({
    address: COUNTER_CONTRACT_ADDRESS,
    abi: countContractAbi,
    walletClient: connectClient
});

const hash = await counterContract.write.count();

Init a Connect Viem Account

import {
  ComethWallet,
  ConnectAdaptor,
  SupportedNetworks
} from '@cometh/connect-sdk'
import { getConnectViemAccount } from '@cometh/connect-sdk-viem'

const walletAdaptor = new ConnectAdaptor({
  chainId: SupportedNetworks.POLYGON,
  apiKey: API_KEY
})

const wallet = new ComethWallet({
  authAdapter: walletAdaptor,
  apiKey: API_KEY,
  rpcUrl: RPC_URL
})

await wallet.connect()

const connectViemAccount = getConnectViemAccount(wallet)

Account Methods

  • Sign Message

const signature = await connectViemAccount.signMessage({ message: 'hello' })
  • Sign Transaction

const tx = { to: DESTINATION, value: VALUE, data: txCallData }

const signature = await connectViemAccount.signTransaction(tx)

Last updated